DoxigAlpha

openFileZ

Same as openFile but the path parameter is null-terminated.

Function parameters

Parameters

#
sub_path:[*:0]const u8
flags:File.OpenFlags

Type definitions in this namespace

Types

#
SymLinkFlags
Use with `Dir.symLink`, `Dir.atomicSymLink`, and `symLinkAbsolute` to

Functions in this namespace

Functions

#
iterateAssumeFirstIteration
Like `iterate`, but will not reset the directory cursor before the first
walk
Recursively iterates over a directory.
openFile
Opens a file for reading or writing, without attempting to create a new file.
openFileZ
Same as `openFile` but the path parameter is null-terminated.
openFileW
Same as `openFile` but Windows-only and the path parameter is
createFile
Creates, opens, or overwrites a file with write access.
createFileZ
Same as `createFile` but the path parameter is null-terminated.
createFileW
Same as `createFile` but Windows-only and the path parameter is
makeDir
Creates a single directory with a relative or absolute path.
makeDirZ
Same as `makeDir`, but `sub_path` is null-terminated.
makeDirW
Creates a single directory with a relative or absolute null-terminated WTF-16 LE-encoded path.
makePath
Calls makeDir iteratively to make an entire path
makePathStatus
Same as `makePath` except returns whether the path already existed or was successfully created.
makeOpenPath
This function performs `makePath`, followed by `openDir`.
realpath
This function returns the canonicalized absolute pathname of
realpathZ
Same as `Dir.realpath` except `pathname` is null-terminated.
realpathW
Windows-only.
realpathAlloc
Same as `Dir.realpath` except caller must free the returned memory.
setAsCwd
Changes the current working directory to the open directory handle.
openDir
Opens a directory at the given path.
openDirZ
Same as `openDir` except the parameter is null-terminated.
openDirW
Same as `openDir` except the path parameter is WTF-16 LE encoded, NT-prefixed.
deleteFile
Delete a file name and possibly the file it refers to, based on an open directory handle.
deleteFileZ
Same as `deleteFile` except the parameter is null-terminated.
deleteFileW
Same as `deleteFile` except the parameter is WTF-16 LE encoded.
deleteDir
Returns `error.DirNotEmpty` if the directory is not empty.
deleteDirZ
Same as `deleteDir` except the parameter is null-terminated.
deleteDirW
Same as `deleteDir` except the parameter is WTF16LE, NT prefixed.
rename
Change the name or location of a file or directory.
renameZ
Same as `rename` except the parameters are null-terminated.
renameW
Same as `rename` except the parameters are WTF16LE, NT prefixed.
symLink
Creates a symbolic link named `sym_link_path` which contains the string `target_path`.
symLinkWasi
WASI-only.
symLinkZ
Same as `symLink`, except the pathname parameters are null-terminated.
symLinkW
Windows-only.
atomicSymLink
Same as `symLink`, except tries to create the symbolic link until it
readLink
Read value of a symbolic link.
readLinkWasi
WASI-only.
readLinkZ
Same as `readLink`, except the `sub_path_c` parameter is null-terminated.
readLinkW
Windows-only.
readFile
Read all of file contents using a preallocated buffer.
readFileAlloc
On success, caller owns returned buffer.
readFileAllocOptions
On success, caller owns returned buffer.
deleteTree
Whether `sub_path` describes a symlink, file, or directory, this function
deleteTreeMinStackSize
Like `deleteTree`, but only keeps one `Iterator` active at a time to minimize the function's stack size.
writeFile
Writes content to the file system, using the file creation flags provided.
access
Test accessing `sub_path`.
accessZ
Same as `access` except the path parameter is null-terminated.
accessW
Same as `access` except asserts the target OS is Windows and the path parameter is
updateFile
Check the file size, mtime, and mode of `source_path` and `dest_path`.
copyFile
Atomically creates a new file at `dest_path` within `dest_dir` with the
atomicFile
Directly access the `.file` field, and then call `AtomicFile.finish` to
statFile
Returns metadata for a file inside the directory.
chmod
Changes the mode of the directory.
chown
Changes the owner and group of the directory.
setPermissions
Sets permissions according to the provided `Permissions` struct.

Error sets in this namespace

Error Sets

#

= posix.fd_t

Values

#
Handle
= posix.fd_t

Source

Implementation

#
pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File.OpenError!File {
    switch (native_os) {
        .windows => {
            const path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path);
            return self.openFileW(path_w.span(), flags);
        },
        // Use the libc API when libc is linked because it implements things
        // such as opening absolute file paths.
        .wasi => if (!builtin.link_libc) {
            return openFile(self, mem.sliceTo(sub_path, 0), flags);
        },
        else => {},
    }

    var os_flags: posix.O = switch (native_os) {
        .wasi => .{
            .read = flags.mode != .write_only,
            .write = flags.mode != .read_only,
        },
        else => .{
            .ACCMODE = switch (flags.mode) {
                .read_only => .RDONLY,
                .write_only => .WRONLY,
                .read_write => .RDWR,
            },
        },
    };
    if (@hasField(posix.O, "CLOEXEC")) os_flags.CLOEXEC = true;
    if (@hasField(posix.O, "LARGEFILE")) os_flags.LARGEFILE = true;
    if (@hasField(posix.O, "NOCTTY")) os_flags.NOCTTY = !flags.allow_ctty;

    // Use the O locking flags if the os supports them to acquire the lock
    // atomically.
    const has_flock_open_flags = @hasField(posix.O, "EXLOCK");
    if (has_flock_open_flags) {
        // Note that the NONBLOCK flag is removed after the openat() call
        // is successful.
        switch (flags.lock) {
            .none => {},
            .shared => {
                os_flags.SHLOCK = true;
                os_flags.NONBLOCK = flags.lock_nonblocking;
            },
            .exclusive => {
                os_flags.EXLOCK = true;
                os_flags.NONBLOCK = flags.lock_nonblocking;
            },
        }
    }
    const fd = try posix.openatZ(self.fd, sub_path, os_flags, 0);
    errdefer posix.close(fd);

    if (have_flock and !has_flock_open_flags and flags.lock != .none) {
        // TODO: integrate async I/O
        const lock_nonblocking: i32 = if (flags.lock_nonblocking) posix.LOCK.NB else 0;
        try posix.flock(fd, switch (flags.lock) {
            .none => unreachable,
            .shared => posix.LOCK.SH | lock_nonblocking,
            .exclusive => posix.LOCK.EX | lock_nonblocking,
        });
    }

    if (has_flock_open_flags and flags.lock_nonblocking) {
        var fl_flags = posix.fcntl(fd, posix.F.GETFL, 0) catch |err| switch (err) {
            error.FileBusy => unreachable,
            error.Locked => unreachable,
            error.PermissionDenied => unreachable,
            error.DeadLock => unreachable,
            error.LockedRegionLimitExceeded => unreachable,
            else => |e| return e,
        };
        fl_flags &= ~@as(usize, 1 << @bitOffsetOf(posix.O, "NONBLOCK"));
        _ = posix.fcntl(fd, posix.F.SETFL, fl_flags) catch |err| switch (err) {
            error.FileBusy => unreachable,
            error.Locked => unreachable,
            error.PermissionDenied => unreachable,
            error.DeadLock => unreachable,
            error.LockedRegionLimitExceeded => unreachable,
            else => |e| return e,
        };
    }

    return .{ .handle = fd };
}