Skip to content

Commit 60df577

Browse files
committed
disks: Add guestimate of device partition paths
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 0232ecb commit 60df577

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

crates/disks/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ impl BlockDevice {
6161
}
6262
}
6363

64+
/// Returns the path to the partition with the given index.
65+
/// No attempt is made to verify the existence of the partition.
66+
pub fn partition_path(&self, index: u32) -> PathBuf {
67+
if let BlockDevice::Disk(disk) = self {
68+
match **disk {
69+
Disk::Scsi(_) | Disk::Virtual(_) => {
70+
// Add N to the end of the device name
71+
return disk.device_path().join(format!("{}{}", disk.name(), index));
72+
}
73+
Disk::Mock(ref d) if d.parts_prefix => {
74+
return PathBuf::from("/dev").join(format!("{}p{}", disk.name(), index));
75+
}
76+
Disk::Mock(_) => {
77+
return PathBuf::from("/dev").join(format!("{}{}", disk.name(), index));
78+
}
79+
_ => {}
80+
}
81+
}
82+
// Add pN to the end of the device name
83+
self.device().with_file_name(format!("{}p{}", self.name(), index))
84+
}
85+
6486
/// Creates a mock block device with a specified number of sectors.
6587
pub fn mock_device(disk: mock::MockDisk) -> Self {
6688
BlockDevice::Disk(Box::new(Disk::Mock(disk)))
@@ -181,4 +203,19 @@ mod tests {
181203
}
182204
}
183205
}
206+
207+
#[test]
208+
fn test_partition_paths() {
209+
// Create a mock SCSI disk
210+
let scsi_disk = mock::MockDisk::new_with_name("sda", 1000, false);
211+
let device = BlockDevice::mock_device(scsi_disk);
212+
assert_eq!(device.partition_path(1).to_str().unwrap(), "/dev/sda1");
213+
assert_eq!(device.partition_path(2).to_str().unwrap(), "/dev/sda2");
214+
215+
// Create a mock NVMe disk
216+
let nvme_disk = mock::MockDisk::new_with_name("nvme0n1", 1000, true);
217+
let device = BlockDevice::mock_device(nvme_disk);
218+
assert_eq!(device.partition_path(1).to_str().unwrap(), "/dev/nvme0n1p1");
219+
assert_eq!(device.partition_path(2).to_str().unwrap(), "/dev/nvme0n1p2");
220+
}
184221
}

crates/disks/src/mock.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,45 @@ use crate::{partition::Partition, BasicDisk};
1515
///
1616
/// This struct wraps a BasicDisk to provide mock functionality for testing.
1717
#[derive(Debug)]
18-
pub struct MockDisk(pub BasicDisk);
18+
pub struct MockDisk {
19+
basic_disk: BasicDisk,
20+
pub parts_prefix: bool,
21+
}
1922

2023
impl Deref for MockDisk {
2124
type Target = BasicDisk;
2225

2326
fn deref(&self) -> &Self::Target {
24-
&self.0
27+
&self.basic_disk
2528
}
2629
}
2730

2831
impl MockDisk {
2932
/// Creates a new mock disk with the specified size in bytes
3033
pub fn new(size_bytes: u64) -> Self {
34+
Self::new_with_name("mock0", size_bytes, false)
35+
}
36+
37+
pub fn new_with_name(name: &str, size_bytes: u64, parts_prefix: bool) -> Self {
3138
let sectors = size_bytes / 512;
3239
let disk = BasicDisk {
33-
name: "mock0".to_string(),
40+
name: name.to_string(),
3441
sectors,
35-
device: PathBuf::from("/dev/mock0"),
42+
device: PathBuf::from(format!("/dev/{}", name)),
3643
model: Some("Mock Device".to_string()),
3744
vendor: Some("Mock Vendor".to_string()),
3845
partitions: Vec::new(),
3946
};
40-
Self(disk)
47+
48+
Self {
49+
basic_disk: disk,
50+
parts_prefix,
51+
}
4152
}
4253

4354
/// Add a partition to the mock disk at the specified byte offsets
4455
pub fn add_partition(&mut self, start_bytes: u64, end_bytes: u64) {
45-
let partition_number = self.0.partitions().len() + 1;
56+
let partition_number = self.basic_disk.partitions().len() + 1;
4657
let start = start_bytes / 512;
4758
let end = end_bytes / 512;
4859

@@ -56,6 +67,6 @@ impl MockDisk {
5667
device: PathBuf::from(format!("/dev/mock0p{}", partition_number)),
5768
};
5869

59-
self.0.partitions_mut().push(partition);
70+
self.basic_disk.partitions_mut().push(partition);
6071
}
6172
}

0 commit comments

Comments
 (0)