use std::ffi::CStr;
use crate::err::Result;
pub struct BlkidDevno(libc::dev_t);
#[cfg(target_os = "linux")]
pub mod ty {
#[allow(non_camel_case_types)]
pub type maj_t = libc::c_uint;
#[allow(non_camel_case_types)]
pub type min_t = libc::c_uint;
}
#[cfg(all(unix, not(target_os = "linux"), not(target_os = "android")))]
pub mod ty {
#[allow(non_camel_case_types)]
pub type maj_t = i32;
#[allow(non_camel_case_types)]
pub type min_t = i32;
}
pub use ty::*;
impl BlkidDevno {
pub(crate) fn new(devno: libc::dev_t) -> Self {
BlkidDevno(devno)
}
pub(crate) fn as_dev_t(&self) -> libc::dev_t {
self.0
}
pub fn from_device_numbers(major: maj_t, minor: min_t) -> Self {
#[allow(unused_unsafe, reason = "No longer unsafe in libc 0.2.133")]
BlkidDevno(unsafe { libc::makedev(major, minor) })
}
pub fn major(&self) -> maj_t {
#[allow(unused_unsafe, reason = "No longer unsafe in libc 0.2.171")]
unsafe {
libc::major(self.0)
}
}
pub fn minor(&self) -> min_t {
#[allow(unused_unsafe, reason = "No longer unsafe in libc 0.2.171")]
unsafe {
libc::minor(self.0)
}
}
pub fn to_devname(&self) -> Result<String> {
let ret = errno_ptr!(unsafe { libblkid_rs_sys::blkid_devno_to_devname(self.0) })?;
let string_ret = unsafe { CStr::from_ptr(ret) }.to_str()?.to_string();
unsafe { libc::free(ret as *mut libc::c_void) };
Ok(string_ret)
}
pub fn to_wholedisk(&self) -> Result<(String, BlkidDevno)> {
let buf = &mut [0u8; 4096];
let mut wholedisk_devno: libc::dev_t = 0;
errno!(unsafe {
libblkid_rs_sys::blkid_devno_to_wholedisk(
self.0,
buf.as_mut_ptr() as *mut libc::c_char,
buf.len(),
&mut wholedisk_devno as *mut _,
)
})?;
let name = std::str::from_utf8(buf)?.to_string();
Ok((name, BlkidDevno(wholedisk_devno)))
}
}