libkernel: read on a directory descriptor returns 0 on Windows where the Linux build fails with EISDIR; fix branch ready #4912
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 DavidSamir on 2026-10-13T03:47:47Z.
On Windows,
read,sceKernelRead,readvandsceKernelReadvon a directory descriptor return 0, the end of a file, andpread/preadvon it fail withESPIPE. The Linux build fails all of them withEISDIR, the error FreeBSD's read(2) documents for a directory that cannot be read as a file. Code that opens a path and reads it, and that tells a directory from a file by that error, sees an empty file on Windows.Cause. A directory descriptor on Windows is a descriptor on the
NULdevice with the directory's state kept beside it (OpenDirectoryDescriptor).getdents,lseek,fstatandfsynclook that state up; the read calls do not, so they readNUL.Observed on
main495bf91a0, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, on a descriptor fromsceKernelOpen(directory, O_RDONLY)(the same withO_DIRECTORY):sceKernelRead(fd, buffer, 8)0x80020015(EISDIR)read(fd, buffer, 8)sceKernelReadv,readv0x80020015; -1, errno 21sceKernelPread,sceKernelPreadv,preadv0x8002001D(ESPIPE); -1, errno 290x80020015; -1, errno 21sceKernelPwrite(fd, "x", 1, 0)0x8002001D0x80020009(EBADF)I have not found a title that stops on this. It came out of running one probe of the file functions on the Windows build and on the Linux build and comparing the two outputs.
Fix, ready to cherry-pick.
DavidSamir:fix/libkernel-read-directory-descriptor-windows(d7ef271ef, one commit onmain495bf91a0, three files, +23/-2). I can't open it as a PR while #4532 is open (one open PR without a merged one).Windows only, three places, each using the existing
File::DirectoryDescriptorPath:NativeRead(read,sceKernelRead): when_readreturns 0 and the descriptor is a directory, the result isEISDIR. The lookup is made only on a read that returned nothing, so a read that returns data costs what it costs today.NativePositioned(pread,pwrite, their vector forms and AIO): where a descriptor that is not a disk file already fails withESPIPE, a directory now fails withEISDIRon a read andEBADFon a write, as on Linux.TransferIovecs(readv): a directory fails withEISDIRbefore anything is read.Nothing is called a second time;
getdents,lseek,fstatandfsyncon the descriptor are not touched.Not changed:
ftruncateon a directory descriptor returns 0 on Windows (a separate fix follows), andreadof zero bytes on the write end of a pipe returns 0 where Linux fails withEBADF.guest_filesystemgains checks on a directory descriptor:readmust fail with errno 21,sceKernelRead,sceKernelReadv,sceKernelPreadandsceKernelPreadvmust return0x80020015,sceKernelPwrite0x80020009, andsceKernelGetdentsmust still list the directory afterwards.Checked on Windows (d7ef271ef on
main495bf91a0, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):Stdio.cppandOpen.cppas onmainand the checks kept,guest_filesystemfails at the first of them (Filesystem check failed at line 613). With the fix it passes 5/5 (--repeat until-fail:5), andguest_file_vector,guest_poll,guest_dup,guest_standard_streams,file_streams,guest_aio,guest_kernel_fcntl,guest_directory_rewindandguest_fdopendirpass 3/3.O_DIRECTORY, both ends of a pipe, a file opened read-write and read-only, and/dev/urandom, withmain's libraries, the branch's and on the Linux build. Againstmainonly the directory lines change, to the Linux results in the table;getdentsbefore and after a rewind,fstat, the pipe, the files and/dev/urandomare identical. One directory line still differs from Linux apart fromftruncate: a read through adupof the descriptor givesEISDIRon the branch andEINVALon Linux.readvand 33 over non-blocking pipes, run with both sets of libraries: two lines differ in the first,readandpreadon a directory; none in the others.readvon a file 4.2 to 4.4 µs withmain's libraries and 4.3 to 4.4 µs with the branch's; write three bytes to a pipe andreadvthem 1.17 to 1.25 µs and 1.18 to 1.21 µs. No difference I can measure.poll: POLLPRI is not supported by WSAPollin both; its number of stderr lines varies from run to run (32 to 46 in four runs withmain's libraries, 35 to 36 in four with the branch's).ctest --parallel 4: 1023 of 1023 pass (7 skipped, no display).tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports no overlap with open pull requests.Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
guest_filesystempasses 3/3, so the new checks match the existing Linux behaviour. Only that target andlibswere built there.Not checked on a console or with a commercial title.
AI-assisted: yes