The EINVAL for a descriptor without a vnode now applies only to a pipe or socket. A character device is a vnode on FreeBSD, so getvnode succeeds and change_dir fails with ENOTDIR; File::IsCharacterDevice keeps that answer for the host character devices (a tty, NUL or /dev/null) and the guest random device. guest_kernel_sys pins it with /dev/random.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
FreeBSD 11.4's vn_seek fails SEEK_CUR and SEEK_END with EOVERFLOW when the base offset plus a positive offset does not fit in an off_t, and with EINVAL only when the result is negative. The host lseek reports EINVAL for the overflow, so the guest saw EINVAL. When the host call fails with EINVAL for a positive offset on SEEK_CUR or SEEK_END, the base position is read back and the call fails with EOVERFLOW if the sum would overflow; the file position is left unchanged.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
On Linux sceKernelGetdirentries copied the host d_type, so a directory under app0 that is a host symlink was listed as a link, and a title that only descends into directory entries skipped it. Inside the app0 tree, report a symlink (or an entry of unknown type) as the type of its target; links elsewhere, and dangling links, stay links.
FreeBSD 11 fopen opens the path with _open, and vn_open_vnode refuses a
directory only for FWRITE or O_TRUNC, so fopen of a directory in a read
mode succeeds; Linux hosts already open it. On Windows CreateFileW
without FILE_FLAG_BACKUP_SEMANTICS refused it with EACCES.
fopen now opens such a path as a libkernel directory descriptor, which
libkernel registers in libc when it loads, and fclose and freopen remove
it from libkernel's table. Reads on the stream fail with EISDIR and set
the error indicator, as on a Linux host and FreeBSD's tmpfs: the Windows
CRT fails them already but reports EINVAL. Writes fail with EBADF, and
fseek, ftell and ungetc work, as on any read-only stream.
FreeBSD 11.4's sys_flock only requires a vnode descriptor, so flock on a directory descriptor succeeds. On Windows the call reached LockFileEx, which rejects a directory handle with ERROR_INVALID_PARAMETER, and flock_nid_postfix turned that into a runtime_error that aborted the process. A directory descriptor now returns 0 without taking a lock; the new guest_file_lock_directory case covers it and the debt entry records that no lock is held.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
FreeBSD lookup() refuses "." and ".." as the last component of a DELETE
lookup with EINVAL (sys/kern/vfs_lookup.c:750-754 and 823-826), before the
unlink and rmdir checks run. unlink and sceKernelUnlink answered EPERM,
rmdir and sceKernelRmdir answered ENOTEMPTY for "..", and unlinkat without
AT_REMOVEDIR answered EPERM. Reuse the RenameDot check added for rename, so a
missing or non-directory parent still reports ENOENT or ENOTDIR first.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
When the native open failed with an error other than ENOENT, OpenNative
probed whether the path is a directory with std::filesystem::is_directory.
With the MinGW runtime that probe can set errno to ENOENT, and OpenNative
then reported the probe's ENOENT instead of the open error. On Windows 11
22621 a symbolic link loop failed with ELOOP from the native open and
sceKernelOpen returned 0x80020002, so guest_filesystem aborted at the
ELOOP check added by #8640.
Keep the open error and restore it when the path is not a directory.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A null buffer, or for pread and pwrite a negative offset, was answered with
EFAULT or EINVAL before the descriptor was looked at, so a descriptor that is not
open (EBADF) or a pipe (ESPIPE) was hidden. FreeBSD 11.4 sys_generic.c looks
the descriptor up first (kern_readv, kern_preadv: fget, then the seekable
check, then the offset) and only reaches the buffer when it copies. The check
runs only when the buffer or offset is invalid, so a normal transfer costs nothing.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The Windows sceKernelGetdirentries stored 0 in *basep on every read, so a
guest looping on the cookie saw it stall (FreeBSD stores the directory
offset the read started at, kern_getdirentries: *basep = loff). Report the
index of the first entry of the read, the same unit lseek takes on these
descriptors, and run guest_directory_entries on Windows, where it was never
built.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
On Windows, utimes, lutimes, sceKernelUtimes and futimes open a
FILE_WRITE_ATTRIBUTES handle and only then encode the times. A time the
host cannot represent returned EINVAL without calling SetFileTime, and
with NTFS last-access updates enabled closing that handle set the file's
access time to the current time, so a rejected call still changed the
file and guest_file_timestamps failed on "access time preserves
microseconds". A rejected time now marks the handle with 0xFFFFFFFF,
which SetFileTime documents as preventing operations on the handle from
updating the times, before the handle is closed. Error order is
unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lstat and readlink threw NotImplemented for every Windows reparse point
other than a symbolic link or a drive junction. A tag without the name
surrogate bit (cloud files, deduplication) does not stand for another path,
so lstat now answers as stat does and readlink returns EINVAL, as FreeBSD
does for a file that is not a link and as CPython's os.lstat and os.readlink
do (bpo-37834). A name surrogate that is neither a symbolic link nor a
junction (a mounted volume, a WSL link) is reported as the reparse point
itself with the mode of its attributes, and readlink returns EINVAL.
The tests set reparse tags with FSCTL_SET_REPARSE_POINT, one without the
name surrogate bit and one with it. A compact /exe (WOF) file cannot serve
as the fixture: on Windows 11 24H2 the WOF filter hides the reparse point
from GetFileAttributesW and FileAttributeTagInfo alike. Windows refuses
every open of an unhandled tag without FILE_FLAG_OPEN_REPARSE_POINT, so the
test requires lstat to agree with stat rather than to succeed.
mashingaan's boykopovar/AnyPS5#6447 returned EINVAL for other reparse
points without the tag check.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
lseek on a descriptor that is not open with an invalid whence answered EINVAL.
FreeBSD 11.4 kern_lseek looks the descriptor up first and only then lets the
file's seek operation judge the whence, so the answer is EBADF.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
sceKernelTruncate's status query fails with too_many_symbolic_link_levels for a link loop and filename_too_long for an over-long name, and the call answered ENOENT for both. It now returns ELOOP or ENAMETOOLONG through PathLookupFailure, and truncate sets the same errno.
#6648 taught open, unlink and stat to report a symbolic link loop and an over-long name, but sceKernelMkdir and sceKernelRmdir still lost them: their std::filesystem queries fail with filename_too_long or too_many_symbolic_link_levels and the code went on to ENOENT, EIO or ENOTDIR. A PathLookupFailure check after those queries now returns the guest error (ELOOP 62, ENAMETOOLONG 63) as FreeBSD's namei does, and the POSIX wrappers set the same errno. guest_path_errors covers the open, unlink, stat, rename, mkdir and rmdir paths with a self-referencing link, a two-link cycle, a 300-byte name and each as a path prefix; it is UNIX-only because it creates symbolic links.
FreeBSD 11.4 kern_renameat looks up the source first (namei on fromnd),
so a missing or dot source fails with ENOENT or EINVAL before the new
name is touched. The guest call rejected a NULL, empty or dot destination
first, so rename(missing, NULL) returned EFAULT and rename(missing, ".")
returned EINVAL where FreeBSD returns ENOENT.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Host logging opens its own FILE on the saved descriptor. The guest FILE stays on the replaced descriptor, so guest printf, puts and fprintf still reach the pipe.
readdir and readdir_r returned d_fileno 0 for every entry on Windows,
because MinGW's _wreaddir reports no inode. FreeBSD 11.4's
_readdir_unlocked (lib/libc/gen/readdir.c) never returns an entry whose
d_ino is 0, and d_fileno is the number lstat reports as st_ino.
The Windows directory stream now reads the listing from the directory
handle with GetFileInformationByHandleEx(FileIdBothDirectoryInfo), so
each entry carries the file ID of the entry itself, symbolic links
included, without opening it. The ID is folded as stat and lstat fold
nFileIndexHigh:nFileIndexLow; the fold moves to WindowsDescriptor.hpp so
both libraries use it. rewinddir restarts the scan, telldir and seekdir
count entries as _wtelldir and _wseekdir did, and opendir fails with the
open error of the directory. A file system that lists no file ID gets a
nonzero number hashed from the name. Linux truncates the host inode to
32 bits as stat does instead of reporting 0 above 2^32.
guest_directory checks that every entry has a nonzero file number, that
the numbers of a file, a subdirectory and symbolic links to both equal
lstat's st_ino, that an entry created or removed while the directory is
open shows up after rewinddir, that a delete-pending entry no lstat can
open is still listed, and that seekdir returns the same number.