move eprints to debug, catch err on file open

This commit is contained in:
Bryson Steck 2025-07-18 01:27:16 -06:00
parent 59e22e31c8
commit 4a04560124
Signed by: bryson
SSH key fingerprint: SHA256:XpKABw/nP4z8UVaH+weLaBnEOD86+cVwif+QjuYLGT4
3 changed files with 19 additions and 18 deletions

View file

@ -8,15 +8,6 @@ pub fn warning(msg: String) {
eprintln!("{} {}", "warning:".yellow().bold(), msg) eprintln!("{} {}", "warning:".yellow().bold(), msg)
} }
pub fn verbose(level: u8, msg_lvl: u8, msg: String) { pub fn debug(msg: String) {
if level < msg_lvl { eprintln!("{} {}", "debug:".purple().bold(), msg);
return;
};
let mut prefix = String::new();
for _ in 0..msg_lvl {
prefix += "=";
}
prefix += "> ";
eprintln!("{}{}", prefix.purple().bold(), msg);
} }

View file

@ -3,8 +3,6 @@ use md5::Md5;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use crate::common;
pub fn hash_md5(mut file: File) -> String { pub fn hash_md5(mut file: File) -> String {
let mut hasher = Md5::new(); let mut hasher = Md5::new();
_ = io::copy(&mut file, &mut hasher); _ = io::copy(&mut file, &mut hasher);

View file

@ -101,14 +101,26 @@ fn hash(info: ThreadInfo) -> Result<(), String> {
}; };
if !*info.quiet && *info.debug { if !*info.quiet && *info.debug {
eprintln!( common::debug(format!(
"thread {} is hashing file '{}'", "thread {} is hashing file '{}'",
info.thread_id, info.thread_id,
filename.as_path().display() filename.as_path().display()
); ));
} }
let file = File::open(&filename).unwrap(); if filename.is_dir() {
common::error(format!("{}: Is a directory", filename.as_path().display()));
continue;
}
let file = match File::open(&filename) {
Err(e) => {
common::error(format!("{}: {}", filename.as_path().display(), e));
continue;
},
Ok(f) => f
};
let res = match &*info.algorithm.as_str() { let res = match &*info.algorithm.as_str() {
"sha256" => hashers::hash_sha256(file), "sha256" => hashers::hash_sha256(file),
"sha384" => hashers::hash_sha384(file), "sha384" => hashers::hash_sha384(file),
@ -123,7 +135,7 @@ fn hash(info: ThreadInfo) -> Result<(), String> {
} }
if !*info.quiet && *info.debug { if !*info.quiet && *info.debug {
eprintln!("thread {} has ran out of work", info.thread_id); common::debug(format!("thread {} has ran out of work", info.thread_id));
} }
Ok(()) Ok(())
@ -150,7 +162,7 @@ fn main() {
}; };
if args.debug { if args.debug {
eprintln!("Starting psha using algorithm {} with {} threads", args.algorithm, cpus) common::debug(format!("Starting psha using algorithm {} with {} threads", args.algorithm, cpus));
} }
if UNSECURE_ALGORITHMS.contains(&args.algorithm.as_str()) { if UNSECURE_ALGORITHMS.contains(&args.algorithm.as_str()) {