diff --git a/usbsas-tools/src/fswriter.rs b/usbsas-tools/src/fswriter.rs index 92943b4..d003974 100644 --- a/usbsas-tools/src/fswriter.rs +++ b/usbsas-tools/src/fswriter.rs @@ -18,13 +18,17 @@ enum Error { #[error("io error: {0}")] IO(#[from] std::io::Error), #[error("{0}")] - Error(String), + Arg(String), + #[error("{0}")] + Write(String), #[error("sandbox: {0}")] Sandbox(#[from] usbsas_sandbox::Error), #[error("process: {0}")] Process(#[from] usbsas_process::Error), #[error("Bad Request")] BadRequest, + #[error("progress error: {0}")] + Progress(#[from] indicatif::style::TemplateError), } type Result = std::result::Result; @@ -75,7 +79,7 @@ impl FsWriter { let fs_size = self.fs.seek(SeekFrom::End(0))?; self.fs.rewind()?; if fs_size % SECTOR_SIZE != 0 { - return Err(Error::Error(format!( + return Err(Error::Write(format!( "fs size ({fs_size}) % sector size ({SECTOR_SIZE}) != 0" ))); } @@ -85,7 +89,7 @@ impl FsWriter { .size(proto::fs2dev::RequestDevSize {})? .size; if fs_size > dev_size { - return Err(Error::Error(format!( + return Err(Error::Write(format!( "filesystem size ({fs_size}) > device size ({dev_size}), aborting" ))); } @@ -113,8 +117,7 @@ impl FsWriter { let pb = indicatif::ProgressBar::new(fs_size); pb.set_style( indicatif::ProgressStyle::default_bar() - .template("[{wide_bar}] {bytes}/{total_bytes} ({eta})") - .map_err(|err| Error::Error(format!("progress bar err: {err}")))? + .template("[{wide_bar}] {bytes}/{total_bytes} ({eta})")? .progress_chars("#>-"), ); @@ -128,8 +131,8 @@ impl FsWriter { pb.set_position(fs_size); break; } - Msg::Error(msg) => return Err(Error::Error(msg.err)), - _ => return Err(Error::Error("an error occured".to_string())), + Msg::Error(msg) => return Err(Error::Write(msg.err)), + _ => return Err(Error::Write("bad resp from fs2dev".to_string())), } } @@ -196,7 +199,7 @@ fn main() -> Result<()> { matches.get_one::("devnum"), ) { (Some(fs), Some(bn), Some(dn)) => (fs, bn, dn), - _ => return Err(Error::Error("missing arg".to_string())), + _ => return Err(Error::Arg("missing arg".to_string())), }; let mut fswriter = FsWriter::new(fs_path.to_owned(), busnum.to_owned(), devnum.to_owned())?; diff --git a/usbsas-tools/src/imager.rs b/usbsas-tools/src/imager.rs index 389d64f..14efd3c 100644 --- a/usbsas-tools/src/imager.rs +++ b/usbsas-tools/src/imager.rs @@ -21,13 +21,17 @@ enum Error { #[error("int error: {0}")] Tryfromint(#[from] std::num::TryFromIntError), #[error("{0}")] - Error(String), + Arg(String), + #[error("{0}")] + OpenDevice(String), #[error("persist error: {0}")] Persist(#[from] tempfile::PersistError), #[error("sandbox: {0}")] Sandbox(#[from] usbsas_sandbox::Error), #[error("process: {0}")] Process(#[from] usbsas_process::Error), + #[error("progress error: {0}")] + Progress(#[from] indicatif::style::TemplateError), } type Result = std::result::Result; @@ -101,7 +105,7 @@ impl Imager { if let Some(proto::scsi::response::Msg::OpenDevice(rep)) = rep.msg { (rep.dev_size, rep.block_size) } else { - return Err(Error::Error("Couldn't open device".to_string())); + return Err(Error::OpenDevice("Couldn't open device".to_string())); }; let mut todo = dev_size; @@ -112,8 +116,8 @@ impl Imager { let pb = indicatif::ProgressBar::new(dev_size); pb.set_style( indicatif::ProgressStyle::default_bar() - .template("[{wide_bar}] {bytes}/{total_bytes} ({eta})") - .map_err(|err| Error::Error(format!("progress bar err: {err}")))? + .template("[{wide_bar}] {bytes}/{total_bytes} ({eta})")? + // .map_err(|err| Error::Progress(format!("progress bar err: {err}")))? .progress_chars("#>-"), ); @@ -250,7 +254,7 @@ fn main() -> Result<()> { ) { (Some(busnum), Some(devnum)) => (busnum.to_owned(), devnum.to_owned()), _ => { - return Err(Error::Error( + return Err(Error::Arg( "Must specify both busnum and devnum".to_string(), )); } diff --git a/usbsas-tools/src/net.rs b/usbsas-tools/src/net.rs index f5250f7..255bc8a 100644 --- a/usbsas-tools/src/net.rs +++ b/usbsas-tools/src/net.rs @@ -26,10 +26,10 @@ enum Error { Upload(String), #[error("analyze error: {0}")] Analyze(String), - #[error("Error: {0}")] - Error(String), #[error("download error: {0}")] Download(String), + #[error("configuration or argument error: {0}")] + ArgConf(String), } type Result = std::result::Result; @@ -66,7 +66,7 @@ fn upload(config_path: &str, bundle_path: &str, id: &str) -> Result<()> { let networks = &config .networks - .ok_or_else(|| Error::Error("No networks in conf".into()))?; + .ok_or_else(|| Error::ArgConf("No networks".into()))?; let network = if networks.len() == 1 { &networks[0] @@ -288,12 +288,12 @@ fn main() -> Result<()> { "analyze" => analyze(config_path, path, id)?, "download" => download(config_path, path, id)?, _ => { - return Err(Error::Error( + return Err(Error::ArgConf( "Bad action specified, either: upload, analyze or download".to_owned(), )) } }, - _ => return Err(Error::Error("args parse failed".to_owned())), + _ => return Err(Error::ArgConf("args parse failed".to_owned())), } Ok(()) diff --git a/usbsas-usbsas/src/main.rs b/usbsas-usbsas/src/main.rs index cf0c016..0a2af95 100644 --- a/usbsas-usbsas/src/main.rs +++ b/usbsas-usbsas/src/main.rs @@ -28,8 +28,6 @@ use usbsas_utils::{self, clap::UsbsasClap, READ_FILE_MAX_SIZE, TAR_DATA_DIR}; enum Error { #[error("io error: {0}")] IO(#[from] std::io::Error), - #[error("{0}")] - Error(String), #[error("analyze error: {0}")] Analyze(String), #[error("download error: {0}")] @@ -44,8 +42,16 @@ enum Error { Process(#[from] usbsas_process::Error), #[error("Not enough space on destination device")] NotEnoughSpace, + #[error("Error filtering files (bad count)")] + Filter, + #[error("File too large")] + FileTooLarge, + #[error("{0}")] + Wipe(String), + #[error("{0}")] + WriteFs(String), #[error("serde_json: {0}")] - JsonError(#[from] serde_json::Error), + Json(#[from] serde_json::Error), #[error("Bad Request")] BadRequest, #[error("State error")] @@ -820,7 +826,7 @@ impl CopyFilesState { path: files.to_vec(), })?; if rep.results.len() != files_count { - return Err(Error::Error("filter error".to_string())); + return Err(Error::Filter); } for (i, f) in files.iter().enumerate().take(files_count) { if rep.results[i] == proto::filter::FilterResult::PathOk as i32 { @@ -876,7 +882,7 @@ impl CopyFilesState { "File '{}' is larger ({}B) than max size ({}B)", &path, attrs.size, max_size ); - return Err(Error::Error("file too large".into())); + return Err(Error::FileTooLarge); } } @@ -1211,7 +1217,7 @@ impl AnalyzeState { Msg::Analyze(res) => { let report_json: serde_json::Value = serde_json::from_str(&res.report)?; log::trace!("analyzer report: {:?}", report_json); - let files_status = report_json["files"].as_object().ok_or(Error::Error( + let files_status = report_json["files"].as_object().ok_or(Error::Analyze( "Couldn't get files from analyzer report".into(), ))?; @@ -1526,19 +1532,14 @@ impl WriteFsState { } else { READ_FILE_MAX_SIZE }; - let rep = match children + let rep = children .tar2files .comm .readfile(proto::files::RequestReadFile { path: path.to_string(), offset, size: size_todo, - }) { - Ok(rep) => rep, - Err(err) => { - return Err(Error::Error(format!("{err}"))); - } - }; + })?; children .files2fs .comm @@ -1615,8 +1616,8 @@ impl WriteFsState { comm.finalcopystatusdone(proto::usbsas::ResponseFinalCopyStatusDone {})?; break; } - Msg::Error(msg) => return Err(Error::Error(msg.err)), - _ => return Err(Error::Error("error writing fs".into())), + Msg::Error(msg) => return Err(Error::WriteFs(msg.err)), + _ => return Err(Error::WriteFs("error writing fs".into())), } } Ok(()) @@ -1739,8 +1740,12 @@ impl WipeState { })? } Msg::CopyStatusDone(_) => break, + Msg::Error(err) => { + log::error!("{}", err.err); + return Err(Error::Wipe(err.err)); + } _ => { - return Err(Error::Error("fs2dev err while wiping".into())); + return Err(Error::Wipe("fs2dev err while wiping".into())); } } } @@ -2013,7 +2018,7 @@ impl Children { Ok(()) } - // If destination is USB, check that device will have enough space to stores + // If destination is USB, check that device will have enough space to store // src files. // Returns max size of a single file (4GB if dest is FAT, None otherwise) fn check_dst_size( @@ -2043,10 +2048,8 @@ impl Children { error!("Aborting, dest dev too small"); return Err(Error::NotEnoughSpace); } - match OutFsType::try_from(usb.fstype) - .map_err(|err| Error::Error(format!("{err}")))? - { - OutFsType::Fat => Ok(Some(0xFFFF_FFFF)), + match OutFsType::try_from(usb.fstype) { + Ok(OutFsType::Fat) => Ok(Some(0xFFFF_FFFF)), _ => Ok(None), } }