From fac8acebba63b56721d84f7e2ece3d01abc8a7ca Mon Sep 17 00:00:00 2001 From: Adubbz Date: Wed, 7 Aug 2019 18:48:31 +1000 Subject: [PATCH] Universally use EnsureEnabled --- .../ncm/source/ncm_contentmetadatabase.cpp | 9 -- .../ncm/source/ncm_contentmetadatabase.hpp | 2 - .../ncm/source/ncm_contentstorage.cpp | 98 +++++-------------- .../ncm/source/ncm_icontentmetadatabase.cpp | 8 ++ .../ncm/source/ncm_icontentmetadatabase.hpp | 2 + .../ncm/source/ncm_icontentstorage.cpp | 8 ++ .../ncm/source/ncm_icontentstorage.hpp | 2 + .../ncm/source/ncm_readonlycontentstorage.cpp | 24 ++--- 8 files changed, 50 insertions(+), 103 deletions(-) diff --git a/stratosphere/ncm/source/ncm_contentmetadatabase.cpp b/stratosphere/ncm/source/ncm_contentmetadatabase.cpp index f84076939..5c0478373 100644 --- a/stratosphere/ncm/source/ncm_contentmetadatabase.cpp +++ b/stratosphere/ncm/source/ncm_contentmetadatabase.cpp @@ -179,15 +179,6 @@ namespace sts::ncm { return ResultSuccess; } - Result ContentMetaDatabaseInterface::EnsureEnabled() { - if (this->disabled) { - return ResultNcmInvalidContentMetaDatabase; - } - - return ResultSuccess; - } - - Result ContentMetaDatabaseInterface::Set(ContentMetaKey key, InBuffer value) { R_DEBUG_START R_TRY(this->EnsureEnabled()); diff --git a/stratosphere/ncm/source/ncm_contentmetadatabase.hpp b/stratosphere/ncm/source/ncm_contentmetadatabase.hpp index d393b9d49..b306631e3 100644 --- a/stratosphere/ncm/source/ncm_contentmetadatabase.hpp +++ b/stratosphere/ncm/source/ncm_contentmetadatabase.hpp @@ -31,8 +31,6 @@ namespace sts::ncm { private: Result GetContentIdByTypeImpl(ContentId* out, const ContentMetaKey& key, ContentType type, std::optional id_offset); Result GetLatestContentMetaKeyImpl(ContentMetaKey* out_key, TitleId tid); - protected: - Result EnsureEnabled(); public: virtual Result Set(ContentMetaKey key, InBuffer value) override; virtual Result Get(Out out_size, ContentMetaKey key, OutBuffer out_value) override; diff --git a/stratosphere/ncm/source/ncm_contentstorage.cpp b/stratosphere/ncm/source/ncm_contentstorage.cpp index 80f57919a..3b0695795 100644 --- a/stratosphere/ncm/source/ncm_contentstorage.cpp +++ b/stratosphere/ncm/source/ncm_contentstorage.cpp @@ -29,10 +29,7 @@ namespace sts::ncm { } Result ContentStorageInterface::Initialize(const char* root_path, MakeContentPathFunc content_path_func, MakePlaceHolderPathFunc placeholder_path_func, bool delay_flush) { - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } - + R_TRY(this->EnsureEnabled()); R_TRY(CheckContentStorageDirectoriesExist(root_path)); const size_t root_path_len = strnlen(root_path, FS_MAX_PATH-1); @@ -95,9 +92,7 @@ namespace sts::ncm { Result ContentStorageInterface::GeneratePlaceHolderId(Out out) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); sts::rnd::GenerateRandomBytes(out.GetPointer(), sizeof(NcmNcaId)); return ResultSuccess; @@ -106,9 +101,7 @@ namespace sts::ncm { Result ContentStorageInterface::CreatePlaceHolder(PlaceHolderId placeholder_id, ContentId content_id, u64 size) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; this->GetContentPath(content_path, content_id); @@ -122,9 +115,7 @@ namespace sts::ncm { Result ContentStorageInterface::DeletePlaceHolder(PlaceHolderId placeholder_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); return this->placeholder_accessor.Delete(placeholder_id); R_DEBUG_END @@ -132,9 +123,7 @@ namespace sts::ncm { Result ContentStorageInterface::HasPlaceHolder(Out out, PlaceHolderId placeholder_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char placeholder_path[FS_MAX_PATH] = {0}; this->placeholder_accessor.GetPlaceHolderPath(placeholder_path, placeholder_id); @@ -154,9 +143,7 @@ namespace sts::ncm { return ResultNcmInvalidOffset; } - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); FILE* f = nullptr; @@ -199,10 +186,7 @@ namespace sts::ncm { Result ContentStorageInterface::Register(PlaceHolderId placeholder_id, ContentId content_id) { R_DEBUG_START this->ClearContentCache(); - - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char placeholder_path[FS_MAX_PATH] = {0}; char content_path[FS_MAX_PATH] = {0}; @@ -227,9 +211,7 @@ namespace sts::ncm { Result ContentStorageInterface::Delete(ContentId content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); this->ClearContentCache(); char content_path[FS_MAX_PATH] = {0}; @@ -249,9 +231,7 @@ namespace sts::ncm { Result ContentStorageInterface::Has(Out out, ContentId content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; this->GetContentPath(content_path, content_id); @@ -266,9 +246,7 @@ namespace sts::ncm { Result ContentStorageInterface::GetPath(OutPointerWithServerSize out, ContentId content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; char common_path[FS_MAX_PATH] = {0}; @@ -282,9 +260,7 @@ namespace sts::ncm { Result ContentStorageInterface::GetPlaceHolderPath(OutPointerWithServerSize out, PlaceHolderId placeholder_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char placeholder_path[FS_MAX_PATH] = {0}; char common_path[FS_MAX_PATH] = {0}; @@ -297,9 +273,7 @@ namespace sts::ncm { Result ContentStorageInterface::CleanupAllPlaceHolder() { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char placeholder_root_path[FS_MAX_PATH] = {0}; this->placeholder_accessor.ClearAllCaches(); @@ -319,9 +293,7 @@ namespace sts::ncm { Result ContentStorageInterface::ListPlaceHolder(Out out_count, OutBuffer out_buf) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char placeholder_root_path[FS_MAX_PATH] = {0}; this->placeholder_accessor.GetPlaceHolderRootPath(placeholder_root_path); @@ -352,9 +324,7 @@ namespace sts::ncm { Result ContentStorageInterface::GetContentCount(Out out_count) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_root_path[FS_MAX_PATH] = {0}; this->GetContentRootPath(content_root_path); @@ -383,9 +353,7 @@ namespace sts::ncm { return ResultNcmInvalidOffset; } - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_root_path[FS_MAX_PATH] = {0}; this->GetContentRootPath(content_root_path); @@ -436,9 +404,7 @@ namespace sts::ncm { Result ContentStorageInterface::GetSizeFromContentId(Out out_size, ContentId content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; this->GetContentPath(content_path, content_id); @@ -464,9 +430,7 @@ namespace sts::ncm { Result ContentStorageInterface::RevertToPlaceHolder(PlaceHolderId placeholder_id, ContentId old_content_id, ContentId new_content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char old_content_path[FS_MAX_PATH] = {0}; char new_content_path[FS_MAX_PATH] = {0}; @@ -497,9 +461,7 @@ namespace sts::ncm { Result ContentStorageInterface::SetPlaceHolderSize(PlaceHolderId placeholder_id, u64 size) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); R_TRY(this->placeholder_accessor.SetSize(placeholder_id, size)); return ResultSuccess; @@ -513,9 +475,7 @@ namespace sts::ncm { return ResultNcmInvalidOffset; } - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; this->GetContentPath(content_path, content_id); @@ -535,9 +495,7 @@ namespace sts::ncm { Result ContentStorageInterface::GetRightsIdFromPlaceHolderId(Out out_rights_id, Out out_key_generation, PlaceHolderId placeholder_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); FsRightsId rights_id = {0}; u8 key_generation = 0; @@ -557,9 +515,7 @@ namespace sts::ncm { Result ContentStorageInterface::GetRightsIdFromContentId(Out out_rights_id, Out out_key_generation, ContentId content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); impl::RightsIdCache* rights_id_cache = impl::GetRightsIdCache(); @@ -625,9 +581,7 @@ namespace sts::ncm { return ResultNcmInvalidOffset; } - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); bool is_development = false; @@ -706,9 +660,7 @@ namespace sts::ncm { Result ContentStorageInterface::GetSizeFromPlaceHolderId(Out out_size, PlaceHolderId placeholder_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); bool found_in_cache = false; size_t size = 0; @@ -768,9 +720,7 @@ namespace sts::ncm { Result ContentStorageInterface::GetRightsIdFromPlaceHolderIdWithCache(Out out_rights_id, Out out_key_generation, PlaceHolderId placeholder_id, ContentId cache_content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); impl::RightsIdCache* rights_id_cache = impl::GetRightsIdCache(); diff --git a/stratosphere/ncm/source/ncm_icontentmetadatabase.cpp b/stratosphere/ncm/source/ncm_icontentmetadatabase.cpp index 7512ccc15..155b21103 100644 --- a/stratosphere/ncm/source/ncm_icontentmetadatabase.cpp +++ b/stratosphere/ncm/source/ncm_icontentmetadatabase.cpp @@ -18,6 +18,14 @@ namespace sts::ncm { + Result IContentMetaDatabase::EnsureEnabled() { + if (this->disabled) { + return ResultNcmInvalidContentMetaDatabase; + } + + return ResultSuccess; + } + Result IContentMetaDatabase::Set(ContentMetaKey key, InBuffer value) { std::abort(); } diff --git a/stratosphere/ncm/source/ncm_icontentmetadatabase.hpp b/stratosphere/ncm/source/ncm_icontentmetadatabase.hpp index 4a9855f91..627e0b839 100644 --- a/stratosphere/ncm/source/ncm_icontentmetadatabase.hpp +++ b/stratosphere/ncm/source/ncm_icontentmetadatabase.hpp @@ -52,6 +52,8 @@ namespace sts::ncm { sts::kvdb::MemoryKeyValueStore* kvs; char mount_name[16]; bool disabled; + protected: + Result EnsureEnabled(); public: IContentMetaDatabase(sts::kvdb::MemoryKeyValueStore* kvs) : kvs(kvs), disabled(false) diff --git a/stratosphere/ncm/source/ncm_icontentstorage.cpp b/stratosphere/ncm/source/ncm_icontentstorage.cpp index 26db32fc5..8eef3d2d3 100644 --- a/stratosphere/ncm/source/ncm_icontentstorage.cpp +++ b/stratosphere/ncm/source/ncm_icontentstorage.cpp @@ -18,6 +18,14 @@ namespace sts::ncm { + Result IContentStorage::EnsureEnabled() { + if (this->disabled) { + return ResultNcmInvalidContentStorage; + } + + return ResultSuccess; + } + Result IContentStorage::GeneratePlaceHolderId(Out out) { std::abort(); } diff --git a/stratosphere/ncm/source/ncm_icontentstorage.hpp b/stratosphere/ncm/source/ncm_icontentstorage.hpp index 6e58ea6c6..3e1d1d1f6 100644 --- a/stratosphere/ncm/source/ncm_icontentstorage.hpp +++ b/stratosphere/ncm/source/ncm_icontentstorage.hpp @@ -59,6 +59,8 @@ namespace sts::ncm { char root_path[FS_MAX_PATH-1]; MakeContentPathFunc make_content_path_func; bool disabled; + protected: + Result EnsureEnabled(); public: virtual Result GeneratePlaceHolderId(Out out); virtual Result CreatePlaceHolder(PlaceHolderId placeholder_id, ContentId content_id, u64 size); diff --git a/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp b/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp index ef4c26452..63f893f98 100644 --- a/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp +++ b/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp @@ -24,9 +24,7 @@ namespace sts::ncm { Result ReadOnlyContentStorageInterface::Initialize(const char* root_path, MakeContentPathFunc content_path_func) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); const size_t root_path_len = strnlen(root_path, FS_MAX_PATH-1); @@ -84,9 +82,7 @@ namespace sts::ncm { Result ReadOnlyContentStorageInterface::Has(Out out, ContentId content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; this->make_content_path_func(content_path, content_id, this->root_path); @@ -106,9 +102,7 @@ namespace sts::ncm { Result ReadOnlyContentStorageInterface::GetPath(OutPointerWithServerSize out, ContentId content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; char common_path[FS_MAX_PATH] = {0}; @@ -160,9 +154,7 @@ namespace sts::ncm { Result ReadOnlyContentStorageInterface::GetSizeFromContentId(Out out_size, ContentId content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; bool is_content_file = false; @@ -210,9 +202,7 @@ namespace sts::ncm { return ResultNcmInvalidOffset; } - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); char content_path[FS_MAX_PATH] = {0}; bool is_content_file = false; @@ -251,9 +241,7 @@ namespace sts::ncm { Result ReadOnlyContentStorageInterface::GetRightsIdFromContentId(Out out_rights_id, Out out_key_generation, ContentId content_id) { R_DEBUG_START - if (this->disabled) { - return ResultNcmInvalidContentStorage; - } + R_TRY(this->EnsureEnabled()); FsRightsId rights_id = {0}; u8 key_generation = 0;