Skip to content

Commit

Permalink
fix: Fetch of parent block device for a sys/block/dev path
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Mar 10, 2021
1 parent d660781 commit 92d1788
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions crates/disk-types/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,33 @@ pub trait BlockDeviceExt {

/// The size of each logical sector, in bytes.
fn get_logical_block_size(&self) -> u64 {
eprintln!("get block size for {:?}", self.sys_block_path());

let block = Block::from_path(&self.sys_block_path())
.expect("device lacks block");

match block.queue_logical_block_size() {
Ok(size) => return size,
Err(_) => {
return Block::from_path(&self.sys_block_path())
.expect("partition does not have a block device")
.parent_device()
return self.get_parent_device()
.expect("partition lacks parent block device")
.queue_logical_block_size()
.expect("parent of partition lacks logical block size");
}
}
}

fn get_parent_device(&self) -> Option<Block> {
self.sys_block_path()
.canonicalize()
.ok()
.and_then(|canon| {
canon.parent()
.and_then(|parent| parent.file_name())
.and_then(|name| name.to_str())
.map(|parent| Path::new("/sys/class/block").join(parent))
})
.and_then(|parent| Block::from_path(&parent).ok())
}

/// The size of each logical sector, in bytes.
fn get_physical_block_size(&self) -> u64 {
let path = sys_block_path(self.get_device_name(), "/queue/physical_block_size");
Expand Down

0 comments on commit 92d1788

Please sign in to comment.