From b2edba152d0256a8921f3a25d67a062163a54f59 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Wed, 22 Jan 2025 19:03:31 -0600 Subject: finish downloaded jres --- src/util.rs | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index 8ad3632..5ea0f26 100644 --- a/src/util.rs +++ b/src/util.rs @@ -7,6 +7,7 @@ use sha1_smol::{Digest, Sha1}; use tokio::fs::File; use tokio::{fs, io}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use crate::util; #[derive(Debug)] pub enum IntegrityError { @@ -151,20 +152,41 @@ impl Error for EnsureFileError { } } +pub async fn should_download(path: impl AsRef, expect_size: Option, expect_sha1: Option) -> Result { + let path = path.as_ref(); + + match verify_file(path, expect_size, expect_sha1).await { + Ok(()) => { + debug!("Skipping download for file {}, integrity matches.", path.display()); + Ok(false) + }, + Err(FileVerifyError::Open(_, e)) if e.kind() == ErrorKind::NotFound => { + debug!("File {} is missing, downloading it.", path.display()); + Ok(true) + }, + Err(FileVerifyError::Integrity(_, e)) => { + warn!("Integrity error on file: {}", e); + + // try to delete the file since it's bad + let _ = fs::remove_file(path).await + .map_err(|e| warn!("Error deleting corrupted/modified file {} (ignoring): {}", path.display(), e)); + Ok(true) + } + Err(FileVerifyError::Open(_, e) | FileVerifyError::Read(_, e)) => { + warn!("Error verifying file {} on disk: {}", path.display(), e); + Err(e) + } + } +} + pub async fn ensure_file(path: impl AsRef, url: Option<&str>, expect_size: Option, expect_sha1: Option, online: bool, force_download: bool) -> Result { let path = path.as_ref(); if !force_download { - match verify_file(path, expect_size, expect_sha1).await { - Ok(_) => { - info!("File {} exists and integrity matches. Skipping.", path.display()); - return Ok(false); - }, - Err(FileVerifyError::Open(_, e)) if e.kind() == ErrorKind::NotFound => (), - Err(FileVerifyError::Integrity(_, e)) => - info!("File {} on disk failed integrity check: {}", path.display(), e), - Err(FileVerifyError::Open(_, e)) | Err(FileVerifyError::Read(_, e)) => - return Err(EnsureFileError::IO { what: "verifying fileon disk", error: e }) + if !should_download(path, expect_size, expect_sha1).await + .map_err(|e| EnsureFileError::IO { what: "verifying file on disk", error: e })? { + + return Ok(false); } } -- cgit v1.2.3-70-g09d2