From 66d66a537c2bd206399e711b9221d3d36a589cdc Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Sun, 6 Oct 2024 13:08:17 +0200 Subject: [PATCH] Switch from failure to anyhow. --- Cargo.lock | 71 +++++------------------ Cargo.toml | 2 +- src/bin/installer_tui.rs | 118 +++++++++++++-------------------------- src/config/mod.rs | 11 ++-- src/lib.rs | 27 +++------ 5 files changed, 66 insertions(+), 163 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5ff74dbcb2..e22280c45b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -68,6 +68,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "anyhow" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" + [[package]] name = "arg_parser" version = "0.1.0" @@ -502,28 +508,6 @@ dependencies = [ "version_check 0.9.4", ] -[[package]] -name = "failure" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" -dependencies = [ - "backtrace", - "failure_derive", -] - -[[package]] -name = "failure_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", - "synstructure", -] - [[package]] name = "fatfs" version = "0.3.6" @@ -1165,9 +1149,9 @@ dependencies = [ name = "redox_installer" version = "0.2.24" dependencies = [ + "anyhow", "arg_parser", "cc", - "failure", "fatfs", "fscommon", "gpt", @@ -1443,7 +1427,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn", ] [[package]] @@ -1511,17 +1495,6 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.68" @@ -1533,18 +1506,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "synstructure" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", - "unicode-xid", -] - [[package]] name = "tar" version = "0.4.41" @@ -1627,7 +1588,7 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn", ] [[package]] @@ -1753,12 +1714,6 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" -[[package]] -name = "unicode-xid" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" - [[package]] name = "untrusted" version = "0.6.2" @@ -1901,7 +1856,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.68", + "syn", "wasm-bindgen-shared", ] @@ -1923,7 +1878,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2171,5 +2126,5 @@ checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn", ] diff --git a/Cargo.toml b/Cargo.toml index b0ad144a59..5b0fa219d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,9 +25,9 @@ name = "redox_installer" path = "src/lib.rs" [dependencies] +anyhow = "1.0.89" arg_parser = "0.1.0" cc = "=1.0.99" # Hack for ring 0.13.5 not building -failure = "0.1.8" fatfs = "0.3.0" fscommon = "0.1.1" gpt = "3.0.0" diff --git a/src/bin/installer_tui.rs b/src/bin/installer_tui.rs index 4976003867..9df1453981 100644 --- a/src/bin/installer_tui.rs +++ b/src/bin/installer_tui.rs @@ -1,14 +1,4 @@ -extern crate arg_parser; -#[macro_use] -extern crate failure; -extern crate pkgar; -extern crate pkgar_core; -extern crate pkgar_keys; -extern crate redox_installer; -extern crate serde; -extern crate termion; -extern crate toml; - +use anyhow::{anyhow, bail, Result}; use pkgar::{ext::EntryExt, PackageHead}; use pkgar_core::PackageSrc; use pkgar_keys::PublicKeyFile; @@ -105,18 +95,14 @@ fn format_size(size: u64) -> String { } } -fn copy_file(src: &Path, dest: &Path, buf: &mut [u8]) -> Result<(), failure::Error> { +fn copy_file(src: &Path, dest: &Path, buf: &mut [u8]) -> Result<()> { if let Some(parent) = dest.parent() { // Parent may be a symlink if !parent.is_symlink() { match fs::create_dir_all(&parent) { Ok(()) => (), Err(err) => { - return Err(format_err!( - "failed to create directory {}: {}", - parent.display(), - err - )); + bail!("failed to create directory {}: {}", parent.display(), err); } } } @@ -125,11 +111,7 @@ fn copy_file(src: &Path, dest: &Path, buf: &mut [u8]) -> Result<(), failure::Err let metadata = match fs::symlink_metadata(&src) { Ok(ok) => ok, Err(err) => { - return Err(format_err!( - "failed to read metadata of {}: {}", - src.display(), - err - )); + bail!("failed to read metadata of {}: {}", src.display(), err); } }; @@ -137,35 +119,27 @@ fn copy_file(src: &Path, dest: &Path, buf: &mut [u8]) -> Result<(), failure::Err let real_src = match fs::read_link(&src) { Ok(ok) => ok, Err(err) => { - return Err(format_err!( - "failed to read link {}: {}", - src.display(), - err - )); + bail!("failed to read link {}: {}", src.display(), err); } }; match symlink(&real_src, &dest) { Ok(()) => (), Err(err) => { - return Err(format_err!( + bail!( "failed to copy link {} ({}) to {}: {}", src.display(), real_src.display(), dest.display(), err - )); + ); } } } else { let mut src_file = match fs::File::open(&src) { Ok(ok) => ok, Err(err) => { - return Err(format_err!( - "failed to open file {}: {}", - src.display(), - err - )); + bail!("failed to open file {}: {}", src.display(), err); } }; @@ -177,11 +151,7 @@ fn copy_file(src: &Path, dest: &Path, buf: &mut [u8]) -> Result<(), failure::Err { Ok(ok) => ok, Err(err) => { - return Err(format_err!( - "failed to create file {}: {}", - dest.display(), - err - )); + bail!("failed to create file {}: {}", dest.display(), err); } }; @@ -189,11 +159,7 @@ fn copy_file(src: &Path, dest: &Path, buf: &mut [u8]) -> Result<(), failure::Err let count = match src_file.read(buf) { Ok(ok) => ok, Err(err) => { - return Err(format_err!( - "failed to read file {}: {}", - src.display(), - err - )); + bail!("failed to read file {}: {}", src.display(), err); } }; @@ -204,11 +170,7 @@ fn copy_file(src: &Path, dest: &Path, buf: &mut [u8]) -> Result<(), failure::Err match dest_file.write_all(&buf[..count]) { Ok(()) => (), Err(err) => { - return Err(format_err!( - "failed to write file {}: {}", - dest.display(), - err - )); + bail!("failed to write file {}: {}", dest.display(), err); } } } @@ -354,45 +316,41 @@ fn main() { password_opt: password_opt.as_ref().map(|x| x.as_bytes()), efi_partition_size: None, }; - let res = with_whole_disk( - &disk_path, - &disk_option, - |mount_path| -> Result<(), failure::Error> { - let mut config: Config = Config::from_file(&root_path.join("filesystem.toml"))?; + let res = with_whole_disk(&disk_path, &disk_option, |mount_path| -> Result<()> { + let mut config: Config = Config::from_file(&root_path.join("filesystem.toml"))?; - // Copy filesystem.toml, which is not packaged - let mut files = vec!["filesystem.toml".to_string()]; + // Copy filesystem.toml, which is not packaged + let mut files = vec!["filesystem.toml".to_string()]; - // Copy files from locally installed packages - if let Err(err) = package_files(&root_path, &mut config, &mut files) { - return Err(format_err!("failed to read package files: {}", err)); - } + // Copy files from locally installed packages + package_files(&root_path, &mut config, &mut files) + // TODO: implement Error trait + .map_err(|err| anyhow!("failed to read package files: {err}"))?; - // Perform config install (after packages have been converted to files) - eprintln!("configuring system"); - let cookbook: Option<&'static str> = None; - redox_installer::install_dir(config, mount_path, cookbook) - .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; + // Perform config install (after packages have been converted to files) + eprintln!("configuring system"); + let cookbook: Option<&'static str> = None; + redox_installer::install_dir(config, mount_path, cookbook) + .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; - // Sort and remove duplicates - files.sort(); - files.dedup(); + // Sort and remove duplicates + files.sort(); + files.dedup(); - // Install files - let mut buf = vec![0; 4 * MIB as usize]; - for (i, name) in files.iter().enumerate() { - eprintln!("copy {} [{}/{}]", name, i, files.len()); + // Install files + let mut buf = vec![0; 4 * MIB as usize]; + for (i, name) in files.iter().enumerate() { + eprintln!("copy {} [{}/{}]", name, i, files.len()); - let src = root_path.join(name); - let dest = mount_path.join(name); - copy_file(&src, &dest, &mut buf)?; - } + let src = root_path.join(name); + let dest = mount_path.join(name); + copy_file(&src, &dest, &mut buf)?; + } - eprintln!("finished installing, unmounting filesystem"); + eprintln!("finished installing, unmounting filesystem"); - Ok(()) - }, - ); + Ok(()) + }); match res { Ok(()) => { diff --git a/src/config/mod.rs b/src/config/mod.rs index 5f11eff2c5..577a7b0d0d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -3,6 +3,9 @@ use std::fs; use std::mem; use std::path::{Path, PathBuf}; +use anyhow::bail; +use anyhow::Result; + pub mod file; pub mod general; pub mod package; @@ -25,16 +28,16 @@ pub struct Config { } impl Config { - pub fn from_file(path: &Path) -> Result { + pub fn from_file(path: &Path) -> Result { let mut config: Config = match fs::read_to_string(&path) { Ok(config_data) => match toml::from_str(&config_data) { Ok(config) => config, Err(err) => { - return Err(format_err!("{}: failed to decode: {}", path.display(), err)); + bail!("{}: failed to decode: {}", path.display(), err); } }, Err(err) => { - return Err(format_err!("{}: failed to read: {}", path.display(), err)); + bail!("{}: failed to read: {}", path.display(), err); } }; @@ -43,7 +46,7 @@ impl Config { let mut configs = mem::take(&mut config.include) .into_iter() .map(|path| Config::from_file(&config_dir.join(path))) - .collect::, failure::Error>>()?; + .collect::>>()?; configs.push(config); // Put ourself last to ensure that it overwrites anything else. config = configs.remove(0); diff --git a/src/lib.rs b/src/lib.rs index 161c750431..4059746f41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,5 @@ #[macro_use] extern crate serde_derive; -extern crate argon2; -extern crate libc; -extern crate liner; -#[macro_use] -extern crate failure; -extern crate pkgutils; -extern crate rand; -extern crate redoxfs; -extern crate syscall; -extern crate termion; mod config; mod disk_wrapper; @@ -19,7 +9,7 @@ pub use crate::config::package::PackageConfig; pub use crate::config::Config; use crate::disk_wrapper::DiskWrapper; -use failure::{err_msg, Error}; +use anyhow::{bail, Result}; use pkgutils::{Package, Repo}; use rand::{rngs::OsRng, RngCore}; use redoxfs::{unmount_path, Disk, DiskIo, FileSystem}; @@ -36,8 +26,6 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; -pub(crate) type Result = std::result::Result; - pub struct DiskOption<'a> { pub bootloader_bios: &'a [u8], pub bootloader_efi: &'a [u8], @@ -62,7 +50,7 @@ fn hash_password(password: &str) -> Result { let hash = argon2::hash_encoded(password.as_bytes(), salt.as_bytes(), &config)?; Ok(hash) } else { - Ok("".to_string()) + Ok("".into()) } } @@ -84,11 +72,10 @@ fn prompt_password(prompt: &str, confirm_prompt: &str) -> Result { let confirm_password = stdin.read_passwd(&mut stdout)?; // Note: Actually comparing two Option values - if confirm_password == password { - Ok(password.unwrap_or("".to_string())) - } else { - Err(err_msg("passwords do not match")) + if confirm_password != password { + bail!("passwords do not match"); } + Ok(password.unwrap_or("".to_string())) } //TODO: error handling @@ -526,7 +513,7 @@ where "i686-unknown-redox" => "BOOTIA32.EFI", "x86_64-unknown-redox" => "BOOTX64.EFI", _ => { - return Err(format_err!("target '{}' not supported", target)); + bail!("target '{target}' not supported"); } }; @@ -539,7 +526,7 @@ where 512 => gpt::disk::LogicalBlockSize::Lb512, _ => { // TODO: support (and test) other block sizes - return Err(format_err!("block size {} not supported", block_size)); + bail!("block size {block_size} not supported"); } };