Cross platform Rust library for checking whether two file paths are the same file.
by BurntSushiRust
Last 12 weeks · 0 commits
2 of 6 standards met
Summary: Add unit tests covering edge cases for Handle::from_path and is_same_file: nonexistent path returns NotFound error, symlink-to-same-file resolves equal, two distinct hardlinks to the same inode compare equal on Unix, and Handle::stdio handles return distinct identities. These exercise existing public APIs without changing behavior. Keep the change narrow so it is straightforward to review. Notes: I kept this scoped to the relevant implementation and tests.
First off, thanks for taking the time to make this crate in the first place! It's made things easier on my personal projects! I would prefer to do this as a discussion, and not a bug, but I didn't see that available on the page. While working with this crate, I had a few ideas on features that I would find useful, and have made an attempt to implement some of them in my own fork: Have a separate, non-owning type that represents a file identity value. This would be the cross-platform representation of the file identity. While the type itself would be completely safe, I understand that for it to actually represent a file identity, the associated file descriptor/handle has to be open. Still, if you need to store values with keys of the file identity, but you don't want to clone a file handle/descriptor in order to put the key in there, and you're willing to handle the precondition that the file handle has to be open for the FileIdentity to mean anything, that seems reasonable. Given a Handle that was created with Handle::from_file(), have a function Handle::into_inner() that converts the handle back to a File. Seeing the current implementation, I know that this is a bit tricky, as the special casing used for Stdin/out/err files does not make that a smooth round-trip, but its hard to work with if you have libraries that expect to convert to/from std::fs::File for owned file descriptors/handles. To implement the above handle conversion safely, and open up the file identity features to more types, would it make sense to make a new FileLikeHandle which will work as long as T implements something like AsRawFd (or AsFd)? All of the metadata queries used to get the file identity do not modify the underlying file, which means you could wrap any type that returns a valid file descriptor/handle. In this way, the Stdin/out/err handles just become instances of FileLikeHandle, with the correct behavior for dropping. Of course, all of the above names are bikesheddable. If you are interested in this, I'm happy to show you what code I have already. Thanks again!
Repository: BurntSushi/same-file. Description: Cross platform Rust library for checking whether two file paths are the same file. Stars: 122, Forks: 26. Primary language: Rust. Languages: Rust (100%). License: Unlicense. Open PRs: 4, open issues: 6. Last activity: 8mo ago. Community health: 42%. Top contributors: BurntSushi, yandexx, gurgalex, AndyGauge, GuillaumeGomez, alexcrichton, KodrAus, cjubb39, igor-raits, jackpot51 and others.