cap_primitives/rustix/fs/
is_same_file.rs

1use crate::fs::{Metadata, MetadataExt};
2use std::{fs, io};
3
4/// Determine if `a` and `b` refer to the same inode on the same device.
5pub(crate) fn is_same_file(a: &fs::File, b: &fs::File) -> io::Result<bool> {
6    let a_metadata = Metadata::from_file(a)?;
7    let b_metadata = Metadata::from_file(b)?;
8    is_same_file_metadata(&a_metadata, &b_metadata)
9}
10
11/// Determine if `a` and `b` are metadata for the same inode on the same
12/// device.
13pub(crate) fn is_same_file_metadata(a: &Metadata, b: &Metadata) -> io::Result<bool> {
14    Ok(a.dev() == b.dev() && a.ino() == b.ino())
15}
16
17/// Determine if `a` and `b` definitely refer to different inodes.
18///
19/// This is similar to `is_same_file`, but is conservative, and doesn't depend
20/// on nightly-only features.
21#[allow(dead_code)]
22pub(crate) fn is_different_file(a: &fs::File, b: &fs::File) -> io::Result<bool> {
23    is_same_file(a, b).map(|same| !same)
24}
25
26/// Determine if `a` and `b` are metadata for definitely different inodes.
27///
28/// This is similar to `is_same_file_metadata`, but is conservative, and
29/// doesn't depend on nightly-only features.
30#[allow(dead_code)]
31pub(crate) fn is_different_file_metadata(a: &Metadata, b: &Metadata) -> io::Result<bool> {
32    is_same_file_metadata(a, b).map(|same| !same)
33}