[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 <vector>
#include "helpers.hpp" #include "helpers.hpp"
#include "memory.hpp" #include "memory.hpp"
#include "result.hpp"
namespace PathType { namespace PathType {
enum : u32 { 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) // Otherwise the fd of the opened file is returned (or nullptr if the opened file doesn't require one)
using FileDescriptor = std::optional<FILE*>; using FileDescriptor = std::optional<FILE*>;
enum class CreateFileResult : u32 { enum class FSResult : u32 {
Success = 0, Success = 0,
AlreadyExists = 0x82044BE, AlreadyExists = 0x82044BE,
FileTooLarge = 0x86044D2 FileTooLarge = 0x86044D2,
};
enum class DeleteFileResult : u32 {
Success = 0,
FileNotFound = 0xC8804470 FileNotFound = 0xC8804470
}; };
@ -149,6 +146,7 @@ public:
protected: protected:
using Handle = u32; using Handle = u32;
static constexpr FileDescriptor NoFile = nullptr; static constexpr FileDescriptor NoFile = nullptr;
static constexpr FileDescriptor FileError = std::nullopt; static constexpr FileDescriptor FileError = std::nullopt;
Memory& mem; Memory& mem;
@ -201,8 +199,8 @@ protected:
public: public:
virtual std::string name() = 0; virtual std::string name() = 0;
virtual u64 getFreeBytes() = 0; virtual u64 getFreeBytes() = 0;
virtual CreateFileResult createFile(const FSPath& path, u64 size) = 0; virtual FSResult createFile(const FSPath& path, u64 size) = 0;
virtual DeleteFileResult deleteFile(const FSPath& path) = 0; virtual FSResult deleteFile(const FSPath& path) = 0;
virtual FormatInfo getFormatInfo(const FSPath& path) { virtual FormatInfo 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
@ -213,9 +211,9 @@ public:
virtual FileDescriptor openFile(const FSPath& path, const FilePerms& perms) = 0; virtual FileDescriptor openFile(const FSPath& path, const FilePerms& perms) = 0;
virtual ArchiveBase* openArchive(const FSPath& path) = 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()); 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 // 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; } u64 getFreeBytes() override { Helpers::panic("ExtSaveData::GetFreeBytes unimplemented"); return 0; }
std::string name() override { return "ExtSaveData"; } std::string name() override { return "ExtSaveData"; }
CreateFileResult createFile(const FSPath& path, u64 size) override; FSResult createFile(const FSPath& path, u64 size) override;
DeleteFileResult deleteFile(const FSPath& path) override; FSResult deleteFile(const FSPath& path) override;
ArchiveBase* openArchive(const FSPath& path) override; ArchiveBase* openArchive(const FSPath& path) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) 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; } u64 getFreeBytes() override { Helpers::panic("NCCH::GetFreeBytes unimplemented"); return 0; }
std::string name() override { return "NCCH"; } std::string name() override { return "NCCH"; }
CreateFileResult createFile(const FSPath& path, u64 size) override; FSResult createFile(const FSPath& path, u64 size) override;
DeleteFileResult deleteFile(const FSPath& path) override; FSResult deleteFile(const FSPath& path) override;
ArchiveBase* openArchive(const FSPath& path) override; ArchiveBase* openArchive(const FSPath& path) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) 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; } u64 getFreeBytes() override { Helpers::panic("SaveData::GetFreeBytes unimplemented"); return 0; }
std::string name() override { return "SaveData"; } std::string name() override { return "SaveData"; }
CreateFileResult createFile(const FSPath& path, u64 size) override; FSResult createFile(const FSPath& path, u64 size) override;
DeleteFileResult deleteFile(const FSPath& path) override; FSResult deleteFile(const FSPath& path) override;
FormatInfo getFormatInfo(const FSPath& path) override; FormatInfo getFormatInfo(const FSPath& path) override;
ArchiveBase* openArchive(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; FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;
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;

View file

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

View file

@ -8,8 +8,8 @@ public:
u64 getFreeBytes() override { return 0; } u64 getFreeBytes() override { return 0; }
std::string name() override { return "SelfNCCH"; } std::string name() override { return "SelfNCCH"; }
CreateFileResult createFile(const FSPath& path, u64 size) override; FSResult createFile(const FSPath& path, u64 size) override;
DeleteFileResult deleteFile(const FSPath& path) override; FSResult deleteFile(const FSPath& path) override;
ArchiveBase* openArchive(const FSPath& path) override; ArchiveBase* openArchive(const FSPath& path) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) 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 // Our loggers here. Enable/disable by toggling the template param
static Logger<false> kernelLogger; static Logger<false> kernelLogger;
static Logger<true> debugStringLogger; // Enables output for the outputDebugString SVC static Logger<true> debugStringLogger; // Enables output for the outputDebugString SVC
@ -48,6 +49,7 @@ namespace Log {
static Logger<false> ptmLogger; static Logger<false> ptmLogger;
static Logger<false> y2rLogger; static Logger<false> y2rLogger;
static Logger<false> srvLogger; static Logger<false> srvLogger;
#undef false
#define MAKE_LOG_FUNCTION(functionName, logger) \ #define MAKE_LOG_FUNCTION(functionName, logger) \
template <typename... Args> \ template <typename... Args> \

View file

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

View file

@ -3,7 +3,7 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
CreateFileResult ExtSaveDataArchive::createFile(const FSPath& path, u64 size) { FSResult ExtSaveDataArchive::createFile(const FSPath& path, u64 size) {
if (size == 0) if (size == 0)
Helpers::panic("ExtSaveData file does not support size == 0"); Helpers::panic("ExtSaveData file does not support size == 0");
@ -15,22 +15,22 @@ CreateFileResult ExtSaveDataArchive::createFile(const FSPath& path, u64 size) {
p += fs::path(path.utf16_string).make_preferred(); p += fs::path(path.utf16_string).make_preferred();
if (fs::exists(p)) if (fs::exists(p))
return CreateFileResult::AlreadyExists; return FSResult::AlreadyExists;
// Create a file of size "size" by creating an empty one, seeking to size - 1 and just writing a 0 there // Create a file of size "size" by creating an empty one, seeking to size - 1 and just writing a 0 there
IOFile file(p.string().c_str(), "wb"); IOFile file(p.string().c_str(), "wb");
if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) { if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) {
return CreateFileResult::Success; return FSResult::Success;
} }
return CreateFileResult::FileTooLarge; return FSResult::FileTooLarge;
} }
Helpers::panic("ExtSaveDataArchive::OpenFile: Failed"); Helpers::panic("ExtSaveDataArchive::OpenFile: Failed");
return CreateFileResult::Success; return FSResult::Success;
} }
DeleteFileResult ExtSaveDataArchive::deleteFile(const FSPath& path) { FSResult ExtSaveDataArchive::deleteFile(const FSPath& path) {
if (path.type == PathType::UTF16) { if (path.type == PathType::UTF16) {
if (!isPathSafe<PathType::UTF16>(path)) if (!isPathSafe<PathType::UTF16>(path))
Helpers::panic("Unsafe path in ExtSaveData::DeleteFile"); Helpers::panic("Unsafe path in ExtSaveData::DeleteFile");
@ -40,11 +40,11 @@ DeleteFileResult ExtSaveDataArchive::deleteFile(const FSPath& path) {
std::error_code ec; std::error_code ec;
bool success = fs::remove(p, ec); bool success = fs::remove(p, ec);
return success ? DeleteFileResult::Success : DeleteFileResult::FileNotFound; return success ? FSResult::Success : FSResult::FileNotFound;
} }
Helpers::panic("ExtSaveDataArchive::DeleteFile: Failed"); Helpers::panic("ExtSaveDataArchive::DeleteFile: Failed");
return DeleteFileResult::Success; return FSResult::Success;
} }
FileDescriptor ExtSaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) { FileDescriptor ExtSaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) {

View file

@ -21,14 +21,14 @@ namespace MediaType {
}; };
}; };
CreateFileResult NCCHArchive::createFile(const FSPath& path, u64 size) { FSResult NCCHArchive::createFile(const FSPath& path, u64 size) {
Helpers::panic("[NCCH] CreateFile not yet supported"); Helpers::panic("[NCCH] CreateFile not yet supported");
return CreateFileResult::Success; return FSResult::Success;
} }
DeleteFileResult NCCHArchive::deleteFile(const FSPath& path) { FSResult NCCHArchive::deleteFile(const FSPath& path) {
Helpers::panic("[NCCH] Unimplemented DeleteFile"); Helpers::panic("[NCCH] Unimplemented DeleteFile");
return DeleteFileResult::Success; return FSResult::Success;
} }
FileDescriptor NCCHArchive::openFile(const FSPath& path, const FilePerms& perms) { FileDescriptor NCCHArchive::openFile(const FSPath& path, const FilePerms& perms) {

View file

@ -4,14 +4,14 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
CreateFileResult SaveDataArchive::createFile(const FSPath& path, u64 size) { FSResult SaveDataArchive::createFile(const FSPath& path, u64 size) {
Helpers::panic("[SaveData] CreateFile not yet supported"); Helpers::panic("[SaveData] CreateFile not yet supported");
return CreateFileResult::Success; return FSResult::Success;
} }
DeleteFileResult SaveDataArchive::deleteFile(const FSPath& path) { FSResult SaveDataArchive::deleteFile(const FSPath& path) {
Helpers::panic("[SaveData] Unimplemented DeleteFile"); Helpers::panic("[SaveData] Unimplemented DeleteFile");
return DeleteFileResult::Success; return FSResult::Success;
} }
FileDescriptor SaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) { FileDescriptor SaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) {
@ -53,10 +53,10 @@ FileDescriptor SaveDataArchive::openFile(const FSPath& path, const FilePerms& pe
return FileError; return FileError;
} }
std::optional<DirectorySession> SaveDataArchive::openDirectory(const FSPath& path) { Rust::Result<DirectorySession, FSResult> SaveDataArchive::openDirectory(const FSPath& path) {
if (!cartHasSaveData()) { if (!cartHasSaveData()) {
printf("Tried to open SaveData directory without save data\n"); printf("Tried to open SaveData directory without save data\n");
return std::nullopt; return Err(FSResult::Success);
} }
if (path.type == PathType::UTF16) { if (path.type == PathType::UTF16) {
@ -66,15 +66,18 @@ std::optional<DirectorySession> SaveDataArchive::openDirectory(const FSPath& pat
fs::path p = IOFile::getAppData() / "SaveData"; fs::path p = IOFile::getAppData() / "SaveData";
p += fs::path(path.utf16_string).make_preferred(); p += fs::path(path.utf16_string).make_preferred();
if (fs::is_regular_file(p))
Helpers::panic("OpenDirectory: Tried to open directory but it's actually a file");
if (fs::is_directory(p)) { if (fs::is_directory(p)) {
return DirectorySession(this, p); return Ok(DirectorySession(this, p));
} else { } else {
Helpers::panic("Directory not found in SaveData::OpenDirectory"); return Err(FSResult::FileNotFound);
} }
} }
Helpers::panic("SaveDataArchive::OpenDirectory: Unimplemented path type"); Helpers::panic("SaveDataArchive::OpenDirectory: Unimplemented path type");
return std::nullopt; return Err(FSResult::Success);
} }
ArchiveBase::FormatInfo SaveDataArchive::getFormatInfo(const FSPath& path) { ArchiveBase::FormatInfo SaveDataArchive::getFormatInfo(const FSPath& path) {

View file

@ -1,14 +1,14 @@
#include "fs/archive_sdmc.hpp" #include "fs/archive_sdmc.hpp"
#include <memory> #include <memory>
CreateFileResult SDMCArchive::createFile(const FSPath& path, u64 size) { FSResult SDMCArchive::createFile(const FSPath& path, u64 size) {
Helpers::panic("[SDMC] CreateFile not yet supported"); Helpers::panic("[SDMC] CreateFile not yet supported");
return CreateFileResult::Success; return FSResult::Success;
} }
DeleteFileResult SDMCArchive::deleteFile(const FSPath& path) { FSResult SDMCArchive::deleteFile(const FSPath& path) {
Helpers::panic("[SDMC] Unimplemented DeleteFile"); Helpers::panic("[SDMC] Unimplemented DeleteFile");
return DeleteFileResult::Success; return FSResult::Success;
} }
FileDescriptor SDMCArchive::openFile(const FSPath& path, const FilePerms& perms) { FileDescriptor SDMCArchive::openFile(const FSPath& path, const FilePerms& perms) {

View file

@ -9,14 +9,14 @@ namespace PathType {
}; };
}; };
CreateFileResult SelfNCCHArchive::createFile(const FSPath& path, u64 size) { FSResult SelfNCCHArchive::createFile(const FSPath& path, u64 size) {
Helpers::panic("[SelfNCCH] CreateFile not yet supported"); Helpers::panic("[SelfNCCH] CreateFile not yet supported");
return CreateFileResult::Success; return FSResult::Success;
} }
DeleteFileResult SelfNCCHArchive::deleteFile(const FSPath& path) { FSResult SelfNCCHArchive::deleteFile(const FSPath& path) {
Helpers::panic("[SelfNCCH] Unimplemented DeleteFile"); Helpers::panic("[SelfNCCH] Unimplemented DeleteFile");
return DeleteFileResult::Success; return FSResult::Success;
} }
FileDescriptor SelfNCCHArchive::openFile(const FSPath& path, const FilePerms& perms) { FileDescriptor SelfNCCHArchive::openFile(const FSPath& path, const FilePerms& perms) {

View file

@ -25,7 +25,7 @@ namespace FSCommands {
}; };
} }
namespace Result { namespace ResultCode {
enum : u32 { enum : u32 {
Success = 0, Success = 0,
FileNotFound = 0xC8804464, // TODO: Verify this FileNotFound = 0xC8804464, // TODO: Verify this
@ -81,16 +81,16 @@ std::optional<Handle> FSService::openFileHandle(ArchiveBase* archive, const FSPa
} }
} }
std::optional<Handle> FSService::openDirectoryHandle(ArchiveBase* archive, const FSPath& path) { Rust::Result<Handle, FSResult> FSService::openDirectoryHandle(ArchiveBase* archive, const FSPath& path) {
std::optional<DirectorySession> opened = archive->openDirectory(path); Rust::Result<DirectorySession, FSResult> opened = archive->openDirectory(path);
if (opened.has_value()) { // If opened doesn't have a value, we failed to open the directory if (opened.isOk()) { // If opened doesn't have a value, we failed to open the directory
auto handle = kernel.makeObject(KernelObjectType::Directory); auto handle = kernel.makeObject(KernelObjectType::Directory);
auto& object = kernel.getObjects()[handle]; auto& object = kernel.getObjects()[handle];
object.data = new DirectorySession(opened.value()); object.data = new DirectorySession(opened.unwrap());
return handle; return Ok(handle);
} else { } else {
return std::nullopt; return Err(opened.unwrapErr());
} }
} }
@ -147,7 +147,7 @@ void FSService::handleSyncRequest(u32 messagePointer) {
void FSService::initialize(u32 messagePointer) { void FSService::initialize(u32 messagePointer) {
log("FS::Initialize\n"); log("FS::Initialize\n");
mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 4, ResultCode::Success);
} }
// TODO: Figure out how this is different from Initialize // TODO: Figure out how this is different from Initialize
@ -165,10 +165,10 @@ void FSService::closeArchive(u32 messagePointer) {
if (object == nullptr) { if (object == nullptr) {
log("FSService::CloseArchive: Tried to close invalid archive %X\n", handle); log("FSService::CloseArchive: Tried to close invalid archive %X\n", handle);
mem.write32(messagePointer + 4, Result::Failure); mem.write32(messagePointer + 4, ResultCode::Failure);
} else { } else {
object->getData<ArchiveSession>()->isOpen = false; object->getData<ArchiveSession>()->isOpen = false;
mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 4, ResultCode::Success);
} }
} }
@ -183,11 +183,11 @@ void FSService::openArchive(u32 messagePointer) {
std::optional<Handle> handle = openArchiveHandle(archiveID, archivePath); std::optional<Handle> handle = openArchiveHandle(archiveID, archivePath);
if (handle.has_value()) { if (handle.has_value()) {
mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 4, ResultCode::Success);
mem.write64(messagePointer + 8, handle.value()); mem.write64(messagePointer + 8, handle.value());
} else { } else {
log("FS::OpenArchive: Failed to open archive with id = %d\n", archiveID); log("FS::OpenArchive: Failed to open archive with id = %d\n", archiveID);
mem.write32(messagePointer + 4, Result::Failure); mem.write32(messagePointer + 4, ResultCode::Failure);
} }
} }
@ -204,7 +204,7 @@ void FSService::openFile(u32 messagePointer) {
auto archiveObject = kernel.getObject(archiveHandle, KernelObjectType::Archive); auto archiveObject = kernel.getObject(archiveHandle, KernelObjectType::Archive);
if (archiveObject == nullptr) [[unlikely]] { if (archiveObject == nullptr) [[unlikely]] {
log("FS::OpenFile: Invalid archive handle %d\n", archiveHandle); log("FS::OpenFile: Invalid archive handle %d\n", archiveHandle);
mem.write32(messagePointer + 4, Result::Failure); mem.write32(messagePointer + 4, ResultCode::Failure);
return; return;
} }
@ -217,9 +217,9 @@ void FSService::openFile(u32 messagePointer) {
std::optional<Handle> handle = openFileHandle(archive, filePath, archivePath, perms); std::optional<Handle> handle = openFileHandle(archive, filePath, archivePath, perms);
if (!handle.has_value()) { if (!handle.has_value()) {
printf("OpenFile failed\n"); printf("OpenFile failed\n");
mem.write32(messagePointer + 4, Result::FileNotFound); mem.write32(messagePointer + 4, ResultCode::FileNotFound);
} else { } else {
mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 4, ResultCode::Success);
mem.write32(messagePointer + 8, 0x10); // "Move handle descriptor" mem.write32(messagePointer + 8, 0x10); // "Move handle descriptor"
mem.write32(messagePointer + 12, handle.value()); mem.write32(messagePointer + 12, handle.value());
} }
@ -235,19 +235,20 @@ void FSService::openDirectory(u32 messagePointer) {
KernelObject* archiveObject = kernel.getObject(archiveHandle, KernelObjectType::Archive); KernelObject* archiveObject = kernel.getObject(archiveHandle, KernelObjectType::Archive);
if (archiveObject == nullptr) [[unlikely]] { if (archiveObject == nullptr) [[unlikely]] {
log("FS::OpenDirectory: Invalid archive handle %d\n", archiveHandle); log("FS::OpenDirectory: Invalid archive handle %d\n", archiveHandle);
mem.write32(messagePointer + 4, Result::Failure); mem.write32(messagePointer + 4, ResultCode::Failure);
return; return;
} }
ArchiveBase* archive = archiveObject->getData<ArchiveSession>()->archive; ArchiveBase* archive = archiveObject->getData<ArchiveSession>()->archive;
const auto dirPath = readPath(pathType, pathPointer, pathSize); const auto dirPath = readPath(pathType, pathPointer, pathSize);
std::optional<Handle> dir = openDirectoryHandle(archive, dirPath); auto dir = openDirectoryHandle(archive, dirPath);
if (dir.has_value()) { if (dir.isOk()) {
mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 4, ResultCode::Success);
mem.write32(messagePointer + 12, dir.value()); mem.write32(messagePointer + 12, dir.unwrap());
} else { } else {
Helpers::panic("FS::OpenDirectory failed"); printf("FS::OpenDirectory failed\n");
mem.write32(messagePointer + 4, static_cast<u32>(dir.unwrapErr()));
} }
} }
@ -282,7 +283,7 @@ void FSService::openFileDirectly(u32 messagePointer) {
if (!handle.has_value()) { if (!handle.has_value()) {
Helpers::panic("OpenFileDirectly: Failed to open file with given path"); Helpers::panic("OpenFileDirectly: Failed to open file with given path");
} else { } else {
mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 4, ResultCode::Success);
mem.write32(messagePointer + 12, handle.value()); mem.write32(messagePointer + 12, handle.value());
} }
} }
@ -300,14 +301,14 @@ void FSService::createFile(u32 messagePointer) {
auto archiveObject = kernel.getObject(archiveHandle, KernelObjectType::Archive); auto archiveObject = kernel.getObject(archiveHandle, KernelObjectType::Archive);
if (archiveObject == nullptr) [[unlikely]] { if (archiveObject == nullptr) [[unlikely]] {
log("FS::OpenFile: Invalid archive handle %d\n", archiveHandle); log("FS::OpenFile: Invalid archive handle %d\n", archiveHandle);
mem.write32(messagePointer + 4, Result::Failure); mem.write32(messagePointer + 4, ResultCode::Failure);
return; return;
} }
ArchiveBase* archive = archiveObject->getData<ArchiveSession>()->archive; ArchiveBase* archive = archiveObject->getData<ArchiveSession>()->archive;
auto filePath = readPath(filePathType, filePathPointer, filePathSize); auto filePath = readPath(filePathType, filePathPointer, filePathSize);
CreateFileResult res = archive->createFile(filePath, size); FSResult res = archive->createFile(filePath, size);
mem.write32(messagePointer + 4, static_cast<u32>(res)); mem.write32(messagePointer + 4, static_cast<u32>(res));
} }
@ -321,14 +322,14 @@ void FSService::deleteFile(u32 messagePointer) {
auto archiveObject = kernel.getObject(archiveHandle, KernelObjectType::Archive); auto archiveObject = kernel.getObject(archiveHandle, KernelObjectType::Archive);
if (archiveObject == nullptr) [[unlikely]] { if (archiveObject == nullptr) [[unlikely]] {
log("FS::DeleteFile: Invalid archive handle %d\n", archiveHandle); log("FS::DeleteFile: Invalid archive handle %d\n", archiveHandle);
mem.write32(messagePointer + 4, Result::Failure); mem.write32(messagePointer + 4, ResultCode::Failure);
return; return;
} }
ArchiveBase* archive = archiveObject->getData<ArchiveSession>()->archive; ArchiveBase* archive = archiveObject->getData<ArchiveSession>()->archive;
auto filePath = readPath(filePathType, filePathPointer, filePathSize); auto filePath = readPath(filePathType, filePathPointer, filePathSize);
DeleteFileResult res = archive->deleteFile(filePath); FSResult res = archive->deleteFile(filePath);
mem.write32(messagePointer + 4, static_cast<u32>(res)); mem.write32(messagePointer + 4, static_cast<u32>(res));
} }
@ -347,7 +348,7 @@ void FSService::getFormatInfo(u32 messagePointer) {
} }
ArchiveBase::FormatInfo info = archive->getFormatInfo(path); ArchiveBase::FormatInfo info = archive->getFormatInfo(path);
mem.write32(messagePointer + 4, Result::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);
@ -357,7 +358,7 @@ void FSService::getFormatInfo(u32 messagePointer) {
void FSService::getPriority(u32 messagePointer) { void FSService::getPriority(u32 messagePointer) {
log("FS::GetPriority\n"); log("FS::GetPriority\n");
mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 4, ResultCode::Success);
mem.write32(messagePointer + 8, priority); mem.write32(messagePointer + 8, priority);
} }
@ -365,12 +366,12 @@ void FSService::setPriority(u32 messagePointer) {
const u32 value = mem.read32(messagePointer + 4); const u32 value = mem.read32(messagePointer + 4);
log("FS::SetPriority (priority = %d)\n", value); log("FS::SetPriority (priority = %d)\n", value);
mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 4, ResultCode::Success);
priority = value; priority = value;
} }
void FSService::isSdmcDetected(u32 messagePointer) { void FSService::isSdmcDetected(u32 messagePointer) {
log("FS::IsSdmcDetected\n"); log("FS::IsSdmcDetected\n");
mem.write32(messagePointer + 4, Result::Success); mem.write32(messagePointer + 4, ResultCode::Success);
mem.write32(messagePointer + 8, 0); // Whether SD is detected. For now we emulate a 3DS without an SD. mem.write32(messagePointer + 8, 0); // Whether SD is detected. For now we emulate a 3DS without an SD.
} }

View file

@ -49,7 +49,9 @@ types::Err<CleanE> Err(E&& val) {
return types::Err<CleanE>(std::forward<E>(val)); return types::Err<CleanE>(std::forward<E>(val));
} }
namespace Rust {
template<typename T, typename E> struct Result; template<typename T, typename E> struct Result;
}
namespace details { namespace details {
@ -84,7 +86,7 @@ template<typename R>
struct ResultOkType { typedef typename std::decay<R>::type type; }; struct ResultOkType { typedef typename std::decay<R>::type type; };
template<typename T, typename E> template<typename T, typename E>
struct ResultOkType<Result<T, E>> { struct ResultOkType<Rust::Result<T, E>> {
typedef T type; typedef T type;
}; };
@ -92,13 +94,13 @@ template<typename R>
struct ResultErrType { typedef R type; }; struct ResultErrType { typedef R type; };
template<typename T, typename E> template<typename T, typename E>
struct ResultErrType<Result<T, E>> { struct ResultErrType<Rust::Result<T, E>> {
typedef typename std::remove_reference<E>::type type; typedef typename std::remove_reference<E>::type type;
}; };
template<typename R> struct IsResult : public std::false_type { }; template<typename R> struct IsResult : public std::false_type { };
template<typename T, typename E> template<typename T, typename E>
struct IsResult<Result<T, E>> : public std::true_type { }; struct IsResult<Rust::Result<T, E>> : public std::true_type { };
namespace ok { namespace ok {
@ -120,7 +122,7 @@ struct Map<Ret (Arg)> {
"Can not map a callback returning a Result, use andThen instead"); "Can not map a callback returning a Result, use andThen instead");
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
static Result<Ret, E> map(const Result<T, E>& result, Func func) { static Rust::Result<Ret, E> map(const Rust::Result<T, E>& result, Func func) {
static_assert( static_assert(
std::is_same<T, Arg>::value || std::is_same<T, Arg>::value ||
@ -141,7 +143,7 @@ template<typename Arg>
struct Map<void (Arg)> { struct Map<void (Arg)> {
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
static Result<void, E> map(const Result<T, E>& result, Func func) { static Rust::Result<void, E> map(const Rust::Result<T, E>& result, Func func) {
if (result.isOk()) { if (result.isOk()) {
func(result.storage().template get<T>()); func(result.storage().template get<T>());
@ -157,7 +159,7 @@ template<typename Ret>
struct Map<Ret (void)> { struct Map<Ret (void)> {
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
static Result<Ret, E> map(const Result<T, E>& result, Func func) { static Rust::Result<Ret, E> map(const Rust::Result<T, E>& result, Func func) {
static_assert(std::is_same<T, void>::value, static_assert(std::is_same<T, void>::value,
"Can not map a void callback on a non-void Result"); "Can not map a void callback on a non-void Result");
@ -175,7 +177,7 @@ template<>
struct Map<void (void)> { struct Map<void (void)> {
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
static Result<void, E> map(const Result<T, E>& result, Func func) { static Rust::Result<void, E> map(const Rust::Result<T, E>& result, Func func) {
static_assert(std::is_same<T, void>::value, static_assert(std::is_same<T, void>::value,
"Can not map a void callback on a non-void Result"); "Can not map a void callback on a non-void Result");
@ -190,10 +192,10 @@ struct Map<void (void)> {
// General specialization for a callback returning a Result // General specialization for a callback returning a Result
template<typename U, typename E, typename Arg> template<typename U, typename E, typename Arg>
struct Map<Result<U, E> (Arg)> { struct Map<Rust::Result<U, E> (Arg)> {
template<typename T, typename Func> template<typename T, typename Func>
static Result<U, E> map(const Result<T, E>& result, Func func) { static Rust::Result<U, E> map(const Rust::Result<T, E>& result, Func func) {
static_assert( static_assert(
std::is_same<T, Arg>::value || std::is_same<T, Arg>::value ||
std::is_convertible<T, Arg>::value, std::is_convertible<T, Arg>::value,
@ -210,10 +212,10 @@ struct Map<Result<U, E> (Arg)> {
// Specialization for a void callback returning a Result // Specialization for a void callback returning a Result
template<typename U, typename E> template<typename U, typename E>
struct Map<Result<U, E> (void)> { struct Map<Rust::Result<U, E> (void)> {
template<typename T, typename Func> template<typename T, typename Func>
static Result<U, E> map(const Result<T, E>& result, Func func) { static Rust::Result<U, E> map(const Rust::Result<T, E>& result, Func func) {
static_assert(std::is_same<T, void>::value, "Can not call a void-callback on a non-void Result"); static_assert(std::is_same<T, void>::value, "Can not call a void-callback on a non-void Result");
if (result.isOk()) { if (result.isOk()) {
@ -258,7 +260,7 @@ struct Map<Ret (Cls::*)(Arg) const> {
"Can not map a callback returning a Result, use orElse instead"); "Can not map a callback returning a Result, use orElse instead");
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
static Result<T, Ret> map(const Result<T, E>& result, Func func) { static Rust::Result<T, Ret> map(const Rust::Result<T, E>& result, Func func) {
if (result.isErr()) { if (result.isErr()) {
auto res = func(result.storage().template get<E>()); auto res = func(result.storage().template get<E>());
return types::Err<Ret>(res); return types::Err<Ret>(res);
@ -268,7 +270,7 @@ struct Map<Ret (Cls::*)(Arg) const> {
} }
template<typename E, typename Func> template<typename E, typename Func>
static Result<void, Ret> map(const Result<void, E>& result, Func func) { static Rust::Result<void, Ret> map(const Rust::Result<void, E>& result, Func func) {
if (result.isErr()) { if (result.isErr()) {
auto res = func(result.storage().template get<E>()); auto res = func(result.storage().template get<E>());
return types::Err<Ret>(res); return types::Err<Ret>(res);
@ -307,7 +309,7 @@ namespace impl {
"then() should not return anything, use map() instead"); "then() should not return anything, use map() instead");
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
static Result<T, E> then(const Result<T, E>& result, Func func) { static Rust::Result<T, E> then(const Rust::Result<T, E>& result, Func func) {
if (result.isOk()) { if (result.isOk()) {
func(result.storage().template get<T>()); func(result.storage().template get<T>());
} }
@ -321,7 +323,7 @@ namespace impl {
"then() should not return anything, use map() instead"); "then() should not return anything, use map() instead");
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
static Result<T, E> then(const Result<T, E>& result, Func func) { static Rust::Result<T, E> then(const Rust::Result<T, E>& result, Func func) {
static_assert(std::is_same<T, void>::value, "Can not call a void-callback on a non-void Result"); static_assert(std::is_same<T, void>::value, "Can not call a void-callback on a non-void Result");
if (result.isOk()) { if (result.isOk()) {
@ -365,10 +367,10 @@ namespace impl {
struct Else<Ret (Cls::*)(Args...) const> : public Else<Ret (Args...)> { }; struct Else<Ret (Cls::*)(Args...) const> : public Else<Ret (Args...)> { };
template<typename T, typename F, typename Arg> template<typename T, typename F, typename Arg>
struct Else<Result<T, F> (Arg)> { struct Else<Rust::Result<T, F> (Arg)> {
template<typename E, typename Func> template<typename E, typename Func>
static Result<T, F> orElse(const Result<T, E>& result, Func func) { static Rust::Result<T, F> orElse(const Rust::Result<T, E>& result, Func func) {
static_assert( static_assert(
std::is_same<E, Arg>::value || std::is_same<E, Arg>::value ||
std::is_convertible<E, Arg>::value, std::is_convertible<E, Arg>::value,
@ -383,7 +385,7 @@ namespace impl {
} }
template<typename E, typename Func> template<typename E, typename Func>
static Result<void, F> orElse(const Result<void, E>& result, Func func) { static Rust::Result<void, F> orElse(const Rust::Result<void, E>& result, Func func) {
if (result.isErr()) { if (result.isErr()) {
auto res = func(result.storage().template get<E>()); auto res = func(result.storage().template get<E>());
return res; return res;
@ -395,10 +397,10 @@ namespace impl {
}; };
template<typename T, typename F> template<typename T, typename F>
struct Else<Result<T, F> (void)> { struct Else<Rust::Result<T, F> (void)> {
template<typename E, typename Func> template<typename E, typename Func>
static Result<T, F> orElse(const Result<T, E>& result, Func func) { static Rust::Result<T, F> orElse(const Rust::Result<T, E>& result, Func func) {
static_assert(std::is_same<T, void>::value, static_assert(std::is_same<T, void>::value,
"Can not call a void-callback on a non-void Result"); "Can not call a void-callback on a non-void Result");
@ -411,7 +413,7 @@ namespace impl {
} }
template<typename E, typename Func> template<typename E, typename Func>
static Result<void, F> orElse(const Result<void, E>& result, Func func) { static Rust::Result<void, F> orElse(const Rust::Result<void, E>& result, Func func) {
if (result.isErr()) { if (result.isErr()) {
auto res = func(); auto res = func();
return res; return res;
@ -457,7 +459,7 @@ namespace impl {
struct Wise<Ret (Arg)> { struct Wise<Ret (Arg)> {
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
static Result<T, E> otherwise(const Result<T, E>& result, Func func) { static Rust::Result<T, E> otherwise(const Rust::Result<T, E>& result, Func func) {
static_assert( static_assert(
std::is_same<E, Arg>::value || std::is_same<E, Arg>::value ||
std::is_convertible<E, Arg>::value, std::is_convertible<E, Arg>::value,
@ -492,13 +494,13 @@ struct Wise<Ret (Cls::*)(Args...) const> : public impl::Wise<Ret (Args...)> { };
template<typename T, typename E, typename Func, template<typename T, typename E, typename Func,
typename Ret = typename Ret =
Result< Rust::Result<
typename details::ResultOkType< typename details::ResultOkType<
typename details::result_of<Func>::type typename details::result_of<Func>::type
>::type, >::type,
E> E>
> >
Ret map(const Result<T, E>& result, Func func) { Ret map(const Rust::Result<T, E>& result, Func func) {
return ok::Map<Func>::map(result, func); return ok::Map<Func>::map(result, func);
} }
@ -510,29 +512,29 @@ template<typename T, typename E, typename Func,
>::type >::type
> >
> >
Ret mapError(const Result<T, E>& result, Func func) { Ret mapError(const Rust::Result<T, E>& result, Func func) {
return err::Map<Func>::map(result, func); return err::Map<Func>::map(result, func);
} }
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
Result<T, E> then(const Result<T, E>& result, Func func) { Rust::Result<T, E> then(const Rust::Result<T, E>& result, Func func) {
return And::Then<Func>::then(result, func); return And::Then<Func>::then(result, func);
} }
template<typename T, typename E, typename Func> template<typename T, typename E, typename Func>
Result<T, E> otherwise(const Result<T, E>& result, Func func) { Rust::Result<T, E> otherwise(const Rust::Result<T, E>& result, Func func) {
return Other::Wise<Func>::otherwise(result, func); return Other::Wise<Func>::otherwise(result, func);
} }
template<typename T, typename E, typename Func, template<typename T, typename E, typename Func,
typename Ret = typename Ret =
Result<T, Rust::Result<T,
typename details::ResultErrType< typename details::ResultErrType<
typename details::result_of<Func>::type typename details::result_of<Func>::type
>::type >::type
> >
> >
Ret orElse(const Result<T, E>& result, Func func) { Ret orElse(const Rust::Result<T, E>& result, Func func) {
return Or::Else<Func>::orElse(result, func); return Or::Else<Func>::orElse(result, func);
} }
@ -700,6 +702,7 @@ typename std::enable_if<
} // namespace rpog } // namespace rpog
namespace Rust {
template<typename T, typename E> template<typename T, typename E>
struct Result { struct Result {
@ -860,7 +863,7 @@ private:
}; };
template<typename T, typename E> template<typename T, typename E>
bool operator==(const Result<T, E>& lhs, const Result<T, E>& rhs) { bool operator==(const Rust::Result<T, E>& lhs, const Rust::Result<T, E>& rhs) {
static_assert(rpog::EqualityComparable<T>::value, "T must be EqualityComparable for Result to be comparable"); static_assert(rpog::EqualityComparable<T>::value, "T must be EqualityComparable for Result to be comparable");
static_assert(rpog::EqualityComparable<E>::value, "E must be EqualityComparable for Result to be comparable"); static_assert(rpog::EqualityComparable<E>::value, "E must be EqualityComparable for Result to be comparable");
@ -873,7 +876,7 @@ bool operator==(const Result<T, E>& lhs, const Result<T, E>& rhs) {
} }
template<typename T, typename E> template<typename T, typename E>
bool operator==(const Result<T, E>& lhs, types::Ok<T> ok) { bool operator==(const Rust::Result<T, E>& lhs, types::Ok<T> ok) {
static_assert(rpog::EqualityComparable<T>::value, "T must be EqualityComparable for Result to be comparable"); static_assert(rpog::EqualityComparable<T>::value, "T must be EqualityComparable for Result to be comparable");
if (!lhs.isOk()) return false; if (!lhs.isOk()) return false;
@ -882,17 +885,18 @@ bool operator==(const Result<T, E>& lhs, types::Ok<T> ok) {
} }
template<typename E> template<typename E>
bool operator==(const Result<void, E>& lhs, types::Ok<void>) { bool operator==(const Rust::Result<void, E>& lhs, types::Ok<void>) {
return lhs.isOk(); return lhs.isOk();
} }
template<typename T, typename E> template<typename T, typename E>
bool operator==(const Result<T, E>& lhs, types::Err<E> err) { bool operator==(const Rust::Result<T, E>& lhs, types::Err<E> err) {
static_assert(rpog::EqualityComparable<E>::value, "E must be EqualityComparable for Result to be comparable"); static_assert(rpog::EqualityComparable<E>::value, "E must be EqualityComparable for Result to be comparable");
if (!lhs.isErr()) return false; if (!lhs.isErr()) return false;
return lhs.storage().template get<E>() == err.val; return lhs.storage().template get<E>() == err.val;
} }
} // end namespace Rust
#define TRY(...) \ #define TRY(...) \
({ \ ({ \