From 1620eea37b53ce340ac64065bbfe6744d554c80f Mon Sep 17 00:00:00 2001 From: Vinicius Rangel Date: Tue, 24 Sep 2024 00:50:18 -0300 Subject: [PATCH] Save data: fix nullptr & concurrent file write (#1049) * Save data: fix nullptr & concurrency file write * Save data memory: fix overriding icon --- src/core/file_format/psf.cpp | 7 ++++++- src/core/libraries/save_data/save_backup.cpp | 2 +- src/core/libraries/save_data/save_memory.cpp | 17 +++++++++++------ src/core/libraries/save_data/savedata.cpp | 6 +++--- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/core/file_format/psf.cpp b/src/core/file_format/psf.cpp index 1df5d430..a5e502f9 100644 --- a/src/core/file_format/psf.cpp +++ b/src/core/file_format/psf.cpp @@ -102,7 +102,12 @@ bool PSF::Encode(const std::filesystem::path& filepath) const { last_write = std::filesystem::file_time_type::clock::now(); const auto psf_buffer = Encode(); - return file.Write(psf_buffer) == psf_buffer.size(); + const size_t written = file.Write(psf_buffer); + if (written != psf_buffer.size()) { + LOG_ERROR(Core, "Failed to write PSF file. Written {} Expected {}", written, + psf_buffer.size()); + } + return written == psf_buffer.size(); } std::vector PSF::Encode() const { diff --git a/src/core/libraries/save_data/save_backup.cpp b/src/core/libraries/save_data/save_backup.cpp index 8f7e0d69..d8281fab 100644 --- a/src/core/libraries/save_data/save_backup.cpp +++ b/src/core/libraries/save_data/save_backup.cpp @@ -119,7 +119,7 @@ static void BackupThreadBody() { std::scoped_lock lk{g_backup_queue_mutex}; g_backup_queue.front().done = true; } - std::this_thread::sleep_for(std::chrono::seconds(10)); // Don't backup too often + std::this_thread::sleep_for(std::chrono::seconds(5)); // Don't backup too often { std::scoped_lock lk{g_backup_queue_mutex}; g_backup_queue.pop_front(); diff --git a/src/core/libraries/save_data/save_memory.cpp b/src/core/libraries/save_data/save_memory.cpp index 6a685ee3..44f5311e 100644 --- a/src/core/libraries/save_data/save_memory.cpp +++ b/src/core/libraries/save_data/save_memory.cpp @@ -190,14 +190,19 @@ void SetIcon(void* buf, size_t buf_size) { if (buf == nullptr) { const auto& src_icon = g_mnt->GetHostPath("/app0/sce_sys/save_data.png"); if (fs::exists(src_icon)) { + if (fs::exists(g_icon_path)) { + fs::remove(g_icon_path); + } fs::copy_file(src_icon, g_icon_path); } - IOFile file(g_icon_path, Common::FS::FileAccessMode::Read); - size_t size = file.GetSize(); - file.Seek(0); - g_icon_memory.resize(size); - file.ReadRaw(g_icon_memory.data(), size); - file.Close(); + if (fs::exists(g_icon_path)) { + IOFile file(g_icon_path, Common::FS::FileAccessMode::Read); + size_t size = file.GetSize(); + file.Seek(0); + g_icon_memory.resize(size); + file.ReadRaw(g_icon_memory.data(), size); + file.Close(); + } } else { g_icon_memory.resize(buf_size); std::memcpy(g_icon_memory.data(), buf, buf_size); diff --git a/src/core/libraries/save_data/savedata.cpp b/src/core/libraries/save_data/savedata.cpp index d62c1b9a..9599c1ff 100644 --- a/src/core/libraries/save_data/savedata.cpp +++ b/src/core/libraries/save_data/savedata.cpp @@ -485,14 +485,14 @@ static Error Umount(const OrbisSaveDataMountPoint* mountPoint, bool call_backup if (instance.has_value()) { const auto& slot_name = instance->GetMountPoint(); if (slot_name == mount_point_str) { + // TODO: check if is busy + instance->Umount(); if (call_backup) { Backup::StartThread(); Backup::NewRequest(instance->GetUserId(), instance->GetTitleId(), instance->GetDirName(), OrbisSaveDataEventType::UMOUNT_BACKUP); } - // TODO: check if is busy - instance->Umount(); instance.reset(); return Error::OK; } @@ -581,10 +581,10 @@ Error PS4_SYSV_ABI sceSaveDataCheckBackupData(const OrbisSaveDataCheckBackupData LOG_INFO(Lib_SaveData, "called with invalid parameter"); return Error::PARAMETER; } - LOG_DEBUG(Lib_SaveData, "called with titleId={}", check->titleId->data.to_view()); const std::string_view title{check->titleId != nullptr ? std::string_view{check->titleId->data} : std::string_view{g_game_serial}}; + LOG_DEBUG(Lib_SaveData, "called with titleId={}", title); const auto save_path = SaveInstance::MakeDirSavePath(check->userId, title, check->dirName->data);