From 4767a2053b90fae952a2b3bbbfef8090b104f4e6 Mon Sep 17 00:00:00 2001 From: wheremyfoodat Date: Sat, 20 May 2023 15:39:31 +0300 Subject: [PATCH] [FS] Better GetFormatInfo --- include/fs/archive_base.hpp | 5 +++-- include/fs/archive_save_data.hpp | 2 +- src/core/fs/archive_save_data.cpp | 19 +++++++++++++++++-- src/core/services/fs.cpp | 19 +++++++++++++------ 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/include/fs/archive_base.hpp b/include/fs/archive_base.hpp index 8de78ad3..5cd0b3bd 100644 --- a/include/fs/archive_base.hpp +++ b/include/fs/archive_base.hpp @@ -204,10 +204,11 @@ public: virtual u64 getFreeBytes() = 0; virtual FSResult createFile(const FSPath& path, u64 size) = 0; virtual FSResult deleteFile(const FSPath& path) = 0; - virtual FormatInfo getFormatInfo(const FSPath& path) { + + virtual Rust::Result getFormatInfo(const FSPath& path) { Helpers::panic("Unimplemented GetFormatInfo for %s archive", name().c_str()); // Return a dummy struct just to avoid the UB of not returning anything, even if we panic - return FormatInfo{ .size = 0, .numOfDirectories = 0, .numOfFiles = 0, .duplicateData = false }; + return Ok(FormatInfo{ .size = 0, .numOfDirectories = 0, .numOfFiles = 0, .duplicateData = false }); } virtual FSResult createDirectory(const FSPath& path) { diff --git a/include/fs/archive_save_data.hpp b/include/fs/archive_save_data.hpp index f7642955..4831f8df 100644 --- a/include/fs/archive_save_data.hpp +++ b/include/fs/archive_save_data.hpp @@ -18,7 +18,7 @@ public: std::optional readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override; void format(const FSPath& path, const FormatInfo& info) override; - FormatInfo getFormatInfo(const FSPath& path) override; + Rust::Result getFormatInfo(const FSPath& path) override; std::filesystem::path getFormatInfoPath() { return IOFile::getAppData() / "FormatInfo" / "SaveData.format"; diff --git a/src/core/fs/archive_save_data.cpp b/src/core/fs/archive_save_data.cpp index 57d2f2fb..758c1312 100644 --- a/src/core/fs/archive_save_data.cpp +++ b/src/core/fs/archive_save_data.cpp @@ -120,8 +120,23 @@ Rust::Result SaveDataArchive::openDirectory(const FS return Err(FSResult::Success); } -ArchiveBase::FormatInfo SaveDataArchive::getFormatInfo(const FSPath& path) { - Helpers::panic("Unimplemented SaveData::GetFormatInfo"); +Rust::Result SaveDataArchive::getFormatInfo(const FSPath& path) { + const fs::path formatInfoPath = getFormatInfoPath(); + IOFile file(formatInfoPath, "rb"); + + // If the file failed to open somehow, we return that the archive is not formatted + if (!file.isOpen()) { + return Err(FSResult::NotFormatted); + } + + FormatInfo ret; + auto [success, bytesRead] = file.readBytes(&ret, sizeof(FormatInfo)); + if (!success || bytesRead != sizeof(FormatInfo)) { + Helpers::warn("SaveData::GetFormatInfo: Format file exists but was not properly read into the FormatInfo struct"); + return Err(FSResult::NotFormatted); + } + + return Ok(ret); } void SaveDataArchive::format(const FSPath& path, const ArchiveBase::FormatInfo& info) { diff --git a/src/core/services/fs.cpp b/src/core/services/fs.cpp index fd85b484..9aebe375 100644 --- a/src/core/services/fs.cpp +++ b/src/core/services/fs.cpp @@ -419,13 +419,20 @@ void FSService::getFormatInfo(u32 messagePointer) { Helpers::panic("OpenArchive: Tried to open unknown archive %d.", archiveID); } - ArchiveBase::FormatInfo info = archive->getFormatInfo(path); mem.write32(messagePointer, IPC::responseHeader(0x845, 5, 0)); - mem.write32(messagePointer + 4, ResultCode::Success); - mem.write32(messagePointer + 8, info.size); - mem.write32(messagePointer + 12, info.numOfDirectories); - mem.write32(messagePointer + 16, info.numOfFiles); - mem.write8(messagePointer + 20, info.duplicateData ? 1 : 0); + Rust::Result res = archive->getFormatInfo(path); + + // If the FormatInfo was returned, write them to the output buffer. Otherwise, write an error code. + if (res.isOk()) { + ArchiveBase::FormatInfo info = res.unwrap(); + mem.write32(messagePointer + 4, ResultCode::Success); + mem.write32(messagePointer + 8, info.size); + mem.write32(messagePointer + 12, info.numOfDirectories); + mem.write32(messagePointer + 16, info.numOfFiles); + mem.write8(messagePointer + 20, info.duplicateData ? 1 : 0); + } else { + mem.write32(messagePointer + 4, static_cast(res.unwrapErr())); + } } void FSService::formatSaveData(u32 messagePointer) {