fix(libc): report the file number of each readdir entry on Windows #8116
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8116"
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 dantdmdarealguy on 2026-10-19T22:28:38Z.
What
On Windows,
readdirandreaddir_rreturnedd_fileno0 for every entry.Directory.cpptook the number from MinGW's_wreaddir, which never reports an inode. FreeBSD never hands out such an entry:_readdir_unlockedskips every record whosed_inois 0 (lib/libc/gen/readdir.cL83, release/11.4.0), and bothreaddirandreaddir_rcall it withskipset (L98, L102, L116, L120). A returnedd_filenois the entry's file number, the same numberlstatreports asst_ino(d_filenois 32 bits in FreeBSD 11,sys/sys/dirent.hL51). Linux already reported the host inode._wopendir/_wreaddir. It opens the directory once and reads the listing from that handle withGetFileInformationByHandleEx(FileIdBothDirectoryInfo), which gives each entry'sFileIdwithout opening the entry. A symbolic link'sFileIdis the link's own, so it matcheslstat.FileIdis thenFileIndexHigh:nFileIndexLowthatstatandlstatalready fold intost_ino(NativeStat.cpp). The fold moves intoWindowsDescriptor.hpp(WindowsFileNumber_nid_no_patch), so libc and libkernel share one definition andd_filenoequalsst_ino.readdircannot fail or throw because of one entry. An entry thatCreateFileWcannot open (a delete-pending file,C:\bootTel.dat,C:\Config.Msi) is listed with its number, as before.rewinddirrestarts the scan (FileIdBothDirectoryRestartInfo), so entries created or removed sinceopendirshow up or disappear after it, as POSIX specifies.telldirreturns the number of entries read andseekdirrestarts and skips that many, as MinGW's_wtelldir/_wseekdirdid. Thefdopendirpath keeps skipping entries by count.FileIdis 0, or the volume rejects the class, in which case the stream falls back toFileFullDirectoryInfo), the entry gets a nonzero number hashed from its name and the directory's file index. That number differs fromst_ino, so it is recorded indocs/dev/debt/libc.md. NTFS lists an ID for every entry.opendirnow fails with the error from opening the directory (for exampleEACCESwithout list permission), where MinGW returned a stream that listed nothing.statalready does, sod_filenoandst_inoagree there too.guest_directorynow checks: every entry has a nonzero file number; the numbers of a file, a subdirectory, and symbolic links to each equallstat'sst_ino; an entry created while the directory is open appears afterrewinddirwith itslstatnumber, and is gone after removal andrewinddir; a delete-pending entry, whichlstatcannot open, is still listed and does not stopreaddir; andseekdirreturns the same number. The test links libkernel forlstat, asguest_path_casealready does. It fails onmain(d_fileno0) and on the first version of this PR (readdirthrows on the delete-pending entry), and passes with the change.readdirover a directory of 10,000 empty files on NTFS (ms per fullopendir/readdir/closedirpass, same machine; each run takes the median of its passes):main587bb343eThe two batches differ because each pass on
mainand on this version fluctuates between about 290 and 700 ms, which comes from thed_typelookup. Both versions spend most of that time in the existingd_typelookup (std::filesystem::symlink_status, a_wstat64per entry). Measured on its own over the same 10,000 entries, listing them takes 1.2 ms withFileIdBothDirectoryInfoand 1.8 ms withFindFirstFileW/FindNextFileW. Opening each entry (CreateFileW+GetFileInformationByHandle+CloseHandle), the step the first version added, takes about 280 ms, andsymlink_statustakes 280 to 670 ms.Tested
Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1714), WinLibs GCC 15.2.0, Release,
BUILD_TESTING=ON:guest_directorywith the new checks, the same test executable run against each build'slibc.prx/libkernel.prx: aborts onmain(Require(fileNumber != 0)). On 1ece463fd it aborts withreaddir: cannot open directory entry ...\pending.txt, error=5. It passes with the change, symbolic link checks included (Developer Mode is on).readdirofC:\: 98 entries, all numbered 0 onmain; 1ece463fd throwsreaddir: cannot open directory entry C:/bootTel.dat, error=5; 98 entries, all nonzero, with the change.--target libs: both succeed, with no warnings in the changed files.ctest -R "guest_directory|guest_fdopendir|guest_filesystem|guest_file_timestamps|guest_path_case|guest_realpath|guest_statvfs|patched_libraries": all 11 tests pass.guest_app_archive*,guest_kernel_app_archive*andguest_fdopendir: all 8 pass.tools/check_test_registration.pyandtools/check_conventions.py --base origin/mainpass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T22:38:10Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/debt/libc.mdL93-94Some 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.
core/libs/prx/libc/CMakeLists.txtcore/libs/prx/libkernel/File/src/NativeStat.cppdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/tests/GuestDirectory.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtcore/libs/prx/libkernel/File/src/NativeStat.cppdocs/dev/debt/libc.mdcore/libs/prx/libc/CMakeLists.txtdocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T05:00:50Z.
Thanks. Making
d_filenomatchst_inois right, and the Linux side now agrees withstattoo. One concern before merge:ReadFileNumberopens every directory entry (CreateFileW+GetFileInformationByHandle+CloseHandle) on eachreaddir. Main did no per-entry open, so a directory with thousands of entries now pays thousands of handle opens, and file-system filters such as Defender make each open more expensive. A failed open other than not-found (for example access denied on a protected entry) also throws out ofreaddir, where main returned the entry.Windows can return the IDs for the whole directory without opening the entries:
GetFileInformationByHandleEx(directoryHandle, FileIdBothDirectoryInfo, ...)(orFileIdExtdDirectoryInfo) givesFileIdper entry. Folding it asWindowsFileNumber_nid_no_patchdoes now (High:Low =FileId) keeps it equal tost_ino.Could you either switch to that, or post main-vs-PR
readdirtimings on Windows for a directory of about 10k files? And please don't abortreaddirwhen a single entry can't be opened: skip it only when it's gone, otherwise fall back to a stable nonzero number.Imported from boykopovar/AnyPS5. Originally posted by dantdmdarealguy on 2026-10-20T08:57:25Z.
Thanks, switched to the directory handle.
readdirnow reads the listing withGetFileInformationByHandleEx(FileIdBothDirectoryInfo)(restart class onopendir/rewinddir) and foldsFileIdwithWindowsFileNumber_nid_no_patch, so nothing is opened per entry. On NTFS the listedFileIdequals thenFileIndexHigh:nFileIndexLowof the entry opened withFILE_FLAG_OPEN_REPARSE_POINT; I checked.,.., files, directories, and file and directory symlinks.FileIdExtdDirectoryInfowasn't needed to matchst_ino.Since no entry is opened, a single entry can no longer abort
readdir. Your access-denied case reproduces on this machine:readdir("C:/")on the previous head threw onC:\bootTel.dat(error 5), and a delete-pending file throws the same way. Both are now listed with theirFileId.guest_directorycovers the delete-pending case, plus symlink numbers, entries created or removed while the directory is open (visible afterrewinddir), andseekdir. If a file system lists no ID (FileId0, or it rejects the class, in which case it falls back toFileFullDirectoryInfo), the entry gets a stable nonzero hash of its name and the directory's index. That case is listed indebt/libc.md.Timings for 10,000 empty files on NTFS (i9-14900K, Windows 11, ms per full
opendir/readdir/closedirpass):One correction on main: it already opens each entry once, in the
d_typelookup (std::filesystem::symlink_status→_wstat64), which is why main sits at 290 to 700 ms rather than near zero. Listing 10,000 entries alone takes 1.2 ms withFileIdBothDirectoryInfo, against 1.8 ms withFindFirstFileW. The previous head added a second open (~280 ms per 10k), and that's gone now. Takingd_typefrom the listing's attributes would remove the remaining per-entry stat and fix symlinks reporting their target's type (libstdc++'ssymlink_statusfollows links on Windows). That's a separate change, so I'll leave it for a follow-up.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T11:29:38Z.
Head
251ca1c10merged ontomain02fb1ce53(clean merge).Windows 11 (10.0.26300), Ryzen 5 9600X, WinLibs GCC 15.2.0 UCRT, Release,
BUILD_TESTING=ON:ctest -R "^(guest_directory|guest_fdopendir|guest_filesystem|guest_rename_dot)": 7 of 7 pass, 3 runs out of 3. The build log has no warnings.Directory.cpp,WindowsDescriptor.hppandNativeStat.cppreverted tomain,guest_directoryfails (exit0xc0000409).opendir/readdirthroughlibc.prxandlstatthroughlibkernel.prx, and setserrnoto 0 before eachreaddir:d_fileno0lstatst_inolstatfailederrnoat end of streamC:/(NTFS)C:/Windows/System32(NTFS)\\wsl.localhost\Ubuntu, mapped as a drive), 3 files and a subdirectory..differs fromlstat(d_fileno3590321765,st_ino1262401), the case the new debt line describes.., the files and the subdirectory match.lstatof a symbolic link on that 9P share throwsstd::system_error(lstat: FSCTL_GET_REPARSE_POINT: Incorrect function) out oflibkernel.prx, where it should return an error. The throw is inmain'sWindowsSymbolicLink.hpp, which this PR does not touch.Ubuntu 24.04 (WSL2), GCC 13.3, Release,
BUILD_TESTING=ON, same merge:guest_directory,guest_fdopendirandguest_filesystempass, 3 runs out of 3. The new symbolic-link andlstatchecks run there and pass. The one build warning is the existing-Wformat-overflowinOpen.cpp, which this PR does not touch. As expected, the checks also pass with the sources reverted on Linux, because the host inode was already reported there.AI-assisted: yes (Claude Code); results above were run on this machine.