Skip to content

Commit

Permalink
du: put excludes in traversaloptions and make size_format enum
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Dec 7, 2023
1 parent d6b10d4 commit 7861559
Showing 1 changed file with 86 additions and 137 deletions.
223 changes: 86 additions & 137 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,20 @@ struct TraversalOptions {
dereference: Deref,
count_links: bool,
verbose: bool,
excludes: Vec<Pattern>,
}

#[derive(Clone)]
struct PrintingOptions {
struct StatPrinter {
total: bool,
inodes: bool,
max_depth: Option<usize>,
threshold: Option<Threshold>,
apparent_size: bool,
// TODO: the size conversion fields should be unified
si: bool,
bytes: bool,
human_readable: bool,
block_size_1k: bool,
block_size_1m: bool,
block_size: u64,
size_format: SizeFormat,
time: Option<Time>,
time_format: String,
line_ending: LineEnding,
summarize: bool,
}

#[derive(PartialEq, Clone)]
Expand All @@ -118,6 +113,12 @@ enum Time {
Created,
}

#[derive(Clone)]

Check warning on line 116 in src/uu/du/src/du.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/du/src/du.rs#L116

Added line #L116 was not covered by tests
enum SizeFormat {
Human(u64),
BlockSize(u64),

Check warning on line 119 in src/uu/du/src/du.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/du/src/du.rs#L118-L119

Added lines #L118 - L119 were not covered by tests
}

#[derive(PartialEq, Eq, Hash, Clone, Copy)]
struct FileInfo {
file_id: u128,
Expand Down Expand Up @@ -295,26 +296,13 @@ fn read_block_size(s: Option<&str>) -> UResult<u64> {
}
}

fn choose_size(options: &PrintingOptions, stat: &Stat) -> u64 {
if options.inodes {
stat.inodes
} else if options.apparent_size || options.bytes {
stat.size
} else {
// The st_blocks field indicates the number of blocks allocated to the file, 512-byte units.
// See: http://linux.die.net/man/2/stat
stat.blocks * 512
}
}

// this takes `my_stat` to avoid having to stat files multiple times.
#[allow(clippy::cognitive_complexity)]
fn du(
mut my_stat: Stat,
options: &TraversalOptions,
depth: usize,
seen_inodes: &mut HashSet<FileInfo>,
exclude: &[Pattern],
print_tx: &mpsc::Sender<UResult<StatPrintInfo>>,
) -> Result<Stat, Box<mpsc::SendError<UResult<StatPrintInfo>>>> {
if my_stat.is_dir {
Expand All @@ -334,7 +322,7 @@ fn du(
match Stat::new(&entry.path(), options) {
Ok(this_stat) => {
// We have an exclude list
for pattern in exclude {
for pattern in &options.excludes {
// Look at all patterns with both short and long paths
// if we have 'du foo' but search to exclude 'foo/bar'
// we need the full path
Expand Down Expand Up @@ -370,14 +358,8 @@ fn du(
}
}

let this_stat = du(
this_stat,
options,
depth + 1,
seen_inodes,
exclude,
print_tx,
)?;
let this_stat =
du(this_stat, options, depth + 1, seen_inodes, print_tx)?;

if !options.separate_dirs {
my_stat.size += this_stat.size;
Expand Down Expand Up @@ -413,52 +395,6 @@ fn du(
Ok(my_stat)
}

fn convert_size_human(size: u64, multiplier: u64, _block_size: u64) -> String {
for &(unit, power) in &UNITS {
let limit = multiplier.pow(power);
if size >= limit {
return format!("{:.1}{}", (size as f64) / (limit as f64), unit);
}
}
if size == 0 {
return "0".to_string();
}
format!("{size}B")
}

fn convert_size_b(size: u64, _multiplier: u64, _block_size: u64) -> String {
format!("{}", ((size as f64) / (1_f64)).ceil())
}

fn convert_size_k(size: u64, multiplier: u64, _block_size: u64) -> String {
format!("{}", ((size as f64) / (multiplier as f64)).ceil())
}

fn convert_size_m(size: u64, multiplier: u64, _block_size: u64) -> String {
format!(
"{}",
((size as f64) / ((multiplier * multiplier) as f64)).ceil()
)
}

fn convert_size_other(size: u64, _multiplier: u64, block_size: u64) -> String {
format!("{}", ((size as f64) / (block_size as f64)).ceil())
}

fn get_convert_size_fn(options: &PrintingOptions) -> Box<dyn Fn(u64, u64, u64) -> String + Send> {
if options.human_readable || options.si {
Box::new(convert_size_human)
} else if options.bytes {
Box::new(convert_size_b)
} else if options.block_size_1k {
Box::new(convert_size_k)
} else if options.block_size_1m {
Box::new(convert_size_m)
} else {
Box::new(convert_size_other)
}
}

#[derive(Debug)]
enum DuError {
InvalidMaxDepthArg(String),
Expand Down Expand Up @@ -554,29 +490,17 @@ struct StatPrintInfo {
depth: usize,
}

struct StatPrinter {
summarize: bool,
options: PrintingOptions,
convert_size: Box<dyn Fn(u64) -> String + Send>,
}

impl StatPrinter {
fn new(options: PrintingOptions, summarize: bool) -> UResult<Self> {
let multiplier: u64 = if options.si { 1000 } else { 1024 };

let convert_size_fn = get_convert_size_fn(&options);

let convert_size: Box<dyn Fn(u64) -> String + Send> = if options.inodes {
Box::new(|size: u64| size.to_string())
fn choose_size(&self, stat: &Stat) -> u64 {
if self.inodes {
stat.inodes
} else if self.apparent_size {
stat.size
} else {
Box::new(move |size: u64| convert_size_fn(size, multiplier, options.block_size))
};

Ok(Self {
summarize,
options,
convert_size,
})
// The st_blocks field indicates the number of blocks allocated to the file, 512-byte units.
// See: http://linux.die.net/man/2/stat
stat.blocks * 512
}
}

fn print_stats(&self, rx: &mpsc::Receiver<UResult<StatPrintInfo>>) -> UResult<()> {
Expand All @@ -587,18 +511,16 @@ impl StatPrinter {
match received {
Ok(message) => match message {
Ok(stat_info) => {
let size = choose_size(&self.options, &stat_info.stat);
let size = self.choose_size(&stat_info.stat);

if stat_info.depth == 0 {
grand_total += size;
}

if !self
.options
.threshold
.map_or(false, |threshold| threshold.should_exclude(size))
&& self
.options
.max_depth
.map_or(true, |max_depth| stat_info.depth <= max_depth)
&& (!self.summarize || stat_info.depth == 0)
Expand All @@ -612,31 +534,59 @@ impl StatPrinter {
}
}

if self.options.total {
print!("{}\ttotal", (self.convert_size)(grand_total));
print!("{}", self.options.line_ending);
if self.total {
print!("{}\ttotal", self.convert_size(grand_total));
print!("{}", self.line_ending);
}

Ok(())
}

fn convert_size(&self, size: u64) -> String {
if self.inodes {
return size.to_string();
}
match self.size_format {
SizeFormat::Human(multiplier) => {
if size == 0 {
return "0".to_string();
}
for &(unit, power) in &UNITS {
let limit = multiplier.pow(power);
if size >= limit {
return format!("{:.1}{}", (size as f64) / (limit as f64), unit);
}
}
format!("{size}B")
}

Check warning on line 561 in src/uu/du/src/du.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/du/src/du.rs#L560-L561

Added lines #L560 - L561 were not covered by tests
SizeFormat::BlockSize(block_size) => div_ceil(size, block_size).to_string(),
}
}

fn print_stat(&self, stat: &Stat, size: u64) -> UResult<()> {
if let Some(time) = self.options.time {
if let Some(time) = self.time {
let secs = get_time_secs(time, stat)?;
let tm = DateTime::<Local>::from(UNIX_EPOCH + Duration::from_secs(secs));
let time_str = tm.format(&self.options.time_format).to_string();
print!("{}\t{}\t", (self.convert_size)(size), time_str);
let time_str = tm.format(&self.time_format).to_string();
print!("{}\t{}\t", self.convert_size(size), time_str);
} else {
print!("{}\t", (self.convert_size)(size));
print!("{}\t", self.convert_size(size));
}

print_verbatim(&stat.path).unwrap();
print!("{}", self.options.line_ending);
print!("{}", self.line_ending);

Ok(())
}
}

// This can be replaced with u64::div_ceil once it is stabilized.
// This implementation approach is optimized for when `b` is a constant,
// particularly a power of two.
pub fn div_ceil(a: u64, b: u64) -> u64 {
(a + b - 1) / b
}

#[uucore::main]
#[allow(clippy::cognitive_complexity)]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Expand Down Expand Up @@ -671,11 +621,23 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
});

let block_size = read_block_size(
matches
.get_one::<String>(options::BLOCK_SIZE)
.map(|s| s.as_str()),
)?;
let size_format = if matches.get_flag(options::HUMAN_READABLE) {
SizeFormat::Human(1024)
} else if matches.get_flag(options::SI) {
SizeFormat::Human(1000)
} else if matches.get_flag(options::BYTES) {
SizeFormat::BlockSize(1)
} else if matches.get_flag(options::BLOCK_SIZE_1K) {
SizeFormat::BlockSize(1024)
} else if matches.get_flag(options::BLOCK_SIZE_1M) {
SizeFormat::BlockSize(1024 * 1024)

Check warning on line 633 in src/uu/du/src/du.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/du/src/du.rs#L633

Added line #L633 was not covered by tests
} else {
SizeFormat::BlockSize(read_block_size(
matches
.get_one::<String>(options::BLOCK_SIZE)
.map(AsRef::as_ref),
)?)
};

let traversal_options = TraversalOptions {
all: matches.get_flag(options::ALL),
Expand All @@ -691,13 +653,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
},
count_links: matches.get_flag(options::COUNT_LINKS),
verbose: matches.get_flag(options::VERBOSE),
excludes: build_exclude_patterns(&matches)?,
};

let printing_options = PrintingOptions {
let stat_printer = StatPrinter {
max_depth,
size_format,
summarize,
total: matches.get_flag(options::TOTAL),
inodes: matches.get_flag(options::INODES),
si: matches.get_flag(options::SI),
threshold: matches
.get_one::<String>(options::THRESHOLD)
.map(|s| {
Expand All @@ -706,36 +670,28 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
})
})
.transpose()?,
apparent_size: matches.get_flag(options::APPARENT_SIZE),
bytes: matches.get_flag(options::BYTES),
apparent_size: matches.get_flag(options::APPARENT_SIZE) || matches.get_flag(options::BYTES),
time,
block_size,
human_readable: matches.get_flag(options::HUMAN_READABLE),
block_size_1k: matches.get_flag(options::BLOCK_SIZE_1K),
block_size_1m: matches.get_flag(options::BLOCK_SIZE_1M),
time_format: parse_time_style(matches.get_one::<String>("time-style").map(|s| s.as_str()))?
.to_string(),
line_ending: LineEnding::from_zero_flag(matches.get_flag(options::NULL)),
};

if printing_options.inodes
if stat_printer.inodes
&& (matches.get_flag(options::APPARENT_SIZE) || matches.get_flag(options::BYTES))
{
show_warning!("options --apparent-size and -b are ineffective with --inodes");
}

// Use separate thread to print output, so we can print finished results while computation is still running
let stat_printer = StatPrinter::new(printing_options.clone(), summarize)?;
let (print_tx, rx) = mpsc::channel::<UResult<StatPrintInfo>>();
let printing_thread = thread::spawn(move || stat_printer.print_stats(&rx));

let excludes = build_exclude_patterns(&matches)?;

'loop_file: for path in files {
// Skip if we don't want to ignore anything
if !&excludes.is_empty() {
if !&traversal_options.excludes.is_empty() {
let path_string = path.to_string_lossy();
for pattern in &excludes {
for pattern in &traversal_options.excludes {
if pattern.matches(&path_string) {
// if the directory is ignored, leave early
if traversal_options.verbose {
Expand All @@ -753,15 +709,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if let Some(inode) = stat.inode {
seen_inodes.insert(inode);
}
let stat = du(
stat,
&traversal_options,
0,
&mut seen_inodes,
&excludes,
&print_tx,
)
.map_err(|e| USimpleError::new(1, e.to_string()))?;
let stat = du(stat, &traversal_options, 0, &mut seen_inodes, &print_tx)
.map_err(|e| USimpleError::new(1, e.to_string()))?;

print_tx
.send(Ok(StatPrintInfo { stat, depth: 0 }))
Expand Down

0 comments on commit 7861559

Please sign in to comment.