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 {
77 /// directly, but it is needed when passing the directory to a child process.
88 /// `null` means cwd.
99 path: ?[]const u8,
10 handle: std.fs.Dir,
10 handle: fs.Dir,
1111
1212 pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 {
1313 if (self.path) |p| {
1414 // 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);
1616 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 });
1818 } else {
19 return std.fs.path.join(allocator, paths);
19 return fs.path.join(allocator, paths);
2020 }
2121 }
2222
2323 pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8 {
2424 if (self.path) |p| {
2525 // 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);
2727 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 });
2929 } else {
30 return std.fs.path.joinZ(allocator, paths);
30 return fs.path.joinZ(allocator, paths);
3131 }
3232 }
3333
......@@ -39,6 +39,20 @@ pub const Directory = struct {
3939 if (self.path) |p| gpa.free(p);
4040 self.* = undefined;
4141 }
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 }
4256};
4357
4458gpa: Allocator,
......@@ -243,10 +257,10 @@ pub const HashHelper = struct {
243257 hh.hasher.final(&bin_digest);
244258
245259 var out_digest: [hex_digest_len]u8 = undefined;
246 _ = std.fmt.bufPrint(
260 _ = fmt.bufPrint(
247261 &out_digest,
248262 "{s}",
249 .{std.fmt.fmtSliceHexLower(&bin_digest)},
263 .{fmt.fmtSliceHexLower(&bin_digest)},
250264 ) catch unreachable;
251265 return out_digest;
252266 }
......@@ -365,10 +379,10 @@ pub const Manifest = struct {
365379 var bin_digest: BinDigest = undefined;
366380 self.hash.hasher.final(&bin_digest);
367381
368 _ = std.fmt.bufPrint(
382 _ = fmt.bufPrint(
369383 &self.hex_digest,
370384 "{s}",
371 .{std.fmt.fmtSliceHexLower(&bin_digest)},
385 .{fmt.fmtSliceHexLower(&bin_digest)},
372386 ) catch unreachable;
373387
374388 self.hash.hasher = hasher_init;
......@@ -469,7 +483,7 @@ pub const Manifest = struct {
469483 cache_hash_file.stat.size = fmt.parseInt(u64, size, 10) catch return error.InvalidFormat;
470484 cache_hash_file.stat.inode = fmt.parseInt(fs.File.INode, inode, 10) catch return error.InvalidFormat;
471485 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;
473487 const prefix = fmt.parseInt(u8, prefix_str, 10) catch return error.InvalidFormat;
474488 if (prefix >= self.cache.prefixes_len) return error.InvalidFormat;
475489
......@@ -806,10 +820,10 @@ pub const Manifest = struct {
806820 self.hash.hasher.final(&bin_digest);
807821
808822 var out_digest: [hex_digest_len]u8 = undefined;
809 _ = std.fmt.bufPrint(
823 _ = fmt.bufPrint(
810824 &out_digest,
811825 "{s}",
812 .{std.fmt.fmtSliceHexLower(&bin_digest)},
826 .{fmt.fmtSliceHexLower(&bin_digest)},
813827 ) catch unreachable;
814828
815829 return out_digest;
......@@ -831,10 +845,10 @@ pub const Manifest = struct {
831845 var encoded_digest: [hex_digest_len]u8 = undefined;
832846
833847 for (self.files.items) |file| {
834 _ = std.fmt.bufPrint(
848 _ = fmt.bufPrint(
835849 &encoded_digest,
836850 "{s}",
837 .{std.fmt.fmtSliceHexLower(&file.bin_digest)},
851 .{fmt.fmtSliceHexLower(&file.bin_digest)},
838852 ) catch unreachable;
839853 try writer.print("{d} {d} {d} {s} {d} {s}\n", .{
840854 file.stat.size,