fix(libc): open a directory with freopen in a read mode on Windows #10433
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10433"
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-24T00:12:48Z.
What
On Windows,
freopenof a stream onto a directory in a read mode (r,rb,re) failed withEACCES, which is the gap #8550 left open. FreeBSD 11freopenopens the path with_open(freopen.c), andvn_open_vnoderefuses a directory only forFWRITEorO_TRUNC. #10251 measured on a PS5 (firmware 13.60) thatfreopenonto a directory in a read mode succeeds there too.freopennow takes the same pathfopentakes since #8550. For a read mode and a path that is a directory, it reopens the stream throughOpenDirectoryHandle, which opens a libkernel directory descriptor and wraps it in a hostFILE. The stream is then marked as a directory stream.OpenDirectoryHandleis the first half of the oldOpenDirectoryStream, split out so thatFileStream::Reopencan call it the way it callsOpenNative.fopenkeeps callingOpenDirectoryStream, which still uses it.The stream behaves as a directory stream from
fopendoes: a read fails withEISDIRand sets the error indicator,fstaton itsfilenoreports a directory, andfclose, or a secondfreopen, removes the descriptor from libkernel's table. A write or update mode keeps failing withEISDIR.stdin,stdoutandstderrkeep their host descriptor numbers 0 to 2 acrossfreopen, so they cannot hold a libkernel directory descriptor.freopenof one of them onto a directory in a read mode now throws instead of failing withEACCES. The debt entry, which #10251 rewrote with the PS5 measurements, keeps that wording and changes only its last clause from "on Windowsfreopenonto a directory fails withEACCES" to the throw for the three standard streams.Test:
guest_filesystemopens a file withfopenand reopens it onto a directory withr.fstatmust report a directory, andfreadmust fail withEISDIRand set the error indicator. It then reopens the stream onto the file withrb.fstatof its descriptor must report a regular file, so the directory registration is gone, and the stream reads the file's data. It reopens the stream onto the directory withreand closes it. On Windows,openmust then return the same descriptor number, as thefopenloop already checks. A reopen onto a directory withr+must fail withEISDIR.Tested
Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release,
-DBUILD_TESTING=ON, onmaina23f8fc9fb.main's libc, the newguest_filesystemchecks fail (exit 0xc0000409 from the failedRequire). With this PR it passes.guest_filesystem,file_streams,guest_freopen_host_standard,guest_fopen_long_host_path,guest_directory*andguest_file_lock_*pass.guest_filesystem,file_streamsandguest_freopen_host_standardpass again on head1e32d23a9c, after the review follow-up.main3a2b919816(heade7798c0e4b):guest_filesystem,file_streams,guest_freopen_host_standardandguest_standard_streamspass.python tools/check_conventions.py --base origin/mainreports no errors.freopenalready opened a directory and the new checks run unchanged. No title or homebrew run.Overlap (
tools/pr_overlap.py):mainis merged here and the entry rebased onto its wording.freopenof a closed stream next to this one and changed the closed-stream checks at the top offreopenandFileStream::Reopen. It merged cleanly with the lines changed here.FileAndHeap.cpp, and #6044, #7202, #8520 and #10319 touchGuestFilesystem.cpp. None of them changesfreopenor 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 MarcZX10 on 2026-10-24T01:00:36Z.
Review: APPROVED
Reviewed the exact PR head
4080a15f229892ce756c1e53670eee4d8795cff7against currentmainand the overlap notes. The change reuses the directory-handle path only for Windows read-modefreopencalls onto directories; write/update modes still fail,fopenretains its existing path, and the standard-stream limitation is recorded as debt. The regression checks directoryfstat,EISDIRplus the error indicator on read, reopening the stream onto a regular file afterward,remode, and update-mode failure.The author reports the focused Windows tests on this head and a negative control against
main; the required hosted checks pass. Linux product tests were not run by me, and the hosted Linux/Windows full-build jobs are skipped under repository policy. The#10251overlap is documentation/measurement, while #9763 concerns adjacent closed-stream handling. I found no blocking issue.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T01:12:44Z.
Review: APPROVED
Checked against main
11d6efa06cand FreeBSD 11.4lib/libc/stdio/freopen.c:FileStream::Reopenon main already takes a(path, mode)open callback and already forgets the libkernel directory descriptor (std::exchange(directory, false)), so reusingOpenDirectoryHandlethere fits well, and the split fromOpenDirectoryStreamkeeps fopen's behaviour unchanged.is_directorybeforeOpenNativegives the same result as fopen's "EACCES, then directory" order, since_wfopennever opens a directory on Windows.openreuses the directory's descriptor number afterfclose. Doing the same afterfreopen(listed, "rb")and after the finalfclosewould cover the forget path inReopentoo.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:15:10Z.
Review: APPROVED
Re-checked head
1e32d23a9cagainst main6cd36e6517:freopen(listed, "rb"), the test now checks thatsceKernelFstatreports a regular file (GuestFilesystem.cpp:695). After the final reopen onto the directory andfclose, it checks on Windows thatopenreuses the same descriptor (:697-706). Together these cover the forget path inReopen.OpenDirectoryHandle:(FileAndHeap.cpp:331), so it's right for both fopen and freopen.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.