summaryrefslogtreecommitdiffstats
path: root/src/util.rs
diff options
context:
space:
mode:
authorLibravatar bigfoot547 <[email protected]>2025-01-22 19:03:31 -0600
committerLibravatar bigfoot547 <[email protected]>2025-01-22 19:03:31 -0600
commitb2edba152d0256a8921f3a25d67a062163a54f59 (patch)
tree0bdc8b1ea4fedbb4d18c8347da76884c381b45b9 /src/util.rs
parentmore jre download stuff (diff)
finish downloaded jres
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs42
1 files changed, 32 insertions, 10 deletions
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<Path>, expect_size: Option<usize>, expect_sha1: Option<Digest>) -> Result<bool, io::Error> {
+ 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<Path>, url: Option<&str>, expect_size: Option<usize>, expect_sha1: Option<Digest>, online: bool, force_download: bool) -> Result<bool, EnsureFileError> {
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);
}
}