From 0b0d6dcf4b05e0db11eecb08d71725c8f4855e50 Mon Sep 17 00:00:00 2001 From: wheremyfoodat Date: Sun, 14 May 2023 19:31:51 +0300 Subject: [PATCH] [FS] Archive::OpenArchive returns Result now --- include/fs/archive_base.hpp | 2 +- include/fs/archive_ext_save_data.hpp | 2 +- include/fs/archive_ncch.hpp | 2 +- include/fs/archive_save_data.hpp | 2 +- include/fs/archive_sdmc.hpp | 2 +- include/fs/archive_self_ncch.hpp | 2 +- src/core/fs/archive_ext_save_data.cpp | 4 ++-- src/core/fs/archive_ncch.cpp | 4 ++-- src/core/fs/archive_save_data.cpp | 6 +++--- src/core/fs/archive_sdmc.cpp | 4 ++-- src/core/fs/archive_self_ncch.cpp | 8 ++++---- src/core/services/fs.cpp | 11 ++++++----- 12 files changed, 25 insertions(+), 24 deletions(-) diff --git a/include/fs/archive_base.hpp b/include/fs/archive_base.hpp index 9af90551..4e5f99f1 100644 --- a/include/fs/archive_base.hpp +++ b/include/fs/archive_base.hpp @@ -217,8 +217,8 @@ public: // Returns nullopt if opening the file failed, otherwise returns a file descriptor to it (nullptr if none is needed) virtual FileDescriptor openFile(const FSPath& path, const FilePerms& perms) = 0; + virtual Rust::Result openArchive(const FSPath& path) = 0; - virtual ArchiveBase* openArchive(const FSPath& path) = 0; virtual Rust::Result openDirectory(const FSPath& path) { Helpers::panic("Unimplemented OpenDirectory for %s archive", name().c_str()); return Err(FSResult::FileNotFound); diff --git a/include/fs/archive_ext_save_data.hpp b/include/fs/archive_ext_save_data.hpp index eeef3b46..b2eab9b7 100644 --- a/include/fs/archive_ext_save_data.hpp +++ b/include/fs/archive_ext_save_data.hpp @@ -12,7 +12,7 @@ public: FSResult createFile(const FSPath& path, u64 size) override; FSResult deleteFile(const FSPath& path) override; - ArchiveBase* openArchive(const FSPath& path) override; + Rust::Result openArchive(const FSPath& path) override; Rust::Result openDirectory(const FSPath& path) override; FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override; std::optional readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override; diff --git a/include/fs/archive_ncch.hpp b/include/fs/archive_ncch.hpp index 8ce3a3b5..9bc84a09 100644 --- a/include/fs/archive_ncch.hpp +++ b/include/fs/archive_ncch.hpp @@ -11,7 +11,7 @@ public: FSResult createFile(const FSPath& path, u64 size) override; FSResult deleteFile(const FSPath& path) override; - ArchiveBase* openArchive(const FSPath& path) override; + Rust::Result openArchive(const FSPath& path) override; FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override; std::optional readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override; diff --git a/include/fs/archive_save_data.hpp b/include/fs/archive_save_data.hpp index b07a461c..4bc0a15e 100644 --- a/include/fs/archive_save_data.hpp +++ b/include/fs/archive_save_data.hpp @@ -13,7 +13,7 @@ public: FSResult deleteFile(const FSPath& path) override; FormatInfo getFormatInfo(const FSPath& path) override; - ArchiveBase* openArchive(const FSPath& path) override; + Rust::Result openArchive(const FSPath& path) override; Rust::Result openDirectory(const FSPath& path) override; FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override; std::optional readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override; diff --git a/include/fs/archive_sdmc.hpp b/include/fs/archive_sdmc.hpp index df8f0221..1e03aa46 100644 --- a/include/fs/archive_sdmc.hpp +++ b/include/fs/archive_sdmc.hpp @@ -11,7 +11,7 @@ public: FSResult createFile(const FSPath& path, u64 size) override; FSResult deleteFile(const FSPath& path) override; - ArchiveBase* openArchive(const FSPath& path) override; + Rust::Result openArchive(const FSPath& path) override; FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override; std::optional readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override; }; \ No newline at end of file diff --git a/include/fs/archive_self_ncch.hpp b/include/fs/archive_self_ncch.hpp index 9b8bbeed..357db93e 100644 --- a/include/fs/archive_self_ncch.hpp +++ b/include/fs/archive_self_ncch.hpp @@ -11,7 +11,7 @@ public: FSResult createFile(const FSPath& path, u64 size) override; FSResult deleteFile(const FSPath& path) override; - ArchiveBase* openArchive(const FSPath& path) override; + Rust::Result openArchive(const FSPath& path) override; FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override; std::optional readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override; diff --git a/src/core/fs/archive_ext_save_data.cpp b/src/core/fs/archive_ext_save_data.cpp index 409de5df..375ba8bc 100644 --- a/src/core/fs/archive_ext_save_data.cpp +++ b/src/core/fs/archive_ext_save_data.cpp @@ -70,12 +70,12 @@ FileDescriptor ExtSaveDataArchive::openFile(const FSPath& path, const FilePerms& return FileError; } -ArchiveBase* ExtSaveDataArchive::openArchive(const FSPath& path) { +Rust::Result ExtSaveDataArchive::openArchive(const FSPath& path) { if (path.type != PathType::Binary || path.binary.size() != 12) { Helpers::panic("ExtSaveData accessed with an invalid path in OpenArchive"); } - return this; + return Ok((ArchiveBase*)this); } Rust::Result ExtSaveDataArchive::openDirectory(const FSPath& path) { diff --git a/src/core/fs/archive_ncch.cpp b/src/core/fs/archive_ncch.cpp index ac6c412f..6deaa0f4 100644 --- a/src/core/fs/archive_ncch.cpp +++ b/src/core/fs/archive_ncch.cpp @@ -48,7 +48,7 @@ FileDescriptor NCCHArchive::openFile(const FSPath& path, const FilePerms& perms) return NoFile; } -ArchiveBase* NCCHArchive::openArchive(const FSPath& path) { +Rust::Result NCCHArchive::openArchive(const FSPath& path) { if (path.type != PathType::Binary || path.binary.size() != 16) { Helpers::panic("NCCHArchive::OpenArchive: Invalid path"); } @@ -57,7 +57,7 @@ ArchiveBase* NCCHArchive::openArchive(const FSPath& path) { if (mediaType != 0) Helpers::panic("NCCH archive. Tried to access a mediatype other than the NAND. Type: %d", mediaType); - return this; + return Ok((ArchiveBase*)this); } std::optional NCCHArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) { diff --git a/src/core/fs/archive_save_data.cpp b/src/core/fs/archive_save_data.cpp index 1107617b..4e8051ac 100644 --- a/src/core/fs/archive_save_data.cpp +++ b/src/core/fs/archive_save_data.cpp @@ -98,13 +98,13 @@ ArchiveBase::FormatInfo SaveDataArchive::getFormatInfo(const FSPath& path) { return FormatInfo{ .size = 0, .numOfDirectories = 255, .numOfFiles = 255, .duplicateData = false }; } -ArchiveBase* SaveDataArchive::openArchive(const FSPath& path) { +Rust::Result SaveDataArchive::openArchive(const FSPath& path) { if (path.type != PathType::Empty) { Helpers::panic("Unimplemented path type for SaveData archive: %d\n", path.type); - return nullptr; + return Err(FSResult::NotFoundInvalid); } - return this; + return Ok((ArchiveBase*)this); } std::optional SaveDataArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) { diff --git a/src/core/fs/archive_sdmc.cpp b/src/core/fs/archive_sdmc.cpp index cef78dec..23564eaa 100644 --- a/src/core/fs/archive_sdmc.cpp +++ b/src/core/fs/archive_sdmc.cpp @@ -16,9 +16,9 @@ FileDescriptor SDMCArchive::openFile(const FSPath& path, const FilePerms& perms) return FileError; } -ArchiveBase* SDMCArchive::openArchive(const FSPath& path) { +Rust::Result SDMCArchive::openArchive(const FSPath& path) { printf("SDMCArchive::OpenArchive: Failed\n"); - return nullptr; + return Ok((ArchiveBase*)nullptr); } std::optional SDMCArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) { diff --git a/src/core/fs/archive_self_ncch.cpp b/src/core/fs/archive_self_ncch.cpp index e1d74774..0447ce8c 100644 --- a/src/core/fs/archive_self_ncch.cpp +++ b/src/core/fs/archive_self_ncch.cpp @@ -40,13 +40,13 @@ FileDescriptor SelfNCCHArchive::openFile(const FSPath& path, const FilePerms& pe return NoFile; // No file descriptor needed for RomFS } -ArchiveBase* SelfNCCHArchive::openArchive(const FSPath& path) { +Rust::Result SelfNCCHArchive::openArchive(const FSPath& path) { if (path.type != PathType::Empty) { - printf("Invalid path type for SelfNCCH archive: %d\n", path.type); - return nullptr; + Helpers::panic("Invalid path type for SelfNCCH archive: %d\n", path.type); + return Err(FSResult::NotFoundInvalid); } - return this; + return Ok((ArchiveBase*)this); } std::optional SelfNCCHArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) { diff --git a/src/core/services/fs.cpp b/src/core/services/fs.cpp index bac0689e..080c0b49 100644 --- a/src/core/services/fs.cpp +++ b/src/core/services/fs.cpp @@ -129,11 +129,11 @@ std::optional FSService::openArchiveHandle(u32 archiveID, const FSPath& return std::nullopt; } - bool opened = archive->openArchive(path); - if (opened) { + Rust::Result res = archive->openArchive(path); + if (res.isOk()) { auto handle = kernel.makeObject(KernelObjectType::Archive); auto& archiveObject = kernel.getObjects()[handle]; - archiveObject.data = new ArchiveSession(archive, path); + archiveObject.data = new ArchiveSession(res.unwrap(), path); return handle; } @@ -334,10 +334,11 @@ void FSService::openFileDirectly(u32 messagePointer) { auto filePath = readPath(filePathType, filePathPointer, filePathSize); const FilePerms perms(openFlags); - archive = archive->openArchive(archivePath); - if (archive == nullptr) [[unlikely]] { + Rust::Result res = archive->openArchive(archivePath); + if (res.isErr()) [[unlikely]] { Helpers::panic("OpenFileDirectly: Failed to open archive with given path"); } + archive = res.unwrap(); std::optional handle = openFileHandle(archive, filePath, archivePath, perms); mem.write32(messagePointer, IPC::responseHeader(0x803, 1, 2));