Skip to main content

quichash_core/hash/computer/
single.rs

1use 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    /// Hash one file from an outer file-parallel pipeline. The caller owns a
20    /// reusable buffer, and this method deliberately avoids nested Rayon work.
21    #[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    /// Compute hash for a single file using streaming I/O or memory mapping
48    ///
49    /// Uses buffered I/O for small files and memory mapping for larger files on
50    /// 64-bit targets. 32-bit targets cap mappings at 2 GiB.
51    ///
52    /// # Safety
53    ///
54    /// Memory mapping assumes the file will not be modified by other processes during hashing.
55    /// If the file is modified concurrently, the hash result may be inconsistent.
56    /// This is acceptable for typical use cases where files are not being actively modified.
57    #[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    /// Compute hash for a single file with optional progress bar
67    ///
68    /// If show_progress is true and the file is larger than 1GB and stdout is a TTY,
69    /// displays a progress bar that updates 10 times per second.
70    #[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        // Get hasher for the specified algorithm
86        let mut hasher = HashRegistry::get_hasher(algorithm)?;
87
88        // Open file for reading with better error context
89        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        // Get file size to determine whether to use memory mapping
93        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        // Determine if we should show progress bar
101        let should_show_progress =
102            show_progress && file_size > PROGRESS_BAR_THRESHOLD && std::io::stdout().is_terminal();
103
104        // Use memory mapping only when it amortizes mapping/page-fault overhead.
105        #[cfg(feature = "mmap")]
106        {
107            if (MMAP_MIN_SIZE..MMAP_THRESHOLD).contains(&file_size) {
108                // Try to memory map the file
109                match unsafe { Mmap::map(&file) } {
110                    Ok(mmap) => {
111                        // Hash the entire mapped file in one go
112                        // Note: Progress bar not shown for mmap as it's very fast
113                        hasher.update(&mmap[..]);
114                    }
115                    Err(_) => {
116                        // Fall back to buffered reading if mmap fails
117                        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                // Use buffered reading outside the platform's mapping range.
131                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        // Finalize hash and convert to hex
148        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    /// Helper method to hash a file using buffered I/O
159    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    /// Helper method to hash a file using buffered I/O with progress bar
181    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        // Create progress bar
194        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            // Update progress bar at the specified interval
222            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        // Finish progress bar
230        pb.finish_and_clear();
231
232        Ok(())
233    }
234
235    /// Compute hash for a file using fast mode (sampling strategy)
236    ///
237    /// For files larger than 300MB, samples three 100MB regions:
238    /// - First 100MB
239    /// - Middle 100MB (centered at file_size/2)
240    /// - Last 100MB
241    ///
242    /// For files smaller than 300MB, uses the full file.
243    pub fn compute_hash_fast(
244        &self,
245        path: &Path,
246        algorithm: &str,
247    ) -> Result<HashResult, HashUtilityError> {
248        // Get hasher for the specified algorithm
249        let mut hasher = HashRegistry::get_hasher(algorithm)?;
250
251        // Open file for reading with better error context
252        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        // Get file size
256        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 is smaller than threshold, hash the entire file
264        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            // Sample three regions: first 100MB, middle 100MB, last 100MB
277
278            // Read first 100MB
279            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            // Calculate middle region: centered at file_size/2
290            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            // Read last 100MB
301            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        // Finalize hash and convert to hex
313        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    /// Helper function to read a specific region of a file
324    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        // Seek to the start position
334        file.seek(std::io::SeekFrom::Start(start))
335            .map_err(|e| HashUtilityError::from_io_error(e, "seeking", Some(path.to_path_buf())))?;
336
337        // Read up to 'length' bytes
338        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; // End of file
348            }
349
350            hasher.update(&buffer[..bytes_read]);
351            bytes_remaining -= bytes_read as u64;
352        }
353
354        Ok(())
355    }
356}