From ed2015d69d9537cc87c74138d2789d318a62e054 Mon Sep 17 00:00:00 2001 From: Ningyuan Li Date: Wed, 14 Jun 2023 01:13:46 +0900 Subject: [PATCH] added exit code --- common/verify/src/ipk/mod.rs | 8 ++- packages/fw-extract/src/main.rs | 2 +- packages/gen-manifest/src/main.rs | 2 + packages/ipk-verify/src/main.rs | 97 +++++++++++++++++++------------ 4 files changed, 69 insertions(+), 40 deletions(-) diff --git a/common/verify/src/ipk/mod.rs b/common/verify/src/ipk/mod.rs index 40e4375..81bf719 100644 --- a/common/verify/src/ipk/mod.rs +++ b/common/verify/src/ipk/mod.rs @@ -1,7 +1,7 @@ use fw_lib::Firmware; use ipk_lib::Package; -use crate::{bin::BinVerifyResult, VerifyWithFirmware}; +use crate::{bin::BinVerifyResult, VerifyResult, VerifyWithFirmware}; pub mod component; @@ -37,3 +37,9 @@ impl VerifyWithFirmware for Package { }; } } + +impl VerifyResult for PackageVerifyResult { + fn is_good(&self) -> bool { + return self.app.is_good() && self.services.iter().all(|s| s.is_good()); + } +} diff --git a/packages/fw-extract/src/main.rs b/packages/fw-extract/src/main.rs index 17b7478..d3ff994 100644 --- a/packages/fw-extract/src/main.rs +++ b/packages/fw-extract/src/main.rs @@ -20,7 +20,7 @@ struct Args { #[arg(short = 'w', long)] rewrite: bool, #[arg(short, long, action = clap::ArgAction::Count)] - debug: u8, + verbose: u8, } struct FirmwareExtractor { diff --git a/packages/gen-manifest/src/main.rs b/packages/gen-manifest/src/main.rs index a749359..d40b31e 100644 --- a/packages/gen-manifest/src/main.rs +++ b/packages/gen-manifest/src/main.rs @@ -21,6 +21,8 @@ struct Args { link: String, #[arg(short, long, value_enum)] root: Option, + #[arg(short, long, action = clap::ArgAction::Count)] + verbose: u8, } #[derive(Debug, Clone, PartialEq, ValueEnum)] diff --git a/packages/ipk-verify/src/main.rs b/packages/ipk-verify/src/main.rs index b8435ec..87e418a 100644 --- a/packages/ipk-verify/src/main.rs +++ b/packages/ipk-verify/src/main.rs @@ -2,6 +2,7 @@ use std::fs::File; use std::io::{Error, Write}; use std::iter; use std::path::PathBuf; +use std::process::exit; use clap::{Parser, ValueEnum}; use is_terminal::IsTerminal; @@ -26,10 +27,14 @@ struct Args { output: Option, #[arg(short, long, value_enum)] format: Option, - #[arg(long)] + #[arg(short, long, required_if_eq("no_summary", "true"))] + details: bool, + #[arg(short = 'S', long)] + no_summary: bool, + #[arg(short = 'r', long)] fw_releases: Option, #[arg(short, long, action = clap::ArgAction::Count)] - debug: u8, + verbose: u8, } #[derive(Debug, Clone, PartialEq, ValueEnum)] @@ -73,7 +78,9 @@ fn main() { if firmwares.is_empty() { eprintln!("No firmware found"); } - for package in args.packages { + let mut all_good = true; + for package in &args.packages { + eprintln!("Opening package {}...", package.to_string_lossy()); let package = match Package::open(&package) { Ok(package) => package, Err(e) => { @@ -81,13 +88,11 @@ fn main() { "Failed to open {}: {e}", package.file_name().unwrap().to_string_lossy() ); + all_good = false; continue; } }; - if to_file { - eprintln!("Verifying package {}...", package.id); - } - output.h2(&format!("Package {}", package.id)).unwrap(); + eprintln!("Verifying package {}...", package.id); let results: Vec<(&Firmware, PackageVerifyResult)> = firmwares .iter() .map(|fw| { @@ -95,47 +100,63 @@ fn main() { return (fw, verify); }) .collect(); + output.h2(&format!("Package {}", package.id)).unwrap(); + + if all_good && !results.iter().all(|(_, r)| r.is_good()) { + all_good = false; + } let (_, result) = results.first().unwrap(); if to_file { eprintln!(" - App {}", result.app.id); } output.h3(&format!("App {}", result.app.id)).unwrap(); - print_component_summary( - results.iter().map(|(fw, res)| (*fw, &res.app)).collect(), - &mut output, - &format, - ) - .unwrap(); - print_component_details( - results.iter().map(|(fw, res)| (*fw, &res.app)).collect(), - &mut output, - ) - .unwrap(); - for idx in 0..result.services.len() { - if to_file { - eprintln!(" - Service {}", result.services.get(idx).unwrap().id); - } - output - .h3(&format!("Service {}", result.services.get(idx).unwrap().id)) - .unwrap(); + if !args.no_summary { print_component_summary( - results - .iter() - .map(|(fw, res)| (*fw, res.services.get(idx).unwrap())) - .collect(), + results.iter().map(|(fw, res)| (*fw, &res.app)).collect(), &mut output, &format, ) .unwrap(); + } + if args.details { print_component_details( - results - .iter() - .map(|(fw, res)| (*fw, res.services.get(idx).unwrap())) - .collect(), + results.iter().map(|(fw, res)| (*fw, &res.app)).collect(), &mut output, ) .unwrap(); } + for idx in 0..result.services.len() { + if to_file { + eprintln!(" - Service {}", result.services.get(idx).unwrap().id); + } + output + .h3(&format!("Service {}", result.services.get(idx).unwrap().id)) + .unwrap(); + if !args.no_summary { + print_component_summary( + results + .iter() + .map(|(fw, res)| (*fw, res.services.get(idx).unwrap())) + .collect(), + &mut output, + &format, + ) + .unwrap(); + } + if args.details { + print_component_details( + results + .iter() + .map(|(fw, res)| (*fw, res.services.get(idx).unwrap())) + .collect(), + &mut output, + ) + .unwrap(); + } + } + } + if !all_good { + exit(1); } } @@ -190,13 +211,13 @@ fn print_component_summary( fn print_component_details( results: Vec<(&Firmware, &ComponentVerifyResult)>, out: &mut Box, -) -> Result<(), Error> { +) -> Result { let (_, result) = *results.first().unwrap(); + out.h4(result.exe.name())?; if results.iter().all(|r| r.1.is_good()) { out.write_fmt(format_args!("All OK\n"))?; - return Ok(()); + return Ok(true); } - out.h4(result.exe.name())?; for (fw, result) in &results { if let ComponentBinVerifyResult::Failed(result) = &result.exe { out.h5(&format!("On {}", fw.info))?; @@ -208,7 +229,7 @@ fn print_component_details( if !required { continue; } - if results.iter().all(|(fw, result)| { + if results.iter().all(|(_, result)| { if let ComponentBinVerifyResult::Failed(_) = result.libs.get(index).unwrap().1 { false } else { @@ -226,7 +247,7 @@ fn print_component_details( } } } - return Ok(()); + return Ok(false); } fn print_bin_verify_details(