跳转到内容

std.Build.Cache.Path

标准库Zig 0.17.1官方原文
源码std/Build/Cache/Path.zig

第 1 行

字段

root_dir: Cache.Directory
sub_path: []const u8 = ""

此 Path 表示的路径,相对于根目录。 空字符串表示路径就是 root_dir。

std.Build.Cache.Path.access

函数

第 121 行

pub fn access(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.AccessOptions) !void

参数

p: Path
io: Io
sub_path: []const u8
flags: Io.Dir.AccessOptions

std.Build.Cache.Path.Allocator

别名

第 6 行

别名目标 std.mem.Allocator

std.Build.Cache.Path.assert

别名

第 5 行

别名目标 std.debug.assert

std.Build.Cache.Path.atomicFile

函数

第 106 行

pub fn atomicFile( p: Path, io: Io, sub_path: []const u8, options: Io.Dir.CreateFileAtomicOptions, buf: *[Io.Dir.max_path_bytes]u8, ) !Io.File.Atomic

参数

p: Path
io: Io
sub_path: []const u8
options: Io.Dir.CreateFileAtomicOptions
buf: *[Io.Dir.max_path_bytes]u8

std.Build.Cache.Path.basename

函数

第 215 行

pub fn basename(p: Path) []const u8

参数

p: Path

std.Build.Cache.Path.Cache

别名

第 7 行

别名目标 std.Build.Cache

std.Build.Cache.Path.clone

函数

第 14 行

pub fn clone(p: Path, arena: Allocator) Allocator.Error!Path

参数

p: Path
arena: Allocator

错误

OutOfMemory

std.Build.Cache.Path.createDirPath

函数

第 131 行

pub fn createDirPath(p: Path, io: Io, sub_path: []const u8) !void

参数

p: Path
io: Io
sub_path: []const u8

std.Build.Cache.Path.createDirPathOpen

函数

第 86 行

pub fn createDirPathOpen(p: Path, io: Io, sub_path: []const u8, opts: Io.Dir.CreateDirPathOpenOptions) !Io.Dir

参数

p: Path
io: Io
sub_path: []const u8
opts: Io.Dir.CreateDirPathOpenOptions

std.Build.Cache.Path.cwd

函数

第 21 行

pub fn cwd() Path

std.Build.Cache.Path.dirname

函数

第 208 行

pub fn dirname(p: Path) ?Path

参数

p: Path

std.Build.Cache.Path.eql

函数

第 192 行

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

参数

self: Path
other: Path

std.Build.Cache.Path.fmtEscapeChar

函数

第 164 行

已弃用,请使用双引号转义形式打印路径。

pub fn fmtEscapeChar(path: Path) std.fmt.Alt(Path, formatEscapeChar)

参数

path: Path

std.Build.Cache.Path.fmtEscapeString

函数

第 149 行

pub fn fmtEscapeString(path: Path) std.fmt.Alt(Path, formatEscapeString)

参数

path: Path

std.Build.Cache.Path.format

函数

第 179 行

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

参数

self: Path
writer: *Io.Writer

错误

WriteFailed

详细诊断信息见 Writer 实现。

std.Build.Cache.Path.formatEscapeChar

函数

第 169 行

已弃用,请使用双引号转义形式打印路径。

pub fn formatEscapeChar(path: Path, writer: *Io.Writer) Io.Writer.Error!void

参数

path: Path
writer: *Io.Writer

错误

WriteFailed

详细诊断信息见 Writer 实现。

std.Build.Cache.Path.formatEscapeString

函数

第 153 行

pub fn formatEscapeString(path: Path, writer: *Io.Writer) Io.Writer.Error!void

参数

path: Path
writer: *Io.Writer

错误

WriteFailed

详细诊断信息见 Writer 实现。

std.Build.Cache.Path.initCwd

函数

第 25 行

pub fn initCwd(sub_path: []const u8) Path

参数

sub_path: []const u8

std.Build.Cache.Path.Io

别名

第 4 行

别名目标 std.Io

std.Build.Cache.Path.join

函数

第 29 行

pub fn join(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path

参数

p: Path
arena: Allocator
sub_path: []const u8

错误

OutOfMemory

std.Build.Cache.Path.joinString

函数

第 49 行

pub fn joinString(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![]u8

参数

p: Path
gpa: Allocator
sub_path: []const u8

错误

OutOfMemory

std.Build.Cache.Path.joinStringZ

函数

第 55 行

pub fn joinStringZ(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![:0]u8

参数

p: Path
gpa: Allocator
sub_path: []const u8

错误

OutOfMemory

std.Build.Cache.Path.openDir

函数

第 71 行

pub fn openDir( p: Path, io: Io, sub_path: []const u8, args: Io.Dir.OpenOptions, ) Io.Dir.OpenError!Io.Dir

参数

p: Path
io: Io
sub_path: []const u8
args: Io.Dir.OpenOptions

错误

AccessDenied
BadPathName PathNameError

文件系统无法编码请求的文件名字节。 原因可能是 Windows 上无效的 WTF-8、WASI 上无效的 UTF-8、 Windows 上的无效字符等,具体取决于文件系统与操作系统。

Canceled Cancelable

调用方请求停止异步操作。

FileNotFound
NameTooLong PathNameError

提供的缓冲区过小, 无法容纳路径名时返回。

NetworkNotFound

在 Windows 上,未找到 \\server 或 \\server\share。

NoDevice
NotDir
PermissionDenied
ProcessFdQuotaExceeded
SymLinkLoop
SystemFdQuotaExceeded
SystemResources
Unexpected UnexpectedError

操作系统返回了未记录在文档中的错误码。

理论上不可能出现此错误,但处理 此错误优于触发未定义行为。

观察到此错误码时,通常意味着 Zig 标准 库需要小幅修补,将该错误码加入 相应函数的错误集合。

std.Build.Cache.Path.openFile

函数

第 61 行

pub fn openFile(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.OpenFileOptions) !Io.File

参数

p: Path
io: Io
sub_path: []const u8
flags: Io.Dir.OpenFileOptions

std.Build.Cache.Path.Path

别名

第 1 行

别名目标 std.Build.Cache.Path

std.Build.Cache.Path.resolvePosix

函数

第 39 行

pub fn resolvePosix(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path

参数

p: Path
arena: Allocator
sub_path: []const u8

错误

OutOfMemory

std.Build.Cache.Path.statFile

函数

第 96 行

pub fn statFile(p: Path, io: Io, sub_path: []const u8) !Io.Dir.Stat

参数

p: Path
io: Io
sub_path: []const u8

std.Build.Cache.Path.std

别名

第 3 行

别名目标 std

std.Build.Cache.Path.stem

函数

第 204 行

pub fn stem(p: Path) []const u8

参数

p: Path

std.Build.Cache.Path.subPathOpt

函数

第 196 行

pub fn subPathOpt(self: Path) ?[]const u8

参数

self: Path

std.Build.Cache.Path.subPathOrDot

函数

第 200 行

pub fn subPathOrDot(self: Path) []const u8

参数

self: Path

std.Build.Cache.Path.TableAdapter

命名空间

第 220 行

用于将 Path 作为 std.ArrayHashMap 的键。

std.Build.Cache.Path.TableAdapter.eql

函数

第 232 行

pub fn eql(self: TableAdapter, a: Cache.Path, b: Cache.Path, b_index: usize) bool

参数

self: TableAdapter
a: Cache.Path
b: Cache.Path
b_index: usize

std.Build.Cache.Path.TableAdapter.hash

函数

第 223 行

pub fn hash(self: TableAdapter, a: Cache.Path) u32

参数

std.Build.Cache.Path.TableAdapter.Hash

别名

第 221 行

别名目标 std.hash.Wyhash

std.Build.Cache.Path.toString

函数

第 141 行

pub fn toString(p: Path, allocator: Allocator) Allocator.Error![]u8

参数

p: Path
allocator: Allocator

错误

OutOfMemory

std.Build.Cache.Path.toStringZ

函数

第 145 行

pub fn toStringZ(p: Path, allocator: Allocator) Allocator.Error![:0]u8

参数

p: Path
allocator: Allocator

错误

OutOfMemory
源码

std/Build/Cache/Path.zig

const Path = @This();

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

root_dir: Cache.Directory,
/// 此 `Path` 表示的路径,相对于根目录。
/// 空字符串表示路径就是 root_dir。
sub_path: []const u8 = "",

pub fn clone(p: Path, arena: Allocator) Allocator.Error!Path {
    return .{
        .root_dir = try p.root_dir.clone(arena),
        .sub_path = try arena.dupe(u8, p.sub_path),
    };
}

pub fn cwd() Path {
    return initCwd("");
}

pub fn initCwd(sub_path: []const u8) Path {
    return .{ .root_dir = .cwd(), .sub_path = sub_path };
}

pub fn join(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path {
    if (sub_path.len == 0) return p;
    const parts: []const []const u8 =
        if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
    return .{
        .root_dir = p.root_dir,
        .sub_path = try Io.Dir.path.join(arena, parts),
    };
}

pub fn resolvePosix(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path {
    if (sub_path.len == 0) return p;
    const new_sub_path = try Io.Dir.path.resolvePosix(arena, &.{ p.sub_path, sub_path });
    return .{
        .root_dir = p.root_dir,
        // 使用 "" 而非 "." 表示 `root_dir` 本身。
        .sub_path = if (std.mem.eql(u8, new_sub_path, ".")) "" else new_sub_path,
    };
}

pub fn joinString(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![]u8 {
    const parts: []const []const u8 =
        if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
    return p.root_dir.join(gpa, parts);
}

pub fn joinStringZ(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![:0]u8 {
    const parts: []const []const u8 =
        if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
    return p.root_dir.joinZ(gpa, parts);
}

pub fn openFile(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.OpenFileOptions) !Io.File {
    var buf: [Io.Dir.max_path_bytes]u8 = undefined;
    const joined_path = if (p.sub_path.len == 0) sub_path else p: {
        break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
            p.sub_path, sub_path,
        }) catch return error.NameTooLong;
    };
    return p.root_dir.handle.openFile(io, joined_path, flags);
}

pub fn openDir(
    p: Path,
    io: Io,
    sub_path: []const u8,
    args: Io.Dir.OpenOptions,
) Io.Dir.OpenError!Io.Dir {
    var buf: [Io.Dir.max_path_bytes]u8 = undefined;
    const joined_path = if (p.sub_path.len == 0) sub_path else p: {
        break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
            p.sub_path, sub_path,
        }) catch return error.NameTooLong;
    };
    return p.root_dir.handle.openDir(io, joined_path, args);
}

pub fn createDirPathOpen(p: Path, io: Io, sub_path: []const u8, opts: Io.Dir.CreateDirPathOpenOptions) !Io.Dir {
    var buf: [Io.Dir.max_path_bytes]u8 = undefined;
    const joined_path = if (p.sub_path.len == 0) sub_path else p: {
        break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
            p.sub_path, sub_path,
        }) catch return error.NameTooLong;
    };
    return p.root_dir.handle.createDirPathOpen(io, joined_path, opts);
}

pub fn statFile(p: Path, io: Io, sub_path: []const u8) !Io.Dir.Stat {
    var buf: [Io.Dir.max_path_bytes]u8 = undefined;
    const joined_path = if (p.sub_path.len == 0) sub_path else p: {
        break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
            p.sub_path, sub_path,
        }) catch return error.NameTooLong;
    };
    return p.root_dir.handle.statFile(io, joined_path, .{});
}

pub fn atomicFile(
    p: Path,
    io: Io,
    sub_path: []const u8,
    options: Io.Dir.CreateFileAtomicOptions,
    buf: *[Io.Dir.max_path_bytes]u8,
) !Io.File.Atomic {
    const joined_path = if (p.sub_path.len == 0) sub_path else p: {
        break :p std.mem.print(buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
            p.sub_path, sub_path,
        }) catch return error.NameTooLong;
    };
    return p.root_dir.handle.createFileAtomic(io, joined_path, options);
}

pub fn access(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.AccessOptions) !void {
    var buf: [Io.Dir.max_path_bytes]u8 = undefined;
    const joined_path = if (p.sub_path.len == 0) sub_path else p: {
        break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
            p.sub_path, sub_path,
        }) catch return error.NameTooLong;
    };
    return p.root_dir.handle.access(io, joined_path, flags);
}

pub fn createDirPath(p: Path, io: Io, sub_path: []const u8) !void {
    var buf: [Io.Dir.max_path_bytes]u8 = undefined;
    const joined_path = if (p.sub_path.len == 0) sub_path else p: {
        break :p std.mem.print(&buf, "{s}" ++ Io.Dir.path.sep_str ++ "{s}", .{
            p.sub_path, sub_path,
        }) catch return error.NameTooLong;
    };
    return p.root_dir.handle.createDirPath(io, joined_path);
}

pub fn toString(p: Path, allocator: Allocator) Allocator.Error![]u8 {
    return allocator.print("{f}", .{p});
}

pub fn toStringZ(p: Path, allocator: Allocator) Allocator.Error![:0]u8 {
    return allocator.printSentinel("{f}", .{p}, 0);
}

pub fn fmtEscapeString(path: Path) std.fmt.Alt(Path, formatEscapeString) {
    return .{ .data = path };
}

pub fn formatEscapeString(path: Path, writer: *Io.Writer) Io.Writer.Error!void {
    if (path.root_dir.path) |p| {
        try std.zig.stringEscape(p, writer);
        if (path.sub_path.len > 0) try std.zig.stringEscape(Io.Dir.path.sep_str, writer);
    }
    if (path.sub_path.len > 0) {
        try std.zig.stringEscape(path.sub_path, writer);
    }
}

/// 已弃用,请使用双引号转义形式打印路径。
pub fn fmtEscapeChar(path: Path) std.fmt.Alt(Path, formatEscapeChar) {
    return .{ .data = path };
}

/// 已弃用,请使用双引号转义形式打印路径。
pub fn formatEscapeChar(path: Path, writer: *Io.Writer) Io.Writer.Error!void {
    if (path.root_dir.path) |p| {
        for (p) |byte| try std.zig.charEscape(byte, writer);
        if (path.sub_path.len > 0) try writer.writeByte(Io.Dir.path.sep);
    }
    if (path.sub_path.len > 0) {
        for (path.sub_path) |byte| try std.zig.charEscape(byte, writer);
    }
}

pub fn format(self: Path, writer: *Io.Writer) Io.Writer.Error!void {
    if (Io.Dir.path.isAbsolute(self.sub_path)) {
        return writer.writeAll(self.sub_path);
    } else if (self.root_dir.path) |p| {
        var bufs: [3][]const u8 = .{ p, Io.Dir.path.sep_str, self.sub_path };
        return writer.writeVecAll(bufs[0..if (self.sub_path.len > 0) 3 else 1]);
    } else if (self.sub_path.len > 0) {
        return writer.writeAll(self.sub_path);
    } else {
        return writer.writeByte('.');
    }
}

pub fn eql(self: Path, other: Path) bool {
    return self.root_dir.eql(other.root_dir) and std.mem.eql(u8, self.sub_path, other.sub_path);
}

pub fn subPathOpt(self: Path) ?[]const u8 {
    return if (self.sub_path.len == 0) null else self.sub_path;
}

pub fn subPathOrDot(self: Path) []const u8 {
    return if (self.sub_path.len == 0) "." else self.sub_path;
}

pub fn stem(p: Path) []const u8 {
    return Io.Dir.path.stem(p.sub_path);
}

pub fn dirname(p: Path) ?Path {
    return .{
        .root_dir = p.root_dir,
        .sub_path = Io.Dir.path.dirname(p.subPathOpt() orelse return null) orelse "",
    };
}

pub fn basename(p: Path) []const u8 {
    return Io.Dir.path.basename(p.sub_path);
}

/// 用于将 `Path` 作为 `std.ArrayHashMap` 的键。
pub const TableAdapter = struct {
    pub const Hash = std.hash.Wyhash;

    pub fn hash(self: TableAdapter, a: Cache.Path) u32 {
        _ = self;
        const seed = switch (@typeInfo(@TypeOf(a.root_dir.handle.handle))) {
            .pointer => @intFromPtr(a.root_dir.handle.handle),
            .int => @as(u32, @bitCast(a.root_dir.handle.handle)),
            else => @compileError("unimplemented hash function"),
        };
        return @truncate(Hash.hash(seed, a.sub_path));
    }
    pub fn eql(self: TableAdapter, a: Cache.Path, b: Cache.Path, b_index: usize) bool {
        _ = self;
        _ = b_index;
        return a.eql(b);
    }
};