implement verify functions, fail counters
This commit is contained in:
parent
84b3e15483
commit
621eff415c
1 changed files with 166 additions and 36 deletions
198
src/main.rs
198
src/main.rs
|
@ -1,9 +1,10 @@
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use std::collections::VecDeque;
|
use std::collections::{HashMap, VecDeque};
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, read_to_string, File};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::sync::atomic::{AtomicI32, Ordering};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::thread::{self, available_parallelism};
|
use std::thread::{self, available_parallelism, JoinHandle};
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
mod hashers;
|
mod hashers;
|
||||||
|
@ -85,12 +86,15 @@ struct Args {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ThreadInfo {
|
struct ThreadInfo {
|
||||||
debug: Arc<bool>,
|
debug: bool,
|
||||||
failures_only: Arc<bool>,
|
quiet: bool,
|
||||||
quiet: Arc<bool>,
|
print_failures: bool,
|
||||||
thread_id: usize,
|
thread_id: usize,
|
||||||
filenames: Arc<Mutex<VecDeque<PathBuf>>>,
|
filenames: Arc<Mutex<VecDeque<PathBuf>>>,
|
||||||
algorithm: Arc<String>,
|
algorithm: Arc<String>,
|
||||||
|
hash_map: Option<Arc<Mutex<HashMap<PathBuf, String>>>>,
|
||||||
|
file_errors: Arc<AtomicI32>,
|
||||||
|
hash_errors: Arc<AtomicI32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash(info: ThreadInfo) -> Result<(), String> {
|
fn hash(info: ThreadInfo) -> Result<(), String> {
|
||||||
|
@ -100,7 +104,7 @@ fn hash(info: ThreadInfo) -> Result<(), String> {
|
||||||
None => break,
|
None => break,
|
||||||
};
|
};
|
||||||
|
|
||||||
if !*info.quiet && *info.debug {
|
if !info.quiet && info.debug {
|
||||||
common::debug(format!(
|
common::debug(format!(
|
||||||
"thread {} is hashing file '{}'",
|
"thread {} is hashing file '{}'",
|
||||||
info.thread_id,
|
info.thread_id,
|
||||||
|
@ -110,12 +114,14 @@ fn hash(info: ThreadInfo) -> Result<(), String> {
|
||||||
|
|
||||||
if filename.is_dir() {
|
if filename.is_dir() {
|
||||||
common::error(format!("{}: Is a directory", filename.as_path().display()));
|
common::error(format!("{}: Is a directory", filename.as_path().display()));
|
||||||
|
info.file_errors.fetch_add(1, Ordering::SeqCst);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let file = match File::open(&filename) {
|
let file = match File::open(&filename) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
common::error(format!("{}: {}", filename.as_path().display(), e));
|
common::error(format!("{}: {}", filename.as_path().display(), e));
|
||||||
|
info.file_errors.fetch_add(1, Ordering::SeqCst);
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
|
@ -138,33 +144,135 @@ fn hash(info: ThreadInfo) -> Result<(), String> {
|
||||||
_ => panic!("Somehow did not pass a supported algorithm"),
|
_ => panic!("Somehow did not pass a supported algorithm"),
|
||||||
};
|
};
|
||||||
|
|
||||||
if !*info.quiet {
|
match &info.hash_map {
|
||||||
|
Some(h) => {
|
||||||
|
if h.lock().unwrap()[&filename] == res && !info.quiet {
|
||||||
|
println!("{}: OK", filename.as_path().display());
|
||||||
|
} else {
|
||||||
|
println!("{}: FAILED", filename.as_path().display());
|
||||||
|
info.hash_errors.fetch_add(1, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
if !info.quiet {
|
||||||
println!("{} {}", res, filename.as_path().display());
|
println!("{} {}", res, filename.as_path().display());
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*info.quiet && *info.debug {
|
if !info.quiet && info.debug {
|
||||||
common::debug(format!("thread {} has ran out of work", info.thread_id));
|
common::debug(format!("thread {} has ran out of work", info.thread_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn verify(
|
||||||
let args = Args::parse();
|
cpus: usize,
|
||||||
let mut buffer = VecDeque::new();
|
algorithm: String,
|
||||||
|
debug: bool,
|
||||||
|
quiet: bool,
|
||||||
|
print_failures: bool,
|
||||||
|
checksum_files: Vec<PathBuf>,
|
||||||
|
) -> (
|
||||||
|
Vec<JoinHandle<Result<(), std::string::String>>>,
|
||||||
|
Arc<AtomicI32>,
|
||||||
|
Arc<AtomicI32>,
|
||||||
|
) {
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
for file in args.files {
|
let mut hash_map: HashMap<PathBuf, String> = HashMap::new();
|
||||||
if args.canonicalize {
|
let mut buffer = VecDeque::new();
|
||||||
match fs::canonicalize(file.as_path()) {
|
for file in checksum_files {
|
||||||
Ok(p) => buffer.push_back(p),
|
match read_to_string(&file) {
|
||||||
Err(e) => panic!("unable to canonicalize {}: {}", file.as_path().display(), e),
|
Err(e) => {
|
||||||
};
|
common::error(format!("{}: {}", file.as_path().display(), e));
|
||||||
} else {
|
continue;
|
||||||
buffer.push_back(file);
|
},
|
||||||
|
Ok(f) => {
|
||||||
|
for line in f.lines() {
|
||||||
|
let split: Vec<String> = line.split_whitespace().map(|x| x.to_string()).collect();
|
||||||
|
// println!("{}, {}", split.size_hint().0, split.size_hint().1)
|
||||||
|
match split.len() {
|
||||||
|
2 => {
|
||||||
|
hash_map.insert(PathBuf::from(split[1].clone()), split[0].clone());
|
||||||
|
buffer.push_back(PathBuf::from(split[1].clone()));
|
||||||
|
},
|
||||||
|
_ => common::error(format!("malformed line: {}", line)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let arc_fe = Arc::new(AtomicI32::new(0));
|
||||||
|
let arc_he = Arc::new(AtomicI32::new(0));
|
||||||
|
let arc_buf = Arc::new(Mutex::new(buffer));
|
||||||
|
let arc_hash = Arc::new(Mutex::new(hash_map));
|
||||||
|
for i in 0..cpus {
|
||||||
|
let safe_fe = Arc::clone(&arc_fe);
|
||||||
|
let safe_he = Arc::clone(&arc_he);
|
||||||
|
let safe_buf = Arc::clone(&arc_buf);
|
||||||
|
let safe_alg = Arc::new(algorithm.clone());
|
||||||
|
let safe_hash = Arc::clone(&arc_hash);
|
||||||
|
handles.push(thread::spawn(move || {
|
||||||
|
hash(ThreadInfo {
|
||||||
|
debug,
|
||||||
|
quiet,
|
||||||
|
print_failures,
|
||||||
|
thread_id: i,
|
||||||
|
filenames: safe_buf,
|
||||||
|
algorithm: safe_alg,
|
||||||
|
hash_map: Some(safe_hash),
|
||||||
|
file_errors: safe_fe,
|
||||||
|
hash_errors: safe_he,
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
return (handles, arc_fe, arc_he);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate(
|
||||||
|
cpus: usize,
|
||||||
|
buffer: VecDeque<PathBuf>,
|
||||||
|
algorithm: String,
|
||||||
|
debug: bool,
|
||||||
|
quiet: bool,
|
||||||
|
) -> (
|
||||||
|
Vec<JoinHandle<Result<(), std::string::String>>>,
|
||||||
|
Arc<AtomicI32>,
|
||||||
|
Arc<AtomicI32>,
|
||||||
|
) {
|
||||||
|
let mut handles = vec![];
|
||||||
|
let arc_fe = Arc::new(AtomicI32::new(0));
|
||||||
|
let arc_he = Arc::new(AtomicI32::new(0));
|
||||||
|
let arc_buf = Arc::new(Mutex::new(buffer));
|
||||||
|
for i in 0..cpus {
|
||||||
|
let safe_fe = Arc::clone(&arc_fe);
|
||||||
|
let safe_he = Arc::clone(&arc_he);
|
||||||
|
let safe_buf = Arc::clone(&arc_buf);
|
||||||
|
let safe_alg = Arc::new(algorithm.clone());
|
||||||
|
handles.push(thread::spawn(move || {
|
||||||
|
hash(ThreadInfo {
|
||||||
|
debug,
|
||||||
|
quiet,
|
||||||
|
print_failures: false,
|
||||||
|
thread_id: i,
|
||||||
|
filenames: safe_buf,
|
||||||
|
algorithm: safe_alg,
|
||||||
|
hash_map: None,
|
||||||
|
file_errors: safe_fe,
|
||||||
|
hash_errors: safe_he,
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
return (handles, arc_fe, arc_he);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args = Args::parse();
|
||||||
let cpus = match args.threads {
|
let cpus = match args.threads {
|
||||||
0 => available_parallelism().unwrap().get(),
|
0 => available_parallelism().unwrap().get(),
|
||||||
_ => args.threads,
|
_ => args.threads,
|
||||||
|
@ -184,20 +292,32 @@ fn main() {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let arc_buf = Arc::new(Mutex::new(buffer));
|
let handles;
|
||||||
for i in 0..cpus {
|
let arc_fe;
|
||||||
let safe_buf = Arc::clone(&arc_buf);
|
let arc_he;
|
||||||
let safe_alg = Arc::new(args.algorithm.clone());
|
if &args.check.len() >= &1 {
|
||||||
handles.push(thread::spawn(move || {
|
(handles, arc_fe, arc_he) = verify(
|
||||||
hash(ThreadInfo {
|
cpus,
|
||||||
debug: Arc::new(args.debug),
|
args.algorithm,
|
||||||
failures_only: Arc::new(args.failures_only),
|
args.debug,
|
||||||
quiet: Arc::new(args.quiet),
|
args.quiet,
|
||||||
thread_id: i,
|
args.failures_only,
|
||||||
filenames: safe_buf,
|
args.check,
|
||||||
algorithm: safe_alg,
|
);
|
||||||
})
|
} else {
|
||||||
}))
|
let mut buffer = VecDeque::new();
|
||||||
|
for file in args.files {
|
||||||
|
if args.canonicalize {
|
||||||
|
match fs::canonicalize(file.as_path()) {
|
||||||
|
Ok(p) => buffer.push_back(p),
|
||||||
|
Err(e) => panic!("unable to canonicalize {}: {}", file.as_path().display(), e),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
buffer.push_back(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(handles, arc_fe, arc_he) = generate(cpus, buffer, args.algorithm, args.debug, args.quiet);
|
||||||
}
|
}
|
||||||
|
|
||||||
for handle in handles {
|
for handle in handles {
|
||||||
|
@ -206,4 +326,14 @@ fn main() {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let fe = arc_fe.load(Ordering::SeqCst);
|
||||||
|
let he = arc_he.load(Ordering::SeqCst);
|
||||||
|
if fe != 0 {
|
||||||
|
common::warning(format!("{} listed files could not be read", fe));
|
||||||
|
}
|
||||||
|
|
||||||
|
if he != 0 {
|
||||||
|
common::warning(format!("{} computed checksums did NOT match", he));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue