File tree 1 file changed +28
-10
lines changed
1 file changed +28
-10
lines changed Original file line number Diff line number Diff line change @@ -320,25 +320,43 @@ 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
+ let mut reader = BufReader :: new ( file) ;
336
+
337
+ loop {
338
+ let buffer = t ! ( reader. fill_buf( ) ) ;
339
+ let l = buffer. len ( ) ;
340
+ // break if EOF
341
+ if l == 0 {
342
+ break ;
343
+ }
344
+ hasher. update ( buffer) ;
345
+ reader. consume ( l) ;
346
+ }
347
+
348
+ let checksum = hex:: encode ( hasher. finalize ( ) . as_slice ( ) ) ;
349
+ let verified = checksum == expected;
350
+
351
+ if !verified {
335
352
println ! (
336
353
"invalid checksum: \n \
337
- found: {found }\n \
354
+ found: {checksum }\n \
338
355
expected: {expected}",
339
356
) ;
340
357
}
341
- return verified;
358
+
359
+ verified
342
360
}
343
361
}
344
362
You can’t perform that action at this time.
0 commit comments