File tree Expand file tree Collapse file tree 2 files changed +50
-11
lines changed Expand file tree Collapse file tree 2 files changed +50
-11
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,12 @@ use crate::config::TomlConfig;
33use super :: { Config , Flags } ;
44use clap:: CommandFactory ;
55use serde:: Deserialize ;
6- use std:: { env, path:: Path } ;
6+ use std:: {
7+ env,
8+ fs:: { remove_file, File } ,
9+ io:: Write ,
10+ path:: Path ,
11+ } ;
712
813fn parse ( config : & str ) -> Config {
914 Config :: parse_inner ( & [ "check" . to_owned ( ) , "--config=/does/not/exist" . to_owned ( ) ] , |& _| {
@@ -196,3 +201,19 @@ fn rust_optimize() {
196201fn invalid_rust_optimize ( ) {
197202 parse ( "rust.optimize = \" a\" " ) ;
198203}
204+
205+ #[ test]
206+ fn verify_file_integrity ( ) {
207+ let config = parse ( "" ) ;
208+
209+ let tempfile = config. tempdir ( ) . join ( ".tmp-test-file" ) ;
210+ File :: create ( & tempfile) . unwrap ( ) . write_all ( b"dummy value" ) . unwrap ( ) ;
211+ assert ! ( tempfile. exists( ) ) ;
212+
213+ assert ! (
214+ config
215+ . verify( & tempfile, "7e255dd9542648a8779268a0f268b891a198e9828e860ed23f826440e786eae5" )
216+ ) ;
217+
218+ remove_file ( tempfile) . unwrap ( ) ;
219+ }
Original file line number Diff line number Diff line change @@ -320,25 +320,43 @@ impl Config {
320320 }
321321
322322 /// 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 {
324324 use sha2:: Digest ;
325325
326326 self . verbose ( & format ! ( "verifying {}" , path. display( ) ) ) ;
327+
328+ if self . dry_run ( ) {
329+ return false ;
330+ }
331+
327332 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 {
335352 println ! (
336353 "invalid checksum: \n \
337- found: {found }\n \
354+ found: {checksum }\n \
338355 expected: {expected}",
339356 ) ;
340357 }
341- return verified;
358+
359+ verified
342360 }
343361}
344362
You can’t perform that action at this time.
0 commit comments