idb/cli/mod.rs
1//! CLI subcommand implementations for the `inno` binary.
2//!
3//! The `inno` binary provides thirteen subcommands for analyzing InnoDB data files,
4//! redo logs, and system tablespaces. CLI argument parsing uses clap derive macros,
5//! with the top-level [`app::Cli`] struct and [`app::Commands`] enum defined in
6//! [`app`] and shared between `main.rs` and `build.rs` (for man page generation)
7//! via `include!()`.
8//!
9//! Each subcommand module follows the same pattern: an `Options` struct holding
10//! the parsed arguments and a `pub fn execute(opts, writer) -> Result<(), IdbError>`
11//! entry point. The `writer: &mut dyn Write` parameter allows output to be
12//! captured in tests or redirected to a file via the global `--output` flag.
13//!
14//! # Subcommands
15//!
16//! | Command | Module | Purpose |
17//! |---------|--------|---------|
18//! | `inno parse` | [`parse`] | Parse FIL headers for every page and show a page-type summary table |
19//! | `inno pages` | [`pages`] | Deep structure analysis of INDEX, UNDO, BLOB/LOB, and SDI pages |
20//! | `inno dump` | [`dump`] | Hex dump of raw bytes by page number or absolute file offset |
21//! | `inno checksum` | [`checksum`] | Validate CRC-32C and legacy InnoDB checksums for every page |
22//! | `inno diff` | [`diff`] | Compare two tablespace files page-by-page and report differences |
23//! | `inno watch` | [`watch`] | Monitor a tablespace file for page-level changes in real time |
24//! | `inno corrupt` | [`corrupt`] | Inject random bytes into a page for testing recovery workflows |
25//! | `inno recover` | [`recover`] | Assess page-level recoverability and count salvageable records |
26//! | `inno find` | [`find`] | Search a MySQL data directory for pages matching a page number |
27//! | `inno tsid` | [`tsid`] | List or look up tablespace (space) IDs across `.ibd`/`.ibu` files |
28//! | `inno sdi` | [`sdi`] | Extract SDI metadata (MySQL 8.0+ serialized data dictionary) |
29//! | `inno log` | [`log`] | Analyze redo log file headers, checkpoints, and data blocks |
30//! | `inno info` | [`info`] | Inspect `ibdata1`, compare LSNs, or query a live MySQL instance |
31//!
32//! # Common patterns
33//!
34//! - **`--json`** — Every subcommand supports structured JSON output via
35//! `#[derive(Serialize)]` structs and `serde_json`.
36//! - **`--page-size`** — Override auto-detected page size (useful for non-standard
37//! 4K, 8K, 32K, or 64K tablespaces).
38//! - **`--verbose` / `-v`** — Show additional detail such as per-page checksum
39//! status, FSEG internals, or MLOG record types.
40//! - **`--color`** (global) — Control colored terminal output (`auto`, `always`,
41//! `never`).
42//! - **`--output` / `-o`** (global) — Redirect output to a file instead of stdout.
43//!
44//! Progress bars (via [`indicatif`]) are displayed for long-running operations
45//! in `parse`, `checksum`, and `find`. The `wprintln!` and `wprint!` macros
46//! wrap `writeln!`/`write!` to convert `io::Error` into `IdbError`.
47
48pub mod app;
49pub mod checksum;
50pub mod corrupt;
51pub mod diff;
52pub mod dump;
53pub mod find;
54pub mod info;
55pub mod log;
56pub mod pages;
57pub mod parse;
58pub mod recover;
59pub mod sdi;
60pub mod tsid;
61pub mod watch;
62
63/// Write a line to the given writer, converting io::Error to IdbError.
64macro_rules! wprintln {
65 ($w:expr) => {
66 writeln!($w).map_err(|e| $crate::IdbError::Io(e.to_string()))
67 };
68 ($w:expr, $($arg:tt)*) => {
69 writeln!($w, $($arg)*).map_err(|e| $crate::IdbError::Io(e.to_string()))
70 };
71}
72
73/// Write (without newline) to the given writer, converting io::Error to IdbError.
74macro_rules! wprint {
75 ($w:expr, $($arg:tt)*) => {
76 write!($w, $($arg)*).map_err(|e| $crate::IdbError::Io(e.to_string()))
77 };
78}
79
80pub(crate) use wprint;
81pub(crate) use wprintln;
82
83use crate::innodb::decryption::DecryptionContext;
84use crate::innodb::keyring::Keyring;
85use crate::innodb::tablespace::Tablespace;
86use crate::IdbError;
87use indicatif::{ProgressBar, ProgressStyle};
88
89/// Set up decryption on a tablespace if a keyring path is provided.
90///
91/// Loads the keyring file, reads the encryption info from page 0,
92/// decrypts the tablespace key, and installs the decryption context
93/// on the tablespace for transparent page decryption.
94pub(crate) fn setup_decryption(
95 ts: &mut Tablespace,
96 keyring_path: &str,
97) -> Result<(), IdbError> {
98 let keyring = Keyring::load(keyring_path)?;
99 let enc_info = ts.encryption_info().ok_or_else(|| {
100 IdbError::Parse(
101 "Keyring provided but tablespace has no encryption info on page 0".to_string(),
102 )
103 })?;
104 let ctx = DecryptionContext::from_encryption_info(enc_info, &keyring)?;
105 ts.set_decryption_context(ctx);
106 Ok(())
107}
108
109/// Create a styled progress bar for iterating over pages or files.
110pub(crate) fn create_progress_bar(count: u64, unit: &str) -> ProgressBar {
111 let pb = ProgressBar::new(count);
112 pb.set_style(
113 ProgressStyle::default_bar()
114 .template(&format!(
115 "{{spinner:.green}} [{{bar:40.cyan/blue}}] {{pos}}/{{len}} {} ({{eta}})",
116 unit
117 ))
118 .unwrap()
119 .progress_chars("#>-"),
120 );
121 pb
122}