//! `zit checkpoint`: the durable rebuild checkpoint's operator surface.
const std = @import("std");
const zit = @import("zit");
const cli = @import("zit").cli;
const storage = @import("zit").storage;
const diagnostics = @import("../cli/diagnostics.zig");
const help_pages = @import("../cli/help.zig");
const not_a_repo_msg = diagnostics.not_a_repo_msg;
// ---------------------------------------------------------------------------
// zit checkpoint — the durable rebuild checkpoint's operator surface. Thin
// wiring only: parse, discover, open, call one zit.checkpoint entry point,
// print. No container parsing, tree building, or resume logic here.
// ---------------------------------------------------------------------------
pub fn runCheckpoint(
gpa: std.mem.Allocator,
io: std.Io,
arena: std.mem.Allocator,
verb_args: []const []const u8,
streams: cli.Streams,
) cli.Exit {
const parsed = switch (cli.parseCheckpointArgs(verb_args)) {
.ok => |p| p,
.unknown_option => |tok| return cli.usageError(streams, "checkpoint", .unknown_option, tok),
.extra_argument => |tok| return cli.usageError(streams, "checkpoint", .extra_argument, tok),
};
if (parsed.help) {
streams.out.writeAll(help_pages.checkpoint_help) catch {};
return .ok;
}
const root = (cli.discoverRoot(arena, io) catch return checkpointOom(streams)) orelse {
streams.err.writeAll(not_a_repo_msg) catch {};
return .failure;
};
var root_dir = root.dir;
defer root_dir.close(io);
return switch (parsed.action) {
.show => checkpointShow(gpa, io, root_dir, streams),
.build => checkpointBuild(gpa, io, root_dir, parsed.checkin, streams),
.verify => checkpointVerify(gpa, io, root_dir, streams),
.drop => blk: {
zit.checkpoint.drop(io, root_dir);
streams.out.writeAll("checkpoint: dropped\n") catch {};
break :blk .ok;
},
};
}
fn checkpointOom(streams: cli.Streams) cli.Exit {
streams.err.writeAll("zit checkpoint: out of memory\n") catch {};
return .failure;
}
fn checkpointShow(gpa: std.mem.Allocator, io: std.Io, root_dir: std.Io.Dir, streams: cli.Streams) cli.Exit {
var file = cli.openStoreRead(io, root_dir) catch |e| {
streams.err.print("zit checkpoint: cannot read repository: {s}\n", .{diagnostics.reasonOf(e)}) catch {};
return .failure;
};
defer file.close(io);
const st = file.stat(io) catch {
streams.err.writeAll("zit checkpoint: cannot read repository: input/output error\n") catch {};
return .failure;
};
var fs = storage.FileStorage.init(io, file, st.size);
var loaded = zit.checkpoint.tryLoad(gpa, io, root_dir, fs.storageFile()) orelse {
streams.out.writeAll("checkpoint: none (or it no longer validates against this log)\n") catch {};
return .ok;
};
defer loaded.deinit(gpa);
streams.out.print(
"checkpoint: witness\n check-in: {s}\n prefix: {d} frames, {d} bytes\n",
.{ loaded.claim.checkin_hash, loaded.claim.prefix_key.frame_count, loaded.claim.prefix_key.end },
) catch {};
streams.out.writeAll(" validates against the live log\n") catch {};
return .ok;
}
fn checkpointBuild(gpa: std.mem.Allocator, io: std.Io, root_dir: std.Io.Dir, checkin: ?[]const u8, streams: cli.Streams) cli.Exit {
var repo: zit.Repository = undefined;
// The ordinary open, caches and all: the derive cache's HIT ≡ recompute
// invariant makes a cache-served view equal to a paranoid derive, and
// `zit checkpoint verify` is the deliberately paranoid surface. Bypassing
// here made every build pay a full derive for nothing.
zit.Repository.open(&repo, gpa, io, root_dir, .{ .mode = .full }) catch |e| {
streams.err.print("zit checkpoint: cannot read repository: {s}\n", .{openWhy(e)}) catch {};
return .failure;
};
defer repo.deinit(gpa);
const report = zit.checkpoint.build(gpa, io, &repo, root_dir, checkin) catch |e| {
streams.err.print("zit checkpoint: build failed: {s}\n", .{buildWhy(e)}) catch {};
return .failure;
};
streams.out.print(
"checkpoint: built at {s}\n files: {d} prefix frames: {d}\n",
.{ report.checkinHash(), report.files, report.prefix_frames },
) catch {};
return .ok;
}
fn checkpointVerify(gpa: std.mem.Allocator, io: std.Io, root_dir: std.Io.Dir, streams: cli.Streams) cli.Exit {
const verdict = zit.checkpoint.verify(gpa, io, root_dir) catch |e| {
streams.err.print("zit checkpoint: verify failed: {s}\n", .{buildWhy(e)}) catch {};
return .failure;
};
switch (verdict) {
.ok => |report| {
streams.out.print("checkpoint: verified ok ({d} files, {d} prefix frames)\n", .{ report.files, report.prefix_frames }) catch {};
return .ok;
},
.no_checkpoint => {
streams.out.writeAll("checkpoint: none to verify\n") catch {};
return .ok;
},
.stale => {
streams.err.writeAll("zit checkpoint: STALE — the sidecar no longer validates against this log; run 'zit checkpoint build' to replace it\n") catch {};
return .failure;
},
.derivation_mismatch => {
streams.err.writeAll("zit checkpoint: DERIVATION MISMATCH — the stored prefix record differs from an independent re-derivation; drop and rebuild\n") catch {};
return .failure;
},
.fileset_mismatch => {
streams.err.writeAll("zit checkpoint: FILE-SET MISMATCH — the fingerprint disagrees with the independently derived file set at the anchor check-in\n") catch {};
return .failure;
},
}
}
fn openWhy(e: anyerror) []const u8 {
return switch (e) {
error.NotARepo => "not a zit repository",
error.Corrupt => "repository is corrupt",
error.OutOfMemory => "out of memory",
else => "input/output error",
};
}
fn buildWhy(e: zit.checkpoint.BuildError) []const u8 {
return switch (e) {
error.OutOfMemory => "out of memory",
error.Storage => "input/output error",
error.Corrupt => "repository is corrupt",
error.CheckinNotFound => "check-in not found in this repository",
error.NotACheckin => "the named artifact is not a check-in",
};
}