libkernel: a guest cannot unlink or rename a file it has open on Windows; fix branch ready #3316

Closed
opened 2026-10-11 07:14:38 +00:00 by DavidSamir · 0 comments
DavidSamir commented 2026-10-11 07:14:38 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-11T07:14:38Z.


On Windows, a guest cannot unlink or rename a file while it holds a descriptor on it. FreeBSD allows both: unlink(2) says "If one or more process have the file open when the last link is removed, the link is removed, but the removal of the file is delayed until all references to it have been closed", and rename(2) lists no error for an open file. Linux builds already behave that way.

Cause. sceKernelOpen opens through _wopen (File/src/Open.cpp), which shares the file for reading and writing but not for deletion, so DeleteFileW and MoveFileExW fail with a sharing violation for as long as the descriptor is open.

Observed on main 1690db9a2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, with a file the guest opened with sceKernelOpen:

call while the descriptor is open Windows FreeBSD, Linux build
sceKernelUnlink(file) 0x8002000D (EACCES), file stays 0
libc remove(file) -1 0
sceKernelRename(file, other) 0x80020005 (EIO) 0

Removing a temporary file right after opening it, and renaming a file that is still open, are common ways to write saves and caches. I have not found a title that stops on this; it was found by probing the file functions, not from a title.

Fix, ready to cherry-pick. DavidSamir:fix/libkernel-open-delete-sharing-windows (2f5ada5c, one commit on main 1690db9a2, two files, +63/-1). I can't open it as a PR because of the intake check.

  • Windows NativeOpen calls CreateFileW with FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE and wraps the handle with _open_osfhandle. Access, creation disposition, the read-only attribute for a created file without the owner write bit, handle inheritance and the errno for each Windows error follow what _wopen does.
  • With delete sharing, DeleteFileW on this Windows build removes the name at once: the path no longer exists, the open descriptor still reads and writes, and the name can be created again before the old descriptor is closed.
  • Not changed: renaming onto a file that is open still fails (MoveFileExW returns access denied; it would need a POSIX-semantics rename), and files the built-in libc opens through _wfopen are not covered.
  • guest_filesystem gains checks: unlink an open file, read through the descriptor, create the name again, rename that file while it is open and write through the descriptor, and remove a file that is open.

Checked on Windows (2f5ada5c on main 1690db9a2, WinLibs GCC 15.2.0 r7, Release):

  • With the fix reverted and the new checks kept, guest_filesystem fails at the unlink of the open file (guest errno 13). With the fix it passes 5/5 (--repeat until-fail:5).
  • A standalone probe compared the new NativeOpen with _wopen over 3276 combinations of access mode, O_CREAT/O_EXCL/O_TRUNC/O_APPEND, creation mode and path (existing, read-only, hidden, missing, directory, file used as a directory, invalid names, device names, UNC, 300 characters): same result, errno, file size, attributes, readability, writability, initial position and append behaviour in every case.
  • 50000 open and close cycles take the same time as with _wopen (2.25 s against 2.27 s). 80000 opens, failed opens included, leave the process handle count unchanged. Eight threads doing 3000 create, write, unlink while open, write, close cycles each: no failure and no file left behind.
  • Titles from the homebrew catalog, run for 25 s with the libraries from main 0e90079f and from the branch on that base (648fd1c8, the same change before the rebase): DOOM (PPSA99666), OpenRCT2 (PPSA99728), ScreenTester5 (PPSA69420) and Prospero Vibrate (PPSA99920) keep running with the same log line counts and the same number of sceKernelOpen calls; Yamagi Quake II (PPSA99007) exits with code 0 after 5 s in both; Middle Ages Ping Pong (PPSA28319, Unity) stops at the same multisampling check in both.
  • Full ctest --parallel 4 passes 770/770 (5 skipped, no display in the session). check_conventions.py --base origin/main passes; pr_overlap.py --branch reports no overlap with open pull requests (run on the previous base, 0e90079f).

Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): guest_filesystem passes 3/3, so the new checks match the existing Linux behaviour. Only that test was built and run there.

Not checked on a console, and not on a Windows version older than this one; whether DeleteFileW removes the name immediately there is not known.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5798902681 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3316). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-11T07:14:38Z. --- On Windows, a guest cannot unlink or rename a file while it holds a descriptor on it. FreeBSD allows both: [unlink(2)](https://man.freebsd.org/cgi/man.cgi?query=unlink&sektion=2) says "If one or more process have the file open when the last link is removed, the link is removed, but the removal of the file is delayed until all references to it have been closed", and [rename(2)](https://man.freebsd.org/cgi/man.cgi?query=rename&sektion=2) lists no error for an open file. Linux builds already behave that way. **Cause.** `sceKernelOpen` opens through `_wopen` ([`File/src/Open.cpp`](https://github.com/boykopovar/AnyPS5/blob/1690db9a2/core/libs/prx/libkernel/File/src/Open.cpp#L23-L25)), which shares the file for reading and writing but not for deletion, so `DeleteFileW` and `MoveFileExW` fail with a sharing violation for as long as the descriptor is open. Observed on `main` 1690db9a2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, with a file the guest opened with `sceKernelOpen`: | call while the descriptor is open | Windows | FreeBSD, Linux build | |---|---|---| | `sceKernelUnlink(file)` | `0x8002000D` (EACCES), file stays | 0 | | libc `remove(file)` | -1 | 0 | | `sceKernelRename(file, other)` | `0x80020005` (EIO) | 0 | Removing a temporary file right after opening it, and renaming a file that is still open, are common ways to write saves and caches. I have not found a title that stops on this; it was found by probing the file functions, not from a title. **Fix, ready to cherry-pick.** [`DavidSamir:fix/libkernel-open-delete-sharing-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libkernel-open-delete-sharing-windows) (2f5ada5c, one commit on `main` 1690db9a2, two files, +63/-1). I can't open it as a PR because of the intake check. - Windows `NativeOpen` calls `CreateFileW` with `FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE` and wraps the handle with `_open_osfhandle`. Access, creation disposition, the read-only attribute for a created file without the owner write bit, handle inheritance and the errno for each Windows error follow what `_wopen` does. - With delete sharing, `DeleteFileW` on this Windows build removes the name at once: the path no longer exists, the open descriptor still reads and writes, and the name can be created again before the old descriptor is closed. - Not changed: renaming **onto** a file that is open still fails (`MoveFileExW` returns access denied; it would need a POSIX-semantics rename), and files the built-in libc opens through `_wfopen` are not covered. - `guest_filesystem` gains checks: unlink an open file, read through the descriptor, create the name again, rename that file while it is open and write through the descriptor, and `remove` a file that is open. **Checked on Windows** (2f5ada5c on `main` 1690db9a2, WinLibs GCC 15.2.0 r7, Release): - With the fix reverted and the new checks kept, `guest_filesystem` fails at the unlink of the open file (guest errno 13). With the fix it passes 5/5 (`--repeat until-fail:5`). - A standalone probe compared the new `NativeOpen` with `_wopen` over 3276 combinations of access mode, `O_CREAT`/`O_EXCL`/`O_TRUNC`/`O_APPEND`, creation mode and path (existing, read-only, hidden, missing, directory, file used as a directory, invalid names, device names, UNC, 300 characters): same result, errno, file size, attributes, readability, writability, initial position and append behaviour in every case. - 50000 open and close cycles take the same time as with `_wopen` (2.25 s against 2.27 s). 80000 opens, failed opens included, leave the process handle count unchanged. Eight threads doing 3000 create, write, unlink while open, write, close cycles each: no failure and no file left behind. - Titles from the homebrew catalog, run for 25 s with the libraries from `main` 0e90079f and from the branch on that base (648fd1c8, the same change before the rebase): DOOM (PPSA99666), OpenRCT2 (PPSA99728), ScreenTester5 (PPSA69420) and Prospero Vibrate (PPSA99920) keep running with the same log line counts and the same number of `sceKernelOpen` calls; Yamagi Quake II (PPSA99007) exits with code 0 after 5 s in both; Middle Ages Ping Pong (PPSA28319, Unity) stops at the same multisampling check in both. - Full `ctest --parallel 4` passes 770/770 (5 skipped, no display in the session). `check_conventions.py --base origin/main` passes; `pr_overlap.py --branch` reports no overlap with open pull requests (run on the previous base, 0e90079f). **Checked on Linux** (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): `guest_filesystem` passes 3/3, so the new checks match the existing Linux behaviour. Only that test was built and run there. Not checked on a console, and not on a Windows version older than this one; whether `DeleteFileW` removes the name immediately there is not known. AI-assisted: yes
Sign in to join this conversation.
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#3316
No description provided.