clonedir_lib/lib.rs
1extern crate reflink;
2#[macro_use] extern crate log;
3extern crate env_logger;
4
5use reflink::reflink_or_copy;
6use std::convert::AsRef;
7use std::fs;
8use std::io;
9use std::path::Path;
10
11/// Clones a directory with reflink
12/// https://docs.rs/reflink/*/reflink/index.html
13/// Some file systems implement COW (copy on write) functionality in order to speed up file copies. On a high level, the new file does not actually get copied, but shares the same on-disk data with the source file. As soon as one of the files is modified, the actual copying is done by the underlying OS.
14///
15/// # Examples
16///
17/// ```
18/// use std::fs;
19/// use std::io::prelude::*;
20///
21/// fn main() {
22/// clonedir_lib::clonedir("src", "tmp/src").expect("error downloading");
23/// let contents = fs::read_to_string("tmp/src/lib.rs").expect("error reading file");
24/// assert!(contents.starts_with("extern"));
25/// }
26/// ```
27pub fn clonedir<A: AsRef<Path>, B: AsRef<Path>>(from: A, to: B) -> io::Result<()> {
28 let from = from.as_ref();
29 let to = to.as_ref();
30 debug!("cloning dir {:?} to {:?}", &from, &to);
31 for f in fs::read_dir(from)?.into_iter().map(|f| f.unwrap().path()) {
32 fs::create_dir_all(&to)?;
33 if f.is_dir() {
34 clonedir(&f, &to)?;
35 } else if f.is_file() {
36 debug!("cloning {:?} to {:?}", &f, &to);
37 reflink_or_copy(&f, to.join(f.file_name().unwrap()))?;
38 }
39 }
40
41 Ok(())
42}