字段
PathstdIoassertAllocatorCacheclonecwdinitCwdjoinresolvePosixjoinStringjoinStringZopenFileopenDircreateDirPathOpenstatFileatomicFileaccesscreateDirPathtoStringtoStringZfmtEscapeStringformatEscapeStringfmtEscapeCharformatEscapeCharformateqlsubPathOptsubPathOrDotstemdirnamebasenameTableAdapter
std.Build.Cache.Path.access
函数
pub fn access(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.AccessOptions) !void
参数
std.Build.Cache.Path.Allocator
别名
别名目标 std.mem.Allocator
std.Build.Cache.Path.assert
别名
别名目标 std.debug.assert
std.Build.Cache.Path.atomicFile
函数
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
函数
pub fn basename(p: Path) []const u8
参数
p: Path
std.Build.Cache.Path.Cache
别名
别名目标 std.Build.Cache
std.Build.Cache.Path.clone
函数
pub fn clone(p: Path, arena: Allocator) Allocator.Error!Path
参数
错误
- OutOfMemory
std.Build.Cache.Path.createDirPath
函数
pub fn createDirPath(p: Path, io: Io, sub_path: []const u8) !void
参数
std.Build.Cache.Path.createDirPathOpen
函数
pub fn createDirPathOpen(p: Path, io: Io, sub_path: []const u8, opts: Io.Dir.CreateDirPathOpenOptions) !Io.Dir
参数
std.Build.Cache.Path.cwd
函数
pub fn cwd() Path
std.Build.Cache.Path.dirname
函数
pub fn dirname(p: Path) ?Path
参数
p: Path
std.Build.Cache.Path.eql
函数
pub fn eql(self: Path, other: Path) bool
参数
std.Build.Cache.Path.fmtEscapeChar
函数
已弃用,请使用双引号转义形式打印路径。
pub fn fmtEscapeChar(path: Path) std.fmt.Alt(Path, formatEscapeChar)
参数
path: Path
std.Build.Cache.Path.fmtEscapeString
函数
pub fn fmtEscapeString(path: Path) std.fmt.Alt(Path, formatEscapeString)
参数
path: Path
std.Build.Cache.Path.format
函数
pub fn format(self: Path, writer: *Io.Writer) Io.Writer.Error!void
参数
错误
- WriteFailed
详细诊断信息见
Writer实现。
std.Build.Cache.Path.formatEscapeChar
函数
已弃用,请使用双引号转义形式打印路径。
pub fn formatEscapeChar(path: Path, writer: *Io.Writer) Io.Writer.Error!void
参数
错误
- WriteFailed
详细诊断信息见
Writer实现。
std.Build.Cache.Path.formatEscapeString
函数
pub fn formatEscapeString(path: Path, writer: *Io.Writer) Io.Writer.Error!void
参数
错误
- WriteFailed
详细诊断信息见
Writer实现。
std.Build.Cache.Path.initCwd
函数
pub fn initCwd(sub_path: []const u8) Path
参数
sub_path: []const u8
std.Build.Cache.Path.Io
别名
别名目标 std.Io
std.Build.Cache.Path.join
函数
pub fn join(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path
参数
错误
- OutOfMemory
std.Build.Cache.Path.joinString
函数
pub fn joinString(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![]u8
参数
错误
- OutOfMemory
std.Build.Cache.Path.joinStringZ
函数
pub fn joinStringZ(p: Path, gpa: Allocator, sub_path: []const u8) Allocator.Error![:0]u8
参数
错误
- OutOfMemory
std.Build.Cache.Path.openDir
函数
pub fn openDir( p: Path, io: Io, sub_path: []const u8, args: Io.Dir.OpenOptions, ) Io.Dir.OpenError!Io.Dir
参数
错误
- 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
函数
pub fn openFile(p: Path, io: Io, sub_path: []const u8, flags: Io.Dir.OpenFileOptions) !Io.File
参数
std.Build.Cache.Path.Path
别名
别名目标 std.Build.Cache.Path
std.Build.Cache.Path.resolvePosix
函数
pub fn resolvePosix(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path
参数
错误
- OutOfMemory
std.Build.Cache.Path.statFile
函数
pub fn statFile(p: Path, io: Io, sub_path: []const u8) !Io.Dir.Stat
参数
std.Build.Cache.Path.std
别名
别名目标 std
std.Build.Cache.Path.stem
函数
pub fn stem(p: Path) []const u8
参数
p: Path
std.Build.Cache.Path.subPathOpt
函数
pub fn subPathOpt(self: Path) ?[]const u8
参数
self: Path
std.Build.Cache.Path.subPathOrDot
函数
pub fn subPathOrDot(self: Path) []const u8
参数
self: Path
std.Build.Cache.Path.TableAdapter
命名空间
用于将 Path 作为 std.ArrayHashMap 的键。
std.Build.Cache.Path.TableAdapter.eql
函数
pub fn eql(self: TableAdapter, a: Cache.Path, b: Cache.Path, b_index: usize) bool
参数
std.Build.Cache.Path.TableAdapter.hash
函数
pub fn hash(self: TableAdapter, a: Cache.Path) u32
参数
self: TableAdapter
a: Cache.Path
std.Build.Cache.Path.TableAdapter.Hash
别名
别名目标 std.hash.Wyhash
std.Build.Cache.Path.toString
函数
pub fn toString(p: Path, allocator: Allocator) Allocator.Error![]u8
参数
错误
- OutOfMemory
std.Build.Cache.Path.toStringZ
函数
pub fn toStringZ(p: Path, allocator: Allocator) Allocator.Error![:0]u8
参数
错误
- 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);
}
};