Skip to main content

waratah/
blob.rs

1//! `waratah blob`: maintenance of the registry's content-addressed blob
2//! store under `{data_dir}/{repository}/blobs`.
3//!
4//! `verify` is an integrity scan: index identity checks, segment record
5//! CRCs and payload identity, and an orphan-record census. It opens the
6//! store through the same recovery path a registry open runs (a torn tail
7//! past a committed durable length is truncated; the scan itself performs
8//! no writes) — it is not a raw read-only pass, and a missing store is
9//! created empty rather than rejected. The store has no migration path by
10//! design — the pre-two-tier per-file layout was written by an unreleased
11//! build, and a data dir holding it is re-imported instead
12//! (`BlobStore::open` refuses the layout loudly).
13
14use std::path::PathBuf;
15
16use waratah_registry::BlobStore;
17use waratah_storage::{MAIN_REPOSITORY_ID, blob_store_dir};
18
19use crate::error::DevError;
20use crate::import::status;
21
22/// Integrity scan: see [`BlobStore::verify`]. The store is opened through
23/// normal recovery first (see the module docs), then scanned; exits
24/// non-zero on any problem, while orphan records are reported as
25/// information, not failures.
26pub fn verify(data_dir: PathBuf) -> Result<(), DevError> {
27    let root = blob_store_dir(&data_dir, MAIN_REPOSITORY_ID);
28    if !root.exists() {
29        // Name both the store path expected and the data dir the operator
30        // passed, so a typo in --data-dir is visible in the output rather
31        // than reading as a store that has nothing in it.
32        status!(
33            "no blob store at {} (derived from --data-dir {}); nothing to verify",
34            root.display(),
35            data_dir.display()
36        );
37        return Ok(());
38    }
39    let store = BlobStore::open(&root)?;
40    let report = store.verify()?;
41    status!(
42        "index entries: {} ({} inline, {} segment); {} segment byte(s) scanned",
43        report.index_entries,
44        report.inline_entries,
45        report.segment_entries,
46        report.segment_bytes_scanned
47    );
48    status!("orphan segment records: {}", report.orphan_records);
49    for problem in &report.problems {
50        status!("problem: {problem}");
51    }
52    if report.problems.is_empty() {
53        status!("blob store at {} is consistent", root.display());
54        Ok(())
55    } else {
56        Err(DevError::Io(std::io::Error::other(format!(
57            "{} integrity problem(s) found in {}",
58            report.problems.len(),
59            root.display()
60        ))))
61    }
62}