| ... | @@ -180,7 +180,8 @@ pub const CacheHash = struct { | ... | @@ -180,7 +180,8 @@ pub const CacheHash = struct { |
| 180 | } | 180 | } |
| 181 | | 181 | |
| 182 | var actual_digest: [BIN_DIGEST_LEN]u8 = undefined; | 182 | var actual_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 183 | try hash_file(self.alloc, &actual_digest, &this_file); | 183 | const contents = try hash_file(self.alloc, &actual_digest, &this_file); |
| | 184 | self.alloc.free(contents); |
| 184 | | 185 | |
| 185 | if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) { | 186 | if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) { |
| 186 | mem.copy(u8, &cache_hash_file.bin_digest, &actual_digest); | 187 | mem.copy(u8, &cache_hash_file.bin_digest, &actual_digest); |
| ... | @@ -211,7 +212,7 @@ pub const CacheHash = struct { | ... | @@ -211,7 +212,7 @@ pub const CacheHash = struct { |
| 211 | self.manifest_dirty = true; | 212 | self.manifest_dirty = true; |
| 212 | while (idx < input_file_count) : (idx += 1) { | 213 | while (idx < input_file_count) : (idx += 1) { |
| 213 | var cache_hash_file = &self.files.items[idx]; | 214 | var cache_hash_file = &self.files.items[idx]; |
| 214 | self.populate_file_hash(cache_hash_file) catch |err| { | 215 | const contents = self.populate_file_hash(cache_hash_file) catch |err| { |
| 215 | self.manifest_file.?.close(); | 216 | self.manifest_file.?.close(); |
| 216 | self.manifest_file = null; | 217 | self.manifest_file = null; |
| 217 | return error.CacheUnavailable; | 218 | return error.CacheUnavailable; |
| ... | @@ -223,7 +224,7 @@ pub const CacheHash = struct { | ... | @@ -223,7 +224,7 @@ pub const CacheHash = struct { |
| 223 | return self.final(); | 224 | return self.final(); |
| 224 | } | 225 | } |
| 225 | | 226 | |
| 226 | pub fn populate_file_hash(self: *@This(), cache_hash_file: *File) !void { | 227 | fn populate_file_hash_fetch(self: *@This(), otherAlloc: *mem.Allocator, cache_hash_file: *File) ![]u8 { |
| 227 | debug.assert(cache_hash_file.path != null); | 228 | debug.assert(cache_hash_file.path != null); |
| 228 | | 229 | |
| 229 | const this_file = try fs.cwd().openFile(cache_hash_file.path.?, .{}); | 230 | const this_file = try fs.cwd().openFile(cache_hash_file.path.?, .{}); |
| ... | @@ -236,8 +237,38 @@ pub const CacheHash = struct { | ... | @@ -236,8 +237,38 @@ pub const CacheHash = struct { |
| 236 | cache_hash_file.stat.inode = 0; | 237 | cache_hash_file.stat.inode = 0; |
| 237 | } | 238 | } |
| 238 | | 239 | |
| 239 | try hash_file(self.alloc, &cache_hash_file.bin_digest, &this_file); | 240 | const contents = try hash_file(otherAlloc, &cache_hash_file.bin_digest, &this_file); |
| 240 | self.blake3.update(&cache_hash_file.bin_digest); | 241 | self.blake3.update(&cache_hash_file.bin_digest); |
| | 242 | |
| | 243 | return contents; |
| | 244 | } |
| | 245 | |
| | 246 | fn populate_file_hash(self: *@This(), cache_hash_file: *File) !void { |
| | 247 | const contents = try self.populate_file_hash_fetch(self.alloc, cache_hash_file); |
| | 248 | self.alloc.free(contents); |
| | 249 | } |
| | 250 | |
| | 251 | /// Add a file as a dependency of process being cached, after the initial hash has been |
| | 252 | /// calculated. Returns the contents of the file, allocated with the given allocator. |
| | 253 | pub fn addFilePostFetch(self: *@This(), otherAlloc: *mem.Allocator, file_path: []const u8) ![]u8 { |
| | 254 | debug.assert(self.manifest_file != null); |
| | 255 | |
| | 256 | var cache_hash_file = try self.files.addOne(); |
| | 257 | cache_hash_file.path = try fs.path.resolve(self.alloc, &[_][]const u8{file_path}); |
| | 258 | |
| | 259 | const contents = self.populate_file_hash_fetch(otherAlloc, cache_hash_file) catch |err| { |
| | 260 | self.manifest_file.close(); |
| | 261 | return err; |
| | 262 | }; |
| | 263 | |
| | 264 | return contents; |
| | 265 | } |
| | 266 | |
| | 267 | /// Add a file as a dependency of process being cached, after the initial hash has been |
| | 268 | /// calculated. |
| | 269 | pub fn addFilePost(self: *@This(), file_path: []const u8) !void { |
| | 270 | const contents = try self.addFilePostFetch(self.alloc, file_path); |
| | 271 | self.alloc.free(contents); |
| 241 | } | 272 | } |
| 242 | | 273 | |
| 243 | /// Returns a base64 encoded hash of the inputs. | 274 | /// Returns a base64 encoded hash of the inputs. |
| ... | @@ -294,15 +325,17 @@ pub const CacheHash = struct { | ... | @@ -294,15 +325,17 @@ pub const CacheHash = struct { |
| 294 | } | 325 | } |
| 295 | }; | 326 | }; |
| 296 | | 327 | |
| 297 | fn hash_file(alloc: *Allocator, bin_digest: []u8, handle: *const fs.File) !void { | 328 | /// Hash the file, and return the contents as an array |
| | 329 | fn hash_file(alloc: *Allocator, bin_digest: []u8, handle: *const fs.File) ![]u8 { |
| 298 | var blake3 = Blake3.init(); | 330 | var blake3 = Blake3.init(); |
| 299 | | 331 | |
| 300 | const contents = try handle.inStream().readAllAlloc(alloc, 64 * 1024); | 332 | const contents = try handle.inStream().readAllAlloc(alloc, 64 * 1024); |
| 301 | defer alloc.free(contents); | | |
| 302 | | 333 | |
| 303 | blake3.update(contents); | 334 | blake3.update(contents); |
| 304 | | 335 | |
| 305 | blake3.final(bin_digest); | 336 | blake3.final(bin_digest); |
| | 337 | |
| | 338 | return contents; |
| 306 | } | 339 | } |
| 307 | | 340 | |
| 308 | /// If the wall clock time, rounded to the same precision as the | 341 | /// If the wall clock time, rounded to the same precision as the |