fix(libkernel): change O_APPEND on a Windows file through fcntl #9256

Merged
Dom3005 merged 2 commits from upstream-pr/9256 into main 2026-10-24 03:37:07 +00:00
Dom3005 commented 2026-10-21 16:40:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dom3005 on 2026-10-21T16:40:12Z.


What

On Windows, fcntl(F_SETFL) threw fcntl: changing O_APPEND on a file is not implemented whenever it set or cleared O_APPEND on a regular file. FreeBSD 11.4's kern_fcntl changes O_APPEND like any other F_SETFL flag.

The guest flag already decides whether write goes through WriteAtEnd. A descriptor opened with O_APPEND also carries the CRT's append flag, though (NativeOpen passes _O_APPEND to _open_osfhandle), so after the guest cleared O_APPEND, ::_write would still seek to the end. The CRT flag has to stay set while O_APPEND is on, because fdopen(fd, "w") on such a descriptor writes through the CRT and must keep appending.

When O_APPEND changes on a Windows file, F_SETFL now duplicates the descriptor's handle, opens it with or without _O_APPEND through _open_osfhandle, and _dup2s it onto the same number. This is the same pattern MoveAppendingHandle in libc uses for fdopen in append mode. On FreeBSD O_APPEND belongs to the open file, so this is done for every descriptor that shares the status (the descriptors dup and F_DUPFD create). The duplicated handle keeps the same file object, so file position and flock locks carry over.

The "Changing O_APPEND on a Windows file" clause is removed from the fcntl entry in docs/dev/debt/libkernel.md.

Tested

Windows 11 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON, on main cecb540a6.

  • guest_kernel_fcntl now runs the same O_APPEND checks on every host, where it used to expect the throw on Windows: append, clear O_APPEND and write at offset 0, set it again through a F_DUPFD duplicate and check that the original descriptor appends, clear it through the duplicate and check that the original no longer appends, then read the file back (Ybcdeabc).
    • Without the fix, it fails with fcntl: changing O_APPEND on a file is not implemented, fd=3.
    • With the fix, it passes in 3 of 3 runs.
  • ctest -E '^agc_driver': 1230/1230 passed.
  • python tools/check_conventions.py: passed.
  • Not run on Linux. The Linux code is unchanged, and the test's new checks follow POSIX O_APPEND semantics, which the host's own F_SETFL provides.

Overlap: tools/pr_overlap.py shows #8544 and #9109 editing the same paragraph of docs/dev/debt/libkernel.md (#8544 changes the pipe F_GETFL part of the fcntl entry), so whichever merges second needs a one-line rebase. #6044, #6977 and #8520 share files, but their diffs don't touch O_APPEND.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4955049233 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9256). Originally posted by [Dom3005](https://github.com/Dom3005) on 2026-10-21T16:40:12Z. --- ### What On Windows, `fcntl(F_SETFL)` threw `fcntl: changing O_APPEND on a file is not implemented` whenever it set or cleared `O_APPEND` on a regular file. FreeBSD 11.4's `kern_fcntl` changes `O_APPEND` like any other `F_SETFL` flag. The guest flag already decides whether `write` goes through `WriteAtEnd`. A descriptor opened with `O_APPEND` also carries the CRT's append flag, though (`NativeOpen` passes `_O_APPEND` to `_open_osfhandle`), so after the guest cleared `O_APPEND`, `::_write` would still seek to the end. The CRT flag has to stay set while `O_APPEND` is on, because `fdopen(fd, "w")` on such a descriptor writes through the CRT and must keep appending. When `O_APPEND` changes on a Windows file, `F_SETFL` now duplicates the descriptor's handle, opens it with or without `_O_APPEND` through `_open_osfhandle`, and `_dup2`s it onto the same number. This is the same pattern `MoveAppendingHandle` in libc uses for `fdopen` in append mode. On FreeBSD `O_APPEND` belongs to the open file, so this is done for every descriptor that shares the status (the descriptors `dup` and `F_DUPFD` create). The duplicated handle keeps the same file object, so file position and `flock` locks carry over. The "Changing `O_APPEND` on a Windows file" clause is removed from the `fcntl` entry in `docs/dev/debt/libkernel.md`. ### Tested Windows 11 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Release, `BUILD_TESTING=ON`, on `main` cecb540a6. - `guest_kernel_fcntl` now runs the same `O_APPEND` checks on every host, where it used to expect the throw on Windows: append, clear `O_APPEND` and write at offset 0, set it again through a `F_DUPFD` duplicate and check that the original descriptor appends, clear it through the duplicate and check that the original no longer appends, then read the file back (`Ybcdeabc`). - Without the fix, it fails with `fcntl: changing O_APPEND on a file is not implemented, fd=3`. - With the fix, it passes in 3 of 3 runs. - `ctest -E '^agc_driver'`: 1230/1230 passed. - `python tools/check_conventions.py`: passed. - Not run on Linux. The Linux code is unchanged, and the test's new checks follow POSIX `O_APPEND` semantics, which the host's own `F_SETFL` provides. Overlap: `tools/pr_overlap.py` shows #8544 and #9109 editing the same paragraph of `docs/dev/debt/libkernel.md` (#8544 changes the pipe `F_GETFL` part of the `fcntl` entry), so whichever merges second needs a one-line rebase. #6044, #6977 and #8520 share files, but their diffs don't touch `O_APPEND`. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
cadenasynyster commented 2026-10-21 21:28:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-21T21:28:16Z.


Independent Windows run of this PR (head da61bf7c1).

Environment: Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 (MinGW), Release, BUILD_TESTING=ON.

Results:

  • PR head: guest_kernel_fcntl passes 3/3 (--repeat until-fail:3).
  • Baseline: I kept the PR's test and replaced only core/libs/prx/libkernel/File/src/DescriptorControl.cpp with the merge-base version (cecb540a6). The test fails with fcntl: changing O_APPEND on a file is not implemented, fd=3, the same as the description. I restored the PR's file afterwards.

Note for anyone repeating the baseline: that failing run aborts after writing abcde and ab, so kernel_fcntl_probe.bin stays behind in build/core/libs/prx/libkernel with 7 bytes. FileFlagsRoundTrip opens it without O_TRUNC, so every later run, on main as well, fails earlier at the FIONREAD check (readable == 3, line 241) until the file is deleted. My first runs of this PR hit that. With the file deleted, main (0c6a5bd59) and this PR both pass. This is in the existing test, not in this PR's change.

Not run: Linux/macOS, fdopen(fd, "w") on a descriptor whose O_APPEND was changed (the description mentions it, but the test does not cover it), the full repository CTest, and any title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6326952397 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9256#issuecomment-6326952397). Originally posted by [cadenasynyster](https://github.com/cadenasynyster) on 2026-10-21T21:28:16Z. --- Independent Windows run of this PR (head da61bf7c1). **Environment:** Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 (MinGW), Release, BUILD_TESTING=ON. **Results:** - PR head: `guest_kernel_fcntl` passes 3/3 (`--repeat until-fail:3`). - Baseline: I kept the PR's test and replaced only `core/libs/prx/libkernel/File/src/DescriptorControl.cpp` with the merge-base version (cecb540a6). The test fails with `fcntl: changing O_APPEND on a file is not implemented, fd=3`, the same as the description. I restored the PR's file afterwards. **Note for anyone repeating the baseline:** that failing run aborts after writing `abcde` and `ab`, so `kernel_fcntl_probe.bin` stays behind in `build/core/libs/prx/libkernel` with 7 bytes. `FileFlagsRoundTrip` opens it without `O_TRUNC`, so every later run, on `main` as well, fails earlier at the `FIONREAD` check (`readable == 3`, line 241) until the file is deleted. My first runs of this PR hit that. With the file deleted, `main` (0c6a5bd59) and this PR both pass. This is in the existing test, not in this PR's change. **Not run:** Linux/macOS, `fdopen(fd, "w")` on a descriptor whose `O_APPEND` was changed (the description mentions it, but the test does not cover it), the full repository CTest, and any title. AI-assisted: yes
Dom3005 commented 2026-10-21 21:55:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dom3005 on 2026-10-21T21:55:59Z.


Thanks for the independent run and the baseline. Before this PR, Windows only expected the throw at that point, so the test could not stop partway and leave the probe file behind. Now it can, so ddcb41a9a removes kernel_fcntl_probe.bin at the start of FileFlagsRoundTrip. I checked it by writing a 7-byte abcdeab probe file into build/core/libs/prx/libkernel: the previous binary fails, and the new one passes --repeat until-fail:3 and removes the file afterwards.

The fdopen(fd, "w") case is still covered only by the reasoning in the description. It goes through libc, so this kernel test does not exercise it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6327423407 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9256#issuecomment-6327423407). Originally posted by [Dom3005](https://github.com/Dom3005) on 2026-10-21T21:55:59Z. --- Thanks for the independent run and the baseline. Before this PR, Windows only expected the throw at that point, so the test could not stop partway and leave the probe file behind. Now it can, so ddcb41a9a removes `kernel_fcntl_probe.bin` at the start of `FileFlagsRoundTrip`. I checked it by writing a 7-byte `abcdeab` probe file into `build/core/libs/prx/libkernel`: the previous binary fails, and the new one passes `--repeat until-fail:3` and removes the file afterwards. The `fdopen(fd, "w")` case is still covered only by the reasoning in the description. It goes through libc, so this kernel test does not exercise it.
DavidSamir commented 2026-10-22 08:50:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T08:50:01Z.


Hi @Dom3005, a merge-order note for two of mine:

  • #9561 (O_FSYNC) changes the O_FSYNC expectation in core/libs/tests/GuestKernelFcntl.cpp next to your lines there, and both edit the fcntl row of docs/dev/debt/libkernel.md.
  • #9557 (pipe SIGIO owner) touches the same fcntl debt row.
    None of the code meets. Proposal: no dependency either way; whichever merges second keeps both test changes and all the debt sentences. Does that work for you? I'll record it in both checklists.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6336710556 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9256#issuecomment-6336710556). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T08:50:01Z. --- Hi @Dom3005, a merge-order note for two of mine: - #9561 (`O_FSYNC`) changes the `O_FSYNC` expectation in `core/libs/tests/GuestKernelFcntl.cpp` next to your lines there, and both edit the `fcntl` row of `docs/dev/debt/libkernel.md`. - #9557 (pipe `SIGIO` owner) touches the same `fcntl` debt row. None of the code meets. Proposal: no dependency either way; whichever merges second keeps both test changes and all the debt sentences. Does that work for you? I'll record it in both checklists. AI-assisted: yes
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!9256
No description provided.