From df6010648087ddf65e6026b2dfe838d1f1e709fb Mon Sep 17 00:00:00 2001 From: chaoskagami Date: Thu, 18 Aug 2016 10:38:15 -0400 Subject: [PATCH] Minor refactor --- include/std/fs.h | 5 +++ source/chainloader.c | 74 +++++++++++------------------------- source/menu.c | 90 ++++++++++++++++---------------------------- source/std/fs.c | 19 +++++++--- 4 files changed, 73 insertions(+), 115 deletions(-) diff --git a/include/std/fs.h b/include/std/fs.h index 941fc13..e1b5de4 100644 --- a/include/std/fs.h +++ b/include/std/fs.h @@ -19,8 +19,11 @@ typedef struct #define SEEK_END 2 int fmount(void); + int fumount(void); +void recurse_call(char *name, void (*call_fun)(char*)); + int rrmdir(char *dir_path); FILE *fopen(const char *filename, const char *mode); @@ -36,9 +39,11 @@ int feof(FILE *fp); size_t fsize(FILE *fp); size_t fwrite(const void *buffer, size_t elementSize, size_t elementCnt, FILE *fp); + size_t fread(void *buffer, size_t elementSize, size_t elementCnt, FILE *fp); size_t write_file(void *data, char *path, size_t size); + size_t read_file(void *data, char *path, size_t size); #endif diff --git a/source/chainloader.c b/source/chainloader.c index 3c13598..7cb1d77 100644 --- a/source/chainloader.c +++ b/source/chainloader.c @@ -78,56 +78,27 @@ void chainload_file(char* chain_file_data) ((void(*)(void*, uint32_t))0x24F00000)(chain_data, size + 256 + 8); // Size of payload + argv. } -// This function is based on PathDeleteWorker from GodMode9. -// It was easier to just import it. -int -list_chain_build_back(char *fpath) -{ - FILINFO fno; - DIR pdir; - char *fname = &fpath[strnlen(fpath, 255)]; - if (f_opendir(&pdir, fpath) != FR_OK) - return 1; - - fname[0] = '/'; - fname++; - - while (f_readdir(&pdir, &fno) == FR_OK) { - strncpy(fname, fno.fname, strlen(fno.fname)); - - if (fno.fname[0] == 0) - break; - - FILINFO f2; - if (f_stat(fpath, &f2) != FR_OK) - break; - - if (f2.fattrib & AM_DIR) { - // return value won't matter - list_chain_build_back(fpath); - } else { - char* basename = &fpath[strlen(fpath) - 1]; - while(basename[0] != '/') basename--; - basename++; - - strncpy(chains[current_chain_index].name, basename, 64); - strncpy(chains[current_chain_index].desc, fpath, 255); - - chains[current_chain_index].index = 0; - chains[current_chain_index].allowed = call_fun; - chains[current_chain_index].a = (uint32_t) chainload_file; - chains[current_chain_index].b = (uint32_t) chains[current_chain_index].desc; - chains[current_chain_index].indent = 0; - - current_chain_index++; - } +void chain_file_hdl(char* fpath) { + FILINFO f2; + if (f_stat(fpath, &f2) != FR_OK) + return; + + if (!(f2.fattrib & AM_DIR)) { + char* basename = &fpath[strlen(fpath) - 1]; + while(basename[0] != '/') basename--; + basename++; + + strncpy(chains[current_chain_index].name, basename, 64); + strncpy(chains[current_chain_index].desc, fpath, 255); + + chains[current_chain_index].index = 0; + chains[current_chain_index].allowed = call_fun; + chains[current_chain_index].a = (uint32_t) chainload_file; + chains[current_chain_index].b = (uint32_t) chains[current_chain_index].desc; + chains[current_chain_index].indent = 0; + + current_chain_index++; } - - f_closedir(&pdir); - --fname; - fname[0] = 0; - - return 0; } // This is dual purpose. When we actually list @@ -149,9 +120,8 @@ list_chain_build(char *name) current_chain_index += 1; - char fpath[256]; - strncpy(fpath, name, 256); - list_chain_build_back(fpath); + recurse_call(name, chain_file_hdl); + chains[current_chain_index].index = -1; if (chains[1].index == -1) diff --git a/source/menu.c b/source/menu.c index 966d55b..1e19a8a 100644 --- a/source/menu.c +++ b/source/menu.c @@ -62,62 +62,35 @@ static struct options_s options[] = { static int current_menu_index_patches = 0; -// This function is based on PathDeleteWorker from GodMode9. -// It was easier to just import it. -int -list_patches_build_back(char *fpath, int desc_is_path) -{ - FILINFO fno; - DIR pdir; - char *fname = &fpath[strnlen(fpath, 255)]; - if (f_opendir(&pdir, fpath) != FR_OK) - return 1; - - fname[0] = '/'; - fname++; - - while (f_readdir(&pdir, &fno) == FR_OK) { - strncpy(fname, fno.fname, strlen(fno.fname)); - - if (fno.fname[0] == 0) - break; - - FILINFO f2; - if (f_stat(fpath, &f2) != FR_OK) - break; - - if (f2.fattrib & AM_DIR) { - // return value won't matter - list_patches_build_back(fpath, desc_is_path); - } else { - struct system_patch p; - read_file(&p, fpath, sizeof(struct system_patch)); - - if (memcmp(p.magic, "AIDA", 4)) - return 0; - - strncpy(patches[current_menu_index_patches].name, p.name, 64); - if (desc_is_path) - strncpy(patches[current_menu_index_patches].desc, fpath, 255); - else - strncpy(patches[current_menu_index_patches].desc, p.desc, 255); - patches[current_menu_index_patches].index = p.uuid; - patches[current_menu_index_patches].allowed = boolean_val; - patches[current_menu_index_patches].a = 0; - patches[current_menu_index_patches].b = 0; - patches[current_menu_index_patches].indent = 0; - if (desc_is_path) - enable_list[p.uuid] = 0; - - current_menu_index_patches++; - } +static int desc_is_fname_sto = 0; + +void patch_func(char* fpath) { + FILINFO f2; + if (f_stat(fpath, &f2) != FR_OK) + return; + + if (!(f2.fattrib & AM_DIR)) { + struct system_patch p; + read_file(&p, fpath, sizeof(struct system_patch)); + + if (memcmp(p.magic, "AIDA", 4)) + return; + + strncpy(patches[current_menu_index_patches].name, p.name, 64); + if (desc_is_fname_sto) + strncpy(patches[current_menu_index_patches].desc, fpath, 255); + else + strncpy(patches[current_menu_index_patches].desc, p.desc, 255); + patches[current_menu_index_patches].index = p.uuid; + patches[current_menu_index_patches].allowed = boolean_val; + patches[current_menu_index_patches].a = 0; + patches[current_menu_index_patches].b = 0; + patches[current_menu_index_patches].indent = 0; + if (desc_is_fname_sto) + enable_list[p.uuid] = 0; + + current_menu_index_patches++; } - - f_closedir(&pdir); - --fname; - fname[0] = 0; - - return 0; } // This is dual purpose. When we actually list @@ -127,6 +100,8 @@ list_patches_build_back(char *fpath, int desc_is_path) void list_patches_build(char *name, int desc_is_fname) { + desc_is_fname_sto = desc_is_fname; + current_menu_index_patches = 0; if (!enable_list) @@ -149,9 +124,8 @@ list_patches_build(char *name, int desc_is_fname) current_menu_index_patches += 1; } - char fpath[256]; - strncpy(fpath, name, 256); - list_patches_build_back(fpath, desc_is_fname); + recurse_call(name, patch_func); + patches[current_menu_index_patches].index = -1; FILE *f; diff --git a/source/std/fs.c b/source/std/fs.c index aa654d7..d4204f8 100644 --- a/source/std/fs.c +++ b/source/std/fs.c @@ -12,7 +12,7 @@ static int set_up_fs = 0; // This function is based on PathDeleteWorker from GodMode9. // It was easier to just import it. int -rrmdir_back(char *fpath) +recurse_call_back(char *fpath, void (*call_fun)(char*)) { FILINFO fno; DIR pdir; @@ -35,9 +35,9 @@ rrmdir_back(char *fpath) if (f2.fattrib & AM_DIR) { // return value won't matter - rrmdir_back(fpath); + recurse_call_back(fpath, call_fun); } else { - f_unlink(fpath); + call_fun(fpath); } } @@ -45,7 +45,15 @@ rrmdir_back(char *fpath) --fname; fname[0] = 0; - return f_unlink(fpath); + call_fun(fpath); + + return 0; +} + +void recurse_call(char *name, void (*call_fun)(char*)) { + char fpath[256]; + strncpy(fpath, name, 256); + recurse_call_back(fpath, call_fun); } int @@ -53,7 +61,8 @@ rrmdir(char *name) { char fpath[256]; strncpy(fpath, name, 256); - return rrmdir_back(fpath); + recurse_call(fpath, f_unlink); + return 0; } int -- 2.39.5