From c4e202b3661795ca78c033cdddbd28f61f26a4ba Mon Sep 17 00:00:00 2001 From: Pk11 Date: Wed, 12 Jan 2022 23:57:34 -0600 Subject: [PATCH] Fix free space calculation I switched it to calculating for the drive, but we actually want just the 1024 blocks given to the 00030004 folder anyways, so this just reverts to the old behaviour, but with a couple fixes --- arm9/src/storage.c | 37 +++++++++++++++++++++++++++---------- arm9/src/storage.h | 2 +- arm9/src/testmenu.c | 2 +- arm9/src/titlemenu.c | 4 ++-- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/arm9/src/storage.c b/arm9/src/storage.c index abcd219..4fdd564 100644 --- a/arm9/src/storage.c +++ b/arm9/src/storage.c @@ -384,7 +384,7 @@ bool deleteDir(char const* path) return result; } -unsigned long long getDirSize(const char* path) +unsigned long long getDirSize(const char* path, u32 blockSize) { if (!path) return 0; @@ -404,7 +404,7 @@ unsigned long long getDirSize(const char* path) char fullpath[512]; sprintf(fullpath, "%s/%s", path, ent->d_name); - size += getDirSize(fullpath); + size += getDirSize(fullpath, blockSize); } else { @@ -412,6 +412,10 @@ unsigned long long getDirSize(const char* path) sprintf(fullpath, "%s/%s", path, ent->d_name); size += getFileSizePath(fullpath); + + // If we've specified a block size, round up to it + if ((size % blockSize) != 0) + size += blockSize - (size % blockSize); } } } @@ -493,7 +497,7 @@ unsigned long long getSDCardFree() if (sdIsInserted()) { struct statvfs st; - if (statvfs("/", &st) == 0) + if (statvfs("sd:/", &st) == 0) return st.f_bsize * st.f_bavail; } @@ -503,18 +507,31 @@ unsigned long long getSDCardFree() //internal storage unsigned long long getDsiSize() { - struct statvfs st; - if (statvfs("nand:/", &st) == 0) - return st.f_bsize * st.f_blocks; - - return 0; + //The DSi has 256MB of internal storage. Some is unavailable and used by other things. + //An empty DSi reads 1024 open blocks + return 1024 * BYTES_PER_BLOCK; } unsigned long long getDsiFree() { + u32 blockSize = 0; struct statvfs st; if (statvfs("nand:/", &st) == 0) - return st.f_bsize * st.f_bavail; + blockSize = st.f_bsize; - return 0; + //Get free space by subtracting file sizes in nand folders + unsigned long long size = getDsiSize(); + unsigned long long appSize = getDirSize(sdnandMode ? "sd:/title/00030004" : "nand:/title/00030004", blockSize); + + //subtract, but don't go under 0 + if (appSize > size) + { + size = 0; + } + else + { + size -= appSize; + } + + return size; } \ No newline at end of file diff --git a/arm9/src/storage.h b/arm9/src/storage.h index 18ce0f1..8701cf6 100644 --- a/arm9/src/storage.h +++ b/arm9/src/storage.h @@ -26,7 +26,7 @@ bool padFile(char const* path, int size); bool dirExists(char const* path); bool copyDir(char const* src, char const* dst); bool deleteDir(char const* path); -unsigned long long getDirSize(char const* path); +unsigned long long getDirSize(char const* path, u32 blockSize); //home menu int getMenuSlots(); diff --git a/arm9/src/testmenu.c b/arm9/src/testmenu.c index 1b8548b..4e16114 100644 --- a/arm9/src/testmenu.c +++ b/arm9/src/testmenu.c @@ -61,7 +61,7 @@ void testMenu() iprintf("\n"); swiWaitForVBlank(); - printf("\t%.0f / %.0f blocks\n", (float)free / BYTES_PER_BLOCK, (float)size / BYTES_PER_BLOCK); + iprintf("\t%d / %d blocks\n", free / BYTES_PER_BLOCK, size / BYTES_PER_BLOCK); } //end diff --git a/arm9/src/titlemenu.c b/arm9/src/titlemenu.c index 5ae2433..9b9211d 100644 --- a/arm9/src/titlemenu.c +++ b/arm9/src/titlemenu.c @@ -278,9 +278,9 @@ static void backup(Menu* m) char* srcpath = (char*)malloc(strlen("nand:/title/") + 32); sprintf(srcpath, "%s:/title/%08x/%08x", sdnandMode ? "sd" : "nand", (unsigned int)tid_high, (unsigned int)tid_low); - if (getSDCardFree() < getDirSize(srcpath)) + if (getSDCardFree() < getDirSize(srcpath, 0)) { - messageBox("Not enough space on NAND."); + messageBox("Not enough space on SD."); } else {