From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sun, 12 May 2024 18:20:19 +0200 Subject: [PATCH] fetch: prevent global cache from being copied Signed-off-by: Jan200101 --- src/Package/Fetch.zig | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Package/Fetch.zig b/src/Package/Fetch.zig index f45a5cc93e..f21409ae76 100644 --- a/src/Package/Fetch.zig +++ b/src/Package/Fetch.zig @@ -1343,6 +1343,7 @@ fn unpackGitPack(f: *Fetch, out_dir: fs.Dir, resource: *Resource) anyerror!Unpac fn recursiveDirectoryCopy(f: *Fetch, dir: fs.Dir, tmp_dir: fs.Dir) anyerror!void { const gpa = f.arena.child_allocator; + const cache_root = f.job_queue.global_cache; // Recursive directory copy. var it = try dir.walk(gpa); defer it.deinit(); @@ -1350,18 +1351,26 @@ fn recursiveDirectoryCopy(f: *Fetch, dir: fs.Dir, tmp_dir: fs.Dir) anyerror!void switch (entry.kind) { .directory => {}, // omit empty directories .file => { - dir.copyFile( - entry.path, - tmp_dir, - entry.path, - .{}, - ) catch |err| switch (err) { - error.FileNotFound => { - if (fs.path.dirname(entry.path)) |dirname| try tmp_dir.makePath(dirname); - try dir.copyFile(entry.path, tmp_dir, entry.path, .{}); - }, - else => |e| return e, - }; + const full_path = try dir.realpathAlloc(gpa, entry.path); + defer gpa.free(full_path); + + const cache_path = try cache_root.handle.realpathAlloc(gpa, "."); + defer gpa.free(cache_path); + + if (!std.mem.startsWith(u8, full_path, cache_path)) { + dir.copyFile( + entry.path, + tmp_dir, + entry.path, + .{}, + ) catch |err| switch (err) { + error.FileNotFound => { + if (fs.path.dirname(entry.path)) |dirname| try tmp_dir.makePath(dirname); + try dir.copyFile(entry.path, tmp_dir, entry.path, .{}); + }, + else => |e| return e, + }; + } }, .sym_link => { var buf: [fs.MAX_PATH_BYTES]u8 = undefined;