[FS] Integrate Result<T, E> to codebase

This commit is contained in:
wheremyfoodat 2023-04-02 00:50:19 +03:00
parent 38eb4d8088
commit 4f2d59ccbe
15 changed files with 122 additions and 114 deletions

View file

@ -9,6 +9,7 @@
#include <vector>
#include "helpers.hpp"
#include "memory.hpp"
#include "result.hpp"
namespace PathType {
enum : u32 {
@ -127,14 +128,10 @@ struct DirectorySession {
// Otherwise the fd of the opened file is returned (or nullptr if the opened file doesn't require one)
using FileDescriptor = std::optional<FILE*>;
enum class CreateFileResult : u32 {
enum class FSResult : u32 {
Success = 0,
AlreadyExists = 0x82044BE,
FileTooLarge = 0x86044D2
};
enum class DeleteFileResult : u32 {
Success = 0,
FileTooLarge = 0x86044D2,
FileNotFound = 0xC8804470
};
@ -149,6 +146,7 @@ public:
protected:
using Handle = u32;
static constexpr FileDescriptor NoFile = nullptr;
static constexpr FileDescriptor FileError = std::nullopt;
Memory& mem;
@ -201,8 +199,8 @@ protected:
public:
virtual std::string name() = 0;
virtual u64 getFreeBytes() = 0;
virtual CreateFileResult createFile(const FSPath& path, u64 size) = 0;
virtual DeleteFileResult deleteFile(const FSPath& path) = 0;
virtual FSResult createFile(const FSPath& path, u64 size) = 0;
virtual FSResult deleteFile(const FSPath& path) = 0;
virtual FormatInfo 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
@ -213,9 +211,9 @@ public:
virtual FileDescriptor openFile(const FSPath& path, const FilePerms& perms) = 0;
virtual ArchiveBase* openArchive(const FSPath& path) = 0;
virtual std::optional<DirectorySession> openDirectory(const FSPath& path) {
virtual Rust::Result<DirectorySession, FSResult> openDirectory(const FSPath& path) {
Helpers::panic("Unimplemented OpenDirectory for %s archive", name().c_str());
return std::nullopt;
return Err(FSResult::FileNotFound);
}
// Read size bytes from a file starting at offset "offset" into a certain buffer in memory

View file

@ -8,8 +8,8 @@ public:
u64 getFreeBytes() override { Helpers::panic("ExtSaveData::GetFreeBytes unimplemented"); return 0; }
std::string name() override { return "ExtSaveData"; }
CreateFileResult createFile(const FSPath& path, u64 size) override;
DeleteFileResult deleteFile(const FSPath& path) override;
FSResult createFile(const FSPath& path, u64 size) override;
FSResult deleteFile(const FSPath& path) override;
ArchiveBase* openArchive(const FSPath& path) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;

View file

@ -8,8 +8,8 @@ public:
u64 getFreeBytes() override { Helpers::panic("NCCH::GetFreeBytes unimplemented"); return 0; }
std::string name() override { return "NCCH"; }
CreateFileResult createFile(const FSPath& path, u64 size) override;
DeleteFileResult deleteFile(const FSPath& path) override;
FSResult createFile(const FSPath& path, u64 size) override;
FSResult deleteFile(const FSPath& path) override;
ArchiveBase* openArchive(const FSPath& path) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;

View file

@ -8,12 +8,12 @@ public:
u64 getFreeBytes() override { Helpers::panic("SaveData::GetFreeBytes unimplemented"); return 0; }
std::string name() override { return "SaveData"; }
CreateFileResult createFile(const FSPath& path, u64 size) override;
DeleteFileResult deleteFile(const FSPath& path) override;
FSResult createFile(const FSPath& path, u64 size) override;
FSResult deleteFile(const FSPath& path) override;
FormatInfo getFormatInfo(const FSPath& path) override;
ArchiveBase* openArchive(const FSPath& path) override;
std::optional<DirectorySession> openDirectory(const FSPath& path) override;
Rust::Result<DirectorySession, FSResult> openDirectory(const FSPath& path) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;
std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override;

View file

@ -8,8 +8,8 @@ public:
u64 getFreeBytes() override { Helpers::panic("SDMC::GetFreeBytes unimplemented"); return 0; }
std::string name() override { return "SDMC"; }
CreateFileResult createFile(const FSPath& path, u64 size) override;
DeleteFileResult deleteFile(const FSPath& path) override;
FSResult createFile(const FSPath& path, u64 size) override;
FSResult deleteFile(const FSPath& path) override;
ArchiveBase* openArchive(const FSPath& path) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;

View file

@ -8,8 +8,8 @@ public:
u64 getFreeBytes() override { return 0; }
std::string name() override { return "SelfNCCH"; }
CreateFileResult createFile(const FSPath& path, u64 size) override;
DeleteFileResult deleteFile(const FSPath& path) override;
FSResult createFile(const FSPath& path, u64 size) override;
FSResult deleteFile(const FSPath& path) override;
ArchiveBase* openArchive(const FSPath& path) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;

View file

@ -17,6 +17,7 @@ namespace Log {
}
};
#define false 0
// Our loggers here. Enable/disable by toggling the template param
static Logger<false> kernelLogger;
static Logger<true> debugStringLogger; // Enables output for the outputDebugString SVC
@ -48,6 +49,7 @@ namespace Log {
static Logger<false> ptmLogger;
static Logger<false> y2rLogger;
static Logger<false> srvLogger;
#undef false
#define MAKE_LOG_FUNCTION(functionName, logger) \
template <typename... Args> \

View file

@ -29,7 +29,7 @@ class FSService {
ArchiveBase* getArchiveFromID(u32 id);
std::optional<Handle> openArchiveHandle(u32 archiveID, const FSPath& path);
std::optional<Handle> openDirectoryHandle(ArchiveBase* archive, const FSPath& path);
Rust::Result<Handle, FSResult> openDirectoryHandle(ArchiveBase* archive, const FSPath& path);
std::optional<Handle> openFileHandle(ArchiveBase* archive, const FSPath& path, const FSPath& archivePath, const FilePerms& perms);
FSPath readPath(u32 type, u32 pointer, u32 size);