libkernel: lseek on a directory descriptor does nothing on Windows, so a directory cannot be listed again after rewinding it #3102
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T22:12:44Z.
What
On Windows a guest directory descriptor sits on the NUL device, so
sceKernelLseekon it returns 0 and does nothing: afterlseek(fd, 0, SEEK_SET)a secondgetdentsreturns 0 entries instead of listing the directory again. FreeBSD'sgetdirentries(2)documents that the position "may be set and retrieved by lseek(2)", and offset 0 is the standard rewind (it is whatrewinddirdoes). A title that walks a directory twice with one descriptor sees an empty second listing. Linux hosts use the reallseekand are fine.Reproduce
Windows 11, WinLibs GCC 15.2.0 r7,
mainat the time of writing: open a directory with a few files, read it withsceKernelGetdentsuntil it returns 0, callsceKernelLseek(fd, 0, SEEK_SET), callsceKernelGetdentsagain: 0 bytes (FreeBSD: the full listing again).Fix
A tested fix is on my fork:
husseinkad:fix/kernel-windows-directory-rewind(three commits onmain; PR #2680, closed by the intake check):File::SeekDirectoryDescriptorhandleslseekon directory descriptors. Offset 0 withSEEK_SETrewinds and the next read lists the live directory again;SEEK_CURreturns the entry index (offset 0 is a pure query that does not touch the snapshot); otherSEEK_SET/SEEK_CURoffsets are entry indexes; a negative result throws like any otherlseekfailure (EINVAL) and an overflow reportsEOVERFLOW;SEEK_ENDthrows viaNotImplemented_nid_no_patch(FreeBSD defines it relative to the directory size, which is not modelled; it used to return the NUL descriptor's 0). Per-descriptor state has its own lock, so a slow listing does not blocklseekon other descriptors, andlseek/closeon non-directory descriptors return before taking any lock while no directory is open. The limits (re-listing costs O(entries) per rewind,getdirentriesstill reporting 0 throughbasep) are in TechnicalDebt. Open PR #2144 also rewritessceKernelLseekinOpen.cpp; the directory hook there would need to follow whichever style lands first.guest_directory_rewindcovers rewind after a partial and a full read, a file added between listings,SEEK_CURexactly 2 after.and.., a seek past the end, negative and overflowing seeks,SEEK_CUR0 not disturbing a listing, rewind aftergetdentsfailed on a too-small buffer, and a reused descriptor behaving as a file; it fails without the change. Probes with 0, 1, 5 and 10,000 entries relisted correctly, and a 3-thread, 3-second stress ofgetdents+ directorylseek+ regular-filelseek(about 12.6 million operations) had no errors. Linux not run.AI-assisted: yes.