master
Benoît 2018-05-02 22:57:39 +02:00
parent 02c71d8f3c
commit 583d7f1b9c
1 changed files with 32 additions and 21 deletions

View File

@ -2,7 +2,6 @@ extern crate clap;
extern crate term; extern crate term;
extern crate sha2; extern crate sha2;
extern crate walkdir; extern crate walkdir;
extern crate generic_array;
use sha2::{Sha256, Digest}; use sha2::{Sha256, Digest};
@ -11,6 +10,7 @@ use std::process;
use std::fs; use std::fs;
use std::io::Read; use std::io::Read;
use walkdir::WalkDir; use walkdir::WalkDir;
use std::fmt;
const BUFFER_SIZE: usize = 1024; const BUFFER_SIZE: usize = 1024;
@ -91,17 +91,30 @@ impl Args {
} }
} }
struct FileToProcess<N: ArrayLength<u8>> { struct FileToProcess {
hash: GenericArray<u8,N>, hash: Vec<u8>,
name: String, name: String,
realpath: String, realpath: String,
} }
impl fmt::Display for FileToProcess {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[").unwrap();
for byte in &self.hash {
write!(f, "{:02x}", byte).unwrap();
}
write!(f, "] - {} ({})",
self.name,
self.realpath)
}
}
impl FileToProcess { impl FileToProcess {
/// From https://github.com/RustCrypto/hashes/blob/master/sha2/examples/sha256sum.rs /// From https://github.com/RustCrypto/hashes/blob/master/sha2/examples/sha256sum.rs
/// Compute digest value for given `Reader` and print it /// Compute digest value for given `Reader` and print it
/// On any error simply return without doing anything /// On any error simply return without doing anything
fn hash<D: Digest + Default, R: Read>(&self, reader: &mut R, name: &str) { fn hash<D: Digest + Default, R: Read>(&mut self, reader: &mut R) {
let mut sh = D::default(); let mut sh = D::default();
let mut buffer = [0u8; BUFFER_SIZE]; let mut buffer = [0u8; BUFFER_SIZE];
@ -115,20 +128,15 @@ impl FileToProcess {
break; break;
} }
} }
self.hash = sh.result();
self.print_result(&sh.result(), name);
}
/// Print digest result as hex string and name pair for i in sh.result() {
fn print_result(&self, sum: &[u8], name: &str) { self.hash.push(i);
for byte in sum {
print!("{:02x}", byte);
} }
println!("\t{}", name);
} }
} }
fn main() { fn main() {
let mut files_candidate = vec![];
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")
@ -196,7 +204,6 @@ fn main() {
} }
// Output // Output
let mut i = 0;
for entry in WalkDir::new(&args.output) for entry in WalkDir::new(&args.output)
.into_iter() .into_iter()
.filter_map(|e| e.ok()) .filter_map(|e| e.ok())
@ -207,20 +214,24 @@ fn main() {
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 a = FileToProcess<32> { let mut a = FileToProcess {
name: format!("[{}] - {}", name: format!("{}",
i,
entry.path().display()), entry.path().display()),
hash: vec![],
realpath: String::from("TODO"),
}; };
a.hash::<Sha256, _>(&mut file, a.hash::<Sha256, _>(&mut file);
&format!("[{}] - {}", println!("{}", a);
i, files_candidate.push(a);
entry.path().display()));
i += 1;
} }
} }
} }
for i in files_candidate {
println!("{}", i);
}
// let inputs = fs::read_dir(&args.input).unwrap(); // let inputs = fs::read_dir(&args.input).unwrap();
// for path in inputs { // for path in inputs {
// let path_str = path.unwrap().path().into_os_string().into_string().unwrap(); // let path_str = path.unwrap().path().into_os_string().into_string().unwrap();