| author | |
| committer | |
| log | 8778dc4bb2bb20779bee3fc964f79437ff3ef9fe |
| tree | 8bed57c050c51c94b40ff3d5b9bd4581d02c7800 |
| parent | 31791ae15bfa8590cb235ce9d7c87a97d5136eef |
2 files changed, 75 insertions(+), 71 deletions(-)
lib/std/Build/Cache.zig+1-71| ... | ... | @@ -2,77 +2,6 @@ |
| 2 | 2 | //! This is not a general-purpose cache. It is designed to be fast and simple, |
| 3 | 3 | //! not to withstand attacks using specially-crafted input. |
| 4 | 4 | |
| 5 | pub 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 | ||
| 76 | 5 | gpa: Allocator, |
| 77 | 6 | manifest_dir: fs.Dir, |
| 78 | 7 | hash: HashHelper = .{}, |
| ... | ... | @@ -88,6 +17,7 @@ mutex: std.Thread.Mutex = .{}, |
| 88 | 17 | prefixes_buffer: [4]Directory = undefined, |
| 89 | 18 | prefixes_len: usize = 0, |
| 90 | 19 | |
| 20 | pub const Directory = @import("Cache/Directory.zig"); | |
| 91 | 21 | pub const DepTokenizer = @import("Cache/DepTokenizer.zig"); |
| 92 | 22 | |
| 93 | 23 | const Cache = @This(); |
lib/std/Build/Cache/Directory.zig created+74| ... | ... | @@ -0,0 +1,74 @@ |
| 1 | const Directory = @This(); | |
| 2 | const std = @import("../../std.zig"); | |
| 3 | const fs = std.fs; | |
| 4 | const fmt = std.fmt; | |
| 5 | const 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. | |
| 10 | path: ?[]const u8, | |
| 11 | handle: fs.Dir, | |
| 12 | ||
| 13 | pub 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 | ||
| 20 | pub fn cwd() Directory { | |
| 21 | return .{ | |
| 22 | .path = null, | |
| 23 | .handle = fs.cwd(), | |
| 24 | }; | |
| 25 | } | |
| 26 | ||
| 27 | pub 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 | ||
| 38 | pub 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. | |
| 52 | pub fn closeAndFree(self: *Directory, gpa: Allocator) void { | |
| 53 | self.handle.close(); | |
| 54 | if (self.path) |p| gpa.free(p); | |
| 55 | self.* = undefined; | |
| 56 | } | |
| 57 | ||
| 58 | pub 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 | ||
| 72 | pub fn eql(self: Directory, other: Directory) bool { | |
| 73 | return self.handle.fd == other.handle.fd; | |
| 74 | } |