Truncate before open

Open the file as read-write, then try truncating.
This fixes read | truncate flag behavior on Windows.
This commit is contained in:
Stephen Miller 2025-04-30 23:11:04 -05:00
parent 218f151aa5
commit 297ac4c896

View File

@ -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) {