From 1799ed9ed33dc9168689fe130912eaf886a8a11f Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Wed, 16 Sep 2015 19:01:53 +0200 Subject: [PATCH] Added utility function to copy all files recursively except files that have an extension present in the ext_blacklist parameter --- src/utils/mod.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index bd9727d6..a3ed5603 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -130,3 +130,38 @@ pub fn remove_dir_content(dir: &Path) -> Result<(), Box> { } Ok(()) } + +/// **Untested!** +/// +/// Copies all files of a directory to another one except the files with the extensions given in the +/// `ext_blacklist` array + +pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blacklist: &[String]) -> Result<(), Box> { + + // Check that from and to are different + if from == to { return Ok(()) } + + for entry in try!(fs::read_dir(from)) { + let entry = try!(entry); + let metadata = try!(entry.metadata()); + + // If the entry is a dir and the recursive option is enabled, call itself + if metadata.is_dir() && recursive { + try!(copy_files_except_ext( + &from.join(entry.file_name()), + &to.join(entry.file_name()), + true, + ext_blacklist + )); + } else if metadata.is_file() { + + // Check if it is in the blacklist + if let Some(ext) = entry.path().extension() { + if ext_blacklist.contains(&String::from(ext.to_str().unwrap())) { continue } + + try!(fs::copy(entry.path(), &to.join(entry.path().file_name().expect("a file should have a file name...")))); + } + } + } + Ok(()) +}