BurntSushi / same-file

Cross platform Rust library for checking whether two file paths are the same file.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`is_same_file` and `Handle::from_path` hang for named pipes on linux

wkordalski opened this issue · comments

How to reproduce:

  1. Create named pipe: mkfifo my_pipe

  2. Execute following program:

    use same_file::is_same_file;
    
    fn main() {
        let r = is_same_file("/path/to/my_pipe", "/path/to/my_pipe");
        println!("{r:?}");
    }

The program prints nothing and does not exit.

same-file: 1.0.6
rustc 1.58.1 (db9d1b20b 2022-01-20)
Linux 5.16.5-arch1-1 x86_64 GNU/Linux (Arch Linux)

It turns out that this is because opening named pipe blocks until the other end of the pipe is not connected.

Do we need to open files to check whether they are the same?
Or comparing metadata.dev() and metadata.ino() is enough? (then we can use std::fs::metadata(path))

Do we need to open files to check whether they are the same?

Yes! That's the entire point of this crate. To do this as correctly as possible. Note that the docs even call this out:

A handle consumes an open file resource as long as it exists.

If you don't keep the file open, then the inode numbers can be reused and thus result in an incorrect result.

If you don't need the level of correctness afforded by this crate, I would probably suggest just rolling your own thing.