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
This commit is contained in:
Pk11 2022-01-12 23:57:34 -06:00
parent f893d21f05
commit c4e202b366
4 changed files with 31 additions and 14 deletions

View File

@ -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;
}

View File

@ -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();

View File

@ -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

View File

@ -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
{