| ... | ... | @@ -130,9 +130,14 @@ pub const CacheHash = struct { |
| 130 | 130 | // TODO: Open file with a file lock |
| 131 | 131 | self.manifest_file = try cwd.createFile(self.manifest_file_path.?, .{ .read = true, .truncate = false }); |
| 132 | 132 | |
| 133 | // create a buffer instead of using readAllAlloc |
| 134 | // See: https://github.com/ziglang/zig/issues/4656 |
| 135 | var file_buffer = try Buffer.initCapacity(self.alloc, 16 * 1024); |
| 136 | defer file_buffer.deinit(); |
| 137 | |
| 133 | 138 | // TODO: Figure out a good max value? |
| 134 | | const file_contents = try self.manifest_file.?.inStream().stream.readAllAlloc(self.alloc, 16 * 1024); |
| 135 | | defer self.alloc.free(file_contents); |
| 139 | try self.manifest_file.?.inStream().stream.readAllBuffer(&file_buffer, 16 * 1024); |
| 140 | const file_contents = file_buffer.toSliceConst(); |
| 136 | 141 | |
| 137 | 142 | const input_file_count = self.files.len; |
| 138 | 143 | var any_file_changed = false; |
| ... | ... | @@ -165,7 +170,6 @@ pub const CacheHash = struct { |
| 165 | 170 | if (cache_hash_file.path != null and !mem.eql(u8, file_path, cache_hash_file.path.?)) { |
| 166 | 171 | return error.InvalidFormat; |
| 167 | 172 | } |
| 168 | | cache_hash_file.path = try mem.dupe(self.alloc, u8, file_path); |
| 169 | 173 | |
| 170 | 174 | const this_file = cwd.openFile(cache_hash_file.path.?, .{ .read = true }) catch { |
| 171 | 175 | self.manifest_file.?.close(); |
| ... | ... | @@ -300,3 +304,45 @@ fn hash_file(alloc: *Allocator, bin_digest: []u8, handle: *const File) !void { |
| 300 | 304 | |
| 301 | 305 | blake3.final(bin_digest); |
| 302 | 306 | } |
| 307 | |
| 308 | test "cache file and the recall it" { |
| 309 | const cwd = fs.cwd(); |
| 310 | |
| 311 | const temp_manifest_dir = "temp_manifest_dir"; |
| 312 | |
| 313 | try cwd.writeFile("test.txt", "Hello, world!\n"); |
| 314 | |
| 315 | var digest1 = try ArrayList(u8).initCapacity(testing.allocator, 32); |
| 316 | defer digest1.deinit(); |
| 317 | var digest2 = try ArrayList(u8).initCapacity(testing.allocator, 32); |
| 318 | defer digest2.deinit(); |
| 319 | |
| 320 | { |
| 321 | var ch = try CacheHash.init(testing.allocator, temp_manifest_dir); |
| 322 | defer ch.release(); |
| 323 | |
| 324 | try ch.cache(@as(u16, 1234)); |
| 325 | try ch.cache_buf("1234"); |
| 326 | try ch.cache_file("test.txt"); |
| 327 | |
| 328 | // There should be nothing in the cache |
| 329 | debug.assert((try ch.hit(&digest1)) == false); |
| 330 | |
| 331 | try ch.final(&digest1); |
| 332 | } |
| 333 | { |
| 334 | var ch = try CacheHash.init(testing.allocator, temp_manifest_dir); |
| 335 | defer ch.release(); |
| 336 | |
| 337 | try ch.cache(@as(u16, 1234)); |
| 338 | try ch.cache_buf("1234"); |
| 339 | try ch.cache_file("test.txt"); |
| 340 | |
| 341 | // Cache hit! We just "built" the same file |
| 342 | debug.assert((try ch.hit(&digest2)) == true); |
| 343 | } |
| 344 | |
| 345 | debug.assert(mem.eql(u8, digest1.toSlice(), digest2.toSlice())); |
| 346 | |
| 347 | try cwd.deleteTree(temp_manifest_dir); |
| 348 | } |