verify local file hash, add retry, add cache busting on retry

This commit is contained in:
2024-09-24 17:53:45 +02:00
parent db3ef786ac
commit 519f70dd45
2 changed files with 44 additions and 11 deletions

View File

@@ -267,15 +267,20 @@ async fn update_dir(
} }
// Prompt user to retry downloads if they fail // Prompt user to retry downloads if they fail
let mut download_complete: bool = false; let mut download_complete = false;
let mut bust_cache = false;
let mut local_hash = String::default();
while !download_complete { while !download_complete {
if let Err(err) = http_async::download_file_progress( let url = format!("{}/{}", MASTER.lock().unwrap(), file.name);
&client, let url = if bust_cache {
pb, bust_cache = false;
&format!("{}/{}", MASTER.lock().unwrap(), file.name), format!("{}?{}", url, misc::random_string(6))
&file_path, } else {
file.size as u64, url
) };
if let Err(err) =
http_async::download_file_progress(&client, pb, &url, &file_path, file.size as u64)
.await .await
{ {
let file_name = file_path.clone().cute_path(); let file_name = file_path.clone().cute_path();
@@ -291,11 +296,28 @@ async fn update_dir(
); );
} }
}; };
local_hash = file_path.get_blake3().unwrap().to_lowercase();
let remote = file.blake3.to_lowercase();
if local_hash != remote {
println_error!("Downloaded file hash does not match remote!\nRemote {remote}, local {local_hash}, {}\nIf this issue persists please try again in 15 minutes.", file_path.cute_path());
println!("Retry download? (Y/n)");
let input = misc::stdin().to_ascii_lowercase();
if input != "n" {
println_info!(
"Retrying download for {} due to hash mismatch",
file_path.cute_path()
);
bust_cache = true;
continue;
}
}
download_complete = true; download_complete = true;
} }
let hash = file_path.get_blake3().unwrap(); hashes.insert(file_name.to_owned(), local_hash);
hashes.insert(file_name.to_owned(), hash.to_lowercase());
#[cfg(unix)] #[cfg(unix)]
if file_name.ends_with(".exe") { if file_name.ends_with(".exe") {
let perms = std::os::unix::fs::PermissionsExt::from_mode(0o755); let perms = std::os::unix::fs::PermissionsExt::from_mode(0o755);

View File

@@ -158,3 +158,14 @@ pub fn save_cache(dir: &Path, cache: structs::Cache) {
println_error!("Failed to save cache: {}", e); println_error!("Failed to save cache: {}", e);
}); });
} }
pub fn random_string(length: u32) -> String {
use rand::Rng;
let mut rng = rand::thread_rng();
let mut result = String::new();
for _ in 0..length {
let random: u8 = rng.gen_range(33..127);
result.push(random as char);
}
result
}