use std::{
fs::{File, Metadata},
io,
os::unix::fs::MetadataExt as _,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FileIdentity {
pub device: u64,
pub inode: u64,
}
impl FileIdentity {
pub fn from_file(file: &File) -> io::Result<Self> {
let metadata = file.metadata()?;
if !metadata.is_file() {
return Err(io::Error::from(io::ErrorKind::InvalidInput));
}
Ok(Self::from_metadata(&metadata))
}
#[must_use]
pub fn from_metadata(metadata: &Metadata) -> Self {
Self {
device: metadata.dev(),
inode: metadata.ino(),
}
}
#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))]
#[cfg_attr(target_vendor = "apple", allow(clippy::cast_sign_loss))]
pub(crate) const fn from_stat(stat: &rustix::fs::Stat) -> Self {
Self {
#[cfg(target_vendor = "apple")]
device: stat.st_dev as u64,
#[cfg(not(target_vendor = "apple"))]
device: stat.st_dev,
inode: stat.st_ino,
}
}
}
#[cfg(test)]
mod tests;