use std::path::Path;
use crate::error::Error;
pub fn same_file<P: AsRef<Path>>(file1: P, file2: P) -> Result<bool, Error> {
let fd1 = unsafe { nc::openat(nc::AT_FDCWD, file1.as_ref(), nc::O_RDONLY, 0)? };
let fd2 = unsafe { nc::openat(nc::AT_FDCWD, file2.as_ref(), nc::O_RDONLY, 0)? };
let is_same = same_file_fd(fd1, fd2)?;
unsafe { nc::close(fd1)? };
unsafe { nc::close(fd2)? };
Ok(is_same)
}
pub fn same_file_fd(fd1: i32, fd2: i32) -> Result<bool, Error> {
let mut stat1 = nc::stat_t::default();
unsafe { nc::fstat(fd1, &mut stat1)? };
let mut stat2 = nc::stat_t::default();
unsafe { nc::fstat(fd2, &mut stat2)? };
Ok(stat1.st_dev == stat2.st_dev && stat1.st_ino == stat2.st_ino)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_same_file() {
assert_eq!(same_file("/etc/passwd", "/etc/passwd"), Ok(true));
}
}