From cb694689aa7e30f32fe89dfdda9613779b2e051f Mon Sep 17 00:00:00 2001 From: d0k3 Date: Tue, 1 Aug 2017 02:03:05 +0200 Subject: [PATCH] Allow creating dummy files --- source/filesys/fsutil.c | 17 +++++++++++++++++ source/filesys/fsutil.h | 3 +++ source/godmode.c | 36 ++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/source/filesys/fsutil.c b/source/filesys/fsutil.c index f7f1a07..0cbe8bd 100644 --- a/source/filesys/fsutil.c +++ b/source/filesys/fsutil.c @@ -263,6 +263,23 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or return ret; } +bool FileCreateDummy(const char* cpath, const char* filename, u64 size) { + char npath[256]; // 256 is the maximum length of a full path + if (!CheckWritePermissions(cpath)) return false; + snprintf(npath, 255, "%s/%s", cpath, filename); + + // create dummy file (fail if already existing) + // then, expand the file size via cluster preallocation + FIL dfile; + if (fx_open(&dfile, npath, FA_WRITE | FA_CREATE_NEW) != FR_OK) + return false; + f_lseek(&dfile, size > 0xFFFFFFFF ? 0xFFFFFFFF : (FSIZE_t) size); + f_sync(&dfile); + fx_close(&dfile); + + return true; +} + 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; diff --git a/source/filesys/fsutil.h b/source/filesys/fsutil.h index c803d52..f6d461f 100644 --- a/source/filesys/fsutil.h +++ b/source/filesys/fsutil.h @@ -42,6 +42,9 @@ 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, u64 off_dest, u64 off_orig, u64 size, u32* flags); +/** Create a dummy file at dest **/ +bool FileCreateDummy(const char* cpath, const char* filename, u64 size); + /** Create a new directory in cpath **/ bool DirCreate(const char* cpath, const char* dirname); diff --git a/source/godmode.c b/source/godmode.c index 02ed288..f5f042f 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -226,8 +226,8 @@ void DrawUserInterface(const char* curr_path, DirEntry* curr_entry, DirStruct* c char instr[512]; snprintf(instr, 512, "%s\n%s%s%s%s%s%s%s%s", FLAVOR " Explorer v"VERSION, // generic start part - (*curr_path) ? ((clipboard->n_entries == 0) ? "L - MARK files (use with \x18\x19\x1A\x1B)\nX - DELETE / [+R] RENAME file(s)\nY - COPY file(s) / [+R] CREATE dir\n" : - "L - MARK files (use with \x18\x19\x1A\x1B)\nX - DELETE / [+R] RENAME file(s)\nY - PASTE file(s) / [+R] CREATE dir\n") : + (*curr_path) ? ((clipboard->n_entries == 0) ? "L - MARK files (use with \x18\x19\x1A\x1B)\nX - DELETE / [+R] RENAME file(s)\nY - COPY files / [+R] CREATE entry\n" : + "L - MARK files (use with \x18\x19\x1A\x1B)\nX - DELETE / [+R] RENAME file(s)\nY - PASTE files / [+R] CREATE entry\n") : ((GetWritePermissions() > PERM_BASE) ? "R+Y - Relock write permissions\n" : ""), (*curr_path) ? "" : (GetMountState()) ? "R+X - Unmount image\n" : "", (*curr_path) ? "" : (CheckSDMountState()) ? "R+B - Unmount SD card\n" : "R+B - Remount SD card\n", @@ -1921,18 +1921,26 @@ u32 GodMode() { (cursor > 1) && (strncmp(current_dir->entry[cursor].name, newname, 256) != 0); cursor--); } } - } else if (pad_state & BUTTON_Y) { // create a folder - char dirname[256]; - snprintf(dirname, 255, "newdir"); - if (ShowStringPrompt(dirname, 256, "Create a new folder here?\nEnter name below.")) { - if (!DirCreate(current_path, dirname)) { - char namestr[36+1]; - TruncateString(namestr, dirname, 36, 12); - ShowPrompt(false, "Failed creating folder:\n%s", namestr); - } else { - GetDirContents(current_dir, current_path); - for (cursor = (current_dir->n_entries) ? current_dir->n_entries - 1 : 0; - (cursor > 1) && (strncmp(current_dir->entry[cursor].name, dirname, 256) != 0); cursor--); + } else if (pad_state & BUTTON_Y) { // create an entry + const char* optionstr[] = { "Create a folder", "Create a dummy file" }; + u32 type = ShowSelectPrompt(2, optionstr, "Create a new entry here?\nSelect type."); + if (type) { + const char* typestr = (type == 1) ? "folder" : (type == 2) ? "file" : NULL; + char ename[256]; + u64 fsize = 0; + snprintf(ename, 255, (type == 1) ? "newdir" : "dummy.bin"); + if ((ShowStringPrompt(ename, 256, "Create a new %s here?\nEnter name below.", typestr)) && + ((type != 2) || ((fsize = ShowNumberPrompt(0, "Create a new %s here?\nEnter file size below.", typestr)) != (u64) -1))) { + if (((type == 1) && !DirCreate(current_path, ename)) || + ((type == 2) && !FileCreateDummy(current_path, ename, fsize))) { + char namestr[36+1]; + TruncateString(namestr, ename, 36, 12); + ShowPrompt(false, "Failed creating %s:\n%s", typestr, namestr); + } else { + GetDirContents(current_dir, current_path); + for (cursor = (current_dir->n_entries) ? current_dir->n_entries - 1 : 0; + (cursor > 1) && (strncmp(current_dir->entry[cursor].name, ename, 256) != 0); cursor--); + } } } }