[FS] Better file flushing

Fixes a small memory leak and some filesystem bugs
This commit is contained in:
wheremyfoodat 2023-07-28 22:42:30 +03:00
parent 9fe770c4e7
commit 0f0a06330c
3 changed files with 11 additions and 0 deletions

View file

@ -27,6 +27,7 @@ class IOFile {
bool seek(std::int64_t offset, int origin = SEEK_SET);
bool rewind();
bool flush();
FILE* getHandle();
static void setAppDataDir(const std::filesystem::path& dir);
static std::filesystem::path getAppData() { return appData; }

View file

@ -156,6 +156,8 @@ Rust::Result<ArchiveBase::FormatInfo, HorizonResult> SaveDataArchive::getFormatI
FormatInfo ret;
auto [success, bytesRead] = file.readBytes(&ret, sizeof(FormatInfo));
file.close();
if (!success || bytesRead != sizeof(FormatInfo)) {
Helpers::warn("SaveData::GetFormatInfo: Format file exists but was not properly read into the FormatInfo struct");
return Err(Result::FS::NotFormatted);
@ -175,6 +177,8 @@ void SaveDataArchive::format(const FSPath& path, const ArchiveBase::FormatInfo&
// Write format info on disk
IOFile file(formatInfoPath, "wb");
file.writeBytes(&info, sizeof(info));
file.flush();
file.close();
}
Rust::Result<ArchiveBase*, HorizonResult> SaveDataArchive::openArchive(const FSPath& path) {

View file

@ -92,6 +92,12 @@ bool IOFile::seek(std::int64_t offset, int origin) {
return true;
}
bool IOFile::flush() {
if (!isOpen() || fflush(handle)) return false;
return true;
}
bool IOFile::rewind() { return seek(0, SEEK_SET); }
FILE* IOFile::getHandle() { return handle; }