mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
commit
d3851a5cf2
1 changed files with 73 additions and 67 deletions
140
du/du.rs
140
du/du.rs
|
@ -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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,7 +89,7 @@ fn du(path: &Path, options_arc: Arc<Options>, depth: uint) -> ~[Arc<FileStat>] {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = os::args();
|
let args = os::args();
|
||||||
let program = args[0].clone();
|
let program = args[0].as_slice();
|
||||||
let opts = ~[
|
let opts = ~[
|
||||||
// In task
|
// In task
|
||||||
getopts::optflag("a", "all", " write counts for all files, not just directories"),
|
getopts::optflag("a", "all", " write counts for all files, not just directories"),
|
||||||
|
@ -155,69 +164,66 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.opt_present("help") {
|
if matches.opt_present("help") {
|
||||||
println!("du {} - estimate file space usage", VERSION);
|
println!("{program} {version} - estimate file space usage
|
||||||
println!("");
|
|
||||||
println!("Usage:");
|
Usage
|
||||||
println!(" {0:s} [OPTION]... [FILE]...", program);
|
{program} [OPTION]... [FILE]...
|
||||||
println!(" {0:s} [OPTION]... --files0-from=F", program);
|
{program} [OPTION]... --files0-from=F
|
||||||
println!("");
|
|
||||||
println!("{}", getopts::usage("Summarize disk usage of each FILE, recursively for directories.", opts));
|
{usage}
|
||||||
println!("Display values are in units of the first available SIZE from
|
|
||||||
|
Display values are in units of the first available SIZE from
|
||||||
--block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environ‐
|
--block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environ‐
|
||||||
ment variables. Otherwise, units default to 1024 bytes (or 512 if
|
ment variables. Otherwise, units default to 1024 bytes (or 512 if
|
||||||
POSIXLY_CORRECT is set).
|
POSIXLY_CORRECT is set).
|
||||||
|
|
||||||
SIZE is an integer and optional unit (example: 10M is 10*1024*1024).
|
SIZE is an integer and optional unit (example: 10M is 10*1024*1024).
|
||||||
Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (pow‐
|
Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (pow‐
|
||||||
ers of 1000).");
|
ers of 1000).",
|
||||||
|
program = program,
|
||||||
|
version = VERSION,
|
||||||
|
usage = getopts::usage("Summarize disk usage of each FILE, recursively for directories.", opts));
|
||||||
return
|
return
|
||||||
} else if matches.opt_present("version") {
|
} else if matches.opt_present("version") {
|
||||||
println!("du version: {}", VERSION);
|
println!("{} version: {}", program, VERSION);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let options = Options{
|
let summarize = matches.opt_present("summarize");
|
||||||
|
|
||||||
|
let max_depth_str = matches.opt_str("max-depth");
|
||||||
|
let max_depth = max_depth_str.as_ref().and_then(|s| from_str::<uint>(*s));
|
||||||
|
match (max_depth_str, max_depth) {
|
||||||
|
(Some(ref s), _) if summarize => {
|
||||||
|
println!("{}: warning: summarizing conflicts with --max-depth={:s}", program, *s);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
(Some(ref s), None) => {
|
||||||
|
println!("{}: invalid maximum depth '{:s}'", program, *s);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
(Some(_), Some(_)) | (None, _) => { /* valid */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
let options = Options {
|
||||||
all: matches.opt_present("all"),
|
all: matches.opt_present("all"),
|
||||||
max_depth: match (matches.opt_present("summarize"), matches.opt_str("max-depth")) {
|
program_name: program.to_owned(),
|
||||||
(true, Some(s)) => match from_str::<uint>(s) {
|
max_depth: max_depth,
|
||||||
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"),
|
total: matches.opt_present("total"),
|
||||||
separate_dirs: matches.opt_present("S"),
|
separate_dirs: matches.opt_present("S"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let strs = matches.free.clone();
|
let strs = if matches.free.is_empty() {~[~"./"]} else {matches.free.clone()};
|
||||||
let strs = match strs.is_empty() {
|
|
||||||
true => ~[~"./"],
|
|
||||||
false => strs
|
|
||||||
};
|
|
||||||
|
|
||||||
let options_arc = Arc::new(options);
|
let options_arc = Arc::new(options);
|
||||||
|
|
||||||
let MB = match matches.opt_present("si") {
|
let MB = match matches.opt_present("si") {
|
||||||
true => 1000 * 1000,
|
true => 1000 * 1000,
|
||||||
false => 1024 * 1024,
|
false => 1024 * 1024,
|
||||||
};
|
};
|
||||||
let KB = match matches.opt_present("si") {
|
let KB = match matches.opt_present("si") {
|
||||||
true => 1000,
|
true => 1000,
|
||||||
false => 1024,
|
false => 1024,
|
||||||
};
|
};
|
||||||
|
|
||||||
let block_size = match matches.opt_str("block-size") {
|
let block_size = match matches.opt_str("block-size") {
|
||||||
|
@ -228,7 +234,7 @@ ers of 1000).");
|
||||||
let mut letters = ~[];
|
let mut letters = ~[];
|
||||||
for c in s.chars() {
|
for c in s.chars() {
|
||||||
if found_letter && c.is_digit() || !found_number && !c.is_digit() {
|
if found_letter && c.is_digit() || !found_number && !c.is_digit() {
|
||||||
println!("du: invalid --block-size argument '{}'", s);
|
println!("{}: invalid --block-size argument '{}'", program, s);
|
||||||
return
|
return
|
||||||
} else if c.is_digit() {
|
} else if c.is_digit() {
|
||||||
found_number = true;
|
found_number = true;
|
||||||
|
@ -251,7 +257,7 @@ ers of 1000).");
|
||||||
"ZB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
"ZB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
||||||
"YB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
"YB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
||||||
_ => {
|
_ => {
|
||||||
println!("du: invalid --block-size argument '{}'", s); return
|
println!("{}: invalid --block-size argument '{}'", program, s); return
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
number * multiple
|
number * multiple
|
||||||
|
@ -284,13 +290,12 @@ ers of 1000).");
|
||||||
"long-iso" => "%Y-%m-%d %H:%M",
|
"long-iso" => "%Y-%m-%d %H:%M",
|
||||||
"iso" => "%Y-%m-%d",
|
"iso" => "%Y-%m-%d",
|
||||||
_ => {
|
_ => {
|
||||||
println!("
|
println!("{program}: invalid argument '{}' for 'time style'
|
||||||
du: invalid argument 'awdwa' for 'time style'
|
|
||||||
Valid arguments are:
|
Valid arguments are:
|
||||||
- 'full-iso'
|
- 'full-iso'
|
||||||
- 'long-iso'
|
- 'long-iso'
|
||||||
- 'iso'
|
- 'iso'
|
||||||
Try 'du --help' for more information.");
|
Try '{program} --help' for more information.", s, program = program);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -304,9 +309,10 @@ Try 'du --help' for more information.");
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut grand_total = 0;
|
let mut grand_total = 0;
|
||||||
for path_str in strs.iter() {
|
for path_str in strs.move_iter() {
|
||||||
let path = Path::new(path_str.clone());
|
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() {
|
||||||
|
@ -326,10 +332,10 @@ Try 'du --help' for more information.");
|
||||||
"created" => stat.created,
|
"created" => stat.created,
|
||||||
"modified" => stat.modified,
|
"modified" => stat.modified,
|
||||||
_ => {
|
_ => {
|
||||||
println!("du: invalid argument 'modified' for '--time'
|
println!("{program}: invalid argument 'modified' for '--time'
|
||||||
Valid arguments are:
|
Valid arguments are:
|
||||||
- 'accessed', 'created', 'modified'
|
- 'accessed', 'created', 'modified'
|
||||||
Try 'du --help' for more information.");
|
Try '{program} --help' for more information.", program = program);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -345,7 +351,7 @@ Try 'du --help' for more information.");
|
||||||
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;
|
||||||
|
@ -353,7 +359,7 @@ Try 'du --help' for more information.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue