From 8244ebb50b9c9891c3c6c6d9688566f0955b5f4f Mon Sep 17 00:00:00 2001 From: rexy712 Date: Thu, 6 Dec 2018 12:59:25 -0800 Subject: [PATCH] moved restore file to user's home directory --- include/globals.h | 2 +- include/restore.h | 7 ++---- src/restore.c | 58 +++++++++++++++++++++++++++++++++++++++------- src/rexbacklight.c | 11 +++++---- 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/include/globals.h b/include/globals.h index f06c1dc..bc38dd0 100644 --- a/include/globals.h +++ b/include/globals.h @@ -21,7 +21,7 @@ const char* device_dir(void); const char* executable_name(void); -const char* restore_file(void); +const char* restore_file_suffix(void); const char* brightness_file(void); const char* max_brightness_file(void); diff --git a/include/restore.h b/include/restore.h index 630f8d8..edfa464 100644 --- a/include/restore.h +++ b/include/restore.h @@ -25,12 +25,9 @@ #include "cmd.h" void save_restore_file(struct string_array* devices); -void process_restore_file(void); -int prep_restore(struct arg_values* curr, RJP_value** root, int* try_restore); +void all_restore(void); +int individual_restore(struct arg_values* curr, RJP_value** root, int* try_restore); RJP_value* find_matching_json_device(const char* name, RJP_value* root); RJP_value* read_restore_file(const char* file); -char* get_home_folder(); -char* get_home_restore_file(); - #endif diff --git a/src/restore.c b/src/restore.c index f049987..37a6bf4 100644 --- a/src/restore.c +++ b/src/restore.c @@ -17,11 +17,11 @@ */ #include -#include //stat, mkdir -#include //stat, mkdir -#include //stat, mkdir #include //strcmp #include +#include //struct passwd +#include //getpwnam +#include //chdir #include "restore.h" #include "common.h" @@ -29,6 +29,36 @@ #include "globals.h" #include "rexbacklight.h" +static char* get_home_folder(void){ + char* user = getenv("SUDO_USER"); + char* home = getenv("HOME"); + if(!home || user){ + if(!user){ + return NULL; + } + struct passwd* pw_entry = getpwnam(user); + if(!pw_entry){ + return NULL; + } + home = pw_entry->pw_dir; + } + return home; +} +static char* restore_file(void){ + char* home_folder = get_home_folder(); + if(!home_folder) + return NULL; + + //"/.config/rexbacklight.json" + size_t home_folder_len = strlen(home_folder); + size_t conf_len = strlen(restore_file_suffix()); + char* rest_file = malloc(home_folder_len + conf_len + 1); + strncpy(rest_file, home_folder, home_folder_len); + strncpy(rest_file+home_folder_len, restore_file_suffix(), conf_len); + rest_file[home_folder_len+conf_len] = 0; + return rest_file; +} + RJP_value* read_restore_file(const char* file){ size_t filesize; char* file_contents; @@ -60,9 +90,13 @@ RJP_value* find_matching_json_device(const char* name, RJP_value* root){ } return NULL; } -int prep_restore(struct arg_values* curr, RJP_value** root, int* try_restore){ + +//run restore on individual device +int individual_restore(struct arg_values* curr, RJP_value** root, int* try_restore){ if(!*root && *try_restore){ - *root = read_restore_file(restore_file()); + char* tmp = restore_file(); + *root = read_restore_file(tmp); + free(tmp); if(!*root){ *try_restore = 0; return 0; @@ -77,8 +111,12 @@ int prep_restore(struct arg_values* curr, RJP_value** root, int* try_restore){ curr->delta = rjp_value_dfloat(match); return 1; } -void process_restore_file(void){ - RJP_value* root = read_restore_file(restore_file()); + +//run restore on all devices +void all_restore(void){ + char* rfil = restore_file(); + RJP_value* root = read_restore_file(rfil); + free(rfil); if(!root) return; struct arg_values tmp = {.next = NULL, @@ -121,13 +159,15 @@ void save_restore_file(struct string_array* devices){ } } char* tmp = rjp_to_json(root); - FILE* restf = fopen(restore_file(), "w"); + char* rfil = restore_file(); + FILE* restf = fopen(rfil, "w"); if(!restf){ - io_error(IO_ERROR_OPEN, IO_ERROR_FILE, restore_file()); + io_error(IO_ERROR_OPEN, IO_ERROR_FILE, rfil); }else{ fprintf(restf, "//File generated by %s\n%s\n", executable_name(), tmp); fclose(restf); } + free(rfil); rjp_free(tmp); rjp_free_value(root); } diff --git a/src/rexbacklight.c b/src/rexbacklight.c index 685874d..76f0e78 100644 --- a/src/rexbacklight.c +++ b/src/rexbacklight.c @@ -39,12 +39,12 @@ //This is where backlight devices can be found in sysfs const char* device_dir(void){return "/sys/class/backlight/";} const char* executable_name(void){return "rexbacklight";} -const char* restore_file(void){return "/var/tmp/rexbacklight.json";} +const char* restore_file_suffix(void){return "/.config/rexbacklight.json";} #elif defined(REXLEDCTL) //This is where led devices can be found in sysfs const char* device_dir(void){return "/sys/class/leds/";} const char* executable_name(void){return "rexledctl";} -const char* restore_file(void){return "/var/tmp/rexledctl.json";} +const char* restore_file_suffix(void){return "/.config/rexledctl.json";} #else #error "UNDEFINED PROGRAM NAME!" #endif @@ -247,7 +247,7 @@ void individual_device_loop(struct arg_values* a){ int try_restore = 1; for(curr = a->next;curr;curr = curr->next){ if(curr->operation == OP_RESTORE){ - if(!prep_restore(curr, &root, &try_restore)) + if(!individual_restore(curr, &root, &try_restore)) continue; } return_value = individual_device_op(curr); @@ -280,6 +280,7 @@ void all_device_loop(struct string_array* device_names, struct arg_values* args) return; } } + save_restore_file(device_names); break; case OP_LIST: do_list(device_names); @@ -300,7 +301,7 @@ void all_device_loop(struct string_array* device_names, struct arg_values* args) } break; case OP_RESTORE:; - process_restore_file(); + all_restore(); break; default: fprintf(stderr, "Internal processing error!\n"); @@ -364,10 +365,10 @@ int main(int argc, char** argv){ //Run selected operation if(args.next){ individual_device_loop(&args); + save_restore_file(&device_names); }else{ all_device_loop(&device_names, &args); } - save_restore_file(&device_names); CLEANUP(); return return_value;