@@ -16,7 +16,10 @@ use reqwest::RequestBuilder;
1616use serde:: { de, Deserialize , Deserializer } ;
1717use sha2:: { Digest , Sha256 } ;
1818use time:: OffsetDateTime ;
19- use tokio:: io:: BufWriter ;
19+ use tokio:: {
20+ fs:: File ,
21+ io:: { AsyncReadExt , BufWriter } ,
22+ } ;
2023use tracing:: { debug, error, info, info_span, warn, Instrument } ;
2124use typed_builder:: TypedBuilder ;
2225
@@ -211,7 +214,7 @@ impl BatchClient<'_> {
211214 let mut retries = 0 ;
212215 ' retry: loop {
213216 let mut req = self . inner . get_with_path ( url. path ( ) ) ?;
214- match Self :: check_if_exists ( path, exp_size) . await ? {
217+ match Self :: check_if_exists ( path, exp_size, & mut hasher ) . await ? {
215218 Header :: Skip => {
216219 return Ok ( ( ) ) ;
217220 }
@@ -261,7 +264,11 @@ impl BatchClient<'_> {
261264 . await
262265 }
263266
264- async fn check_if_exists ( path : & Path , exp_size : u64 ) -> crate :: Result < Header > {
267+ async fn check_if_exists (
268+ path : & Path ,
269+ exp_size : u64 ,
270+ hasher : & mut Option < Sha256 > ,
271+ ) -> crate :: Result < Header > {
265272 let Ok ( metadata) = tokio:: fs:: metadata ( path) . await else {
266273 return Ok ( Header :: Range ( None ) ) ;
267274 } ;
@@ -273,6 +280,17 @@ impl BatchClient<'_> {
273280 total_bytes = exp_size,
274281 "Found existing file, resuming download"
275282 ) ;
283+ if let Some ( hasher) = hasher {
284+ let mut buf = vec ! [ 0 ; 1 << 23 ] ;
285+ let mut file = File :: open ( path) . await ?;
286+ loop {
287+ let read_size = file. read ( & mut buf) . await ?;
288+ if read_size == 0 {
289+ break ;
290+ }
291+ hasher. update ( & buf[ ..read_size] ) ;
292+ }
293+ }
276294 }
277295 Ordering :: Equal => {
278296 debug ! ( "Skipping download as file already exists and matches expected size" ) ;
@@ -299,7 +317,7 @@ impl BatchClient<'_> {
299317 if hash_hex != exp_hash_hex {
300318 warn ! (
301319 hash_hex,
302- exp_hash_hex, "Downloaded file failed checksum validation "
320+ exp_hash_hex, "Downloaded file failed checksum verification "
303321 ) ;
304322 } else {
305323 debug ! ( "Successfully verified checksum" ) ;
0 commit comments