ncm: placeholder accessor cleanup

This commit is contained in:
Adubbz 2020-02-29 11:40:37 +11:00
parent 1483df1f77
commit 7a4ecc67a1
3 changed files with 28 additions and 35 deletions

View File

@ -201,7 +201,7 @@ namespace ams::ncm::impl {
return FindContentMetaDatabaseEntry(out, storage_id); return FindContentMetaDatabaseEntry(out, storage_id);
} }
inline Result GetContentStorageNotActiveResult(StorageId storage_id) { ALWAYS_INLINE Result GetContentStorageNotActiveResult(StorageId storage_id) {
switch (storage_id) { switch (storage_id) {
case StorageId::GameCard: case StorageId::GameCard:
return ResultGameCardContentStorageNotActive(); return ResultGameCardContentStorageNotActive();
@ -216,7 +216,7 @@ namespace ams::ncm::impl {
} }
} }
inline Result GetContentMetaDatabaseNotActiveResult(StorageId storage_id) { ALWAYS_INLINE Result GetContentMetaDatabaseNotActiveResult(StorageId storage_id) {
switch (storage_id) { switch (storage_id) {
case StorageId::GameCard: case StorageId::GameCard:
return ResultGameCardContentMetaDatabaseNotActive(); return ResultGameCardContentMetaDatabaseNotActive();
@ -241,7 +241,7 @@ namespace ams::ncm::impl {
return ResultSuccess(); return ResultSuccess();
} }
void ReplaceMountName(char *out_path, const char *mount_name, const char *root_path) { inline void ReplaceMountName(char *out_path, const char *mount_name, const char *root_path) {
strcpy(out_path, mount_name); strcpy(out_path, mount_name);
strcat(out_path, strchr(root_path, ':')); strcat(out_path, strchr(root_path, ':'));
} }

View File

@ -22,6 +22,17 @@
namespace ams::ncm::impl { namespace ams::ncm::impl {
namespace {
ALWAYS_INLINE Result ConvertNotFoundResult(Result r) {
R_TRY_CATCH(r) {
R_CONVERT(ams::fs::ResultPathNotFound, ncm::ResultPlaceHolderNotFound())
} R_END_TRY_CATCH;
return ResultSuccess();
}
}
Result PlaceHolderAccessor::Open(FILE** out_handle, PlaceHolderId placeholder_id) { Result PlaceHolderAccessor::Open(FILE** out_handle, PlaceHolderId placeholder_id) {
R_UNLESS(!this->LoadFromCache(out_handle, placeholder_id), ResultSuccess()); R_UNLESS(!this->LoadFromCache(out_handle, placeholder_id), ResultSuccess());
char placeholder_path[FS_MAX_PATH] = {0}; char placeholder_path[FS_MAX_PATH] = {0};
@ -57,15 +68,12 @@ namespace ams::ncm::impl {
PlaceHolderAccessor::CacheEntry *PlaceHolderAccessor::GetFreeEntry() { PlaceHolderAccessor::CacheEntry *PlaceHolderAccessor::GetFreeEntry() {
/* Try to find an already free entry. */ /* Try to find an already free entry. */
CacheEntry *entry = this->FindInCache(InvalidPlaceHolderId); if (CacheEntry *entry = this->FindInCache(InvalidPlaceHolderId); entry != nullptr) {
if (entry) {
return entry; return entry;
} }
/* Get the oldest entry. */ /* Get the oldest entry. */
{ CacheEntry *entry = &this->caches[0];
entry = &this->caches[0];
for (size_t i = 1; i < MaxCaches; i++) { for (size_t i = 1; i < MaxCaches; i++) {
if (entry->counter > this->caches[i].counter) { if (entry->counter > this->caches[i].counter) {
entry = &this->caches[i]; entry = &this->caches[i];
@ -74,7 +82,6 @@ namespace ams::ncm::impl {
this->Invalidate(entry); this->Invalidate(entry);
return entry; return entry;
} }
}
void PlaceHolderAccessor::StoreToCache(FILE *handle, PlaceHolderId placeholder_id) { void PlaceHolderAccessor::StoreToCache(FILE *handle, PlaceHolderId placeholder_id) {
std::scoped_lock lk(this->cache_mutex); std::scoped_lock lk(this->cache_mutex);
@ -106,10 +113,10 @@ namespace ams::ncm::impl {
return 1; return 1;
} else if (this->make_placeholder_path_func == static_cast<MakePlaceHolderPathFunc>(path::MakePlaceHolderPathHashByteLayered)) { } else if (this->make_placeholder_path_func == static_cast<MakePlaceHolderPathFunc>(path::MakePlaceHolderPathHashByteLayered)) {
return 2; return 2;
} } else {
AMS_ABORT(); AMS_ABORT();
} }
}
void PlaceHolderAccessor::GetPath(char *placeholder_path_out, PlaceHolderId placeholder_id) { void PlaceHolderAccessor::GetPath(char *placeholder_path_out, PlaceHolderId placeholder_id) {
std::scoped_lock lock(this->cache_mutex); std::scoped_lock lock(this->cache_mutex);
@ -135,12 +142,7 @@ namespace ams::ncm::impl {
char placeholder_path[FS_MAX_PATH] = {0}; char placeholder_path[FS_MAX_PATH] = {0};
this->GetPath(placeholder_path, placeholder_id); this->GetPath(placeholder_path, placeholder_id);
R_UNLESS(std::remove(placeholder_path) == 0, ConvertNotFoundResult(fsdevGetLastResult()));
if (std::remove(placeholder_path) != 0) {
R_TRY_CATCH(fsdevGetLastResult()) {
R_CONVERT(ams::fs::ResultPathNotFound, ncm::ResultPlaceHolderNotFound())
} R_END_TRY_CATCH;
}
return ResultSuccess(); return ResultSuccess();
} }
@ -148,13 +150,8 @@ namespace ams::ncm::impl {
Result PlaceHolderAccessor::Write(PlaceHolderId placeholder_id, size_t offset, const void *buffer, size_t size) { Result PlaceHolderAccessor::Write(PlaceHolderId placeholder_id, size_t offset, const void *buffer, size_t size) {
FILE *f = nullptr; FILE *f = nullptr;
R_TRY_CATCH(this->Open(&f, placeholder_id)) { R_TRY(ConvertNotFoundResult(this->Open(&f, placeholder_id)));
R_CONVERT(ams::fs::ResultPathNotFound, ncm::ResultPlaceHolderNotFound()) ON_SCOPE_EXIT { this->StoreToCache(f, placeholder_id); };
} R_END_TRY_CATCH;
ON_SCOPE_EXIT {
this->StoreToCache(f, placeholder_id);
};
R_TRY(fs::WriteFile(f, offset, buffer, size, !this->delay_flush)); R_TRY(fs::WriteFile(f, offset, buffer, size, !this->delay_flush));
return ResultSuccess(); return ResultSuccess();
@ -163,11 +160,7 @@ namespace ams::ncm::impl {
Result PlaceHolderAccessor::SetSize(PlaceHolderId placeholder_id, size_t size) { Result PlaceHolderAccessor::SetSize(PlaceHolderId placeholder_id, size_t size) {
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);
if (truncate(placeholder_path, size) == -1) { R_UNLESS(truncate(placeholder_path, size) != -1, ConvertNotFoundResult(fsdevGetLastResult()));
R_TRY_CATCH(fsdevGetLastResult()) {
R_CONVERT(ams::fs::ResultPathNotFound, ncm::ResultPlaceHolderNotFound())
} R_END_TRY_CATCH;
}
return ResultSuccess(); return ResultSuccess();
} }

View File

@ -22,7 +22,7 @@
namespace ams::ncm::fs { namespace ams::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, ams::fs::WriteOption option);
Result ReadFile(FILE *f, size_t offset, void *buffer, size_t size); 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);