From 7f7a6e130427f1ec1723d0ab6cc4a242343a7cac Mon Sep 17 00:00:00 2001 From: Adubbz Date: Mon, 2 Mar 2020 21:40:44 +1100 Subject: [PATCH] ncm: move content meta database impl out of interface file --- .../ncm/ncm_i_content_meta_database.hpp | 21 ----- .../ncm/source/impl/ncm_content_manager.cpp | 7 +- ...cpp => ncm_content_meta_database_impl.cpp} | 80 ++++++------------- ...hpp => ncm_content_meta_database_impl.hpp} | 17 +--- .../ncm_content_meta_database_impl_base.hpp | 48 +++++++++++ ...m_on_memory_content_meta_database_impl.cpp | 49 ++++++++++++ ...m_on_memory_content_meta_database_impl.hpp | 34 ++++++++ 7 files changed, 165 insertions(+), 91 deletions(-) rename stratosphere/ncm/source/{ncm_content_meta_database.cpp => ncm_content_meta_database_impl.cpp} (81%) rename stratosphere/ncm/source/{ncm_content_meta_database.hpp => ncm_content_meta_database_impl.hpp} (80%) create mode 100644 stratosphere/ncm/source/ncm_content_meta_database_impl_base.hpp create mode 100644 stratosphere/ncm/source/ncm_on_memory_content_meta_database_impl.cpp create mode 100644 stratosphere/ncm/source/ncm_on_memory_content_meta_database_impl.hpp diff --git a/libraries/libstratosphere/include/stratosphere/ncm/ncm_i_content_meta_database.hpp b/libraries/libstratosphere/include/stratosphere/ncm/ncm_i_content_meta_database.hpp index 0bc223203..38d64afda 100644 --- a/libraries/libstratosphere/include/stratosphere/ncm/ncm_i_content_meta_database.hpp +++ b/libraries/libstratosphere/include/stratosphere/ncm/ncm_i_content_meta_database.hpp @@ -46,27 +46,6 @@ namespace ams::ncm { GetRequiredApplicationVersion = 19, GetContentIdByTypeAndIdOffset = 20, }; - protected: - ams::kvdb::MemoryKeyValueStore *kvs; - char mount_name[16]; - bool disabled; - protected: - Result EnsureEnabled() { - R_UNLESS(!this->disabled, ncm::ResultInvalidContentMetaDatabase()); - return ResultSuccess(); - } - public: - IContentMetaDatabase(ams::kvdb::MemoryKeyValueStore *kvs) : - kvs(kvs), disabled(false) - { - /* ... */ - } - - IContentMetaDatabase(ams::kvdb::MemoryKeyValueStore *kvs, const char *mount_name) : - IContentMetaDatabase(kvs) - { - std::strcpy(this->mount_name, mount_name); - } public: /* Actual commands. */ virtual Result Set(ContentMetaKey key, sf::InBuffer value) = 0; diff --git a/stratosphere/ncm/source/impl/ncm_content_manager.cpp b/stratosphere/ncm/source/impl/ncm_content_manager.cpp index 57491163c..342ef813e 100644 --- a/stratosphere/ncm/source/impl/ncm_content_manager.cpp +++ b/stratosphere/ncm/source/impl/ncm_content_manager.cpp @@ -19,7 +19,8 @@ #include #include -#include "../ncm_content_meta_database.hpp" +#include "../ncm_content_meta_database_impl.hpp" +#include "../ncm_on_memory_content_meta_database_impl.hpp" #include "../ncm_content_storage_impl.hpp" #include "../ncm_fs.hpp" #include "../ncm_make_path.hpp" @@ -585,12 +586,12 @@ namespace ams::ncm::impl { auto mount_guard = SCOPE_GUARD { fs::Unmount(entry->mount_point); }; R_TRY(entry->kvs->Initialize(entry->meta_path, entry->max_content_metas)); R_TRY(entry->kvs->Load()); - entry->content_meta_database = std::make_shared(std::addressof(*entry->kvs), entry->mount_point); + entry->content_meta_database = std::make_shared(std::addressof(*entry->kvs), entry->mount_point); mount_guard.Cancel(); } else { R_TRY(entry->kvs->Initialize(entry->max_content_metas)); R_TRY(entry->kvs->Load()); - entry->content_meta_database = std::make_shared(std::addressof(*entry->kvs)); + entry->content_meta_database = std::make_shared(std::addressof(*entry->kvs)); } return ResultSuccess(); diff --git a/stratosphere/ncm/source/ncm_content_meta_database.cpp b/stratosphere/ncm/source/ncm_content_meta_database_impl.cpp similarity index 81% rename from stratosphere/ncm/source/ncm_content_meta_database.cpp rename to stratosphere/ncm/source/ncm_content_meta_database_impl.cpp index 3d794cb09..0c75a2f8e 100644 --- a/stratosphere/ncm/source/ncm_content_meta_database.cpp +++ b/stratosphere/ncm/source/ncm_content_meta_database_impl.cpp @@ -14,7 +14,7 @@ * along with this program. If not, see . */ -#include "ncm_content_meta_database.hpp" +#include "ncm_content_meta_database_impl.hpp" #include "ncm_utils.hpp" namespace ams::ncm { @@ -83,7 +83,7 @@ namespace ams::ncm { } - Result ContentMetaDatabaseInterface::GetContentIdByTypeImpl(ContentId *out, const ContentMetaKey& key, ContentType type, std::optional id_offset) { + Result ContentMetaDatabaseImpl::GetContentIdByTypeImpl(ContentId *out, const ContentMetaKey& key, ContentType type, std::optional id_offset) { R_TRY(this->EnsureEnabled()); const auto it = this->kvs->lower_bound(key); @@ -129,7 +129,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetLatestContentMetaKeyImpl(ContentMetaKey *out_key, ProgramId id) { + Result ContentMetaDatabaseImpl::GetLatestContentMetaKeyImpl(ContentMetaKey *out_key, ProgramId id) { R_TRY(this->EnsureEnabled()); ContentMetaKey key = {0}; @@ -148,26 +148,26 @@ namespace ams::ncm { return ncm::ResultContentMetaNotFound(); } - Result ContentMetaDatabaseInterface::Set(ContentMetaKey key, sf::InBuffer value) { + Result ContentMetaDatabaseImpl::Set(ContentMetaKey key, sf::InBuffer value) { R_TRY(this->EnsureEnabled()); return this->kvs->Set(key, value.GetPointer(), value.GetSize()); } - Result ContentMetaDatabaseInterface::Get(sf::Out out_size, ContentMetaKey key, sf::OutBuffer out_value) { + Result ContentMetaDatabaseImpl::Get(sf::Out out_size, ContentMetaKey key, sf::OutBuffer out_value) { R_TRY(this->EnsureEnabled()); return this->kvs->Get(out_size.GetPointer(), out_value.GetPointer(), out_value.GetSize(), key); } - Result ContentMetaDatabaseInterface::Remove(ContentMetaKey key) { + Result ContentMetaDatabaseImpl::Remove(ContentMetaKey key) { R_TRY(this->EnsureEnabled()); return this->kvs->Remove(key); } - Result ContentMetaDatabaseInterface::GetContentIdByType(sf::Out out_content_id, ContentMetaKey key, ContentType type) { + Result ContentMetaDatabaseImpl::GetContentIdByType(sf::Out out_content_id, ContentMetaKey key, ContentType type) { return this->GetContentIdByTypeImpl(out_content_id.GetPointer(), key, type, std::nullopt); } - Result ContentMetaDatabaseInterface::ListContentInfo(sf::Out out_count, const sf::OutArray &out_info, ContentMetaKey key, u32 offset) { + Result ContentMetaDatabaseImpl::ListContentInfo(sf::Out out_count, const sf::OutArray &out_info, ContentMetaKey key, u32 offset) { R_UNLESS(offset <= std::numeric_limits::max(), ncm::ResultInvalidOffset()); R_TRY(this->EnsureEnabled()); @@ -185,7 +185,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::List(sf::Out out_entries_total, sf::Out out_entries_written, const sf::OutArray &out_info, ContentMetaType type, ProgramId application_program_id, ProgramId program_id_min, ProgramId program_id_max, ContentInstallType install_type) { + Result ContentMetaDatabaseImpl::List(sf::Out out_entries_total, sf::Out out_entries_written, const sf::OutArray &out_info, ContentMetaType type, ProgramId application_program_id, ProgramId program_id_min, ProgramId program_id_max, ContentInstallType install_type) { R_TRY(this->EnsureEnabled()); size_t entries_total = 0; @@ -243,12 +243,12 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetLatestContentMetaKey(sf::Out out_key, ProgramId program_id) { + Result ContentMetaDatabaseImpl::GetLatestContentMetaKey(sf::Out out_key, ProgramId program_id) { R_TRY(this->EnsureEnabled()); return this->GetLatestContentMetaKeyImpl(out_key.GetPointer(), program_id); } - Result ContentMetaDatabaseInterface::ListApplication(sf::Out out_entries_total, sf::Out out_entries_written, const sf::OutArray &out_keys, ContentMetaType type) { + Result ContentMetaDatabaseImpl::ListApplication(sf::Out out_entries_total, sf::Out out_entries_written, const sf::OutArray &out_keys, ContentMetaType type) { R_TRY(this->EnsureEnabled()); size_t entries_total = 0; @@ -300,7 +300,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::Has(sf::Out out, const ContentMetaKey &key) { + Result ContentMetaDatabaseImpl::Has(sf::Out out, const ContentMetaKey &key) { R_TRY(this->EnsureEnabled()); if (this->kvs->GetCount() == 0) { @@ -318,7 +318,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::HasAll(sf::Out out, const sf::InArray &keys) { + Result ContentMetaDatabaseImpl::HasAll(sf::Out out, const sf::InArray &keys) { R_TRY(this->EnsureEnabled()); for (size_t i = 0; i < keys.GetSize(); i++) { bool has; @@ -330,7 +330,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetSize(sf::Out out_size, ContentMetaKey key) { + Result ContentMetaDatabaseImpl::GetSize(sf::Out out_size, ContentMetaKey key) { R_TRY(this->EnsureEnabled()); R_UNLESS(this->kvs->GetCount() != 0, ncm::ResultContentMetaNotFound()); @@ -342,7 +342,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetRequiredSystemVersion(sf::Out out_version, ContentMetaKey key) { + Result ContentMetaDatabaseImpl::GetRequiredSystemVersion(sf::Out out_version, ContentMetaKey key) { R_TRY(this->EnsureEnabled()); R_UNLESS(key.type == ContentMetaType::Application || key.type == ContentMetaType::Patch, ncm::ResultInvalidContentMetaKey()); @@ -357,7 +357,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetPatchId(sf::Out out_patch_id, ContentMetaKey key) { + Result ContentMetaDatabaseImpl::GetPatchId(sf::Out out_patch_id, ContentMetaKey key) { R_TRY(this->EnsureEnabled()); R_UNLESS(key.type == ContentMetaType::Application, ncm::ResultInvalidContentMetaKey()); @@ -370,12 +370,12 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::DisableForcibly() { + Result ContentMetaDatabaseImpl::DisableForcibly() { this->disabled = true; return ResultSuccess(); } - Result ContentMetaDatabaseInterface::LookupOrphanContent(const sf::OutArray &out_orphaned, const sf::InArray &content_ids) { + Result ContentMetaDatabaseImpl::LookupOrphanContent(const sf::OutArray &out_orphaned, const sf::InArray &content_ids) { R_TRY(this->EnsureEnabled()); R_UNLESS(out_orphaned.GetSize() >= content_ids.GetSize(), ncm::ResultBufferInsufficient()); @@ -417,14 +417,14 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::Commit() { + Result ContentMetaDatabaseImpl::Commit() { R_TRY(this->EnsureEnabled()); R_TRY(this->kvs->Save()); R_TRY(fsdevCommitDevice(this->mount_name)); return ResultSuccess(); } - Result ContentMetaDatabaseInterface::HasContent(sf::Out out, ContentMetaKey key, ContentId content_id) { + Result ContentMetaDatabaseImpl::HasContent(sf::Out out, ContentMetaKey key, ContentId content_id) { const ContentMetaHeader *header = nullptr; size_t value_size = 0; R_TRY(GetContentMetaValuePointer(&header, &value_size, key, this->kvs)); @@ -445,7 +445,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::ListContentMetaInfo(sf::Out out_entries_written, const sf::OutArray &out_meta_info, ContentMetaKey key, u32 start_index) { + Result ContentMetaDatabaseImpl::ListContentMetaInfo(sf::Out out_entries_written, const sf::OutArray &out_meta_info, ContentMetaKey key, u32 start_index) { R_UNLESS(start_index <= std::numeric_limits::max(), ncm::ResultInvalidOffset()); R_TRY(this->EnsureEnabled()); @@ -474,7 +474,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetAttributes(sf::Out out_attributes, ContentMetaKey key) { + Result ContentMetaDatabaseImpl::GetAttributes(sf::Out out_attributes, ContentMetaKey key) { R_TRY(this->EnsureEnabled()); const ContentMetaHeader *header = nullptr; @@ -484,7 +484,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetRequiredApplicationVersion(sf::Out out_version, ContentMetaKey key) { + Result ContentMetaDatabaseImpl::GetRequiredApplicationVersion(sf::Out out_version, ContentMetaKey key) { R_TRY(this->EnsureEnabled()); const ContentMetaHeader *value = nullptr; @@ -504,14 +504,14 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetContentIdByTypeAndIdOffset(sf::Out out_content_id, ContentMetaKey key, ContentType type, u8 id_offset) { + Result ContentMetaDatabaseImpl::GetContentIdByTypeAndIdOffset(sf::Out out_content_id, ContentMetaKey key, ContentType type, u8 id_offset) { ContentId content_id; R_TRY(this->GetContentIdByTypeImpl(&content_id, key, type, std::optional(id_offset))); out_content_id.SetValue(content_id); return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetLatestProgram(ContentId *out_content_id, ProgramId program_id) { + Result ContentMetaDatabaseImpl::GetLatestProgram(ContentId *out_content_id, ProgramId program_id) { ContentMetaKey key; R_TRY(this->GetLatestContentMetaKey(&key, program_id)); @@ -519,7 +519,7 @@ namespace ams::ncm { return ResultSuccess(); } - Result ContentMetaDatabaseInterface::GetLatestData(ContentId *out_content_id, ProgramId program_id) { + Result ContentMetaDatabaseImpl::GetLatestData(ContentId *out_content_id, ProgramId program_id) { ContentMetaKey key; R_TRY(this->GetLatestContentMetaKey(&key, program_id)); @@ -527,32 +527,4 @@ namespace ams::ncm { return ResultSuccess(); } - Result OnMemoryContentMetaDatabaseInterface::GetLatestContentMetaKey(sf::Out out_key, ProgramId program_id) { - R_TRY(this->EnsureEnabled()); - - const ContentMetaKey key = ContentMetaKey::Make(program_id, 0, ContentMetaType::Unknown); - - std::optional found_key; - for (auto entry = this->kvs->lower_bound(key); entry != this->kvs->end(); entry++) { - if (entry->GetKey().id != program_id) { - break; - } - - found_key = entry->GetKey(); - } - - R_UNLESS(found_key, ncm::ResultContentMetaNotFound()); - *out_key = *found_key; - return ResultSuccess(); - } - - Result OnMemoryContentMetaDatabaseInterface::LookupOrphanContent(const sf::OutArray &out_orphaned, const sf::InArray &content_ids) { - return ResultInvalidContentMetaDatabase(); - } - - Result OnMemoryContentMetaDatabaseInterface::Commit() { - R_TRY(this->EnsureEnabled()); - return ResultSuccess(); - } - } diff --git a/stratosphere/ncm/source/ncm_content_meta_database.hpp b/stratosphere/ncm/source/ncm_content_meta_database_impl.hpp similarity index 80% rename from stratosphere/ncm/source/ncm_content_meta_database.hpp rename to stratosphere/ncm/source/ncm_content_meta_database_impl.hpp index 7231e1eec..6a1741bbc 100644 --- a/stratosphere/ncm/source/ncm_content_meta_database.hpp +++ b/stratosphere/ncm/source/ncm_content_meta_database_impl.hpp @@ -17,14 +17,15 @@ #pragma once #include #include +#include "ncm_content_meta_database_impl_base.hpp" namespace ams::ncm { - class ContentMetaDatabaseInterface : public IContentMetaDatabase { + class ContentMetaDatabaseImpl : public ContentMetaDatabaseImplBase { public: - ContentMetaDatabaseInterface(ams::kvdb::MemoryKeyValueStore *kvs, const char *mount_name) : IContentMetaDatabase(kvs, mount_name) { + ContentMetaDatabaseImpl(ams::kvdb::MemoryKeyValueStore *kvs, const char *mount_name) : ContentMetaDatabaseImplBase(kvs, mount_name) { } - ContentMetaDatabaseInterface(ams::kvdb::MemoryKeyValueStore *kvs) : IContentMetaDatabase(kvs) { + ContentMetaDatabaseImpl(ams::kvdb::MemoryKeyValueStore *kvs) : ContentMetaDatabaseImplBase(kvs) { } private: Result GetContentIdByTypeImpl(ContentId *out, const ContentMetaKey& key, ContentType type, std::optional id_offset); @@ -57,14 +58,4 @@ namespace ams::ncm { virtual Result GetLatestData(ContentId *out_content_id, ProgramId program_id) override; }; - class OnMemoryContentMetaDatabaseInterface : public ContentMetaDatabaseInterface { - public: - OnMemoryContentMetaDatabaseInterface(ams::kvdb::MemoryKeyValueStore *kvs) : ContentMetaDatabaseInterface(kvs) { - } - public: - virtual Result GetLatestContentMetaKey(sf::Out out_key, ProgramId id) override; - virtual Result LookupOrphanContent(const sf::OutArray &out_orphaned, const sf::InArray &content_ids) override; - virtual Result Commit() override; - }; - } diff --git a/stratosphere/ncm/source/ncm_content_meta_database_impl_base.hpp b/stratosphere/ncm/source/ncm_content_meta_database_impl_base.hpp new file mode 100644 index 000000000..799672bf2 --- /dev/null +++ b/stratosphere/ncm/source/ncm_content_meta_database_impl_base.hpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019-2020 Adubbz, Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include + +namespace ams::ncm { + + class ContentMetaDatabaseImplBase : public IContentMetaDatabase { + NON_COPYABLE(ContentMetaDatabaseImplBase); + NON_MOVEABLE(ContentMetaDatabaseImplBase); + protected: + ams::kvdb::MemoryKeyValueStore *kvs; + char mount_name[16]; + bool disabled; + protected: + ContentMetaDatabaseImplBase(ams::kvdb::MemoryKeyValueStore *kvs) : + kvs(kvs), disabled(false) + { + /* ... */ + } + + ContentMetaDatabaseImplBase(ams::kvdb::MemoryKeyValueStore *kvs, const char *mount_name) : + ContentMetaDatabaseImplBase(kvs) + { + std::strcpy(this->mount_name, mount_name); + } + protected: + Result EnsureEnabled() { + R_UNLESS(!this->disabled, ncm::ResultInvalidContentMetaDatabase()); + return ResultSuccess(); + } + }; + +} \ No newline at end of file diff --git a/stratosphere/ncm/source/ncm_on_memory_content_meta_database_impl.cpp b/stratosphere/ncm/source/ncm_on_memory_content_meta_database_impl.cpp new file mode 100644 index 000000000..21d55030e --- /dev/null +++ b/stratosphere/ncm/source/ncm_on_memory_content_meta_database_impl.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019-2020 Adubbz, Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "ncm_on_memory_content_meta_database_impl.hpp" + +namespace ams::ncm { + + Result OnMemoryContentMetaDatabaseImpl::GetLatestContentMetaKey(sf::Out out_key, ProgramId program_id) { + R_TRY(this->EnsureEnabled()); + + const ContentMetaKey key = ContentMetaKey::Make(program_id, 0, ContentMetaType::Unknown); + + std::optional found_key; + for (auto entry = this->kvs->lower_bound(key); entry != this->kvs->end(); entry++) { + if (entry->GetKey().id != program_id) { + break; + } + + found_key = entry->GetKey(); + } + + R_UNLESS(found_key, ncm::ResultContentMetaNotFound()); + *out_key = *found_key; + return ResultSuccess(); + } + + Result OnMemoryContentMetaDatabaseImpl::LookupOrphanContent(const sf::OutArray &out_orphaned, const sf::InArray &content_ids) { + return ResultInvalidContentMetaDatabase(); + } + + Result OnMemoryContentMetaDatabaseImpl::Commit() { + R_TRY(this->EnsureEnabled()); + return ResultSuccess(); + } + +} diff --git a/stratosphere/ncm/source/ncm_on_memory_content_meta_database_impl.hpp b/stratosphere/ncm/source/ncm_on_memory_content_meta_database_impl.hpp new file mode 100644 index 000000000..f142f002c --- /dev/null +++ b/stratosphere/ncm/source/ncm_on_memory_content_meta_database_impl.hpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019-2020 Adubbz, Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include +#include "ncm_content_meta_database_impl.hpp" + +namespace ams::ncm { + + class OnMemoryContentMetaDatabaseImpl : public ContentMetaDatabaseImpl { + public: + OnMemoryContentMetaDatabaseImpl(ams::kvdb::MemoryKeyValueStore *kvs) : ContentMetaDatabaseImpl(kvs) { + } + public: + virtual Result GetLatestContentMetaKey(sf::Out out_key, ProgramId id) override; + virtual Result LookupOrphanContent(const sf::OutArray &out_orphaned, const sf::InArray &content_ids) override; + virtual Result Commit() override; + }; + +} \ No newline at end of file