From e6fe1ccb7907a89911547c15bb60570038a52ebf Mon Sep 17 00:00:00 2001 From: Xiao Deshi Date: Mon, 19 Oct 2026 11:54:39 +0800 Subject: [PATCH] chore: zig fmt the tracked Zig sources `zig fmt --check` reported nine tracked files out of conformance. This runs zig fmt over them, changing nothing else. Scope build.zig sandboxd/src/events.zig, transcript.zig, files.zig, execctl.zig, watch.zig, processes.zig, transcript_test.zig tandem/src/storage/rqlite.zig One reported path is deliberately left alone: tandem/zig-pkg/libxev-*/src/bench/ping-pongs.zig sits inside the fetched dependency cache (gitignored via /tandem/zig-pkg), so it is neither tracked nor ours to reformat. Verification Every file is whitespace-only at the token level: stripping all spaces, tabs and newlines from the old and new content yields identical bytes, per file. So no expression, string or operator moved. - zig build sandboxd -> all three musl targets build - zig fmt --check -> clean for build.zig and sandboxd/src/*.zig - sandboxd unit tests -> the same 12 pre-existing failures before and after, identical by name (PTY tests plus a readAllFromFdTrunc 1 MiB off-by-one, both unrelated to formatting and all Linux-side) Needed because an earlier change to build.zig (making static linking Linux-only) would otherwise ship with an unformatted build.zig; a clean tree also keeps the next real edit reviewable. --- build.zig | 14 +++++++------- sandboxd/src/events.zig | 4 +--- sandboxd/src/execctl.zig | 6 +----- sandboxd/src/files.zig | 4 +--- sandboxd/src/processes.zig | 4 +--- sandboxd/src/transcript.zig | 5 +---- sandboxd/src/transcript_test.zig | 1 - sandboxd/src/watch.zig | 9 +++------ tandem/src/storage/rqlite.zig | 18 +++++++----------- 9 files changed, 22 insertions(+), 43 deletions(-) diff --git a/build.zig b/build.zig index 74de893c8..870ba3b90 100644 --- a/build.zig +++ b/build.zig @@ -169,9 +169,9 @@ pub fn build(b: *std.Build) !void { containerd_build.setEnvironmentVariable("GOPATH", b.fmt("{s}/build", .{root})); containerd_build.setCwd(b.path(containerd_src)); containerd_build.addArgs(&.{ - "-tags", shim_tags, - "-ldflags", ldflags, - "-o", "bin/containerd", + "-tags", shim_tags, + "-ldflags", ldflags, + "-o", "bin/containerd", "./cmd/containerd", }); containerd_build.step.dependOn(&download_cmd.step); @@ -266,7 +266,7 @@ pub fn build(b: *std.Build) !void { const cni_clone_abs = b.fmt("{s}/.cni-build", .{root}); const cni_workdir_abs = b.fmt("{s}/src/github.com/containernetworking/plugins", .{cni_clone_abs}); const cni_clone = b.addSystemCommand(&.{ - bash, "-c", + bash, "-c", b.fmt("rm -rf {s} && mkdir -p {s} && git clone --single-branch --depth=1 --branch={s} https://github.com/rancher/plugins.git {s}", .{ cni_clone_abs, cni_clone_abs, cni_version, cni_workdir_abs }), }); const cni_build = b.addSystemCommand(&.{ "go", "build" }); @@ -406,9 +406,9 @@ fn addSandboxCLIBuild(b: *std.Build, all_step: *std.Build.Step, v: VersionInfo) const out_path = b.getInstallPath(.bin, out_name); const go_build = b.addSystemCommand(&[_][]const u8{ - "go", "build", - "-ldflags", ldflags, - "-o", out_path, + "go", "build", + "-ldflags", ldflags, + "-o", out_path, "./cmd/sandboxcli/", }); go_build.setEnvironmentVariable("GOOS", t.goos); diff --git a/sandboxd/src/events.zig b/sandboxd/src/events.zig index 4a0d99827..9df5ba96f 100644 --- a/sandboxd/src/events.zig +++ b/sandboxd/src/events.zig @@ -10,7 +10,6 @@ const std = @import("std"); /// /// Format per line: {"t":,"ev":"","sid":"",...} /// Events: exec_start, exec_end, file_write, file_read, bg_submit, bg_poll. - const EVENT_DIR = "/workspace/.k8e_events"; /// Hard cap for the event file; when exceeded the file is rotated (old -> .1). pub const max_event_bytes: usize = 4 * 1024 * 1024; @@ -45,8 +44,7 @@ pub fn appendAt(dir: []const u8, session_id: []const u8, event: []const u8, extr const ts = getUnixSeconds(); var line_buf: [MAX_LINE]u8 = undefined; - const line = std.fmt.bufPrint(&line_buf, "{{\"t\":{d},\"ev\":\"{s}\",\"sid\":\"{s}\"{s}}}\n", - .{ ts, event, session_id, extra }) catch return; + const line = std.fmt.bufPrint(&line_buf, "{{\"t\":{d},\"ev\":\"{s}\",\"sid\":\"{s}\"{s}}}\n", .{ ts, event, session_id, extra }) catch return; _ = std.os.linux.write(@intCast(fd), line.ptr, line.len); } diff --git a/sandboxd/src/execctl.zig b/sandboxd/src/execctl.zig index e7987caf6..7197a63e0 100644 --- a/sandboxd/src/execctl.zig +++ b/sandboxd/src/execctl.zig @@ -317,7 +317,6 @@ pub fn handleSignal(allocator: std.mem.Allocator, client_fd: i32, body: []const try main.writeResponse(client_fd, "200 OK", "application/json", "{\"ok\":true}"); } - const StdinRequest = struct { pid: std.os.linux.pid_t, data: []const u8 = "", @@ -375,9 +374,7 @@ pub fn handleProcessList(allocator: std.mem.Allocator, client_fd: i32) !void { if (wrote_any) try out.append(','); wrote_any = true; - const entry = try std.fmt.bufPrint(&entry_buf, - "{{\"pid\":{d},\"alive\":{s},\"config\":\"{s}\"}}", - .{ pid, if (alive) "true" else "false", escaped }); + const entry = try std.fmt.bufPrint(&entry_buf, "{{\"pid\":{d},\"alive\":{s},\"config\":\"{s}\"}}", .{ pid, if (alive) "true" else "false", escaped }); try out.appendSlice(entry); } @@ -392,7 +389,6 @@ pub fn handleProcessList(allocator: std.mem.Allocator, client_fd: i32) !void { /// the buffered output, then a done marker. This slice replays the buffer; /// live tailing a still-running process from a second consumer needs a /// multi-reader pipe model (documented P1 follow-up). - /// parsePidQuery extracts the integer value of the "pid" query parameter. fn parsePidQuery(query: []const u8) ?[]const u8 { const prefix = "pid="; diff --git a/sandboxd/src/files.zig b/sandboxd/src/files.zig index cd8957c87..66b2c235c 100644 --- a/sandboxd/src/files.zig +++ b/sandboxd/src/files.zig @@ -463,9 +463,7 @@ pub fn handleStat(allocator: std.mem.Allocator, client_fd: i32, body: []const u8 const target_escaped = try exec.jsonEscape(allocator, symlink_target); defer allocator.free(target_escaped); - const resp = try std.fmt.allocPrint(allocator, - "{{\"type\":\"{s}\",\"size\":{d},\"mode\":\"{o}\",\"uid\":{d},\"gid\":{d},\"mtime\":{d},\"name\":\"{s}\",\"symlink_target\":\"{s}\"}}", - .{ ftype, stx.size, mode_u32 & 0o7777, stx.uid, stx.gid, stx.mtime.sec, req.path, target_escaped }); + const resp = try std.fmt.allocPrint(allocator, "{{\"type\":\"{s}\",\"size\":{d},\"mode\":\"{o}\",\"uid\":{d},\"gid\":{d},\"mtime\":{d},\"name\":\"{s}\",\"symlink_target\":\"{s}\"}}", .{ ftype, stx.size, mode_u32 & 0o7777, stx.uid, stx.gid, stx.mtime.sec, req.path, target_escaped }); defer allocator.free(resp); try main.writeResponse(client_fd, "200 OK", "application/json", resp); } diff --git a/sandboxd/src/processes.zig b/sandboxd/src/processes.zig index 684ca230d..309a2267e 100644 --- a/sandboxd/src/processes.zig +++ b/sandboxd/src/processes.zig @@ -9,7 +9,6 @@ const exec = @import("exec.zig"); /// visible in the pod's pid namespace. Reading never triggers collection and /// an idle daemon does zero extra work (the /proc scan happens only when a /// consumer requests it). - /// handleProcesses serves GET /processes — JSON array of running processes. pub fn handleProcesses(allocator: std.mem.Allocator, client_fd: i32, query: []const u8) !void { _ = query; @@ -52,8 +51,7 @@ pub fn handleProcesses(allocator: std.mem.Allocator, client_fd: i32, query: []co if (wrote_any) try out.append(','); wrote_any = true; var entry_buf: [512]u8 = undefined; - const entry = try std.fmt.bufPrint(&entry_buf, "{{\"pid\":{d},\"comm\":\"{s}\",\"state\":\"{c}\"}}", - .{ pid, escaped, state }); + const entry = try std.fmt.bufPrint(&entry_buf, "{{\"pid\":{d},\"comm\":\"{s}\",\"state\":\"{c}\"}}", .{ pid, escaped, state }); try out.appendSlice(entry); } } diff --git a/sandboxd/src/transcript.zig b/sandboxd/src/transcript.zig index c2453e7c4..62645d0bf 100644 --- a/sandboxd/src/transcript.zig +++ b/sandboxd/src/transcript.zig @@ -11,7 +11,6 @@ const main = @import("main.zig"); /// /// Format per line: ` \n` where stream is one of /// stdout|stderr|cmd. - const TRANSCRIPT_DIR = "/workspace/.k8e_transcripts"; /// Hard cap for a single window read (bounds per-request memory). pub const max_window_bytes: usize = 256 * 1024; @@ -188,9 +187,7 @@ pub fn handleTranscript(allocator: std.mem.Allocator, client_fd: i32, query: []c const escaped = try exec_json_escape(allocator, w.output); defer allocator.free(escaped); - const resp = try std.fmt.allocPrint(allocator, - "{{\"output\":\"{s}\",\"offset\":{d},\"next_offset\":{d},\"truncated_before\":{s},\"eof\":{s}}}", - .{ escaped, w.absolute_offset, w.next_offset, if (w.truncated_before) "true" else "false", if (w.eof) "true" else "false" }); + const resp = try std.fmt.allocPrint(allocator, "{{\"output\":\"{s}\",\"offset\":{d},\"next_offset\":{d},\"truncated_before\":{s},\"eof\":{s}}}", .{ escaped, w.absolute_offset, w.next_offset, if (w.truncated_before) "true" else "false", if (w.eof) "true" else "false" }); defer allocator.free(resp); try main.writeResponse(client_fd, "200 OK", "application/json", resp); } diff --git a/sandboxd/src/transcript_test.zig b/sandboxd/src/transcript_test.zig index 571b51750..71ab91c6e 100644 --- a/sandboxd/src/transcript_test.zig +++ b/sandboxd/src/transcript_test.zig @@ -6,7 +6,6 @@ const transcript = @import("transcript.zig"); // those trigger SIGSYS (same pre-existing limitation as exec_test runCommand // fork tests). These tests are Linux-only and run in CI. - const TEST_BASE = "/tmp/k8e-transcript-test"; fn setupDir() void { diff --git a/sandboxd/src/watch.zig b/sandboxd/src/watch.zig index 4ec4d0ddc..1ed797916 100644 --- a/sandboxd/src/watch.zig +++ b/sandboxd/src/watch.zig @@ -13,7 +13,6 @@ const main = @import("main.zig"); /// Watchers are created with a globally-incrementing id. The buffers are /// plain memory: a daemon restart drops them, matching the process table's /// honest non-persistence. - const WATCHER_MAX = 32; const EVENT_BUF_MAX = 64; @@ -151,9 +150,8 @@ pub fn handleCreate(allocator: std.mem.Allocator, client_fd: i32, body: []const try main.writeResponse(client_fd, "400 Bad Request", "application/json", "{\"error\":\"path too long\"}"); return; }; - const wd_raw = std.os.linux.inotify_add_watch(inotify_fd, path_z, - std.os.linux.IN.CREATE | std.os.linux.IN.MODIFY | std.os.linux.IN.DELETE | - std.os.linux.IN.MOVED_FROM | std.os.linux.IN.MOVED_TO | std.os.linux.IN.ATTRIB); + const wd_raw = std.os.linux.inotify_add_watch(inotify_fd, path_z, std.os.linux.IN.CREATE | std.os.linux.IN.MODIFY | std.os.linux.IN.DELETE | + std.os.linux.IN.MOVED_FROM | std.os.linux.IN.MOVED_TO | std.os.linux.IN.ATTRIB); const wd: isize = @bitCast(wd_raw); if (wd < 0) { try main.writeResponse(client_fd, "404 Not Found", "application/json", "{\"error\":\"path not found\"}"); @@ -217,8 +215,7 @@ pub fn handleEvents(allocator: std.mem.Allocator, client_fd: i32, query: []const if (wrote_any) try out.append(','); wrote_any = true; var entry_buf: [320]u8 = undefined; - const entry = try std.fmt.bufPrint(&entry_buf, "{{\"name\":\"{s}\",\"type\":{d}}}", - .{ name, w.types[slot] }); + const entry = try std.fmt.bufPrint(&entry_buf, "{{\"name\":\"{s}\",\"type\":{d}}}", .{ name, w.types[slot] }); try out.appendSlice(entry); } w.cursor = w.len; diff --git a/tandem/src/storage/rqlite.zig b/tandem/src/storage/rqlite.zig index 81246f542..64e33e8b1 100644 --- a/tandem/src/storage/rqlite.zig +++ b/tandem/src/storage/rqlite.zig @@ -779,8 +779,7 @@ pub const SqlBuilder = struct { pub fn atomicDeleteRangeSql(allocator: Allocator, key: []const u8, range_end: []const u8) ![]u8 { const predicate = try keyPredicate(allocator, key, range_end); defer allocator.free(predicate); - return std.fmt.allocPrint(allocator, - "BEGIN;DROP TABLE IF EXISTS tandem_delete_prev;CREATE TEMP TABLE tandem_delete_prev AS SELECT key,value,create_revision,mod_revision,version,lease FROM kv WHERE {s};" ++ + return std.fmt.allocPrint(allocator, "BEGIN;DROP TABLE IF EXISTS tandem_delete_prev;CREATE TEMP TABLE tandem_delete_prev AS SELECT key,value,create_revision,mod_revision,version,lease FROM kv WHERE {s};" ++ "UPDATE revision SET current_revision=current_revision+1 WHERE id=1 AND EXISTS(SELECT 1 FROM tandem_delete_prev);" ++ "INSERT INTO kv_history(key,value,create_revision,mod_revision,version,lease,deleted) SELECT key,value,create_revision,(SELECT current_revision FROM revision WHERE id=1),version,lease,1 FROM tandem_delete_prev;" ++ "DELETE FROM lease_keys WHERE key IN (SELECT key FROM tandem_delete_prev);" ++ @@ -999,16 +998,14 @@ pub const SqlBuilder = struct { /// deletions split across revisions, and a client counting revisions to /// order its own writes saw a gap that was never a real write. pub fn revokeLeaseWithKeysSql(allocator: Allocator, lease_id: i64) ![]u8 { - return std.fmt.allocPrint(allocator, - "BEGIN;" ++ + return std.fmt.allocPrint(allocator, "BEGIN;" ++ "UPDATE revision SET current_revision=current_revision+1 WHERE id=1 AND EXISTS(SELECT 1 FROM kv WHERE lease={d});" ++ "INSERT INTO kv_history(key,value,create_revision,mod_revision,version,lease,deleted) " ++ "SELECT key,value,create_revision,(SELECT current_revision FROM revision WHERE id=1),version,lease,1 FROM kv WHERE lease={d};" ++ "DELETE FROM kv WHERE lease={d};" ++ "DELETE FROM lease_keys WHERE lease_id={d};" ++ "DELETE FROM leases WHERE id={d};" ++ - "COMMIT;", - .{ lease_id, lease_id, lease_id, lease_id, lease_id }); + "COMMIT;", .{ lease_id, lease_id, lease_id, lease_id, lease_id }); } /// Expire one leased key: record the tombstone, drop the row and the lease @@ -1055,12 +1052,10 @@ pub const SqlBuilder = struct { defer allocator.free(hex); const guard = try leaseOwnerGuardSql(allocator, lease_id, owner, now); defer allocator.free(guard); - return std.fmt.allocPrint(allocator, - "BEGIN;UPDATE revision SET current_revision=current_revision+1 WHERE id=1 AND EXISTS(SELECT 1 FROM kv WHERE key=X'{s}' AND lease={d}) AND {s};" ++ + return std.fmt.allocPrint(allocator, "BEGIN;UPDATE revision SET current_revision=current_revision+1 WHERE id=1 AND EXISTS(SELECT 1 FROM kv WHERE key=X'{s}' AND lease={d}) AND {s};" ++ "INSERT INTO kv_history(key,value,create_revision,mod_revision,version,lease,deleted) SELECT key,value,create_revision,(SELECT current_revision FROM revision WHERE id=1),version,lease,1 FROM kv WHERE key=X'{s}' AND lease={d} AND {s};" ++ "DELETE FROM kv WHERE key=X'{s}' AND lease={d} AND {s};" ++ - "DELETE FROM lease_keys WHERE key=X'{s}' AND lease_id={d} AND {s};COMMIT;", - .{ hex, lease_id, guard, hex, lease_id, guard, hex, lease_id, guard, hex, lease_id, guard }); + "DELETE FROM lease_keys WHERE key=X'{s}' AND lease_id={d} AND {s};COMMIT;", .{ hex, lease_id, guard, hex, lease_id, guard, hex, lease_id, guard, hex, lease_id, guard }); } pub fn revokeLeaseOwnedSql(allocator: Allocator, lease_id: i64, owner: []const u8, now: i64) ![]u8 { @@ -1750,7 +1745,8 @@ test "the rqlite status parser reads nested leader and raft fields" { test "a status without a leader reports none rather than a fabricated one" { // A node that has not elected itself still answers /status, and the // caller reports "no leader" instead of the constant 1 this used to send. - const body = \\{"store":{"ready":false,"node_id":"n1","leader":{},"raft":{"term":0,"applied_index":0}}} + const body = + \\{"store":{"ready":false,"node_id":"n1","leader":{},"raft":{"term":0,"applied_index":0}}} ; const status = try parseStatusResponse(testing.allocator, body); defer testing.allocator.free(status.leader_address);