| ... | ... | @@ -7,19 +7,18 @@ const std = @import("std.zig"); |
| 7 | 7 | const crypto = std.crypto; |
| 8 | 8 | const fs = std.fs; |
| 9 | 9 | const base64 = std.base64; |
| 10 | | const ArrayList = std.ArrayList; |
| 11 | 10 | const assert = std.debug.assert; |
| 12 | 11 | const testing = std.testing; |
| 13 | 12 | const mem = std.mem; |
| 14 | 13 | const fmt = std.fmt; |
| 15 | 14 | const Allocator = std.mem.Allocator; |
| 16 | 15 | |
| 17 | | const base64_encoder = fs.base64_encoder; |
| 18 | | const base64_decoder = fs.base64_decoder; |
| 16 | pub const base64_encoder = fs.base64_encoder; |
| 17 | pub const base64_decoder = fs.base64_decoder; |
| 19 | 18 | /// 16 would be 128 bits - Even with 2^54 cache entries, the probably of a collision would be under 10^-6 |
| 20 | 19 | /// We round up to 18 to avoid the `==` padding after base64 encoding. |
| 21 | | const BIN_DIGEST_LEN = 18; |
| 22 | | const BASE64_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN); |
| 20 | pub const BIN_DIGEST_LEN = 18; |
| 21 | pub const BASE64_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN); |
| 23 | 22 | |
| 24 | 23 | const MANIFEST_FILE_SIZE_MAX = 50 * 1024 * 1024; |
| 25 | 24 | |
| ... | ... | @@ -51,87 +50,105 @@ pub const File = struct { |
| 51 | 50 | } |
| 52 | 51 | }; |
| 53 | 52 | |
| 54 | | /// CacheHash manages project-local `zig-cache` directories. |
| 55 | | /// This is not a general-purpose cache. |
| 56 | | /// It is designed to be fast and simple, not to withstand attacks using specially-crafted input. |
| 57 | | pub const CacheHash = struct { |
| 58 | | allocator: *Allocator, |
| 59 | | /// Current state for incremental hashing. |
| 60 | | hasher: Hasher, |
| 53 | pub const Cache = struct { |
| 54 | gpa: *Allocator, |
| 61 | 55 | manifest_dir: fs.Dir, |
| 62 | | manifest_file: ?fs.File, |
| 63 | | manifest_dirty: bool, |
| 64 | | owns_manifest_dir: bool, |
| 65 | | files: ArrayList(File), |
| 66 | | b64_digest: [BASE64_DIGEST_LEN]u8, |
| 56 | hash: HashHelper = .{}, |
| 67 | 57 | |
| 68 | | /// Be sure to call release after successful initialization. |
| 69 | | pub fn init(allocator: *Allocator, dir: fs.Dir, manifest_dir_path: []const u8) !CacheHash { |
| 58 | /// Be sure to call `CacheHash.deinit` after successful initialization. |
| 59 | pub fn obtain(cache: *const Cache) CacheHash { |
| 70 | 60 | return CacheHash{ |
| 71 | | .allocator = allocator, |
| 72 | | .hasher = hasher_init, |
| 73 | | .manifest_dir = try dir.makeOpenPath(manifest_dir_path, .{}), |
| 61 | .cache = cache, |
| 62 | .hash = cache.hash, |
| 74 | 63 | .manifest_file = null, |
| 75 | 64 | .manifest_dirty = false, |
| 76 | | .owns_manifest_dir = true, |
| 77 | | .files = ArrayList(File).init(allocator), |
| 78 | 65 | .b64_digest = undefined, |
| 79 | 66 | }; |
| 80 | 67 | } |
| 68 | }; |
| 81 | 69 | |
| 82 | | /// Allows one to fork a CacheHash instance into another one, which does not require an additional |
| 83 | | /// directory handle to be opened. The new instance inherits the hash state. |
| 84 | | pub fn clone(self: CacheHash) CacheHash { |
| 85 | | assert(self.manifest_file == null); |
| 86 | | assert(files.items.len == 0); |
| 87 | | return .{ |
| 88 | | .allocator = self.allocator, |
| 89 | | .hasher = self.hasher, |
| 90 | | .manifest_dir = self.manifest_dir, |
| 91 | | .manifest_file = null, |
| 92 | | .manifest_dirty = false, |
| 93 | | .owns_manifest_dir = false, |
| 94 | | .files = ArrayList(File).init(allocator), |
| 95 | | .b64_digest = undefined, |
| 96 | | }; |
| 97 | | } |
| 70 | pub const HashHelper = struct { |
| 71 | hasher: Hasher = hasher_init, |
| 98 | 72 | |
| 99 | 73 | /// Record a slice of bytes as an dependency of the process being cached |
| 100 | | pub fn addBytes(self: *CacheHash, bytes: []const u8) void { |
| 101 | | assert(self.manifest_file == null); |
| 102 | | |
| 103 | | self.hasher.update(mem.asBytes(&bytes.len)); |
| 104 | | self.hasher.update(bytes); |
| 74 | pub fn addBytes(hh: *HashHelper, bytes: []const u8) void { |
| 75 | hh.hasher.update(mem.asBytes(&bytes.len)); |
| 76 | hh.hasher.update(bytes); |
| 105 | 77 | } |
| 106 | 78 | |
| 107 | | pub fn addListOfBytes(self: *CacheHash, list_of_bytes: []const []const u8) void { |
| 108 | | assert(self.manifest_file == null); |
| 79 | pub fn addOptionalBytes(hh: *HashHelper, optional_bytes: ?[]const u8) void { |
| 80 | hh.add(optional_bytes != null); |
| 81 | hh.addBytes(optional_bytes orelse return); |
| 82 | } |
| 109 | 83 | |
| 110 | | self.add(list_of_bytes.items.len); |
| 111 | | for (list_of_bytes) |bytes| self.addBytes(bytes); |
| 84 | pub fn addListOfBytes(hh: *HashHelper, list_of_bytes: []const []const u8) void { |
| 85 | hh.add(list_of_bytes.items.len); |
| 86 | for (list_of_bytes) |bytes| hh.addBytes(bytes); |
| 112 | 87 | } |
| 113 | 88 | |
| 114 | 89 | /// Convert the input value into bytes and record it as a dependency of the process being cached. |
| 115 | | pub fn add(self: *CacheHash, x: anytype) void { |
| 116 | | assert(self.manifest_file == null); |
| 117 | | |
| 90 | pub fn add(hh: *HashHelper, x: anytype) void { |
| 118 | 91 | switch (@TypeOf(x)) { |
| 119 | 92 | std.builtin.Version => { |
| 120 | | self.add(x.major); |
| 121 | | self.add(x.minor); |
| 122 | | self.add(x.patch); |
| 93 | hh.add(x.major); |
| 94 | hh.add(x.minor); |
| 95 | hh.add(x.patch); |
| 123 | 96 | return; |
| 124 | 97 | }, |
| 125 | 98 | else => {}, |
| 126 | 99 | } |
| 127 | 100 | |
| 128 | 101 | switch (@typeInfo(@TypeOf(x))) { |
| 129 | | .Bool, .Int, .Enum, .Array => self.addBytes(mem.asBytes(&x)), |
| 102 | .Bool, .Int, .Enum, .Array => hh.addBytes(mem.asBytes(&x)), |
| 130 | 103 | else => @compileError("unable to hash type " ++ @typeName(@TypeOf(x))), |
| 131 | 104 | } |
| 132 | 105 | } |
| 133 | 106 | |
| 134 | | /// Add a file as a dependency of process being cached. When `CacheHash.hit` is |
| 107 | pub fn addOptional(hh: *HashHelper, optional: anytype) void { |
| 108 | hh.add(optional != null); |
| 109 | hh.add(optional orelse return); |
| 110 | } |
| 111 | |
| 112 | /// Returns a base64 encoded hash of the inputs, without modifying state. |
| 113 | pub fn peek(hh: HashHelper) [BASE64_DIGEST_LEN]u8 { |
| 114 | var copy = hh; |
| 115 | return copy.final(); |
| 116 | } |
| 117 | |
| 118 | /// Returns a base64 encoded hash of the inputs, mutating the state of the hasher. |
| 119 | pub fn final(hh: *HashHelper) [BASE64_DIGEST_LEN]u8 { |
| 120 | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 121 | hh.hasher.final(&bin_digest); |
| 122 | |
| 123 | var out_digest: [BASE64_DIGEST_LEN]u8 = undefined; |
| 124 | base64_encoder.encode(&out_digest, &bin_digest); |
| 125 | |
| 126 | return out_digest; |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | pub const Lock = struct { |
| 131 | manifest_file: fs.File, |
| 132 | |
| 133 | pub fn release(lock: *Lock) void { |
| 134 | lock.manifest_file.close(); |
| 135 | lock.* = undefined; |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | /// CacheHash manages project-local `zig-cache` directories. |
| 140 | /// This is not a general-purpose cache. |
| 141 | /// It is designed to be fast and simple, not to withstand attacks using specially-crafted input. |
| 142 | pub const CacheHash = struct { |
| 143 | cache: *const Cache, |
| 144 | /// Current state for incremental hashing. |
| 145 | hash: HashHelper, |
| 146 | manifest_file: ?fs.File, |
| 147 | manifest_dirty: bool, |
| 148 | files: std.ArrayListUnmanaged(File) = .{}, |
| 149 | b64_digest: [BASE64_DIGEST_LEN]u8, |
| 150 | |
| 151 | /// Add a file as a dependency of process being cached. When `hit` is |
| 135 | 152 | /// called, the file's contents will be checked to ensure that it matches |
| 136 | 153 | /// the contents from previous times. |
| 137 | 154 | /// |
| ... | ... | @@ -139,8 +156,8 @@ pub const CacheHash = struct { |
| 139 | 156 | /// are allowed to take up in memory. If max_file_size is null, then the contents |
| 140 | 157 | /// will not be loaded into memory. |
| 141 | 158 | /// |
| 142 | | /// Returns the index of the entry in the `CacheHash.files` ArrayList. You can use it |
| 143 | | /// to access the contents of the file after calling `CacheHash.hit()` like so: |
| 159 | /// Returns the index of the entry in the `files` array list. You can use it |
| 160 | /// to access the contents of the file after calling `hit()` like so: |
| 144 | 161 | /// |
| 145 | 162 | /// ``` |
| 146 | 163 | /// var file_contents = cache_hash.files.items[file_index].contents.?; |
| ... | ... | @@ -148,8 +165,8 @@ pub const CacheHash = struct { |
| 148 | 165 | pub fn addFile(self: *CacheHash, file_path: []const u8, max_file_size: ?usize) !usize { |
| 149 | 166 | assert(self.manifest_file == null); |
| 150 | 167 | |
| 151 | | try self.files.ensureCapacity(self.files.items.len + 1); |
| 152 | | const resolved_path = try fs.path.resolve(self.allocator, &[_][]const u8{file_path}); |
| 168 | try self.files.ensureCapacity(self.cache.gpa, self.files.items.len + 1); |
| 169 | const resolved_path = try fs.path.resolve(self.cache.gpa, &[_][]const u8{file_path}); |
| 153 | 170 | |
| 154 | 171 | const idx = self.files.items.len; |
| 155 | 172 | self.files.addOneAssumeCapacity().* = .{ |
| ... | ... | @@ -160,35 +177,53 @@ pub const CacheHash = struct { |
| 160 | 177 | .bin_digest = undefined, |
| 161 | 178 | }; |
| 162 | 179 | |
| 163 | | self.addBytes(resolved_path); |
| 180 | self.hash.addBytes(resolved_path); |
| 164 | 181 | |
| 165 | 182 | return idx; |
| 166 | 183 | } |
| 167 | 184 | |
| 168 | | /// Check the cache to see if the input exists in it. If it exists, a base64 encoding |
| 169 | | /// of it's hash will be returned; otherwise, null will be returned. |
| 185 | pub fn addOptionalFile(self: *CacheHash, optional_file_path: ?[]const u8) !void { |
| 186 | self.hash.add(optional_file_path != null); |
| 187 | const file_path = optional_file_path orelse return; |
| 188 | _ = try self.addFile(file_path, null); |
| 189 | } |
| 190 | |
| 191 | pub fn addListOfFiles(self: *CacheHash, list_of_files: []const []const u8) !void { |
| 192 | self.hash.add(list_of_files.len); |
| 193 | for (list_of_files) |file_path| { |
| 194 | _ = try self.addFile(file_path, null); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /// Check the cache to see if the input exists in it. If it exists, returns `true`. |
| 199 | /// A base64 encoding of its hash is available by calling `final`. |
| 170 | 200 | /// |
| 171 | 201 | /// This function will also acquire an exclusive lock to the manifest file. This means |
| 172 | 202 | /// that a process holding a CacheHash will block any other process attempting to |
| 173 | 203 | /// acquire the lock. |
| 174 | 204 | /// |
| 175 | | /// The lock on the manifest file is released when `CacheHash.release` is called. |
| 176 | | pub fn hit(self: *CacheHash) !?[BASE64_DIGEST_LEN]u8 { |
| 205 | /// The lock on the manifest file is released when `deinit` is called. As another |
| 206 | /// option, one may call `toOwnedLock` to obtain a smaller object which can represent |
| 207 | /// the lock. `deinit` is safe to call whether or not `toOwnedLock` has been called. |
| 208 | pub fn hit(self: *CacheHash) !bool { |
| 177 | 209 | assert(self.manifest_file == null); |
| 178 | 210 | |
| 211 | const ext = ".txt"; |
| 212 | var manifest_file_path: [self.b64_digest.len + ext.len]u8 = undefined; |
| 213 | |
| 179 | 214 | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 180 | | self.hasher.final(&bin_digest); |
| 215 | self.hash.hasher.final(&bin_digest); |
| 181 | 216 | |
| 182 | 217 | base64_encoder.encode(self.b64_digest[0..], &bin_digest); |
| 183 | 218 | |
| 184 | | self.hasher = hasher_init; |
| 185 | | self.hasher.update(&bin_digest); |
| 219 | self.hash.hasher = hasher_init; |
| 220 | self.hash.hasher.update(&bin_digest); |
| 186 | 221 | |
| 187 | | const manifest_file_path = try fmt.allocPrint(self.allocator, "{}.txt", .{self.b64_digest}); |
| 188 | | defer self.allocator.free(manifest_file_path); |
| 222 | mem.copy(u8, &manifest_file_path, &self.b64_digest); |
| 223 | manifest_file_path[self.b64_digest.len..][0..ext.len].* = ext.*; |
| 189 | 224 | |
| 190 | 225 | if (self.files.items.len != 0) { |
| 191 | | self.manifest_file = try self.manifest_dir.createFile(manifest_file_path, .{ |
| 226 | self.manifest_file = try self.cache.manifest_dir.createFile(&manifest_file_path, .{ |
| 192 | 227 | .read = true, |
| 193 | 228 | .truncate = false, |
| 194 | 229 | .lock = .Exclusive, |
| ... | ... | @@ -196,26 +231,26 @@ pub const CacheHash = struct { |
| 196 | 231 | } else { |
| 197 | 232 | // If there are no file inputs, we check if the manifest file exists instead of |
| 198 | 233 | // comparing the hashes on the files used for the cached item |
| 199 | | self.manifest_file = self.manifest_dir.openFile(manifest_file_path, .{ |
| 234 | self.manifest_file = self.cache.manifest_dir.openFile(&manifest_file_path, .{ |
| 200 | 235 | .read = true, |
| 201 | 236 | .write = true, |
| 202 | 237 | .lock = .Exclusive, |
| 203 | 238 | }) catch |err| switch (err) { |
| 204 | 239 | error.FileNotFound => { |
| 205 | 240 | self.manifest_dirty = true; |
| 206 | | self.manifest_file = try self.manifest_dir.createFile(manifest_file_path, .{ |
| 241 | self.manifest_file = try self.cache.manifest_dir.createFile(&manifest_file_path, .{ |
| 207 | 242 | .read = true, |
| 208 | 243 | .truncate = false, |
| 209 | 244 | .lock = .Exclusive, |
| 210 | 245 | }); |
| 211 | | return null; |
| 246 | return false; |
| 212 | 247 | }, |
| 213 | 248 | else => |e| return e, |
| 214 | 249 | }; |
| 215 | 250 | } |
| 216 | 251 | |
| 217 | | const file_contents = try self.manifest_file.?.inStream().readAllAlloc(self.allocator, MANIFEST_FILE_SIZE_MAX); |
| 218 | | defer self.allocator.free(file_contents); |
| 252 | const file_contents = try self.manifest_file.?.inStream().readAllAlloc(self.cache.gpa, MANIFEST_FILE_SIZE_MAX); |
| 253 | defer self.cache.gpa.free(file_contents); |
| 219 | 254 | |
| 220 | 255 | const input_file_count = self.files.items.len; |
| 221 | 256 | var any_file_changed = false; |
| ... | ... | @@ -225,7 +260,7 @@ pub const CacheHash = struct { |
| 225 | 260 | defer idx += 1; |
| 226 | 261 | |
| 227 | 262 | const cache_hash_file = if (idx < input_file_count) &self.files.items[idx] else blk: { |
| 228 | | const new = try self.files.addOne(); |
| 263 | const new = try self.files.addOne(self.cache.gpa); |
| 229 | 264 | new.* = .{ |
| 230 | 265 | .path = null, |
| 231 | 266 | .contents = null, |
| ... | ... | @@ -258,7 +293,7 @@ pub const CacheHash = struct { |
| 258 | 293 | } |
| 259 | 294 | |
| 260 | 295 | if (cache_hash_file.path == null) { |
| 261 | | cache_hash_file.path = try self.allocator.dupe(u8, file_path); |
| 296 | cache_hash_file.path = try self.cache.gpa.dupe(u8, file_path); |
| 262 | 297 | } |
| 263 | 298 | |
| 264 | 299 | const this_file = fs.cwd().openFile(cache_hash_file.path.?, .{ .read = true }) catch { |
| ... | ... | @@ -292,7 +327,7 @@ pub const CacheHash = struct { |
| 292 | 327 | } |
| 293 | 328 | |
| 294 | 329 | if (!any_file_changed) { |
| 295 | | self.hasher.update(&cache_hash_file.bin_digest); |
| 330 | self.hash.hasher.update(&cache_hash_file.bin_digest); |
| 296 | 331 | } |
| 297 | 332 | } |
| 298 | 333 | |
| ... | ... | @@ -300,19 +335,19 @@ pub const CacheHash = struct { |
| 300 | 335 | // cache miss |
| 301 | 336 | // keep the manifest file open |
| 302 | 337 | // reset the hash |
| 303 | | self.hasher = hasher_init; |
| 304 | | self.hasher.update(&bin_digest); |
| 338 | self.hash.hasher = hasher_init; |
| 339 | self.hash.hasher.update(&bin_digest); |
| 305 | 340 | |
| 306 | 341 | // Remove files not in the initial hash |
| 307 | 342 | for (self.files.items[input_file_count..]) |*file| { |
| 308 | | file.deinit(self.allocator); |
| 343 | file.deinit(self.cache.gpa); |
| 309 | 344 | } |
| 310 | | self.files.shrink(input_file_count); |
| 345 | self.files.shrinkRetainingCapacity(input_file_count); |
| 311 | 346 | |
| 312 | 347 | for (self.files.items) |file| { |
| 313 | | self.hasher.update(&file.bin_digest); |
| 348 | self.hash.hasher.update(&file.bin_digest); |
| 314 | 349 | } |
| 315 | | return null; |
| 350 | return false; |
| 316 | 351 | } |
| 317 | 352 | |
| 318 | 353 | if (idx < input_file_count) { |
| ... | ... | @@ -321,10 +356,10 @@ pub const CacheHash = struct { |
| 321 | 356 | const ch_file = &self.files.items[idx]; |
| 322 | 357 | try self.populateFileHash(ch_file); |
| 323 | 358 | } |
| 324 | | return null; |
| 359 | return false; |
| 325 | 360 | } |
| 326 | 361 | |
| 327 | | return self.final(); |
| 362 | return true; |
| 328 | 363 | } |
| 329 | 364 | |
| 330 | 365 | fn populateFileHash(self: *CacheHash, ch_file: *File) !void { |
| ... | ... | @@ -343,8 +378,8 @@ pub const CacheHash = struct { |
| 343 | 378 | return error.FileTooBig; |
| 344 | 379 | } |
| 345 | 380 | |
| 346 | | const contents = try self.allocator.alloc(u8, @intCast(usize, ch_file.stat.size)); |
| 347 | | errdefer self.allocator.free(contents); |
| 381 | const contents = try self.cache.gpa.alloc(u8, @intCast(usize, ch_file.stat.size)); |
| 382 | errdefer self.cache.gpa.free(contents); |
| 348 | 383 | |
| 349 | 384 | // Hash while reading from disk, to keep the contents in the cpu cache while |
| 350 | 385 | // doing hashing. |
| ... | ... | @@ -364,7 +399,7 @@ pub const CacheHash = struct { |
| 364 | 399 | try hashFile(file, &ch_file.bin_digest); |
| 365 | 400 | } |
| 366 | 401 | |
| 367 | | self.hasher.update(&ch_file.bin_digest); |
| 402 | self.hash.hasher.update(&ch_file.bin_digest); |
| 368 | 403 | } |
| 369 | 404 | |
| 370 | 405 | /// Add a file as a dependency of process being cached, after the initial hash has been |
| ... | ... | @@ -374,10 +409,10 @@ pub const CacheHash = struct { |
| 374 | 409 | pub fn addFilePostFetch(self: *CacheHash, file_path: []const u8, max_file_size: usize) ![]u8 { |
| 375 | 410 | assert(self.manifest_file != null); |
| 376 | 411 | |
| 377 | | const resolved_path = try fs.path.resolve(self.allocator, &[_][]const u8{file_path}); |
| 378 | | errdefer self.allocator.free(resolved_path); |
| 412 | const resolved_path = try fs.path.resolve(self.cache.gpa, &[_][]const u8{file_path}); |
| 413 | errdefer self.cache.gpa.free(resolved_path); |
| 379 | 414 | |
| 380 | | const new_ch_file = try self.files.addOne(); |
| 415 | const new_ch_file = try self.files.addOne(self.cache.gpa); |
| 381 | 416 | new_ch_file.* = .{ |
| 382 | 417 | .path = resolved_path, |
| 383 | 418 | .max_file_size = max_file_size, |
| ... | ... | @@ -385,7 +420,7 @@ pub const CacheHash = struct { |
| 385 | 420 | .bin_digest = undefined, |
| 386 | 421 | .contents = null, |
| 387 | 422 | }; |
| 388 | | errdefer self.files.shrink(self.files.items.len - 1); |
| 423 | errdefer self.files.shrinkRetainingCapacity(self.files.items.len - 1); |
| 389 | 424 | |
| 390 | 425 | try self.populateFileHash(new_ch_file); |
| 391 | 426 | |
| ... | ... | @@ -399,10 +434,10 @@ pub const CacheHash = struct { |
| 399 | 434 | pub fn addFilePost(self: *CacheHash, file_path: []const u8) !void { |
| 400 | 435 | assert(self.manifest_file != null); |
| 401 | 436 | |
| 402 | | const resolved_path = try fs.path.resolve(self.allocator, &[_][]const u8{file_path}); |
| 403 | | errdefer self.allocator.free(resolved_path); |
| 437 | const resolved_path = try fs.path.resolve(self.cache.gpa, &[_][]const u8{file_path}); |
| 438 | errdefer self.cache.gpa.free(resolved_path); |
| 404 | 439 | |
| 405 | | const new_ch_file = try self.files.addOne(); |
| 440 | const new_ch_file = try self.files.addOne(self.cache.gpa); |
| 406 | 441 | new_ch_file.* = .{ |
| 407 | 442 | .path = resolved_path, |
| 408 | 443 | .max_file_size = null, |
| ... | ... | @@ -410,7 +445,7 @@ pub const CacheHash = struct { |
| 410 | 445 | .bin_digest = undefined, |
| 411 | 446 | .contents = null, |
| 412 | 447 | }; |
| 413 | | errdefer self.files.shrink(self.files.items.len - 1); |
| 448 | errdefer self.files.shrinkRetainingCapacity(self.files.items.len - 1); |
| 414 | 449 | |
| 415 | 450 | try self.populateFileHash(new_ch_file); |
| 416 | 451 | } |
| ... | ... | @@ -426,7 +461,7 @@ pub const CacheHash = struct { |
| 426 | 461 | // the artifacts to cache. |
| 427 | 462 | |
| 428 | 463 | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 429 | | self.hasher.final(&bin_digest); |
| 464 | self.hash.hasher.final(&bin_digest); |
| 430 | 465 | |
| 431 | 466 | var out_digest: [BASE64_DIGEST_LEN]u8 = undefined; |
| 432 | 467 | base64_encoder.encode(&out_digest, &bin_digest); |
| ... | ... | @@ -436,45 +471,48 @@ pub const CacheHash = struct { |
| 436 | 471 | |
| 437 | 472 | pub fn writeManifest(self: *CacheHash) !void { |
| 438 | 473 | assert(self.manifest_file != null); |
| 474 | if (!self.manifest_dirty) return; |
| 439 | 475 | |
| 440 | 476 | var encoded_digest: [BASE64_DIGEST_LEN]u8 = undefined; |
| 441 | | var contents = ArrayList(u8).init(self.allocator); |
| 442 | | var outStream = contents.outStream(); |
| 477 | var contents = std.ArrayList(u8).init(self.cache.gpa); |
| 478 | var writer = contents.writer(); |
| 443 | 479 | defer contents.deinit(); |
| 444 | 480 | |
| 445 | 481 | for (self.files.items) |file| { |
| 446 | 482 | base64_encoder.encode(encoded_digest[0..], &file.bin_digest); |
| 447 | | try outStream.print("{} {} {} {} {}\n", .{ file.stat.size, file.stat.inode, file.stat.mtime, encoded_digest[0..], file.path }); |
| 483 | try writer.print("{} {} {} {} {}\n", .{ |
| 484 | file.stat.size, |
| 485 | file.stat.inode, |
| 486 | file.stat.mtime, |
| 487 | encoded_digest[0..], |
| 488 | file.path, |
| 489 | }); |
| 448 | 490 | } |
| 449 | 491 | |
| 450 | 492 | try self.manifest_file.?.pwriteAll(contents.items, 0); |
| 451 | 493 | self.manifest_dirty = false; |
| 452 | 494 | } |
| 453 | 495 | |
| 496 | /// Obtain only the data needed to maintain a lock on the manifest file. |
| 497 | /// The `CacheHash` remains safe to deinit. |
| 498 | /// Don't forget to call `writeManifest` before this! |
| 499 | pub fn toOwnedLock(self: *CacheHash) Lock { |
| 500 | const manifest_file = self.manifest_file.?; |
| 501 | self.manifest_file = null; |
| 502 | return Lock{ .manifest_file = manifest_file }; |
| 503 | } |
| 504 | |
| 454 | 505 | /// Releases the manifest file and frees any memory the CacheHash was using. |
| 455 | 506 | /// `CacheHash.hit` must be called first. |
| 456 | | /// |
| 457 | | /// Will also attempt to write to the manifest file if the manifest is dirty. |
| 458 | | /// Writing to the manifest file can fail, but this function ignores those errors. |
| 459 | | /// To detect failures from writing the manifest, one may explicitly call |
| 460 | | /// `writeManifest` before `release`. |
| 461 | | pub fn release(self: *CacheHash) void { |
| 507 | /// Don't forget to call `writeManifest` before this! |
| 508 | pub fn deinit(self: *CacheHash) void { |
| 462 | 509 | if (self.manifest_file) |file| { |
| 463 | | if (self.manifest_dirty) { |
| 464 | | // To handle these errors, API users should call |
| 465 | | // writeManifest before release(). |
| 466 | | self.writeManifest() catch {}; |
| 467 | | } |
| 468 | | |
| 469 | 510 | file.close(); |
| 470 | 511 | } |
| 471 | | |
| 472 | 512 | for (self.files.items) |*file| { |
| 473 | | file.deinit(self.allocator); |
| 513 | file.deinit(self.cache.gpa); |
| 474 | 514 | } |
| 475 | | self.files.deinit(); |
| 476 | | if (self.owns_manifest_dir) |
| 477 | | self.manifest_dir.close(); |
| 515 | self.files.deinit(self.cache.gpa); |
| 478 | 516 | } |
| 479 | 517 | }; |
| 480 | 518 | |
| ... | ... | @@ -542,31 +580,41 @@ test "cache file and then recall it" { |
| 542 | 580 | var digest1: [BASE64_DIGEST_LEN]u8 = undefined; |
| 543 | 581 | var digest2: [BASE64_DIGEST_LEN]u8 = undefined; |
| 544 | 582 | |
| 583 | var cache = Cache{ |
| 584 | .gpa = testing.allocator, |
| 585 | .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}), |
| 586 | }; |
| 587 | defer cache.manifest_dir.close(); |
| 588 | |
| 545 | 589 | { |
| 546 | | var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir); |
| 547 | | defer ch.release(); |
| 590 | var ch = cache.obtain(); |
| 591 | defer ch.deinit(); |
| 548 | 592 | |
| 549 | | ch.add(true); |
| 550 | | ch.add(@as(u16, 1234)); |
| 551 | | ch.addBytes("1234"); |
| 593 | ch.hash.add(true); |
| 594 | ch.hash.add(@as(u16, 1234)); |
| 595 | ch.hash.addBytes("1234"); |
| 552 | 596 | _ = try ch.addFile(temp_file, null); |
| 553 | 597 | |
| 554 | 598 | // There should be nothing in the cache |
| 555 | | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 599 | testing.expectEqual(false, try ch.hit()); |
| 556 | 600 | |
| 557 | 601 | digest1 = ch.final(); |
| 602 | try ch.writeManifest(); |
| 558 | 603 | } |
| 559 | 604 | { |
| 560 | | var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir); |
| 561 | | defer ch.release(); |
| 605 | var ch = cache.obtain(); |
| 606 | defer ch.deinit(); |
| 562 | 607 | |
| 563 | | ch.add(true); |
| 564 | | ch.add(@as(u16, 1234)); |
| 565 | | ch.addBytes("1234"); |
| 608 | ch.hash.add(true); |
| 609 | ch.hash.add(@as(u16, 1234)); |
| 610 | ch.hash.addBytes("1234"); |
| 566 | 611 | _ = try ch.addFile(temp_file, null); |
| 567 | 612 | |
| 568 | 613 | // Cache hit! We just "built" the same file |
| 569 | | digest2 = (try ch.hit()).?; |
| 614 | testing.expect(try ch.hit()); |
| 615 | digest2 = ch.final(); |
| 616 | |
| 617 | try ch.writeManifest(); |
| 570 | 618 | } |
| 571 | 619 | |
| 572 | 620 | testing.expectEqual(digest1, digest2); |
| ... | ... | @@ -612,37 +660,47 @@ test "check that changing a file makes cache fail" { |
| 612 | 660 | var digest1: [BASE64_DIGEST_LEN]u8 = undefined; |
| 613 | 661 | var digest2: [BASE64_DIGEST_LEN]u8 = undefined; |
| 614 | 662 | |
| 663 | var cache = Cache{ |
| 664 | .gpa = testing.allocator, |
| 665 | .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}), |
| 666 | }; |
| 667 | defer cache.manifest_dir.close(); |
| 668 | |
| 615 | 669 | { |
| 616 | | var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir); |
| 617 | | defer ch.release(); |
| 670 | var ch = cache.obtain(); |
| 671 | defer ch.deinit(); |
| 618 | 672 | |
| 619 | | ch.addBytes("1234"); |
| 673 | ch.hash.addBytes("1234"); |
| 620 | 674 | const temp_file_idx = try ch.addFile(temp_file, 100); |
| 621 | 675 | |
| 622 | 676 | // There should be nothing in the cache |
| 623 | | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 677 | testing.expectEqual(false, try ch.hit()); |
| 624 | 678 | |
| 625 | 679 | testing.expect(mem.eql(u8, original_temp_file_contents, ch.files.items[temp_file_idx].contents.?)); |
| 626 | 680 | |
| 627 | 681 | digest1 = ch.final(); |
| 682 | |
| 683 | try ch.writeManifest(); |
| 628 | 684 | } |
| 629 | 685 | |
| 630 | 686 | try cwd.writeFile(temp_file, updated_temp_file_contents); |
| 631 | 687 | |
| 632 | 688 | { |
| 633 | | var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir); |
| 634 | | defer ch.release(); |
| 689 | var ch = cache.obtain(); |
| 690 | defer ch.deinit(); |
| 635 | 691 | |
| 636 | | ch.addBytes("1234"); |
| 692 | ch.hash.addBytes("1234"); |
| 637 | 693 | const temp_file_idx = try ch.addFile(temp_file, 100); |
| 638 | 694 | |
| 639 | 695 | // A file that we depend on has been updated, so the cache should not contain an entry for it |
| 640 | | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 696 | testing.expectEqual(false, try ch.hit()); |
| 641 | 697 | |
| 642 | 698 | // The cache system does not keep the contents of re-hashed input files. |
| 643 | 699 | testing.expect(ch.files.items[temp_file_idx].contents == null); |
| 644 | 700 | |
| 645 | 701 | digest2 = ch.final(); |
| 702 | |
| 703 | try ch.writeManifest(); |
| 646 | 704 | } |
| 647 | 705 | |
| 648 | 706 | testing.expect(!mem.eql(u8, digest1[0..], digest2[0..])); |
| ... | ... | @@ -663,24 +721,34 @@ test "no file inputs" { |
| 663 | 721 | var digest1: [BASE64_DIGEST_LEN]u8 = undefined; |
| 664 | 722 | var digest2: [BASE64_DIGEST_LEN]u8 = undefined; |
| 665 | 723 | |
| 724 | var cache = Cache{ |
| 725 | .gpa = testing.allocator, |
| 726 | .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}), |
| 727 | }; |
| 728 | defer cache.manifest_dir.close(); |
| 729 | |
| 666 | 730 | { |
| 667 | | var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir); |
| 668 | | defer ch.release(); |
| 731 | var ch = cache.obtain(); |
| 732 | defer ch.deinit(); |
| 669 | 733 | |
| 670 | | ch.addBytes("1234"); |
| 734 | ch.hash.addBytes("1234"); |
| 671 | 735 | |
| 672 | 736 | // There should be nothing in the cache |
| 673 | | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 737 | testing.expectEqual(false, try ch.hit()); |
| 674 | 738 | |
| 675 | 739 | digest1 = ch.final(); |
| 740 | |
| 741 | try ch.writeManifest(); |
| 676 | 742 | } |
| 677 | 743 | { |
| 678 | | var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir); |
| 679 | | defer ch.release(); |
| 744 | var ch = cache.obtain(); |
| 745 | defer ch.deinit(); |
| 680 | 746 | |
| 681 | | ch.addBytes("1234"); |
| 747 | ch.hash.addBytes("1234"); |
| 682 | 748 | |
| 683 | | digest2 = (try ch.hit()).?; |
| 749 | testing.expect(try ch.hit()); |
| 750 | digest2 = ch.final(); |
| 751 | try ch.writeManifest(); |
| 684 | 752 | } |
| 685 | 753 | |
| 686 | 754 | testing.expectEqual(digest1, digest2); |
| ... | ... | @@ -709,28 +777,38 @@ test "CacheHashes with files added after initial hash work" { |
| 709 | 777 | var digest2: [BASE64_DIGEST_LEN]u8 = undefined; |
| 710 | 778 | var digest3: [BASE64_DIGEST_LEN]u8 = undefined; |
| 711 | 779 | |
| 780 | var cache = Cache{ |
| 781 | .gpa = testing.allocator, |
| 782 | .manifest_dir = try cwd.makeOpenPath(temp_manifest_dir, .{}), |
| 783 | }; |
| 784 | defer cache.manifest_dir.close(); |
| 785 | |
| 712 | 786 | { |
| 713 | | var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir); |
| 714 | | defer ch.release(); |
| 787 | var ch = cache.obtain(); |
| 788 | defer ch.deinit(); |
| 715 | 789 | |
| 716 | | ch.addBytes("1234"); |
| 790 | ch.hash.addBytes("1234"); |
| 717 | 791 | _ = try ch.addFile(temp_file1, null); |
| 718 | 792 | |
| 719 | 793 | // There should be nothing in the cache |
| 720 | | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 794 | testing.expectEqual(false, try ch.hit()); |
| 721 | 795 | |
| 722 | 796 | _ = try ch.addFilePost(temp_file2); |
| 723 | 797 | |
| 724 | 798 | digest1 = ch.final(); |
| 799 | try ch.writeManifest(); |
| 725 | 800 | } |
| 726 | 801 | { |
| 727 | | var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir); |
| 728 | | defer ch.release(); |
| 802 | var ch = cache.obtain(); |
| 803 | defer ch.deinit(); |
| 729 | 804 | |
| 730 | | ch.addBytes("1234"); |
| 805 | ch.hash.addBytes("1234"); |
| 731 | 806 | _ = try ch.addFile(temp_file1, null); |
| 732 | 807 | |
| 733 | | digest2 = (try ch.hit()).?; |
| 808 | testing.expect(try ch.hit()); |
| 809 | digest2 = ch.final(); |
| 810 | |
| 811 | try ch.writeManifest(); |
| 734 | 812 | } |
| 735 | 813 | testing.expect(mem.eql(u8, &digest1, &digest2)); |
| 736 | 814 | |
| ... | ... | @@ -743,18 +821,20 @@ test "CacheHashes with files added after initial hash work" { |
| 743 | 821 | } |
| 744 | 822 | |
| 745 | 823 | { |
| 746 | | var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir); |
| 747 | | defer ch.release(); |
| 824 | var ch = cache.obtain(); |
| 825 | defer ch.deinit(); |
| 748 | 826 | |
| 749 | | ch.addBytes("1234"); |
| 827 | ch.hash.addBytes("1234"); |
| 750 | 828 | _ = try ch.addFile(temp_file1, null); |
| 751 | 829 | |
| 752 | 830 | // A file that we depend on has been updated, so the cache should not contain an entry for it |
| 753 | | testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit()); |
| 831 | testing.expectEqual(false, try ch.hit()); |
| 754 | 832 | |
| 755 | 833 | _ = try ch.addFilePost(temp_file2); |
| 756 | 834 | |
| 757 | 835 | digest3 = ch.final(); |
| 836 | |
| 837 | try ch.writeManifest(); |
| 758 | 838 | } |
| 759 | 839 | |
| 760 | 840 | testing.expect(!mem.eql(u8, &digest1, &digest3)); |