libkernel: a guest cannot unlink or rename a file it has open on Windows; fix branch ready #3316
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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-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.
sceKernelOpenopens through_wopen(File/src/Open.cpp), which shares the file for reading and writing but not for deletion, soDeleteFileWandMoveFileExWfail with a sharing violation for as long as the descriptor is open.Observed on
main1690db9a2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, with a file the guest opened withsceKernelOpen:sceKernelUnlink(file)0x8002000D(EACCES), file staysremove(file)sceKernelRename(file, other)0x80020005(EIO)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 onmain1690db9a2, two files, +63/-1). I can't open it as a PR because of the intake check.NativeOpencallsCreateFileWwithFILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETEand 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_wopendoes.DeleteFileWon 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.MoveFileExWreturns access denied; it would need a POSIX-semantics rename), and files the built-in libc opens through_wfopenare not covered.guest_filesystemgains 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, andremovea file that is open.Checked on Windows (2f5ada5c on
main1690db9a2, WinLibs GCC 15.2.0 r7, Release):guest_filesystemfails at the unlink of the open file (guest errno 13). With the fix it passes 5/5 (--repeat until-fail:5).NativeOpenwith_wopenover 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._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.main0e90079fand 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 ofsceKernelOpencalls; 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.ctest --parallel 4passes 770/770 (5 skipped, no display in the session).check_conventions.py --base origin/mainpasses;pr_overlap.py --branchreports 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_filesystempasses 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
DeleteFileWremoves the name immediately there is not known.AI-assisted: yes