authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-02 16:31:16-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:13-07:00
loge0561ad79be81f55d525cc9b847e0fc7f481ee16
tree50694881014125056faa9cd73d4ef90ee5545e0b
parenta2dc49a0f3d5761eae271d9b353542edb6e8f6e9

std.Build.Cache.Directory: add a format() method


1 files changed, 30 insertions(+), 16 deletions(-)

lib/std/Build/Cache.zig+30-16
...@@ -7,27 +7,27 @@ pub const Directory = struct {...@@ -7,27 +7,27 @@ pub const Directory = struct {
7 /// directly, but it is needed when passing the directory to a child process.7 /// directly, but it is needed when passing the directory to a child process.
8 /// `null` means cwd.8 /// `null` means cwd.
9 path: ?[]const u8,9 path: ?[]const u8,
10 handle: std.fs.Dir,10 handle: fs.Dir,
1111
12 pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 {12 pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 {
13 if (self.path) |p| {13 if (self.path) |p| {
14 // TODO clean way to do this with only 1 allocation14 // TODO clean way to do this with only 1 allocation
15 const part2 = try std.fs.path.join(allocator, paths);15 const part2 = try fs.path.join(allocator, paths);
16 defer allocator.free(part2);16 defer allocator.free(part2);
17 return std.fs.path.join(allocator, &[_][]const u8{ p, part2 });17 return fs.path.join(allocator, &[_][]const u8{ p, part2 });
18 } else {18 } else {
19 return std.fs.path.join(allocator, paths);19 return fs.path.join(allocator, paths);
20 }20 }
21 }21 }
2222
23 pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8 {23 pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8 {
24 if (self.path) |p| {24 if (self.path) |p| {
25 // TODO clean way to do this with only 1 allocation25 // TODO clean way to do this with only 1 allocation
26 const part2 = try std.fs.path.join(allocator, paths);26 const part2 = try fs.path.join(allocator, paths);
27 defer allocator.free(part2);27 defer allocator.free(part2);
28 return std.fs.path.joinZ(allocator, &[_][]const u8{ p, part2 });28 return fs.path.joinZ(allocator, &[_][]const u8{ p, part2 });
29 } else {29 } else {
30 return std.fs.path.joinZ(allocator, paths);30 return fs.path.joinZ(allocator, paths);
31 }31 }
32 }32 }
3333
...@@ -39,6 +39,20 @@ pub const Directory = struct {...@@ -39,6 +39,20 @@ pub const Directory = struct {
39 if (self.path) |p| gpa.free(p);39 if (self.path) |p| gpa.free(p);
40 self.* = undefined;40 self.* = undefined;
41 }41 }
42
43 pub fn format(
44 self: Directory,
45 comptime fmt_string: []const u8,
46 options: fmt.FormatOptions,
47 writer: anytype,
48 ) !void {
49 _ = options;
50 if (fmt_string.len != 0) fmt.invalidFmtError(fmt, self);
51 if (self.path) |p| {
52 try writer.writeAll(p);
53 try writer.writeAll(fs.path.sep_str);
54 }
55 }
42};56};
4357
44gpa: Allocator,58gpa: Allocator,
...@@ -243,10 +257,10 @@ pub const HashHelper = struct {...@@ -243,10 +257,10 @@ pub const HashHelper = struct {
243 hh.hasher.final(&bin_digest);257 hh.hasher.final(&bin_digest);
244258
245 var out_digest: [hex_digest_len]u8 = undefined;259 var out_digest: [hex_digest_len]u8 = undefined;
246 _ = std.fmt.bufPrint(260 _ = fmt.bufPrint(
247 &out_digest,261 &out_digest,
248 "{s}",262 "{s}",
249 .{std.fmt.fmtSliceHexLower(&bin_digest)},263 .{fmt.fmtSliceHexLower(&bin_digest)},
250 ) catch unreachable;264 ) catch unreachable;
251 return out_digest;265 return out_digest;
252 }266 }
...@@ -365,10 +379,10 @@ pub const Manifest = struct {...@@ -365,10 +379,10 @@ pub const Manifest = struct {
365 var bin_digest: BinDigest = undefined;379 var bin_digest: BinDigest = undefined;
366 self.hash.hasher.final(&bin_digest);380 self.hash.hasher.final(&bin_digest);
367381
368 _ = std.fmt.bufPrint(382 _ = fmt.bufPrint(
369 &self.hex_digest,383 &self.hex_digest,
370 "{s}",384 "{s}",
371 .{std.fmt.fmtSliceHexLower(&bin_digest)},385 .{fmt.fmtSliceHexLower(&bin_digest)},
372 ) catch unreachable;386 ) catch unreachable;
373387
374 self.hash.hasher = hasher_init;388 self.hash.hasher = hasher_init;
...@@ -469,7 +483,7 @@ pub const Manifest = struct {...@@ -469,7 +483,7 @@ pub const Manifest = struct {
469 cache_hash_file.stat.size = fmt.parseInt(u64, size, 10) catch return error.InvalidFormat;483 cache_hash_file.stat.size = fmt.parseInt(u64, size, 10) catch return error.InvalidFormat;
470 cache_hash_file.stat.inode = fmt.parseInt(fs.File.INode, inode, 10) catch return error.InvalidFormat;484 cache_hash_file.stat.inode = fmt.parseInt(fs.File.INode, inode, 10) catch return error.InvalidFormat;
471 cache_hash_file.stat.mtime = fmt.parseInt(i64, mtime_nsec_str, 10) catch return error.InvalidFormat;485 cache_hash_file.stat.mtime = fmt.parseInt(i64, mtime_nsec_str, 10) catch return error.InvalidFormat;
472 _ = std.fmt.hexToBytes(&cache_hash_file.bin_digest, digest_str) catch return error.InvalidFormat;486 _ = fmt.hexToBytes(&cache_hash_file.bin_digest, digest_str) catch return error.InvalidFormat;
473 const prefix = fmt.parseInt(u8, prefix_str, 10) catch return error.InvalidFormat;487 const prefix = fmt.parseInt(u8, prefix_str, 10) catch return error.InvalidFormat;
474 if (prefix >= self.cache.prefixes_len) return error.InvalidFormat;488 if (prefix >= self.cache.prefixes_len) return error.InvalidFormat;
475489
...@@ -806,10 +820,10 @@ pub const Manifest = struct {...@@ -806,10 +820,10 @@ pub const Manifest = struct {
806 self.hash.hasher.final(&bin_digest);820 self.hash.hasher.final(&bin_digest);
807821
808 var out_digest: [hex_digest_len]u8 = undefined;822 var out_digest: [hex_digest_len]u8 = undefined;
809 _ = std.fmt.bufPrint(823 _ = fmt.bufPrint(
810 &out_digest,824 &out_digest,
811 "{s}",825 "{s}",
812 .{std.fmt.fmtSliceHexLower(&bin_digest)},826 .{fmt.fmtSliceHexLower(&bin_digest)},
813 ) catch unreachable;827 ) catch unreachable;
814828
815 return out_digest;829 return out_digest;
...@@ -831,10 +845,10 @@ pub const Manifest = struct {...@@ -831,10 +845,10 @@ pub const Manifest = struct {
831 var encoded_digest: [hex_digest_len]u8 = undefined;845 var encoded_digest: [hex_digest_len]u8 = undefined;
832846
833 for (self.files.items) |file| {847 for (self.files.items) |file| {
834 _ = std.fmt.bufPrint(848 _ = fmt.bufPrint(
835 &encoded_digest,849 &encoded_digest,
836 "{s}",850 "{s}",
837 .{std.fmt.fmtSliceHexLower(&file.bin_digest)},851 .{fmt.fmtSliceHexLower(&file.bin_digest)},
838 ) catch unreachable;852 ) catch unreachable;
839 try writer.print("{d} {d} {d} {s} {d} {s}\n", .{853 try writer.print("{d} {d} {d} {s} {d} {s}\n", .{
840 file.stat.size,854 file.stat.size,