Repository navigation
fix(libc): open fopen streams with delete sharing on Windows - #4090
Merged
oneandonlydean merged 1 commit intoOct 12, 2026
Merged
oneandonlydean merged 1 commit into
oneandonlydean merged 1 commit into
Conversation
fopen opened its stream with _wfopen, which shares read and write access but not delete access. While the guest held a stream open, rename of its file failed with EIO and remove with EBUSY. POSIX rename and remove work on open files, as on Linux and FreeBSD, and since boykopovar#3677 the descriptors sceKernelOpen returns share delete access. fopen now creates the handle with CreateFileW sharing read, write and delete, with the access and creation disposition of the mode, and wraps it with _open_osfhandle and _wfdopen, keeping append mode. A failed CreateFileW sets errno through the error mapping boykopovar#3677 added to libkernel's NativeOpen, which moves to libc as WindowsOpenError_nid_no_patch so that both use it. freopen still reopens through _wfreopen; TechnicalDebt records it.
9 tasks done
10 tasks done
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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,
fopenopened its stream with_wfopen, which shares read and write access but not delete access. While the guest held a stream open,renameof its file failed withEIOandremovewithEBUSY. POSIXrenameandremovework on a file that is open, as on Linux and FreeBSD, and since #3677 the descriptorssceKernelOpenreturns share delete access, so a file opened withopencould be moved or removed but the same file opened withfopencould not.fopennow creates the handle the way #3677'sNativeOpendoes:CreateFileWsharing read, write and delete, an inheritable handle, and the access and creation disposition of the mode (ropens an existing file,wcreates or truncates,aopens or creates,xcreates a new file,+adds the other direction). The handle is wrapped with_open_osfhandle, with_O_APPENDforamodes, and_wfdopenwith the same mode minusx. WhenCreateFileWfails,errnocomes from #3677's error mapping, which moves from libkernel'sOpen.cppto libc asWindowsOpenError_nid_no_patch(declared inGeneral.hpp) so that both use the same table;Open.cppkeeps its behaviour. There is no_wfopenfallback.freopenstill reopens through_wfreopen, which does not share delete access; TechnicalDebt records it.Tested
Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release,
-DBUILD_TESTING=ON, onmainat 325e0a8 with #4021 applied locally, sincemain's Windows build stops inSocket.cppwithout it:file_streamsnow opens a stream withw+b, renames and removes its file while the stream is open, and reads the data back through the stream; then checks thatabappends across two streams and thatwbxon an existing file fails withEEXIST. Withmain'sFileAndHeap.cpp,General.cpp,General.hppandOpen.cppthe test fails:renamereturns -1 witherrno5 (EIO) andremove-1 witherrno16 (EBUSY). With the change it passes, 20 runs in a row, as doesguest_filesystemwith fix(libkernel): allow delete sharing for open Windows files #3677's delete-sharing checks forsceKernelOpen.core/libs/prx/libc/CMakeLists.txtandcore/libs/prx/libkernel/CMakeLists.txtthat build on Windows pass.tools/check_conventions.py --base origin/mainpasses.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)