| ... | ... | @@ -180,7 +180,8 @@ pub const CacheHash = struct { |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 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 | 186 | if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) { |
| 186 | 187 | mem.copy(u8, &cache_hash_file.bin_digest, &actual_digest); |
| ... | ... | @@ -211,7 +212,7 @@ pub const CacheHash = struct { |
| 211 | 212 | self.manifest_dirty = true; |
| 212 | 213 | while (idx < input_file_count) : (idx += 1) { |
| 213 | 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 | 216 | self.manifest_file.?.close(); |
| 216 | 217 | self.manifest_file = null; |
| 217 | 218 | return error.CacheUnavailable; |
| ... | ... | @@ -223,7 +224,7 @@ pub const CacheHash = struct { |
| 223 | 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 | 228 | debug.assert(cache_hash_file.path != null); |
| 228 | 229 | |
| 229 | 230 | const this_file = try fs.cwd().openFile(cache_hash_file.path.?, .{}); |
| ... | ... | @@ -236,8 +237,38 @@ pub const CacheHash = struct { |
| 236 | 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 | 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 | 274 | /// Returns a base64 encoded hash of the inputs. |
| ... | ... | @@ -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 | 330 | var blake3 = Blake3.init(); |
| 299 | 331 | |
| 300 | 332 | const contents = try handle.inStream().readAllAlloc(alloc, 64 * 1024); |
| 301 | | defer alloc.free(contents); |
| 302 | 333 | |
| 303 | 334 | blake3.update(contents); |
| 304 | 335 | |
| 305 | 336 | blake3.final(bin_digest); |
| 337 | |
| 338 | return contents; |
| 306 | 339 | } |
| 307 | 340 | |
| 308 | 341 | /// If the wall clock time, rounded to the same precision as the |