fix(libc): open a directory with freopen in a read mode on Windows #10433

Merged
Retr0G28 merged 3 commits from upstream-pr/10433 into main 2026-10-29 13:59:57 +00:00
Retr0G28 commented 2026-10-24 00:12:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-24T00:12:48Z.


What

On Windows, freopen of a stream onto a directory in a read mode (r, rb, re) failed with EACCES, which is the gap #8550 left open. FreeBSD 11 freopen opens the path with _open (freopen.c), and vn_open_vnode refuses a directory only for FWRITE or O_TRUNC. #10251 measured on a PS5 (firmware 13.60) that freopen onto a directory in a read mode succeeds there too.

freopen now takes the same path fopen takes since #8550. For a read mode and a path that is a directory, it reopens the stream through OpenDirectoryHandle, which opens a libkernel directory descriptor and wraps it in a host FILE. The stream is then marked as a directory stream. OpenDirectoryHandle is the first half of the old OpenDirectoryStream, split out so that FileStream::Reopen can call it the way it calls OpenNative. fopen keeps calling OpenDirectoryStream, which still uses it.

The stream behaves as a directory stream from fopen does: a read fails with EISDIR and sets the error indicator, fstat on its fileno reports a directory, and fclose, or a second freopen, removes the descriptor from libkernel's table. A write or update mode keeps failing with EISDIR.

stdin, stdout and stderr keep their host descriptor numbers 0 to 2 across freopen, so they cannot hold a libkernel directory descriptor. freopen of one of them onto a directory in a read mode now throws instead of failing with EACCES. The debt entry, which #10251 rewrote with the PS5 measurements, keeps that wording and changes only its last clause from "on Windows freopen onto a directory fails with EACCES" to the throw for the three standard streams.

Test: guest_filesystem opens a file with fopen and reopens it onto a directory with r. fstat must report a directory, and fread must fail with EISDIR and set the error indicator. It then reopens the stream onto the file with rb. fstat of 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 with re and closes it. On Windows, open must then return the same descriptor number, as the fopen loop already checks. A reopen onto a directory with r+ must fail with EISDIR.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, -DBUILD_TESTING=ON, on main a23f8fc9fb.

  • With main's libc, the new guest_filesystem checks fail (exit 0xc0000409 from the failed Require). With this PR it passes.
  • guest_filesystem, file_streams, guest_freopen_host_standard, guest_fopen_long_host_path, guest_directory* and guest_file_lock_* pass. guest_filesystem, file_streams and guest_freopen_host_standard pass again on head 1e32d23a9c, after the review follow-up.
  • After merging main 3a2b919816 (head e7798c0e4b): guest_filesystem, file_streams, guest_freopen_host_standard and guest_standard_streams pass.
  • python tools/check_conventions.py --base origin/main reports no errors.
  • Not run on Linux, where freopen already opened a directory and the new checks run unchanged. No title or homebrew run.

Overlap (tools/pr_overlap.py):

  • #10251 (docs only), now merged, rewrote the same debt entry with what it measured on a PS5. main is merged here and the entry rebased onto its wording.
  • #9763, now merged, added the debt entry for freopen of a closed stream next to this one and changed the closed-stream checks at the top of freopen and FileStream::Reopen. It merged cleanly with the lines changed here.
  • Of the PRs it could not compare, #7201 touches FileAndHeap.cpp, and #6044, #7202, #8520 and #10319 touch GuestFilesystem.cpp. None of them changes freopen or directory streams.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4990866640 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10433). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-24T00:12:48Z. --- ### What On Windows, `freopen` of a stream onto a directory in a read mode (`r`, `rb`, `re`) failed with `EACCES`, which is the gap #8550 left open. FreeBSD 11 `freopen` opens the path with `_open` ([`freopen.c`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdio/freopen.c#L153-L161)), and `vn_open_vnode` refuses a directory only for `FWRITE` or `O_TRUNC`. #10251 measured on a PS5 (firmware 13.60) that `freopen` onto a directory in a read mode succeeds there too. `freopen` now takes the same path `fopen` takes since #8550. For a read mode and a path that is a directory, it reopens the stream through `OpenDirectoryHandle`, which opens a libkernel directory descriptor and wraps it in a host `FILE`. The stream is then marked as a directory stream. `OpenDirectoryHandle` is the first half of the old `OpenDirectoryStream`, split out so that `FileStream::Reopen` can call it the way it calls `OpenNative`. `fopen` keeps calling `OpenDirectoryStream`, which still uses it. The stream behaves as a directory stream from `fopen` does: a read fails with `EISDIR` and sets the error indicator, `fstat` on its `fileno` reports a directory, and `fclose`, or a second `freopen`, removes the descriptor from libkernel's table. A write or update mode keeps failing with `EISDIR`. `stdin`, `stdout` and `stderr` keep their host descriptor numbers 0 to 2 across `freopen`, so they cannot hold a libkernel directory descriptor. `freopen` of one of them onto a directory in a read mode now throws instead of failing with `EACCES`. The debt entry, which #10251 rewrote with the PS5 measurements, keeps that wording and changes only its last clause from "on Windows `freopen` onto a directory fails with `EACCES`" to the throw for the three standard streams. Test: `guest_filesystem` opens a file with `fopen` and reopens it onto a directory with `r`. `fstat` must report a directory, and `fread` must fail with `EISDIR` and set the error indicator. It then reopens the stream onto the file with `rb`. `fstat` of 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 with `re` and closes it. On Windows, `open` must then return the same descriptor number, as the `fopen` loop already checks. A reopen onto a directory with `r+` must fail with `EISDIR`. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, on `main` a23f8fc9fb. - With `main`'s libc, the new `guest_filesystem` checks fail (exit 0xc0000409 from the failed `Require`). With this PR it passes. - `guest_filesystem`, `file_streams`, `guest_freopen_host_standard`, `guest_fopen_long_host_path`, `guest_directory*` and `guest_file_lock_*` pass. `guest_filesystem`, `file_streams` and `guest_freopen_host_standard` pass again on head 1e32d23a9c, after the review follow-up. - After merging `main` 3a2b919816 (head e7798c0e4b): `guest_filesystem`, `file_streams`, `guest_freopen_host_standard` and `guest_standard_streams` pass. - `python tools/check_conventions.py --base origin/main` reports no errors. - Not run on Linux, where `freopen` already opened a directory and the new checks run unchanged. No title or homebrew run. Overlap (`tools/pr_overlap.py`): - #10251 (docs only), now merged, rewrote the same debt entry with what it measured on a PS5. `main` is merged here and the entry rebased onto its wording. - #9763, now merged, added the debt entry for `freopen` of a closed stream next to this one and changed the closed-stream checks at the top of `freopen` and `FileStream::Reopen`. It merged cleanly with the lines changed here. - Of the PRs it could not compare, #7201 touches `FileAndHeap.cpp`, and #6044, #7202, #8520 and #10319 touch `GuestFilesystem.cpp`. None of them changes `freopen` or directory streams. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-24 01:00:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T01:00:36Z.

Review: APPROVED


Reviewed the exact PR head 4080a15f229892ce756c1e53670eee4d8795cff7 against current main and the overlap notes. The change reuses the directory-handle path only for Windows read-mode freopen calls onto directories; write/update modes still fail, fopen retains its existing path, and the standard-stream limitation is recorded as debt. The regression checks directory fstat, EISDIR plus the error indicator on read, reopening the stream onto a regular file afterward, re mode, 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 #10251 overlap is documentation/measurement, while #9763 concerns adjacent closed-stream handling. I found no blocking issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5635959619 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10433#pullrequestreview-5635959619). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T01:00:36Z. **Review:** APPROVED --- Reviewed the exact PR head `4080a15f229892ce756c1e53670eee4d8795cff7` against current `main` and the overlap notes. The change reuses the directory-handle path only for Windows read-mode `freopen` calls onto directories; write/update modes still fail, `fopen` retains its existing path, and the standard-stream limitation is recorded as debt. The regression checks directory `fstat`, `EISDIR` plus the error indicator on read, reopening the stream onto a regular file afterward, `re` mode, 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 `#10251` overlap is documentation/measurement, while #9763 concerns adjacent closed-stream handling. I found no blocking issue.
harisibrar1 commented 2026-10-24 01:12:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T01:12:44Z.

Review: APPROVED


Checked against main 11d6efa06c and FreeBSD 11.4 lib/libc/stdio/freopen.c:

  • This still merges cleanly with main. FileStream::Reopen on main already takes a (path, mode) open callback and already forgets the libkernel directory descriptor (std::exchange(directory, false)), so reusing OpenDirectoryHandle there fits well, and the split from OpenDirectoryStream keeps fopen's behaviour unchanged.
  • Checking is_directory before OpenNative gives the same result as fopen's "EACCES, then directory" order, since _wfopen never opens a directory on Windows.
  • Optional: the fopen loop checks on Windows that open reuses the directory's descriptor number after fclose. Doing the same after freopen(listed, "rb") and after the final fclose would cover the forget path in Reopen too.
  • Nit: the "fopen: libkernel has not registered…" message is now reached from freopen as well.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636045882 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10433#pullrequestreview-5636045882). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T01:12:44Z. **Review:** APPROVED --- Checked against main 11d6efa06c and FreeBSD 11.4 `lib/libc/stdio/freopen.c`: - This still merges cleanly with main. `FileStream::Reopen` on main already takes a `(path, mode)` open callback and already forgets the libkernel directory descriptor (`std::exchange(directory, false)`), so reusing `OpenDirectoryHandle` there fits well, and the split from `OpenDirectoryStream` keeps fopen's behaviour unchanged. - Checking `is_directory` before `OpenNative` gives the same result as fopen's "EACCES, then directory" order, since `_wfopen` never opens a directory on Windows. - Optional: the fopen loop checks on Windows that `open` reuses the directory's descriptor number after `fclose`. Doing the same after `freopen(listed, "rb")` and after the final `fclose` would cover the forget path in `Reopen` too. - Nit: the "fopen: libkernel has not registered…" message is now reached from freopen as well. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
harisibrar1 commented 2026-10-24 13:15:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:15:10Z.

Review: APPROVED


Re-checked head 1e32d23a9c against main 6cd36e6517:

  • After freopen(listed, "rb"), the test now checks that sceKernelFstat reports a regular file (GuestFilesystem.cpp:695). After the final reopen onto the directory and fclose, it checks on Windows that open reuses the same descriptor (:697-706). Together these cover the forget path in Reopen.
  • The error message now starts with OpenDirectoryHandle: (FileAndHeap.cpp:331), so it's right for both fopen and freopen.
  • It still merges cleanly into main. Thanks for the extra coverage, LGTM.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638379272 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10433#pullrequestreview-5638379272). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:15:10Z. **Review:** APPROVED --- Re-checked head 1e32d23a9c against main 6cd36e6517: - After `freopen(listed, "rb")`, the test now checks that `sceKernelFstat` reports a regular file (GuestFilesystem.cpp:695). After the final reopen onto the directory and `fclose`, it checks on Windows that `open` reuses the same descriptor (:697-706). Together these cover the forget path in `Reopen`. - The error message now starts with `OpenDirectoryHandle:` (FileAndHeap.cpp:331), so it's right for both fopen and freopen. - It still merges cleanly into main. Thanks for the extra coverage, LGTM. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Sign in to join this conversation.
No reviewers
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!10433
No description provided.