| ... | ... | @@ -6,7 +6,6 @@ const Cache = @This(); |
| 6 | 6 | const std = @import("std"); |
| 7 | 7 | const crypto = std.crypto; |
| 8 | 8 | const fs = std.fs; |
| 9 | | const base64 = std.base64; |
| 10 | 9 | const assert = std.debug.assert; |
| 11 | 10 | const testing = std.testing; |
| 12 | 11 | const mem = std.mem; |
| ... | ... | @@ -20,18 +19,15 @@ pub fn obtain(cache: *const Cache) CacheHash { |
| 20 | 19 | .hash = cache.hash, |
| 21 | 20 | .manifest_file = null, |
| 22 | 21 | .manifest_dirty = false, |
| 23 | | .b64_digest = undefined, |
| 22 | .hex_digest = undefined, |
| 24 | 23 | }; |
| 25 | 24 | } |
| 26 | 25 | |
| 27 | | pub const base64_encoder = fs.base64_encoder; |
| 28 | | pub const base64_decoder = fs.base64_decoder; |
| 29 | 26 | /// This is 128 bits - Even with 2^54 cache entries, the probably of a collision would be under 10^-6 |
| 30 | | /// Currently we use SipHash and so this value must be 16 not any higher. |
| 31 | | pub const BIN_DIGEST_LEN = 16; |
| 32 | | pub const BASE64_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN); |
| 27 | pub const bin_digest_len = 16; |
| 28 | pub const hex_digest_len = bin_digest_len * 2; |
| 33 | 29 | |
| 34 | | const MANIFEST_FILE_SIZE_MAX = 50 * 1024 * 1024; |
| 30 | const manifest_file_size_max = 50 * 1024 * 1024; |
| 35 | 31 | |
| 36 | 32 | /// The type used for hashing file contents. Currently, this is SipHash128(1, 3), because it |
| 37 | 33 | /// provides enough collision resistance for the CacheHash use cases, while being one of our |
| ... | ... | @@ -45,7 +41,7 @@ pub const File = struct { |
| 45 | 41 | path: ?[]const u8, |
| 46 | 42 | max_file_size: ?usize, |
| 47 | 43 | stat: fs.File.Stat, |
| 48 | | bin_digest: [BIN_DIGEST_LEN]u8, |
| 44 | bin_digest: [bin_digest_len]u8, |
| 49 | 45 | contents: ?[]const u8, |
| 50 | 46 | |
| 51 | 47 | pub fn deinit(self: *File, allocator: *Allocator) void { |
| ... | ... | @@ -118,20 +114,19 @@ pub const HashHelper = struct { |
| 118 | 114 | hh.add(optional orelse return); |
| 119 | 115 | } |
| 120 | 116 | |
| 121 | | /// Returns a base64 encoded hash of the inputs, without modifying state. |
| 122 | | pub fn peek(hh: HashHelper) [BASE64_DIGEST_LEN]u8 { |
| 117 | /// Returns a hex encoded hash of the inputs, without modifying state. |
| 118 | pub fn peek(hh: HashHelper) [hex_digest_len]u8 { |
| 123 | 119 | var copy = hh; |
| 124 | 120 | return copy.final(); |
| 125 | 121 | } |
| 126 | 122 | |
| 127 | | /// Returns a base64 encoded hash of the inputs, mutating the state of the hasher. |
| 128 | | pub fn final(hh: *HashHelper) [BASE64_DIGEST_LEN]u8 { |
| 129 | | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 123 | /// Returns a hex encoded hash of the inputs, mutating the state of the hasher. |
| 124 | pub fn final(hh: *HashHelper) [hex_digest_len]u8 { |
| 125 | var bin_digest: [bin_digest_len]u8 = undefined; |
| 130 | 126 | hh.hasher.final(&bin_digest); |
| 131 | 127 | |
| 132 | | var out_digest: [BASE64_DIGEST_LEN]u8 = undefined; |
| 133 | | base64_encoder.encode(&out_digest, &bin_digest); |
| 134 | | |
| 128 | var out_digest: [hex_digest_len]u8 = undefined; |
| 129 | _ = std.fmt.bufPrint(&out_digest, "{x}", .{bin_digest}) catch unreachable; |
| 135 | 130 | return out_digest; |
| 136 | 131 | } |
| 137 | 132 | }; |
| ... | ... | @@ -155,7 +150,7 @@ pub const CacheHash = struct { |
| 155 | 150 | manifest_file: ?fs.File, |
| 156 | 151 | manifest_dirty: bool, |
| 157 | 152 | files: std.ArrayListUnmanaged(File) = .{}, |
| 158 | | b64_digest: [BASE64_DIGEST_LEN]u8, |
| 153 | hex_digest: [hex_digest_len]u8, |
| 159 | 154 | |
| 160 | 155 | /// Add a file as a dependency of process being cached. When `hit` is |
| 161 | 156 | /// called, the file's contents will be checked to ensure that it matches |
| ... | ... | @@ -205,7 +200,7 @@ pub const CacheHash = struct { |
| 205 | 200 | } |
| 206 | 201 | |
| 207 | 202 | /// Check the cache to see if the input exists in it. If it exists, returns `true`. |
| 208 | | /// A base64 encoding of its hash is available by calling `final`. |
| 203 | /// A hex encoding of its hash is available by calling `final`. |
| 209 | 204 | /// |
| 210 | 205 | /// This function will also acquire an exclusive lock to the manifest file. This means |
| 211 | 206 | /// that a process holding a CacheHash will block any other process attempting to |
| ... | ... | @@ -218,18 +213,18 @@ pub const CacheHash = struct { |
| 218 | 213 | assert(self.manifest_file == null); |
| 219 | 214 | |
| 220 | 215 | const ext = ".txt"; |
| 221 | | var manifest_file_path: [self.b64_digest.len + ext.len]u8 = undefined; |
| 216 | var manifest_file_path: [self.hex_digest.len + ext.len]u8 = undefined; |
| 222 | 217 | |
| 223 | | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 218 | var bin_digest: [bin_digest_len]u8 = undefined; |
| 224 | 219 | self.hash.hasher.final(&bin_digest); |
| 225 | 220 | |
| 226 | | base64_encoder.encode(self.b64_digest[0..], &bin_digest); |
| 221 | _ = std.fmt.bufPrint(&self.hex_digest, "{x}", .{bin_digest}) catch unreachable; |
| 227 | 222 | |
| 228 | 223 | self.hash.hasher = hasher_init; |
| 229 | 224 | self.hash.hasher.update(&bin_digest); |
| 230 | 225 | |
| 231 | | mem.copy(u8, &manifest_file_path, &self.b64_digest); |
| 232 | | manifest_file_path[self.b64_digest.len..][0..ext.len].* = ext.*; |
| 226 | mem.copy(u8, &manifest_file_path, &self.hex_digest); |
| 227 | manifest_file_path[self.hex_digest.len..][0..ext.len].* = ext.*; |
| 233 | 228 | |
| 234 | 229 | if (self.files.items.len != 0) { |
| 235 | 230 | self.manifest_file = try self.cache.manifest_dir.createFile(&manifest_file_path, .{ |
| ... | ... | @@ -258,7 +253,7 @@ pub const CacheHash = struct { |
| 258 | 253 | }; |
| 259 | 254 | } |
| 260 | 255 | |
| 261 | | const file_contents = try self.manifest_file.?.inStream().readAllAlloc(self.cache.gpa, MANIFEST_FILE_SIZE_MAX); |
| 256 | const file_contents = try self.manifest_file.?.inStream().readAllAlloc(self.cache.gpa, manifest_file_size_max); |
| 262 | 257 | defer self.cache.gpa.free(file_contents); |
| 263 | 258 | |
| 264 | 259 | const input_file_count = self.files.items.len; |
| ... | ... | @@ -290,7 +285,7 @@ pub const CacheHash = struct { |
| 290 | 285 | cache_hash_file.stat.size = fmt.parseInt(u64, size, 10) catch return error.InvalidFormat; |
| 291 | 286 | cache_hash_file.stat.inode = fmt.parseInt(fs.File.INode, inode, 10) catch return error.InvalidFormat; |
| 292 | 287 | cache_hash_file.stat.mtime = fmt.parseInt(i64, mtime_nsec_str, 10) catch return error.InvalidFormat; |
| 293 | | base64_decoder.decode(&cache_hash_file.bin_digest, digest_str) catch return error.InvalidFormat; |
| 288 | std.fmt.hexToBytes(&cache_hash_file.bin_digest, digest_str) catch return error.InvalidFormat; |
| 294 | 289 | |
| 295 | 290 | if (file_path.len == 0) { |
| 296 | 291 | return error.InvalidFormat; |
| ... | ... | @@ -325,7 +320,7 @@ pub const CacheHash = struct { |
| 325 | 320 | cache_hash_file.stat.inode = 0; |
| 326 | 321 | } |
| 327 | 322 | |
| 328 | | var actual_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 323 | var actual_digest: [bin_digest_len]u8 = undefined; |
| 329 | 324 | try hashFile(this_file, &actual_digest); |
| 330 | 325 | |
| 331 | 326 | if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) { |
| ... | ... | @@ -462,7 +457,7 @@ pub const CacheHash = struct { |
| 462 | 457 | pub fn addDepFilePost(self: *CacheHash, dir: fs.Dir, dep_file_basename: []const u8) !void { |
| 463 | 458 | assert(self.manifest_file != null); |
| 464 | 459 | |
| 465 | | const dep_file_contents = try dir.readFileAlloc(self.cache.gpa, dep_file_basename, MANIFEST_FILE_SIZE_MAX); |
| 460 | const dep_file_contents = try dir.readFileAlloc(self.cache.gpa, dep_file_basename, manifest_file_size_max); |
| 466 | 461 | defer self.cache.gpa.free(dep_file_contents); |
| 467 | 462 | |
| 468 | 463 | const DepTokenizer = @import("DepTokenizer.zig"); |
| ... | ... | @@ -498,8 +493,8 @@ pub const CacheHash = struct { |
| 498 | 493 | } |
| 499 | 494 | } |
| 500 | 495 | |
| 501 | | /// Returns a base64 encoded hash of the inputs. |
| 502 | | pub fn final(self: *CacheHash) [BASE64_DIGEST_LEN]u8 { |
| 496 | /// Returns a hex encoded hash of the inputs. |
| 497 | pub fn final(self: *CacheHash) [hex_digest_len]u8 { |
| 503 | 498 | assert(self.manifest_file != null); |
| 504 | 499 | |
| 505 | 500 | // We don't close the manifest file yet, because we want to |
| ... | ... | @@ -508,11 +503,11 @@ pub const CacheHash = struct { |
| 508 | 503 | // cache_release is called we still might be working on creating |
| 509 | 504 | // the artifacts to cache. |
| 510 | 505 | |
| 511 | | var bin_digest: [BIN_DIGEST_LEN]u8 = undefined; |
| 506 | var bin_digest: [bin_digest_len]u8 = undefined; |
| 512 | 507 | self.hash.hasher.final(&bin_digest); |
| 513 | 508 | |
| 514 | | var out_digest: [BASE64_DIGEST_LEN]u8 = undefined; |
| 515 | | base64_encoder.encode(&out_digest, &bin_digest); |
| 509 | var out_digest: [hex_digest_len]u8 = undefined; |
| 510 | _ = std.fmt.bufPrint(&out_digest, "{x}", .{bin_digest}) catch unreachable; |
| 516 | 511 | |
| 517 | 512 | return out_digest; |
| 518 | 513 | } |
| ... | ... | @@ -521,18 +516,18 @@ pub const CacheHash = struct { |
| 521 | 516 | assert(self.manifest_file != null); |
| 522 | 517 | if (!self.manifest_dirty) return; |
| 523 | 518 | |
| 524 | | var encoded_digest: [BASE64_DIGEST_LEN]u8 = undefined; |
| 519 | var encoded_digest: [hex_digest_len]u8 = undefined; |
| 525 | 520 | var contents = std.ArrayList(u8).init(self.cache.gpa); |
| 526 | 521 | var writer = contents.writer(); |
| 527 | 522 | defer contents.deinit(); |
| 528 | 523 | |
| 529 | 524 | for (self.files.items) |file| { |
| 530 | | base64_encoder.encode(encoded_digest[0..], &file.bin_digest); |
| 531 | | try writer.print("{} {} {} {} {}\n", .{ |
| 525 | _ = std.fmt.bufPrint(&encoded_digest, "{x}", .{file.bin_digest}) catch unreachable; |
| 526 | try writer.print("{d} {d} {d} {s} {s}\n", .{ |
| 532 | 527 | file.stat.size, |
| 533 | 528 | file.stat.inode, |
| 534 | 529 | file.stat.mtime, |
| 535 | | encoded_digest[0..], |
| 530 | &encoded_digest, |
| 536 | 531 | file.path, |
| 537 | 532 | }); |
| 538 | 533 | } |
| ... | ... | @@ -625,8 +620,8 @@ test "cache file and then recall it" { |
| 625 | 620 | std.time.sleep(1); |
| 626 | 621 | } |
| 627 | 622 | |
| 628 | | var digest1: [BASE64_DIGEST_LEN]u8 = undefined; |
| 629 | | var digest2: [BASE64_DIGEST_LEN]u8 = undefined; |
| 623 | var digest1: [hex_digest_len]u8 = undefined; |
| 624 | var digest2: [hex_digest_len]u8 = undefined; |
| 630 | 625 | |
| 631 | 626 | { |
| 632 | 627 | var cache = Cache{ |
| ... | ... | @@ -707,8 +702,8 @@ test "check that changing a file makes cache fail" { |
| 707 | 702 | std.time.sleep(1); |
| 708 | 703 | } |
| 709 | 704 | |
| 710 | | var digest1: [BASE64_DIGEST_LEN]u8 = undefined; |
| 711 | | var digest2: [BASE64_DIGEST_LEN]u8 = undefined; |
| 705 | var digest1: [hex_digest_len]u8 = undefined; |
| 706 | var digest2: [hex_digest_len]u8 = undefined; |
| 712 | 707 | |
| 713 | 708 | { |
| 714 | 709 | var cache = Cache{ |
| ... | ... | @@ -770,8 +765,8 @@ test "no file inputs" { |
| 770 | 765 | const temp_manifest_dir = "no_file_inputs_manifest_dir"; |
| 771 | 766 | defer cwd.deleteTree(temp_manifest_dir) catch {}; |
| 772 | 767 | |
| 773 | | var digest1: [BASE64_DIGEST_LEN]u8 = undefined; |
| 774 | | var digest2: [BASE64_DIGEST_LEN]u8 = undefined; |
| 768 | var digest1: [hex_digest_len]u8 = undefined; |
| 769 | var digest2: [hex_digest_len]u8 = undefined; |
| 775 | 770 | |
| 776 | 771 | var cache = Cache{ |
| 777 | 772 | .gpa = testing.allocator, |
| ... | ... | @@ -825,9 +820,9 @@ test "CacheHashes with files added after initial hash work" { |
| 825 | 820 | std.time.sleep(1); |
| 826 | 821 | } |
| 827 | 822 | |
| 828 | | var digest1: [BASE64_DIGEST_LEN]u8 = undefined; |
| 829 | | var digest2: [BASE64_DIGEST_LEN]u8 = undefined; |
| 830 | | var digest3: [BASE64_DIGEST_LEN]u8 = undefined; |
| 823 | var digest1: [hex_digest_len]u8 = undefined; |
| 824 | var digest2: [hex_digest_len]u8 = undefined; |
| 825 | var digest3: [hex_digest_len]u8 = undefined; |
| 831 | 826 | |
| 832 | 827 | { |
| 833 | 828 | var cache = Cache{ |