From 50c9091d2ca11358e4d1186b328e0899debaeaae Mon Sep 17 00:00:00 2001 From: d0k3 Date: Mon, 21 Mar 2016 23:18:33 +0100 Subject: [PATCH] Prevented a possible bug when unmounting the SD --- source/fs.c | 15 ++++++++++++--- source/fs.h | 3 ++- source/godmode.c | 6 ++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/source/fs.c b/source/fs.c index 7fe758d..64afda9 100644 --- a/source/fs.c +++ b/source/fs.c @@ -39,7 +39,7 @@ bool InitNandFS() { return true; } -void DeinitFS() { +void DeinitNandFS() { for (u32 i = MAX_FS; i > 0; i--) { if (fs_mounted[i]) { char fsname[8]; @@ -50,6 +50,13 @@ void DeinitFS() { } } +void DeinitSDCardFS() { + if (fs_mounted[0]) { + f_mount(NULL, "0:", 1); + fs_mounted[0] = false; + } +} + int PathToNumFS(const char* path) { int fsnum = *path - (int) '0'; if ((fsnum < 0) || (fsnum >= MAX_FS) || (path[1] != ':')) { @@ -234,10 +241,12 @@ bool PathCopyWorker(char* dest, char* orig, bool overwrite) { for (size_t pos = 0; (pos < fsize) && ret; pos += MAIN_BUFFER_SIZE) { UINT bytes_read = 0; UINT bytes_written = 0; - f_read(&ofile, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read); + if (f_read(&ofile, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read) != FR_OK) + ret = false; if (!ShowProgress(pos + (bytes_read / 2), fsize, orig)) ret = false; - f_write(&dfile, MAIN_BUFFER, bytes_read, &bytes_written); + if (f_write(&dfile, MAIN_BUFFER, bytes_read, &bytes_written) != FR_OK) + ret = false; if (bytes_read != bytes_written) ret = false; } diff --git a/source/fs.h b/source/fs.h index 18a7aa2..73771d3 100644 --- a/source/fs.h +++ b/source/fs.h @@ -26,7 +26,8 @@ typedef struct { bool InitSDCardFS(); bool InitNandFS(); -void DeinitFS(); +void DeinitNandFS(); +void DeinitSDCardFS(); /** Check if writing to this path is allowed **/ bool CheckWritePermissions(const char* path); diff --git a/source/godmode.c b/source/godmode.c index 39562fe..2715009 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -189,7 +189,8 @@ u32 GodMode() { scroll = 0; } } else if ((pad_state & BUTTON_B) && (pad_state & BUTTON_R1)) { // unmount SD card - DeinitFS(); + DeinitNandFS(); + DeinitSDCardFS(); ShowPrompt(false, "SD card unmounted, you can eject now.\nPut it back in before you press ."); while (!InitSDCardFS()) { if (!ShowPrompt(true, "Reinitialising SD card failed! Retry?")) @@ -336,7 +337,8 @@ u32 GodMode() { } } - DeinitFS(); + DeinitNandFS(); + DeinitSDCardFS(); return exit_mode; }