2014-03-31 16:40:21 +00:00
|
|
|
|
#![crate_id(name="du", vers="1.0.0", author="Derek Chiang")]
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
|
*
|
|
|
|
|
* (c) Derek Chiang <derekchiang93@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-03-31 16:40:21 +00:00
|
|
|
|
#![allow(uppercase_variables)]
|
|
|
|
|
#![feature(macro_rules)]
|
2014-02-19 01:10:32 +00:00
|
|
|
|
|
|
|
|
|
extern crate getopts;
|
2014-04-07 22:43:34 +00:00
|
|
|
|
extern crate libc;
|
2014-02-23 21:31:51 +00:00
|
|
|
|
extern crate time;
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
|
|
|
|
use std::os;
|
2014-03-19 07:45:20 +00:00
|
|
|
|
use std::io::{stderr, fs, FileStat, TypeDirectory};
|
2013-12-08 07:44:32 +00:00
|
|
|
|
use std::option::Option;
|
2013-12-04 02:08:42 +00:00
|
|
|
|
use std::path::Path;
|
2014-06-12 04:29:16 +00:00
|
|
|
|
use std::sync::{Arc, Future};
|
2014-02-23 21:31:51 +00:00
|
|
|
|
use time::Timespec;
|
2014-02-19 01:10:32 +00:00
|
|
|
|
|
2014-02-23 22:17:48 +00:00
|
|
|
|
#[path = "../common/util.rs"]
|
2014-02-19 01:10:32 +00:00
|
|
|
|
mod util;
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
2014-02-19 01:10:32 +00:00
|
|
|
|
static NAME: &'static str = "du";
|
2013-12-04 02:08:42 +00:00
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
|
|
|
|
|
2013-12-08 07:44:32 +00:00
|
|
|
|
struct Options {
|
|
|
|
|
all: bool,
|
2014-05-25 09:20:52 +00:00
|
|
|
|
program_name: String,
|
2013-12-08 07:44:32 +00:00
|
|
|
|
max_depth: Option<uint>,
|
2013-12-09 07:11:11 +00:00
|
|
|
|
total: bool,
|
|
|
|
|
separate_dirs: bool,
|
2013-12-08 07:44:32 +00:00
|
|
|
|
}
|
2013-12-08 06:36:36 +00:00
|
|
|
|
|
2014-05-17 17:25:11 +00:00
|
|
|
|
struct Stat {
|
|
|
|
|
path: Path,
|
|
|
|
|
fstat: FileStat,
|
|
|
|
|
}
|
2014-03-19 07:45:20 +00:00
|
|
|
|
// this takes `my_stat` to avoid having to stat files multiple times.
|
2014-05-17 17:25:11 +00:00
|
|
|
|
fn du(path: &Path, mut my_stat: Stat,
|
|
|
|
|
options: Arc<Options>, depth: uint) -> Vec<Arc<Stat>> {
|
2014-04-26 05:03:08 +00:00
|
|
|
|
let mut stats = vec!();
|
|
|
|
|
let mut futures = vec!();
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
2014-05-17 17:25:11 +00:00
|
|
|
|
if my_stat.fstat.kind == TypeDirectory {
|
2014-03-19 07:45:20 +00:00
|
|
|
|
let read = match fs::readdir(path) {
|
|
|
|
|
Ok(read) => read,
|
|
|
|
|
Err(e) => {
|
2014-03-31 16:40:21 +00:00
|
|
|
|
safe_writeln!(&mut stderr(), "{}: cannot read directory ‘{}‘: {}",
|
|
|
|
|
options.program_name, path.display(), e);
|
2014-04-26 05:03:08 +00:00
|
|
|
|
return vec!(Arc::new(my_stat))
|
2013-12-08 06:36:36 +00:00
|
|
|
|
}
|
2014-03-19 07:45:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for f in read.move_iter() {
|
2014-05-17 17:25:11 +00:00
|
|
|
|
let this_stat = Stat{path: f.clone(), fstat: safe_unwrap!(fs::lstat(&f))};
|
|
|
|
|
if this_stat.fstat.kind == TypeDirectory {
|
2014-03-26 01:37:14 +00:00
|
|
|
|
let oa_clone = options.clone();
|
2014-03-19 07:45:20 +00:00
|
|
|
|
futures.push(Future::spawn(proc() { du(&f, this_stat, oa_clone, depth + 1) }))
|
|
|
|
|
} else {
|
2014-05-17 17:25:11 +00:00
|
|
|
|
my_stat.fstat.size += this_stat.fstat.size;
|
|
|
|
|
my_stat.fstat.unstable.blocks += this_stat.fstat.unstable.blocks;
|
2014-03-19 07:45:20 +00:00
|
|
|
|
if options.all {
|
|
|
|
|
stats.push(Arc::new(this_stat))
|
|
|
|
|
}
|
2013-12-08 06:36:36 +00:00
|
|
|
|
}
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for future in futures.mut_iter() {
|
2014-04-26 05:03:08 +00:00
|
|
|
|
for stat in future.get().move_iter().rev() {
|
2013-12-09 07:11:11 +00:00
|
|
|
|
if !options.separate_dirs && stat.path.dir_path() == my_stat.path {
|
2014-05-17 17:25:11 +00:00
|
|
|
|
my_stat.fstat.size += stat.fstat.size;
|
|
|
|
|
my_stat.fstat.unstable.blocks += stat.fstat.unstable.blocks;
|
2013-12-08 06:36:36 +00:00
|
|
|
|
}
|
2013-12-08 07:44:32 +00:00
|
|
|
|
if options.max_depth == None || depth < options.max_depth.unwrap() {
|
2014-03-26 01:37:14 +00:00
|
|
|
|
stats.push(stat.clone());
|
2013-12-08 07:44:32 +00:00
|
|
|
|
}
|
2013-12-08 06:36:36 +00:00
|
|
|
|
}
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-08 06:36:36 +00:00
|
|
|
|
stats.push(Arc::new(my_stat));
|
|
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
|
stats
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-28 12:01:30 +00:00
|
|
|
|
#[allow(dead_code)]
|
2014-06-08 07:56:37 +00:00
|
|
|
|
fn main() { os::set_exit_status(uumain(os::args())); }
|
2014-05-28 11:43:37 +00:00
|
|
|
|
|
2014-06-08 07:56:37 +00:00
|
|
|
|
pub fn uumain(args: Vec<String>) -> int {
|
2014-05-16 08:32:58 +00:00
|
|
|
|
let program = args.get(0).as_slice();
|
2014-05-30 08:35:54 +00:00
|
|
|
|
let opts = [
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// In task
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("a", "all", " write counts for all files, not just directories"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("", "apparent-size", "print apparent sizes, rather than disk usage;
|
2013-12-09 07:11:11 +00:00
|
|
|
|
although the apparent size is usually smaller, it may be larger due to holes
|
|
|
|
|
in ('sparse') files, internal fragmentation, indirect blocks, and the like"),
|
2013-12-11 10:09:45 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optopt("B", "block-size", "scale sizes by SIZE before printing them.
|
2013-12-11 10:09:45 +00:00
|
|
|
|
E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.",
|
|
|
|
|
"SIZE"),
|
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("c", "total", "produce a grand total"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// In task
|
2014-02-19 01:10:32 +00:00
|
|
|
|
// getopts::optflag("D", "dereference-args", "dereference only symlinks that are listed
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// on the command line"),
|
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
// getopts::optopt("", "files0-from", "summarize disk usage of the NUL-terminated file
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// names specified in file F;
|
|
|
|
|
// If F is - then read names from standard input", "F"),
|
|
|
|
|
// // In task
|
2014-02-19 01:10:32 +00:00
|
|
|
|
// getopts::optflag("H", "", "equivalent to --dereference-args (-D)"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("", "si", "like -h, but use powers of 1000 not 1024"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("k", "", "like --block-size=1K"),
|
2013-12-11 10:09:45 +00:00
|
|
|
|
// In task
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("l", "count-links", "count sizes many times if hard linked"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// // In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("m", "", "like --block-size=1M"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// // In task
|
2014-02-19 01:10:32 +00:00
|
|
|
|
// getopts::optflag("L", "dereference", "dereference all symbolic links"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// // In task
|
2014-02-19 01:10:32 +00:00
|
|
|
|
// getopts::optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// // In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("0", "null", "end each output line with 0 byte rather than newline"),
|
2013-12-11 10:09:45 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("S", "separate-dirs", "do not include size of subdirectories"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflag("s", "summarize", "display only a total for each argument"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// // In task
|
2014-02-19 01:10:32 +00:00
|
|
|
|
// getopts::optflag("x", "one-file-system", "skip directories on different file systems"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// // In task
|
2014-02-19 01:10:32 +00:00
|
|
|
|
// getopts::optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
// // In task
|
2014-02-19 01:10:32 +00:00
|
|
|
|
// getopts::optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"),
|
2013-12-11 10:09:45 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optopt("d", "max-depth", "print the total for a directory (or file, with --all)
|
2013-12-08 06:36:36 +00:00
|
|
|
|
only if it is N or fewer levels below the command
|
|
|
|
|
line argument; --max-depth=0 is the same as --summarize", "N"),
|
2013-12-11 10:09:45 +00:00
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optflagopt("", "time", "show time of the last modification of any file in the
|
2013-12-11 23:20:52 +00:00
|
|
|
|
directory, or any of its subdirectories. If WORD is given, show time as WORD instead of modification time:
|
2013-12-11 10:09:45 +00:00
|
|
|
|
atime, access, use, ctime or status", "WORD"),
|
|
|
|
|
// In main
|
2014-02-19 01:10:32 +00:00
|
|
|
|
getopts::optopt("", "time-style", "show times using style STYLE:
|
2013-12-11 10:09:45 +00:00
|
|
|
|
full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE"),
|
2014-02-19 05:59:12 +00:00
|
|
|
|
getopts::optflag("", "help", "display this help and exit"),
|
2014-02-10 04:56:13 +00:00
|
|
|
|
getopts::optflag("V", "version", "output version information and exit"),
|
2013-12-04 02:08:42 +00:00
|
|
|
|
];
|
|
|
|
|
|
2014-02-19 01:10:32 +00:00
|
|
|
|
let matches = match getopts::getopts(args.tail(), opts) {
|
2013-12-04 02:08:42 +00:00
|
|
|
|
Ok(m) => m,
|
|
|
|
|
Err(f) => {
|
2014-06-15 10:50:40 +00:00
|
|
|
|
show_error!("Invalid options\n{}", f);
|
2014-06-09 01:49:06 +00:00
|
|
|
|
return 1;
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if matches.opt_present("help") {
|
2014-03-19 06:57:35 +00:00
|
|
|
|
println!("{program} {version} - estimate file space usage
|
|
|
|
|
|
|
|
|
|
Usage
|
|
|
|
|
{program} [OPTION]... [FILE]...
|
|
|
|
|
{program} [OPTION]... --files0-from=F
|
|
|
|
|
|
|
|
|
|
{usage}
|
|
|
|
|
|
|
|
|
|
Display values are in units of the first available SIZE from
|
2013-12-08 06:36:36 +00:00
|
|
|
|
--block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environ‐
|
|
|
|
|
ment variables. Otherwise, units default to 1024 bytes (or 512 if
|
|
|
|
|
POSIXLY_CORRECT is set).
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
2013-12-08 06:36:36 +00:00
|
|
|
|
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‐
|
2014-03-19 06:57:35 +00:00
|
|
|
|
ers of 1000).",
|
|
|
|
|
program = program,
|
|
|
|
|
version = VERSION,
|
|
|
|
|
usage = getopts::usage("Summarize disk usage of each FILE, recursively for directories.", opts));
|
2014-06-08 07:56:37 +00:00
|
|
|
|
return 0;
|
2014-02-09 11:00:41 +00:00
|
|
|
|
} else if matches.opt_present("version") {
|
2014-03-19 06:57:35 +00:00
|
|
|
|
println!("{} version: {}", program, VERSION);
|
2014-06-08 07:56:37 +00:00
|
|
|
|
return 0;
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-19 06:56:40 +00:00
|
|
|
|
let summarize = matches.opt_present("summarize");
|
|
|
|
|
|
|
|
|
|
let max_depth_str = matches.opt_str("max-depth");
|
2014-05-17 17:25:11 +00:00
|
|
|
|
let max_depth = max_depth_str.as_ref().and_then(|s| from_str::<uint>(s.as_slice()));
|
2014-03-19 06:56:40 +00:00
|
|
|
|
match (max_depth_str, max_depth) {
|
|
|
|
|
(Some(ref s), _) if summarize => {
|
|
|
|
|
println!("{}: warning: summarizing conflicts with --max-depth={:s}", program, *s);
|
2014-06-08 07:56:37 +00:00
|
|
|
|
return 0;
|
2014-03-19 06:56:40 +00:00
|
|
|
|
}
|
|
|
|
|
(Some(ref s), None) => {
|
|
|
|
|
println!("{}: invalid maximum depth '{:s}'", program, *s);
|
2014-06-08 07:56:37 +00:00
|
|
|
|
return 0;
|
2014-03-19 06:56:40 +00:00
|
|
|
|
}
|
|
|
|
|
(Some(_), Some(_)) | (None, _) => { /* valid */ }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let options = Options {
|
2013-12-08 07:44:32 +00:00
|
|
|
|
all: matches.opt_present("all"),
|
2014-05-28 06:33:39 +00:00
|
|
|
|
program_name: program.to_string(),
|
2014-03-19 06:56:40 +00:00
|
|
|
|
max_depth: max_depth,
|
2013-12-09 07:11:11 +00:00
|
|
|
|
total: matches.opt_present("total"),
|
|
|
|
|
separate_dirs: matches.opt_present("S"),
|
2013-12-08 07:44:32 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-05-28 06:33:39 +00:00
|
|
|
|
let strs = if matches.free.is_empty() {vec!("./".to_string())} else {matches.free.clone()};
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
2013-12-08 07:44:32 +00:00
|
|
|
|
let options_arc = Arc::new(options);
|
2013-12-08 06:36:36 +00:00
|
|
|
|
|
2013-12-09 07:11:11 +00:00
|
|
|
|
let MB = match matches.opt_present("si") {
|
|
|
|
|
true => 1000 * 1000,
|
2014-03-19 06:57:35 +00:00
|
|
|
|
false => 1024 * 1024,
|
2013-12-09 07:11:11 +00:00
|
|
|
|
};
|
|
|
|
|
let KB = match matches.opt_present("si") {
|
|
|
|
|
true => 1000,
|
2014-03-19 06:57:35 +00:00
|
|
|
|
false => 1024,
|
2013-12-09 07:11:11 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-12-11 10:09:45 +00:00
|
|
|
|
let block_size = match matches.opt_str("block-size") {
|
|
|
|
|
Some(s) => {
|
|
|
|
|
let mut found_number = false;
|
|
|
|
|
let mut found_letter = false;
|
2014-04-26 05:03:08 +00:00
|
|
|
|
let mut numbers = vec!();
|
|
|
|
|
let mut letters = vec!();
|
2014-05-17 17:25:11 +00:00
|
|
|
|
for c in s.as_slice().chars() {
|
2013-12-11 10:09:45 +00:00
|
|
|
|
if found_letter && c.is_digit() || !found_number && !c.is_digit() {
|
2014-03-19 06:57:35 +00:00
|
|
|
|
println!("{}: invalid --block-size argument '{}'", program, s);
|
2014-06-08 07:56:37 +00:00
|
|
|
|
return 0;
|
2013-12-11 10:09:45 +00:00
|
|
|
|
} else if c.is_digit() {
|
|
|
|
|
found_number = true;
|
|
|
|
|
numbers.push(c as u8);
|
|
|
|
|
} else if c.is_alphabetic() {
|
|
|
|
|
found_letter = true;
|
|
|
|
|
letters.push(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-26 05:03:08 +00:00
|
|
|
|
let number = std::uint::parse_bytes(numbers.as_slice(), 10).unwrap();
|
|
|
|
|
let multiple = match std::str::from_chars(letters.as_slice()).as_slice() {
|
2013-12-11 10:09:45 +00:00
|
|
|
|
"K" => 1024, "M" => 1024 * 1024, "G" => 1024 * 1024 * 1024,
|
|
|
|
|
"T" => 1024 * 1024 * 1024 * 1024, "P" => 1024 * 1024 * 1024 * 1024 * 1024,
|
|
|
|
|
"E" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
|
|
|
|
|
"Z" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
|
|
|
|
|
"Y" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
|
|
|
|
|
"KB" => 1000, "MB" => 1000 * 1000, "GB" => 1000 * 1000 * 1000,
|
|
|
|
|
"TB" => 1000 * 1000 * 1000 * 1000, "PB" => 1000 * 1000 * 1000 * 1000 * 1000,
|
|
|
|
|
"EB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
|
|
|
|
"ZB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
|
|
|
|
"YB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
|
|
|
|
_ => {
|
2014-06-08 07:56:37 +00:00
|
|
|
|
println!("{}: invalid --block-size argument '{}'", program, s); return 0;
|
2013-12-11 10:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
number * multiple
|
|
|
|
|
},
|
|
|
|
|
None => 1024
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-25 09:20:52 +00:00
|
|
|
|
let convert_size = |size: u64| -> String {
|
2013-12-09 07:11:11 +00:00
|
|
|
|
if matches.opt_present("human-readable") || matches.opt_present("si") {
|
2013-12-08 09:22:09 +00:00
|
|
|
|
if size > MB {
|
2013-12-11 10:09:45 +00:00
|
|
|
|
format!("{:.1f}M", (size as f64) / (MB as f64))
|
2013-12-08 09:22:09 +00:00
|
|
|
|
} else if size > KB {
|
2013-12-11 10:09:45 +00:00
|
|
|
|
format!("{:.1f}K", (size as f64) / (KB as f64))
|
2013-12-08 09:22:09 +00:00
|
|
|
|
} else {
|
|
|
|
|
format!("{}B", size)
|
|
|
|
|
}
|
|
|
|
|
} else if matches.opt_present("k") {
|
|
|
|
|
format!("{}", ((size as f64) / (KB as f64)).ceil())
|
|
|
|
|
} else if matches.opt_present("m") {
|
|
|
|
|
format!("{}", ((size as f64) / (MB as f64)).ceil())
|
|
|
|
|
} else {
|
2013-12-11 10:09:45 +00:00
|
|
|
|
format!("{}", ((size as f64) / (block_size as f64)).ceil())
|
2013-12-08 09:22:09 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-12-11 10:09:45 +00:00
|
|
|
|
let time_format_str = match matches.opt_str("time-style") {
|
|
|
|
|
Some(s) => {
|
|
|
|
|
match s.as_slice() {
|
2013-12-11 23:20:52 +00:00
|
|
|
|
"full-iso" => "%Y-%m-%d %H:%M:%S.%f %z",
|
2013-12-11 10:09:45 +00:00
|
|
|
|
"long-iso" => "%Y-%m-%d %H:%M",
|
|
|
|
|
"iso" => "%Y-%m-%d",
|
|
|
|
|
_ => {
|
2014-03-19 06:57:35 +00:00
|
|
|
|
println!("{program}: invalid argument '{}' for 'time style'
|
2013-12-11 10:09:45 +00:00
|
|
|
|
Valid arguments are:
|
|
|
|
|
- 'full-iso'
|
|
|
|
|
- 'long-iso'
|
|
|
|
|
- 'iso'
|
2014-03-19 06:57:35 +00:00
|
|
|
|
Try '{program} --help' for more information.", s, program = program);
|
2014-06-08 07:56:37 +00:00
|
|
|
|
return 0;
|
2013-12-11 10:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => "%Y-%m-%d %H:%M"
|
|
|
|
|
};
|
|
|
|
|
|
2013-12-09 07:11:11 +00:00
|
|
|
|
let line_separator = match matches.opt_present("0") {
|
|
|
|
|
true => "\0",
|
|
|
|
|
false => "\n",
|
|
|
|
|
};
|
|
|
|
|
|
2013-12-08 07:44:32 +00:00
|
|
|
|
let mut grand_total = 0;
|
2014-03-19 07:12:43 +00:00
|
|
|
|
for path_str in strs.move_iter() {
|
|
|
|
|
let path = Path::new(path_str);
|
2014-03-19 07:45:20 +00:00
|
|
|
|
let stat = safe_unwrap!(fs::lstat(&path));
|
2014-05-17 17:25:11 +00:00
|
|
|
|
let iter = du(&path, Stat{path: path.clone(), fstat: stat}, options_arc.clone(), 0).move_iter();
|
2013-12-08 07:44:32 +00:00
|
|
|
|
let (_, len) = iter.size_hint();
|
|
|
|
|
let len = len.unwrap();
|
2014-03-26 01:37:14 +00:00
|
|
|
|
for (index, stat) in iter.enumerate() {
|
2013-12-09 07:11:11 +00:00
|
|
|
|
let size = match matches.opt_present("apparent-size") {
|
2014-05-17 17:25:11 +00:00
|
|
|
|
true => stat.fstat.unstable.nlink * stat.fstat.size,
|
2013-12-09 07:11:11 +00:00
|
|
|
|
// C's stat is such that each block is assume to be 512 bytes
|
|
|
|
|
// See: http://linux.die.net/man/2/stat
|
2014-05-17 17:25:11 +00:00
|
|
|
|
false => stat.fstat.unstable.blocks * 512,
|
2013-12-09 07:11:11 +00:00
|
|
|
|
};
|
2013-12-11 10:09:45 +00:00
|
|
|
|
if matches.opt_present("time") {
|
|
|
|
|
let time_str = {
|
|
|
|
|
let (secs, nsecs) = {
|
|
|
|
|
let time = match matches.opt_str("time") {
|
|
|
|
|
Some(s) => match s.as_slice() {
|
2014-05-17 17:25:11 +00:00
|
|
|
|
"accessed" => stat.fstat.accessed,
|
|
|
|
|
"created" => stat.fstat.created,
|
|
|
|
|
"modified" => stat.fstat.modified,
|
2013-12-11 10:09:45 +00:00
|
|
|
|
_ => {
|
2014-03-19 06:57:35 +00:00
|
|
|
|
println!("{program}: invalid argument 'modified' for '--time'
|
2013-12-11 10:09:45 +00:00
|
|
|
|
Valid arguments are:
|
|
|
|
|
- 'accessed', 'created', 'modified'
|
2014-03-19 06:57:35 +00:00
|
|
|
|
Try '{program} --help' for more information.", program = program);
|
2014-06-08 07:56:37 +00:00
|
|
|
|
return 0;
|
2013-12-11 10:09:45 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2014-05-17 17:25:11 +00:00
|
|
|
|
None => stat.fstat.modified
|
2013-12-11 10:09:45 +00:00
|
|
|
|
};
|
2013-12-11 23:20:52 +00:00
|
|
|
|
((time / 1000) as i64, (time % 1000 * 1000000) as i32)
|
2013-12-11 10:09:45 +00:00
|
|
|
|
};
|
|
|
|
|
let time_spec = Timespec::new(secs, nsecs);
|
2014-02-23 21:31:51 +00:00
|
|
|
|
time::at(time_spec).strftime(time_format_str)
|
2013-12-11 10:09:45 +00:00
|
|
|
|
};
|
2014-06-17 14:51:08 +00:00
|
|
|
|
if !summarize || (summarize && index == len-1) {
|
|
|
|
|
print!("{:<10} {:<30} {}{}", convert_size(size), time_str, stat.path.display(), line_separator);
|
|
|
|
|
}
|
2013-12-11 10:09:45 +00:00
|
|
|
|
} else {
|
2014-06-17 14:51:08 +00:00
|
|
|
|
if !summarize || (summarize && index == len-1) {
|
|
|
|
|
print!("{:<10} {}{}", convert_size(size), stat.path.display(), line_separator);
|
|
|
|
|
}
|
2013-12-11 10:09:45 +00:00
|
|
|
|
}
|
2014-03-26 01:37:14 +00:00
|
|
|
|
if options_arc.total && index == (len - 1) {
|
2013-12-08 07:44:32 +00:00
|
|
|
|
// The last element will be the total size of the the path under
|
|
|
|
|
// path_str. We add it to the grand total.
|
2013-12-09 07:11:11 +00:00
|
|
|
|
grand_total += size;
|
2013-12-08 07:44:32 +00:00
|
|
|
|
}
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-08 07:44:32 +00:00
|
|
|
|
|
2014-03-26 01:37:14 +00:00
|
|
|
|
if options_arc.total {
|
2013-12-09 07:11:11 +00:00
|
|
|
|
print!("{:<10} total", convert_size(grand_total));
|
2014-02-19 01:10:32 +00:00
|
|
|
|
print!("{}", line_separator);
|
2013-12-08 07:44:32 +00:00
|
|
|
|
}
|
2014-06-08 07:56:37 +00:00
|
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
|
0
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|