Skip to content

Commit

Permalink
package fetching: support .tar.zst archives
Browse files Browse the repository at this point in the history
  • Loading branch information
dweiller committed Oct 16, 2023
1 parent ca690ff commit be9ff46
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Package/Fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -752,12 +752,14 @@ const FileType = enum {
tar,
@"tar.gz",
@"tar.xz",
@"tar.zst",
git_pack,

fn fromPath(file_path: []const u8) ?FileType {
if (ascii.endsWithIgnoreCase(file_path, ".tar")) return .tar;
if (ascii.endsWithIgnoreCase(file_path, ".tar.gz")) return .@"tar.gz";
if (ascii.endsWithIgnoreCase(file_path, ".tar.xz")) return .@"tar.xz";
if (ascii.endsWithIgnoreCase(file_path, ".tar.zst")) return .@"tar.zst";
return null;
}

Expand Down Expand Up @@ -1015,6 +1017,7 @@ fn unpackResource(
.tar => try unpackTarball(f, tmp_directory.handle, resource.reader()),
.@"tar.gz" => try unpackTarballCompressed(f, tmp_directory.handle, resource, std.compress.gzip),
.@"tar.xz" => try unpackTarballCompressed(f, tmp_directory.handle, resource, std.compress.xz),
.@"tar.zst" => try unpackTarballCompressed(f, tmp_directory.handle, resource, ZstdWrapper),
.git_pack => unpackGitPack(f, tmp_directory.handle, resource) catch |err| switch (err) {
error.FetchFailed => return error.FetchFailed,
error.OutOfMemory => return error.OutOfMemory,
Expand All @@ -1026,6 +1029,18 @@ fn unpackResource(
}
}

// due to slight differences in the API of std.compress.(gzip|xz) and std.compress.zstd, zstd is
// wrapped for generic use in unpackTarballCompressed: see github.com/ziglang/zig/issues/14739
const ZstdWrapper = struct {
fn DecompressType(comptime T: type) type {
return error{}!std.compress.zstd.DecompressStream(T, .{});
}

fn decompress(allocator: Allocator, reader: anytype) DecompressType(@TypeOf(reader)) {
return std.compress.zstd.decompressStream(allocator, reader);
}
};

fn unpackTarballCompressed(
f: *Fetch,
out_dir: fs.Dir,
Expand Down

0 comments on commit be9ff46

Please sign in to comment.