Skip to content

feat(libkernel): support SEEK_DATA and SEEK_HOLE in lseek - #61

Open
Thimo188 wants to merge 3 commits into
mainfrom
upstream-pr/6936
Open

Thimo188 wants to merge 3 commits into
mainfrom
upstream-pr/6936

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#6936, originally opened by AlexOfTheForest.

Source commit: f06949da78cf801f47702e27f0dd987f4b80e184. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

FreeBSD 11.4's lseek accepts SEEK_DATA (3) and SEEK_HOLE (4); AnyPS5 rejected them, sceKernelLseek threw on any unknown whence, and lseek returned EINVAL before looking at the descriptor. Three commits:

  • fix(libkernel): an unknown whence fails as in FreeBSD's kern_lseek. The file is fetched first, so a closed descriptor is EBADF and a pipe ESPIPE whatever the whence. vn_seek rejects the unknown whence with EINVAL. sceKernelLseek no longer throws.
  • feat(libkernel): SEEK_DATA/SEEK_HOLE as vn_seek and vn_bmap_seekhole define them:
    • Regular file: an offset at or past the end is ENXIO. Inside the file, SEEK_DATA finds the next data and SEEK_HOLE the next hole, with the end of the file counting as one.
    • Directory: ENOTTY. Pipe: ESPIPE.
    • Hosts: Linux and macOS ask the host for the same regions. Windows reports no holes, so SEEK_HOLE returns the file size.
    • A negative offset, shared memory and devices throw, as the source does not settle them.
  • feat(libkernel): the same on app0.zar descriptors, which have no holes.

The logic lives in File/src/Seek.cpp. The console's filesystems are unknown (one without FIOSEEKDATA would answer ENOTTY), so this is recorded in docs/dev/debt/libkernel.md. GuestPosixScalarIo expected EINVAL for whence 3; it now checks an unknown whence (5) instead.

Tested

Linux x86-64, GCC.

  • New test guest_lseek_whence:
    • unknown whence: EINVAL, EBADF and ESPIPE, through both sceKernelLseek and lseek;
    • SEEK_DATA/SEEK_HOLE inside a file, at its end and past it, and on an empty file, a directory and a pipe;
    • the position is unchanged after an error, and negative offsets throw.
  • guest_kernel_app_archive covers archived files. guest_posix_scalar_io, guest_filesystem and the archive tests pass.
  • Each commit was built and tested on its own.
  • Not tested on Windows or macOS.

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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

…of throwing

sceKernelLseek threw on a whence other than SET, CUR and END, and lseek returned EINVAL before looking at the descriptor. FreeBSD 11.4's kern_lseek fetches the file first and dispatches to its fo_seek: a closed descriptor is EBADF, a pipe ESPIPE whatever the whence, and vn_seek rejects the unknown whence with EINVAL. Both calls now resolve the descriptor before the whence.
FreeBSD 11.4 accepts whence 3 and 4: vn_seek forwards them to FIOSEEKDATA and FIOSEEKHOLE, which UFS answers through vn_bmap_seekhole. An offset at or past the end of a regular file is ENXIO, a directory ENOTTY and a pipe ESPIPE; inside the file SEEK_DATA finds the next data and SEEK_HOLE the next hole, the end of the file counting as one. Linux and macOS ask the host for the same regions; Windows has no holes to report, so SEEK_DATA keeps the offset and SEEK_HOLE moves to the size. A negative offset, shared memory and devices throw, as the source does not settle them.
An archived descriptor fell through to AppArchiveDescriptorSeek, which throws on any whence but SET, CUR and END. An archived file has no holes: SEEK_DATA keeps an offset inside the file, SEEK_HOLE moves to its size and an offset at or past the end is ENXIO, as for a regular file.
@Thimo188 Thimo188 closed this Dec 2, 2026
@Thimo188 Thimo188 reopened this Dec 2, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants