Add threading (1th for input, 1th for output)

master
Benoît 2018-05-08 14:18:03 +02:00
parent f97430c21a
commit 19fcd69cb0
1 changed files with 34 additions and 22 deletions

View File

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