1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

du: make it correct.

Previously, this was handling symlinks wrong (i.e. `du` was following
them, not just looking at them as files). Handling them correctly
requires using `lstat`.

Also, this would abort when reading a file with invalid permissions (or
one that doesn't exist... as a link target of a symlink), rather than
just warning and continuing.

Also, this patch reduces the number of `stat`s done per file to exactly
one (i.e. the minimum).
This commit is contained in:
Huon Wilson 2014-03-19 18:45:20 +11:00
parent 3c1466c4ed
commit 6072554512

View file

@ -17,8 +17,7 @@ extern crate sync;
extern crate time; extern crate time;
use std::os; use std::os;
use std::io::fs; use std::io::{stderr, fs, FileStat, TypeDirectory};
use std::io::FileStat;
use std::option::Option; use std::option::Option;
use std::path::Path; use std::path::Path;
use time::Timespec; use time::Timespec;
@ -32,30 +31,40 @@ static VERSION: &'static str = "1.0.0";
struct Options { struct Options {
all: bool, all: bool,
program_name: ~str,
max_depth: Option<uint>, max_depth: Option<uint>,
total: bool, total: bool,
separate_dirs: bool, separate_dirs: bool,
} }
fn du(path: &Path, options_arc: Arc<Options>, depth: uint) -> ~[Arc<FileStat>] { // this takes `my_stat` to avoid having to stat files multiple times.
fn du(path: &Path, mut my_stat: FileStat,
options_arc: Arc<Options>, depth: uint) -> ~[Arc<FileStat>] {
let mut stats = ~[]; let mut stats = ~[];
let mut futures = ~[]; let mut futures = ~[];
let options = options_arc.get(); let options = options_arc.get();
let mut my_stat = safe_unwrap!(path.stat());
for f in safe_unwrap!(fs::readdir(path)).move_iter() { if my_stat.kind == TypeDirectory {
match f.is_file() { let read = match fs::readdir(path) {
true => { Ok(read) => read,
let stat = safe_unwrap!(f.stat()); Err(e) => {
my_stat.size += stat.size; writeln!(&mut stderr(), "{}: cannot read directory {}: {}",
my_stat.unstable.blocks += stat.unstable.blocks; options.program_name, path.display(), e);
if options.all { return ~[Arc::new(my_stat)]
stats.push(Arc::new(stat))
}
} }
false => { };
for f in read.move_iter() {
let this_stat = safe_unwrap!(fs::lstat(&f));
if this_stat.kind == TypeDirectory {
let oa_clone = options_arc.clone(); let oa_clone = options_arc.clone();
futures.push(Future::spawn(proc() { du(&f, oa_clone, depth + 1) })) futures.push(Future::spawn(proc() { du(&f, this_stat, oa_clone, depth + 1) }))
} else {
my_stat.size += this_stat.size;
my_stat.unstable.blocks += this_stat.unstable.blocks;
if options.all {
stats.push(Arc::new(this_stat))
}
} }
} }
} }
@ -198,6 +207,7 @@ ers of 1000).",
let options = Options { let options = Options {
all: matches.opt_present("all"), all: matches.opt_present("all"),
program_name: program.to_owned(),
max_depth: max_depth, max_depth: max_depth,
total: matches.opt_present("total"), total: matches.opt_present("total"),
separate_dirs: matches.opt_present("S"), separate_dirs: matches.opt_present("S"),
@ -301,7 +311,8 @@ Try '{program} --help' for more information.", s, program = program);
let mut grand_total = 0; let mut grand_total = 0;
for path_str in strs.move_iter() { for path_str in strs.move_iter() {
let path = Path::new(path_str); let path = Path::new(path_str);
let iter = du(&path, options_arc.clone(), 0).move_iter(); let stat = safe_unwrap!(fs::lstat(&path));
let iter = du(&path, stat, options_arc.clone(), 0).move_iter();
let (_, len) = iter.size_hint(); let (_, len) = iter.size_hint();
let len = len.unwrap(); let len = len.unwrap();
for (index, stat_arc) in iter.enumerate() { for (index, stat_arc) in iter.enumerate() {
@ -340,7 +351,7 @@ Try '{program} --help' for more information.", s, program = program);
print!("{:<10} {}", convert_size(size), stat.path.display()); print!("{:<10} {}", convert_size(size), stat.path.display());
} }
print!("{}", line_separator); print!("{}", line_separator);
if options.total && index == (len - 1) { if options_arc.get().total && index == (len - 1) {
// The last element will be the total size of the the path under // The last element will be the total size of the the path under
// path_str. We add it to the grand total. // path_str. We add it to the grand total.
grand_total += size; grand_total += size;
@ -348,7 +359,7 @@ Try '{program} --help' for more information.", s, program = program);
} }
} }
if options.total { if options_arc.get().total {
print!("{:<10} total", convert_size(grand_total)); print!("{:<10} total", convert_size(grand_total));
print!("{}", line_separator); print!("{}", line_separator);
} }