blkreader/lib.rs
1//! # blkreader
2//!
3//! A Rust crate for reading file data directly from block devices using extent information.
4//!
5//! ## Overview
6//!
7//! `blkreader` provides a mechanism to read file data directly from the underlying block device
8//! by querying the file's extent information via the Linux `FIEMAP` ioctl. This is particularly
9//! useful in scenarios where:
10//!
11//! - Storage space has been pre-allocated using `fallocate` + `fdatasync`
12//! - Extent information has been persisted to disk
13//! - The file's data may not have been fully synced (written extent state not persisted)
14//! - You need to recover raw data from the block device
15//!
16//! ## Features
17//!
18//! - Query file extent information using `FIEMAP` ioctl via [`blkmap`]
19//! - Resolve block device paths using [`blkpath`]
20//! - Read data directly from block devices using Direct I/O
21//! - Global block device cache for improved performance
22//! - Configurable handling of holes and unwritten extents
23//! - Fallback to regular file I/O when safe
24//!
25//! ## Direct I/O Alignment Requirements
26//!
27//! When reading directly from block devices (not using fallback mode), the following
28//! alignment requirements must be met for Direct I/O:
29//!
30//! - **Buffer alignment**: The buffer must be aligned to at least 512 bytes (sector size).
31//! For optimal performance on modern devices, 4096-byte alignment is recommended.
32//! - **Offset alignment**: The read offset should be aligned to 512 bytes.
33//! - **Length alignment**: The read length should be aligned to 512 bytes.
34//!
35//! If alignment requirements are not met, the underlying read may fail with an
36//! `EINVAL` error. The CLI tool handles alignment automatically.
37//!
38//! ## Example
39//!
40//! ```no_run
41//! use blkreader::{BlkReader, Options};
42//! use std::path::Path;
43//!
44//! let path = Path::new("/path/to/file");
45//! // Buffer should be aligned; using 4096 bytes which is a common block size
46//! let mut buf = vec![0u8; 4096];
47//!
48//! // Simple read (offset 0 is aligned)
49//! let bytes_read = path.blk_read_at(&mut buf, 0).unwrap();
50//!
51//! // Read with options
52//! let options = Options::default();
53//! let state = path.blk_read_at_opt(&mut buf, 0, &options).unwrap();
54//! println!("Read {} bytes from {}", state.bytes_read, state.block_device_path.display());
55//! ```
56//!
57//! ## Safety
58//!
59//! This crate requires root privileges to read from block devices. The CLI tool
60//! automatically requests sudo permissions when needed.
61
62mod cache;
63mod options;
64mod reader;
65mod state;
66
67pub use blkmap::FiemapExtent as Extent;
68pub use options::Options;
69pub use reader::BlkReader;
70pub use state::State;