2023-07-04 00:51:47 -04:00
|
|
|
use {
|
|
|
|
crate::Error,
|
|
|
|
std::io::{Read, Write},
|
|
|
|
};
|
|
|
|
|
2023-07-04 20:13:38 -04:00
|
|
|
/// Represents the major and minor numbers of a Unix special Device node
|
2023-07-04 00:51:47 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Special {
|
|
|
|
pub major: u32,
|
|
|
|
pub minor: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Special {
|
|
|
|
pub fn read<T: Read>(reader: &mut T) -> Result<Self, Error> {
|
|
|
|
let mut buf = [0; 8];
|
|
|
|
reader.read_exact(&mut buf)?;
|
|
|
|
let major: [u8; 4] = buf[..4].try_into()?;
|
|
|
|
let major = u32::from_le_bytes(major);
|
|
|
|
let minor: [u8; 4] = buf[4..].try_into()?;
|
|
|
|
let minor = u32::from_le_bytes(minor);
|
|
|
|
Ok(Self { major, minor })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write<T: Write>(&self, writer: &mut T) -> Result<(), Error> {
|
|
|
|
writer.write_all(&self.major.to_le_bytes())?;
|
|
|
|
writer.write_all(&self.minor.to_le_bytes())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn from_rdev(rdev: u32) -> Self {
|
|
|
|
Self {
|
|
|
|
major: rdev >> 8,
|
|
|
|
minor: rdev & 0xff,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|