Started work on File

This commit is contained in:
Caleb Gardner
2025-05-14 06:59:56 -05:00
parent b0c71c59f8
commit 6dd3054006
8 changed files with 91 additions and 72 deletions
+14
View File
@@ -0,0 +1,14 @@
const inode = @import("inode.zig");
const Reader = @import("squashfs.zig").Reader;
const MetadataReader = @import("metadata_reader.zig").MetadataReader;
pub const File = struct {
rdr: *Reader,
inode: inode.Inode,
name: []const u8,
dir_entries: []const void = undefined, //TODO
pub fn fromRef(ref: inode.InodeRef, rdr: *Reader) !File {
var meta_rdr: MetadataReader = .init(rdr.super.comp, rdr: io.AnyReader, alloc: std.mem.Allocator)
}
};
+7
View File
@@ -0,0 +1,7 @@
const std = @import("std");
const FileOffsetReader = struct {
file: std.fs.File,
pub fn any(self: *FileOffsetReader) !std.io.AnyReader {}
};
+4 -4
View File
@@ -73,16 +73,16 @@ pub fn readInode(rdr: io.AnyReader, block_size: u32, alloc: std.mem.Allocator) !
.ext_dir = try .init(rdr, alloc),
},
.file => .{
.file = try file.readFileInode(rdr, block_size, alloc),
.file = try .init(rdr, block_size, alloc),
},
.ext_file => .{
.ext_file = try file.readExtFileInode(rdr, block_size, alloc),
.ext_file = try .init(rdr, block_size, alloc),
},
.symlink => .{
.symlink = try sym.readSymlinkInode(rdr, alloc),
.symlink = try .init(rdr, alloc),
},
.ext_symlink => .{
.ext_symlink = try sym.readExtSymlinkInode(rdr, alloc),
.ext_symlink = try .init(rdr, alloc),
},
.block_device => .{
.block_device = try rdr.readStruct(misc.DeviceInode),
+2 -4
View File
@@ -53,13 +53,11 @@ pub const ExtDirInode = struct {
.xattr_index = try rdr.readInt(u32, std.builtin.Endian.little),
.indexes = undefined,
};
var tmp = std.ArrayList(DirIndex).init(alloc);
try tmp.resize(out.dir_index_count);
out.indexes = try alloc.alloc(DirIndex, out.dir_index_count);
var i: u16 = 0;
while (i < out.dir_index_count) : (i += 1) {
tmp.items[i] = try .init(rdr, alloc);
out.indexes[i] = try .init(rdr, alloc);
}
out.indexes = tmp.items;
return out;
}
};
+6 -6
View File
@@ -6,9 +6,8 @@ pub const FileInode = struct {
frag_block_offset: u32,
size: u32,
block_sizes: []const u32,
};
pub fn readFileInode(rdr: std.io.AnyReader, block_size: u32, alloc: std.mem.Allocator) !FileInode {
pub fn init(rdr: std.io.AnyReader, block_size: u32, alloc: std.mem.Allocator) !FileInode {
var out = FileInode{
.start = try rdr.readInt(u32, std.builtin.Endian.little),
.frag_index = try rdr.readInt(u32, std.builtin.Endian.little),
@@ -23,7 +22,8 @@ pub fn readFileInode(rdr: std.io.AnyReader, block_size: u32, alloc: std.mem.Allo
out.block_sizes = try alloc.alloc(u32, block_num);
_ = try rdr.readAll(std.mem.asBytes(&out.block_sizes));
return out;
}
}
};
pub const ExtFileInode = struct {
start: u64,
@@ -34,9 +34,8 @@ pub const ExtFileInode = struct {
frag_block_offset: u32,
xattr_index: u32,
block_sizes: []const u32,
};
pub fn readExtFileInode(rdr: std.io.AnyReader, block_size: u32, alloc: std.mem.Allocator) !ExtFileInode {
pub fn init(rdr: std.io.AnyReader, block_size: u32, alloc: std.mem.Allocator) !ExtFileInode {
var out = ExtFileInode{
.start = try rdr.readInt(u64, std.builtin.Endian.little),
.size = try rdr.readInt(u64, std.builtin.Endian.little),
@@ -54,4 +53,5 @@ pub fn readExtFileInode(rdr: std.io.AnyReader, block_size: u32, alloc: std.mem.A
out.block_sizes = try alloc.alloc(u32, block_num);
_ = try rdr.readAll(std.mem.asBytes(&out.block_sizes));
return out;
}
}
};
+6 -6
View File
@@ -5,9 +5,8 @@ pub const SymlinkInode = struct {
hard_links: u32,
target_size: u32,
path: []u8,
};
pub fn readSymlinkInode(rdr: io.AnyReader, alloc: std.mem.Allocator) !SymlinkInode {
pub fn init(rdr: io.AnyReader, alloc: std.mem.Allocator) !SymlinkInode {
var out = SymlinkInode{
.hard_links = try rdr.readInt(u32, std.builtin.Endian.little),
.target_size = try rdr.readInt(u32, std.builtin.Endian.little),
@@ -16,16 +15,16 @@ pub fn readSymlinkInode(rdr: io.AnyReader, alloc: std.mem.Allocator) !SymlinkIno
out.path = try alloc.alloc(u8, out.target_size + 1);
_ = try rdr.readAll(out.path);
return out;
}
}
};
pub const ExtSymlinkInode = struct {
hard_links: u32,
target_size: u32,
path: []u8,
xattr_index: u32,
};
pub fn readExtSymlinkInode(rdr: io.AnyReader, alloc: std.mem.Allocator) !ExtSymlinkInode {
pub fn init(rdr: io.AnyReader, alloc: std.mem.Allocator) !ExtSymlinkInode {
var out = ExtSymlinkInode{
.hard_links = try rdr.readInt(u32, std.builtin.Endian.little),
.target_size = try rdr.readInt(u32, std.builtin.Endian.little),
@@ -36,4 +35,5 @@ pub fn readExtSymlinkInode(rdr: io.AnyReader, alloc: std.mem.Allocator) !ExtSyml
_ = try rdr.readAll(out.path);
out.xattr_index = try rdr.readInt(u32, std.builtin.Endian.little);
return out;
}
}
};
+5 -4
View File
@@ -4,23 +4,24 @@ const fs = std.fs;
const Superblock = @import("superblock.zig").Superblock;
const inode = @import("inode.zig");
const MetadataReader = @import("metadata_reader.zig").MetadataReader;
const File = @import("file.zig").File;
pub const Reader = struct {
super: Superblock,
rdr: fs.File,
root: inode.Inode,
alloc: std.heap.ArenaAllocator,
root: File,
alloc: std.heap.GeneralPurposeAllocator(.{}),
pub fn deinit(self: *Reader) void {
self.rdr.close();
self.alloc.deinit();
// _ = self.alloc.deinit();
}
};
pub fn newReader(filename: []const u8) !Reader {
const file = try std.fs.cwd().openFile(filename, .{});
errdefer file.close();
var alloc: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
var alloc: std.heap.GeneralPurposeAllocator(.{}) = .init;
errdefer _ = alloc.deinit();
const super = try file.reader().readStruct(Superblock);
try super.valid();
-1
View File
@@ -8,5 +8,4 @@ const testFileName = "testing/LinuxPATest.sfs";
test "open test file" {
var reader = try squashfs.newReader(testFileName);
defer reader.deinit();
print("{}\n", .{reader.root});
}