fix(libc): replace an open or read-only destination in rename on Windows #4532
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4532"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-12T14:49:19Z.
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
main0428328b7, 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 #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,
69b9d186donmain0428328b7, 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 onmain0428328b7and merges onto495bf91a0without 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)Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T16:31:26Z.
Windows test: head
4f29a2006merged onto currentmaind592d54e1(clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release,-j2: full build 0 errors; ctest with the filterguest_filesystem: guest_filesystem passed (1 test(s), 0 failed);python tools/check_conventions.py --base upstream/mainpassed. I did not run the new tests againstmainwithout the change.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T00:21:04Z.
RenameNative_nid_no_patchtriesfs::renameand, after access denied, renames again throughSetFileInformationByHandle(Rename.cpp:19-41). Retrying after an error isn't allowed here; please choose the path before the call (on Windows,FileRenameInfoExfor a file onto an existing file) and let its error stand.Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T02:29:19Z.
Done in
69b9d186d(force-pushed, rebased ontomain0428328b7).RenameNative_nid_no_patchno longer callsfs::renamefirst. The caller says before the call which path to take: a file onto an existing file goes toSetFileInformationByHandlewithFileRenameInfoEx, everything else tofs::rename. One call is made and its error is returned; theerrnosave and restore that went with the second attempt is gone too.renamealready had both statuses from its own checks, andsceKernelRenamereads the destination's status once on Windows to choose, so a rename to a free name makes the same host calls as onmain.One visible difference from the first version: when another program holds the source or the destination without delete sharing,
renamenow fails with 16 (EBUSY, the sharing violation of the call that was made) wheremainreports 13 or 5. The rename fails in both and both paths are left as they were.Rerun on Windows with
69b9d186d, full builds; the description has the details:guest_filesystemfails with the three source files as onmain(line 339 (guest errno 13, Win32 error 5)) and passes 5/5 with the change; six neighbouring tests pass 3/3.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.main's libraries: renames to a free name and replacements of a closed file show no consistent difference.ctest: 1001 of 1001.guest_filesystemandguest_kernel_renamepass 3/3.AI-assisted: yes