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

View file

@ -69,7 +69,7 @@ namespace sts::ncm::impl {
CacheEntry *FindInCache(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();
};

View file

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

View file

@ -22,36 +22,32 @@
namespace sts::ncm {
Result HasFile(bool* out, const char* path) {
errno = 0;
struct stat st;
if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) {
*out = true;
} else {
*out = false;
}
/* It is a valid state for the file to not exist. */
if (errno != 0 && errno != ENOENT && errno != ENOTDIR) {
return fsdevGetLastResult();
R_TRY_CATCH(fsdevGetLastResult()) {
R_CATCH(ResultFsPathNotFound) {
*out = false;
}
} R_END_TRY_CATCH;
}
return ResultSuccess;
}
Result HasDirectory(bool* out, const char* path) {
errno = 0;
struct stat st;
if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
*out = true;
} else {
*out = false;
}
/* It is a valid state for the directory to not exist. */
if (errno != 0 && errno != ENOENT && errno != ENOTDIR) {
return fsdevGetLastResult();
R_TRY_CATCH(fsdevGetLastResult()) {
R_CATCH(ResultFsPathNotFound) {
*out = false;
}
} R_END_TRY_CATCH;
}
return ResultSuccess;
@ -61,8 +57,6 @@ namespace sts::ncm {
char content_root[FS_MAX_PATH] = {0};
char placeholder_root[FS_MAX_PATH] = {0};
errno = 0;
bool has_root = false;
R_TRY(HasDirectory(&has_root, root_path));
if (!has_root) {
@ -118,13 +112,9 @@ namespace sts::ncm {
if (path_len != 0) {
for (size_t i = 0; i < path_len; 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 '/' */
working_path_buf[i + 1] = 0;
mkdir(working_path_buf + 1, S_IRWXU);
if (errno != 0) {
if (mkdir(working_path_buf + 1, S_IRWXU) == -1) {
R_TRY_CATCH(fsdevGetLastResult()) {
R_CATCH(ResultFsPathAlreadyExists) {
/* 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;
errno = 0;
stat(content_path, &st);
if (errno != 0) {
if (stat(content_path, &st) == -1) {
return fsdevGetLastResult();
}
@ -186,23 +183,21 @@ namespace sts::ncm {
path::GetContentMetaPath(content_path, content_id, this->make_content_path_func, this->root_path);
}
errno = 0;
FILE* f = fopen(content_path, "rb");
ON_SCOPE_EXIT {
fclose(f);
};
if (f == NULL || errno != 0) {
if (f == nullptr) {
return fsdevGetLastResult();
}
if (!fseek(f, offset, SEEK_SET)) {
return fsdevGetLastResult();
}
errno = 0;
fseek(f, offset, SEEK_SET);
fread(buf.buffer, buf.num_elements, 1, f);
if (errno != 0) {
return fsdevGetLastResult();
if (fread(buf.buffer, 1, buf.num_elements, f) != buf.num_elements && ferror(f)) {
return fsdevGetLastResult();
}
return ResultSuccess;