Skip to content

Commit

Permalink
Implement dropped packages
Browse files Browse the repository at this point in the history
  • Loading branch information
isbm committed Nov 6, 2023
1 parent 6e7c187 commit ddda8cb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
22 changes: 21 additions & 1 deletion src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Profile {
f_expl_keep: Vec<PathBuf>,

packages: Vec<String>,
dropped_packages: Vec<String>,
targets: Vec<String>,
}

Expand All @@ -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![],
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -250,4 +265,9 @@ impl Profile {
pub fn get_packages(&self) -> &Vec<String> {
&self.packages
}

/// Get dropped packages
pub fn get_dropped_packages(&self) -> &Vec<String> {
&self.dropped_packages
}
}
5 changes: 5 additions & 0 deletions src/scanner/binlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<PathBuf>>()
}

/// Bogus trait implementation, does nothing in this case
fn exclude(&mut self, _: Vec<String>) -> &mut Self {
self
}
}
14 changes: 12 additions & 2 deletions src/scanner/debpkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
};
use colored::Colorize;
use std::{
collections::HashSet,
io::{Error, ErrorKind},
path::PathBuf,
};
Expand All @@ -17,12 +18,13 @@ use std::{
pub struct DebPackageScanner {
commons: ScannerCommons,
autodeps: Autodeps,
excluded_packages: HashSet<String>,
}

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.
Expand Down Expand Up @@ -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::<Vec<String>>())
.trace(pkgname.to_owned())
{
log::info!("Keeping dependency package: {}", p.bright_yellow());
match self.get_package_contents(p.to_owned()) {
Ok(fp) => {
Expand All @@ -127,4 +132,9 @@ impl Scanner for DebPackageScanner {

out
}

fn exclude(&mut self, pkgs: Vec<String>) -> &mut Self {
self.excluded_packages.extend(pkgs);
self
}
}
4 changes: 4 additions & 0 deletions src/scanner/general.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>;

/// Add packages to be excluded from the scan
fn exclude(&mut self, pkgs: Vec<String>) -> &mut Self;
}

pub struct ScannerCommons {
Expand Down
13 changes: 11 additions & 2 deletions src/scanner/tracedeb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use std::{collections::HashSet, process::Command};

pub struct DebPackageTrace {
data: HashSet<String>,
exclude: HashSet<String>,
}

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
Expand Down Expand Up @@ -57,6 +58,14 @@ impl PkgDepTrace for DebPackageTrace {
fn trace(&mut self, pkgname: String) -> Vec<String> {
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<String>) -> &mut Self {
self.exclude.extend(pkgs);
self
}
}
1 change: 1 addition & 0 deletions src/scanner/traceitf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// Package dependency trace
pub trait PkgDepTrace {
fn trace(&mut self, pkgname: String) -> Vec<String>;
fn exclude(&mut self, pkgs: Vec<String>) -> &mut Self;
}

0 comments on commit ddda8cb

Please sign in to comment.