Crate same_file

source ·
Expand description

This crate provides a safe and simple cross platform way to determine whether two file paths refer to the same file or directory.

Most uses of this crate should be limited to the top-level is_same_file function, which takes two file paths and returns true if they refer to the same file or directory:

use same_file::is_same_file;

assert!(is_same_file("/bin/sh", "/usr/bin/sh")?);

Additionally, this crate provides a Handle type that permits a more efficient equality check depending on your access pattern. For example, if one wanted to check whether any path in a list of paths corresponded to the process’ stdout handle, then one could build a handle once for stdout. The equality check for each file in the list then only requires one stat call instead of two. The code might look like this:

use same_file::Handle;

let candidates = &[
    "examples/is_same_file.rs",
    "examples/is_stderr.rs",
    "examples/stderr",
];
let stdout_handle = Handle::stdout()?;
for candidate in candidates {
    let handle = Handle::from_path(candidate)?;
    if stdout_handle == handle {
        println!("{:?} is stdout!", candidate);
    } else {
        println!("{:?} is NOT stdout!", candidate);
    }
}

See examples/is_stderr.rs for a runnable example and compare the output of:

  • cargo run --example is_stderr 2> examples/stderr and
  • cargo run --example is_stderr.

Structs§

  • A handle to a file that can be tested for equality with other handles.

Functions§

  • Returns true if the two file paths may correspond to the same file.