mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-07-07 10:15:45 +00:00
block_util: Move block device detection into it's own function
Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
parent
6c8bd1f476
commit
8813456aef
@ -52,6 +52,17 @@ enum BlockSize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DiskTopology {
|
impl DiskTopology {
|
||||||
|
fn is_block_device(f: &mut File) -> std::io::Result<bool> {
|
||||||
|
let mut stat = std::mem::MaybeUninit::<libc::stat>::uninit();
|
||||||
|
let ret = unsafe { libc::fstat(f.as_raw_fd(), stat.as_mut_ptr()) };
|
||||||
|
if ret != 0 {
|
||||||
|
return Err(std::io::Error::last_os_error());
|
||||||
|
}
|
||||||
|
|
||||||
|
let is_block = unsafe { (*stat.as_ptr()).st_mode & S_IFMT == S_IFBLK };
|
||||||
|
Ok(is_block)
|
||||||
|
}
|
||||||
|
|
||||||
// libc::ioctl() takes different types on different architectures
|
// libc::ioctl() takes different types on different architectures
|
||||||
#[allow(clippy::useless_conversion)]
|
#[allow(clippy::useless_conversion)]
|
||||||
fn query_block_size(f: &mut File, block_size_type: BlockSize) -> std::io::Result<u64> {
|
fn query_block_size(f: &mut File, block_size_type: BlockSize) -> std::io::Result<u64> {
|
||||||
@ -78,14 +89,7 @@ impl DiskTopology {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn probe(f: &mut File) -> std::io::Result<Self> {
|
pub fn probe(f: &mut File) -> std::io::Result<Self> {
|
||||||
let mut stat = std::mem::MaybeUninit::<libc::stat>::uninit();
|
if !Self::is_block_device(f)? {
|
||||||
let ret = unsafe { libc::fstat(f.as_raw_fd(), stat.as_mut_ptr()) };
|
|
||||||
if ret != 0 {
|
|
||||||
return Err(std::io::Error::last_os_error());
|
|
||||||
}
|
|
||||||
|
|
||||||
let is_block = unsafe { (*stat.as_ptr()).st_mode & S_IFMT == S_IFBLK };
|
|
||||||
if !is_block {
|
|
||||||
return Ok(DiskTopology::default());
|
return Ok(DiskTopology::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user