From 9828198dd83c8c5ec0cfad29786fac179766e470 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Fri, 3 Nov 2023 20:29:36 +0100 Subject: [PATCH 1/3] Set autodeps modes: off (default), mark-only for the considerations or autocleanup. --- src/clidef.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/clidef.rs b/src/clidef.rs index 22cb267..65dd712 100644 --- a/src/clidef.rs +++ b/src/clidef.rs @@ -54,8 +54,10 @@ pub fn cli(version: &'static str) -> Command { Arg::new("autodeps") .short('a') .long("autodeps") - .action(clap::ArgAction::SetTrue) - .help(format!("Include graph of package dependencies\n{}", "NOTE: This can increase the size, but might not always be useful".yellow())) + .default_value("none") + .value_name("mode") + .value_parser(["free", "clean", "none"]) + .help(format!("Auto-add package dependencies.\n{}", " NOTE: This can increase the size, but might not always be useful\n".yellow())) ) .arg( Arg::new("root") From 993899e5c7b245dd4baa13d33fd78b8621a03c7b Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Fri, 3 Nov 2023 20:31:08 +0100 Subject: [PATCH 2/3] Implement autodeps levels --- src/filters/resources.rs | 12 ++++++++---- src/main.rs | 2 +- src/procdata.rs | 25 +++++++++++++++++++------ src/scanner/debpkg.rs | 17 ++++++++++------- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/filters/resources.rs b/src/filters/resources.rs index 45b1e4f..92134e0 100644 --- a/src/filters/resources.rs +++ b/src/filters/resources.rs @@ -1,5 +1,5 @@ use super::{defs, intf::DataFilter}; -use crate::profile::Profile; +use crate::{procdata::Autodeps, profile::Profile}; use std::{ collections::HashSet, path::{Path, PathBuf}, @@ -7,13 +7,14 @@ use std::{ pub struct ResourcesDataFilter { data: Vec, + autodeps: Autodeps, remove_archives: bool, remove_images: bool, // not blobs (qcow2, raw etc) but images, like JPEG, PNG, XPM... } impl ResourcesDataFilter { - pub fn new(data: Vec, profile: Profile) -> Self { - let mut rdf = ResourcesDataFilter { data, remove_archives: false, remove_images: false }; + pub fn new(data: Vec, profile: Profile, autodeps: Autodeps) -> Self { + let mut rdf = ResourcesDataFilter { data, autodeps, remove_archives: false, remove_images: false }; if profile.filter_arc() { log::debug!("Removing archives"); rdf.remove_archives = true; @@ -89,7 +90,10 @@ impl DataFilter for ResourcesDataFilter { fn filter(&self, data: &mut HashSet) { let mut out: Vec = Vec::default(); for p in &self.data { - if self.filter_archives(p) || self.filter_images(p) { + if self.filter_archives(p) + || self.filter_images(p) + || (self.autodeps == Autodeps::Clean && ResourcesDataFilter::is_potential_junk(p.to_str().unwrap())) + { continue; } out.push(p.to_owned()); diff --git a/src/main.rs b/src/main.rs index 04b2bf5..d900cdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,7 +141,7 @@ fn main() -> Result<(), std::io::Error> { if let Err(err) = procdata::TintProcessor::new(rpth) .set_profile(get_profile(cli, ¶ms)) .set_dry_run(params.get_flag("dry-run")) - .set_autodeps(params.get_flag("autodeps")) + .set_autodeps(params.get_one::("autodeps").unwrap().to_string()) .start() { log::error!("{}", err); diff --git a/src/procdata.rs b/src/procdata.rs index cea1ecc..c15fde7 100644 --- a/src/procdata.rs +++ b/src/procdata.rs @@ -14,18 +14,25 @@ use std::{ path::{Path, PathBuf}, }; +/// Autodependency mode +#[derive(Clone, Copy, PartialEq)] +pub enum Autodeps { + Undef, + Free, + Clean, +} /// Main processing of profiles or other data #[derive(Clone)] pub struct TintProcessor { profile: Profile, root: PathBuf, dry_run: bool, - autodeps: bool, + autodeps: Autodeps, } impl TintProcessor { pub fn new(root: PathBuf) -> Self { - TintProcessor { profile: Profile::default(), root, dry_run: true, autodeps: false } + TintProcessor { profile: Profile::default(), root, dry_run: true, autodeps: Autodeps::Free } } /// Set configuration from a profile @@ -41,8 +48,13 @@ impl TintProcessor { } /// Set flag for automatic dependency tracing - pub fn set_autodeps(&mut self, ad: bool) -> &mut Self { - self.autodeps = ad; + pub fn set_autodeps(&mut self, ad: String) -> &mut Self { + match ad.as_str() { + "free" => self.autodeps = Autodeps::Free, + "clean" => self.autodeps = Autodeps::Clean, + _ => self.autodeps = Autodeps::Undef, + } + self } @@ -160,7 +172,7 @@ impl TintProcessor { // and then let TextDataFilter removes what still should be removed. // The idea is to keep parts only relevant to the runtime. log::debug!("Filtering packages"); - let pscan = DebPackageScanner::new(false); // XXX: Maybe --autodeps=LEVEL to optionally include these too? + let pscan = DebPackageScanner::new(Autodeps::Undef); for p in self.profile.get_packages() { log::debug!("Getting content of package \"{}\"", p); paths.extend(pscan.get_package_contents(p.to_string())?); @@ -186,7 +198,8 @@ impl TintProcessor { // Remove resources log::debug!("Filtering resources"); - ResourcesDataFilter::new(paths.clone().into_iter().collect::>(), self.profile.to_owned()).filter(&mut paths); + ResourcesDataFilter::new(paths.clone().into_iter().collect::>(), self.profile.to_owned(), self.autodeps) + .filter(&mut paths); // Scan rootfs log::debug!("Scanning existing rootfs"); diff --git a/src/scanner/debpkg.rs b/src/scanner/debpkg.rs index cb0c248..f2e41fa 100644 --- a/src/scanner/debpkg.rs +++ b/src/scanner/debpkg.rs @@ -1,7 +1,10 @@ -use crate::scanner::{ - general::{Scanner, ScannerCommons}, - tracedeb, - traceitf::PkgDepTrace, +use crate::{ + procdata::Autodeps, + scanner::{ + general::{Scanner, ScannerCommons}, + tracedeb, + traceitf::PkgDepTrace, + }, }; use colored::Colorize; use std::{ @@ -13,12 +16,12 @@ use std::{ /// a target belongs to. pub struct DebPackageScanner { commons: ScannerCommons, - autodeps: bool, + autodeps: Autodeps, } impl DebPackageScanner { /// Constructor - pub fn new(autodeps: bool) -> Self { + pub fn new(autodeps: Autodeps) -> Self { DebPackageScanner { commons: ScannerCommons::new(), autodeps } } @@ -106,7 +109,7 @@ impl Scanner for DebPackageScanner { } } - if self.autodeps { + if self.autodeps != Autodeps::Undef { // Trace dependencies graph for the package for p in tracedeb::DebPackageTrace::new().trace(pkgname.to_owned()) { log::info!("Keeping dependency package: {}", p.bright_yellow()); From dbee737c905b53bea8fa70b05b4424a452cf8b57 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Fri, 3 Nov 2023 21:41:17 +0100 Subject: [PATCH 3/3] Add "tight" mode: remove junkfiles without actual autodeps resolve --- src/clidef.rs | 2 +- src/filters/resources.rs | 7 ++++++- src/procdata.rs | 4 +++- src/scanner/debpkg.rs | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/clidef.rs b/src/clidef.rs index 65dd712..5370926 100644 --- a/src/clidef.rs +++ b/src/clidef.rs @@ -56,7 +56,7 @@ pub fn cli(version: &'static str) -> Command { .long("autodeps") .default_value("none") .value_name("mode") - .value_parser(["free", "clean", "none"]) + .value_parser(["free", "clean", "tight", "none"]) .help(format!("Auto-add package dependencies.\n{}", " NOTE: This can increase the size, but might not always be useful\n".yellow())) ) .arg( diff --git a/src/filters/resources.rs b/src/filters/resources.rs index 92134e0..723c81d 100644 --- a/src/filters/resources.rs +++ b/src/filters/resources.rs @@ -88,11 +88,16 @@ impl ResourcesDataFilter { impl DataFilter for ResourcesDataFilter { fn filter(&self, data: &mut HashSet) { + if self.autodeps == Autodeps::Clean || self.autodeps == Autodeps::Tight { + log::info!("Automatically removing potential junk resources"); + } + let mut out: Vec = Vec::default(); for p in &self.data { if self.filter_archives(p) || self.filter_images(p) - || (self.autodeps == Autodeps::Clean && ResourcesDataFilter::is_potential_junk(p.to_str().unwrap())) + || ((self.autodeps == Autodeps::Clean || self.autodeps == Autodeps::Tight) + && ResourcesDataFilter::is_potential_junk(p.file_name().unwrap().to_str().unwrap())) { continue; } diff --git a/src/procdata.rs b/src/procdata.rs index c15fde7..9b03179 100644 --- a/src/procdata.rs +++ b/src/procdata.rs @@ -15,11 +15,12 @@ use std::{ }; /// Autodependency mode -#[derive(Clone, Copy, PartialEq)] +#[derive(Clone, Copy, PartialEq, Debug)] pub enum Autodeps { Undef, Free, Clean, + Tight, } /// Main processing of profiles or other data #[derive(Clone)] @@ -52,6 +53,7 @@ impl TintProcessor { match ad.as_str() { "free" => self.autodeps = Autodeps::Free, "clean" => self.autodeps = Autodeps::Clean, + "tight" => self.autodeps = Autodeps::Tight, _ => self.autodeps = Autodeps::Undef, } diff --git a/src/scanner/debpkg.rs b/src/scanner/debpkg.rs index f2e41fa..0845d1a 100644 --- a/src/scanner/debpkg.rs +++ b/src/scanner/debpkg.rs @@ -109,7 +109,7 @@ impl Scanner for DebPackageScanner { } } - if self.autodeps != Autodeps::Undef { + if self.autodeps == Autodeps::Clean || self.autodeps == Autodeps::Free { // Trace dependencies graph for the package for p in tracedeb::DebPackageTrace::new().trace(pkgname.to_owned()) { log::info!("Keeping dependency package: {}", p.bright_yellow());