idb/cli/mod.rs
1//! CLI subcommand implementations for the `inno` binary.
2//!
3//! The `inno` binary provides twenty 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 repair` | [`repair`] | Recalculate and fix corrupt page checksums |
27//! | `inno find` | [`find`] | Search a MySQL data directory for pages matching a page number |
28//! | `inno tsid` | [`tsid`] | List or look up tablespace (space) IDs across `.ibd`/`.ibu` files |
29//! | `inno sdi` | [`sdi`] | Extract SDI metadata (MySQL 8.0+ serialized data dictionary) |
30//! | `inno schema` | [`schema`] | Extract schema and reconstruct DDL from tablespace metadata |
31//! | `inno export` | [`export`] | Export record-level data from INDEX pages as CSV, JSON, or hex |
32//! | `inno health` | [`health`] | Per-index B+Tree health metrics (fill factor, fragmentation, garbage) |
33//! | `inno log` | [`log`] | Analyze redo log file headers, checkpoints, and data blocks |
34//! | `inno info` | [`info`] | Inspect `ibdata1`, compare LSNs, or query a live MySQL instance |
35//! | `inno defrag` | [`defrag`] | Defragment a tablespace by reclaiming free space and reordering pages |
36//! | `inno transplant` | [`transplant`] | Copy specific pages from a donor tablespace into a target |
37//! | `inno audit` | [`audit`] | Audit a data directory for integrity, health, or corrupt pages |
38//! | `inno completions` | — | Generate shell completion scripts for bash, zsh, fish, or powershell |
39//!
40//! # Common patterns
41//!
42//! - **`--json`** — Every subcommand supports structured JSON output via
43//! `#[derive(Serialize)]` structs and `serde_json`.
44//! - **`--page-size`** — Override auto-detected page size (useful for non-standard
45//! 4K, 8K, 32K, or 64K tablespaces).
46//! - **`--verbose` / `-v`** — Show additional detail such as per-page checksum
47//! status, FSEG internals, or MLOG record types.
48//! - **`--color`** (global) — Control colored terminal output (`auto`, `always`,
49//! `never`).
50//! - **`--output` / `-o`** (global) — Redirect output to a file instead of stdout.
51//!
52//! Progress bars (via [`indicatif`]) are displayed for long-running operations
53//! in `parse`, `checksum`, `find`, and `audit`. The `wprintln!` and `wprint!` macros
54//! wrap `writeln!`/`write!` to convert `io::Error` into `IdbError`.
55
56pub mod app;
57pub mod audit;
58pub mod binlog;
59pub mod checksum;
60pub mod compat;
61pub mod corrupt;
62pub mod defrag;
63pub mod diff;
64pub mod dump;
65pub mod export;
66pub mod find;
67pub mod health;
68pub mod info;
69pub mod log;
70pub mod pages;
71pub mod parse;
72pub mod recover;
73pub mod repair;
74pub mod schema;
75pub mod sdi;
76pub mod transplant;
77pub mod tsid;
78pub mod undelete;
79pub mod undo;
80pub mod validate;
81pub mod verify;
82pub mod watch;
83
84/// Write a line to the given writer, converting io::Error to IdbError.
85macro_rules! wprintln {
86 ($w:expr) => {
87 writeln!($w).map_err(|e| $crate::IdbError::Io(e.to_string()))
88 };
89 ($w:expr, $($arg:tt)*) => {
90 writeln!($w, $($arg)*).map_err(|e| $crate::IdbError::Io(e.to_string()))
91 };
92}
93
94/// Write (without newline) to the given writer, converting io::Error to IdbError.
95macro_rules! wprint {
96 ($w:expr, $($arg:tt)*) => {
97 write!($w, $($arg)*).map_err(|e| $crate::IdbError::Io(e.to_string()))
98 };
99}
100
101pub(crate) use wprint;
102pub(crate) use wprintln;
103
104/// Escape a string value for CSV output.
105///
106/// If the value contains a comma, double-quote, or newline, it is enclosed
107/// in double-quotes with internal double-quotes doubled. Otherwise the value
108/// is returned as-is.
109pub(crate) fn csv_escape(val: &str) -> String {
110 if val.contains(',') || val.contains('"') || val.contains('\n') {
111 format!("\"{}\"", val.replace('"', "\"\""))
112 } else {
113 val.to_string()
114 }
115}
116
117use crate::innodb::decryption::DecryptionContext;
118use crate::innodb::keyring::Keyring;
119use crate::innodb::tablespace::Tablespace;
120use crate::IdbError;
121use indicatif::{ProgressBar, ProgressStyle};
122
123/// Open a tablespace file, selecting mmap or buffered I/O based on the flag.
124///
125/// When `use_mmap` is true, the file is memory-mapped via `mmap(2)` for
126/// potentially better performance on large files (especially with parallel
127/// processing). When `page_size` is `Some`, auto-detection is bypassed.
128pub(crate) fn open_tablespace(
129 path: &str,
130 page_size: Option<u32>,
131 use_mmap: bool,
132) -> Result<Tablespace, IdbError> {
133 match (use_mmap, page_size) {
134 (true, Some(ps)) => Tablespace::open_mmap_with_page_size(path, ps),
135 (true, None) => Tablespace::open_mmap(path),
136 (false, Some(ps)) => Tablespace::open_with_page_size(path, ps),
137 (false, None) => Tablespace::open(path),
138 }
139}
140
141/// Set up decryption on a tablespace if a keyring path is provided.
142///
143/// Loads the keyring file, reads the encryption info from page 0,
144/// decrypts the tablespace key, and installs the decryption context
145/// on the tablespace for transparent page decryption.
146pub(crate) fn setup_decryption(ts: &mut Tablespace, keyring_path: &str) -> Result<(), IdbError> {
147 let keyring = Keyring::load(keyring_path)?;
148 let enc_info = ts.encryption_info().ok_or_else(|| {
149 IdbError::Parse(
150 "Keyring provided but tablespace has no encryption info on page 0".to_string(),
151 )
152 })?;
153 let ctx = DecryptionContext::from_encryption_info(enc_info, &keyring)?;
154 ts.set_decryption_context(ctx);
155 Ok(())
156}
157
158/// Create a styled progress bar for iterating over pages or files.
159pub(crate) fn create_progress_bar(count: u64, unit: &str) -> ProgressBar {
160 let pb = ProgressBar::new(count);
161 pb.set_style(
162 ProgressStyle::default_bar()
163 .template(&format!(
164 "{{spinner:.green}} [{{bar:40.cyan/blue}}] {{pos}}/{{len}} {} ({{eta}})",
165 unit
166 ))
167 .unwrap()
168 .progress_chars("#>-"),
169 );
170 pb
171}