| ... | @@ -10,7 +10,6 @@ const assert = std.debug.assert; | ... | @@ -10,7 +10,6 @@ const assert = std.debug.assert; |
| 10 | const log = std.log.scoped(.package); | 10 | const log = std.log.scoped(.package); |
| 11 | const main = @import("main.zig"); | 11 | const main = @import("main.zig"); |
| 12 | const ThreadPool = std.Thread.Pool; | 12 | const ThreadPool = std.Thread.Pool; |
| 13 | const WaitGroup = std.Thread.WaitGroup; | | |
| 14 | | 13 | |
| 15 | const Compilation = @import("Compilation.zig"); | 14 | const Compilation = @import("Compilation.zig"); |
| 16 | const Module = @import("Module.zig"); | 15 | const Module = @import("Module.zig"); |
| ... | @@ -18,6 +17,7 @@ const Cache = std.Build.Cache; | ... | @@ -18,6 +17,7 @@ const Cache = std.Build.Cache; |
| 18 | const build_options = @import("build_options"); | 17 | const build_options = @import("build_options"); |
| 19 | const Manifest = @import("Manifest.zig"); | 18 | const Manifest = @import("Manifest.zig"); |
| 20 | const git = @import("git.zig"); | 19 | const git = @import("git.zig"); |
| | 20 | const computePackageHash = @import("Package/hash.zig").compute; |
| 21 | | 21 | |
| 22 | pub const Table = std.StringHashMapUnmanaged(*Package); | 22 | pub const Table = std.StringHashMapUnmanaged(*Package); |
| 23 | | 23 | |
| ... | @@ -1147,81 +1147,6 @@ fn unpackGitPack( | ... | @@ -1147,81 +1147,6 @@ fn unpackGitPack( |
| 1147 | try out_dir.deleteTree(".git"); | 1147 | try out_dir.deleteTree(".git"); |
| 1148 | } | 1148 | } |
| 1149 | | 1149 | |
| 1150 | const HashedFile = struct { | | |
| 1151 | fs_path: []const u8, | | |
| 1152 | normalized_path: []const u8, | | |
| 1153 | hash: [Manifest.Hash.digest_length]u8, | | |
| 1154 | failure: Error!void, | | |
| 1155 | | | |
| 1156 | const Error = fs.File.OpenError || fs.File.ReadError || fs.File.StatError; | | |
| 1157 | | | |
| 1158 | fn lessThan(context: void, lhs: *const HashedFile, rhs: *const HashedFile) bool { | | |
| 1159 | _ = context; | | |
| 1160 | return mem.lessThan(u8, lhs.normalized_path, rhs.normalized_path); | | |
| 1161 | } | | |
| 1162 | }; | | |
| 1163 | | | |
| 1164 | fn computePackageHash( | | |
| 1165 | thread_pool: *ThreadPool, | | |
| 1166 | pkg_dir: fs.IterableDir, | | |
| 1167 | ) ![Manifest.Hash.digest_length]u8 { | | |
| 1168 | const gpa = thread_pool.allocator; | | |
| 1169 | | | |
| 1170 | // We'll use an arena allocator for the path name strings since they all | | |
| 1171 | // need to be in memory for sorting. | | |
| 1172 | var arena_instance = std.heap.ArenaAllocator.init(gpa); | | |
| 1173 | defer arena_instance.deinit(); | | |
| 1174 | const arena = arena_instance.allocator(); | | |
| 1175 | | | |
| 1176 | // Collect all files, recursively, then sort. | | |
| 1177 | var all_files = std.ArrayList(*HashedFile).init(gpa); | | |
| 1178 | defer all_files.deinit(); | | |
| 1179 | | | |
| 1180 | var walker = try pkg_dir.walk(gpa); | | |
| 1181 | defer walker.deinit(); | | |
| 1182 | | | |
| 1183 | { | | |
| 1184 | // The final hash will be a hash of each file hashed independently. This | | |
| 1185 | // allows hashing in parallel. | | |
| 1186 | var wait_group: WaitGroup = .{}; | | |
| 1187 | defer wait_group.wait(); | | |
| 1188 | | | |
| 1189 | while (try walker.next()) |entry| { | | |
| 1190 | switch (entry.kind) { | | |
| 1191 | .directory => continue, | | |
| 1192 | .file => {}, | | |
| 1193 | else => return error.IllegalFileTypeInPackage, | | |
| 1194 | } | | |
| 1195 | const hashed_file = try arena.create(HashedFile); | | |
| 1196 | const fs_path = try arena.dupe(u8, entry.path); | | |
| 1197 | hashed_file.* = .{ | | |
| 1198 | .fs_path = fs_path, | | |
| 1199 | .normalized_path = try normalizePath(arena, fs_path), | | |
| 1200 | .hash = undefined, // to be populated by the worker | | |
| 1201 | .failure = undefined, // to be populated by the worker | | |
| 1202 | }; | | |
| 1203 | wait_group.start(); | | |
| 1204 | try thread_pool.spawn(workerHashFile, .{ pkg_dir.dir, hashed_file, &wait_group }); | | |
| 1205 | | | |
| 1206 | try all_files.append(hashed_file); | | |
| 1207 | } | | |
| 1208 | } | | |
| 1209 | | | |
| 1210 | mem.sort(*HashedFile, all_files.items, {}, HashedFile.lessThan); | | |
| 1211 | | | |
| 1212 | var hasher = Manifest.Hash.init(.{}); | | |
| 1213 | var any_failures = false; | | |
| 1214 | for (all_files.items) |hashed_file| { | | |
| 1215 | hashed_file.failure catch |err| { | | |
| 1216 | any_failures = true; | | |
| 1217 | std.log.err("unable to hash '{s}': {s}", .{ hashed_file.fs_path, @errorName(err) }); | | |
| 1218 | }; | | |
| 1219 | hasher.update(&hashed_file.hash); | | |
| 1220 | } | | |
| 1221 | if (any_failures) return error.PackageHashUnavailable; | | |
| 1222 | return hasher.finalResult(); | | |
| 1223 | } | | |
| 1224 | | | |
| 1225 | /// Compute the hash of a file path. | 1150 | /// Compute the hash of a file path. |
| 1226 | fn computePathHash(gpa: Allocator, dir: Compilation.Directory, path: []const u8) ![Manifest.Hash.digest_length]u8 { | 1151 | fn computePathHash(gpa: Allocator, dir: Compilation.Directory, path: []const u8) ![Manifest.Hash.digest_length]u8 { |
| 1227 | const resolved_path = try std.fs.path.resolve(gpa, &.{ dir.path.?, path }); | 1152 | const resolved_path = try std.fs.path.resolve(gpa, &.{ dir.path.?, path }); |
| ... | @@ -1240,57 +1165,6 @@ fn isDirectory(root_dir: Compilation.Directory, path: []const u8) !bool { | ... | @@ -1240,57 +1165,6 @@ fn isDirectory(root_dir: Compilation.Directory, path: []const u8) !bool { |
| 1240 | return true; | 1165 | return true; |
| 1241 | } | 1166 | } |
| 1242 | | 1167 | |
| 1243 | /// Make a file system path identical independently of operating system path inconsistencies. | | |
| 1244 | /// This converts backslashes into forward slashes. | | |
| 1245 | fn normalizePath(arena: Allocator, fs_path: []const u8) ![]const u8 { | | |
| 1246 | const canonical_sep = '/'; | | |
| 1247 | | | |
| 1248 | if (fs.path.sep == canonical_sep) | | |
| 1249 | return fs_path; | | |
| 1250 | | | |
| 1251 | const normalized = try arena.dupe(u8, fs_path); | | |
| 1252 | for (normalized) |*byte| { | | |
| 1253 | switch (byte.*) { | | |
| 1254 | fs.path.sep => byte.* = canonical_sep, | | |
| 1255 | else => continue, | | |
| 1256 | } | | |
| 1257 | } | | |
| 1258 | return normalized; | | |
| 1259 | } | | |
| 1260 | | | |
| 1261 | fn workerHashFile(dir: fs.Dir, hashed_file: *HashedFile, wg: *WaitGroup) void { | | |
| 1262 | defer wg.finish(); | | |
| 1263 | hashed_file.failure = hashFileFallible(dir, hashed_file); | | |
| 1264 | } | | |
| 1265 | | | |
| 1266 | fn hashFileFallible(dir: fs.Dir, hashed_file: *HashedFile) HashedFile.Error!void { | | |
| 1267 | var buf: [8000]u8 = undefined; | | |
| 1268 | var file = try dir.openFile(hashed_file.fs_path, .{}); | | |
| 1269 | defer file.close(); | | |
| 1270 | var hasher = Manifest.Hash.init(.{}); | | |
| 1271 | hasher.update(hashed_file.normalized_path); | | |
| 1272 | hasher.update(&.{ 0, @intFromBool(try isExecutable(file)) }); | | |
| 1273 | while (true) { | | |
| 1274 | const bytes_read = try file.read(&buf); | | |
| 1275 | if (bytes_read == 0) break; | | |
| 1276 | hasher.update(buf[0..bytes_read]); | | |
| 1277 | } | | |
| 1278 | hasher.final(&hashed_file.hash); | | |
| 1279 | } | | |
| 1280 | | | |
| 1281 | fn isExecutable(file: fs.File) !bool { | | |
| 1282 | if (builtin.os.tag == .windows) { | | |
| 1283 | // TODO check the ACL on Windows. | | |
| 1284 | // Until this is implemented, this could be a false negative on | | |
| 1285 | // Windows, which is why we do not yet set executable_bit_only above | | |
| 1286 | // when unpacking the tarball. | | |
| 1287 | return false; | | |
| 1288 | } else { | | |
| 1289 | const stat = try file.stat(); | | |
| 1290 | return (stat.mode & std.os.S.IXUSR) != 0; | | |
| 1291 | } | | |
| 1292 | } | | |
| 1293 | | | |
| 1294 | fn renameTmpIntoCache( | 1168 | fn renameTmpIntoCache( |
| 1295 | cache_dir: fs.Dir, | 1169 | cache_dir: fs.Dir, |
| 1296 | tmp_dir_sub_path: []const u8, | 1170 | tmp_dir_sub_path: []const u8, |