checksum: Fix EOF handling

Using `Read::read_exact` is not the correct way to read a file in
chunks.  When it returns `UnexpectedEof`, the buffer has data in it, but
is zero-padded to the end.  This causes the calculated checksum to be
incorrect.
master
Dustin 2024-01-12 09:42:31 -06:00
parent 6d0bfeedaf
commit 4c7192582d
1 changed files with 4 additions and 7 deletions

View File

@ -303,14 +303,11 @@ fn checksum(path: impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
let mut blake = Blake2b512::new();
loop {
let mut buf = vec![0u8; 16384];
match f.read_exact(&mut buf) {
Ok(_) => blake.update(buf),
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
blake.update(buf);
let sz = f.read(&mut buf)?;
if sz == 0 {
break;
}
Err(e) => return Err(e),
}
blake.update(&buf[..sz]);
}
Ok(blake.finalize().to_vec())
}