Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove -v/--print-progress option #50

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/download_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let tempdir = tempfile::tempdir()?;
let path = tempdir.path().join("tmpfile");
let res = download_and_hash(&client, url, &path, None, None, false)?;
let res = download_and_hash(&client, url, &path, None, None)?;
tempdir.close()?;

println!("hash: {}", res.hash_sha256);
Expand Down
2 changes: 1 addition & 1 deletion examples/full_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let tempdir = tempfile::tempdir()?;
let path = tempdir.path().join("tmpfile");
let res = ue_rs::download_and_hash(&client, url.clone(), &path, Some(expected_sha256.clone()), None, false).context(format!("download_and_hash({url:?}) failed"))?;
let res = ue_rs::download_and_hash(&client, url.clone(), &path, Some(expected_sha256.clone()), None).context(format!("download_and_hash({url:?}) failed"))?;
tempdir.close()?;

println!("\texpected sha256: {}", expected_sha256);
Expand Down
18 changes: 5 additions & 13 deletions src/bin/download_sysext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'a> Package<'a> {
Ok(())
}

fn download(&mut self, into_dir: &Path, client: &Client, print_progress: bool) -> Result<()> {
fn download(&mut self, into_dir: &Path, client: &Client) -> Result<()> {
// FIXME: use _range_start for completing downloads
let _range_start = match self.status {
PackageStatus::ToDownload => 0,
Expand All @@ -115,7 +115,6 @@ impl<'a> Package<'a> {
&path,
self.hash_sha256.clone(),
self.hash_sha1.clone(),
print_progress,
) {
Ok(ok) => ok,
Err(err) => {
Expand Down Expand Up @@ -251,12 +250,12 @@ fn get_pkgs_to_download<'a>(resp: &'a omaha::Response, glob_set: &GlobSet)
}

// Read data from remote URL into File
fn fetch_url_to_file<'a, U>(path: &'a Path, input_url: U, client: &'a Client, print_progress: bool) -> Result<Package<'a>>
fn fetch_url_to_file<'a, U>(path: &'a Path, input_url: U, client: &'a Client) -> Result<Package<'a>>
where
U: reqwest::IntoUrl + From<U> + std::clone::Clone + std::fmt::Debug,
Url: From<U>,
{
let r = ue_rs::download_and_hash(client, input_url.clone(), path, None, None, print_progress).context(format!("unable to download data(url {:?})", input_url))?;
let r = ue_rs::download_and_hash(client, input_url.clone(), path, None, None).context(format!("unable to download data(url {:?})", input_url))?;

Ok(Package {
name: Cow::Borrowed(path.file_name().unwrap_or(OsStr::new("fakepackage")).to_str().unwrap_or("fakepackage")),
Expand All @@ -268,10 +267,10 @@ where
})
}

fn do_download_verify(pkg: &mut Package<'_>, output_filename: Option<String>, output_dir: &Path, unverified_dir: &Path, pubkey_file: &str, client: &Client, print_progress: bool) -> Result<()> {
fn do_download_verify(pkg: &mut Package<'_>, output_filename: Option<String>, output_dir: &Path, unverified_dir: &Path, pubkey_file: &str, client: &Client) -> Result<()> {
pkg.check_download(unverified_dir)?;

pkg.download(unverified_dir, client, print_progress).context(format!("unable to download \"{:?}\"", pkg.name))?;
pkg.download(unverified_dir, client).context(format!("unable to download \"{:?}\"", pkg.name))?;

// Unverified payload is stored in e.g. "output_dir/.unverified/oem.gz".
// Verified payload is stored in e.g. "output_dir/oem.raw".
Expand Down Expand Up @@ -319,10 +318,6 @@ struct Args {
/// only take the first matching entry
#[argh(switch, short = 't')]
take_first_match: bool,

/// report download progress
#[argh(switch, short = 'v')]
print_progress: bool,
}

impl Args {
Expand Down Expand Up @@ -399,7 +394,6 @@ fn main() -> Result<(), Box<dyn Error>> {
&temp_payload_path,
Url::from_str(url.as_str()).context(anyhow!("failed to convert into url ({:?})", url))?,
&client,
args.print_progress,
)?;
do_download_verify(
&mut pkg_fake,
Expand All @@ -408,7 +402,6 @@ fn main() -> Result<(), Box<dyn Error>> {
unverified_dir.as_path(),
args.pubkey_file.as_str(),
&client,
args.print_progress,
)?;

// verify only a fake package, early exit and skip the rest.
Expand Down Expand Up @@ -442,7 +435,6 @@ fn main() -> Result<(), Box<dyn Error>> {
unverified_dir.as_path(),
args.pubkey_file.as_str(),
&client,
args.print_progress,
)?;
if args.take_first_match {
break;
Expand Down
34 changes: 5 additions & 29 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,7 @@ pub fn hash_on_disk<T: omaha::HashAlgo>(path: &Path, maxlen: Option<usize>) -> R
Ok(omaha::Hash::from_bytes(Box::new(hasher).finalize()))
}

fn do_download_and_hash<U>(
client: &Client,
url: U,
path: &Path,
expected_sha256: Option<omaha::Hash<omaha::Sha256>>,
expected_sha1: Option<omaha::Hash<omaha::Sha1>>,
print_progress: bool,
) -> Result<DownloadResult>
fn do_download_and_hash<U>(client: &Client, url: U, path: &Path, expected_sha256: Option<omaha::Hash<omaha::Sha256>>, expected_sha1: Option<omaha::Hash<omaha::Sha1>>) -> Result<DownloadResult>
where
U: reqwest::IntoUrl + Clone,
Url: From<U>,
Expand Down Expand Up @@ -96,9 +89,8 @@ where
}
}

if print_progress {
println!("writing to {}", path.display());
}
println!("writing to {}", path.display());

let mut file = File::create(path).context(format!("failed to create path ({:?})", path.display()))?;
res.copy_to(&mut file)?;

Expand Down Expand Up @@ -126,29 +118,13 @@ where
})
}

pub fn download_and_hash<U>(
client: &Client,
url: U,
path: &Path,
expected_sha256: Option<omaha::Hash<omaha::Sha256>>,
expected_sha1: Option<omaha::Hash<omaha::Sha1>>,
print_progress: bool,
) -> Result<DownloadResult>
pub fn download_and_hash<U>(client: &Client, url: U, path: &Path, expected_sha256: Option<omaha::Hash<omaha::Sha256>>, expected_sha1: Option<omaha::Hash<omaha::Sha1>>) -> Result<DownloadResult>
where
U: reqwest::IntoUrl + Clone,
Url: From<U>,
{
crate::retry_loop(
|| {
do_download_and_hash(
client,
url.clone(),
path,
expected_sha256.clone(),
expected_sha1.clone(),
print_progress,
)
},
|| do_download_and_hash(client, url.clone(), path, expected_sha256.clone(), expected_sha1.clone()),
MAX_DOWNLOAD_RETRY,
)
}
Loading