Skip to main content

Crate livedisk

Crate livedisk 

Source
Expand description

§livedisk

Cross-platform enumeration of the live system’s physical disks and partitions — diskutil list / lsblk / diskpart, but as a library with one unified model across macOS, Linux, and Windows.

for disk in livedisk::enumerate()? {
    println!("{} — {}", disk.name, livedisk::human_size(disk.size_bytes));
    for part in &disk.partitions {
        println!("  {} {}", part.name, livedisk::human_size(part.size_bytes));
    }
}

Discovery is the only OS-specific part. Each backend (sysfs on Linux, the IOKit IOMedia registry on macOS, DeviceIoControl on Windows) fills the same PhysicalDisk/Partition structs; everything downstream — the render_overview bar chart, the per-disk render_disk_bar, the render_listing view, and the JSON form — is platform-agnostic.

open_device opens a chosen device node as a sized Read + Seek so a partition/filesystem analyzer can run on the live disk exactly as it would on an image file.

Listing layout/metadata works unprivileged on all three platforms (it reads the kernel’s device registry, not raw sectors); only reading a device needs root/Administrator. Backends therefore never silently return an empty list on a permission problem — they surface Error.

Structs§

Partition
A partition (slice/volume) within a PhysicalDisk.
PhysicalDisk
A whole physical (or, on macOS, synthesized) disk on the live system.

Enums§

Error
Failure enumerating live devices.

Functions§

enumerate
Enumerate every physical disk on the live system, each with its partitions.
human_size
Format a byte count the way disk utilities do — decimal (SI) units with one fractional digit (4.0 TB, 524.3 MB, 24.6 KB), matching diskutil/ lsblk so output is recognisable. Bytes under 1000 render as N B.
open_device
Open a live block device for reading and return it with its size in bytes.
render_disk_bar
Render the proportional bar plus legend for one disk. width is the bar’s inner column count; color selects ANSI-coloured solid blocks (TTY) versus ASCII glyphs (pipe-safe). The disk’s largest partition is drawn in the primary palette colour.
render_disks
Render the enumerated disks as a unified, indented text table — the disk4n6 list human view. Whole disks are flush-left; their partitions are indented beneath them, so the layout reads the same on every platform.
render_listing
Render the full disk4n6 list view: each disk as a header line followed by its proportional partition bar (see render_disk_bar). Synthesized disks (macOS APFS containers, Linux device-mapper) whose volumes share space rather than occupy fixed extents get a plain volume list instead of a — misleading — proportional bar. color selects ANSI vs ASCII (the caller passes whether stdout is a TTY).
render_overview
Render an at-a-glance overview comparing the physical disks’ capacities — a horizontal bar chart, one disk per line, each bar’s length proportional to that disk’s size relative to the largest, so the biggest disk fills the row and the rest read as fractions of it. Each line also shows the absolute size and the disk’s share of total storage. Synthesized disks (APFS containers, device-mapper) are excluded because they overlay physical space rather than add to it. Returns empty when fewer than two physical disks exist.