Skip to content

Commit

Permalink
Merge pull request #27 from isbm/isbm-pkglist-dryrun
Browse files Browse the repository at this point in the history
Add packagelist for the dry-run
  • Loading branch information
isbm authored Nov 10, 2023
2 parents 32046e4 + a3502ab commit 90d0304
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
64 changes: 64 additions & 0 deletions src/scanner/debftrace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::rootfs::RootFS;

use super::traceitf::PkgFileTrace;
use std::{
collections::HashMap,
fs::{self, DirEntry},
path::PathBuf,
};

pub struct DebPkgFileTrace {
file_to_pkg: HashMap<PathBuf, String>,
}

impl DebPkgFileTrace {
pub fn new() -> Self {
let mut d = DebPkgFileTrace { file_to_pkg: HashMap::default() };
d.load();
d
}

/// Read dpkg cache. All of it.
fn load(&mut self) {
if let Ok(rd) = fs::read_dir("/var/lib/dpkg/info") {
for d in rd.filter_map(Result::ok).collect::<Vec<DirEntry>>() {
if d.path().to_str().unwrap().ends_with(".list") {
self.load_pkg(d.path());
}
}
}
}

fn load_pkg(&mut self, pinfo: PathBuf) {
// Path to package name
let pkgname = &pinfo
.file_name()
.unwrap_or_default()
.to_str()
.unwrap()
.strip_suffix(".list")
.unwrap()
.split(':')
.collect::<Vec<&str>>()[0];

if let Ok(pkg_data) = fs::read_to_string(&pinfo) {
for f_pth in pkg_data.split('\n').collect::<Vec<&str>>().iter().map(PathBuf::from) {
if f_pth.exists() && f_pth.is_file() {
self.file_to_pkg.insert(f_pth, pkgname.to_string().to_owned());
}
}
}
}
}

impl PkgFileTrace for DebPkgFileTrace {
fn trace(&mut self, filename: PathBuf) -> Option<String> {
for p in RootFS::expand_target(filename, true) {
if let Some(pkg) = self.file_to_pkg.get(&p) {
return Some(pkg.to_owned());
}
}

None
}
}
19 changes: 17 additions & 2 deletions src/scanner/dlst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
Data lister (fancy STDOUT printer)
*/

use crate::filters::resources;
use crate::{
filters::resources,
scanner::{debftrace::DebPkgFileTrace, traceitf::PkgFileTrace},
};
use bytesize::ByteSize;
use colored::Colorize;
use filesize::PathExt;
use std::{
collections::HashSet,
os::unix::prelude::PermissionsExt,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -110,6 +114,17 @@ impl<'a> ContentFormatter<'a> {
d_size += p.metadata().unwrap().len();
}

// Collect preserved packages
let mut pkgs: HashSet<String> = HashSet::default();
let mut pt = DebPkgFileTrace::new();
for p in self.fs_data {
if let Some(pkg) = pt.trace(p.clone()) {
pkgs.insert(pkg);
}
}
let mut pkgs = pkgs.into_iter().collect::<Vec<String>>();
pkgs.sort();

// Print the summary
println!(
"\nRemoved {} files, releasing {} of a disk space",
Expand All @@ -128,7 +143,7 @@ impl<'a> ContentFormatter<'a> {
ByteSize::b(j_size).to_string().bright_yellow()
);
}
println!("");
println!("Kept {} packages as follows:\n {}\n", pkgs.len().to_string().bright_yellow(), pkgs.join(", "));
}

/// Get dir/name split, painted accordingly
Expand Down
1 change: 1 addition & 0 deletions src/scanner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod binlib;
pub mod debftrace;
pub mod debpkg;
pub(crate) mod dlst;
pub mod general;
Expand Down
7 changes: 7 additions & 0 deletions src/scanner/traceitf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use std::path::PathBuf;

/// Package dependency trace
pub trait PkgDepTrace {
fn trace(&mut self, pkgname: String) -> Vec<String>;
fn exclude(&mut self, pkgs: Vec<String>) -> &mut Self;
}

pub trait PkgFileTrace {
/// Return a package name, to which this file belongs to
fn trace(&mut self, filename: PathBuf) -> Option<String>;
}

0 comments on commit 90d0304

Please sign in to comment.