| ... | @@ -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, |
| 22 | | 23 | |
| 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 | }; |
| 30 | | 35 | |
| ... | @@ -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` is | 82 | /// 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 matches | 83 | /// 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); |
| 82 | | 94 | |
| | 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}); |
| 85 | | 98 | |
| 86 | self.addSlice(cache_hash_file.path.?); | 99 | self.addSlice(cache_hash_file.path.?); |
| | 100 | |
| | 101 | return idx; |
| 87 | } | 102 | } |
| 88 | | 103 | |
| 89 | /// Check the cache to see if the input exists in it. If it exists, a base64 encoding | 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,8 +206,7 @@ pub const CacheHash = struct { |
| 191 | } | 206 | } |
| 192 | | 207 | |
| 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); | | |
| 196 | | 210 | |
| 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 | } |
| 254 | | 268 | |
| 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 | } |
| 259 | | 272 | |
| 260 | /// Add a file as a dependency of process being cached, after the initial hash has been | 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,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); |
| 385 | | 398 | |
| 386 | // There should be nothing in the cache | 399 | // 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); |
| 399 | | 412 | |
| 400 | // Cache hit! We just "built" the same file | 413 | // 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; |
| 434 | | 447 | |
| 435 | ch.add("1234"); | 448 | ch.add("1234"); |
| 436 | try ch.addFile(temp_file); | 449 | _ = try ch.addFile(temp_file); |
| 437 | | 450 | |
| 438 | // There should be nothing in the cache | 451 | // 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; |
| 449 | | 462 | |
| 450 | ch.add("1234"); | 463 | ch.add("1234"); |
| 451 | try ch.addFile(temp_file); | 464 | _ = try ch.addFile(temp_file); |
| 452 | | 465 | |
| 453 | // A file that we depend on has been updated, so the cache should not contain an entry for it | 466 | // 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()); |