fix UTF8 shenanigans and other fun bugs

This commit is contained in:
Arisotura 2021-10-28 18:00:55 +02:00
parent 0f59c1af4d
commit 53a36781cf
3 changed files with 33 additions and 22 deletions

View File

@ -25,6 +25,16 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
// really, Windows?
#ifdef __WIN32__
#define melon_fseek _fseeki64
#define melon_ftell _ftelli64
#else
#define melon_fseek fseek
#define melon_ftell ftell
#endif // __WIN32__
FATStorage::FATStorage(std::string filename, u64 size, bool readonly, std::string sourcedir) FATStorage::FATStorage(std::string filename, u64 size, bool readonly, std::string sourcedir)
{ {
ReadOnly = readonly; ReadOnly = readonly;
@ -138,7 +148,7 @@ u32 FATStorage::ReadSectorsInternal(FILE* file, u64 filelen, u32 start, u32 num,
num = len >> 9; num = len >> 9;
} }
fseek(file, addr, SEEK_SET); melon_fseek(file, addr, SEEK_SET);
u32 res = fread(data, 0x200, num, file); u32 res = fread(data, 0x200, num, file);
if (res < num) if (res < num)
@ -167,7 +177,7 @@ u32 FATStorage::WriteSectorsInternal(FILE* file, u64 filelen, u32 start, u32 num
num = len >> 9; num = len >> 9;
} }
fseek(file, addr, SEEK_SET); melon_fseek(file, addr, SEEK_SET);
u32 res = fwrite(data, 0x200, num, file); u32 res = fwrite(data, 0x200, num, file);
return res; return res;
@ -332,7 +342,7 @@ void FATStorage::SaveIndex()
} }
bool FATStorage::ExportFile(std::string path, std::string out) bool FATStorage::ExportFile(std::string path, fs::path out)
{ {
FF_FIL file; FF_FIL file;
FILE* fout; FILE* fout;
@ -353,7 +363,7 @@ bool FATStorage::ExportFile(std::string path, std::string out)
err); err);
} }
fout = fopen(out.c_str(), "wb"); fout = Platform::OpenFile(out.u8string().c_str(), "wb");
if (!fout) if (!fout)
{ {
f_close(&file); f_close(&file);
@ -401,13 +411,14 @@ void FATStorage::ExportDirectory(std::string path, std::string outbase, int leve
if (!info.fname[0]) break; if (!info.fname[0]) break;
std::string fullpath = path + info.fname; std::string fullpath = path + info.fname;
fs::path outpath = fs::u8path(outbase + "/" + fullpath);
if (info.fattrib & AM_DIR) if (info.fattrib & AM_DIR)
{ {
if (DirIndex.count(fullpath) < 1) if (DirIndex.count(fullpath) < 1)
{ {
std::string dirpath = outbase+"/"+fullpath; std::error_code err;
mkdir(dirpath.c_str()); fs::create_directory(outpath, err);
DirIndexEntry entry; DirIndexEntry entry;
entry.Path = fullpath; entry.Path = fullpath;
@ -448,7 +459,6 @@ void FATStorage::ExportDirectory(std::string path, std::string outbase, int leve
if (doexport) if (doexport)
{ {
std::string outpath = outbase+"/"+fullpath;
if (ExportFile("0:/"+fullpath, outpath)) if (ExportFile("0:/"+fullpath, outpath))
{ {
fs::file_time_type modtime = fs::last_write_time(outpath); fs::file_time_type modtime = fs::last_write_time(outpath);
@ -465,7 +475,7 @@ void FATStorage::ExportDirectory(std::string path, std::string outbase, int leve
} }
std::error_code err; std::error_code err;
fs::permissions(outbase+"/"+fullpath, fs::permissions(outpath,
fs::perms::owner_read | fs::perms::owner_write, fs::perms::owner_read | fs::perms::owner_write,
(info.fattrib & AM_RDO) ? fs::perm_options::remove : fs::perm_options::add, (info.fattrib & AM_RDO) ? fs::perm_options::remove : fs::perm_options::add,
err); err);
@ -513,7 +523,7 @@ bool FATStorage::DeleteHostDirectory(std::string path, std::string outbase, int
for (const auto& key : filedeletelist) for (const auto& key : filedeletelist)
{ {
std::string fullpath = outbase + "/" + key; fs::path fullpath = fs::u8path(outbase + "/" + key);
std::error_code err; std::error_code err;
fs::permissions(fullpath, fs::permissions(fullpath,
fs::perms::owner_read | fs::perms::owner_write, fs::perms::owner_read | fs::perms::owner_write,
@ -532,7 +542,7 @@ bool FATStorage::DeleteHostDirectory(std::string path, std::string outbase, int
} }
{ {
std::string fullpath = outbase + "/" + path; fs::path fullpath = fs::u8path(outbase + "/" + path);
std::error_code err; std::error_code err;
fs::permissions(fullpath, fs::permissions(fullpath,
@ -579,7 +589,7 @@ void FATStorage::ExportChanges(std::string outbase)
for (const auto& key : deletelist) for (const auto& key : deletelist)
{ {
std::string fullpath = outbase + "/" + key; fs::path fullpath = fs::u8path(outbase + "/" + key);
std::error_code err; std::error_code err;
fs::permissions(fullpath, fs::permissions(fullpath,
@ -725,7 +735,7 @@ void FATStorage::CleanupDirectory(std::string sourcedir, std::string path, int l
{ {
if (DirIndex.count(fullpath) < 1) if (DirIndex.count(fullpath) < 1)
dirdeletelist.push_back(fullpath); dirdeletelist.push_back(fullpath);
else if (!fs::is_directory(sourcedir+"/"+fullpath)) else if (!fs::is_directory(fs::u8path(sourcedir+"/"+fullpath)))
{ {
DirIndex.erase(fullpath); DirIndex.erase(fullpath);
dirdeletelist.push_back(fullpath); dirdeletelist.push_back(fullpath);
@ -737,7 +747,7 @@ void FATStorage::CleanupDirectory(std::string sourcedir, std::string path, int l
{ {
if (FileIndex.count(fullpath) < 1) if (FileIndex.count(fullpath) < 1)
filedeletelist.push_back(fullpath); filedeletelist.push_back(fullpath);
else if (!fs::is_regular_file(sourcedir+"/"+fullpath)) else if (!fs::is_regular_file(fs::u8path(sourcedir+"/"+fullpath)))
{ {
FileIndex.erase(fullpath); FileIndex.erase(fullpath);
filedeletelist.push_back(fullpath); filedeletelist.push_back(fullpath);
@ -765,13 +775,13 @@ void FATStorage::CleanupDirectory(std::string sourcedir, std::string path, int l
} }
} }
bool FATStorage::ImportFile(std::string path, std::string in) bool FATStorage::ImportFile(std::string path, fs::path in)
{ {
FF_FIL file; FF_FIL file;
FILE* fin; FILE* fin;
FRESULT res; FRESULT res;
fin = fopen(in.c_str(), "rb"); fin = Platform::OpenFile(in.u8string().c_str(), "rb");
if (!fin) if (!fin)
return false; return false;
@ -824,7 +834,7 @@ bool FATStorage::ImportDirectory(std::string sourcedir)
// * files will be added if they aren't in the index, or if the size or last-modified-date don't match // * files will be added if they aren't in the index, or if the size or last-modified-date don't match
for (auto& entry : fs::recursive_directory_iterator(sourcedir)) for (auto& entry : fs::recursive_directory_iterator(sourcedir))
{ {
std::string fullpath = entry.path().string(); std::string fullpath = entry.path().u8string();
std::string innerpath = fullpath.substr(srclen); std::string innerpath = fullpath.substr(srclen);
if (innerpath[0] == '/' || innerpath[0] == '\\') if (innerpath[0] == '/' || innerpath[0] == '\\')
innerpath = innerpath.substr(1); innerpath = innerpath.substr(1);
@ -882,7 +892,7 @@ bool FATStorage::ImportDirectory(std::string sourcedir)
ientry.LastModified = lastmodified_raw; ientry.LastModified = lastmodified_raw;
innerpath = "0:/" + innerpath; innerpath = "0:/" + innerpath;
if (ImportFile(innerpath, fullpath)) if (ImportFile(innerpath, entry.path()))
{ {
FF_FILINFO finfo; FF_FILINFO finfo;
f_stat(innerpath.c_str(), &finfo); f_stat(innerpath.c_str(), &finfo);
@ -963,8 +973,9 @@ bool FATStorage::Load(std::string filename, u64 size, std::string sourcedir)
if (FileSize == 0) if (FileSize == 0)
{ {
fseek(FF_File, 0, SEEK_END); melon_fseek(FF_File, 0, SEEK_END);
FileSize = ftell(FF_File); FileSize = melon_ftell(FF_File);
printf("FILESIZE DETERMINED TO BE %016llX (%d) (%d)\n", FileSize, sizeof(long), errno);
} }
} }

View File

@ -62,7 +62,7 @@ private:
void LoadIndex(); void LoadIndex();
void SaveIndex(); void SaveIndex();
bool ExportFile(std::string path, std::string out); bool ExportFile(std::string path, std::filesystem::path out);
void ExportDirectory(std::string path, std::string outbase, int level); void ExportDirectory(std::string path, std::string outbase, int level);
bool DeleteHostDirectory(std::string path, std::string outbase, int level); bool DeleteHostDirectory(std::string path, std::string outbase, int level);
void ExportChanges(std::string outbase); void ExportChanges(std::string outbase);
@ -70,7 +70,7 @@ private:
bool CanFitFile(u32 len); bool CanFitFile(u32 len);
bool DeleteDirectory(std::string path, int level); bool DeleteDirectory(std::string path, int level);
void CleanupDirectory(std::string sourcedir, std::string path, int level); void CleanupDirectory(std::string sourcedir, std::string path, int level);
bool ImportFile(std::string path, std::string in); bool ImportFile(std::string path, std::filesystem::path in);
bool ImportDirectory(std::string sourcedir); bool ImportDirectory(std::string sourcedir);
u64 GetDirectorySize(std::string sourcedir); u64 GetDirectorySize(std::string sourcedir);

View File

@ -26,7 +26,7 @@
<item> <item>
<widget class="QTabWidget" name="tabWidget"> <widget class="QTabWidget" name="tabWidget">
<property name="currentIndex"> <property name="currentIndex">
<number>1</number> <number>0</number>
</property> </property>
<widget class="QWidget" name="tab"> <widget class="QWidget" name="tab">
<attribute name="title"> <attribute name="title">