| ... | @@ -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. |
| 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 | gpa: Allocator, | 5 | gpa: Allocator, |
| 77 | manifest_dir: fs.Dir, | 6 | manifest_dir: fs.Dir, |
| 78 | hash: HashHelper = .{}, | 7 | hash: HashHelper = .{}, |
| ... | @@ -88,6 +17,7 @@ mutex: std.Thread.Mutex = .{}, | ... | @@ -88,6 +17,7 @@ mutex: std.Thread.Mutex = .{}, |
| 88 | prefixes_buffer: [4]Directory = undefined, | 17 | prefixes_buffer: [4]Directory = undefined, |
| 89 | prefixes_len: usize = 0, | 18 | prefixes_len: usize = 0, |
| 90 | | 19 | |
| | 20 | pub const Directory = @import("Cache/Directory.zig"); |
| 91 | pub const DepTokenizer = @import("Cache/DepTokenizer.zig"); | 21 | pub const DepTokenizer = @import("Cache/DepTokenizer.zig"); |
| 92 | | 22 | |
| 93 | const Cache = @This(); | 23 | const Cache = @This(); |