fix(libkernel): stat a path with one host query #4938
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4938"
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 fdfawef324 on 2026-10-13T05:40:34Z.
What
On Windows,
sceKernelStat(andstat, which calls it) queried each existing path four times:std::filesystem::existsinResolveHostPath(General.cpp). This ran under the working-directory mutex, and also under the alias mutex for an aliased path.std::filesystem::statusinsceKernelStat(Open.cpp)._wstat64inDoStat(NativeStat.cpp).CreateFileWinDoStat.lstatqueried it three times. The queries could disagree.sceKernelStatthrewFillFileStat: stat failedin these cases:statusandDoStat;.(file.txt/.,file.txt\.,fileLink/.).The change:
ResolvePathProbing_nid_no_patch) in place of theexistscheck on the exact host name.ResolvePath_nid_no_patchand the two-argumentResolvePathError_nid_no_patchpassGuestPathExists_nid_no_patch, the former check, so other callers are unchanged.ResolvePathError_nid_no_patchpasses the probe it is given.File::StatGuestPathpasses a probe that does the whole stat:CreateFileW;GetFileType,GetFileInformationByHandleandGetFileInformationByHandleEx(FileBasicInfo)on that handle.sceKernelStatandlstatuseFile::StatGuestPath.sceKernelStattries the relinked.guest.prxname (File::PathOrRelinkedModule, from #4688) only when that query reportsENOENT, so a stat of an existing file stays one query.FillFileStat(path)andfstatof a directory descriptor use the same single-open query. They therefore also throw when a handle query fails, and reportENOENTwhen the open fails.GetFileTyperesult other than a disk file, character device or pipe now throws. The previous revision returnedEBADFthere, whichsceKernelStatfolded intoENOENT.chdirstill resolves under the lock.stat(2)orlstat(2).Paths that name a directory
A guest path that ends in
/or\, or whose last component is.or.., names a directory, as in FreeBSDlookup():statandlstatfollow a final link for it;ENOTDIRwhen the result is not a directory.The check uses the guest path, not the resolved host path. Resolution can drop the trailing separator, because it trims an aliased path. Path resolution itself is unchanged: it still turns
file/..intoENOTDIRandmissing/..intoENOENT, as main does.I probed this with a scratch program outside the repository, on this machine. It ran against main's libraries, the previous revision of this PR, and this revision.
statagreed withsceKernelStatin every row.sceKernelStat/lstatstatandlstatfile.txt/.,file.txt\.,FILE.TXT/.FillFileStat/ENOENT0100666, size 10 /ENOENTENOTDIRfileLink/.,FILELINK/.FillFileStat/ENOENT0100666, size 10 /ENOENTENOTDIRfile.txt/./,file.txt/.//,file.txt/ENOTDIR/ENOENTENOTDIRENOTDIRfile.txt/..,file.txt/../ENOTDIRENOTDIRENOTDIRmissing/.,missing/..,missing/./ENOENTENOENTENOENTFolder/.,Folder/./,FOLDER/.,Folder/..dirLink/.DIRLINK/./Where main threw, the new values follow FreeBSD. A component after a non-directory reports
ENOTDIR, and a.component after a link follows the link.lstatoffile.txt/reportedENOENTon main. It now reportsENOTDIR, as FreeBSD does.Mode and device fields
st_mode,st_rdevand the device and pipe fields keep the rules of the UCRT_wstat64(Windows SDK 10.0.26100ucrt/filesystem/stat.cpp):.exe,.cmd,.bator.comadds the execute bits;st_rdevis the drive number;S_IFCHRorS_IFIFO, withst_devandst_rdev0xffffffff.Times, size, nlink, dev and ino come from the handle, as before. On Windows,
st_blksizeis the constant 512 andst_blocksis computed from the size, as before.Links, and where this differs from
_wstat64Links follow FreeBSD on purpose, not the system
_wstat64. An earlier round of the same scratch probe compared the system_wstat64(with both absolute and relative paths) againstsceKernelStat,statandlstat, on main and on this branch. Each case ran with no suffix, with/and with\._wstat640100666, size 00100666, size 100100666, size 100100666, size 0010066601004440100444, size 001004440100666040555040555040777link.exetofile.txt,plainLinktotool.exe0100777,01006660100666,040777FillFileStatENOENT040777FillFileStatENOENT0100666FillFileStatENOENTfileLink/ENOENTENOTDIRENOTDIRfileLink\,file.txt\ENOENTFillFileStatENOTDIRdanglingFileLink/,dirAsFileLink/ENOENTENOTDIRENOENTroDirLink/,roJunction\040555(the link)040555040777Junction/Inner.txt/,dirLink/Inner.txt/0100666ENOTDIR;lstatreports the fileENOTDIRfromstatandlstatlstat("dirLink/"),lstat("Junction\")lstat("file.txt/"),lstat("fileLink/")ENOENTENOTDIRHow links are handled:
statfollows a final link. It opens a final symbolic link or junction withoutFILE_FLAG_OPEN_REPARSE_POINT, as FreeBSDstat(2)follows it. The file type, the read-only bit and every other field come from the target. Only the execute bits come from the name given, as in_wstat64._wstat64reports the link itself. It takes the path throughGetFileInformationByName(theGetFileInfoByNamefast path instat.cpp). That call reports a final link as itself, with or without a trailing separator: the link's own directory attribute and read-only bit, and size 0. ItsCreateFileWfallback, taken only when that call fails, follows links as this code does.CreateFileWcannot follow reportsENOENT. This covers a dangling link, a directory symbolic link to a file, and a file symbolic link to a directory._wstat64reports the link in these cases.lstatof a path that names a directory follows the final link, as described above.lstatof a link without such a suffix still throwsNotImplemented. #4691 changes the errno around that throw.The other deviations from
_wstat64:CreateFileWcannot open reportsENOENT._wstat64fabricates a directory dated 1980-01-01.ENOTDIR._wstat64reportsENOENT, or the file itself when an earlier component is a link.GetFileTypereports an unexpected type or either handle query fails, the call throws._wstat64returns the mapped error.ENOENT. FreeBSD reports the cause:EACCES,ENAMETOOLONG,ELOOP, orENOTDIRfor a path that continues past a file.Each of these deviations is recorded in the "Windows file identity" entry of
docs/dev/debt/libkernel.md. None of these values has been compared with a console.No speed change is claimed: no title was run and no trace was captured.
Related pull requests
.guest.prxnames) is now in main. This PR is rebased onto it and keeps itsTestRelinkedModuleNames, which passes.lstatrejects a Windows symbolic link)Stdio.cpp(lstat_nid_postfix).File::StatGuestPath.%VAR%mount tokens)General.hppandGeneral.cpp.ExpandPathSegmentruns before the probe, so whichever PR lands second rebases.docs/dev/debt/libkernel.mdhunk.I last checked #4691, #4565 and #4379 for the previous revision. I have not contacted their authors yet, so the order is a proposal until they agree. This PR works without any of them.
Tested
OS: Windows Server 2025, GPU RTX 3060, GCC 15.2 (winlibs UCRT). Not tested with a title.
Linux was not built locally. The Linux branches of the change and of the test are reasoned only. A full Linux CI run needs a collaborator to start it.
guest_filesystemhas three new tests:TestStatFieldscomparessceKernelStatandstatagainstGetFileInformationByHandleandFILE_BASIC_INFO(stat(2)on Linux). It checks mode, size, nlink, ino, dev, rdev, blocks, blksize and all four times for:.EXE;/;040777);0100444).TestStatFieldsalso checkslstat("Junction/")against the directory, and the junctionlstatthrow.TestStatFieldschecks the error code and that the buffer is untouched for:/;/or\(ENOTDIRfrom all three calls);TestStatDotComponentsstarts from the reviewer's reproduction and pins the first table above forsceKernelStat,statandlstat:file.txtwith/.,/./,\.,/.//,/..and/../, plusFILE.TXT/.,FileLink/.andFILELINK/.:ENOTDIR, with the buffer untouched;Missingwith/.,/..and/./:ENOENT;Folder/.,Folder/./,FOLDER/.,Junction/.andjunction/./: the directory's fields, withlstatequal tosceKernelStatofFolder;Folder/..: the parent's fields.file.txtwith/,/.,\.and/./,Missing/.,Folder/.andJunction/.SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE. If that fails, the test prints that the file-link cases were not tested, asGuestKernelSysdoes. Here they ran.TestStatDuringRemovalstats a file while another thread creates and removes it 300 times.Red:
guest_filesystemfails at the firstfile.txt/.check in 2 of 2 runs.sceKernelStatreturns 0 there.TestStatDotComponentsaborts onfile.txt/.withFillFileStat: stat failed. In the committed order,TestStatFieldsaborts first, atlstat("Junction/")(lstat of a Windows symbolic link not implemented).st_mode(main reports040555);lstat("Data.bin/")(ENOENT);Data.bin\and the dangling junction;Green, after the rebase onto current main (
88107b7de):guest_filesystempassed 5 of 5 runs (0.5 s each).ctest --test-dir build-dev/core/libs/prx/libkernel -j 4: 121/121 passed.ctest --test-dir build-dev/core/libs/prx/libc -j 4: 82/83 passed.log_macrosfails the same way on main.tools/check_test_registration.py: 749 test executables, none new.Not rerun this round: the other users of the resolver (AppContent, AvPlayer, Font, LibcInternal, Ngs2, PlayGo, SaveData). Relative to the previous revision, this one does not change libc. The previous review ran them, and they passed.
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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T08:20:57Z.
Windows test of this PR.
Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,
BUILD_TESTING=ON,-j2,libusb-cmakefrom a local checkout. Built frommainc2565f014with this PR and ten other open PRs (#4972, #4967, #4966, #4945, #4938, #4936, #4933, #4898, #4896, #4888) merged together; the merges had no conflict.Result: guest_filesystem (1 of 1, 5.4 s) pass.
Not checked: whether the test fails without the change in the library code; the PR was not tested alone, only in the combined merge; no title run; Linux not tested.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:35:42Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed