From 19fcd69cb052b59ee26c2c2b0f5311c2a2069d7b Mon Sep 17 00:00:00 2001 From: beneth Date: Tue, 8 May 2018 14:18:03 +0200 Subject: [PATCH] Add threading (1th for input, 1th for output) --- src/main.rs | 56 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 22 deletions(-) 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); }