diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-03-21 16:11:59 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-03-21 16:11:59 -0700 |
| commit | 8778dc4bb2bb20779bee3fc964f79437ff3ef9fe (patch) | |
| tree | 8bed57c050c51c94b40ff3d5b9bd4581d02c7800 /lib/std/Build | |
| parent | 31791ae15bfa8590cb235ce9d7c87a97d5136eef (diff) | |
| download | zig-8778dc4bb2bb20779bee3fc964f79437ff3ef9fe.tar.gz zig-8778dc4bb2bb20779bee3fc964f79437ff3ef9fe.zip | |
extract std.Build.Cache.Directory into separate file
Diffstat (limited to 'lib/std/Build')
| -rw-r--r-- | lib/std/Build/Cache.zig | 72 | ||||
| -rw-r--r-- | lib/std/Build/Cache/Directory.zig | 74 |
2 files changed, 75 insertions, 71 deletions
diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig index c18a748de2..a33e574871 100644 --- a/lib/std/Build/Cache.zig +++ b/lib/std/Build/Cache.zig @@ -2,77 +2,6 @@ //! This is not a general-purpose cache. It is designed to be fast and simple, //! not to withstand attacks using specially-crafted input. -pub const Directory = struct { - /// This field is redundant for operations that can act on the open directory handle - /// directly, but it is needed when passing the directory to a child process. - /// `null` means cwd. - path: ?[]const u8, - handle: fs.Dir, - - pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory { - return .{ - .path = if (d.path) |p| try arena.dupe(u8, p) else null, - .handle = d.handle, - }; - } - - pub fn cwd() Directory { - return .{ - .path = null, - .handle = fs.cwd(), - }; - } - - pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 { - if (self.path) |p| { - // TODO clean way to do this with only 1 allocation - const part2 = try fs.path.join(allocator, paths); - defer allocator.free(part2); - return fs.path.join(allocator, &[_][]const u8{ p, part2 }); - } else { - return fs.path.join(allocator, paths); - } - } - - pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8 { - if (self.path) |p| { - // TODO clean way to do this with only 1 allocation - const part2 = try fs.path.join(allocator, paths); - defer allocator.free(part2); - return fs.path.joinZ(allocator, &[_][]const u8{ p, part2 }); - } else { - return fs.path.joinZ(allocator, paths); - } - } - - /// Whether or not the handle should be closed, or the path should be freed - /// is determined by usage, however this function is provided for convenience - /// if it happens to be what the caller needs. - pub fn closeAndFree(self: *Directory, gpa: Allocator) void { - self.handle.close(); - if (self.path) |p| gpa.free(p); - self.* = undefined; - } - - pub fn format( - self: Directory, - comptime fmt_string: []const u8, - options: fmt.FormatOptions, - writer: anytype, - ) !void { - _ = options; - if (fmt_string.len != 0) fmt.invalidFmtError(fmt_string, self); - if (self.path) |p| { - try writer.writeAll(p); - try writer.writeAll(fs.path.sep_str); - } - } - - pub fn eql(self: Directory, other: Directory) bool { - return self.handle.fd == other.handle.fd; - } -}; - gpa: Allocator, manifest_dir: fs.Dir, hash: HashHelper = .{}, @@ -88,6 +17,7 @@ mutex: std.Thread.Mutex = .{}, prefixes_buffer: [4]Directory = undefined, prefixes_len: usize = 0, +pub const Directory = @import("Cache/Directory.zig"); pub const DepTokenizer = @import("Cache/DepTokenizer.zig"); const Cache = @This(); diff --git a/lib/std/Build/Cache/Directory.zig b/lib/std/Build/Cache/Directory.zig new file mode 100644 index 0000000000..4de1cc18f1 --- /dev/null +++ b/lib/std/Build/Cache/Directory.zig @@ -0,0 +1,74 @@ +const Directory = @This(); +const std = @import("../../std.zig"); +const fs = std.fs; +const fmt = std.fmt; +const Allocator = std.mem.Allocator; + +/// This field is redundant for operations that can act on the open directory handle +/// directly, but it is needed when passing the directory to a child process. +/// `null` means cwd. +path: ?[]const u8, +handle: fs.Dir, + +pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory { + return .{ + .path = if (d.path) |p| try arena.dupe(u8, p) else null, + .handle = d.handle, + }; +} + +pub fn cwd() Directory { + return .{ + .path = null, + .handle = fs.cwd(), + }; +} + +pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 { + if (self.path) |p| { + // TODO clean way to do this with only 1 allocation + const part2 = try fs.path.join(allocator, paths); + defer allocator.free(part2); + return fs.path.join(allocator, &[_][]const u8{ p, part2 }); + } else { + return fs.path.join(allocator, paths); + } +} + +pub fn joinZ(self: Directory, allocator: Allocator, paths: []const []const u8) ![:0]u8 { + if (self.path) |p| { + // TODO clean way to do this with only 1 allocation + const part2 = try fs.path.join(allocator, paths); + defer allocator.free(part2); + return fs.path.joinZ(allocator, &[_][]const u8{ p, part2 }); + } else { + return fs.path.joinZ(allocator, paths); + } +} + +/// Whether or not the handle should be closed, or the path should be freed +/// is determined by usage, however this function is provided for convenience +/// if it happens to be what the caller needs. +pub fn closeAndFree(self: *Directory, gpa: Allocator) void { + self.handle.close(); + if (self.path) |p| gpa.free(p); + self.* = undefined; +} + +pub fn format( + self: Directory, + comptime fmt_string: []const u8, + options: fmt.FormatOptions, + writer: anytype, +) !void { + _ = options; + if (fmt_string.len != 0) fmt.invalidFmtError(fmt_string, self); + if (self.path) |p| { + try writer.writeAll(p); + try writer.writeAll(fs.path.sep_str); + } +} + +pub fn eql(self: Directory, other: Directory) bool { + return self.handle.fd == other.handle.fd; +} |
