Finished work (theoretically) on single threaded extraction

Started work on Multi-threaded extraction
Added decompression cache.
Added XattrTable
This commit is contained in:
Caleb Gardner
2026-06-13 06:06:27 -05:00
parent f3318e55a3
commit 8a9ff7bf5f
11 changed files with 664 additions and 39 deletions
+32 -18
View File
@@ -1,9 +1,10 @@
const std = @import("std");
const Writer = std.Io.Writer;
const Io = std.Io;
const Writer = Io.Writer;
const builtin = @import("builtin");
const build = @import("build_options");
const config = @import("config");
const squashfs = @import("zig_squashfs");
const squashfs = @import("squashfs");
//TODO: Add more options
const help_mgs =
@@ -30,7 +31,7 @@ const help_mgs =
const errors = error{InvalidArguments};
var archive: []const u8 = "";
var extLoc: []const u8 = "squashfs-root";
var ext_loc: []const u8 = "squashfs-root";
var offset: u64 = 0;
var threads: u32 = 0;
var verbose: bool = false;
@@ -38,35 +39,48 @@ var ignore_xattrs: bool = false;
var ignore_permissions: bool = false;
var force: bool = false;
pub fn main() !void {
const alloc = std.heap.smp_allocator;
var stdout = std.fs.File.stdout();
var out = stdout.writer(&[0]u8{});
pub fn main(init: std.process.Init) !void {
const io = init.io;
const alloc = init.alloc;
var stdout = Io.File.stdout();
var out = stdout.writer(io, &[0]u8{});
defer out.interface.flush() catch {};
try handleArgs(alloc, &out.interface);
try handleArgs(init.minimal.args, &out.interface);
if (archive.len == 0) {
try out.interface.print("You must provide a squashfs archive\n", .{});
try out.interface.print(help_mgs, .{});
return;
}
var fil: std.fs.File = try std.fs.cwd().openFile(archive, .{}); //TODO: Handle error gracefully.
var fil = try Io.Dir.cwd().openFile(io, archive, .{}); //TODO: Handle error gracefully.
defer fil.close();
var arc: squashfs.Archive = try .init(alloc, fil, offset); //TODO: Update when memory size matters. //TODO: Handle error gracefully.
var arc: squashfs.Archive = try .init(io, fil, offset); //TODO: Update when memory size matters. //TODO: Handle error gracefully.
defer arc.deinit();
const options: squashfs.ExtractionOptions = .{
.threads = if (threads == 0) try std.Thread.getCpuCount() else threads,
.single_threaded = threads == 1,
.verbose = verbose,
.verbose_writer = if (verbose) &out.interface else null,
.ignore_xattr = ignore_xattrs,
.ignore_permissions = ignore_permissions,
};
if (force)
try std.fs.cwd().deleteTree(extLoc);
try arc.extract(alloc, extLoc, options); //TODO: Handle error gracefully.
try std.fs.cwd().deleteTree(ext_loc);
if (threads > 1) {
var limited_io = Io.Threaded.init(alloc, .{
.argv0 = .init(init.minimal.args),
.async_limit = threads,
.concurrent_limit = threads,
.environ = init.minimal.environ,
});
try arc.extract(alloc, limited_io.io(), ext_loc, options); //TODO: Handle error gracefully.
} else {
try arc.extract(alloc, io, ext_loc, options); //TODO: Handle error gracefully.
}
}
fn handleArgs(alloc: std.mem.Allocator, out: *Writer) !void {
var args = try std.process.argsWithAllocator(alloc);
fn handleArgs(main_args: std.process.Args, out: *Writer) !void {
var args = main_args.iterate();
defer args.deinit();
_ = args.next(); // args[0] is the application launch command.
while (args.next()) |arg| {
@@ -87,7 +101,7 @@ fn handleArgs(alloc: std.mem.Allocator, out: *Writer) !void {
try out.print("-d must be followed by a location\n", .{});
return errors.InvalidArguments;
}
extLoc = nxt.?;
ext_loc = nxt.?;
continue;
} else if (std.mem.eql(u8, arg, "-p")) {
const nxt = args.next();
@@ -114,7 +128,7 @@ fn handleArgs(alloc: std.mem.Allocator, out: *Writer) !void {
continue;
} else if (std.mem.eql(u8, arg, "--version")) {
try out.print("zig-unsquashfs v", .{});
try config.version.format(out);
try build.version.format(out);
try out.print("\nBuilt using Zig {s} in {} mode\n", .{ builtin.zig_version_string, builtin.mode });
std.process.exit(0);
return;