From 6eee3f5fe7cfd6bfce1b4f1abdca3533ab005b7e Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 8 Mar 2020 16:33:49 -0700 Subject: [PATCH] stratosphere-except-ldr: use fs bindings (this temporarily breaks loader) --- .../include/stratosphere/fs/fs_filesystem.hpp | 8 +- .../kvdb/kvdb_file_key_value_store.hpp | 4 +- .../kvdb/kvdb_memory_key_value_store.hpp | 6 +- .../include/stratosphere/util/util_ini.hpp | 4 +- .../libstratosphere/source/cfg/cfg_flags.cpp | 29 +-- .../source/cfg/cfg_override.cpp | 31 ++-- .../source/fs/fsa/fs_filesystem_accessor.cpp | 2 +- .../source/patcher/patcher_api.cpp | 169 +++++++++++------- .../libstratosphere/source/util/util_ini.cpp | 48 +++-- .../creport/source/creport_scoped_file.hpp | 2 +- .../dmnt/source/cheat/impl/dmnt_cheat_api.cpp | 96 +++++----- .../dmnt/source/cheat/impl/dmnt_cheat_vm.cpp | 57 ++++-- .../dmnt/source/cheat/impl/dmnt_cheat_vm.hpp | 5 +- stratosphere/dmnt/source/dmnt_main.cpp | 3 +- stratosphere/fatal/source/fatal_main.cpp | 1 - .../fatal/source/fatal_scoped_file.hpp | 2 +- stratosphere/ro/source/ro_main.cpp | 3 +- 17 files changed, 283 insertions(+), 187 deletions(-) diff --git a/libraries/libstratosphere/include/stratosphere/fs/fs_filesystem.hpp b/libraries/libstratosphere/include/stratosphere/fs/fs_filesystem.hpp index 79a77c505..98e9187b7 100644 --- a/libraries/libstratosphere/include/stratosphere/fs/fs_filesystem.hpp +++ b/libraries/libstratosphere/include/stratosphere/fs/fs_filesystem.hpp @@ -25,12 +25,12 @@ namespace ams::fs { } enum OpenMode { - OpenMode_Read = ::FsOpenMode_Read, - OpenMode_Write = ::FsOpenMode_Write, - OpenMode_Append = ::FsOpenMode_Append, + OpenMode_Read = (1 << 0), + OpenMode_Write = (1 << 1), + OpenMode_AllowAppend = (1 << 2), OpenMode_ReadWrite = (OpenMode_Read | OpenMode_Write), - OpenMode_All = (OpenMode_ReadWrite | OpenMode_Append), + OpenMode_All = (OpenMode_ReadWrite | OpenMode_AllowAppend), }; enum OpenDirectoryMode { diff --git a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_file_key_value_store.hpp b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_file_key_value_store.hpp index 40e5fbc84..ac3d5ae97 100644 --- a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_file_key_value_store.hpp +++ b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_file_key_value_store.hpp @@ -15,6 +15,7 @@ */ #pragma once #include +#include #include namespace ams::kvdb { @@ -23,12 +24,11 @@ namespace ams::kvdb { NON_COPYABLE(FileKeyValueStore); NON_MOVEABLE(FileKeyValueStore); public: - static constexpr size_t MaxPathLength = 0x300; /* TODO: FS_MAX_PATH - 1? */ static constexpr size_t MaxFileLength = 0xFF; static constexpr char FileExtension[5] = ".val"; static constexpr size_t FileExtensionLength = sizeof(FileExtension) - 1; static constexpr size_t MaxKeySize = (MaxFileLength - FileExtensionLength) / 2; - using Path = kvdb::BoundedString; + using Path = kvdb::BoundedString; using FileName = kvdb::BoundedString; private: /* Subtypes. */ diff --git a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_memory_key_value_store.hpp b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_memory_key_value_store.hpp index 65ca787ed..75391902a 100644 --- a/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_memory_key_value_store.hpp +++ b/libraries/libstratosphere/include/stratosphere/kvdb/kvdb_memory_key_value_store.hpp @@ -15,8 +15,9 @@ */ #pragma once -#include #include +#include +#include #include #include #include @@ -252,8 +253,7 @@ namespace ams::kvdb { } }; private: - static constexpr size_t MaxPathLen = 0x300; /* TODO: FS_MAX_PATH - 1? */ - using Path = kvdb::BoundedString; + using Path = kvdb::BoundedString; private: Index index; Path path; diff --git a/libraries/libstratosphere/include/stratosphere/util/util_ini.hpp b/libraries/libstratosphere/include/stratosphere/util/util_ini.hpp index 895ee4fab..2497a27d6 100644 --- a/libraries/libstratosphere/include/stratosphere/util/util_ini.hpp +++ b/libraries/libstratosphere/include/stratosphere/util/util_ini.hpp @@ -16,6 +16,7 @@ #pragma once #include +#include namespace ams::util::ini { @@ -24,8 +25,7 @@ namespace ams::util::ini { /* Utilities for dealing with INI file configuration. */ int ParseString(const char *ini_str, void *user_ctx, Handler h); - int ParseFile(FILE *f, void *user_ctx, Handler h); - int ParseFile(FsFile *f, 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); } \ No newline at end of file diff --git a/libraries/libstratosphere/source/cfg/cfg_flags.cpp b/libraries/libstratosphere/source/cfg/cfg_flags.cpp index 03fc69270..c607cbabc 100644 --- a/libraries/libstratosphere/source/cfg/cfg_flags.cpp +++ b/libraries/libstratosphere/source/cfg/cfg_flags.cpp @@ -20,7 +20,13 @@ namespace ams::cfg { namespace { + std::atomic g_flag_mount_count; + /* Helper. */ + void GetFlagMountName(char *dst) { + std::snprintf(dst, fs::MountNameLengthMax + 1, "#flag%08x", g_flag_mount_count.fetch_add(1)); + } + bool HasFlagFile(const char *flag_path) { /* All flags are not present until the SD card is. */ if (!IsSdCardInitialized()) { @@ -28,20 +34,23 @@ namespace ams::cfg { } /* Mount the SD card. */ - FsFileSystem sd_fs = {}; - if (R_FAILED(fsOpenSdCardFileSystem(&sd_fs))) { + char mount_name[fs::MountNameLengthMax + 1]; + GetFlagMountName(mount_name); + if (R_FAILED(fs::MountSdCard(mount_name))) { return false; } - ON_SCOPE_EXIT { serviceClose(&sd_fs.s); }; + ON_SCOPE_EXIT { fs::Unmount(mount_name); }; - /* Open the file. */ - FsFile flag_file; - if (R_FAILED(fsFsOpenFile(&sd_fs, flag_path, FsOpenMode_Read, &flag_file))) { + /* Check if the entry exists. */ + char full_path[fs::EntryNameLengthMax + 1]; + std::snprintf(full_path, sizeof(full_path), "%s:/%s", mount_name, flag_path[0] == '/' ? flag_path + 1 : flag_path); + + bool has_file; + if (R_FAILED(fs::HasFile(std::addressof(has_file), full_path))) { return false; } - fsFileClose(&flag_file); - return true; + return has_file; } } @@ -52,13 +61,13 @@ namespace ams::cfg { } bool HasContentSpecificFlag(ncm::ProgramId program_id, const char *flag) { - char content_flag[FS_MAX_PATH]; + char content_flag[fs::EntryNameLengthMax + 1]; std::snprintf(content_flag, sizeof(content_flag) - 1, "/atmosphere/contents/%016lx/flags/%s.flag", static_cast(program_id), flag); return HasFlagFile(content_flag); } bool HasGlobalFlag(const char *flag) { - char global_flag[FS_MAX_PATH]; + char global_flag[fs::EntryNameLengthMax + 1]; std::snprintf(global_flag, sizeof(global_flag) - 1, "/atmosphere/flags/%s.flag", flag); return HasFlagFile(global_flag); } diff --git a/libraries/libstratosphere/source/cfg/cfg_override.cpp b/libraries/libstratosphere/source/cfg/cfg_override.cpp index f12e5193c..c31cb16b4 100644 --- a/libraries/libstratosphere/source/cfg/cfg_override.cpp +++ b/libraries/libstratosphere/source/cfg/cfg_override.cpp @@ -272,23 +272,34 @@ namespace ams::cfg { return g_hbl_override_config.override_any_app && ncm::IsApplicationId(program_id) && !IsAnySpecificHblProgramId(program_id); } + std::atomic g_ini_mount_count; + + void GetIniMountName(char *dst) { + std::snprintf(dst, fs::MountNameLengthMax + 1, "#ini%08x", g_ini_mount_count.fetch_add(1)); + } + void ParseIniFile(util::ini::Handler handler, const char *path, void *user_ctx) { /* Mount the SD card. */ - FsFileSystem sd_fs = {}; - if (R_FAILED(fsOpenSdCardFileSystem(&sd_fs))) { + char mount_name[fs::MountNameLengthMax + 1]; + GetIniMountName(mount_name); + if (R_FAILED(fs::MountSdCard(mount_name))) { return; } - ON_SCOPE_EXIT { serviceClose(&sd_fs.s); }; + ON_SCOPE_EXIT { fs::Unmount(mount_name); }; /* Open the file. */ - FsFile config_file; - if (R_FAILED(fsFsOpenFile(&sd_fs, path, FsOpenMode_Read, &config_file))) { - return; + fs::FileHandle file; + { + char full_path[fs::EntryNameLengthMax + 1]; + std::snprintf(full_path, sizeof(full_path), "%s:/%s", mount_name, path[0] == '/' ? path + 1 : path); + if (R_FAILED(fs::OpenFile(std::addressof(file), full_path, fs::OpenMode_Read))) { + return; + } } - ON_SCOPE_EXIT { fsFileClose(&config_file); }; + ON_SCOPE_EXIT { fs::CloseFile(file); }; /* Parse the config. */ - util::ini::ParseFile(&config_file, user_ctx, handler); + util::ini::ParseFile(file, user_ctx, handler); } void RefreshOverrideConfiguration() { @@ -296,8 +307,8 @@ namespace ams::cfg { } ContentSpecificOverrideConfig GetContentOverrideConfig(ncm::ProgramId program_id) { - char path[FS_MAX_PATH]; - std::snprintf(path, sizeof(path) - 1, "/atmosphere/contents/%016lx/config.ini", static_cast(program_id)); + char path[fs::EntryNameLengthMax + 1]; + std::snprintf(path, sizeof(path), "/atmosphere/contents/%016lx/config.ini", static_cast(program_id)); ContentSpecificOverrideConfig config = { .override_key = g_default_override_key, diff --git a/libraries/libstratosphere/source/fs/fsa/fs_filesystem_accessor.cpp b/libraries/libstratosphere/source/fs/fsa/fs_filesystem_accessor.cpp index cdd17854b..c8bf11997 100644 --- a/libraries/libstratosphere/source/fs/fsa/fs_filesystem_accessor.cpp +++ b/libraries/libstratosphere/source/fs/fsa/fs_filesystem_accessor.cpp @@ -179,7 +179,7 @@ namespace ams::fs::impl { } if (this->path_cache_attached) { - if (mode & OpenMode_Append) { + if (mode & OpenMode_AllowAppend) { /* TODO: Append Path cache */ } else { /* TODO: Non-append path cache */ diff --git a/libraries/libstratosphere/source/patcher/patcher_api.cpp b/libraries/libstratosphere/source/patcher/patcher_api.cpp index 1809b13c0..c140e0c23 100644 --- a/libraries/libstratosphere/source/patcher/patcher_api.cpp +++ b/libraries/libstratosphere/source/patcher/patcher_api.cpp @@ -13,7 +13,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#include #include /* IPS Patching adapted from Luma3DS (https://github.com/AuroraWright/Luma3DS/blob/master/sysmodules/loader/source/patcher.c) */ @@ -31,6 +30,10 @@ namespace ams::patcher { constexpr size_t IpsFileExtensionLength = std::strlen(IpsFileExtension); constexpr size_t ModuleIpsPatchLength = 2 * sizeof(ro::ModuleId) + IpsFileExtensionLength; + /* Global data. */ + os::Mutex apply_patch_lock; + u8 g_patch_read_buffer[os::MemoryPageSize]; + /* Helpers. */ inline u8 ConvertHexNybble(const char nybble) { if ('0' <= nybble && nybble <= '9') { @@ -45,7 +48,7 @@ namespace ams::patcher { bool ParseModuleIdFromPath(ro::ModuleId *out_module_id, const char *name, size_t name_len, size_t extension_len) { /* Validate name is hex module id. */ for (unsigned int i = 0; i < name_len - extension_len; i++) { - if (std::isxdigit(name[i]) == 0) { + if (!std::isxdigit(static_cast(name[i]))) { return false; } } @@ -70,6 +73,28 @@ namespace ams::patcher { return std::memcmp(&module_id_from_name, module_id, sizeof(*module_id)) == 0; } + bool IsIpsFileForModule(const char *name, const ro::ModuleId *module_id) { + const size_t name_len = std::strlen(name); + + /* The path must be correct size for a build id (with trailing zeroes optionally trimmed) + ".ips". */ + if (!(IpsFileExtensionLength < name_len && name_len <= ModuleIpsPatchLength)) { + return false; + } + + /* The path must be an even number of characters to conform. */ + if (!util::IsAligned(name_len, 2)) { + return false; + } + + /* The path needs to end with .ips. */ + if (std::strcmp(name + name_len - IpsFileExtensionLength, IpsFileExtension) != 0) { + return false; + } + + /* The path needs to match the module id. */ + return MatchesModuleId(name, name_len, IpsFileExtensionLength, module_id); + } + inline bool IsIpsTail(bool is_ips32, u8 *buffer) { if (is_ips32) { return std::memcmp(buffer, Ips32TailMagic, sizeof(Ips32TailMagic)) == 0; @@ -90,13 +115,19 @@ namespace ams::patcher { return (buffer[0] << 8) | (buffer[1]); } - void ApplyIpsPatch(u8 *mapped_module, size_t mapped_size, size_t protected_size, size_t offset, bool is_ips32, FILE *f_ips) { + void ApplyIpsPatch(u8 *mapped_module, size_t mapped_size, size_t protected_size, size_t offset, bool is_ips32, fs::FileHandle file) { /* Validate offset/protected size. */ AMS_ABORT_UNLESS(offset <= protected_size); + s64 file_offset = sizeof(IpsHeadMagic); + auto ReadData = [&](void *dst, size_t size) ALWAYS_INLINE_LAMBDA { + R_ABORT_UNLESS(fs::ReadFile(file, file_offset, dst, size)); + file_offset += size; + }; + u8 buffer[sizeof(Ips32TailMagic)]; while (true) { - AMS_ABORT_UNLESS(fread(buffer, is_ips32 ? sizeof(Ips32TailMagic) : sizeof(IpsTailMagic), 1, f_ips) == 1); + ReadData(buffer, is_ips32 ? sizeof(Ips32TailMagic) : sizeof(IpsTailMagic)); if (IsIpsTail(is_ips32, buffer)) { break; @@ -106,18 +137,18 @@ namespace ams::patcher { u32 patch_offset = GetIpsPatchOffset(is_ips32, buffer); /* Size of patch. */ - AMS_ABORT_UNLESS(fread(buffer, 2, 1, f_ips) == 1); + ReadData(buffer, 2); u32 patch_size = GetIpsPatchSize(is_ips32, buffer); /* Check for RLE encoding. */ if (patch_size == 0) { /* Size of RLE. */ - AMS_ABORT_UNLESS(fread(buffer, 2, 1, f_ips) == 1); + ReadData(buffer, 2); u32 rle_size = (buffer[0] << 8) | (buffer[1]); /* Value for RLE. */ - AMS_ABORT_UNLESS(fread(buffer, 1, 1, f_ips) == 1); + ReadData(buffer, 1); /* Ensure we don't write to protected region. */ if (patch_offset < protected_size) { @@ -145,9 +176,9 @@ namespace ams::patcher { const u32 diff = protected_size - patch_offset; patch_offset += diff; patch_size -= diff; - fseek(f_ips, diff, SEEK_CUR); + file_offset += diff; } else { - fseek(f_ips, patch_size, SEEK_CUR); + file_offset += patch_size; continue; } } @@ -160,9 +191,19 @@ namespace ams::patcher { if (patch_offset + read_size > mapped_size) { read_size = mapped_size - patch_offset; } - AMS_ABORT_UNLESS(fread(mapped_module + patch_offset, read_size, 1, f_ips) == 1); + { + size_t remaining = read_size; + size_t copy_offset = 0; + while (remaining > 0) { + const size_t cur_read = std::min(remaining, sizeof(g_patch_read_buffer)); + ReadData(g_patch_read_buffer, cur_read); + std::memcpy(mapped_module + copy_offset, g_patch_read_buffer, cur_read); + remaining -= cur_read; + copy_offset += cur_read; + } + } if (patch_size > read_size) { - fseek(f_ips, patch_size - read_size, SEEK_CUR); + file_offset += patch_size - read_size; } } } @@ -171,64 +212,72 @@ namespace ams::patcher { } void LocateAndApplyIpsPatchesToModule(const char *patch_dir_name, size_t protected_size, size_t offset, const ro::ModuleId *module_id, u8 *mapped_module, size_t mapped_size) { - /* Inspect all patches from /atmosphere//<*>/<*>.ips */ - char path[FS_MAX_PATH+1] = {0}; - std::snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/%s", patch_dir_name); + /* Ensure only one thread tries to apply patches at a time. */ + std::scoped_lock lk(apply_patch_lock); - DIR *patches_dir = opendir(path); - struct dirent *pdir_ent; - if (patches_dir != NULL) { - /* Iterate over the patches directory to find patch subdirectories. */ - while ((pdir_ent = readdir(patches_dir)) != NULL) { - if (std::strcmp(pdir_ent->d_name, ".") == 0 || std::strcmp(pdir_ent->d_name, "..") == 0) { + /* Inspect all patches from /atmosphere//<*>/<*>.ips */ + char path[fs::EntryNameLengthMax + 1]; + std::snprintf(path, sizeof(path), "sdmc:/atmosphere/%s", patch_dir_name); + const size_t patches_dir_path_len = std::strlen(path); + + /* Open the patch directory. */ + fs::DirectoryHandle patches_dir; + if (R_FAILED(fs::OpenDirectory(std::addressof(patches_dir), path, fs::OpenDirectoryMode_Directory))) { + return; + } + ON_SCOPE_EXIT { fs::CloseDirectory(patches_dir); }; + + /* Iterate over the patches directory to find patch subdirectories. */ + while (true) { + /* Read the next entry. */ + s64 count; + fs::DirectoryEntry entry; + if (R_FAILED(fs::ReadDirectory(std::addressof(count), std::addressof(entry), patches_dir, 1)) || count == 0) { + break; + } + + /* Print the path for this directory. */ + std::snprintf(path + patches_dir_path_len, sizeof(path) - patches_dir_path_len, "/%s", entry.name); + const size_t patch_dir_path_len = patches_dir_path_len + 1 + std::strlen(entry.name); + + /* Open the patch directory. */ + fs::DirectoryHandle patch_dir; + if (R_FAILED(fs::OpenDirectory(std::addressof(patch_dir), path, fs::OpenDirectoryMode_File))) { + continue; + } + ON_SCOPE_EXIT { fs::CloseDirectory(patch_dir); }; + + /* Iterate over files in the patch directory. */ + while (true) { + if (R_FAILED(fs::ReadDirectory(std::addressof(count), std::addressof(entry), patch_dir, 1)) || count == 0) { + break; + } + + /* Check if this file is an ips. */ + if (!IsIpsFileForModule(entry.name, module_id)) { continue; } - std::snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/%s/%s", patch_dir_name, pdir_ent->d_name); - DIR *patch_dir = opendir(path); - struct dirent *ent; - if (patch_dir != NULL) { - /* Iterate over the patch subdirectory to find .ips patches. */ - while ((ent = readdir(patch_dir)) != NULL) { - if (std::strcmp(ent->d_name, ".") == 0 || std::strcmp(ent->d_name, "..") == 0) { - continue; - } + /* Print the path for this file. */ + std::snprintf(path + patch_dir_path_len, sizeof(path) - patch_dir_path_len, "/%s", entry.name); - size_t name_len = strlen(ent->d_name); - if (!(IpsFileExtensionLength < name_len && name_len <= ModuleIpsPatchLength)) { - continue; - } - if ((name_len & 1) != 0) { - continue; - } - if (std::strcmp(ent->d_name + name_len - IpsFileExtensionLength, IpsFileExtension) != 0) { - continue; - } - if (!MatchesModuleId(ent->d_name, name_len, IpsFileExtensionLength, module_id)) { - continue; - } + /* Open the file. */ + fs::FileHandle file; + if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) { + continue; + } + ON_SCOPE_EXIT { fs::CloseFile(file); }; - std::snprintf(path, sizeof(path) - 1, "sdmc:/atmosphere/%s/%s/%s", patch_dir_name, pdir_ent->d_name, ent->d_name); - FILE *f_ips = fopen(path, "rb"); - if (f_ips == NULL) { - continue; - } - ON_SCOPE_EXIT { fclose(f_ips); }; - - u8 header[5]; - if (fread(header, 5, 1, f_ips) == 1) { - if (std::memcmp(header, IpsHeadMagic, 5) == 0) { - ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, false, f_ips); - } else if (std::memcmp(header, Ips32HeadMagic, 5) == 0) { - ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, true, f_ips); - } - } - fclose(f_ips); + /* Read the header. */ + u8 header[sizeof(IpsHeadMagic)]; + if (R_SUCCEEDED(fs::ReadFile(file, 0, header, sizeof(header)))) { + if (std::memcmp(header, IpsHeadMagic, sizeof(header)) == 0) { + ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, false, file); + } else if (std::memcmp(header, Ips32HeadMagic, sizeof(header)) == 0) { + ApplyIpsPatch(mapped_module, mapped_size, protected_size, offset, true, file); } - closedir(patch_dir); } } - closedir(patches_dir); } } diff --git a/libraries/libstratosphere/source/util/util_ini.cpp b/libraries/libstratosphere/source/util/util_ini.cpp index ed92fd8bc..06852eeb2 100644 --- a/libraries/libstratosphere/source/util/util_ini.cpp +++ b/libraries/libstratosphere/source/util/util_ini.cpp @@ -24,34 +24,30 @@ namespace ams::util::ini { namespace { - struct FsFileContext { - FsFile *f; - size_t offset; - size_t num_left; + struct FileContext { + fs::FileHandle file; + s64 offset; + s64 num_left; - explicit FsFileContext(FsFile *f) : f(f), offset(0) { - s64 size; - R_ABORT_UNLESS(fsFileGetSize(this->f, &size)); - this->num_left = size_t(size); + explicit FileContext(fs::FileHandle f) : file(f), offset(0) { + R_ABORT_UNLESS(fs::GetFileSize(std::addressof(this->num_left), this->file)); } }; - char *ini_reader_fs_file(char *str, int num, void *stream) { - FsFileContext *ctx = reinterpret_cast(stream); + char *ini_reader_file_handle(char *str, int num, void *stream) { + FileContext *ctx = static_cast(stream); if (ctx->num_left == 0 || num < 2) { return nullptr; } /* Read as many bytes as we can. */ - size_t try_read = std::min(size_t(num - 1), ctx->num_left); - size_t actually_read; - R_ABORT_UNLESS(fsFileRead(ctx->f, ctx->offset, str, try_read, FsReadOption_None, &actually_read)); - AMS_ABORT_UNLESS(actually_read == try_read); + s64 cur_read = std::min(num - 1, ctx->num_left); + R_ABORT_UNLESS(fs::ReadFile(ctx->file, ctx->offset, str, cur_read, fs::ReadOption())); /* Only "read" up to the first \n. */ - size_t offset = actually_read; - for (size_t i = 0; i < actually_read; i++) { + size_t offset = cur_read; + for (auto i = 0; i < cur_read; i++) { if (str[i] == '\n') { offset = i + 1; break; @@ -62,7 +58,7 @@ namespace ams::util::ini { str[offset] = '\0'; /* Update context. */ - ctx->offset += offset; + ctx->offset += offset; ctx->num_left -= offset; return str; @@ -75,17 +71,19 @@ namespace ams::util::ini { return ini_parse_string(ini_str, h, user_ctx); } - int ParseFile(FILE *f, void *user_ctx, Handler h) { - return ini_parse_file(f, h, user_ctx); - } - - int ParseFile(FsFile *f, void *user_ctx, Handler h) { - FsFileContext ctx(f); - return ini_parse_stream(ini_reader_fs_file, &ctx, h, user_ctx); + int ParseFile(fs::FileHandle file, void *user_ctx, Handler h) { + FileContext ctx(file); + return ini_parse_stream(ini_reader_file_handle, &ctx, h, user_ctx); } int ParseFile(const char *path, void *user_ctx, Handler h) { - return ini_parse(path, h, user_ctx); + 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); } } \ No newline at end of file diff --git a/stratosphere/creport/source/creport_scoped_file.hpp b/stratosphere/creport/source/creport_scoped_file.hpp index e82956f87..95be9f54a 100644 --- a/stratosphere/creport/source/creport_scoped_file.hpp +++ b/stratosphere/creport/source/creport_scoped_file.hpp @@ -28,7 +28,7 @@ namespace ams::creport { public: ScopedFile(const char *path) : file(), offset(), opened(false) { if (R_SUCCEEDED(fs::CreateFile(path, 0))) { - this->opened = R_SUCCEEDED(fs::OpenFile(std::addressof(this->file), path, fs::OpenMode_Write | fs::OpenMode_Append)); + this->opened = R_SUCCEEDED(fs::OpenFile(std::addressof(this->file), path, fs::OpenMode_Write | fs::OpenMode_AllowAppend)); } } diff --git a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp index b94d9c83d..ccd463fc7 100644 --- a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp +++ b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp @@ -877,78 +877,74 @@ namespace ams::dmnt::cheat::impl { this->ResetAllCheatEntries(); /* Open the file for program/build_id. */ - FILE *f_cht = nullptr; + fs::FileHandle file; { - char path[FS_MAX_PATH+1] = {0}; - std::snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/contents/%016lx/cheats/%02x%02x%02x%02x%02x%02x%02x%02x.txt", static_cast(program_id), + char path[fs::EntryNameLengthMax + 1]; + std::snprintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/cheats/%02x%02x%02x%02x%02x%02x%02x%02x.txt", program_id.value, build_id[0], build_id[1], build_id[2], build_id[3], build_id[4], build_id[5], build_id[6], build_id[7]); - - f_cht = fopen(path, "rb"); + if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) { + return false; + } } - - /* Check for open failure. */ - if (f_cht == nullptr) { - return false; - } - ON_SCOPE_EXIT { fclose(f_cht); }; + ON_SCOPE_EXIT { fs::CloseFile(file); }; /* Get file size. */ - fseek(f_cht, 0L, SEEK_END); - const size_t cht_sz = ftell(f_cht); - fseek(f_cht, 0L, SEEK_SET); + s64 file_size; + if (R_FAILED(fs::GetFileSize(std::addressof(file_size), file))) { + return false; + } /* Allocate cheat txt buffer. */ - char *cht_txt = reinterpret_cast(std::malloc(cht_sz + 1)); + char *cht_txt = static_cast(std::malloc(file_size + 1)); if (cht_txt == nullptr) { return false; } ON_SCOPE_EXIT { std::free(cht_txt); }; /* Read cheats into buffer. */ - if (fread(cht_txt, 1, cht_sz, f_cht) != cht_sz) { + if (R_FAILED(fs::ReadFile(file, 0, cht_txt, file_size))) { return false; } - cht_txt[cht_sz] = 0; + cht_txt[file_size] = '\x00'; /* Parse cheat buffer. */ return this->ParseCheats(cht_txt, std::strlen(cht_txt)); } bool CheatProcessManager::LoadCheatToggles(const ncm::ProgramId program_id) { - /* Open the file for program_id. */ - FILE *f_tg = nullptr; - { - char path[FS_MAX_PATH+1] = {0}; - std::snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", static_cast(program_id)); - f_tg = fopen(path, "rb"); - } - /* Unless we successfully parse, don't save toggles on close. */ this->should_save_cheat_toggles = false; - /* Check for null, which is allowed. */ - if (f_tg == nullptr) { - return true; + /* Open the file for program_id. */ + fs::FileHandle file; + { + char path[fs::EntryNameLengthMax + 1]; + std::snprintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", program_id.value); + if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Read))) { + /* No file presence is allowed. */ + return true; + } } - ON_SCOPE_EXIT { fclose(f_tg); }; + ON_SCOPE_EXIT { fs::CloseFile(file); }; /* Get file size. */ - fseek(f_tg, 0L, SEEK_END); - const size_t tg_sz = ftell(f_tg); - fseek(f_tg, 0L, SEEK_SET); + s64 file_size; + if (R_FAILED(fs::GetFileSize(std::addressof(file_size), file))) { + return false; + } /* Allocate toggle txt buffer. */ - char *tg_txt = reinterpret_cast(std::malloc(tg_sz + 1)); + char *tg_txt = static_cast(std::malloc(file_size + 1)); if (tg_txt == nullptr) { return false; } ON_SCOPE_EXIT { std::free(tg_txt); }; /* Read cheats into buffer. */ - if (fread(tg_txt, 1, tg_sz, f_tg) != tg_sz) { + if (R_FAILED(fs::ReadFile(file, 0, tg_txt, file_size))) { return false; } - tg_txt[tg_sz] = 0; + tg_txt[file_size] = '\x00'; /* Parse toggle buffer. */ this->should_save_cheat_toggles = this->ParseCheatToggles(tg_txt, std::strlen(tg_txt)); @@ -957,24 +953,34 @@ namespace ams::dmnt::cheat::impl { void CheatProcessManager::SaveCheatToggles(const ncm::ProgramId program_id) { /* Open the file for program_id. */ - FILE *f_tg = nullptr; + fs::FileHandle file; { - char path[FS_MAX_PATH+1] = {0}; - std::snprintf(path, FS_MAX_PATH, "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", static_cast(program_id)); - if ((f_tg = fopen(path, "wb")) == nullptr) { + char path[fs::EntryNameLengthMax + 1]; + std::snprintf(path, sizeof(path), "sdmc:/atmosphere/contents/%016lx/cheats/toggles.txt", program_id.value); + fs::DeleteFile(path); + fs::CreateFile(path, 0); + if (R_FAILED(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Write | fs::OpenMode_AllowAppend))) { return; } } - ON_SCOPE_EXIT { fclose(f_tg); }; + ON_SCOPE_EXIT { fs::CloseFile(file); }; + + s64 offset = 0; + char buf[0x100]; /* Save all non-master cheats. */ for (size_t i = 1; i < MaxCheatCount; i++) { if (this->cheat_entries[i].definition.num_opcodes != 0) { - fprintf(f_tg, "[%s]\n", this->cheat_entries[i].definition.readable_name); - if (this->cheat_entries[i].enabled) { - fprintf(f_tg, "true\n"); - } else { - fprintf(f_tg, "false\n"); + std::snprintf(buf, sizeof(buf), "[%s]\n", this->cheat_entries[i].definition.readable_name); + const size_t name_len = std::strlen(buf); + if (R_SUCCEEDED(fs::WriteFile(file, offset, buf, name_len, fs::WriteOption::Flush))) { + offset += name_len; + } + + const char *entry = this->cheat_entries[i].enabled ? "true\n" : "false\n"; + const size_t entry_len = std::strlen(entry); + if (R_SUCCEEDED(fs::WriteFile(file, offset, entry, entry_len, fs::WriteOption::Flush))) { + offset += entry_len; } } } diff --git a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.cpp b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.cpp index f568d7970..173416126 100644 --- a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.cpp +++ b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.cpp @@ -21,43 +21,66 @@ namespace ams::dmnt::cheat::impl { void CheatVirtualMachine::DebugLog(u32 log_id, u64 value) { /* Just unconditionally try to create the log folder. */ - mkdir("/atmosphere/cheat_vm_logs", 0777); - FILE *f_log = NULL; + fs::EnsureDirectoryRecursively("sdmc:/atmosphere/cheat_vm_logs"); + + fs::FileHandle log_file; { - char log_path[FS_MAX_PATH]; - snprintf(log_path, sizeof(log_path), "/atmosphere/cheat_vm_logs/%x.log", log_id); - f_log = fopen(log_path, "ab"); + char log_path[fs::EntryNameLengthMax + 1]; + std::snprintf(log_path, sizeof(log_path), "sdmc:/atmosphere/cheat_vm_logs/%08x.log", log_id); + if (R_FAILED(fs::OpenFile(std::addressof(log_file), log_path, fs::OpenMode_Write | fs::OpenMode_AllowAppend))) { + return; + } } - if (f_log != NULL) { - ON_SCOPE_EXIT { fclose(f_log); }; - fprintf(f_log, "%016lx\n", value); + ON_SCOPE_EXIT { fs::CloseFile(log_file); }; + + s64 log_offset; + if (R_FAILED(fs::GetFileSize(std::addressof(log_offset), log_file))) { + return; } + + char log_value[18]; + std::snprintf(log_value, sizeof(log_value), "%016lx\n", value); + fs::WriteFile(log_file, log_offset, log_value, std::strlen(log_value), fs::WriteOption::Flush); } void CheatVirtualMachine::OpenDebugLogFile() { #ifdef DMNT_CHEAT_VM_DEBUG_LOG CloseDebugLogFile(); - this->debug_log_file = fopen("cheat_vm_log.txt", "ab"); + R_ABORT_UNLESS(fs::OpenFile(std::addressof(this->debug_log_file), "sdmc:/atmosphere/cheat_vm_logs/debug_log.txt")); + this->debug_log_file_offset = 0; #endif } void CheatVirtualMachine::CloseDebugLogFile() { #ifdef DMNT_CHEAT_VM_DEBUG_LOG - if (this->debug_log_file != NULL) { - fclose(this->debug_log_file); - this->debug_log_file = NULL; + if (this->has_debug_log_file) { + fs::CloseFile(this->debug_log_file); } + this->has_debug_log_file = false; #endif } void CheatVirtualMachine::LogToDebugFile(const char *format, ...) { #ifdef DMNT_CHEAT_VM_DEBUG_LOG - if (this->debug_log_file != NULL) { - va_list arglist; - va_start(arglist, format); - vfprintf(this->debug_log_file, format, arglist); - va_end(arglist); + if (!this->has_debug_log_file) { + return; } + + { + std::va_list vl; + va_start(vl, format); + std::vsnprintf(this->debug_log_format_buf, sizeof(this->debug_log_format_buf) - 1, format, vl); + va_end(vl); + } + + size_t fmt_len = std::strlen(this->debug_log_format_buf); + if (this->debug_log_format_buf[fmt_len - 1] != '\n') { + this->debug_log_format_buf[fmt_len + 0] = '\n'; + this->debug_log_format_buf[fmt_len + 1] = '\x00'; + fmt_len += 1; + } + + fs::WriteFile(this->debug_log_file, this->debug_log_offset, this->debug_log_format_buf, fmt_len, fs::WriteOption::Flush); #endif } diff --git a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.hpp b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.hpp index 93282c275..0f267496f 100644 --- a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.hpp +++ b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.hpp @@ -296,7 +296,10 @@ namespace ams::dmnt::cheat::impl { void Execute(const CheatProcessMetadata *metadata); #ifdef DMNT_CHEAT_VM_DEBUG_LOG private: - FILE *debug_log_file = NULL; + fs::FileHandle debug_log_file; + s64 debug_log_file_offset; + bool has_debug_log_file; + char debug_log_format_buf[0x100]; #endif }; diff --git a/stratosphere/dmnt/source/dmnt_main.cpp b/stratosphere/dmnt/source/dmnt_main.cpp index 119cb65bd..f4c5a3842 100644 --- a/stratosphere/dmnt/source/dmnt_main.cpp +++ b/stratosphere/dmnt/source/dmnt_main.cpp @@ -78,14 +78,13 @@ void __appInit(void) { R_ABORT_UNLESS(fsInitialize()); }); - R_ABORT_UNLESS(fsdevMountSdmc()); + R_ABORT_UNLESS(fs::MountSdCard("sdmc")); ams::CheckApiVersion(); } void __appExit(void) { /* Cleanup services. */ - fsdevUnmountAll(); fsExit(); hidExit(); setsysExit(); diff --git a/stratosphere/fatal/source/fatal_main.cpp b/stratosphere/fatal/source/fatal_main.cpp index bad6aaa60..e23cbe1a4 100644 --- a/stratosphere/fatal/source/fatal_main.cpp +++ b/stratosphere/fatal/source/fatal_main.cpp @@ -103,7 +103,6 @@ void __appInit(void) { void __appExit(void) { /* Cleanup services. */ - fsdevUnmountAll(); fsExit(); plExit(); gpioExit(); diff --git a/stratosphere/fatal/source/fatal_scoped_file.hpp b/stratosphere/fatal/source/fatal_scoped_file.hpp index 73603d3ea..65050e085 100644 --- a/stratosphere/fatal/source/fatal_scoped_file.hpp +++ b/stratosphere/fatal/source/fatal_scoped_file.hpp @@ -28,7 +28,7 @@ namespace ams::fatal::srv { public: ScopedFile(const char *path) : file(), offset(), opened(false) { if (R_SUCCEEDED(fs::CreateFile(path, 0))) { - this->opened = R_SUCCEEDED(fs::OpenFile(std::addressof(this->file), path, fs::OpenMode_Write | fs::OpenMode_Append)); + this->opened = R_SUCCEEDED(fs::OpenFile(std::addressof(this->file), path, fs::OpenMode_Write | fs::OpenMode_AllowAppend)); } } diff --git a/stratosphere/ro/source/ro_main.cpp b/stratosphere/ro/source/ro_main.cpp index 36344558f..13c0c69f7 100644 --- a/stratosphere/ro/source/ro_main.cpp +++ b/stratosphere/ro/source/ro_main.cpp @@ -70,13 +70,12 @@ void __appInit(void) { } }); - R_ABORT_UNLESS(fsdevMountSdmc()); + R_ABORT_UNLESS(fs::MountSdCard("sdmc")); ams::CheckApiVersion(); } void __appExit(void) { - fsdevUnmountAll(); fsExit(); if (hos::GetVersion() < hos::Version_300) { pminfoExit();