From 297ac4c8967e8a0961e26b1a60597e72677600e7 Mon Sep 17 00:00:00 2001 From: Stephen Miller Date: Wed, 30 Apr 2025 23:11:04 -0500 Subject: [PATCH] Truncate before open Open the file as read-write, then try truncating. This fixes read | truncate flag behavior on Windows. --- src/core/libraries/kernel/file_system.cpp | 24 +++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index 7cfc5ca19..8d1829781 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -177,6 +177,20 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { return -1; } } else { + // Start by opening as read-write so we can truncate on all platforms. + // Since open starts by closing the file, this won't interfere with later open calls. + e = file->f.Open(file->m_host_name, Common::FS::FileAccessMode::ReadWrite); + + if (truncate && read_only) { + // Can't open files with truncate flag in a read only directory + h->DeleteHandle(handle); + *__Error() = POSIX_EROFS; + return -1; + } else if (truncate && e == 0) { + // If the file was opened successfully and truncate was enabled, reduce size to 0 + file->f.SetSize(file->m_host_name.string().c_str(), 0); + } + if (read) { // Read only e = file->f.Open(file->m_host_name, Common::FS::FileAccessMode::Read); @@ -200,16 +214,6 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { *__Error() = POSIX_EINVAL; return -1; } - - if (truncate && read_only) { - // Can't open files with truncate flag in a read only directory - h->DeleteHandle(handle); - *__Error() = POSIX_EROFS; - return -1; - } else if (truncate && e == 0) { - // If the file was opened successfully and truncate was enabled, reduce size to 0 - file->f.SetSize(file->m_host_name.string().c_str(), 0); - } } if (e != 0) {