Skip to main content

idb/
lib.rs

1//! InnoDB file analysis toolkit.
2//!
3//! The `innodb-utils` crate (library name `idb`) provides Rust types and
4//! functions for parsing, inspecting, and manipulating InnoDB tablespace
5//! files (`.ibd`), redo log files, and system tablespace data (`ibdata1`).
6//!
7//! # CLI Reference
8//!
9//! Install the `inno` binary and use its subcommands to work with InnoDB
10//! files from the command line.
11//!
12//! ## Installation
13//!
14//! ```text
15//! cargo install innodb-utils          # crates.io
16//! brew install ringo380/tap/inno      # Homebrew (macOS/Linux)
17//! ```
18//!
19//! Pre-built binaries for Linux and macOS (x86_64 + aarch64) are available
20//! on the [GitHub releases page](https://github.com/ringo380/idb-utils/releases).
21//!
22//! ## Subcommands
23//!
24//! | Command | Purpose |
25//! |---------|---------|
26//! | [`inno parse`](cli::app::Commands::Parse) | Parse `.ibd` file and display page summary |
27//! | [`inno pages`](cli::app::Commands::Pages) | Detailed page structure analysis (INDEX, UNDO, LOB, SDI) |
28//! | [`inno dump`](cli::app::Commands::Dump) | Hex dump of raw page bytes |
29//! | [`inno checksum`](cli::app::Commands::Checksum) | Validate page checksums (CRC-32C, legacy, MariaDB full\_crc32) |
30//! | [`inno diff`](cli::app::Commands::Diff) | Compare two tablespace files page-by-page |
31//! | [`inno watch`](cli::app::Commands::Watch) | Monitor a tablespace file for page-level changes |
32//! | [`inno corrupt`](cli::app::Commands::Corrupt) | Intentionally corrupt pages for testing |
33//! | [`inno recover`](cli::app::Commands::Recover) | Assess page-level recoverability and count salvageable records |
34//! | [`inno find`](cli::app::Commands::Find) | Search data directory for pages by number |
35//! | [`inno tsid`](cli::app::Commands::Tsid) | List or find tablespace IDs |
36//! | [`inno sdi`](cli::app::Commands::Sdi) | Extract SDI metadata (MySQL 8.0+) |
37//! | [`inno schema`](cli::app::Commands::Schema) | Extract schema and reconstruct DDL from SDI |
38//! | [`inno log`](cli::app::Commands::Log) | Analyze InnoDB redo log files |
39//! | [`inno info`](cli::app::Commands::Info) | Inspect ibdata1, compare LSNs, query MySQL |
40//!
41//! ## Global options
42//!
43//! All subcommands accept `--color <auto|always|never>` and `--output <file>`.
44//! Most subcommands also accept `--json` for machine-readable output and
45//! `--page-size` to override auto-detection.
46//!
47//! See the [`cli`] module for full details.
48//!
49//! # Library API
50//!
51//! Add `idb` as a dependency to use the parsing library directly:
52//!
53//! ```toml
54//! [dependencies]
55//! idb = { package = "innodb-utils", version = "1" }
56//! ```
57//!
58//! ## Quick example
59//!
60//! ```no_run
61//! use idb::innodb::tablespace::Tablespace;
62//! use idb::innodb::checksum::validate_checksum;
63//! use idb::innodb::page::FilHeader;
64//!
65//! // Open a tablespace (page size is auto-detected from page 0)
66//! let mut ts = Tablespace::open("table.ibd").unwrap();
67//!
68//! // Read and inspect a page
69//! let page = ts.read_page(0).unwrap();
70//! let header = FilHeader::parse(&page).unwrap();
71//! println!("Page type: {}", header.page_type);
72//!
73//! // Validate the page checksum
74//! let result = validate_checksum(&page, ts.page_size(), None);
75//! println!("Checksum valid: {}", result.valid);
76//! ```
77//!
78//! ## Key entry points
79//!
80//! | Type / Function | Purpose |
81//! |-----------------|---------|
82//! | [`Tablespace`](innodb::tablespace::Tablespace) | Open `.ibd` files, read pages, iterate |
83//! | [`FilHeader`](innodb::page::FilHeader) | Parse the 38-byte header on every InnoDB page |
84//! | [`PageType`](innodb::page_types::PageType) | Map page type codes to names and descriptions |
85//! | [`validate_checksum`](innodb::checksum::validate_checksum) | CRC-32C, legacy, and MariaDB full\_crc32 validation |
86//! | [`extract_sdi_from_pages`](innodb::sdi::extract_sdi_from_pages) | SDI metadata extraction (MySQL 8.0+) |
87//! | [`LogFile`](innodb::log::LogFile) | Read and inspect redo log files |
88//! | [`VendorInfo`](innodb::vendor::VendorInfo) | Detected vendor (MySQL / Percona / MariaDB) and format details |
89//!
90//! ## Module overview
91//!
92//! | Module | Purpose |
93//! |--------|---------|
94//! | [`innodb::tablespace`] | File I/O, page size detection, page iteration |
95//! | [`innodb::page`] | FIL header/trailer, FSP header parsing |
96//! | [`innodb::page_types`] | Page type enum with names and descriptions |
97//! | [`innodb::checksum`] | CRC-32C and legacy InnoDB checksum algorithms |
98//! | [`innodb::index`] | INDEX page internals (B+Tree header, FSEG) |
99//! | [`innodb::record`] | Row-level record parsing (compact format) |
100//! | [`innodb::sdi`] | SDI metadata extraction and decompression |
101//! | [`innodb::log`] | Redo log file structure and block parsing |
102//! | [`innodb::undo`] | UNDO log page structures |
103//! | [`innodb::lob`] | Large object (BLOB/LOB) page headers |
104//! | [`innodb::compression`] | Compression detection and decompression |
105//! | [`innodb::encryption`] | Encryption detection and encryption info parsing |
106//! | [`innodb::decryption`] | AES-256-CBC page decryption with keyring support |
107//! | [`innodb::keyring`] | MySQL `keyring_file` plugin binary format reader |
108//! | [`innodb::vendor`] | Vendor detection (MySQL, Percona, MariaDB) and format info |
109//! | [`innodb::constants`] | InnoDB page/file structure constants |
110//!
111//! ## Feature flags
112//!
113//! | Feature | Default | Description |
114//! |---------|---------|-------------|
115//! | `mysql` | off | Enables live MySQL queries via `mysql_async` + `tokio` (used by `inno info`). |
116//!
117//! # WASM
118//!
119//! The library can be compiled to WebAssembly for browser-based InnoDB file analysis:
120//!
121//! ```bash
122//! wasm-pack build --release --target web --no-default-features
123//! ```
124//!
125//! The WASM API exposes JSON-returning functions for all core operations:
126//! `get_tablespace_info`, `parse_tablespace`, `analyze_pages`,
127//! `validate_checksums`, `extract_sdi`, `diff_tablespaces`, `hex_dump_page`,
128//! `assess_recovery`, and `parse_redo_log`. Each function accepts raw file
129//! bytes as a `Uint8Array` and returns a JSON string (or plain text for hex
130//! dumps). See the [`wasm`] module documentation for details on individual
131//! function signatures and return schemas.
132//!
133//! A live web analyzer built on these bindings is available at
134//! <https://ringo380.github.io/idb-utils/>.
135
136#[cfg(feature = "cli")]
137pub mod cli;
138pub mod innodb;
139pub mod util;
140#[cfg(target_arch = "wasm32")]
141pub mod wasm;
142
143use thiserror::Error;
144
145/// Errors returned by `idb` operations.
146#[derive(Error, Debug)]
147pub enum IdbError {
148    /// An I/O error occurred (file open, read, seek, or write failure).
149    #[error("I/O error: {0}")]
150    Io(String),
151
152    /// A parse error occurred (malformed binary data or unexpected values).
153    #[error("Parse error: {0}")]
154    Parse(String),
155
156    /// An invalid argument was supplied (out-of-range page number, bad option, etc.).
157    #[error("Invalid argument: {0}")]
158    Argument(String),
159}