From 9fc43120b86ecabf56f5e62b589121c73bb99206 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Mon, 24 Feb 2020 23:53:39 +1100 Subject: [PATCH] Adopted R_UNLESS and R_CONVERT --- stratosphere/ncm/source/impl/lr_manager.cpp | 4 +- .../ncm/source/impl/ncm_content_manager.cpp | 166 +++++------------- .../source/impl/ncm_placeholder_accessor.cpp | 28 +-- .../lr_addoncontentlocationresolver.cpp | 20 +-- .../ncm/source/lr_contentlocationresolver.cpp | 39 ++-- .../lr_redirectonlylocationresolver.cpp | 36 ++-- .../source/lr_registeredlocationresolver.cpp | 10 +- .../ncm/source/ncm_contentmetadatabase.cpp | 72 ++------ .../ncm/source/ncm_contentstorage.cpp | 79 ++------- stratosphere/ncm/source/ncm_fs.cpp | 118 ++++--------- stratosphere/ncm/source/ncm_fs.hpp | 8 +- .../ncm/source/ncm_icontentmetadatabase.hpp | 4 +- .../ncm/source/ncm_icontentstorage.hpp | 4 +- .../ncm/source/ncm_readonlycontentstorage.cpp | 11 +- stratosphere/ncm/source/ncm_utils.cpp | 5 +- 15 files changed, 163 insertions(+), 441 deletions(-) diff --git a/stratosphere/ncm/source/impl/lr_manager.cpp b/stratosphere/ncm/source/impl/lr_manager.cpp index 7b293dd37..04aef7a93 100644 --- a/stratosphere/ncm/source/impl/lr_manager.cpp +++ b/stratosphere/ncm/source/impl/lr_manager.cpp @@ -66,9 +66,7 @@ namespace ams::lr::impl { std::scoped_lock lk(g_mutex); auto resolver = g_location_resolvers.Find(storage_id); - if (!resolver) { - return ResultUnknownStorageId(); - } + R_UNLESS(resolver, ResultUnknownStorageId()); if (storage_id != ncm::StorageId::Host) { (*resolver)->Refresh(); diff --git a/stratosphere/ncm/source/impl/ncm_content_manager.cpp b/stratosphere/ncm/source/impl/ncm_content_manager.cpp index 54a2c0837..a275e14e8 100644 --- a/stratosphere/ncm/source/impl/ncm_content_manager.cpp +++ b/stratosphere/ncm/source/impl/ncm_content_manager.cpp @@ -148,9 +148,7 @@ namespace ams::ncm::impl { std::scoped_lock lk(g_mutex); /* Already initialized. */ - if (g_initialized) { - return ResultSuccess(); - } + R_UNLESS(!g_initialized, ResultSuccess()); size_t cur_storage_index = g_num_content_storage_entries; @@ -283,16 +281,11 @@ namespace ams::ncm::impl { Result CreateContentStorage(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentStorageEntry* entry = FindContentStorageEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } - + R_UNLESS(entry, ResultUnknownStorage()); R_TRY(fs::MountContentStorage(entry->mount_point, entry->content_storage_id)); ON_SCOPE_EXIT { @@ -308,15 +301,11 @@ namespace ams::ncm::impl { Result VerifyContentStorage(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentStorageEntry* entry = FindContentStorageEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } + R_UNLESS(entry, ResultUnknownStorage()); MountName mount_name = fs::CreateUniqueMountName(); char mount_root[128] = {0}; @@ -336,15 +325,11 @@ namespace ams::ncm::impl { Result OpenContentStorage(std::shared_ptr* out, StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentStorageEntry* entry = FindContentStorageEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } + R_UNLESS(entry, ResultUnknownStorage()); auto content_storage = entry->content_storage; @@ -380,19 +365,11 @@ namespace ams::ncm::impl { Result CloseContentStorageForcibly(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); ContentStorageEntry* entry = FindContentStorageEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } - - if (!entry->content_storage) { - return ResultSuccess(); - } + R_UNLESS(entry, ResultUnknownStorage()); + R_UNLESS(entry->content_storage, ResultSuccess()); /* N doesn't bother checking the result of this */ entry->content_storage->DisableForcibly(); @@ -404,20 +381,13 @@ namespace ams::ncm::impl { Result ActivateContentStorage(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentStorageEntry* entry = FindContentStorageEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } - + R_UNLESS(entry, ResultUnknownStorage()); /* Already activated. */ - if (entry->content_storage != nullptr) { - return ResultSuccess(); - } + R_UNLESS(entry->content_storage == nullptr, ResultSuccess()); if (storage_id == StorageId::GameCard) { FsGameCardHandle gc_hnd; @@ -462,20 +432,13 @@ namespace ams::ncm::impl { Result InactivateContentStorage(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentStorageEntry* entry = FindContentStorageEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } - + R_UNLESS(entry, ResultUnknownStorage()); /* Already inactivated. */ - if (entry->content_storage == nullptr) { - return ResultSuccess(); - } + R_UNLESS(entry->content_storage != nullptr, ResultSuccess()); entry->content_storage->DisableForcibly(); entry->content_storage = nullptr; @@ -486,15 +449,12 @@ namespace ams::ncm::impl { Result CreateContentMetaDatabase(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || storage_id == StorageId::GameCard || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(storage_id != StorageId::GameCard, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } + R_UNLESS(entry, ResultUnknownStorage()); /* N doesn't bother checking the result of this. */ fsDisableAutoSaveDataCreation(); @@ -519,22 +479,14 @@ namespace ams::ncm::impl { Result VerifyContentMetaDatabase(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::GameCard) { - return ResultSuccess(); - } - - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::GameCard, ResultSuccess()); + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } + R_UNLESS(entry, ResultUnknownStorage()); bool mounted_save_data = false; - if (!entry->content_meta_database) { R_TRY(fs::MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)); mounted_save_data = true; @@ -548,9 +500,7 @@ namespace ams::ncm::impl { bool has_meta_path = false; R_TRY(fs::HasDirectory(&has_meta_path, entry->meta_path)); - if (!has_meta_path) { - return ResultInvalidContentMetaDatabase(); - } + R_UNLESS(has_meta_path, ResultInvalidContentMetaDatabase()); return ResultSuccess(); } @@ -558,15 +508,11 @@ namespace ams::ncm::impl { Result OpenContentMetaDatabase(std::shared_ptr* out, StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } + R_UNLESS(entry, ResultUnknownStorage()); std::shared_ptr content_meta_db = entry->content_meta_database; @@ -602,21 +548,13 @@ namespace ams::ncm::impl { Result CloseContentMetaDatabaseForcibly(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } + R_UNLESS(entry, ResultUnknownStorage()); std::shared_ptr content_meta_db = entry->content_meta_database; - - if (!content_meta_db) { - return ResultSuccess(); - } + R_UNLESS(content_meta_db, ResultSuccess()); /* N doesn't bother checking the result of this */ content_meta_db->DisableForcibly(); @@ -633,15 +571,11 @@ namespace ams::ncm::impl { Result CleanupContentMetaDatabase(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id); - - if (!entry) { - return ResultUnknownStorage(); - } + R_UNLESS(entry, ResultUnknownStorage()); R_TRY(fsDeleteSaveDataFileSystemBySaveDataSpaceId(entry->save_meta.space_id, entry->save_meta.id)); return ResultSuccess(); @@ -650,16 +584,13 @@ namespace ams::ncm::impl { Result ActivateContentMetaDatabase(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id); - + R_UNLESS(entry, ResultUnknownStorage()); /* Already activated. */ - if (entry->content_meta_database != nullptr) { - return ResultSuccess(); - } + R_UNLESS(entry->content_meta_database == nullptr, ResultSuccess()); /* Make a brand new kvs. N doesn't quite do this, but we will for cleanliness. */ entry->kvs.emplace(); @@ -686,16 +617,13 @@ namespace ams::ncm::impl { Result InactivateContentMetaDatabase(StorageId storage_id) { std::scoped_lock lk(g_mutex); - if (storage_id == StorageId::None || static_cast(storage_id) == 6) { - return ResultUnknownStorage(); - } + R_UNLESS(storage_id != StorageId::None, ResultUnknownStorage()); + R_UNLESS(static_cast(storage_id) != 6, ResultUnknownStorage()); ContentMetaDBEntry* entry = FindContentMetaDBEntry(storage_id); - + R_UNLESS(entry, ResultUnknownStorage()); /* Already inactivated. */ - if (entry->content_meta_database == nullptr) { - return ResultSuccess(); - } + R_UNLESS(entry->content_meta_database != nullptr, ResultSuccess()); entry->content_meta_database->DisableForcibly(); entry->content_meta_database = nullptr; diff --git a/stratosphere/ncm/source/impl/ncm_placeholder_accessor.cpp b/stratosphere/ncm/source/impl/ncm_placeholder_accessor.cpp index 7b3ee44be..1f2f76e3d 100644 --- a/stratosphere/ncm/source/impl/ncm_placeholder_accessor.cpp +++ b/stratosphere/ncm/source/impl/ncm_placeholder_accessor.cpp @@ -23,9 +23,7 @@ namespace ams::ncm::impl { Result PlaceHolderAccessor::Open(FILE** out_handle, PlaceHolderId placeholder_id) { - if (this->LoadFromCache(out_handle, placeholder_id)) { - return ResultSuccess(); - } + R_UNLESS(!this->LoadFromCache(out_handle, placeholder_id), ResultSuccess()); char placeholder_path[FS_MAX_PATH] = {0}; this->MakePath(placeholder_path, placeholder_id); @@ -127,9 +125,7 @@ namespace ams::ncm::impl { this->GetPath(placeholder_path, placeholder_id); R_TRY_CATCH(fsdevCreateFile(placeholder_path, size, FsCreateOption_BigFile)) { - R_CATCH(ams::fs::ResultPathAlreadyExists) { - return ResultPlaceHolderAlreadyExists(); - } + R_CONVERT(ams::fs::ResultPathAlreadyExists, ResultPlaceHolderAlreadyExists()) } R_END_TRY_CATCH; return ResultSuccess(); @@ -142,9 +138,7 @@ namespace ams::ncm::impl { if (std::remove(placeholder_path) != 0) { R_TRY_CATCH(fsdevGetLastResult()) { - R_CATCH(ams::fs::ResultPathNotFound) { - return ResultPlaceHolderNotFound(); - } + R_CONVERT(ams::fs::ResultPathNotFound, ResultPlaceHolderNotFound()) } R_END_TRY_CATCH; } @@ -155,9 +149,7 @@ namespace ams::ncm::impl { FILE* f = nullptr; R_TRY_CATCH(this->Open(&f, placeholder_id)) { - R_CATCH(ams::fs::ResultPathNotFound) { - return ResultPlaceHolderNotFound(); - } + R_CONVERT(ams::fs::ResultPathNotFound, ResultPlaceHolderNotFound()) } R_END_TRY_CATCH; ON_SCOPE_EXIT { @@ -173,9 +165,7 @@ namespace ams::ncm::impl { this->MakePath(placeholder_path, placeholder_id); if (truncate(placeholder_path, size) == -1) { R_TRY_CATCH(fsdevGetLastResult()) { - R_CATCH(ams::fs::ResultPathNotFound) { - return ResultPlaceHolderNotFound(); - } + R_CONVERT(ams::fs::ResultPathNotFound, ResultPlaceHolderNotFound()) } R_END_TRY_CATCH; } @@ -207,13 +197,9 @@ namespace ams::ncm::impl { this->StoreToCache(f, placeholder_id); - if (fseek(f, 0L, SEEK_END) != 0) { - return fsdevGetLastResult(); - } + R_UNLESS(fseek(f, 0L, SEEK_END) == 0, fsdevGetLastResult()); size_t size = ftell(f); - if (fseek(f, 0L, SEEK_SET) != 0) { - return fsdevGetLastResult(); - } + R_UNLESS(fseek(f, 0L, SEEK_SET) == 0, fsdevGetLastResult()); *found_in_cache = true; *out_size = size; diff --git a/stratosphere/ncm/source/lr_addoncontentlocationresolver.cpp b/stratosphere/ncm/source/lr_addoncontentlocationresolver.cpp index 5e9aa0524..01ff5d29c 100644 --- a/stratosphere/ncm/source/lr_addoncontentlocationresolver.cpp +++ b/stratosphere/ncm/source/lr_addoncontentlocationresolver.cpp @@ -21,13 +21,9 @@ namespace ams::lr { Result AddOnContentLocationResolverInterface::ResolveAddOnContentPath(sf::Out out, ncm::ProgramId id) { ncm::StorageId storage_id = ncm::StorageId::None; - - if (!this->registered_storages.Find(&storage_id, id)) { - return ResultAddOnContentNotFound(); - } + R_UNLESS(this->registered_storages.Find(&storage_id, id), ResultAddOnContentNotFound()); std::shared_ptr content_meta_database; - ncm::ContentId data_content_id; R_TRY(ncm::impl::OpenContentMetaDatabase(&content_meta_database, storage_id)); R_TRY(content_meta_database->GetLatestData(&data_content_id, id)); @@ -40,18 +36,12 @@ namespace ams::lr { } Result AddOnContentLocationResolverInterface::RegisterAddOnContentStorageDeprecated(ncm::StorageId storage_id, ncm::ProgramId id) { - if (!this->registered_storages.Register(id, storage_id, ncm::ProgramId::Invalid)) { - return ResultTooManyRegisteredPaths(); - } - + R_UNLESS(this->registered_storages.Register(id, storage_id, ncm::ProgramId::Invalid), ResultTooManyRegisteredPaths()); return ResultSuccess(); } Result AddOnContentLocationResolverInterface::RegisterAddOnContentStorage(ncm::StorageId storage_id, ncm::ProgramId id, ncm::ProgramId application_id) { - if (!this->registered_storages.Register(id, storage_id, application_id)) { - return ResultTooManyRegisteredPaths(); - } - + R_UNLESS(this->registered_storages.Register(id, storage_id, application_id), ResultTooManyRegisteredPaths()); return ResultSuccess(); } @@ -63,10 +53,10 @@ namespace ams::lr { Result AddOnContentLocationResolverInterface::RefreshApplicationAddOnContent(const sf::InArray &ids) { if (ids.GetSize() == 0) { this->registered_storages.Clear(); - return ResultSuccess(); + } else { + this->registered_storages.ClearExcluding(ids.GetPointer(), ids.GetSize()); } - this->registered_storages.ClearExcluding(ids.GetPointer(), ids.GetSize()); return ResultSuccess(); } diff --git a/stratosphere/ncm/source/lr_contentlocationresolver.cpp b/stratosphere/ncm/source/lr_contentlocationresolver.cpp index dcf074a16..c8c0312c6 100644 --- a/stratosphere/ncm/source/lr_contentlocationresolver.cpp +++ b/stratosphere/ncm/source/lr_contentlocationresolver.cpp @@ -28,16 +28,11 @@ namespace ams::lr { } Result ContentLocationResolverInterface::ResolveProgramPath(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->program_redirector, id)) { - return ResultSuccess(); - } - + R_UNLESS(!this->GetRedirectedPath(out.GetPointer(), &this->program_redirector, id), ResultSuccess()); ncm::ContentId program_content_id; R_TRY_CATCH(this->content_meta_database->GetLatestProgram(&program_content_id, id)) { - R_CATCH(ncm::ResultContentMetaNotFound) { - return ResultProgramNotFound(); - } + R_CONVERT(ncm::ResultContentMetaNotFound, ResultProgramNotFound()) } R_END_TRY_CATCH; this->GetContentStoragePath(out.GetPointer(), program_content_id); @@ -50,19 +45,13 @@ namespace ams::lr { } Result ContentLocationResolverInterface::ResolveApplicationControlPath(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->app_control_redirector, id)) { - return ResultSuccess(); - } - - return ResultControlNotFound(); + R_UNLESS(this->GetRedirectedPath(out.GetPointer(), &this->app_control_redirector, id), ResultControlNotFound()); + return ResultSuccess(); } Result ContentLocationResolverInterface::ResolveApplicationHtmlDocumentPath(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->html_docs_redirector, id)) { - return ResultSuccess(); - } - - return ResultHtmlDocumentNotFound(); + R_UNLESS(this->GetRedirectedPath(out.GetPointer(), &this->html_docs_redirector, id), ResultHtmlDocumentNotFound()); + return ResultSuccess(); } Result ContentLocationResolverInterface::ResolveDataPath(sf::Out out, ncm::ProgramId id) { @@ -94,11 +83,8 @@ namespace ams::lr { } Result ContentLocationResolverInterface::ResolveApplicationLegalInformationPath(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->legal_info_redirector, id)) { - return ResultSuccess(); - } - - return ResultLegalInformationNotFound(); + R_UNLESS(this->GetRedirectedPath(out.GetPointer(), &this->legal_info_redirector, id), ResultLegalInformationNotFound()); + return ResultSuccess(); } Result ContentLocationResolverInterface::RedirectApplicationLegalInformationPathDeprecated(const Path &path, ncm::ProgramId id) { @@ -120,7 +106,6 @@ namespace ams::lr { this->content_meta_database = std::move(content_meta_database); this->content_storage = std::move(content_storage); this->ClearRedirections(); - return ResultSuccess(); } @@ -165,14 +150,10 @@ namespace ams::lr { } Result ContentLocationResolverInterface::ResolveProgramPathForDebug(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->debug_program_redirector, id)) { - return ResultSuccess(); - } + R_UNLESS(!this->GetRedirectedPath(out.GetPointer(), &this->debug_program_redirector, id), ResultSuccess()); R_TRY_CATCH(this->ResolveProgramPath(out.GetPointer(), id)) { - R_CATCH(ResultProgramNotFound) { - return ResultDebugProgramNotFound(); - } + R_CONVERT(ResultProgramNotFound, ResultDebugProgramNotFound()) } R_END_TRY_CATCH; return ResultSuccess(); diff --git a/stratosphere/ncm/source/lr_redirectonlylocationresolver.cpp b/stratosphere/ncm/source/lr_redirectonlylocationresolver.cpp index 73ccd6473..3f11044be 100644 --- a/stratosphere/ncm/source/lr_redirectonlylocationresolver.cpp +++ b/stratosphere/ncm/source/lr_redirectonlylocationresolver.cpp @@ -28,11 +28,8 @@ namespace ams::lr { } Result RedirectOnlyLocationResolverInterface::ResolveProgramPath(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->program_redirector, id)) { - return ResultSuccess(); - } - - return ResultProgramNotFound(); + R_UNLESS(this->GetRedirectedPath(out.GetPointer(), &this->program_redirector, id), ResultProgramNotFound()); + return ResultSuccess(); } Result RedirectOnlyLocationResolverInterface::RedirectProgramPath(const Path &path, ncm::ProgramId id) { @@ -41,19 +38,13 @@ namespace ams::lr { } Result RedirectOnlyLocationResolverInterface::ResolveApplicationControlPath(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->app_control_redirector, id)) { - return ResultSuccess(); - } - - return ResultControlNotFound(); + R_UNLESS(this->GetRedirectedPath(out.GetPointer(), &this->app_control_redirector, id), ResultControlNotFound()); + return ResultSuccess(); } Result RedirectOnlyLocationResolverInterface::ResolveApplicationHtmlDocumentPath(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->html_docs_redirector, id)) { - return ResultSuccess(); - } - - return ResultHtmlDocumentNotFound(); + R_UNLESS(this->GetRedirectedPath(out.GetPointer(), &this->html_docs_redirector, id), ResultHtmlDocumentNotFound()); + return ResultSuccess(); } Result RedirectOnlyLocationResolverInterface::ResolveDataPath(sf::Out out, ncm::ProgramId id) { @@ -81,11 +72,8 @@ namespace ams::lr { } Result RedirectOnlyLocationResolverInterface::ResolveApplicationLegalInformationPath(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->legal_info_redirector, id)) { - return ResultSuccess(); - } - - return ResultLegalInformationNotFound(); + R_UNLESS(this->GetRedirectedPath(out.GetPointer(), &this->legal_info_redirector, id), ResultLegalInformationNotFound()); + return ResultSuccess(); } Result RedirectOnlyLocationResolverInterface::RedirectApplicationLegalInformationPathDeprecated(const Path &path, ncm::ProgramId id) { @@ -152,14 +140,10 @@ namespace ams::lr { } Result RedirectOnlyLocationResolverInterface::ResolveProgramPathForDebug(sf::Out out, ncm::ProgramId id) { - if (this->GetRedirectedPath(out.GetPointer(), &this->debug_program_redirector, id)) { - return ResultSuccess(); - } + R_UNLESS(!this->GetRedirectedPath(out.GetPointer(), &this->debug_program_redirector, id), ResultSuccess()); R_TRY_CATCH(this->ResolveProgramPath(out.GetPointer(), id)) { - R_CATCH(ResultProgramNotFound) { - return ResultDebugProgramNotFound(); - } + R_CONVERT(ResultProgramNotFound, ResultDebugProgramNotFound()) } R_END_TRY_CATCH; return ResultSuccess(); diff --git a/stratosphere/ncm/source/lr_registeredlocationresolver.cpp b/stratosphere/ncm/source/lr_registeredlocationresolver.cpp index 812fde3b1..fe0b69cb3 100644 --- a/stratosphere/ncm/source/lr_registeredlocationresolver.cpp +++ b/stratosphere/ncm/source/lr_registeredlocationresolver.cpp @@ -63,10 +63,7 @@ namespace ams::lr { } Result RegisteredLocationResolverInterface::ResolveProgramPath(sf::Out out, ncm::ProgramId id) { - if (!this->ResolvePath(out.GetPointer(), &this->program_redirector, &this->registered_program_locations, id)) { - return ResultProgramNotFound(); - } - + R_UNLESS(this->ResolvePath(out.GetPointer(), &this->program_redirector, &this->registered_program_locations, id), ResultProgramNotFound()); return ResultSuccess(); } @@ -96,10 +93,7 @@ namespace ams::lr { } Result RegisteredLocationResolverInterface::ResolveHtmlDocumentPath(sf::Out out, ncm::ProgramId id) { - if (!this->ResolvePath(out.GetPointer(), &this->html_docs_redirector, &this->registered_html_docs_locations, id)) { - return ResultHtmlDocumentNotFound(); - } - + R_UNLESS(this->ResolvePath(out.GetPointer(), &this->html_docs_redirector, &this->registered_html_docs_locations, id), ResultHtmlDocumentNotFound()); return ResultSuccess(); } diff --git a/stratosphere/ncm/source/ncm_contentmetadatabase.cpp b/stratosphere/ncm/source/ncm_contentmetadatabase.cpp index 147d6531f..f4696925c 100644 --- a/stratosphere/ncm/source/ncm_contentmetadatabase.cpp +++ b/stratosphere/ncm/source/ncm_contentmetadatabase.cpp @@ -74,9 +74,7 @@ namespace ams::ncm { Result GetContentMetaSize(size_t *out, const ContentMetaKey &key, const kvdb::MemoryKeyValueStore *kvs) { R_TRY_CATCH(kvs->GetValueSize(out, key)) { - R_CATCH(kvdb::ResultKeyNotFound) { - return ResultContentMetaNotFound(); - } + R_CONVERT(kvdb::ResultKeyNotFound, ResultContentMetaNotFound()) } R_END_TRY_CATCH; return ResultSuccess(); @@ -94,9 +92,8 @@ namespace ams::ncm { R_TRY(this->EnsureEnabled()); const auto it = this->kvs->lower_bound(key); - if (it == this->kvs->end() || it->GetKey().id != key.id) { - return ResultContentMetaNotFound(); - } + R_UNLESS(it != this->kvs->end(), ResultContentMetaNotFound()); + R_UNLESS(it->GetKey().id == key.id, ResultContentMetaNotFound()); const auto stored_key = it->GetKey(); const void* value = nullptr; @@ -105,9 +102,7 @@ namespace ams::ncm { R_TRY(GetContentMetaValuePointer(&value, &value_size, stored_key, this->kvs)); const auto header = GetValueHeader(value); - if (header->content_count == 0) { - return ResultContentNotFound(); - } + R_UNLESS(header->content_count != 0, ResultContentNotFound()); const ContentInfo* content_infos = GetValueContentInfos(value); const ContentInfo* found_content_info = nullptr; @@ -135,10 +130,7 @@ namespace ams::ncm { found_content_info = lowest_id_offset_info; } - if (!found_content_info) { - return ResultContentNotFound(); - } - + R_UNLESS(found_content_info, ResultContentNotFound()); *out = found_content_info->content_id; return ResultSuccess(); } @@ -161,10 +153,7 @@ namespace ams::ncm { } } - if (!found_key) { - return ResultContentMetaNotFound(); - } - + R_UNLESS(found_key, ResultContentMetaNotFound()); *out_key = key; return ResultSuccess(); } @@ -195,11 +184,9 @@ namespace ams::ncm { } Result ContentMetaDatabaseInterface::ListContentInfo(sf::Out out_count, const sf::OutArray &out_info, ContentMetaKey key, u32 offset) { - if (offset >> 0x1f != 0) { - return ResultInvalidOffset(); - } - + R_UNLESS(offset >> 0x1f == 0, ResultInvalidOffset()); R_TRY(this->EnsureEnabled()); + const void *value = nullptr; size_t value_size = 0; R_TRY(GetContentMetaValuePointer(&value, &value_size, key, this->kvs)); @@ -367,15 +354,11 @@ namespace ams::ncm { Result ContentMetaDatabaseInterface::GetSize(sf::Out out_size, ContentMetaKey key) { R_TRY(this->EnsureEnabled()); - - if (this->kvs->GetCount() == 0) { - return ResultContentMetaNotFound(); - } + R_UNLESS(this->kvs->GetCount() != 0, ResultContentMetaNotFound()); const auto it = this->kvs->lower_bound(key); - if (it == this->kvs->end() || it->GetKey() != key) { - return ResultContentMetaNotFound(); - } + R_UNLESS(it != this->kvs->end(), ResultContentMetaNotFound()); + R_UNLESS(it->GetKey() == key, ResultContentMetaNotFound()); out_size.SetValue(it->GetValueSize()); return ResultSuccess(); @@ -383,10 +366,7 @@ namespace ams::ncm { Result ContentMetaDatabaseInterface::GetRequiredSystemVersion(sf::Out out_version, ContentMetaKey key) { R_TRY(this->EnsureEnabled()); - - if (key.type != ContentMetaType::Application && key.type != ContentMetaType::Patch) { - return ResultInvalidContentMetaKey(); - } + R_UNLESS(key.type == ContentMetaType::Application || key.type == ContentMetaType::Patch, ResultInvalidContentMetaKey()); const void* value = nullptr; size_t value_size = 0; @@ -401,10 +381,7 @@ namespace ams::ncm { Result ContentMetaDatabaseInterface::GetPatchId(sf::Out out_patch_id, ContentMetaKey key) { R_TRY(this->EnsureEnabled()); - - if (key.type != ContentMetaType::Application) { - return ResultInvalidContentMetaKey(); - } + R_UNLESS(key.type != ContentMetaType::Application, ResultInvalidContentMetaKey()); const void* value = nullptr; size_t value_size = 0; @@ -422,19 +399,14 @@ namespace ams::ncm { Result ContentMetaDatabaseInterface::LookupOrphanContent(const sf::OutArray &out_orphaned, const sf::InArray &content_ids) { R_TRY(this->EnsureEnabled()); - - if (out_orphaned.GetSize() < content_ids.GetSize()) { - return ResultBufferInsufficient(); - } + R_UNLESS(out_orphaned.GetSize() >= content_ids.GetSize(), ResultBufferInsufficient()); /* Default to orphaned for all content ids. */ if (out_orphaned.GetSize() > 0) { std::fill_n(out_orphaned.GetPointer(), out_orphaned.GetSize(), true); } - if (this->kvs->GetCount() == 0) { - return ResultSuccess(); - } + R_UNLESS(this->kvs->GetCount() != 0, ResultSuccess()); for (auto entry = this->kvs->begin(); entry != this->kvs->end(); entry++) { const auto value = entry->GetValuePointer(); @@ -498,10 +470,7 @@ namespace ams::ncm { } Result ContentMetaDatabaseInterface::ListContentMetaInfo(sf::Out out_entries_written, const sf::OutArray &out_meta_info, ContentMetaKey key, u32 start_index) { - if (start_index >> 0x1f != 0) { - return ResultInvalidOffset(); - } - + R_UNLESS(start_index >> 0x1f == 0, ResultInvalidOffset()); R_TRY(this->EnsureEnabled()); const void* value = nullptr; @@ -555,9 +524,7 @@ namespace ams::ncm { return ResultSuccess(); } - if (key.type != ContentMetaType::AddOnContent) { - return ResultInvalidContentMetaKey(); - } + R_UNLESS(key.type == ContentMetaType::AddOnContent, ResultInvalidContentMetaKey()); const auto ext_header = GetValueExtendedHeader(value); out_version.SetValue(ext_header->required_application_version); return ResultSuccess(); @@ -600,10 +567,7 @@ namespace ams::ncm { found_key = entry->GetKey(); } - if (!found_key) { - return ResultContentMetaNotFound(); - } - + R_UNLESS(found_key, ResultContentMetaNotFound()); *out_key = *found_key; return ResultSuccess(); } diff --git a/stratosphere/ncm/source/ncm_contentstorage.cpp b/stratosphere/ncm/source/ncm_contentstorage.cpp index 4f641f114..846b9a22f 100644 --- a/stratosphere/ncm/source/ncm_contentstorage.cpp +++ b/stratosphere/ncm/source/ncm_contentstorage.cpp @@ -68,18 +68,14 @@ namespace ams::ncm { } Result ContentStorageInterface::OpenCachedContentFile(ContentId content_id) { - if (this->cached_content_id == content_id) { - return ResultSuccess(); - } + R_UNLESS(this->cached_content_id != content_id, ResultSuccess()); this->ClearContentCache(); char content_path[FS_MAX_PATH] = {0}; this->GetContentPath(content_path, content_id); R_TRY_CATCH(fs::OpenFile(&this->content_cache_file_handle, content_path, FsOpenMode_Read)) { - R_CATCH(ams::fs::ResultPathNotFound) { - return ResultContentNotFound(); - } + R_CONVERT(ams::fs::ResultPathNotFound, ResultContentNotFound()) } R_END_TRY_CATCH; this->cached_content_id = content_id; @@ -127,10 +123,7 @@ namespace ams::ncm { Result ContentStorageInterface::WritePlaceHolder(PlaceHolderId placeholder_id, u64 offset, sf::InBuffer data) { /* Offset is too large */ - if (offset >> 0x3f != 0) { - return ResultInvalidOffset(); - } - + R_UNLESS(offset >> 0x3f == 0, ResultInvalidOffset()); R_TRY(this->EnsureEnabled()); R_TRY(this->placeholder_accessor.Write(placeholder_id, offset, data.GetPointer(), data.GetSize())); return ResultSuccess(); @@ -148,12 +141,8 @@ namespace ams::ncm { if (rename(placeholder_path, content_path) != 0) { R_TRY_CATCH(fsdevGetLastResult()) { - R_CATCH(ams::fs::ResultPathNotFound) { - return ResultPlaceHolderNotFound(); - } - R_CATCH(ams::fs::ResultPathAlreadyExists) { - return ResultContentAlreadyExists(); - } + R_CONVERT(ams::fs::ResultPathNotFound, ResultPlaceHolderNotFound()) + R_CONVERT(ams::fs::ResultPathAlreadyExists, ResultContentAlreadyExists()) } R_END_TRY_CATCH; } @@ -169,9 +158,7 @@ namespace ams::ncm { if (std::remove(content_path) != 0) { R_TRY_CATCH(fsdevGetLastResult()) { - R_CATCH(ams::fs::ResultPathNotFound) { - return ResultContentNotFound(); - } + R_CONVERT(ams::fs::ResultPathNotFound, ResultContentNotFound()) } R_END_TRY_CATCH; } @@ -223,11 +210,7 @@ namespace ams::ncm { /* Nintendo uses CleanDirectoryRecursively which is 3.0.0+. We'll just delete the directory and recreate it to support all firmwares. */ R_TRY(fsdevDeleteDirectoryRecursively(placeholder_root_path)); - - if (mkdir(placeholder_root_path, S_IRWXU) == -1) { - return fsdevGetLastResult(); - } - + R_UNLESS(mkdir(placeholder_root_path, S_IRWXU) != -1, fsdevGetLastResult()); return ResultSuccess(); } @@ -244,9 +227,7 @@ namespace ams::ncm { *should_retry_dir_read = false; if (dir_entry->d_type == DT_REG) { - if (entry_count > out_buf.GetSize()) { - return ResultBufferInsufficient(); - } + R_UNLESS(entry_count <= out_buf.GetSize(), ResultBufferInsufficient()); PlaceHolderId cur_entry_placeholder_id = {0}; R_TRY(GetPlaceHolderIdFromDirEntry(&cur_entry_placeholder_id, dir_entry)); @@ -283,11 +264,8 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentStorageInterface::ListContentId(sf::Out out_count, const sf::OutArray &out_buf, u32 start_offset) { - if (start_offset >> 0x1f != 0) { - return ResultInvalidOffset(); - } - + Result ContentStorageInterface::ListContentId(sf::Out out_count, const sf::OutArray &out_buf, u32 start_offset) { + R_UNLESS(start_offset >> 0x1f == 0, ResultInvalidOffset()); R_TRY(this->EnsureEnabled()); char content_root_path[FS_MAX_PATH] = {0}; @@ -342,10 +320,7 @@ namespace ams::ncm { this->GetContentPath(content_path, content_id); struct stat st; - if (stat(content_path, &st) == -1) { - return fsdevGetLastResult(); - } - + R_UNLESS(stat(content_path, &st) != -1, fsdevGetLastResult()); out_size.SetValue(st.st_size); return ResultSuccess(); } @@ -374,12 +349,8 @@ namespace ams::ncm { this->placeholder_accessor.GetPath(placeholder_path, placeholder_id); if (rename(old_content_path, placeholder_path) != 0) { R_TRY_CATCH(fsdevGetLastResult()) { - R_CATCH(ams::fs::ResultPathNotFound) { - return ResultPlaceHolderNotFound(); - } - R_CATCH(ams::fs::ResultPathAlreadyExists) { - return ResultPlaceHolderAlreadyExists(); - } + R_CONVERT(ams::fs::ResultPathNotFound, ResultPlaceHolderNotFound()) + R_CONVERT(ams::fs::ResultPathAlreadyExists, ResultPlaceHolderAlreadyExists()) } R_END_TRY_CATCH; } @@ -394,10 +365,7 @@ namespace ams::ncm { Result ContentStorageInterface::ReadContentIdFile(sf::OutBuffer buf, ContentId content_id, u64 offset) { /* Offset is too large */ - if (offset >> 0x3f != 0) { - return ResultInvalidOffset(); - } - + R_UNLESS(offset >> 0x3f == 0, ResultInvalidOffset()); R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; this->GetContentPath(content_path, content_id); @@ -484,10 +452,7 @@ namespace ams::ncm { Result ContentStorageInterface::WriteContentForDebug(ContentId content_id, u64 offset, sf::InBuffer data) { /* Offset is too large */ - if (offset >> 0x3f != 0) { - return ResultInvalidOffset(); - } - + R_UNLESS(offset >> 0x3f == 0, ResultInvalidOffset()); R_TRY(this->EnsureEnabled()); bool is_development = false; @@ -515,20 +480,14 @@ namespace ams::ncm { Result ContentStorageInterface::GetFreeSpaceSize(sf::Out out_size) { struct statvfs st = {0}; - if (statvfs(this->root_path, &st) == -1) { - return fsdevGetLastResult(); - } - + R_UNLESS(statvfs(this->root_path, &st) != -1, fsdevGetLastResult()); out_size.SetValue(st.f_bfree); return ResultSuccess(); } Result ContentStorageInterface::GetTotalSpaceSize(sf::Out out_size) { struct statvfs st = {0}; - if (statvfs(this->root_path, &st) == -1) { - return fsdevGetLastResult(); - } - + R_UNLESS(statvfs(this->root_path, &st) != -1, fsdevGetLastResult()); out_size.SetValue(st.f_blocks); return ResultSuccess(); } @@ -555,9 +514,7 @@ namespace ams::ncm { struct stat st; this->placeholder_accessor.GetPath(placeholder_path, placeholder_id); - if (stat(placeholder_path, &st) == -1) { - return fsdevGetLastResult(); - } + R_UNLESS(stat(placeholder_path, &st) != -1, fsdevGetLastResult()); out_size.SetValue(st.st_size); return ResultSuccess(); diff --git a/stratosphere/ncm/source/ncm_fs.cpp b/stratosphere/ncm/source/ncm_fs.cpp index 1444c2299..7e476ca1f 100644 --- a/stratosphere/ncm/source/ncm_fs.cpp +++ b/stratosphere/ncm/source/ncm_fs.cpp @@ -28,9 +28,7 @@ namespace ams::ncm::fs { /* Manually check if the file already exists, so it doesn't get created automatically. */ R_TRY(HasFile(&has, path)); - if (!has) { - return ams::fs::ResultPathNotFound(); - } + R_UNLESS(has, ams::fs::ResultPathNotFound()); const char* fopen_mode = ""; @@ -40,32 +38,19 @@ namespace ams::ncm::fs { fopen_mode = "rb"; } FILE* f = fopen(path, fopen_mode); - - if (f == nullptr) { - return fsdevGetLastResult(); - } + R_UNLESS(f != nullptr, fsdevGetLastResult()); *out = f; return ResultSuccess(); } Result WriteFile(FILE* f, size_t offset, const void* buffer, size_t size, u32 option) { - if (fseek(f, 0, SEEK_END) != 0) { - return fsdevGetLastResult(); - } + R_UNLESS(fseek(f, 0, SEEK_END) == 0, fsdevGetLastResult()); size_t existing_size = ftell(f); - if (offset + size > existing_size) { - return ams::fs::ResultFileExtensionWithoutOpenModeAllowAppend(); - } - - if (fseek(f, offset, SEEK_SET) != 0) { - return fsdevGetLastResult(); - } - - if (fwrite(buffer, 1, size, f) != size) { - return fsdevGetLastResult(); - } + R_UNLESS(offset + size <= existing_size, ams::fs::ResultFileExtensionWithoutOpenModeAllowAppend()); + R_UNLESS(fseek(f, offset, SEEK_SET) == 0, fsdevGetLastResult()); + R_UNLESS(fwrite(buffer, 1, size, f) == size, fsdevGetLastResult()); if (option & FsWriteOption_Flush) { fflush(f); @@ -75,14 +60,8 @@ namespace ams::ncm::fs { } Result ReadFile(FILE* f, size_t offset, void* buffer, size_t size) { - if (fseek(f, offset, SEEK_SET) != 0) { - return fsdevGetLastResult(); - } - - if (fread(buffer, 1, size, f) != size && ferror(f)) { - return fsdevGetLastResult(); - } - + R_UNLESS(fseek(f, offset, SEEK_SET) == 0, fsdevGetLastResult()); + R_UNLESS(fread(buffer, 1, size, f) == size || !ferror(f), fsdevGetLastResult()); return ResultSuccess(); } @@ -124,25 +103,17 @@ namespace ams::ncm::fs { bool has_root = false; R_TRY(HasDirectory(&has_root, root_path)); - if (!has_root) { - return ResultStorageRootNotFound(); - } + R_UNLESS(has_root, ResultStorageRootNotFound()); path::GetContentRootPath(content_root, root_path); - bool has_content_root = false; R_TRY(HasDirectory(&has_content_root, content_root)); - if (!has_content_root) { - return ResultStoragePathNotFound(); - } + R_UNLESS(has_content_root, ResultStoragePathNotFound()); path::GetPlaceHolderRootPath(placeholder_root, root_path); - bool has_placeholder_root = false; R_TRY(HasDirectory(&has_placeholder_root, placeholder_root)); - if (!has_placeholder_root) { - return ResultStoragePathNotFound(); - } + R_UNLESS(has_placeholder_root, ResultStoragePathNotFound()); return ResultSuccess(); } @@ -172,36 +143,31 @@ namespace ams::ncm::fs { } Result EnsureRecursively(const char* path) { - if (!path) { - return ams::fs::ResultNullptrArgument(); - } + R_UNLESS(path, ams::fs::ResultNullptrArgument()); size_t path_len = strlen(path); char working_path_buf[FS_MAX_PATH] = {0}; - if (path_len + 1 < FS_MAX_PATH) { - strncpy(working_path_buf + 1, path, FS_MAX_PATH-1); + R_UNLESS(path_len + 1 < FS_MAX_PATH, ResultAllocationFailed()); + strncpy(working_path_buf + 1, path, FS_MAX_PATH-1); - if (path_len != 0) { - for (size_t i = 0; i < path_len; i++) { - if (i != 0 && working_path_buf[i + 1] == '/' && working_path_buf[i] != ':') { - /* Temporarily make the path terminate before the '/' */ - working_path_buf[i + 1] = 0; - if (mkdir(working_path_buf + 1, S_IRWXU) == -1) { - R_TRY_CATCH(fsdevGetLastResult()) { - R_CATCH(ams::fs::ResultPathAlreadyExists) { - /* If the path already exists, that's okay. Anything else is an error. */ - } - } R_END_TRY_CATCH; - } - - /* Restore the path to its former state */ - working_path_buf[i + 1] = '/'; + if (path_len != 0) { + for (size_t i = 0; i < path_len; i++) { + if (i != 0 && working_path_buf[i + 1] == '/' && working_path_buf[i] != ':') { + /* Temporarily make the path terminate before the '/' */ + working_path_buf[i + 1] = 0; + if (mkdir(working_path_buf + 1, S_IRWXU) == -1) { + R_TRY_CATCH(fsdevGetLastResult()) { + R_CATCH(ams::fs::ResultPathAlreadyExists) { + /* If the path already exists, that's okay. Anything else is an error. */ + } + } R_END_TRY_CATCH; } + + /* Restore the path to its former state */ + working_path_buf[i + 1] = '/'; } } - } else { - return ResultAllocationFailed(); } return ResultSuccess(); @@ -237,18 +203,15 @@ namespace ams::ncm::fs { const char* unqual_path = strchr(path, ':'); /* We should be given a qualified path. */ - if (!unqual_path || unqual_path > path + 0xf) { - return ams::fs::ResultInvalidMountName(); - } + R_UNLESS(unqual_path, ams::fs::ResultInvalidMountName()); + R_UNLESS(unqual_path <= path + 0xf, ams::fs::ResultInvalidMountName()); strncpy(mount_name->name, path, unqual_path - path); return ResultSuccess(); } Result MountSystemSaveData(const char* mount_point, FsSaveDataSpaceId space_id, u64 save_id) { - if (!mount_point) { - return ams::fs::ResultNullptrArgument(); - } + R_UNLESS(mount_point, ams::fs::ResultNullptrArgument()); FsSaveDataAttribute save = { .system_save_data_id = save_id, @@ -276,9 +239,7 @@ namespace ams::ncm::fs { std::map g_mount_content_storage; Result MountContentStorage(const char* mount_point, FsContentStorageId id) { - if (!mount_point) { - return ams::fs::ResultNullptrArgument(); - } + R_UNLESS(mount_point, ams::fs::ResultNullptrArgument()); FsFileSystem fs; R_TRY(fsOpenContentStorageFileSystem(&fs, id)); @@ -325,9 +286,7 @@ namespace ams::ncm::fs { } Result Unmount(const char* mount_point) { - if (!mount_point) { - return ams::fs::ResultNullptrArgument(); - } + R_UNLESS(mount_point, ams::fs::ResultNullptrArgument()); /* Erase any content storage mappings which may potentially exist. */ g_mount_content_storage.erase(mount_point); @@ -340,16 +299,13 @@ namespace ams::ncm::fs { } Result ConvertToFsCommonPath(char* out_common_path, size_t out_len, const char* path) { - if (!out_common_path || !path) { - return ams::fs::ResultNullptrArgument(); - } + R_UNLESS(out_common_path, ams::fs::ResultNullptrArgument()); + R_UNLESS(path, ams::fs::ResultNullptrArgument()); MountName mount_name = {0}; R_TRY(GetMountNameFromPath(&mount_name, path)); - - if (!fsdevGetDeviceFileSystem(mount_name.name) || g_mount_content_storage.find(mount_name.name) == g_mount_content_storage.end()) { - return ams::fs::ResultNotMounted(); - } + R_UNLESS(fsdevGetDeviceFileSystem(mount_name.name), ams::fs::ResultNotMounted()); + R_UNLESS(g_mount_content_storage.find(mount_name.name) != g_mount_content_storage.end(), ams::fs::ResultNotMounted()); char translated_path[FS_MAX_PATH] = {0}; std::string common_mount_name = g_mount_content_storage[mount_name.name]; diff --git a/stratosphere/ncm/source/ncm_fs.hpp b/stratosphere/ncm/source/ncm_fs.hpp index 00c7cd1af..1ba2c150e 100644 --- a/stratosphere/ncm/source/ncm_fs.hpp +++ b/stratosphere/ncm/source/ncm_fs.hpp @@ -54,17 +54,13 @@ namespace ams::ncm::fs { Result TraverseDirectory(bool* out_should_continue, const char* root_path, int max_level, F f) { DIR *dir; struct dirent* dir_entry = nullptr; - if (max_level < 1) { - return ResultSuccess(); - } + R_UNLESS(max_level >= 1, ResultSuccess()); bool retry_dir_read = true; while (retry_dir_read) { retry_dir_read = false; - if ((dir = opendir(root_path)) == nullptr) { - return fsdevGetLastResult(); - } + R_UNLESS((dir = opendir(root_path)) != nullptr, fsdevGetLastResult()); ON_SCOPE_EXIT { closedir(dir); }; while ((dir_entry = readdir(dir)) != nullptr) { diff --git a/stratosphere/ncm/source/ncm_icontentmetadatabase.hpp b/stratosphere/ncm/source/ncm_icontentmetadatabase.hpp index 8423c18a9..7687f7e99 100644 --- a/stratosphere/ncm/source/ncm_icontentmetadatabase.hpp +++ b/stratosphere/ncm/source/ncm_icontentmetadatabase.hpp @@ -52,9 +52,7 @@ namespace ams::ncm { bool disabled; protected: Result EnsureEnabled() { - if (this->disabled) { - return ResultInvalidContentMetaDatabase(); - } + R_UNLESS(!this->disabled, ResultInvalidContentMetaDatabase()); return ResultSuccess(); } public: diff --git a/stratosphere/ncm/source/ncm_icontentstorage.hpp b/stratosphere/ncm/source/ncm_icontentstorage.hpp index 7d9adcc82..1f75a0f24 100644 --- a/stratosphere/ncm/source/ncm_icontentstorage.hpp +++ b/stratosphere/ncm/source/ncm_icontentstorage.hpp @@ -58,9 +58,7 @@ namespace ams::ncm { bool disabled; protected: Result EnsureEnabled() { - if (this->disabled) { - return ResultInvalidContentStorage(); - } + R_UNLESS(!this->disabled, ResultInvalidContentStorage()); return ResultSuccess(); } public: diff --git a/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp b/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp index 9bf4a2892..72ab32aa9 100644 --- a/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp +++ b/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp @@ -134,10 +134,7 @@ namespace ams::ncm { } struct stat st; - if (stat(content_path, &st) == -1) { - return fsdevGetLastResult(); - } - + R_UNLESS(stat(content_path, &st) != -1, fsdevGetLastResult()); out_size.SetValue(st.st_size); return ResultSuccess(); } @@ -157,10 +154,7 @@ namespace ams::ncm { Result ReadOnlyContentStorageInterface::ReadContentIdFile(sf::OutBuffer buf, ContentId content_id, u64 offset) { /* Offset is too large */ - if (offset >> 0x3f != 0) { - return ResultInvalidOffset(); - } - + R_UNLESS(offset >> 0x3f == 0, ResultInvalidOffset()); R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; @@ -181,7 +175,6 @@ namespace ams::ncm { }; R_TRY(fs::ReadFile(f, offset, buf.GetPointer(), buf.GetSize())); - return ResultSuccess(); } diff --git a/stratosphere/ncm/source/ncm_utils.cpp b/stratosphere/ncm/source/ncm_utils.cpp index 81f11bc87..888857588 100644 --- a/stratosphere/ncm/source/ncm_utils.cpp +++ b/stratosphere/ncm/source/ncm_utils.cpp @@ -31,9 +31,8 @@ namespace ams::ncm { } Result GetPlaceHolderIdFromDirEntry(PlaceHolderId* out, struct dirent* dir_entry) { - if (strnlen(dir_entry->d_name, 0x30) != 0x24 || strncmp(dir_entry->d_name + 0x20, ".nca", 4) != 0) { - return ResultInvalidPlaceHolderDirectoryEntry(); - } + R_UNLESS(strnlen(dir_entry->d_name, 0x30) == 0x24, ResultInvalidPlaceHolderDirectoryEntry()); + R_UNLESS(strncmp(dir_entry->d_name + 0x20, ".nca", 4) == 0, ResultInvalidPlaceHolderDirectoryEntry()); PlaceHolderId placeholder_id = {0}; char byte_string[2];