From 213ca72fa172ef2b46addc923eea911d89a63272 Mon Sep 17 00:00:00 2001 From: Stephen Miller <56742918+StevenMiller123@users.noreply.github.com> Date: Sun, 15 Jun 2025 14:43:39 -0500 Subject: [PATCH] Filesystem: Fixes for posix_rename and write (#3099) * Fix rename We shouldn't be leaving a copy of the original filename laying around. This fixes one of a few broken savedata checks in DRAGON BALL XENOVERSE (CUSA01341) * sceKernelWrite hack Seems like std::fwrite has some weird edge cases we aren't handling properly. Until we get to the bottom of this issue, here's a hack that bypasses it. This fixes saves in DRAGON BALL XENOVERSE (CUSA01341) * hack fix * Improved "hack" * Fix rename for Windows users Turns out, we're using copy instead of rename for a reason, and that same reason came up when adding the remove call. Also adds a log for the sceKernelWrite issue, since that's definitely a hack that needs to be debugged. * A real fix for the sceKernelWrite issue Turns out, some data was just buffered. Running Flush fixes that problem. * Move fflush call to WriteRaw To prevent future cases of this issue. --- src/common/io_file.h | 4 +++- src/core/libraries/kernel/file_system.cpp | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/common/io_file.h b/src/common/io_file.h index 45787a092..cb01e154a 100644 --- a/src/common/io_file.h +++ b/src/common/io_file.h @@ -186,7 +186,9 @@ public: template size_t WriteRaw(const void* data, size_t size) const { - return std::fwrite(data, sizeof(T), size, file); + auto bytes = std::fwrite(data, sizeof(T), size, file); + std::fflush(file); + return bytes; } template diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index fecc606fd..76d1a3339 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -293,6 +293,7 @@ s64 PS4_SYSV_ABI write(s32 fd, const void* buf, size_t nbytes) { } return result; } + return file->f.WriteRaw(buf, nbytes); } @@ -750,7 +751,24 @@ s32 PS4_SYSV_ABI posix_rename(const char* from, const char* to) { *__Error() = POSIX_ENOTEMPTY; return -1; } + + // On Windows, std::filesystem::rename will error if the file has been opened before. std::filesystem::copy(src_path, dst_path, std::filesystem::copy_options::overwrite_existing); + auto* h = Common::Singleton::Instance(); + auto file = h->GetFile(src_path); + if (file) { + // We need to force ReadWrite if the file had Write access before + // Otherwise f.Open will clear the file contents. + auto access_mode = file->f.GetAccessMode() == Common::FS::FileAccessMode::Write + ? Common::FS::FileAccessMode::ReadWrite + : file->f.GetAccessMode(); + file->f.Close(); + std::filesystem::remove(src_path); + file->f.Open(dst_path, access_mode); + } else { + std::filesystem::remove(src_path); + } + return ORBIS_OK; }