diff options
| author | 2025-01-12 03:58:01 -0600 | |
|---|---|---|
| committer | 2025-01-12 03:58:01 -0600 | |
| commit | c0986823af246ccee2247b881974a2b7ce6ee491 (patch) | |
| tree | 9dcb8a66692d5c0067450c60e1de72bd4fce92a5 /src/launcher/download.rs | |
| parent | use a macro for map_err (diff) | |
add some logging and stuff
Diffstat (limited to 'src/launcher/download.rs')
| -rw-r--r-- | src/launcher/download.rs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/launcher/download.rs b/src/launcher/download.rs index 28c3b86..8c24cae 100644 --- a/src/launcher/download.rs +++ b/src/launcher/download.rs @@ -3,6 +3,7 @@ use std::fmt::{Debug, Display, Formatter}; use std::io::ErrorKind; use std::path::{Path, PathBuf}; use futures::{stream, Stream, StreamExt}; +use log::debug; use reqwest::{Client, IntoUrl, Method, RequestBuilder}; use sha1_smol::{Digest, Sha1}; use tokio::fs; @@ -101,7 +102,7 @@ impl<T: Download> MultiDownloader<T> { pub async fn perform(&mut self) -> impl Stream<Item = Result<(), PhaseDownloadError<T>>> { stream::iter(self.jobs.iter_mut()).map(|job| { let client = &self.client; - + macro_rules! map_err { ($result:expr, $phase:expr, $job:expr) => { match $result { @@ -117,7 +118,7 @@ impl<T: Download> MultiDownloader<T> { .header(reqwest::header::USER_AGENT, USER_AGENT)).await, Phase::Prepare, job) else { return Ok(()) }; - + let mut data = map_err!(map_err!(rq.send().await, Phase::Send, job).error_for_status(), Phase::Send, job).bytes_stream(); while let Some(bytes) = data.next().await { @@ -220,13 +221,21 @@ impl Download for VerifiedDownload { Ok(file) => file, Err(e) => return if e.kind() == ErrorKind::NotFound { // assume the parent folder exists (responsibility of the caller to ensure this) + debug!("File {} does not exist, downloading it.", self.path.to_string_lossy()); self.open_output().await?; Ok(Some(req)) } else { + debug!("Error opening {}: {}", self.path.to_string_lossy(), e); Err(e.into()) } }; + // short-circuit this + if self.expect_size.is_none() && self.expect_sha1.is_none() { + debug!("No size or sha1 for {}, have to assume it's good.", self.path.to_string_lossy()); + return Ok(None); + } + let mut tally = 0usize; let mut sha1 = Sha1::new(); @@ -236,7 +245,10 @@ impl Download for VerifiedDownload { Ok(n) => n, Err(e) => match e.kind() { ErrorKind::Interrupted => continue, - _ => return Err(e.into()) + _ => { + debug!("Error reading {}: {}", self.path.to_string_lossy(), e); + return Err(e.into()); + } } }; @@ -248,6 +260,7 @@ impl Download for VerifiedDownload { if self.expect_sha1.is_none_or(|d| d == sha1.digest()) && self.expect_size.is_none_or(|s| s == tally) { + debug!("Not downloading {}, sha1 and size match.", self.path.to_string_lossy()); return Ok(None); } @@ -255,6 +268,8 @@ impl Download for VerifiedDownload { // potentially racy to close the file and reopen it... :/ self.open_output().await?; + + debug!("Downloading {} because sha1 or size does not match.", self.path.to_string_lossy()); Ok(Some(req)) } @@ -271,13 +286,17 @@ impl Download for VerifiedDownload { if let Some(d) = self.expect_sha1 { if d != digest { + debug!("Could not download {}: sha1 mismatch (exp {}, got {}).", self.path.to_string_lossy(), d, digest); return Err(IntegrityError::Sha1Mismatch { expect: d, actual: digest }.into()); } } else if let Some(s) = self.expect_size { if s != self.tally { + debug!("Could not download {}: size mismatch (exp {}, got {}).", self.path.to_string_lossy(), s, self.tally); return Err(IntegrityError::SizeMismatch { expect: s, actual: self.tally }.into()); } } + + debug!("Successfully downloaded {} ({} bytes).", self.path.to_string_lossy(), self.tally); // release the file descriptor (don't want to wait until it's dropped automatically because idk when that would be) drop(self.file.take().unwrap()); |
