From 4a04560124fbff8b4839abf3b144133d65d81c0a Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Fri, 18 Jul 2025 01:27:16 -0600 Subject: [PATCH] move eprints to debug, catch err on file open --- src/common.rs | 13 ++----------- src/hashers.rs | 2 -- src/main.rs | 22 +++++++++++++++++----- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/common.rs b/src/common.rs index a73d419..90e74f6 100644 --- a/src/common.rs +++ b/src/common.rs @@ -8,15 +8,6 @@ pub fn warning(msg: String) { eprintln!("{} {}", "warning:".yellow().bold(), msg) } -pub fn verbose(level: u8, msg_lvl: u8, msg: String) { - if level < msg_lvl { - return; - }; - let mut prefix = String::new(); - for _ in 0..msg_lvl { - prefix += "="; - } - - prefix += "> "; - eprintln!("{}{}", prefix.purple().bold(), msg); +pub fn debug(msg: String) { + eprintln!("{} {}", "debug:".purple().bold(), msg); } diff --git a/src/hashers.rs b/src/hashers.rs index 8707ba0..8bd4a9b 100644 --- a/src/hashers.rs +++ b/src/hashers.rs @@ -3,8 +3,6 @@ use md5::Md5; use std::fs::File; use std::io; -use crate::common; - pub fn hash_md5(mut file: File) -> String { let mut hasher = Md5::new(); _ = io::copy(&mut file, &mut hasher); diff --git a/src/main.rs b/src/main.rs index eeafb48..ed7216d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,14 +101,26 @@ fn hash(info: ThreadInfo) -> Result<(), String> { }; if !*info.quiet && *info.debug { - eprintln!( + common::debug(format!( "thread {} is hashing file '{}'", info.thread_id, 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() { "sha256" => hashers::hash_sha256(file), "sha384" => hashers::hash_sha384(file), @@ -123,7 +135,7 @@ fn hash(info: ThreadInfo) -> Result<(), String> { } 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(()) @@ -150,7 +162,7 @@ fn main() { }; 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()) {