authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-04-15 20:13:26-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 13:48:43-04:00
logf13c67bcfed1c509abd13e83ae36479b793583d2
treeb0255c670a2191bfe313092aa7c2617adc8af73a
parent4d62d97076a10cc6371fb6cbbfec8c906611a72b

Return an index from `CacheHash.addFile`

This makes it possible for the user to retrieve the contents of the file without running into data races.

1 files changed, 22 insertions(+), 9 deletions(-)

lib/std/cache_hash.zig+22-9
...@@ -19,12 +19,17 @@ pub const File = struct {...@@ -19,12 +19,17 @@ pub const File = struct {
19 path: ?[]const u8,19 path: ?[]const u8,
20 stat: fs.File.Stat,20 stat: fs.File.Stat,
21 bin_digest: [BIN_DIGEST_LEN]u8,21 bin_digest: [BIN_DIGEST_LEN]u8,
22 contents: ?[]const u8 = null,
2223
23 pub fn deinit(self: *@This(), alloc: *Allocator) void {24 pub fn deinit(self: *@This(), alloc: *Allocator) void {
24 if (self.path) |owned_slice| {25 if (self.path) |owned_slice| {
25 alloc.free(owned_slice);26 alloc.free(owned_slice);
26 self.path = null;27 self.path = null;
27 }28 }
29 if (self.contents) |contents| {
30 alloc.free(contents);
31 self.contents = null;
32 }
28 }33 }
29};34};
3035
...@@ -77,13 +82,23 @@ pub const CacheHash = struct {...@@ -77,13 +82,23 @@ pub const CacheHash = struct {
77 /// Add a file as a dependency of process being cached. When `CacheHash.hit` is82 /// Add a file as a dependency of process being cached. When `CacheHash.hit` is
78 /// called, the file's contents will be checked to ensure that it matches83 /// called, the file's contents will be checked to ensure that it matches
79 /// the contents from previous times.84 /// the contents from previous times.
80 pub fn addFile(self: *@This(), file_path: []const u8) !void {85 ///
86 /// Returns the index of the entry in the `CacheHash.files` ArrayList. You can use it
87 /// to access the contents of the file after calling `CacheHash.hit()` like so:
88 ///
89 /// ```
90 /// var file_contents = cache_hash.files.items[file_index].contents.?;
91 /// ```
92 pub fn addFile(self: *@This(), file_path: []const u8) !usize {
81 debug.assert(self.manifest_file == null);93 debug.assert(self.manifest_file == null);
8294
95 const idx = self.files.items.len;
83 var cache_hash_file = try self.files.addOne();96 var cache_hash_file = try self.files.addOne();
84 cache_hash_file.path = try fs.path.resolve(self.alloc, &[_][]const u8{file_path});97 cache_hash_file.path = try fs.path.resolve(self.alloc, &[_][]const u8{file_path});
8598
86 self.addSlice(cache_hash_file.path.?);99 self.addSlice(cache_hash_file.path.?);
100
101 return idx;
87 }102 }
88103
89 /// Check the cache to see if the input exists in it. If it exists, a base64 encoding104 /// Check the cache to see if the input exists in it. If it exists, a base64 encoding
...@@ -191,8 +206,7 @@ pub const CacheHash = struct {...@@ -191,8 +206,7 @@ pub const CacheHash = struct {
191 }206 }
192207
193 var actual_digest: [BIN_DIGEST_LEN]u8 = undefined;208 var actual_digest: [BIN_DIGEST_LEN]u8 = undefined;
194 const contents = try hash_file(self.alloc, &actual_digest, &this_file);209 cache_hash_file.contents = try hash_file(self.alloc, &actual_digest, &this_file);
195 self.alloc.free(contents);
196210
197 if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) {211 if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) {
198 mem.copy(u8, &cache_hash_file.bin_digest, &actual_digest);212 mem.copy(u8, &cache_hash_file.bin_digest, &actual_digest);
...@@ -253,8 +267,7 @@ pub const CacheHash = struct {...@@ -253,8 +267,7 @@ pub const CacheHash = struct {
253 }267 }
254268
255 fn populate_file_hash(self: *@This(), cache_hash_file: *File) !void {269 fn populate_file_hash(self: *@This(), cache_hash_file: *File) !void {
256 const contents = try self.populate_file_hash_fetch(self.alloc, cache_hash_file);270 cache_hash_file.contents = try self.populate_file_hash_fetch(self.alloc, cache_hash_file);
257 self.alloc.free(contents);
258 }271 }
259272
260 /// Add a file as a dependency of process being cached, after the initial hash has been273 /// Add a file as a dependency of process being cached, after the initial hash has been
...@@ -381,7 +394,7 @@ test "cache file and then recall it" {...@@ -381,7 +394,7 @@ test "cache file and then recall it" {
381 ch.add(true);394 ch.add(true);
382 ch.add(@as(u16, 1234));395 ch.add(@as(u16, 1234));
383 ch.add("1234");396 ch.add("1234");
384 try ch.addFile(temp_file);397 _ = try ch.addFile(temp_file);
385398
386 // There should be nothing in the cache399 // There should be nothing in the cache
387 testing.expectEqual(@as(?[64]u8, null), try ch.hit());400 testing.expectEqual(@as(?[64]u8, null), try ch.hit());
...@@ -395,7 +408,7 @@ test "cache file and then recall it" {...@@ -395,7 +408,7 @@ test "cache file and then recall it" {
395 ch.add(true);408 ch.add(true);
396 ch.add(@as(u16, 1234));409 ch.add(@as(u16, 1234));
397 ch.add("1234");410 ch.add("1234");
398 try ch.addFile(temp_file);411 _ = try ch.addFile(temp_file);
399412
400 // Cache hit! We just "built" the same file413 // Cache hit! We just "built" the same file
401 digest2 = (try ch.hit()).?;414 digest2 = (try ch.hit()).?;
...@@ -433,7 +446,7 @@ test "check that changing a file makes cache fail" {...@@ -433,7 +446,7 @@ test "check that changing a file makes cache fail" {
433 defer ch.release() catch unreachable;446 defer ch.release() catch unreachable;
434447
435 ch.add("1234");448 ch.add("1234");
436 try ch.addFile(temp_file);449 _ = try ch.addFile(temp_file);
437450
438 // There should be nothing in the cache451 // There should be nothing in the cache
439 testing.expectEqual(@as(?[64]u8, null), try ch.hit());452 testing.expectEqual(@as(?[64]u8, null), try ch.hit());
...@@ -448,7 +461,7 @@ test "check that changing a file makes cache fail" {...@@ -448,7 +461,7 @@ test "check that changing a file makes cache fail" {
448 defer ch.release() catch unreachable;461 defer ch.release() catch unreachable;
449462
450 ch.add("1234");463 ch.add("1234");
451 try ch.addFile(temp_file);464 _ = try ch.addFile(temp_file);
452465
453 // A file that we depend on has been updated, so the cache should not contain an entry for it466 // A file that we depend on has been updated, so the cache should not contain an entry for it
454 testing.expectEqual(@as(?[64]u8, null), try ch.hit());467 testing.expectEqual(@as(?[64]u8, null), try ch.hit());