ncm: Misc cleanup

This commit is contained in:
Adubbz 2019-08-24 16:02:08 +10:00
parent 2939557f88
commit deaca0dacb
9 changed files with 83 additions and 80 deletions

View File

@ -17,10 +17,13 @@
#pragma once #pragma once
#include <type_traits> #include <type_traits>
#include "../lr/lr_types.hpp"
#include "../util/util_uuid.hpp" #include "../util/util_uuid.hpp"
namespace sts::ncm { namespace sts::ncm {
using Path = lr::Path;
/* Storage IDs. */ /* Storage IDs. */
enum class StorageId : u8 { enum class StorageId : u8 {
None = 0, None = 0,

View File

@ -349,7 +349,7 @@ namespace sts::updater {
void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize); void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
size_t size; size_t size;
R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))); R_TRY(fs::ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type)));
if (HasEks(boot_image_update_type)) { if (HasEks(boot_image_update_type)) {
R_TRY(boot0_accessor.UpdateEks(bct, work)); R_TRY(boot0_accessor.UpdateEks(bct, work));
} }
@ -411,7 +411,7 @@ namespace sts::updater {
void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize); void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
size_t size; size_t size;
R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))); R_TRY(fs::ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type)));
if (HasEks(boot_image_update_type)) { if (HasEks(boot_image_update_type)) {
R_TRY(boot0_accessor.UpdateEks(bct, work)); R_TRY(boot0_accessor.UpdateEks(bct, work));
} }
@ -465,7 +465,7 @@ namespace sts::updater {
void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize); void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
size_t size; size_t size;
R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type))); R_TRY(fs::ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type)));
if (HasEks(boot_image_update_type)) { if (HasEks(boot_image_update_type)) {
R_TRY(accessor.UpdateEks(bct, work)); R_TRY(accessor.UpdateEks(bct, work));
} }

View File

@ -50,7 +50,7 @@ namespace sts::ncm::impl {
this->storage_id = storage_id; this->storage_id = storage_id;
this->content_storage_id = content_storage_id; this->content_storage_id = content_storage_id;
this->content_storage = nullptr; this->content_storage = nullptr;
MountName mount_name = CreateUniqueMountName(); MountName mount_name = fs::CreateUniqueMountName();
strcpy(this->mount_point, mount_name.name); strcpy(this->mount_point, mount_name.name);
snprintf(this->root_path, 0x80, "%s:/", this->mount_point); snprintf(this->root_path, 0x80, "%s:/", this->mount_point);
} }
@ -90,7 +90,7 @@ namespace sts::ncm::impl {
this->save_meta = save_meta; this->save_meta = save_meta;
this->content_meta_database = nullptr; this->content_meta_database = nullptr;
this->kvs.reset(); this->kvs.reset();
MountName mount_name = CreateUniqueMountName(); MountName mount_name = fs::CreateUniqueMountName();
strcpy(this->mount_point, mount_name.name); strcpy(this->mount_point, mount_name.name);
this->mount_point[0] = '#'; this->mount_point[0] = '#';
snprintf(this->meta_path, 0x80, "%s:/meta", this->mount_point); snprintf(this->meta_path, 0x80, "%s:/meta", this->mount_point);
@ -195,8 +195,8 @@ namespace sts::ncm::impl {
} }
u32 current_flags = 0; u32 current_flags = 0;
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_200 && R_SUCCEEDED(GetSaveDataFlags(&current_flags, 0x8000000000000120)) && current_flags != (FsSaveDataFlags_SurviveFactoryReset | FsSaveDataFlags_SurviveFactoryResetForRefurbishment)) { if (GetRuntimeFirmwareVersion() >= FirmwareVersion_200 && R_SUCCEEDED(fs::GetSaveDataFlags(&current_flags, 0x8000000000000120)) && current_flags != (FsSaveDataFlags_SurviveFactoryReset | FsSaveDataFlags_SurviveFactoryResetForRefurbishment)) {
SetSaveDataFlags(0x8000000000000120, FsSaveDataSpaceId_NandSystem, FsSaveDataFlags_SurviveFactoryReset | FsSaveDataFlags_SurviveFactoryResetForRefurbishment); fs::SetSaveDataFlags(0x8000000000000120, FsSaveDataSpaceId_NandSystem, FsSaveDataFlags_SurviveFactoryReset | FsSaveDataFlags_SurviveFactoryResetForRefurbishment);
} }
R_TRY(ActivateContentMetaDatabase(StorageId::NandSystem)); R_TRY(ActivateContentMetaDatabase(StorageId::NandSystem));
@ -291,14 +291,14 @@ namespace sts::ncm::impl {
return ResultNcmUnknownStorage; return ResultNcmUnknownStorage;
} }
R_TRY(MountContentStorage(entry->mount_point, entry->content_storage_id)); R_TRY(fs::MountContentStorage(entry->mount_point, entry->content_storage_id));
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
Unmount(entry->mount_point); fs::Unmount(entry->mount_point);
}; };
R_TRY(EnsureDirectoryRecursively(entry->root_path)); R_TRY(fs::EnsureDirectoryRecursively(entry->root_path));
R_TRY(EnsureContentAndPlaceHolderRoot(entry->root_path)); R_TRY(fs::EnsureContentAndPlaceHolderRoot(entry->root_path));
return ResultSuccess; return ResultSuccess;
} }
@ -316,17 +316,17 @@ namespace sts::ncm::impl {
return ResultNcmUnknownStorage; return ResultNcmUnknownStorage;
} }
MountName mount_name = CreateUniqueMountName(); MountName mount_name = fs::CreateUniqueMountName();
char mount_root[128] = {0}; char mount_root[128] = {0};
strcpy(mount_root, mount_name.name); strcpy(mount_root, mount_name.name);
strcat(mount_root, strchr(entry->root_path, ':')); strcat(mount_root, strchr(entry->root_path, ':'));
R_TRY(MountContentStorage(mount_name.name, entry->content_storage_id)); R_TRY(fs::MountContentStorage(mount_name.name, entry->content_storage_id));
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
Unmount(mount_name.name); fs::Unmount(mount_name.name);
}; };
R_TRY(CheckContentStorageDirectoriesExist(mount_root)); R_TRY(fs::CheckContentStorageDirectoriesExist(mount_root));
return ResultSuccess; return ResultSuccess;
} }
@ -394,7 +394,7 @@ namespace sts::ncm::impl {
/* N doesn't bother checking the result of this */ /* N doesn't bother checking the result of this */
entry->content_storage->DisableForcibly(); entry->content_storage->DisableForcibly();
Unmount(entry->mount_point); fs::Unmount(entry->mount_point);
entry->content_storage = nullptr; entry->content_storage = nullptr;
return ResultSuccess; return ResultSuccess;
} }
@ -419,17 +419,17 @@ namespace sts::ncm::impl {
if (storage_id == StorageId::GameCard) { if (storage_id == StorageId::GameCard) {
FsGameCardHandle gc_hnd; FsGameCardHandle gc_hnd;
R_TRY(GetGameCardHandle(&gc_hnd)); R_TRY(fs::GetGameCardHandle(&gc_hnd));
R_TRY(MountGameCardPartition(entry->mount_point, gc_hnd, FsGameCardPartiton_Secure)); R_TRY(fs::MountGameCardPartition(entry->mount_point, gc_hnd, FsGameCardPartiton_Secure));
auto mount_guard = SCOPE_GUARD { Unmount(entry->mount_point); }; auto mount_guard = SCOPE_GUARD { fs::Unmount(entry->mount_point); };
auto content_storage = std::make_shared<ReadOnlyContentStorageInterface>(); auto content_storage = std::make_shared<ReadOnlyContentStorageInterface>();
R_TRY(content_storage->Initialize(entry->root_path, path::MakeContentPathFlat)); R_TRY(content_storage->Initialize(entry->root_path, path::MakeContentPathFlat));
entry->content_storage = std::move(content_storage); entry->content_storage = std::move(content_storage);
mount_guard.Cancel(); mount_guard.Cancel();
} else { } else {
R_TRY(MountContentStorage(entry->mount_point, entry->content_storage_id)); R_TRY(fs::MountContentStorage(entry->mount_point, entry->content_storage_id));
auto mount_guard = SCOPE_GUARD { Unmount(entry->mount_point); }; auto mount_guard = SCOPE_GUARD { fs::Unmount(entry->mount_point); };
MakeContentPathFunc content_path_func = nullptr; MakeContentPathFunc content_path_func = nullptr;
MakePlaceHolderPathFunc placeholder_path_func = nullptr; MakePlaceHolderPathFunc placeholder_path_func = nullptr;
bool delay_flush = false; bool delay_flush = false;
@ -477,7 +477,7 @@ namespace sts::ncm::impl {
entry->content_storage->DisableForcibly(); entry->content_storage->DisableForcibly();
entry->content_storage = nullptr; entry->content_storage = nullptr;
Unmount(entry->mount_point); fs::Unmount(entry->mount_point);
return ResultSuccess; return ResultSuccess;
} }
@ -497,18 +497,18 @@ namespace sts::ncm::impl {
/* N doesn't bother checking the result of this. */ /* N doesn't bother checking the result of this. */
fsDisableAutoSaveDataCreation(); fsDisableAutoSaveDataCreation();
R_TRY_CATCH(MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)) { R_TRY_CATCH(fs::MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)) {
R_CATCH(ResultFsTargetNotFound) { R_CATCH(ResultFsTargetNotFound) {
R_TRY(fsCreate_SystemSaveData(entry->save_meta.space_id, entry->save_meta.id, entry->save_meta.size, entry->save_meta.journal_size, entry->save_meta.flags)); R_TRY(fsCreate_SystemSaveData(entry->save_meta.space_id, entry->save_meta.id, entry->save_meta.size, entry->save_meta.journal_size, entry->save_meta.flags));
R_TRY(MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)); R_TRY(fs::MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id));
} }
} R_END_TRY_CATCH; } R_END_TRY_CATCH;
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
Unmount(entry->mount_point); fs::Unmount(entry->mount_point);
}; };
R_TRY(EnsureDirectoryRecursively(entry->meta_path)); R_TRY(fs::EnsureDirectoryRecursively(entry->meta_path));
R_TRY(fsdevCommitDevice(entry->mount_point)); R_TRY(fsdevCommitDevice(entry->mount_point));
return ResultSuccess; return ResultSuccess;
@ -534,18 +534,18 @@ namespace sts::ncm::impl {
bool mounted_save_data = false; bool mounted_save_data = false;
if (!entry->content_meta_database) { if (!entry->content_meta_database) {
R_TRY(MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)); R_TRY(fs::MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id));
mounted_save_data = true; mounted_save_data = true;
} }
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
if (mounted_save_data) { if (mounted_save_data) {
Unmount(entry->mount_point); fs::Unmount(entry->mount_point);
} }
}; };
bool has_meta_path = false; bool has_meta_path = false;
R_TRY(HasDirectory(&has_meta_path, entry->meta_path)); R_TRY(fs::HasDirectory(&has_meta_path, entry->meta_path));
if (!has_meta_path) { if (!has_meta_path) {
return ResultNcmInvalidContentMetaDatabase; return ResultNcmInvalidContentMetaDatabase;
} }
@ -620,7 +620,7 @@ namespace sts::ncm::impl {
content_meta_db->DisableForcibly(); content_meta_db->DisableForcibly();
if (storage_id != StorageId::GameCard) { if (storage_id != StorageId::GameCard) {
Unmount(entry->mount_point); fs::Unmount(entry->mount_point);
} }
entry->content_meta_database = nullptr; entry->content_meta_database = nullptr;
@ -663,8 +663,8 @@ namespace sts::ncm::impl {
entry->kvs.emplace(); entry->kvs.emplace();
if (storage_id != StorageId::GameCard) { if (storage_id != StorageId::GameCard) {
R_TRY(MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id)); R_TRY(fs::MountSystemSaveData(entry->mount_point, entry->save_meta.space_id, entry->save_meta.id));
auto mount_guard = SCOPE_GUARD { Unmount(entry->mount_point); }; 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->Initialize(entry->meta_path, entry->max_content_metas));
R_TRY(entry->kvs->Load()); R_TRY(entry->kvs->Load());
@ -701,7 +701,7 @@ namespace sts::ncm::impl {
entry->kvs.reset(); entry->kvs.reset();
if (storage_id != StorageId::GameCard) { if (storage_id != StorageId::GameCard) {
Unmount(entry->mount_point); fs::Unmount(entry->mount_point);
} }
return ResultSuccess; return ResultSuccess;

View File

@ -30,7 +30,7 @@ namespace sts::ncm::impl {
this->MakePath(placeholder_path, placeholder_id); this->MakePath(placeholder_path, placeholder_id);
FILE* f = nullptr; FILE* f = nullptr;
R_TRY(OpenFile(&f, placeholder_path, FS_OPEN_WRITE)); R_TRY(fs::OpenFile(&f, placeholder_path, FS_OPEN_WRITE));
*out_handle = f; *out_handle = f;
return ResultSuccess; return ResultSuccess;
@ -164,7 +164,7 @@ namespace sts::ncm::impl {
this->StoreToCache(f, placeholder_id); this->StoreToCache(f, placeholder_id);
}; };
R_TRY(WriteFile(f, offset, buffer, size, !this->delay_flush)); R_TRY(fs::WriteFile(f, offset, buffer, size, !this->delay_flush));
return ResultSuccess; return ResultSuccess;
} }
@ -223,7 +223,7 @@ namespace sts::ncm::impl {
Result PlaceHolderAccessor::EnsureRecursively(PlaceHolderId placeholder_id) { Result PlaceHolderAccessor::EnsureRecursively(PlaceHolderId placeholder_id) {
char placeholder_path[FS_MAX_PATH] = {0}; char placeholder_path[FS_MAX_PATH] = {0};
this->MakePath(placeholder_path, placeholder_id); this->MakePath(placeholder_path, placeholder_id);
R_TRY(EnsureParentDirectoryRecursively(placeholder_path)); R_TRY(fs::EnsureParentDirectoryRecursively(placeholder_path));
return ResultSuccess; return ResultSuccess;
} }

View File

@ -21,15 +21,15 @@ namespace sts::ncm {
namespace { namespace {
struct InstallContentMetaHeader { struct ContentMetaHeader {
u16 extended_header_size; u16 extended_header_size;
u16 content_count; u16 content_count;
u16 content_meta_count; u16 content_meta_count;
ContentMetaAttribute attributes; ContentMetaAttribute attributes;
u8 padding; StorageId storage_id;
}; };
static_assert(sizeof(InstallContentMetaHeader) == 0x8, "InstallContentMetaHeader definition!"); static_assert(sizeof(ContentMetaHeader) == 0x8, "ContentMetaHeader definition!");
struct ApplicationMetaExtendedHeader { struct ApplicationMetaExtendedHeader {
TitleId patch_id; TitleId patch_id;
@ -54,17 +54,17 @@ namespace sts::ncm {
u32 extended_data_size; u32 extended_data_size;
}; };
inline const InstallContentMetaHeader* GetValueHeader(const void* value) { inline const ContentMetaHeader* GetValueHeader(const void* value) {
return reinterpret_cast<const InstallContentMetaHeader*>(value); return reinterpret_cast<const ContentMetaHeader*>(value);
} }
template<typename ExtendedHeaderType> template<typename ExtendedHeaderType>
inline const ExtendedHeaderType* GetValueExtendedHeader(const void* value) { inline const ExtendedHeaderType* GetValueExtendedHeader(const void* value) {
return reinterpret_cast<const ExtendedHeaderType*>(reinterpret_cast<const u8*>(value) + sizeof(InstallContentMetaHeader)); return reinterpret_cast<const ExtendedHeaderType*>(reinterpret_cast<const u8*>(value) + sizeof(ContentMetaHeader));
} }
inline const ContentInfo* GetValueContentInfos(const void* value) { inline const ContentInfo* GetValueContentInfos(const void* value) {
return reinterpret_cast<const ContentInfo*>(reinterpret_cast<const u8*>(value) + sizeof(InstallContentMetaHeader) + GetValueHeader(value)->extended_header_size); return reinterpret_cast<const ContentInfo*>(reinterpret_cast<const u8*>(value) + sizeof(ContentMetaHeader) + GetValueHeader(value)->extended_header_size);
} }
inline const ContentMetaInfo* GetValueContentMetaInfos(const void* value) { inline const ContentMetaInfo* GetValueContentMetaInfos(const void* value) {

View File

@ -28,7 +28,7 @@ namespace sts::ncm {
Result ContentStorageInterface::Initialize(const char* root_path, MakeContentPathFunc content_path_func, MakePlaceHolderPathFunc placeholder_path_func, bool delay_flush) { Result ContentStorageInterface::Initialize(const char* root_path, MakeContentPathFunc content_path_func, MakePlaceHolderPathFunc placeholder_path_func, bool delay_flush) {
R_TRY(this->EnsureEnabled()); R_TRY(this->EnsureEnabled());
R_TRY(CheckContentStorageDirectoriesExist(root_path)); R_TRY(fs::CheckContentStorageDirectoriesExist(root_path));
const size_t root_path_len = strnlen(root_path, FS_MAX_PATH-1); const size_t root_path_len = strnlen(root_path, FS_MAX_PATH-1);
if (root_path_len >= FS_MAX_PATH-1) { if (root_path_len >= FS_MAX_PATH-1) {
@ -76,7 +76,7 @@ namespace sts::ncm {
char content_path[FS_MAX_PATH] = {0}; char content_path[FS_MAX_PATH] = {0};
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
R_TRY_CATCH(OpenFile(&this->content_cache_file_handle, content_path, FS_OPEN_READ)) { R_TRY_CATCH(fs::OpenFile(&this->content_cache_file_handle, content_path, FS_OPEN_READ)) {
R_CATCH(ResultFsPathNotFound) { R_CATCH(ResultFsPathNotFound) {
return ResultNcmContentNotFound; return ResultNcmContentNotFound;
} }
@ -101,7 +101,7 @@ namespace sts::ncm {
char content_path[FS_MAX_PATH] = {0}; char content_path[FS_MAX_PATH] = {0};
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
R_TRY(EnsureParentDirectoryRecursively(content_path)); R_TRY(fs::EnsureParentDirectoryRecursively(content_path));
R_TRY(this->placeholder_accessor.Create(placeholder_id, size)); R_TRY(this->placeholder_accessor.Create(placeholder_id, size));
return ResultSuccess; return ResultSuccess;
@ -119,7 +119,7 @@ namespace sts::ncm {
this->placeholder_accessor.MakePath(placeholder_path, placeholder_id); this->placeholder_accessor.MakePath(placeholder_path, placeholder_id);
bool has = false; bool has = false;
R_TRY(HasFile(&has, placeholder_path)); R_TRY(fs::HasFile(&has, placeholder_path));
out.SetValue(has); out.SetValue(has);
return ResultSuccess; return ResultSuccess;
@ -185,7 +185,7 @@ namespace sts::ncm {
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
bool has = false; bool has = false;
R_TRY(HasFile(&has, content_path)); R_TRY(fs::HasFile(&has, content_path));
out.SetValue(has); out.SetValue(has);
return ResultSuccess; return ResultSuccess;
@ -197,7 +197,7 @@ namespace sts::ncm {
char content_path[FS_MAX_PATH] = {0}; char content_path[FS_MAX_PATH] = {0};
char common_path[FS_MAX_PATH] = {0}; char common_path[FS_MAX_PATH] = {0};
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
R_TRY(ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, content_path)); R_TRY(fs::ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, content_path));
*out.pointer = common_path; *out.pointer = common_path;
return ResultSuccess; return ResultSuccess;
} }
@ -208,7 +208,7 @@ namespace sts::ncm {
char placeholder_path[FS_MAX_PATH] = {0}; char placeholder_path[FS_MAX_PATH] = {0};
char common_path[FS_MAX_PATH] = {0}; char common_path[FS_MAX_PATH] = {0};
this->placeholder_accessor.GetPath(placeholder_path, placeholder_id); this->placeholder_accessor.GetPath(placeholder_path, placeholder_id);
R_TRY(ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, placeholder_path)); R_TRY(fs::ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, placeholder_path));
*out.pointer = common_path; *out.pointer = common_path;
return ResultSuccess; return ResultSuccess;
} }
@ -239,7 +239,7 @@ namespace sts::ncm {
const unsigned int dir_depth = this->placeholder_accessor.GetDirectoryDepth(); const unsigned int dir_depth = this->placeholder_accessor.GetDirectoryDepth();
size_t entry_count = 0; size_t entry_count = 0;
R_TRY(TraverseDirectory(placeholder_root_path, dir_depth, [&](bool* should_continue, bool* should_retry_dir_read, const char* current_path, struct dirent* dir_entry) { R_TRY(fs::TraverseDirectory(placeholder_root_path, dir_depth, [&](bool* should_continue, bool* should_retry_dir_read, const char* current_path, struct dirent* dir_entry) {
*should_continue = true; *should_continue = true;
*should_retry_dir_read = false; *should_retry_dir_read = false;
@ -268,7 +268,7 @@ namespace sts::ncm {
const unsigned int dir_depth = this->GetContentDirectoryDepth(); const unsigned int dir_depth = this->GetContentDirectoryDepth();
u32 content_count = 0; u32 content_count = 0;
R_TRY(TraverseDirectory(content_root_path, dir_depth, [&](bool* should_continue, bool* should_retry_dir_read, const char* current_path, struct dirent* dir_entry) { R_TRY(fs::TraverseDirectory(content_root_path, dir_depth, [&](bool* should_continue, bool* should_retry_dir_read, const char* current_path, struct dirent* dir_entry) {
*should_continue = true; *should_continue = true;
*should_retry_dir_read = false; *should_retry_dir_read = false;
@ -295,7 +295,7 @@ namespace sts::ncm {
const unsigned int dir_depth = this->GetContentDirectoryDepth(); const unsigned int dir_depth = this->GetContentDirectoryDepth();
size_t entry_count = 0; size_t entry_count = 0;
R_TRY(TraverseDirectory(content_root_path, dir_depth, [&](bool* should_continue, bool* should_retry_dir_read, const char* current_path, struct dirent* dir_entry) { R_TRY(fs::TraverseDirectory(content_root_path, dir_depth, [&](bool* should_continue, bool* should_retry_dir_read, const char* current_path, struct dirent* dir_entry) {
*should_retry_dir_read = false; *should_retry_dir_read = false;
*should_continue = true; *should_continue = true;
@ -368,7 +368,7 @@ namespace sts::ncm {
/* Ensure the new content path is ready. */ /* Ensure the new content path is ready. */
this->GetContentPath(new_content_path, new_content_id); this->GetContentPath(new_content_path, new_content_id);
R_TRY(EnsureParentDirectoryRecursively(new_content_path)); R_TRY(fs::EnsureParentDirectoryRecursively(new_content_path));
R_TRY(this->placeholder_accessor.EnsureRecursively(placeholder_id)); R_TRY(this->placeholder_accessor.EnsureRecursively(placeholder_id));
this->placeholder_accessor.GetPath(placeholder_path, placeholder_id); this->placeholder_accessor.GetPath(placeholder_path, placeholder_id);
@ -402,7 +402,7 @@ namespace sts::ncm {
char content_path[FS_MAX_PATH] = {0}; char content_path[FS_MAX_PATH] = {0};
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
R_TRY(this->OpenCachedContentFile(content_id)); R_TRY(this->OpenCachedContentFile(content_id));
R_TRY(ReadFile(this->content_cache_file_handle, offset, buf.buffer, buf.num_elements)); R_TRY(fs::ReadFile(this->content_cache_file_handle, offset, buf.buffer, buf.num_elements));
return ResultSuccess; return ResultSuccess;
} }
@ -416,7 +416,7 @@ namespace sts::ncm {
char placeholder_path[FS_MAX_PATH] = {0}; char placeholder_path[FS_MAX_PATH] = {0};
char common_path[FS_MAX_PATH] = {0}; char common_path[FS_MAX_PATH] = {0};
this->placeholder_accessor.GetPath(placeholder_path, placeholder_id); this->placeholder_accessor.GetPath(placeholder_path, placeholder_id);
R_TRY(ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, placeholder_path)); R_TRY(fs::ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, placeholder_path));
R_TRY(fsGetRightsIdAndKeyGenerationByPath(common_path, &key_generation, &rights_id)); R_TRY(fsGetRightsIdAndKeyGenerationByPath(common_path, &key_generation, &rights_id));
out_rights_id.SetValue(rights_id); out_rights_id.SetValue(rights_id);
@ -452,7 +452,7 @@ namespace sts::ncm {
char content_path[FS_MAX_PATH] = {0}; char content_path[FS_MAX_PATH] = {0};
char common_path[FS_MAX_PATH] = {0}; char common_path[FS_MAX_PATH] = {0};
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
R_TRY(ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, content_path)); R_TRY(fs::ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, content_path));
R_TRY(fsGetRightsIdAndKeyGenerationByPath(common_path, &key_generation, &rights_id)); R_TRY(fsGetRightsIdAndKeyGenerationByPath(common_path, &key_generation, &rights_id));
{ {
@ -504,13 +504,13 @@ namespace sts::ncm {
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
FILE* f = nullptr; FILE* f = nullptr;
R_TRY(OpenFile(&f, content_path, FS_OPEN_WRITE)); R_TRY(fs::OpenFile(&f, content_path, FS_OPEN_WRITE));
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
fclose(f); fclose(f);
}; };
R_TRY(WriteFile(f, offset, data.buffer, data.num_elements, FS_WRITEOPTION_FLUSH)); R_TRY(fs::WriteFile(f, offset, data.buffer, data.num_elements, FS_WRITEOPTION_FLUSH));
return ResultSuccess; return ResultSuccess;
} }
@ -584,14 +584,14 @@ namespace sts::ncm {
return ResultSuccess; return ResultSuccess;
}; };
R_TRY(TraverseDirectory(content_root_path, dir_depth, fix_file_attributes)); R_TRY(fs::TraverseDirectory(content_root_path, dir_depth, fix_file_attributes));
char placeholder_root_path[FS_MAX_PATH] = {0}; char placeholder_root_path[FS_MAX_PATH] = {0};
this->placeholder_accessor.InvalidateAll(); this->placeholder_accessor.InvalidateAll();
this->placeholder_accessor.MakeRootPath(placeholder_root_path); this->placeholder_accessor.MakeRootPath(placeholder_root_path);
dir_depth = this->placeholder_accessor.GetDirectoryDepth(); dir_depth = this->placeholder_accessor.GetDirectoryDepth();
R_TRY(TraverseDirectory(placeholder_root_path, dir_depth, fix_file_attributes)); R_TRY(fs::TraverseDirectory(placeholder_root_path, dir_depth, fix_file_attributes));
return ResultSuccess; return ResultSuccess;
} }
@ -623,7 +623,7 @@ namespace sts::ncm {
char placeholder_path[FS_MAX_PATH] = {0}; char placeholder_path[FS_MAX_PATH] = {0};
char common_path[FS_MAX_PATH] = {0}; char common_path[FS_MAX_PATH] = {0};
this->placeholder_accessor.GetPath(placeholder_path, placeholder_id); this->placeholder_accessor.GetPath(placeholder_path, placeholder_id);
R_TRY(ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, placeholder_path)); R_TRY(fs::ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, placeholder_path));
R_TRY(fsGetRightsIdAndKeyGenerationByPath(common_path, &key_generation, &rights_id)); R_TRY(fsGetRightsIdAndKeyGenerationByPath(common_path, &key_generation, &rights_id));
{ {

View File

@ -21,7 +21,7 @@
#include "ncm_fs.hpp" #include "ncm_fs.hpp"
#include "ncm_path_utils.hpp" #include "ncm_path_utils.hpp"
namespace sts::ncm { namespace sts::ncm::fs {
Result OpenFile(FILE** out, const char* path, u32 mode) { Result OpenFile(FILE** out, const char* path, u32 mode) {
bool has = false; bool has = false;

View File

@ -19,7 +19,7 @@
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include <sys/dirent.h> #include <sys/dirent.h>
namespace sts::ncm { namespace sts::ncm::fs {
Result OpenFile(FILE** out, const char* path, u32 mode); Result OpenFile(FILE** out, const char* path, u32 mode);
Result WriteFile(FILE* f, size_t offset, const void* buffer, size_t size, u32 option); Result WriteFile(FILE* f, size_t offset, const void* buffer, size_t size, u32 option);
@ -50,12 +50,6 @@ namespace sts::ncm {
Result GetSaveDataFlags(u32* out_flags, u64 save_id); Result GetSaveDataFlags(u32* out_flags, u64 save_id);
Result SetSaveDataFlags(u64 save_id, FsSaveDataSpaceId space_id, u32 flags); Result SetSaveDataFlags(u64 save_id, FsSaveDataSpaceId space_id, u32 flags);
template<typename F>
Result TraverseDirectory(const char* root_path, int max_level, F f) {
bool should_continue = false;
return TraverseDirectory(&should_continue, root_path, max_level, f);
}
template<typename F> template<typename F>
Result TraverseDirectory(bool* out_should_continue, const char* root_path, int max_level, F f) { Result TraverseDirectory(bool* out_should_continue, const char* root_path, int max_level, F f) {
DIR *dir; DIR *dir;
@ -111,4 +105,10 @@ namespace sts::ncm {
return ResultSuccess; return ResultSuccess;
}; };
template<typename F>
Result TraverseDirectory(const char* root_path, int max_level, F f) {
bool should_continue = false;
return TraverseDirectory(&should_continue, root_path, max_level, f);
}
} }

View File

@ -69,11 +69,11 @@ namespace sts::ncm {
this->make_content_path_func(content_path, content_id, this->root_path); this->make_content_path_func(content_path, content_id, this->root_path);
bool has = false; bool has = false;
R_TRY(HasFile(&has, content_path)); R_TRY(fs::HasFile(&has, content_path));
if (!has) { if (!has) {
path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path); path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path);
R_TRY(HasFile(&has, content_path)); R_TRY(fs::HasFile(&has, content_path));
} }
out.SetValue(has); out.SetValue(has);
@ -88,13 +88,13 @@ namespace sts::ncm {
bool is_content_meta_file = false; bool is_content_meta_file = false;
path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path); path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path);
R_TRY(HasFile(&is_content_meta_file, content_path)); R_TRY(fs::HasFile(&is_content_meta_file, content_path));
if (!is_content_meta_file) { if (!is_content_meta_file) {
this->make_content_path_func(content_path, content_id, this->root_path); this->make_content_path_func(content_path, content_id, this->root_path);
} }
R_TRY(ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, content_path)); R_TRY(fs::ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, content_path));
*out.pointer = common_path; *out.pointer = common_path;
return ResultSuccess; return ResultSuccess;
@ -127,7 +127,7 @@ namespace sts::ncm {
bool is_content_file = false; bool is_content_file = false;
this->make_content_path_func(content_path, content_id, this->root_path); this->make_content_path_func(content_path, content_id, this->root_path);
R_TRY(HasFile(&is_content_file, content_path)); R_TRY(fs::HasFile(&is_content_file, content_path));
if (!is_content_file) { if (!is_content_file) {
path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path); path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path);
@ -167,20 +167,20 @@ namespace sts::ncm {
bool is_content_file = false; bool is_content_file = false;
this->make_content_path_func(content_path, content_id, this->root_path); this->make_content_path_func(content_path, content_id, this->root_path);
R_TRY(HasFile(&is_content_file, content_path)); R_TRY(fs::HasFile(&is_content_file, content_path));
if (!is_content_file) { if (!is_content_file) {
path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path); path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path);
} }
FILE* f = nullptr; FILE* f = nullptr;
R_TRY(OpenFile(&f, content_path, FS_OPEN_READ)); R_TRY(fs::OpenFile(&f, content_path, FS_OPEN_READ));
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
fclose(f); fclose(f);
}; };
R_TRY(ReadFile(f, offset, buf.buffer, buf.num_elements)); R_TRY(fs::ReadFile(f, offset, buf.buffer, buf.num_elements));
return ResultSuccess; return ResultSuccess;
} }
@ -200,13 +200,13 @@ namespace sts::ncm {
bool is_content_meta_file = false; bool is_content_meta_file = false;
path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path); path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path);
R_TRY(HasFile(&is_content_meta_file, content_path)); R_TRY(fs::HasFile(&is_content_meta_file, content_path));
if (!is_content_meta_file) { if (!is_content_meta_file) {
this->make_content_path_func(content_path, content_id, this->root_path); this->make_content_path_func(content_path, content_id, this->root_path);
} }
R_TRY(ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, content_path)); R_TRY(fs::ConvertToFsCommonPath(common_path, FS_MAX_PATH-1, content_path));
R_TRY(fsGetRightsIdAndKeyGenerationByPath(common_path, &key_generation, &rights_id)); R_TRY(fsGetRightsIdAndKeyGenerationByPath(common_path, &key_generation, &rights_id));
out_rights_id.SetValue(rights_id); out_rights_id.SetValue(rights_id);