From 4141f761050fc7289f0f4aaed09f4cd666a325de Mon Sep 17 00:00:00 2001 From: Elad <18193363+elad335@users.noreply.github.com> Date: Fri, 3 Oct 2025 14:04:04 +0300 Subject: [PATCH] Win32/vm_native.cpp: Improve sparse memory file mapping robustness --- rpcs3/util/vm_native.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/rpcs3/util/vm_native.cpp b/rpcs3/util/vm_native.cpp index 156af86548..ed1e0060c9 100644 --- a/rpcs3/util/vm_native.cpp +++ b/rpcs3/util/vm_native.cpp @@ -536,21 +536,33 @@ namespace utils { f.close(); - for (u32 try_count = 3, i = 0; !f && i < try_count; i++) + for (u32 i = 1; i <= 3; i++) + { + // Cleanup + fs::remove_file(fmt::format("%s.%d.tmp", path, i)); + } + + for (u32 try_count = 3, i = 0; !f && i < try_count; i++, ::Sleep(100)) { // Bug workaround: removing old file may be safer than rewriting it if (!fs::remove_file(path) && fs::g_tls_error != fs::error::noent) { - return false; + // Try MoveFile + fs::rename(path, fmt::format("%s.%d.tmp", path, i + 1), true); } - if (!f.open(path, fs::read + fs::write + fs::create + fs::excl) && fs::g_tls_error != fs::error::exist) + if (f.open(path, fs::read + fs::write + fs::create + fs::excl)) + { + return true; + } + + if (fs::g_tls_error != fs::error::exist && fs::g_tls_error != fs::error::acces) { return false; } } - return f.operator bool(); + return false; }; std::string storage1 = fs::get_temp_dir();