Skip to content

Commit

Permalink
Merge pull request #26 from isbm/isbm-pkg-removal-fix
Browse files Browse the repository at this point in the history
Add pkg removal fix
  • Loading branch information
isbm authored Nov 9, 2023
2 parents 858baaf + 900f091 commit 32046e4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
15 changes: 13 additions & 2 deletions src/procdata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
filters::{dirs::PathsDataFilter, intf::DataFilter, resources::ResourcesDataFilter, texts::TextDataFilter},
profile::Profile,
rootfs,
rootfs::{self, RootFS},
scanner::{binlib::ElfScanner, debpkg::DebPackageScanner, dlst::ContentFormatter, general::Scanner},
shcall::ShellScript,
};
Expand Down Expand Up @@ -196,7 +196,7 @@ impl TintProcessor {
// Scan content of all profile packages (if any)
// 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");
log::debug!("Adding requested packages");
let pscan = DebPackageScanner::new(Autodeps::Undef);
for p in self.profile.get_packages() {
log::debug!("Getting content of package \"{}\"", p);
Expand Down Expand Up @@ -226,6 +226,17 @@ impl TintProcessor {
ResourcesDataFilter::new(paths.clone().into_iter().collect::<Vec<PathBuf>>(), self.profile.to_owned(), self.autodeps)
.filter(&mut paths);

// Remove package content before dissection
// XXX: Exlude .so binaries also from the Elf reader?
for p in self.profile.get_dropped_packages() {
log::debug!("Removing dropped package contents from \"{}\"", p);
for p in pscan.get_package_contents(p.to_string())? {
for p in RootFS::expand_target(p, true) {
paths.remove(&p);
}
}
}

// Scan rootfs
log::debug!("Scanning existing rootfs");
let mut p = rootfs::RootFS::new()
Expand Down
39 changes: 30 additions & 9 deletions src/rootfs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::{HashMap, HashSet},
fs,
path::{Path, PathBuf},
vec,
};
Expand All @@ -9,11 +10,13 @@ pub struct RootFS {
tmp: bool, // keep /tmp
rootfs: HashSet<PathBuf>,
ptree: Vec<PathBuf>,
broken_links: HashSet<PathBuf>,
}

impl RootFS {
pub fn new() -> Self {
let mut rf = RootFS { pds: true, tmp: true, rootfs: HashSet::default(), ptree: vec![] };
let mut rf =
RootFS { pds: true, tmp: true, rootfs: HashSet::default(), broken_links: HashSet::default(), ptree: Vec::default() };
rf.scan();

rf
Expand Down Expand Up @@ -47,11 +50,12 @@ impl RootFS {
}

for x in src {
for y in self.expand_target(x) {
for y in Self::expand_target(x, false) {
rfs.remove(&y);
}
}

rfs.extend(self.broken_links.to_owned());
rfs.into_iter().collect::<Vec<PathBuf>>()
}

Expand All @@ -61,7 +65,7 @@ impl RootFS {
/// the database still pointing to the old-fashioned location (e.g. "/bin").
/// In this case fall-back is used to find also in "/bin/<binary>" if
/// search for the "/usr/bin/<binary>" fails.
fn expand_target(&self, target: PathBuf) -> Vec<PathBuf> {
pub fn expand_target(target: PathBuf, reverse: bool) -> Vec<PathBuf> {
let mut p = PathBuf::from(&target);
let fname = p.file_name().unwrap().to_owned();

Expand All @@ -77,8 +81,20 @@ impl RootFS {
("/usr/lib64/".to_string(), "/lib64/".to_string()),
]);

for (_fd, fl) in aliases {
if fdir.starts_with(&fl) {
for (fd, fl) in aliases {
if reverse && fdir.starts_with(&fd) {
let mut out: Vec<PathBuf> = Vec::default();

let dpth = PathBuf::from(PathBuf::from(fdir).join(&fname).to_str().unwrap().to_string());
let dlnk = PathBuf::from(dpth.to_str().unwrap().strip_prefix("/usr").unwrap());

for p in [dpth, dlnk] {
if p.exists() {
out.push(p);
}
}
return out;
} else if fdir.starts_with(&fl) {
return vec![
PathBuf::from(PathBuf::from(fdir).join(&fname).to_str().unwrap().to_string()),
PathBuf::from(PathBuf::from(format!("/usr{}", fdir)).join(fname).to_str().unwrap().to_string()),
Expand All @@ -91,7 +107,7 @@ impl RootFS {

/// Diff the whole rootfs to see what's inside.
fn scan(&mut self) {
for rde in walkdir::WalkDir::new("/").follow_root_links(true).contents_first(true).follow_links(false) {
for rde in walkdir::WalkDir::new("/").follow_root_links(true).contents_first(true).follow_links(true) {
match rde {
Ok(entry) => {
let p = entry.into_path();
Expand All @@ -109,12 +125,17 @@ impl RootFS {
continue;
}

if p.is_file() {
self.rootfs.insert(p);
if p.is_file() && p.exists() {
if let Ok(p) = fs::canonicalize(p) {
self.rootfs.insert(p);
}
}
}
Err(err) => {
log::warn!("Unable to access \"{}\"", err);
log::debug!("Unable to access \"{}\"", err);
if let Some(p) = err.path() {
self.broken_links.insert(p.to_path_buf());
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/scanner/dlst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ impl<'a> ContentFormatter<'a> {

if let Some(fsr) = self.fs_removed {
for p in fsr {
total_size += p.size_on_disk_fast(&p.metadata().unwrap()).unwrap();
if p.exists() {
total_size += p.size_on_disk_fast(&p.metadata().unwrap()).unwrap();
}
total_files += 1;
log::debug!(" - {}", p.to_str().unwrap());
}
Expand Down

0 comments on commit 32046e4

Please sign in to comment.