Skip to content

Commit

Permalink
remove zarc in favor us custom unzip implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Apr 22, 2024
1 parent 208f538 commit 29f93e5
Show file tree
Hide file tree
Showing 7 changed files with 685 additions and 19 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,4 @@ Zigup is currently built/tested using zig 0.11.0.

# Dependencies

The windows target depends on https://github.com/SuperAuguste/zarc to extract zip files. This repo might point to my fork if there are needed changes pending the acceptance of a PR: https://github.com/marler8997/zarc.

On linux and macos, zigup depends on `tar` to extract the compiler archive files (this may change in the future).
14 changes: 11 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ pub fn build(b: *std.Build) !void {
}

addTest(b, exe, target, optimize);

{
const unzip = b.addExecutable(.{
.name = "unzip",
.root_source_file = b.path("unzip.zig"),
.target = target,
.optimize = optimize,
});
const install = b.addInstallArtifact(unzip, .{});
b.step("unzip", "Build/install the unzip cmdline tool").dependOn(&install.step);
}
}

fn addTest(
Expand Down Expand Up @@ -94,9 +105,6 @@ fn addZigupExe(

if (target.result.os.tag == .windows) {
exe.root_module.addImport("win32exelink", win32exelink_mod.?);
if (b.lazyDependency("zarc", .{})) |zarc_dep| {
exe.root_module.addImport("zarc", zarc_dep.module("zarc"));
}
}

return exe;
Expand Down
9 changes: 1 addition & 8 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
.name = "zigup",
.version = "0.0.1",

.dependencies = .{
.zarc = .{
.url = "https://github.com/marler8997/zarc/archive/1556f8e6c79e81d792024d5266e282638fcc50b5.tar.gz",
.hash = "1220ad2d1e1e8c3b7c4734eaa90f9633ca2c04c5bf346ede1a4f1b18657db36aa164",
.lazy = true,
},
},

.paths = .{
"LICENSE",
"README.md",
Expand All @@ -19,5 +11,6 @@
"test.zig",
"win32exelink.zig",
"zigup.zig",
"zip.zig",
},
}
82 changes: 82 additions & 0 deletions unzip.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const builtin = @import("builtin");
const std = @import("std");

fn oom(e: error{OutOfMemory}) noreturn {
@panic(@errorName(e));
}
fn fatal(comptime fmt: []const u8, args: anytype) noreturn {
std.log.err(fmt, args);
std.process.exit(0xff);
}

fn usage() noreturn {
std.log.err("Usage: unzip [-d DIR] ZIP_FILE", .{});
std.process.exit(1);
}

var windows_args_arena = if (builtin.os.tag == .windows)
std.heap.ArenaAllocator.init(std.heap.page_allocator) else struct{}{};
pub fn cmdlineArgs() [][*:0]u8 {
if (builtin.os.tag == .windows) {
const slices = std.process.argsAlloc(windows_args_arena.allocator()) catch |err| switch (err) {
error.OutOfMemory => oom(error.OutOfMemory),
//error.InvalidCmdLine => @panic("InvalidCmdLine"),
error.Overflow => @panic("Overflow while parsing command line"),
};
const args = windows_args_arena.allocator().alloc([*:0]u8, slices.len - 1) catch |e| oom(e);
for (slices[1..], 0..) |slice, i| {
args[i] = slice.ptr;
}
return args;
}
return std.os.argv.ptr[1 .. std.os.argv.len];
}

pub fn main() !void {
var cmdline_opt: struct {
dir_arg: ?[]u8 = null,
} = .{};

const cmd_args = blk: {
const cmd_args = cmdlineArgs();
var arg_index: usize = 0;
var non_option_len: usize = 0;
while (arg_index < cmd_args.len) : (arg_index += 1) {
const arg = std.mem.span(cmd_args[arg_index]);
if (!std.mem.startsWith(u8, arg, "-")) {
cmd_args[non_option_len] = arg;
non_option_len += 1;
} else if (std.mem.eql(u8, arg, "-d")) {
arg_index += 1;
if (arg_index == cmd_args.len)
fatal("option '{s}' requires an argument", .{arg});
cmdline_opt.dir_arg = std.mem.span(cmd_args[arg_index]);
} else {
fatal("unknown cmdline option '{s}'", .{arg});
}
}
break :blk cmd_args[0 .. non_option_len];
};

if (cmd_args.len != 1) usage();
const zip_file_arg = std.mem.span(cmd_args[0]);

var out_dir = blk: {
if (cmdline_opt.dir_arg) |dir| {
break :blk std.fs.cwd().openDir(dir, .{}) catch |err| switch (err) {
error.FileNotFound => {
try std.fs.cwd().makePath(dir);
break :blk try std.fs.cwd().openDir(dir, .{});
},
else => fatal("failed to open output directory '{s}' with {s}", .{dir, @errorName(err)}),
};
}
break :blk std.fs.cwd();
};
defer if (cmdline_opt.dir_arg) |_| out_dir.close();

const zip_file = std.fs.cwd().openFile(zip_file_arg, .{}) catch |err|
fatal("open '{s}' failed: {s}", .{zip_file_arg, @errorName(err)});
defer zip_file.close();
try @import("zip.zig").pipeToFileSystem(out_dir, zip_file);
}
9 changes: 3 additions & 6 deletions zigup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const std = @import("std");
const builtin = @import("builtin");
const mem = std.mem;

const zip = @import("zip.zig");

const ArrayList = std.ArrayList;
const Allocator = mem.Allocator;

const zarc = @import("zarc");

const fixdeletetree = @import("fixdeletetree.zig");

const arch = switch (builtin.cpu.arch) {
Expand Down Expand Up @@ -1007,10 +1007,7 @@ fn installCompiler(allocator: Allocator, compiler_dir: []const u8, url: []const
var timer = try std.time.Timer.start();
var archive_file = try std.fs.openFileAbsolute(archive_absolute, .{});
defer archive_file.close();
const reader = archive_file.reader();
var archive = try zarc.zip.load(allocator, reader);
defer archive.deinit(allocator);
_ = try archive.extract(reader, installing_dir_opened, .{});
try zip.pipeToFileSystem(installing_dir_opened, archive_file);
const time = timer.read();
loginfo("extracted archive in {d:.2} s", .{@as(f32, @floatFromInt(time)) / @as(f32, @floatFromInt(std.time.ns_per_s))});
}
Expand Down
Loading

0 comments on commit 29f93e5

Please sign in to comment.