1
- use anyhow:: { bail, Result } ;
2
1
use sha2:: { Digest , Sha256 } ;
3
2
use std:: {
4
3
env,
4
+ fs:: { copy, create_dir_all, read_to_string, remove_dir_all, rename, set_permissions, write} ,
5
5
io:: { Cursor , Read } ,
6
6
path:: { Path , PathBuf } ,
7
7
} ;
@@ -93,78 +93,78 @@ impl OvmfPaths {
93
93
/// 1. Command-line arg
94
94
/// 2. Environment variable
95
95
/// 3. Prebuilt file (automatically downloaded)
96
- pub fn find_ovmf_file ( file_type : OvmfFileType ) -> Result < PathBuf > {
96
+ pub fn find_ovmf_file ( file_type : OvmfFileType ) -> PathBuf {
97
97
if let Some ( path) = file_type. get_user_provided_path ( ) {
98
98
// The user provided an exact path to use; verify that it
99
99
// exists.
100
100
if path. exists ( ) {
101
- Ok ( path)
101
+ path
102
102
} else {
103
- bail ! (
103
+ panic ! (
104
104
"ovmf {} file does not exist: {}" ,
105
105
file_type. as_str( ) ,
106
106
path. display( )
107
107
) ;
108
108
}
109
109
} else {
110
- let prebuilt_dir = update_prebuilt ( ) ? ;
110
+ let prebuilt_dir = update_prebuilt ( ) ;
111
111
112
- Ok ( prebuilt_dir. join ( format ! (
112
+ prebuilt_dir. join ( format ! (
113
113
"x86_64/{}.{}" ,
114
114
file_type. as_str( ) ,
115
115
file_type. extension( )
116
- ) ) )
116
+ ) )
117
117
}
118
118
}
119
119
120
120
/// Find path to OVMF files by the strategy documented for
121
121
/// [`Self::find_ovmf_file`].
122
- pub fn find ( ) -> Result < Self > {
123
- let code = Self :: find_ovmf_file ( OvmfFileType :: Code ) ? ;
124
- let vars = Self :: find_ovmf_file ( OvmfFileType :: Vars ) ? ;
125
- let shell = Self :: find_ovmf_file ( OvmfFileType :: Shell ) ? ;
122
+ pub fn find ( ) -> Self {
123
+ let code = Self :: find_ovmf_file ( OvmfFileType :: Code ) ;
124
+ let vars = Self :: find_ovmf_file ( OvmfFileType :: Vars ) ;
125
+ let shell = Self :: find_ovmf_file ( OvmfFileType :: Shell ) ;
126
126
127
- Ok ( Self {
127
+ Self {
128
128
code,
129
129
vars,
130
130
shell,
131
131
temp_dir : None ,
132
132
temp_vars : None ,
133
- } )
133
+ }
134
134
}
135
135
136
136
/// Creates a copy with a writable, temp copy
137
- pub fn with_temp_vars ( & mut self ) -> Result < ( ) > {
138
- let temp_dir = TempDir :: new ( ) ? ;
137
+ pub fn with_temp_vars ( & mut self ) {
138
+ let temp_dir = TempDir :: new ( ) . expect ( "Failed to create temp directory" ) ;
139
139
let temp_path = temp_dir. path ( ) ;
140
140
141
141
// Make a copy of the OVMF vars file so that it can be used
142
142
// read+write without modifying the original. Under AArch64, some
143
143
// versions of OVMF won't boot if the vars file isn't writeable.
144
144
let ovmf_vars = temp_path. join ( "ovmf_vars" ) ;
145
- fs_err :: copy ( & self . vars , & ovmf_vars) ? ;
145
+ copy ( & self . vars , & ovmf_vars) . expect ( "Failed to copy vars file to a temp location" ) ;
146
146
// Necessary, as for example on NixOS, the files are read-only inside
147
147
// the Nix store.
148
148
#[ cfg( target_os = "linux" ) ]
149
- fs_err:: set_permissions ( & ovmf_vars, Permissions :: from_mode ( 0o666 ) ) ?;
149
+ set_permissions ( & ovmf_vars, Permissions :: from_mode ( 0o666 ) )
150
+ . expect ( "Failed to set permissions on temp vars file" ) ;
150
151
151
152
self . temp_vars = Some ( ovmf_vars) ;
152
153
self . temp_dir = Some ( temp_dir) ;
153
- Ok ( ( ) )
154
154
}
155
155
}
156
156
157
157
/// Update the local copy of the prebuilt OVMF files. Does nothing if the local
158
158
/// copy is already up to date.
159
- fn update_prebuilt ( ) -> Result < PathBuf > {
159
+ fn update_prebuilt ( ) -> PathBuf {
160
160
let prebuilt_dir = Path :: new ( OVMF_PREBUILT_DIR ) ;
161
161
let hash_path = prebuilt_dir. join ( "sha256" ) ;
162
162
163
163
// Check if the hash file already has the expected hash in it. If so, assume
164
164
// that we've already got the correct prebuilt downloaded and unpacked.
165
- if let Ok ( current_hash) = fs_err :: read_to_string ( & hash_path) {
165
+ if let Ok ( current_hash) = read_to_string ( & hash_path) {
166
166
if current_hash == OVMF_PREBUILT_HASH {
167
- return Ok ( prebuilt_dir. to_path_buf ( ) ) ;
167
+ return prebuilt_dir. to_path_buf ( ) ;
168
168
}
169
169
}
170
170
@@ -174,12 +174,12 @@ fn update_prebuilt() -> Result<PathBuf> {
174
174
release = OVMF_PREBUILT_TAG
175
175
) ;
176
176
177
- let data = download_url ( & url) ? ;
177
+ let data = download_url ( & url) ;
178
178
179
179
// Validate the hash.
180
180
let actual_hash = format ! ( "{:x}" , Sha256 :: digest( & data) ) ;
181
181
if actual_hash != OVMF_PREBUILT_HASH {
182
- bail ! (
182
+ panic ! (
183
183
"file hash {actual_hash} does not match {}" ,
184
184
OVMF_PREBUILT_HASH
185
185
) ;
@@ -189,27 +189,29 @@ fn update_prebuilt() -> Result<PathBuf> {
189
189
println ! ( "decompressing tarball" ) ;
190
190
let mut decompressed = Vec :: new ( ) ;
191
191
let mut compressed = Cursor :: new ( data) ;
192
- lzma_rs:: xz_decompress ( & mut compressed, & mut decompressed) ?;
192
+ lzma_rs:: xz_decompress ( & mut compressed, & mut decompressed)
193
+ . expect ( "Failed to decompress tarball" ) ;
193
194
194
195
// Clear out the existing prebuilt dir, if present.
195
- let _ = fs_err :: remove_dir_all ( prebuilt_dir) ;
196
+ let _ = remove_dir_all ( prebuilt_dir) ;
196
197
197
198
// Extract the files.
198
- extract_prebuilt ( & decompressed, prebuilt_dir) ? ;
199
+ extract_prebuilt ( & decompressed, prebuilt_dir) ;
199
200
200
201
// Rename the x64 directory to x86_64, to match `Arch::as_str`.
201
- fs_err:: rename ( prebuilt_dir. join ( "x64" ) , prebuilt_dir. join ( "x86_64" ) ) ?;
202
+ rename ( prebuilt_dir. join ( "x64" ) , prebuilt_dir. join ( "x86_64" ) )
203
+ . expect ( "Failed to rename x64 artefacts" ) ;
202
204
203
205
// Write out the hash file. When we upgrade to a new release of
204
206
// ovmf-prebuilt, the hash will no longer match, triggering a fresh
205
207
// download.
206
- fs_err :: write ( & hash_path, actual_hash) ? ;
208
+ write ( & hash_path, actual_hash) . expect ( "Failed to save calculated hash" ) ;
207
209
208
- Ok ( prebuilt_dir. to_path_buf ( ) )
210
+ prebuilt_dir. to_path_buf ( )
209
211
}
210
212
211
213
/// Download `url` and return the raw data.
212
- fn download_url ( url : & str ) -> Result < Vec < u8 > > {
214
+ fn download_url ( url : & str ) -> Vec < u8 > {
213
215
let agent: Agent = ureq:: AgentBuilder :: new ( )
214
216
. user_agent ( "uefi-rs-ovmf-downloader" )
215
217
. build ( ) ;
@@ -219,43 +221,42 @@ fn download_url(url: &str) -> Result<Vec<u8>> {
219
221
220
222
// Download the file.
221
223
println ! ( "downloading {url}" ) ;
222
- let resp = agent. get ( url) . call ( ) ? ;
224
+ let resp = agent. get ( url) . call ( ) . unwrap ( ) ;
223
225
let mut data = Vec :: with_capacity ( max_size_in_bytes) ;
224
226
resp. into_reader ( )
225
227
. take ( max_size_in_bytes. try_into ( ) . unwrap ( ) )
226
- . read_to_end ( & mut data) ?;
228
+ . read_to_end ( & mut data)
229
+ . unwrap ( ) ;
227
230
println ! ( "received {} bytes" , data. len( ) ) ;
228
231
229
- Ok ( data)
232
+ data
230
233
}
231
234
232
235
// Extract the tarball's files into `prebuilt_dir`.
233
236
//
234
237
// `tarball_data` is raw decompressed tar data.
235
- fn extract_prebuilt ( tarball_data : & [ u8 ] , prebuilt_dir : & Path ) -> Result < ( ) > {
238
+ fn extract_prebuilt ( tarball_data : & [ u8 ] , prebuilt_dir : & Path ) {
236
239
let cursor = Cursor :: new ( tarball_data) ;
237
240
let mut archive = Archive :: new ( cursor) ;
238
241
239
242
// Extract each file entry.
240
- for entry in archive. entries ( ) ? {
241
- let mut entry = entry? ;
243
+ for entry in archive. entries ( ) . unwrap ( ) {
244
+ let mut entry = entry. unwrap ( ) ;
242
245
243
246
// Skip directories.
244
247
if entry. size ( ) == 0 {
245
248
continue ;
246
249
}
247
250
248
- let path = entry. path ( ) ? ;
251
+ let path = entry. path ( ) . unwrap ( ) ;
249
252
// Strip the leading directory, which is the release name.
250
253
let path: PathBuf = path. components ( ) . skip ( 1 ) . collect ( ) ;
251
254
252
255
let dir = path. parent ( ) . unwrap ( ) ;
253
256
let dst_dir = prebuilt_dir. join ( dir) ;
254
257
let dst_path = prebuilt_dir. join ( path) ;
255
258
println ! ( "unpacking to {}" , dst_path. display( ) ) ;
256
- fs_err :: create_dir_all ( dst_dir) ? ;
257
- entry. unpack ( dst_path) ? ;
259
+ create_dir_all ( dst_dir) . unwrap ( ) ;
260
+ entry. unpack ( dst_path) . unwrap ( ) ;
258
261
}
259
-
260
- Ok ( ( ) )
261
262
}
0 commit comments