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
parent
6d0bfeedaf
commit
4c7192582d
11
src/main.rs
11
src/main.rs
|
@ -303,14 +303,11 @@ fn checksum(path: impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
|
||||||
let mut blake = Blake2b512::new();
|
let mut blake = Blake2b512::new();
|
||||||
loop {
|
loop {
|
||||||
let mut buf = vec![0u8; 16384];
|
let mut buf = vec![0u8; 16384];
|
||||||
match f.read_exact(&mut buf) {
|
let sz = f.read(&mut buf)?;
|
||||||
Ok(_) => blake.update(buf),
|
if sz == 0 {
|
||||||
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
|
break;
|
||||||
blake.update(buf);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
}
|
}
|
||||||
|
blake.update(&buf[..sz]);
|
||||||
}
|
}
|
||||||
Ok(blake.finalize().to_vec())
|
Ok(blake.finalize().to_vec())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue