diff --git a/stratosphere/ncm/source/ncm_contentstorage.cpp b/stratosphere/ncm/source/ncm_contentstorage.cpp index b62f792ed..3a698a9ba 100644 --- a/stratosphere/ncm/source/ncm_contentstorage.cpp +++ b/stratosphere/ncm/source/ncm_contentstorage.cpp @@ -456,18 +456,10 @@ namespace sts::ncm { } R_TRY(this->EnsureEnabled()); - char content_path[FS_MAX_PATH] = {0}; this->GetContentPath(content_path, content_id); R_TRY(this->OpenCachedContentFile(content_id)); - - if (fseek(this->content_cache_file_handle, offset, SEEK_SET) != 0) { - return fsdevGetLastResult(); - } - - if (fread(buf.buffer, 1, buf.num_elements, this->content_cache_file_handle) != buf.num_elements && ferror(this->content_cache_file_handle)) { - return fsdevGetLastResult(); - } + R_TRY(ReadFile(this->content_cache_file_handle, offset, buf.buffer, buf.num_elements)); return ResultSuccess; R_DEBUG_END diff --git a/stratosphere/ncm/source/ncm_fs.cpp b/stratosphere/ncm/source/ncm_fs.cpp index 6a5616aa1..03a1941b8 100644 --- a/stratosphere/ncm/source/ncm_fs.cpp +++ b/stratosphere/ncm/source/ncm_fs.cpp @@ -68,7 +68,7 @@ namespace sts::ncm { return fsdevGetLastResult(); } - if (fwrite(buffer, size, 1, f) != 1) { + if (fwrite(buffer, 1, size, f) != size) { return fsdevGetLastResult(); } @@ -80,6 +80,18 @@ namespace sts::ncm { R_DEBUG_END } + Result ReadFile(FILE* f, size_t offset, void* buffer, size_t size) { + if (fseek(f, offset, SEEK_SET) != 0) { + return fsdevGetLastResult(); + } + + if (fread(buffer, 1, size, f) != size && ferror(f)) { + return fsdevGetLastResult(); + } + + return ResultSuccess; + } + Result HasFile(bool* out, const char* path) { struct stat st; @@ -219,6 +231,18 @@ namespace sts::ncm { return mount_name; } + Result GetMountNameFromPath(MountName* mount_name, const char* path) { + const char* unqual_path = strchr(path, ':'); + + /* We should be given a qualified path. */ + if (!unqual_path || unqual_path > path + 0xf) { + return ResultFsInvalidMountName; + } + + strncpy(mount_name->name, path, unqual_path - path); + return ResultSuccess; + } + Result MountSystemSaveData(const char* mount_point, FsSaveDataSpaceId space_id, u64 save_id) { if (!mount_point) { return ResultFsNullptrArgument; @@ -319,14 +343,7 @@ namespace sts::ncm { } MountName mount_name = {0}; - const char* unqual_path = strchr(path, ':'); - - /* We should be given a qualified path. */ - if (!unqual_path || unqual_path > path + 0xf) { - return ResultInvalidMountName; - } - - strncpy(mount_name.name, path, unqual_path - path); + R_TRY(GetMountNameFromPath(&mount_name, path)); if (!fsdevGetDeviceFileSystem(mount_name.name) || g_mount_content_storage.find(mount_name.name) == g_mount_content_storage.end()) { return ResultFsMountNameNotFound; diff --git a/stratosphere/ncm/source/ncm_fs.hpp b/stratosphere/ncm/source/ncm_fs.hpp index 335580d48..4e857406d 100644 --- a/stratosphere/ncm/source/ncm_fs.hpp +++ b/stratosphere/ncm/source/ncm_fs.hpp @@ -25,6 +25,7 @@ namespace sts::ncm { 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 ReadFile(FILE* f, size_t offset, void* buffer, size_t size); Result HasFile(bool* out, const char* path); Result HasDirectory(bool* out, const char* path); @@ -40,6 +41,8 @@ namespace sts::ncm { Result GetGameCardHandle(FsGameCardHandle* out_handle); MountName CreateUniqueMountName(); + Result GetMountNameFromPath(MountName* mount_name, const char* path); + Result MountSystemSaveData(const char* mount_point, FsSaveDataSpaceId space_id, u64 save_id); Result MountContentStorage(const char* mount_point, FsContentStorageId id); Result MountGameCardPartition(const char* mount_point, const FsGameCardHandle handle, FsGameCardPartiton partition); diff --git a/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp b/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp index 63f893f98..b70b36d7d 100644 --- a/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp +++ b/stratosphere/ncm/source/ncm_readonlycontentstorage.cpp @@ -221,13 +221,7 @@ namespace sts::ncm { fclose(f); }; - if (fseek(f, offset, SEEK_SET) != 0) { - return fsdevGetLastResult(); - } - - if (fread(buf.buffer, 1, buf.num_elements, f) != buf.num_elements && ferror(f)) { - return fsdevGetLastResult(); - } + R_TRY(ReadFile(f, offset, buf.buffer, buf.num_elements)); return ResultSuccess; R_DEBUG_END diff --git a/stratosphere/ncm/source/ncm_types.hpp b/stratosphere/ncm/source/ncm_types.hpp index 587e59404..17c0b5d00 100644 --- a/stratosphere/ncm/source/ncm_types.hpp +++ b/stratosphere/ncm/source/ncm_types.hpp @@ -152,13 +152,13 @@ namespace sts::ncm { typedef void (*MakePlaceHolderPathFunc)(char* out, PlaceHolderId placeholder_id, const char* root); // TODO: Move to libstrat - static constexpr Result ResultNcmStoragePathNotFound = MAKERESULT(Module_Ncm, 1); - static constexpr Result ResultNcmInvalidPlaceHolderDirectoryEntry = MAKERESULT(Module_Ncm, 170); - static constexpr Result ResultNcmInvalidContentStorageOperation = MAKERESULT(Module_Ncm, 190); - static constexpr Result ResultNcmStorageRootNotFound = MAKERESULT(Module_Ncm, 310); + static constexpr Result ResultNcmStoragePathNotFound = MAKERESULT(Module_Ncm, 1); + static constexpr Result ResultNcmInvalidPlaceHolderDirectoryEntry = MAKERESULT(Module_Ncm, 170); + static constexpr Result ResultNcmInvalidContentStorageOperation = MAKERESULT(Module_Ncm, 190); + static constexpr Result ResultNcmStorageRootNotFound = MAKERESULT(Module_Ncm, 310); - static constexpr Result ResultFileExtensionWithoutOpenModeAllowAppend = MAKERESULT(Module_Fs, 6201); - static constexpr Result ResultInvalidMountName = MAKERESULT(Module_Fs, 6065); - static constexpr Result ResultFsMountNameNotFound = MAKERESULT(Module_Fs, 6905); + static constexpr Result ResultFsFileExtensionWithoutOpenModeAllowAppend = MAKERESULT(Module_Fs, 6201); + static constexpr Result ResultFsInvalidMountName = MAKERESULT(Module_Fs, 6065); + static constexpr Result ResultFsMountNameNotFound = MAKERESULT(Module_Fs, 6905); } \ No newline at end of file