mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Implemented a few more options
This commit is contained in:
parent
5ab82df876
commit
14edd5704b
1 changed files with 56 additions and 9 deletions
63
du/du.rs
63
du/du.rs
|
@ -15,18 +15,24 @@ use std::os;
|
||||||
use std::io::stderr;
|
use std::io::stderr;
|
||||||
use std::io::fs;
|
use std::io::fs;
|
||||||
use std::io::FileStat;
|
use std::io::FileStat;
|
||||||
|
use std::option::Option;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use extra::arc::Arc;
|
use extra::arc::Arc;
|
||||||
use extra::future::Future;
|
use extra::future::Future;
|
||||||
use extra::getopts::{groups, Matches};
|
use extra::getopts::groups;
|
||||||
|
|
||||||
static VERSION: &'static str = "1.0.0";
|
static VERSION: &'static str = "1.0.0";
|
||||||
|
|
||||||
|
struct Options {
|
||||||
|
all: bool,
|
||||||
|
max_depth: Option<uint>,
|
||||||
|
total: bool
|
||||||
|
}
|
||||||
|
|
||||||
fn du(path: &Path, matches_arc: Arc<Matches>) -> ~[Arc<FileStat>] {
|
fn du(path: &Path, options_arc: Arc<Options>, depth: uint) -> ~[Arc<FileStat>] {
|
||||||
let mut stats = ~[];
|
let mut stats = ~[];
|
||||||
let mut futures = ~[];
|
let mut futures = ~[];
|
||||||
let matches = matches_arc.get();
|
let options = options_arc.get();
|
||||||
let mut my_stat = path.stat();
|
let mut my_stat = path.stat();
|
||||||
|
|
||||||
for f in fs::readdir(path).move_iter() {
|
for f in fs::readdir(path).move_iter() {
|
||||||
|
@ -34,13 +40,13 @@ fn du(path: &Path, matches_arc: Arc<Matches>) -> ~[Arc<FileStat>] {
|
||||||
true => {
|
true => {
|
||||||
let stat = f.stat();
|
let stat = f.stat();
|
||||||
my_stat.size += stat.size;
|
my_stat.size += stat.size;
|
||||||
if matches.opt_present("all") {
|
if options.all {
|
||||||
stats.push(Arc::new(stat))
|
stats.push(Arc::new(stat))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
let ma_clone = matches_arc.clone();
|
let oa_clone = options_arc.clone();
|
||||||
futures.push(do Future::spawn { du(&f, ma_clone) })
|
futures.push(do Future::spawn { du(&f, oa_clone, depth + 1) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,9 +57,11 @@ fn du(path: &Path, matches_arc: Arc<Matches>) -> ~[Arc<FileStat>] {
|
||||||
if stat.path.dir_path() == my_stat.path {
|
if stat.path.dir_path() == my_stat.path {
|
||||||
my_stat.size += stat.size;
|
my_stat.size += stat.size;
|
||||||
}
|
}
|
||||||
|
if options.max_depth == None || depth < options.max_depth.unwrap() {
|
||||||
stats.push(stat_arc.clone());
|
stats.push(stat_arc.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
stats.push(Arc::new(my_stat));
|
stats.push(Arc::new(my_stat));
|
||||||
|
|
||||||
|
@ -159,19 +167,58 @@ ers of 1000).");
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let options = Options{
|
||||||
|
all: matches.opt_present("all"),
|
||||||
|
max_depth: match (matches.opt_present("summarize"), matches.opt_str("max-depth")) {
|
||||||
|
(true, Some(s)) => match from_str::<uint>(s) {
|
||||||
|
Some(_) => {
|
||||||
|
println!("du: warning: summarizing conflicts with --max-depth={:s}", s);
|
||||||
|
return
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
println!("du: invalid maximum depth '{:s}'", s);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(true, None) => Some(0),
|
||||||
|
(false, Some(s)) => match from_str::<uint>(s) {
|
||||||
|
Some(u) => Some(u),
|
||||||
|
None => {
|
||||||
|
println!("du: invalid maximum depth '{:s}'", s);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(false, None) => None
|
||||||
|
},
|
||||||
|
total: matches.opt_present("total")
|
||||||
|
};
|
||||||
|
|
||||||
let strs = matches.free.clone();
|
let strs = matches.free.clone();
|
||||||
let strs = match strs.is_empty() {
|
let strs = match strs.is_empty() {
|
||||||
true => ~[~"./"],
|
true => ~[~"./"],
|
||||||
false => strs
|
false => strs
|
||||||
};
|
};
|
||||||
|
|
||||||
let matches_arc = Arc::new(matches);
|
let options_arc = Arc::new(options);
|
||||||
|
|
||||||
|
let mut grand_total = 0;
|
||||||
for path_str in strs.iter() {
|
for path_str in strs.iter() {
|
||||||
let path = Path::init(path_str.clone());
|
let path = Path::init(path_str.clone());
|
||||||
for stat_arc in du(&path, matches_arc.clone()).move_iter() {
|
let iter = du(&path, options_arc.clone(), 0).move_iter();
|
||||||
|
let (_, len) = iter.size_hint();
|
||||||
|
let len = len.unwrap();
|
||||||
|
for (index, stat_arc) in iter.enumerate() {
|
||||||
let stat = stat_arc.get();
|
let stat = stat_arc.get();
|
||||||
println!("{:<10} {}", stat.size, stat.path.display());
|
println!("{:<10} {}", stat.size, stat.path.display());
|
||||||
|
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 += stat.size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.total {
|
||||||
|
println!("{:<10} total", grand_total);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue