From 93004be59ed4cfe90074b8095badcd4051c39cf5 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 9 Mar 2020 03:58:02 -0700 Subject: [PATCH] ams: finish stdio -> fs bindings for stratosphere --- .../include/stratosphere/util/util_ini.hpp | 8 ++- .../libstratosphere/source/util/util_ini.cpp | 53 ++++++++++++++++--- stratosphere/ams_mitm/source/amsmitm_main.cpp | 1 - .../source/fs_mitm/fs_mitm_service.cpp | 6 +-- .../fsmitm_readonly_layered_filesystem.hpp | 4 +- .../source/set_mitm/settings_sd_kvs.cpp | 14 ++--- stratosphere/boot2/source/boot2_main.cpp | 1 - stratosphere/creport/source/creport_main.cpp | 1 - stratosphere/dmnt/source/dmnt_main.cpp | 1 - stratosphere/fatal/source/fatal_main.cpp | 1 - .../loader/source/ldr_launch_record.cpp | 32 +---------- stratosphere/loader/source/ldr_main.cpp | 2 - stratosphere/ro/source/ro_main.cpp | 1 - 13 files changed, 67 insertions(+), 58 deletions(-) diff --git a/libraries/libstratosphere/include/stratosphere/util/util_ini.hpp b/libraries/libstratosphere/include/stratosphere/util/util_ini.hpp index 2497a27d6..b8c5dc4dd 100644 --- a/libraries/libstratosphere/include/stratosphere/util/util_ini.hpp +++ b/libraries/libstratosphere/include/stratosphere/util/util_ini.hpp @@ -18,6 +18,12 @@ #include #include +namespace ams::fs::fsa { + + class IFile; + +} + namespace ams::util::ini { /* Ini handler type. */ @@ -26,6 +32,6 @@ namespace ams::util::ini { /* Utilities for dealing with INI file configuration. */ int ParseString(const char *ini_str, void *user_ctx, Handler h); int ParseFile(fs::FileHandle file, void *user_ctx, Handler h); - int ParseFile(const char *path, void *user_ctx, Handler h); + int ParseFile(fs::fsa::IFile *file, void *user_ctx, Handler h); } \ No newline at end of file diff --git a/libraries/libstratosphere/source/util/util_ini.cpp b/libraries/libstratosphere/source/util/util_ini.cpp index 06852eeb2..232d85bd3 100644 --- a/libraries/libstratosphere/source/util/util_ini.cpp +++ b/libraries/libstratosphere/source/util/util_ini.cpp @@ -34,6 +34,16 @@ namespace ams::util::ini { } }; + struct IFileContext { + fs::fsa::IFile *file; + s64 offset; + s64 num_left; + + explicit IFileContext(fs::fsa::IFile *f) : file(f), offset(0) { + R_ABORT_UNLESS(file->GetSize(std::addressof(this->num_left))); + } + }; + char *ini_reader_file_handle(char *str, int num, void *stream) { FileContext *ctx = static_cast(stream); @@ -64,6 +74,38 @@ namespace ams::util::ini { return str; } + char *ini_reader_ifile(char *str, int num, void *stream) { + IFileContext *ctx = static_cast(stream); + + if (ctx->num_left == 0 || num < 2) { + return nullptr; + } + + /* Read as many bytes as we can. */ + s64 cur_read = std::min(num - 1, ctx->num_left); + size_t read; + R_ABORT_UNLESS(ctx->file->Read(std::addressof(read), ctx->offset, str, cur_read, fs::ReadOption())); + AMS_ABORT_UNLESS(static_cast(read) == cur_read); + + /* Only "read" up to the first \n. */ + size_t offset = cur_read; + for (auto i = 0; i < cur_read; i++) { + if (str[i] == '\n') { + offset = i + 1; + break; + } + } + + /* Ensure null termination. */ + str[offset] = '\0'; + + /* Update context. */ + ctx->offset += offset; + ctx->num_left -= offset; + + return str; + } + } /* Utilities for dealing with INI file configuration. */ @@ -76,14 +118,9 @@ namespace ams::util::ini { return ini_parse_stream(ini_reader_file_handle, &ctx, h, user_ctx); } - int ParseFile(const char *path, void *user_ctx, Handler h) { - fs::FileHandle file; - if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) { - return -1; - } - ON_SCOPE_EXIT { fs::CloseFile(file); }; - - return ParseFile(file, user_ctx, h); + int ParseFile(fs::fsa::IFile *file, void *user_ctx, Handler h) { + IFileContext ctx(file); + return ini_parse_stream(ini_reader_ifile, &ctx, h, user_ctx); } } \ No newline at end of file diff --git a/stratosphere/ams_mitm/source/amsmitm_main.cpp b/stratosphere/ams_mitm/source/amsmitm_main.cpp index 7e47169dd..e6e0f5a71 100644 --- a/stratosphere/ams_mitm/source/amsmitm_main.cpp +++ b/stratosphere/ams_mitm/source/amsmitm_main.cpp @@ -22,7 +22,6 @@ extern "C" { u32 __nx_applet_type = AppletType_None; u32 __nx_fs_num_sessions = 1; - u32 __nx_fsdev_direntry_cache_size = 1; #define INNER_HEAP_SIZE 0x1000000 size_t nx_inner_heap_size = INNER_HEAP_SIZE; diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_mitm_service.cpp b/stratosphere/ams_mitm/source/fs_mitm/fs_mitm_service.cpp index 71d3e544e..7ff723007 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fs_mitm_service.cpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_mitm_service.cpp @@ -85,7 +85,7 @@ namespace ams::mitm::fs { const sf::cmif::DomainObjectId target_object_id{serviceGetObjectId(&sd_fs.s)}; std::unique_ptr sd_ifs = std::make_unique(sd_fs); - out.SetValue(std::make_shared(std::make_shared(std::make_unique(std::move(sd_ifs), AtmosphereHblWebContentDir)), false), target_object_id); + out.SetValue(std::make_shared(std::make_shared(std::make_unique(std::move(sd_ifs), AtmosphereHblWebContentDir)), false), target_object_id); return ResultSuccess(); } @@ -126,7 +126,7 @@ namespace ams::mitm::fs { new_fs = std::make_shared(std::move(subdir_fs), std::make_unique(base_fs)); } else { /* Without an existing FS, just make a read only adapter to the subdirectory. */ - new_fs = std::make_shared(std::move(subdir_fs)); + new_fs = std::make_shared(std::move(subdir_fs)); } out.SetValue(std::make_shared(std::move(new_fs), false), target_object_id); @@ -220,7 +220,7 @@ namespace ams::mitm::fs { } /* Ensure the directory exists. */ - R_TRY(fssystem::EnsureDirectoryExistsRecursively(sd_ifs.get(), save_dir_path)); + R_TRY(fssystem::EnsureDirectoryRecursively(sd_ifs.get(), save_dir_path)); /* Create directory savedata filesystem. */ std::unique_ptr subdir_fs = std::make_unique(sd_ifs, save_dir_path); diff --git a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_readonly_layered_filesystem.hpp b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_readonly_layered_filesystem.hpp index de07e1358..4e8a3f580 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_readonly_layered_filesystem.hpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_readonly_layered_filesystem.hpp @@ -20,8 +20,8 @@ namespace ams::mitm::fs { class ReadOnlyLayeredFileSystem : public ams::fs::fsa::IFileSystem { private: - ams::fs::ReadOnlyFileSystemAdapter fs_1; - ams::fs::ReadOnlyFileSystemAdapter fs_2; + ams::fs::ReadOnlyFileSystem fs_1; + ams::fs::ReadOnlyFileSystem fs_2; public: explicit ReadOnlyLayeredFileSystem(std::unique_ptr a, std::unique_ptr b) : fs_1(std::move(a)), fs_2(std::move(b)) { /* ... */ } diff --git a/stratosphere/ams_mitm/source/set_mitm/settings_sd_kvs.cpp b/stratosphere/ams_mitm/source/set_mitm/settings_sd_kvs.cpp index 5122f3f0c..7e211a945 100644 --- a/stratosphere/ams_mitm/source/set_mitm/settings_sd_kvs.cpp +++ b/stratosphere/ams_mitm/source/set_mitm/settings_sd_kvs.cpp @@ -290,15 +290,17 @@ namespace ams::settings::fwdbg { Result LoadSdCardKeyValueStore() { /* Open file. */ - FsFile config_file; - if (R_FAILED(ams::mitm::fs::OpenAtmosphereSdFile(&config_file, "/config/system_settings.ini", fs::OpenMode_Read))) { - /* It's okay if the file isn't readable/present, because we already loaded defaults. */ - return ResultSuccess(); + /* It's okay if the file isn't readable/present, because we already loaded defaults. */ + std::unique_ptr file; + { + FsFile f; + R_SUCCEED_IF(R_FAILED(ams::mitm::fs::OpenAtmosphereSdFile(std::addressof(f), "/config/system_settings.ini", fs::OpenMode_Read))); + file = std::make_unique(f); } - ON_SCOPE_EXIT { fsFileClose(&config_file); }; + AMS_ABORT_UNLESS(file != nullptr); Result parse_result = ResultSuccess(); - util::ini::ParseFile(&config_file, &parse_result, SystemSettingsIniHandler); + util::ini::ParseFile(file.get(), &parse_result, SystemSettingsIniHandler); R_TRY(parse_result); return ResultSuccess(); diff --git a/stratosphere/boot2/source/boot2_main.cpp b/stratosphere/boot2/source/boot2_main.cpp index 37c2f6aa9..4b1fcff47 100644 --- a/stratosphere/boot2/source/boot2_main.cpp +++ b/stratosphere/boot2/source/boot2_main.cpp @@ -20,7 +20,6 @@ extern "C" { u32 __nx_applet_type = AppletType_None; u32 __nx_fs_num_sessions = 1; - u32 __nx_fsdev_direntry_cache_size = 1; #define INNER_HEAP_SIZE 0x2000 size_t nx_inner_heap_size = INNER_HEAP_SIZE; diff --git a/stratosphere/creport/source/creport_main.cpp b/stratosphere/creport/source/creport_main.cpp index 127dc9dd5..9241e663e 100644 --- a/stratosphere/creport/source/creport_main.cpp +++ b/stratosphere/creport/source/creport_main.cpp @@ -22,7 +22,6 @@ extern "C" { u32 __nx_applet_type = AppletType_None; u32 __nx_fs_num_sessions = 1; - u32 __nx_fsdev_direntry_cache_size = 1; #define INNER_HEAP_SIZE 0x4000 size_t nx_inner_heap_size = INNER_HEAP_SIZE; diff --git a/stratosphere/dmnt/source/dmnt_main.cpp b/stratosphere/dmnt/source/dmnt_main.cpp index f4c5a3842..4d999dfbf 100644 --- a/stratosphere/dmnt/source/dmnt_main.cpp +++ b/stratosphere/dmnt/source/dmnt_main.cpp @@ -21,7 +21,6 @@ extern "C" { u32 __nx_applet_type = AppletType_None; u32 __nx_fs_num_sessions = 1; - u32 __nx_fsdev_direntry_cache_size = 1; /* TODO: Evaluate how much this can be reduced by. */ #define INNER_HEAP_SIZE 0x20000 diff --git a/stratosphere/fatal/source/fatal_main.cpp b/stratosphere/fatal/source/fatal_main.cpp index e23cbe1a4..2eab8d712 100644 --- a/stratosphere/fatal/source/fatal_main.cpp +++ b/stratosphere/fatal/source/fatal_main.cpp @@ -23,7 +23,6 @@ extern "C" { u32 __nx_applet_type = AppletType_None; u32 __nx_fs_num_sessions = 1; - u32 __nx_fsdev_direntry_cache_size = 1; #define INNER_HEAP_SIZE 0x240000 size_t nx_inner_heap_size = INNER_HEAP_SIZE; diff --git a/stratosphere/loader/source/ldr_launch_record.cpp b/stratosphere/loader/source/ldr_launch_record.cpp index 53ade01af..fc7e48e9c 100644 --- a/stratosphere/loader/source/ldr_launch_record.cpp +++ b/stratosphere/loader/source/ldr_launch_record.cpp @@ -22,43 +22,15 @@ namespace ams::ldr { /* Global cache. */ std::set g_launched_programs; - constexpr size_t NumSystemProgramIds = ncm::SystemProgramId::End.value - ncm::SystemProgramId::Start.value + 1; - static_assert(util::IsPowerOfTwo(NumSystemProgramIds)); - static_assert(util::IsAligned(NumSystemProgramIds, BITSIZEOF(u64))); - - bool IsTrackableSystemProgramId(ncm::ProgramId program_id) { - return ncm::SystemProgramId::Start <= program_id && program_id <= ncm::SystemProgramId::End; - } - - u64 g_system_launch_records[NumSystemProgramIds / BITSIZEOF(u64)]; - - void SetLaunchedSystemProgram(ncm::SystemProgramId program_id) { - const u64 val = program_id.value - ncm::SystemProgramId::Start.value; - g_system_launch_records[val / BITSIZEOF(u64)] |= (1ul << (val % BITSIZEOF(u64))); - } - - bool HasLaunchedSystemProgram(ncm::SystemProgramId program_id) { - const u64 val = program_id.value - ncm::SystemProgramId::Start.value; - return (g_system_launch_records[val / BITSIZEOF(u64)] & (1ul << (val % BITSIZEOF(u64)))) != 0; - } - } /* Launch Record API. */ bool HasLaunchedProgram(ncm::ProgramId program_id) { - if (IsTrackableSystemProgramId(program_id)) { - return HasLaunchedSystemProgram(ncm::SystemProgramId{program_id.value}); - } else { - return g_launched_programs.find(program_id.value) != g_launched_programs.end(); - } + return g_launched_programs.find(program_id.value) != g_launched_programs.end(); } void SetLaunchedProgram(ncm::ProgramId program_id) { - if (IsTrackableSystemProgramId(program_id)) { - SetLaunchedSystemProgram(ncm::SystemProgramId{program_id.value}); - } else { - g_launched_programs.insert(static_cast(program_id)); - } + g_launched_programs.insert(program_id.value); } } diff --git a/stratosphere/loader/source/ldr_main.cpp b/stratosphere/loader/source/ldr_main.cpp index 327e91e92..8dba34b66 100644 --- a/stratosphere/loader/source/ldr_main.cpp +++ b/stratosphere/loader/source/ldr_main.cpp @@ -21,7 +21,6 @@ extern "C" { u32 __nx_applet_type = AppletType_None; u32 __nx_fs_num_sessions = 1; - u32 __nx_fsdev_direntry_cache_size = 1; #define INNER_HEAP_SIZE 0x8000 size_t nx_inner_heap_size = INNER_HEAP_SIZE; @@ -82,7 +81,6 @@ void __appInit(void) { void __appExit(void) { /* Cleanup services. */ - fsdevUnmountAll(); fsldrExit(); lr::Finalize(); fsExit(); diff --git a/stratosphere/ro/source/ro_main.cpp b/stratosphere/ro/source/ro_main.cpp index 13c0c69f7..2171451ae 100644 --- a/stratosphere/ro/source/ro_main.cpp +++ b/stratosphere/ro/source/ro_main.cpp @@ -21,7 +21,6 @@ extern "C" { u32 __nx_applet_type = AppletType_None; u32 __nx_fs_num_sessions = 1; - u32 __nx_fsdev_direntry_cache_size = 1; #define INNER_HEAP_SIZE 0x4000 size_t nx_inner_heap_size = INNER_HEAP_SIZE;