From ddda8cbe13b6d27ddd919ea2897d3c19806c43f6 Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Mon, 6 Nov 2023 17:40:45 +0100 Subject: [PATCH] Implement dropped packages --- src/profile.rs | 22 +++++++++++++++++++++- src/scanner/binlib.rs | 5 +++++ src/scanner/debpkg.rs | 14 ++++++++++++-- src/scanner/general.rs | 4 ++++ src/scanner/tracedeb.rs | 13 +++++++++++-- src/scanner/traceitf.rs | 1 + 6 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/profile.rs b/src/profile.rs index 92f4864..b9b33ed 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -31,6 +31,7 @@ pub struct Profile { f_expl_keep: Vec, packages: Vec, + dropped_packages: Vec, targets: Vec, } @@ -48,6 +49,7 @@ impl Profile { f_img: true, f_arc: true, packages: vec![], + dropped_packages: vec![], targets: vec![], f_expl_prune: vec![], f_expl_keep: vec![], @@ -111,7 +113,20 @@ impl Profile { self.targets.extend(p.targets); if let Some(pkgs) = p.packages { - self.packages.extend(pkgs); + for p in &pkgs { + let mut p = p.replace(' ', ""); + if let Some(p) = p.strip_prefix('-') { + log::debug!("Dropping contents from package \"{}\"", p); + self.dropped_packages.push(p.to_string()); + continue; + } + + if let Some(s) = p.strip_prefix('+') { + p = s.to_string(); + } + + self.packages.push(p); + } } Ok(()) @@ -250,4 +265,9 @@ impl Profile { pub fn get_packages(&self) -> &Vec { &self.packages } + + /// Get dropped packages + pub fn get_dropped_packages(&self) -> &Vec { + &self.dropped_packages + } } diff --git a/src/scanner/binlib.rs b/src/scanner/binlib.rs index d2b735d..d44bb41 100644 --- a/src/scanner/binlib.rs +++ b/src/scanner/binlib.rs @@ -41,4 +41,9 @@ impl Scanner for ElfScanner { log::debug!("Scanning for dependencies in {}", pth.to_str().unwrap()); self.get_dynlibs(pth.to_str().unwrap().to_string()).iter().map(PathBuf::from).collect::>() } + + /// Bogus trait implementation, does nothing in this case + fn exclude(&mut self, _: Vec) -> &mut Self { + self + } } diff --git a/src/scanner/debpkg.rs b/src/scanner/debpkg.rs index 0845d1a..9e4fa1a 100644 --- a/src/scanner/debpkg.rs +++ b/src/scanner/debpkg.rs @@ -8,6 +8,7 @@ use crate::{ }; use colored::Colorize; use std::{ + collections::HashSet, io::{Error, ErrorKind}, path::PathBuf, }; @@ -17,12 +18,13 @@ use std::{ pub struct DebPackageScanner { commons: ScannerCommons, autodeps: Autodeps, + excluded_packages: HashSet, } impl DebPackageScanner { /// Constructor pub fn new(autodeps: Autodeps) -> Self { - DebPackageScanner { commons: ScannerCommons::new(), autodeps } + DebPackageScanner { commons: ScannerCommons::new(), autodeps, excluded_packages: HashSet::default() } } /// Expands target taking to the account Linux /bin symlinks to /usr/bin etc. @@ -111,7 +113,10 @@ impl Scanner for DebPackageScanner { 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()) { + for p in tracedeb::DebPackageTrace::new() + .exclude(self.excluded_packages.clone().into_iter().collect::>()) + .trace(pkgname.to_owned()) + { log::info!("Keeping dependency package: {}", p.bright_yellow()); match self.get_package_contents(p.to_owned()) { Ok(fp) => { @@ -127,4 +132,9 @@ impl Scanner for DebPackageScanner { out } + + fn exclude(&mut self, pkgs: Vec) -> &mut Self { + self.excluded_packages.extend(pkgs); + self + } } diff --git a/src/scanner/general.rs b/src/scanner/general.rs index e5ef9cb..e450ae3 100644 --- a/src/scanner/general.rs +++ b/src/scanner/general.rs @@ -1,7 +1,11 @@ use std::{fs, io::Error, path::PathBuf, process::Command}; pub(crate) trait Scanner { + /// Scan path fn scan(&mut self, pth: PathBuf) -> Vec; + + /// Add packages to be excluded from the scan + fn exclude(&mut self, pkgs: Vec) -> &mut Self; } pub struct ScannerCommons { diff --git a/src/scanner/tracedeb.rs b/src/scanner/tracedeb.rs index c9484b4..55a55d7 100644 --- a/src/scanner/tracedeb.rs +++ b/src/scanner/tracedeb.rs @@ -3,11 +3,12 @@ use std::{collections::HashSet, process::Command}; pub struct DebPackageTrace { data: HashSet, + exclude: HashSet, } impl DebPackageTrace { pub fn new() -> Self { - DebPackageTrace { data: HashSet::default() } + DebPackageTrace { data: HashSet::default(), exclude: HashSet::default() } } /// Get list of package dependencies for the first nearby level @@ -57,6 +58,14 @@ impl PkgDepTrace for DebPackageTrace { fn trace(&mut self, pkgname: String) -> Vec { log::info!("Getting dependencies for a package {}", pkgname); - self.get_dependencies(pkgname, true) + let mut d = self.get_dependencies(pkgname, true); + d.retain(|p| !self.exclude.contains(p)); + + d + } + + fn exclude(&mut self, pkgs: Vec) -> &mut Self { + self.exclude.extend(pkgs); + self } } diff --git a/src/scanner/traceitf.rs b/src/scanner/traceitf.rs index f31b2eb..593349a 100644 --- a/src/scanner/traceitf.rs +++ b/src/scanner/traceitf.rs @@ -1,4 +1,5 @@ /// Package dependency trace pub trait PkgDepTrace { fn trace(&mut self, pkgname: String) -> Vec; + fn exclude(&mut self, pkgs: Vec) -> &mut Self; }