io_block/
os.rs

1use crate::BlockSize;
2use cfg_if::cfg_if;
3use std::fs::File;
4use std::io;
5
6cfg_if! {
7    if #[cfg(target_os = "linux")] {
8        pub mod linux;
9        use linux::BlockDev as OsBlockDev;
10    } else if #[cfg(target_vendor = "apple")] {
11        pub mod darwin;
12        use darwin::BlockDev as OsBlockDev;
13    } else {
14        compile_error!("Unsupported OS");
15    }
16
17    // dragonflybsd: `getdisktabbyname`
18    // freebsd: `getdiskbyname`
19}
20
21/// A block device on the target operating system
22///
23/// # Portability
24///
25/// - MacOS (darwin) will refuse to open block devices read-only if the file system is mounted,
26///   either giving "Resource busy" or "Operation not permitted" depending on which disk (external or
27///   internal) is opened. It is possible to use `IoRegistryEntryCreateCFProperties` to obtain
28///   details while the device is mounted, but this is not implemented.
29pub struct BlockDev {
30    inner: OsBlockDev,
31}
32
33impl BlockDev {
34    pub fn from_file(i: File) -> io::Result<BlockDev> {
35        Ok(BlockDev {
36            inner: OsBlockDev::from_file(i)?,
37        })
38    }
39}
40
41impl BlockSize for BlockDev {
42    fn block_size_physical(&self) -> io::Result<u64> {
43        self.inner.block_size_physical()
44    }
45
46    fn block_count(&self) -> io::Result<u64> {
47        self.inner.block_count()
48    }
49
50    fn block_size_logical(&self) -> io::Result<u64> {
51        self.inner.block_size_logical()
52    }
53}