Finishing up a few things.

Architecting out a few things (including extraction)
This commit is contained in:
Caleb Gardner
2026-06-10 05:46:12 -05:00
parent 5c04933520
commit 8087a59c5c
8 changed files with 236 additions and 5 deletions
+37 -1
View File
@@ -4,12 +4,16 @@ const MemoryMap = Io.File.MemoryMap;
const Decomp = @import("decomp.zig");
const Inode = @import("inode.zig");
const File = @import("file.zig");
const ExtractionOptions = @import("options.zig");
const Extract = @import("extract.zig");
const Archive = @This();
super: Superblock,
map: MemoryMap,
decomp: Decomp.Fn,
pub fn init(io: Io, file: Io.File, offset: u64) !Archive {
var rdr = file.reader(io, &[0]u8{});
@@ -28,12 +32,44 @@ pub fn init(io: Io, file: Io.File, offset: u64) !Archive {
.len = super.size,
.protection = .{ .read = true },
}),
.decomp = try Decomp.getFn(super.compression),
};
}
pub fn deinit(self: *Archive, io: Io) void {
self.map.destroy(io);
}
pub fn root(self: Archive, alloc: std.mem.Allocator) !File {
return .initRef(alloc, self.super, self.map.memory, self.decomp, "", self.super.root_ref);
}
pub fn open(self: Archive, alloc: std.mem.Allocator, filepath: []const u8) !File {
const root_file = try self.root(alloc);
const path = std.mem.trim(u8, filepath, "/");
if (path.len == 0 or (path.len == 1 and path[0] == '.'))
return root_file;
defer root_file.deinit();
return root_file.open(alloc, filepath);
}
pub fn extract(self: Archive, alloc: std.mem.Allocator, io: Io, location: []const u8, options: ExtractionOptions) !void {
const root_inode: Inode = try .initRef(
alloc,
self.map.memory,
self.decomp,
self.super.inode_start,
self.super.block_size,
self.super.root_ref,
);
defer root_inode.deinit(alloc);
root_inode.extract(alloc, io, self.super, self.map.memory, self.decomp, location, options);
}
// Superblock
const SQUASHFS_MAGIC: u32 = std.mem.readInt(u32, "hsqs", .little);
@@ -54,7 +90,7 @@ pub const Superblock = extern struct {
frag_count: u32,
compression: Decomp.Enum,
block_log: u16,
flags: packed struct {
flags: packed struct(u16) {
inode_uncompressed: bool,
data_uncompressed: bool,
check: bool,
View File
View File
+18
View File
@@ -0,0 +1,18 @@
const std = @import("std");
const Io = std.Io;
const Superblock = @import("archive.zig").Superblock;
const Inode = @import("inode.zig");
const Decomp = @import("decomp.zig");
const ExtractionOptions = @import("options.zig");
pub fn extract(
alloc: std.mem.Allocator,
io: Io,
super: Superblock,
data: []u8,
decomp: Decomp.Fn,
inode: Inode,
ext_loc: []const u8,
options: ExtractionOptions,
) !void {}
+18
View File
@@ -0,0 +1,18 @@
const std = @import("std");
const Io = std.Io;
const Superblock = @import("archive.zig").Superblock;
const Inode = @import("inode.zig");
const Decomp = @import("decomp.zig");
const ExtractionOptions = @import("options.zig");
pub fn extract(
alloc: std.mem.Allocator,
io: Io,
super: Superblock,
data: []u8,
decomp: Decomp.Fn,
inode: Inode,
ext_loc: []const u8,
options: ExtractionOptions,
) !void {}
+24
View File
@@ -0,0 +1,24 @@
const std = @import("std");
const Io = std.Io;
const Superblock = @import("archive.zig").Superblock;
const Decomp = @import("decomp.zig");
const Inode = @import("inode.zig");
const ExtractionOptions = @import("options.zig");
const Single = @import("extract-single.zig");
const Multi = @import("extract-multi.zig");
pub fn extract(
alloc: std.mem.Allocator,
io: Io,
super: Superblock,
data: []u8,
decomp: Decomp.Fn,
inode: Inode,
ext_loc: []const u8,
options: ExtractionOptions,
) !void {
if (options.single_threaded)
return Single.extract(alloc, io, super, data, decomp, inode, ext_loc, options);
return Multi.extract(alloc, io, super, data, decomp, inode, ext_loc, options);
}
+119 -1
View File
@@ -1,26 +1,144 @@
//! A squashfs file/directory. Basically a wrapper around an Inode.
const std = @import("std");
const Io = std.Io;
const Directory = @import("directory.zig");
const Inode = @import("inode.zig");
const MetadataReader = @import("meta_rdr.zig");
const Decomp = @import("decomp.zig");
const Superblock = @import("archive.zig").Superblock;
const ExtractionOptions = @import("options.zig");
const Extract = @import("extract.zig");
const File = @This();
alloc: std.mem.Allocator,
super: Superblock,
data: []u8,
decomp: Decomp.Fn,
name: []const u8,
inode: Inode,
/// The given allocator should have been used to create the name and inode.
pub fn init(alloc: std.mem.Allocator, name: []const u8, inode: Inode) File {
pub fn init(alloc: std.mem.Allocator, super: Superblock, data: []u8, decomp: Decomp.Fn, name: []const u8, inode: Inode) File {
return .{
.alloc = alloc,
.super = super,
.data = data,
.decomp = decomp,
.name = name,
.inode = inode,
};
}
/// The name from the directory entry will be duplicated so it's safe to deinit the entry.
pub fn initEntry(alloc: std.mem.Allocator, super: Superblock, data: []u8, decomp: Decomp.Fn, entry: Directory.Entry) !File {
const inode: Inode = try .initEntry(alloc, data, decomp, super.inode_start, super.block_size, entry);
errdefer inode.deinit(alloc);
const new_name = try alloc.dupe(entry.name);
return init(alloc, super, data, decomp, new_name, inode);
}
/// The given name is will be duplicated so it's safe to free it afterwards.
pub fn initRef(alloc: std.mem.Allocator, super: Superblock, data: []u8, decomp: Decomp.Fn, name: []const u8, ref: Inode.Ref) !File {
const inode: Inode = try .initRef(alloc, data, decomp, super.inode_start, super.block_size, ref);
errdefer inode.deinit(alloc);
const new_name = try alloc.dupe(name);
return init(alloc, super, data, decomp, new_name, inode);
}
pub fn copy(self: File, alloc: std.mem.Allocator) !File {
const inode = self.inode;
switch (inode.data) {
.file => |*f| f.blocks = try alloc.dupe(self.inode.data.file.blocks),
.ext_file => |*f| f.blocks = try alloc.dupe(self.inode.data.file.blocks),
.symlink => |*s| s.target = try alloc.dupe(self.inode.data.symlink.target),
.ext_symlink => |*s| s.target = try alloc.dupe(self.inode.data.symlink.target),
else => {},
}
errdefer inode.deinit(alloc);
const new_name = try alloc.dupe(self.name);
return init(alloc, self.super, self.data, self.decomp, new_name, inode);
}
pub fn deinit(self: File) void {
self.alloc.free(self.name);
self.inode.deinit(self.alloc);
}
/// If the given File is a directory, open a sub-file at the given filepath.
/// If the given filepath resolves to the calling File ("" or "."), a copy of the File is returned.
pub fn open(self: File, alloc: std.mem.Allocator, filepath: []const u8) !File {
var size: u64 = 0;
var meta: MetadataReader = switch (self.inode.data) {
.dir => |d| blk: {
size = d.size;
var meta: MetadataReader = .init(alloc, self.data, self.decomp, self.super.dir_start + d.block_start);
try meta.interface.discardAll(d.block_offset);
break :blk meta;
},
.ext_dir => |d| blk: {
size = d.size;
var meta: MetadataReader = .init(alloc, self.data, self.decomp, self.super.dir_start + d.block_start);
try meta.interface.discardAll(d.block_offset);
break :blk meta;
},
else => error.NotDirectory,
};
const path = std.mem.trim(u8, filepath, "/");
if (path.len == 0 or (path.len == 1 and path[0] == '.'))
return self.copy(alloc);
const first_element: []u8 = std.mem.sliceTo(path, '/');
const file: File = blk: {
var directory: Directory = try .init(alloc, &meta.interface, size);
defer directory.deinit(alloc);
var entries = directory.entries;
var idx = entries.len / 2;
while (entries.len > 0) {
switch (std.mem.order(u8, first_element, entries[idx].name)) {
.eq => break,
.lt => entries = entries[0..idx],
.gt => entries = entries[idx..],
}
idx = entries.len / 2;
} else {
return error.NotFound;
}
break :blk try initEntry(alloc, self.super, self.data, self.decomp, entries[idx]);
};
if (first_element.len == path.len)
return file;
defer file.deinit();
return file.open(alloc, path[first_element.len..]);
}
pub fn extract(self: File, alloc: std.mem.Allocator, io: Io, location: []const u8, options: ExtractionOptions) !void {
return Extract.extract(
alloc,
io,
self.super,
self.data,
self.decomp,
self.inode,
location,
options,
);
}
+20 -3
View File
@@ -5,13 +5,16 @@ const Reader = Io.Reader;
const Decomp = @import("decomp.zig");
const Directory = @import("directory.zig");
const MetadataReader = @import("meta_rdr.zig");
const Superblock = @import("archive.zig").Superblock;
const Extract = @import("extract.zig");
const ExtractionOptions = @import("options.zig");
const Inode = @This();
hdr: Header,
data: Data,
pub fn init(alloc: std.mem.Allocator, rdr: *Reader, block_size: u32) !Inode {
pub fn init(alloc: std.mem.Allocator, block_size: u32, rdr: *Reader) !Inode {
var hdr: Header = undefined;
try rdr.readSliceEndian(Header, @ptrCast(&hdr), .little);
@@ -33,8 +36,18 @@ pub fn init(alloc: std.mem.Allocator, rdr: *Reader, block_size: u32) !Inode {
};
return .{ .hdr = hdr, .data = data };
}
pub fn initRef(alloc: std.mem.Allocator, ref: Ref, data: []u8, decomp: Decomp.Fn, inode_start: u64, block_size: u32) !Inode {}
pub fn initEntry(alloc: std.mem.Allocator, entry: Directory.Entry, block_size: u32) !Inode {}
pub fn initRef(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, inode_start: u64, block_size: u32, ref: Ref) !Inode {
var meta: MetadataReader = .init(alloc, data, decomp, inode_start + ref.block_start);
try meta.interface.discardAll(ref.block_offset);
return .init(alloc, &meta.interface, block_size);
}
pub fn initEntry(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, inode_start: u64, block_size: u32, entry: Directory.Entry) !Inode {
var meta: MetadataReader = .init(alloc, data, decomp, inode_start + entry.block_start);
try meta.interface.discardAll(entry.block_offset);
return .init(alloc, &meta.interface, block_size);
}
pub fn deinit(self: Inode, alloc: std.mem.Allocator) void {
switch (self.data) {
.file => |f| alloc.free(f.blocks),
@@ -45,6 +58,10 @@ pub fn deinit(self: Inode, alloc: std.mem.Allocator) void {
}
}
pub fn extract(self: Inode, alloc: std.mem.Allocator, io: Io, super: Superblock, data: []u8, decomp: Decomp.Fn, location: []const u8, options: ExtractionOptions) !void {
return Extract.extract(alloc, io, super, data, decomp, self, location, options);
}
// Types
pub const Ref = packed struct {