Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct StatPrinter {
time_format: String,
line_ending: LineEnding,
summarize: bool,
separate_dirs: bool,
total_text: String,
}

Expand Down Expand Up @@ -579,6 +580,7 @@ fn safe_du(
print_tx.send(Ok(StatPrintInfo {
stat: this_stat,
depth: depth + 1,
is_dir: true,
}))?;
} else {
my_stat.size += this_stat.size;
Expand All @@ -593,6 +595,7 @@ fn safe_du(
print_tx.send(Ok(StatPrintInfo {
stat: this_stat,
depth: depth + 1,
is_dir: false,
}))?;
}
}
Expand Down Expand Up @@ -741,6 +744,7 @@ fn du_regular(
print_tx.send(Ok(StatPrintInfo {
stat: this_stat,
depth: depth + 1,
is_dir: true,
}))?;
} else {
my_stat.size += this_stat.size;
Expand All @@ -756,6 +760,7 @@ fn du_regular(
print_tx.send(Ok(StatPrintInfo {
stat: this_stat,
depth: depth + 1,
is_dir: false,
}))?;
}
}
Expand Down Expand Up @@ -843,6 +848,8 @@ fn build_exclude_patterns(matches: &ArgMatches) -> UResult<Vec<Pattern>> {
struct StatPrintInfo {
stat: Stat,
depth: usize,
/// Real directory bit; safe traversal leaves a placeholder metadata on children.
is_dir: bool,
}

impl StatPrinter {
Expand All @@ -860,14 +867,22 @@ impl StatPrinter {

fn print_stats(&self, rx: &mpsc::Receiver<UResult<StatPrintInfo>>) -> UResult<()> {
let mut grand_total = 0;
// Depth 0 already includes children, except with --separate-dirs.
let count_separate_dirs = self.total && self.separate_dirs;
// Total time starts at 1970-01-01 and moves forward only.
let track_total_time = self.total && self.time.is_some();
let mut grand_time = SystemTime::UNIX_EPOCH;
while let Ok(received) = rx.recv() {
match received {
Ok(stat_info) => {
let size = self.choose_size(&stat_info.stat);

if stat_info.depth == 0 {
if stat_info.depth == 0 || (count_separate_dirs && stat_info.is_dir) {
grand_total += size;
}
if track_total_time && let Some(time) = stat_info.stat.latest_time {
grand_time = grand_time.max(time);
}

if !self
.threshold
Expand All @@ -885,13 +900,9 @@ impl StatPrinter {
}

if self.total {
write!(
stdout(),
"{}\t{}{}",
self.convert_size(grand_total),
self.total_text,
self.line_ending
)?;
let time = track_total_time.then_some(grand_time);
self.write_size_and_time(grand_total, time)?;
write!(stdout(), "{}{}", self.total_text, self.line_ending)?;
}

Ok(())
Expand Down Expand Up @@ -919,10 +930,18 @@ impl StatPrinter {
}

fn print_stat(&self, stat: &Stat, size: u64) -> UResult<()> {
self.write_size_and_time(size, stat.latest_time)?;
print_verbatim(&stat.path)?;
write!(stdout(), "{}", self.line_ending)?;

Ok(())
}

fn write_size_and_time(&self, size: u64, time: Option<SystemTime>) -> UResult<()> {
write!(stdout(), "{}\t", self.convert_size(size))?;

if self.time.is_some() {
if let Some(time) = stat.latest_time {
if let Some(time) = time {
format_system_time(
&mut stdout(),
time,
Expand All @@ -935,9 +954,6 @@ impl StatPrinter {
}
}

print_verbatim(&stat.path)?;
write!(stdout(), "{}", self.line_ending)?;

Ok(())
}
}
Expand Down Expand Up @@ -1151,6 +1167,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
time,
time_format,
line_ending: LineEnding::from_zero_flag(matches.get_flag(options::NULL)),
separate_dirs: matches.get_flag(options::SEPARATE_DIRS),
total_text: translate!("du-total"),
};

Expand Down Expand Up @@ -1218,8 +1235,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Some(stat),
) {
Ok(stat) => {
let is_dir = stat.metadata.is_dir();
print_tx
.send(Ok(StatPrintInfo { stat, depth: 0 }))
.send(Ok(StatPrintInfo {
stat,
depth: 0,
is_dir,
}))
.map_err(|e| USimpleError::new(1, e.to_string()))?;
}
Err(e) => {
Expand Down Expand Up @@ -1248,8 +1270,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
)
.map_err(|e| USimpleError::new(1, e.to_string()))?;

let is_dir = stat.metadata.is_dir();
print_tx
.send(Ok(StatPrintInfo { stat, depth: 0 }))
.send(Ok(StatPrintInfo {
stat,
depth: 0,
is_dir,
}))
.map_err(|e| USimpleError::new(1, e.to_string()))?;
} else {
#[cfg(unix)]
Expand Down
Loading
Loading