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