oxedyne/fe2o3/fe2o3_o3db_sync/examples/jdat_store_check.rs
3.7 KiB, 5 runs
created by r1870400018:67323, 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 | //! Checks the JDAT text a COPY of an Ozone store holds against the decoder's refusals, and prints |
| 2 | //! only counts and where each refusal lies, never a value (see |
| 3 | //! `oxedyne_fe2o3_o3db_sync::textscan`). |
| 4 | //! |
| 5 | //! ```ignore |
| 6 | //! jdat_store_check --o3db DIR --key KEYFILE # Steel or Daimond: DIR is a copy of the Ozone store |
| 7 | //! ``` |
| 8 | //! |
| 9 | //! Oxegen's own tables are framed text files, checked by `jdat_check --logs DIR` in |
| 10 | //! `oxedyne_fe2o3_jdat`, which builds static. This one links the Ozone store and is built for the |
| 11 | //! host's glibc. |
| 12 | //! |
| 13 | //! Exit status: 0 when every text read, 1 when any was refused, 2 on a usage error, an unreadable |
| 14 | //! store, or a store that holds nothing to read. |
| 15 | //! |
| 16 | //! # Safety |
| 17 | //! - Scan a `cp -a` COPY, never a live store: a second process on one store is never safe, even |
| 18 | //! though an open that is only read from writes nothing (since 2026-10-09). |
| 19 | //! - The store is opened for the gateway's parameterisation (16-byte `u128` user ids, AES-256-GCM |
| 20 | //! at rest, CRC-32), as `o3db_migrate` is (`oxedyne_fe2o3_o3db_sync::gateway`). The at-rest key |
| 21 | //! is read from the path given and is never defaulted. |
| 22 | //! |
| 23 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 24 | //! Anthropic Claude |
| 25 | |
| 26 | use oxedyne_fe2o3_core::prelude::*; |
| 27 | use oxedyne_fe2o3_o3db_sync::{ |
| 28 | base::constant, |
| 29 | comm::response::Wait, |
| 30 | gateway, |
| 31 | textscan, |
| 32 | }; |
| 33 | use oxedyne_fe2o3_jdat::string::scan::TextScan; |
| 34 | |
| 35 | use std::{ |
| 36 | path::PathBuf, |
| 37 | process, |
| 38 | thread, |
| 39 | time::Duration, |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | fn main() { |
| 44 | match run() { |
| 45 | Ok(code) => process::exit(code), |
| 46 | Err(e) => { |
| 47 | eprintln!("jdat_store_check FAILED: {}", e); |
| 48 | process::exit(2); |
| 49 | }, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn run() -> Outcome<i32> { |
| 54 | log_set_level!("warn"); |
| 55 | let (dir, key_path) = res!(parse()); |
| 56 | let key = res!(gateway::load_key(&key_path)); |
| 57 | let (db, answered) = res!(gateway::open_store(&dir, None, &key, false, "jdat-store-check")); |
| 58 | // A report of "clean" from a store that did not answer would be false, so this tool, which |
| 59 | // only reports, stops here. |
| 60 | if let Err(e) = gateway::require_answer(answered, "jdat-store-check", &dir) { |
| 61 | let _ = db.shutdown(); |
| 62 | return Err(e); |
| 63 | } |
| 64 | let wait = Wait { |
| 65 | max_wait: Duration::from_secs(3600), |
| 66 | check_interval: constant::CHECK_INTERVAL, |
| 67 | }; |
| 68 | let mut scan = TextScan::default(); |
| 69 | let outcome = textscan::scan_o3db(db.api(), wait, &mut scan); |
| 70 | res!(db.shutdown()); |
| 71 | thread::sleep(Duration::from_secs(1)); |
| 72 | res!(outcome); |
| 73 | if scan.records == 0 { |
| 74 | return Err(err!( |
| 75 | "The store at {:?} holds no live key; wrong directory or wrong key?", dir; |
| 76 | Missing, Input)); |
| 77 | } |
| 78 | print!("{}", scan.report()); |
| 79 | if scan.is_clean() { |
| 80 | println!("CLEAN: every text read."); |
| 81 | Ok(0) |
| 82 | } else { |
| 83 | println!("NOT CLEAN: see the REFUSED lines."); |
| 84 | Ok(1) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn parse() -> Outcome<(PathBuf, PathBuf)> { |
| 89 | let usage = "Usage: --o3db DIR --key KEYFILE."; |
| 90 | let mut o3db: Option<PathBuf> = None; |
| 91 | let mut key: Option<PathBuf> = None; |
| 92 | let mut it = std::env::args().skip(1); |
| 93 | while let Some(arg) = it.next() { |
| 94 | let slot = match arg.as_str() { |
| 95 | "--o3db" => &mut o3db, |
| 96 | "--key" => &mut key, |
| 97 | other => return Err(err!("Unrecognised argument {:?}. {}", other, usage; |
| 98 | Invalid, Input)), |
| 99 | }; |
| 100 | *slot = Some(PathBuf::from(res!(it.next().ok_or_else(|| err!( |
| 101 | "Argument {} needs a value. {}", arg, usage; Missing, Input))))); |
| 102 | } |
| 103 | match (o3db, key) { |
| 104 | (Some(dir), Some(key)) => Ok((dir, key)), |
| 105 | _ => Err(err!("Give --o3db DIR with --key KEYFILE. {}", usage; Invalid, Input)), |
| 106 | } |
| 107 | } |