//! `zit stat`: the store's size, content and overhead accounting, and the
//! artifacts it holds.
const std = @import("std");
const zit = @import("zit");
const cli = @import("zit").cli;
const cmd_sync = @import("sync.zig");
const logstore = @import("zit").logstore;
const storage = @import("zit").storage;
const storestat = @import("zit").storestat;
const artifact = zit.artifact;
const diagnostics = @import("../diagnostics.zig");
const help_pages = @import("../help.zig");
const reasonOf = diagnostics.reasonOf;
const stateOom = diagnostics.stateOom;
const repoReadFail = diagnostics.repoReadFail;
const storeReadFail = diagnostics.storeReadFail;
const stat_help = help_pages.stat_help;
/// Which artifacts `zit stat --artifacts` lists. `all` is the unfiltered roll;
/// `full` and `delta` partition it by storage form, which is the one property
/// the frame walk already knows and so costs nothing extra to filter on.
const StatType = enum { all, full, delta };
pub fn runStat(
gpa: std.mem.Allocator,
io: std.Io,
arena: std.mem.Allocator,
verb_args: []const []const u8,
streams: cli.Streams,
amb: cli.Ambient,
) cli.Exit {
var list_artifacts = false;
var want: StatType = .all;
var typed = false;
for (verb_args) |tok| {
if (std.mem.eql(u8, tok, "-h") or std.mem.eql(u8, tok, "--help")) {
streams.out.writeAll(stat_help) catch {};
return .ok;
} else if (std.mem.eql(u8, tok, "--artifacts")) {
list_artifacts = true;
} else if (std.mem.startsWith(u8, tok, "--type=")) {
// A second --type is a mistake, not a refinement: silently keeping
// the last one would answer a question the user did not ask.
if (typed) return cli.usageError(streams, "stat", .repeated_option, "--type");
const val = tok["--type=".len..];
want = if (std.mem.eql(u8, val, "full"))
.full
else if (std.mem.eql(u8, val, "delta"))
.delta
else
return cli.usageError(streams, "stat", .unknown_option, tok);
typed = true;
} else if (tok.len > 1 and tok[0] == '-') {
return cli.usageError(streams, "stat", .unknown_option, tok);
} else {
return cli.usageError(streams, "stat", .extra_argument, tok);
}
}
// `--type` selects among artifacts; without a listing there is nothing for
// it to select, and silently ignoring it would hide a mistaken invocation.
if (typed and !list_artifacts) {
streams.err.writeAll("zit stat: --type needs --artifacts\n") catch {};
return .usage;
}
// `-R` joins the read-only family here. `stat` was refused it for a
// plumbing reason (its handler took no redirected repository), never a
// policy one, and the cost of that accident was real: a repository's
// project code was reachable ONLY by speaking the sync protocol to a server
// you started yourself, so a consumer needing one constant had to spawn a
// subprocess and a listening socket per repository.
const root = (cli.resolveRoot(arena, io, amb.repo_override) catch
return stateOom(streams, "stat")) orelse {
diagnostics.reportNoRoot(streams, io, "stat", amb.repo_override);
return .failure;
};
var root_dir = root.dir;
defer root_dir.close(io);
var file = cli.openStoreRead(io, root_dir) catch |e| return storeReadFail(streams, "stat", e);
defer file.close(io);
const st = file.stat(io) catch return repoReadFail(streams, "stat");
var fs = storage.FileStorage.init(io, file, st.size);
// Adopt the persisted scan index for the config/roll read; the
// census below stays a MEASURED physical walk (`storestat.walk`), so
// every printed number is still read from the frames, never the cache.
var store = cmd_sync.openLogAdoptingIndex(gpa, io, root_dir, fs.storageFile(), .refresh_on_scan) catch |e| switch (e) {
error.OutOfMemory => return stateOom(streams, "stat"),
else => {
streams.err.print("zit stat: cannot read repository: {s}\n", .{reasonOf(e)}) catch {};
return .failure;
},
};
defer store.deinit(gpa);
if (list_artifacts) return statArtifacts(gpa, &store, want, streams);
var fs2 = storage.FileStorage.init(io, file, st.size);
const bd = storestat.walk(fs2.storageFile(), st.size) catch |e| {
streams.err.print("zit stat: cannot read repository: {s}\n", .{reasonOf(e)}) catch {};
return .failure;
};
// The repository's IDENTITY, printed first because it is what a
// consumer most often came for. `project-code` is the RFC section 6.3 shared
// secret's first input; before this it was obtainable only by speaking the
// sync protocol to a server you started yourself, which cost a subprocess, a
// listening socket and a startup stall per repository.
//
// Emitted unconditionally, with an empty value on a repository that has
// none, so a parser reads one stable shape: a missing LINE and an empty
// VALUE are different facts, and only one of them means "this repository
// predates project codes".
var identity_oc = store.readConfig(gpa) catch null;
defer if (identity_oc) |*oc| oc.deinit(gpa);
const project_code: []const u8 = if (identity_oc) |oc| oc.config.project_code else "";
// The stored byte is opaque to storage (it round-trips whatever it was
// given), so a value outside the enum is reported as the raw number rather
// than guessed at: a repository written by something else is a fact worth
// seeing, not one to normalise away.
var policy_buf: [16]u8 = undefined;
const hash_policy: []const u8 = if (identity_oc) |oc| blk: {
const raw = oc.config.hash_policy;
break :blk if (std.enums.fromInt(artifact.HashPolicy, raw)) |p|
@tagName(p)
else
std.fmt.bufPrint(&policy_buf, "{d}", .{raw}) catch "";
} else "";
streams.out.print("project-code {s}\nhash-policy {s}\n", .{ project_code, hash_policy }) catch {};
streams.out.print(
\\store-bytes {d}
\\header-bytes {d}
\\content-bytes {d}
\\frame-overhead-bytes {d}
\\config-bytes {d}
\\shun-bytes {d}
\\shun-rows {d}
\\user-bytes {d}
\\user-frames {d}
\\artifacts {d}
\\full-form {d}
\\full-form-bytes {d}
\\deltas {d}
\\delta-bytes {d}
\\
, .{
st.size, logstore.header_len, bd.contentBytes(), bd.contentOverhead(),
bd.configBytes(), bd.shunBytes(), bd.shunRows(), bd.userBytes(),
bd.userFrames(), bd.artifacts(), bd.fullForm(), bd.fullFormBytes(),
bd.deltas(), bd.deltaBytes(),
}) catch {};
return .ok;
}
/// Emits one lowercase-hex artifact hash per line, in ascending hash order, so
/// two stores' rolls compare with `comm`. Reads the already-open store's index
/// — no further I/O, and no per-artifact process.
fn statArtifacts(
gpa: std.mem.Allocator,
store: *logstore.LogStore,
want: StatType,
streams: cli.Streams,
) cli.Exit {
const rids = store.ridsInPersistedOrder(gpa) catch return stateOom(streams, "stat");
defer gpa.free(rids);
var hashes: std.ArrayListUnmanaged([]const u8) = .empty;
defer hashes.deinit(gpa);
hashes.ensureTotalCapacity(gpa, rids.len) catch return stateOom(streams, "stat");
for (rids) |rid| {
const kind = store.kindOf(rid);
// The config record is repository metadata, not a stored artifact, so
// it is absent from every listing and from the `artifacts` count.
if (kind == .config) continue;
const keep = switch (want) {
.all => true,
.delta => kind == .delta,
.full => kind == .raw or kind == .zlib,
};
if (keep) hashes.appendAssumeCapacity(store.hashOf(rid));
}
// The listing is a filtered projection of the persisted rid set — never more
// hashes than records walked.
std.debug.assert(hashes.items.len <= rids.len);
std.mem.sort([]const u8, hashes.items, {}, struct {
fn lt(_: void, a: []const u8, b: []const u8) bool {
return std.mem.order(u8, a, b) == .lt;
}
}.lt);
for (hashes.items) |h| streams.out.print("{x}\n", .{h}) catch {};
return .ok;
}