Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_o3db_sync/examples/o3db_verify.rs

5.2 KiB, 25 runs

created by r1870400018:69889, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Reads every live value of a COPY of an Ozone store and names each key that does not read back
2//! whole: a chunked value torn between two writes fails its AES-GCM tag (see
3//! `oxedyne_fe2o3_o3db_sync::verify`).
4//!
5//! ```ignore
6//! o3db_verify --o3db DIR [--key KEYFILE] [--scan-secs N] # or O3DB_KEY_FILE=KEYFILE in the environment
7//! o3db_verify --o3db DIR --clear-refusal # no key needed
8//! ```
9//!
10//! Exit status: 0 when every live key read back, 1 when any failed, 2 on a usage error, an
11//! unreadable store, or a store that holds no live key.
12//!
13//! `--clear-refusal` removes the store's refusal marker (`Refusal::clear`), left when a sync
14//! failed, and opens nothing. Run it only once the files in doubt are checked, or the store is
15//! restored: the marker stands because the kernel reports a failed write-back only once.
16//!
17//! # Safety
18//! - Check a `cp -a` COPY taken with the store's own process stopped, never a live store, and
19//! never with a second process on one store. The check issues no write; the store's bots still
20//! survey its files when it opens.
21//! - The store is opened for the gateway's parameterisation (16-byte `u128` user ids, AES-256-GCM
22//! at rest, CRC-32), as `o3db_migrate` is (`oxedyne_fe2o3_o3db_sync::gateway`). The at-rest key
23//! is read from the file named by `--key` or `O3DB_KEY_FILE`, and is never defaulted.
24//!
25//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
26//! Anthropic Claude
27
28use oxedyne_fe2o3_core::prelude::*;
29use oxedyne_fe2o3_o3db_sync::{
30 base::constant,
31 comm::response::Wait,
32 file::refusal::Refusal,
33 gateway,
34 verify,
35};
36
37use std::{
38 path::PathBuf,
39 process,
40 thread,
41 time::Duration,
42};
43
44
45fn main() {
46 match run() {
47 Ok(code) => process::exit(code),
48 Err(e) => {
49 eprintln!("o3db_verify FAILED: {}", e);
50 process::exit(2);
51 },
52 }
53}
54
55fn run() -> Outcome<i32> {
56 log_set_level!("warn");
57 let (dir, key_path, secs) = match res!(parse()) {
58 Args::Verify(dir, key_path, secs) => (dir, key_path, secs),
59 Args::Clear(dir) => return clear(&dir),
60 };
61 let key = res!(gateway::load_key(&key_path));
62 let (db, answered) = res!(gateway::open_store(&dir, None, &key, false, "o3db-verify"));
63 if let Err(e) = gateway::require_answer(answered, "o3db-verify", &dir) {
64 let _ = db.shutdown();
65 return Err(e);
66 }
67 let wait = Wait {
68 max_wait: Duration::from_secs(secs),
69 check_interval: constant::CHECK_INTERVAL,
70 };
71 let outcome = verify::verify_live_set(db.api(), None, wait);
72 res!(db.shutdown());
73 thread::sleep(Duration::from_secs(1));
74 let rep = res!(outcome);
75 if rep.keys == 0 {
76 return Err(err!(
77 "The store at {:?} holds no live key; wrong directory?", dir;
78 Missing, Input));
79 }
80 print!("{}", rep.summary(200));
81 if rep.clean() {
82 println!("CLEAN: every live key read back whole.");
83 Ok(0)
84 } else {
85 println!("NOT CLEAN: {} key(s) failed to read; see the FAILED lines.", rep.failures.len());
86 Ok(1)
87 }
88}
89
90fn clear(dir: &PathBuf) -> Outcome<i32> {
91 match res!(Refusal::clear(dir)) {
92 true => println!("CLEARED: the refusal marker of the store at {:?} is removed, so the \
93 store opens again. Any fault record beside it still cuts its files in doubt at the \
94 next open.", dir),
95 false => println!("NO REFUSAL: the store at {:?} holds no refusal marker.", dir),
96 }
97 Ok(0)
98}
99
100enum Args {
101 Verify(PathBuf, PathBuf, u64), // store, key file, scan seconds
102 Clear(PathBuf), // store
103}
104
105fn parse() -> Outcome<Args> {
106 let usage = "Usage: --o3db DIR [--key KEYFILE] [--scan-secs N], or O3DB_KEY_FILE for the key; \
107 or --o3db DIR --clear-refusal.";
108 let mut o3db: Option<PathBuf> = None;
109 let mut key: Option<PathBuf> = std::env::var_os("O3DB_KEY_FILE").map(PathBuf::from);
110 let mut secs: u64 = 3600;
111 let mut clear: bool = false;
112 let mut it = std::env::args().skip(1);
113 while let Some(arg) = it.next() {
114 if arg == "--clear-refusal" {
115 clear = true;
116 continue;
117 }
118 let val = res!(it.next().ok_or_else(|| err!(
119 "Argument {} needs a value. {}", arg, usage; Missing, Input)));
120 match arg.as_str() {
121 "--o3db" => o3db = Some(PathBuf::from(val)),
122 "--key" => key = Some(PathBuf::from(val)),
123 "--scan-secs" => secs = res!(val.parse::<u64>().map_err(|_| err!(
124 "--scan-secs must be a whole number of seconds, got {:?}.", val; Invalid, Input))),
125 other => return Err(err!("Unrecognised argument {:?}. {}", other, usage;
126 Invalid, Input)),
127 }
128 }
129 match (o3db, key, clear) {
130 (Some(dir), _, true) => Ok(Args::Clear(dir)),
131 (Some(dir), Some(key), false) => Ok(Args::Verify(dir, key, secs)),
132 (None, _, _) => Err(err!("Give --o3db DIR. {}", usage; Missing, Input)),
133 (_, None, false) => Err(err!("Give --key KEYFILE or set O3DB_KEY_FILE. {}", usage;
134 Missing, Input, Key)),
135 }
136}