fix(libkernel_sys): fail readlink through a regular file with ENOTDIR on Windows #8312

Merged
altuux merged 1 commit from upstream-pr/8312 into main 2026-10-20 23:06:43 +00:00
altuux commented 2026-10-20 09:45:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by altuux on 2026-10-20T09:45:28Z.


What

A path whose directory part is a regular file, such as file/child, failed with ENOENT in readlink and readlinkat on Windows, where FreeBSD 11.4 fails it with ENOTDIR (vfs_cache_lookup, sys/kern/vfs_cache.c). The not-found error of ReadNativeLink now checks whether the nearest existing ancestor is not a directory (HasNonDirectoryPrefix), matching the behavior of open, stat, lstat, access, unlink, chmod, truncate, utimes, rename, remove, opendir, and fopen.

Tested

tools/check_conventions.py --base upstream/main and tools/pr_overlap.py --branch fix/libkernel-sys-readlink-notdir passed cleanly. Added assertions in GuestKernelSys.cpp for readlink_nid_postfix and readlinkat_nid_postfix verifying that paths through a regular file return -1 with error 20 (ENOTDIR).

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:4933971740 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8312). Originally posted by [altuux](https://github.com/altuux) on 2026-10-20T09:45:28Z. --- ### What A path whose directory part is a regular file, such as `file/child`, failed with `ENOENT` in `readlink` and `readlinkat` on Windows, where FreeBSD 11.4 fails it with `ENOTDIR` (`vfs_cache_lookup`, `sys/kern/vfs_cache.c`). The not-found error of `ReadNativeLink` now checks whether the nearest existing ancestor is not a directory (`HasNonDirectoryPrefix`), matching the behavior of `open`, `stat`, `lstat`, `access`, `unlink`, `chmod`, `truncate`, `utimes`, `rename`, `remove`, `opendir`, and `fopen`. ### Tested `tools/check_conventions.py --base upstream/main` and `tools/pr_overlap.py --branch fix/libkernel-sys-readlink-notdir` passed cleanly. Added assertions in `GuestKernelSys.cpp` for `readlink_nid_postfix` and `readlinkat_nid_postfix` verifying that paths through a regular file return -1 with error 20 (`ENOTDIR`). ### 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
github-actions[bot] commented 2026-10-20 10:00:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T10:00:11Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel_sys/Export.cpp
PR 5528 core/libs/prx/libkernel_sys/Export.cpp
core/libs/tests/GuestKernelSys.cpp
PR 6044 core/libs/tests/GuestKernelSys.cpp
PR 7201 core/libs/prx/libkernel_sys/Export.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6288388543 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8312#issuecomment-6288388543). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T10:00:11Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel_sys/Export.cpp` | | [PR 5528](https://www.github.com/boykopovar/AnyPS5/pull/5528) | `core/libs/prx/libkernel_sys/Export.cpp`<br>`core/libs/tests/GuestKernelSys.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestKernelSys.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel_sys/Export.cpp` |
xXPBSXx commented 2026-10-20 16:59:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T16:59:39Z.


Windows data point for this PR (not a review of the approach, which matches the other ENOTDIR changes).

Measurement

  • Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release (CMAKE_BUILD_TYPE=Release in the CMakeCache), ninja -j4.
  • This PR's head 7d40d6f420 merged onto main 1b08a33b12 (the merge completes without conflicts).
  • Merged as is, the new test line readlink_nid_postfix((file / "child").string().c_str(), ...) needs adjusting to current main (the variable file is now a std::string): GuestKernelSys.cpp:301:40: error: no match for 'operator/' (operand types are 'const std::__cxx11::basic_string<char>' and 'const char [6]'). I changed only that line to (root / "file.txt" / "child").string().c_str(); nothing else.
  • guest_kernel_sys: Kernel sys check failed at line 301 (guest errno 2), 3 of 3 runs. That is the new readlink line, which expects errno 20.
  • With one extra change on top, in ReadNativeLink (if (error) return Fail(GuestError(error)); becomes if (error) return Fail(error == std::errc::no_such_file_or_directory && HasNonDirectoryPrefix(native) ? GuestEnotdir : GuestError(error));), guest_kernel_sys passes, 3 of 3 runs.
  • Logs (setup and build step with the SHAs and the compiler output, plus the two ctest logs) available on request.
  • Not run: Linux, macOS, a console.

Interpretation (probably, not verified further)

On this Windows host std::filesystem::symlink_status seems to return a no_such_file_or_directory error code for file.txt/child instead of a not_found status. ReadNativeLink then returns at the if (error) line, where GuestError maps that code to ENOENT, before the not_found branch that this PR changes, so the branch is not reached for this path. I did not check other hosts.

Suggestion

Apply the same prefix check in the if (error) branch of ReadNativeLink, and rebase the test line onto the current main. My own ENOTDIR change for link, symlink, pathconf and fchmodat (#8522) leaves readlink and readlinkat out so the two PRs do not overlap (merge-tree of the two branches is clean).

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6296430831 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8312#issuecomment-6296430831). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T16:59:39Z. --- Windows data point for this PR (not a review of the approach, which matches the other ENOTDIR changes). **Measurement** - Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release (`CMAKE_BUILD_TYPE=Release` in the CMakeCache), `ninja -j4`. - This PR's head `7d40d6f420` merged onto `main` `1b08a33b12` (the merge completes without conflicts). - Merged as is, the new test line `readlink_nid_postfix((file / "child").string().c_str(), ...)` needs adjusting to current `main` (the variable `file` is now a `std::string`): `GuestKernelSys.cpp:301:40: error: no match for 'operator/' (operand types are 'const std::__cxx11::basic_string<char>' and 'const char [6]')`. I changed only that line to `(root / "file.txt" / "child").string().c_str()`; nothing else. - `guest_kernel_sys`: `Kernel sys check failed at line 301 (guest errno 2)`, 3 of 3 runs. That is the new `readlink` line, which expects errno 20. - With one extra change on top, in `ReadNativeLink` (`if (error) return Fail(GuestError(error));` becomes `if (error) return Fail(error == std::errc::no_such_file_or_directory && HasNonDirectoryPrefix(native) ? GuestEnotdir : GuestError(error));`), `guest_kernel_sys` passes, 3 of 3 runs. - Logs (setup and build step with the SHAs and the compiler output, plus the two ctest logs) available on request. - Not run: Linux, macOS, a console. **Interpretation (probably, not verified further)** On this Windows host `std::filesystem::symlink_status` seems to return a `no_such_file_or_directory` error code for `file.txt/child` instead of a `not_found` status. `ReadNativeLink` then returns at the `if (error)` line, where `GuestError` maps that code to ENOENT, before the `not_found` branch that this PR changes, so the branch is not reached for this path. I did not check other hosts. **Suggestion** Apply the same prefix check in the `if (error)` branch of `ReadNativeLink`, and rebase the test line onto the current `main`. My own ENOTDIR change for `link`, `symlink`, `pathconf` and `fchmodat` (#8522) leaves `readlink` and `readlinkat` out so the two PRs do not overlap (`merge-tree` of the two branches is clean). AI-assisted (Claude Code).
altuux commented 2026-10-20 18:26:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by altuux on 2026-10-20T18:26:44Z.


Thanks a lot for the data point and testing it on MinGW! Rebased onto current main, adjusted the test path to (root / "file.txt" / "child"), and applied the HasNonDirectoryPrefix check to the if (error) branch as suggested.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6298126577 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8312#issuecomment-6298126577). Originally posted by [altuux](https://github.com/altuux) on 2026-10-20T18:26:44Z. --- Thanks a lot for the data point and testing it on MinGW! Rebased onto current main, adjusted the test path to `(root / "file.txt" / "child")`, and applied the `HasNonDirectoryPrefix` check to the `if (error)` branch as suggested.
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!8312
No description provided.