From b64a3ec44a6c85f452a846f8dae5a2e3fe6a1013 Mon Sep 17 00:00:00 2001 From: "Caleb J. Gardner" Date: Sat, 7 Feb 2026 06:57:55 -0600 Subject: [PATCH] Archive.extract now directly uses Inode instead of File. --- build.zig | 4 +++- src/archive.zig | 23 ++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index 0d51302..8212fbd 100644 --- a/build.zig +++ b/build.zig @@ -1,14 +1,16 @@ const std = @import("std"); pub fn build(b: *std.Build) void { + const static = b.option(bool, "static_build", "Build static"); const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseFast }); const linkage: std.builtin.LinkMode = .static; // TODO: Add argument to set link mode. + const use_c_libs: bool = false; + _ = use_c_libs; const mod = b.addModule("zig_squashfs", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, - // .imports = &.{}, }); const exe = b.addExecutable(.{ .name = "unsquashfs", diff --git a/src/archive.zig b/src/archive.zig index 5dab2ad..ec41ca2 100644 --- a/src/archive.zig +++ b/src/archive.zig @@ -141,7 +141,24 @@ pub fn open(self: *Archive, path: []const u8) !SfsFile { pub fn extract(self: *Archive, path: []const u8, options: ExtractionOptions) !void { if (!self.setup) try self.setupValues(); - var root_fil = try self.root(); - defer root_fil.deinit(); - return root_fil.extract(path, options); + var alloc = self.allocator(); + var ext_path: []u8 = undefined; + if (std.fs.cwd().statFile(path)) |stat| { + if (stat.kind == .directory) { + ext_path = @constCast(path); + } else return error.ExtractionPathExists; + } else |err| { + if (err == error.FileNotFound) { + ext_path = @constCast(path); + } else { + std.log.err("Error stat-ing extraction path {s}: {}\n", .{ path, err }); + return err; + } + } + defer if (ext_path.len > path.len) alloc.free(ext_path); + var rdr = try self.fil.readerAt(self.super.root_ref.block_start + self.super.inode_start, &[0]u8{}); + var meta: MetadataReader = .init(self.allocator(), &rdr.interface, self.decomp); + try meta.interface.discardAll(self.super.root_ref.block_offset); + const in: Inode = try .read(self.allocator(), &meta.interface, self.super.block_size); + try in.extractTo(self, ext_path, options); }