Skip to content

std.Build.Cache.Directory

Standard libraryZig 0.17.0Official documentation
Sourcestd/Build/Cache/Directory.zig

Source line 1

Fields

path: ?[]const u8

This field is redundant for operations that can act on the open directory handle directly, but it is needed when passing the directory to a child process. null means cwd.

handle: Io.Dir

std.Build.Cache.Directory.Allocator

alias

Source line 8

Alias of std.mem.Allocator

std.Build.Cache.Directory.assert

alias

Source line 6

Alias of std.debug.assert

std.Build.Cache.Directory.clone

function

Source line 16

pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory

Parameters

d: Directory
arena: Allocator

Errors

OutOfMemory

std.Build.Cache.Directory.closeAndFree

function

Source line 55

Whether or not the handle should be closed, or the path should be freed is determined by usage, however this function is provided for convenience if it happens to be what the caller needs.

pub fn closeAndFree(self: *Directory, gpa: Allocator, io: Io) void

Parameters

self: *Directory
gpa: Allocator
io: Io

std.Build.Cache.Directory.cwd

function

Source line 23

pub fn cwd() Directory

std.Build.Cache.Directory.Directory

alias

Source line 1

Alias of std.Build.Cache.Directory

std.Build.Cache.Directory.eql

function

Source line 68

pub fn eql(self: Directory, other: Directory) bool

Parameters

self: Directory
other: Directory

std.Build.Cache.Directory.fmt

alias

Source line 7

Alias of std.fmt

std.Build.Cache.Directory.format

function

Source line 61

pub fn format(self: Directory, writer: *std.Io.Writer) std.Io.Writer.Error!void

Parameters

self: Directory
writer: *std.Io.Writer

Errors

WriteFailed

See the Writer implementation for detailed diagnostics.

std.Build.Cache.Directory.fs

alias

Source line 5

Alias of std.fs

std.Build.Cache.Directory.Io

alias

Source line 4

Alias of std.Io

std.Build.Cache.Directory.join

function

Source line 30

pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8

Parameters

self: Directory
allocator: Allocator
paths: []const []const u8

std.Build.Cache.Directory.joinZ

function

Source line 41

pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8

Parameters

self: Directory
allocator: Allocator
paths: []const []const u8

std.Build.Cache.Directory.std

alias

Source line 3

Alias of std

Source code

std/Build/Cache/Directory.zig

const Directory = @This();

const std = @import("../../std.zig");
const Io = std.Io;
const fs = std.fs;
const assert = std.debug.assert;
const fmt = std.fmt;
const Allocator = std.mem.Allocator;

/// This field is redundant for operations that can act on the open directory handle
/// directly, but it is needed when passing the directory to a child process.
/// `null` means cwd.
path: ?[]const u8,
handle: Io.Dir,

pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory {
    return .{
        .path = if (d.path) |p| try arena.dupe(u8, p) else null,
        .handle = d.handle,
    };
}

pub fn cwd() Directory {
    return .{
        .path = null,
        .handle = .cwd(),
    };
}

pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 {
    if (self.path) |p| {
        // TODO clean way to do this with only 1 allocation
        const part2 = try fs.path.join(allocator, paths);
        defer allocator.free(part2);
        return fs.path.join(allocator, &[_][]const u8{ p, part2 });
    } else {
        return fs.path.join(allocator, paths);
    }
}

pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8 {
    if (self.path) |p| {
        // TODO clean way to do this with only 1 allocation
        const part2 = try fs.path.join(allocator, paths);
        defer allocator.free(part2);
        return fs.path.joinZ(allocator, &[_][]const u8{ p, part2 });
    } else {
        return fs.path.joinZ(allocator, paths);
    }
}

/// Whether or not the handle should be closed, or the path should be freed
/// is determined by usage, however this function is provided for convenience
/// if it happens to be what the caller needs.
pub fn closeAndFree(self: *Directory, gpa: Allocator, io: Io) void {
    self.handle.close(io);
    if (self.path) |p| gpa.free(p);
    self.* = undefined;
}

pub fn format(self: Directory, writer: *std.Io.Writer) std.Io.Writer.Error!void {
    if (self.path) |p| {
        try writer.writeAll(p);
        try writer.writeAll(fs.path.sep_str);
    }
}

pub fn eql(self: Directory, other: Directory) bool {
    return self.handle.handle == other.handle.handle;
}