From 8813456aefd98a34330a1cd806f4e8a1b7c48e72 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Fri, 17 Dec 2021 11:11:55 +0000 Subject: [PATCH] block_util: Move block device detection into it's own function Signed-off-by: Rob Bradford --- block_util/src/async_io.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/block_util/src/async_io.rs b/block_util/src/async_io.rs index 005943a5d..e00f03ad1 100644 --- a/block_util/src/async_io.rs +++ b/block_util/src/async_io.rs @@ -52,6 +52,17 @@ enum BlockSize { } impl DiskTopology { + fn is_block_device(f: &mut File) -> std::io::Result { + let mut stat = std::mem::MaybeUninit::::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 #[allow(clippy::useless_conversion)] fn query_block_size(f: &mut File, block_size_type: BlockSize) -> std::io::Result { @@ -78,14 +89,7 @@ impl DiskTopology { } pub fn probe(f: &mut File) -> std::io::Result { - let mut stat = std::mem::MaybeUninit::::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 }; - if !is_block { + if !Self::is_block_device(f)? { return Ok(DiskTopology::default()); }