Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d00acb03b9 | ||
|
|
7f09a8ff69 | ||
|
|
68d1a9d26d | ||
|
|
2e4364b863 | ||
|
|
2e33e19cdd | ||
|
|
173e76469f | ||
|
|
c5d75d3839 | ||
|
|
e0b697dd0b | ||
|
|
9f2a4bcc1e | ||
|
|
78dbaaed7e | ||
|
|
0039b19b4b |
+1
-22
@@ -11,7 +11,7 @@
|
||||
|
||||
"build": {
|
||||
"command": "zig",
|
||||
"args": ["build", "-Ddebug=true"],
|
||||
"args": ["build", "-Ddebug=true", "-Dstatic=true"],
|
||||
},
|
||||
|
||||
"program": "zig-out/bin/unsquashfs",
|
||||
@@ -22,25 +22,4 @@
|
||||
"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",
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#! /usr/bin/env bash
|
||||
|
||||
ARCHIVE="testing/LinuxPATest.sfs"
|
||||
|
||||
REF_EXT_LOC="testing/LinuxPAReference"
|
||||
PROG_EXT_LOC="testing/LinuxPABinTest"
|
||||
|
||||
echo "Building with ReleaseFast"
|
||||
echo ""
|
||||
|
||||
zig build -Doptimize=ReleaseFast
|
||||
|
||||
echo "Testing Multi-threaded Performance"
|
||||
echo ""
|
||||
|
||||
hyperfine --warmup 5 --prepare "rm -rf $REF_EXT_LOC && rm -rf $PROG_EXT_LOC" "unsquashfs -d $REF_EXT_LOC $ARCHIVE" "zig-out/bin/unsquashfs -d $PROG_EXT_LOC $ARCHIVE"
|
||||
|
||||
echo ""
|
||||
echo "Testing Single-threaded Performance"
|
||||
echo ""
|
||||
|
||||
hyperfine --warmup 5 --prepare "rm -rf $REF_EXT_LOC && rm -rf $PROG_EXT_LOC" "unsquashfs -p 1 -d $REF_EXT_LOC $ARCHIVE" "zig-out/bin/unsquashfs -p 1 -d $PROG_EXT_LOC $ARCHIVE"
|
||||
@@ -1,109 +1,103 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) !void {
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
const target = b.standardTargetOptions(.{});
|
||||
|
||||
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 zig_decomp = b.option(bool, "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 static = b.option(bool, "static", "Statically link libraries.") 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: 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();
|
||||
options.addOption(bool, "use_zig_decomp", use_zig_decomp);
|
||||
options.addOption(bool, "allow_lzo", allow_lzo);
|
||||
options.addOption(std.SemanticVersion, "version", version);
|
||||
const version: std.SemanticVersion = try .parse(if (version_string_option[0] == 'v') version_string_option[1..] else version_string_option);
|
||||
|
||||
const c = b.addTranslateC(.{
|
||||
.optimize = optimize,
|
||||
const build_config = b.addOptions();
|
||||
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,
|
||||
.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("lzma", .{});
|
||||
c.linkSystemLibrary("lz4", .{});
|
||||
c.linkSystemLibrary("zstd", .{});
|
||||
c.linkSystemLibrary("lz4", .{});
|
||||
c.linkSystemLibrary("lzma", .{});
|
||||
if (allow_lzo)
|
||||
c.linkSystemLibrary("minilzo", .{});
|
||||
}
|
||||
|
||||
const lib = b.addLibrary(.{
|
||||
.name = "squashfs",
|
||||
.use_llvm = debug,
|
||||
.version = version,
|
||||
.root_module = b.createModule(.{
|
||||
.root_module = b.addModule("zig_squashfs", .{
|
||||
.root_source_file = b.path("src/root.zig"),
|
||||
.optimize = optimize,
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.valgrind = debug,
|
||||
.imports = &.{
|
||||
.{ .name = "c", .module = c.createModule() },
|
||||
.{ .name = "build_options", .module = options.createModule() },
|
||||
.{ .name = "c", .module = c },
|
||||
.{ .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(.{
|
||||
.name = "unsquashfs",
|
||||
.use_llvm = debug,
|
||||
.version = version,
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/bin/unsquashfs.zig"),
|
||||
.optimize = optimize,
|
||||
.target = target,
|
||||
.valgrind = debug,
|
||||
.optimize = optimize,
|
||||
.imports = &.{
|
||||
.{ .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);
|
||||
|
||||
const lib_tests = b.addTest(.{
|
||||
const mod_tests = b.addTest(.{
|
||||
.root_module = lib.root_module,
|
||||
.use_llvm = debug,
|
||||
});
|
||||
const run_lib_tests = b.addRunArtifact(lib_tests);
|
||||
const exe_tests = b.addTest(.{
|
||||
.root_module = exe.root_module,
|
||||
.use_llvm = debug,
|
||||
});
|
||||
const run_exe_tests = b.addRunArtifact(exe_tests);
|
||||
|
||||
const run_mod_tests = b.addRunArtifact(mod_tests);
|
||||
const test_step = b.step("test", "Run tests");
|
||||
test_step.dependOn(&run_lib_tests.step);
|
||||
test_step.dependOn(&run_exe_tests.step);
|
||||
test_step.dependOn(&run_mod_tests.step);
|
||||
|
||||
// zls build check steps
|
||||
const lib_check = b.addLibrary(.{
|
||||
@@ -119,7 +113,4 @@ pub fn build(b: *std.Build) !void {
|
||||
const check = b.step("check", "Check if unsquashfs compiles");
|
||||
check.dependOn(&lib_check.step);
|
||||
check.dependOn(&exe_check.step);
|
||||
|
||||
test_step.dependOn(&lib_check.step);
|
||||
test_step.dependOn(&exe_check.step);
|
||||
}
|
||||
|
||||
+2
-6
@@ -9,17 +9,13 @@
|
||||
.hash = "zlib_ng-2.3.3-pre1-2HYS4MlFAADc-eSZv4_xjkxZRCdGUVOYZKdtqhybstgk",
|
||||
},
|
||||
.zstd = .{
|
||||
.url = "git+https://github.com/allyourcodebase/zstd.git?ref=1.5.7-1#e1a501be57f42c541e8a5597e4b59a074dfd09a3",
|
||||
.hash = "zstd-1.5.7-1-KEItkAMwAAD6OKY3m0OOmXG7aL-aLUfrDqbP5J5oYapU",
|
||||
.url = "git+https://github.com/allyourcodebase/zstd.git?ref=1.5.7-2#4ccb01e6fceae86fb9fe36c60b8a103cdc058155",
|
||||
.hash = "zstd-1.5.7-2-KEItkAMwAADNhCbGaImKrjXjnSA51jcqEbGCA3RasVZ9",
|
||||
},
|
||||
.lz4 = .{
|
||||
.url = "git+https://github.com/allyourcodebase/lz4.git?ref=1.10.0-6#41f52ab227caf9d48cf88c89a4d2946caa12b102",
|
||||
.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 = .{
|
||||
.url = "git+https://github.com/CalebQ42/zig-minilzo.git#f72a0e20a30fad38cd10104f393c8f3440478697",
|
||||
.hash = "minilzo-2.10.0-Ij7BO7YLAABd6oK2YdimXwvFTOC8wD1sNm0PHGoRNEwR",
|
||||
|
||||
+93
-73
@@ -1,88 +1,84 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
const MemoryMap = Io.File.MemoryMap;
|
||||
|
||||
const Decomp = @import("decomp.zig");
|
||||
const Inode = @import("inode.zig");
|
||||
const util = @import("utils/util.zig");
|
||||
|
||||
const File = @import("file.zig");
|
||||
const ExtractionOptions = @import("options.zig");
|
||||
const Extract = @import("extract.zig");
|
||||
const Inode = @import("inode.zig");
|
||||
const Decomp = @import("decomp.zig");
|
||||
const Options = @import("options.zig");
|
||||
const Decompress = @import("utils/decompress.zig");
|
||||
|
||||
const Archive = @This();
|
||||
|
||||
super: Superblock,
|
||||
map: Io.File.MemoryMap,
|
||||
|
||||
map: MemoryMap,
|
||||
decomp: Decomp.Fn,
|
||||
super: Super,
|
||||
root_inode_ref: Inode.Reference,
|
||||
|
||||
pub fn init(io: Io, file: Io.File, offset: u64) !Archive {
|
||||
var rdr = file.reader(io, &[0]u8{});
|
||||
try rdr.seekTo(offset);
|
||||
var map = try file.createMemoryMap(io, .{
|
||||
.len = try file.length(io) - offset,
|
||||
.offset = offset,
|
||||
.protection = .{ .read = true },
|
||||
});
|
||||
|
||||
var super: Superblock = undefined;
|
||||
try rdr.interface.readSliceEndian(Superblock, @ptrCast(&super), .little);
|
||||
|
||||
try super.validate();
|
||||
var superblock = util.readValue(Superblock, map.memory[0..@sizeOf(Superblock)]);
|
||||
const super = try superblock.checkAndMinimize();
|
||||
|
||||
return .{
|
||||
.super = super,
|
||||
.map = map,
|
||||
|
||||
.map = try file.createMemoryMap(io, .{
|
||||
.offset = offset,
|
||||
.len = super.size,
|
||||
.protection = .{ .read = true },
|
||||
}),
|
||||
.decomp = try Decomp.getFn(super.compression),
|
||||
.super = super,
|
||||
.root_inode_ref = superblock.root_inode_ref,
|
||||
};
|
||||
}
|
||||
pub fn deinit(self: *Archive, io: Io) void {
|
||||
self.map.destroy(io);
|
||||
}
|
||||
|
||||
pub fn root(self: Archive, alloc: std.mem.Allocator) !File {
|
||||
return .initRef(alloc, self.super, self.map.memory, self.decomp, "", self.super.root_ref);
|
||||
pub fn root(self: *Archive, alloc: std.mem.Allocator) !File {
|
||||
return .{
|
||||
.alloc = alloc,
|
||||
|
||||
.data = self.map.memory,
|
||||
.super = self.super,
|
||||
|
||||
.inode = try .readLocation(
|
||||
alloc,
|
||||
self.map.memory,
|
||||
self.root_inode_ref.start,
|
||||
self.root_inode_ref.offset,
|
||||
self.super,
|
||||
),
|
||||
.name = "",
|
||||
};
|
||||
}
|
||||
|
||||
pub fn open(self: Archive, alloc: std.mem.Allocator, filepath: []const u8) !File {
|
||||
const root_file = try self.root(alloc);
|
||||
|
||||
const path = std.mem.trim(u8, filepath, "/");
|
||||
|
||||
if (path.len == 0 or (path.len == 1 and path[0] == '.'))
|
||||
return root_file;
|
||||
|
||||
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_file.open(alloc, filepath);
|
||||
return root.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(
|
||||
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.decomp,
|
||||
self.super.inode_start,
|
||||
self.super.block_size,
|
||||
self.super.root_ref,
|
||||
self.root_inode_ref.start,
|
||||
self.root_inode_ref.offset,
|
||||
self.super,
|
||||
);
|
||||
defer root_inode.deinit(alloc);
|
||||
|
||||
return root_inode.extract(alloc, io, self.super, self.map.memory, self.decomp, location, options);
|
||||
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
|
||||
|
||||
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,
|
||||
inode_count: u32,
|
||||
mod_time: u32,
|
||||
@@ -94,38 +90,62 @@ pub const Superblock = extern struct {
|
||||
inode_uncompressed: bool,
|
||||
data_uncompressed: bool,
|
||||
check: bool,
|
||||
frag_uncompressed: bool,
|
||||
fragment_uncompressed: bool,
|
||||
fragment_never: bool,
|
||||
fragment_always: bool,
|
||||
duplicates: bool,
|
||||
de_duplicate: bool,
|
||||
exportable: bool,
|
||||
xattr_uncompressed: bool,
|
||||
xattr_never: bool,
|
||||
compression_options: bool,
|
||||
ids_uncompressed: bool,
|
||||
id_uncompressed: bool,
|
||||
_: u4,
|
||||
},
|
||||
id_count: u16,
|
||||
ver_maj: u16,
|
||||
ver_min: u16,
|
||||
root_ref: Inode.Ref,
|
||||
version_major: u16,
|
||||
version_minor: u16,
|
||||
root_inode_ref: Inode.Reference,
|
||||
size: u64,
|
||||
id_start: u64,
|
||||
xattr_start: u64,
|
||||
inode_start: u64,
|
||||
dir_start: u64,
|
||||
frag_start: u64,
|
||||
export_start: u64,
|
||||
id_table_start: u64,
|
||||
xattr_table_start: u64,
|
||||
inode_table_start: u64,
|
||||
dir_table_start: u64,
|
||||
frag_table_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.
|
||||
pub fn validate(self: Superblock) !void {
|
||||
if (self.magic != SQUASHFS_MAGIC)
|
||||
return SuperblockError.InvalidMagic;
|
||||
if (self.flags.check)
|
||||
return SuperblockError.InvalidCheck;
|
||||
if (self.ver_maj != 4 or self.ver_min != 0)
|
||||
return SuperblockError.InvalidVersion;
|
||||
fn checkAndMinimize(self: Superblock) !Super {
|
||||
if (self.magic != MAGIC)
|
||||
return error.InvalidMagic;
|
||||
if (self.version_major != 4 or self.version_minor != 0)
|
||||
return error.IncompatibleVersion;
|
||||
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,
|
||||
};
|
||||
|
||||
+26
-149
@@ -1,10 +1,11 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
const Writer = Io.Writer;
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const build = @import("build");
|
||||
const config = @import("build_config");
|
||||
const squashfs = @import("squashfs");
|
||||
const Archive = squashfs.Archive;
|
||||
const Options = squashfs.Options;
|
||||
|
||||
//TODO: Add more options
|
||||
const help_mgs =
|
||||
@@ -13,8 +14,6 @@ const help_mgs =
|
||||
\\
|
||||
\\Options:
|
||||
\\ -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.
|
||||
\\ -dx Don't set xattr values
|
||||
@@ -30,162 +29,40 @@ const help_mgs =
|
||||
\\
|
||||
;
|
||||
|
||||
const errors = error{InvalidArguments};
|
||||
|
||||
var archive: []const u8 = "";
|
||||
var arc_loc: []const u8 = "";
|
||||
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 {
|
||||
var io = init.io;
|
||||
const alloc = init.gpa;
|
||||
|
||||
var stdout = Io.File.stdout();
|
||||
var out = stdout.writer(io, &[0]u8{});
|
||||
defer out.interface.flush() catch {};
|
||||
// TODO: process args
|
||||
|
||||
try handleArgs(alloc, init.minimal.args, &out.interface);
|
||||
if (archive.len == 0) {
|
||||
try out.interface.print("You must provide a squashfs archive\n", .{});
|
||||
try out.interface.print(help_mgs, .{});
|
||||
return;
|
||||
var limited_io: Io.Threaded = undefined;
|
||||
if (threads != 0) {
|
||||
limited_io = if (threads == 1)
|
||||
Io.Threaded.init_single_threaded
|
||||
else
|
||||
Io.Threaded.init(alloc, .{
|
||||
.async_limit = .limited(threads),
|
||||
.concurrent_limit = .limited(threads),
|
||||
.argv0 = .init(init.minimal.args),
|
||||
.environ = init.minimal.environ,
|
||||
});
|
||||
io = limited_io.io();
|
||||
}
|
||||
|
||||
var fil = try Io.Dir.cwd().openFile(io, archive, .{}); //TODO: Handle error gracefully.
|
||||
var fil = try Io.Dir.cwd().openFile(io, arc_loc, .{});
|
||||
defer fil.close(io);
|
||||
|
||||
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,
|
||||
};
|
||||
var arc: Archive = try .open(io, fil, offset);
|
||||
defer arc.close(io);
|
||||
|
||||
if (threads > 1) {
|
||||
limited = Io.Threaded.init(alloc, .{
|
||||
.argv0 = .init(init.minimal.args),
|
||||
.async_limit = @enumFromInt(threads),
|
||||
.concurrent_limit = @enumFromInt(threads),
|
||||
.environ = init.minimal.environ,
|
||||
});
|
||||
io = limited.?.io();
|
||||
}
|
||||
|
||||
if (force)
|
||||
try Io.Dir.cwd().deleteTree(io, ext_loc);
|
||||
|
||||
if (files.len > 0) {
|
||||
for (0..files.len) |i| {
|
||||
var sfs_fil = try arc.open(alloc, files[i]);
|
||||
defer sfs_fil.deinit();
|
||||
|
||||
const last_ind = std.mem.lastIndexOf(u8, files[i], "/");
|
||||
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 = try alloc.alloc([:0]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;
|
||||
}
|
||||
try arc.extract(alloc, io, ext_loc, options);
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
#include <zlib.h>
|
||||
#include <lzma.h>
|
||||
#include <lz4.h>
|
||||
#include <lzma.h>
|
||||
#include <zlib.h>
|
||||
#include <zstd.h>
|
||||
|
||||
#ifdef ALLOW_LZO
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
const Decomp = @import("../decomp.zig");
|
||||
const FileFinish = @import("../extract-multi.zig").FileFinish;
|
||||
const Multi = @import("../extract-multi.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, select: *Io.Select(Multi.SelectUnion), finish: *FileFinish) !void {
|
||||
if (self.size == 0) return;
|
||||
|
||||
var read_offset: u64 = self.start;
|
||||
for (0.., self.blocks) |i, block| {
|
||||
try select.concurrent(.reg, blockThread, .{ self, alloc, io, finish.file.file, read_offset, @truncate(i), finish });
|
||||
read_offset += block.size;
|
||||
}
|
||||
if (self.frag_data != null)
|
||||
try select.concurrent(.reg, fragThread, .{ self, io, finish.file.file, finish });
|
||||
}
|
||||
|
||||
fn blockThread(
|
||||
self: Extractor,
|
||||
alloc: std.mem.Allocator,
|
||||
io: Io,
|
||||
file: Io.File,
|
||||
read_offset: u64,
|
||||
block_idx: u32,
|
||||
finish: *FileFinish,
|
||||
) Multi.Error!void {
|
||||
const size = if (self.frag_data == null and block_idx == self.blocks.len - 1)
|
||||
self.size % self.block_size
|
||||
else
|
||||
self.block_size;
|
||||
|
||||
const block = self.blocks[block_idx];
|
||||
|
||||
var wrt = file.writer(io, &[0]u8{});
|
||||
try wrt.seekTo(block_idx * self.block_size);
|
||||
|
||||
if (block.size == 0) {
|
||||
try wrt.interface.splatByteAll(0, size);
|
||||
try finish.finish(io);
|
||||
return;
|
||||
}
|
||||
|
||||
const data = self.data[read_offset..][0..block.size];
|
||||
|
||||
if (block.uncompressed) {
|
||||
try wrt.interface.writeAll(data);
|
||||
try finish.finish(io);
|
||||
return;
|
||||
}
|
||||
|
||||
if (self.cache != null) {
|
||||
const decomp_block = try self.cache.?.get(io, read_offset, block.size);
|
||||
try wrt.interface.writeAll(decomp_block);
|
||||
} else {
|
||||
const tmp = try alloc.alloc(u8, size);
|
||||
defer alloc.free(tmp);
|
||||
|
||||
_ = try self.decomp(alloc, data, tmp);
|
||||
try wrt.interface.writeAll(tmp);
|
||||
}
|
||||
try finish.finish(io);
|
||||
}
|
||||
fn fragThread(
|
||||
self: Extractor,
|
||||
io: Io,
|
||||
file: Io.File,
|
||||
finish: *FileFinish,
|
||||
) Multi.Error!void {
|
||||
const size = self.size % self.block_size;
|
||||
|
||||
var wrt = file.writer(io, &[0]u8{});
|
||||
try wrt.seekTo(self.blocks.len * self.block_size);
|
||||
|
||||
try wrt.interface.writeAll(self.frag_data.?[self.frag_offset..][0..size]);
|
||||
try finish.finish(io);
|
||||
}
|
||||
@@ -1,180 +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_alloc: bool = false,
|
||||
|
||||
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 deinit(self: *Reader) void {
|
||||
if (self.block_alloc)
|
||||
self.alloc.free(self.interface.buffer);
|
||||
}
|
||||
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_alloc) self.alloc.free(self.interface.buffer);
|
||||
|
||||
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.block_alloc = true;
|
||||
self.interface.buffer = self.alloc.alloc(u8, size) catch return error.ReadFailed;
|
||||
errdefer {
|
||||
self.alloc.free(self.interface.buffer);
|
||||
self.interface.buffer = &[0]u8{};
|
||||
}
|
||||
|
||||
_ = self.decomp(self.alloc, self.data[self.offset..][0..block.size], self.interface.buffer) catch return error.ReadFailed;
|
||||
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
@@ -1,23 +1,8 @@
|
||||
const std = @import("std");
|
||||
const build = @import("build_options");
|
||||
const Io = std.Io;
|
||||
|
||||
const c = @import("c_decomp.zig");
|
||||
const zig = @import("zig_decomp.zig");
|
||||
|
||||
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;
|
||||
const c = @import("c");
|
||||
const build = @import("build_config");
|
||||
|
||||
pub const Enum = enum(u16) {
|
||||
gzip = 1,
|
||||
@@ -26,9 +11,138 @@ pub const Enum = enum(u16) {
|
||||
xz,
|
||||
lz4,
|
||||
zstd,
|
||||
};
|
||||
|
||||
pub const Error = error{
|
||||
OutOfMemory,
|
||||
DecompressionFailed,
|
||||
pub fn func(self: Enum) !Fn {
|
||||
return switch (self) {
|
||||
.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
@@ -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,
|
||||
};
|
||||
@@ -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,
|
||||
};
|
||||
@@ -1,503 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
const Atomic = std.atomic.Value;
|
||||
|
||||
const DataExtractor = @import("data/extractor.zig");
|
||||
const Decomp = @import("decomp.zig");
|
||||
const Directory = @import("directory.zig");
|
||||
const ExtractionOption = @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, filepath: []const u8, options: ExtractionOption) !void {
|
||||
const path = std.mem.trim(u8, filepath, "/");
|
||||
|
||||
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();
|
||||
|
||||
var cache: Cache = .init(alloc, data, decomp);
|
||||
defer cache.deinit();
|
||||
|
||||
var err: ?Error = null;
|
||||
var group: Io.Group = .init;
|
||||
|
||||
group.async(
|
||||
io,
|
||||
groupExtract,
|
||||
.{ alloc, io, data, decomp, super, &group, &frag_table, &id_table, &xattr_table, &cache, &err, inode, path, options, null },
|
||||
);
|
||||
}
|
||||
|
||||
fn groupExtract(
|
||||
alloc: std.mem.Allocator,
|
||||
io: Io,
|
||||
data: []u8,
|
||||
decomp: Decomp.Fn,
|
||||
super: Superblock,
|
||||
group: *Io.Group,
|
||||
frag_table: *Lookup.Table(Lookup.FragEntry),
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
cache: *Cache,
|
||||
err: *?Error,
|
||||
inode: Inode,
|
||||
path: []const u8,
|
||||
options: ExtractionOption,
|
||||
parent: ?*Parent,
|
||||
) error{Canceled}!void {
|
||||
if (err != null) {
|
||||
if (parent != null) {
|
||||
alloc.free(path);
|
||||
inode.deinit(alloc);
|
||||
parent.?.finish(alloc, io);
|
||||
}
|
||||
return;
|
||||
}
|
||||
switch (inode.hdr.type) {
|
||||
.dir, .ext_dir => group.async(io, extractDir, .{ alloc, io, data, decomp, super, group, frag_table, id_table, xattr_table, cache, inode, path, options, parent }),
|
||||
.file, .ext_file => group.async(io, extractReg, .{ alloc, io, data, decomp, super.block_size, group, frag_table, cache, inode, path, options, parent }),
|
||||
.symlink, .ext_symlink => group.async(io, extractSymlink, .{ alloc, io, inode, path, parent }),
|
||||
else => group.async(io, extractNod, .{ alloc, io, id_table, xattr_table, inode, path, options, parent }),
|
||||
}
|
||||
}
|
||||
|
||||
fn extractDir(
|
||||
alloc: std.mem.Allocator,
|
||||
io: Io,
|
||||
data: []u8,
|
||||
decomp: Decomp.Fn,
|
||||
super: Superblock,
|
||||
group: *Io.Group,
|
||||
frag_table: *Lookup.Table(Lookup.FragEntry),
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
cache: *Cache,
|
||||
err: *?Error,
|
||||
inode: Inode,
|
||||
path: []const u8,
|
||||
options: ExtractionOption,
|
||||
parent: ?*Parent,
|
||||
) Error!void {
|
||||
var xattr_idx: u32 = 0xFFFFFFFF;
|
||||
|
||||
const dir: Directory = switch (inode.data) {
|
||||
.dir => |d| blk: {
|
||||
var meta: MetadataReader = .init(alloc, data, decomp, super.dir_start + d.block_start);
|
||||
try meta.interface.discardAll(d.block_offset);
|
||||
|
||||
break :blk try .init(alloc, &meta.interface, d.size);
|
||||
},
|
||||
.ext_dir => |d| blk: {
|
||||
xattr_idx = d.xattr_idx;
|
||||
|
||||
var meta: MetadataReader = .init(alloc, data, decomp, super.dir_start + d.block_start);
|
||||
try meta.interface.discardAll(d.block_offset);
|
||||
|
||||
break :blk try .init(alloc, &meta.interface, d.size);
|
||||
},
|
||||
else => unreachable,
|
||||
};
|
||||
defer dir.deinit(alloc);
|
||||
|
||||
try Io.Dir.cwd().createDirPath(io, path);
|
||||
|
||||
if (dir.entries.len == 0) {
|
||||
try setMetadataPath(alloc, io, inode.hdr, id_table, xattr_table, path, xattr_idx, options);
|
||||
if (parent != null) {
|
||||
try parent.?.finish(io);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const cur_dir = try Parent.create(alloc, id_table, xattr_table, err, inode.hdr, path, options, parent, xattr_idx, dir.entries);
|
||||
|
||||
for (dir.entries) |entry| {
|
||||
const 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 |e| {
|
||||
new_inode.deinit(alloc);
|
||||
return e;
|
||||
};
|
||||
|
||||
group.async(
|
||||
io,
|
||||
groupExtract,
|
||||
.{ alloc, io, data, decomp, super, group, frag_table, id_table, xattr_table, cache, err, new_inode, new_path, options, cur_dir },
|
||||
);
|
||||
}
|
||||
}
|
||||
fn extractReg(
|
||||
alloc: std.mem.Allocator,
|
||||
io: Io,
|
||||
data: []u8,
|
||||
decomp: Decomp.Fn,
|
||||
block_size: u32,
|
||||
group: *Io.Group,
|
||||
frag_table: *Lookup.Table(Lookup.FragEntry),
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
cache: *Cache,
|
||||
err: *?Error,
|
||||
inode: Inode,
|
||||
path: []const u8,
|
||||
options: ExtractionOption,
|
||||
parent: ?*Parent,
|
||||
) Error!void {
|
||||
var blocks: usize = 0;
|
||||
var xattr_idx: u32 = 0xFFFFFFFF;
|
||||
|
||||
var ext: DataExtractor = switch (inode.data) {
|
||||
.file => |f| blk: {
|
||||
var ext: DataExtractor = .init(data, decomp, block_size, f.blocks, f.block_start, f.size);
|
||||
blocks = f.blocks.len;
|
||||
|
||||
if (f.frag_idx != 0xFFFFFFFF) {
|
||||
blocks += 1;
|
||||
const frag: Lookup.FragEntry = try frag_table.get(io, f.frag_idx);
|
||||
if (frag.size.uncompressed) {
|
||||
ext.addFrag(data[frag.block_start..][0..frag.size.size], f.frag_offset);
|
||||
} else {
|
||||
const frag_block = try cache.get(io, frag.block_start, frag.size.size);
|
||||
ext.addFrag(frag_block, f.frag_offset);
|
||||
}
|
||||
}
|
||||
break :blk ext;
|
||||
},
|
||||
.ext_file => |f| blk: {
|
||||
xattr_idx = f.xattr_idx;
|
||||
|
||||
var ext: DataExtractor = .init(data, decomp, block_size, f.blocks, f.block_start, f.size);
|
||||
blocks = f.blocks.len;
|
||||
|
||||
if (f.frag_idx != 0xFFFFFFFF) {
|
||||
blocks += 1;
|
||||
const frag: Lookup.FragEntry = try frag_table.get(io, f.frag_idx);
|
||||
if (frag.size.uncompressed) {
|
||||
ext.addFrag(data[frag.block_start..][0..frag.size.size], f.frag_offset);
|
||||
} else {
|
||||
const frag_block = try cache.get(io, frag.block_start, frag.size.size);
|
||||
ext.addFrag(frag_block, f.frag_offset);
|
||||
}
|
||||
}
|
||||
break :blk ext;
|
||||
},
|
||||
else => unreachable,
|
||||
};
|
||||
|
||||
const fin = try FileFinish.create(alloc, io, id_table, xattr_table, err, inode.hdr, path, options, parent, xattr_idx, blocks);
|
||||
|
||||
try ext.extractAsync(alloc, io, group, fin);
|
||||
}
|
||||
fn extractSymlink(alloc: std.mem.Allocator, io: Io, inode: Inode, path: []const u8, parent: ?*Parent) Error!void {
|
||||
defer if (parent != null) {
|
||||
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, .{});
|
||||
|
||||
if (parent != null)
|
||||
parent.?.finish(io);
|
||||
}
|
||||
fn extractNod(
|
||||
alloc: std.mem.Allocator,
|
||||
io: Io,
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
inode: Inode,
|
||||
path: []const u8,
|
||||
parent: ?*Parent,
|
||||
options: ExtractionOption,
|
||||
) Error!void {
|
||||
var xattr_idx: u32 = 0xFFFFFFFF;
|
||||
var device: u32 = 0;
|
||||
var mode: u32 = undefined;
|
||||
|
||||
const DT = std.os.linux.DT;
|
||||
|
||||
switch (inode.data) {
|
||||
.block_dev => |d| {
|
||||
mode = DT.BLK;
|
||||
device = d.device;
|
||||
},
|
||||
.ext_block_dev => |d| {
|
||||
mode = DT.BLK;
|
||||
device = d.device;
|
||||
xattr_idx = d.xattr_idx;
|
||||
},
|
||||
.char_dev => |d| {
|
||||
mode = DT.CHR;
|
||||
device = d.device;
|
||||
},
|
||||
.ext_char_dev => |d| {
|
||||
mode = DT.CHR;
|
||||
device = d.device;
|
||||
xattr_idx = d.xattr_idx;
|
||||
},
|
||||
.fifo => {
|
||||
mode = DT.FIFO;
|
||||
},
|
||||
.ext_fifo => |f| {
|
||||
mode = DT.FIFO;
|
||||
xattr_idx = f.xattr_idx;
|
||||
},
|
||||
.socket => {
|
||||
mode = DT.SOCK;
|
||||
},
|
||||
.ext_socket => |s| {
|
||||
mode = DT.SOCK;
|
||||
xattr_idx = s.xattr_idx;
|
||||
},
|
||||
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, device);
|
||||
if (res != 0)
|
||||
return Error.Mknod;
|
||||
|
||||
try setMetadataPath(alloc, io, inode.hdr, id_table, xattr_table, path, xattr_idx, options);
|
||||
|
||||
if (parent != null)
|
||||
try parent.?.finish(io);
|
||||
}
|
||||
|
||||
fn setMetadataPath(
|
||||
alloc: std.mem.Allocator,
|
||||
io: Io,
|
||||
hdr: Inode.Header,
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
path: []const u8,
|
||||
xattr_idx: u32,
|
||||
options: ExtractionOption,
|
||||
) Error!void {
|
||||
if (options.ignore_permissions and (options.ignore_xattr or xattr_idx == 0xFFFFFFFF)) return;
|
||||
|
||||
var file = try Io.Dir.cwd().openFile(io, path, .{});
|
||||
defer file.close(io);
|
||||
|
||||
return setMetadata(alloc, io, hdr, id_table, xattr_table, file, xattr_idx, options);
|
||||
}
|
||||
fn setMetadata(
|
||||
alloc: std.mem.Allocator,
|
||||
io: Io,
|
||||
hdr: Inode.Header,
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
file: Io.File,
|
||||
xattr_idx: u32,
|
||||
options: ExtractionOption,
|
||||
) Error!void {
|
||||
if (options.ignore_permissions and (options.ignore_xattr or xattr_idx == 0xFFFFFFFF)) return;
|
||||
|
||||
if (!options.ignore_xattr and xattr_idx != 0xFFFFFFFF) {
|
||||
var xattr = try xattr_table.get(alloc, io, 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.SetXattr;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.ignore_permissions) return;
|
||||
|
||||
try file.setTimestamps(io, .{
|
||||
.modify_timestamp = .init(
|
||||
Io.Timestamp.fromNanoseconds(@as(i96, @intCast(hdr.mod_time)) * std.time.ns_per_s),
|
||||
),
|
||||
});
|
||||
try file.setPermissions(io, @enumFromInt(hdr.permissions));
|
||||
try file.setOwner(io, try id_table.get(io, hdr.uid_idx), try id_table.get(io, hdr.gid_idx));
|
||||
}
|
||||
|
||||
// Types
|
||||
|
||||
pub const Error = error{ Mknod, SetXattr } || std.mem.Allocator.Error || Io.Cancelable || Io.Reader.Error || Io.Dir.CreateDirPathError || Cache.Error ||
|
||||
Io.File.SetPermissionsError || Io.Dir.SymLinkError || Io.File.SeekError || Io.Writer.Error || Io.File.Atomic.LinkError || Io.ConcurrentError;
|
||||
|
||||
const FileFinish = struct {
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
err: *?Error,
|
||||
|
||||
inode: Inode,
|
||||
options: ExtractionOption,
|
||||
parent: ?*Parent,
|
||||
xattr_idx: u32,
|
||||
|
||||
file: Io.File.Atomic,
|
||||
|
||||
atomic: Atomic(usize),
|
||||
has_err: bool = false,
|
||||
|
||||
fn create(
|
||||
alloc: std.mem.Allocator,
|
||||
io: Io,
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
err: *?Error,
|
||||
hdr: Inode.Header,
|
||||
path: []const u8,
|
||||
options: ExtractionOption,
|
||||
parent: ?*Parent,
|
||||
xattr_idx: u32,
|
||||
blocks: usize,
|
||||
) Error!*FileFinish {
|
||||
const out = try alloc.create(FileFinish);
|
||||
out.* = .{
|
||||
.id_table = id_table,
|
||||
.xattr_table = xattr_table,
|
||||
.err = err,
|
||||
|
||||
.hdr = hdr,
|
||||
.path = path,
|
||||
.options = options,
|
||||
.parent = parent,
|
||||
.xattr_idx = xattr_idx,
|
||||
|
||||
.file = Io.Dir.cwd().createFileAtomic(io, path, .{}),
|
||||
|
||||
.atomic = .init(blocks),
|
||||
};
|
||||
return out;
|
||||
}
|
||||
|
||||
fn finish(self: *FileFinish, alloc: std.mem.Allocator, io: Io) void {
|
||||
if (self.atomic.fetchSub(1, .release) > 0) return;
|
||||
|
||||
defer {
|
||||
if (self.parent != null) {
|
||||
alloc.free(self.path);
|
||||
if (self.has_err)
|
||||
self.parent.?.errFinish(alloc, io)
|
||||
else
|
||||
self.parent.?.finish(alloc, io);
|
||||
}
|
||||
alloc.destroy(self);
|
||||
}
|
||||
|
||||
if (self.has_err) return;
|
||||
|
||||
setMetadata(alloc, io, self.hdr, self.id_table, self.xattr_table, self.file, self.xattr_idx, self.options) catch |err| {
|
||||
self.has_err = true;
|
||||
self.err.* = err;
|
||||
return;
|
||||
};
|
||||
self.file.link(io) catch |err| {
|
||||
self.has_err = true;
|
||||
self.err.* = err;
|
||||
return;
|
||||
};
|
||||
|
||||
self.file.deinit(io);
|
||||
}
|
||||
fn errFinish(self: *FileFinish, alloc: std.mem.Allocator, io: Io) void {
|
||||
self.has_err = true;
|
||||
|
||||
if (self.atomic.fetchSub(1, .release) > 0) return;
|
||||
|
||||
if (self.parent != null) {
|
||||
alloc.free(self.path);
|
||||
self.parent.?.errFinish(alloc, io);
|
||||
}
|
||||
self.file.deinit(io);
|
||||
alloc.destroy(self);
|
||||
}
|
||||
};
|
||||
const Parent = struct {
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
err: *?Error,
|
||||
|
||||
hdr: Inode.Header,
|
||||
path: []const u8,
|
||||
options: ExtractionOption,
|
||||
parent: ?*Parent,
|
||||
xattr_idx: u32,
|
||||
|
||||
atomic: Atomic(usize),
|
||||
has_err: bool = false,
|
||||
|
||||
fn create(
|
||||
alloc: std.mem.Allocator,
|
||||
id_table: *Lookup.Table(u16),
|
||||
xattr_table: *XattrTable,
|
||||
err: *?Error,
|
||||
hdr: Inode.Header,
|
||||
path: []const u8,
|
||||
options: ExtractionOption,
|
||||
parent: ?*Parent,
|
||||
xattr_idx: u32,
|
||||
num: usize,
|
||||
) Error!*Parent {
|
||||
const out = try alloc.create(Parent);
|
||||
out.* = .{
|
||||
.id_table = id_table,
|
||||
.xattr_table = xattr_table,
|
||||
.err = err,
|
||||
|
||||
.hdr = hdr,
|
||||
.path = path,
|
||||
.options = options,
|
||||
.parent = parent,
|
||||
.xattr_idx = xattr_idx,
|
||||
|
||||
.atomic = .init(num),
|
||||
};
|
||||
return out;
|
||||
}
|
||||
|
||||
fn finish(self: *Parent, alloc: std.mem.Allocator, io: Io) void {
|
||||
if (self.atomic.fetchSub(1, .release) > 0) return;
|
||||
|
||||
defer {
|
||||
if (self.parent != null) {
|
||||
alloc.free(self.path);
|
||||
if (self.has_err)
|
||||
self.parent.?.errFinish(alloc, io)
|
||||
else
|
||||
self.parent.?.finish(alloc, io);
|
||||
}
|
||||
alloc.destroy(self);
|
||||
}
|
||||
|
||||
if (self.has_err) return;
|
||||
|
||||
setMetadataPath(alloc, io, self.hdr, self.id_table, self.xattr_table, self.path, self.xattr_idx, self.options) catch |err| {
|
||||
self.has_err = true;
|
||||
self.err.* = err;
|
||||
};
|
||||
}
|
||||
fn errFinish(self: *Parent, alloc: std.mem.Allocator, io: Io) void {
|
||||
self.has_err = true;
|
||||
|
||||
if (self.atomic.fetchSub(1, .release) > 0) return;
|
||||
|
||||
if (self.parent != null) {
|
||||
alloc.free(self.path);
|
||||
self.parent.?.errFinish(alloc, io);
|
||||
}
|
||||
alloc.destroy(self);
|
||||
}
|
||||
};
|
||||
@@ -1,267 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
const Archive = @import("archive.zig");
|
||||
const Superblock = Archive.Superblock;
|
||||
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 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_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;
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
defer rdr.deinit();
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
const Decomp = @import("decomp.zig");
|
||||
const ExtractionOptions = @import("options.zig");
|
||||
const Inode = @import("inode.zig");
|
||||
const Multi = @import("extract-multi.zig");
|
||||
const Single = @import("extract-single.zig");
|
||||
const Superblock = @import("archive.zig").Superblock;
|
||||
|
||||
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);
|
||||
}
|
||||
+110
-96
@@ -1,99 +1,73 @@
|
||||
//! A squashfs file/directory. Basically a wrapper around an Inode.
|
||||
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
const Directory = @import("directory.zig");
|
||||
const Inode = @import("inode.zig");
|
||||
const MetadataReader = @import("meta_rdr.zig");
|
||||
const Decomp = @import("decomp.zig");
|
||||
const Superblock = @import("archive.zig").Superblock;
|
||||
const ExtractionOptions = @import("options.zig");
|
||||
const Extract = @import("extract.zig");
|
||||
const Directory = @import("dir.zig");
|
||||
const MetadataReader = @import("utils/meta.zig");
|
||||
const Options = @import("options.zig");
|
||||
const DataReader = @import("utils/data_reader.zig");
|
||||
const Decompress = @import("utils/decompress.zig");
|
||||
const Super = @import("archive.zig").Super;
|
||||
|
||||
const File = @This();
|
||||
|
||||
alloc: std.mem.Allocator,
|
||||
|
||||
super: Superblock,
|
||||
data: []u8,
|
||||
decomp: Decomp.Fn,
|
||||
super: Super,
|
||||
|
||||
name: []const u8,
|
||||
inode: Inode,
|
||||
|
||||
/// The given allocator should have been used to create the name and inode.
|
||||
pub fn init(alloc: std.mem.Allocator, super: Superblock, data: []u8, decomp: Decomp.Fn, name: []const u8, inode: Inode) File {
|
||||
pub fn copy(self: File, alloc: std.mem.Allocator) !File {
|
||||
const new_name = try alloc.dupe(u8, self.name);
|
||||
errdefer alloc.free(new_name);
|
||||
|
||||
return .{
|
||||
.alloc = alloc,
|
||||
|
||||
.super = super,
|
||||
.data = data,
|
||||
.decomp = decomp,
|
||||
.data = self.data,
|
||||
.super = self.super,
|
||||
|
||||
.name = name,
|
||||
.inode = inode,
|
||||
.name = new_name,
|
||||
.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 {
|
||||
self.alloc.free(self.name);
|
||||
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.
|
||||
/// If the given filepath resolves to the calling File ("" or "."), a copy of the File is returned.
|
||||
pub fn open(self: File, alloc: std.mem.Allocator, filepath: []const u8) !File {
|
||||
var size: u64 = 0;
|
||||
var meta: MetadataReader = switch (self.inode.data) {
|
||||
.dir => |d| blk: {
|
||||
size = d.size;
|
||||
|
||||
var meta: MetadataReader = .init(alloc, self.data, self.decomp, self.super.dir_start + d.block_start);
|
||||
try meta.interface.discardAll(d.block_offset);
|
||||
break :blk meta;
|
||||
},
|
||||
.ext_dir => |d| blk: {
|
||||
size = d.size;
|
||||
|
||||
var meta: MetadataReader = .init(alloc, self.data, self.decomp, self.super.dir_start + d.block_start);
|
||||
try meta.interface.discardAll(d.block_offset);
|
||||
break :blk meta;
|
||||
},
|
||||
else => return error.NotDirectory,
|
||||
/// Reader interface to read a regular file's contents.
|
||||
pub fn reader(self: File, alloc: std.mem.Allocator) !DataReader {
|
||||
return switch (self.inode.header.type) {
|
||||
.file => |f| .init(
|
||||
alloc,
|
||||
self.data,
|
||||
self.super.decomp_fn,
|
||||
self.super.block_size,
|
||||
f.start,
|
||||
f.size,
|
||||
f.blocks,
|
||||
),
|
||||
.ext_file => |f| .init(
|
||||
alloc,
|
||||
self.data,
|
||||
self.super.decomp_fn,
|
||||
self.super.block_size,
|
||||
f.start,
|
||||
f.size,
|
||||
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, "/");
|
||||
|
||||
@@ -101,44 +75,84 @@ pub fn open(self: File, alloc: std.mem.Allocator, filepath: []const u8) !File {
|
||||
return self.copy(alloc);
|
||||
|
||||
const first_element: []const u8 = std.mem.sliceTo(path, '/');
|
||||
const final = first_element.len == path.len;
|
||||
|
||||
const file: File = blk: {
|
||||
var directory: Directory = try .init(alloc, &meta.interface, size);
|
||||
defer directory.deinit(alloc);
|
||||
var dir_rdr: MetadataReader = .init(alloc, self.data[self.inode.data.dir.start + self.super.dir_table_start ..], self.super.decomp_fn);
|
||||
try dir_rdr.interface.discardAll(self.inode.data.dir.offset);
|
||||
|
||||
var entries = directory.entries;
|
||||
var idx = entries.len / 2;
|
||||
const dir_size = self.inode.data.dir.size;
|
||||
|
||||
while (entries.len > 0) {
|
||||
switch (std.mem.order(u8, first_element, entries[idx].name)) {
|
||||
const entry: Directory.Entry = blk: {
|
||||
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,
|
||||
.lt => entries = entries[0..idx],
|
||||
.gt => entries = entries[idx..],
|
||||
}
|
||||
idx = entries.len / 2;
|
||||
} else {
|
||||
return error.NotFound;
|
||||
if (!std.mem.eql(u8, first_element, entries[0].name))
|
||||
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)
|
||||
return file;
|
||||
defer file.deinit();
|
||||
if (entry.type != .dir)
|
||||
return error.NotFound;
|
||||
|
||||
return file.open(alloc, path[first_element.len..]);
|
||||
const transient_file: File = .{
|
||||
.alloc = alloc,
|
||||
|
||||
.data = self.data,
|
||||
.super = self.super,
|
||||
|
||||
.inode = try .readLocation(
|
||||
alloc,
|
||||
self.data,
|
||||
entry.start,
|
||||
entry.offset,
|
||||
self.super,
|
||||
),
|
||||
.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 ..]);
|
||||
}
|
||||
|
||||
pub fn extract(self: File, alloc: std.mem.Allocator, io: Io, location: []const u8, options: ExtractionOptions) !void {
|
||||
return Extract.extract(
|
||||
alloc,
|
||||
io,
|
||||
self.super,
|
||||
self.data,
|
||||
self.decomp,
|
||||
self.inode,
|
||||
location,
|
||||
options,
|
||||
);
|
||||
/// 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);
|
||||
}
|
||||
|
||||
@@ -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
@@ -1,75 +1,73 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
const Reader = Io.Reader;
|
||||
|
||||
const Decomp = @import("decomp.zig");
|
||||
const Directory = @import("directory.zig");
|
||||
const MetadataReader = @import("meta_rdr.zig");
|
||||
const Superblock = @import("archive.zig").Superblock;
|
||||
const Extract = @import("extract.zig");
|
||||
const ExtractionOptions = @import("options.zig");
|
||||
const util = @import("utils/util.zig");
|
||||
|
||||
const MetadataReader = @import("utils/meta.zig");
|
||||
const Super = @import("archive.zig").Super;
|
||||
|
||||
const Inode = @This();
|
||||
|
||||
hdr: Header,
|
||||
header: Header,
|
||||
data: Data,
|
||||
|
||||
pub fn init(alloc: std.mem.Allocator, block_size: u32, rdr: *Reader) !Inode {
|
||||
var hdr: Header = undefined;
|
||||
try rdr.readSliceEndian(Header, @ptrCast(&hdr), .little);
|
||||
pub fn readLocation(alloc: std.mem.Allocator, data: []u8, start: u32, offset: u16, super: Super) !Inode {
|
||||
var meta: MetadataReader = .init(alloc, data[super.inode_table_start + start ..], super.decomp_fn);
|
||||
try meta.interface.discardAll(offset);
|
||||
|
||||
const data: Data = switch (hdr.type) {
|
||||
.dir => .{ .dir = try .init(rdr) },
|
||||
.ext_dir => .{ .ext_dir = try .init(rdr) },
|
||||
.file => .{ .file = try .init(alloc, rdr, block_size) },
|
||||
.ext_file => .{ .ext_file = try .init(alloc, rdr, block_size) },
|
||||
.symlink => .{ .symlink = try .init(alloc, rdr) },
|
||||
.ext_symlink => .{ .ext_symlink = try .init(alloc, rdr) },
|
||||
.block_dev => .{ .block_dev = try .init(rdr) },
|
||||
.ext_block_dev => .{ .ext_block_dev = try .init(rdr) },
|
||||
.char_dev => .{ .char_dev = try .init(rdr) },
|
||||
.ext_char_dev => .{ .ext_char_dev = try .init(rdr) },
|
||||
.fifo => .{ .fifo = try .init(rdr) },
|
||||
.ext_fifo => .{ .ext_fifo = try .init(rdr) },
|
||||
.socket => .{ .socket = try .init(rdr) },
|
||||
.ext_socket => .{ .ext_socket = try .init(rdr) },
|
||||
return read(alloc, &meta.interface, super.block_size);
|
||||
}
|
||||
pub fn read(alloc: std.mem.Allocator, rdr: *Io.Reader, block_size: u32) !Inode {
|
||||
const hdr = try util.readValueRdr(Header, rdr);
|
||||
|
||||
return .{
|
||||
.header = hdr,
|
||||
.data = switch (hdr.type) {
|
||||
.dir => .{ .dir = try .readBasic(rdr) },
|
||||
.ext_dir => .{ .dir = try .readExt(rdr) },
|
||||
.file => .{ .file = try .readBasic(rdr, alloc, block_size) },
|
||||
.ext_file => .{ .file = try .readExt(rdr, alloc, block_size) },
|
||||
.symlink, .ext_symlink => .{ .symlink = try .read(rdr, alloc) },
|
||||
.block_dev, .char_dev => .{ .dev = try .readBasic(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 {
|
||||
var meta: MetadataReader = .init(alloc, data, decomp, inode_start + ref.block_start);
|
||||
try meta.interface.discardAll(ref.block_offset);
|
||||
pub fn copy(self: Inode, alloc: std.mem.Allocator) !Inode {
|
||||
var new_inode = self;
|
||||
|
||||
return .init(alloc, block_size, &meta.interface);
|
||||
}
|
||||
pub fn initEntry(alloc: std.mem.Allocator, data: []u8, decomp: Decomp.Fn, inode_start: u64, block_size: u32, entry: Directory.Entry) !Inode {
|
||||
var meta: MetadataReader = .init(alloc, data, decomp, inode_start + entry.block_start);
|
||||
try meta.interface.discardAll(entry.block_offset);
|
||||
switch (new_inode.data) {
|
||||
.file => |*f| f.blocks = try alloc.dupe(BlockSize, f.blocks),
|
||||
.symlink => |*f| f.target = try alloc.dupe(u8, f.target),
|
||||
else => {},
|
||||
}
|
||||
|
||||
return .init(alloc, block_size, &meta.interface);
|
||||
return new_inode;
|
||||
}
|
||||
pub fn deinit(self: Inode, alloc: std.mem.Allocator) void {
|
||||
switch (self.data) {
|
||||
.file => |f| alloc.free(f.blocks),
|
||||
.ext_file => |f| alloc.free(f.blocks),
|
||||
.symlink => |s| alloc.free(s.target),
|
||||
.ext_symlink => |s| alloc.free(s.target),
|
||||
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
|
||||
|
||||
pub const Ref = packed struct(u64) {
|
||||
block_offset: u16,
|
||||
block_start: u32,
|
||||
pub const Reference = packed struct(u64) {
|
||||
offset: u16,
|
||||
start: u32,
|
||||
_: u16,
|
||||
};
|
||||
|
||||
pub const BlockSize = packed struct(u32) {
|
||||
size: u23,
|
||||
uncompressed: bool,
|
||||
_: u8,
|
||||
};
|
||||
|
||||
pub const Type = enum(u16) {
|
||||
dir = 1,
|
||||
file,
|
||||
@@ -89,208 +87,216 @@ pub const Type = enum(u16) {
|
||||
|
||||
pub const Header = extern struct {
|
||||
type: Type,
|
||||
permissions: u16,
|
||||
permission: u16,
|
||||
uid_idx: u16,
|
||||
gid_idx: u16,
|
||||
mod_time: u32,
|
||||
num: u32,
|
||||
inode_num: u32,
|
||||
};
|
||||
|
||||
pub const Data = union(Type) {
|
||||
pub const Data = union(enum) {
|
||||
dir: Dir,
|
||||
file: File,
|
||||
symlink: Symlink,
|
||||
block_dev: Dev,
|
||||
char_dev: Dev,
|
||||
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,
|
||||
dev: Dev,
|
||||
fifo: Fifo,
|
||||
};
|
||||
|
||||
pub const DataBlock = packed struct(u32) {
|
||||
size: u24,
|
||||
uncompressed: bool,
|
||||
_: u7,
|
||||
};
|
||||
// Inode data types
|
||||
|
||||
pub const Dir = extern struct {
|
||||
block_start: u32,
|
||||
hard_links: u32,
|
||||
size: u16,
|
||||
block_offset: u16,
|
||||
parent_num: u32,
|
||||
pub const Dir = struct {
|
||||
//RegularDir structure:
|
||||
// start: u32,
|
||||
// hard_links: u32,
|
||||
// size: u16,
|
||||
// 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,
|
||||
size: u32,
|
||||
block_start: u32,
|
||||
parent_num: u32,
|
||||
idx_count: u16,
|
||||
block_offset: u16,
|
||||
xattr_idx: u32,
|
||||
start: u32,
|
||||
offset: u16,
|
||||
xattr_idx: ?u32,
|
||||
|
||||
fn init(rdr: *Reader) !ExtDir {
|
||||
var new: ExtDir = undefined;
|
||||
try rdr.readSliceEndian(ExtDir, @ptrCast(&new), .little);
|
||||
return new;
|
||||
pub fn readBasic(rdr: *Io.Reader) !Dir {
|
||||
var raw: [12]u8 = undefined;
|
||||
try rdr.readSliceAll(&raw);
|
||||
|
||||
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 {
|
||||
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_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,
|
||||
sparse: u64,
|
||||
hard_links: u32,
|
||||
frag_idx: u32,
|
||||
frag_offset: u32,
|
||||
xattr_idx: u32,
|
||||
blocks: []DataBlock,
|
||||
blocks: []BlockSize,
|
||||
xattr_idx: ?u32,
|
||||
|
||||
fn init(alloc: std.mem.Allocator, rdr: *Reader, block_size: u32) !ExtFile {
|
||||
var data: [40]u8 = undefined;
|
||||
try rdr.readSliceAll(&data);
|
||||
pub fn readBasic(rdr: *Io.Reader, alloc: std.mem.Allocator, block_size: u32) !File {
|
||||
var raw: [16]u8 = undefined;
|
||||
try rdr.readSliceAll(&raw);
|
||||
|
||||
const frag_idx = std.mem.readInt(u32, data[28..32], .little);
|
||||
const size = std.mem.readInt(u64, data[8..16], .little);
|
||||
const frag_idx = util.readValue(u32, raw[4..8]);
|
||||
const size = util.readValue(u32, raw[12..16]);
|
||||
|
||||
var blocks_num = size / block_size;
|
||||
if (size % block_size != 0 and frag_idx == 0xFFFFFFFF)
|
||||
blocks_num += 1;
|
||||
const block_num = if (frag_idx == 0xFFFFFFFF)
|
||||
try std.math.divCeil(usize, size, block_size)
|
||||
else
|
||||
size / block_size;
|
||||
|
||||
const blocks = try alloc.alloc(DataBlock, blocks_num);
|
||||
errdefer alloc.free(blocks);
|
||||
try rdr.readSliceEndian(DataBlock, blocks, .little);
|
||||
const sizes = try alloc.alloc(BlockSize, block_num);
|
||||
try rdr.readSliceEndian(BlockSize, sizes, .little);
|
||||
|
||||
return .{
|
||||
.block_start = std.mem.readInt(u64, data[0..8], .little),
|
||||
.size = size,
|
||||
.sparse = std.mem.readInt(u64, data[16..24], .little),
|
||||
.hard_links = std.mem.readInt(u32, data[24..28], .little),
|
||||
.start = util.readValue(u32, raw[0..4]),
|
||||
.frag_idx = frag_idx,
|
||||
.frag_offset = std.mem.readInt(u32, data[32..36], .little),
|
||||
.xattr_idx = std.mem.readInt(u32, data[36..], .little),
|
||||
.blocks = blocks,
|
||||
.frag_offset = util.readValue(u32, raw[8..12]),
|
||||
.size = size,
|
||||
.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 {
|
||||
hard_links: u32,
|
||||
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 {
|
||||
var data: [8]u8 = undefined;
|
||||
try rdr.readSliceAll(&data);
|
||||
pub fn read(rdr: *Io.Reader, alloc: std.mem.Allocator) !Symlink {
|
||||
var raw: [8]u8 = undefined;
|
||||
try rdr.readSliceAll(&raw);
|
||||
|
||||
const target_size = std.mem.readInt(u32, data[4..], .little);
|
||||
|
||||
const target = try alloc.alloc(u8, target_size);
|
||||
const target = try alloc.alloc(u8, std.mem.readInt(u32, raw[4..], .little));
|
||||
errdefer alloc.free(target);
|
||||
|
||||
try rdr.readSliceEndian(u8, target, .little);
|
||||
|
||||
return .{
|
||||
.hard_links = std.mem.readInt(u32, data[0..4], .little),
|
||||
.hard_links = std.mem.readInt(u32, raw[0..4], .little),
|
||||
.target = target,
|
||||
};
|
||||
}
|
||||
};
|
||||
pub const ExtSymlink = struct {
|
||||
|
||||
pub const Dev = struct {
|
||||
hard_links: u32,
|
||||
target: []const u8,
|
||||
xattr_idx: u32,
|
||||
device: u32,
|
||||
// xattr_idx omitted on basic device inodes.
|
||||
xattr_idx: ?u32,
|
||||
|
||||
fn init(alloc: std.mem.Allocator, rdr: *Reader) !ExtSymlink {
|
||||
const sym: Symlink = try .init(alloc, rdr);
|
||||
errdefer alloc.free(sym.target);
|
||||
|
||||
var xattr_idx: u32 = undefined;
|
||||
try rdr.readSliceEndian(u32, @ptrCast(&xattr_idx), .little);
|
||||
pub fn readBasic(rdr: *Io.Reader) !Dev {
|
||||
var raw: [8]u8 = undefined;
|
||||
try rdr.readSliceAll(&raw);
|
||||
|
||||
return .{
|
||||
.hard_links = sym.hard_links,
|
||||
.target = sym.target,
|
||||
.xattr_idx = xattr_idx,
|
||||
.hard_links = util.readValue(u32, raw[0..4]),
|
||||
.device = util.readValue(u32, raw[4..8]),
|
||||
.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 {
|
||||
var new: Dev = undefined;
|
||||
try rdr.readSliceEndian(Dev, @ptrCast(&new), .little);
|
||||
return new;
|
||||
}
|
||||
};
|
||||
pub const ExtDev = extern struct {
|
||||
pub const Fifo = struct {
|
||||
hard_links: u32,
|
||||
device: u32,
|
||||
xattr_idx: u32,
|
||||
// Xattr_idx omitted on basic fifo inodes.
|
||||
xattr_idx: ?u32,
|
||||
|
||||
fn init(rdr: *Reader) !ExtDev {
|
||||
var new: ExtDev = undefined;
|
||||
try rdr.readSliceEndian(ExtDev, @ptrCast(&new), .little);
|
||||
return new;
|
||||
}
|
||||
};
|
||||
pub const IPC = extern struct {
|
||||
hard_links: u32,
|
||||
pub fn readBasic(rdr: *Io.Reader) !Fifo {
|
||||
var raw: [4]u8 = undefined;
|
||||
try rdr.readSliceAll(&raw);
|
||||
|
||||
fn init(rdr: *Reader) !IPC {
|
||||
var new: IPC = undefined;
|
||||
try rdr.readSliceEndian(IPC, @ptrCast(&new), .little);
|
||||
return new;
|
||||
return .{
|
||||
.hard_links = util.readValue(u32, raw[0..4]),
|
||||
.xattr_idx = null,
|
||||
};
|
||||
}
|
||||
};
|
||||
pub const ExtIPC = extern struct {
|
||||
hard_links: u32,
|
||||
xattr_idx: u32,
|
||||
pub fn readExt(rdr: *Io.Reader) !Fifo {
|
||||
var raw: [8]u8 = undefined;
|
||||
try rdr.readSliceAll(&raw);
|
||||
|
||||
fn init(rdr: *Reader) !ExtIPC {
|
||||
var new: ExtIPC = undefined;
|
||||
try rdr.readSliceEndian(ExtIPC, @ptrCast(&new), .little);
|
||||
return new;
|
||||
return .{
|
||||
.hard_links = util.readValue(u32, raw[0..4]),
|
||||
.xattr_idx = util.readValue(u32, raw[4..8]),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
+145
-49
@@ -1,83 +1,179 @@
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
const DataBlock = @import("inode.zig").DataBlock;
|
||||
const Decomp = @import("decomp.zig");
|
||||
const MetadataReader = @import("meta_rdr.zig");
|
||||
const ProtectedMap = @import("util/protected_map.zig").ProtectedMap;
|
||||
const Reference = @import("inode.zig").Reference;
|
||||
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 {
|
||||
const VALUES_PER_BLOCK = 8192 / @sizeOf(T);
|
||||
|
||||
const Self = @This();
|
||||
|
||||
const ITEMS_PER_BLOCK = 8192 / @sizeOf(T);
|
||||
|
||||
data: []u8,
|
||||
decomp: Decomp.Fn,
|
||||
|
||||
table_start: u64,
|
||||
table_num: u32,
|
||||
start: u64,
|
||||
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 .{
|
||||
.data = data,
|
||||
.decomp = decomp,
|
||||
|
||||
.table_start = table_start,
|
||||
.table_num = table_num,
|
||||
|
||||
.table = .init(alloc),
|
||||
.start = start,
|
||||
.count = count,
|
||||
.block_count = try std.math.divCeil(u32, count, ITEMS_PER_BLOCK),
|
||||
};
|
||||
}
|
||||
pub fn deinit(self: *Self) void {
|
||||
var iter = self.table.map.valueIterator();
|
||||
pub fn deinit(self: *Self, alloc: std.mem.Allocator) void {
|
||||
var iter = self.table.valueIterator();
|
||||
while (iter.next()) |block|
|
||||
alloc.free(block.*);
|
||||
|
||||
while (iter.next()) |val| {
|
||||
if (val.err != null) continue;
|
||||
|
||||
self.table.alloc.free(val.value);
|
||||
}
|
||||
self.table.deinit();
|
||||
self.table.deinit(alloc);
|
||||
}
|
||||
|
||||
pub fn get(self: *Self, io: Io, idx: u32) !T {
|
||||
const block = idx / VALUES_PER_BLOCK;
|
||||
const block_idx = idx % VALUES_PER_BLOCK;
|
||||
pub fn get(self: *Self, alloc: std.mem.Allocator, io: Io, idx: u32) !T {
|
||||
if (idx >= self.count) return error.InvalidIndex;
|
||||
|
||||
const values = try self.table.getOrPut(io, block, .{
|
||||
self.table.alloc,
|
||||
self.data,
|
||||
self.decomp,
|
||||
self.table_start,
|
||||
self.table_num,
|
||||
block,
|
||||
});
|
||||
const block_idx = idx / ITEMS_PER_BLOCK;
|
||||
const value_idx = idx % ITEMS_PER_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 {
|
||||
const offset: u64 = std.mem.readInt(u64, data[table_start + (block_idx * 8) ..][0..8], .little);
|
||||
try self.mut.lock(io);
|
||||
defer self.mut.unlock(io);
|
||||
|
||||
const block = try alloc.alloc(T, if (block_idx == (table_num - 1) / VALUES_PER_BLOCK)
|
||||
table_num % VALUES_PER_BLOCK
|
||||
else
|
||||
VALUES_PER_BLOCK);
|
||||
const start = util.readValue(u64, self.data[self.start + (block_idx * 8) ..][0..8]);
|
||||
|
||||
var meta: MetadataReader = .init(alloc, data, decomp, offset);
|
||||
try meta.interface.readSliceEndian(T, block, .little);
|
||||
const len = if (block_idx == self.block_count - 1) self.count % ITEMS_PER_BLOCK else ITEMS_PER_BLOCK;
|
||||
|
||||
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 {
|
||||
block_start: u64,
|
||||
size: DataBlock,
|
||||
_: u32,
|
||||
pub fn init(data: []u8, decomp: Decomp.Fn, start: u64) !Xattr {
|
||||
const kv_start = util.readValue(u64, data[start..][0..8]);
|
||||
const count = util.readValue(u32, data[start..][8..12]);
|
||||
|
||||
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,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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
@@ -1,30 +1,23 @@
|
||||
//! Options for file/directory extraction.
|
||||
|
||||
const std = @import("std");
|
||||
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,
|
||||
/// Where to print verbose log.
|
||||
verbose_writer: ?*Writer = null,
|
||||
|
||||
pub const default: ExtractionOptions = .{};
|
||||
pub const single_threaded_default: ExtractionOptions = .{ .single_threaded = true };
|
||||
ignore_permissions: bool = false,
|
||||
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 .{
|
||||
.verbose = true,
|
||||
.verbose_writer = wrt,
|
||||
.threads = try std.Thread.getCpuCount(),
|
||||
};
|
||||
}
|
||||
|
||||
+2
-3
@@ -1,7 +1,6 @@
|
||||
pub const Archive = @import("archive.zig");
|
||||
pub const File = @import("file.zig");
|
||||
pub const ExtractionOptions = @import("options.zig");
|
||||
pub const Options = @import("options.zig");
|
||||
|
||||
test {
|
||||
@import("std").testing.refAllDecls(@import("test.zig"));
|
||||
@import("std").testing.refAllDecls(@import("tests.zig"));
|
||||
}
|
||||
|
||||
-153
@@ -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,
|
||||
};
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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,
|
||||
};
|
||||
@@ -1,85 +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);
|
||||
const value = self.map.getPtr(key);
|
||||
self.mut.unlockShared(io);
|
||||
if (value != null) {
|
||||
if (!value.?.filled.isSet())
|
||||
try value.?.filled.wait(io);
|
||||
|
||||
if (value.?.err != null) return value.?.err.?;
|
||||
return &value.?.value;
|
||||
}
|
||||
}
|
||||
var value: *ProtectedValue = blk: {
|
||||
try self.mut.lock(io);
|
||||
defer self.mut.unlock(io);
|
||||
|
||||
const res = try self.map.getOrPut(key);
|
||||
if (res.found_existing)
|
||||
return self.getOrPut(io, key, create_fn_args);
|
||||
res.value_ptr.* = .{};
|
||||
break :blk res.value_ptr;
|
||||
};
|
||||
defer value.filled.set(io);
|
||||
|
||||
value.value = if (@TypeOf(CreateError) == void)
|
||||
@call(.auto, create_fn, create_fn_args)
|
||||
else
|
||||
@call(.auto, create_fn, create_fn_args) catch |err| {
|
||||
value.err = err;
|
||||
return err;
|
||||
};
|
||||
|
||||
return &value.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,
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
};
|
||||
@@ -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
@@ -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,
|
||||
};
|
||||
Reference in New Issue
Block a user