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
use super::*;
use std::fs;
use std::io;
pub fn deep_copy<P: AsRef<Path>>(send_err: Sender<io::Error>, from: PathDir, to: P) {
let to = ch_try!(
send_err,
create_dir_maybe(to).map_err(|err| err.into()),
return
);
let (send_file, recv_file) = ch::bounded(128);
take!(=send_err as errs, =to as to_walk);
spawn(move || {
walk_and_create_dirs(from, to_walk, errs, send_file);
});
for _ in 0..num_cpus::get() {
take!(=send_err, =recv_file, =to);
spawn(move || {
for (from, to_postfix) in recv_file {
ch_try!(
send_err,
from.copy(to.join(to_postfix)).map_err(|err| err.into()),
continue
);
}
});
}
}
fn walk_and_create_dirs(
from: PathDir,
to: PathDir,
send_err: Sender<io::Error>,
send_file: Sender<(PathFile, PathBuf)>,
) {
let mut it = from.walk().follow_links(true).into_iter();
loop {
let entry = match it.next() {
Some(entry) => entry,
None => break,
};
macro_rules! handle_err {
($entry:expr) => {
match $entry {
Ok(e) => e,
Err(err) => {
ch!(send_err <- err.into());
continue;
}
}
};
}
let entry = handle_err!(entry);
let to_postfix = entry
.path()
.strip_prefix(&from)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e));
let to_postfix = handle_err!(to_postfix);
match handle_err!(PathType::new(entry.path())) {
PathType::Dir(_) => {
if let Err(err) = PathDir::create(to.join(to_postfix)) {
ch!(send_err <- err.into());
it.skip_current_dir();
}
}
PathType::File(from_file) => {
ch!(send_file <- (from_file, to_postfix.to_path_buf()));
}
}
}
}
fn create_dir_maybe<P: AsRef<Path>>(path: P) -> path_abs::Result<PathDir> {
let arc = PathArc::new(path);
fs::create_dir(&arc).map_err(|err| path_abs::Error::new(err, "creating dir", arc.clone()))?;
PathDir::new(arc)
}