errno begone

This commit is contained in:
Adubbz 2019-07-27 11:59:56 +10:00
parent 65b7ac9039
commit 8f65beef78
5 changed files with 65 additions and 105 deletions

View file

@ -49,7 +49,7 @@ namespace sts::ncm::impl {
if (found_cache) { if (found_cache) {
/* Flush and close */ /* Flush and close */
fflush(found_cache->handle)); fflush(found_cache->handle);
fclose(found_cache->handle); fclose(found_cache->handle);
std::fill(found_cache->id.uuid, found_cache->id.uuid + sizeof(PlaceHolderId), 0); std::fill(found_cache->id.uuid, found_cache->id.uuid + sizeof(PlaceHolderId), 0);
} }
@ -94,23 +94,20 @@ namespace sts::ncm::impl {
char placeholder_path[FS_MAX_PATH] = {0}; char placeholder_path[FS_MAX_PATH] = {0};
this->GetPlaceHolderPath(placeholder_path, placeholder_id); this->GetPlaceHolderPath(placeholder_path, placeholder_id);
errno = 0; FILE* f = fopen(placeholder_path, "r+b");
*out_handle = fopen(placeholder_path, "r+b");
if (errno != 0) { if (f == nullptr) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
*out_handle = f;
return ResultSuccess; return ResultSuccess;
} }
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};
errno = 0;
this->GetPlaceHolderPath(placeholder_path, placeholder_id); this->GetPlaceHolderPath(placeholder_path, placeholder_id);
truncate(placeholder_path, size); if (truncate(placeholder_path, size) == -1) {
if (errno != 0) {
R_TRY_CATCH(fsdevGetLastResult()) { R_TRY_CATCH(fsdevGetLastResult()) {
R_CATCH(ResultFsPathNotFound) { R_CATCH(ResultFsPathNotFound) {
return ResultNcmPlaceHolderNotFound; return ResultNcmPlaceHolderNotFound;
@ -144,14 +141,13 @@ namespace sts::ncm::impl {
f = cache_entry->handle; f = cache_entry->handle;
} }
this->FlushCache(f, placeholder_id); this->StoreToCache(f, placeholder_id);
errno = 0; if (!fseek(f, 0L, SEEK_END)) {
fseek(f, 0L, SEEK_END); return fsdevGetLastResult();
}
size_t size = ftell(f); size_t size = ftell(f);
fseek(f, 0L, SEEK_SET); if (!fseek(f, 0L, SEEK_SET)) {
if (errno != 0) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
@ -190,7 +186,7 @@ namespace sts::ncm::impl {
return nullptr; return nullptr;
} }
void PlaceHolderAccessor::FlushCache(FILE* handle, PlaceHolderId placeholder_id) { void PlaceHolderAccessor::StoreToCache(FILE* handle, PlaceHolderId placeholder_id) {
std::scoped_lock<HosMutex> lk(this->cache_mutex); std::scoped_lock<HosMutex> lk(this->cache_mutex);
CacheEntry* cache = nullptr; CacheEntry* cache = nullptr;
@ -226,7 +222,7 @@ namespace sts::ncm::impl {
CacheEntry* cache = &this->caches[i]; CacheEntry* cache = &this->caches[i];
if (cache->id != InvalidUuid) { if (cache->id != InvalidUuid) {
fflush(cache->handle)); fflush(cache->handle);
fclose(cache->handle); fclose(cache->handle);
cache->id = InvalidUuid; cache->id = InvalidUuid;
} }

View file

@ -69,7 +69,7 @@ namespace sts::ncm::impl {
CacheEntry *FindInCache(PlaceHolderId placeholder_id); CacheEntry *FindInCache(PlaceHolderId placeholder_id);
bool LoadFromCache(FILE** out_handle, PlaceHolderId placeholder_id); bool LoadFromCache(FILE** out_handle, PlaceHolderId placeholder_id);
void FlushCache(FILE* handle, PlaceHolderId placeholder_id); void StoreToCache(FILE* handle, PlaceHolderId placeholder_id);
void ClearAllCaches(); void ClearAllCaches();
}; };

View file

@ -80,10 +80,9 @@ namespace sts::ncm {
this->ClearContentCache(); this->ClearContentCache();
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);
errno = 0;
this->content_cache_file_handle = fopen(content_path, "rb"); this->content_cache_file_handle = fopen(content_path, "rb");
if (this->content_cache_file_handle == NULL || errno != 0) { if (this->content_cache_file_handle == NULL) {
R_TRY_CATCH(fsdevGetLastResult()) { R_TRY_CATCH(fsdevGetLastResult()) {
R_CATCH(ResultFsPathNotFound) { R_CATCH(ResultFsPathNotFound) {
return ResultNcmContentNotFound; return ResultNcmContentNotFound;
@ -159,20 +158,20 @@ namespace sts::ncm {
} }
} R_END_TRY_CATCH; } R_END_TRY_CATCH;
errno = 0; if (!fseek(f, offset, SEEK_SET)) {
fseek(f, offset, SEEK_SET);
fwrite(data.buffer, sizeof(u8), data.num_elements, f);
if (!this->placeholder_accessor.delay_flush) {
fflush(f));
}
this->placeholder_accessor.FlushCache(f, placeholder_id);
if (errno != 0) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
if (fwrite(data.buffer, sizeof(u8), data.num_elements, f) != data.num_elements) {
return fsdevGetLastResult();
}
if (!this->placeholder_accessor.delay_flush) {
fflush(f);
}
this->placeholder_accessor.StoreToCache(f, placeholder_id);
return ResultSuccess; return ResultSuccess;
} }
@ -189,10 +188,7 @@ namespace sts::ncm {
this->placeholder_accessor.GetPlaceHolderPathUncached(placeholder_path, placeholder_id); this->placeholder_accessor.GetPlaceHolderPathUncached(placeholder_path, placeholder_id);
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
errno = 0; if (!rename(placeholder_path, content_path)) {
rename(placeholder_path, content_path);
if (errno != 0) {
R_TRY_CATCH(fsdevGetLastResult()) { R_TRY_CATCH(fsdevGetLastResult()) {
R_CATCH(ResultFsPathNotFound) { R_CATCH(ResultFsPathNotFound) {
return ResultNcmPlaceHolderNotFound; return ResultNcmPlaceHolderNotFound;
@ -395,10 +391,7 @@ namespace sts::ncm {
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
struct stat st; struct stat st;
errno = 0; if (stat(content_path, &st) == -1) {
stat(content_path, &st);
if (errno != 0) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
@ -430,10 +423,7 @@ namespace sts::ncm {
R_TRY(this->placeholder_accessor.EnsureRecursively(placeholder_id)); R_TRY(this->placeholder_accessor.EnsureRecursively(placeholder_id));
this->placeholder_accessor.GetPlaceHolderPathUncached(placeholder_path, placeholder_id); this->placeholder_accessor.GetPlaceHolderPathUncached(placeholder_path, placeholder_id);
errno = 0; if (!rename(old_content_path, placeholder_path)) {
rename(old_content_path, placeholder_path);
if (errno != 0) {
R_TRY_CATCH(fsdevGetLastResult()) { R_TRY_CATCH(fsdevGetLastResult()) {
R_CATCH(ResultFsPathNotFound) { R_CATCH(ResultFsPathNotFound) {
return ResultNcmPlaceHolderNotFound; return ResultNcmPlaceHolderNotFound;
@ -442,7 +432,7 @@ namespace sts::ncm {
return ResultNcmPlaceHolderAlreadyExists; return ResultNcmPlaceHolderAlreadyExists;
} }
} R_END_TRY_CATCH; } R_END_TRY_CATCH;
} }
return ResultSuccess; return ResultSuccess;
} }
@ -470,11 +460,11 @@ namespace sts::ncm {
this->GetContentPath(content_path, content_id); this->GetContentPath(content_path, content_id);
R_TRY(this->OpenCachedContentFile(content_id)); R_TRY(this->OpenCachedContentFile(content_id));
errno = 0; if (!fseek(this->content_cache_file_handle, offset, SEEK_SET)) {
fseek(this->content_cache_file_handle, offset, SEEK_SET); return fsdevGetLastResult();
fread(buf.buffer, buf.num_elements, 1, this->content_cache_file_handle); }
if (errno != 0) { 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 fsdevGetLastResult();
} }
@ -563,8 +553,6 @@ namespace sts::ncm {
} }
Result ContentStorageInterface::WriteContentForDebug(ContentId content_id, u64 offset, InBuffer<u8> data) { Result ContentStorageInterface::WriteContentForDebug(ContentId content_id, u64 offset, InBuffer<u8> data) {
FILE* f = nullptr;
/* Offset is too large */ /* Offset is too large */
if (offset >> 0x3f != 0) { if (offset >> 0x3f != 0) {
return ResultNcmInvalidOffset; return ResultNcmInvalidOffset;
@ -585,34 +573,31 @@ namespace sts::ncm {
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);
errno = 0; FILE* f = fopen(content_path, "r+b");
f = fopen(content_path, "r+b");
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
fclose(f); fclose(f);
}; };
if (f == NULL || errno != 0) { if (f == nullptr) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
fseek(f, offset, SEEK_SET); if (!fseek(f, offset, SEEK_SET)) {
fwrite(data.buffer, sizeof(u8), data.num_elements, f);
fflush(f));
if (errno != 0) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
if (fwrite(data.buffer, sizeof(u8), data.num_elements, f) != data.num_elements) {
return fsdevGetLastResult();
}
fflush(f);
return ResultSuccess; return ResultSuccess;
} }
Result ContentStorageInterface::GetFreeSpaceSize(Out<u64> out_size) { Result ContentStorageInterface::GetFreeSpaceSize(Out<u64> out_size) {
struct statvfs st = {0}; struct statvfs st = {0};
errno = 0; if (statvfs(this->root_path, &st) == -1) {
statvfs(this->root_path, &st);
if (errno != 0) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
@ -622,10 +607,7 @@ namespace sts::ncm {
Result ContentStorageInterface::GetTotalSpaceSize(Out<u64> out_size) { Result ContentStorageInterface::GetTotalSpaceSize(Out<u64> out_size) {
struct statvfs st = {0}; struct statvfs st = {0};
errno = 0; if (statvfs(this->root_path, &st) == -1) {
statvfs(this->root_path, &st);
if (errno != 0) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
@ -657,11 +639,8 @@ namespace sts::ncm {
struct stat st; struct stat st;
this->placeholder_accessor.GetPlaceHolderPathUncached(placeholder_path, placeholder_id); this->placeholder_accessor.GetPlaceHolderPathUncached(placeholder_path, placeholder_id);
errno = 0; if (stat(placeholder_path, &st) == -1) {
stat(placeholder_path, &st); return fsdevGetLastResult();;
if (errno != 0) {
return fsdevGetLastResult();
} }
out_size.SetValue(st.st_size); out_size.SetValue(st.st_size);

View file

@ -22,36 +22,32 @@
namespace sts::ncm { namespace sts::ncm {
Result HasFile(bool* out, const char* path) { Result HasFile(bool* out, const char* path) {
errno = 0;
struct stat st; struct stat st;
if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) { if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
*out = true; *out = true;
} else { } else {
*out = false; R_TRY_CATCH(fsdevGetLastResult()) {
} R_CATCH(ResultFsPathNotFound) {
*out = false;
/* It is a valid state for the file to not exist. */ }
if (errno != 0 && errno != ENOENT && errno != ENOTDIR) { } R_END_TRY_CATCH;
return fsdevGetLastResult();
} }
return ResultSuccess; return ResultSuccess;
} }
Result HasDirectory(bool* out, const char* path) { Result HasDirectory(bool* out, const char* path) {
errno = 0;
struct stat st; struct stat st;
if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) { if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
*out = true; *out = true;
} else { } else {
*out = false; R_TRY_CATCH(fsdevGetLastResult()) {
} R_CATCH(ResultFsPathNotFound) {
*out = false;
/* It is a valid state for the directory to not exist. */ }
if (errno != 0 && errno != ENOENT && errno != ENOTDIR) { } R_END_TRY_CATCH;
return fsdevGetLastResult();
} }
return ResultSuccess; return ResultSuccess;
@ -61,8 +57,6 @@ namespace sts::ncm {
char content_root[FS_MAX_PATH] = {0}; char content_root[FS_MAX_PATH] = {0};
char placeholder_root[FS_MAX_PATH] = {0}; char placeholder_root[FS_MAX_PATH] = {0};
errno = 0;
bool has_root = false; bool has_root = false;
R_TRY(HasDirectory(&has_root, root_path)); R_TRY(HasDirectory(&has_root, root_path));
if (!has_root) { if (!has_root) {
@ -118,13 +112,9 @@ namespace sts::ncm {
if (path_len != 0) { if (path_len != 0) {
for (size_t i = 0; i < path_len; i++) { for (size_t i = 0; i < path_len; i++) {
if (i != 0 && working_path_buf[i + 1] == '/' && working_path_buf[i] != ':') { if (i != 0 && working_path_buf[i + 1] == '/' && working_path_buf[i] != ':') {
/* Wipe the errno to prevent cross-contamination */
errno = 0;
/* Temporarily make the path terminate before the '/' */ /* Temporarily make the path terminate before the '/' */
working_path_buf[i + 1] = 0; working_path_buf[i + 1] = 0;
mkdir(working_path_buf + 1, S_IRWXU); if (mkdir(working_path_buf + 1, S_IRWXU) == -1) {
if (errno != 0) {
R_TRY_CATCH(fsdevGetLastResult()) { R_TRY_CATCH(fsdevGetLastResult()) {
R_CATCH(ResultFsPathAlreadyExists) { R_CATCH(ResultFsPathAlreadyExists) {
/* If the path already exists, that's okay. Anything else is an error. */ /* If the path already exists, that's okay. Anything else is an error. */

View file

@ -142,10 +142,7 @@ namespace sts::ncm {
} }
struct stat st; struct stat st;
errno = 0; if (stat(content_path, &st) == -1) {
stat(content_path, &st);
if (errno != 0) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
@ -186,23 +183,21 @@ namespace sts::ncm {
path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path); path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path);
} }
errno = 0;
FILE* f = fopen(content_path, "rb"); FILE* f = fopen(content_path, "rb");
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
fclose(f); fclose(f);
}; };
if (f == NULL || errno != 0) { if (f == nullptr) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
if (!fseek(f, offset, SEEK_SET)) {
return fsdevGetLastResult();
}
errno = 0; if (fread(buf.buffer, 1, buf.num_elements, f) != buf.num_elements && ferror(f)) {
fseek(f, offset, SEEK_SET); return fsdevGetLastResult();
fread(buf.buffer, buf.num_elements, 1, f);
if (errno != 0) {
return fsdevGetLastResult();
} }
return ResultSuccess; return ResultSuccess;