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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::{
iter,
path::{Path, PathBuf},
};
use relative_path::RelativePathBuf;
use super::{RootEntry, Filter};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct VfsRoot(pub u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FileType {
File,
Dir,
}
impl FileType {
pub(crate) fn is_dir(&self) -> bool {
*self == FileType::Dir
}
}
impl std::convert::From<std::fs::FileType> for FileType {
fn from(v: std::fs::FileType) -> Self {
if v.is_file() {
FileType::File
} else {
FileType::Dir
}
}
}
struct RootData {
root: PathBuf,
filter: Box<dyn Filter>,
canonical_path: Option<PathBuf>,
excluded_dirs: Vec<RelativePathBuf>,
}
pub(crate) struct Roots {
roots: Vec<RootData>,
}
impl Roots {
pub(crate) fn new(mut paths: Vec<RootEntry>) -> Roots {
paths.sort_by(|a, b| a.path.cmp(&b.path));
paths.dedup();
paths.sort_by_key(|it| std::cmp::Reverse(it.path.as_os_str().len()));
let nested_roots = paths
.iter()
.enumerate()
.map(|(i, entry)| {
paths[..i]
.iter()
.filter_map(|it| rel_path(&entry.path, &it.path))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let roots = paths
.into_iter()
.zip(nested_roots.into_iter())
.map(|(entry, nested_roots)| RootData::new(entry, nested_roots))
.collect::<Vec<_>>();
Roots { roots }
}
pub(crate) fn find(
&self,
path: &Path,
expected: FileType,
) -> Option<(VfsRoot, RelativePathBuf)> {
self.iter().find_map(|root| {
let rel_path = self.contains(root, path, expected)?;
Some((root, rel_path))
})
}
pub(crate) fn len(&self) -> usize {
self.roots.len()
}
pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = VfsRoot> + 'a {
(0..self.roots.len()).into_iter().map(|idx| VfsRoot(idx as u32))
}
pub(crate) fn path(&self, root: VfsRoot) -> &Path {
self.root(root).path().as_path()
}
pub(crate) fn contains(
&self,
root: VfsRoot,
path: &Path,
expected: FileType,
) -> Option<RelativePathBuf> {
let data = self.root(root);
iter::once(data.path())
.chain(data.canonical_path.as_ref().into_iter())
.find_map(|base| to_relative_path(base, path, &data, expected))
}
fn root(&self, root: VfsRoot) -> &RootData {
&self.roots[root.0 as usize]
}
}
impl RootData {
fn new(entry: RootEntry, excluded_dirs: Vec<RelativePathBuf>) -> RootData {
let mut canonical_path = entry.path.canonicalize().ok();
if Some(&entry.path) == canonical_path.as_ref() {
canonical_path = None;
}
RootData { root: entry.path, filter: entry.filter, canonical_path, excluded_dirs }
}
fn path(&self) -> &PathBuf {
&self.root
}
fn is_included(&self, rel_path: &RelativePathBuf, expected: FileType) -> bool {
if self.excluded_dirs.iter().any(|d| rel_path.starts_with(d)) {
return false;
}
let parent_included =
rel_path.parent().map(|d| self.filter.include_dir(&d)).unwrap_or(true);
if !parent_included {
return false;
}
match expected {
FileType::File => self.filter.include_file(&rel_path),
FileType::Dir => self.filter.include_dir(&rel_path),
}
}
}
fn rel_path(base: &Path, path: &Path) -> Option<RelativePathBuf> {
let path = path.strip_prefix(base).ok()?;
RelativePathBuf::from_path(path).ok()
}
fn to_relative_path(
base: &Path,
path: &Path,
data: &RootData,
expected: FileType,
) -> Option<RelativePathBuf> {
let rel_path = rel_path(base, path)?;
if rel_path.as_str().is_empty() {
return Some(rel_path);
}
if data.is_included(&rel_path, expected) {
Some(rel_path)
} else {
None
}
}