Skip to content

Commit 078d6ac

Browse files
committed
disks: Add more pretty printing and APIs
Signed-off-by: Ikey Doherty <[email protected]>
1 parent f7270aa commit 078d6ac

File tree

4 files changed

+79
-42
lines changed

4 files changed

+79
-42
lines changed

crates/disks/src/lib.rs

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: MPL-2.0
44

55
use std::{
6-
fs, io,
6+
fmt, fs, io,
77
path::{Path, PathBuf},
88
};
99

@@ -54,20 +54,76 @@ pub struct BasicDisk {
5454
pub partitions: Vec<Partition>,
5555
}
5656

57+
impl fmt::Display for Disk {
58+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59+
let bytes = self.size();
60+
let gib = bytes as f64 / 1_073_741_824.0;
61+
62+
write!(f, "{} ({:.2} GiB)", self.name(), gib)?;
63+
64+
if let Some(vendor) = self.vendor() {
65+
write!(f, " - {}", vendor)?;
66+
}
67+
68+
if let Some(model) = self.model() {
69+
write!(f, " {}", model)?;
70+
}
71+
72+
Ok(())
73+
}
74+
}
75+
5776
impl Disk {
5877
/// Returns the name of the disk device.
5978
pub fn name(&self) -> &str {
6079
match self {
61-
Disk::Scsi(disk) => disk.name(),
62-
Disk::Nvme(disk) => disk.name(),
80+
Disk::Scsi(disk) => &disk.disk.name,
81+
Disk::Nvme(disk) => &disk.disk.name,
6382
}
6483
}
6584

6685
/// Returns the partitions on the disk.
6786
pub fn partitions(&self) -> &[Partition] {
6887
match self {
69-
Disk::Scsi(disk) => disk.partitions(),
70-
Disk::Nvme(disk) => disk.partitions(),
88+
Disk::Scsi(disk) => &disk.disk.partitions,
89+
Disk::Nvme(disk) => &disk.disk.partitions,
90+
}
91+
}
92+
93+
/// Returns the path to the disk device in dev.
94+
pub fn device_path(&self) -> &Path {
95+
match self {
96+
Disk::Scsi(disk) => &disk.disk.device,
97+
Disk::Nvme(disk) => &disk.disk.device,
98+
}
99+
}
100+
101+
/// Returns the total number of sectors on the disk.
102+
pub fn sectors(&self) -> u64 {
103+
match self {
104+
Disk::Scsi(disk) => disk.disk.sectors,
105+
Disk::Nvme(disk) => disk.disk.sectors,
106+
}
107+
}
108+
109+
/// Returns the size of the disk in bytes.
110+
pub fn size(&self) -> u64 {
111+
self.sectors() * 512
112+
}
113+
114+
/// Returns the model name of the disk.
115+
pub fn model(&self) -> Option<&str> {
116+
match self {
117+
Disk::Scsi(disk) => disk.disk.model.as_deref(),
118+
Disk::Nvme(disk) => disk.disk.model.as_deref(),
119+
}
120+
}
121+
122+
/// Returns the vendor name of the disk.
123+
pub fn vendor(&self) -> Option<&str> {
124+
match self {
125+
Disk::Scsi(disk) => disk.disk.vendor.as_deref(),
126+
Disk::Nvme(disk) => disk.disk.vendor.as_deref(),
71127
}
72128
}
73129
}
@@ -170,9 +226,9 @@ mod tests {
170226
let devices = BlockDevice::discover().unwrap();
171227
for device in &devices {
172228
if let BlockDevice::Disk(disk) = device {
173-
println!("{}:", disk.name());
229+
println!("{}: {disk}", disk.name());
174230
for partition in disk.partitions() {
175-
println!("├─{}", partition.name);
231+
println!("├─{} {partition}", partition.name);
176232
}
177233
}
178234
}

crates/disks/src/nvme.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! This module provides functionality to enumerate and handle NVMe (Non-Volatile Memory Express)
88
//! storage devices by parsing sysfs paths and device names.
99
10-
use crate::{partition::Partition, BasicDisk, DiskInit};
10+
use crate::{BasicDisk, DiskInit};
1111
use regex::Regex;
1212
use std::{path::Path, sync::OnceLock};
1313

@@ -18,7 +18,7 @@ static NVME_PATTERN: OnceLock<Regex> = OnceLock::new();
1818
#[derive(Debug)]
1919
pub struct Disk {
2020
/// The underlying basic disk implementation
21-
disk: BasicDisk,
21+
pub(crate) disk: BasicDisk,
2222
}
2323

2424
impl DiskInit for Disk {
@@ -43,18 +43,3 @@ impl DiskInit for Disk {
4343
}
4444
}
4545
}
46-
47-
impl Disk {
48-
/// Returns the name of the NVMe disk (e.g. "nvme0n1")
49-
///
50-
/// # Returns
51-
/// * A string slice containing the disk name
52-
pub fn name(&self) -> &str {
53-
&self.disk.name
54-
}
55-
56-
/// Returns the partitions on the disk
57-
pub fn partitions(&self) -> &[Partition] {
58-
&self.disk.partitions
59-
}
60-
}

crates/disks/src/partition.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// SPDX-License-Identifier: MPL-2.0
44

5+
use std::fmt;
56
use std::path::{Path, PathBuf};
67

78
use crate::{sysfs::sysfs_read, DEVFS_DIR, SYSFS_DIR};
@@ -26,6 +27,17 @@ pub struct Partition {
2627
pub device: PathBuf,
2728
}
2829

30+
impl fmt::Display for Partition {
31+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32+
write!(
33+
f,
34+
"{name} {size:.2} GiB",
35+
name = self.name,
36+
size = self.size as f64 * 512.0 / (1024.0 * 1024.0 * 1024.0)
37+
)
38+
}
39+
}
40+
2941
impl Partition {
3042
/// Creates a new Partition instance from a sysfs path and partition name.
3143
///

crates/disks/src/scsi.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
1111
use std::path::Path;
1212

13-
use crate::{partition::Partition, BasicDisk, DiskInit};
13+
use crate::{BasicDisk, DiskInit};
1414

1515
/// Represents a SCSI disk device.
1616
///
1717
/// This struct wraps a BasicDisk to provide SCSI-specific functionality.
1818
#[derive(Debug)]
1919
pub struct Disk {
20-
disk: BasicDisk,
20+
pub(crate) disk: BasicDisk,
2121
}
2222

2323
impl DiskInit for Disk {
@@ -43,19 +43,3 @@ impl DiskInit for Disk {
4343
}
4444
}
4545
}
46-
47-
impl Disk {
48-
/// Returns the name of the disk device.
49-
///
50-
/// # Returns
51-
///
52-
/// The device name (e.g. "sda", "sdb")
53-
pub fn name(&self) -> &str {
54-
&self.disk.name
55-
}
56-
57-
/// Returns the partitions on the disk.
58-
pub fn partitions(&self) -> &[Partition] {
59-
&self.disk.partitions
60-
}
61-
}

0 commit comments

Comments
 (0)