feat(libkernel_sys): implement lchflags #9442

Merged
Gruzzberry merged 2 commits from upstream-pr/9442 into main 2026-10-28 00:00:19 +00:00
Gruzzberry commented 2026-10-21 22:15:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-21T22:15:50Z.


What

lchflags was a throwing stub in libkernel_sys. FreeBSD 11.4 implements it as chflags without following a symbolic link in the last component: sys_lchflags calls kern_chflagsat with AT_SYMLINK_NOFOLLOW, and kern_chflagsat only switches namei from FOLLOW to NOFOLLOW.

chflags in Stdio.cpp now goes through ChangeFileFlags_nid_no_patch, which takes the same follow choice. When not following, a symbolic link in the last component is the target, so a dangling link is accepted instead of failing the lookup. The link is recognised with File::FillLinkStat, the guest lstat, because under MinGW std::filesystem::symlink_status reports a Windows symbolic link as the file or directory it names. A trailing slash still follows, as in FreeBSD. The flag checks are the existing ones, unchanged: unknown bits EOPNOTSUPP, SF_SNAPSHOT EPERM, any other non-zero flag throws because the host keeps no BSD file flags. chflags itself behaves as before.

Tested

Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, -DBUILD_TESTING=ON, on main d6eafa54d0 (rebased), Developer Mode on so the link cases run.

  • guest_kernel_sys: the new lchflags checks (a regular file, EFAULT, ENOENT for empty and missing paths, ENOTDIR through a file, EOPNOTSUPP, EPERM, a dangling link and a working link with flags 0, EOPNOTSUPP and EPERM on the link, the throw for UF_IMMUTABLE) abort on main with lchflags_nid_postfix not implemented and pass with this PR.
  • guest_filesystem (the existing chflags and fchflags checks) and guest_file_timestamps: pass.
  • Full suite on main d6eafa54d0 and on main with #9442, #9469, #9474, #9487 and #9496 merged together, same machine: 1890 tests each, identical results (1869 passed, 18 skipped; on both, the 3 agc_driver_sample_order* tests fail and agc_driver_image_array_window_past_layer_limit is not built, as its 64 KiB alignas is rejected by this MinGW toolchain).
  • python tools/check_conventions.py --base upstream/main: passes.
  • Linux: built and tested by @jotitadev on the previous head (Ubuntu 26.04, GCC 15.2; guest_kernel_sys passes, and fails with lchflags_nid_postfix not implemented without the change); see the review.
  • Not tested: a console, a title run.

Overlaps

  • #9423 (merged) made PathError copy the guest path; this PR is rebased onto it, so ChangeFileFlags_nid_no_patch takes the copied path for both chflags and lchflags.
  • #10625 (@Dom3005) adds declarations in the same GuestKernelSys.cpp block; agreed on #9442: this PR goes first and #10625 rebases, keeping both.
  • #9200 (lchmod) removes the stub next to lchflags in Export.Discovery.cpp, and #9097 and #9103 add code next to these lines in GuestKernelSys.cpp and libkernel_sys/Export.cpp: text-only, whichever lands second rebases.
  • #6044 moves GuestKernelSys.cpp to tests/process/; if it lands first, the new cases move with the file in the rebase.
  • tools/pr_overlap.py --branch on the rebased head lists no other direct overlap; of the 22 pull requests it could not compare, none changes chflags, LookupError or lchflags.

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); no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source: none added
  • Generic behaviour, not specific to one title
  • Depends on: none (#9423 merged and this PR is rebased onto it). Merge order: before #10625 (agreed with @Dom3005); text-only neighbours #9200, #9097 and #9103: this PR goes second and rebases.
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4959095349 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9442). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-21T22:15:50Z. --- ### What `lchflags` was a throwing stub in libkernel_sys. FreeBSD 11.4 implements it as `chflags` without following a symbolic link in the last component: [`sys_lchflags`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/vfs_syscalls.c#L2358-L2363) calls `kern_chflagsat` with `AT_SYMLINK_NOFOLLOW`, and `kern_chflagsat` only switches `namei` from `FOLLOW` to `NOFOLLOW`. `chflags` in `Stdio.cpp` now goes through `ChangeFileFlags_nid_no_patch`, which takes the same follow choice. When not following, a symbolic link in the last component is the target, so a dangling link is accepted instead of failing the lookup. The link is recognised with `File::FillLinkStat`, the guest `lstat`, because under MinGW `std::filesystem::symlink_status` reports a Windows symbolic link as the file or directory it names. A trailing slash still follows, as in FreeBSD. The flag checks are the existing ones, unchanged: unknown bits `EOPNOTSUPP`, `SF_SNAPSHOT` `EPERM`, any other non-zero flag throws because the host keeps no BSD file flags. `chflags` itself behaves as before. ### Tested Windows 11 (10.0.26200), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release, `-DBUILD_TESTING=ON`, on `main` d6eafa54d0 (rebased), Developer Mode on so the link cases run. - `guest_kernel_sys`: the new `lchflags` checks (a regular file, `EFAULT`, `ENOENT` for empty and missing paths, `ENOTDIR` through a file, `EOPNOTSUPP`, `EPERM`, a dangling link and a working link with flags 0, `EOPNOTSUPP` and `EPERM` on the link, the throw for `UF_IMMUTABLE`) abort on `main` with `lchflags_nid_postfix not implemented` and pass with this PR. - `guest_filesystem` (the existing `chflags` and `fchflags` checks) and `guest_file_timestamps`: pass. - Full suite on `main` d6eafa54d0 and on `main` with #9442, #9469, #9474, #9487 and #9496 merged together, same machine: 1890 tests each, identical results (1869 passed, 18 skipped; on both, the 3 `agc_driver_sample_order*` tests fail and `agc_driver_image_array_window_past_layer_limit` is not built, as its 64 KiB `alignas` is rejected by this MinGW toolchain). - `python tools/check_conventions.py --base upstream/main`: passes. - Linux: built and tested by @jotitadev on the previous head (Ubuntu 26.04, GCC 15.2; `guest_kernel_sys` passes, and fails with `lchflags_nid_postfix not implemented` without the change); see the review. - Not tested: a console, a title run. ### Overlaps - #9423 (merged) made `PathError` copy the guest path; this PR is rebased onto it, so `ChangeFileFlags_nid_no_patch` takes the copied path for both `chflags` and `lchflags`. - #10625 (@Dom3005) adds declarations in the same `GuestKernelSys.cpp` block; agreed on #9442: this PR goes first and #10625 rebases, keeping both. - #9200 (`lchmod`) removes the stub next to `lchflags` in `Export.Discovery.cpp`, and #9097 and #9103 add code next to these lines in `GuestKernelSys.cpp` and `libkernel_sys/Export.cpp`: text-only, whichever lands second rebases. - #6044 moves `GuestKernelSys.cpp` to `tests/process/`; if it lands first, the new cases move with the file in the rebase. - `tools/pr_overlap.py --branch` on the rebased head lists no other direct overlap; of the 22 pull requests it could not compare, none changes `chflags`, `LookupError` or `lchflags`. ### Checklist - [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`); no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source: none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#9423 merged and this PR is rebased onto it). Merge order: before #10625 (agreed with @Dom3005); text-only neighbours #9200, #9097 and #9103: this PR goes second and rebases. - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Gruzzberry commented 2026-10-21 22:52:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-21T22:52:14Z.


Updated: the link check now uses File::FillLinkStat (the guest lstat) instead of std::filesystem::symlink_status. Measured on Windows 11 with MinGW-w64 GCC 15.2.0 r7: symlink_status and status both report a symbolic link, dangling or not, as the regular file or directory it names, so the earlier check was never true on Windows and the dangling-link test only passed because chflags does not follow links there either. With chflags fixed to follow links (separate PR), the earlier version fails the dangling-link check and this one passes. guest_kernel_sys and guest_filesystem pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6328326086 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9442#issuecomment-6328326086). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-21T22:52:14Z. --- Updated: the link check now uses `File::FillLinkStat` (the guest `lstat`) instead of `std::filesystem::symlink_status`. Measured on Windows 11 with MinGW-w64 GCC 15.2.0 r7: `symlink_status` and `status` both report a symbolic link, dangling or not, as the regular file or directory it names, so the earlier check was never true on Windows and the dangling-link test only passed because `chflags` does not follow links there either. With `chflags` fixed to follow links (separate PR), the earlier version fails the dangling-link check and this one passes. `guest_kernel_sys` and `guest_filesystem` pass.
jotitadev commented 2026-10-22 00:06:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T00:06:44Z.

Review: COMMENTED


Linux run of the current change, since the description lists only Windows ("Not tested: Linux").

Machine: Linux x86_64, i7-8700 (6 cores / 12 threads), Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON. Head e5d6ec946 merged onto main a6dcea927 (the merge touches Stdio.cpp, libkernel_sys/Export.Discovery.cpp, libkernel_sys/Export.cpp and GuestKernelSys.cpp).

  • Build ok. guest_kernel_sys passes with the change, including the new lchflags cases (regular file, EFAULT, ENOENT, ENOTDIR, EOPNOTSUPP, EPERM, the dangling and working link cases and the UF_IMMUTABLE throw).
  • Restoring the three implementation files to main and keeping the test aborts guest_kernel_sys:
    terminate called after throwing an instance of 'std::runtime_error'
    what(): lchflags_nid_postfix not implemented
    so the regression reproduces on Linux as well and the test covers the change.
  • The export moves from the discovery stub (Export.Discovery.cpp) to a real entry in Export.cpp; the patched/unpatched libkernel_sys libraries build and the NID/convention tests pass, so the NID patching stays intact.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607719748 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9442#pullrequestreview-5607719748). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-22T00:06:44Z. **Review:** COMMENTED --- Linux run of the current change, since the description lists only Windows ("Not tested: Linux"). Machine: Linux x86_64, i7-8700 (6 cores / 12 threads), Ubuntu 26.04, GCC 15.2, Release, `BUILD_TESTING=ON`. Head `e5d6ec946` merged onto `main` `a6dcea927` (the merge touches `Stdio.cpp`, `libkernel_sys/Export.Discovery.cpp`, `libkernel_sys/Export.cpp` and `GuestKernelSys.cpp`). - Build ok. `guest_kernel_sys` passes with the change, including the new `lchflags` cases (regular file, `EFAULT`, `ENOENT`, `ENOTDIR`, `EOPNOTSUPP`, `EPERM`, the dangling and working link cases and the `UF_IMMUTABLE` throw). - Restoring the three implementation files to `main` and keeping the test aborts `guest_kernel_sys`: `terminate called after throwing an instance of 'std::runtime_error'` `what(): lchflags_nid_postfix not implemented` so the regression reproduces on Linux as well and the test covers the change. - The export moves from the discovery stub (`Export.Discovery.cpp`) to a real entry in `Export.cpp`; the patched/unpatched `libkernel_sys` libraries build and the NID/convention tests pass, so the NID patching stays intact. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-10-22 01:36:09 +00:00 (Migrated from github.com)

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

Review: DISMISSED


I reviewed the lchflags implementation and its source/test context. The cited FreeBSD syscall uses AT_SYMLINK_NOFOLLOW; the helper uses guest lstat so a dangling final-component link is recognized on MinGW, while chflags retains follow behavior. The existing unsupported-flag results are preserved. The author reports Windows tests and an independent reviewer reports Linux build/regression results; hosted eligibility, conventions, labels and report pass, while product/relinker jobs were skipped. I did not run local tests.

The current overlap results need author coordination before merge:

  • #9423 changes the PathError contract and the chflags code in Stdio.cpp; this PR refactors that same chflags implementation and calls the old PathError signature. The branches must be reconciled so checked guest-path copying and the new no-follow helper both remain.
  • #9103 inserts chown/lchown/fchownat exports in libkernel_sys/Export.cpp at the same fstatat/unlink insertion area where this PR adds lchflags.
  • #6044 renames GuestKernelSys.cpp to tests/process/GuestKernelSys.cpp, while this PR adds the regression cases to the old path. Coordinate so the final registered test target includes the new coverage after the move.

Please agree the order with these authors, rebase/compose the changes, and record Depends on for any prerequisite. The present none entries do not document these direct overlaps. The scan also listed #9040, #9097 and #6525 at nearby test lines; I manually compared their hunks and found their helper/call insertions separate from the lchflags additions, so I am not requesting changes for those.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5608267134 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9442#pullrequestreview-5608267134). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T01:36:09Z. **Review:** DISMISSED --- I reviewed the `lchflags` implementation and its source/test context. The cited FreeBSD syscall uses `AT_SYMLINK_NOFOLLOW`; the helper uses guest `lstat` so a dangling final-component link is recognized on MinGW, while `chflags` retains follow behavior. The existing unsupported-flag results are preserved. The author reports Windows tests and an independent reviewer reports Linux build/regression results; hosted eligibility, conventions, labels and report pass, while product/relinker jobs were skipped. I did not run local tests. The current overlap results need author coordination before merge: - #9423 changes the `PathError` contract and the `chflags` code in `Stdio.cpp`; this PR refactors that same `chflags` implementation and calls the old `PathError` signature. The branches must be reconciled so checked guest-path copying and the new no-follow helper both remain. - #9103 inserts `chown`/`lchown`/`fchownat` exports in `libkernel_sys/Export.cpp` at the same `fstatat`/`unlink` insertion area where this PR adds `lchflags`. - #6044 renames `GuestKernelSys.cpp` to `tests/process/GuestKernelSys.cpp`, while this PR adds the regression cases to the old path. Coordinate so the final registered test target includes the new coverage after the move. Please agree the order with these authors, rebase/compose the changes, and record `Depends on` for any prerequisite. The present `none` entries do not document these direct overlaps. The scan also listed #9040, #9097 and #6525 at nearby test lines; I manually compared their hunks and found their helper/call insertions separate from the lchflags additions, so I am not requesting changes for those.
Gruzzberry commented 2026-10-22 06:48:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-22T06:48:59Z.


Rebased onto main d6eafa54d0. guest_kernel_sys and guest_filesystem pass on the new head, the new checks fail on that main without the change, and check_conventions.py passes. Full suite on main d6eafa54d0 and on main with #9442, #9469, #9474, #9487 and #9496 merged together: 1890 tests each, identical results.

Overlaps, now listed in the description under Overlaps: I asked on #9423 to land it first and will then rebase this PR so ChangeFileFlags_nid_no_patch takes the copied path for chflags and lchflags. #9200, #9097 and #9103 are adjacent text only, and the new cases follow GuestKernelSys.cpp if #6044 moves it first. Thanks @jotitadev for the Linux run; it is credited in the description.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6334630272 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9442#issuecomment-6334630272). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-22T06:48:59Z. --- Rebased onto `main` d6eafa54d0. `guest_kernel_sys` and `guest_filesystem` pass on the new head, the new checks fail on that `main` without the change, and `check_conventions.py` passes. Full suite on `main` d6eafa54d0 and on `main` with #9442, #9469, #9474, #9487 and #9496 merged together: 1890 tests each, identical results. Overlaps, now listed in the description under Overlaps: I asked on #9423 to land it first and will then rebase this PR so `ChangeFileFlags_nid_no_patch` takes the copied path for `chflags` and `lchflags`. #9200, #9097 and #9103 are adjacent text only, and the new cases follow `GuestKernelSys.cpp` if #6044 moves it first. Thanks @jotitadev for the Linux run; it is credited in the description.
Gruzzberry commented 2026-10-22 11:40:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-22T11:40:53Z.


Holding this as a draft, following the direction on #5239: the open queue is above 700 and no named title needs this change on current main. I will only mark it ready again with a current-main title ID, its Game bring-up discussion and the log line where it stops on this, or once the queue is below 700. Thanks for the reviews so far; the requested fixes are being made on the branch in the meantime.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339854786 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9442#issuecomment-6339854786). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-22T11:40:53Z. --- Holding this as a draft, following the direction on #5239: the open queue is above 700 and no named title needs this change on current `main`. I will only mark it ready again with a current-main title ID, its Game bring-up discussion and the log line where it stops on this, or once the queue is below 700. Thanks for the reviews so far; the requested fixes are being made on the branch in the meantime.
MarcZX10 commented 2026-10-23 18:43:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T18:43:17Z.

Review: COMMENTED


The implementation and reported Windows/Linux checks look well-supported. One documentation follow-up: please update the existing chflags debt entry in docs/dev/debt/libkernel.md to include lchflags and its no-follow behavior, including that this behavior and the console filesystem/flag handling have not been measured on a PS5. The entry currently documents the same shared flag limitations for chflags/fchflags, but does not name the newly implemented API or its symlink-specific path behavior. No blocking code issue found in this review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5632980921 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9442#pullrequestreview-5632980921). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T18:43:17Z. **Review:** COMMENTED --- The implementation and reported Windows/Linux checks look well-supported. One documentation follow-up: please update the existing `chflags` debt entry in `docs/dev/debt/libkernel.md` to include `lchflags` and its no-follow behavior, including that this behavior and the console filesystem/flag handling have not been measured on a PS5. The entry currently documents the same shared flag limitations for `chflags`/`fchflags`, but does not name the newly implemented API or its symlink-specific path behavior. No blocking code issue found in this review.
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!9442
No description provided.