authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-16 16:47:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-16 16:47:47-07:00
logb171a6f25d5257f615b6369c11f8307dcf076c0d
treeaacbc8f10addf524d4a3ff9a483ba1991e27eb85
parent1456f95b3c043aa7e5b655c362cc3bd05aa3c795

Package.Fetch: normalize path separators in symlinks

closes #17549

2 files changed, 22 insertions(+), 19 deletions(-)

lib/std/mem.zig+5-6
......@@ -3810,12 +3810,11 @@ test "replace" {
38103810 try testing.expectEqualStrings(expected, output[0..expected.len]);
38113811}
38123812
3813/// Replace all occurrences of `needle` with `replacement`.
3814pub fn replaceScalar(comptime T: type, slice: []T, needle: T, replacement: T) void {
3815 for (slice, 0..) |e, i| {
3816 if (e == needle) {
3817 slice[i] = replacement;
3818 }
3813/// Replace all occurrences of `match` with `replacement`.
3814pub fn replaceScalar(comptime T: type, slice: []T, match: T, replacement: T) void {
3815 for (slice) |*e| {
3816 if (e.* == match)
3817 e.* = replacement;
38193818 }
38203819}
38213820
src/Package/Fetch.zig+17-13
......@@ -1329,7 +1329,7 @@ fn computeHash(
13291329 const hashed_file = try arena.create(HashedFile);
13301330 hashed_file.* = .{
13311331 .fs_path = fs_path,
1332 .normalized_path = try normalizePath(arena, fs_path),
1332 .normalized_path = try normalizePathAlloc(arena, fs_path),
13331333 .kind = kind,
13341334 .hash = undefined, // to be populated by the worker
13351335 .failure = undefined, // to be populated by the worker
......@@ -1429,6 +1429,12 @@ fn hashFileFallible(dir: fs.Dir, hashed_file: *HashedFile) HashedFile.Error!void
14291429 },
14301430 .sym_link => {
14311431 const link_name = try dir.readLink(hashed_file.fs_path, &buf);
1432 if (fs.path.sep != canonical_sep) {
1433 // Package hashes are intended to be consistent across
1434 // platforms which means we must normalize path separators
1435 // inside symlinks.
1436 normalizePath(link_name);
1437 }
14321438 hasher.update(link_name);
14331439 },
14341440 }
......@@ -1484,22 +1490,20 @@ const HashedFile = struct {
14841490
14851491/// Make a file system path identical independently of operating system path inconsistencies.
14861492/// This converts backslashes into forward slashes.
1487fn normalizePath(arena: Allocator, fs_path: []const u8) ![]const u8 {
1488 const canonical_sep = '/';
1489
1490 if (fs.path.sep == canonical_sep)
1491 return fs_path;
1492
1493fn normalizePathAlloc(arena: Allocator, fs_path: []const u8) ![]const u8 {
1494 if (fs.path.sep == canonical_sep) return fs_path;
14931495 const normalized = try arena.dupe(u8, fs_path);
1494 for (normalized) |*byte| {
1495 switch (byte.*) {
1496 fs.path.sep => byte.* = canonical_sep,
1497 else => continue,
1498 }
1499 }
1496 normalizePath(normalized);
15001497 return normalized;
15011498}
15021499
1500const canonical_sep = fs.path.sep_posix;
1501
1502fn normalizePath(bytes: []u8) void {
1503 assert(fs.path.sep != canonical_sep);
1504 std.mem.replaceScalar(u8, bytes, fs.path.sep, canonical_sep);
1505}
1506
15031507const Filter = struct {
15041508 include_paths: std.StringArrayHashMapUnmanaged(void) = .{},
15051509