Skip to content

Commit 0db84d3

Browse files
author
Felix "xq" Queißner
committed
Makes --size unconditional, removes guessing disk size
1 parent c051270 commit 0db84d3

File tree

6 files changed

+13
-99
lines changed

6 files changed

+13
-99
lines changed

justfile

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ behaviour-tests: \
2323
(behaviour-test "tests/part/mbr/basic-single-part-unsized.dis")
2424

2525
behaviour-test script: install
26-
./zig-out/bin/dim --output .zig-cache/disk.img --script "{{script}}"
2726
./zig-out/bin/dim --output .zig-cache/disk.img --script "{{script}}" --size 30M
2827

2928

src/components/EmptyData.zig

+2-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@ const dim = @import("../dim.zig");
99
const EmptyData = @This();
1010

1111
pub fn parse(ctx: dim.Context) !dim.Content {
12-
const pf = try ctx.alloc_object(EmptyData);
13-
pf.* = .{};
14-
return .create_handle(pf, .create(@This(), .{
15-
.guess_size_fn = guess_size,
12+
_ = ctx;
13+
return .create_handle(undefined, .create(@This(), .{
1614
.render_fn = render,
1715
}));
1816
}
1917

20-
fn guess_size(_: *EmptyData) dim.Content.GuessError!dim.SizeGuess {
21-
return .{ .at_least = 0 };
22-
}
23-
2418
fn render(self: *EmptyData, stream: *dim.BinaryStream) dim.Content.RenderError!void {
2519
_ = self;
2620
_ = stream;

src/components/FillData.zig

-5
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ pub fn parse(ctx: dim.Context) !dim.Content {
1515
.fill_value = try ctx.parse_integer(u8, 0),
1616
};
1717
return .create_handle(pf, .create(@This(), .{
18-
.guess_size_fn = guess_size,
1918
.render_fn = render,
2019
}));
2120
}
2221

23-
fn guess_size(_: *FillData) dim.Content.GuessError!dim.SizeGuess {
24-
return .{ .at_least = 0 };
25-
}
26-
2722
fn render(self: *FillData, stream: *dim.BinaryStream) dim.Content.RenderError!void {
2823
try stream.writer().writeByteNTimes(
2924
self.fill_value,

src/components/PasteFile.zig

-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@ pub fn parse(ctx: dim.Context) !dim.Content {
1111
.file_handle = try ctx.parse_file_name(),
1212
};
1313
return .create_handle(pf, .create(@This(), .{
14-
.guess_size_fn = guess_size,
1514
.render_fn = render,
1615
}));
1716
}
1817

19-
fn guess_size(self: *PasteFile) dim.Content.GuessError!dim.SizeGuess {
20-
const size = try self.file_handle.get_size();
21-
22-
return .{ .exact = size };
23-
}
24-
2518
fn render(self: *PasteFile, stream: *dim.BinaryStream) dim.Content.RenderError!void {
2619
try self.file_handle.copy_to(stream);
2720
}

src/components/part/MbrPartitionTable.zig

-30
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub fn parse(ctx: dim.Context) !dim.Content {
6161
}
6262

6363
return .create_handle(pf, .create(PartTable, .{
64-
.guess_size_fn = guess_size,
6564
.render_fn = render,
6665
}));
6766
}
@@ -105,35 +104,6 @@ fn parse_partition(ctx: dim.Context) !Partition {
105104
return part;
106105
}
107106

108-
fn guess_size(self: *PartTable) dim.Content.GuessError!dim.SizeGuess {
109-
var upper_bound: u64 = 512;
110-
var all_parts_bounded = true;
111-
112-
for (self.partitions) |mpart| {
113-
const part = mpart orelse continue;
114-
115-
if (part.offset != null and part.size != null) {
116-
upper_bound = @max(upper_bound, part.offset.? + part.size.?);
117-
} else {
118-
all_parts_bounded = false;
119-
}
120-
}
121-
if (all_parts_bounded)
122-
return .{ .exact = upper_bound };
123-
124-
for (self.partitions) |mpart| {
125-
const part = mpart orelse continue;
126-
127-
if (part.offset != null and part.size != null) {
128-
upper_bound = @max(upper_bound, part.offset.? + part.size.?);
129-
} else {
130-
all_parts_bounded = false;
131-
}
132-
}
133-
134-
@panic("not implemented yet!");
135-
}
136-
137107
fn render(self: *PartTable, stream: *dim.BinaryStream) dim.Content.RenderError!void {
138108
_ = self;
139109
_ = stream;

src/dim.zig

+11-48
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const max_script_size = 10 * DiskSize.MiB;
1111

1212
const Options = struct {
1313
output: ?[]const u8 = null,
14-
size: ?DiskSize = null,
14+
size: DiskSize = DiskSize.empty,
1515
script: ?[]const u8 = null,
1616
@"import-env": bool = false,
1717
};
@@ -79,6 +79,11 @@ pub fn main() !u8 {
7979
if (bad_args)
8080
return 1;
8181

82+
const size_limit: u64 = options.size.size_in_bytes();
83+
if (size_limit == 0) {
84+
return fatal("--size must be given!");
85+
}
86+
8287
var current_dir = try std.fs.cwd().openDir(".", .{});
8388
defer current_dir.close();
8489

@@ -122,21 +127,14 @@ pub fn main() !u8 {
122127
return 1;
123128
}
124129

125-
const root_size_estimation = try root_content.guess_required_size();
126-
std.log.info("root size: {}", .{root_size_estimation});
127-
128130
{
129131
var output_file = try current_dir.atomicFile(output_path, .{});
130132
defer output_file.deinit();
131133

132-
const size_limit: ?u64 = if (options.size) |disk_size| blk: {
133-
try output_file.file.setEndPos(disk_size.size_in_bytes());
134-
135-
break :blk disk_size.size_in_bytes();
136-
} else null;
134+
try output_file.file.setEndPos(size_limit);
137135

138136
var stream = BinaryStream{
139-
.capacity = size_limit orelse 0,
137+
.capacity = size_limit,
140138
};
141139

142140
try root_content.render(&stream);
@@ -346,23 +344,7 @@ pub const Content = struct {
346344
obj: *anyopaque,
347345
vtable: *const VTable,
348346

349-
pub const empty: Content = .{
350-
.obj = undefined,
351-
.vtable = &emptyVTable,
352-
};
353-
354-
const emptyVTable: VTable = blk: {
355-
const Wrap = struct {
356-
fn render(_: *anyopaque, _: *BinaryStream) RenderError!void {}
357-
fn guess_size_fn(_: *anyopaque) GuessError!SizeGuess {
358-
return .{ .exact = 0 };
359-
}
360-
};
361-
break :blk .{
362-
.render_fn = Wrap.render,
363-
.guess_size_fn = Wrap.guess_size_fn,
364-
};
365-
};
347+
pub const empty: Content = @import("components/EmptyData.zig").parse(undefined) catch unreachable;
366348

367349
pub fn create_handle(obj: *anyopaque, vtable: *const VTable) Content {
368350
return .{ .obj = obj, .vtable = vtable };
@@ -373,22 +355,13 @@ pub const Content = struct {
373355
try content.vtable.render_fn(content.obj, stream);
374356
}
375357

376-
/// Attempts to determine the required size of the content.
377-
///
378-
/// This may not be an exact guess, so the result can have
379-
pub fn guess_required_size(content: Content) GuessError!SizeGuess {
380-
return try content.vtable.guess_size_fn(content.obj);
381-
}
382-
383358
pub const VTable = struct {
384359
render_fn: *const fn (*anyopaque, *BinaryStream) RenderError!void,
385-
guess_size_fn: *const fn (*anyopaque) GuessError!SizeGuess,
386360

387361
pub fn create(
388362
comptime Container: type,
389363
comptime funcs: struct {
390364
render_fn: *const fn (*Container, *BinaryStream) RenderError!void,
391-
guess_size_fn: *const fn (*Container) GuessError!SizeGuess,
392365
},
393366
) *const VTable {
394367
const Wrap = struct {
@@ -398,15 +371,9 @@ pub const Content = struct {
398371
stream,
399372
);
400373
}
401-
fn guess_size(self: *anyopaque) GuessError!SizeGuess {
402-
return funcs.guess_size_fn(
403-
@ptrCast(@alignCast(self)),
404-
);
405-
}
406374
};
407375
return comptime &.{
408376
.render_fn = Wrap.render,
409-
.guess_size_fn = Wrap.guess_size,
410377
};
411378
}
412379
};
@@ -541,12 +508,6 @@ pub const FileHandle = struct {
541508
}
542509
};
543510

544-
pub const SizeGuess = union(enum) {
545-
unknown,
546-
exact: u64,
547-
at_least: u64,
548-
};
549-
550511
pub const BinaryStream = struct {
551512
pub const WriteError = error{IoError};
552513
pub const Writer = std.io.Writer(*BinaryStream, WriteError, write_some);
@@ -580,6 +541,8 @@ const DiskSize = enum(u64) {
580541
const MiB = 1024 * 1024;
581542
const GiB = 1024 * 1024 * 1024;
582543

544+
pub const empty: DiskSize = @enumFromInt(0);
545+
583546
_,
584547

585548
pub fn parse(str: []const u8) error{ InvalidSize, Overflow }!DiskSize {

0 commit comments

Comments
 (0)