1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::fs::{
is_root_dir, open_dir_unchecked, read_dir_unchecked, FollowSymlinks, MaybeOwnedFile, Metadata,
};
use std::fs;
use std::path::{Component, PathBuf};
pub(crate) fn file_path_by_searching(file: &fs::File) -> Option<PathBuf> {
let mut base = MaybeOwnedFile::borrowed_noassert(file);
let mut components = Vec::new();
'next_component: loop {
let mut iter =
read_dir_unchecked(&base, Component::ParentDir.as_ref(), FollowSymlinks::No).ok()?;
let metadata = Metadata::from_file(&*base).ok()?;
while let Some(child) = iter.next() {
let child = child.ok()?;
if child.is_same_file(&metadata).ok()? {
components.push(child.file_name());
base = MaybeOwnedFile::owned_noassert(
open_dir_unchecked(&base, Component::ParentDir.as_ref()).ok()?,
);
continue 'next_component;
}
}
if is_root_dir(&base, &iter).ok()? {
break;
}
return None;
}
let mut path = PathBuf::new();
path.push(Component::RootDir);
for component in components.iter().rev() {
path.push(component);
}
Some(path)
}