authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-06 21:02:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 13:48:43-04:00
log27bf1f781b5246914ba2fbdf556dbf72bc926b17
tree41952d6dc7d686a90be2dc0cb42064e1e4de86aa
parent55a3925ab79adae174faa97ae132518541c549cb

Store fs.Dir instead of path to dir


1 files changed, 18 insertions(+), 28 deletions(-)

lib/std/cache_hash.zig+18-28
......@@ -36,8 +36,7 @@ pub const File = struct {
3636pub const CacheHash = struct {
3737 alloc: *Allocator,
3838 blake3: Blake3,
39 manifest_dir: []const u8,
40 manifest_file_path: ?[]const u8,
39 manifest_dir: fs.Dir,
4140 manifest_file: ?fs.File,
4241 manifest_dirty: bool,
4342 force_check_manifest: bool,
......@@ -45,11 +44,13 @@ pub const CacheHash = struct {
4544 b64_digest: ArrayList(u8),
4645
4746 pub fn init(alloc: *Allocator, manifest_dir_path: []const u8) !@This() {
47 try fs.cwd().makePath(manifest_dir_path);
48 const manifest_dir = try fs.cwd().openDirTraverse(manifest_dir_path);
49
4850 return CacheHash{
4951 .alloc = alloc,
5052 .blake3 = Blake3.init(),
51 .manifest_dir = manifest_dir_path,
52 .manifest_file_path = null,
53 .manifest_dir = manifest_dir,
5354 .manifest_file = null,
5455 .manifest_dirty = false,
5556 .force_check_manifest = false,
......@@ -59,14 +60,14 @@ pub const CacheHash = struct {
5960 }
6061
6162 pub fn cache_buf(self: *@This(), val: []const u8) void {
62 debug.assert(self.manifest_file_path == null);
63 debug.assert(self.manifest_file == null);
6364
6465 self.blake3.update(val);
6566 self.blake3.update(&[_]u8{0});
6667 }
6768
6869 pub fn cache(self: *@This(), val: var) void {
69 debug.assert(self.manifest_file_path == null);
70 debug.assert(self.manifest_file == null);
7071
7172 const val_type = @TypeOf(val);
7273 switch (@typeInfo(val_type)) {
......@@ -88,7 +89,7 @@ pub const CacheHash = struct {
8889 }
8990
9091 pub fn cache_file(self: *@This(), file_path: []const u8) !void {
91 debug.assert(self.manifest_file_path == null);
92 debug.assert(self.manifest_file == null);
9293
9394 var cache_hash_file = try self.files.addOne();
9495 cache_hash_file.path = try fs.path.resolve(self.alloc, &[_][]const u8{file_path});
......@@ -97,7 +98,7 @@ pub const CacheHash = struct {
9798 }
9899
99100 pub fn hit(self: *@This(), out_digest: *ArrayList(u8)) !bool {
100 debug.assert(self.manifest_file_path == null);
101 debug.assert(self.manifest_file == null);
101102
102103 var bin_digest: [BIN_DIGEST_LEN]u8 = undefined;
103104 self.blake3.final(&bin_digest);
......@@ -116,21 +117,12 @@ pub const CacheHash = struct {
116117 self.blake3.update(&bin_digest);
117118
118119 {
119 const manifest_file_path_slice = try fs.path.join(self.alloc, &[_][]const u8{ self.manifest_dir, self.b64_digest.toSlice() });
120 var path_buf = ArrayList(u8).fromOwnedSlice(self.alloc, manifest_file_path_slice);
121 defer path_buf.deinit();
122 try path_buf.appendSlice(".txt");
120 const manifest_file_path = try fmt.allocPrint(self.alloc, "{}.txt", .{self.b64_digest.toSlice()});
121 defer self.alloc.free(manifest_file_path);
123122
124 self.manifest_file_path = path_buf.toOwnedSlice();
123 self.manifest_file = try self.manifest_dir.createFile(manifest_file_path, .{ .read = true, .truncate = false });
125124 }
126125
127 const cwd = fs.cwd();
128
129 try cwd.makePath(self.manifest_dir);
130
131 // TODO: Open file with a file lock
132 self.manifest_file = try cwd.createFile(self.manifest_file_path.?, .{ .read = true, .truncate = false });
133
134126 // create a buffer instead of using readAllAlloc
135127 // See: https://github.com/ziglang/zig/issues/4656
136128 var file_buffer = try Buffer.initCapacity(self.alloc, 16 * 1024);
......@@ -172,7 +164,7 @@ pub const CacheHash = struct {
172164 return error.InvalidFormat;
173165 }
174166
175 const this_file = cwd.openFile(cache_hash_file.path.?, .{ .read = true }) catch {
167 const this_file = fs.cwd().openFile(cache_hash_file.path.?, .{ .read = true }) catch {
176168 self.manifest_file.?.close();
177169 self.manifest_file = null;
178170 return error.CacheUnavailable;
......@@ -245,7 +237,7 @@ pub const CacheHash = struct {
245237 }
246238
247239 pub fn final(self: *@This(), out_digest: *ArrayList(u8)) !void {
248 debug.assert(self.manifest_file_path != null);
240 debug.assert(self.manifest_file != null);
249241
250242 var bin_digest: [BIN_DIGEST_LEN]u8 = undefined;
251243 self.blake3.final(&bin_digest);
......@@ -256,7 +248,7 @@ pub const CacheHash = struct {
256248 }
257249
258250 pub fn write_manifest(self: *@This()) !void {
259 debug.assert(self.manifest_file_path != null);
251 debug.assert(self.manifest_file != null);
260252
261253 const OUT_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN);
262254 var encoded_digest = try Buffer.initSize(self.alloc, OUT_DIGEST_LEN);
......@@ -274,23 +266,21 @@ pub const CacheHash = struct {
274266 }
275267
276268 pub fn release(self: *@This()) void {
277 debug.assert(self.manifest_file_path != null);
269 debug.assert(self.manifest_file != null);
278270
279271 if (self.manifest_dirty) {
280272 self.write_manifest() catch |err| {
281 debug.warn("Unable to write cache file '{}': {}\n", .{ self.manifest_file_path, err });
273 debug.warn("Unable to write cache file '{}': {}\n", .{ self.b64_digest, err });
282274 };
283275 }
284276
285277 self.manifest_file.?.close();
286 if (self.manifest_file_path) |owned_slice| {
287 self.alloc.free(owned_slice);
288 }
289278 for (self.files.toSlice()) |*file| {
290279 file.deinit(self.alloc);
291280 }
292281 self.files.deinit();
293282 self.b64_digest.deinit();
283 self.manifest_dir.close();
294284 }
295285};
296286