1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-04 06:57:47 +00:00

du: remove inefficient multi-threading

This commit is contained in:
Alex Lyon 2017-12-10 09:52:22 -08:00
parent 9316fb4603
commit 2efd2b38be
No known key found for this signature in database
GPG key ID: B517F04B325131B1

View file

@ -17,17 +17,15 @@ extern crate time;
extern crate uucore;
use std::fs;
use std::iter;
use std::io::{stderr, Write};
use std::os::unix::fs::MetadataExt;
use std::path::PathBuf;
use std::sync::Arc;
use time::Timespec;
use std::sync::mpsc::channel;
use std::thread;
static NAME: &'static str = "du";
static SUMMARY: &'static str = "estimate file space usage";
static LONG_HELP: &'static str = "
const NAME: &'static str = "du";
const SUMMARY: &'static str = "estimate file space usage";
const LONG_HELP: &'static str = "
Display values are in units of the first available SIZE from
--block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environ
ment variables. Otherwise, units default to 1024 bytes (or 512 if
@ -58,10 +56,10 @@ struct Stat {
}
impl Stat {
fn new(path: &PathBuf) -> Stat {
let metadata = safe_unwrap!(fs::symlink_metadata(path));
fn new(path: PathBuf) -> Stat {
let metadata = safe_unwrap!(fs::symlink_metadata(&path));
Stat {
path: path.clone(),
path: path,
is_dir: metadata.is_dir(),
size: metadata.len(),
blocks: metadata.blocks() as u64,
@ -74,56 +72,51 @@ impl Stat {
}
// this takes `my_stat` to avoid having to stat files multiple times.
fn du(path: &PathBuf, mut my_stat: Stat, options: Arc<Options>, depth: usize) -> Vec<Arc<Stat>> {
// XXX: this should use the impl Trait return type when it is stabilized
fn du(mut my_stat: Stat, options: &Options, depth: usize)
-> Box<DoubleEndedIterator<Item = Stat>>
{
let mut stats = vec!();
let mut futures = vec!();
if my_stat.is_dir {
let read = match fs::read_dir(path) {
let read = match fs::read_dir(&my_stat.path) {
Ok(read) => read,
Err(e) => {
safe_writeln!(stderr(), "{}: cannot read directory {}: {}",
options.program_name, path.display(), e);
return vec!(Arc::new(my_stat))
options.program_name, my_stat.path.display(), e);
return Box::new(iter::once(my_stat))
}
};
for f in read.into_iter() {
let entry = f.unwrap();
let this_stat = Stat::new(&entry.path());
let entry = crash_if_err!(1, f);
let this_stat = Stat::new(entry.path());
if this_stat.is_dir {
let oa_clone = options.clone();
let (tx, rx) = channel();
thread::spawn(move || {
let result = du(&entry.path(), this_stat, oa_clone, depth + 1);
tx.send(result)
});
futures.push(rx);
futures.push(du(this_stat, options, depth + 1));
} else {
my_stat.size += this_stat.size;
my_stat.blocks += this_stat.blocks;
if options.all {
stats.push(Arc::new(this_stat))
stats.push(this_stat);
}
}
}
}
for rx in &mut futures {
for stat in rx.recv().unwrap().into_iter().rev() {
if !options.separate_dirs && stat.path.parent().unwrap().to_path_buf() == my_stat.path {
stats.extend(futures.into_iter().flat_map(|val| val).rev().filter_map(|stat| {
if !options.separate_dirs && stat.path.parent().unwrap() == my_stat.path {
my_stat.size += stat.size;
my_stat.blocks += stat.blocks;
}
if options.max_depth == None || depth < options.max_depth.unwrap() {
stats.push(stat.clone());
Some(stat)
} else {
None
}
}
}
stats.push(Arc::new(my_stat));
stats
}));
stats.push(my_stat);
Box::new(stats.into_iter())
}
pub fn uumain(args: Vec<String>) -> i32 {
@ -218,8 +211,6 @@ pub fn uumain(args: Vec<String>) -> i32 {
let strs = if matches.free.is_empty() {vec!("./".to_owned())} else {matches.free.clone()};
let options_arc = Arc::new(options);
let MB = if matches.opt_present("si") {1000 * 1000} else {1024 * 1024};
let KB = if matches.opt_present("si") {1000} else {1024};
@ -304,7 +295,7 @@ Try '{} --help' for more information.", s, NAME);
let mut grand_total = 0;
for path_str in strs.into_iter() {
let path = PathBuf::from(path_str);
let iter = du(&path, Stat::new(&path), options_arc.clone(), 0).into_iter();
let iter = du(Stat::new(path), &options, 0).into_iter();
let (_, len) = iter.size_hint();
let len = len.unwrap();
for (index, stat) in iter.enumerate() {
@ -346,7 +337,7 @@ Try '{} --help' for more information.", s, NAME);
print!("{}\t{}{}", convert_size(size), stat.path.display(), line_separator);
}
}
if options_arc.total && index == (len - 1) {
if options.total && index == (len - 1) {
// The last element will be the total size of the the path under
// path_str. We add it to the grand total.
grand_total += size;
@ -354,7 +345,7 @@ Try '{} --help' for more information.", s, NAME);
}
}
if options_arc.total {
if options.total {
print!("{}\ttotal", convert_size(grand_total));
print!("{}", line_separator);
}