diff options
author | Jesse Talavera <jesse@jesse.tg> | 2024-01-03 07:32:17 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-03 13:32:17 +0100 |
commit | d1cbc41115e65e9441ce6cb98d94af2923f42d79 (patch) | |
tree | 6e1d8dc36083a767184d38a635814d0c62ee4436 /src/FATStorage.cpp | |
parent | 8bfc6df8de216eff0be4be1dbe37b19a741bed51 (diff) |
Slight fixups with `FATStorage` (#1934)
* Reload the SD card for `CartSD` and all subclasses
* Make `ROMManager::LoadDLDISDCard` delegate to `GetDLDISDCardArgs`
* Add a method overload for `CartSD::SetSDCard`
* Initialize new SD card images with the correct size
* Sync the old card to the host (if applicable) when move-assigning a new one
* Only sync the old card to the host if it's not read-only
* Remove static state in `FATStorage`
- Replace `FF_ReadStorage` and `FF_WriteStorage` with lambda functions
- Keep open and use the single `File` handle throughout the `FATStorage`'s life
Diffstat (limited to 'src/FATStorage.cpp')
-rw-r--r-- | src/FATStorage.cpp | 65 |
1 files changed, 20 insertions, 45 deletions
diff --git a/src/FATStorage.cpp b/src/FATStorage.cpp index 9a1a9ad..0f1bf23 100644 --- a/src/FATStorage.cpp +++ b/src/FATStorage.cpp @@ -48,8 +48,6 @@ FATStorage::FATStorage(FATStorageArgs&& args) noexcept : SourceDir(std::move(args.SourceDir)) { Load(FilePath, FileSize, SourceDir); - - File = Platform::OpenLocalFile(FilePath, FileMode::ReadWriteExisting); } FATStorage::FATStorage(FATStorage&& other) noexcept @@ -71,7 +69,10 @@ FATStorage& FATStorage::operator=(FATStorage&& other) noexcept if (this != &other) { if (File) + { // Sync this file's contents to the host (if applicable) before closing it + if (!ReadOnly) Save(); CloseFile(File); + } FilePath = std::move(other.FilePath); IndexPath = std::move(other.IndexPath); @@ -83,6 +84,7 @@ FATStorage& FATStorage::operator=(FATStorage&& other) noexcept FileIndex = std::move(other.FileIndex); other.File = nullptr; + other.SourceDir = std::nullopt; } return *this; @@ -99,11 +101,8 @@ FATStorage::~FATStorage() bool FATStorage::InjectFile(const std::string& path, u8* data, u32 len) { if (!File) return false; - if (FF_File) return false; - FF_File = File; - FF_FileSize = FileSize; - ff_disk_open(FF_ReadStorage, FF_WriteStorage, (LBA_t)(FileSize>>9)); + ff_disk_open(FF_ReadStorage(), FF_WriteStorage(), (LBA_t)(FileSize>>9)); FRESULT res; FATFS fs; @@ -112,7 +111,6 @@ bool FATStorage::InjectFile(const std::string& path, u8* data, u32 len) if (res != FR_OK) { ff_disk_close(); - FF_File = nullptr; return false; } @@ -124,7 +122,6 @@ bool FATStorage::InjectFile(const std::string& path, u8* data, u32 len) { f_unmount("0:"); ff_disk_close(); - FF_File = nullptr; return false; } @@ -134,18 +131,14 @@ bool FATStorage::InjectFile(const std::string& path, u8* data, u32 len) f_unmount("0:"); ff_disk_close(); - FF_File = nullptr; return nwrite==len; } u32 FATStorage::ReadFile(const std::string& path, u32 start, u32 len, u8* data) { if (!File) return false; - if (FF_File) return false; - FF_File = File; - FF_FileSize = FileSize; - ff_disk_open(FF_ReadStorage, FF_WriteStorage, (LBA_t)(FileSize>>9)); + ff_disk_open(FF_ReadStorage(), FF_WriteStorage(), (LBA_t)(FileSize>>9)); FRESULT res; FATFS fs; @@ -154,7 +147,6 @@ u32 FATStorage::ReadFile(const std::string& path, u32 start, u32 len, u8* data) if (res != FR_OK) { ff_disk_close(); - FF_File = nullptr; return false; } @@ -166,7 +158,6 @@ u32 FATStorage::ReadFile(const std::string& path, u32 start, u32 len, u8* data) { f_unmount("0:"); ff_disk_close(); - FF_File = nullptr; return false; } @@ -177,7 +168,6 @@ u32 FATStorage::ReadFile(const std::string& path, u32 start, u32 len, u8* data) f_unmount("0:"); ff_disk_close(); - FF_File = nullptr; return nread; } @@ -197,18 +187,18 @@ u64 FATStorage::GetSectorCount() const return FileSize / 0x200; } - -FileHandle* FATStorage::FF_File; -u64 FATStorage::FF_FileSize; - -UINT FATStorage::FF_ReadStorage(BYTE* buf, LBA_t sector, UINT num) +ff_disk_read_cb FATStorage::FF_ReadStorage() const noexcept { - return ReadSectorsInternal(FF_File, FF_FileSize, sector, num, buf); + return [this](BYTE* buf, LBA_t sector, UINT num) { + return ReadSectorsInternal(File, FileSize, sector, num, buf); + }; } -UINT FATStorage::FF_WriteStorage(const BYTE* buf, LBA_t sector, UINT num) +ff_disk_write_cb FATStorage::FF_WriteStorage() const noexcept { - return WriteSectorsInternal(FF_File, FF_FileSize, sector, num, buf); + return [this](const BYTE* buf, LBA_t sector, UINT num) { + return WriteSectorsInternal(File, FileSize, sector, num, buf); + }; } @@ -1030,8 +1020,8 @@ bool FATStorage::Load(const std::string& filename, u64 size, const std::optional // with a minimum 128MB extra, otherwise size is defaulted to 512MB bool isnew = !Platform::LocalFileExists(filename); - FF_File = Platform::OpenLocalFile(filename, static_cast<FileMode>(FileMode::ReadWrite | FileMode::Preserve)); - if (!FF_File) + File = Platform::OpenLocalFile(filename, static_cast<FileMode>(FileMode::ReadWrite | FileMode::Preserve)); + if (!File) return false; IndexPath = FilePath + ".idx"; @@ -1047,7 +1037,7 @@ bool FATStorage::Load(const std::string& filename, u64 size, const std::optional if (FileSize == 0) { - FileSize = FileLength(FF_File); + FileSize = FileLength(File); } } @@ -1061,8 +1051,7 @@ bool FATStorage::Load(const std::string& filename, u64 size, const std::optional } else { - FF_FileSize = FileSize; - ff_disk_open(FF_ReadStorage, FF_WriteStorage, (LBA_t)(FF_FileSize>>9)); + ff_disk_open(FF_ReadStorage(), FF_WriteStorage(), (LBA_t)(FileSize>>9)); res = f_mount(&fs, "0:", 1); if (res != FR_OK) @@ -1098,9 +1087,8 @@ bool FATStorage::Load(const std::string& filename, u64 size, const std::optional FileSize = 0x20000000ULL; // 512MB } - FF_FileSize = FileSize; ff_disk_close(); - ff_disk_open(FF_ReadStorage, FF_WriteStorage, (LBA_t)(FF_FileSize>>9)); + ff_disk_open(FF_ReadStorage(), FF_WriteStorage(), (LBA_t)(FileSize>>9)); DirIndex.clear(); FileIndex.clear(); @@ -1137,8 +1125,6 @@ bool FATStorage::Load(const std::string& filename, u64 size, const std::optional f_unmount("0:"); ff_disk_close(); - CloseFile(FF_File); - FF_File = nullptr; return true; } @@ -1150,14 +1136,7 @@ bool FATStorage::Save() return true; // Not an error. } - FF_File = Platform::OpenLocalFile(FilePath, FileMode::ReadWriteExisting); - if (!FF_File) - { - return false; - } - - FF_FileSize = FileSize; - ff_disk_open(FF_ReadStorage, FF_WriteStorage, (LBA_t)(FileSize>>9)); + ff_disk_open(FF_ReadStorage(), FF_WriteStorage(), (LBA_t)(FileSize>>9)); FRESULT res; FATFS fs; @@ -1166,8 +1145,6 @@ bool FATStorage::Save() if (res != FR_OK) { ff_disk_close(); - CloseFile(FF_File); - FF_File = nullptr; return false; } @@ -1178,8 +1155,6 @@ bool FATStorage::Save() f_unmount("0:"); ff_disk_close(); - CloseFile(FF_File); - FF_File = nullptr; return true; } |