Fix getFileSize failing (#145)

* Try to fix getFileSize failing

* Fix file sizes over 2 GB
This commit is contained in:
Pk11 2022-01-08 16:31:41 -06:00 committed by GitHub
parent 3e92e876e9
commit 750e3cbf99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 105 additions and 130 deletions

View File

@ -47,7 +47,7 @@ GRAPHICS := ../gfx
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
ARCH := -mthumb-interwork ARCH := -mthumb-interwork
CFLAGS := -g -Wall -O2\ CFLAGS := -g -Wall -Wno-psabi -O2\
-march=armv5te -mtune=arm946e-s -fomit-frame-pointer\ -march=armv5te -mtune=arm946e-s -fomit-frame-pointer\
-ffast-math \ -ffast-math \
$(ARCH) $(ARCH)

View File

@ -159,13 +159,13 @@ void dm_drawBottomScreen(void) {
switch(dmOperations[dmCursorPosition]) { switch(dmOperations[dmCursorPosition]) {
case DriveMenuOperation::sdCard: case DriveMenuOperation::sdCard:
font->printf(0, 0, false, Alignment::left, Palette::white, STR_SDCARD_LABEL.c_str(), sdLabel[0] == 0 ? STR_UNTITLED.c_str() : sdLabel); font->printf(0, 0, false, Alignment::left, Palette::white, STR_SDCARD_LABEL.c_str(), sdLabel[0] == 0 ? STR_UNTITLED.c_str() : sdLabel);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_SD_FAT.c_str(), getDriveBytes(sdSize).c_str()); font->printf(0, 1, false, Alignment::left, Palette::white, STR_SD_FAT.c_str(), getBytes(sdSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getDriveBytes(getBytesFree("sd:/")).c_str()); font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getBytes(getBytesFree("sd:/")).c_str());
break; break;
case DriveMenuOperation::flashcard: case DriveMenuOperation::flashcard:
font->printf(0, 0, false, Alignment::left, Palette::white, STR_FLASHCARD_LABEL.c_str(), fatLabel[0] == 0 ? STR_UNTITLED.c_str() : fatLabel); font->printf(0, 0, false, Alignment::left, Palette::white, STR_FLASHCARD_LABEL.c_str(), fatLabel[0] == 0 ? STR_UNTITLED.c_str() : fatLabel);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_SLOT1_FAT.c_str(), getDriveBytes(fatSize).c_str()); font->printf(0, 1, false, Alignment::left, Palette::white, STR_SLOT1_FAT.c_str(), getBytes(fatSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getDriveBytes(getBytesFree("fat:/")).c_str()); font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getBytes(getBytesFree("fat:/")).c_str());
break; break;
case DriveMenuOperation::gbaCart: case DriveMenuOperation::gbaCart:
font->printf(0, 0, false, Alignment::left, Palette::white, STR_GBA_GAMECART.c_str(), romTitle[1]); font->printf(0, 0, false, Alignment::left, Palette::white, STR_GBA_GAMECART.c_str(), romTitle[1]);
@ -185,17 +185,17 @@ void dm_drawBottomScreen(void) {
break; break;
case DriveMenuOperation::ramDrive: case DriveMenuOperation::ramDrive:
font->print(0, 0, false, STR_RAMDRIVE_LABEL); font->print(0, 0, false, STR_RAMDRIVE_LABEL);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_RAMDRIVE_FAT.c_str(), getDriveBytes(ramdSize).c_str()); font->printf(0, 1, false, Alignment::left, Palette::white, STR_RAMDRIVE_FAT.c_str(), getBytes(ramdSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getDriveBytes(getBytesFree("ram:/")).c_str()); font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getBytes(getBytesFree("ram:/")).c_str());
break; break;
case DriveMenuOperation::sysNand: case DriveMenuOperation::sysNand:
font->print(0, 0, false, STR_SYSNAND_LABEL); font->print(0, 0, false, STR_SYSNAND_LABEL);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_SYSNAND_FAT.c_str(), getDriveBytes(nandSize).c_str()); font->printf(0, 1, false, Alignment::left, Palette::white, STR_SYSNAND_FAT.c_str(), getBytes(nandSize).c_str());
font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getDriveBytes(getBytesFree("nand:/")).c_str()); font->printf(0, 2, false, Alignment::left, Palette::white, STR_N_FREE.c_str(), getBytes(getBytesFree("nand:/")).c_str());
break; break;
case DriveMenuOperation::fatImage: case DriveMenuOperation::fatImage:
font->printf(0, 0, false, Alignment::left, Palette::white, STR_FAT_LABEL.c_str(), imgLabel[0] == 0 ? STR_UNTITLED.c_str() : imgLabel); font->printf(0, 0, false, Alignment::left, Palette::white, STR_FAT_LABEL.c_str(), imgLabel[0] == 0 ? STR_UNTITLED.c_str() : imgLabel);
font->printf(0, 1, false, Alignment::left, Palette::white, STR_FAT_IMAGE.c_str(), getDriveBytes(imgSize).c_str()); font->printf(0, 1, false, Alignment::left, Palette::white, STR_FAT_IMAGE.c_str(), getBytes(imgSize).c_str());
break; break;
case DriveMenuOperation::none: case DriveMenuOperation::none:
break; break;

View File

@ -50,40 +50,6 @@ u64 fatSize = 0;
u64 imgSize = 0; u64 imgSize = 0;
u32 ramdSize = 0; u32 ramdSize = 0;
static float getGbNumber(u64 bytes) {
float gbNumber = 0.0f;
for (u64 i = 0; i <= bytes; i += 0x6666666) {
gbNumber += 0.1f;
}
return gbNumber;
}
static float getTbNumber(u64 bytes) {
float tbNumber = 0.0f;
for (u64 i = 0; i <= bytes; i += 0x1999999999) {
tbNumber += 0.01f;
}
return tbNumber;
}
std::string getDriveBytes(u64 bytes)
{
char buffer[32];
if (bytes < (1024 * 1024))
sniprintf(buffer, sizeof(buffer), STR_N_KB.c_str(), (int)bytes >> 10);
else if (bytes >= (1024 * 1024) && bytes < (1024 * 1024 * 1024))
sniprintf(buffer, sizeof(buffer), STR_N_MB.c_str(), (int)bytes >> 20);
else if (bytes >= 0x40000000 && bytes < 0x10000000000)
snprintf(buffer, sizeof(buffer), STR_N_GB_FLOAT.c_str(), getGbNumber(bytes));
else
snprintf(buffer, sizeof(buffer), STR_N_TB_FLOAT.c_str(), getTbNumber(bytes));
return buffer;
}
const char* getDrivePath(void) { const char* getDrivePath(void) {
switch (currentDrive) { switch (currentDrive) {
case Drive::sdCard: case Drive::sdCard:

View File

@ -34,7 +34,6 @@ extern u64 sdSize;
extern u64 fatSize; extern u64 fatSize;
extern u64 imgSize; extern u64 imgSize;
extern u32 ramdSize; extern u32 ramdSize;
extern std::string getDriveBytes(u64 bytes);
extern const char* getDrivePath(void); extern const char* getDrivePath(void);

View File

@ -20,7 +20,23 @@ std::vector<ClipboardFile> clipboard;
bool clipboardOn = false; bool clipboardOn = false;
bool clipboardUsed = true; bool clipboardUsed = true;
std::string getBytes(int bytes) { static float getGbNumber(u64 bytes) {
float gbNumber = 0.0f;
for (u64 i = 0; i <= bytes; i += 0x6666666) {
gbNumber += 0.1f;
}
return gbNumber;
}
static float getTbNumber(u64 bytes) {
float tbNumber = 0.0f;
for (u64 i = 0; i <= bytes; i += 0x1999999999) {
tbNumber += 0.01f;
}
return tbNumber;
}
std::string getBytes(off_t bytes) {
char buffer[32]; char buffer[32];
if (bytes == 1) if (bytes == 1)
sniprintf(buffer, sizeof(buffer), STR_1_BYTE.c_str()); sniprintf(buffer, sizeof(buffer), STR_1_BYTE.c_str());
@ -34,23 +50,19 @@ std::string getBytes(int bytes) {
else if (bytes < (1024 * 1024 * 1024)) else if (bytes < (1024 * 1024 * 1024))
sniprintf(buffer, sizeof(buffer), STR_N_MB.c_str(), bytes >> 20); sniprintf(buffer, sizeof(buffer), STR_N_MB.c_str(), bytes >> 20);
else if (bytes < 0x10000000000)
snprintf(buffer, sizeof(buffer), STR_N_GB_FLOAT.c_str(), getGbNumber(bytes));
else else
sniprintf(buffer, sizeof(buffer), STR_N_GB.c_str(), bytes >> 30); snprintf(buffer, sizeof(buffer), STR_N_TB_FLOAT.c_str(), getTbNumber(bytes));
return buffer; return buffer;
} }
off_t getFileSize(const char *fileName) { off_t getFileSize(const char *fileName) {
FILE* fp = fopen(fileName, "rb"); struct stat st;
off_t fsize = 0; stat(fileName, &st);
if (fp) { return st.st_size;
fseek(fp, 0, SEEK_END);
fsize = ftell(fp); // Get source file's size
fseek(fp, 0, SEEK_SET);
}
fclose(fp);
return fsize;
} }
bool calculateSHA1(const char *fileName, u8 *sha1) { bool calculateSHA1(const char *fileName, u8 *sha1) {

View File

@ -19,7 +19,7 @@ extern std::vector<ClipboardFile> clipboard;
extern bool clipboardOn; extern bool clipboardOn;
extern bool clipboardUsed; extern bool clipboardUsed;
extern std::string getBytes(int bytes); extern std::string getBytes(off_t bytes);
extern off_t getFileSize(const char *fileName); extern off_t getFileSize(const char *fileName);
extern bool calculateSHA1(const char *fileName, u8 *sha1); extern bool calculateSHA1(const char *fileName, u8 *sha1);

View File

@ -1,64 +1,64 @@
/*----------------------------------------------------------------- /*-----------------------------------------------------------------
Copyright (C) 2005 - 2010 Copyright (C) 2005 - 2010
Michael "Chishm" Chisholm Michael "Chishm" Chisholm
Dave "WinterMute" Murphy Dave "WinterMute" Murphy
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
------------------------------------------------------------------*/ ------------------------------------------------------------------*/
#ifndef FILE_BROWSE_H #ifndef FILE_BROWSE_H
#define FILE_BROWSE_H #define FILE_BROWSE_H
#include <string> #include <string>
#include <vector> #include <vector>
struct DirEntry { struct DirEntry {
DirEntry(std::string name, size_t size, bool isDirectory, bool isApp, bool selected = false) : name(name), size(size), isDirectory(isDirectory), isApp(isApp), selected(selected) {} DirEntry(std::string name, off_t size, bool isDirectory, bool isApp, bool selected = false) : name(name), size(size), isDirectory(isDirectory), isApp(isApp), selected(selected) {}
DirEntry() {} DirEntry() {}
std::string name; std::string name;
int size; off_t size;
bool isDirectory; bool isDirectory;
bool isApp; bool isApp;
bool selected = false; bool selected = false;
}; };
enum class FileOperation { enum class FileOperation {
none, none,
bootFile, bootFile,
bootstrapFile, bootstrapFile,
mountNitroFS, mountNitroFS,
ndsInfo, ndsInfo,
trimNds, trimNds,
mountImg, mountImg,
restoreSaveNds, restoreSaveNds,
restoreSaveGba, restoreSaveGba,
showInfo, showInfo,
copySdOut, copySdOut,
copyFatOut, copyFatOut,
calculateSHA1, calculateSHA1,
hexEdit, hexEdit,
loadFont, loadFont,
}; };
bool extension(const std::string_view filename, const std::vector<std::string_view> &extensions); bool extension(const std::string_view filename, const std::vector<std::string_view> &extensions);
std::string browseForFile (void); std::string browseForFile (void);
void getDirectoryContents (std::vector<DirEntry>& dirContents); void getDirectoryContents (std::vector<DirEntry>& dirContents);
#endif //FILE_BROWSE_H #endif //FILE_BROWSE_H

View File

@ -182,10 +182,9 @@ STRING(START_RETURN_B_BACKSPACE, "(START Return, \\B Backspace)")
// Byte counts // Byte counts
STRING(1_BYTE, "1 Byte") STRING(1_BYTE, "1 Byte")
STRING(N_BYTES, "%d Bytes") STRING(N_BYTES, "%lld Bytes")
STRING(N_KB, "%d KB") STRING(N_KB, "%lld KB")
STRING(N_MB, "%d MB") STRING(N_MB, "%lld MB")
STRING(N_GB, "%d GB")
STRING(N_GB_FLOAT, "%.1f GB") STRING(N_GB_FLOAT, "%.1f GB")
STRING(N_TB_FLOAT, "%.1f TB") STRING(N_TB_FLOAT, "%.1f TB")

View File

@ -172,10 +172,9 @@ A_APPLY_B_CANCEL=(\A apply, \B cancel)
START_RETURN_B_BACKSPACE=(START Return, \B Backspace) START_RETURN_B_BACKSPACE=(START Return, \B Backspace)
1_BYTE=1 Byte 1_BYTE=1 Byte
N_BYTES=%d Bytes N_BYTES=%lld Bytes
N_KB=%d KB N_KB=%lld KB
N_MB=%d MB N_MB=%lld MB
N_GB=%d GB
N_GB_FLOAT=%.1f GB N_GB_FLOAT=%.1f GB
N_TB_FLOAT=%.1f TB N_TB_FLOAT=%.1f TB