Save data: fix nullptr & concurrent file write (#1049)

* Save data: fix nullptr & concurrency file write

* Save data memory: fix overriding icon
This commit is contained in:
Vinicius Rangel 2024-09-24 00:50:18 -03:00 committed by GitHub
parent f97f73f0b5
commit 1620eea37b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 11 deletions

View file

@ -102,7 +102,12 @@ bool PSF::Encode(const std::filesystem::path& filepath) const {
last_write = std::filesystem::file_time_type::clock::now(); last_write = std::filesystem::file_time_type::clock::now();
const auto psf_buffer = Encode(); 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<u8> PSF::Encode() const { std::vector<u8> PSF::Encode() const {

View file

@ -119,7 +119,7 @@ static void BackupThreadBody() {
std::scoped_lock lk{g_backup_queue_mutex}; std::scoped_lock lk{g_backup_queue_mutex};
g_backup_queue.front().done = true; 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}; std::scoped_lock lk{g_backup_queue_mutex};
g_backup_queue.pop_front(); g_backup_queue.pop_front();

View file

@ -190,14 +190,19 @@ void SetIcon(void* buf, size_t buf_size) {
if (buf == nullptr) { if (buf == nullptr) {
const auto& src_icon = g_mnt->GetHostPath("/app0/sce_sys/save_data.png"); const auto& src_icon = g_mnt->GetHostPath("/app0/sce_sys/save_data.png");
if (fs::exists(src_icon)) { if (fs::exists(src_icon)) {
if (fs::exists(g_icon_path)) {
fs::remove(g_icon_path);
}
fs::copy_file(src_icon, g_icon_path); fs::copy_file(src_icon, g_icon_path);
} }
IOFile file(g_icon_path, Common::FS::FileAccessMode::Read); if (fs::exists(g_icon_path)) {
size_t size = file.GetSize(); IOFile file(g_icon_path, Common::FS::FileAccessMode::Read);
file.Seek(0); size_t size = file.GetSize();
g_icon_memory.resize(size); file.Seek(0);
file.ReadRaw<u8>(g_icon_memory.data(), size); g_icon_memory.resize(size);
file.Close(); file.ReadRaw<u8>(g_icon_memory.data(), size);
file.Close();
}
} else { } else {
g_icon_memory.resize(buf_size); g_icon_memory.resize(buf_size);
std::memcpy(g_icon_memory.data(), buf, buf_size); std::memcpy(g_icon_memory.data(), buf, buf_size);

View file

@ -485,14 +485,14 @@ static Error Umount(const OrbisSaveDataMountPoint* mountPoint, bool call_backup
if (instance.has_value()) { if (instance.has_value()) {
const auto& slot_name = instance->GetMountPoint(); const auto& slot_name = instance->GetMountPoint();
if (slot_name == mount_point_str) { if (slot_name == mount_point_str) {
// TODO: check if is busy
instance->Umount();
if (call_backup) { if (call_backup) {
Backup::StartThread(); Backup::StartThread();
Backup::NewRequest(instance->GetUserId(), instance->GetTitleId(), Backup::NewRequest(instance->GetUserId(), instance->GetTitleId(),
instance->GetDirName(), instance->GetDirName(),
OrbisSaveDataEventType::UMOUNT_BACKUP); OrbisSaveDataEventType::UMOUNT_BACKUP);
} }
// TODO: check if is busy
instance->Umount();
instance.reset(); instance.reset();
return Error::OK; return Error::OK;
} }
@ -581,10 +581,10 @@ Error PS4_SYSV_ABI sceSaveDataCheckBackupData(const OrbisSaveDataCheckBackupData
LOG_INFO(Lib_SaveData, "called with invalid parameter"); LOG_INFO(Lib_SaveData, "called with invalid parameter");
return Error::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} const std::string_view title{check->titleId != nullptr ? std::string_view{check->titleId->data}
: std::string_view{g_game_serial}}; : std::string_view{g_game_serial}};
LOG_DEBUG(Lib_SaveData, "called with titleId={}", title);
const auto save_path = const auto save_path =
SaveInstance::MakeDirSavePath(check->userId, title, check->dirName->data); SaveInstance::MakeDirSavePath(check->userId, title, check->dirName->data);