]> Chaos Git - corbenik/corbenik.git/commitdiff
Minor refactor
authorchaoskagami <chaos.kagami@gmail.com>
Thu, 18 Aug 2016 14:38:15 +0000 (10:38 -0400)
committerchaoskagami <chaos.kagami@gmail.com>
Thu, 18 Aug 2016 14:38:15 +0000 (10:38 -0400)
include/std/fs.h
source/chainloader.c
source/menu.c
source/std/fs.c

index 941fc1332fc3268229f5c76f6bcbf43a5e2b9ed2..e1b5de483394c7903cd3d2bfa376fee42d7ee8b9 100644 (file)
@@ -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
index 3c13598e6751b50725486de9b92e59f4411bd4ae..7cb1d77018be847102cb90ec27ef0eb3662b0840 100644 (file)
@@ -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)
index 966d55b3f315a7374c2239088768d8c6f71707f8..1e19a8adeaef5cf6c07da2331a43db6274899675 100644 (file)
@@ -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;
index aa654d726f1ae94e88312e1fefea7bdba4718eea..d4204f8345bde823eada2aa9fa655ad0b3288e9e 100644 (file)
@@ -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