Skip to content

Commit d22bbf4

Browse files
authored
Merge pull request #95 from james-parky/dev-anyhow-rem
Remove `anyhow` Dependency
2 parents 9d372a7 + 2197e66 commit d22bbf4

File tree

17 files changed

+326
-219
lines changed

17 files changed

+326
-219
lines changed

Cargo.lock

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
anyhow = "1.0.75"
109
argh = "0.1"
1110
env_logger = "0.10"
1211
globset = "0.4"

examples/full_test.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::error::Error;
22
use std::borrow::Cow;
33

4-
use anyhow::{Context, Result};
54
use hard_xml::XmlRead;
65
use url::Url;
76
use omaha::Sha256Digest;
87

9-
fn get_pkgs_to_download(resp: &omaha::Response) -> Result<Vec<(Url, Sha256Digest)>> {
8+
fn get_pkgs_to_download(resp: &omaha::Response) -> Result<Vec<(Url, Sha256Digest)>, ()> {
109
let mut to_download: Vec<(Url, Sha256Digest)> = Vec::new();
1110

1211
for app in &resp.apps {
@@ -59,19 +58,18 @@ fn main() -> Result<(), Box<dyn Error>> {
5958
track: Cow::Borrowed(TRACK_DEFAULT),
6059
};
6160

62-
let response_text = ue_rs::request::perform(&client, parameters).context(format!(
63-
"perform({APP_VERSION_DEFAULT}, {MACHINE_ID_DEFAULT}, {TRACK_DEFAULT}) failed"
64-
))?;
61+
let response_text = ue_rs::request::perform(&client, parameters)?;
6562

6663
println!("response:\n\t{:#?}", response_text);
6764
println!();
6865

6966
////
7067
// parse response
7168
////
72-
let resp = omaha::Response::from_str(&response_text).context("failed to parse response")?;
69+
let resp = omaha::Response::from_str(&response_text)?;
7370

74-
let pkgs_to_dl = get_pkgs_to_download(&resp).context("failed to get packages to download")?;
71+
// TODO: fine for an example but shouldnt recommend
72+
let pkgs_to_dl = get_pkgs_to_download(&resp).unwrap();
7573

7674
////
7775
// download
@@ -81,7 +79,7 @@ fn main() -> Result<(), Box<dyn Error>> {
8179

8280
let tempdir = tempfile::tempdir()?;
8381
let path = tempdir.path().join("tmpfile");
84-
let res = ue_rs::download_and_hash(&client, url.clone(), &path, Some(expected_sha256.clone()), None).context(format!("download_and_hash({url:?}) failed"))?;
82+
let res = ue_rs::download_and_hash(&client, url.clone(), &path, Some(expected_sha256.clone()), None)?;
8583
tempdir.close()?;
8684

8785
println!("\texpected sha256: {:?}", expected_sha256);

examples/request.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::error::Error;
22
use std::borrow::Cow;
33

4-
use anyhow::Context;
5-
64
use ue_rs::request;
75

86
fn main() -> Result<(), Box<dyn Error>> {
@@ -19,9 +17,7 @@ fn main() -> Result<(), Box<dyn Error>> {
1917
track: Cow::Borrowed(TRACK_DEFAULT),
2018
};
2119

22-
let response = request::perform(&client, parameters).context(format!(
23-
"perform({APP_VERSION_DEFAULT}, {MACHINE_ID_DEFAULT}, {TRACK_DEFAULT}) failed"
24-
))?;
20+
let response = request::perform(&client, parameters)?;
2521

2622
println!("response:\n\t{:#?}", response);
2723

examples/response.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::error::Error;
22

3-
use anyhow::Context;
43
use hard_xml::XmlRead;
54
use omaha;
65

@@ -30,7 +29,8 @@ fn main() -> Result<(), Box<dyn Error>> {
3029
println!("{}", RESPONSE_XML);
3130
println!();
3231

33-
let resp = omaha::Response::from_str(RESPONSE_XML).context("failed to create response")?;
32+
// TODO: fine for a small example but shouldnt be encouraged
33+
let resp = omaha::Response::from_str(RESPONSE_XML).unwrap();
3434

3535
println!("{:#?}", resp);
3636
println!();
@@ -70,7 +70,8 @@ fn main() -> Result<(), Box<dyn Error>> {
7070
for url in &app.update_check.urls {
7171
println!(
7272
" {}",
73-
url.join(&pkg.name).context(format!("failed to join URL with {:?}", pkg.name))?
73+
// TODO: fine for a small example but shouldnt be encouraged
74+
url.join(&pkg.name).unwrap()
7475
);
7576
}
7677

src/bin/download_sysext.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::path::Path;
66
#[macro_use]
77
extern crate log;
88

9-
use anyhow::Result;
109
use argh::FromArgs;
1110
use globset::{Glob, GlobSet, GlobSetBuilder};
1211

src/download/mod.rs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ use std::path::Path;
99
use std::str::FromStr;
1010
use std::time::Duration;
1111

12-
use anyhow::{Context, Result, anyhow, bail};
1312
use globset::GlobSet;
1413
use hard_xml::XmlRead;
1514
use log::{debug, info, warn};
16-
use reqwest::{StatusCode, blocking::Client, redirect::Policy};
15+
use reqwest::{blocking::Client, redirect::Policy};
1716
use url::Url;
1817

1918
use crate::{Package, PackageStatus};
2019
use omaha::{Sha1Digest, Sha256Digest};
20+
use crate::error::Error;
21+
use crate::Result;
2122

2223
const DOWNLOAD_TIMEOUT: u64 = 3600;
2324
const HTTP_CONN_TIMEOUT: u64 = 20;
@@ -33,9 +34,9 @@ pub struct DownloadResult {
3334
}
3435

3536
pub fn hash_on_disk<T: omaha::Hasher>(path: &Path, maxlen: Option<usize>) -> Result<T::Output> {
36-
let file = File::open(path).context(format!("File::open({:?})", path))?;
37+
let file = File::open(path).map_err(Error::OpenFile)?;
3738

38-
let filelen = file.metadata().context(format!("failed to get metadata of {:?}", path))?.len() as usize;
39+
let filelen = file.metadata().map_err(Error::GetFileMetadata)?.len() as usize;
3940

4041
let mut maxlen_to_read: usize = match maxlen {
4142
Some(len) => {
@@ -61,7 +62,7 @@ pub fn hash_on_disk<T: omaha::Hasher>(path: &Path, maxlen: Option<usize>) -> Res
6162
databuf.truncate(maxlen_to_read);
6263
}
6364

64-
freader.read_exact(&mut databuf).context(format!("failed to read_exact(chunklen {:?})", databuf.len()))?;
65+
freader.read_exact(&mut databuf).map_err(Error::ReadFromFile)?;
6566

6667
maxlen_to_read -= databuf.len();
6768

@@ -78,33 +79,23 @@ where
7879
{
7980
let client_url = url.clone();
8081

81-
#[rustfmt::skip]
82-
let mut res = client.get(url.clone())
83-
.send()
84-
.context(format!("client get & send{:?} failed ", client_url.as_str()))?;
82+
let mut res = client.get(url.clone()).send().map_err(|err| Error::SendGetRequest(url.into(), err))?;
8583

8684
// Redirect was already handled at this point, so there is no need to touch
8785
// response or url again. Simply print info and continue.
8886
if <U as Into<Url>>::into(client_url) != *res.url() {
8987
info!("redirected to URL {:?}", res.url());
9088
}
9189

92-
// Return immediately on download failure on the client side.
93-
let status = res.status();
94-
95-
if !status.is_success() {
96-
match status {
97-
StatusCode::FORBIDDEN | StatusCode::NOT_FOUND => {
98-
bail!("cannnot fetch remotely with status code {:?}", status);
99-
}
100-
_ => bail!("general failure with status code {:?}", status),
101-
}
90+
match res.status() {
91+
status if !status.is_success() => return Err(Error::GetRequestFailed(status)),
92+
_ => {}
10293
}
10394

10495
println!("writing to {}", path.display());
10596

106-
let mut file = File::create(path).context(format!("failed to create path ({:?})", path.display()))?;
107-
res.copy_to(&mut file)?;
97+
let mut file = File::create(path).map_err(Error::CreateFile)?;
98+
res.copy_to(&mut file).map_err(Error::CopyRequestBodyToFile)?;
10899

109100
let calculated_sha256 = hash_on_disk::<omaha::Sha256>(path, None)?;
110101
let calculated_sha1 = hash_on_disk::<omaha::Sha1>(path, None)?;
@@ -116,11 +107,14 @@ where
116107
debug!(" calculated sha1: {calculated_sha1:?}");
117108
debug!(" sha1 match? {}", expected_sha1 == Some(calculated_sha1));
118109

119-
if expected_sha256.is_some() && expected_sha256 != Some(calculated_sha256) {
120-
bail!("checksum mismatch for sha256");
110+
match expected_sha256 {
111+
Some(exp) if exp != calculated_sha256 => return Err(Error::Sha256ChecksumMismatch(exp, calculated_sha256)),
112+
_ => {}
121113
}
122-
if expected_sha1.is_some() && expected_sha1 != Some(calculated_sha1) {
123-
bail!("checksum mismatch for sha1");
114+
115+
match expected_sha1 {
116+
Some(exp) if exp != calculated_sha1 => return Err(Error::Sha1ChecksumMismatch(exp, calculated_sha1)),
117+
_ => {}
124118
}
125119

126120
Ok(DownloadResult {
@@ -189,13 +183,13 @@ where
189183
U: reqwest::IntoUrl + From<U> + std::clone::Clone + std::fmt::Debug,
190184
Url: From<U>,
191185
{
192-
let r = download_and_hash(client, input_url.clone(), path, None, None).context(format!("unable to download data(url {input_url:?})"))?;
186+
let r = download_and_hash(client, input_url.clone(), path, None, None)?;
193187

194188
Ok(Package {
195189
name: Cow::Borrowed(path.file_name().unwrap_or(OsStr::new("fakepackage")).to_str().unwrap_or("fakepackage")),
196190
hash_sha256: Some(r.hash_sha256),
197191
hash_sha1: Some(r.hash_sha1),
198-
size: r.data.metadata().context(format!("failed to get metadata, path ({:?})", path.display()))?.len() as usize,
192+
size: r.data.metadata().map_err(Error::GetFileMetadata)?.len() as usize,
199193
url: input_url.into(),
200194
status: PackageStatus::Unverified,
201195
})
@@ -204,19 +198,19 @@ where
204198
fn do_download_verify(pkg: &mut Package<'_>, output_filename: Option<String>, output_dir: &Path, unverified_dir: &Path, pubkey_file: &str, client: &Client) -> Result<()> {
205199
pkg.check_download(unverified_dir)?;
206200

207-
pkg.download(unverified_dir, client).context(format!("unable to download \"{:?}\"", pkg.name))?;
201+
pkg.download(unverified_dir, client)?;
208202

209203
// Unverified payload is stored in e.g. "output_dir/.unverified/oem.gz".
210204
// Verified payload is stored in e.g. "output_dir/oem.raw".
211205
let pkg_unverified = unverified_dir.join(&*pkg.name);
212206
let mut pkg_verified = output_dir.join(output_filename.as_ref().map(OsStr::new).unwrap_or(pkg_unverified.with_extension("raw").file_name().unwrap_or_default()));
213207
pkg_verified.set_extension("raw");
214208

215-
let datablobspath = pkg.verify_signature_on_disk(&pkg_unverified, pubkey_file).context(format!("unable to verify signature \"{}\"", pkg.name))?;
209+
let datablobspath = pkg.verify_signature_on_disk(&pkg_unverified, pubkey_file)?;
216210

217211
// write extracted data into the final data.
218212
debug!("data blobs written into file {pkg_verified:?}");
219-
fs::rename(datablobspath, pkg_verified)?;
213+
fs::rename(datablobspath, pkg_verified).map_err(Error::RenameFile)?;
220214

221215
Ok(())
222216
}
@@ -264,27 +258,28 @@ impl DownloadVerify {
264258

265259
let unverified_dir = output_dir.join(UNVERFIED_SUFFIX);
266260
let temp_dir = output_dir.join(TMP_SUFFIX);
267-
fs::create_dir_all(&unverified_dir)?;
268-
fs::create_dir_all(&temp_dir)?;
261+
fs::create_dir_all(&unverified_dir).map_err(Error::CreateDirectory)?;
262+
fs::create_dir_all(&temp_dir).map_err(Error::CreateDirectory)?;
269263

270264
// The default policy of reqwest Client supports max 10 attempts on HTTP redirect.
271265
let client = Client::builder()
272266
.tcp_keepalive(Duration::from_secs(HTTP_CONN_TIMEOUT))
273267
.connect_timeout(Duration::from_secs(HTTP_CONN_TIMEOUT))
274268
.timeout(Duration::from_secs(DOWNLOAD_TIMEOUT))
275269
.redirect(Policy::default())
276-
.build()?;
270+
.build()
271+
.map_err(Error::BuildClient)?;
277272

278273
if self.payload_url.is_some() {
279274
let url = self.payload_url.clone().unwrap();
280-
let u = Url::parse(&url)?;
281-
let fname = u.path_segments().ok_or(anyhow!("failed to get path segments, url ({:?})", u))?.next_back().ok_or(anyhow!("failed to get path segments, url ({:?})", u))?;
275+
let u = Url::parse(&url).map_err(Error::ParseUrl)?;
276+
let fname = u.path_segments().ok_or(Error::InvalidBaseUrl(u.clone()))?.next_back().ok_or(Error::EmptyUrlIterator)?;
282277
let mut pkg_fake: Package;
283278

284279
let temp_payload_path = unverified_dir.join(fname);
285280
pkg_fake = fetch_url_to_file(
286281
&temp_payload_path,
287-
Url::from_str(url.as_str()).context(anyhow!("failed to convert into url ({:?})", self.payload_url))?,
282+
Url::from_str(url.as_str()).map_err(Error::ParseUrl)?,
288283
&client,
289284
)?;
290285
do_download_verify(
@@ -303,7 +298,7 @@ impl DownloadVerify {
303298
////
304299
// parse response
305300
////
306-
let resp = omaha::Response::from_str(&self.input_xml)?;
301+
let resp = omaha::Response::from_str(&self.input_xml).map_err(Error::InvalidHashDigestString)?;
307302

308303
let mut pkgs_to_dl = get_pkgs_to_download(&resp, &self.glob_set)?;
309304

@@ -329,7 +324,7 @@ impl DownloadVerify {
329324
}
330325

331326
// clean up data
332-
fs::remove_dir_all(temp_dir)?;
327+
fs::remove_dir_all(temp_dir).map_err(Error::RemoveDirectory)?;
333328

334329
Ok(())
335330
}

0 commit comments

Comments
 (0)