fix(libkernel): rewind Windows directory descriptors on lseek #2680

Closed
husseinkad wants to merge 3 commits from upstream-pr/2680 into main
husseinkad commented 2026-10-10 10:50:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T10:50:04Z.


What

On Windows a guest directory descriptor sits on the NUL device, so sceKernelLseek on it did nothing: after lseek(fd, 0, SEEK_SET) a second getdents returned 0 entries instead of listing the directory again. FreeBSD getdirentries(2) documents that the position "may be set and retrieved by lseek(2)", and offset 0 is the standard rewind.

File::SeekDirectoryDescriptor now handles lseek on directory descriptors (hook in sceKernelLseek):

  • offset 0 with SEEK_SET rewinds; the next read lists the live directory again
  • SEEK_CUR returns the entry index (offset 0 is a pure query that does not touch the snapshot)
  • other SEEK_SET/SEEK_CUR offsets are entry indexes; a negative result throws like any other lseek failure (EINVAL), an offset overflow reports EOVERFLOW
  • SEEK_END throws via NotImplemented_nid_no_patch (FreeBSD defines it relative to the directory size, which is not modelled; it used to return 0 from the NUL descriptor)

Per-descriptor state has its own lock, so a slow listing does not block lseek on other descriptors, and lseek/close on non-directory descriptors return before taking any lock while no directory is open. The limits (O(entries) re-listing per rewind, getdirentries still reporting 0 through basep) are recorded in TechnicalDebt.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release:

  • New guest_directory_rewind: rewind after a partial and a full read, a file added between listings, SEEK_CUR exactly 2 after . and .., a seek past the end, negative and overflowing seeks, SEEK_CUR 0 not disturbing a listing, rewind after getdents failed on a too-small buffer, and a reused descriptor that was a directory behaving as a file. It fails without the change.
  • ctest -R "guest_directory|guest_filesystem|guest_file_timestamps": 6 of 6 pass.
  • Outside the repo: a 3-thread, 3 s stress of getdents + directory lseek + regular-file lseek (about 12.6M operations) had 0 errors and no crash; separate probes with 0, 1, 5 and 10,000 entries relisted correctly.
  • tools/check_conventions.py --base origin/main passes; tools/pr_overlap.py --branch reports no overlap. Open PR #2144 also rewrites sceKernelLseek in Open.cpp (error returns instead of throws); the directory hook there would need to follow whichever style lands first.
  • Linux not run (the NUL-descriptor limitation is Windows-only, and the test is registered for Windows).

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
  • 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:4810639125 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2680). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T10:50:04Z. --- ### What On Windows a guest directory descriptor sits on the NUL device, so `sceKernelLseek` on it did nothing: after `lseek(fd, 0, SEEK_SET)` a second `getdents` returned 0 entries instead of listing the directory again. FreeBSD `getdirentries(2)` documents that the position "may be set and retrieved by lseek(2)", and offset 0 is the standard rewind. `File::SeekDirectoryDescriptor` now handles `lseek` on directory descriptors (hook in `sceKernelLseek`): - offset 0 with `SEEK_SET` rewinds; the next read lists the live directory again - `SEEK_CUR` returns the entry index (offset 0 is a pure query that does not touch the snapshot) - other `SEEK_SET`/`SEEK_CUR` offsets are entry indexes; a negative result throws like any other `lseek` failure (`EINVAL`), an offset overflow reports `EOVERFLOW` - `SEEK_END` throws via `NotImplemented_nid_no_patch` (FreeBSD defines it relative to the directory size, which is not modelled; it used to return 0 from the NUL descriptor) Per-descriptor state has its own lock, so a slow listing does not block `lseek` on other descriptors, and `lseek`/`close` on non-directory descriptors return before taking any lock while no directory is open. The limits (O(entries) re-listing per rewind, `getdirentries` still reporting 0 through `basep`) are recorded in TechnicalDebt. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release: - New `guest_directory_rewind`: rewind after a partial and a full read, a file added between listings, `SEEK_CUR` exactly 2 after `.` and `..`, a seek past the end, negative and overflowing seeks, `SEEK_CUR` 0 not disturbing a listing, rewind after `getdents` failed on a too-small buffer, and a reused descriptor that was a directory behaving as a file. It fails without the change. - `ctest -R "guest_directory|guest_filesystem|guest_file_timestamps"`: 6 of 6 pass. - Outside the repo: a 3-thread, 3 s stress of `getdents` + directory `lseek` + regular-file `lseek` (about 12.6M operations) had 0 errors and no crash; separate probes with 0, 1, 5 and 10,000 entries relisted correctly. - `tools/check_conventions.py --base origin/main` passes; `tools/pr_overlap.py --branch` reports no overlap. Open PR #2144 also rewrites `sceKernelLseek` in `Open.cpp` (error returns instead of throws); the directory hook there would need to follow whichever style lands first. - Linux not run (the NUL-descriptor limitation is Windows-only, and the test is registered for Windows). ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [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-10 10:50:19 +00:00 (Migrated from github.com)

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


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096725552 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2680#issuecomment-6096725552). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T10:50:19Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

Pull request closed

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!2680
No description provided.