Skip to content

Commit

Permalink
Move dry-run result to the formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
isbm committed Nov 7, 2023
1 parent bf170b5 commit 357a590
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
21 changes: 1 addition & 20 deletions src/procdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use crate::{
rootfs,
scanner::{binlib::ElfScanner, debpkg::DebPackageScanner, dlst::ContentFormatter, general::Scanner},
};
use bytesize::ByteSize;
use filesize::PathExt;
use std::fs::{self, canonicalize, remove_file, DirEntry, File};
use std::{
collections::HashSet,
Expand Down Expand Up @@ -127,22 +125,6 @@ impl TintProcessor {
Ok(())
}

/// Perform only a dry-run
fn dry_run(&self, paths: Vec<PathBuf>) -> Result<(), Error> {
let mut total_size: u64 = 0;
let mut total_files: usize = 0;

for p in paths {
total_size += p.size_on_disk_fast(&p.metadata().unwrap()).unwrap();
total_files += 1;
log::debug!(" - {}", p.to_str().unwrap());
}

println!("\nTotal files to be removed: {}, disk size freed: {}\n", total_files, ByteSize::b(total_size));

Ok(())
}

fn ext_path(p: HashSet<PathBuf>, mut np: HashSet<PathBuf>) -> HashSet<PathBuf> {
for tgt in p.iter() {
if tgt.is_symlink() {
Expand Down Expand Up @@ -228,8 +210,7 @@ impl TintProcessor {
paths.sort();

if self.dry_run {
self.dry_run(p)?;
ContentFormatter::new(&paths).format();
ContentFormatter::new(&paths).set_removed(&p).format();
} else {
self.apply_changes(p)?;
}
Expand Down
33 changes: 31 additions & 2 deletions src/scanner/dlst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Data lister (fancy STDOUT printer)
use crate::filters::resources;
use bytesize::ByteSize;
use colored::Colorize;
use filesize::PathExt;
use std::{
os::unix::prelude::PermissionsExt,
path::{Path, PathBuf},
Expand All @@ -15,11 +16,33 @@ use std::{
pub struct ContentFormatter<'a> {
fs_data: &'a Vec<PathBuf>,
last_dir: String,
fs_removed: Option<&'a Vec<PathBuf>>,
}

impl<'a> ContentFormatter<'a> {
pub(crate) fn new(fs_data: &'a Vec<PathBuf>) -> Self {
ContentFormatter { fs_data, last_dir: "".to_string() }
Self { fs_data, last_dir: "".to_string(), fs_removed: None }
}

/// Set removed data
pub(crate) fn set_removed(&mut self, r: &'a Vec<PathBuf>) -> &mut Self {
self.fs_removed = Some(r);
self
}

/// Perform only a dry-run
fn format_removed(&self) -> (u64, u64) {
let mut total_size: u64 = 0;
let mut total_files: u64 = 0;

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

#[allow(clippy::println_empty_string)]
Expand All @@ -30,6 +53,7 @@ impl<'a> ContentFormatter<'a> {
let mut j_total: u64 = 0; // total junk files
let mut d_total: u64 = 0;
let mut d_size: u64 = 0;
let (t_r_files, t_r_size) = self.format_removed();

for (pi, p) in self.fs_data.iter().enumerate() {
let mut t_leaf: String = "".to_string();
Expand Down Expand Up @@ -86,7 +110,12 @@ impl<'a> ContentFormatter<'a> {

// Print the summary
println!(
"\nPreserved {} files, taking {} of a disk space",
"\nRemoved {} files, releasing {} of a disk space",
t_r_files.to_string().bright_green(),
ByteSize::b(t_r_size).to_string().bright_yellow()
);
println!(
"Preserved {} files, taking {} of a disk space",
(d_len + 1).to_string().bright_green(),
ByteSize::b(t_size).to_string().bright_yellow()
);
Expand Down

0 comments on commit 357a590

Please sign in to comment.