From 0d521004a118e90468c826ef924fcee87f326bc1 Mon Sep 17 00:00:00 2001 From: d0k3 Date: Tue, 22 Mar 2016 19:44:21 +0100 Subject: [PATCH] Improved FindVirtualFile() function --- source/fs.c | 24 +++++++++++++++++------- source/nand/virtual.c | 10 ++++++---- source/nand/virtual.h | 2 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/source/fs.c b/source/fs.c index 2df3a59..f5d44fd 100644 --- a/source/fs.c +++ b/source/fs.c @@ -156,7 +156,7 @@ bool PathCopyVirtual(const char* destdir, const char* orig) { if (oname == NULL) return false; // not a proper origin path oname++; - snprintf(dest, 256, "%s/%s", destdir, oname); + snprintf(dest, 255, "%s/%s", destdir, oname); TruncateString(deststr, dest, 36, 8); TruncateString(origstr, orig, 36, 8); @@ -166,9 +166,9 @@ bool PathCopyVirtual(const char* destdir, const char* orig) { VirtualFile ovfile; u32 osize; - if (!FindVirtualFile(&dvfile, dest)) + if (!FindVirtualFile(&dvfile, dest, 0)) return false; - if (!FindVirtualFile(&ovfile, orig)) + if (!FindVirtualFile(&ovfile, orig, 0)) return false; osize = ovfile.size; if (dvfile.size != osize) { // almost impossible, but so what... @@ -196,13 +196,23 @@ bool PathCopyVirtual(const char* destdir, const char* orig) { FIL ofile; u32 osize; - if (!FindVirtualFile(&dvfile, dest)) - return false; if (f_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK) return false; f_lseek(&ofile, 0); f_sync(&ofile); osize = f_size(&ofile); + if (!FindVirtualFile(&dvfile, dest, 0)) { + if (!FindVirtualFile(&dvfile, dest, osize)) { + f_close(&ofile); + return false; + } + snprintf(dest, 255, "%s/%s", destdir, dvfile.name); + if (!ShowPrompt(true, "Entry not found: %s\nInject into %s instead?", deststr, dest)) { + f_close(&ofile); + return false; + } + TruncateString(deststr, dest, 36, 8); + } if (dvfile.size != osize) { char osizestr[32]; char dsizestr[32]; @@ -239,7 +249,7 @@ bool PathCopyVirtual(const char* destdir, const char* orig) { FIL dfile; u32 osize; - if (!FindVirtualFile(&ovfile, orig)) + if (!FindVirtualFile(&ovfile, orig, 0)) return false; // check if destination exists if (f_stat(dest, NULL) == FR_OK) { @@ -563,7 +573,7 @@ bool GetVirtualDirContentsWorker(DirStruct* contents, const char* path) { VirtualFile vfile; DirEntry* entry = &(contents->entry[contents->n_entries]); snprintf(entry->path, 256, "%s/%s", path, virtualFileList[n]); - if (!FindVirtualFile(&vfile, entry->path)) continue; + if (!FindVirtualFile(&vfile, entry->path, 0)) continue; entry->name = entry->path + strnlen(path, 256) + 1; entry->size = vfile.size; entry->type = T_FILE; diff --git a/source/nand/virtual.c b/source/nand/virtual.c index d118b2e..eb003c6 100644 --- a/source/nand/virtual.c +++ b/source/nand/virtual.c @@ -22,8 +22,8 @@ VirtualFile virtualFileTemplates[] = { { "nand.bin" , 0x00000000, 0x00000000, 0xFF, VFLAG_ON_ALL | VFLAG_NAND_SIZE }, { "nand_minsize.bin" , 0x00000000, 0x3AF00000, 0xFF, VFLAG_ON_O3DS }, { "nand_minsize.bin" , 0x00000000, 0x4D800000, 0xFF, VFLAG_ON_N3DS | VFLAG_ON_NO3DS }, - { "nand_hdr.bin" , 0x00000000, 0x00000200, 0xFF, VFLAG_ON_ALL }, - { "sector0x96.bin" , 0x00012C00, 0x00000200, 0xFF, VFLAG_ON_ALL } + { "sector0x96.bin" , 0x00012C00, 0x00000200, 0xFF, VFLAG_ON_ALL }, + { "nand_hdr.bin" , 0x00000000, 0x00000200, 0xFF, VFLAG_ON_ALL } }; u32 IsVirtualPath(const char* path) { @@ -45,7 +45,7 @@ bool CheckVirtualPath(const char* path) { return false; } -bool FindVirtualFile(VirtualFile* vfile, const char* path) +bool FindVirtualFile(VirtualFile* vfile, const char* path, u32 size) { char* fname = strchr(path, '/'); bool on_emunand = false; @@ -68,7 +68,9 @@ bool FindVirtualFile(VirtualFile* vfile, const char* path) VirtualFile* curr_template = NULL; for (u32 i = 0; i < n_templates; i++) { curr_template = &virtualFileTemplates[i]; - if ((curr_template->flags & nand_type) && (strncmp(fname, curr_template->name, 32) == 0)) + if ((curr_template->flags & nand_type) && (strncasecmp(fname, curr_template->name, 32) == 0)) + break; + else if (size && (curr_template->size == size)) //search by size should be a last resort solution break; curr_template = NULL; } diff --git a/source/nand/virtual.h b/source/nand/virtual.h index 336e7e6..1afa5cc 100644 --- a/source/nand/virtual.h +++ b/source/nand/virtual.h @@ -23,6 +23,6 @@ typedef struct { u32 IsVirtualPath(const char* path); bool CheckVirtualPath(const char* path); -bool FindVirtualFile(VirtualFile* vfile, const char* path); +bool FindVirtualFile(VirtualFile* vfile, const char* path, u32 size); int ReadVirtualFile(const VirtualFile* vfile, u8* buffer, u32 offset, u32 count); int WriteVirtualFile(const VirtualFile* vfile, const u8* buffer, u32 offset, u32 count);