fix(libc): replace an open or read-only destination in rename on Windows #4532

Merged
DavidSamir merged 1 commit from upstream-pr/4532 into main 2026-10-13 11:38:14 +00:00
DavidSamir commented 2026-10-12 14:49:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-12T14:49:19Z.


What

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 main 0428328b7, 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 #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, 69b9d186d on main 0428328b7, 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 main 0428328b7 and merges onto 495bf91a0 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
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4831072809 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4532). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-12T14:49:19Z. --- ### What 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)](https://man.freebsd.org/cgi/man.cgi?query=rename&sektion=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 `main` 0428328b7, 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 #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, 69b9d186d on `main` 0428328b7, 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 `main` 0428328b7 and merges onto 495bf91a0 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes
xXPBSXx commented 2026-10-12 16:31:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T16:31:26Z.


Windows test: head 4f29a2006 merged onto current main d592d54e1 (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.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6128390224 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4532#issuecomment-6128390224). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T16:31:26Z. --- Windows test: head `4f29a2006` merged onto current `main` `d592d54e1` (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. AI-assisted (Claude Code).
oneandonlydean commented 2026-10-13 00:21:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T00:21:04Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135184553 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4532#issuecomment-6135184553). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T00:21:04Z. --- `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.
DavidSamir commented 2026-10-13 02:29:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T02:29:19Z.


Done in 69b9d186d (force-pushed, rebased onto main 0428328b7).

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 69b9d186d, 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.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6136887183 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4532#issuecomment-6136887183). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T02:29:19Z. --- Done in 69b9d186d (force-pushed, rebased onto `main` 0428328b7). `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 69b9d186d, 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. AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4532
No description provided.