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" {...@@ -3810,12 +3810,11 @@ test "replace" {
3810 try testing.expectEqualStrings(expected, output[0..expected.len]);3810 try testing.expectEqualStrings(expected, output[0..expected.len]);
3811}3811}
38123812
3813/// Replace all occurrences of `needle` with `replacement`.3813/// Replace all occurrences of `match` with `replacement`.
3814pub fn replaceScalar(comptime T: type, slice: []T, needle: T, replacement: T) void {3814pub fn replaceScalar(comptime T: type, slice: []T, match: T, replacement: T) void {
3815 for (slice, 0..) |e, i| {3815 for (slice) |*e| {
3816 if (e == needle) {3816 if (e.* == match)
3817 slice[i] = replacement;3817 e.* = replacement;
3818 }
3819 }3818 }
3820}3819}
38213820
src/Package/Fetch.zig+17-13
...@@ -1329,7 +1329,7 @@ fn computeHash(...@@ -1329,7 +1329,7 @@ fn computeHash(
1329 const hashed_file = try arena.create(HashedFile);1329 const hashed_file = try arena.create(HashedFile);
1330 hashed_file.* = .{1330 hashed_file.* = .{
1331 .fs_path = fs_path,1331 .fs_path = fs_path,
1332 .normalized_path = try normalizePath(arena, fs_path),1332 .normalized_path = try normalizePathAlloc(arena, fs_path),
1333 .kind = kind,1333 .kind = kind,
1334 .hash = undefined, // to be populated by the worker1334 .hash = undefined, // to be populated by the worker
1335 .failure = undefined, // to be populated by the worker1335 .failure = undefined, // to be populated by the worker
...@@ -1429,6 +1429,12 @@ fn hashFileFallible(dir: fs.Dir, hashed_file: *HashedFile) HashedFile.Error!void...@@ -1429,6 +1429,12 @@ fn hashFileFallible(dir: fs.Dir, hashed_file: *HashedFile) HashedFile.Error!void
1429 },1429 },
1430 .sym_link => {1430 .sym_link => {
1431 const link_name = try dir.readLink(hashed_file.fs_path, &buf);1431 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 }
1432 hasher.update(link_name);1438 hasher.update(link_name);
1433 },1439 },
1434 }1440 }
...@@ -1484,22 +1490,20 @@ const HashedFile = struct {...@@ -1484,22 +1490,20 @@ const HashedFile = struct {
14841490
1485/// Make a file system path identical independently of operating system path inconsistencies.1491/// Make a file system path identical independently of operating system path inconsistencies.
1486/// This converts backslashes into forward slashes.1492/// This converts backslashes into forward slashes.
1487fn normalizePath(arena: Allocator, fs_path: []const u8) ![]const u8 {1493fn normalizePathAlloc(arena: Allocator, fs_path: []const u8) ![]const u8 {
1488 const canonical_sep = '/';1494 if (fs.path.sep == canonical_sep) return fs_path;
1489
1490 if (fs.path.sep == canonical_sep)
1491 return fs_path;
1492
1493 const normalized = try arena.dupe(u8, fs_path);1495 const normalized = try arena.dupe(u8, fs_path);
1494 for (normalized) |*byte| {1496 normalizePath(normalized);
1495 switch (byte.*) {
1496 fs.path.sep => byte.* = canonical_sep,
1497 else => continue,
1498 }
1499 }
1500 return normalized;1497 return normalized;
1501}1498}
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
1503const Filter = struct {1507const Filter = struct {
1504 include_paths: std.StringArrayHashMapUnmanaged(void) = .{},1508 include_paths: std.StringArrayHashMapUnmanaged(void) = .{},
15051509