Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d00acb03b9 | ||
|
|
7f09a8ff69 | ||
|
|
68d1a9d26d | ||
|
|
2e4364b863 | ||
|
|
2e33e19cdd | ||
|
|
173e76469f | ||
|
|
c5d75d3839 |
+61
-20
@@ -1,50 +1,77 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Io = std.Io;
|
const Io = std.Io;
|
||||||
|
|
||||||
|
const util = @import("utils/util.zig");
|
||||||
|
|
||||||
const File = @import("file.zig");
|
const File = @import("file.zig");
|
||||||
const Inode = @import("inode.zig");
|
const Inode = @import("inode.zig");
|
||||||
const Util = @import("utils/util.zig");
|
|
||||||
const Decomp = @import("decomp.zig");
|
const Decomp = @import("decomp.zig");
|
||||||
|
|
||||||
const Options = @import("options.zig");
|
const Options = @import("options.zig");
|
||||||
|
const Decompress = @import("utils/decompress.zig");
|
||||||
|
|
||||||
const Archive = @This();
|
const Archive = @This();
|
||||||
|
|
||||||
map: Io.File.MemoryMap,
|
map: Io.File.MemoryMap,
|
||||||
|
|
||||||
super: Super,
|
super: Super,
|
||||||
|
root_inode_ref: Inode.Reference,
|
||||||
|
|
||||||
pub fn open(io: Io, file: Io.File, offset: u64) !Archive {
|
pub fn init(io: Io, file: Io.File, offset: u64) !Archive {
|
||||||
var map = try file.createMemoryMap(io, .{
|
var map = try file.createMemoryMap(io, .{
|
||||||
.len = try file.length(io) - offset,
|
.len = try file.length(io) - offset,
|
||||||
.offset = offset,
|
.offset = offset,
|
||||||
.protection = .{ .read = true },
|
.protection = .{ .read = true },
|
||||||
});
|
});
|
||||||
|
|
||||||
var superblock = Util.readValue(Superblock, map.memory[0..@sizeOf(Superblock)]);
|
var superblock = util.readValue(Superblock, map.memory[0..@sizeOf(Superblock)]);
|
||||||
const super = try superblock.check();
|
const super = try superblock.checkAndMinimize();
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.map = map,
|
.map = map,
|
||||||
|
|
||||||
.super = super,
|
.super = super,
|
||||||
|
.root_inode_ref = superblock.root_inode_ref,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
pub fn close(self: *Archive, io: Io) void {
|
pub fn deinit(self: *Archive, io: Io) void {
|
||||||
self.map.destroy(io);
|
self.map.destroy(io);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn root(self: *Archive) !File {
|
pub fn root(self: *Archive, alloc: std.mem.Allocator) !File {
|
||||||
_ = self;
|
return .{
|
||||||
return error.TODO;
|
.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 {
|
||||||
|
var root_file = try self.root(alloc);
|
||||||
|
defer root_file.deinit();
|
||||||
|
|
||||||
|
return root.open(alloc, filepath);
|
||||||
}
|
}
|
||||||
pub fn extract(self: *Archive, alloc: std.mem.Allocator, io: Io, ext_loc: []const u8, options: Options) !void {
|
pub fn extract(self: *Archive, alloc: std.mem.Allocator, io: Io, ext_loc: []const u8, options: Options) !void {
|
||||||
_ = self;
|
const root_inode: Inode = try .readLocation(
|
||||||
_ = alloc;
|
alloc,
|
||||||
_ = io;
|
self.map.memory,
|
||||||
_ = ext_loc;
|
self.root_inode_ref.start,
|
||||||
_ = options;
|
self.root_inode_ref.offset,
|
||||||
return error.TODO;
|
self.super,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (options.single_threaded)
|
||||||
|
return Decompress.single(alloc, io, self.map.memory, self.super, root_inode, ext_loc, options);
|
||||||
|
return Decompress.multi(alloc, io, self.map.memory, self.super, root_inode, ext_loc, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Superblock
|
// Superblock
|
||||||
@@ -59,11 +86,25 @@ const Superblock = extern struct {
|
|||||||
frag_count: u32,
|
frag_count: u32,
|
||||||
compression: Decomp.Enum,
|
compression: Decomp.Enum,
|
||||||
block_log: u16,
|
block_log: u16,
|
||||||
flags: u16, // TODO: break out into packed struct.
|
flags: packed struct(u16) {
|
||||||
|
inode_uncompressed: bool,
|
||||||
|
data_uncompressed: bool,
|
||||||
|
check: bool,
|
||||||
|
fragment_uncompressed: bool,
|
||||||
|
fragment_never: bool,
|
||||||
|
fragment_always: bool,
|
||||||
|
de_duplicate: bool,
|
||||||
|
exportable: bool,
|
||||||
|
xattr_uncompressed: bool,
|
||||||
|
xattr_never: bool,
|
||||||
|
compression_options: bool,
|
||||||
|
id_uncompressed: bool,
|
||||||
|
_: u4,
|
||||||
|
},
|
||||||
id_count: u16,
|
id_count: u16,
|
||||||
version_major: u16,
|
version_major: u16,
|
||||||
version_minor: u16,
|
version_minor: u16,
|
||||||
root_inode_ref: Inode.Reference, // TODO: Change to inode reference packed struct.
|
root_inode_ref: Inode.Reference,
|
||||||
size: u64,
|
size: u64,
|
||||||
id_table_start: u64,
|
id_table_start: u64,
|
||||||
xattr_table_start: u64,
|
xattr_table_start: u64,
|
||||||
@@ -72,20 +113,21 @@ const Superblock = extern struct {
|
|||||||
frag_table_start: u64,
|
frag_table_start: u64,
|
||||||
export_table_start: u64,
|
export_table_start: u64,
|
||||||
|
|
||||||
fn check(self: Superblock) !Super {
|
fn checkAndMinimize(self: Superblock) !Super {
|
||||||
if (self.magic != MAGIC)
|
if (self.magic != MAGIC)
|
||||||
return error.InvalidMagic;
|
return error.InvalidMagic;
|
||||||
if (self.version_major != 4 or self.version_minor != 0)
|
if (self.version_major != 4 or self.version_minor != 0)
|
||||||
return error.IncompatibleVersion;
|
return error.IncompatibleVersion;
|
||||||
if (std.math.log2(self.block_size) != self.block_log)
|
if (std.math.log2(self.block_size) != self.block_log)
|
||||||
return error.BadBlockLog;
|
return error.BadBlockLog;
|
||||||
|
if (self.flags.check)
|
||||||
|
return error.BadCheckFlag;
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.block_size = self.block_size,
|
.block_size = self.block_size,
|
||||||
.frag_count = self.frag_count,
|
.frag_count = self.frag_count,
|
||||||
.decomp_fn = try self.compression.func(),
|
.decomp_fn = try self.compression.func(),
|
||||||
.id_count = self.id_count,
|
.id_count = self.id_count,
|
||||||
.root_inode_ref = self.root_inode_ref,
|
|
||||||
.id_table_start = self.id_table_start,
|
.id_table_start = self.id_table_start,
|
||||||
.xattr_table_start = self.xattr_table_start,
|
.xattr_table_start = self.xattr_table_start,
|
||||||
.inode_table_start = self.inode_table_start,
|
.inode_table_start = self.inode_table_start,
|
||||||
@@ -100,7 +142,6 @@ pub const Super = struct {
|
|||||||
frag_count: u32,
|
frag_count: u32,
|
||||||
decomp_fn: Decomp.Fn,
|
decomp_fn: Decomp.Fn,
|
||||||
id_count: u16,
|
id_count: u16,
|
||||||
root_inode_ref: Inode.Reference,
|
|
||||||
id_table_start: u64,
|
id_table_start: u64,
|
||||||
xattr_table_start: u64,
|
xattr_table_start: u64,
|
||||||
inode_table_start: u64,
|
inode_table_start: u64,
|
||||||
|
|||||||
+84
-36
@@ -16,7 +16,7 @@ pub const Enum = enum(u16) {
|
|||||||
return switch (self) {
|
return switch (self) {
|
||||||
.gzip => if (build.zig_decomp) zigZlib else cZlib,
|
.gzip => if (build.zig_decomp) zigZlib else cZlib,
|
||||||
.lzma => if (build.zig_decomp) zigLzma else cLzma,
|
.lzma => if (build.zig_decomp) zigLzma else cLzma,
|
||||||
.lzo => if (build.zig_decomp) error.LzoUnsupported else cLzo,
|
.lzo => if (build.zig_decomp or !build.allow_lzo) error.LzoUnsupported else cLzo,
|
||||||
.xz => if (build.zig_decomp) zigXz else cXz,
|
.xz => if (build.zig_decomp) zigXz else cXz,
|
||||||
.lz4 => if (build.zig_decomp) error.Lz4Unsupported else cLz4,
|
.lz4 => if (build.zig_decomp) error.Lz4Unsupported else cLz4,
|
||||||
.zstd => if (build.zig_decomp) zigZstd else cZstd,
|
.zstd => if (build.zig_decomp) zigZstd else cZstd,
|
||||||
@@ -25,7 +25,7 @@ pub const Enum = enum(u16) {
|
|||||||
};
|
};
|
||||||
pub const Fn = *const fn (alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize;
|
pub const Fn = *const fn (alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize;
|
||||||
|
|
||||||
pub const Error = Io.Reader.Error;
|
pub const Error = Io.Reader.Error || std.mem.Allocator.Error;
|
||||||
|
|
||||||
// Actual decomp functions
|
// Actual decomp functions
|
||||||
|
|
||||||
@@ -45,56 +45,104 @@ fn cZlib(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
|||||||
const res = c.inflate(&stream, c.Z_FULL_FLUSH);
|
const res = c.inflate(&stream, c.Z_FULL_FLUSH);
|
||||||
if (res != c.Z_OK)
|
if (res != c.Z_OK)
|
||||||
return Error.ReadFailed;
|
return Error.ReadFailed;
|
||||||
return 0;
|
return stream.total_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn zigLzma(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
fn zigLzma(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
||||||
var rdr: Io.Reader = .fixed(in);
|
var rdr: Io.Reader = .fixed(in);
|
||||||
var decomp: std.compress.lzma.Decompress = .initOptions(&rdr, alloc, out, .{}, 0);
|
|
||||||
|
var decomp: std.compress.lzma.Decompress = .initOptions(&rdr, alloc, &[0]u8{}, .{}, 0);
|
||||||
|
defer decomp.deinit();
|
||||||
|
|
||||||
|
return decomp.reader.readSliceShort(out);
|
||||||
}
|
}
|
||||||
fn cLzma(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
fn cLzma(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
||||||
_ = alloc;
|
var stream: c.lzma_stream = .{
|
||||||
_ = in;
|
.avail_in = in.len,
|
||||||
_ = out;
|
.next_in = in.ptr,
|
||||||
return Error.ReadFailed;
|
.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(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
fn cLzo(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
||||||
_ = alloc;
|
var res = c.lzo_init();
|
||||||
_ = in;
|
if (res != c.LZO_E_OK)
|
||||||
_ = out;
|
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;
|
return Error.ReadFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn zigXz(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
fn zigXz(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
||||||
_ = alloc;
|
var rdr: Io.Reader = .fixed(in);
|
||||||
_ = in;
|
|
||||||
_ = out;
|
var decomp: std.compress.xz.Decompress = .init(&rdr, alloc, &[0]u8{});
|
||||||
return Error.ReadFailed;
|
defer decomp.deinit();
|
||||||
|
|
||||||
|
return decomp.reader.readSliceShort(out);
|
||||||
}
|
}
|
||||||
fn cXz(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
fn cXz(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
||||||
_ = alloc;
|
var stream: c.lzma_stream = .{
|
||||||
_ = in;
|
.avail_in = in.len,
|
||||||
_ = out;
|
.next_in = in.ptr,
|
||||||
return Error.ReadFailed;
|
.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(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
fn cLz4(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
||||||
_ = alloc;
|
const res = c.LZ4_decompress_safe(in.ptr, out.ptr, @intCast(in.len), @intCast(out.len));
|
||||||
_ = in;
|
if (res < 0) return Error.ReadFailed;
|
||||||
_ = out;
|
|
||||||
return Error.ReadFailed;
|
return @abs(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn zigZstd(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
fn zigZstd(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
||||||
_ = alloc;
|
var rdr: Io.Reader = .fixed(in);
|
||||||
_ = in;
|
|
||||||
_ = out;
|
const buf = try alloc.alloc(u8, 1024 * 1024);
|
||||||
return Error.ReadFailed;
|
defer alloc.free(buf);
|
||||||
|
|
||||||
|
var decomp: std.compress.zstd.Decompress = .init(&rdr, buf, .{
|
||||||
|
.window_len = 1024 * 1024,
|
||||||
|
});
|
||||||
|
|
||||||
|
return decomp.reader.readSliceShort(out);
|
||||||
}
|
}
|
||||||
fn cZstd(alloc: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
fn cZstd(_: std.mem.Allocator, in: []u8, out: []u8) Error!usize {
|
||||||
_ = alloc;
|
const res = c.ZSTD_decompress(out.ptr, out.len, in.ptr, in.len);
|
||||||
_ = in;
|
if (c.ZSTD_isError(res) == 1) {
|
||||||
_ = out;
|
std.debug.print("{s}\n", .{c.ZSTD_getErrorName(res)});
|
||||||
return Error.ReadFailed;
|
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,
|
||||||
|
};
|
||||||
+143
-1
@@ -2,15 +2,157 @@ const std = @import("std");
|
|||||||
const Io = std.Io;
|
const Io = std.Io;
|
||||||
|
|
||||||
const Inode = @import("inode.zig");
|
const Inode = @import("inode.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();
|
const File = @This();
|
||||||
|
|
||||||
alloc: std.mem.Allocator,
|
alloc: std.mem.Allocator,
|
||||||
|
|
||||||
|
data: []u8,
|
||||||
|
super: Super,
|
||||||
|
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
inode: Inode,
|
inode: Inode,
|
||||||
|
|
||||||
pub fn close(self: File) void {
|
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,
|
||||||
|
|
||||||
|
.data = self.data,
|
||||||
|
.super = self.super,
|
||||||
|
|
||||||
|
.name = new_name,
|
||||||
|
.inode = try self.inode.copy(alloc),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub fn deinit(self: File) void {
|
||||||
self.inode.deinit(self.alloc);
|
self.inode.deinit(self.alloc);
|
||||||
self.alloc.free(self.name);
|
self.alloc.free(self.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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, "/");
|
||||||
|
|
||||||
|
if (path.len == 0 or (path.len == 1 and path[0] == '.'))
|
||||||
|
return self.copy(alloc);
|
||||||
|
|
||||||
|
const first_element: []const u8 = std.mem.sliceTo(path, '/');
|
||||||
|
const final = first_element.len == path.len;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
const dir_size = self.inode.data.dir.size;
|
||||||
|
|
||||||
|
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..],
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!std.mem.eql(u8, first_element, entries[0].name))
|
||||||
|
return error.NotFound;
|
||||||
|
idx = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (entry.type != .dir)
|
||||||
|
return error.NotFound;
|
||||||
|
|
||||||
|
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 ..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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,
|
||||||
|
};
|
||||||
+234
-39
@@ -1,17 +1,55 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Io = std.Io;
|
const Io = std.Io;
|
||||||
|
|
||||||
|
const util = @import("utils/util.zig");
|
||||||
|
|
||||||
|
const MetadataReader = @import("utils/meta.zig");
|
||||||
|
const Super = @import("archive.zig").Super;
|
||||||
|
|
||||||
const Inode = @This();
|
const Inode = @This();
|
||||||
|
|
||||||
header: Header,
|
header: Header,
|
||||||
data: Data,
|
data: Data,
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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) },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub fn copy(self: Inode, alloc: std.mem.Allocator) !Inode {
|
||||||
|
var new_inode = self;
|
||||||
|
|
||||||
|
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 new_inode;
|
||||||
|
}
|
||||||
pub fn deinit(self: Inode, alloc: std.mem.Allocator) void {
|
pub fn deinit(self: Inode, alloc: std.mem.Allocator) void {
|
||||||
switch (self.data) {
|
switch (self.data) {
|
||||||
.file => |f| alloc.free(f.blocks),
|
.file => |f| alloc.free(f.blocks),
|
||||||
.ext_file => |f| alloc.free(f.blocks),
|
|
||||||
.symlink => |s| alloc.free(s.target),
|
.symlink => |s| alloc.free(s.target),
|
||||||
.ext_symlink => |s| alloc.free(s.target),
|
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,12 +58,18 @@ pub fn deinit(self: Inode, alloc: std.mem.Allocator) void {
|
|||||||
|
|
||||||
pub const Reference = packed struct(u64) {
|
pub const Reference = packed struct(u64) {
|
||||||
offset: u16,
|
offset: u16,
|
||||||
block: u32,
|
start: u32,
|
||||||
_: u16,
|
_: u16,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const BlockSize = packed struct(u32) {
|
||||||
|
size: u23,
|
||||||
|
uncompressed: bool,
|
||||||
|
_: u8,
|
||||||
|
};
|
||||||
|
|
||||||
pub const Type = enum(u16) {
|
pub const Type = enum(u16) {
|
||||||
dir,
|
dir = 1,
|
||||||
file,
|
file,
|
||||||
symlink,
|
symlink,
|
||||||
block_dev,
|
block_dev,
|
||||||
@@ -42,7 +86,7 @@ pub const Type = enum(u16) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub const Header = extern struct {
|
pub const Header = extern struct {
|
||||||
type: Type, // TODO: enum
|
type: Type,
|
||||||
permission: u16,
|
permission: u16,
|
||||||
uid_idx: u16,
|
uid_idx: u16,
|
||||||
gid_idx: u16,
|
gid_idx: u16,
|
||||||
@@ -50,58 +94,209 @@ pub const Header = extern struct {
|
|||||||
inode_num: u32,
|
inode_num: u32,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Data = union(Type) {
|
pub const Data = union(enum) {
|
||||||
dir: Dir,
|
dir: Dir,
|
||||||
file: File,
|
file: File,
|
||||||
symlink: Symlink,
|
symlink: Symlink,
|
||||||
block_dev: Dev,
|
dev: Dev,
|
||||||
char_dev: Dev,
|
|
||||||
fifo: Fifo,
|
fifo: Fifo,
|
||||||
socket: Fifo,
|
|
||||||
ext_dir: ExtDir,
|
|
||||||
ext_file: ExtFile,
|
|
||||||
ext_symlink: ExtSymlink,
|
|
||||||
ext_block_dev: ExtDev,
|
|
||||||
ext_char_dev: ExtDev,
|
|
||||||
ext_fifo: ExtFifo,
|
|
||||||
ext_socket: ExtFifo,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Inode data types
|
// Inode data types
|
||||||
|
|
||||||
pub const Dir = extern struct {
|
pub const Dir = struct {
|
||||||
block: u32,
|
//RegularDir structure:
|
||||||
hard_links: u32,
|
// start: u32,
|
||||||
size: u16,
|
// hard_links: u32,
|
||||||
offset: u16,
|
// size: u16,
|
||||||
parent_inode_num: u32,
|
// offset: u16,
|
||||||
};
|
// parent_inode_num: u32,
|
||||||
pub const ExtDir = extern struct {
|
// ExtDir structure
|
||||||
|
// hard_links: u32,
|
||||||
|
// size: u32,
|
||||||
|
// start: u32,
|
||||||
|
// parent_inode_num: u32,
|
||||||
|
// idx_count: u16,
|
||||||
|
// offset: u16,
|
||||||
|
// xattr_idx: u32,
|
||||||
|
// dir_indexes: []DirIndex,
|
||||||
|
|
||||||
hard_links: u32,
|
hard_links: u32,
|
||||||
size: u32,
|
size: u32,
|
||||||
block: u32,
|
start: u32,
|
||||||
parent_inode_num: u32,
|
|
||||||
idx_count: u16,
|
|
||||||
offset: u16,
|
offset: u16,
|
||||||
xattr_idx: u32,
|
xattr_idx: ?u32,
|
||||||
|
|
||||||
|
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 {
|
pub const File = struct {
|
||||||
blocks: []u64,
|
//RegularFile structure:
|
||||||
};
|
// start: u32,
|
||||||
pub const ExtFile = struct {
|
// frag_idx: u32,
|
||||||
blocks: []u64,
|
// 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: u64,
|
||||||
|
blocks: []BlockSize,
|
||||||
|
xattr_idx: ?u32,
|
||||||
|
|
||||||
|
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 = util.readValue(u32, raw[4..8]);
|
||||||
|
const size = util.readValue(u32, raw[12..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(u32, raw[0..4]),
|
||||||
|
.frag_idx = frag_idx,
|
||||||
|
.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 {
|
pub const Symlink = struct {
|
||||||
|
hard_links: u32,
|
||||||
target: []const u8,
|
target: []const u8,
|
||||||
};
|
// xattr_idx is ignored on extended symlinks as you can't apply them to symlinks anyway.
|
||||||
pub const ExtSymlink = struct {
|
|
||||||
target: []const u8,
|
pub fn read(rdr: *Io.Reader, alloc: std.mem.Allocator) !Symlink {
|
||||||
|
var raw: [8]u8 = undefined;
|
||||||
|
try rdr.readSliceAll(&raw);
|
||||||
|
|
||||||
|
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, raw[0..4], .little),
|
||||||
|
.target = target,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Dev = extern struct {};
|
pub const Dev = struct {
|
||||||
pub const ExtDev = extern struct {};
|
hard_links: u32,
|
||||||
|
device: u32,
|
||||||
|
// xattr_idx omitted on basic device inodes.
|
||||||
|
xattr_idx: ?u32,
|
||||||
|
|
||||||
pub const Fifo = extern struct {};
|
pub fn readBasic(rdr: *Io.Reader) !Dev {
|
||||||
pub const ExtFifo = extern struct {};
|
var raw: [8]u8 = undefined;
|
||||||
|
try rdr.readSliceAll(&raw);
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.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 Fifo = struct {
|
||||||
|
hard_links: u32,
|
||||||
|
// Xattr_idx omitted on basic fifo inodes.
|
||||||
|
xattr_idx: ?u32,
|
||||||
|
|
||||||
|
pub fn readBasic(rdr: *Io.Reader) !Fifo {
|
||||||
|
var raw: [4]u8 = undefined;
|
||||||
|
try rdr.readSliceAll(&raw);
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.hard_links = util.readValue(u32, raw[0..4]),
|
||||||
|
.xattr_idx = null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub fn readExt(rdr: *Io.Reader) !Fifo {
|
||||||
|
var raw: [8]u8 = undefined;
|
||||||
|
try rdr.readSliceAll(&raw);
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.hard_links = util.readValue(u32, raw[0..4]),
|
||||||
|
.xattr_idx = util.readValue(u32, raw[4..8]),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
+179
@@ -0,0 +1,179 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const Io = std.Io;
|
||||||
|
|
||||||
|
const Decomp = @import("decomp.zig");
|
||||||
|
const Reference = @import("inode.zig").Reference;
|
||||||
|
const MetadataReader = @import("utils/meta.zig");
|
||||||
|
const util = @import("utils/util.zig");
|
||||||
|
|
||||||
|
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 Self = @This();
|
||||||
|
|
||||||
|
const ITEMS_PER_BLOCK = 8192 / @sizeOf(T);
|
||||||
|
|
||||||
|
data: []u8,
|
||||||
|
decomp: Decomp.Fn,
|
||||||
|
|
||||||
|
start: u64,
|
||||||
|
count: u32,
|
||||||
|
block_count: u32,
|
||||||
|
|
||||||
|
table: std.hash_map.AutoHashMapUnmanaged(u32, []T) = .empty,
|
||||||
|
mut: Io.Mutex = .init,
|
||||||
|
|
||||||
|
pub fn init(data: []u8, decomp: Decomp.Fn, start: u64, count: u32) !Self {
|
||||||
|
return .{
|
||||||
|
.data = data,
|
||||||
|
.decomp = decomp,
|
||||||
|
|
||||||
|
.start = start,
|
||||||
|
.count = count,
|
||||||
|
.block_count = try std.math.divCeil(u32, count, ITEMS_PER_BLOCK),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub fn deinit(self: *Self, alloc: std.mem.Allocator) void {
|
||||||
|
var iter = self.table.valueIterator();
|
||||||
|
while (iter.next()) |block|
|
||||||
|
alloc.free(block.*);
|
||||||
|
|
||||||
|
self.table.deinit(alloc);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(self: *Self, alloc: std.mem.Allocator, io: Io, idx: u32) !T {
|
||||||
|
if (idx >= self.count) return error.InvalidIndex;
|
||||||
|
|
||||||
|
const block_idx = idx / ITEMS_PER_BLOCK;
|
||||||
|
const value_idx = idx % ITEMS_PER_BLOCK;
|
||||||
|
|
||||||
|
const init_get = self.table.get(block_idx);
|
||||||
|
if (init_get != null) return init_get.?[value_idx];
|
||||||
|
|
||||||
|
try self.mut.lock(io);
|
||||||
|
defer self.mut.unlock(io);
|
||||||
|
|
||||||
|
const start = util.readValue(u64, self.data[self.start + (block_idx * 8) ..][0..8]);
|
||||||
|
|
||||||
|
const len = if (block_idx == self.block_count - 1) self.count % ITEMS_PER_BLOCK else ITEMS_PER_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];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Xattr = struct {
|
||||||
|
kv_start: u64,
|
||||||
|
lookup: Table(XattrLookup),
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -11,7 +11,10 @@ ignore_xattr: bool = false,
|
|||||||
|
|
||||||
dereference_symlink: bool = false,
|
dereference_symlink: bool = false,
|
||||||
|
|
||||||
|
single_threaded: bool = false,
|
||||||
|
|
||||||
pub const default: Options = .{};
|
pub const default: Options = .{};
|
||||||
|
pub const single_threaded_default: Options = .{ .single_threaded = true };
|
||||||
pub fn verboseDefault(wrt: *Writer) Options {
|
pub fn verboseDefault(wrt: *Writer) Options {
|
||||||
return .{
|
return .{
|
||||||
.verbose = true,
|
.verbose = true,
|
||||||
|
|||||||
+22
-5
@@ -8,15 +8,32 @@ const Archive = @import("archive.zig");
|
|||||||
|
|
||||||
const TEST_ARCHIVE = "testing/LinuxPATest.sfs";
|
const TEST_ARCHIVE = "testing/LinuxPATest.sfs";
|
||||||
|
|
||||||
|
const OPEN_TEST_LOCATION = "PortableApps/gVimPortable/GVim-v8.2.2965.glibc2.15-x86_64.AppImage";
|
||||||
|
|
||||||
test "Open" {
|
test "Open" {
|
||||||
var archive_file = try Io.Dir.cwd().openFile(io, TEST_ARCHIVE, .{});
|
var archive_file = try Io.Dir.cwd().openFile(io, TEST_ARCHIVE, .{});
|
||||||
defer archive_file.close(io);
|
defer archive_file.close(io);
|
||||||
|
|
||||||
var arc: Archive = try .open(io, archive_file, 0);
|
var arc: Archive = try .init(io, archive_file, 0);
|
||||||
defer arc.close(io);
|
defer arc.deinit(io);
|
||||||
|
|
||||||
std.debug.print("{}\n", .{arc.super});
|
var root = try arc.root(alloc);
|
||||||
|
defer root.deinit();
|
||||||
|
|
||||||
var root = try arc.root();
|
var test_open = try root.open(alloc, OPEN_TEST_LOCATION);
|
||||||
defer root.close();
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,109 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const Io = std.Io;
|
||||||
|
|
||||||
|
const util = @import("util.zig");
|
||||||
|
|
||||||
const Decomp = @import("../decomp.zig");
|
const Decomp = @import("../decomp.zig");
|
||||||
|
|
||||||
const MetadataReader = @This();
|
const MetadataReader = @This();
|
||||||
|
|
||||||
|
alloc: std.mem.Allocator,
|
||||||
|
|
||||||
data: []u8,
|
data: []u8,
|
||||||
decomp: Decomp.Fn,
|
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,
|
||||||
|
};
|
||||||
|
|||||||
@@ -20,3 +20,9 @@ pub fn readValue(comptime T: type, bytes: []u8) T {
|
|||||||
}
|
}
|
||||||
return res;
|
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);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user