File tree Expand file tree Collapse file tree 1 file changed +29
-10
lines changed Expand file tree Collapse file tree 1 file changed +29
-10
lines changed Original file line number Diff line number Diff line change @@ -320,25 +320,44 @@ impl Config {
320
320
}
321
321
322
322
/// Returns whether the SHA256 checksum of `path` matches `expected`.
323
- fn verify ( & self , path : & Path , expected : & str ) -> bool {
323
+ pub ( crate ) fn verify ( & self , path : & Path , expected : & str ) -> bool {
324
324
use sha2:: Digest ;
325
325
326
326
self . verbose ( & format ! ( "verifying {}" , path. display( ) ) ) ;
327
+
328
+ if self . dry_run ( ) {
329
+ return false ;
330
+ }
331
+
327
332
let mut hasher = sha2:: Sha256 :: new ( ) ;
328
- // FIXME: this is ok for rustfmt (4.1 MB large at time of writing), but it seems memory-intensive for rustc and larger components.
329
- // Consider using streaming IO instead?
330
- let contents = if self . dry_run ( ) { vec ! [ ] } else { t ! ( fs:: read( path) ) } ;
331
- hasher. update ( & contents) ;
332
- let found = hex:: encode ( hasher. finalize ( ) . as_slice ( ) ) ;
333
- let verified = found == expected;
334
- if !verified && !self . dry_run ( ) {
333
+
334
+ let file = t ! ( File :: open( path) ) ;
335
+ const BUFFER_SIZE : usize = 4096 ; // read in chunks of 4KB
336
+ let mut reader = BufReader :: with_capacity ( BUFFER_SIZE , file) ;
337
+
338
+ loop {
339
+ let buffer = t ! ( reader. fill_buf( ) ) ;
340
+ let l = buffer. len ( ) ;
341
+ // break if EOF
342
+ if l == 0 {
343
+ break ;
344
+ }
345
+ hasher. update ( buffer) ;
346
+ reader. consume ( l) ;
347
+ }
348
+
349
+ let checksum = hex:: encode ( hasher. finalize ( ) . as_slice ( ) ) ;
350
+ let verified = checksum == expected;
351
+
352
+ if !verified {
335
353
println ! (
336
354
"invalid checksum: \n \
337
- found: {found }\n \
355
+ found: {checksum }\n \
338
356
expected: {expected}",
339
357
) ;
340
358
}
341
- return verified;
359
+
360
+ verified
342
361
}
343
362
}
344
363
You can’t perform that action at this time.
0 commit comments