Skip to content

Commit

Permalink
Package.Fetch: normalize path separators in symlinks
Browse files Browse the repository at this point in the history
closes #17549
  • Loading branch information
andrewrk committed Oct 16, 2023
1 parent 1456f95 commit b171a6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
11 changes: 5 additions & 6 deletions lib/std/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3810,12 +3810,11 @@ test "replace" {
try testing.expectEqualStrings(expected, output[0..expected.len]);
}

/// Replace all occurrences of `needle` with `replacement`.
pub fn replaceScalar(comptime T: type, slice: []T, needle: T, replacement: T) void {
for (slice, 0..) |e, i| {
if (e == needle) {
slice[i] = replacement;
}
/// Replace all occurrences of `match` with `replacement`.
pub fn replaceScalar(comptime T: type, slice: []T, match: T, replacement: T) void {
for (slice) |*e| {
if (e.* == match)
e.* = replacement;
}
}

Expand Down
30 changes: 17 additions & 13 deletions src/Package/Fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ fn computeHash(
const hashed_file = try arena.create(HashedFile);
hashed_file.* = .{
.fs_path = fs_path,
.normalized_path = try normalizePath(arena, fs_path),
.normalized_path = try normalizePathAlloc(arena, fs_path),
.kind = kind,
.hash = undefined, // to be populated by the worker
.failure = undefined, // to be populated by the worker
Expand Down Expand Up @@ -1429,6 +1429,12 @@ fn hashFileFallible(dir: fs.Dir, hashed_file: *HashedFile) HashedFile.Error!void
},
.sym_link => {
const link_name = try dir.readLink(hashed_file.fs_path, &buf);
if (fs.path.sep != canonical_sep) {
// Package hashes are intended to be consistent across
// platforms which means we must normalize path separators
// inside symlinks.
normalizePath(link_name);
}
hasher.update(link_name);
},
}
Expand Down Expand Up @@ -1484,22 +1490,20 @@ const HashedFile = struct {

/// Make a file system path identical independently of operating system path inconsistencies.
/// This converts backslashes into forward slashes.
fn normalizePath(arena: Allocator, fs_path: []const u8) ![]const u8 {
const canonical_sep = '/';

if (fs.path.sep == canonical_sep)
return fs_path;

fn normalizePathAlloc(arena: Allocator, fs_path: []const u8) ![]const u8 {
if (fs.path.sep == canonical_sep) return fs_path;
const normalized = try arena.dupe(u8, fs_path);
for (normalized) |*byte| {
switch (byte.*) {
fs.path.sep => byte.* = canonical_sep,
else => continue,
}
}
normalizePath(normalized);
return normalized;
}

const canonical_sep = fs.path.sep_posix;

fn normalizePath(bytes: []u8) void {
assert(fs.path.sep != canonical_sep);
std.mem.replaceScalar(u8, bytes, fs.path.sep, canonical_sep);
}

const Filter = struct {
include_paths: std.StringArrayHashMapUnmanaged(void) = .{},

Expand Down

0 comments on commit b171a6f

Please sign in to comment.