quichash_core/hash/computer/
single.rs1use std::fs::File;
2use std::io::IsTerminal;
3use std::io::{Read, Seek};
4use std::path::Path;
5use std::str::FromStr;
6
7#[cfg(feature = "mmap")]
8use memmap2::Mmap;
9
10use super::super::HashResult;
11use super::super::algorithm::Algorithm;
12use super::super::file::*;
13use super::super::hasher::{Hasher, bytes_to_hex};
14use super::super::registry::HashRegistry;
15use super::HashComputer;
16use crate::error::HashUtilityError;
17
18impl HashComputer {
19 #[cfg(feature = "filesystem")]
22 #[allow(unsafe_code)]
23 pub(crate) fn compute_hash_for_worker(
24 &self,
25 path: &Path,
26 algorithm: &str,
27 fast_mode: bool,
28 file_size: u64,
29 buffer: &mut [u8],
30 ) -> Result<HashResult, HashUtilityError> {
31 let parsed = Algorithm::from_str(algorithm)?;
32 let mode = if fast_mode {
33 super::super::HashMode::Sampled
34 } else {
35 super::super::HashMode::Full
36 };
37 let digest =
38 super::super::file::hash_file_mode_worker(path, &[parsed], mode, file_size, buffer)?
39 .remove(0);
40 Ok(HashResult {
41 algorithm: algorithm.to_owned(),
42 hash: digest.to_hex(),
43 file_path: path.to_owned(),
44 })
45 }
46
47 #[allow(unsafe_code)]
58 pub fn compute_hash(
59 &self,
60 path: &Path,
61 algorithm: &str,
62 ) -> Result<HashResult, HashUtilityError> {
63 self.compute_hash_with_progress(path, algorithm, false)
64 }
65
66 #[allow(unsafe_code)]
71 pub fn compute_hash_with_progress(
72 &self,
73 path: &Path,
74 algorithm: &str,
75 show_progress: bool,
76 ) -> Result<HashResult, HashUtilityError> {
77 if !show_progress && Algorithm::from_str(algorithm).ok() == Some(Algorithm::Blake3) {
78 let digest = hash_file(path, &[Algorithm::Blake3])?.remove(0);
79 return Ok(HashResult {
80 algorithm: algorithm.to_owned(),
81 hash: digest.to_hex(),
82 file_path: path.to_owned(),
83 });
84 }
85 let mut hasher = HashRegistry::get_hasher(algorithm)?;
87
88 let file = super::super::io_strategy::open(path, super::super::HashMode::Full)
90 .map_err(|e| HashUtilityError::from_io_error(e, "reading", Some(path.to_path_buf())))?;
91
92 let file_size = file
94 .metadata()
95 .map_err(|e| {
96 HashUtilityError::from_io_error(e, "reading metadata", Some(path.to_path_buf()))
97 })?
98 .len();
99
100 let should_show_progress =
102 show_progress && file_size > PROGRESS_BAR_THRESHOLD && std::io::stdout().is_terminal();
103
104 #[cfg(feature = "mmap")]
106 {
107 if (MMAP_MIN_SIZE..MMAP_THRESHOLD).contains(&file_size) {
108 match unsafe { Mmap::map(&file) } {
110 Ok(mmap) => {
111 hasher.update(&mmap[..]);
114 }
115 Err(_) => {
116 if should_show_progress {
118 self.hash_with_buffered_io_progress(
119 &mut hasher,
120 file,
121 path,
122 file_size,
123 )?;
124 } else {
125 self.hash_with_buffered_io(&mut hasher, file, path)?;
126 }
127 }
128 }
129 } else {
130 if should_show_progress {
132 self.hash_with_buffered_io_progress(&mut hasher, file, path, file_size)?;
133 } else {
134 self.hash_with_buffered_io(&mut hasher, file, path)?;
135 }
136 }
137 }
138 #[cfg(not(feature = "mmap"))]
139 {
140 if should_show_progress {
141 self.hash_with_buffered_io_progress(&mut hasher, file, path, file_size)?;
142 } else {
143 self.hash_with_buffered_io(&mut hasher, file, path)?;
144 }
145 }
146
147 let hash_bytes = hasher.finalize();
149 let hash_hex = bytes_to_hex(&hash_bytes);
150
151 Ok(HashResult {
152 algorithm: algorithm.to_string(),
153 hash: hash_hex,
154 file_path: path.to_path_buf(),
155 })
156 }
157
158 pub(crate) fn hash_with_buffered_io(
160 &self,
161 hasher: &mut Box<dyn Hasher>,
162 mut file: File,
163 path: &Path,
164 ) -> Result<(), HashUtilityError> {
165 let mut buffer = vec![0u8; self.buffer_size];
166
167 loop {
168 let bytes_read = file.read(&mut buffer).map_err(|e| {
169 HashUtilityError::from_io_error(e, "reading", Some(path.to_path_buf()))
170 })?;
171 if bytes_read == 0 {
172 break;
173 }
174 hasher.update(&buffer[..bytes_read]);
175 }
176
177 Ok(())
178 }
179
180 pub(crate) fn hash_with_buffered_io_progress(
182 &self,
183 hasher: &mut Box<dyn Hasher>,
184 mut file: File,
185 path: &Path,
186 file_size: u64,
187 ) -> Result<(), HashUtilityError> {
188 use crate::operation::{
189 LegacyProgress as ProgressBar, LegacyProgressStyle as ProgressStyle,
190 };
191 use std::time::{Duration, Instant};
192
193 let pb = ProgressBar::new(file_size);
195 pb.set_style(
196 ProgressStyle::default_bar()
197 .template(
198 "{msg}\n[{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})",
199 )
200 .unwrap()
201 .progress_chars("#>-"),
202 );
203 pb.set_message(format!("Hashing: {}", path.display()));
204
205 let mut buffer = vec![0u8; self.buffer_size];
206 let mut bytes_processed = 0u64;
207 let mut last_update = Instant::now();
208 let update_interval = Duration::from_millis(PROGRESS_UPDATE_INTERVAL_MS);
209
210 loop {
211 let bytes_read = file.read(&mut buffer).map_err(|e| {
212 pb.finish_and_clear();
213 HashUtilityError::from_io_error(e, "reading", Some(path.to_path_buf()))
214 })?;
215 if bytes_read == 0 {
216 break;
217 }
218 hasher.update(&buffer[..bytes_read]);
219 bytes_processed += bytes_read as u64;
220
221 let now = Instant::now();
223 if now.duration_since(last_update) >= update_interval {
224 pb.set_position(bytes_processed);
225 last_update = now;
226 }
227 }
228
229 pb.finish_and_clear();
231
232 Ok(())
233 }
234
235 pub fn compute_hash_fast(
244 &self,
245 path: &Path,
246 algorithm: &str,
247 ) -> Result<HashResult, HashUtilityError> {
248 let mut hasher = HashRegistry::get_hasher(algorithm)?;
250
251 let mut file = super::super::io_strategy::open(path, super::super::HashMode::Sampled)
253 .map_err(|e| HashUtilityError::from_io_error(e, "reading", Some(path.to_path_buf())))?;
254
255 let file_size = file
257 .metadata()
258 .map_err(|e| {
259 HashUtilityError::from_io_error(e, "reading metadata", Some(path.to_path_buf()))
260 })?
261 .len();
262
263 if file_size < FAST_MODE_THRESHOLD {
265 let mut buffer = vec![0u8; self.buffer_size];
266 loop {
267 let bytes_read = file.read(&mut buffer).map_err(|e| {
268 HashUtilityError::from_io_error(e, "reading", Some(path.to_path_buf()))
269 })?;
270 if bytes_read == 0 {
271 break;
272 }
273 hasher.update(&buffer[..bytes_read]);
274 }
275 } else {
276 let mut buffer = vec![0_u8; self.buffer_size];
280 self.read_region(
281 &mut file,
282 &mut hasher,
283 &mut buffer,
284 0,
285 FAST_MODE_SAMPLE_SIZE,
286 path,
287 )?;
288
289 let middle_start = (file_size / 2).saturating_sub(FAST_MODE_SAMPLE_SIZE / 2);
291 self.read_region(
292 &mut file,
293 &mut hasher,
294 &mut buffer,
295 middle_start,
296 FAST_MODE_SAMPLE_SIZE,
297 path,
298 )?;
299
300 let last_start = file_size.saturating_sub(FAST_MODE_SAMPLE_SIZE);
302 self.read_region(
303 &mut file,
304 &mut hasher,
305 &mut buffer,
306 last_start,
307 FAST_MODE_SAMPLE_SIZE,
308 path,
309 )?;
310 }
311
312 let hash_bytes = hasher.finalize();
314 let hash_hex = bytes_to_hex(&hash_bytes);
315
316 Ok(HashResult {
317 algorithm: algorithm.to_string(),
318 hash: hash_hex,
319 file_path: path.to_path_buf(),
320 })
321 }
322
323 fn read_region(
325 &self,
326 file: &mut File,
327 hasher: &mut Box<dyn Hasher>,
328 buffer: &mut [u8],
329 start: u64,
330 length: u64,
331 path: &Path,
332 ) -> Result<(), HashUtilityError> {
333 file.seek(std::io::SeekFrom::Start(start))
335 .map_err(|e| HashUtilityError::from_io_error(e, "seeking", Some(path.to_path_buf())))?;
336
337 let mut bytes_remaining = length;
339
340 while bytes_remaining > 0 {
341 let to_read = std::cmp::min(bytes_remaining, buffer.len() as u64) as usize;
342 let bytes_read = file.read(&mut buffer[..to_read]).map_err(|e| {
343 HashUtilityError::from_io_error(e, "reading", Some(path.to_path_buf()))
344 })?;
345
346 if bytes_read == 0 {
347 break; }
349
350 hasher.update(&buffer[..bytes_read]);
351 bytes_remaining -= bytes_read as u64;
352 }
353
354 Ok(())
355 }
356}