From 4c881f6e4018996039b15417b9bf641b338091bd Mon Sep 17 00:00:00 2001 From: Stephen Miller Date: Sun, 15 Jun 2025 12:59:19 -0500 Subject: [PATCH] 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. --- src/core/libraries/kernel/file_system.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index 39f9f82b7..5e6fd0852 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -301,6 +301,9 @@ s64 PS4_SYSV_ABI write(s32 fd, const void* buf, size_t nbytes) { expected_file_size += bytes_written; auto actual_file_size = file->f.GetSize(); if (expected_file_size != actual_file_size) { + LOG_WARNING(Kernel_Fs, + "Unexpected behavior from fwrite. Expected size {:#x}, actual size {:#x}", + expected_file_size, actual_file_size); file->f.SetSize(expected_file_size); } return bytes_written; @@ -760,8 +763,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); - std::filesystem::remove(src_path); + 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; }