|
| 1 | +root_dir: Cache.Directory, |
| 2 | +/// The path, relative to the root dir, that this `Path` represents. |
| 3 | +/// Empty string means the root_dir is the path. |
| 4 | +sub_path: []const u8 = "", |
| 5 | + |
| 6 | +pub fn clone(p: Path, arena: Allocator) Allocator.Error!Path { |
| 7 | + return .{ |
| 8 | + .root_dir = try p.root_dir.clone(arena), |
| 9 | + .sub_path = try arena.dupe(u8, p.sub_path), |
| 10 | + }; |
| 11 | +} |
| 12 | + |
| 13 | +pub fn cwd() Path { |
| 14 | + return .{ .root_dir = Cache.Directory.cwd() }; |
| 15 | +} |
| 16 | + |
| 17 | +pub fn join(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path { |
| 18 | + if (sub_path.len == 0) return p; |
| 19 | + const parts: []const []const u8 = |
| 20 | + if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path }; |
| 21 | + return .{ |
| 22 | + .root_dir = p.root_dir, |
| 23 | + .sub_path = try fs.path.join(arena, parts), |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +pub fn resolvePosix(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path { |
| 28 | + if (sub_path.len == 0) return p; |
| 29 | + return .{ |
| 30 | + .root_dir = p.root_dir, |
| 31 | + .sub_path = try fs.path.resolvePosix(arena, &.{ p.sub_path, sub_path }), |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +pub fn joinString(p: Path, allocator: Allocator, sub_path: []const u8) Allocator.Error![]u8 { |
| 36 | + const parts: []const []const u8 = |
| 37 | + if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path }; |
| 38 | + return p.root_dir.join(allocator, parts); |
| 39 | +} |
| 40 | + |
| 41 | +pub fn joinStringZ(p: Path, allocator: Allocator, sub_path: []const u8) Allocator.Error![:0]u8 { |
| 42 | + const parts: []const []const u8 = |
| 43 | + if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path }; |
| 44 | + return p.root_dir.joinZ(allocator, parts); |
| 45 | +} |
| 46 | + |
| 47 | +pub fn openFile( |
| 48 | + p: Path, |
| 49 | + sub_path: []const u8, |
| 50 | + flags: fs.File.OpenFlags, |
| 51 | +) !fs.File { |
| 52 | + var buf: [fs.MAX_PATH_BYTES]u8 = undefined; |
| 53 | + const joined_path = if (p.sub_path.len == 0) sub_path else p: { |
| 54 | + break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{ |
| 55 | + p.sub_path, sub_path, |
| 56 | + }) catch return error.NameTooLong; |
| 57 | + }; |
| 58 | + return p.root_dir.handle.openFile(joined_path, flags); |
| 59 | +} |
| 60 | + |
| 61 | +pub fn makeOpenPath(p: Path, sub_path: []const u8, opts: fs.OpenDirOptions) !fs.Dir { |
| 62 | + var buf: [fs.MAX_PATH_BYTES]u8 = undefined; |
| 63 | + const joined_path = if (p.sub_path.len == 0) sub_path else p: { |
| 64 | + break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{ |
| 65 | + p.sub_path, sub_path, |
| 66 | + }) catch return error.NameTooLong; |
| 67 | + }; |
| 68 | + return p.root_dir.handle.makeOpenPath(joined_path, opts); |
| 69 | +} |
| 70 | + |
| 71 | +pub fn statFile(p: Path, sub_path: []const u8) !fs.Dir.Stat { |
| 72 | + var buf: [fs.MAX_PATH_BYTES]u8 = undefined; |
| 73 | + const joined_path = if (p.sub_path.len == 0) sub_path else p: { |
| 74 | + break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{ |
| 75 | + p.sub_path, sub_path, |
| 76 | + }) catch return error.NameTooLong; |
| 77 | + }; |
| 78 | + return p.root_dir.handle.statFile(joined_path); |
| 79 | +} |
| 80 | + |
| 81 | +pub fn atomicFile( |
| 82 | + p: Path, |
| 83 | + sub_path: []const u8, |
| 84 | + options: fs.Dir.AtomicFileOptions, |
| 85 | + buf: *[fs.MAX_PATH_BYTES]u8, |
| 86 | +) !fs.AtomicFile { |
| 87 | + const joined_path = if (p.sub_path.len == 0) sub_path else p: { |
| 88 | + break :p std.fmt.bufPrint(buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{ |
| 89 | + p.sub_path, sub_path, |
| 90 | + }) catch return error.NameTooLong; |
| 91 | + }; |
| 92 | + return p.root_dir.handle.atomicFile(joined_path, options); |
| 93 | +} |
| 94 | + |
| 95 | +pub fn access(p: Path, sub_path: []const u8, flags: fs.File.OpenFlags) !void { |
| 96 | + var buf: [fs.MAX_PATH_BYTES]u8 = undefined; |
| 97 | + const joined_path = if (p.sub_path.len == 0) sub_path else p: { |
| 98 | + break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{ |
| 99 | + p.sub_path, sub_path, |
| 100 | + }) catch return error.NameTooLong; |
| 101 | + }; |
| 102 | + return p.root_dir.handle.access(joined_path, flags); |
| 103 | +} |
| 104 | + |
| 105 | +pub fn makePath(p: Path, sub_path: []const u8) !void { |
| 106 | + var buf: [fs.MAX_PATH_BYTES]u8 = undefined; |
| 107 | + const joined_path = if (p.sub_path.len == 0) sub_path else p: { |
| 108 | + break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{ |
| 109 | + p.sub_path, sub_path, |
| 110 | + }) catch return error.NameTooLong; |
| 111 | + }; |
| 112 | + return p.root_dir.handle.makePath(joined_path); |
| 113 | +} |
| 114 | + |
| 115 | +pub fn format( |
| 116 | + self: Path, |
| 117 | + comptime fmt_string: []const u8, |
| 118 | + options: std.fmt.FormatOptions, |
| 119 | + writer: anytype, |
| 120 | +) !void { |
| 121 | + if (fmt_string.len == 1) { |
| 122 | + // Quote-escape the string. |
| 123 | + const stringEscape = std.zig.stringEscape; |
| 124 | + const f = switch (fmt_string[0]) { |
| 125 | + 'q' => "", |
| 126 | + '\'' => '\'', |
| 127 | + else => @compileError("unsupported format string: " ++ fmt_string), |
| 128 | + }; |
| 129 | + if (self.root_dir.path) |p| { |
| 130 | + try stringEscape(p, f, options, writer); |
| 131 | + if (self.sub_path.len > 0) try stringEscape(fs.path.sep_str, f, options, writer); |
| 132 | + } |
| 133 | + if (self.sub_path.len > 0) { |
| 134 | + try stringEscape(self.sub_path, f, options, writer); |
| 135 | + } |
| 136 | + return; |
| 137 | + } |
| 138 | + if (fmt_string.len > 0) |
| 139 | + std.fmt.invalidFmtError(fmt_string, self); |
| 140 | + if (self.root_dir.path) |p| { |
| 141 | + try writer.writeAll(p); |
| 142 | + try writer.writeAll(fs.path.sep_str); |
| 143 | + } |
| 144 | + if (self.sub_path.len > 0) { |
| 145 | + try writer.writeAll(self.sub_path); |
| 146 | + try writer.writeAll(fs.path.sep_str); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +const Path = @This(); |
| 151 | +const std = @import("../../std.zig"); |
| 152 | +const fs = std.fs; |
| 153 | +const Allocator = std.mem.Allocator; |
| 154 | +const Cache = std.Build.Cache; |
0 commit comments