authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-21 16:11:59-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-21 16:11:59-07:00
log8778dc4bb2bb20779bee3fc964f79437ff3ef9fe
tree8bed57c050c51c94b40ff3d5b9bd4581d02c7800
parent31791ae15bfa8590cb235ce9d7c87a97d5136eef

extract std.Build.Cache.Directory into separate file


2 files changed, 75 insertions(+), 71 deletions(-)

lib/std/Build/Cache.zig+1-71
...@@ -2,77 +2,6 @@...@@ -2,77 +2,6 @@
2//! This is not a general-purpose cache. It is designed to be fast and simple,2//! This is not a general-purpose cache. It is designed to be fast and simple,
3//! not to withstand attacks using specially-crafted input.3//! not to withstand attacks using specially-crafted input.
44
5pub const Directory = struct {
6 /// This field is redundant for operations that can act on the open directory handle
7 /// directly, but it is needed when passing the directory to a child process.
8 /// `null` means cwd.
9 path: ?[]const u8,
10 handle: fs.Dir,
11
12 pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory {
13 return .{
14 .path = if (d.path) |p| try arena.dupe(u8, p) else null,
15 .handle = d.handle,
16 };
17 }
18
19 pub fn cwd() Directory {
20 return .{
21 .path = null,
22 .handle = fs.cwd(),
23 };
24 }
25
26 pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 {
27 if (self.path) |p| {
28 // TODO clean way to do this with only 1 allocation
29 const part2 = try fs.path.join(allocator, paths);
30 defer allocator.free(part2);
31 return fs.path.join(allocator, &[_][]const u8{ p, part2 });
32 } else {
33 return fs.path.join(allocator, paths);
34 }
35 }
36
37 pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8 {
38 if (self.path) |p| {
39 // TODO clean way to do this with only 1 allocation
40 const part2 = try fs.path.join(allocator, paths);
41 defer allocator.free(part2);
42 return fs.path.joinZ(allocator, &[_][]const u8{ p, part2 });
43 } else {
44 return fs.path.joinZ(allocator, paths);
45 }
46 }
47
48 /// Whether or not the handle should be closed, or the path should be freed
49 /// is determined by usage, however this function is provided for convenience
50 /// if it happens to be what the caller needs.
51 pub fn closeAndFree(self: *Directory, gpa: Allocator) void {
52 self.handle.close();
53 if (self.path) |p| gpa.free(p);
54 self.* = undefined;
55 }
56
57 pub fn format(
58 self: Directory,
59 comptime fmt_string: []const u8,
60 options: fmt.FormatOptions,
61 writer: anytype,
62 ) !void {
63 _ = options;
64 if (fmt_string.len != 0) fmt.invalidFmtError(fmt_string, self);
65 if (self.path) |p| {
66 try writer.writeAll(p);
67 try writer.writeAll(fs.path.sep_str);
68 }
69 }
70
71 pub fn eql(self: Directory, other: Directory) bool {
72 return self.handle.fd == other.handle.fd;
73 }
74};
75
76gpa: Allocator,5gpa: Allocator,
77manifest_dir: fs.Dir,6manifest_dir: fs.Dir,
78hash: HashHelper = .{},7hash: HashHelper = .{},
...@@ -88,6 +17,7 @@ mutex: std.Thread.Mutex = .{},...@@ -88,6 +17,7 @@ mutex: std.Thread.Mutex = .{},
88prefixes_buffer: [4]Directory = undefined,17prefixes_buffer: [4]Directory = undefined,
89prefixes_len: usize = 0,18prefixes_len: usize = 0,
9019
20pub const Directory = @import("Cache/Directory.zig");
91pub const DepTokenizer = @import("Cache/DepTokenizer.zig");21pub const DepTokenizer = @import("Cache/DepTokenizer.zig");
9222
93const Cache = @This();23const Cache = @This();
lib/std/Build/Cache/Directory.zig created+74
...@@ -0,0 +1,74 @@
1const Directory = @This();
2const std = @import("../../std.zig");
3const fs = std.fs;
4const fmt = std.fmt;
5const Allocator = std.mem.Allocator;
6
7/// This field is redundant for operations that can act on the open directory handle
8/// directly, but it is needed when passing the directory to a child process.
9/// `null` means cwd.
10path: ?[]const u8,
11handle: fs.Dir,
12
13pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory {
14 return .{
15 .path = if (d.path) |p| try arena.dupe(u8, p) else null,
16 .handle = d.handle,
17 };
18}
19
20pub fn cwd() Directory {
21 return .{
22 .path = null,
23 .handle = fs.cwd(),
24 };
25}
26
27pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 {
28 if (self.path) |p| {
29 // TODO clean way to do this with only 1 allocation
30 const part2 = try fs.path.join(allocator, paths);
31 defer allocator.free(part2);
32 return fs.path.join(allocator, &[_][]const u8{ p, part2 });
33 } else {
34 return fs.path.join(allocator, paths);
35 }
36}
37
38pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8 {
39 if (self.path) |p| {
40 // TODO clean way to do this with only 1 allocation
41 const part2 = try fs.path.join(allocator, paths);
42 defer allocator.free(part2);
43 return fs.path.joinZ(allocator, &[_][]const u8{ p, part2 });
44 } else {
45 return fs.path.joinZ(allocator, paths);
46 }
47}
48
49/// Whether or not the handle should be closed, or the path should be freed
50/// is determined by usage, however this function is provided for convenience
51/// if it happens to be what the caller needs.
52pub fn closeAndFree(self: *Directory, gpa: Allocator) void {
53 self.handle.close();
54 if (self.path) |p| gpa.free(p);
55 self.* = undefined;
56}
57
58pub fn format(
59 self: Directory,
60 comptime fmt_string: []const u8,
61 options: fmt.FormatOptions,
62 writer: anytype,
63) !void {
64 _ = options;
65 if (fmt_string.len != 0) fmt.invalidFmtError(fmt_string, self);
66 if (self.path) |p| {
67 try writer.writeAll(p);
68 try writer.writeAll(fs.path.sep_str);
69 }
70}
71
72pub fn eql(self: Directory, other: Directory) bool {
73 return self.handle.fd == other.handle.fd;
74}