fix(libc): open fopen streams with delete sharing on Windows #4090

Merged
EagleBearerYS merged 1 commit from upstream-pr/4090 into main 2026-10-12 08:39:19 +00:00
EagleBearerYS commented 2026-10-11 23:01:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-11T23:01:49Z.


What

On Windows, 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 a file that is open, as on Linux and FreeBSD, and since #3677 the descriptors sceKernelOpen returns share delete access, so a file opened with open could be moved or removed but the same file opened with fopen could not.

fopen now creates the handle the way #3677's NativeOpen does: CreateFileW sharing read, write and delete, an inheritable handle, and the access and creation disposition of the mode (r opens an existing file, w creates or truncates, a opens or creates, x creates a new file, + adds the other direction). The handle is wrapped with _open_osfhandle, with _O_APPEND for a modes, and _wfdopen with the same mode minus x. When CreateFileW fails, errno comes from #3677's error mapping, which moves from libkernel's Open.cpp to libc as WindowsOpenError_nid_no_patch (declared in General.hpp) so that both use the same table; Open.cpp keeps its behaviour. There is no _wfopen fallback.

freopen still 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, on main at 325e0a8b with #4021 applied locally, since main's Windows build stops in Socket.cpp without it:

  • file_streams now opens a stream with w+b, renames and removes its file while the stream is open, and reads the data back through the stream; then checks that ab appends across two streams and that wbx on an existing file fails with EEXIST. With main's FileAndHeap.cpp, General.cpp, General.hpp and Open.cpp the test fails: rename returns -1 with errno 5 (EIO) and remove -1 with errno 16 (EBUSY). With the change it passes, 20 runs in a row, as does guest_filesystem with #3677's delete-sharing checks for sceKernelOpen.
  • All 128 tests registered in core/libs/prx/libc/CMakeLists.txt and core/libs/prx/libkernel/CMakeLists.txt that build on Windows pass.
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: Linux (the change is Windows-only), macOS, any title.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823048935 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4090). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-11T23:01:49Z. --- ### What On Windows, `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 a file that is open, as on Linux and FreeBSD, and since #3677 the descriptors `sceKernelOpen` returns share delete access, so a file opened with `open` could be moved or removed but the same file opened with `fopen` could not. `fopen` now creates the handle the way #3677's `NativeOpen` does: `CreateFileW` sharing read, write and delete, an inheritable handle, and the access and creation disposition of the mode (`r` opens an existing file, `w` creates or truncates, `a` opens or creates, `x` creates a new file, `+` adds the other direction). The handle is wrapped with `_open_osfhandle`, with `_O_APPEND` for `a` modes, and `_wfdopen` with the same mode minus `x`. When `CreateFileW` fails, `errno` comes from #3677's error mapping, which moves from libkernel's `Open.cpp` to libc as `WindowsOpenError_nid_no_patch` (declared in `General.hpp`) so that both use the same table; `Open.cpp` keeps its behaviour. There is no `_wfopen` fallback. `freopen` still 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`, on `main` at 325e0a8b with #4021 applied locally, since `main`'s Windows build stops in `Socket.cpp` without it: - `file_streams` now opens a stream with `w+b`, renames and removes its file while the stream is open, and reads the data back through the stream; then checks that `ab` appends across two streams and that `wbx` on an existing file fails with `EEXIST`. With `main`'s `FileAndHeap.cpp`, `General.cpp`, `General.hpp` and `Open.cpp` the test fails: `rename` returns -1 with `errno` 5 (`EIO`) and `remove` -1 with `errno` 16 (`EBUSY`). With the change it passes, 20 runs in a row, as does `guest_filesystem` with #3677's delete-sharing checks for `sceKernelOpen`. - All 128 tests registered in `core/libs/prx/libc/CMakeLists.txt` and `core/libs/prx/libkernel/CMakeLists.txt` that build on Windows pass. - `tools/check_conventions.py --base origin/main` passes. - Not tested: Linux (the change is Windows-only), macOS, any title. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
github-actions[bot] commented 2026-10-12 08:34:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T08:34:59Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
#4380 core/libs/tests/FileStreams.cpp L21, L93
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6120676046 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4090#issuecomment-6120676046). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T08:34:59Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4380 | `core/libs/tests/FileStreams.cpp` [L21](https://github.com/boykopovar/AnyPS5/blob/9bc362351780feb4ac57e0c10790ce5727f78cc6/core/libs/tests/FileStreams.cpp#L21), [L93](https://github.com/boykopovar/AnyPS5/blob/9bc362351780feb4ac57e0c10790ce5727f78cc6/core/libs/tests/FileStreams.cpp#L93) | | |
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!4090
No description provided.