diff --git a/src/main.rs b/src/main.rs index ecb218e..c8ae8c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,8 @@ use std::fs; use std::io::Read; use walkdir::WalkDir; use std::fmt; +use std::thread; +use std::sync::{Arc, Mutex}; const BUFFER_SIZE: usize = 1024; @@ -134,7 +136,7 @@ impl FileToProcess { } fn main() { - let mut files_candidate = vec![]; + let files_candidate = Arc::new(Mutex::new(Vec::new())); let mut t = term::stdout().unwrap(); let matches = App::new("rdupe") .version("0.1.0") @@ -201,33 +203,43 @@ fn main() { } // Walk through path 1 & 2 [Todo: threading] - for s in &[&args.input, &args.output] { - for entry in WalkDir::new(&s) - .into_iter() - .filter_map(|e| e.ok()) - { - // symlink_metadata does not follow symlink :-] - let metadata = fs::symlink_metadata(entry.path()).unwrap(); - let ft = metadata.file_type(); + let mut children = vec![]; + let args_source = vec![args.input, args.output]; + for s in args_source { + let fc = files_candidate.clone(); + children.push(thread::spawn(move || { + for entry in WalkDir::new(&s) + .into_iter() + .filter_map(|e| e.ok()) + { + // symlink_metadata does not follow symlink :-] + let metadata = fs::symlink_metadata(entry.path()).unwrap(); + let ft = metadata.file_type(); - if ft.is_file() { - if let Ok(mut file) = fs::File::open(&entry.path()) { - let mut a = FileToProcess { - name: format!("{}", - entry.path().display()), - hash: vec![], - realpath: String::from("TODO"), - }; + if ft.is_file() { + if let Ok(mut file) = fs::File::open(&entry.path()) { + let mut a = FileToProcess { + name: format!("{}", + entry.path().display()), + hash: vec![], + realpath: String::from("TODO"), + }; - // compute file hash - a.hash::(&mut file); - files_candidate.push(a); + // compute file hash + a.hash::(&mut file); + fc.lock().unwrap().push(a); + } } } - } + })); } - for i in files_candidate { + for child in children { + // Wait for the thread to finish. Returns a result. + let _ = child.join(); + } + + for i in files_candidate.lock().unwrap().iter() { println!("{}", i); }