From e1a5e3115285334553600fe10774608b18e9fbbb Mon Sep 17 00:00:00 2001 From: Adubbz Date: Wed, 4 Mar 2020 20:11:32 +1100 Subject: [PATCH] ncm: move to content manager impl --- .../ncm/source/impl/ncm_content_manager.cpp | 626 ------------------ .../ncm/source/impl/ncm_content_manager.hpp | 45 -- .../ncm/source/ncm_content_manager_impl.cpp | 399 ++++++++++- .../ncm/source/ncm_content_manager_impl.hpp | 177 +++++ stratosphere/ncm/source/ncm_main.cpp | 9 +- 5 files changed, 559 insertions(+), 697 deletions(-) delete mode 100644 stratosphere/ncm/source/impl/ncm_content_manager.cpp delete mode 100644 stratosphere/ncm/source/impl/ncm_content_manager.hpp diff --git a/stratosphere/ncm/source/impl/ncm_content_manager.cpp b/stratosphere/ncm/source/impl/ncm_content_manager.cpp deleted file mode 100644 index fce9ad590..000000000 --- a/stratosphere/ncm/source/impl/ncm_content_manager.cpp +++ /dev/null @@ -1,626 +0,0 @@ -/* - * 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 -#include -#include -#include - -#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" -#include "../ncm_read_only_content_storage_impl.hpp" -#include "ncm_content_manager.hpp" -#include "ncm_rights_cache.hpp" - -namespace ams::ncm::impl { - - namespace { - - struct ContentStorageRoot { - NON_COPYABLE(ContentStorageRoot); - NON_MOVEABLE(ContentStorageRoot); - - char mount_point[16]; - char path[128]; - StorageId storage_id; - FsContentStorageId content_storage_id; - std::shared_ptr content_storage; - - inline ContentStorageRoot() : storage_id(StorageId::None), - content_storage_id(FsContentStorageId_System), content_storage(nullptr) { - mount_point[0] = '\0'; - path[0] = '\0'; - } - - inline void Initialize(StorageId storage_id, FsContentStorageId content_storage_id) { - this->storage_id = storage_id; - this->content_storage_id = content_storage_id; - this->content_storage = nullptr; - MountName mount_name = fs::CreateUniqueMountName(); - std::strcpy(this->mount_point, mount_name.name); - snprintf(this->path, 0x80, "%s:/", this->mount_point); - } - }; - - struct SaveDataMeta { - u64 id; - u64 size; - u64 journal_size; - u32 flags; - FsSaveDataSpaceId space_id; - }; - - static_assert(sizeof(SaveDataMeta) == 0x20, "SaveDataMeta definition!"); - - constexpr u64 BuiltInSystemSaveDataId = 0x8000000000000120; - constexpr u64 BuiltInSystemSaveDataSize = 0x6c000; - constexpr u64 BuiltInSystemSaveDataJournalSize = 0x6c000; - constexpr u32 BuiltInSystemSaveDataFlags = FsSaveDataFlags_KeepAfterResettingSystemSaveData | FsSaveDataFlags_KeepAfterRefurbishment; - - constexpr SaveDataMeta BuiltInSystemSaveDataMeta = { - .id = BuiltInSystemSaveDataId, - .size = BuiltInSystemSaveDataSize, - .journal_size = BuiltInSystemSaveDataJournalSize, - .flags = BuiltInSystemSaveDataFlags, - .space_id = FsSaveDataSpaceId_System - }; - - constexpr u64 BuiltInUserSaveDataId = 0x8000000000000121; - constexpr u64 BuiltInUserSaveDataSize = 0x29e000; - constexpr u64 BuiltInUserSaveDataJournalSize = 0x29e000; - constexpr u32 BuiltInUserSaveDataFlags = 0; - - constexpr SaveDataMeta BuiltInUserSaveDataMeta = { - .id = BuiltInUserSaveDataId, - .size = BuiltInUserSaveDataSize, - .journal_size = BuiltInUserSaveDataJournalSize, - .flags = BuiltInUserSaveDataFlags, - .space_id = FsSaveDataSpaceId_System - }; - - constexpr u64 SdCardSaveDataId = 0x8000000000000124; - constexpr u64 SdCardSaveDataSize = 0xa08000; - constexpr u64 SdCardSaveDataJournalSize = 0xa08000; - constexpr u32 SdCardSaveDataFlags = 0; - - constexpr SaveDataMeta SdCardSaveDataMeta = { - .id = SdCardSaveDataId, - .size = SdCardSaveDataSize, - .journal_size = SdCardSaveDataJournalSize, - .flags = SdCardSaveDataFlags, - .space_id = FsSaveDataSpaceId_SdSystem, - }; - - struct ContentMetaDatabaseEntry { - NON_COPYABLE(ContentMetaDatabaseEntry); - NON_MOVEABLE(ContentMetaDatabaseEntry); - - char mount_point[16]; - char meta_path[128]; - StorageId storage_id; - SaveDataMeta save_meta; - std::shared_ptr content_meta_database; - std::optional> kvs; - u32 max_content_metas; - - inline ContentMetaDatabaseEntry() : storage_id(StorageId::None), save_meta({0}), - content_meta_database(nullptr), kvs(std::nullopt), max_content_metas(0) { - mount_point[0] = '\0'; - meta_path[0] = '\0'; - } - - Result Initialize(StorageId storage_id, const SaveDataMeta& save_meta, size_t max_content_metas) { - this->storage_id = storage_id; - this->max_content_metas = max_content_metas; - this->save_meta = save_meta; - this->content_meta_database = nullptr; - this->kvs = std::nullopt; - MountName mount_name = fs::CreateUniqueMountName(); - strcpy(this->mount_point, mount_name.name); - this->mount_point[0] = '#'; - snprintf(this->meta_path, 0x80, "%s:/meta", this->mount_point); - return ResultSuccess(); - } - - Result InitializeGameCard(size_t max_content_metas) { - this->storage_id = StorageId::GameCard; - this->max_content_metas = max_content_metas; - this->content_meta_database = nullptr; - this->kvs = std::nullopt; - return ResultSuccess(); - } - }; - - constexpr size_t MaxBuiltInSystemContentMetaCount = 0x800; - constexpr size_t MaxBuiltInUserContentMetaCount = 0x2000; - constexpr size_t MaxSdCardContentMetaCount = 0x2000; - - constexpr size_t MaxContentStorageEntries = 8; - constexpr size_t MaxContentMetaDatabaseEntries = 8; - - os::Mutex g_mutex; - bool g_initialized = false; - ContentStorageRoot g_content_storage_roots[MaxContentStorageEntries]; - ContentMetaDatabaseEntry g_content_meta_entries[MaxContentMetaDatabaseEntries]; - u32 g_num_content_storage_entries; - u32 g_num_content_meta_entries; - RightsIdCache g_rights_id_cache; - - constexpr inline bool IsUniqueStorage(StorageId id) { - return id != StorageId::None && id != StorageId::Any; - } - - Result FindContentStorageRoot(ContentStorageRoot **out, StorageId storage_id) { - for (size_t i = 0; i < MaxContentStorageEntries; i++) { - ContentStorageRoot *root = &g_content_storage_roots[i]; - - if (root->storage_id == storage_id) { - *out = root; - return ResultSuccess(); - } - } - - return ncm::ResultUnknownStorage(); - } - - Result GetUniqueContentStorageRoot(ContentStorageRoot **out, StorageId storage_id) { - R_UNLESS(IsUniqueStorage(storage_id), ncm::ResultUnknownStorage()); - return FindContentStorageRoot(out, storage_id); - } - - Result FindContentMetaDatabaseEntry(ContentMetaDatabaseEntry **out, StorageId storage_id) { - for (size_t i = 0; i < MaxContentMetaDatabaseEntries; i++) { - ContentMetaDatabaseEntry *entry = &g_content_meta_entries[i]; - - if (entry->storage_id == storage_id) { - *out = entry; - return ResultSuccess(); - } - } - - return ncm::ResultUnknownStorage(); - } - - Result GetUniqueContentMetaDatabaseEntry(ContentMetaDatabaseEntry **out, StorageId storage_id) { - R_UNLESS(IsUniqueStorage(storage_id), ncm::ResultUnknownStorage()); - return FindContentMetaDatabaseEntry(out, storage_id); - } - - ALWAYS_INLINE Result GetContentStorageNotActiveResult(StorageId storage_id) { - switch (storage_id) { - case StorageId::GameCard: - return ResultGameCardContentStorageNotActive(); - case StorageId::BuiltInSystem: - return ResultNandSystemContentStorageNotActive(); - case StorageId::BuiltInUser: - return ResultNandUserContentStorageNotActive(); - case StorageId::SdCard: - return ResultSdCardContentStorageNotActive(); - default: - return ResultUnknownContentStorageNotActive(); - } - } - - ALWAYS_INLINE Result GetContentMetaDatabaseNotActiveResult(StorageId storage_id) { - switch (storage_id) { - case StorageId::GameCard: - return ResultGameCardContentMetaDatabaseNotActive(); - case StorageId::BuiltInSystem: - return ResultNandSystemContentMetaDatabaseNotActive(); - case StorageId::BuiltInUser: - return ResultNandUserContentMetaDatabaseNotActive(); - case StorageId::SdCard: - return ResultSdCardContentMetaDatabaseNotActive(); - default: - return ResultUnknownContentMetaDatabaseNotActive(); - } - } - - Result EnsureAndMountSystemSaveData(const char *mount_name, const SaveDataMeta &save_meta) { - R_TRY_CATCH(fs::MountSystemSaveData(mount_name, save_meta.space_id, save_meta.id)) { - R_CATCH(ams::fs::ResultTargetNotFound) { - R_TRY(fsCreate_SystemSaveData(save_meta.space_id, save_meta.id, save_meta.size, save_meta.journal_size, save_meta.flags)); - R_TRY(fs::MountSystemSaveData(mount_name, save_meta.space_id, save_meta.id)); - } - } R_END_TRY_CATCH; - return ResultSuccess(); - } - - inline void ReplaceMountName(char *out_path, const char *mount_name, const char *root_path) { - strcpy(out_path, mount_name); - strcat(out_path, strchr(root_path, ':')); - } - - } - - Result InitializeContentManager() { - std::scoped_lock lk(g_mutex); - - /* Already initialized. */ - R_UNLESS(!g_initialized, ResultSuccess()); - - for (size_t i = 0; i < MaxContentStorageEntries; i++) { - ContentStorageRoot *entry = &g_content_storage_roots[i]; - entry->storage_id = StorageId::None; - } - - for (size_t i = 0; i < MaxContentMetaDatabaseEntries; i++) { - ContentMetaDatabaseEntry *entry = &g_content_meta_entries[i]; - entry->storage_id = StorageId::None; - } - - /* First, setup the BuiltInSystem storage entry. */ - g_content_storage_roots[g_num_content_storage_entries++].Initialize(StorageId::BuiltInSystem, FsContentStorageId_System); - - if (R_FAILED(impl::VerifyContentStorage(StorageId::BuiltInSystem))) { - R_TRY(impl::CreateContentStorage(StorageId::BuiltInSystem)); - } - - R_TRY(impl::ActivateContentStorage(StorageId::BuiltInSystem)); - - /* Next, the BuiltInSystem content meta entry. */ - R_TRY(g_content_meta_entries[g_num_content_meta_entries++].Initialize(StorageId::BuiltInSystem, BuiltInSystemSaveDataMeta, MaxBuiltInSystemContentMetaCount)); - - if (R_FAILED(impl::VerifyContentMetaDatabase(StorageId::BuiltInSystem))) { - R_TRY(impl::CreateContentMetaDatabase(StorageId::BuiltInSystem)); - - /* TODO: N supports a number of unused modes here, we don't bother implementing them currently. */ - } - - u32 current_flags = 0; - if (hos::GetVersion() >= hos::Version_200 && R_SUCCEEDED(fs::GetSaveDataFlags(¤t_flags, BuiltInSystemSaveDataId)) && current_flags != (FsSaveDataFlags_KeepAfterResettingSystemSaveData | FsSaveDataFlags_KeepAfterRefurbishment)) { - fs::SetSaveDataFlags(BuiltInSystemSaveDataId, FsSaveDataSpaceId_System, FsSaveDataFlags_KeepAfterResettingSystemSaveData | FsSaveDataFlags_KeepAfterRefurbishment); - } - - R_TRY(impl::ActivateContentMetaDatabase(StorageId::BuiltInSystem)); - - /* Now for BuiltInUser's content storage and content meta entries. */ - g_content_storage_roots[g_num_content_storage_entries++].Initialize(StorageId::BuiltInUser, FsContentStorageId_User); - R_TRY(g_content_meta_entries[g_num_content_meta_entries++].Initialize(StorageId::BuiltInUser, BuiltInUserSaveDataMeta, MaxBuiltInUserContentMetaCount)); - - /* Beyond this point N no longer appears to bother */ - /* incrementing the count for content storage entries or content meta entries. */ - - /* Next SdCard's content storage and content meta entries. */ - g_content_storage_roots[2].Initialize(StorageId::SdCard, FsContentStorageId_SdCard); - R_TRY(g_content_meta_entries[2].Initialize(StorageId::SdCard, SdCardSaveDataMeta, MaxSdCardContentMetaCount)); - - /* GameCard's content storage and content meta entries. */ - /* N doesn't set a content storage id for game cards, so we'll just use 0 (System). */ - g_content_storage_roots[3].Initialize(StorageId::GameCard, FsContentStorageId_System); - R_TRY(g_content_meta_entries[3].InitializeGameCard(0x800)); - - g_initialized = true; - return ResultSuccess(); - } - - void FinalizeContentManager() { - { - std::scoped_lock lk(g_mutex); - - for (size_t i = 0; i < MaxContentStorageEntries; i++) { - ContentStorageRoot *entry = &g_content_storage_roots[i]; - impl::InactivateContentStorage(entry->storage_id); - } - - for (size_t i = 0; i < MaxContentMetaDatabaseEntries; i++) { - ContentMetaDatabaseEntry *entry = &g_content_meta_entries[i]; - impl::InactivateContentMetaDatabase(entry->storage_id); - } - } - - for (size_t i = 0; i < MaxContentMetaDatabaseEntries; i++) { - ContentMetaDatabaseEntry *entry = &g_content_meta_entries[i]; - entry->kvs.reset(); - } - - for (size_t i = 0; i < MaxContentStorageEntries; i++) { - ContentStorageRoot *entry = &g_content_storage_roots[i]; - entry->content_storage = nullptr; - } - } - - Result CreateContentStorage(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - ContentStorageRoot *root; - R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); - - R_TRY(fs::MountContentStorage(root->mount_point, root->content_storage_id)); - ON_SCOPE_EXIT { fs::Unmount(root->mount_point); }; - - R_TRY(fs::EnsureDirectoryRecursively(root->path)); - R_TRY(fs::EnsureContentAndPlaceHolderRoot(root->path)); - - return ResultSuccess(); - } - - Result VerifyContentStorage(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - ContentStorageRoot *root; - R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); - - char mount_root[0x80] = {}; - auto mount_name = fs::CreateUniqueMountName(); /* should this be fs::? should it be ncm::? ncm::impl? */ - ReplaceMountName(mount_root, mount_name.name, root->path); - - R_TRY(fs::MountContentStorage(mount_name.name, root->content_storage_id)); - ON_SCOPE_EXIT { fs::Unmount(mount_name.name); }; - - R_TRY(fs::CheckContentStorageDirectoriesExist(mount_root)); - - return ResultSuccess(); - } - - Result OpenContentStorage(std::shared_ptr *out, StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - ContentStorageRoot *root; - R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); - - auto content_storage = root->content_storage; - - if (hos::GetVersion() >= hos::Version_200) { - R_UNLESS(content_storage, GetContentStorageNotActiveResult(storage_id)); - } else { - /* 1.0.0 activates content storages as soon as they are opened. */ - if (!content_storage) { - R_TRY(impl::ActivateContentStorage(storage_id)); - content_storage = root->content_storage; - } - } - - *out = std::move(content_storage); - return ResultSuccess(); - } - - Result CloseContentStorageForcibly(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - R_UNLESS(storage_id != StorageId::None, ncm::ResultUnknownStorage()); - ContentStorageRoot *root; - R_TRY(FindContentStorageRoot(std::addressof(root), storage_id)); - - if (root->content_storage) { - /* N doesn't bother checking the result of this */ - root->content_storage->DisableForcibly(); - fs::Unmount(root->mount_point); - root->content_storage = nullptr; - } - - return ResultSuccess(); - } - - Result ActivateContentStorage(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - ContentStorageRoot *root; - R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); - - /* Already activated. */ - R_UNLESS(root->content_storage == nullptr, ResultSuccess()); - - if (storage_id == StorageId::GameCard) { - FsGameCardHandle gc_hnd; - R_TRY(fs::GetGameCardHandle(&gc_hnd)); - R_TRY(fs::MountGameCardPartition(root->mount_point, gc_hnd, FsGameCardPartition_Secure)); - } else { - R_TRY(fs::MountContentStorage(root->mount_point, root->content_storage_id)); - } - - auto mount_guard = SCOPE_GUARD { fs::Unmount(root->mount_point); }; - - if (storage_id == StorageId::GameCard) { - auto content_storage = std::make_shared(); - R_TRY(content_storage->Initialize(root->path, path::MakeContentPathFlat)); - root->content_storage = std::move(content_storage); - } else { - MakeContentPathFunc content_path_func = nullptr; - MakePlaceHolderPathFunc placeholder_path_func = nullptr; - bool delay_flush = false; - auto content_storage = std::make_shared(); - - switch (storage_id) { - case StorageId::BuiltInSystem: - content_path_func = path::MakeContentPathFlat; - placeholder_path_func = path::MakePlaceHolderPathFlat; - break; - case StorageId::SdCard: - delay_flush = true; - default: - content_path_func = path::MakeContentPathHashByteLayered; - placeholder_path_func = path::MakePlaceHolderPathHashByteLayered; - break; - } - - R_TRY(content_storage->Initialize(root->path, content_path_func, placeholder_path_func, delay_flush, &g_rights_id_cache)); - root->content_storage = std::move(content_storage); - } - - mount_guard.Cancel(); - return ResultSuccess(); - } - - Result InactivateContentStorage(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - ContentStorageRoot *root; - R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); - - /* Already inactivated. */ - R_UNLESS(root->content_storage != nullptr, ResultSuccess()); - - root->content_storage->DisableForcibly(); - root->content_storage = nullptr; - fs::Unmount(root->mount_point); - return ResultSuccess(); - } - - Result CreateContentMetaDatabase(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - R_UNLESS(storage_id != StorageId::GameCard, ncm::ResultUnknownStorage()); - ContentMetaDatabaseEntry *entry; - R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); - - /* N doesn't bother checking the result of this. */ - fsDisableAutoSaveDataCreation(); - - R_TRY(EnsureAndMountSystemSaveData(entry->mount_point, entry->save_meta)); - ON_SCOPE_EXIT { fs::Unmount(entry->mount_point); }; - - R_TRY(fs::EnsureDirectoryRecursively(entry->meta_path)); - R_TRY(fsdevCommitDevice(entry->mount_point)); - - return ResultSuccess(); - } - - Result VerifyContentMetaDatabase(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - R_UNLESS(storage_id != StorageId::GameCard, ResultSuccess()); - ContentMetaDatabaseEntry *entry; - R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); - - auto mount_guard = SCOPE_GUARD { fs::Unmount(entry->mount_point); }; - if (!entry->content_meta_database) { - R_TRY(fs::MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)); - } else { - mount_guard.Cancel(); - } - - bool has_meta_path = false; - R_TRY(fs::HasDirectory(&has_meta_path, entry->meta_path)); - R_UNLESS(has_meta_path, ncm::ResultInvalidContentMetaDatabase()); - - return ResultSuccess(); - } - - Result OpenContentMetaDatabase(std::shared_ptr *out, StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - ContentMetaDatabaseEntry *entry; - R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); - - auto content_meta_db = entry->content_meta_database; - - if (hos::GetVersion() >= hos::Version_200) { - R_UNLESS(content_meta_db, GetContentMetaDatabaseNotActiveResult(storage_id)); - } else { - /* 1.0.0 activates content meta databases as soon as they are opened. */ - if (!content_meta_db) { - R_TRY(impl::ActivateContentMetaDatabase(storage_id)); - content_meta_db = entry->content_meta_database; - } - } - - *out = std::move(content_meta_db); - return ResultSuccess(); - } - - Result CloseContentMetaDatabaseForcibly(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - R_UNLESS(storage_id != StorageId::None, ncm::ResultUnknownStorage()); - ContentMetaDatabaseEntry *entry; - R_TRY(FindContentMetaDatabaseEntry(&entry, storage_id)); - - auto content_meta_db = entry->content_meta_database; - - if (content_meta_db) { - /* N doesn't bother checking the result of this */ - content_meta_db->DisableForcibly(); - - if (storage_id != StorageId::GameCard) { - fs::Unmount(entry->mount_point); - } - - entry->content_meta_database = nullptr; - entry->kvs = std::nullopt; - } - - return ResultSuccess(); - } - - Result CleanupContentMetaDatabase(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - ContentMetaDatabaseEntry *entry; - R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); - - R_TRY(fsDeleteSaveDataFileSystemBySaveDataSpaceId(entry->save_meta.space_id, entry->save_meta.id)); - return ResultSuccess(); - } - - Result ActivateContentMetaDatabase(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - ContentMetaDatabaseEntry *entry; - R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); - - /* Already activated. */ - 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(); - - if (storage_id != StorageId::GameCard) { - R_TRY(fs::MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)); - 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); - 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)); - } - - return ResultSuccess(); - } - - Result InactivateContentMetaDatabase(StorageId storage_id) { - std::scoped_lock lk(g_mutex); - - ContentMetaDatabaseEntry *entry; - R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); - - /* Already inactivated. */ - if (entry->content_meta_database != nullptr) { - entry->content_meta_database->DisableForcibly(); - entry->content_meta_database = nullptr; - /* This should lead to Index's destructor performing cleanup for us. */ - entry->kvs = std::nullopt; - - if (storage_id != StorageId::GameCard) { - fs::Unmount(entry->mount_point); - } - } - - return ResultSuccess(); - } - - Result InvalidateRightsIdCache() { - g_rights_id_cache.Invalidate(); - return ResultSuccess(); - } - -} diff --git a/stratosphere/ncm/source/impl/ncm_content_manager.hpp b/stratosphere/ncm/source/impl/ncm_content_manager.hpp deleted file mode 100644 index 7f68dda18..000000000 --- a/stratosphere/ncm/source/impl/ncm_content_manager.hpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 - -namespace ams::ncm::impl { - - /* Initialization/Finalization. */ - Result InitializeContentManager(); - void FinalizeContentManager(); - - /* Content Storage Management. */ - Result CreateContentStorage(StorageId storage_id); - Result VerifyContentStorage(StorageId storage_id); - Result OpenContentStorage(std::shared_ptr *out, StorageId storage_id); - Result CloseContentStorageForcibly(StorageId storage_id); - Result ActivateContentStorage(StorageId storage_id); - Result InactivateContentStorage(StorageId storage_id); - - /* Content Meta Database Management. */ - Result CreateContentMetaDatabase(StorageId storage_id); - Result VerifyContentMetaDatabase(StorageId storage_id); - Result OpenContentMetaDatabase(std::shared_ptr *out, StorageId storage_id); - Result CloseContentMetaDatabaseForcibly(StorageId storage_id); - Result CleanupContentMetaDatabase(StorageId storage_id); - Result ActivateContentMetaDatabase(StorageId storage_id); - Result InactivateContentMetaDatabase(StorageId storage_id); - Result InvalidateRightsIdCache(); - -} diff --git a/stratosphere/ncm/source/ncm_content_manager_impl.cpp b/stratosphere/ncm/source/ncm_content_manager_impl.cpp index 1f9708df1..d995521eb 100644 --- a/stratosphere/ncm/source/ncm_content_manager_impl.cpp +++ b/stratosphere/ncm/source/ncm_content_manager_impl.cpp @@ -15,70 +15,433 @@ */ #include "ncm_content_manager_impl.hpp" -#include "impl/ncm_content_manager.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_make_path.hpp" +#include "ncm_read_only_content_storage_impl.hpp" namespace ams::ncm { + namespace { + + constexpr u64 BuiltInSystemSaveDataId = 0x8000000000000120; + constexpr u64 BuiltInSystemSaveDataSize = 0x6c000; + constexpr u64 BuiltInSystemSaveDataJournalSize = 0x6c000; + constexpr u32 BuiltInSystemSaveDataFlags = FsSaveDataFlags_KeepAfterResettingSystemSaveData | FsSaveDataFlags_KeepAfterRefurbishment; + + constexpr SystemSaveDataInfo BuiltInSystemSystemSaveDataInfo = { + .id = BuiltInSystemSaveDataId, + .size = BuiltInSystemSaveDataSize, + .journal_size = BuiltInSystemSaveDataJournalSize, + .flags = BuiltInSystemSaveDataFlags, + .space_id = FsSaveDataSpaceId_System + }; + + constexpr u64 BuiltInUserSaveDataId = 0x8000000000000121; + constexpr u64 BuiltInUserSaveDataSize = 0x29e000; + constexpr u64 BuiltInUserSaveDataJournalSize = 0x29e000; + constexpr u32 BuiltInUserSaveDataFlags = 0; + + constexpr SystemSaveDataInfo BuiltInUserSystemSaveDataInfo = { + .id = BuiltInUserSaveDataId, + .size = BuiltInUserSaveDataSize, + .journal_size = BuiltInUserSaveDataJournalSize, + .flags = BuiltInUserSaveDataFlags, + .space_id = FsSaveDataSpaceId_System + }; + + constexpr u64 SdCardSaveDataId = 0x8000000000000124; + constexpr u64 SdCardSaveDataSize = 0xa08000; + constexpr u64 SdCardSaveDataJournalSize = 0xa08000; + constexpr u32 SdCardSaveDataFlags = 0; + + constexpr SystemSaveDataInfo SdCardSystemSaveDataInfo = { + .id = SdCardSaveDataId, + .size = SdCardSaveDataSize, + .journal_size = SdCardSaveDataJournalSize, + .flags = SdCardSaveDataFlags, + .space_id = FsSaveDataSpaceId_SdSystem, + }; + + constexpr size_t MaxBuiltInSystemContentMetaCount = 0x800; + constexpr size_t MaxBuiltInUserContentMetaCount = 0x2000; + constexpr size_t MaxSdCardContentMetaCount = 0x2000; + + } + + Result ContentManagerImpl::~ContentManagerImpl() { + { + std::scoped_lock lk(this->mutex); + + for (size_t i = 0; i < MaxContentStorageEntries; i++) { + ContentStorageRoot *entry = &this->content_storage_roots[i]; + this->InactivateContentStorage(entry->storage_id); + } + + for (size_t i = 0; i < MaxContentMetaDatabaseEntries; i++) { + ContentMetaDatabaseEntry *entry = &this->content_meta_entries[i]; + this->InactivateContentMetaDatabase(entry->storage_id); + } + } + + for (size_t i = 0; i < MaxContentMetaDatabaseEntries; i++) { + ContentMetaDatabaseEntry *entry = &this->content_meta_entries[i]; + entry->kvs.reset(); + } + + for (size_t i = 0; i < MaxContentStorageEntries; i++) { + ContentStorageRoot *entry = &this->content_storage_roots[i]; + entry->content_storage = nullptr; + } + } + + Result ContentManagerImpl::Initialize() { + std::scoped_lock lk(this->mutex); + + /* Already initialized. */ + R_UNLESS(!this->initialized, ResultSuccess()); + + for (size_t i = 0; i < MaxContentStorageEntries; i++) { + ContentStorageRoot *entry = &this->content_storage_roots[i]; + entry->storage_id = StorageId::None; + } + + for (size_t i = 0; i < MaxContentMetaDatabaseEntries; i++) { + ContentMetaDatabaseEntry *entry = &this->content_meta_entries[i]; + entry->storage_id = StorageId::None; + } + + /* First, setup the BuiltInSystem storage entry. */ + this->content_storage_roots[this->num_content_storage_entries++].Initialize(StorageId::BuiltInSystem, FsContentStorageId_System); + + if (R_FAILED(this->VerifyContentStorage(StorageId::BuiltInSystem))) { + R_TRY(this->CreateContentStorage(StorageId::BuiltInSystem)); + } + + R_TRY(this->ActivateContentStorage(StorageId::BuiltInSystem)); + + /* Next, the BuiltInSystem content meta entry. */ + R_TRY(this->content_meta_entries[this->num_content_meta_entries++].Initialize(StorageId::BuiltInSystem, BuiltInSystemSystemSaveDataInfo, MaxBuiltInSystemContentMetaCount)); + + if (R_FAILED(this->VerifyContentMetaDatabase(StorageId::BuiltInSystem))) { + R_TRY(this->CreateContentMetaDatabase(StorageId::BuiltInSystem)); + + /* TODO: N supports a number of unused modes here, we don't bother implementing them currently. */ + } + + u32 current_flags = 0; + if (hos::GetVersion() >= hos::Version_200 && R_SUCCEEDED(fs::GetSaveDataFlags(¤t_flags, BuiltInSystemSaveDataId)) && current_flags != (FsSaveDataFlags_KeepAfterResettingSystemSaveData | FsSaveDataFlags_KeepAfterRefurbishment)) { + fs::SetSaveDataFlags(BuiltInSystemSaveDataId, FsSaveDataSpaceId_System, FsSaveDataFlags_KeepAfterResettingSystemSaveData | FsSaveDataFlags_KeepAfterRefurbishment); + } + + R_TRY(this->ActivateContentMetaDatabase(StorageId::BuiltInSystem)); + + /* Now for BuiltInUser's content storage and content meta entries. */ + this->content_storage_roots[this->num_content_storage_entries++].Initialize(StorageId::BuiltInUser, FsContentStorageId_User); + R_TRY(this->content_meta_entries[this->num_content_meta_entries++].Initialize(StorageId::BuiltInUser, BuiltInUserSystemSaveDataInfo, MaxBuiltInUserContentMetaCount)); + + /* Beyond this point N no longer appears to bother */ + /* incrementing the count for content storage entries or content meta entries. */ + + /* Next SdCard's content storage and content meta entries. */ + this->content_storage_roots[2].Initialize(StorageId::SdCard, FsContentStorageId_SdCard); + R_TRY(this->content_meta_entries[2].Initialize(StorageId::SdCard, SdCardSystemSaveDataInfo, MaxSdCardContentMetaCount)); + + /* GameCard's content storage and content meta entries. */ + /* N doesn't set a content storage id for game cards, so we'll just use 0 (System). */ + this->content_storage_roots[3].Initialize(StorageId::GameCard, FsContentStorageId_System); + R_TRY(this->content_meta_entries[3].InitializeGameCard(0x800)); + + this->initialized = true; + return ResultSuccess(); + } + Result ContentManagerImpl::CreateContentStorage(StorageId storage_id) { - return impl::CreateContentStorage(storage_id); + std::scoped_lock lk(this->mutex); + + ContentStorageRoot *root; + R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); + + R_TRY(fs::MountContentStorage(root->mount_point, root->content_storage_id)); + ON_SCOPE_EXIT { fs::Unmount(root->mount_point); }; + + R_TRY(fs::EnsureDirectoryRecursively(root->path)); + R_TRY(fs::EnsureContentAndPlaceHolderRoot(root->path)); + + return ResultSuccess(); } Result ContentManagerImpl::CreateContentMetaDatabase(StorageId storage_id) { - return impl::CreateContentMetaDatabase(storage_id); + std::scoped_lock lk(this->mutex); + + R_UNLESS(storage_id != StorageId::GameCard, ncm::ResultUnknownStorage()); + ContentMetaDatabaseEntry *entry; + R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); + + /* N doesn't bother checking the result of this. */ + fsDisableAutoSaveDataCreation(); + + R_TRY(EnsureAndMountSystemSaveData(entry->mount_point, entry->save_meta)); + ON_SCOPE_EXIT { fs::Unmount(entry->mount_point); }; + + R_TRY(fs::EnsureDirectoryRecursively(entry->meta_path)); + R_TRY(fsdevCommitDevice(entry->mount_point)); + + return ResultSuccess(); } Result ContentManagerImpl::VerifyContentStorage(StorageId storage_id) { - return impl::VerifyContentStorage(storage_id); + std::scoped_lock lk(this->mutex); + + ContentStorageRoot *root; + R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); + + char mount_root[0x80] = {}; + auto mount_name = ncm::fs::CreateUniqueMountName(); /* should this be fs::? should it be ncm::? ncm::impl? */ + ReplaceMountName(mount_root, mount_name.name, root->path); + + R_TRY(fs::MountContentStorage(mount_name.name, root->content_storage_id)); + ON_SCOPE_EXIT { fs::Unmount(mount_name.name); }; + + R_TRY(fs::CheckContentStorageDirectoriesExist(mount_root)); + + return ResultSuccess(); } Result ContentManagerImpl::VerifyContentMetaDatabase(StorageId storage_id) { - return impl::VerifyContentMetaDatabase(storage_id); + std::scoped_lock lk(this->mutex); + + R_UNLESS(storage_id != StorageId::GameCard, ResultSuccess()); + ContentMetaDatabaseEntry *entry; + R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); + + auto mount_guard = SCOPE_GUARD { fs::Unmount(entry->mount_point); }; + if (!entry->content_meta_database) { + R_TRY(fs::MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)); + } else { + mount_guard.Cancel(); + } + + bool has_meta_path = false; + R_TRY(fs::HasDirectory(&has_meta_path, entry->meta_path)); + R_UNLESS(has_meta_path, ncm::ResultInvalidContentMetaDatabase()); + + return ResultSuccess(); } Result ContentManagerImpl::OpenContentStorage(sf::Out> out, StorageId storage_id) { - std::shared_ptr content_storage; - R_TRY(impl::OpenContentStorage(&content_storage, storage_id)); + std::scoped_lock lk(this->mutex); + + ContentStorageRoot *root; + R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); + + auto content_storage = root->content_storage; + + if (hos::GetVersion() >= hos::Version_200) { + R_UNLESS(content_storage, GetContentStorageNotActiveResult(storage_id)); + } else { + /* 1.0.0 activates content storages as soon as they are opened. */ + if (!content_storage) { + R_TRY(this->ActivateContentStorage(storage_id)); + content_storage = root->content_storage; + } + } + out.SetValue(std::move(content_storage)); return ResultSuccess(); } Result ContentManagerImpl::OpenContentMetaDatabase(sf::Out> out, StorageId storage_id) { - std::shared_ptr content_meta_database; - R_TRY(impl::OpenContentMetaDatabase(&content_meta_database, storage_id)); - out.SetValue(std::move(content_meta_database)); + std::scoped_lock lk(this->mutex); + + ContentMetaDatabaseEntry *entry; + R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); + + auto content_meta_db = entry->content_meta_database; + + if (hos::GetVersion() >= hos::Version_200) { + R_UNLESS(content_meta_db, GetContentMetaDatabaseNotActiveResult(storage_id)); + } else { + /* 1.0.0 activates content meta databases as soon as they are opened. */ + if (!content_meta_db) { + R_TRY(this->ActivateContentMetaDatabase(storage_id)); + content_meta_db = entry->content_meta_database; + } + } + + out.SetValue(std::move(content_meta_db)); return ResultSuccess(); } Result ContentManagerImpl::CloseContentStorageForcibly(StorageId storage_id) { - return impl::CloseContentStorageForcibly(storage_id); + std::scoped_lock lk(this->mutex); + + R_UNLESS(storage_id != StorageId::None, ncm::ResultUnknownStorage()); + ContentStorageRoot *root; + R_TRY(FindContentStorageRoot(std::addressof(root), storage_id)); + + if (root->content_storage) { + /* N doesn't bother checking the result of this */ + root->content_storage->DisableForcibly(); + fs::Unmount(root->mount_point); + root->content_storage = nullptr; + } + + return ResultSuccess(); } Result ContentManagerImpl::CloseContentMetaDatabaseForcibly(StorageId storage_id) { - return impl::CloseContentMetaDatabaseForcibly(storage_id); + std::scoped_lock lk(this->mutex); + + R_UNLESS(storage_id != StorageId::None, ncm::ResultUnknownStorage()); + ContentMetaDatabaseEntry *entry; + R_TRY(FindContentMetaDatabaseEntry(&entry, storage_id)); + + auto content_meta_db = entry->content_meta_database; + + if (content_meta_db) { + /* N doesn't bother checking the result of this */ + content_meta_db->DisableForcibly(); + + if (storage_id != StorageId::GameCard) { + fs::Unmount(entry->mount_point); + } + + entry->content_meta_database = nullptr; + entry->kvs = std::nullopt; + } + + return ResultSuccess(); } Result ContentManagerImpl::CleanupContentMetaDatabase(StorageId storage_id) { - return impl::CleanupContentMetaDatabase(storage_id); + std::scoped_lock lk(this->mutex); + + ContentMetaDatabaseEntry *entry; + R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); + + R_TRY(fsDeleteSaveDataFileSystemBySaveDataSpaceId(entry->save_meta.space_id, entry->save_meta.id)); + return ResultSuccess(); } Result ContentManagerImpl::ActivateContentStorage(StorageId storage_id) { - return impl::ActivateContentStorage(storage_id); + std::scoped_lock lk(this->mutex); + + ContentStorageRoot *root; + R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); + + /* Already activated. */ + R_UNLESS(root->content_storage == nullptr, ResultSuccess()); + + if (storage_id == StorageId::GameCard) { + FsGameCardHandle gc_hnd; + R_TRY(fs::GetGameCardHandle(&gc_hnd)); + R_TRY(fs::MountGameCardPartition(root->mount_point, gc_hnd, FsGameCardPartition_Secure)); + } else { + R_TRY(fs::MountContentStorage(root->mount_point, root->content_storage_id)); + } + + auto mount_guard = SCOPE_GUARD { fs::Unmount(root->mount_point); }; + + if (storage_id == StorageId::GameCard) { + auto content_storage = std::make_shared(); + R_TRY(content_storage->Initialize(root->path, path::MakeContentPathFlat)); + root->content_storage = std::move(content_storage); + } else { + MakeContentPathFunc content_path_func = nullptr; + MakePlaceHolderPathFunc placeholder_path_func = nullptr; + bool delay_flush = false; + auto content_storage = std::make_shared(); + + switch (storage_id) { + case StorageId::BuiltInSystem: + content_path_func = path::MakeContentPathFlat; + placeholder_path_func = path::MakePlaceHolderPathFlat; + break; + case StorageId::SdCard: + delay_flush = true; + default: + content_path_func = path::MakeContentPathHashByteLayered; + placeholder_path_func = path::MakePlaceHolderPathHashByteLayered; + break; + } + + R_TRY(content_storage->Initialize(root->path, content_path_func, placeholder_path_func, delay_flush, &this->rights_id_cache)); + root->content_storage = std::move(content_storage); + } + + mount_guard.Cancel(); + return ResultSuccess(); } Result ContentManagerImpl::InactivateContentStorage(StorageId storage_id) { - return impl::InactivateContentStorage(storage_id); + std::scoped_lock lk(this->mutex); + + ContentStorageRoot *root; + R_TRY(GetUniqueContentStorageRoot(std::addressof(root), storage_id)); + + /* Already inactivated. */ + R_UNLESS(root->content_storage != nullptr, ResultSuccess()); + + root->content_storage->DisableForcibly(); + root->content_storage = nullptr; + fs::Unmount(root->mount_point); + return ResultSuccess(); } Result ContentManagerImpl::ActivateContentMetaDatabase(StorageId storage_id) { - return impl::ActivateContentMetaDatabase(storage_id); + std::scoped_lock lk(this->mutex); + + ContentMetaDatabaseEntry *entry; + R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); + + /* Already activated. */ + 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(); + + if (storage_id != StorageId::GameCard) { + R_TRY(fs::MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)); + 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); + 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)); + } + + return ResultSuccess(); } Result ContentManagerImpl::InactivateContentMetaDatabase(StorageId storage_id) { - return impl::InactivateContentMetaDatabase(storage_id); + std::scoped_lock lk(this->mutex); + + ContentMetaDatabaseEntry *entry; + R_TRY(GetUniqueContentMetaDatabaseEntry(&entry, storage_id)); + + /* Already inactivated. */ + if (entry->content_meta_database != nullptr) { + entry->content_meta_database->DisableForcibly(); + entry->content_meta_database = nullptr; + /* This should lead to Index's destructor performing cleanup for us. */ + entry->kvs = std::nullopt; + + if (storage_id != StorageId::GameCard) { + fs::Unmount(entry->mount_point); + } + } + + return ResultSuccess(); } Result ContentManagerImpl::InvalidateRightsIdCache() { - return impl::InvalidateRightsIdCache(); + this->rights_id_cache.Invalidate(); + return ResultSuccess(); } } diff --git a/stratosphere/ncm/source/ncm_content_manager_impl.hpp b/stratosphere/ncm/source/ncm_content_manager_impl.hpp index 7cbef9c85..db507e934 100644 --- a/stratosphere/ncm/source/ncm_content_manager_impl.hpp +++ b/stratosphere/ncm/source/ncm_content_manager_impl.hpp @@ -16,10 +16,187 @@ #pragma once #include +#include "impl/ncm_rights_cache.hpp" +#include "ncm_fs.hpp" namespace ams::ncm { + struct SystemSaveDataInfo { + u64 id; + u64 size; + u64 journal_size; + u32 flags; + FsSaveDataSpaceId space_id; + }; + + static_assert(sizeof(SystemSaveDataInfo) == 0x20, "SystemSaveDataInfo definition!"); + class ContentManagerImpl final : public IContentManager { + private: + constexpr static size_t MaxContentStorageEntries = 8; + constexpr static size_t MaxContentMetaDatabaseEntries = 8; + private: + struct ContentStorageRoot { + NON_COPYABLE(ContentStorageRoot); + NON_MOVEABLE(ContentStorageRoot); + + char mount_point[16]; + char path[128]; + StorageId storage_id; + FsContentStorageId content_storage_id; + std::shared_ptr content_storage; + + inline ContentStorageRoot() : storage_id(StorageId::None), + content_storage_id(FsContentStorageId_System), content_storage(nullptr) { + mount_point[0] = '\0'; + path[0] = '\0'; + } + + inline void Initialize(StorageId storage_id, FsContentStorageId content_storage_id) { + this->storage_id = storage_id; + this->content_storage_id = content_storage_id; + this->content_storage = nullptr; + MountName mount_name = ncm::fs::CreateUniqueMountName(); + std::strcpy(this->mount_point, mount_name.name); + snprintf(this->path, 0x80, "%s:/", this->mount_point); + } + }; + + struct ContentMetaDatabaseEntry { + NON_COPYABLE(ContentMetaDatabaseEntry); + NON_MOVEABLE(ContentMetaDatabaseEntry); + + char mount_point[16]; + char meta_path[128]; + StorageId storage_id; + SystemSaveDataInfo save_meta; + std::shared_ptr content_meta_database; + std::optional> kvs; + u32 max_content_metas; + + inline ContentMetaDatabaseEntry() : storage_id(StorageId::None), save_meta({0}), + content_meta_database(nullptr), kvs(std::nullopt), max_content_metas(0) { + mount_point[0] = '\0'; + meta_path[0] = '\0'; + } + + Result Initialize(StorageId storage_id, const SystemSaveDataInfo& save_meta, size_t max_content_metas) { + this->storage_id = storage_id; + this->max_content_metas = max_content_metas; + this->save_meta = save_meta; + this->content_meta_database = nullptr; + this->kvs = std::nullopt; + MountName mount_name = ncm::fs::CreateUniqueMountName(); + strcpy(this->mount_point, mount_name.name); + this->mount_point[0] = '#'; + snprintf(this->meta_path, 0x80, "%s:/meta", this->mount_point); + return ResultSuccess(); + } + + Result InitializeGameCard(size_t max_content_metas) { + this->storage_id = StorageId::GameCard; + this->max_content_metas = max_content_metas; + this->content_meta_database = nullptr; + this->kvs = std::nullopt; + return ResultSuccess(); + } + }; + private: + os::Mutex mutex; + bool initialized = false; + ContentStorageRoot content_storage_roots[MaxContentStorageEntries]; + ContentMetaDatabaseEntry content_meta_entries[MaxContentMetaDatabaseEntries]; + u32 num_content_storage_entries; + u32 num_content_meta_entries; + impl::RightsIdCache rights_id_cache; + public: + ContentManagerImpl() { /* ... */ }; + ~ContentManagerImpl(); + public: + Result Initialize(); + private: + constexpr inline bool IsUniqueStorage(StorageId id) { + return id != StorageId::None && id != StorageId::Any; + } + + Result FindContentStorageRoot(ContentStorageRoot **out, StorageId storage_id) { + for (size_t i = 0; i < MaxContentStorageEntries; i++) { + ContentStorageRoot *root = &this->content_storage_roots[i]; + + if (root->storage_id == storage_id) { + *out = root; + return ResultSuccess(); + } + } + return ncm::ResultUnknownStorage(); + } + + Result GetUniqueContentStorageRoot(ContentStorageRoot **out, StorageId storage_id) { + R_UNLESS(IsUniqueStorage(storage_id), ncm::ResultUnknownStorage()); + return FindContentStorageRoot(out, storage_id); + } + + Result FindContentMetaDatabaseEntry(ContentMetaDatabaseEntry **out, StorageId storage_id) { + for (size_t i = 0; i < MaxContentMetaDatabaseEntries; i++) { + ContentMetaDatabaseEntry *entry = &this->content_meta_entries[i]; + + if (entry->storage_id == storage_id) { + *out = entry; + return ResultSuccess(); + } + } + return ncm::ResultUnknownStorage(); + } + + Result GetUniqueContentMetaDatabaseEntry(ContentMetaDatabaseEntry **out, StorageId storage_id) { + R_UNLESS(IsUniqueStorage(storage_id), ncm::ResultUnknownStorage()); + return FindContentMetaDatabaseEntry(out, storage_id); + } + + ALWAYS_INLINE Result GetContentStorageNotActiveResult(StorageId storage_id) { + switch (storage_id) { + case StorageId::GameCard: + return ResultGameCardContentStorageNotActive(); + case StorageId::BuiltInSystem: + return ResultNandSystemContentStorageNotActive(); + case StorageId::BuiltInUser: + return ResultNandUserContentStorageNotActive(); + case StorageId::SdCard: + return ResultSdCardContentStorageNotActive(); + default: + return ResultUnknownContentStorageNotActive(); + } + } + + ALWAYS_INLINE Result GetContentMetaDatabaseNotActiveResult(StorageId storage_id) { + switch (storage_id) { + case StorageId::GameCard: + return ResultGameCardContentMetaDatabaseNotActive(); + case StorageId::BuiltInSystem: + return ResultNandSystemContentMetaDatabaseNotActive(); + case StorageId::BuiltInUser: + return ResultNandUserContentMetaDatabaseNotActive(); + case StorageId::SdCard: + return ResultSdCardContentMetaDatabaseNotActive(); + default: + return ResultUnknownContentMetaDatabaseNotActive(); + } + } + + Result EnsureAndMountSystemSaveData(const char *mount_name, const SystemSaveDataInfo &save_meta) { + R_TRY_CATCH(fs::MountSystemSaveData(mount_name, save_meta.space_id, save_meta.id)) { + R_CATCH(ams::fs::ResultTargetNotFound) { + R_TRY(fsCreate_SystemSaveData(save_meta.space_id, save_meta.id, save_meta.size, save_meta.journal_size, save_meta.flags)); + R_TRY(fs::MountSystemSaveData(mount_name, save_meta.space_id, save_meta.id)); + } + } R_END_TRY_CATCH; + return ResultSuccess(); + } + + inline void ReplaceMountName(char *out_path, const char *mount_name, const char *root_path) { + strcpy(out_path, mount_name); + strcat(out_path, strchr(root_path, ':')); + } public: virtual Result CreateContentStorage(StorageId storage_id) override; virtual Result CreateContentMetaDatabase(StorageId storage_id) override; diff --git a/stratosphere/ncm/source/ncm_main.cpp b/stratosphere/ncm/source/ncm_main.cpp index 4b5b66289..ca3b477e0 100644 --- a/stratosphere/ncm/source/ncm_main.cpp +++ b/stratosphere/ncm/source/ncm_main.cpp @@ -16,8 +16,6 @@ #include #include - -#include "impl/ncm_content_manager.hpp" #include "ncm_content_manager_impl.hpp" extern "C" { @@ -197,9 +195,7 @@ void ContentManagerServerMain(void *arg) { int main(int argc, char **argv) { - /* Initialize content manager implementation. */ - /* TODO: Move Initialize/Finalize into ContentManagerImpl ctor/dtor, initialize client library with static object. */ - R_ABORT_UNLESS(ams::ncm::impl::InitializeContentManager()); + /* TODO: Initialize content manager implementation. */ R_ABORT_UNLESS(g_ncm_server_manager.Initialize()); R_ABORT_UNLESS(g_ncm_server_manager.StartThreads()); @@ -210,8 +206,5 @@ int main(int argc, char **argv) g_ncm_server_manager.Wait(); g_lr_server_manager.Wait(); - /* TODO: This call is eventually automatic at scope exit. */ - ams::ncm::impl::FinalizeContentManager(); - return 0; }