Skip to content

Commit

Permalink
Split logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Jun 20, 2024
1 parent 8a7eca9 commit 1dbb515
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/download.zig
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ fn downloadAndExtract(
download_node.end();

var extract_node = root_node.start("Extracting", 1);
const c_allocator = std.heap.c_allocator;

// ~/.zm/versions/zig-macos-x86_64-0.10.0.tar.xz
const data_allocator = tools.getAllocator();
Expand All @@ -175,12 +174,16 @@ fn downloadAndExtract(
const downloaded_file = try zvm_dir.openFile(downloaded_file_path, .{});
defer downloaded_file.close();

_ = try lib.extract_tarxz_to_dir(c_allocator, zvm_dir_version, downloaded_file);
if (builtin.os.tag == .windows) {
try lib.extract_zip_dir(zvm_dir_version, downloaded_file);
} else {
try lib.extract_tarxz_to_dir(allocator, zvm_dir_version, downloaded_file);
}

extract_node.end();

var result: [32]u8 = undefined;
sha256.final(&result);
//std.debug.print("Hash computation complete. Hash: {s}\n", .{std.fmt.fmtSliceHexLower(&result)});
return result;
}

Expand Down
12 changes: 4 additions & 8 deletions src/extract.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ const builtin = @import("builtin");
const tools = @import("tools.zig");

pub fn extract_tarxz_to_dir(allocator: std.mem.Allocator, outDir: std.fs.Dir, file: std.fs.File) !void {
if (builtin.os.tag == .windows) {
try extract_zip_dir(outDir, file);
} else {
var buffered_reader = std.io.bufferedReader(file.reader());
var decompressed = try std.compress.xz.decompress(allocator, buffered_reader.reader());
defer decompressed.deinit();
try std.tar.pipeToFileSystem(outDir, decompressed.reader(), .{ .mode_mode = .executable_bit_only, .strip_components = 1 });
}
var buffered_reader = std.io.bufferedReader(file.reader());
var decompressed = try std.compress.xz.decompress(allocator, buffered_reader.reader());
defer decompressed.deinit();
try std.tar.pipeToFileSystem(outDir, decompressed.reader(), .{ .mode_mode = .executable_bit_only, .strip_components = 1 });
}

pub fn extract_zip_dir(outDir: std.fs.Dir, file: std.fs.File) !void {
Expand Down

0 comments on commit 1dbb515

Please sign in to comment.