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 {
1919 path: ?[]const u8,
2020 stat: fs.File.Stat,
2121 bin_digest: [BIN_DIGEST_LEN]u8,
22 contents: ?[]const u8 = null,
2223
2324 pub fn deinit(self: *@This(), alloc: *Allocator) void {
2425 if (self.path) |owned_slice| {
2526 alloc.free(owned_slice);
2627 self.path = null;
2728 }
29 if (self.contents) |contents| {
30 alloc.free(contents);
31 self.contents = null;
32 }
2833 }
2934};
3035
......@@ -77,13 +82,23 @@ pub const CacheHash = struct {
7782 /// Add a file as a dependency of process being cached. When `CacheHash.hit` is
7883 /// called, the file's contents will be checked to ensure that it matches
7984 /// 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 {
8193 debug.assert(self.manifest_file == null);
8294
95 const idx = self.files.items.len;
8396 var cache_hash_file = try self.files.addOne();
8497 cache_hash_file.path = try fs.path.resolve(self.alloc, &[_][]const u8{file_path});
8598
8699 self.addSlice(cache_hash_file.path.?);
100
101 return idx;
87102 }
88103
89104 /// 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 {
191206 }
192207
193208 var actual_digest: [BIN_DIGEST_LEN]u8 = undefined;
194 const contents = try hash_file(self.alloc, &actual_digest, &this_file);
195 self.alloc.free(contents);
209 cache_hash_file.contents = try hash_file(self.alloc, &actual_digest, &this_file);
196210
197211 if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) {
198212 mem.copy(u8, &cache_hash_file.bin_digest, &actual_digest);
......@@ -253,8 +267,7 @@ pub const CacheHash = struct {
253267 }
254268
255269 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);
257 self.alloc.free(contents);
270 cache_hash_file.contents = try self.populate_file_hash_fetch(self.alloc, cache_hash_file);
258271 }
259272
260273 /// 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" {
381394 ch.add(true);
382395 ch.add(@as(u16, 1234));
383396 ch.add("1234");
384 try ch.addFile(temp_file);
397 _ = try ch.addFile(temp_file);
385398
386399 // There should be nothing in the cache
387400 testing.expectEqual(@as(?[64]u8, null), try ch.hit());
......@@ -395,7 +408,7 @@ test "cache file and then recall it" {
395408 ch.add(true);
396409 ch.add(@as(u16, 1234));
397410 ch.add("1234");
398 try ch.addFile(temp_file);
411 _ = try ch.addFile(temp_file);
399412
400413 // Cache hit! We just "built" the same file
401414 digest2 = (try ch.hit()).?;
......@@ -433,7 +446,7 @@ test "check that changing a file makes cache fail" {
433446 defer ch.release() catch unreachable;
434447
435448 ch.add("1234");
436 try ch.addFile(temp_file);
449 _ = try ch.addFile(temp_file);
437450
438451 // There should be nothing in the cache
439452 testing.expectEqual(@as(?[64]u8, null), try ch.hit());
......@@ -448,7 +461,7 @@ test "check that changing a file makes cache fail" {
448461 defer ch.release() catch unreachable;
449462
450463 ch.add("1234");
451 try ch.addFile(temp_file);
464 _ = try ch.addFile(temp_file);
452465
453466 // A file that we depend on has been updated, so the cache should not contain an entry for it
454467 testing.expectEqual(@as(?[64]u8, null), try ch.hit());