2015-12-08 02:42:08 +00:00
|
|
|
|
#![crate_name = "uu_du"]
|
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-02-23 21:31:51 +00:00
|
|
|
|
extern crate time;
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
2015-11-24 01:00:51 +00:00
|
|
|
|
#[macro_use]
|
|
|
|
|
extern crate uucore;
|
|
|
|
|
|
2018-04-20 08:26:29 +00:00
|
|
|
|
// XXX: remove when we no longer support 1.22.0
|
|
|
|
|
use std::ascii::AsciiExt;
|
2018-04-03 11:44:53 +00:00
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
use std::env;
|
2015-05-14 22:35:55 +00:00
|
|
|
|
use std::fs;
|
2018-03-14 20:26:22 +00:00
|
|
|
|
use std::io::{stderr, Result, Write};
|
2018-04-03 11:44:53 +00:00
|
|
|
|
use std::iter;
|
2015-05-14 22:35:55 +00:00
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
|
|
|
|
use std::path::PathBuf;
|
2014-02-23 21:31:51 +00:00
|
|
|
|
use time::Timespec;
|
2014-02-19 01:10:32 +00:00
|
|
|
|
|
2017-12-10 17:52:22 +00:00
|
|
|
|
const NAME: &'static str = "du";
|
|
|
|
|
const SUMMARY: &'static str = "estimate file space usage";
|
|
|
|
|
const LONG_HELP: &'static str = "
|
2016-08-14 01:37:48 +00:00
|
|
|
|
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
|
2018-03-22 16:05:33 +00:00
|
|
|
|
POSIXLY_CORRECT is set).
|
2016-08-14 01:37:48 +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‐
|
|
|
|
|
ers of 1000).
|
2017-12-10 17:52:22 +00:00
|
|
|
|
";
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
2018-03-22 17:15:44 +00:00
|
|
|
|
// TODO: Suport Z & Y (currently limited by size of u64)
|
2018-04-20 10:56:47 +00:00
|
|
|
|
const UNITS: [(char, u32); 6] = [('E', 6), ('P', 5), ('T', 4), ('G', 3), ('M', 2), ('K', 1)];
|
2018-03-22 17:15:44 +00:00
|
|
|
|
|
2013-12-08 07:44:32 +00:00
|
|
|
|
struct Options {
|
|
|
|
|
all: bool,
|
2014-05-25 09:20:52 +00:00
|
|
|
|
program_name: String,
|
2015-01-10 18:07:08 +00:00
|
|
|
|
max_depth: Option<usize>,
|
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 {
|
2015-05-14 22:35:55 +00:00
|
|
|
|
path: PathBuf,
|
|
|
|
|
is_dir: bool,
|
|
|
|
|
size: u64,
|
|
|
|
|
blocks: u64,
|
|
|
|
|
nlink: u64,
|
2018-03-20 20:40:27 +00:00
|
|
|
|
inode: u64,
|
2015-05-14 22:35:55 +00:00
|
|
|
|
created: u64,
|
|
|
|
|
accessed: u64,
|
|
|
|
|
modified: u64,
|
2014-05-17 17:25:11 +00:00
|
|
|
|
}
|
2015-05-14 22:35:55 +00:00
|
|
|
|
|
|
|
|
|
impl Stat {
|
2018-03-14 20:26:22 +00:00
|
|
|
|
fn new(path: PathBuf) -> Result<Stat> {
|
2018-03-15 18:56:21 +00:00
|
|
|
|
let metadata = fs::symlink_metadata(&path)?;
|
|
|
|
|
Ok(Stat {
|
|
|
|
|
path: path,
|
|
|
|
|
is_dir: metadata.is_dir(),
|
|
|
|
|
size: metadata.len(),
|
|
|
|
|
blocks: metadata.blocks() as u64,
|
|
|
|
|
nlink: metadata.nlink() as u64,
|
2018-03-20 20:40:27 +00:00
|
|
|
|
inode: metadata.ino() as u64,
|
2018-03-15 18:56:21 +00:00
|
|
|
|
created: metadata.mtime() as u64,
|
|
|
|
|
accessed: metadata.atime() as u64,
|
|
|
|
|
modified: metadata.mtime() as u64,
|
|
|
|
|
})
|
2015-05-14 22:35:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 07:54:49 +00:00
|
|
|
|
fn unit_string_to_number(s: &str) -> Option<u64> {
|
|
|
|
|
let mut offset = 0;
|
|
|
|
|
let mut s_chars = s.chars().rev();
|
|
|
|
|
|
|
|
|
|
let (mut ch, multiple) = match s_chars.next() {
|
|
|
|
|
Some('B') | Some('b') => ('B', 1000u64),
|
|
|
|
|
Some(ch) => (ch, 1024u64),
|
|
|
|
|
None => return None,
|
|
|
|
|
};
|
|
|
|
|
if ch == 'B' {
|
|
|
|
|
ch = s_chars.next()?;
|
|
|
|
|
offset += 1;
|
|
|
|
|
}
|
2018-04-20 08:26:29 +00:00
|
|
|
|
ch = ch.to_ascii_uppercase();
|
2018-04-20 07:54:49 +00:00
|
|
|
|
|
|
|
|
|
let unit = UNITS
|
|
|
|
|
.iter()
|
2018-04-20 10:56:47 +00:00
|
|
|
|
.rev()
|
2018-04-20 08:26:29 +00:00
|
|
|
|
.find(|&&(unit_ch, _)| unit_ch == ch)
|
|
|
|
|
.map(|&(_, val)| {
|
2018-04-20 07:54:49 +00:00
|
|
|
|
// we found a match, so increment offset
|
|
|
|
|
offset += 1;
|
2018-04-20 08:26:29 +00:00
|
|
|
|
val
|
2018-04-20 07:54:49 +00:00
|
|
|
|
})
|
|
|
|
|
.or_else(|| if multiple == 1024 { Some(0) } else { None })?;
|
|
|
|
|
|
|
|
|
|
let number = s[..s.len() - offset].parse::<u64>().ok()?;
|
|
|
|
|
|
|
|
|
|
Some(number * multiple.pow(unit))
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 11:44:53 +00:00
|
|
|
|
fn translate_to_pure_number(s: &Option<String>) -> Option<u64> {
|
|
|
|
|
match s {
|
2018-04-20 07:54:49 +00:00
|
|
|
|
&Some(ref s) => unit_string_to_number(s),
|
2018-04-03 11:44:53 +00:00
|
|
|
|
&None => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_block_size(s: Option<String>) -> u64 {
|
|
|
|
|
match translate_to_pure_number(&s) {
|
|
|
|
|
Some(v) => v,
|
|
|
|
|
None => {
|
2018-04-20 07:54:49 +00:00
|
|
|
|
if let Some(value) = s {
|
|
|
|
|
show_error!("invalid --block-size argument '{}'", value);
|
2018-04-03 11:44:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-04-20 07:54:49 +00:00
|
|
|
|
for env_var in &["DU_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
|
|
|
|
|
if let Some(quantity) = translate_to_pure_number(&env::var(env_var).ok()) {
|
|
|
|
|
return quantity;
|
2018-04-03 11:44:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if env::var("POSIXLY_CORRECT").is_ok() {
|
|
|
|
|
512
|
|
|
|
|
} else {
|
|
|
|
|
1024
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-20 16:10:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-19 07:45:20 +00:00
|
|
|
|
// this takes `my_stat` to avoid having to stat files multiple times.
|
2017-12-10 17:52:22 +00:00
|
|
|
|
// XXX: this should use the impl Trait return type when it is stabilized
|
2018-03-20 20:40:27 +00:00
|
|
|
|
fn du(
|
|
|
|
|
mut my_stat: Stat,
|
|
|
|
|
options: &Options,
|
|
|
|
|
depth: usize,
|
|
|
|
|
inodes: &mut HashSet<u64>,
|
|
|
|
|
) -> Box<DoubleEndedIterator<Item = Stat>> {
|
2018-03-12 08:20:58 +00:00
|
|
|
|
let mut stats = vec![];
|
|
|
|
|
let mut futures = vec![];
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
2015-05-14 22:35:55 +00:00
|
|
|
|
if my_stat.is_dir {
|
2017-12-10 17:52:22 +00:00
|
|
|
|
let read = match fs::read_dir(&my_stat.path) {
|
2014-03-19 07:45:20 +00:00
|
|
|
|
Ok(read) => read,
|
|
|
|
|
Err(e) => {
|
2018-03-12 08:20:58 +00:00
|
|
|
|
safe_writeln!(
|
|
|
|
|
stderr(),
|
|
|
|
|
"{}: cannot read directory ‘{}‘: {}",
|
|
|
|
|
options.program_name,
|
|
|
|
|
my_stat.path.display(),
|
|
|
|
|
e
|
|
|
|
|
);
|
|
|
|
|
return Box::new(iter::once(my_stat));
|
2013-12-08 06:36:36 +00:00
|
|
|
|
}
|
2014-03-19 07:45:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-09-17 15:11:39 +00:00
|
|
|
|
for f in read.into_iter() {
|
2018-03-13 19:45:23 +00:00
|
|
|
|
match f {
|
2018-04-20 07:54:49 +00:00
|
|
|
|
Ok(entry) => {
|
|
|
|
|
match Stat::new(entry.path()) {
|
|
|
|
|
Ok(this_stat) => {
|
|
|
|
|
if this_stat.is_dir {
|
|
|
|
|
futures.push(du(this_stat, options, depth + 1, inodes));
|
|
|
|
|
} else {
|
|
|
|
|
if inodes.contains(&this_stat.inode) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
inodes.insert(this_stat.inode);
|
|
|
|
|
my_stat.size += this_stat.size;
|
|
|
|
|
my_stat.blocks += this_stat.blocks;
|
|
|
|
|
if options.all {
|
|
|
|
|
stats.push(this_stat);
|
|
|
|
|
}
|
2018-03-13 19:45:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-20 07:54:49 +00:00
|
|
|
|
Err(error) => show_error!("{}", error),
|
2018-03-13 19:45:23 +00:00
|
|
|
|
}
|
2018-04-20 07:54:49 +00:00
|
|
|
|
}
|
2018-03-13 19:45:23 +00:00
|
|
|
|
Err(error) => show_error!("{}", error),
|
2013-12-08 06:36:36 +00:00
|
|
|
|
}
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 07:54:49 +00:00
|
|
|
|
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() {
|
|
|
|
|
Some(stat)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
));
|
2017-12-10 17:52:22 +00:00
|
|
|
|
stats.push(my_stat);
|
|
|
|
|
Box::new(stats.into_iter())
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 10:56:47 +00:00
|
|
|
|
fn convert_size_human(size: u64, multiplier: u64, _block_size: u64) -> String {
|
|
|
|
|
for &(unit, power) in &UNITS {
|
|
|
|
|
let limit = multiplier.pow(power);
|
|
|
|
|
if size >= limit {
|
|
|
|
|
return format!("{:.1}{}", (size as f64) / (limit as f64), unit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
format!("{}B", size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn convert_size_k(size: u64, multiplier: u64, _block_size: u64) -> String {
|
|
|
|
|
format!("{}", ((size as f64) / (multiplier as f64)).ceil())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn convert_size_m(size: u64, multiplier: u64, _block_size: u64) -> String {
|
|
|
|
|
format!("{}", ((size as f64) / ((multiplier * multiplier) as f64)).ceil())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn convert_size_other(size: u64, _multiplier: u64, block_size: u64) -> String {
|
|
|
|
|
format!("{}", ((size as f64) / (block_size as f64)).ceil())
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2018-03-12 08:20:58 +00:00
|
|
|
|
let syntax = format!(
|
|
|
|
|
"[OPTION]... [FILE]...
|
|
|
|
|
{0} [OPTION]... --files0-from=F",
|
|
|
|
|
NAME
|
|
|
|
|
);
|
2016-08-14 01:37:48 +00:00
|
|
|
|
let matches = new_coreopts!(&syntax, SUMMARY, LONG_HELP)
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In task
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("a", "all", " write counts for all files, not just directories")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.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
|
2016-08-14 01:37:48 +00:00
|
|
|
|
in ('sparse') files, internal fragmentation, indirect blocks, and the like")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.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.",
|
2016-08-14 01:37:48 +00:00
|
|
|
|
"SIZE")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("c", "total", "produce a grand total")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In task
|
|
|
|
|
// opts.optflag("D", "dereference-args", "dereference only symlinks that are listed
|
|
|
|
|
// on the command line"),
|
|
|
|
|
// In main
|
|
|
|
|
// opts.optopt("", "files0-from", "summarize disk usage of the NUL-terminated file
|
|
|
|
|
// names specified in file F;
|
|
|
|
|
// If F is - then read names from standard input", "F"),
|
|
|
|
|
// // In task
|
|
|
|
|
// opts.optflag("H", "", "equivalent to --dereference-args (-D)"),
|
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("", "si", "like -h, but use powers of 1000 not 1024")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("k", "", "like --block-size=1K")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In task
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("l", "count-links", "count sizes many times if hard linked")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// // In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("m", "", "like --block-size=1M")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// // In task
|
|
|
|
|
// opts.optflag("L", "dereference", "dereference all symbolic links"),
|
|
|
|
|
// // In task
|
|
|
|
|
// opts.optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"),
|
|
|
|
|
// // In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("0", "null", "end each output line with 0 byte rather than newline")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("S", "separate-dirs", "do not include size of subdirectories")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflag("s", "summarize", "display only a total for each argument")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// // In task
|
|
|
|
|
// opts.optflag("x", "one-file-system", "skip directories on different file systems"),
|
|
|
|
|
// // In task
|
|
|
|
|
// opts.optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"),
|
|
|
|
|
// // In task
|
|
|
|
|
// opts.optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"),
|
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.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
|
2016-08-14 01:37:48 +00:00
|
|
|
|
line argument; --max-depth=0 is the same as --summarize", "N")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optflagopt("", "time", "show time of the last modification of any file in the
|
2018-03-12 08:20:58 +00:00
|
|
|
|
directory, or any of its subdirectories. If WORD is given, show time as WORD instead
|
|
|
|
|
of modification time: atime, access, use, ctime or status", "WORD")
|
2015-05-22 01:31:21 +00:00
|
|
|
|
// In main
|
2016-08-14 01:37:48 +00:00
|
|
|
|
.optopt("", "time-style", "show times using style STYLE:
|
|
|
|
|
full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE")
|
|
|
|
|
.parse(args);
|
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");
|
2015-02-03 21:19:13 +00:00
|
|
|
|
let max_depth = max_depth_str.as_ref().and_then(|s| s.parse::<usize>().ok());
|
2014-03-19 06:56:40 +00:00
|
|
|
|
match (max_depth_str, max_depth) {
|
|
|
|
|
(Some(ref s), _) if summarize => {
|
2014-11-21 09:09:43 +00:00
|
|
|
|
show_error!("summarizing conflicts with --max-depth={}", *s);
|
2014-06-19 13:16:27 +00:00
|
|
|
|
return 1;
|
2014-03-19 06:56:40 +00:00
|
|
|
|
}
|
|
|
|
|
(Some(ref s), None) => {
|
2014-11-21 09:09:43 +00:00
|
|
|
|
show_error!("invalid maximum depth '{}'", *s);
|
2014-06-19 13:16:27 +00:00
|
|
|
|
return 1;
|
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"),
|
2016-01-05 19:42:52 +00:00
|
|
|
|
program_name: NAME.to_owned(),
|
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
|
|
|
|
};
|
|
|
|
|
|
2018-03-12 08:20:58 +00:00
|
|
|
|
let strs = if matches.free.is_empty() {
|
|
|
|
|
vec!["./".to_owned()]
|
|
|
|
|
} else {
|
|
|
|
|
matches.free.clone()
|
|
|
|
|
};
|
2013-12-04 02:08:42 +00:00
|
|
|
|
|
2018-04-03 11:44:53 +00:00
|
|
|
|
let block_size = read_block_size(matches.opt_str("block-size"));
|
2013-12-11 10:09:45 +00:00
|
|
|
|
|
2018-04-20 10:56:47 +00:00
|
|
|
|
let multiplier: u64 = if matches.opt_present("si") {
|
|
|
|
|
1000
|
|
|
|
|
} else {
|
|
|
|
|
1024
|
|
|
|
|
};
|
|
|
|
|
let convert_size_fn = {
|
2013-12-09 07:11:11 +00:00
|
|
|
|
if matches.opt_present("human-readable") || matches.opt_present("si") {
|
2018-04-20 10:56:47 +00:00
|
|
|
|
convert_size_human
|
2013-12-08 09:22:09 +00:00
|
|
|
|
} else if matches.opt_present("k") {
|
2018-04-20 10:56:47 +00:00
|
|
|
|
convert_size_k
|
2013-12-08 09:22:09 +00:00
|
|
|
|
} else if matches.opt_present("m") {
|
2018-04-20 10:56:47 +00:00
|
|
|
|
convert_size_m
|
2013-12-08 09:22:09 +00:00
|
|
|
|
} else {
|
2018-04-20 10:56:47 +00:00
|
|
|
|
convert_size_other
|
2013-12-08 09:22:09 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2018-04-20 10:56:47 +00:00
|
|
|
|
let convert_size = |size| convert_size_fn(size, multiplier, block_size);
|
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") {
|
2018-04-20 07:54:49 +00:00
|
|
|
|
Some(s) => {
|
|
|
|
|
match &s[..] {
|
|
|
|
|
"full-iso" => "%Y-%m-%d %H:%M:%S.%f %z",
|
|
|
|
|
"long-iso" => "%Y-%m-%d %H:%M",
|
|
|
|
|
"iso" => "%Y-%m-%d",
|
|
|
|
|
_ => {
|
|
|
|
|
show_error!(
|
|
|
|
|
"invalid argument '{}' for 'time style'
|
2013-12-11 10:09:45 +00:00
|
|
|
|
Valid arguments are:
|
|
|
|
|
- 'full-iso'
|
|
|
|
|
- 'long-iso'
|
|
|
|
|
- 'iso'
|
2018-03-12 08:20:58 +00:00
|
|
|
|
Try '{} --help' for more information.",
|
2018-04-20 07:54:49 +00:00
|
|
|
|
s,
|
|
|
|
|
NAME
|
|
|
|
|
);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2013-12-11 10:09:45 +00:00
|
|
|
|
}
|
2018-04-20 07:54:49 +00:00
|
|
|
|
}
|
2018-03-12 08:20:58 +00:00
|
|
|
|
None => "%Y-%m-%d %H:%M",
|
2013-12-11 10:09:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-03-12 08:20:58 +00:00
|
|
|
|
let line_separator = if matches.opt_present("0") { "\0" } else { "\n" };
|
2013-12-09 07:11:11 +00:00
|
|
|
|
|
2013-12-08 07:44:32 +00:00
|
|
|
|
let mut grand_total = 0;
|
2014-09-17 15:11:39 +00:00
|
|
|
|
for path_str in strs.into_iter() {
|
2018-03-20 16:10:05 +00:00
|
|
|
|
let path = PathBuf::from(&path_str);
|
2018-03-14 20:26:22 +00:00
|
|
|
|
match Stat::new(path) {
|
|
|
|
|
Ok(stat) => {
|
2018-03-20 20:40:27 +00:00
|
|
|
|
let mut inodes: HashSet<u64> = HashSet::new();
|
|
|
|
|
|
|
|
|
|
let iter = du(stat, &options, 0, &mut inodes).into_iter();
|
2018-03-14 20:26:22 +00:00
|
|
|
|
let (_, len) = iter.size_hint();
|
|
|
|
|
let len = len.unwrap();
|
|
|
|
|
for (index, stat) in iter.enumerate() {
|
|
|
|
|
let size = if matches.opt_present("apparent-size") {
|
|
|
|
|
stat.nlink * stat.size
|
|
|
|
|
} else {
|
|
|
|
|
// C's stat is such that each block is assume to be 512 bytes
|
|
|
|
|
// See: http://linux.die.net/man/2/stat
|
|
|
|
|
stat.blocks * 512
|
|
|
|
|
};
|
|
|
|
|
if matches.opt_present("time") {
|
|
|
|
|
let tm = {
|
|
|
|
|
let (secs, nsecs) = {
|
|
|
|
|
let time = match matches.opt_str("time") {
|
2018-04-20 07:54:49 +00:00
|
|
|
|
Some(s) => {
|
|
|
|
|
match &s[..] {
|
|
|
|
|
"accessed" => stat.accessed,
|
|
|
|
|
"created" => stat.created,
|
|
|
|
|
"modified" => stat.modified,
|
|
|
|
|
_ => {
|
|
|
|
|
show_error!(
|
|
|
|
|
"invalid argument 'modified' for '--time'
|
2013-12-11 10:09:45 +00:00
|
|
|
|
Valid arguments are:
|
|
|
|
|
- 'accessed', 'created', 'modified'
|
2018-03-12 08:20:58 +00:00
|
|
|
|
Try '{} --help' for more information.",
|
2018-04-20 07:54:49 +00:00
|
|
|
|
NAME
|
|
|
|
|
);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2018-03-14 20:26:22 +00:00
|
|
|
|
}
|
2018-04-20 07:54:49 +00:00
|
|
|
|
}
|
2018-03-14 20:26:22 +00:00
|
|
|
|
None => stat.modified,
|
|
|
|
|
};
|
|
|
|
|
((time / 1000) as i64, (time % 1000 * 1000000) as i32)
|
|
|
|
|
};
|
|
|
|
|
time::at(Timespec::new(secs, nsecs))
|
2013-12-11 10:09:45 +00:00
|
|
|
|
};
|
2018-03-14 20:26:22 +00:00
|
|
|
|
if !summarize || (summarize && index == len - 1) {
|
|
|
|
|
let time_str = tm.strftime(time_format_str).unwrap();
|
|
|
|
|
print!(
|
|
|
|
|
"{}\t{}\t{}{}",
|
|
|
|
|
convert_size(size),
|
|
|
|
|
time_str,
|
|
|
|
|
stat.path.display(),
|
|
|
|
|
line_separator
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if !summarize || (summarize && index == len - 1) {
|
|
|
|
|
print!(
|
|
|
|
|
"{}\t{}{}",
|
|
|
|
|
convert_size(size),
|
|
|
|
|
stat.path.display(),
|
|
|
|
|
line_separator
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
2014-06-17 14:51:08 +00:00
|
|
|
|
}
|
2013-12-11 10:09:45 +00:00
|
|
|
|
}
|
2018-03-20 16:10:05 +00:00
|
|
|
|
Err(_) => {
|
2018-03-22 16:05:33 +00:00
|
|
|
|
show_error!("{}: {}", path_str, "No such file or directory");
|
2018-03-20 16:10:05 +00:00
|
|
|
|
}
|
2013-12-04 02:08:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-08 07:44:32 +00:00
|
|
|
|
|
2017-12-10 17:52:22 +00:00
|
|
|
|
if options.total {
|
2014-06-19 12:50:51 +00:00
|
|
|
|
print!("{}\ttotal", 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
|
|
|
|
}
|
2018-04-03 11:44:53 +00:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test_du {
|
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_translate_to_pure_number() {
|
|
|
|
|
let test_data = [
|
|
|
|
|
(Some("10".to_string()), Some(10)),
|
|
|
|
|
(Some("10K".to_string()), Some(10 * 1024)),
|
|
|
|
|
(Some("5M".to_string()), Some(5 * 1024 * 1024)),
|
|
|
|
|
(Some("900KB".to_string()), Some(900 * 1000)),
|
|
|
|
|
(Some("BAD_STRING".to_string()), None),
|
|
|
|
|
];
|
|
|
|
|
for it in test_data.into_iter() {
|
|
|
|
|
assert_eq!(translate_to_pure_number(&it.0), it.1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_read_block_size() {
|
|
|
|
|
let test_data = [
|
|
|
|
|
(Some("10".to_string()), 10),
|
|
|
|
|
(None, 1024),
|
|
|
|
|
(Some("BAD_STRING".to_string()), 1024),
|
|
|
|
|
];
|
|
|
|
|
for it in test_data.into_iter() {
|
|
|
|
|
assert_eq!(read_block_size(it.0.clone()), it.1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|