cap_primitives/rustix/linux/fs/
file_path.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use super::procfs::get_path_from_proc_self_fd;
use std::fs;
use std::path::PathBuf;

pub(crate) fn file_path(file: &fs::File) -> Option<PathBuf> {
    use std::os::unix::fs::MetadataExt;

    // Ignore paths that don't start with '/', which are things like
    // `socket:[3556564]` or similar.
    let path = get_path_from_proc_self_fd(file)
        .ok()
        .filter(|path| path.starts_with("/"))?;

    // Linux appends the string " (deleted)" when a file is deleted; avoid
    // treating that as the actual name. Check this after doing the `readlink`
    // above so that we're conservative about concurrent deletions.
    if file.metadata().ok()?.nlink() == 0 {
        return None;
    }

    Some(path)
}