Skip to main content

idb/cli/
mod.rs

1//! CLI subcommand implementations for the `inno` binary.
2//!
3//! The `inno` binary provides eleven 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 corrupt` | [`corrupt`] | Inject random bytes into a page for testing recovery workflows |
23//! | `inno recover` | [`recover`] | Assess page-level recoverability and count salvageable records |
24//! | `inno find` | [`find`] | Search a MySQL data directory for pages matching a page number |
25//! | `inno tsid` | [`tsid`] | List or look up tablespace (space) IDs across `.ibd`/`.ibu` files |
26//! | `inno sdi` | [`sdi`] | Extract SDI metadata (MySQL 8.0+ serialized data dictionary) |
27//! | `inno log` | [`log`] | Analyze redo log file headers, checkpoints, and data blocks |
28//! | `inno info` | [`info`] | Inspect `ibdata1`, compare LSNs, or query a live MySQL instance |
29//!
30//! # Common patterns
31//!
32//! - **`--json`** — Every subcommand supports structured JSON output via
33//!   `#[derive(Serialize)]` structs and `serde_json`.
34//! - **`--page-size`** — Override auto-detected page size (useful for non-standard
35//!   4K, 8K, 32K, or 64K tablespaces).
36//! - **`--verbose` / `-v`** — Show additional detail such as per-page checksum
37//!   status, FSEG internals, or MLOG record types.
38//! - **`--color`** (global) — Control colored terminal output (`auto`, `always`,
39//!   `never`).
40//! - **`--output` / `-o`** (global) — Redirect output to a file instead of stdout.
41//!
42//! Progress bars (via [`indicatif`]) are displayed for long-running operations
43//! in `parse`, `checksum`, and `find`. The `wprintln!` and `wprint!` macros
44//! wrap `writeln!`/`write!` to convert `io::Error` into `IdbError`.
45
46pub mod app;
47pub mod checksum;
48pub mod corrupt;
49pub mod dump;
50pub mod find;
51pub mod info;
52pub mod log;
53pub mod pages;
54pub mod parse;
55pub mod recover;
56pub mod sdi;
57pub mod tsid;
58
59/// Write a line to the given writer, converting io::Error to IdbError.
60macro_rules! wprintln {
61    ($w:expr) => {
62        writeln!($w).map_err(|e| $crate::IdbError::Io(e.to_string()))
63    };
64    ($w:expr, $($arg:tt)*) => {
65        writeln!($w, $($arg)*).map_err(|e| $crate::IdbError::Io(e.to_string()))
66    };
67}
68
69/// Write (without newline) to the given writer, converting io::Error to IdbError.
70macro_rules! wprint {
71    ($w:expr, $($arg:tt)*) => {
72        write!($w, $($arg)*).map_err(|e| $crate::IdbError::Io(e.to_string()))
73    };
74}
75
76pub(crate) use wprintln;
77pub(crate) use wprint;
78
79use indicatif::{ProgressBar, ProgressStyle};
80
81/// Create a styled progress bar for iterating over pages or files.
82pub(crate) fn create_progress_bar(count: u64, unit: &str) -> ProgressBar {
83    let pb = ProgressBar::new(count);
84    pb.set_style(
85        ProgressStyle::default_bar()
86            .template(&format!(
87                "{{spinner:.green}} [{{bar:40.cyan/blue}}] {{pos}}/{{len}} {} ({{eta}})",
88                unit
89            ))
90            .unwrap()
91            .progress_chars("#>-"),
92    );
93    pb
94}