You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(libc): replace an open or read-only destination in rename on Windows - #4532
On Windows, rename and sceKernelRename fail when the destination is a file that is open or has the read-only attribute. FreeBSD's rename(2) removes an existing destination first and has no error for either case, and the Linux build already replaces both. Writing a new file and renaming it over the current one is the usual way to save, so a title that still holds the old save or config file open cannot replace it.
Since #3677 and #4090 a file the guest has open can be renamed and removed, but it still cannot be renamed onto: MoveFileExW refuses with access denied.
Observed on main0428328, Windows 11 (10.0.26300), with to opened by sceKernelOpen:
call
main
FreeBSD, Linux build
rename(from, to), to open
-1, errno 13
0
sceKernelRename(from, to), to open
0x80020005
0
rename(from, to), to read-only
-1, errno 13
0
sceKernelRename(from, to), to read-only
0x80020005
0
Both functions now end in one RenameNative_nid_no_patch in Rename.cpp, and the caller tells it which of two host calls to make before it makes one:
a file onto an existing file: SetFileInformationByHandle with FileRenameInfoEx, replace-if-exists and POSIX semantics, the call fix(cache): replace entries while Windows readers retain old files #3660 uses for the shader cache, with ignore-read-only added when the destination has that attribute;
everything else (a new name, a directory): std::filesystem::rename, as today.
Whichever call is made, its error is the result. Nothing is tried a second time. rename already has both paths' status from its own checks; sceKernelRename reads the destination's status once on Windows to choose.
After a replace, a descriptor that was open on the old destination keeps reading and writing the old file, and the name holds the new one.
Not changed: the directory cases (renaming a directory onto an open empty directory fails before and after). A file that another program holds without delete sharing still cannot be replaced or moved; for rename the error for it is now EBUSY (16), from the sharing violation, where main gives EACCES or EIO.
Tested
Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7, Release, 69b9d18 on main0428328, full builds.
guest_filesystem gains checks, for rename and for sceKernelRename: replace a destination that is open, read the old contents through its descriptor, and replace a destination made read-only with sceKernelChmod(0400). With the three source files as on main and the checks kept it fails at the open-destination replace (line 339 (guest errno 13, Win32 error 5)). With the fix it passes 5/5 (--repeat until-fail:5), and guest_kernel_rename, file_streams, guest_kernel_sync, guest_posix_filesystem, guest_standard_streams and guest_dup pass 3/3.
A standalone probe ran both functions over 100 source and destination combinations each (missing, file, read-only, hidden, empty and non-empty directory, file and directory open by the guest, file open by another handle with and without delete sharing) and compared result, errno and the state of both paths with main's libraries. 52 of the 200 change. 30 go from failure to success, all a file source onto a read-only or open destination. 22 fail in both with the same state left behind and only the error number differs: the 11 combinations with a file another handle holds without delete sharing, in each of the two functions (rename reports 16 where it reported 13 or 5; sceKernelRename returns 0x80020005 in both). The other 148, and a same-name and a case-only rename, are identical.
Same probe: 10000 replacements of a file the guest has open, no failure, process handle count unchanged (113 before and after). Eight threads doing 2000 write-new, replace-open, write-old, close cycles each: no failure and no file left behind; main fails all of them.
Timing, three alternating runs of main's libraries and the branch's: 20000 renames to a free name 9.2 to 16.1 s against 9.3 to 10.0 s; 10000 replacements of a closed file 5.8 to 8.3 s against 5.9 to 6.2 s. No consistent difference on this machine.
Titles from the homebrew catalog, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), PSXS5 (PPSA97510), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000) and OpenNOW (PPSA99082) end at the same place in both, with the same number of stderr lines.
Full ctest --parallel 4: 1001 of 1001 pass (7 skipped, no display).
tools/check_conventions.py --base origin/main passes. The branch is on main0428328 and merges onto 495bf91 without conflict; I have not rebuilt there.
Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): guest_filesystem and guest_kernel_rename pass 3/3, so the new checks match the existing Linux behaviour. Only those targets and libs were built there.
Not tested: a console, macOS, a Windows version older than this one (a tester ran the first version of this PR on Windows 10), and a file system other than NTFS. I have not found a title that stops on this; it was found by probing the file functions.
Checklist
Based on current main; no other open PR implements the same functions
Windows test: head 4f29a2006 merged onto current maind592d54e1 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter guest_filesystem: guest_filesystem passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.
RenameNative_nid_no_patch tries fs::rename and, after access denied, renames again through SetFileInformationByHandle (Rename.cpp:19-41). Retrying after an error isn't allowed here; please choose the path before the call (on Windows, FileRenameInfoEx for a file onto an existing file) and let its error stand.
Done in 69b9d18 (force-pushed, rebased onto main0428328).
RenameNative_nid_no_patch no longer calls fs::rename first. The caller says before the call which path to take: a file onto an existing file goes to SetFileInformationByHandle with FileRenameInfoEx, everything else to fs::rename. One call is made and its error is returned; the errno save and restore that went with the second attempt is gone too. rename already had both statuses from its own checks, and sceKernelRename reads the destination's status once on Windows to choose, so a rename to a free name makes the same host calls as on main.
One visible difference from the first version: when another program holds the source or the destination without delete sharing, rename now fails with 16 (EBUSY, the sharing violation of the call that was made) where main reports 13 or 5. The rename fails in both and both paths are left as they were.
Rerun on Windows with 69b9d18, full builds; the description has the details:
guest_filesystem fails with the three source files as on main (line 339 (guest errno 13, Win32 error 5)) and passes 5/5 with the change; six neighbouring tests pass 3/3.
The 200-combination probe against main's libraries: 30 failures become successes (a file onto an open or read-only file), 22 failures keep failing with a different error number (the foreign-handle case above), 148 are identical.
10000 replacements of an open file with no failure and no handle left; eight threads, 16000 cycles, no failure.
Timing against main's libraries: renames to a free name and replacements of a closed file show no consistent difference.
Eleven catalog titles end at the same place with both sets of libraries. Full ctest: 1001 of 1001.
Linux (WSL2, GCC 13.3.0): guest_filesystem and guest_kernel_rename pass 3/3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
On Windows,
renameandsceKernelRenamefail when the destination is a file that is open or has the read-only attribute. FreeBSD's rename(2) removes an existing destination first and has no error for either case, and the Linux build already replaces both. Writing a new file and renaming it over the current one is the usual way to save, so a title that still holds the old save or config file open cannot replace it.Since #3677 and #4090 a file the guest has open can be renamed and removed, but it still cannot be renamed onto:
MoveFileExWrefuses with access denied.Observed on
main0428328, Windows 11 (10.0.26300), withtoopened bysceKernelOpen:mainrename(from, to),toopensceKernelRename(from, to),toopen0x80020005rename(from, to),toread-onlysceKernelRename(from, to),toread-only0x80020005Both functions now end in one
RenameNative_nid_no_patchinRename.cpp, and the caller tells it which of two host calls to make before it makes one:SetFileInformationByHandlewithFileRenameInfoEx, replace-if-exists and POSIX semantics, the call fix(cache): replace entries while Windows readers retain old files #3660 uses for the shader cache, with ignore-read-only added when the destination has that attribute;std::filesystem::rename, as today.Whichever call is made, its error is the result. Nothing is tried a second time.
renamealready has both paths' status from its own checks;sceKernelRenamereads the destination's status once on Windows to choose.After a replace, a descriptor that was open on the old destination keeps reading and writing the old file, and the name holds the new one.
Not changed: the directory cases (renaming a directory onto an open empty directory fails before and after). A file that another program holds without delete sharing still cannot be replaced or moved; for
renamethe error for it is nowEBUSY(16), from the sharing violation, wheremaingivesEACCESorEIO.Tested
Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7, Release, 69b9d18 on
main0428328, full builds.guest_filesystemgains checks, forrenameand forsceKernelRename: replace a destination that is open, read the old contents through its descriptor, and replace a destination made read-only withsceKernelChmod(0400). With the three source files as onmainand the checks kept it fails at the open-destination replace (line 339 (guest errno 13, Win32 error 5)). With the fix it passes 5/5 (--repeat until-fail:5), andguest_kernel_rename,file_streams,guest_kernel_sync,guest_posix_filesystem,guest_standard_streamsandguest_duppass 3/3.main's libraries. 52 of the 200 change. 30 go from failure to success, all a file source onto a read-only or open destination. 22 fail in both with the same state left behind and only the error number differs: the 11 combinations with a file another handle holds without delete sharing, in each of the two functions (renamereports 16 where it reported 13 or 5;sceKernelRenamereturns0x80020005in both). The other 148, and a same-name and a case-only rename, are identical.mainfails all of them.main's libraries and the branch's: 20000 renames to a free name 9.2 to 16.1 s against 9.3 to 10.0 s; 10000 replacements of a closed file 5.8 to 8.3 s against 5.9 to 6.2 s. No consistent difference on this machine.ctest --parallel 4: 1001 of 1001 pass (7 skipped, no display).tools/check_conventions.py --base origin/mainpasses. The branch is onmain0428328 and merges onto 495bf91 without conflict; I have not rebuilt there.Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
guest_filesystemandguest_kernel_renamepass 3/3, so the new checks match the existing Linux behaviour. Only those targets andlibswere built there.Not tested: a console, macOS, a Windows version older than this one (a tester ran the first version of this PR on Windows 10), and a file system other than NTFS. I have not found a title that stops on this; it was found by probing the file functions.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)