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