authorgravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-04-03 21:20:39+02:00
committergravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-04-03 21:20:39+02:00
log8545cb014795b4137d1ad92c62d08014723abf9f
tree49eec6b863d887a3f19054956a86f082e2817f37
parent363acf49910bbc0ff2b8acecea027f3f17bbce25

fetch: use package root detection pipeToFileSystem

Based on: https://github.com/ziglang/zig/pull/19111#discussion_r1548620985 In pipeToFileSystem we are already iterating over all files in tarball. Inspecting them there for existence of single root folder saves two syscalls later.

1 files changed, 4 insertions(+), 42 deletions(-)

src/Package/Fetch.zig+4-42
...@@ -625,16 +625,6 @@ fn loadManifest(f: *Fetch, pkg_root: Cache.Path) RunError!void {...@@ -625,16 +625,6 @@ fn loadManifest(f: *Fetch, pkg_root: Cache.Path) RunError!void {
625 }625 }
626}626}
627627
628fn archivePackageDir(arena: Allocator, out_dir: fs.Dir) !?[]const u8 {
629 var iter = out_dir.iterate();
630 if (try iter.next()) |entry| {
631 if (try iter.next() == null and entry.kind == .directory) {
632 return try arena.dupe(u8, entry.name);
633 }
634 }
635 return null;
636}
637
638fn queueJobsForDeps(f: *Fetch) RunError!void {628fn queueJobsForDeps(f: *Fetch) RunError!void {
639 assert(f.job_queue.recursive);629 assert(f.job_queue.recursive);
640630
...@@ -1235,7 +1225,10 @@ fn unpackTarball(f: *Fetch, out_dir: fs.Dir, reader: anytype) RunError!?[]const...@@ -1235,7 +1225,10 @@ fn unpackTarball(f: *Fetch, out_dir: fs.Dir, reader: anytype) RunError!?[]const
1235 return error.FetchFailed;1225 return error.FetchFailed;
1236 }1226 }
12371227
1238 return archivePackageDir(arena, out_dir) catch null;1228 return if (diagnostics.root_dir) |root_dir|
1229 return try arena.dupe(u8, root_dir)
1230 else
1231 null;
1239}1232}
12401233
1241fn unpackGitPack(f: *Fetch, out_dir: fs.Dir, resource: *Resource) anyerror!void {1234fn unpackGitPack(f: *Fetch, out_dir: fs.Dir, resource: *Resource) anyerror!void {
...@@ -1752,34 +1745,3 @@ test {...@@ -1752,34 +1745,3 @@ test {
1752 _ = Filter;1745 _ = Filter;
1753 _ = FileType;1746 _ = FileType;
1754}1747}
1755
1756test archivePackageDir {
1757 const testing = std.testing;
1758
1759 var tmp = std.testing.tmpDir(.{ .iterate = true });
1760 defer tmp.cleanup();
1761
1762 // folder1
1763 // ├── folder2
1764 // ├── file1
1765 //
1766 try tmp.dir.makePath("folder1/folder2");
1767 (try tmp.dir.createFile("folder1/file1", .{})).close();
1768
1769 // start at root returns folder1 as package root
1770 const sub_path = (try archivePackageDir(testing.allocator, tmp.dir)).?;
1771 try testing.expectEqualStrings("folder1", sub_path);
1772 testing.allocator.free(sub_path);
1773
1774 // start at folder1 returns null
1775 try testing.expect(null == (try archivePackageDir(
1776 testing.allocator,
1777 try tmp.dir.openDir("folder1", .{ .iterate = true }),
1778 )));
1779
1780 // start at folder1/folder2 returns null
1781 try testing.expect(null == (try archivePackageDir(
1782 testing.allocator,
1783 try tmp.dir.openDir("folder1/folder2", .{ .iterate = true }),
1784 )));
1785}