-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirutils.rs
290 lines (239 loc) · 8.14 KB
/
dirutils.rs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use rayon::iter::ParallelDrainRange;
use rayon::iter::ParallelIterator;
use std::cmp::min;
use std::collections::vec_deque::VecDeque;
use std::fmt::{Display, Formatter};
use std::fs;
use std::iter::from_fn;
use std::path::{Path, PathBuf};
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone)]
pub struct FileSize {
pub path: PathBuf,
pub size: u64,
}
type Result = std::result::Result<FileSize, Error>;
enum VisitedPath {
Result(Result),
Dir((PathBuf, u32)),
}
impl FileSize {
fn new(path: PathBuf, size: u64) -> Self {
Self { path, size }
}
}
impl From<(&str, u64)> for FileSize {
fn from((path, size): (&str, u64)) -> Self {
Self::new(PathBuf::from(path), size)
}
}
#[derive(Debug)]
pub struct Error {
path: PathBuf,
io_error: std::io::Error,
}
impl Error {
fn new(path: PathBuf, io_error: std::io::Error) -> Self {
Self { path, io_error }
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"error while reading path {:?}: {}",
self.path, self.io_error
)
}
}
// max number of paths that will be traversed in parallel at any time
pub const DEFAULT_PARALLEL_PATHS: u32 = 1_000;
pub fn visit(
path: &Path,
max_depth: Option<u32>,
max_parallel: Option<u32>,
) -> impl Iterator<Item = Result> {
let max_parallel = max_parallel.unwrap_or(DEFAULT_PARALLEL_PATHS) as usize;
let mut result_queue = VecDeque::new();
let mut dir_queue = VecDeque::new();
match read_path(path, 0) {
Some(VisitedPath::Dir((path, _))) => dir_queue.push_back((path, 0)),
Some(VisitedPath::Result(result)) => result_queue.push_back(result),
None => (),
}
from_fn(move || -> Option<Result> {
while result_queue.is_empty() && !dir_queue.is_empty() {
let path_bits: Vec<_> = dir_queue
.par_drain(..(min(max_parallel, dir_queue.len())))
.flat_map(|(path, depth)| read_dir(&path, depth))
.collect();
path_bits.into_iter().for_each(|path_bit| match path_bit {
VisitedPath::Result(result) => result_queue.push_back(result),
VisitedPath::Dir((path, depth)) => {
if max_depth.map_or(true, |max_depth| depth <= max_depth) {
dir_queue.push_back((path, depth))
}
}
});
}
result_queue.pop_front()
})
}
fn read_dir(dir_path: &Path, depth: u32) -> Vec<VisitedPath> {
let dir_entries = fs::read_dir(dir_path);
if dir_entries.is_err() {
return vec![VisitedPath::Result(Err(Error::new(
dir_path.to_owned(),
dir_entries.err().unwrap(),
)))];
}
dir_entries
.unwrap()
.into_iter()
.map_while(|dir_entry| match dir_entry {
Ok(dir_entry) => read_path(&dir_entry.path(), depth),
Err(error) => Some(VisitedPath::Result(Err(Error::new(
dir_path.to_owned(),
error,
)))),
})
.collect()
}
fn read_path(path: &Path, depth: u32) -> Option<VisitedPath> {
match fs::symlink_metadata(path) {
Ok(metadata) if metadata.is_file() => Some(VisitedPath::Result(Ok(FileSize::new(
path.to_owned(),
metadata.len(),
)))),
Ok(metadata) if metadata.is_dir() => Some(VisitedPath::Dir((path.to_owned(), depth + 1))),
Ok(_) => None,
Err(error) => Some(VisitedPath::Result(Err(Error::new(path.to_owned(), error)))),
}
}
#[cfg(test)]
mod tests {
use super::visit;
use super::FileSize;
use rand::Rng;
use std::fs::create_dir;
use std::os::unix::fs::symlink;
use std::path::{Path, PathBuf};
use std::{env, fs};
#[test]
fn can_list_files() {
let mut test_files = [
FileSize::from(("foo", 100)),
FileSize::from(("boo", 200)),
FileSize::from(("goo", 300)),
];
let test_dir = create_new_dir_with_files(&mut test_files, None);
let mut dir_list = visit(test_dir.as_path(), None, Some(1))
.map(|r| r.ok().unwrap())
.collect::<Vec<_>>();
dir_list.sort();
test_files.sort();
dir_list
.iter()
.zip(test_files.iter())
.for_each(|(retrieved, expected)| assert_eq!(*retrieved, *expected))
}
#[test]
fn can_list_files_recursively() {
let mut test_files = [
FileSize::from(("foo", 100)),
FileSize::from(("boo", 200)),
FileSize::from(("goo", 300)),
];
let test_dir = create_new_dir_with_files(&mut test_files, None);
let mut test_files_sub_dir = [
FileSize::from(("abc", 340)),
FileSize::from(("def", 50)),
FileSize::from(("ghi", 2)),
];
create_new_dir_with_files(&mut test_files_sub_dir, Some(test_dir.as_path()));
let mut dir_list = visit(test_dir.as_path(), None, Some(1))
.map(|r| r.ok().unwrap())
.collect::<Vec<_>>();
dir_list.sort();
let mut expected = [test_files, test_files_sub_dir].concat();
expected.sort();
dir_list
.iter()
.zip(expected.iter())
.for_each(|(retrieved, expected)| assert_eq!(*retrieved, *expected))
}
#[test]
fn can_limit_depth() {
let test_files = [
FileSize::from(("foo", 100)),
FileSize::from(("boo", 200)),
FileSize::from(("goo", 300)),
];
let mut dir = PathBuf::new();
let mut topdir = PathBuf::new();
for level in 0..=4 {
dir = create_new_dir_with_files(
&mut test_files.clone(),
if level == 0 {
None
} else {
Option::from(dir.as_path())
},
);
if level == 0 {
topdir = dir.clone();
}
}
assert_eq!(
visit(topdir.as_path(), Some(3), Some(1))
.map(|r| r.unwrap())
.count(),
12 // 4 levels (0..=3) with 3 files each
);
}
#[test]
#[ignore]
fn returns_errors() {
let temp_dir = env::temp_dir();
let path_with_error = temp_dir.join("broken_link");
symlink("/does_not_exist", &path_with_error).unwrap();
let error = visit(temp_dir.as_path(), None, Some(1)).next().unwrap();
assert!(error.is_err());
assert_eq!(error.err().unwrap().path, path_with_error);
}
#[test]
fn accepts_file_as_input_path() {
let mut test_file = [FileSize::from(("foo", 200))];
create_new_dir_with_files(&mut test_file, None);
let size_entries = visit(test_file[0].path.as_path(), None, Some(1))
.map(|r| r.unwrap())
.collect::<Vec<_>>();
assert_eq!(size_entries, test_file)
}
#[test]
fn accepts_nonexistent_paths() {
let mut rng = rand::thread_rng();
let path = PathBuf::from(format!("{}", rng.gen::<u32>()));
let result = visit(path.as_path(), None, Some(1)).next().unwrap();
assert!(result.is_err());
assert_eq!(result.err().unwrap().path, path);
}
/// Creates `test_files` in a new dir with a randomly generated name
/// place dir in `dest` (if provided) or in a temporary system folder
fn create_new_dir_with_files(test_files: &mut [FileSize], dest: Option<&Path>) -> PathBuf {
let mut rng = rand::thread_rng();
let temp_dir = env::temp_dir();
let parent_dir = dest.unwrap_or(temp_dir.as_path());
let subdir: u32 = rng.gen();
let test_dir = parent_dir.join(format!("{}", subdir));
create_dir(test_dir.as_path()).expect(&format!(
"Could not create temporary directory: {}",
test_dir.display()
));
test_files.iter_mut().for_each(|f| {
f.path = test_dir.join(&f.path);
fs::write(&f.path, str::repeat("0", f.size as usize))
.expect("failed to write test file");
});
test_dir
}
}