| ... | ... | @@ -19,12 +19,17 @@ pub const File = struct { |
| 19 | 19 | path: ?[]const u8, |
| 20 | 20 | stat: fs.File.Stat, |
| 21 | 21 | bin_digest: [BIN_DIGEST_LEN]u8, |
| 22 | contents: ?[]const u8 = null, |
| 22 | 23 | |
| 23 | 24 | pub fn deinit(self: *@This(), alloc: *Allocator) void { |
| 24 | 25 | if (self.path) |owned_slice| { |
| 25 | 26 | alloc.free(owned_slice); |
| 26 | 27 | self.path = null; |
| 27 | 28 | } |
| 29 | if (self.contents) |contents| { |
| 30 | alloc.free(contents); |
| 31 | self.contents = null; |
| 32 | } |
| 28 | 33 | } |
| 29 | 34 | }; |
| 30 | 35 | |
| ... | ... | @@ -77,13 +82,23 @@ pub const CacheHash = struct { |
| 77 | 82 | /// Add a file as a dependency of process being cached. When `CacheHash.hit` is |
| 78 | 83 | /// called, the file's contents will be checked to ensure that it matches |
| 79 | 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 | 93 | debug.assert(self.manifest_file == null); |
| 82 | 94 | |
| 95 | const idx = self.files.items.len; |
| 83 | 96 | var cache_hash_file = try self.files.addOne(); |
| 84 | 97 | cache_hash_file.path = try fs.path.resolve(self.alloc, &[_][]const u8{file_path}); |
| 85 | 98 | |
| 86 | 99 | self.addSlice(cache_hash_file.path.?); |
| 100 | |
| 101 | return idx; |
| 87 | 102 | } |
| 88 | 103 | |
| 89 | 104 | /// 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 | 206 | } |
| 192 | 207 | |
| 193 | 208 | 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); |
| 196 | 210 | |
| 197 | 211 | if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) { |
| 198 | 212 | mem.copy(u8, &cache_hash_file.bin_digest, &actual_digest); |
| ... | ... | @@ -253,8 +267,7 @@ pub const CacheHash = struct { |
| 253 | 267 | } |
| 254 | 268 | |
| 255 | 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); |
| 257 | | self.alloc.free(contents); |
| 270 | cache_hash_file.contents = try self.populate_file_hash_fetch(self.alloc, cache_hash_file); |
| 258 | 271 | } |
| 259 | 272 | |
| 260 | 273 | /// 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 | 394 | ch.add(true); |
| 382 | 395 | ch.add(@as(u16, 1234)); |
| 383 | 396 | ch.add("1234"); |
| 384 | | try ch.addFile(temp_file); |
| 397 | _ = try ch.addFile(temp_file); |
| 385 | 398 | |
| 386 | 399 | // There should be nothing in the cache |
| 387 | 400 | testing.expectEqual(@as(?[64]u8, null), try ch.hit()); |
| ... | ... | @@ -395,7 +408,7 @@ test "cache file and then recall it" { |
| 395 | 408 | ch.add(true); |
| 396 | 409 | ch.add(@as(u16, 1234)); |
| 397 | 410 | ch.add("1234"); |
| 398 | | try ch.addFile(temp_file); |
| 411 | _ = try ch.addFile(temp_file); |
| 399 | 412 | |
| 400 | 413 | // Cache hit! We just "built" the same file |
| 401 | 414 | digest2 = (try ch.hit()).?; |
| ... | ... | @@ -433,7 +446,7 @@ test "check that changing a file makes cache fail" { |
| 433 | 446 | defer ch.release() catch unreachable; |
| 434 | 447 | |
| 435 | 448 | ch.add("1234"); |
| 436 | | try ch.addFile(temp_file); |
| 449 | _ = try ch.addFile(temp_file); |
| 437 | 450 | |
| 438 | 451 | // There should be nothing in the cache |
| 439 | 452 | testing.expectEqual(@as(?[64]u8, null), try ch.hit()); |
| ... | ... | @@ -448,7 +461,7 @@ test "check that changing a file makes cache fail" { |
| 448 | 461 | defer ch.release() catch unreachable; |
| 449 | 462 | |
| 450 | 463 | ch.add("1234"); |
| 451 | | try ch.addFile(temp_file); |
| 464 | _ = try ch.addFile(temp_file); |
| 452 | 465 | |
| 453 | 466 | // A file that we depend on has been updated, so the cache should not contain an entry for it |
| 454 | 467 | testing.expectEqual(@as(?[64]u8, null), try ch.hit()); |