11 Commits
Author SHA1 Message Date
Caleb J. Gardner d00acb03b9 Small changed & fixes 2026-09-09 20:54:36 -05:00
Caleb J. Gardner 7f09a8ff69 Fixed File.open 2026-09-08 21:54:47 -05:00
Caleb J. Gardner 68d1a9d26d Renamed Table.zig to Lookup.Zig
Added Lookup.Value (reason for above)
Added FragCache
Small tweaks & fixes (I forgot about)
2026-09-08 21:37:30 -05:00
Caleb J. Gardner 2e4364b863 Started work on xattr table reading 2026-09-05 22:37:04 -05:00
Caleb J. Gardner 2e33e19cdd Added lookup Tables
Simplified Inode Data types & union
Started work on decompression
2026-09-02 10:53:01 -05:00
Caleb J. Gardner 173e76469f Created DataReader (untested)
Other minor fixes & tweaks
2026-08-30 02:37:37 -05:00
Caleb J. Gardner c5d75d3839 Further work on making everything work
Directory table reading
Finished MetadataReader
2026-08-29 06:40:34 -05:00
Caleb J. Gardner e0b697dd0b More work, specifically on decompression 2026-08-14 17:17:22 -05:00
Caleb J. Gardner 9f2a4bcc1e More work on stuff 2026-08-14 10:55:09 -05:00
Caleb J. Gardner 78dbaaed7e Re-adding stuff 2026-08-08 21:56:22 -05:00
Caleb J. Gardner 0039b19b4b Restarting...... Again. I have a problem. 2026-08-08 02:07:32 -05:00
34 changed files with 1463 additions and 2386 deletions
+1 -22
View File
@@ -11,7 +11,7 @@
"build": { "build": {
"command": "zig", "command": "zig",
"args": ["build", "-Ddebug=true"], "args": ["build", "-Ddebug=true", "-Dstatic=true"],
}, },
"program": "zig-out/bin/unsquashfs", "program": "zig-out/bin/unsquashfs",
@@ -22,25 +22,4 @@
"testing/LinuxPATest.sfs", "testing/LinuxPATest.sfs",
], ],
}, },
{
"label": "Build & Run Single-Threaded",
"adapter": "CodeLLDB",
"request": "launch",
"build": {
"command": "zig",
"args": ["build", "-Ddebug=true"],
},
"program": "zig-out/bin/unsquashfs",
"args": [
"--force",
"-p",
"1",
"-d",
"testing/TestExtractUnsquashfs",
"testing/LinuxPATest.sfs",
],
},
] ]
+55 -52
View File
@@ -1,93 +1,94 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.Build) !void { pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{}); const zig_decomp = b.option(bool, "zig_decomp", "Use zig standard library for decompression.") orelse false;
const target = b.standardTargetOptions(.{}); const allow_lzo = b.option(bool, "allow_lzo", "Compile with lzo support.") orelse false;
const static = b.option(bool, "static", "Statically link libraries.") orelse false;
const use_zig_decomp = b.option(bool, "use_zig_decomp", "Use zig standard library for decompression.") orelse false;
const allow_lzo = b.option(bool, "allow_lzo", "Compile with lzo support") orelse false;
const dynamic = b.option(bool, "dynamic", "Use dynamic instead of static linking.") orelse false;
const debug = b.option(bool, "debug", "Enable options to make debugging easier.") orelse false; const debug = b.option(bool, "debug", "Enable options to make debugging easier.") orelse false;
const version_string_option = b.option([]const u8, "version", "Version of the library/binary") orelse "0.0.0-testing"; const version_string_option = b.option([]const u8, "version", "Version of the library/binary") orelse "0.0.0-testing";
const version: std.SemanticVersion = try .parse(version_string_option); const target = b.standardTargetOptions(.{});
const optimize: std.builtin.OptimizeMode = if (debug) .Debug else b.standardOptimizeOption(.{});
const options = b.addOptions(); const version: std.SemanticVersion = try .parse(if (version_string_option[0] == 'v') version_string_option[1..] else version_string_option);
options.addOption(bool, "use_zig_decomp", use_zig_decomp);
options.addOption(bool, "allow_lzo", allow_lzo);
options.addOption(std.SemanticVersion, "version", version);
const c = b.addTranslateC(.{ const build_config = b.addOptions();
.optimize = optimize, build_config.addOption(bool, "zig_decomp", zig_decomp);
build_config.addOption(bool, "allow_lzo", allow_lzo);
build_config.addOption(bool, "debug", debug);
build_config.addOption(std.SemanticVersion, "version", version);
const c_h = b.addTranslateC(.{
.target = target, .target = target,
.root_source_file = b.path("c.h"), .optimize = optimize,
.root_source_file = b.path("src/c.h"),
}); });
if (allow_lzo) c.defineCMacro("ALLOW_LZO", null); if (allow_lzo) c_h.defineCMacro("ALLOW_LZO", null);
const c = c_h.createModule();
if (!use_zig_decomp and dynamic) { if (static) {
const zng = b.dependency("zlib_ng", .{
.target = target,
.optimize = optimize,
.zlib_compat = true,
});
c.linkLibrary(zng.artifact("zng"));
const zstd = b.dependency("zstd", .{ .optimize = optimize, .target = target });
c.linkLibrary(zstd.artifact("zstd"));
const lz4 = b.dependency("lz4", .{ .optimize = optimize, .target = target });
c.linkLibrary(lz4.artifact("lz4"));
const xz = b.dependency("xz", .{ .optimize = optimize, .target = target });
c.linkLibrary(xz.artifact("lzma"));
if (allow_lzo) {
const lzo = b.dependency("minilzo", .{ .optimize = optimize, .target = target });
c.linkLibrary(lzo.artifact("minilzo"));
}
} else {
c.linkSystemLibrary("z", .{}); c.linkSystemLibrary("z", .{});
c.linkSystemLibrary("lzma", .{});
c.linkSystemLibrary("lz4", .{});
c.linkSystemLibrary("zstd", .{}); c.linkSystemLibrary("zstd", .{});
c.linkSystemLibrary("lz4", .{});
c.linkSystemLibrary("lzma", .{});
if (allow_lzo) if (allow_lzo)
c.linkSystemLibrary("minilzo", .{}); c.linkSystemLibrary("minilzo", .{});
} }
const lib = b.addLibrary(.{ const lib = b.addLibrary(.{
.name = "squashfs", .name = "squashfs",
.use_llvm = debug, .root_module = b.addModule("zig_squashfs", .{
.version = version,
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"), .root_source_file = b.path("src/root.zig"),
.optimize = optimize,
.target = target, .target = target,
.optimize = optimize,
.valgrind = debug, .valgrind = debug,
.imports = &.{ .imports = &.{
.{ .name = "c", .module = c.createModule() }, .{ .name = "c", .module = c },
.{ .name = "build_options", .module = options.createModule() }, .{ .name = "build_config", .module = build_config.createModule() },
}, },
}), }),
.use_llvm = debug,
.version = version,
}); });
if (!use_zig_decomp and !dynamic) {
const zng = b.dependency("zlib_ng", .{ .optimize = optimize, .target = target, .zlib_compat = true });
lib.root_module.linkLibrary(zng.artifact("zng"));
const zstd = b.dependency("zstd", .{ .optimize = optimize, .target = target });
lib.root_module.linkLibrary(zstd.artifact("zstd"));
const lz4 = b.dependency("lz4", .{ .optimize = optimize, .target = target });
lib.root_module.linkLibrary(lz4.artifact("lz4"));
const xz = b.dependency("xz", .{ .optimize = optimize, .target = target });
lib.root_module.linkLibrary(xz.artifact("lzma"));
if (allow_lzo) {
const lzo = b.dependency("minilzo", .{ .optimize = optimize, .target = target });
lib.root_module.linkLibrary(lzo.artifact("minilzo"));
}
}
b.installArtifact(lib);
const unsquashfs_options = b.addOptions();
unsquashfs_options.addOption(std.SemanticVersion, "version", version);
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "unsquashfs", .name = "unsquashfs",
.use_llvm = debug,
.version = version,
.root_module = b.createModule(.{ .root_module = b.createModule(.{
.root_source_file = b.path("src/bin/unsquashfs.zig"), .root_source_file = b.path("src/bin/unsquashfs.zig"),
.optimize = optimize,
.target = target, .target = target,
.valgrind = debug, .optimize = optimize,
.imports = &.{ .imports = &.{
.{ .name = "squashfs", .module = lib.root_module }, .{ .name = "squashfs", .module = lib.root_module },
.{ .name = "build", .module = unsquashfs_options.createModule() }, .{ .name = "build_config", .module = build_config.createModule() },
}, },
.valgrind = debug,
}), }),
.use_llvm = debug,
.version = version,
}); });
b.installArtifact(lib);
b.installArtifact(exe); b.installArtifact(exe);
const mod_tests = b.addTest(.{ const mod_tests = b.addTest(.{
@@ -102,10 +103,12 @@ pub fn build(b: *std.Build) !void {
const lib_check = b.addLibrary(.{ const lib_check = b.addLibrary(.{
.name = "squashfs", .name = "squashfs",
.root_module = lib.root_module, .root_module = lib.root_module,
.use_llvm = debug,
}); });
const exe_check = b.addExecutable(.{ const exe_check = b.addExecutable(.{
.name = "unsquashfs", .name = "unsquashfs",
.root_module = exe.root_module, .root_module = exe.root_module,
.use_llvm = debug,
}); });
const check = b.step("check", "Check if unsquashfs compiles"); const check = b.step("check", "Check if unsquashfs compiles");
check.dependOn(&lib_check.step); check.dependOn(&lib_check.step);
+2 -6
View File
@@ -9,17 +9,13 @@
.hash = "zlib_ng-2.3.3-pre1-2HYS4MlFAADc-eSZv4_xjkxZRCdGUVOYZKdtqhybstgk", .hash = "zlib_ng-2.3.3-pre1-2HYS4MlFAADc-eSZv4_xjkxZRCdGUVOYZKdtqhybstgk",
}, },
.zstd = .{ .zstd = .{
.url = "git+https://github.com/allyourcodebase/zstd.git?ref=1.5.7-1#e1a501be57f42c541e8a5597e4b59a074dfd09a3", .url = "git+https://github.com/allyourcodebase/zstd.git?ref=1.5.7-2#4ccb01e6fceae86fb9fe36c60b8a103cdc058155",
.hash = "zstd-1.5.7-1-KEItkAMwAAD6OKY3m0OOmXG7aL-aLUfrDqbP5J5oYapU", .hash = "zstd-1.5.7-2-KEItkAMwAADNhCbGaImKrjXjnSA51jcqEbGCA3RasVZ9",
}, },
.lz4 = .{ .lz4 = .{
.url = "git+https://github.com/allyourcodebase/lz4.git?ref=1.10.0-6#41f52ab227caf9d48cf88c89a4d2946caa12b102", .url = "git+https://github.com/allyourcodebase/lz4.git?ref=1.10.0-6#41f52ab227caf9d48cf88c89a4d2946caa12b102",
.hash = "lz4-1.10.0-6-ewyzw-4NAAAWDpY4xpiqr4LQhZQAC0x_rGnW2iPh6jk2", .hash = "lz4-1.10.0-6-ewyzw-4NAAAWDpY4xpiqr4LQhZQAC0x_rGnW2iPh6jk2",
}, },
.xz = .{
.url = "git+https://github.com/akunaakwei/zig-xz.git#e2d389262c8291907e3e4c6fb119819141c16c0f",
.hash = "xz-5.8.2-6v47_JYeAABSL-jonprpL5-E_YaaGc4B5xrbe93WsJ3G",
},
.minilzo = .{ .minilzo = .{
.url = "git+https://github.com/CalebQ42/zig-minilzo.git#f72a0e20a30fad38cd10104f393c8f3440478697", .url = "git+https://github.com/CalebQ42/zig-minilzo.git#f72a0e20a30fad38cd10104f393c8f3440478697",
.hash = "minilzo-2.10.0-Ij7BO7YLAABd6oK2YdimXwvFTOC8wD1sNm0PHGoRNEwR", .hash = "minilzo-2.10.0-Ij7BO7YLAABd6oK2YdimXwvFTOC8wD1sNm0PHGoRNEwR",
-10
View File
@@ -1,10 +0,0 @@
#!/bin/sh
zig test \
-lc \
-lz \
-llzma \
-lminilzo \
-llz4 \
-lzstd \
src/test.zig
+96 -76
View File
@@ -1,88 +1,84 @@
const std = @import("std"); const std = @import("std");
const Io = std.Io; const Io = std.Io;
const MemoryMap = Io.File.MemoryMap;
const Decomp = @import("decomp.zig"); const util = @import("utils/util.zig");
const Inode = @import("inode.zig");
const File = @import("file.zig"); const File = @import("file.zig");
const ExtractionOptions = @import("options.zig"); const Inode = @import("inode.zig");
const Extract = @import("extract.zig"); const Decomp = @import("decomp.zig");
const Options = @import("options.zig");
const Decompress = @import("utils/decompress.zig");
const Archive = @This(); const Archive = @This();
super: Superblock, map: Io.File.MemoryMap,
map: MemoryMap, super: Super,
decomp: Decomp.Fn, root_inode_ref: Inode.Reference,
pub fn init(io: Io, file: Io.File, offset: u64) !Archive { pub fn init(io: Io, file: Io.File, offset: u64) !Archive {
var rdr = file.reader(io, &[0]u8{}); var map = try file.createMemoryMap(io, .{
try rdr.seekTo(offset); .len = try file.length(io) - offset,
.offset = offset,
.protection = .{ .read = true },
});
var super: Superblock = undefined; var superblock = util.readValue(Superblock, map.memory[0..@sizeOf(Superblock)]);
try rdr.interface.readSliceEndian(Superblock, @ptrCast(&super), .little); const super = try superblock.checkAndMinimize();
try super.validate();
return .{ return .{
.super = super, .map = map,
.map = try file.createMemoryMap(io, .{ .super = super,
.offset = offset, .root_inode_ref = superblock.root_inode_ref,
.len = super.size,
.protection = .{ .read = true },
}),
.decomp = try Decomp.getFn(super.compression),
}; };
} }
pub fn deinit(self: *Archive, io: Io) void { pub fn deinit(self: *Archive, io: Io) void {
self.map.destroy(io); self.map.destroy(io);
} }
pub fn root(self: Archive, alloc: std.mem.Allocator) !File { pub fn root(self: *Archive, alloc: std.mem.Allocator) !File {
return .initRef(alloc, self.super, self.map.memory, self.decomp, "", self.super.root_ref); return .{
} .alloc = alloc,
pub fn open(self: Archive, alloc: std.mem.Allocator, filepath: []const u8) !File { .data = self.map.memory,
const root_file = try self.root(alloc); .super = self.super,
const path = std.mem.trim(u8, filepath, "/"); .inode = try .readLocation(
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, alloc,
self.map.memory, self.map.memory,
self.decomp, self.root_inode_ref.start,
self.super.inode_start, self.root_inode_ref.offset,
self.super.block_size, self.super,
self.super.root_ref, ),
); .name = "",
defer root_inode.deinit(alloc); };
}
pub fn open(self: *Archive, alloc: std.mem.Allocator, filepath: []const u8) !File {
var root_file = try self.root(alloc);
defer root_file.deinit();
return root_inode.extract(alloc, io, self.super, self.map.memory, self.decomp, location, options); return root.open(alloc, filepath);
}
pub fn extract(self: *Archive, alloc: std.mem.Allocator, io: Io, ext_loc: []const u8, options: Options) !void {
const root_inode: Inode = try .readLocation(
alloc,
self.map.memory,
self.root_inode_ref.start,
self.root_inode_ref.offset,
self.super,
);
if (options.single_threaded)
return Decompress.single(alloc, io, self.map.memory, self.super, root_inode, ext_loc, options);
return Decompress.multi(alloc, io, self.map.memory, self.super, root_inode, ext_loc, options);
} }
// Superblock // Superblock
const SQUASHFS_MAGIC: u32 = std.mem.readInt(u32, "hsqs", .little); const Superblock = extern struct {
pub const MAGIC: u32 = 0x73717368;
const SuperblockError = error{
InvalidMagic,
InvalidBlockLog,
InvalidVersion,
InvalidCheck,
};
/// A squashfs Superblock
pub const Superblock = extern struct {
magic: u32, magic: u32,
inode_count: u32, inode_count: u32,
mod_time: u32, mod_time: u32,
@@ -94,38 +90,62 @@ pub const Superblock = extern struct {
inode_uncompressed: bool, inode_uncompressed: bool,
data_uncompressed: bool, data_uncompressed: bool,
check: bool, check: bool,
frag_uncompressed: bool, fragment_uncompressed: bool,
fragment_never: bool, fragment_never: bool,
fragment_always: bool, fragment_always: bool,
duplicates: bool, de_duplicate: bool,
exportable: bool, exportable: bool,
xattr_uncompressed: bool, xattr_uncompressed: bool,
xattr_never: bool, xattr_never: bool,
compression_options: bool, compression_options: bool,
ids_uncompressed: bool, id_uncompressed: bool,
_: u4, _: u4,
}, },
id_count: u16, id_count: u16,
ver_maj: u16, version_major: u16,
ver_min: u16, version_minor: u16,
root_ref: Inode.Ref, root_inode_ref: Inode.Reference,
size: u64, size: u64,
id_start: u64, id_table_start: u64,
xattr_start: u64, xattr_table_start: u64,
inode_start: u64, inode_table_start: u64,
dir_start: u64, dir_table_start: u64,
frag_start: u64, frag_table_start: u64,
export_start: u64, export_table_start: u64,
/// Validate the Superblock. If an error is returned, it's likely the archive is corrupted or not a squashfs archive. fn checkAndMinimize(self: Superblock) !Super {
pub fn validate(self: Superblock) !void { if (self.magic != MAGIC)
if (self.magic != SQUASHFS_MAGIC) return error.InvalidMagic;
return SuperblockError.InvalidMagic; if (self.version_major != 4 or self.version_minor != 0)
if (self.flags.check) return error.IncompatibleVersion;
return SuperblockError.InvalidCheck;
if (self.ver_maj != 4 or self.ver_min != 0)
return SuperblockError.InvalidVersion;
if (std.math.log2(self.block_size) != self.block_log) if (std.math.log2(self.block_size) != self.block_log)
return SuperblockError.InvalidBlockLog; return error.BadBlockLog;
if (self.flags.check)
return error.BadCheckFlag;
return .{
.block_size = self.block_size,
.frag_count = self.frag_count,
.decomp_fn = try self.compression.func(),
.id_count = self.id_count,
.id_table_start = self.id_table_start,
.xattr_table_start = self.xattr_table_start,
.inode_table_start = self.inode_table_start,
.dir_table_start = self.dir_table_start,
.frag_table_start = self.frag_table_start,
.export_table_start = self.export_table_start,
};
} }
}; };
pub const Super = struct {
block_size: u32,
frag_count: u32,
decomp_fn: Decomp.Fn,
id_count: u16,
id_table_start: u64,
xattr_table_start: u64,
inode_table_start: u64,
dir_table_start: u64,
frag_table_start: u64,
export_table_start: u64,
};
+24 -145
View File
@@ -1,10 +1,11 @@
const std = @import("std"); const std = @import("std");
const Io = std.Io; const Io = std.Io;
const Writer = Io.Writer; const Writer = Io.Writer;
const builtin = @import("builtin");
const build = @import("build");
const config = @import("build_config");
const squashfs = @import("squashfs"); const squashfs = @import("squashfs");
const Archive = squashfs.Archive;
const Options = squashfs.Options;
//TODO: Add more options //TODO: Add more options
const help_mgs = const help_mgs =
@@ -13,8 +14,6 @@ const help_mgs =
\\ \\
\\Options: \\Options:
\\ -d <location> Extract to the given location instead of "squashfs-root" \\ -d <location> Extract to the given location instead of "squashfs-root"
\\ -f <filepath> Extract the files at the given filepath instead of the entire archive
\\ Can be given multiple times.
\\ \\
\\ -o <offset> Start reading the archive at the given offset. \\ -o <offset> Start reading the archive at the given offset.
\\ -dx Don't set xattr values \\ -dx Don't set xattr values
@@ -30,160 +29,40 @@ const help_mgs =
\\ \\
; ;
const errors = error{InvalidArguments}; var arc_loc: []const u8 = "";
var archive: []const u8 = "";
var ext_loc: []const u8 = "squashfs-root"; var ext_loc: []const u8 = "squashfs-root";
var offset: u64 = 0;
var threads: u32 = 0;
var verbose: bool = false;
var ignore_xattrs: bool = false;
var ignore_permissions: bool = false;
var force: bool = false;
var files: [][:0]const u8 = &[0][:0]const u8{};
var limited: ?Io.Threaded = null; var offset: u64 = 0;
var threads: usize = 0;
var force: bool = false;
var options: Options = .default;
pub fn main(init: std.process.Init) !void { pub fn main(init: std.process.Init) !void {
var io = init.io; var io = init.io;
const alloc = init.gpa; const alloc = init.gpa;
var stdout = Io.File.stdout(); // TODO: process args
var out = stdout.writer(io, &[0]u8{});
defer out.interface.flush() catch {};
try handleArgs(init.minimal.args, &out.interface); var limited_io: Io.Threaded = undefined;
if (archive.len == 0) { if (threads != 0) {
try out.interface.print("You must provide a squashfs archive\n", .{}); limited_io = if (threads == 1)
try out.interface.print(help_mgs, .{}); Io.Threaded.init_single_threaded
return; else
} Io.Threaded.init(alloc, .{
var fil = try Io.Dir.cwd().openFile(io, archive, .{}); //TODO: Handle error gracefully. .async_limit = .limited(threads),
defer fil.close(io); .concurrent_limit = .limited(threads),
var arc: squashfs.Archive = try .init(io, fil, offset); //TODO: Update when memory size matters. //TODO: Handle error gracefully.
defer arc.deinit(io);
const options: squashfs.ExtractionOptions = .{
.single_threaded = (threads == 1),
.verbose = verbose,
.verbose_writer = if (verbose) &out.interface else null,
.ignore_xattr = ignore_xattrs,
.ignore_permissions = ignore_permissions,
};
if (threads > 1) {
limited = Io.Threaded.init(alloc, .{
.argv0 = .init(init.minimal.args), .argv0 = .init(init.minimal.args),
.async_limit = @enumFromInt(threads),
.concurrent_limit = @enumFromInt(threads),
.environ = init.minimal.environ, .environ = init.minimal.environ,
}); });
io = limited.?.io(); io = limited_io.io();
} }
if (force) var fil = try Io.Dir.cwd().openFile(io, arc_loc, .{});
try Io.Dir.cwd().deleteTree(io, ext_loc); defer fil.close(io);
if (files.len > 0) { var arc: Archive = try .open(io, fil, offset);
for (0..files.len) |i| { defer arc.close(io);
var sfs_fil = try arc.open(alloc, files[i]);
defer sfs_fil.deinit();
const last_ind = std.mem.lastIndexOf(u8, files[i], "/"); try arc.extract(alloc, io, ext_loc, options);
const file_name = if (last_ind != null) files[i][last_ind..] else files[i];
const fil_ext_loc = if (ext_loc.len > 0)
try std.mem.concat(alloc, u8, &.{ ext_loc, "/", file_name })
else
file_name;
defer if (fil_ext_loc.len != file_name.len) alloc.free(fil_ext_loc);
try sfs_fil.extract(alloc, io, fil_ext_loc, options);
}
} else {
try arc.extract(alloc, io, ext_loc, options); //TODO: Handle error gracefully.
}
}
fn handleArgs(alloc: std.mem.Allocator, 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| {
if (std.mem.eql(u8, arg, "-o")) {
const nxt = args.next();
if (nxt == null or nxt.?.len == 0) {
try out.print("-o must be followed by a number\n", .{});
return errors.InvalidArguments;
}
offset = std.fmt.parseInt(u64, nxt.?, 10) catch {
try out.print("-o must be followed by a number\n", .{});
return errors.InvalidArguments;
};
continue;
} else if (std.mem.eql(u8, arg, "-f")) {
const nxt = args.next();
if (nxt == null or nxt.?.len == 0) {
try out.print("-d must be followed by a location\n", .{});
return errors.InvalidArguments;
}
if (!alloc.resize(files, files.len + 1)) {
const new_alloc = alloc.alloc([]const u8, files.len + 1);
@memcpy(new_alloc[0..files.len], files);
alloc.free(files);
files = new_alloc;
} else {
files.len += 1;
}
files[files.len - 1] = nxt.?;
continue;
} else if (std.mem.eql(u8, arg, "-d")) {
const nxt = args.next();
if (nxt == null or nxt.?.len == 0) {
try out.print("-d must be followed by a location\n", .{});
return errors.InvalidArguments;
}
ext_loc = std.mem.trim(u8, nxt.?, "/");
continue;
} else if (std.mem.eql(u8, arg, "-p")) {
const nxt = args.next();
if (nxt == null or nxt.?.len == 0) {
try out.print("-p must be followed by a number\n", .{});
return errors.InvalidArguments;
}
threads = std.fmt.parseInt(u32, nxt.?, 10) catch {
try out.print("-p must be followed by a number\n", .{});
return errors.InvalidArguments;
};
continue;
} else if (std.mem.eql(u8, arg, "-v")) {
verbose = true;
continue;
} else if (std.mem.eql(u8, arg, "-dx")) {
ignore_xattrs = true;
continue;
} else if (std.mem.eql(u8, arg, "-dp")) {
ignore_permissions = true;
continue;
} else if (std.mem.eql(u8, arg, "--force")) {
force = true;
continue;
} else if (std.mem.eql(u8, arg, "--version")) {
try out.print("zig-unsquashfs v", .{});
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;
} else if (std.mem.eql(u8, arg, "--help")) {
try out.print(help_mgs, .{});
std.process.exit(0);
return;
}
if (archive.len > 0) {
try out.print("you can only provide one file at a time\n", .{});
try out.print(help_mgs, .{});
return errors.InvalidArguments;
}
archive = arg;
}
} }
+2 -2
View File
@@ -1,6 +1,6 @@
#include <zlib.h>
#include <lzma.h>
#include <lz4.h> #include <lz4.h>
#include <lzma.h>
#include <zlib.h>
#include <zstd.h> #include <zstd.h>
#ifdef ALLOW_LZO #ifdef ALLOW_LZO
-56
View File
@@ -1,56 +0,0 @@
const std = @import("std");
const c = @import("c");
const Error = @import("decomp.zig").Error;
pub fn zlib(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var stream: c.z_stream = .{
.next_in = in.ptr,
.avail_in = @truncate(in.len),
.next_out = out.ptr,
.avail_out = @truncate(out.len),
};
var res = c.inflateInit(&stream);
if (res != c.Z_OK)
return Error.DecompressionFailed;
res = c.inflate(&stream, c.Z_FULL_FLUSH);
if (res != c.Z_OK)
return Error.DecompressionFailed;
return stream.total_out;
}
pub fn lzma(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var stream: c.lzma_stream = .{
.next_in = in.ptr,
.avail_in = in.len,
.next_out = out.ptr,
.avail_out = out.len,
};
var res = c.lzma_auto_decoder(&stream, 0, 0);
if (res != c.LZMA_OK)
return Error.DecompressionFailed;
while (res == c.LZMA_OK)
res = c.lzma_code(&stream, c.LZMA_RUN);
if (res != c.LZMA_STREAM_END)
return Error.DecompressionFailed;
return stream.total_out;
}
pub fn lz4(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
const res = c.LZ4_decompress_safe(
in.ptr,
out.ptr,
@intCast(in.len),
@intCast(out.len),
);
if (res < 0)
return Error.DecompressionFailed;
return @abs(res);
}
pub fn zstd(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
const res = c.ZSTD_decompress(out.ptr, out.len, in.ptr, in.len);
if (c.ZSTD_isError(res) != 0) {
std.debug.print("decompression failed: {s}\n", .{c.ZSTD_getErrorName(res)});
return Error.DecompressionFailed;
}
return res;
}
-125
View File
@@ -1,125 +0,0 @@
const std = @import("std");
const Io = std.Io;
const Decomp = @import("../decomp.zig");
const DataBlock = @import("../inode.zig").DataBlock;
const Cache = @import("../util/cache.zig");
const Extractor = @This();
data: []u8,
decomp: Decomp.Fn,
block_size: u32,
blocks: []DataBlock,
start: u64,
size: u64,
frag_data: ?[]u8 = null,
frag_offset: u32 = 0,
cache: ?*Cache = null,
pub fn init(data: []u8, decomp: Decomp.Fn, block_size: u32, blocks: []DataBlock, start: u64, size: u64) Extractor {
return .{
.data = data,
.decomp = decomp,
.block_size = block_size,
.blocks = blocks,
.start = start,
.size = size,
};
}
pub fn addFrag(self: *Extractor, frag_data: []u8, frag_offset: u32) void {
self.frag_data = frag_data;
self.frag_offset = frag_offset;
}
pub fn addCache(self: *Extractor, cache: *Cache) void {
self.cache = cache;
}
pub fn extractAsync(self: Extractor, alloc: std.mem.Allocator, io: Io, file: Io.File) Error!void {
if (self.size == 0) return;
try file.writePositionalAll(io, &[0]u8{}, self.size);
var map = try file.createMemoryMap(io, .{
.len = self.size,
.protection = .{ .write = true },
});
defer map.destroy(io);
var err: ?Error = null;
var group: Io.Group = .init;
var read_offset: u64 = self.start;
for (0.., self.blocks) |i, block| {
group.async(io, blockThread, .{ self, alloc, io, map.memory, read_offset, @truncate(i), &err });
read_offset += block.size;
}
if (self.frag_data != null)
group.async(io, fragThread, .{ self, map.memory });
try group.await(io);
if (err != null)
return err.?;
try map.write(io);
}
fn blockThread(self: Extractor, alloc: std.mem.Allocator, io: Io, map_data: []u8, read_offset: u64, block_idx: u32, err: *?Error) error{Canceled}!void {
const size = if (self.frag_data == null and block_idx == (self.size - 1 / self.block_size))
self.size % self.block_size
else
self.block_size;
const offset = block_idx * self.block_size;
const block = self.blocks[block_idx];
if (block.size == 0) {
@memset(map_data[offset..][0..size], 0);
return;
}
const data = self.data[read_offset..][0..block.size];
if (block.uncompressed) {
@memcpy(map_data[offset..][0..block.size], data);
return;
}
std.debug.print("offset: {} start: {} block: {any}\n", .{ read_offset, self.start, block });
if (self.cache != null) {
const decomp_block = self.cache.?.get(io, read_offset, block.size) catch |inner_err| {
std.debug.print("cache extractor err: {}\n", .{inner_err});
switch (inner_err) {
error.Canceled => return error.Canceled,
else => |e| err.* = e,
}
return;
};
std.debug.print("cached block size: {} should be {}\n", .{ decomp_block.len, size });
@memcpy(map_data[offset..][0..size], decomp_block[0..size]);
} else {
_ = self.decomp(alloc, data, map_data[offset..][0..size]) catch |inner_err| {
std.debug.print("data decomp err: {}\n", .{inner_err});
err.* = inner_err;
};
}
}
fn fragThread(self: Extractor, map_data: []u8) error{Canceled}!void {
const size = self.size % self.block_size;
const offset = self.blocks.len * self.block_size;
@memcpy(map_data[offset..][0..size], self.frag_data.?[self.frag_offset..][0..size]);
}
// Types
pub const Error = Io.File.WritePositionalError || Io.File.MemoryMap.CreateError || Decomp.Error || Cache.Error;
-168
View File
@@ -1,168 +0,0 @@
const std = @import("std");
const Io = std.Io;
const Decomp = @import("../decomp.zig");
const DataBlock = @import("../inode.zig").DataBlock;
const Cache = @import("../util/cache.zig");
const Reader = @This();
alloc: std.mem.Allocator,
data: []u8,
decomp: Decomp.Fn,
block_size: u32,
blocks: []DataBlock,
size: u64,
offset: u64,
block_idx: u32 = 0,
sparse_block: bool = false,
frag_data: ?[]u8 = null,
frag_offset: u32 = 0,
io: ?Io = null,
cache: ?*Cache = null,
block: [1024 * 1024]u8 = undefined,
interface: Io.Reader = .{
.buffer = &[0]u8{},
.end = 0,
.seek = 0,
.vtable = &.{
.stream = stream,
.discard = discard,
.readVec = readVec,
},
},
pub fn init(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, block_size: u32, blocks: []DataBlock, size: u64, data_start: u64) Reader {
return .{
.alloc = alloc,
.data = data,
.decomp = decomp,
.block_size = block_size,
.blocks = blocks,
.size = size,
.offset = data_start,
};
}
pub fn addFrag(self: *Reader, frag_data: []u8, frag_offset: u32) void {
self.frag_data = frag_data;
self.frag_offset = frag_offset;
}
pub fn addCache(self: *Reader, io: Io, cache: *Cache) void {
self.io = io;
self.cache = cache;
}
fn advance(self: *Reader) Io.Reader.Error!void {
if (self.block_idx > self.blocks.len) return error.EndOfStream;
defer self.block_idx += 1;
self.interface.seek = 0;
errdefer self.interface.end = 0;
if (self.block_idx == self.blocks.len) {
if (self.frag_data == null) return error.EndOfStream;
self.sparse_block = false;
const size = self.size % self.block_size;
self.interface.buffer = self.frag_data.?[self.frag_offset..][0..size];
self.interface.end = size;
return;
}
const size = if (self.frag_data == null and self.block_idx == self.blocks.len - 1)
self.size % self.block_size
else
self.block_size;
const block = self.blocks[self.block_idx];
defer self.offset += block.size;
if (block.size == 0) {
self.sparse_block = true;
self.interface.end = size;
return;
} else {
self.sparse_block = false;
}
if (block.uncompressed) {
self.interface.buffer = self.data[self.offset..][0..block.size];
self.interface.end = block.size;
return;
}
if (self.cache == null) {
_ = self.decomp(self.alloc, self.data[self.offset..][0..block.size], self.block[0..size]) catch return error.ReadFailed;
self.interface.buffer = self.block[0..size];
self.interface.end = size;
} else {
self.interface.buffer = self.cache.?.get(self.io.?, self.offset, block.size) catch return error.ReadFailed;
self.interface.end = self.interface.buffer.len;
}
}
fn stream(r: *Io.Reader, w: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize {
const self: *Reader = @fieldParentPtr("interface", r);
if (r.seek >= r.end)
try self.advance();
if (limit == .nothing) return 0;
const to_write = @min(@intFromEnum(limit), r.end - r.seek);
const wrote = if (self.sparse_block)
try w.splatByte(0, to_write)
else
try w.write(r.buffer[r.seek..][0..to_write]);
r.seek += wrote;
return wrote;
}
fn discard(r: *Io.Reader, limit: Io.Limit) Io.Reader.Error!usize {
if (r.seek >= r.end) {
const self: *Reader = @fieldParentPtr("interface", r);
try self.advance();
}
if (limit == .nothing) return 0;
const to_discard = @min(@intFromEnum(limit), r.end - r.seek);
r.seek += to_discard;
return to_discard;
}
fn readVec(r: *Io.Reader, vec: [][]u8) Io.Reader.Error!usize {
const self: *Reader = @fieldParentPtr("interface", r);
if (r.seek >= r.end)
try self.advance();
var copied: usize = 0;
for (vec) |v| {
const to_cpy = @min(v.len, r.end - r.seek);
if (self.sparse_block)
@memset(v[0..to_cpy], 0)
else
@memcpy(v[0..to_cpy], r.buffer[r.seek..][0..to_cpy]);
copied += to_cpy;
if (r.seek >= r.end) break;
}
return copied;
}
+136 -22
View File
@@ -1,23 +1,8 @@
const std = @import("std"); const std = @import("std");
const build = @import("build_options"); const Io = std.Io;
const c = @import("c_decomp.zig"); const c = @import("c");
const zig = @import("zig_decomp.zig"); const build = @import("build_config");
pub fn getFn(e: Enum) !Fn {
return switch (e) {
.gzip => if (build.use_zig_decomp) zig.zlib else c.zlib,
.lzma => if (build.use_zig_decomp) zig.lzma else c.lzma,
.lzo => if (build.use_zig_decomp or !build.allow_lzo) error.LzoUnsupported else c.lzo,
.xz => if (build.use_zig_decomp) zig.xz else c.lzma,
.lz4 => if (build.use_zig_decomp) error.Lz4Unsupported else c.lz4,
.zstd => if (build.use_zig_decomp) zig.zstd else c.zstd,
};
}
// Types
pub const Fn = *const fn (std.mem.Allocator, in: []u8, out: []u8) Error!usize;
pub const Enum = enum(u16) { pub const Enum = enum(u16) {
gzip = 1, gzip = 1,
@@ -26,9 +11,138 @@ pub const Enum = enum(u16) {
xz, xz,
lz4, lz4,
zstd, zstd,
};
pub const Error = error{ pub fn func(self: Enum) !Fn {
OutOfMemory, return switch (self) {
DecompressionFailed, .gzip => if (build.zig_decomp) zigZlib else cZlib,
.lzma => if (build.zig_decomp) zigLzma else cLzma,
.lzo => if (build.zig_decomp or !build.allow_lzo) error.LzoUnsupported else cLzo,
.xz => if (build.zig_decomp) zigXz else cXz,
.lz4 => if (build.zig_decomp) error.Lz4Unsupported else cLz4,
.zstd => if (build.zig_decomp) zigZstd else cZstd,
};
}
}; };
pub const Fn = *const fn (alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize;
pub const Error = Io.Reader.Error || std.mem.Allocator.Error;
// Actual decomp functions
fn zigZlib(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var rdr: Io.Reader = .fixed(in);
var decomp: std.compress.flate.Decompress = .init(&rdr, .zlib, &[0]u8{});
return decomp.reader.readSliceShort(out);
}
fn cZlib(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var stream: c.z_stream = .{
.avail_in = @truncate(in.len),
.next_in = in.ptr,
.avail_out = @truncate(out.len),
.next_out = out.ptr,
};
const res = c.inflate(&stream, c.Z_FULL_FLUSH);
if (res != c.Z_OK)
return Error.ReadFailed;
return stream.total_out;
}
fn zigLzma(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var rdr: Io.Reader = .fixed(in);
var decomp: std.compress.lzma.Decompress = .initOptions(&rdr, alloc, &[0]u8{}, .{}, 0);
defer decomp.deinit();
return decomp.reader.readSliceShort(out);
}
fn cLzma(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var stream: c.lzma_stream = .{
.avail_in = in.len,
.next_in = in.ptr,
.avail_out = out.len,
.next_out = out.ptr,
};
defer c.lzma_end(&stream);
var res = c.lzma_alone_decoder(&stream, 0);
if (res != c.LZMA_OK)
return Error.ReadFailed;
while (res == c.LZMA_OK)
res = c.lzma_code(&stream, c.LZMA_RUN);
if (res != c.LZMA_FINISH)
return Error.ReadFailed;
return stream.total_out;
}
fn cLzo(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var res = c.lzo_init();
if (res != c.LZO_E_OK)
return Error.ReadFailed;
var len = out.len;
res = c.lzo1x_decompress(in.ptr, in.len, out.ptr, &len, null);
if (res != c.LZO_E_OK)
return Error.ReadFailed;
return Error.ReadFailed;
}
fn zigXz(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var rdr: Io.Reader = .fixed(in);
var decomp: std.compress.xz.Decompress = .init(&rdr, alloc, &[0]u8{});
defer decomp.deinit();
return decomp.reader.readSliceShort(out);
}
fn cXz(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var stream: c.lzma_stream = .{
.avail_in = in.len,
.next_in = in.ptr,
.avail_out = out.len,
.next_out = out.ptr,
};
defer c.lzma_end(&stream);
var res = c.lzma_stream_decoder(&stream, 0, 0);
if (res != c.LZMA_OK)
return Error.ReadFailed;
while (res == c.LZMA_OK)
res = c.lzma_code(&stream, c.LZMA_RUN);
if (res != c.LZMA_FINISH)
return Error.ReadFailed;
return stream.total_out;
}
fn cLz4(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
const res = c.LZ4_decompress_safe(in.ptr, out.ptr, @intCast(in.len), @intCast(out.len));
if (res < 0) return Error.ReadFailed;
return @abs(res);
}
fn zigZstd(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
var rdr: Io.Reader = .fixed(in);
const buf = try alloc.alloc(u8, 1024 * 1024);
defer alloc.free(buf);
var decomp: std.compress.zstd.Decompress = .init(&rdr, buf, .{
.window_len = 1024 * 1024,
});
return decomp.reader.readSliceShort(out);
}
fn cZstd(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
const res = c.ZSTD_decompress(out.ptr, out.len, in.ptr, in.len);
if (c.ZSTD_isError(res) == 1) {
std.debug.print("{s}\n", .{c.ZSTD_getErrorName(res)});
return Error.ReadFailed;
}
return res;
}
+74
View File
@@ -0,0 +1,74 @@
const std = @import("std");
const Io = std.Io;
const utils = @import("utils/util.zig");
const Inode = @import("inode.zig");
const Directory = @This();
entries: []Entry,
pub fn read(alloc: std.mem.Allocator, rdr: *Io.Reader, size: u32) !Directory {
var red: u32 = 3;
var out: std.ArrayList(Entry) = try .initCapacity(alloc, 10);
errdefer out.deinit(alloc);
while (red < size) {
const hdr: Header = try utils.readValueRdr(Header, rdr);
red += @sizeOf(Header);
try out.ensureUnusedCapacity(alloc, hdr.count + 1);
for (0..hdr.count + 1) |_| {
const raw: RawEntry = try utils.readValueRdr(RawEntry, rdr);
const name = try alloc.alloc(u8, raw.name_size + 1);
errdefer alloc.free(name);
try rdr.readSliceAll(name);
out.addOneAssumeCapacity().* = .{
.start = hdr.start,
.offset = raw.offset,
.type = raw.type,
.name = name,
};
red += @sizeOf(RawEntry) + raw.name_size + 1;
}
}
return .{ .entries = try out.toOwnedSlice(alloc) };
}
pub fn deinit(self: Directory, alloc: std.mem.Allocator) void {
for (self.entries) |entry|
entry.deinit(alloc);
alloc.free(self.entries);
}
// Types
pub const Entry = struct {
start: u32,
offset: u16,
type: Inode.Type,
name: []const u8,
pub fn deinit(self: Entry, alloc: std.mem.Allocator) void {
alloc.free(self.name);
}
};
const Header = extern struct {
count: u32,
start: u32,
inode_num: u32,
};
const RawEntry = extern struct {
offset: u16,
inode_num_offset: i16,
type: Inode.Type,
name_size: u16,
};
-85
View File
@@ -1,85 +0,0 @@
const std = @import("std");
const Reader = std.Io.Reader;
const Inode = @import("inode.zig");
const Directory = @This();
entries: []Entry,
pub fn init(alloc: std.mem.Allocator, rdr: *Reader, size: u64) !Directory {
if (size <= 3) return Directory{ .entries = &[0]Entry{} };
var hdr: Header = undefined;
var raw: RawEntry = undefined;
var read: u64 = 3;
var out: std.ArrayList(Entry) = try .initCapacity(alloc, 50);
errdefer {
for (out.items) |entry|
entry.deinit(alloc);
out.deinit(alloc);
}
while (read < size) {
try rdr.readSliceEndian(Header, @ptrCast(&hdr), .little);
read += @sizeOf(Header);
try out.ensureUnusedCapacity(alloc, hdr.count + 1);
for (0..hdr.count + 1) |_| {
try rdr.readSliceEndian(RawEntry, @ptrCast(&raw), .little);
const name = try alloc.alloc(u8, raw.name_size + 1);
try rdr.readSliceEndian(u8, name, .little);
const entry = out.addOneAssumeCapacity();
entry.* = .{
.name = name,
.block_start = hdr.block_start,
.block_offset = raw.block_offset,
.type = raw.type,
};
read += @sizeOf(RawEntry) + raw.name_size + 1;
}
}
return .{ .entries = try out.toOwnedSlice(alloc) };
}
pub fn deinit(self: Directory, alloc: std.mem.Allocator) void {
for (self.entries) |entry|
entry.deinit(alloc);
alloc.free(self.entries);
}
// Types
pub const Error = error{OutOfMemory} || Reader.Error;
pub const Entry = struct {
name: []const u8,
block_start: u32,
block_offset: u16,
type: Inode.Type,
pub fn deinit(self: Entry, alloc: std.mem.Allocator) void {
alloc.free(self.name);
}
};
const Header = extern struct {
count: u32,
block_start: u32,
num: u32,
};
const RawEntry = extern struct {
block_offset: u16,
num_offset: i16,
type: Inode.Type,
name_size: u16,
};
-427
View File
@@ -1,427 +0,0 @@
const std = @import("std");
const Io = std.Io;
const DataExtractor = @import("data/extractor.zig");
const DataReader = @import("data/reader.zig");
const Decomp = @import("decomp.zig");
const Directory = @import("directory.zig");
const ExtractionOptions = @import("options.zig");
const Inode = @import("inode.zig");
const Lookup = @import("lookup.zig");
const MetadataReader = @import("meta_rdr.zig");
const Superblock = @import("archive.zig").Superblock;
const Cache = @import("util/cache.zig");
const XattrTable = @import("xattr.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 {
const path = std.mem.trim(u8, ext_loc, "/");
var id_table: Lookup.Table(u16) = .init(alloc, data, decomp, super.id_start, super.id_count);
defer id_table.deinit();
var xattr_table: XattrTable = .init(alloc, data, decomp, super.xattr_start);
defer xattr_table.deinit();
var buf: [150]ReturnUnion = undefined;
var sel: Io.Select(ReturnUnion) = .init(io, &buf);
defer while (sel.cancel()) |res|
switch (res) {
.path => |p| {
const path_return = p catch continue;
if (path_return.path.len != path.len)
alloc.free(path_return.path);
},
else => {},
};
var loop = io.async(finishLoop, .{ alloc, io, &sel, &id_table, &xattr_table, inode.hdr.num, options });
var cache: Cache = .init(alloc, data, decomp);
defer cache.deinit();
var frag_table: Lookup.Table(Lookup.FragEntry) = .init(alloc, data, decomp, super.frag_start, super.frag_count);
defer frag_table.deinit();
switch (inode.hdr.type) {
.file, .ext_file => sel.async(
.path,
extractFile,
.{ alloc, io, data, decomp, super.block_size, &cache, &frag_table, inode, path, true },
),
.dir, .ext_dir => sel.async(
.path,
extractDir,
.{ alloc, io, super, data, decomp, &sel, &cache, &frag_table, inode, path, true },
),
.symlink, .ext_symlink => sel.async(
.void,
extractSymlink,
.{ alloc, io, inode, path, true },
),
else => sel.async(
.path,
extractNod,
.{ alloc, inode, path, true },
),
}
try loop.await(io);
}
fn dirOrder(_: void, a: PathReturn, b: PathReturn) std.math.Order {
return std.math.order(std.mem.count(u8, a.path, "/"), std.mem.count(u8, b.path, "/"));
}
fn finishLoop(alloc: std.mem.Allocator, io: Io, sel: *Io.Select(ReturnUnion), id_table: *Lookup.Table(u16), xattr_table: *XattrTable, start_num: u32, options: ExtractionOptions) !void {
var dirs: std.PriorityDequeue(PathReturn, void, dirOrder) = .empty;
defer dirs.deinit(alloc);
errdefer while (dirs.popMax()) |d|
if (d.hdr.num != start_num) alloc.free(d.path);
while (true) {
const value: ReturnUnion = try sel.await();
const path_ret = switch (value) {
.void => {
_ = sel.group.token.load(.unordered) orelse break;
continue;
},
.path => |p| try p,
};
if (options.ignore_permissions and (options.ignore_xattr or path_ret.xattr_idx == null)) {
if (path_ret.hdr.num != start_num)
alloc.free(path_ret.path);
continue;
}
if (path_ret.hdr.type == .dir or path_ret.hdr.type == .ext_dir) {
dirs.push(alloc, path_ret) catch |err| {
if (path_ret.hdr.num != start_num)
alloc.free(path_ret.path);
return err;
};
continue;
}
defer if (path_ret.hdr.num != start_num)
alloc.free(path_ret.path);
var file = try Io.Dir.cwd().openFile(io, path_ret.path, .{});
defer file.close(io);
if (!options.ignore_xattr and path_ret.xattr_idx != null) {
const xattr = try xattr_table.get(alloc, io, path_ret.xattr_idx.?);
defer xattr.deinit(alloc);
for (xattr.kvs) |kv| {
const res = std.os.linux.fsetxattr(file.handle, kv.key, kv.value.ptr, kv.value.len, 0);
if (res != 0)
return error.SetXattrError;
}
}
if (!options.ignore_permissions) {
try file.setTimestamps(io, .{
.modify_timestamp = .init(Io.Timestamp.fromNanoseconds(@as(i96, @intCast(path_ret.hdr.mod_time)) * std.time.ns_per_s)),
});
try file.setPermissions(io, @enumFromInt(path_ret.hdr.permissions));
try file.setOwner(io, try id_table.get(io, path_ret.hdr.uid_idx), try id_table.get(io, path_ret.hdr.gid_idx));
}
_ = sel.group.token.load(.unordered) orelse break;
}
while (dirs.popMax()) |path_ret| {
if (path_ret.hdr.num != start_num)
alloc.free(path_ret.path);
var file = try Io.Dir.cwd().openFile(io, path_ret.path, .{});
defer file.close(io);
if (!options.ignore_xattr and path_ret.xattr_idx != null) {
const xattr = try xattr_table.get(alloc, io, path_ret.xattr_idx.?);
defer xattr.deinit(alloc);
for (xattr.kvs) |kv| {
const res = std.os.linux.fsetxattr(file.handle, kv.key, kv.value.ptr, kv.value.len, 0);
if (res != 0)
return error.SetXattrError;
}
}
if (!options.ignore_permissions) {
try file.setTimestamps(io, .{
.modify_timestamp = .init(Io.Timestamp.fromNanoseconds(@as(i96, @intCast(path_ret.hdr.mod_time)) * std.time.ns_per_s)),
});
try file.setPermissions(io, @enumFromInt(path_ret.hdr.permissions));
try file.setOwner(io, try id_table.get(io, path_ret.hdr.uid_idx), try id_table.get(io, path_ret.hdr.gid_idx));
}
}
}
fn extractDir(
alloc: std.mem.Allocator,
io: Io,
super: Superblock,
data: []u8,
decomp: Decomp.Fn,
sel: *Io.Select(ReturnUnion),
cache: *Cache,
frag_table: *Lookup.Table(Lookup.FragEntry),
inode: Inode,
path: []const u8,
origin: bool,
) Error!PathReturn {
defer if (!origin) inode.deinit(alloc);
errdefer if (!origin) alloc.free(path);
var ret: PathReturn = .{
.hdr = inode.hdr,
.path = path,
};
try Io.Dir.cwd().createDirPath(io, path);
var dir: Directory = switch (inode.data) {
.dir => |d| blk: {
var meta: MetadataReader = .init(alloc, data, decomp, d.block_start + super.dir_start);
try meta.interface.discardAll(d.block_offset);
break :blk try Directory.init(alloc, &meta.interface, d.size);
},
.ext_dir => |d| blk: {
if (d.xattr_idx != 0xFFFFFFFF) ret.xattr_idx = d.xattr_idx;
var meta: MetadataReader = .init(alloc, data, decomp, d.block_start + super.dir_start);
try meta.interface.discardAll(d.block_offset);
break :blk try Directory.init(alloc, &meta.interface, d.size);
},
else => unreachable,
};
defer dir.deinit(alloc);
for (dir.entries) |entry| {
var new_inode: Inode = try .initEntry(alloc, data, decomp, super.inode_start, super.block_size, entry);
const new_path = std.mem.concat(alloc, u8, &.{ path, "/", entry.name }) catch |err| {
new_inode.deinit(alloc);
return err;
};
switch (entry.type) {
.dir => sel.async(.path, extractDir, .{ alloc, io, super, data, decomp, sel, cache, frag_table, new_inode, new_path, false }),
.file => sel.async(.path, extractFile, .{ alloc, io, data, decomp, super.block_size, cache, frag_table, new_inode, new_path, false }),
.symlink => sel.async(.void, extractSymlink, .{ alloc, io, new_inode, new_path, false }),
else => sel.async(.path, extractNod, .{ alloc, new_inode, new_path, false }),
}
}
return ret;
}
fn extractFile(
alloc: std.mem.Allocator,
io: Io,
data: []u8,
decomp: Decomp.Fn,
block_size: u32,
cache: *Cache,
frag_table: *Lookup.Table(Lookup.FragEntry),
inode: Inode,
path: []const u8,
origin: bool,
) Error!PathReturn {
defer if (!origin) inode.deinit(alloc);
errdefer if (!origin) alloc.free(path);
try io.checkCancel();
var ret: PathReturn = .{
.hdr = inode.hdr,
.path = path,
};
// var ext: DataExtractor = switch (inode.data) {
// .file => |f| blk: {
// var rdr: DataExtractor = .init(data, decomp, block_size, f.blocks, f.size, f.block_start);
// rdr.addCache(cache);
// if (f.frag_idx == 0xFFFFFFFF) break :blk rdr;
// const entry: Lookup.FragEntry = try frag_table.get(io, f.frag_idx);
// if (entry.size.uncompressed) {
// rdr.addFrag(data[entry.block_start..][0..entry.size.size], f.frag_offset);
// } else {
// rdr.addFrag(try cache.get(io, entry.block_start, entry.size.size), f.frag_offset);
// }
// break :blk rdr;
// },
// .ext_file => |f| blk: {
// if (f.xattr_idx != 0xFFFFFFFF) ret.xattr_idx = f.xattr_idx;
// var rdr: DataExtractor = .init(data, decomp, block_size, f.blocks, f.size, f.block_start);
// rdr.addCache(cache);
// if (f.frag_idx == 0xFFFFFFFF) break :blk rdr;
// const entry: Lookup.FragEntry = try frag_table.get(io, f.frag_idx);
// if (entry.size.uncompressed) {
// rdr.addFrag(data[entry.block_start..][0..entry.size.size], f.frag_offset);
// } else {
// rdr.addFrag(try cache.get(io, entry.block_start, entry.size.size), f.frag_offset);
// }
// break :blk rdr;
// },
// else => unreachable,
// };
// var atomic = try Io.Dir.cwd().createFileAtomic(io, path, .{});
// defer atomic.deinit(io);
// try ext.extractAsync(alloc, io, atomic.file);
// try atomic.link(io);
var rdr: DataReader = switch (inode.data) {
.file => |f| blk: {
var rdr: DataReader = .init(alloc, data, decomp, block_size, f.blocks, f.size, f.block_start);
rdr.addCache(io, cache);
if (f.frag_idx == 0xFFFFFFFF) break :blk rdr;
const entry: Lookup.FragEntry = try frag_table.get(io, f.frag_idx);
if (entry.size.uncompressed) {
rdr.addFrag(data[entry.block_start..][0..entry.size.size], f.frag_offset);
} else {
rdr.addFrag(try cache.get(io, entry.block_start, entry.size.size), f.frag_offset);
}
break :blk rdr;
},
.ext_file => |f| blk: {
if (f.xattr_idx != 0xFFFFFFFF) ret.xattr_idx = f.xattr_idx;
var rdr: DataReader = .init(alloc, data, decomp, block_size, f.blocks, f.size, f.block_start);
rdr.addCache(io, cache);
if (f.frag_idx == 0xFFFFFFFF) break :blk rdr;
const entry: Lookup.FragEntry = try frag_table.get(io, f.frag_idx);
if (entry.size.uncompressed) {
rdr.addFrag(data[entry.block_start..][0..entry.size.size], f.frag_offset);
} else {
rdr.addFrag(try cache.get(io, entry.block_start, entry.size.size), f.frag_offset);
}
break :blk rdr;
},
else => unreachable,
};
var atomic = try Io.Dir.cwd().createFileAtomic(io, path, .{});
defer atomic.deinit(io);
var writer = atomic.file.writer(io, &[0]u8{});
_ = try rdr.interface.streamRemaining(&writer.interface);
try writer.flush();
try atomic.link(io);
return ret;
}
fn extractSymlink(alloc: std.mem.Allocator, io: Io, inode: Inode, path: []const u8, origin: bool) Error!void {
defer if (!origin) {
inode.deinit(alloc);
alloc.free(path);
};
const target = switch (inode.data) {
.symlink => |s| s.target,
.ext_symlink => |s| s.target,
else => unreachable,
};
try Io.Dir.cwd().symLink(io, target, path, .{});
}
fn extractNod(alloc: std.mem.Allocator, inode: Inode, path: []const u8, origin: bool) Error!PathReturn {
errdefer if (!origin)
alloc.free(path);
var ret: PathReturn = .{
.hdr = inode.hdr,
.path = path,
};
var dev: u32 = 0;
var mode: u32 = undefined;
const DT = std.posix.DT;
switch (inode.data) {
.char_dev => |d| {
dev = d.device;
mode = DT.CHR;
},
.block_dev => |d| {
dev = d.device;
mode = DT.BLK;
},
.ext_char_dev => |d| {
if (d.xattr_idx != 0xFFFFFFFF) ret.xattr_idx = d.xattr_idx;
dev = d.device;
mode = DT.CHR;
},
.ext_block_dev => |d| {
if (d.xattr_idx != 0xFFFFFFFF) ret.xattr_idx = d.xattr_idx;
dev = d.device;
mode = DT.BLK;
},
.fifo => mode = DT.FIFO,
.socket => mode = DT.SOCK,
.ext_fifo => |i| {
if (i.xattr_idx != 0xFFFFFFFF) ret.xattr_idx = i.xattr_idx;
mode = DT.FIFO;
},
.ext_socket => |i| {
if (i.xattr_idx != 0xFFFFFFFF) ret.xattr_idx = i.xattr_idx;
mode = DT.SOCK;
},
else => unreachable,
}
const sentinel_path = try alloc.dupeSentinel(u8, path, 0);
defer alloc.free(sentinel_path);
const res = std.os.linux.mknod(sentinel_path, mode, dev);
if (res != 0)
return error.MknodError;
return ret;
}
// Types
const ReturnUnion = union(enum) {
path: Error!PathReturn,
void: Error!void,
};
const Error = error{MknodError} || Decomp.Error || Directory.Error || DataExtractor.Error || Io.Dir.CreateDirPathError ||
Io.Dir.SymLinkError || Io.File.Atomic.LinkError || Io.Reader.StreamRemainingError;
const PathReturn = struct {
hdr: Inode.Header,
path: []const u8,
xattr_idx: ?u32 = null,
};
-265
View File
@@ -1,265 +0,0 @@
const std = @import("std");
const Io = std.Io;
const DataReader = @import("data/reader.zig");
const Decomp = @import("decomp.zig");
const Directory = @import("directory.zig");
const ExtractionOptions = @import("options.zig");
const Inode = @import("inode.zig");
const Lookup = @import("lookup.zig");
const MetadaReader = @import("meta_rdr.zig");
const Superblock = @import("archive.zig").Superblock;
const Cache = @import("util/cache.zig");
const XattrTable = @import("xattr.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 {
const path = std.mem.trim(u8, ext_loc, "/");
var cache: Cache = .init(alloc, data, decomp);
defer cache.deinit();
var frag_table: Lookup.Table(Lookup.FragEntry) = .init(alloc, data, decomp, super.frag_start, super.frag_count);
defer frag_table.deinit();
var id_table: Lookup.Table(u16) = .init(alloc, data, decomp, super.id_start, super.id_count);
defer id_table.deinit();
var xattr_table: XattrTable = .init(alloc, data, decomp, super.xattr_start);
defer xattr_table.deinit();
return switch (inode.hdr.type) {
.file, .ext_file => try extractFile(alloc, io, super, data, decomp, &cache, &frag_table, &id_table, &xattr_table, inode, path, options),
.dir, .ext_dir => try extractDir(alloc, io, super, data, decomp, &cache, &frag_table, &id_table, &xattr_table, inode, path, options),
.symlink, .ext_symlink => try extractSymlink(io, inode, path),
else => try extractNod(alloc, io, &id_table, &xattr_table, inode, path, options),
};
}
fn setMetadata(
alloc: std.mem.Allocator,
io: Io,
id_table: *Lookup.Table(u16),
xattr_table: *XattrTable,
inode: Inode,
path: []const u8,
options: ExtractionOptions,
xattr_idx: ?u32,
) !void {
if (options.ignore_permissions and (options.ignore_xattr or xattr_idx == null)) return;
var fil: Io.File = try Io.Dir.cwd().openFile(io, path, .{});
defer fil.close(io);
if (!options.ignore_permissions) {
try fil.setTimestamps(io, .{
.modify_timestamp = .init(Io.Timestamp.fromNanoseconds(@as(i96, @intCast(inode.hdr.mod_time)) * std.time.ns_per_s)),
});
try fil.setPermissions(io, @enumFromInt(inode.hdr.permissions));
try fil.setOwner(io, try id_table.get(io, inode.hdr.uid_idx), try id_table.get(io, inode.hdr.gid_idx));
}
if (!options.ignore_xattr and xattr_idx != null) {
const xattr = try xattr_table.get(alloc, io, xattr_idx.?);
defer xattr.deinit(alloc);
for (xattr.kvs) |kv| {
const res = std.os.linux.fsetxattr(fil.handle, kv.key, kv.value.ptr, kv.value.len, 0);
if (res != 0)
return error.SetXattrError;
}
}
}
fn extractDir(
alloc: std.mem.Allocator,
io: Io,
super: Superblock,
data: []u8,
decomp: Decomp.Fn,
cache: *Cache,
frag_table: *Lookup.Table(Lookup.FragEntry),
id_table: *Lookup.Table(u16),
xattr_table: *XattrTable,
inode: Inode,
path: []const u8,
options: ExtractionOptions,
) !void {
var xattr_idx: ?u32 = null;
try Io.Dir.cwd().createDirPath(io, path);
var dir: Directory = switch (inode.data) {
.dir => |d| blk: {
var meta: MetadaReader = .init(alloc, data, decomp, d.block_start + super.dir_start);
try meta.interface.discardAll(d.block_offset);
break :blk try Directory.init(alloc, &meta.interface, d.size);
},
.ext_dir => |d| blk: {
if (d.xattr_idx != 0xFFFFFFFF) xattr_idx = d.xattr_idx;
var meta: MetadaReader = .init(alloc, data, decomp, d.block_start + super.dir_start);
try meta.interface.discardAll(d.block_offset);
break :blk try Directory.init(alloc, &meta.interface, d.size);
},
else => unreachable,
};
defer dir.deinit(alloc);
for (dir.entries) |entry| {
var new_inode: Inode = try .initEntry(alloc, data, decomp, super.inode_start, super.block_size, entry);
defer new_inode.deinit(alloc);
const new_path = try std.mem.concat(alloc, u8, &.{ path, "/", entry.name });
defer alloc.free(new_path);
switch (entry.type) {
.file => try extractFile(alloc, io, super, data, decomp, cache, frag_table, id_table, xattr_table, new_inode, new_path, options),
.dir => try extractDir(alloc, io, super, data, decomp, cache, frag_table, id_table, xattr_table, new_inode, new_path, options),
.symlink => try extractSymlink(io, new_inode, new_path),
else => try extractNod(alloc, io, id_table, xattr_table, new_inode, new_path, options),
}
}
try setMetadata(alloc, io, id_table, xattr_table, inode, path, options, xattr_idx);
}
fn extractFile(
alloc: std.mem.Allocator,
io: Io,
super: Superblock,
data: []u8,
decomp: Decomp.Fn,
cache: *Cache,
frag_table: *Lookup.Table(Lookup.FragEntry),
id_table: *Lookup.Table(u16),
xattr_table: *XattrTable,
inode: Inode,
path: []const u8,
options: ExtractionOptions,
) !void {
var xattr_idx: ?u32 = null;
var rdr: DataReader = switch (inode.data) {
.file => |f| blk: {
var rdr: DataReader = .init(alloc, data, decomp, super.block_size, f.blocks, f.size, f.block_start);
rdr.addCache(io, cache);
if (f.frag_idx == 0xFFFFFFFF) break :blk rdr;
const entry: Lookup.FragEntry = try frag_table.get(io, f.frag_idx);
if (entry.size.uncompressed) {
rdr.addFrag(data[entry.block_start..][0..entry.size.size], f.frag_offset);
} else {
rdr.addFrag(try cache.get(io, entry.block_start, entry.size.size), f.frag_offset);
}
break :blk rdr;
},
.ext_file => |f| blk: {
if (f.xattr_idx != 0xFFFFFFFF) xattr_idx = f.xattr_idx;
var rdr: DataReader = .init(alloc, data, decomp, super.block_size, f.blocks, f.size, f.block_start);
rdr.addCache(io, cache);
if (f.frag_idx == 0xFFFFFFFF) break :blk rdr;
const entry: Lookup.FragEntry = try frag_table.get(io, f.frag_idx);
if (entry.size.uncompressed) {
rdr.addFrag(data[entry.block_start..][0..entry.size.size], f.frag_offset);
} else {
rdr.addFrag(try cache.get(io, entry.block_start, entry.size.size), f.frag_offset);
}
break :blk rdr;
},
else => unreachable,
};
var atomic = try Io.Dir.cwd().createFileAtomic(io, path, .{});
defer atomic.deinit(io);
var writer = atomic.file.writer(io, &[0]u8{});
_ = try rdr.interface.streamRemaining(&writer.interface);
try writer.flush();
try atomic.link(io);
try setMetadata(alloc, io, id_table, xattr_table, inode, path, options, xattr_idx);
}
fn extractSymlink(io: Io, inode: Inode, path: []const u8) !void {
return switch (inode.data) {
.symlink => |s| Io.Dir.cwd().symLink(io, s.target, path, .{}),
.ext_symlink => |s| Io.Dir.cwd().symLink(io, s.target, path, .{}),
else => unreachable,
};
}
fn extractNod(
alloc: std.mem.Allocator,
io: Io,
id_table: *Lookup.Table(u16),
xattr_table: *XattrTable,
inode: Inode,
path: []const u8,
options: ExtractionOptions,
) !void {
var dev: u32 = 0;
var mode: u32 = undefined;
var xattr_idx: ?u32 = null;
const DT = std.posix.DT;
switch (inode.data) {
.char_dev => |d| {
dev = d.device;
mode = DT.CHR;
},
.block_dev => |d| {
dev = d.device;
mode = DT.BLK;
},
.ext_char_dev => |d| {
if (d.xattr_idx != 0xFFFFFFFF) xattr_idx = d.xattr_idx;
dev = d.device;
mode = DT.CHR;
},
.ext_block_dev => |d| {
if (d.xattr_idx != 0xFFFFFFFF) xattr_idx = d.xattr_idx;
dev = d.device;
mode = DT.BLK;
},
.fifo => mode = DT.FIFO,
.socket => mode = DT.SOCK,
.ext_fifo => |i| {
if (i.xattr_idx != 0xFFFFFFFF) xattr_idx = i.xattr_idx;
mode = DT.FIFO;
},
.ext_socket => |i| {
if (i.xattr_idx != 0xFFFFFFFF) xattr_idx = i.xattr_idx;
mode = DT.SOCK;
},
else => unreachable,
}
const sentinel_path = try alloc.dupeSentinel(u8, path, 0);
defer alloc.free(sentinel_path);
const res = std.os.linux.mknod(sentinel_path, mode, dev);
if (res != 0)
return error.MknodError;
try setMetadata(alloc, io, id_table, xattr_table, inode, path, options, xattr_idx);
}
-24
View File
@@ -1,24 +0,0 @@
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);
}
+108 -94
View File
@@ -1,99 +1,73 @@
//! A squashfs file/directory. Basically a wrapper around an Inode.
const std = @import("std"); const std = @import("std");
const Io = std.Io; const Io = std.Io;
const Directory = @import("directory.zig");
const Inode = @import("inode.zig"); const Inode = @import("inode.zig");
const MetadataReader = @import("meta_rdr.zig"); const Directory = @import("dir.zig");
const Decomp = @import("decomp.zig"); const MetadataReader = @import("utils/meta.zig");
const Superblock = @import("archive.zig").Superblock; const Options = @import("options.zig");
const ExtractionOptions = @import("options.zig"); const DataReader = @import("utils/data_reader.zig");
const Extract = @import("extract.zig"); const Decompress = @import("utils/decompress.zig");
const Super = @import("archive.zig").Super;
const File = @This(); const File = @This();
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
super: Superblock,
data: []u8, data: []u8,
decomp: Decomp.Fn, super: Super,
name: []const u8, name: []const u8,
inode: Inode, inode: Inode,
/// The given allocator should have been used to create the name and inode. pub fn copy(self: File, alloc: std.mem.Allocator) !File {
pub fn init(alloc: std.mem.Allocator, super: Superblock, data: []u8, decomp: Decomp.Fn, name: []const u8, inode: Inode) File { const new_name = try alloc.dupe(u8, self.name);
errdefer alloc.free(new_name);
return .{ return .{
.alloc = alloc, .alloc = alloc,
.super = super, .data = self.data,
.data = data, .super = self.super,
.decomp = decomp,
.name = name, .name = new_name,
.inode = inode, .inode = try self.inode.copy(alloc),
}; };
} }
/// 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(u8, 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(u8, name);
return init(alloc, super, data, decomp, new_name, inode);
}
pub fn copy(self: File, alloc: std.mem.Allocator) !File {
var inode = self.inode;
switch (inode.data) {
.file => |*f| f.blocks = try alloc.dupe(Inode.DataBlock, self.inode.data.file.blocks),
.ext_file => |*f| f.blocks = try alloc.dupe(Inode.DataBlock, self.inode.data.file.blocks),
.symlink => |*s| s.target = try alloc.dupe(u8, self.inode.data.symlink.target),
.ext_symlink => |*s| s.target = try alloc.dupe(u8, self.inode.data.symlink.target),
else => {},
}
errdefer inode.deinit(alloc);
const new_name = try alloc.dupe(u8, self.name);
return init(alloc, self.super, self.data, self.decomp, new_name, inode);
}
pub fn deinit(self: File) void { pub fn deinit(self: File) void {
self.alloc.free(self.name);
self.inode.deinit(self.alloc); self.inode.deinit(self.alloc);
self.alloc.free(self.name);
} }
/// If the given File is a directory, open a sub-file at the given filepath. /// Reader interface to read a regular file's contents.
/// If the given filepath resolves to the calling File ("" or "."), a copy of the File is returned. pub fn reader(self: File, alloc: std.mem.Allocator) !DataReader {
pub fn open(self: File, alloc: std.mem.Allocator, filepath: []const u8) !File { return switch (self.inode.header.type) {
var size: u64 = 0; .file => |f| .init(
var meta: MetadataReader = switch (self.inode.data) { alloc,
.dir => |d| blk: { self.data,
size = d.size; self.super.decomp_fn,
self.super.block_size,
var meta: MetadataReader = .init(alloc, self.data, self.decomp, self.super.dir_start + d.block_start); f.start,
try meta.interface.discardAll(d.block_offset); f.size,
break :blk meta; f.blocks,
}, ),
.ext_dir => |d| blk: { .ext_file => |f| .init(
size = d.size; alloc,
self.data,
var meta: MetadataReader = .init(alloc, self.data, self.decomp, self.super.dir_start + d.block_start); self.super.decomp_fn,
try meta.interface.discardAll(d.block_offset); self.super.block_size,
break :blk meta; f.start,
}, f.size,
else => return error.NotDirectory, f.blocks,
),
else => error.NotRegularFile,
}; };
}
pub fn open(self: File, alloc: std.mem.Allocator, filepath: []const u8) !File {
switch (self.inode.header.type) {
.dir, .ext_dir => {},
else => return error.NotDirectory,
}
const path = std.mem.trim(u8, filepath, "/"); const path = std.mem.trim(u8, filepath, "/");
@@ -101,44 +75,84 @@ pub fn open(self: File, alloc: std.mem.Allocator, filepath: []const u8) !File {
return self.copy(alloc); return self.copy(alloc);
const first_element: []const u8 = std.mem.sliceTo(path, '/'); const first_element: []const u8 = std.mem.sliceTo(path, '/');
const final = first_element.len == path.len;
const file: File = blk: { var dir_rdr: MetadataReader = .init(alloc, self.data[self.inode.data.dir.start + self.super.dir_table_start ..], self.super.decomp_fn);
var directory: Directory = try .init(alloc, &meta.interface, size); try dir_rdr.interface.discardAll(self.inode.data.dir.offset);
defer directory.deinit(alloc);
var entries = directory.entries; const dir_size = self.inode.data.dir.size;
var idx = entries.len / 2;
while (entries.len > 0) { const entry: Directory.Entry = blk: {
switch (std.mem.order(u8, first_element, entries[idx].name)) { var dir: Directory = try .read(alloc, &dir_rdr.interface, dir_size);
defer dir.deinit(alloc);
var entries = dir.entries;
var idx: usize = undefined;
while (entries.len > 1) {
idx = entries.len / 2;
const val = entries[idx];
switch (std.mem.order(u8, first_element, val.name)) {
.eq => break, .eq => break,
.lt => entries = entries[0..idx], .lt => entries = entries[0..idx],
.gt => entries = entries[idx..], .gt => entries = entries[idx..],
} }
idx = entries.len / 2;
} else { } else {
if (!std.mem.eql(u8, first_element, entries[0].name))
return error.NotFound; return error.NotFound;
idx = 0;
} }
break :blk try initEntry(alloc, self.super, self.data, self.decomp, entries[idx]); if (!final) break :blk entries[idx];
const inode: Inode = try .readLocation(
alloc,
self.data,
entries[idx].start,
entries[idx].offset,
self.super,
);
errdefer inode.deinit(alloc);
return .{
.alloc = alloc,
.data = self.data,
.super = self.super,
.inode = inode,
.name = try alloc.dupe(u8, entries[idx].name),
};
}; };
if (first_element.len == path.len) if (entry.type != .dir)
return file; return error.NotFound;
defer file.deinit();
return file.open(alloc, path[first_element.len..]); const transient_file: File = .{
} .alloc = alloc,
pub fn extract(self: File, alloc: std.mem.Allocator, io: Io, location: []const u8, options: ExtractionOptions) !void { .data = self.data,
return Extract.extract( .super = self.super,
.inode = try .readLocation(
alloc, alloc,
io,
self.super,
self.data, self.data,
self.decomp, entry.start,
self.inode, entry.offset,
location, self.super,
options, ),
); .name = "",
}; // We don't care about deiniting this file since dir inodes don't need to be deinited and we don't have an allocd name.
return transient_file.open(alloc, path[first_element.len + 1 ..]);
}
/// Extract the given File to the location.
pub fn extract(self: File, alloc: std.mem.Allocator, io: Io, ext_loc: []const u8, options: Options) !void {
// TODO: do some basic processing to check if ext_loc is a folder & if self is regular file and adjust accordingly.
if (options.single_threaded)
return Decompress.single(alloc, io, self.map.memory, self.super, self.inode, ext_loc, options);
return Decompress.multi(alloc, io, self.map.memory, self.super, self.inode, ext_loc, options);
} }
+72
View File
@@ -0,0 +1,72 @@
const std = @import("std");
const Io = std.Io;
const Lookup = @import("lookup.zig");
const Decomp = @import("decomp.zig");
const BlockSize = @import("inode.zig").BlockSize;
const FragCache = @This();
block_size: u32,
entries: Lookup.Table(Entry),
cache: std.AutoHashMapUnmanaged(u32, CacheData) = .empty,
mut: Io.Mutex = .init,
pub fn init(data: []u8, decomp: Decomp.Fn, block_size: u32, start: u64, count: u32) !FragCache {
return .{
.block_size = block_size,
.entries = try .init(data, decomp, start, count),
};
}
pub fn deinit(self: *FragCache, alloc: std.mem.Allocator) void {
self.entries.deinit(alloc);
var iter = self.cache.valueIterator();
while (iter.next()) |cache| {
if (cache.allocd)
alloc.free(cache.data);
}
self.cache.deinit(alloc);
}
pub fn get(self: *FragCache, alloc: std.mem.Allocator, io: Io, idx: u32) ![]u8 {
const cache = self.cache.get(idx);
if (cache != null) return cache.?.data;
const entry = try self.entries.get(alloc, io, idx);
const data = self.entries.data[entry.start..][0..entry.size.size];
if (entry.size.uncompressed)
return data;
try self.mut.lock(io);
defer self.mut.unlock(io);
const cache_data = try alloc.alloc(u8, self.block_size);
errdefer alloc.free(cache_data);
_ = try self.entries.decomp(alloc, data, cache_data);
try self.cache.put(alloc, idx, .{
.data = cache_data,
.allocd = true,
});
return cache_data;
}
// Types
pub const Entry = extern struct {
start: u64,
size: BlockSize,
_: u32,
};
const CacheData = struct {
data: []u8,
allocd: bool,
};
+197 -191
View File
@@ -1,75 +1,73 @@
const std = @import("std"); const std = @import("std");
const Io = std.Io; const Io = std.Io;
const Reader = Io.Reader;
const Decomp = @import("decomp.zig"); const util = @import("utils/util.zig");
const Directory = @import("directory.zig");
const MetadataReader = @import("meta_rdr.zig"); const MetadataReader = @import("utils/meta.zig");
const Superblock = @import("archive.zig").Superblock; const Super = @import("archive.zig").Super;
const Extract = @import("extract.zig");
const ExtractionOptions = @import("options.zig");
const Inode = @This(); const Inode = @This();
hdr: Header, header: Header,
data: Data, data: Data,
pub fn init(alloc: std.mem.Allocator, block_size: u32, rdr: *Reader) !Inode { pub fn readLocation(alloc: std.mem.Allocator, data: []u8, start: u32, offset: u16, super: Super) !Inode {
var hdr: Header = undefined; var meta: MetadataReader = .init(alloc, data[super.inode_table_start + start ..], super.decomp_fn);
try rdr.readSliceEndian(Header, @ptrCast(&hdr), .little); try meta.interface.discardAll(offset);
const data: Data = switch (hdr.type) { return read(alloc, &meta.interface, super.block_size);
.dir => .{ .dir = try .init(rdr) }, }
.ext_dir => .{ .ext_dir = try .init(rdr) }, pub fn read(alloc: std.mem.Allocator, rdr: *Io.Reader, block_size: u32) !Inode {
.file => .{ .file = try .init(alloc, rdr, block_size) }, const hdr = try util.readValueRdr(Header, rdr);
.ext_file => .{ .ext_file = try .init(alloc, rdr, block_size) },
.symlink => .{ .symlink = try .init(alloc, rdr) }, return .{
.ext_symlink => .{ .ext_symlink = try .init(alloc, rdr) }, .header = hdr,
.block_dev => .{ .block_dev = try .init(rdr) }, .data = switch (hdr.type) {
.ext_block_dev => .{ .ext_block_dev = try .init(rdr) }, .dir => .{ .dir = try .readBasic(rdr) },
.char_dev => .{ .char_dev = try .init(rdr) }, .ext_dir => .{ .dir = try .readExt(rdr) },
.ext_char_dev => .{ .ext_char_dev = try .init(rdr) }, .file => .{ .file = try .readBasic(rdr, alloc, block_size) },
.fifo => .{ .fifo = try .init(rdr) }, .ext_file => .{ .file = try .readExt(rdr, alloc, block_size) },
.ext_fifo => .{ .ext_fifo = try .init(rdr) }, .symlink, .ext_symlink => .{ .symlink = try .read(rdr, alloc) },
.socket => .{ .socket = try .init(rdr) }, .block_dev, .char_dev => .{ .dev = try .readBasic(rdr) },
.ext_socket => .{ .ext_socket = try .init(rdr) }, .ext_block_dev, .ext_char_dev => .{ .dev = try .readExt(rdr) },
.fifo, .socket => .{ .fifo = try .readBasic(rdr) },
.ext_fifo, .ext_socket => .{ .fifo = try .readExt(rdr) },
},
}; };
return .{ .hdr = hdr, .data = data };
} }
pub fn initRef(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, inode_start: u64, block_size: u32, ref: Ref) !Inode { pub fn copy(self: Inode, alloc: std.mem.Allocator) !Inode {
var meta: MetadataReader = .init(alloc, data, decomp, inode_start + ref.block_start); var new_inode = self;
try meta.interface.discardAll(ref.block_offset);
return .init(alloc, block_size, &meta.interface); switch (new_inode.data) {
} .file => |*f| f.blocks = try alloc.dupe(BlockSize, f.blocks),
pub fn initEntry(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, inode_start: u64, block_size: u32, entry: Directory.Entry) !Inode { .symlink => |*f| f.target = try alloc.dupe(u8, f.target),
var meta: MetadataReader = .init(alloc, data, decomp, inode_start + entry.block_start); else => {},
try meta.interface.discardAll(entry.block_offset); }
return .init(alloc, block_size, &meta.interface); return new_inode;
} }
pub fn deinit(self: Inode, alloc: std.mem.Allocator) void { pub fn deinit(self: Inode, alloc: std.mem.Allocator) void {
switch (self.data) { switch (self.data) {
.file => |f| alloc.free(f.blocks), .file => |f| alloc.free(f.blocks),
.ext_file => |f| alloc.free(f.blocks),
.symlink => |s| alloc.free(s.target), .symlink => |s| alloc.free(s.target),
.ext_symlink => |s| alloc.free(s.target),
else => {}, else => {},
} }
} }
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 // Types
pub const Ref = packed struct(u64) { pub const Reference = packed struct(u64) {
block_offset: u16, offset: u16,
block_start: u32, start: u32,
_: u16, _: u16,
}; };
pub const BlockSize = packed struct(u32) {
size: u23,
uncompressed: bool,
_: u8,
};
pub const Type = enum(u16) { pub const Type = enum(u16) {
dir = 1, dir = 1,
file, file,
@@ -89,208 +87,216 @@ pub const Type = enum(u16) {
pub const Header = extern struct { pub const Header = extern struct {
type: Type, type: Type,
permissions: u16, permission: u16,
uid_idx: u16, uid_idx: u16,
gid_idx: u16, gid_idx: u16,
mod_time: u32, mod_time: u32,
num: u32, inode_num: u32,
}; };
pub const Data = union(Type) { pub const Data = union(enum) {
dir: Dir, dir: Dir,
file: File, file: File,
symlink: Symlink, symlink: Symlink,
block_dev: Dev, dev: Dev,
char_dev: Dev, fifo: Fifo,
fifo: IPC,
socket: IPC,
ext_dir: ExtDir,
ext_file: ExtFile,
ext_symlink: ExtSymlink,
ext_block_dev: ExtDev,
ext_char_dev: ExtDev,
ext_fifo: ExtIPC,
ext_socket: ExtIPC,
}; };
pub const DataBlock = packed struct(u32) { // Inode data types
size: u24,
uncompressed: bool,
_: u7,
};
pub const Dir = extern struct { pub const Dir = struct {
block_start: u32, //RegularDir structure:
hard_links: u32, // start: u32,
size: u16, // hard_links: u32,
block_offset: u16, // size: u16,
parent_num: u32, // offset: u16,
// parent_inode_num: u32,
// ExtDir structure
// hard_links: u32,
// size: u32,
// start: u32,
// parent_inode_num: u32,
// idx_count: u16,
// offset: u16,
// xattr_idx: u32,
// dir_indexes: []DirIndex,
fn init(rdr: *Reader) !Dir {
var new: Dir = undefined;
try rdr.readSliceEndian(Dir, @ptrCast(&new), .little);
return new;
}
};
pub const ExtDir = extern struct {
hard_links: u32, hard_links: u32,
size: u32, size: u32,
block_start: u32, start: u32,
parent_num: u32, offset: u16,
idx_count: u16, xattr_idx: ?u32,
block_offset: u16,
xattr_idx: u32,
fn init(rdr: *Reader) !ExtDir { pub fn readBasic(rdr: *Io.Reader) !Dir {
var new: ExtDir = undefined; var raw: [12]u8 = undefined;
try rdr.readSliceEndian(ExtDir, @ptrCast(&new), .little); try rdr.readSliceAll(&raw);
return new;
return .{
.start = util.readValue(u32, raw[0..4]),
.hard_links = util.readValue(u32, raw[4..8]),
.size = util.readValue(u16, raw[8..10]),
.offset = util.readValue(u16, raw[10..12]),
.xattr_idx = null,
};
}
pub fn readExt(rdr: *Io.Reader) !Dir {
var raw: [24]u8 = undefined;
try rdr.readSliceAll(&raw);
return .{
.hard_links = util.readValue(u32, raw[0..4]),
.size = util.readValue(u32, raw[4..8]),
.start = util.readValue(u32, raw[8..12]),
.offset = util.readValue(u16, raw[18..20]),
.xattr_idx = util.readValue(u32, raw[20..24]),
};
} }
}; };
pub const File = struct { pub const File = struct {
block_start: u32, //RegularFile structure:
// start: u32,
// frag_idx: u32,
// frag_offset: u32,
// size: u32,
// block_sizes: []BlockSize,
//ExtFile structure:
// start: u64,
// size: u64,
// sparse: u64,
// hard_links: u32,
// frag_idx: u32,
// frag_offset: u32,
// xattr_idx: u32,
// block_sizes: []BlockSize,
start: u64,
frag_idx: u32, frag_idx: u32,
frag_offset: u32, frag_offset: u32,
size: u32,
blocks: []DataBlock,
fn init(alloc: std.mem.Allocator, rdr: *Reader, block_size: u32) !File {
var data: [16]u8 = undefined;
try rdr.readSliceAll(&data);
const frag_idx = std.mem.readInt(u32, data[4..8], .little);
const size = std.mem.readInt(u32, data[12..], .little);
var blocks_num = size / block_size;
if (size % block_size != 0 and frag_idx == 0xFFFFFFFF)
blocks_num += 1;
const blocks = try alloc.alloc(DataBlock, blocks_num);
errdefer alloc.free(blocks);
try rdr.readSliceEndian(DataBlock, blocks, .little);
return .{
.block_start = std.mem.readInt(u32, data[0..4], .little),
.frag_idx = frag_idx,
.frag_offset = std.mem.readInt(u32, data[8..12], .little),
.size = size,
.blocks = blocks,
};
}
};
pub const ExtFile = struct {
block_start: u64,
size: u64, size: u64,
sparse: u64, blocks: []BlockSize,
hard_links: u32, xattr_idx: ?u32,
frag_idx: u32,
frag_offset: u32,
xattr_idx: u32,
blocks: []DataBlock,
fn init(alloc: std.mem.Allocator, rdr: *Reader, block_size: u32) !ExtFile { pub fn readBasic(rdr: *Io.Reader, alloc: std.mem.Allocator, block_size: u32) !File {
var data: [40]u8 = undefined; var raw: [16]u8 = undefined;
try rdr.readSliceAll(&data); try rdr.readSliceAll(&raw);
const frag_idx = std.mem.readInt(u32, data[28..32], .little); const frag_idx = util.readValue(u32, raw[4..8]);
const size = std.mem.readInt(u64, data[8..16], .little); const size = util.readValue(u32, raw[12..16]);
var blocks_num = size / block_size; const block_num = if (frag_idx == 0xFFFFFFFF)
if (size % block_size != 0 and frag_idx == 0xFFFFFFFF) try std.math.divCeil(usize, size, block_size)
blocks_num += 1; else
size / block_size;
const blocks = try alloc.alloc(DataBlock, blocks_num); const sizes = try alloc.alloc(BlockSize, block_num);
errdefer alloc.free(blocks); try rdr.readSliceEndian(BlockSize, sizes, .little);
try rdr.readSliceEndian(DataBlock, blocks, .little);
return .{ return .{
.block_start = std.mem.readInt(u64, data[0..8], .little), .start = util.readValue(u32, raw[0..4]),
.size = size,
.sparse = std.mem.readInt(u64, data[16..24], .little),
.hard_links = std.mem.readInt(u32, data[24..28], .little),
.frag_idx = frag_idx, .frag_idx = frag_idx,
.frag_offset = std.mem.readInt(u32, data[32..36], .little), .frag_offset = util.readValue(u32, raw[8..12]),
.xattr_idx = std.mem.readInt(u32, data[36..], .little), .size = size,
.blocks = blocks, .blocks = sizes,
.xattr_idx = null,
};
}
pub fn readExt(rdr: *Io.Reader, alloc: std.mem.Allocator, block_size: u32) !File {
var raw: [40]u8 = undefined;
try rdr.readSliceAll(&raw);
const frag_idx = util.readValue(u32, raw[28..32]);
const size = util.readValue(u64, raw[8..16]);
const block_num = if (frag_idx == 0xFFFFFFFF)
try std.math.divCeil(usize, size, block_size)
else
size / block_size;
const sizes = try alloc.alloc(BlockSize, block_num);
try rdr.readSliceEndian(BlockSize, sizes, .little);
return .{
.start = util.readValue(u64, raw[0..8]),
.size = size,
.frag_idx = frag_idx,
.frag_offset = util.readValue(u32, raw[32..36]),
.blocks = sizes,
.xattr_idx = util.readValue(u32, raw[36..40]),
}; };
} }
}; };
pub const Symlink = struct { pub const Symlink = struct {
hard_links: u32, hard_links: u32,
target: []const u8, target: []const u8,
// xattr_idx is ignored on extended symlinks as you can't apply them to symlinks anyway.
fn init(alloc: std.mem.Allocator, rdr: *Reader) !Symlink { pub fn read(rdr: *Io.Reader, alloc: std.mem.Allocator) !Symlink {
var data: [8]u8 = undefined; var raw: [8]u8 = undefined;
try rdr.readSliceAll(&data); try rdr.readSliceAll(&raw);
const target_size = std.mem.readInt(u32, data[4..], .little); const target = try alloc.alloc(u8, std.mem.readInt(u32, raw[4..], .little));
const target = try alloc.alloc(u8, target_size);
errdefer alloc.free(target); errdefer alloc.free(target);
try rdr.readSliceEndian(u8, target, .little); try rdr.readSliceEndian(u8, target, .little);
return .{ return .{
.hard_links = std.mem.readInt(u32, data[0..4], .little), .hard_links = std.mem.readInt(u32, raw[0..4], .little),
.target = target, .target = target,
}; };
} }
}; };
pub const ExtSymlink = struct {
pub const Dev = struct {
hard_links: u32, hard_links: u32,
target: []const u8, device: u32,
xattr_idx: u32, // xattr_idx omitted on basic device inodes.
xattr_idx: ?u32,
fn init(alloc: std.mem.Allocator, rdr: *Reader) !ExtSymlink { pub fn readBasic(rdr: *Io.Reader) !Dev {
const sym: Symlink = try .init(alloc, rdr); var raw: [8]u8 = undefined;
errdefer alloc.free(sym.target); try rdr.readSliceAll(&raw);
var xattr_idx: u32 = undefined;
try rdr.readSliceEndian(u32, @ptrCast(&xattr_idx), .little);
return .{ return .{
.hard_links = sym.hard_links, .hard_links = util.readValue(u32, raw[0..4]),
.target = sym.target, .device = util.readValue(u32, raw[4..8]),
.xattr_idx = xattr_idx, .xattr_idx = null,
};
}
pub fn readExt(rdr: *Io.Reader) !Dev {
var raw: [12]u8 = undefined;
try rdr.readSliceAll(&raw);
return .{
.hard_links = util.readValue(u32, raw[0..4]),
.device = util.readValue(u32, raw[4..8]),
.xattr_idx = util.readValue(u32, raw[8..12]),
}; };
} }
}; };
pub const Dev = extern struct {
hard_links: u32,
device: u32,
fn init(rdr: *Reader) !Dev { pub const Fifo = struct {
var new: Dev = undefined;
try rdr.readSliceEndian(Dev, @ptrCast(&new), .little);
return new;
}
};
pub const ExtDev = extern struct {
hard_links: u32, hard_links: u32,
device: u32, // Xattr_idx omitted on basic fifo inodes.
xattr_idx: u32, xattr_idx: ?u32,
fn init(rdr: *Reader) !ExtDev { pub fn readBasic(rdr: *Io.Reader) !Fifo {
var new: ExtDev = undefined; var raw: [4]u8 = undefined;
try rdr.readSliceEndian(ExtDev, @ptrCast(&new), .little); try rdr.readSliceAll(&raw);
return new;
}
};
pub const IPC = extern struct {
hard_links: u32,
fn init(rdr: *Reader) !IPC { return .{
var new: IPC = undefined; .hard_links = util.readValue(u32, raw[0..4]),
try rdr.readSliceEndian(IPC, @ptrCast(&new), .little); .xattr_idx = null,
return new; };
} }
}; pub fn readExt(rdr: *Io.Reader) !Fifo {
pub const ExtIPC = extern struct { var raw: [8]u8 = undefined;
hard_links: u32, try rdr.readSliceAll(&raw);
xattr_idx: u32,
fn init(rdr: *Reader) !ExtIPC { return .{
var new: ExtIPC = undefined; .hard_links = util.readValue(u32, raw[0..4]),
try rdr.readSliceEndian(ExtIPC, @ptrCast(&new), .little); .xattr_idx = util.readValue(u32, raw[4..8]),
return new; };
} }
}; };
+145 -49
View File
@@ -1,83 +1,179 @@
const std = @import("std"); const std = @import("std");
const Io = std.Io; const Io = std.Io;
const DataBlock = @import("inode.zig").DataBlock;
const Decomp = @import("decomp.zig"); const Decomp = @import("decomp.zig");
const MetadataReader = @import("meta_rdr.zig"); const Reference = @import("inode.zig").Reference;
const ProtectedMap = @import("util/protected_map.zig").ProtectedMap; const MetadataReader = @import("utils/meta.zig");
const util = @import("utils/util.zig");
pub fn Table(comptime T: anytype) type { pub fn Value(comptime T: type, alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, table_start: u64, idx: u32) !T {
const ITEMS_PER_BLOCK = 8192 / @sizeOf(T);
const block_idx = idx / ITEMS_PER_BLOCK;
const value_idx = idx % ITEMS_PER_BLOCK;
const start = util.readValue(u64, data[table_start + (block_idx * 8) ..][0..8]);
var meta: MetadataReader = .init(alloc, data[start..], decomp);
try meta.interface.discardAll(value_idx * @sizeOf(T));
return util.readValueRdr(T, &meta.interface);
}
pub fn Table(comptime T: type) type {
return struct { return struct {
const VALUES_PER_BLOCK = 8192 / @sizeOf(T);
const Self = @This(); const Self = @This();
const ITEMS_PER_BLOCK = 8192 / @sizeOf(T);
data: []u8, data: []u8,
decomp: Decomp.Fn, decomp: Decomp.Fn,
table_start: u64, start: u64,
table_num: u32, count: u32,
block_count: u32,
table: ProtectedMap(u32, []T, getBlock), table: std.hash_map.AutoHashMapUnmanaged(u32, []T) = .empty,
mut: Io.Mutex = .init,
pub fn init(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, table_start: u64, table_num: u32) Self { pub fn init(data: []u8, decomp: Decomp.Fn, start: u64, count: u32) !Self {
return .{ return .{
.data = data, .data = data,
.decomp = decomp, .decomp = decomp,
.table_start = table_start, .start = start,
.table_num = table_num, .count = count,
.block_count = try std.math.divCeil(u32, count, ITEMS_PER_BLOCK),
.table = .init(alloc),
}; };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self, alloc: std.mem.Allocator) void {
var iter = self.table.map.valueIterator(); var iter = self.table.valueIterator();
while (iter.next()) |block|
alloc.free(block.*);
while (iter.next()) |val| { self.table.deinit(alloc);
if (val.err != null) continue;
self.table.alloc.free(val.value);
}
self.table.deinit();
} }
pub fn get(self: *Self, io: Io, idx: u32) !T { pub fn get(self: *Self, alloc: std.mem.Allocator, io: Io, idx: u32) !T {
const block = idx / VALUES_PER_BLOCK; if (idx >= self.count) return error.InvalidIndex;
const block_idx = idx % VALUES_PER_BLOCK;
const values = try self.table.getOrPut(io, block, .{ const block_idx = idx / ITEMS_PER_BLOCK;
self.table.alloc, const value_idx = idx % ITEMS_PER_BLOCK;
self.data,
self.decomp,
self.table_start,
self.table_num,
block,
});
return values.*[block_idx]; const init_get = self.table.get(block_idx);
} if (init_get != null) return init_get.?[value_idx];
fn getBlock(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, table_start: u64, table_num: u32, block_idx: u32) ![]T { try self.mut.lock(io);
const offset: u64 = std.mem.readInt(u64, data[table_start + (block_idx * 8) ..][0..8], .little); defer self.mut.unlock(io);
const block = try alloc.alloc(T, if (block_idx == (table_num - 1) / VALUES_PER_BLOCK) const start = util.readValue(u64, self.data[self.start + (block_idx * 8) ..][0..8]);
table_num % VALUES_PER_BLOCK
else
VALUES_PER_BLOCK);
var meta: MetadataReader = .init(alloc, data, decomp, offset); const len = if (block_idx == self.block_count - 1) self.count % ITEMS_PER_BLOCK else ITEMS_PER_BLOCK;
try meta.interface.readSliceEndian(T, block, .little);
return block; const new_block = try alloc.alloc(T, len);
errdefer alloc.free(new_block);
var meta: MetadataReader = .init(alloc, self.data[start..], self.decomp);
try meta.interface.readSliceEndian(T, new_block, .little);
try self.table.put(alloc, block_idx, new_block);
return new_block[value_idx];
} }
}; };
} }
// Types pub const Xattr = struct {
kv_start: u64,
lookup: Table(XattrLookup),
pub const FragEntry = extern struct { pub fn init(data: []u8, decomp: Decomp.Fn, start: u64) !Xattr {
block_start: u64, const kv_start = util.readValue(u64, data[start..][0..8]);
size: DataBlock, const count = util.readValue(u32, data[start..][8..12]);
_: u32,
return .{
.kv_start = kv_start,
.lookup = try .init(data, decomp, start + 16, count),
};
}
pub fn deinit(self: *Xattr, alloc: std.mem.Allocator) void {
self.lookup.deinit(alloc);
}
pub fn get(self: *Xattr, alloc: std.mem.Allocator, io: Io, idx: u32) ![]KV {
const lookup = try self.lookup.get(alloc, io, idx);
var meta: MetadataReader = .init(alloc, self.lookup.data[self.kv_start + lookup.ref.start ..], self.lookup.decomp);
try meta.interface.discardAll(lookup.ref.offset);
const kvs = try alloc.alloc(KV, lookup.count);
errdefer alloc.free(kvs);
for (kvs) |*kv| {
const entry = try util.readValueRdr(KeyEntry, &meta.interface);
const prefix = switch (entry.type.type) {
.user => "user.",
.trusted => "trusted.",
.security => "security.",
};
kv.name = try alloc.allocSentinel(u8, entry.name_size + prefix.len, 0);
errdefer alloc.free(kv.name);
try meta.interface.readSliceEndian(u8, kv.name[prefix.len..], .little);
@memcpy(kv.name[0..prefix.len], prefix);
var value_meta = if (entry.type.out_of_line) blk: {
const ool_ref = try util.readValueRdr(Reference, &meta.interface);
var ool_meta: MetadataReader = .init(alloc, self.lookup.data[self.kv_start + ool_ref.start ..], self.lookup.decomp);
try ool_meta.interface.discardAll(ool_ref.offset);
break :blk ool_meta;
} else meta;
const value_len = try util.readValueRdr(u32, &value_meta.interface);
kv.value = try alloc.alloc(u8, value_len);
errdefer alloc.free(kv.value);
try value_meta.interface.readSliceEndian(u8, kv.value, .little);
}
return kvs;
}
//types
pub const KV = struct {
name: [:0]u8,
value: []u8,
pub fn deinit(self: KV, alloc: std.mem.Allocator) void {
alloc.free(self.name);
alloc.free(self.value);
}
};
const XattrLookup = extern struct {
ref: Reference,
count: u32,
size: u32,
};
const KeyEntry = extern struct {
type: packed struct(u16) {
type: enum(u8) {
user,
trusted,
security,
},
out_of_line: bool,
_: u7,
},
name_size: u16,
};
}; };
-114
View File
@@ -1,114 +0,0 @@
const std = @import("std");
const Io = std.Io;
const Reader = Io.Reader;
const Writer = Io.Writer;
const Limit = Io.Limit;
const Decomp = @import("decomp.zig");
const MetadataReader = @This();
alloc: std.mem.Allocator,
data: []u8,
decomp: Decomp.Fn,
cur_offset: u64,
block: [8192]u8 = undefined,
interface: Reader = .{
.buffer = &[0]u8{},
.end = 0,
.seek = 0,
.vtable = &.{
.stream = stream,
.discard = discard,
.readVec = readVec,
},
},
pub fn init(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, offset: u64) MetadataReader {
return .{
.alloc = alloc,
.data = data,
.decomp = decomp,
.cur_offset = offset,
};
}
fn advance(self: *MetadataReader) Reader.Error!void {
self.interface.seek = 0;
errdefer self.interface.end = 0;
const hdr: Header = @bitCast(std.mem.readInt(u16, self.data[self.cur_offset..][0..2], .little));
if (hdr.size == 0 or hdr.size > 8192) return Reader.Error.ReadFailed;
defer self.cur_offset += hdr.size + 2;
const block = self.data[self.cur_offset + 2 ..][0..hdr.size];
if (hdr.uncompressed) {
self.interface.end = hdr.size;
self.interface.buffer = block;
return;
}
const decomp_size = self.decomp(self.alloc, block, &self.block) catch return Reader.Error.ReadFailed;
self.interface.buffer = self.block[0..decomp_size];
self.interface.end = decomp_size;
}
fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize {
if (r.seek >= r.end) {
const self: *MetadataReader = @fieldParentPtr("interface", r);
try self.advance();
}
if (limit == .nothing) return 0;
const to_write = @min(@intFromEnum(limit), r.end - r.seek);
const wrote = try w.write(r.buffer[r.seek..][0..to_write]);
r.seek += wrote;
return wrote;
}
fn discard(r: *Reader, limit: Limit) Reader.Error!usize {
if (r.seek >= r.end) {
const self: *MetadataReader = @fieldParentPtr("interface", r);
try self.advance();
}
if (limit == .nothing) return 0;
const to_discard = @min(@intFromEnum(limit), r.end - r.seek);
r.seek += to_discard;
return to_discard;
}
fn readVec(r: *Reader, vec: [][]u8) Reader.Error!usize {
if (r.seek >= r.end) {
const self: *MetadataReader = @fieldParentPtr("interface", r);
try self.advance();
}
var wrote: usize = 0;
for (vec) |v| {
const to_cpy = @min(v.len, r.end - r.seek);
@memcpy(v[0..to_cpy], r.buffer[r.seek..][0..to_cpy]);
wrote += to_cpy;
r.seek += to_cpy;
if (r.seek >= r.end) break;
}
return wrote;
}
// Types
const Header = packed struct(u16) {
size: u15,
uncompressed: bool,
};
+10 -17
View File
@@ -1,30 +1,23 @@
//! Options for file/directory extraction.
const std = @import("std"); const std = @import("std");
const Writer = std.Io.Writer; const Writer = std.Io.Writer;
const ExtractionOptions = @This(); const Options = @This();
/// Whether to use single threaded extraction. For finer control, create an std.Io instance.
single_threaded: bool = false,
/// Don't set the file's owner & permissions after extraction
ignore_permissions: bool = false,
/// Don't set xattr values. Currently xattrs are never set anyway.
ignore_xattr: bool = false,
/// Replace symlinks with their target.
dereference_symlinks: bool = false,
/// Verbose logging. If true, verbose_writer must be set
verbose: bool = false, verbose: bool = false,
/// Where to print verbose log.
verbose_writer: ?*Writer = null, verbose_writer: ?*Writer = null,
pub const default: ExtractionOptions = .{}; ignore_permissions: bool = false,
pub const single_threaded_default: ExtractionOptions = .{ .single_threaded = true }; ignore_xattr: bool = false,
pub fn VerboseDefault(wrt: *Writer) !ExtractionOptions { dereference_symlink: bool = false,
single_threaded: bool = false,
pub const default: Options = .{};
pub const single_threaded_default: Options = .{ .single_threaded = true };
pub fn verboseDefault(wrt: *Writer) Options {
return .{ return .{
.verbose = true, .verbose = true,
.verbose_writer = wrt, .verbose_writer = wrt,
.threads = try std.Thread.getCpuCount(),
}; };
} }
+2 -3
View File
@@ -1,7 +1,6 @@
pub const Archive = @import("archive.zig"); pub const Archive = @import("archive.zig");
pub const File = @import("file.zig"); pub const Options = @import("options.zig");
pub const ExtractionOptions = @import("options.zig");
test { test {
@import("std").testing.refAllDecls(@import("test.zig")); @import("std").testing.refAllDecls(@import("tests.zig"));
} }
-153
View File
@@ -1,153 +0,0 @@
const std = @import("std");
const Io = std.Io;
const testing = std.testing;
const Archive = @import("archive.zig");
const TestArchive = "testing/LinuxPATest.sfs";
test "Basics" {
const io = testing.io;
const alloc = testing.allocator;
var archive_file = try Io.Dir.cwd().openFile(io, TestArchive, .{});
defer archive_file.close(io);
var archive: Archive = try .init(io, archive_file, 0);
defer archive.deinit(io);
try testing.expectEqualDeep(archive.super, LinuxPATestCorrectSuperblock);
var root = try archive.root(alloc);
defer root.deinit();
}
const TestFile = "Start.exe";
const TestFileExtractLocation = "testing/Start.exe";
test "ExtractSingleFileMT" {
const io = testing.io;
const alloc = testing.allocator;
Io.Dir.cwd().deleteFile(io, TestFileExtractLocation) catch {};
var archive_file = try Io.Dir.cwd().openFile(io, TestArchive, .{});
defer archive_file.close(io);
var archive: Archive = try .init(io, archive_file, 0);
defer archive.deinit(io);
var start_exe = try archive.open(alloc, TestFile);
defer start_exe.deinit();
try start_exe.extract(alloc, io, TestFileExtractLocation, .default);
}
test "ExtractSingleFileST" {
const io = testing.io;
const alloc = testing.allocator;
Io.Dir.cwd().deleteFile(io, TestFileExtractLocation) catch {};
var archive_file = try Io.Dir.cwd().openFile(io, TestArchive, .{});
defer archive_file.close(io);
var archive: Archive = try .init(io, archive_file, 0);
defer archive.deinit(io);
var start_exe = try archive.open(alloc, TestFile);
defer start_exe.deinit();
try start_exe.extract(alloc, io, TestFileExtractLocation, .single_threaded_default);
}
const TestFullExtractLocationSTOption = "testing/TestExtractSTOption";
test "ExtractCompleteArchiveSingleThreadedOption" {
const io = testing.io;
const alloc = testing.allocator;
Io.Dir.cwd().deleteTree(io, TestFullExtractLocationSTOption) catch {};
var archive_file = try Io.Dir.cwd().openFile(io, TestArchive, .{});
defer archive_file.close(io);
var archive: Archive = try .init(io, archive_file, 0);
defer archive.deinit(io);
try archive.extract(alloc, io, TestFullExtractLocationSTOption, .single_threaded_default);
}
const TestFullExtractLocationMT = "testing/TestExtractMT";
test "ExtractCompleteArchiveMultiThreaded" {
const io = testing.io;
const alloc = testing.allocator;
Io.Dir.cwd().deleteTree(io, TestFullExtractLocationMT) catch {};
var archive_file = try Io.Dir.cwd().openFile(io, TestArchive, .{});
defer archive_file.close(io);
var archive: Archive = try .init(io, archive_file, 0);
defer archive.deinit(io);
try archive.extract(alloc, io, TestFullExtractLocationMT, .default);
}
const TestFullExtractLocationSTIo = "testing/TestExtractSTIo";
// test "ExtractCompleteArchiveSingleThreadedIo" {
// const io = Io.Threaded.global_single_threaded.io();
// const alloc = testing.allocator;
// Io.Dir.cwd().deleteFile(io, TestFullExtractLocationSTIo) catch {};
// var archive_file = try Io.Dir.cwd().openFile(io, TestArchive, .{});
// defer archive_file.close(io);
// var archive: Archive = try .init(io, archive_file, 0);
// defer archive.deinit(io);
// try archive.extract(alloc, io, TestFullExtractLocationSTIo, .default);
// }
const LinuxPATestCorrectSuperblock: Archive.Superblock = .{
.magic = std.mem.readInt(u32, "hsqs", .little),
.inode_count = 2974,
.mod_time = 1632696724,
.block_size = 131072,
.frag_count = 264,
.compression = .zstd,
.block_log = 17,
.flags = .{
.inode_uncompressed = false,
.data_uncompressed = false,
.check = false,
.frag_uncompressed = false,
.fragment_never = false,
.fragment_always = false,
.duplicates = true,
.exportable = true,
.xattr_uncompressed = false,
.xattr_never = false,
.compression_options = false,
.ids_uncompressed = false,
._ = 0,
},
.id_count = 1,
.ver_maj = 4,
.ver_min = 0,
.root_ref = .{
.block_offset = 1363,
.block_start = 29237,
._ = 0,
},
.size = 106841744,
.id_start = 106841632,
.xattr_start = 106841720,
.inode_start = 106778274,
.dir_start = 106807998,
.frag_start = 106837747,
.export_start = 106841602,
};
+39
View File
@@ -0,0 +1,39 @@
const std = @import("std");
const Io = std.Io;
var alloc = std.testing.allocator;
var io = std.testing.io;
const Archive = @import("archive.zig");
const TEST_ARCHIVE = "testing/LinuxPATest.sfs";
const OPEN_TEST_LOCATION = "PortableApps/gVimPortable/GVim-v8.2.2965.glibc2.15-x86_64.AppImage";
test "Open" {
var archive_file = try Io.Dir.cwd().openFile(io, TEST_ARCHIVE, .{});
defer archive_file.close(io);
var arc: Archive = try .init(io, archive_file, 0);
defer arc.deinit(io);
var root = try arc.root(alloc);
defer root.deinit();
var test_open = try root.open(alloc, OPEN_TEST_LOCATION);
defer test_open.deinit();
}
const FULL_EXTRACT_LOCATION = "testing/TestExtractST";
test "FullExtractSingleThreaded" {
Io.Dir.cwd().deleteTree(io, FULL_EXTRACT_LOCATION) catch {};
var archive_file = try Io.Dir.cwd().openFile(io, TEST_ARCHIVE, .{});
defer archive_file.close(io);
var arc: Archive = try .init(io, archive_file, 0);
defer arc.deinit(io);
try arc.extract(alloc, io, FULL_EXTRACT_LOCATION, .default);
}
-52
View File
@@ -1,52 +0,0 @@
const std = @import("std");
const Io = std.Io;
const Decomp = @import("../decomp.zig");
const CacheMap = @import("protected_map.zig").ProtectedMap(u64, CachedBlock, decompressBlock);
const Cache = @This();
alloc: std.mem.Allocator,
data: []u8,
decomp: Decomp.Fn,
cache: CacheMap,
pub fn init(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn) Cache {
return .{
.alloc = alloc,
.data = data,
.decomp = decomp,
.cache = .init(alloc),
};
}
pub fn deinit(self: *Cache) void {
self.cache.deinit();
}
pub fn get(self: *Cache, io: Io, offset: u64, compressed_size: u32) Error![]u8 {
const res: *CachedBlock = try self.cache.getOrPut(io, offset, .{ self.alloc, self.data, self.decomp, offset, compressed_size });
return res.block[0..res.size];
}
fn decompressBlock(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, offset: u64, compressed_size: u32) !CachedBlock {
const block = data[offset..][0..compressed_size];
var new_cache: CachedBlock = undefined;
new_cache.size = @truncate(try decomp(alloc, block, &new_cache.block));
return new_cache;
}
// Types
pub const Error = CacheMap.Error;
const CachedBlock = struct {
block: [1024 * 1024]u8,
size: u32,
};
-91
View File
@@ -1,91 +0,0 @@
const std = @import("std");
const Io = std.Io;
pub fn ProtectedMap(comptime K: anytype, comptime T: anytype, comptime create_fn: anytype) type {
const fn_info = @typeInfo(@TypeOf(create_fn));
std.debug.assert(std.meta.activeTag(fn_info) == .@"fn");
const ret_info = fn_info.@"fn".return_type;
std.debug.assert(ret_info != null);
std.debug.assert(ret_info == T or
(std.meta.activeTag(@typeInfo(ret_info.?)) == .error_union and @typeInfo(ret_info.?).error_union.payload == T));
return struct {
const Map = @This();
alloc: std.mem.Allocator,
map: std.AutoHashMap(K, ProtectedValue),
mut: Io.RwLock = .init,
pub fn init(alloc: std.mem.Allocator) Map {
return .{
.alloc = alloc,
.map = .init(alloc),
};
}
pub fn deinit(self: *Map) void {
self.map.deinit();
}
pub fn getOrPut(self: *Map, io: Io, key: K, create_fn_args: std.meta.ArgsTuple(@TypeOf(create_fn))) Error!*T {
{
try self.mut.lockShared(io);
defer self.mut.unlockShared(io);
const value = self.map.getPtr(key);
if (value != null) {
if (!value.?.filled.isSet())
try value.?.filled.wait(io);
if (value.?.err != null) return value.?.err.?;
return &value.?.value;
}
}
try self.mut.lock(io);
const res = self.map.getOrPut(key) catch |err| {
self.mut.unlock(io);
return err;
};
if (res.found_existing) {
self.mut.unlock(io);
return self.getOrPut(io, key, create_fn_args);
}
res.value_ptr.* = .{};
defer res.value_ptr.filled.set(io);
self.mut.unlock(io);
self.mut.lockSharedUncancelable(io);
defer self.mut.unlockShared(io);
res.value_ptr.value = if (@TypeOf(CreateError) == void)
@call(.auto, create_fn, create_fn_args)
else
@call(.auto, create_fn, create_fn_args) catch |err| {
res.value_ptr.err = err;
return err;
};
return &res.value_ptr.value;
}
// Map Types
pub const Error = error{ Canceled, OutOfMemory } ||
if (@TypeOf(CreateError) == void) error{} else CreateError;
const CreateError: type = switch (@typeInfo(ret_info.?)) {
.error_union => |e| e.error_set,
else => void,
};
const ProtectedValue = struct {
value: T = undefined,
err: ?CreateError = null,
filled: Io.Event = .unset,
};
};
}
+141
View File
@@ -0,0 +1,141 @@
const std = @import("std");
const Io = std.Io;
const Decomp = @import("../decomp.zig");
const BlockSize = @import("../inode.zig").BlockSize;
const DataReader = @This();
alloc: std.mem.Allocator,
data: []u8,
decomp: Decomp.Fn,
block_size: u32,
size: u64,
blocks: []BlockSize,
frag: ?[]u8 = null,
idx: u32 = 0,
buf: []u8,
interface: Io.Reader = .{
.buffer = &[0]u8{},
.seek = 0,
.end = 0,
.vtable = &.{
.stream = stream,
.discard = discard,
.readVec = readVec,
},
},
pub fn init(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, block_size: u32, size: u64, blocks: []BlockSize) !DataReader {
return .{
.alloc = alloc,
.data = data,
.decomp = decomp,
.block_size = block_size,
.size = size,
.blocks = blocks,
.buf = if (blocks.len > 0) try alloc.alloc(u8, 1024 * 1024) else &[0]u8{},
};
}
pub fn deinit(self: DataReader) void {
self.alloc.free(self.buf);
}
pub fn addFrag(self: *DataReader, frag: []u8) void {
self.frag = frag;
}
fn advance(self: *DataReader) Io.Reader.Error!void {
if (self.idx > self.blocks.len) return error.EndOfStream;
defer self.idx += 1;
self.interface.seek = 0;
errdefer self.interface.end = 0;
if (self.idx == self.blocks.len) {
if (self.frag == null) return error.EndOfStream;
self.interface.buffer = self.frag.?;
self.interface.end = self.frag.?.len;
return;
}
self.interface.end = if (self.frag == null and self.idx == self.blocks.len - 1)
self.size % self.block_size
else
self.block_size;
const block = self.blocks[self.idx];
if (block.size == 0) {
@memset(self.buf[0..self.interface.end], 0);
self.interface.buffer = self.buf;
return;
}
if (block.uncompressed) {
self.interface.buffer = self.data;
self.data = self.data[self.interface.end..];
return;
}
_ = self.decomp(self.alloc, self.data[0..block.size], self.buf[0..self.interface.end]) catch return error.ReadFailed;
self.data = self.data[block.size..];
self.interface.buffer = self.buf;
return;
}
fn stream(rdr: *Io.Reader, wrt: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize {
if (rdr.seek >= rdr.end) {
var meta: *DataReader = @fieldParentPtr("interface", rdr);
try meta.advance();
}
if (limit == .nothing) return 0;
const size = @min(@intFromEnum(limit), rdr.end - rdr.seek);
const wrote = try wrt.write(rdr.buffer[rdr.seek..][0..size]);
rdr.seek += wrote;
return wrote;
}
fn discard(rdr: *Io.Reader, limit: Io.Limit) Io.Reader.Error!usize {
if (rdr.seek >= rdr.end) {
var meta: *DataReader = @fieldParentPtr("interface", rdr);
try meta.advance();
}
if (limit == .nothing) return 0;
const size = @min(@intFromEnum(limit), rdr.end - rdr.seek);
rdr.seek += size;
return size;
}
fn readVec(rdr: *Io.Reader, vec: [][]u8) Io.Reader.Error!usize {
if (rdr.seek >= rdr.end) {
var meta: *DataReader = @fieldParentPtr("interface", rdr);
try meta.advance();
}
var wrote: usize = 0;
for (vec) |s| {
const size = @min(rdr.end - rdr.seek, s.len);
@memcpy(s[0..size], rdr.buffer[rdr.seek..][0..size]);
wrote += size;
rdr.seek += size;
if (rdr.seek >= rdr.end) break;
}
return wrote;
}
+219
View File
@@ -0,0 +1,219 @@
const std = @import("std");
const Io = std.Io;
const Super = @import("../archive.zig").Super;
const Decomp = @import("../decomp.zig");
const Directory = @import("../dir.zig");
const FragCache = @import("../frag_cache.zig");
const Inode = @import("../inode.zig");
const Lookup = @import("../lookup.zig");
const Options = @import("../options.zig");
const DataReader = @import("data_reader.zig");
const MetadataReader = @import("meta.zig");
// TODO: Add verbose logging.
pub fn single(alloc: std.mem.Allocator, io: Io, data: []u8, super: Super, inode: Inode, filepath: []const u8, options: Options) !void {
const path = std.mem.trim(u8, filepath, "/");
var id_table: Lookup.Table(u16) = try .init(data, super.decomp_fn, super.id_table_start, super.id_count);
defer id_table.deinit(alloc);
var xattr_table: Lookup.Xattr = try .init(data, super.decomp_fn, super.xattr_table_start);
defer xattr_table.deinit(alloc);
var frag_cache: FragCache = try .init(data, super.decomp_fn, super.block_size, super.frag_table_start, super.frag_count);
defer frag_cache.deinit(alloc);
var pool: std.ArrayList(InodeAndPath) = try .initCapacity(alloc, 15);
pool.appendAssumeCapacity(.{
.inode = inode,
.path = path,
.root = true,
});
defer {
while (pool.pop()) |in|
in.deinit(alloc);
pool.deinit(alloc);
}
var dirs: std.ArrayList(InodeAndPath) = .empty;
defer {
while (dirs.pop()) |dir|
alloc.free(dir.path);
dirs.deinit(alloc);
}
var cwd = Io.Dir.cwd();
while (pool.pop()) |in| {
switch (in.inode.data) {
.dir => |d| {
try cwd.createDirPath(io, in.path);
if (d.size <= 3) {
defer if (in.path.len != path.len)
in.deinit(alloc);
var fil = try cwd.openFile(io, in.path, .{});
defer fil.close(io);
try applyPermissions(alloc, io, &id_table, &xattr_table, in.inode.header, d.xattr_idx, fil, options);
continue;
}
var meta: MetadataReader = .init(alloc, data[d.start..], super.decomp_fn);
try meta.interface.discardAll(d.offset);
var dir: Directory = try .read(alloc, &meta.interface, d.size);
defer dir.deinit(alloc);
for (dir.entries) |entry| {
const new_inode: Inode = try .readLocation(alloc, data, entry.start, entry.offset, super);
const new_path = std.mem.concat(alloc, u8, &.{ in.path, "/", entry.name }) catch |err| {
new_inode.deinit(alloc);
return err;
};
try pool.append(alloc, .{
.inode = new_inode,
.path = new_path,
.root = false,
});
}
try dirs.append(alloc, in);
},
.file => |f| {
defer if (in.path.len != path.len)
in.deinit(alloc);
var atomic = try cwd.createFileAtomic(io, in.path, .{});
defer atomic.deinit(io);
var rdr: DataReader = try .init(alloc, data[f.start..], super.decomp_fn, super.block_size, f.size, f.blocks);
defer rdr.deinit();
if (f.frag_idx != 0xFFFFFFFF) {
const frag = try frag_cache.get(alloc, io, f.frag_idx);
rdr.addFrag(frag);
}
var buf: [1024 * 50]u8 = undefined; // TODO: find a resonable buffer size.
var writer = atomic.file.writer(io, &buf);
_ = try rdr.interface.streamRemaining(&writer.interface);
try applyPermissions(alloc, io, &id_table, &xattr_table, in.inode.header, f.xattr_idx, atomic.file, options);
try atomic.link(io);
},
.symlink => |s| {
defer if (in.path.len != path.len)
in.deinit(alloc);
try cwd.symLink(io, s.target, in.path, .{});
},
.dev => |d| {
defer if (in.path.len != path.len)
in.deinit(alloc);
const mode: u32 = switch (in.inode.header.type) {
.char_dev, .ext_char_dev => std.os.linux.DT.CHR,
.block_dev, .ext_block_dev => std.os.linux.DT.BLK,
else => unreachable,
};
const sent_path = try alloc.dupeSentinel(u8, in.path, 0);
const res = std.os.linux.mknod(sent_path, mode, d.device);
alloc.free(sent_path);
if (res != 0)
return error.MkNodError;
var fil = try cwd.openFile(io, in.path, .{});
defer fil.close(io);
try applyPermissions(alloc, io, &id_table, &xattr_table, in.inode.header, d.xattr_idx, fil, options);
},
.fifo => |f| {
defer if (in.path.len != path.len)
in.deinit(alloc);
const mode: u32 = switch (in.inode.header.type) {
.fifo, .ext_fifo => std.os.linux.DT.FIFO,
.socket, .ext_socket => std.os.linux.DT.SOCK,
else => unreachable,
};
const sent_path = try alloc.dupeSentinel(u8, in.path, 0);
const res = std.os.linux.mknod(sent_path, mode, 0);
alloc.free(sent_path);
if (res != 0)
return error.MkNodError;
var fil = try cwd.openFile(io, in.path, .{});
defer fil.close(io);
try applyPermissions(alloc, io, &id_table, &xattr_table, in.inode.header, f.xattr_idx, fil, options);
},
}
}
while (dirs.pop()) |dir| {
defer if (dir.path.len != path.len)
alloc.free(dir.path);
var fil = try cwd.openFile(io, dir.path, .{});
defer fil.close(io);
try applyPermissions(alloc, io, &id_table, &xattr_table, dir.inode.header, dir.inode.data.dir.xattr_idx, fil, options);
}
}
pub fn multi(alloc: std.mem.Allocator, io: Io, data: []const u8, super: Super, inode: Inode, filepath: []const u8, options: Options) !void {
_ = alloc;
_ = io;
_ = data;
_ = super;
_ = inode;
_ = filepath;
_ = options;
return error.TODO;
}
// Utils
const InodeAndPath = struct {
inode: Inode,
path: []const u8,
root: bool,
fn deinit(self: InodeAndPath, alloc: std.mem.Allocator) void {
if (self.root) return;
self.inode.deinit(alloc);
alloc.free(self.path);
}
};
fn applyPermissions(alloc: std.mem.Allocator, io: Io, id_table: *Lookup.Table(u16), xattr_table: *Lookup.Xattr, header: Inode.Header, xattr_idx: ?u32, fil: Io.File, options: Options) !void {
if (!options.ignore_permissions) {
try fil.setPermissions(io, @enumFromInt(header.permission));
try fil.setTimestamps(io, .{ .modify_timestamp = .{ .new = .fromNanoseconds(@as(i96, @intCast(header.mod_time)) * std.time.ns_per_ms) } });
try fil.setOwner(io, try id_table.get(alloc, io, header.uid_idx), try id_table.get(alloc, io, header.gid_idx));
}
if (!options.ignore_xattr and xattr_idx != null) {
const xattrs = try xattr_table.get(alloc, io, xattr_idx.?);
defer {
for (xattrs) |kv|
kv.deinit(alloc);
alloc.free(xattrs);
}
for (xattrs) |kv| {
const res = std.os.linux.fsetxattr(fil.handle, kv.name, kv.value.ptr, kv.value.len, 0);
if (res != 0)
return error.FSetXattrError;
}
}
}
+109
View File
@@ -0,0 +1,109 @@
const std = @import("std");
const Io = std.Io;
const util = @import("util.zig");
const Decomp = @import("../decomp.zig");
const MetadataReader = @This();
alloc: std.mem.Allocator,
data: []u8,
decomp: Decomp.Fn,
decomp_block: [8192]u8 = undefined,
interface: Io.Reader = .{
.buffer = &[0]u8{},
.end = 0,
.seek = 0,
.vtable = &.{
.stream = stream,
.discard = discard,
.readVec = readVec,
},
},
pub fn init(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn) MetadataReader {
return .{
.alloc = alloc,
.data = data,
.decomp = decomp,
};
}
fn advance(self: *MetadataReader) Io.Reader.Error!void {
self.interface.seek = 0;
errdefer self.interface.end = 0;
const hdr = util.readValue(Header, self.data[0..2]);
const block = self.data[2..][0..hdr.size];
self.data = self.data[hdr.size + 2 ..];
if (hdr.uncompressed) {
self.interface.end = hdr.size;
self.interface.buffer = block;
return;
}
self.interface.end = self.decomp(self.alloc, block, &self.decomp_block) catch return error.ReadFailed;
self.interface.buffer = self.decomp_block[0..self.interface.end];
}
fn stream(rdr: *Io.Reader, wrt: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize {
if (rdr.seek >= rdr.end) {
var meta: *MetadataReader = @fieldParentPtr("interface", rdr);
try meta.advance();
}
if (limit == .nothing) return 0;
const size = @min(@intFromEnum(limit), rdr.end - rdr.seek);
const wrote = try wrt.write(rdr.buffer[rdr.seek..][0..size]);
rdr.seek += wrote;
return wrote;
}
fn discard(rdr: *Io.Reader, limit: Io.Limit) Io.Reader.Error!usize {
if (rdr.seek >= rdr.end) {
var meta: *MetadataReader = @fieldParentPtr("interface", rdr);
try meta.advance();
}
if (limit == .nothing) return 0;
const size = @min(@intFromEnum(limit), rdr.end - rdr.seek);
rdr.seek += size;
return size;
}
fn readVec(rdr: *Io.Reader, vec: [][]u8) Io.Reader.Error!usize {
if (rdr.seek >= rdr.end) {
var meta: *MetadataReader = @fieldParentPtr("interface", rdr);
try meta.advance();
}
var wrote: usize = 0;
for (vec) |s| {
const size = @min(rdr.end - rdr.seek, s.len);
@memcpy(s[0..size], rdr.buffer[rdr.seek..][0..size]);
wrote += size;
rdr.seek += size;
if (rdr.seek >= rdr.end) break;
}
return wrote;
}
// Types
const Header = packed struct(u16) {
size: u15,
uncompressed: bool,
};
+28
View File
@@ -0,0 +1,28 @@
const std = @import("std");
const builtin = @import("builtin");
pub fn readValue(comptime T: type, bytes: []u8) T {
var res = std.mem.bytesToValue(T, bytes);
if (comptime builtin.cpu.arch.endian() != .little) {
switch (@typeInfo(T)) {
.@"enum" => res = std.mem.nativeToLittle(@typeInfo(T).@"enum".tag_type, res),
.@"struct" => |s| {
if (s.layout == .@"packed") {
res = @bitCast(std.mem.nativeToLittle(s.backing_integer.?, @bitCast(res)));
} else {
std.mem.byteSwapAllFields(T, &res);
}
},
.int => res = std.mem.nativeToLittle(T, res),
.array => |a| std.mem.byteSwapAllElements(a.child, res),
else => unreachable,
}
}
return res;
}
pub fn readValueRdr(comptime T: type, rdr: *std.Io.Reader) !T {
var tmp: [@sizeOf(T)]u8 = undefined;
try rdr.readSliceAll(&tmp);
return readValue(T, &tmp);
}
-134
View File
@@ -1,134 +0,0 @@
const std = @import("std");
const Io = std.Io;
const Decomp = @import("decomp.zig");
const Lookup = @import("lookup.zig");
const MetadataReader = @import("meta_rdr.zig");
const Ref = @import("inode.zig").Ref;
const XattrTable = @This();
table_start: u64,
table: Lookup.Table(Entry),
pub fn init(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, table_start: u64) XattrTable {
const start = std.mem.readInt(u64, data[table_start..][0..8], .little);
const table_num = std.mem.readInt(u32, data[table_start + 8 ..][0..4], .little);
return .{
.table_start = start,
.table = .init(alloc, data, decomp, table_start + 16, table_num),
};
}
pub fn deinit(self: *XattrTable) void {
self.table.deinit();
}
pub fn get(self: *XattrTable, alloc: std.mem.Allocator, io: Io, idx: u32) !Xattr {
const entry: Entry = try self.table.get(io, idx);
var out: std.ArrayList(KeyValue) = try .initCapacity(alloc, entry.count);
errdefer {
for (out.items) |kv|
kv.deinit(alloc);
out.deinit(alloc);
}
var meta: MetadataReader = .init(alloc, self.table.data, self.table.decomp, self.table_start + entry.ref.block_start);
try meta.interface.discardAll(entry.ref.block_offset);
for (0..entry.count) |_| {
var key_entry: KeyEntry = undefined;
try meta.interface.readSliceEndian(KeyEntry, @ptrCast(&key_entry), .little);
const prefix = switch (key_entry.type.type) {
.user => "user.",
.trusted => "trusted.",
.security => "security.",
};
const key = try alloc.allocSentinel(u8, key_entry.name_size + prefix.len, 0);
errdefer alloc.free(key);
@memcpy(key[0..prefix.len], prefix);
try meta.interface.readSliceEndian(u8, key[prefix.len..][0..key_entry.name_size], .little);
const value = if (key_entry.type.out_of_line) blk: {
try meta.interface.discardAll(4);
var ref: Ref = undefined;
try meta.interface.readSliceEndian(Ref, @ptrCast(&ref), .little);
var value_meta: MetadataReader = .init(alloc, self.table.data, self.table.decomp, self.table_start + ref.block_start);
try value_meta.interface.discardAll(ref.block_offset);
break :blk try readValue(alloc, &value_meta.interface);
} else try readValue(alloc, &meta.interface);
const ptr = out.addOneAssumeCapacity();
ptr.* = .{
.key = key,
.value = value,
};
}
return .{ .kvs = try out.toOwnedSlice(alloc) };
}
fn readValue(alloc: std.mem.Allocator, rdr: *Io.Reader) ![]u8 {
var size: u32 = undefined;
try rdr.readSliceEndian(u32, @ptrCast(&size), .little);
const value = try alloc.alloc(u8, size);
errdefer alloc.free(value);
try rdr.readSliceEndian(u8, value, .little);
return value;
}
// Types
const Xattr = struct {
kvs: []KeyValue,
pub fn deinit(self: Xattr, alloc: std.mem.Allocator) void {
for (self.kvs) |kv|
kv.deinit(alloc);
alloc.free(self.kvs);
}
};
const KeyValue = struct {
key: [:0]const u8,
value: []const u8,
pub fn deinit(self: KeyValue, alloc: std.mem.Allocator) void {
alloc.free(self.key);
alloc.free(self.value);
}
};
const KeyEntry = extern struct {
type: packed struct(u16) {
type: enum(u8) {
user,
trusted,
security,
},
out_of_line: bool,
_: u7,
},
name_size: u16,
};
const Entry = extern struct {
ref: Ref,
count: u32,
size: u32,
};
View File