Skip to content

Commit

Permalink
Merge pull request #137 from chenxiaolong/anyhow
Browse files Browse the repository at this point in the history
Remove unnecessary use of anyhow macro
  • Loading branch information
chenxiaolong authored Sep 7, 2023
2 parents ddabb19 + 0d2a158 commit e40e036
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 115 deletions.
22 changes: 11 additions & 11 deletions avbroot/src/cli/avb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ pub fn verify_headers(

let path = directory.join(format!("{name}.img"));
let raw_reader =
File::open(&path).with_context(|| anyhow!("Failed to open for reading: {path:?}"))?;
File::open(&path).with_context(|| format!("Failed to open for reading: {path:?}"))?;
let (header, _, _) = avb::load_image(BufReader::new(raw_reader))
.with_context(|| anyhow!("Failed to load vbmeta structures: {path:?}"))?;
.with_context(|| format!("Failed to load vbmeta structures: {path:?}"))?;

// Verify the header's signature.
let public_key = header
.verify()
.with_context(|| anyhow!("Failed to verify header signature: {path:?}"))?;
.with_context(|| format!("Failed to verify header signature: {path:?}"))?;

if let Some(k) = &public_key {
let prefix = format!("{name} has a signed vbmeta header");
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn verify_headers(
}
avb::Descriptor::ChainPartition(d) => {
let target_key = avb::decode_public_key(&d.public_key).with_context(|| {
anyhow!("Failed to decode chained public key for: {target_name}")
format!("Failed to decode chained public key for: {target_name}")
})?;

verify_headers(directory, target_name, Some(&target_key), seen, descriptors)?;
Expand Down Expand Up @@ -134,12 +134,12 @@ pub fn verify_descriptors(
|| Ok(Box::new(BufReader::new(reader.clone()))),
cancel_signal,
)
.with_context(|| anyhow!("Failed to verify hashtree descriptor for: {name}"))?;
.with_context(|| format!("Failed to verify hashtree descriptor for: {name}"))?;
}
Descriptor::Hash(d) => {
status!("Verifying hash descriptor for: {name}");
d.verify(BufReader::new(reader), cancel_signal)
.with_context(|| anyhow!("Failed to verify hash descriptor for: {name}"))?;
.with_context(|| format!("Failed to verify hash descriptor for: {name}"))?;
}
_ => unreachable!("Non-verifiable descriptor: {descriptor:?}"),
}
Expand All @@ -153,20 +153,20 @@ pub fn avb_main(cli: &AvbCli, cancel_signal: &Arc<AtomicBool>) -> Result<()> {
match &cli.command {
AvbCommand::Dump(c) => {
let raw_reader = File::open(&c.input)
.with_context(|| anyhow!("Failed to open for reading: {:?}", c.input))?;
.with_context(|| format!("Failed to open for reading: {:?}", c.input))?;
let reader = BufReader::new(raw_reader);
let (header, footer, image_size) = avb::load_image(reader)
.with_context(|| anyhow!("Failed to load vbmeta structures: {:?}", c.input))?;
.with_context(|| format!("Failed to load vbmeta structures: {:?}", c.input))?;

println!("Image size: {image_size}");
println!("Header: {header:#?}");
println!("Footer: {footer:#?}");
}
AvbCommand::Verify(c) => {
let public_key = if let Some(p) = &c.public_key {
let data = fs::read(p).with_context(|| anyhow!("Failed to read file: {p:?}"))?;
let data = fs::read(p).with_context(|| format!("Failed to read file: {p:?}"))?;
let key = avb::decode_public_key(&data)
.with_context(|| anyhow!("Failed to decode public key: {p:?}"))?;
.with_context(|| format!("Failed to decode public key: {p:?}"))?;

Some(key)
} else {
Expand All @@ -177,7 +177,7 @@ pub fn avb_main(cli: &AvbCli, cancel_signal: &Arc<AtomicBool>) -> Result<()> {
let name = c
.input
.file_stem()
.with_context(|| anyhow!("Path is not a file: {:?}", c.input))?
.with_context(|| format!("Path is not a file: {:?}", c.input))?
.to_str()
.ok_or_else(|| anyhow!("Invalid UTF-8: {:?}", c.input))?;

Expand Down
14 changes: 7 additions & 7 deletions avbroot/src/cli/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
path::{Path, PathBuf},
};

use anyhow::{anyhow, bail, Context, Result};
use anyhow::{bail, Context, Result};
use clap::{Parser, Subcommand};

use crate::{
Expand Down Expand Up @@ -82,7 +82,7 @@ fn read_avb_header_if_exists(path: &Path) -> Result<Option<Header>> {
Err(e) => Err(e).with_context(|| format!("Failed to open for reading: {path:?}"))?,
};
let header = Header::from_reader(BufReader::new(file))
.with_context(|| anyhow!("Failed to read vbmeta header: {path:?}"))?;
.with_context(|| format!("Failed to read vbmeta header: {path:?}"))?;

Ok(Some(header))
}
Expand All @@ -106,7 +106,7 @@ fn write_text_if_not_empty(path: &Path, text: &str) -> Result<()> {

fn write_avb_header(path: &Path, header: &Header) -> Result<()> {
let file =
File::create(path).with_context(|| anyhow!("Failed to open for writing: {path:?}"))?;
File::create(path).with_context(|| format!("Failed to open for writing: {path:?}"))?;
header.to_writer(BufWriter::new(file))?;

Ok(())
Expand Down Expand Up @@ -272,9 +272,9 @@ fn info_subcommand(boot_cli: &BootCli, cli: &InfoCli) -> Result<()> {

pub fn magisk_info_subcommand(cli: &MagiskInfoCli) -> Result<()> {
let raw_reader = File::open(&cli.image)
.with_context(|| anyhow!("Failed to open for reading: {:?}", cli.image))?;
.with_context(|| format!("Failed to open for reading: {:?}", cli.image))?;
let boot_image = BootImage::from_reader(BufReader::new(raw_reader))
.with_context(|| anyhow!("Failed to load boot image: {:?}", cli.image))?;
.with_context(|| format!("Failed to load boot image: {:?}", cli.image))?;

let mut ramdisks = vec![];

Expand All @@ -297,9 +297,9 @@ pub fn magisk_info_subcommand(cli: &MagiskInfoCli) -> Result<()> {
for (i, ramdisk) in ramdisks.iter().enumerate() {
let reader = Cursor::new(ramdisk);
let reader = CompressedReader::new(reader, true)
.with_context(|| anyhow!("Failed to load ramdisk #{i}"))?;
.with_context(|| format!("Failed to load ramdisk #{i}"))?;
let entries = cpio::load(reader, false)
.with_context(|| anyhow!("Failed to load ramdisk #{i} cpio"))?;
.with_context(|| format!("Failed to load ramdisk #{i} cpio"))?;

if let Some(e) = entries.iter().find(|e| e.name == b".backup/.magisk") {
io::stdout().write_all(&e.content)?;
Expand Down
16 changes: 8 additions & 8 deletions avbroot/src/cli/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
time::Duration,
};

use anyhow::{anyhow, Context, Result};
use anyhow::{Context, Result};
use clap::{Args, Parser, Subcommand};

use crate::{
Expand All @@ -36,41 +36,41 @@ pub fn key_main(cli: &KeyCli) -> Result<()> {
crypto::generate_rsa_key_pair().context("Failed to generate RSA keypair")?;

crypto::write_pem_key_file(&c.output, &private_key, &passphrase)
.with_context(|| anyhow!("Failed to write private key: {:?}", c.output))?;
.with_context(|| format!("Failed to write private key: {:?}", c.output))?;
}
KeyCommand::GenerateCert(c) => {
let passphrase = get_passphrase(&c.passphrase, &c.key);
let private_key = crypto::read_pem_key_file(&c.key, &passphrase)
.with_context(|| anyhow!("Failed to load key: {:?}", c.key))?;
.with_context(|| format!("Failed to load key: {:?}", c.key))?;

let validity = Duration::from_secs(c.validity * 24 * 60 * 60);
let cert = crypto::generate_cert(&private_key, rand::random(), validity, &c.subject)
.context("Failed to generate certificate")?;

crypto::write_pem_cert_file(&c.output, &cert)
.with_context(|| anyhow!("Failed to write certificate: {:?}", c.output))?;
.with_context(|| format!("Failed to write certificate: {:?}", c.output))?;
}
KeyCommand::ExtractAvb(c) => {
let public_key = if let Some(p) = &c.input.key {
let passphrase = get_passphrase(&c.passphrase, p);
let private_key = crypto::read_pem_key_file(p, &passphrase)
.with_context(|| anyhow!("Failed to load key: {p:?}"))?;
.with_context(|| format!("Failed to load key: {p:?}"))?;

private_key.to_public_key()
} else if let Some(p) = &c.input.cert {
let certificate = crypto::read_pem_cert_file(p)
.with_context(|| anyhow!("Failed to load certificate: {p:?}"))?;
.with_context(|| format!("Failed to load certificate: {p:?}"))?;

crypto::get_public_key(&certificate)?
} else {
unreachable!()
};

let encoded = avb::encode_public_key(&public_key)
.with_context(|| anyhow!("Failed to encode public key in AVB format"))?;
.context("Failed to encode public key in AVB format")?;

fs::write(&c.output, encoded)
.with_context(|| anyhow!("Failed to write public key: {:?}", c.output))?;
.with_context(|| format!("Failed to write public key: {:?}", c.output))?;
}
}

Expand Down
Loading

0 comments on commit e40e036

Please sign in to comment.