fix(libc): open a directory with fopen in a read mode on Windows #8550
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8550"
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 Retr0G28 on 2026-10-20T16:15:54Z.
What
On Windows,
fopenof a directory in a read mode (r,rb,re) failed withEACCES, becauseCreateFileWwithoutFILE_FLAG_BACKUP_SEMANTICSrefuses a directory. FreeBSD 11fopenopens the path with_open(lib/libc/stdio/fopen.c), andvn_open_vnoderefuses a directory only forFWRITEorO_TRUNC(sys/kern/vfs_vnops.c), so the call succeeds there. Linux hosts already open it. #8192 already made the write modes fail withEISDIR.When the read-mode open fails with
EACCESand the path is a directory,fopennow opens it as a libkernel directory descriptor (File::OpenDirectoryDescriptor), the same kind of descriptoropenreturns for a directory, and wraps it in the stream. libc cannot link libkernel, since libkernel links libc, so libkernel registers its open and forget functions in libc when it loads (LibcRegisterDirectoryStreams_nid_no_patch).fcloseandfreopenremove the descriptor from libkernel's table before closing it, so a later file that gets the same number is not taken for a directory.The stream then behaves as a directory stream does on a Linux host:
fread,fgetc,getc_unlocked,fgets,fscanf,vfscanf, and the wide andgetcforms built on them) returns 0,EOForNULL, sets the error indicator and setserrnotoEISDIR. The Windows CRT already fails the read and sets the indicator (ReadFileon a directory handle fails withERROR_INVALID_FUNCTION), but reportsEINVAL, or leaveserrnoalone infscanf, so a failed read on a directory stream with the indicator set reportsEISDIR;EBADFand sets the error indicator, as on any read-only stream;fseek,ftell,ungetc,fileno,feof,ferror,clearerr,setvbufand the lock functions work, andfflushreturns 0, as FreeBSD does on a stream that is not open for writing.FreeBSD 11's own result for a read depends on the filesystem:
tmpfs_readfails withEISDIR(sys/fs/tmpfs/tmpfs_vnops.c), whileffs_readreturns the raw directory blocks (sys/ufs/ffs/ffs_vnops.c). What the console's filesystems return is unverified, which the entry indocs/dev/debt/libc.mdrecords. That entry is also cut to the part that is still open:freopenonto a directory in a read mode still fails withEACCES.Tests:
guest_filesystemopens a directory withr,rbandreon every host. For each mode it checks:filenoplussceKernelFstatreport a directory;feof,ferrorandfflushreturn 0;fread,fgetc,fgetsandfscanffail withEISDIRand set the error indicator, whichclearerrclears;fseekandftellsucceed at 0;fputcfails withEBADF.On Windows it then closes the stream and checks that
openof a regular file reuses the descriptor number and reads the file's data.file_streamsexpected the old WindowsEACCESforrb. It now checkswbandEISDIRon every host. That test links only libc, so no directory streams are registered in it.Tested
Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release,
-DBUILD_TESTING=ON, rebased onmain47fa6eecf.main's libc,guest_filesystemfails at thefopencheck:Filesystem check failed at line 609(guest errno 13,EACCES).guest_filesystempasses. Iffclosedoes not remove the descriptor from libkernel's table,guest_filesystemfails at the reuse check:readon the new file returns guest errno 21.fseek/ftell/ungetcsucceeding) were measured with a standalone program on a_fdopenstream over a directory handle.-k 0: the only failure is the MinGW alignment error inImageArrayWindowPastLayerLimit.cppthat is already onmain(fixed by #8310). ctest-R "fopen|stdio|stream|file|directory|libc|scanf|printf|random|errors|dup|fcntl" -E "^agc_|guest_host_libc|windows_working_directory": 60 of 60 pass (the two excluded tests need the relinker binary); a test that timed out under parallel load passes when run alone.python tools/check_conventions.py --base origin/mainpasses.guest_filesystemblock now runs there too. It expects glibc's behaviour on a directory stream (EISDIRfromread,EBADFfrom a write to a read-only stream,lseekon a directory succeeding), so the Linux CI run is the check.Overlap (
tools/pr_overlap.py):DirectoryDescriptor.cpp, and #7447 editsFileStream::Closenext to the new lines. Whichever merges second needs a small rebase. Neither opens directory streams.SwitchToReadingbefore each read; the directory reads keep going through it, since a directory stream is not an update stream. #8150 changes the same stdio files and does not touch directory streams.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T16:27:11Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libc/include/FileStream.hppL277-278core/libs/prx/libkernel/File/src/DirectoryDescriptor.cppL6-14, L64-65Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
docs/dev/debt/libc.mdcore/libs/prx/libc/include/FileStream.hppcore/libs/prx/libc/src/StandardStreams.cppcore/libs/prx/libc/include/FileStream.hppcore/libs/prx/libc/src/StandardStreams.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/tests/FileStreams.cppcore/libs/tests/GuestFilesystem.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/src/FileAndHeap.cppdocs/dev/debt/libc.mdcore/libs/prx/libc/include/FileStream.hppcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libc/src/StandardStreams.cppcore/libs/prx/libkernel/File/src/DirectoryDescriptor.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/src/StandardStreams.cppcore/libs/tests/FileStreams.cppdocs/dev/debt/libc.mdcore/libs/prx/libc/include/FileStream.hppcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libc/src/StandardStreams.cppcore/libs/prx/libkernel/File/src/DirectoryDescriptor.cppcore/libs/prx/libkernel/File/src/DirectoryDescriptor.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T23:19:11Z.
@Retr0G28 Thanks. The FreeBSD references and the descriptor-reuse check make this easy to follow, and the registration from libkernel follows the existing
LibcExit_nid_no_patchdirection.One request, about what a caller sees after the
fopensucceeds. Today on Windows,fopenof a directory in a read mode returns NULL withEACCES, and code that probes a path that way just moves on. With this change thefopensucceeds, and thenfread,fgetc,fgets,fseek,ftellorfprintfon the stream throw. So a caller that opens a path, then reads or measures it, goes from an error it can handle to a stop. On Linux the same stream already reads withEISDIR(the host'sreadrefuses a directory), and FreeBSD'stmpfs_readdoes the same, as you noted.Could the read functions on a directory stream fail the way the Linux host does instead of throwing: return 0 or EOF, set the stream's error flag, and set
errnotoEISDIR? Then a directory stream behaves the same on both hosts. If you'd rather keep the throw, a title or homebrew that needsfopenof a directory to succeed would show that the trade is worth it. The PR currently lists none.After that, the bar is a full CI run with
guest_filesystemandfile_streamspassing on Linux and Windows. #8520 also edits the top ofDirectoryDescriptor.cpp, so whichever merges second needs the small rebase you mentioned.Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-21T01:30:09Z.
@oneandonlydean Thanks, agreed: a stop where the caller had an error it could handle is the wrong trade, so the throws are gone. Rebased on
main(40764df67).A directory stream now behaves the way it does on a Linux host:
fread,fgetc,getc_unlocked,fgets,fscanf,vfscanf, and the wide andgetcforms built on them) return 0,EOForNULL, set the error indicator, and seterrnotoEISDIR.EBADFand set the indicator.fseek,ftellandungetcwork.Most of this comes from the host for free. I measured a
_fdopenstream over a directory handle: the CRT already fails reads and sets the indicator (ReadFilegivesERROR_INVALID_FUNCTION), fails writes withEBADF, and lets seeks succeed. The difference iserrno: reads reportEINVAL, or leaveerrnounchanged infscanf. So after a failed read on a directory stream with the indicator set,errnobecomesEISDIR. A directory read never reaches end of file, so the indicator can only mean the refused read.The
guest_filesystemblock now runs on every host and checks the read, write and seek results above. Only the descriptor-reuse check stays Windows-only. I have no Linux machine, so the Linux leg of the CI run is the check there. The debt entry now records that what the console's filesystems return for a directory read is unverified (FreeBSD 11 tmpfs givesEISDIR, FFS returns the raw blocks).Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-21T18:43:57Z.
Independent Windows run of this PR (head
36226741f).Environment: Windows 11 (10.0.26300), Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 (MinGW), Release, BUILD_TESTING=ON. I built only the libs and the five test targets below.
Results:
Not run: Linux/macOS, the rest of the 60-test selection in the description, the full repository CTest, and any title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T04:03:33Z.
@Retr0G28 thanks, this is what I hoped for: reading a directory stream now fails with EISDIR and sets the error indicator instead of throwing, and the new cases cover fread, fgetc, fgets and fscanf. Sorry for the slow reply. I'm sending it for a full CI run.