Results: Implement namespaced, type-safe results.

Because I was working on multiple things at once, this commit also:
- Adds wrappers for/linker flags to wrap CXX exceptions to make them
  abort. This saves ~0x8000 of memory in every system module.
- Broadly replaces lines of the pattern if (cond) { return ResultX; }
  with R_UNLESS(!cond, ResultX());.
- Reworks the R_TRY_CATCH macros (and the result macros in general).
This commit is contained in:
Michael Scire
2019-10-24 01:40:44 -07:00
committed by SciresM
parent 15773e4755
commit 4059dc6187
169 changed files with 2172 additions and 1868 deletions

View File

@@ -48,7 +48,7 @@ Result FsDirUtils::CopyFile(IFileSystem *dst_fs, IFileSystem *src_fs, const FsPa
offset += read_size;
}
return ResultSuccess;
return ResultSuccess();
}
Result FsDirUtils::CopyDirectoryRecursively(IFileSystem *dst_fs, IFileSystem *src_fs, const FsPath &dst_path, const FsPath &src_path, void *work_buf, size_t work_buf_size) {
@@ -75,7 +75,7 @@ Result FsDirUtils::CopyDirectoryRecursively(IFileSystem *dst_fs, IFileSystem *sr
}
p[1] = 0;
return ResultSuccess;
return ResultSuccess();
},
[&](const FsPath &path, const FsDirectoryEntry *dir_ent) -> Result { /* On File */
/* Just copy the file to the new fs. */
@@ -113,5 +113,5 @@ Result FsDirUtils::EnsureDirectoryExists(IFileSystem *fs, const FsPath &path) {
}
} R_END_TRY_CATCH;
return ResultSuccess;
return ResultSuccess();
}

View File

@@ -71,7 +71,7 @@ class FsDirUtils {
work_path.str[parent_len] = 0;
}
return ResultSuccess;
return ResultSuccess();
}
public:
@@ -137,6 +137,6 @@ class FsDirUtils {
} R_END_TRY_CATCH;
}
return ResultSuccess;
return ResultSuccess();
}
};

View File

@@ -54,7 +54,7 @@ Result DirectoryRedirectionFileSystem::Initialize(const char *before, const char
this->before_dir_len = strlen(this->before_dir);
this->after_dir_len = strlen(this->after_dir);
return ResultSuccess;
return ResultSuccess();
}
Result DirectoryRedirectionFileSystem::GetFullPath(char *out, size_t out_size, const char *relative_path) {
@@ -79,7 +79,7 @@ Result DirectoryRedirectionFileSystem::GetFullPath(char *out, size_t out_size, c
}
std::strncpy(out, this->after_dir, out_size);
out[out_size - 1] = 0;
return ResultSuccess;
return ResultSuccess();
} else {
return FsPathUtils::Normalize(out, out_size, relative_path, nullptr);
}

View File

@@ -114,7 +114,7 @@ Result DirectorySaveDataFileSystem::AllocateWorkBuffer(void **out_buf, size_t *o
if (buf != nullptr) {
*out_buf = buf;
*out_size = try_size;
return ResultSuccess;
return ResultSuccess();
}
/* Divide size by two. */
@@ -162,7 +162,7 @@ Result DirectorySaveDataFileSystem::CopySaveFromProxy() {
R_TRY(FsDirUtils::CopyDirectoryRecursively(this, this->proxy_save_fs.get(), FsPathUtils::RootPath, FsPathUtils::RootPath, work_buf, work_buf_size));
return this->Commit();
}
return ResultSuccess;
return ResultSuccess();
}
/* ================================================================================================ */
@@ -258,7 +258,7 @@ Result DirectorySaveDataFileSystem::OpenFileImpl(std::unique_ptr<IFile> &out_fil
this->open_writable_files++;
}
return ResultSuccess;
return ResultSuccess();
}
Result DirectorySaveDataFileSystem::OpenDirectoryImpl(std::unique_ptr<IDirectory> &out_dir, const FsPath &path, DirectoryOpenMode mode) {
@@ -303,7 +303,7 @@ Result DirectorySaveDataFileSystem::CommitImpl() {
R_TRY(FsDirUtils::RetryUntilTargetNotLocked(std::move(RenameSynchDir)));
/* TODO: Should I call this->fs->Commit()? Nintendo does not. */
return ResultSuccess;
return ResultSuccess();
}
Result DirectorySaveDataFileSystem::GetFreeSpaceSizeImpl(uint64_t *out, const FsPath &path) {

View File

@@ -25,14 +25,14 @@ Result FileStorage::UpdateSize() {
if (this->size == InvalidSize) {
return this->file->GetSize(&this->size);
}
return ResultSuccess;
return ResultSuccess();
}
Result FileStorage::Read(void *buffer, size_t size, u64 offset) {
u64 read_size;
if (size == 0) {
return ResultSuccess;
return ResultSuccess();
}
if (buffer == nullptr) {
return ResultFsNullptrArgument;
@@ -47,12 +47,12 @@ Result FileStorage::Read(void *buffer, size_t size, u64 offset) {
if (read_size != size && read_size) {
return this->Read(reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(buffer) + read_size), size - read_size, offset + read_size);
}
return ResultSuccess;
return ResultSuccess();
}
Result FileStorage::Write(void *buffer, size_t size, u64 offset) {
if (size == 0) {
return ResultSuccess;
return ResultSuccess();
}
if (buffer == nullptr) {
return ResultFsNullptrArgument;
@@ -72,7 +72,7 @@ Result FileStorage::Flush() {
Result FileStorage::GetSize(u64 *out_size) {
R_TRY(this->UpdateSize());
*out_size = this->size;
return ResultSuccess;
return ResultSuccess();
}
Result FileStorage::SetSize(u64 size) {
@@ -92,7 +92,7 @@ Result FileStorage::OperateRange(FsOperationId operation_type, u64 offset, u64 s
/* N checks for size == sizeof(*out_range_info) here, but that's because their wrapper api is bad. */
std::memset(out_range_info, 0, sizeof(*out_range_info));
}
return ResultSuccess;
return ResultSuccess();
}
R_TRY(this->UpdateSize());
/* N checks for positivity + signed overflow on offset/size here, but we're using unsigned types... */

View File

@@ -28,7 +28,7 @@ class IDirectory {
}
if (max_entries == 0) {
*out_count = 0;
return ResultSuccess;
return ResultSuccess();
}
if (out_entries == nullptr) {
return ResultFsNullptrArgument;
@@ -107,7 +107,7 @@ class ProxyDirectory : public IDirectory {
R_TRY(fsDirRead(this->base_dir.get(), 0, &count, max_entries, out_entries));
*out_count = count;
return ResultSuccess;
return ResultSuccess();
}
virtual Result GetEntryCountImpl(uint64_t *count) {
return fsDirGetEntryCount(this->base_dir.get(), count);

View File

@@ -31,7 +31,7 @@ class IFile {
}
if (size == 0) {
*out = 0;
return ResultSuccess;
return ResultSuccess();
}
if (buffer == nullptr) {
return ResultFsNullptrArgument;
@@ -56,7 +56,7 @@ class IFile {
Result Write(uint64_t offset, void *buffer, uint64_t size, uint32_t flags) {
if (size == 0) {
return ResultSuccess;
return ResultSuccess();
}
if (buffer == nullptr) {
return ResultFsNullptrArgument;
@@ -66,7 +66,7 @@ class IFile {
Result Write(uint64_t offset, void *buffer, uint64_t size, bool flush = false) {
if (size == 0) {
return ResultSuccess;
return ResultSuccess();
}
if (buffer == nullptr) {
return ResultFsNullptrArgument;
@@ -175,7 +175,7 @@ class ProxyFile : public IFile {
R_TRY(fsFileRead(this->base_file.get(), offset, buffer, size, FS_READOPTION_NONE, &out_sz));
*out = out_sz;
return ResultSuccess;
return ResultSuccess();
}
virtual Result GetSizeImpl(u64 *out) override {
return fsFileGetSize(this->base_file.get(), out);

View File

@@ -276,7 +276,7 @@ class IFileSystemInterface : public IServiceObject {
R_TRY(this->base_fs->GetEntryType(&type, path));
out_type.SetValue(type);
return ResultSuccess;
return ResultSuccess();
}
virtual Result OpenFile(Out<std::shared_ptr<IFileInterface>> out_intf, InPointer<char> in_path, uint32_t mode) final {
@@ -287,7 +287,7 @@ class IFileSystemInterface : public IServiceObject {
R_TRY(this->base_fs->OpenFile(out_file, path, static_cast<OpenMode>(mode)));
out_intf.SetValue(std::make_shared<IFileInterface>(std::move(out_file)));
return ResultSuccess;
return ResultSuccess();
}
virtual Result OpenDirectory(Out<std::shared_ptr<IDirectoryInterface>> out_intf, InPointer<char> in_path, uint32_t mode) final {
@@ -298,7 +298,7 @@ class IFileSystemInterface : public IServiceObject {
R_TRY(this->base_fs->OpenDirectory(out_dir, path, static_cast<DirectoryOpenMode>(mode)));
out_intf.SetValue(std::make_shared<IDirectoryInterface>(std::move(out_dir)));
return ResultSuccess;
return ResultSuccess();
}
virtual Result Commit() final {
@@ -419,14 +419,14 @@ class ProxyFileSystem : public IFileSystem {
R_TRY(fsFsGetEntryType(this->base_fs.get(), path.str, &type));
*out = static_cast<DirectoryEntryType>(static_cast<u32>(type));
return ResultSuccess;
return ResultSuccess();
}
virtual Result OpenFileImpl(std::unique_ptr<IFile> &out_file, const FsPath &path, OpenMode mode) {
FsFile f;
R_TRY(fsFsOpenFile(this->base_fs.get(), path.str, static_cast<int>(mode), &f));
out_file = std::make_unique<ProxyFile>(f);
return ResultSuccess;
return ResultSuccess();
}
virtual Result OpenDirectoryImpl(std::unique_ptr<IDirectory> &out_dir, const FsPath &path, DirectoryOpenMode mode) {
@@ -434,7 +434,7 @@ class ProxyFileSystem : public IFileSystem {
R_TRY(fsFsOpenDirectory(this->base_fs.get(), path.str, static_cast<int>(mode), &d));
out_dir = std::make_unique<ProxyDirectory>(d);
return ResultSuccess;
return ResultSuccess();
}
virtual Result CommitImpl() {

View File

@@ -102,7 +102,7 @@ class IROStorage : public IStorage {
return ResultFsUnsupportedOperation;
};
virtual Result Flush() final {
return ResultSuccess;
return ResultSuccess();
};
virtual Result SetSize(u64 size) final {
(void)(size);

View File

@@ -28,7 +28,7 @@ Result FsPathUtils::VerifyPath(const char *path, size_t max_path_len, size_t max
const char c = *(cur++);
/* If terminated, we're done. */
if (c == 0) {
return ResultSuccess;
return ResultSuccess();
}
/* TODO: Nintendo converts the path from utf-8 to utf-32, one character at a time. */
@@ -110,7 +110,7 @@ Result FsPathUtils::IsNormalized(bool *out, const char *path) {
/* It is unclear why first separator and separator are separate states... */
if (c == '/') {
*out = false;
return ResultSuccess;
return ResultSuccess();
} else if (c == '.') {
state = PathState::CurrentDir;
} else {
@@ -120,7 +120,7 @@ Result FsPathUtils::IsNormalized(bool *out, const char *path) {
case PathState::CurrentDir:
if (c == '/') {
*out = false;
return ResultSuccess;
return ResultSuccess();
} else if (c == '.') {
state = PathState::ParentDir;
} else {
@@ -130,7 +130,7 @@ Result FsPathUtils::IsNormalized(bool *out, const char *path) {
case PathState::ParentDir:
if (c == '/') {
*out = false;
return ResultSuccess;
return ResultSuccess();
} else {
state = PathState::Normal;
}
@@ -138,7 +138,7 @@ Result FsPathUtils::IsNormalized(bool *out, const char *path) {
case PathState::WindowsDriveLetter:
if (c == ':') {
*out = true;
return ResultSuccess;
return ResultSuccess();
} else {
return ResultFsInvalidPathFormat;
}
@@ -161,7 +161,7 @@ Result FsPathUtils::IsNormalized(bool *out, const char *path) {
break;
}
return ResultSuccess;
return ResultSuccess();
}
Result FsPathUtils::Normalize(char *out, size_t max_out_size, const char *src, size_t *out_len) {
@@ -263,5 +263,5 @@ Result FsPathUtils::Normalize(char *out, size_t max_out_size, const char *src, s
R_ASSERT(FsPathUtils::IsNormalized(&normalized, out));
STS_ASSERT(normalized);
return ResultSuccess;
return ResultSuccess();
}

View File

@@ -53,7 +53,7 @@ Result FsSaveUtils::GetSaveDataSpaceIdString(const char **out_str, u8 space_id)
*out_str = str;
}
return ResultSuccess;
return ResultSuccess();
}
Result FsSaveUtils::GetSaveDataTypeString(const char **out_str, u8 save_data_type) {
@@ -95,7 +95,7 @@ Result FsSaveUtils::GetSaveDataTypeString(const char **out_str, u8 save_data_typ
*out_str = str;
}
return ResultSuccess;
return ResultSuccess();
}
Result FsSaveUtils::GetSaveDataDirectoryPath(FsPath &out_path, u8 space_id, u8 save_data_type, u64 title_id, u128 user_id, u64 save_id) {
@@ -121,5 +121,5 @@ Result FsSaveUtils::GetSaveDataDirectoryPath(FsPath &out_path, u8 space_id, u8 s
return ResultFsTooLongPath;
}
return ResultSuccess;
return ResultSuccess();
}

View File

@@ -49,7 +49,7 @@ Result SubDirectoryFileSystem::Initialize(const char *bp) {
std::strncpy(this->base_path, normal_path, this->base_path_len);
this->base_path[this->base_path_len-1] = 0;
return ResultSuccess;
return ResultSuccess();
}
Result SubDirectoryFileSystem::GetFullPath(char *out, size_t out_size, const char *relative_path) {

View File

@@ -67,7 +67,7 @@ Result Boot0Storage::Write(void *_buffer, size_t size, u64 offset) {
if (offset < EksEnd) {
if (offset + size < EksEnd) {
/* Ignore writes falling strictly within the region. */
return ResultSuccess;
return ResultSuccess();
} else {
/* Only write past the end of the keyblob region. */
buffer = buffer + (EksEnd - offset);
@@ -80,7 +80,7 @@ Result Boot0Storage::Write(void *_buffer, size_t size, u64 offset) {
}
if (size == 0) {
return ResultSuccess;
return ResultSuccess();
}
/* We care about protecting autorcm from NS. */

View File

@@ -72,7 +72,7 @@ class SectoredProxyStorage : public ProxyStorage {
}
}
return ResultSuccess;
return ResultSuccess();
}
virtual Result Write(void *_buffer, size_t size, u64 offset) override {
@@ -112,7 +112,7 @@ class SectoredProxyStorage : public ProxyStorage {
}
}
return ResultSuccess;
return ResultSuccess();
}
};

View File

@@ -48,7 +48,7 @@ LayeredRomFS::LayeredRomFS(std::shared_ptr<IROStorage> s_r, std::shared_ptr<IROS
Result LayeredRomFS::Read(void *buffer, size_t size, u64 offset) {
/* Size zero reads should always succeed. */
if (size == 0) {
return ResultSuccess;
return ResultSuccess();
}
/* Validate size. */
@@ -136,16 +136,16 @@ Result LayeredRomFS::Read(void *buffer, size_t size, u64 offset) {
}
}
return ResultSuccess;
return ResultSuccess();
}
Result LayeredRomFS::GetSize(u64 *out_size) {
*out_size = (*this->p_source_infos)[this->p_source_infos->size() - 1].virtual_offset + (*this->p_source_infos)[this->p_source_infos->size() - 1].size;
return ResultSuccess;
return ResultSuccess();
}
Result LayeredRomFS::OperateRange(FsOperationId operation_type, u64 offset, u64 size, FsRangeInfo *out_range_info) {
/* TODO: How should I implement this for a virtual romfs? */
if (operation_type == FsOperationId_QueryRange) {
*out_range_info = {0};
}
return ResultSuccess;
return ResultSuccess();
}

View File

@@ -42,7 +42,7 @@ class RomFileStorage : public IROStorage {
if (out_sz != size && out_sz) {
R_TRY(this->Read((void *)((uintptr_t)buffer + out_sz), size - out_sz, offset + out_sz));
}
return ResultSuccess;
return ResultSuccess();
};
Result GetSize(u64 *out_size) override {
return fsFileGetSize(this->base_file, out_size);

View File

@@ -92,7 +92,7 @@ Result FsMitmService::OpenHblWebContentFileSystem(Out<std::shared_ptr<IFileSyste
out_fs.ChangeObjectId(sd_fs.s.object_id);
}
return ResultSuccess;
return ResultSuccess();
}
Result FsMitmService::OpenFileSystemWithPatch(Out<std::shared_ptr<IFileSystemInterface>> out_fs, u64 title_id, u32 filesystem_type) {
@@ -163,7 +163,7 @@ Result FsMitmService::OpenSdCardFileSystem(Out<std::shared_ptr<IFileSystemInterf
out_fs.ChangeObjectId(sd_fs.s.object_id);
}
return ResultSuccess;
return ResultSuccess();
}
Result FsMitmService::OpenSaveDataFileSystem(Out<std::shared_ptr<IFileSystemInterface>> out_fs, u8 space_id, FsSave save_struct) {
@@ -222,7 +222,7 @@ Result FsMitmService::OpenSaveDataFileSystem(Out<std::shared_ptr<IFileSystemInte
out_fs.ChangeObjectId(sd_fs.s.object_id);
}
return ResultSuccess;
return ResultSuccess();
}
}
@@ -272,7 +272,7 @@ Result FsMitmService::OpenBisStorage(Out<std::shared_ptr<IStorageInterface>> out
out_storage.ChangeObjectId(bis_storage.s.object_id);
}
return ResultSuccess;
return ResultSuccess();
}
/* Add redirection for RomFS to the SD card. */
@@ -299,7 +299,7 @@ Result FsMitmService::OpenDataStorageByCurrentProcess(Out<std::shared_ptr<IStora
out_storage.ChangeObjectId(s.s.object_id);
}
out_storage.SetValue(std::move(cached_storage));
return ResultSuccess;
return ResultSuccess();
}
}
@@ -326,7 +326,7 @@ Result FsMitmService::OpenDataStorageByCurrentProcess(Out<std::shared_ptr<IStora
}
}
return ResultSuccess;
return ResultSuccess();
}
/* Add redirection for System Data Archives to the SD card. */
@@ -355,7 +355,7 @@ Result FsMitmService::OpenDataStorageByDataId(Out<std::shared_ptr<IStorageInterf
out_storage.ChangeObjectId(s.s.object_id);
}
out_storage.SetValue(std::move(cached_storage));
return ResultSuccess;
return ResultSuccess();
}
}
@@ -382,5 +382,5 @@ Result FsMitmService::OpenDataStorageByDataId(Out<std::shared_ptr<IStorageInterf
}
}
return ResultSuccess;
return ResultSuccess();
}