[FS] Better GetFormatInfo

This commit is contained in:
wheremyfoodat 2023-05-20 15:39:31 +03:00
parent 01d16fdfd1
commit 4767a2053b
4 changed files with 34 additions and 11 deletions

View file

@ -204,10 +204,11 @@ public:
virtual u64 getFreeBytes() = 0; virtual u64 getFreeBytes() = 0;
virtual FSResult createFile(const FSPath& path, u64 size) = 0; virtual FSResult createFile(const FSPath& path, u64 size) = 0;
virtual FSResult deleteFile(const FSPath& path) = 0; virtual FSResult deleteFile(const FSPath& path) = 0;
virtual FormatInfo getFormatInfo(const FSPath& path) {
virtual Rust::Result<FormatInfo, FSResult> getFormatInfo(const FSPath& path) {
Helpers::panic("Unimplemented GetFormatInfo for %s archive", name().c_str()); 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 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) { virtual FSResult createDirectory(const FSPath& path) {

View file

@ -18,7 +18,7 @@ public:
std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override; std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override;
void format(const FSPath& path, const FormatInfo& info) override; void format(const FSPath& path, const FormatInfo& info) override;
FormatInfo getFormatInfo(const FSPath& path) override; Rust::Result<FormatInfo, FSResult> getFormatInfo(const FSPath& path) override;
std::filesystem::path getFormatInfoPath() { std::filesystem::path getFormatInfoPath() {
return IOFile::getAppData() / "FormatInfo" / "SaveData.format"; return IOFile::getAppData() / "FormatInfo" / "SaveData.format";

View file

@ -120,8 +120,23 @@ Rust::Result<DirectorySession, FSResult> SaveDataArchive::openDirectory(const FS
return Err(FSResult::Success); return Err(FSResult::Success);
} }
ArchiveBase::FormatInfo SaveDataArchive::getFormatInfo(const FSPath& path) { Rust::Result<ArchiveBase::FormatInfo, FSResult> SaveDataArchive::getFormatInfo(const FSPath& path) {
Helpers::panic("Unimplemented SaveData::GetFormatInfo"); 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) { void SaveDataArchive::format(const FSPath& path, const ArchiveBase::FormatInfo& info) {

View file

@ -419,13 +419,20 @@ void FSService::getFormatInfo(u32 messagePointer) {
Helpers::panic("OpenArchive: Tried to open unknown archive %d.", archiveID); 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, IPC::responseHeader(0x845, 5, 0));
Rust::Result<ArchiveBase::FormatInfo, FSResult> 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 + 4, ResultCode::Success);
mem.write32(messagePointer + 8, info.size); mem.write32(messagePointer + 8, info.size);
mem.write32(messagePointer + 12, info.numOfDirectories); mem.write32(messagePointer + 12, info.numOfDirectories);
mem.write32(messagePointer + 16, info.numOfFiles); mem.write32(messagePointer + 16, info.numOfFiles);
mem.write8(messagePointer + 20, info.duplicateData ? 1 : 0); mem.write8(messagePointer + 20, info.duplicateData ? 1 : 0);
} else {
mem.write32(messagePointer + 4, static_cast<u32>(res.unwrapErr()));
}
} }
void FSService::formatSaveData(u32 messagePointer) { void FSService::formatSaveData(u32 messagePointer) {