@@ -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}
0 commit comments