From 95ce901489df683e9d1a4e6e210d3e2f97931063 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 22 Feb 2025 16:17:31 +0100 Subject: Remove local share gpu screen recorder directory before install --- main.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index 907333c..2114ca1 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -72,9 +73,16 @@ static bool create_local_kms_server_proxy_directory(const char *home) { for(size_t i = 0; paths[i]; ++i) { const char *path_part = paths[i]; snprintf(path, sizeof(path), "%s/%s", home, path_part); - err = mkdir(path, S_IRWXU); - if(err == -1 && errno != EEXIST) - return false; + err = mkdir(path, 0755); + if(err == -1) { + if(errno == EEXIST) { + err = chmod(path, 0755); + if(err == -1) + return false; + } else { + return false; + } + } } return true; @@ -150,8 +158,43 @@ static bool set_gsr_files_set_permissions_and_capabilities(const char *kms_serve return true; } +/* This works even if the files require root access to modify, since we are just unlinking, not modifying them */ +static bool remove_directory_recursive(const char *filepath) { + DIR *dir = opendir(filepath); + if(!dir) + return false; + + char file[PATH_MAX]; + struct dirent *entry; + while((entry = readdir(dir)) != NULL) { + if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + continue; + + snprintf(file, sizeof(file), "%s/%s", filepath, entry->d_name); + struct stat st; + if(stat(file, &st) == -1) + continue; + + if(S_ISDIR(st.st_mode)) { + if(!remove_directory_recursive(file)) + return false; + } + + if(remove(file) != 0) + return false; + } + + return true; +} + +static bool remove_local_gsr_files(const char *user_homepath) { + char gsr_dir_local_filepath[PATH_MAX]; + snprintf(gsr_dir_local_filepath, sizeof(gsr_dir_local_filepath), "%s/.local/share/gpu-screen-recorder", user_homepath); + return remove_directory_recursive(gsr_dir_local_filepath); +} + /* |gsr_global_hotkeys_local_filepath| can be NULL */ -static bool setup_local_files(const char *user_homepath, const char *kms_server_proxy_local_filepath, const char *gsr_kms_server_local_filepath, const char *gsr_global_hotkeys_local_filepath) { +static bool setup_local_gsr_files(const char *user_homepath, const char *kms_server_proxy_local_filepath, const char *gsr_kms_server_local_filepath, const char *gsr_global_hotkeys_local_filepath) { if(!create_local_kms_server_proxy_directory(user_homepath)) { fprintf(stderr, "Error: failed to create ~/.local/share/gpu-screen-recorder directory\n"); return false; @@ -206,7 +249,8 @@ static int setup_gsr_ui(const char *user_homepath) { return 0; } else { - if(!setup_local_files(user_homepath, kms_server_proxy_local_filepath, gsr_kms_server_local_filepath, gsr_global_hotkeys_local_filepath)) + remove_local_gsr_files(user_homepath); + if(!setup_local_gsr_files(user_homepath, kms_server_proxy_local_filepath, gsr_kms_server_local_filepath, gsr_global_hotkeys_local_filepath)) return 1; const char *args[] = { "pkexec", kms_server_proxy_local_filepath, "setup-gsr-ui", user_homepath, NULL }; @@ -254,7 +298,8 @@ static int launch_gsr_kms_server(const char *initial_socket_path, const char *ca const char *args[] = { gsr_kms_server_local_filepath, initial_socket_path, card_path, NULL }; return execv(args[0], (char *const*)args); } else { - if(!setup_local_files(user_homepath, kms_server_proxy_local_filepath, gsr_kms_server_local_filepath, NULL)) + remove_local_gsr_files(user_homepath); + if(!setup_local_gsr_files(user_homepath, kms_server_proxy_local_filepath, gsr_kms_server_local_filepath, NULL)) return 1; const char *args[] = { "pkexec", kms_server_proxy_local_filepath, initial_socket_path, card_path, user_homepath, NULL }; @@ -305,9 +350,9 @@ static int launch_gsr_global_hotkeys(char **argv) { } argv[2] = gsr_global_hotkeys_local_filepath; - int d = execv(argv[2], argv + 2); + const int result = execv(argv[2], argv + 2); perror(argv[2]); - return d; + return result; } int main(int argc, char **argv) { -- cgit v1.2.3-70-g09d2