Determine if two paths are on the same filesystem

Viewed 250

Given two Path structures, how can I determine if they are on the same file system?

I need to move a file using std::fs::rename, but I only want to perform the rename if the source and destination are on the same filesystem (otherwise, the rename is bound to fail).

In bash, I'd use stat -c "%d" PATH. Rust has std::fs::metadata, but it doesn't seem to return the fsid.

1 Answers

Based on eggyal's comment: The following worked:

use std::os::unix::fs::MetadataExt;
// ...
let path1_meta = fs::metadata(path1).unwrap();
let path2_meta = fs::metadata(path2).unwrap();

if path1_meta.dev() != path2_meta.dev() {
    panic!("Directories need to be on the same file system");
}
Related