Prevent automatic placeholder creation on open

This commit is contained in:
Adubbz 2019-08-06 14:13:15 +10:00
parent f080f6793b
commit 6e5c0bde51
7 changed files with 55 additions and 24 deletions

View file

@ -23,7 +23,7 @@ namespace sts::debug {
} }
size_t g_curr_log_offset = 0; size_t g_curr_log_offset = 0;
size_t g_log_skip = 4; size_t g_log_skip = 1000;
char __attribute__ ((aligned (0x1000))) g_work_page[0x1000]; char __attribute__ ((aligned (0x1000))) g_work_page[0x1000];

View file

@ -100,12 +100,9 @@ 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);
FILE* f = fopen(placeholder_path, "r+b");
if (f == nullptr) { FILE* f = nullptr;
debug::DebugLog("Failed to open placeholder %s\n", placeholder_path); R_TRY(OpenFile(&f, placeholder_path, FS_OPEN_WRITE));
return fsdevGetLastResult();
}
*out_handle = f; *out_handle = f;
return ResultSuccess; return ResultSuccess;

View file

@ -95,6 +95,8 @@ namespace sts::ncm {
Result ContentMetaDatabaseInterface::GetContentIdByTypeImpl(ContentId* out, const ContentMetaKey& key, ContentType type, std::optional<u8> id_offset) { Result ContentMetaDatabaseInterface::GetContentIdByTypeImpl(ContentId* out, const ContentMetaKey& key, ContentType type, std::optional<u8> id_offset) {
R_TRY(this->EnsureEnabled()); R_TRY(this->EnsureEnabled());
D_LOG("key: 0x%lx\n", key.id);
const auto it = this->kvs->lower_bound(key); const auto it = this->kvs->lower_bound(key);
if (it == this->kvs->end() || it->GetKey().id != key.id) { if (it == this->kvs->end() || it->GetKey().id != key.id) {
return ResultNcmContentMetaNotFound; return ResultNcmContentMetaNotFound;
@ -384,6 +386,7 @@ namespace sts::ncm {
out.SetValue(has); out.SetValue(has);
return ResultSuccess; return ResultSuccess;
debug::DebugLog("Has 0x%lx\n", key.id);
R_DEBUG_END R_DEBUG_END
} }

View file

@ -82,15 +82,12 @@ 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);
this->content_cache_file_handle = fopen(content_path, "rb");
if (this->content_cache_file_handle == NULL) { R_TRY_CATCH(OpenFile(&this->content_cache_file_handle, content_path, FS_OPEN_READ)) {
R_TRY_CATCH(fsdevGetLastResult()) { R_CATCH(ResultFsPathNotFound) {
R_CATCH(ResultFsPathNotFound) { return ResultNcmContentNotFound;
return ResultNcmContentNotFound; }
} } R_END_TRY_CATCH;
} R_END_TRY_CATCH;
}
this->cached_content_id = content_id; this->cached_content_id = content_id;
return ResultSuccess; return ResultSuccess;
@ -624,15 +621,13 @@ 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);
FILE* f = fopen(content_path, "r+b"); FILE* f = nullptr;
R_TRY(OpenFile(&f, content_path, FS_OPEN_WRITE));
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
fclose(f); fclose(f);
}; };
if (f == nullptr) {
return fsdevGetLastResult();
}
if (fseek(f, offset, SEEK_SET) != 0) { if (fseek(f, offset, SEEK_SET) != 0) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }

View file

@ -15,12 +15,49 @@
*/ */
#include <map> #include <map>
#include <fcntl.h>
#include <sys/iosupport.h>
#include "ncm_fs.hpp" #include "ncm_fs.hpp"
#include "ncm_path_utils.hpp" #include "ncm_path_utils.hpp"
namespace sts::ncm { namespace sts::ncm {
Result OpenFile(FILE** out, const char* path, u32 mode) {
bool has = false;
/* Manually check if the file already exists, so it doesn't get created automatically. */
R_TRY(HasFile(&has, path));
if (!has) {
return ResultFsPathNotFound;
}
const char* fopen_mode = "";
if (mode & FS_OPEN_APPEND) {
if (mode & FS_OPEN_READ) {
fopen_mode = "r+b";
} else if (mode & FS_OPEN_WRITE) {
fopen_mode = "w+b";
}
} else {
if (mode & FS_OPEN_READ) {
fopen_mode = "rb";
} else if (mode & FS_OPEN_WRITE) {
fopen_mode = "wb";
}
}
FILE* f = fopen(path, fopen_mode);
if (f == nullptr) {
return fsdevGetLastResult();
}
*out = f;
return ResultSuccess;
}
Result HasFile(bool* out, const char* path) { Result HasFile(bool* out, const char* path) {
struct stat st; struct stat st;

View file

@ -23,6 +23,7 @@
namespace sts::ncm { namespace sts::ncm {
Result OpenFile(FILE** out, const char* path, u32 mode);
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);

View file

@ -224,15 +224,13 @@ 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);
} }
FILE* f = fopen(content_path, "rb"); FILE* f = nullptr;
R_TRY(OpenFile(&f, content_path, FS_OPEN_READ));
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
fclose(f); fclose(f);
}; };
if (f == nullptr) {
return fsdevGetLastResult();
}
if (fseek(f, offset, SEEK_SET) != 0) { if (fseek(f, offset, SEEK_SET) != 0) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }