aboutsummaryrefslogtreecommitdiff
path: root/src/lib/resources.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/resources.zig')
-rw-r--r--src/lib/resources.zig25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/lib/resources.zig b/src/lib/resources.zig
index 93e9f04..0c87ede 100644
--- a/src/lib/resources.zig
+++ b/src/lib/resources.zig
@@ -20,6 +20,11 @@ const tar = std.tar;
const File = tar.Iterator(io.FixedBufferStream([]const u8).Reader).File;
+const FileContext = struct {
+ buffer: []const u8,
+ fbs: io.FixedBufferStream([]const u8),
+};
+
const iofs = struct {
const debug = struct {
fn write(ptr: [*]const u8, len: usize) callconv(.C) Result(usize) {
@@ -271,8 +276,14 @@ fn provideProcessSelf() !void {
fn addFile(path: []const u8, file: File) !void {
const allocator = vfs.treeRoot().allocator;
- const initializer = try allocator.create(File);
- initializer.* = file;
+ const buffer = try allocator.alloc(u8, file.size);
+ try file.reader().readNoEof(buffer);
+
+ const initializer = try allocator.create(FileContext);
+ initializer.* = .{
+ .buffer = buffer,
+ .fbs = io.fixedBufferStream(@as([]const u8, buffer)),
+ };
try vfs.provideResource(path, .{
.tag = .file,
@@ -306,9 +317,9 @@ fn open(context: *vfs.FileContext) !void {
const allocator = vfs.treeRoot().allocator;
const inner = context.inner orelse return Error.NoTarFileInitializer;
- const old_context: *File = @alignCast(@ptrCast(inner));
+ const old_context: *FileContext = @alignCast(@ptrCast(inner));
- const new_context = try allocator.create(File);
+ const new_context = try allocator.create(FileContext);
new_context.* = old_context.*;
context.inner = new_context;
@@ -321,17 +332,17 @@ fn read(context: *vfs.FileContext, ptr: [*]u8, len: usize) callconv(.C) Result(u
};
}
- const inner: *File = @alignCast(@ptrCast(context.inner.?));
+ const inner: *FileContext = @alignCast(@ptrCast(context.inner.?));
const buffer = ptr[0..len];
- return Result(usize).fromAnyTypeOrError(inner.read(buffer));
+ return Result(usize).fromAnyTypeOrError(inner.fbs.read(buffer));
}
fn close(context: *vfs.FileContext) callconv(.C) void {
if (context.inner) |inner_opaque| {
const allocator = vfs.treeRoot().allocator;
- const inner: *File = @alignCast(@ptrCast(inner_opaque));
+ const inner: *FileContext = @alignCast(@ptrCast(inner_opaque));
allocator.destroy(inner);
}
}