Skip to main content

livedisk/
lib.rs

1//! # livedisk
2//!
3//! Cross-platform enumeration of the **live system's** physical disks and
4//! partitions — `diskutil list` / `lsblk` / `diskpart`, but as a library with
5//! one unified model across macOS, Linux, and Windows.
6//!
7//! ```no_run
8//! for disk in livedisk::enumerate()? {
9//!     println!("{} — {}", disk.name, livedisk::human_size(disk.size_bytes));
10//!     for part in &disk.partitions {
11//!         println!("  {} {}", part.name, livedisk::human_size(part.size_bytes));
12//!     }
13//! }
14//! # Ok::<(), livedisk::Error>(())
15//! ```
16//!
17//! Discovery is the only OS-specific part. Each backend (sysfs on Linux, the
18//! `IOKit` `IOMedia` registry on macOS, `DeviceIoControl` on Windows) fills the
19//! same [`PhysicalDisk`]/[`Partition`] structs; everything downstream — the
20//! [`render_overview`] bar chart, the per-disk [`render_disk_bar`], the
21//! [`render_listing`] view, and the JSON form — is platform-agnostic.
22//!
23//! [`open_device`] opens a chosen device node as a sized `Read + Seek` so a
24//! partition/filesystem analyzer can run on the live disk exactly as it would on
25//! an image file.
26//!
27//! Listing layout/metadata works **unprivileged** on all three platforms (it
28//! reads the kernel's device registry, not raw sectors); only *reading a device*
29//! needs root/Administrator. Backends therefore never silently return an empty
30//! list on a permission problem — they surface [`Error`].
31
32use core::fmt::Write as _;
33use std::fs::File;
34use std::io::{Seek, SeekFrom};
35use std::path::Path;
36
37mod bar;
38pub use bar::{render_disk_bar, render_overview};
39
40// Pure sysfs parsing for the Linux backend lives in its own module compiled on
41// every target, so its tests run regardless of host; only the file/dir I/O in
42// `linux` is Linux-gated. `dead_code` is expected when not building for Linux.
43#[cfg(target_os = "linux")]
44mod linux;
45#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
46mod sysfs;
47
48// Pure DRIVE_LAYOUT_INFORMATION_EX byte parsing for the Windows backend, on the
49// same always-compiled / Windows-gated-I/O split as `sysfs`/`linux`.
50#[cfg_attr(not(windows), allow(dead_code))]
51mod drive_layout;
52#[cfg(target_os = "macos")]
53mod macos;
54#[cfg(windows)]
55mod windows;
56
57/// Internal parsers of externally-supplied data, exposed **only** under the
58/// `fuzzing` feature for the fuzz harness — not part of the public API. The
59/// wrappers discard results; fuzzing asserts these never panic on malformed
60/// input.
61#[cfg(feature = "fuzzing")]
62#[doc(hidden)]
63pub mod fuzz_api {
64    /// Drive the Windows `DRIVE_LAYOUT_INFORMATION_EX` byte parser.
65    pub fn parse_drive_layout(buf: &[u8]) {
66        let _ = crate::drive_layout::parse_drive_layout(buf);
67    }
68
69    /// Drive the `/proc/mounts` text parser.
70    pub fn parse_mounts(s: &str) {
71        let _ = crate::sysfs::parse_mounts(s);
72    }
73}
74
75/// A whole physical (or, on macOS, synthesized) disk on the live system.
76///
77/// `size_bytes` and the sector sizes come from the OS/driver layer, not from the
78/// on-disk partition table — only the kernel knows the device's true geometry.
79#[derive(Debug, Clone, PartialEq, Eq)]
80#[cfg_attr(feature = "serde", derive(serde::Serialize))]
81pub struct PhysicalDisk {
82    /// OS path to open for raw access (`/dev/disk0`, `/dev/sda`,
83    /// `\\.\PhysicalDrive0`).
84    pub device_path: String,
85    /// Short kernel identifier (`disk0`, `sda`, `PhysicalDrive0`).
86    pub name: String,
87    /// Total device size in bytes, as reported by the driver.
88    pub size_bytes: u64,
89    /// Smallest addressable I/O unit (logical sector), in bytes.
90    pub logical_sector_size: u32,
91    /// Physical sector size in bytes (4096 on 4Kn/512e media; may exceed
92    /// `logical_sector_size`).
93    pub physical_sector_size: u32,
94    /// Device model string, when the driver exposes one.
95    pub model: Option<String>,
96    /// Device serial number, when the driver exposes one.
97    pub serial: Option<String>,
98    /// Removable media (USB stick, SD card, optical).
99    pub removable: bool,
100    /// Device is write-protected / read-only at the driver level.
101    pub read_only: bool,
102    /// Not a backing physical device but a kernel-synthesized one (macOS APFS
103    /// container, Linux device-mapper/LVM). Real evidence imaging targets the
104    /// backing physical disk; synthesized disks are shown for completeness.
105    pub synthesized: bool,
106    /// Partitions/slices carved out of this disk, in on-disk order.
107    pub partitions: Vec<Partition>,
108}
109
110/// A partition (slice/volume) within a [`PhysicalDisk`].
111#[derive(Debug, Clone, PartialEq, Eq)]
112#[cfg_attr(feature = "serde", derive(serde::Serialize))]
113pub struct Partition {
114    /// OS path to open for raw access to just this partition.
115    pub device_path: String,
116    /// Short kernel identifier (`disk0s1`, `sda1`, `nvme0n1p1`).
117    pub name: String,
118    /// Byte offset of the partition's first sector from the start of the disk.
119    pub start_offset: u64,
120    /// Partition length in bytes.
121    pub size_bytes: u64,
122    /// Partition type as the OS names it (GPT type GUID/name, MBR type byte, or
123    /// platform content hint), when known.
124    pub partition_type: Option<String>,
125    /// Current mount point, when the partition is mounted.
126    pub mount_point: Option<String>,
127    /// Mounted filesystem type, when known.
128    pub filesystem: Option<String>,
129    /// Volume label, when known.
130    pub label: Option<String>,
131}
132
133/// Failure enumerating live devices.
134#[derive(Debug, thiserror::Error)]
135pub enum Error {
136    /// Live enumeration has no backend for this target OS.
137    #[error("live device enumeration is not supported on this platform")]
138    Unsupported,
139    /// An I/O error while reading the OS device registry.
140    #[error("I/O error enumerating devices: {0}")]
141    Io(#[from] std::io::Error),
142    /// The platform enumeration API returned an error.
143    #[error("device enumeration failed: {0}")]
144    Os(String),
145}
146
147/// Enumerate every physical disk on the live system, each with its partitions.
148///
149/// Dispatches to the platform backend. The list is best-effort complete: a disk
150/// whose details cannot be read is still listed with whatever the OS provided.
151///
152/// # Errors
153/// [`Error::Unsupported`] on a target without a backend, [`Error::Io`] /
154/// [`Error::Os`] when the OS device registry cannot be read.
155pub fn enumerate() -> Result<Vec<PhysicalDisk>, Error> {
156    #[cfg(target_os = "linux")]
157    {
158        linux::enumerate()
159    }
160    #[cfg(target_os = "macos")]
161    {
162        macos::enumerate()
163    }
164    #[cfg(windows)]
165    {
166        windows::enumerate()
167    }
168    #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
169    {
170        Err(Error::Unsupported)
171    }
172}
173
174/// Open a live block device for reading and return it with its size in bytes.
175///
176/// Block devices report `metadata().len() == 0`, so the size is obtained by
177/// seeking to the end; the handle is rewound to the start before returning, so
178/// the caller gets a fresh `Read + Seek` view ready for partition/filesystem
179/// analysis. Reading a raw device typically requires root/Administrator — the
180/// returned [`std::io::Error`] surfaces a permission failure rather than masking
181/// it.
182///
183/// # Errors
184/// Propagates any I/O error from opening or seeking the device.
185pub fn open_device(path: &Path) -> std::io::Result<(File, u64)> {
186    let mut file = File::open(path)?;
187    let size = file.seek(SeekFrom::End(0))?;
188    file.seek(SeekFrom::Start(0))?;
189    Ok((file, size))
190}
191
192/// Format a byte count the way disk utilities do — decimal (SI) units with one
193/// fractional digit (`4.0 TB`, `524.3 MB`, `24.6 KB`), matching `diskutil`/
194/// `lsblk` so output is recognisable. Bytes under 1000 render as `N B`.
195#[must_use]
196pub fn human_size(bytes: u64) -> String {
197    const UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"];
198    if bytes < 1000 {
199        return format!("{bytes} B");
200    }
201    let mut value = bytes as f64;
202    let mut unit = 0;
203    while value >= 1000.0 && unit < UNITS.len() - 1 {
204        value /= 1000.0;
205        unit += 1;
206    }
207    format!("{value:.1} {}", UNITS[unit])
208}
209
210/// Render the enumerated disks as a unified, indented text table — the
211/// `disk4n6 list` human view. Whole disks are flush-left; their partitions are
212/// indented beneath them, so the layout reads the same on every platform.
213#[must_use]
214pub fn render_disks(disks: &[PhysicalDisk]) -> String {
215    let mut s = String::new();
216    if disks.is_empty() {
217        s.push_str("No disks found.\n");
218        return s;
219    }
220    let _ = writeln!(s, "{:<14} {:>10}  {:<6} INFO", "NAME", "SIZE", "TYPE");
221    for d in disks {
222        let kind = if d.synthesized { "synth" } else { "disk" };
223        let mut info = d.model.clone().unwrap_or_default();
224        if d.removable {
225            info = if info.is_empty() {
226                "removable".to_string()
227            } else {
228                format!("{info} (removable)")
229            };
230        }
231        let _ = writeln!(
232            s,
233            "{:<14} {:>10}  {:<6} {}",
234            d.name,
235            human_size(d.size_bytes),
236            kind,
237            info.trim()
238        );
239        for p in &d.partitions {
240            let indented = format!("  {}", p.name);
241            let _ = writeln!(
242                s,
243                "{:<14} {:>10}  {:<6} {}",
244                indented,
245                human_size(p.size_bytes),
246                "part",
247                partition_info(p)
248            );
249        }
250    }
251    s
252}
253
254/// The trailing description column for a partition row: type, then mount point
255/// and label when present (`Apple_APFS  /Volumes/Data [DATA]`).
256fn partition_info(p: &Partition) -> String {
257    let mut parts: Vec<String> = Vec::new();
258    if let Some(t) = &p.partition_type {
259        parts.push(t.clone());
260    }
261    if let Some(m) = &p.mount_point {
262        parts.push(m.clone());
263    }
264    if let Some(l) = &p.label {
265        parts.push(format!("[{l}]"));
266    }
267    parts.join("  ")
268}
269
270/// Render the full `disk4n6 list` view: each disk as a header line followed by
271/// its proportional partition bar (see [`render_disk_bar`]). Synthesized disks
272/// (macOS APFS containers, Linux device-mapper) whose volumes share space rather
273/// than occupy fixed extents get a plain volume list instead of a — misleading —
274/// proportional bar. `color` selects ANSI vs ASCII (the caller passes whether
275/// stdout is a TTY).
276#[must_use]
277pub fn render_listing(disks: &[PhysicalDisk], width: usize, color: bool) -> String {
278    if disks.is_empty() {
279        return "No disks found.\n".to_string();
280    }
281    let mut s = String::new();
282    // At-a-glance comparison of the physical disks' capacities, then per-disk
283    // detail. Empty (and skipped) when there are fewer than two physical disks.
284    let overview = render_overview(disks, width, color);
285    if !overview.is_empty() {
286        s.push_str(&overview);
287        s.push('\n');
288    }
289    // Physical disks are colour-indexed in overview order; the per-disk bar
290    // reuses that index as its accent so a disk's largest partition matches the
291    // colour representing it in the overview.
292    let mut phys_idx = 0;
293    for d in disks {
294        let kind = if d.synthesized { " (synthesized)" } else { "" };
295        let model = d
296            .model
297            .as_deref()
298            .map(|m| format!("  {m}"))
299            .unwrap_or_default();
300        let _ = writeln!(
301            s,
302            "{}  {}{kind}{model}",
303            d.device_path,
304            human_size(d.size_bytes)
305        );
306        if d.partitions.is_empty() {
307            s.push_str("  (no partitions)\n");
308        } else if d.synthesized {
309            for p in &d.partitions {
310                let _ = writeln!(
311                    s,
312                    "  {:<16} {:>10}  {}",
313                    p.name,
314                    human_size(p.size_bytes),
315                    partition_info(p)
316                );
317            }
318            s.push_str("  (volumes share container space)\n");
319        } else {
320            s.push_str(&bar::disk_bar(d, width, color, phys_idx));
321        }
322        if !d.synthesized {
323            phys_idx += 1;
324        }
325        s.push('\n');
326    }
327    s
328}
329
330#[cfg(test)]
331mod tests {
332    use super::*;
333
334    fn sample_disk() -> PhysicalDisk {
335        PhysicalDisk {
336            device_path: "/dev/disk0".into(),
337            name: "disk0".into(),
338            size_bytes: 4_000_000_000_000,
339            logical_sector_size: 512,
340            physical_sector_size: 4096,
341            model: Some("APPLE SSD AP4096".into()),
342            serial: None,
343            removable: false,
344            read_only: false,
345            synthesized: false,
346            partitions: vec![Partition {
347                device_path: "/dev/disk0s1".into(),
348                name: "disk0s1".into(),
349                start_offset: 20480,
350                size_bytes: 524_300_000,
351                partition_type: Some("Apple_APFS_ISC".into()),
352                mount_point: None,
353                filesystem: None,
354                label: None,
355            }],
356        }
357    }
358
359    #[test]
360    fn human_size_matches_decimal_units() {
361        assert_eq!(human_size(512), "512 B");
362        assert_eq!(human_size(999), "999 B");
363        assert_eq!(human_size(1000), "1.0 KB");
364        assert_eq!(human_size(24_576), "24.6 KB");
365        assert_eq!(human_size(524_300_000), "524.3 MB");
366        assert_eq!(human_size(5_400_000_000), "5.4 GB");
367        assert_eq!(human_size(4_000_000_000_000), "4.0 TB");
368    }
369
370    #[test]
371    fn render_disks_shows_disk_then_indented_partitions() {
372        let out = render_disks(&[sample_disk()]);
373        assert!(out.contains("NAME"));
374        assert!(out.contains("disk0"));
375        assert!(out.contains("4.0 TB"));
376        assert!(out.contains("APPLE SSD AP4096"));
377        // The partition is indented and tagged `part` with its type.
378        assert!(out.contains("  disk0s1"));
379        assert!(out.contains("Apple_APFS_ISC"));
380        let disk_line = out.lines().find(|l| l.contains("disk0 ")).unwrap();
381        assert!(disk_line.contains("disk"));
382    }
383
384    #[test]
385    fn render_disks_empty_is_explicit() {
386        assert_eq!(render_disks(&[]), "No disks found.\n");
387    }
388
389    #[test]
390    fn partition_info_joins_type_mount_label() {
391        let p = Partition {
392            device_path: "/dev/disk0s2".into(),
393            name: "disk0s2".into(),
394            start_offset: 0,
395            size_bytes: 1,
396            partition_type: Some("Apple_APFS".into()),
397            mount_point: Some("/Volumes/Data".into()),
398            label: Some("DATA".into()),
399            filesystem: None,
400        };
401        assert_eq!(partition_info(&p), "Apple_APFS  /Volumes/Data  [DATA]");
402    }
403
404    #[test]
405    fn removable_flag_annotates_info() {
406        let mut d = sample_disk();
407        d.model = None;
408        d.removable = true;
409        let out = render_disks(&[d]);
410        assert!(out.contains("removable"));
411    }
412
413    #[test]
414    fn render_listing_draws_bar_for_physical_disk() {
415        let out = render_listing(&[sample_disk()], 40, false);
416        assert!(out.contains("/dev/disk0"));
417        assert!(out.contains("4.0 TB"));
418        assert!(out.contains("APPLE SSD AP4096"));
419        assert!(out.contains('['), "physical disk gets a proportional bar");
420    }
421
422    #[test]
423    fn render_listing_lists_volumes_for_synthesized_disk() {
424        let mut d = sample_disk();
425        d.synthesized = true;
426        d.model = None;
427        let out = render_listing(&[d], 40, false);
428        assert!(out.contains("(synthesized)"));
429        assert!(out.contains("share container space"));
430        // No proportional bar for shared-space volumes.
431        assert!(!out.contains('['));
432    }
433
434    #[test]
435    fn render_listing_empty_is_explicit() {
436        assert_eq!(render_listing(&[], 40, false), "No disks found.\n");
437    }
438
439    // Smoke tests exercising the OS-facing entry points against the real host
440    // (drives the platform backend + open_device end-to-end; on CI this covers
441    // the sysfs/IOKit/DeviceIoControl dispatch). Output is host-dependent, so
442    // they assert only that the calls run, not a specific device list.
443    #[test]
444    fn enumerate_runs_on_host() {
445        // Lists the machine's disks, or fails loud where raw access needs
446        // privileges — never panics.
447        let _ = enumerate();
448    }
449
450    #[cfg(unix)]
451    #[test]
452    fn open_device_sizes_dev_null_to_zero() {
453        let (_file, size) = open_device(Path::new("/dev/null")).unwrap();
454        assert_eq!(size, 0);
455    }
456}