1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::{path::Path, sync::atomic::AtomicBool};
use git_features::progress::Progress;
pub mod checksum {
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum Error {
#[error("Interrupted by user")]
Interrupted,
#[error("index checksum mismatch: expected {expected}, got {actual}")]
Mismatch {
expected: git_hash::ObjectId,
actual: git_hash::ObjectId,
},
}
}
pub fn fan(data: &[u32]) -> Option<usize> {
data.windows(2)
.enumerate()
.find_map(|(win_index, v)| (v[0] > v[1]).then(|| win_index))
}
pub fn checksum_on_disk_or_mmap(
data_path: &Path,
data: &[u8],
expected: git_hash::ObjectId,
object_hash: git_hash::Kind,
mut progress: impl Progress,
should_interrupt: &AtomicBool,
) -> Result<git_hash::ObjectId, checksum::Error> {
let data_len_without_trailer = data.len() - object_hash.len_in_bytes();
let actual = match git_features::hash::bytes_of_file(
data_path,
data_len_without_trailer,
object_hash,
&mut progress,
should_interrupt,
) {
Ok(id) => id,
Err(err) if err.kind() == std::io::ErrorKind::Interrupted => return Err(checksum::Error::Interrupted),
Err(_io_err) => {
let start = std::time::Instant::now();
let mut hasher = git_features::hash::hasher(object_hash);
hasher.update(&data[..data_len_without_trailer]);
progress.inc_by(data_len_without_trailer);
progress.show_throughput(start);
git_hash::ObjectId::from(hasher.digest())
}
};
if actual == expected {
Ok(actual)
} else {
Err(checksum::Error::Mismatch { actual, expected })
}
}