-
Notifications
You must be signed in to change notification settings - Fork 217
/
files.rs
41 lines (37 loc) · 1.29 KB
/
files.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
use anyhow::Result;
use ignore::WalkBuilder;
use std::{fs, io, path::Path};
/// Copies a directory and all its contents to the destination path, recursively.
pub fn recursive_copy_dir<T: AsRef<Path>, Q: AsRef<Path>>(source: T, dest: Q) -> Result<()> {
let walker = WalkBuilder::new(&source)
.follow_links(false)
// this includes hidden directories & files
.standard_filters(false)
.hidden(false)
.build();
for entry in walker {
let entry = entry?;
if let Some(file_type) = entry.file_type() {
let from = entry.path();
let to = dest.as_ref().join(from.strip_prefix(&source)?);
// create directories
if file_type.is_dir() {
if let Err(e) = fs::create_dir(to) {
match e.kind() {
io::ErrorKind::AlreadyExists => {}
_ => return Err(e.into()),
}
}
}
// copy files
else if file_type.is_file() {
fs::copy(from, &to)?;
// replace CRLF with LF
if let Ok(data) = fs::read_to_string(from) {
fs::write(&to, data.replace("\r\n", "\n"))?;
}
}
}
}
Ok(())
}