@@ -76,16 +76,17 @@ impl Drop for Server {
76
76
}
77
77
}
78
78
79
- async fn file_not_exist_or_hash_mismatch ( filename : & Path , expected_hash : & str ) -> Result < bool , ( ) > {
80
- match File :: open ( filename) . await {
81
- Ok ( mut file) => {
82
- let mut buffer = Vec :: new ( ) ;
83
- file. read_to_end ( & mut buffer) . await . map_err ( |_| ( ) ) ?;
79
+ async fn hashes_match ( mut file : File , expected_hash : & str ) -> Result < bool , ( ) > {
80
+ let mut buffer = Vec :: new ( ) ;
81
+ file. read_to_end ( & mut buffer) . await . map_err ( |_| ( ) ) ?;
84
82
85
- let actual_hash = hex:: encode ( bitcoin:: hashes:: sha256:: Hash :: hash ( & buffer) ) ;
83
+ let actual_hash = hex:: encode ( bitcoin:: hashes:: sha256:: Hash :: hash ( & buffer) ) ;
84
+ Ok ( actual_hash == expected_hash)
85
+ }
86
86
87
- Ok ( actual_hash != expected_hash)
88
- }
87
+ async fn file_not_exist_or_hash_mismatch ( filename : & Path , expected_hash : & str ) -> Result < bool , ( ) > {
88
+ match File :: open ( filename) . await {
89
+ Ok ( file) => Ok ( !hashes_match ( file, expected_hash) . await ?) ,
89
90
Err ( ref e) if e. kind ( ) == io:: ErrorKind :: NotFound => Ok ( true ) ,
90
91
Err ( _) => Err ( ( ) ) ,
91
92
}
@@ -133,6 +134,22 @@ async fn download_simulators() -> Result<Vec<String>, ()> {
133
134
fs:: set_permissions ( & filename, std:: fs:: Permissions :: from_mode ( 0o755 ) )
134
135
. await
135
136
. map_err ( |_| ( ) ) ?;
137
+ match File :: open ( & filename) . await {
138
+ Ok ( file) => {
139
+ if !hashes_match ( file, & simulator. sha256 )
140
+ . await
141
+ . map_err ( |_| ( ) ) ?
142
+ {
143
+ eprintln ! (
144
+ "Hash mismatch for simulator file '{}', expected {}" ,
145
+ filename. display( ) ,
146
+ simulator. sha256
147
+ ) ;
148
+ return Err ( ( ) ) ;
149
+ }
150
+ }
151
+ Err ( _) => return Err ( ( ) ) , // This should never happen as we just created it.
152
+ }
136
153
}
137
154
filenames. push ( filename. to_str ( ) . unwrap ( ) . to_string ( ) ) ;
138
155
}
0 commit comments