Skip to content

Commit c9d500a

Browse files
committed
Add -v/--volume support and free memory
1 parent 13f51da commit c9d500a

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

src/main.zig

+40-11
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ fn getEnvFull(key: []const u8) ?[:0]const u8 {
3737
return null;
3838
}
3939

40-
fn processArgs(file: std.fs.File, allocator: std.mem.Allocator) !void {
40+
fn processArgs(file: std.fs.File, parentAllocator: std.mem.Allocator) !void {
41+
var arena = std.heap.ArenaAllocator.init(parentAllocator);
42+
defer arena.deinit();
43+
const allocator = arena.allocator();
44+
4145
var jsonReader = std.json.reader(allocator, file.reader());
4246

4347
// TODO: having to specify max_value_len seems like a bug
4448
var root_value = try std.json.Value.jsonParse(allocator, &jsonReader, .{ .max_value_len = 99999999 });
4549

4650
var args_json: *std.ArrayList(std.json.Value) = undefined;
4751
var env_json: *std.ArrayList(std.json.Value) = undefined;
52+
var mounts_json: *std.ArrayList(std.json.Value) = undefined;
4853

4954
switch (root_value) {
5055
.object => |*object| {
@@ -74,6 +79,19 @@ fn processArgs(file: std.fs.File, allocator: std.mem.Allocator) !void {
7479
},
7580
else => return error.InvalidJSON,
7681
}
82+
83+
if (object.getPtr("mounts")) |mountsVal| {
84+
switch (mountsVal.*) {
85+
.array => |*mountsArr| {
86+
mounts_json = mountsArr;
87+
},
88+
else => return error.InvalidJSON,
89+
}
90+
} else {
91+
var array = std.json.Array.init(allocator);
92+
mounts_json = &array;
93+
try object.put("mounts", std.json.Value{ .array = array });
94+
}
7795
},
7896
else => return error.InvalidJSON,
7997
}
@@ -89,12 +107,22 @@ fn processArgs(file: std.fs.File, allocator: std.mem.Allocator) !void {
89107
} else {
90108
try env_json.append(std.json.Value{ .string = getEnvFull(environment_variable) orelse @panic("environment variable does not exist") });
91109
}
92-
} else if (eql(u8, arg, "-p")) {
93-
_ = args.next();
94-
@panic("not implemented");
95-
} else if (eql(u8, arg, "-v")) {
96-
_ = args.next();
97-
@panic("not implemented");
110+
} else if (eql(u8, arg, "-v") or eql(u8, arg, "--volume")) {
111+
const volume_syntax = args.next() orelse @panic("expected volume syntax");
112+
113+
var mount = std.json.ObjectMap.init(allocator);
114+
115+
var options = std.json.Array.init(allocator);
116+
try options.append(std.json.Value { .string = "rw" });
117+
try options.append(std.json.Value { .string = "rbind" });
118+
try mount.put("options", std.json.Value{ .array = options });
119+
120+
const separator = std.mem.indexOfScalar(u8, volume_syntax, ':') orelse @panic("no volume destination specified");
121+
122+
try mount.put("source", std.json.Value{ .string = volume_syntax[0..separator] });
123+
try mount.put("destination", std.json.Value{ .string = volume_syntax[separator + 1..] });
124+
125+
try mounts_json.append(std.json.Value{ .object = mount });
98126
} else if (eql(u8, arg, "--")) {
99127
while (args.next()) |arg_inner| {
100128
try args_json.append(std.json.Value{ .string = arg_inner });
@@ -113,6 +141,7 @@ fn processArgs(file: std.fs.File, allocator: std.mem.Allocator) !void {
113141

114142
pub fn main() !void {
115143
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
144+
defer _ = gpa.deinit();
116145
const allocator = gpa.allocator();
117146
// defer _ = gpa.deinit();
118147
var args = std.process.args();
@@ -142,7 +171,7 @@ pub fn main() !void {
142171

143172
const args_buf = [_][]const u8{ squashfuse_path, "-o", offsetArg, executable_path, filesystem_bundle_dir_null };
144173

145-
var mountProcess = std.ChildProcess.init(&args_buf, gpa.allocator());
174+
var mountProcess = std.ChildProcess.init(&args_buf, allocator);
146175
_ = try mountProcess.spawnAndWait();
147176

148177
const overlayfs_options = try std.fmt.allocPrint(allocator, "lowerdir={s},upperdir={s}/upper,workdir={s}/upper", .{
@@ -169,13 +198,13 @@ pub fn main() !void {
169198
try processArgs(file, allocator);
170199
}
171200

172-
var crunProcess = std.ChildProcess.init(&[_][]const u8{ crun_path, "run", "-b", mount_dir_path, "crun_docker_c_id" }, gpa.allocator());
201+
var crunProcess = std.ChildProcess.init(&[_][]const u8{ crun_path, "run", "-b", mount_dir_path, "crun_docker_c_id" }, allocator);
173202
_ = try crunProcess.spawnAndWait();
174203

175-
var umountOverlayProcess = std.ChildProcess.init(&[_][]const u8{ "umount", mount_dir_path }, gpa.allocator());
204+
var umountOverlayProcess = std.ChildProcess.init(&[_][]const u8{ "umount", mount_dir_path }, allocator);
176205
_ = try umountOverlayProcess.spawnAndWait();
177206

178-
var umountProcess = std.ChildProcess.init(&[_][]const u8{ "umount", filesystem_bundle_dir_null }, gpa.allocator());
207+
var umountProcess = std.ChildProcess.init(&[_][]const u8{ "umount", filesystem_bundle_dir_null }, allocator);
179208
_ = try umountProcess.spawnAndWait();
180209

181210
// TODO: clean up /tmp

0 commit comments

Comments
 (0)