Function statFile [src]

Returns metadata for a file inside the directory. On Windows, this requires three syscalls. On other operating systems, it only takes one. Symlinks are followed. sub_path may be absolute, in which case self is ignored. On Windows, sub_path should be encoded as WTF-8. On WASI, sub_path should be encoded as valid UTF-8. On other platforms, sub_path is an opaque sequence of bytes with no particular encoding.

Prototype

pub fn statFile(self: Dir, sub_path: []const u8) StatFileError!Stat

Parameters

self: Dirsub_path: []const u8

Possible Errors

AccessDenied OpenError

In WASI, this error may occur when the file descriptor does not hold the required rights to open a new resource relative to it.

AntivirusInterference OpenError

On Windows, antivirus software is enabled by default. It can be disabled, but Windows Update sometimes ignores the user's preference and re-enables it. When enabled, antivirus software on Windows intercepts file system operations and makes them significantly slower in addition to possibly failing with this error code.

BadPathName OpenError

On Windows, file paths cannot contain these characters: '/', '*', '?', '"', '<', '>', '|'

DeviceBusy OpenError
FileBusy OpenError

One of these three things:

  • pathname refers to an executable image which is currently being executed and write access was requested.
  • pathname refers to a file that is currently in use as a swap file, and the O_TRUNC flag was specified.
  • pathname refers to a file that is currently being read by the kernel (e.g., for module/firmware loading), and write access was requested.
FileLocksNotSupported OpenError

The underlying filesystem does not support file locks

FileNotFound OpenError

Either:

  • One of the path components does not exist.
  • Cwd was used, but cwd has been deleted.
  • The path associated with the open directory handle has been deleted.
  • On macOS, multiple processes or threads raced to create the same file with O.EXCL set to false.
FileTooBig OpenError

The file is too large to be opened. This error is unreachable for 64-bit targets, as well as when opening directories.

InvalidUtf8 OpenError

WASI-only; file paths must be valid UTF-8.

InvalidWtf8 OpenError

Windows-only; file paths provided by the user must be valid WTF-8. https://simonsapin.github.io/wtf-8/

IsDir OpenError

The path refers to directory but the DIRECTORY flag was not provided.

NameTooLong OpenError

The path exceeded max_path_bytes bytes.

NetworkNotFound OpenError

On Windows, \\server or \\server\share was not found.

NoDevice OpenError
NoSpaceLeft OpenError

A new path cannot be created because the device has no room for the new file. This error is only reachable when the CREAT flag is provided.

NotDir OpenError

A component used as a directory in the path was not, in fact, a directory, or DIRECTORY was specified and the path was not a directory.

PathAlreadyExists OpenError

The path already exists and the CREAT and EXCL flags were provided.

PermissionDenied OpenError
PipeBusy OpenError
ProcessFdQuotaExceeded OpenError
SharingViolation OpenError
SymLinkLoop OpenError
SystemFdQuotaExceeded OpenError
SystemResources OpenError

Insufficient kernel memory was available, or the named file is a FIFO and per-user hard limit on memory allocation for pipes has been reached.

Unexpected UnexpectedError

The Operating System returned an undocumented error code.

This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.

When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.

WouldBlock OpenError

Source

pub fn statFile(self: Dir, sub_path: []const u8) StatFileError!Stat { if (native_os == .windows) { var file = try self.openFile(sub_path, .{}); defer file.close(); return file.stat(); } if (native_os == .wasi and !builtin.link_libc) { const st = try std.os.fstatat_wasi(self.fd, sub_path, .{ .SYMLINK_FOLLOW = true }); return Stat.fromWasi(st); } if (native_os == .linux) { const sub_path_c = try posix.toPosixPath(sub_path); var stx = std.mem.zeroes(linux.Statx); const rc = linux.statx( self.fd, &sub_path_c, linux.AT.NO_AUTOMOUNT, linux.STATX_TYPE | linux.STATX_MODE | linux.STATX_ATIME | linux.STATX_MTIME | linux.STATX_CTIME, &stx, ); return switch (linux.E.init(rc)) { .SUCCESS => Stat.fromLinux(stx), .ACCES => error.AccessDenied, .BADF => unreachable, .FAULT => unreachable, .INVAL => unreachable, .LOOP => error.SymLinkLoop, .NAMETOOLONG => unreachable, // Handled by posix.toPosixPath() above. .NOENT, .NOTDIR => error.FileNotFound, .NOMEM => error.SystemResources, else => |err| posix.unexpectedErrno(err), }; } const st = try posix.fstatat(self.fd, sub_path, 0); return Stat.fromPosix(st); }