Skip to content

Commit

Permalink
Make the footer an arbitrary struct
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsIrl committed Aug 16, 2024
1 parent 6a236ac commit 246eebc
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
32 changes: 27 additions & 5 deletions src/common.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
const std = @import("std");
const builtin = @import("builtin");

const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const native_endian = builtin.target.cpu.arch.endian();

pub const Footer = extern struct {
offset: u64,
// TODO: make use of this field, currently ignored
require_mapped_uids: bool = false,
};

pub fn mkdtemp(in: []u8) !void {
try std.posix.getrandom(in[in.len - 6 ..]);
Expand All @@ -24,12 +32,26 @@ pub fn extract_file(tmpDir: []const u8, name: []const u8, data: []const u8, allo
return path;
}

pub fn getOffset(path: []const u8) !u64 {
pub fn getFooter(path: []const u8) !Footer {
var file = try std.fs.cwd().openFile(path, .{});
try file.seekFromEnd(-8);
try file.seekFromEnd(-@sizeOf(Footer));

var footer: Footer = undefined;
std.debug.assert(try file.readAll(std.mem.asBytes(&footer)) == @sizeOf(Footer));

if (native_endian != std.builtin.Endian.little) {
std.mem.byteSwapAllFields(Footer, footer[0]);
}

var buffer: [8]u8 = undefined;
std.debug.assert(try file.readAll(&buffer) == 8);
return footer;
}

pub fn writeFooter(file: std.fs.File, footer: Footer) !void {
comptime std.debug.assert(@typeInfo(Footer).Struct.layout != .auto);

if (native_endian != std.builtin.Endian.little) {
std.mem.byteSwapAllFields(Footer, &footer);
}

return std.mem.readInt(u64, buffer[0..8], std.builtin.Endian.big);
try file.writeAll(std.mem.asBytes(&footer));
}
12 changes: 6 additions & 6 deletions src/dockerc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,25 @@ pub fn main() !void {
});

var runtime_content: []const u8 = undefined;
var runtime_content_len_u64: [8]u8 = undefined;

if (res.args.arch) |arch| {
try skopeo_args.append("--override-arch");
try skopeo_args.append(arch);

if (std.mem.eql(u8, arch, "amd64")) {
runtime_content = runtime_content_x86_64;
runtime_content_len_u64 = runtime_content_len_u64_x86_64;
} else if (std.mem.eql(u8, arch, "arm64")) {
runtime_content = runtime_content_aarch64;
runtime_content_len_u64 = runtime_content_len_u64_aarch64;
} else {
std.debug.panic("unsupported arch: {s}\n", .{arch});
}
} else {
switch (builtin.target.cpu.arch) {
.x86_64 => {
runtime_content = runtime_content_x86_64;
runtime_content_len_u64 = runtime_content_len_u64_x86_64;
},
.aarch64 => {
runtime_content = runtime_content_aarch64;
runtime_content_len_u64 = runtime_content_len_u64_aarch64;
},
else => {
std.debug.panic("unsupported arch: {}", .{builtin.target.cpu.arch});
Expand Down Expand Up @@ -201,6 +196,11 @@ pub fn main() !void {

try file.writeAll(runtime_content);
try file.seekFromEnd(0);
try file.writeAll(&runtime_content_len_u64);

try common.writeFooter(file, common.Footer{
.offset = runtime_content.len,
.require_mapped_uids = false,
});

try file.chmod(0o755);
}
2 changes: 1 addition & 1 deletion src/extract_squashfs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn main() !void {

if (args.next()) |dockercGeneratedBinary| {
if (args.next()) |squashfsOutput| {
const offset = try common.getOffset(dockercGeneratedBinary);
const offset = (try common.getFooter(dockercGeneratedBinary)).offset;

const readFile = try std.fs.cwd().openFile(dockercGeneratedBinary, .{});
const writeFile = try std.fs.cwd().createFile(squashfsOutput, .{});
Expand Down
3 changes: 2 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@ pub fn main() !u8 {
const mount_dir_path = try std.fmt.allocPrintZ(allocator, "{s}/mount", .{temp_dir_path});
defer allocator.free(mount_dir_path);

const offsetArg = try std.fmt.allocPrintZ(allocator, "offset={}", .{try common.getOffset(executable_path)});
const footer = try common.getFooter(executable_path);
const offsetArg = try std.fmt.allocPrintZ(allocator, "offset={}", .{footer.offset});
defer allocator.free(offsetArg);

const args_buf = [_:null]?[*:0]const u8{ "squashfuse", "-o", offsetArg, executable_path, filesystem_bundle_dir_null };
Expand Down
8 changes: 6 additions & 2 deletions src/replace.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn main() !void {

if (rawPath) |path| {
if (rawPathOutput) |path_output| {
const offset = try common.getOffset(path);
const offset = (try common.getFooter(path)).offset;

const readFile = try std.fs.cwd().openFile(path, .{});
const writeFile = try std.fs.cwd().createFile(path_output, .{});
Expand All @@ -22,7 +22,11 @@ pub fn main() !void {
assert(len_to_write == len_written);

try writeFile.seekFromEnd(0);
try writeFile.writeAll(&runtimes.runtime_content_len_u64_x86_64);

try common.writeFooter(writeFile, common.Footer{
.offset = runtimes.runtime_content_x86_64.len,
});

try writeFile.chmod(0o755);

writeFile.close();
Expand Down
9 changes: 0 additions & 9 deletions src/runtimes.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
const std = @import("std");

fn get_runtime_content_len_u64(runtime_content: []const u8) [8]u8 {
var buf: [8]u8 = undefined;
std.mem.writeInt(u64, &buf, runtime_content.len, .big);
return buf;
}

pub const runtime_content_x86_64 = @embedFile("runtime_x86_64");
pub const runtime_content_aarch64 = @embedFile("runtime_aarch64");

pub const runtime_content_len_u64_x86_64 = get_runtime_content_len_u64(runtime_content_x86_64);
pub const runtime_content_len_u64_aarch64 = get_runtime_content_len_u64(runtime_content_aarch64);

0 comments on commit 246eebc

Please sign in to comment.