Started on zig-unquashfs

This commit is contained in:
Caleb Gardner
2025-05-21 20:49:13 -05:00
parent e91d75458e
commit 213dfa8b92
3 changed files with 58 additions and 30 deletions
+11
View File
@@ -14,6 +14,17 @@ pub fn build(b: *std.Build) void {
.root_module = lib_mod, .root_module = lib_mod,
}); });
b.installArtifact(lib); b.installArtifact(lib);
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.linkage = .static,
.name = "unsquashfs",
.root_module = exe_mod,
});
b.installArtifact(exe);
const lib_unit_tests = b.addTest(.{ const lib_unit_tests = b.addTest(.{
.root_module = lib_mod, .root_module = lib_mod,
}); });
+27 -30
View File
@@ -23,6 +23,25 @@ pub const File = struct {
NotFound, NotFound,
}; };
fn fromDirEntry(read: *Reader, ent: DirEntry) !File {
var offset_rdr = read.holder.readerAt(ent.block_start + read.super.inode_table_start);
var meta_rdr: MetadataReader = try .init(
read.alloc,
offset_rdr.any(),
read.super.decomp,
);
defer meta_rdr.deinit();
try meta_rdr.skip(ent.offset);
return .{
.name = ent.name,
.inode = try .init(
read.alloc,
meta_rdr.any(),
read.super.block_size,
),
};
}
pub fn deinit(self: *File, alloc: std.mem.Allocator) void { pub fn deinit(self: *File, alloc: std.mem.Allocator) void {
self.inode.deinit(); self.inode.deinit();
alloc.free(self.name); alloc.free(self.name);
@@ -56,7 +75,7 @@ pub const File = struct {
if (ent == null) { if (ent == null) {
return FileError.NotFound; return FileError.NotFound;
} }
var fil = try fileFromDirEntry(reader, ent.?); var fil = try fromDirEntry(reader, ent.?);
return fil.realOpen(reader, clean_path[split_idx..], false); return fil.realOpen(reader, clean_path[split_idx..], false);
} }
@@ -68,6 +87,13 @@ pub const File = struct {
}; };
} }
pub fn iterator(self: *File, read: *Reader) !FileIterator {
switch (self.inode.header.inode_type){
.dir, ext_dir => {}
else => return FileError.NotDirectory,
}
}
fn readDirEntries(self: *File, reader: *Reader) !void { fn readDirEntries(self: *File, reader: *Reader) !void {
if (self.dirEntries != null) return; if (self.dirEntries != null) return;
var block_start: u32 = 0; var block_start: u32 = 0;
@@ -104,32 +130,3 @@ pub const File = struct {
return self.data_rdr.?.read(bytes); return self.data_rdr.?.read(bytes);
} }
}; };
fn fileFromDirEntry(read: *Reader, ent: DirEntry) !File {
var offset_rdr = read.holder.readerAt(ent.block_start + read.super.inode_table_start);
var meta_rdr: MetadataReader = .init(
read.alloc,
read.super.decomp,
offset_rdr.any(),
);
defer meta_rdr.deinit();
try meta_rdr.skip(ent.offset);
// Copy name so we can clean-up the DirEntrys without causing issues.
const name = try read.alloc.alloc(u8, ent.name.len);
errdefer read.alloc.free(name);
@memcpy(name, ent.name);
var out: File = .{
.name = name,
.inode = try .init(
read.alloc,
meta_rdr.any(),
read.super.block_size,
),
};
errdefer out.deinit(read.alloc);
out.data_rdr = switch (out.inode.data) {
.file, .ext_file => try .init(&out, read),
else => null,
};
return out;
}
+20
View File
@@ -0,0 +1,20 @@
const std = @import("std");
const Reader = @import("reader.zig");
const stdout = std.io.getStdOut();
pub fn main() !void {
const alloc: std.heap.GeneralPurposeAllocator(.{}) = .init();
const args = try std.process.argsWithAllocator(alloc.allocator());
defer args.deinit();
while (args.next()) |arg| {
if (std.mem.eql(u8, arg, "--help")) {
help();
return;
}
}
//TODO
}
fn help() void {}