|
| 1 | +use std::fmt::Display; |
| 2 | +use std::time::{Duration, Instant}; |
| 3 | + |
| 4 | +use anyhow::Result; |
| 5 | +use rayon::prelude::*; |
| 6 | + |
| 7 | +use crate::cli::config::ConfigSettings; |
| 8 | +use crate::core::types::BasicHash; |
| 9 | +use crate::hash::algorithms::call_hasher; |
| 10 | +use crate::io::files::get_required_filenames; |
| 11 | +use crate::progress::ProgressManager; |
| 12 | + |
| 13 | +pub fn worker_func(config: &ConfigSettings) -> Result<()> { |
| 14 | + if config.debug_mode { |
| 15 | + show_initial_info(config); |
| 16 | + } |
| 17 | + |
| 18 | + let paths = get_required_filenames(config)?; |
| 19 | + |
| 20 | + if paths.is_empty() { |
| 21 | + if config.debug_mode { |
| 22 | + eprintln!("No files found"); |
| 23 | + } |
| 24 | + return Ok(()); |
| 25 | + } |
| 26 | + |
| 27 | + if config.debug_mode { |
| 28 | + eprintln!("Files to hash: {paths:?}"); |
| 29 | + } |
| 30 | + |
| 31 | + if config.single_thread || paths.len() == 1 { |
| 32 | + file_hashes_st(config, &paths); |
| 33 | + } else { |
| 34 | + file_hashes_mt(config, &paths); |
| 35 | + } |
| 36 | + |
| 37 | + Ok(()) |
| 38 | +} |
| 39 | + |
| 40 | +fn show_initial_info(config: &ConfigSettings) { |
| 41 | + crate::cli::args::show_help(false); |
| 42 | + eprintln!(); |
| 43 | + eprintln!("Config: {config:?}"); |
| 44 | + if config.supplied_paths.is_empty() { |
| 45 | + eprintln!("No path specified, reading from stdin"); |
| 46 | + } else { |
| 47 | + eprintln!( |
| 48 | + "Paths: {} file path(s) supplied", |
| 49 | + config.supplied_paths.len() |
| 50 | + ); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fn file_hashes_st<S>(config: &ConfigSettings, paths: &[S]) |
| 55 | +where |
| 56 | + S: AsRef<str> + Display + Send + Sync, |
| 57 | +{ |
| 58 | + if config.debug_mode { |
| 59 | + eprintln!("Single-threaded mode"); |
| 60 | + eprintln!("Algorithm: {:?}", config.algorithm); |
| 61 | + } |
| 62 | + |
| 63 | + for pathstr in paths { |
| 64 | + let file_hash = hash_with_progress(config, pathstr.as_ref().to_string()); |
| 65 | + |
| 66 | + match file_hash { |
| 67 | + Ok(basic_hash) => { |
| 68 | + if config.exclude_fn { |
| 69 | + println!("{basic_hash}"); |
| 70 | + } else { |
| 71 | + println!("{basic_hash} {pathstr}"); |
| 72 | + } |
| 73 | + } |
| 74 | + Err(e) => eprintln!("File error for '{pathstr}': {e}"), |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +fn file_hashes_mt<S>(config: &ConfigSettings, paths: &[S]) |
| 80 | +where |
| 81 | + S: AsRef<str> + Sync + Display, |
| 82 | +{ |
| 83 | + if config.debug_mode { |
| 84 | + eprintln!("Multi-threaded mode"); |
| 85 | + eprintln!("Algorithm: {:?}", config.algorithm); |
| 86 | + } |
| 87 | + |
| 88 | + let overall_progress = if config.no_progress { |
| 89 | + None |
| 90 | + } else { |
| 91 | + ProgressManager::create_overall_progress(paths.len(), config.debug_mode) |
| 92 | + }; |
| 93 | + |
| 94 | + paths.par_iter().for_each(|pathstr| { |
| 95 | + let file_hash = hash_with_progress(config, pathstr.as_ref().to_string()); |
| 96 | + |
| 97 | + if let Some(ref pb) = overall_progress { |
| 98 | + pb.inc(1); |
| 99 | + } |
| 100 | + |
| 101 | + match file_hash { |
| 102 | + Ok(basic_hash) => { |
| 103 | + if config.exclude_fn { |
| 104 | + println!("{basic_hash}"); |
| 105 | + } else { |
| 106 | + println!("{basic_hash} {pathstr}"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + Err(e) => eprintln!("File error for '{pathstr}': {e}"), |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + if let Some(pb) = overall_progress { |
| 115 | + pb.finish_with_message("Complete!"); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +fn hash_with_progress<S>(config: &ConfigSettings, pathstr: S) -> Result<BasicHash> |
| 120 | +where |
| 121 | + S: AsRef<str> + Display + Clone + Send + 'static, |
| 122 | +{ |
| 123 | + let pathstr_clone = pathstr.clone(); |
| 124 | + |
| 125 | + let progress_handle = if config.no_progress { |
| 126 | + None |
| 127 | + } else { |
| 128 | + ProgressManager::create_file_progress(pathstr_clone.clone(), config.debug_mode) |
| 129 | + }; |
| 130 | + |
| 131 | + let start_time = Instant::now(); |
| 132 | + let result = call_hasher(config.algorithm, config.encoding, pathstr); |
| 133 | + let elapsed = start_time.elapsed(); |
| 134 | + |
| 135 | + if let Some(handle) = progress_handle { |
| 136 | + handle.finish(config.debug_mode); |
| 137 | + } |
| 138 | + |
| 139 | + if config.debug_mode && elapsed >= Duration::from_millis(ProgressManager::threshold_millis()) { |
| 140 | + eprintln!( |
| 141 | + "File '{}' took {:.2}s to hash", |
| 142 | + pathstr_clone, |
| 143 | + elapsed.as_secs_f64() |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + result |
| 148 | +} |
0 commit comments