lib.rs |
!
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:
```rust,no_run
# use std::error::Error;
use same_file::is_same_file;
# fn try_main() -> Result<(), Box<Error>> {
assert!(is_same_file("/bin/sh", "/usr/bin/sh")?);
# Ok(())
# }
#
# fn main() {
# try_main().unwrap();
# }
```
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:
```rust,no_run
# use std::error::Error;
use same_file::Handle;
# fn try_main() -> Result<(), Box<Error>> {
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);
}
}
# Ok(())
# }
#
# fn main() {
# try_main().unwrap();
# }
```
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`.
[`is_same_file`]: fn.is_same_file.html
[`Handle`]: struct.Handle.html
[`examples/is_stderr.rs`]: https://github.com/BurntSushi/same-file/blob/master/examples/is_same_file.rs
|
16326 |
unix.rs |
|
3000 |
unknown.rs |
|
1132 |
win.rs |
|
5781 |