From b575fac6ad523d0ee520d12b7e9a135cf611091a Mon Sep 17 00:00:00 2001 From: d0k3 Date: Tue, 28 Feb 2017 16:46:24 +0100 Subject: [PATCH] Misc code reorganization & cleanup --- source/fs/fsperm.c | 1 - source/fs/fsutil.c | 168 ++++++++++++++++++++++----------------------- source/fs/fsutil.h | 18 ++--- source/godmode.c | 2 +- 4 files changed, 94 insertions(+), 95 deletions(-) diff --git a/source/fs/fsperm.c b/source/fs/fsperm.c index 5227f0a..4e1d551 100644 --- a/source/fs/fsperm.c +++ b/source/fs/fsperm.c @@ -11,7 +11,6 @@ #define PATH_SYS_LVL3 "S:/firm0.bin", "S:/firm1.bin", "S:/nand.bin", "S:/nand_minsize.bin", "S:/nand_hdr.bin", \ "S:/sector0x96.bin", "S:/twlmbr.bin" #define PATH_EMU_LVL1 "E:/ctrnand_fat.bin", "E:/ctrnand_full.bin", "E:/nand.bin", "E:/nand_minsize.bin", "E:/nand_hdr.bin" -#define PATH_PERM_CHK "E:", "S:", "1:/rw/sys", "1:/private" // write permissions - careful with this static u32 write_permissions = PERM_BASE; diff --git a/source/fs/fsutil.c b/source/fs/fsutil.c index 0486b53..a17606b 100644 --- a/source/fs/fsutil.c +++ b/source/fs/fsutil.c @@ -259,6 +259,88 @@ bool FileInjectFile(const char* dest, const char* orig, u32 offset) { return ret; } +bool DirBuilderWorker(char* dest) { + DIR tmp_dir; + if (fa_opendir(&tmp_dir, dest) != FR_OK) { + char* slash = strrchr(dest, '/'); + if (!slash) return false; + *slash = '\0'; + if (!DirBuilderWorker(dest)) return false; + *slash = '/'; + return (fa_mkdir(dest) == FR_OK); + } else { + f_closedir(&tmp_dir); + return true; + } +} + +bool DirBuilder(const char* destdir) { + char fdpath[256]; // 256 is the maximum length of a full path + strncpy(fdpath, destdir, 255); + return DirBuilderWorker(fdpath); +} + +bool DirCreate(const char* cpath, const char* dirname) { + char npath[256]; // 256 is the maximum length of a full path + if (!CheckWritePermissions(cpath)) return false; + snprintf(npath, 255, "%s/%s", cpath, dirname); + return (fa_mkdir(npath) == FR_OK); +} + +bool DirInfoWorker(char* fpath, bool virtual, u64* tsize, u32* tdirs, u32* tfiles) { + char* fname = fpath + strnlen(fpath, 256 - 1); + bool ret = true; + if (virtual) { + VirtualDir vdir; + VirtualFile vfile; + if (!GetVirtualDir(&vdir, fpath)) return false; // get dir reader object + while (ReadVirtualDir(&vfile, &vdir)) { + if (vfile.flags & VFLAG_DIR) { + (*tdirs)++; + *(fname++) = '/'; + GetVirtualFilename(fname, &vfile, (256 - 1) - (fname - fpath)); + if (!DirInfoWorker(fpath, virtual, tsize, tdirs, tfiles)) ret = false; + *(--fname) = '\0'; + } else { + *tsize += vfile.size; + (*tfiles)++; + } + } + } else { + DIR pdir; + FILINFO fno; + if (fa_opendir(&pdir, fpath) != FR_OK) return false; // get dir reader object + while (f_readdir(&pdir, &fno) == FR_OK) { + if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0)) + continue; // filter out virtual entries + if (fno.fname[0] == 0) break; // end of dir + if (fno.fattrib & AM_DIR) { + (*tdirs)++; + *(fname++) = '/'; + strncpy(fname, fno.fname, (256 - 1) - (fname - fpath)); + if (!DirInfoWorker(fpath, virtual, tsize, tdirs, tfiles)) ret = false; + *(--fname) = '\0'; + } else { + *tsize += fno.fsize; + (*tfiles)++; + } + } + f_closedir(&pdir); + } + + return ret; +} + +bool DirInfo(const char* path, u64* tsize, u32* tdirs, u32* tfiles) { + bool virtual = (DriveType(path) & DRV_VIRTUAL); + char fpath[256]; + strncpy(fpath, path, 255); + *tsize = *tdirs = *tfiles = 0; + ShowString("Analyzing dir, please wait..."); + bool res = DirInfoWorker(fpath, virtual, tsize, tdirs, tfiles); + return res; +} + bool PathCopyVrtToVrt(const char* destdir, const char* orig, u32* flags) { VirtualFile dvfile; VirtualFile ovfile; @@ -686,11 +768,11 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) { int ddrvtype = DriveType(destdir); int odrvtype = DriveType(orig); if (!(ddrvtype & DRV_VIRTUAL)) { // FAT / virtual to FAT - if (flags && (*flags & BUILD_PATH)) DirBuilder(destdir); char fdpath[256]; // 256 is the maximum length of a full path char fopath[256]; strncpy(fdpath, destdir, 255); strncpy(fopath, orig, 255); + if (flags && (*flags & BUILD_PATH)) DirBuilderWorker(fdpath); bool res = (odrvtype & DRV_VIRTUAL) ? PathCopyVrtToFat(fdpath, fopath, flags) : PathCopyWorker(fdpath, fopath, flags, false); return res; @@ -714,11 +796,11 @@ bool PathMove(const char* destdir, const char* orig, u32* flags) { ShowPrompt(false, "Error: Moving is not possible here"); return false; } else { - if (flags && (*flags & BUILD_PATH)) DirBuilder(destdir); char fdpath[256]; // 256 is the maximum length of a full path char fopath[256]; strncpy(fdpath, destdir, 255); strncpy(fopath, orig, 255); + if (flags && (*flags & BUILD_PATH)) DirBuilderWorker(fdpath); bool same_drv = (strncmp(orig, destdir, 2) == 0); bool res = PathCopyWorker(fdpath, fopath, flags, same_drv); if (res && (!flags || !(*flags&SKIP_CUR))) PathDelete(orig); @@ -777,88 +859,6 @@ bool PathRename(const char* path, const char* newname) { return (f_rename(path, npath) == FR_OK); } -bool DirBuilderWorker(char* dest) { - DIR tmp_dir; - if (fa_opendir(&tmp_dir, dest) != FR_OK) { - char* slash = strrchr(dest, '/'); - if (!slash) return false; - *slash = '\0'; - if (!DirBuilderWorker(dest)) return false; - *slash = '/'; - return (fa_mkdir(dest) == FR_OK); - } else { - f_closedir(&tmp_dir); - return true; - } -} - -bool DirBuilder(const char* destdir) { - char fdpath[256]; // 256 is the maximum length of a full path - strncpy(fdpath, destdir, 255); - return DirBuilderWorker(destdir); -} - -bool DirCreate(const char* cpath, const char* dirname) { - char npath[256]; // 256 is the maximum length of a full path - if (!CheckWritePermissions(cpath)) return false; - snprintf(npath, 255, "%s/%s", cpath, dirname); - return (fa_mkdir(npath) == FR_OK); -} - -bool DirInfoWorker(char* fpath, bool virtual, u64* tsize, u32* tdirs, u32* tfiles) { - char* fname = fpath + strnlen(fpath, 256 - 1); - bool ret = true; - if (virtual) { - VirtualDir vdir; - VirtualFile vfile; - if (!GetVirtualDir(&vdir, fpath)) return false; // get dir reader object - while (ReadVirtualDir(&vfile, &vdir)) { - if (vfile.flags & VFLAG_DIR) { - (*tdirs)++; - *(fname++) = '/'; - GetVirtualFilename(fname, &vfile, (256 - 1) - (fname - fpath)); - if (!DirInfoWorker(fpath, virtual, tsize, tdirs, tfiles)) ret = false; - *(--fname) = '\0'; - } else { - *tsize += vfile.size; - (*tfiles)++; - } - } - } else { - DIR pdir; - FILINFO fno; - if (fa_opendir(&pdir, fpath) != FR_OK) return false; // get dir reader object - while (f_readdir(&pdir, &fno) == FR_OK) { - if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0)) - continue; // filter out virtual entries - if (fno.fname[0] == 0) break; // end of dir - if (fno.fattrib & AM_DIR) { - (*tdirs)++; - *(fname++) = '/'; - strncpy(fname, fno.fname, (256 - 1) - (fname - fpath)); - if (!DirInfoWorker(fpath, virtual, tsize, tdirs, tfiles)) ret = false; - *(--fname) = '\0'; - } else { - *tsize += fno.fsize; - (*tfiles)++; - } - } - f_closedir(&pdir); - } - - return ret; -} - -bool DirInfo(const char* path, u64* tsize, u32* tdirs, u32* tfiles) { - bool virtual = (DriveType(path) & DRV_VIRTUAL); - char fpath[256]; - strncpy(fpath, path, 255); - *tsize = *tdirs = *tfiles = 0; - ShowString("Analyzing dir, please wait..."); - bool res = DirInfoWorker(fpath, virtual, tsize, tdirs, tfiles); - return res; -} - void CreateScreenshot() { const u8 bmp_header[54] = { 0x42, 0x4D, 0x36, 0xCA, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, diff --git a/source/fs/fsutil.h b/source/fs/fsutil.h index 687cd3b..88c726d 100644 --- a/source/fs/fsutil.h +++ b/source/fs/fsutil.h @@ -44,6 +44,15 @@ u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file); /** Inject file into file @offset **/ bool FileInjectFile(const char* dest, const char* orig, u32 offset); +/** Recursively build a directory **/ +bool DirBuilder(const char* destdir); + +/** Create a new directory in cpath **/ +bool DirCreate(const char* cpath, const char* dirname); + +/** Get # of files, subdirs and total size for directory **/ +bool DirInfo(const char* path, u64* tsize, u32* tdirs, u32* tfiles); + /** Recursively copy a file or directory **/ bool PathCopy(const char* destdir, const char* orig, u32* flags); @@ -56,14 +65,5 @@ bool PathDelete(const char* path); /** Rename file / folder in path to new name **/ bool PathRename(const char* path, const char* newname); -/** Recursively build a directory **/ -bool DirBuilder(const char* destdir); - -/** Create a new directory in cpath **/ -bool DirCreate(const char* cpath, const char* dirname); - -/** Get # of files, subdirs and total size for directory **/ -bool DirInfo(const char* path, u64* tsize, u32* tdirs, u32* tfiles); - /** Create a screenshot of the current framebuffer **/ void CreateScreenshot(); diff --git a/source/godmode.c b/source/godmode.c index 1d1a5c7..840fc16 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -1096,7 +1096,7 @@ u32 HomeMoreMenu(char* current_path, DirStruct* current_dir, DirStruct* clipboar int sys = (DriveType("1:")) ? (int) ++n_opt : -1; int emu = (DriveType("4:")) ? (int) ++n_opt : -1; if (sys > 0) optionstr[sys - 1] = "Backup SysNAND"; - if (emu > 0) optionstr[emu - 1] = "BAckup EmuNAND"; + if (emu > 0) optionstr[emu - 1] = "Backup EmuNAND"; user_select = (n_opt > 1) ? ShowSelectPrompt(n_opt, optionstr, promptstr) : n_opt; if (user_select > 0) { u32 flags = CALC_SHA;