Skip to content

Commit 9416552

Browse files
committed
fix: remove image while sha256 is error
1 parent 6dcfa51 commit 9416552

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

xtask/src/image.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,19 @@ async fn image_download(image_name: &str, output_dir: Option<String>, extract: b
330330
}
331331
Ok(false) => {
332332
println!("File verification failed, SHA256 does not match, will re-download");
333+
// Remove the invalid file before downloading
334+
match fs::remove_file(&output_path) {
335+
Ok(_) => println!("Successfully removed invalid file"),
336+
Err(e) => println!("Warning: Failed to remove invalid file: {}, but will continue with download", e),
337+
}
333338
}
334339
Err(e) => {
335340
println!("Error verifying file: {}, will re-download", e);
341+
// Remove the potentially corrupted file before downloading
342+
match fs::remove_file(&output_path) {
343+
Ok(_) => println!("Successfully removed potentially corrupted file"),
344+
Err(remove_err) => println!("Warning: Failed to remove potentially corrupted file: {}, but will continue with download", remove_err),
345+
}
336346
}
337347
}
338348
} else {
@@ -354,8 +364,13 @@ async fn image_download(image_name: &str, output_dir: Option<String>, extract: b
354364

355365
let bytes = response.bytes().await?;
356366

357-
// Write all bytes at once
358-
let mut file = File::create(&output_path).await?;
367+
// Write all bytes at once, ensuring we overwrite any existing file
368+
let mut file = tokio::fs::OpenOptions::new()
369+
.write(true)
370+
.create(true)
371+
.truncate(true)
372+
.open(&output_path)
373+
.await?;
359374
file.write_all(&bytes).await?;
360375

361376
println!("Download completed ({} bytes)", bytes.len());
@@ -367,9 +382,19 @@ async fn image_download(image_name: &str, output_dir: Option<String>, extract: b
367382
println!("Download completed, file verification successful");
368383
}
369384
Ok(false) => {
385+
// Remove the invalid downloaded file
386+
match fs::remove_file(&output_path) {
387+
Ok(_) => println!("Successfully removed invalid downloaded file"),
388+
Err(e) => println!("Warning: Failed to remove invalid downloaded file: {}", e),
389+
}
370390
return Err(anyhow!("Downloaded file SHA256 verification failed"));
371391
}
372392
Err(e) => {
393+
// Remove the potentially corrupted downloaded file
394+
match fs::remove_file(&output_path) {
395+
Ok(_) => println!("Successfully removed potentially corrupted downloaded file"),
396+
Err(remove_err) => println!("Warning: Failed to remove potentially corrupted downloaded file: {}", remove_err),
397+
}
373398
return Err(anyhow!("Error verifying downloaded file: {}", e));
374399
}
375400
}

0 commit comments

Comments
 (0)