Misc cleanup

This commit is contained in:
Adubbz 2019-08-08 16:42:25 +10:00
parent 7dce15bcda
commit e528269b53
5 changed files with 38 additions and 32 deletions

View file

@ -456,18 +456,10 @@ namespace sts::ncm {
} }
R_TRY(this->EnsureEnabled()); R_TRY(this->EnsureEnabled());
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));
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();
}
return ResultSuccess; return ResultSuccess;
R_DEBUG_END R_DEBUG_END

View file

@ -68,7 +68,7 @@ namespace sts::ncm {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
if (fwrite(buffer, size, 1, f) != 1) { if (fwrite(buffer, 1, size, f) != size) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
@ -80,6 +80,18 @@ namespace sts::ncm {
R_DEBUG_END 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) { Result HasFile(bool* out, const char* path) {
struct stat st; struct stat st;
@ -219,6 +231,18 @@ namespace sts::ncm {
return mount_name; 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) { Result MountSystemSaveData(const char* mount_point, FsSaveDataSpaceId space_id, u64 save_id) {
if (!mount_point) { if (!mount_point) {
return ResultFsNullptrArgument; return ResultFsNullptrArgument;
@ -319,14 +343,7 @@ namespace sts::ncm {
} }
MountName mount_name = {0}; MountName mount_name = {0};
const char* unqual_path = strchr(path, ':'); R_TRY(GetMountNameFromPath(&mount_name, 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);
if (!fsdevGetDeviceFileSystem(mount_name.name) || g_mount_content_storage.find(mount_name.name) == g_mount_content_storage.end()) { if (!fsdevGetDeviceFileSystem(mount_name.name) || g_mount_content_storage.find(mount_name.name) == g_mount_content_storage.end()) {
return ResultFsMountNameNotFound; return ResultFsMountNameNotFound;

View file

@ -25,6 +25,7 @@ namespace sts::ncm {
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);
Result ReadFile(FILE* f, size_t offset, void* buffer, size_t size);
Result HasFile(bool* out, const char* path); Result HasFile(bool* out, const char* path);
Result HasDirectory(bool* out, const char* path); Result HasDirectory(bool* out, const char* path);
@ -40,6 +41,8 @@ namespace sts::ncm {
Result GetGameCardHandle(FsGameCardHandle* out_handle); Result GetGameCardHandle(FsGameCardHandle* out_handle);
MountName CreateUniqueMountName(); MountName CreateUniqueMountName();
Result GetMountNameFromPath(MountName* mount_name, const char* path);
Result MountSystemSaveData(const char* mount_point, FsSaveDataSpaceId space_id, u64 save_id); Result MountSystemSaveData(const char* mount_point, FsSaveDataSpaceId space_id, u64 save_id);
Result MountContentStorage(const char* mount_point, FsContentStorageId id); Result MountContentStorage(const char* mount_point, FsContentStorageId id);
Result MountGameCardPartition(const char* mount_point, const FsGameCardHandle handle, FsGameCardPartiton partition); Result MountGameCardPartition(const char* mount_point, const FsGameCardHandle handle, FsGameCardPartiton partition);

View file

@ -221,13 +221,7 @@ namespace sts::ncm {
fclose(f); fclose(f);
}; };
if (fseek(f, offset, SEEK_SET) != 0) { R_TRY(ReadFile(f, offset, buf.buffer, buf.num_elements));
return fsdevGetLastResult();
}
if (fread(buf.buffer, 1, buf.num_elements, f) != buf.num_elements && ferror(f)) {
return fsdevGetLastResult();
}
return ResultSuccess; return ResultSuccess;
R_DEBUG_END R_DEBUG_END

View file

@ -157,8 +157,8 @@ namespace sts::ncm {
static constexpr Result ResultNcmInvalidContentStorageOperation = MAKERESULT(Module_Ncm, 190); static constexpr Result ResultNcmInvalidContentStorageOperation = MAKERESULT(Module_Ncm, 190);
static constexpr Result ResultNcmStorageRootNotFound = MAKERESULT(Module_Ncm, 310); static constexpr Result ResultNcmStorageRootNotFound = MAKERESULT(Module_Ncm, 310);
static constexpr Result ResultFileExtensionWithoutOpenModeAllowAppend = MAKERESULT(Module_Fs, 6201); static constexpr Result ResultFsFileExtensionWithoutOpenModeAllowAppend = MAKERESULT(Module_Fs, 6201);
static constexpr Result ResultInvalidMountName = MAKERESULT(Module_Fs, 6065); static constexpr Result ResultFsInvalidMountName = MAKERESULT(Module_Fs, 6065);
static constexpr Result ResultFsMountNameNotFound = MAKERESULT(Module_Fs, 6905); static constexpr Result ResultFsMountNameNotFound = MAKERESULT(Module_Fs, 6905);
} }