moved restore file to user's home directory
This commit is contained in:
parent
abb03dcd40
commit
8244ebb50b
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
const char* device_dir(void);
|
const char* device_dir(void);
|
||||||
const char* executable_name(void);
|
const char* executable_name(void);
|
||||||
const char* restore_file(void);
|
const char* restore_file_suffix(void);
|
||||||
const char* brightness_file(void);
|
const char* brightness_file(void);
|
||||||
const char* max_brightness_file(void);
|
const char* max_brightness_file(void);
|
||||||
|
|
||||||
|
|||||||
@ -25,12 +25,9 @@
|
|||||||
#include "cmd.h"
|
#include "cmd.h"
|
||||||
|
|
||||||
void save_restore_file(struct string_array* devices);
|
void save_restore_file(struct string_array* devices);
|
||||||
void process_restore_file(void);
|
void all_restore(void);
|
||||||
int prep_restore(struct arg_values* curr, RJP_value** root, int* try_restore);
|
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* find_matching_json_device(const char* name, RJP_value* root);
|
||||||
RJP_value* read_restore_file(const char* file);
|
RJP_value* read_restore_file(const char* file);
|
||||||
char* get_home_folder();
|
|
||||||
char* get_home_restore_file();
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -17,11 +17,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h> //stat, mkdir
|
|
||||||
#include <sys/stat.h> //stat, mkdir
|
|
||||||
#include <unistd.h> //stat, mkdir
|
|
||||||
#include <string.h> //strcmp
|
#include <string.h> //strcmp
|
||||||
#include <rjp.h>
|
#include <rjp.h>
|
||||||
|
#include <sys/types.h> //struct passwd
|
||||||
|
#include <pwd.h> //getpwnam
|
||||||
|
#include <unistd.h> //chdir
|
||||||
|
|
||||||
#include "restore.h"
|
#include "restore.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
@ -29,6 +29,36 @@
|
|||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "rexbacklight.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){
|
RJP_value* read_restore_file(const char* file){
|
||||||
size_t filesize;
|
size_t filesize;
|
||||||
char* file_contents;
|
char* file_contents;
|
||||||
@ -60,9 +90,13 @@ RJP_value* find_matching_json_device(const char* name, RJP_value* root){
|
|||||||
}
|
}
|
||||||
return NULL;
|
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){
|
if(!*root && *try_restore){
|
||||||
*root = read_restore_file(restore_file());
|
char* tmp = restore_file();
|
||||||
|
*root = read_restore_file(tmp);
|
||||||
|
free(tmp);
|
||||||
if(!*root){
|
if(!*root){
|
||||||
*try_restore = 0;
|
*try_restore = 0;
|
||||||
return 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);
|
curr->delta = rjp_value_dfloat(match);
|
||||||
return 1;
|
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)
|
if(!root)
|
||||||
return;
|
return;
|
||||||
struct arg_values tmp = {.next = NULL,
|
struct arg_values tmp = {.next = NULL,
|
||||||
@ -121,13 +159,15 @@ void save_restore_file(struct string_array* devices){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
char* tmp = rjp_to_json(root);
|
char* tmp = rjp_to_json(root);
|
||||||
FILE* restf = fopen(restore_file(), "w");
|
char* rfil = restore_file();
|
||||||
|
FILE* restf = fopen(rfil, "w");
|
||||||
if(!restf){
|
if(!restf){
|
||||||
io_error(IO_ERROR_OPEN, IO_ERROR_FILE, restore_file());
|
io_error(IO_ERROR_OPEN, IO_ERROR_FILE, rfil);
|
||||||
}else{
|
}else{
|
||||||
fprintf(restf, "//File generated by %s\n%s\n", executable_name(), tmp);
|
fprintf(restf, "//File generated by %s\n%s\n", executable_name(), tmp);
|
||||||
fclose(restf);
|
fclose(restf);
|
||||||
}
|
}
|
||||||
|
free(rfil);
|
||||||
rjp_free(tmp);
|
rjp_free(tmp);
|
||||||
rjp_free_value(root);
|
rjp_free_value(root);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,12 +39,12 @@
|
|||||||
//This is where backlight devices can be found in sysfs
|
//This is where backlight devices can be found in sysfs
|
||||||
const char* device_dir(void){return "/sys/class/backlight/";}
|
const char* device_dir(void){return "/sys/class/backlight/";}
|
||||||
const char* executable_name(void){return "rexbacklight";}
|
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)
|
#elif defined(REXLEDCTL)
|
||||||
//This is where led devices can be found in sysfs
|
//This is where led devices can be found in sysfs
|
||||||
const char* device_dir(void){return "/sys/class/leds/";}
|
const char* device_dir(void){return "/sys/class/leds/";}
|
||||||
const char* executable_name(void){return "rexledctl";}
|
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
|
#else
|
||||||
#error "UNDEFINED PROGRAM NAME!"
|
#error "UNDEFINED PROGRAM NAME!"
|
||||||
#endif
|
#endif
|
||||||
@ -247,7 +247,7 @@ void individual_device_loop(struct arg_values* a){
|
|||||||
int try_restore = 1;
|
int try_restore = 1;
|
||||||
for(curr = a->next;curr;curr = curr->next){
|
for(curr = a->next;curr;curr = curr->next){
|
||||||
if(curr->operation == OP_RESTORE){
|
if(curr->operation == OP_RESTORE){
|
||||||
if(!prep_restore(curr, &root, &try_restore))
|
if(!individual_restore(curr, &root, &try_restore))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return_value = individual_device_op(curr);
|
return_value = individual_device_op(curr);
|
||||||
@ -280,6 +280,7 @@ void all_device_loop(struct string_array* device_names, struct arg_values* args)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
save_restore_file(device_names);
|
||||||
break;
|
break;
|
||||||
case OP_LIST:
|
case OP_LIST:
|
||||||
do_list(device_names);
|
do_list(device_names);
|
||||||
@ -300,7 +301,7 @@ void all_device_loop(struct string_array* device_names, struct arg_values* args)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case OP_RESTORE:;
|
case OP_RESTORE:;
|
||||||
process_restore_file();
|
all_restore();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Internal processing error!\n");
|
fprintf(stderr, "Internal processing error!\n");
|
||||||
@ -364,10 +365,10 @@ int main(int argc, char** argv){
|
|||||||
//Run selected operation
|
//Run selected operation
|
||||||
if(args.next){
|
if(args.next){
|
||||||
individual_device_loop(&args);
|
individual_device_loop(&args);
|
||||||
|
save_restore_file(&device_names);
|
||||||
}else{
|
}else{
|
||||||
all_device_loop(&device_names, &args);
|
all_device_loop(&device_names, &args);
|
||||||
}
|
}
|
||||||
save_restore_file(&device_names);
|
|
||||||
|
|
||||||
CLEANUP();
|
CLEANUP();
|
||||||
return return_value;
|
return return_value;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user