From 025adec19e923c74ae690531dce8537aa19aefa7 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Tue, 3 Dec 2013 18:59:47 -0500 Subject: [PATCH 01/10] Add du to Makefile --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index f225ed4d7..208defe08 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ EXES := \ cat \ echo \ env \ + du \ false \ printenv \ true \ From 32cd344022f4d812df1b27e4ee3ea7229acac131 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Tue, 3 Dec 2013 19:00:07 -0500 Subject: [PATCH 02/10] Remove du from to-do list --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 0fe404faa..0f1927887 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,6 @@ To do - df - dircolors - dirname -- du - expand - expr - extent-scan From 661ac1be78a5675319fb8baf099796845249af89 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Tue, 3 Dec 2013 21:08:42 -0500 Subject: [PATCH 03/10] Get basic functionality to work --- du/du.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 du/du.rs diff --git a/du/du.rs b/du/du.rs new file mode 100644 index 000000000..230845aae --- /dev/null +++ b/du/du.rs @@ -0,0 +1,97 @@ +#[link(name="du", vers="1.0.0", author="Derek Chiang")]; + +/* + * This file is part of the uutils coreutils package. + * + * (c) Derek Chiang + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +extern mod extra; + +use std::os; +use std::io::stderr; +use std::io::fs; +use std::io::FileStat; +use std::path::Path; +use std::uint; +use std::comm::{Port, Chan, SharedChan, stream}; +use extra::arc::Arc; +use extra::future::Future; +use extra::getopts::groups; + +static VERSION: &'static str = "1.0.0"; + +fn du(path: &Path) -> ~[Arc] { + let mut stats = ~[]; + let mut futures = ~[]; + stats.push(Arc::new(path.stat())); + + for f in fs::readdir(path).move_iter() { + match f.is_file() { + true => stats.push(Arc::new(f.stat())), + false => futures.push(do Future::spawn { du(&f) }) + } + } + + for future in futures.mut_iter() { + stats.push_all(future.get()); + } + + return stats; +} + +fn main() { + let args = os::args(); + let program = args[0].clone(); + let opts = ~[ + groups::optflag("a", "all", " write counts for all files, not just directories"), + groups::optflag("", "apparent-size", "print apparent sizes, rather than disk usage; + although the apparent size is usually smaller, it may be larger due to holes + in ('sparse') files, internal fragmentation, indirect blocks, and the like"), + groups::optopt("B", "block-size", "scale sizes by SIZE before printing them. + E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.", + "SIZE"), + groups::optflag("", "help", "display this help and exit"), + groups::optflag("", "version", "output version information and exit"), + ]; + + let matches = match groups::getopts(args.tail(), opts) { + Ok(m) => m, + Err(f) => { + writeln!(&mut stderr() as &mut Writer, + "Invalid options\n{}", f.to_err_msg()); + os::set_exit_status(1); + return + } + }; + + if matches.opt_present("help") { + println("du " + VERSION + " - estimate file space usage"); + println(""); + println("Usage:"); + println!(" {0:s} [OPTION]... [FILE]...", program); + println!(" {0:s} [OPTION]... --files0-from=F", program); + println(""); + println(groups::usage("Summarize disk usage of each FILE, recursively for directories.", opts)); + return; + } + + if !matches.free.is_empty() { + } + + let strs = match matches.free { + [] => ~[~"./"], + x => x + }; + + for path_str in strs.iter() { + let path = Path::init(path_str.clone()); + for stat_arc in du(&path).move_iter() { + let stat = stat_arc.get(); + println!("{:<10} {}", stat.size, stat.path.display()); + } + } +} From 5ab82df876662a620af36aa6a0cdcd046d2962ca Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Sun, 8 Dec 2013 01:36:36 -0500 Subject: [PATCH 04/10] Getting it to display the correct directory size --- du/du.rs | 124 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 102 insertions(+), 22 deletions(-) diff --git a/du/du.rs b/du/du.rs index 230845aae..766933e9b 100644 --- a/du/du.rs +++ b/du/du.rs @@ -16,30 +16,47 @@ use std::io::stderr; use std::io::fs; use std::io::FileStat; use std::path::Path; -use std::uint; -use std::comm::{Port, Chan, SharedChan, stream}; use extra::arc::Arc; use extra::future::Future; -use extra::getopts::groups; +use extra::getopts::{groups, Matches}; static VERSION: &'static str = "1.0.0"; -fn du(path: &Path) -> ~[Arc] { + +fn du(path: &Path, matches_arc: Arc) -> ~[Arc] { let mut stats = ~[]; let mut futures = ~[]; - stats.push(Arc::new(path.stat())); + let matches = matches_arc.get(); + let mut my_stat = path.stat(); for f in fs::readdir(path).move_iter() { match f.is_file() { - true => stats.push(Arc::new(f.stat())), - false => futures.push(do Future::spawn { du(&f) }) + true => { + let stat = f.stat(); + my_stat.size += stat.size; + if matches.opt_present("all") { + stats.push(Arc::new(stat)) + } + } + false => { + let ma_clone = matches_arc.clone(); + futures.push(do Future::spawn { du(&f, ma_clone) }) + } } } for future in futures.mut_iter() { - stats.push_all(future.get()); + for stat_arc in future.get().move_rev_iter() { + let stat = stat_arc.get(); + if stat.path.dir_path() == my_stat.path { + my_stat.size += stat.size; + } + stats.push(stat_arc.clone()); + } } + stats.push(Arc::new(my_stat)); + return stats; } @@ -47,13 +64,68 @@ fn main() { let args = os::args(); let program = args[0].clone(); let opts = ~[ + // In task groups::optflag("a", "all", " write counts for all files, not just directories"), - groups::optflag("", "apparent-size", "print apparent sizes, rather than disk usage; - although the apparent size is usually smaller, it may be larger due to holes - in ('sparse') files, internal fragmentation, indirect blocks, and the like"), - groups::optopt("B", "block-size", "scale sizes by SIZE before printing them. - E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.", - "SIZE"), + // // In main + // groups::optflag("", "apparent-size", "print apparent sizes, rather than disk usage; + // although the apparent size is usually smaller, it may be larger due to holes + // in ('sparse') files, internal fragmentation, indirect blocks, and the like"), + // In main + // groups::optopt("B", "block-size", "scale sizes by SIZE before printing them. + // E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.", + // "SIZE"), + // // In main + // groups::optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'"), + // In main + groups::optflag("c", "total", "produce a grand total"), + // In task + // groups::optflag("D", "dereference-args", "dereference only symlinks that are listed + // on the command line"), + // In main + // groups::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 + // groups::optflag("H", "", "equivalent to --dereference-args (-D)"), + // In main + groups::optflag("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"), + // In main + groups::optflag("", "si", "like -h, but use powers of 1000 not 1024"), + // In main + groups::optflag("k", "", "like --block-size=1K"), + // // In task + // groups::optflag("l", "count-links", "count sizes many times if hard linked"), + // // In main + // groups::optflag("m", "", "like --block-size=1M"), + // // In task + // groups::optflag("L", "dereference", "dereference all symbolic links"), + // // In task + // groups::optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"), + // // In main + groups::optflag("0", "null", "end each output line with 0 byte rather than newline"), + // In main? + groups::optflag("S", "separate-dirs", "do not include size of subdirectories"), + // In main + groups::optflag("s", "summarize", "display only a total for each argument"), + // // In task + // groups::optflag("x", "one-file-system", "skip directories on different file systems"), + // // In task + // groups::optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"), + // // In task + // groups::optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"), + // // In main + groups::optopt("d", "max-depth", "print the total for a directory (or file, with --all) + only if it is N or fewer levels below the command + line argument; --max-depth=0 is the same as --summarize", "N"), + // // In main + // groups::optflag("", "time", "show time of the last modification of any file in the + // directory, or any of its subdirectories"), + // // In main + // groups::optopt("", "time", "show time as WORD instead of modification time: + // atime, access, use, ctime or status", "WORD"), + // // In main + // groups::optopt("", "time-style", "show times using style STYLE: + // full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE"), groups::optflag("", "help", "display this help and exit"), groups::optflag("", "version", "output version information and exit"), ]; @@ -76,20 +148,28 @@ fn main() { println!(" {0:s} [OPTION]... --files0-from=F", program); println(""); println(groups::usage("Summarize disk usage of each FILE, recursively for directories.", opts)); - return; + println("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 +POSIXLY_CORRECT is set). + +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)."); + return } - if !matches.free.is_empty() { - } - - let strs = match matches.free { - [] => ~[~"./"], - x => x + let strs = matches.free.clone(); + let strs = match strs.is_empty() { + true => ~[~"./"], + false => strs }; + let matches_arc = Arc::new(matches); + for path_str in strs.iter() { let path = Path::init(path_str.clone()); - for stat_arc in du(&path).move_iter() { + for stat_arc in du(&path, matches_arc.clone()).move_iter() { let stat = stat_arc.get(); println!("{:<10} {}", stat.size, stat.path.display()); } From 14edd5704baa6630640c4079ca52782f88b2ded4 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Sun, 8 Dec 2013 02:44:32 -0500 Subject: [PATCH 05/10] Implemented a few more options --- du/du.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/du/du.rs b/du/du.rs index 766933e9b..477d31893 100644 --- a/du/du.rs +++ b/du/du.rs @@ -15,18 +15,24 @@ use std::os; use std::io::stderr; use std::io::fs; use std::io::FileStat; +use std::option::Option; use std::path::Path; use extra::arc::Arc; use extra::future::Future; -use extra::getopts::{groups, Matches}; +use extra::getopts::groups; static VERSION: &'static str = "1.0.0"; +struct Options { + all: bool, + max_depth: Option, + total: bool +} -fn du(path: &Path, matches_arc: Arc) -> ~[Arc] { +fn du(path: &Path, options_arc: Arc, depth: uint) -> ~[Arc] { let mut stats = ~[]; let mut futures = ~[]; - let matches = matches_arc.get(); + let options = options_arc.get(); let mut my_stat = path.stat(); for f in fs::readdir(path).move_iter() { @@ -34,13 +40,13 @@ fn du(path: &Path, matches_arc: Arc) -> ~[Arc] { true => { let stat = f.stat(); my_stat.size += stat.size; - if matches.opt_present("all") { + if options.all { stats.push(Arc::new(stat)) } } false => { - let ma_clone = matches_arc.clone(); - futures.push(do Future::spawn { du(&f, ma_clone) }) + let oa_clone = options_arc.clone(); + futures.push(do Future::spawn { du(&f, oa_clone, depth + 1) }) } } } @@ -51,7 +57,9 @@ fn du(path: &Path, matches_arc: Arc) -> ~[Arc] { if stat.path.dir_path() == my_stat.path { my_stat.size += stat.size; } - stats.push(stat_arc.clone()); + if options.max_depth == None || depth < options.max_depth.unwrap() { + stats.push(stat_arc.clone()); + } } } @@ -159,19 +167,58 @@ ers of 1000)."); 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::(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::(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 = match strs.is_empty() { true => ~[~"./"], 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() { 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(); 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); + } } From 9eae93e8a1486e5d0ff5755a5a443f944c0e4958 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Sun, 8 Dec 2013 04:22:09 -0500 Subject: [PATCH 06/10] Add formatting-related logic --- du/du.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/du/du.rs b/du/du.rs index 477d31893..52d4b32ea 100644 --- a/du/du.rs +++ b/du/du.rs @@ -104,7 +104,7 @@ fn main() { // // In task // groups::optflag("l", "count-links", "count sizes many times if hard linked"), // // In main - // groups::optflag("m", "", "like --block-size=1M"), + groups::optflag("m", "", "like --block-size=1M"), // // In task // groups::optflag("L", "dereference", "dereference all symbolic links"), // // In task @@ -201,6 +201,26 @@ ers of 1000)."); let options_arc = Arc::new(options); + let MB = 1024 * 1024; + let KB = 1024; + let convert_size = |size: u64| -> ~str { + if matches.opt_present("human-readable") { + if size > MB { + format!("{:.1f}MB", (size as f64) / (MB as f64)) + } else if size > KB { + format!("{:.1f}KB", (size as f64) / (KB as f64)) + } 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 { + format!("{}", ((size as f64) / (KB as f64)).ceil()) + } + }; + let mut grand_total = 0; for path_str in strs.iter() { let path = Path::init(path_str.clone()); @@ -209,7 +229,7 @@ ers of 1000)."); let len = len.unwrap(); for (index, stat_arc) in iter.enumerate() { let stat = stat_arc.get(); - println!("{:<10} {}", stat.size, stat.path.display()); + println!("{:<10} {}", convert_size(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. @@ -219,6 +239,6 @@ ers of 1000)."); } if options.total { - println!("{:<10} total", grand_total); + println!("{:<10} total", convert_size(grand_total)); } } From a228acbb50cbfe335b1082efe1ded63a24c45cc8 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Mon, 9 Dec 2013 02:11:11 -0500 Subject: [PATCH 07/10] Correctly calculate file size using number of blocks --- du/du.rs | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/du/du.rs b/du/du.rs index 52d4b32ea..8a7d9cf42 100644 --- a/du/du.rs +++ b/du/du.rs @@ -26,7 +26,8 @@ static VERSION: &'static str = "1.0.0"; struct Options { all: bool, max_depth: Option, - total: bool + total: bool, + separate_dirs: bool, } fn du(path: &Path, options_arc: Arc, depth: uint) -> ~[Arc] { @@ -40,6 +41,7 @@ fn du(path: &Path, options_arc: Arc, depth: uint) -> ~[Arc] { true => { let stat = f.stat(); my_stat.size += stat.size; + my_stat.unstable.blocks += stat.unstable.blocks; if options.all { stats.push(Arc::new(stat)) } @@ -54,8 +56,9 @@ fn du(path: &Path, options_arc: Arc, depth: uint) -> ~[Arc] { for future in futures.mut_iter() { for stat_arc in future.get().move_rev_iter() { let stat = stat_arc.get(); - if stat.path.dir_path() == my_stat.path { + if !options.separate_dirs && stat.path.dir_path() == my_stat.path { my_stat.size += stat.size; + my_stat.unstable.blocks += stat.unstable.blocks; } if options.max_depth == None || depth < options.max_depth.unwrap() { stats.push(stat_arc.clone()); @@ -74,11 +77,11 @@ fn main() { let opts = ~[ // In task groups::optflag("a", "all", " write counts for all files, not just directories"), - // // In main - // groups::optflag("", "apparent-size", "print apparent sizes, rather than disk usage; - // although the apparent size is usually smaller, it may be larger due to holes - // in ('sparse') files, internal fragmentation, indirect blocks, and the like"), // In main + groups::optflag("", "apparent-size", "print apparent sizes, rather than disk usage; + although the apparent size is usually smaller, it may be larger due to holes + in ('sparse') files, internal fragmentation, indirect blocks, and the like"), + // // In main // groups::optopt("B", "block-size", "scale sizes by SIZE before printing them. // E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.", // "SIZE"), @@ -190,7 +193,8 @@ ers of 1000)."); }, (false, None) => None }, - total: matches.opt_present("total") + total: matches.opt_present("total"), + separate_dirs: matches.opt_present("S"), }; let strs = matches.free.clone(); @@ -201,10 +205,17 @@ ers of 1000)."); let options_arc = Arc::new(options); - let MB = 1024 * 1024; - let KB = 1024; + let MB = match matches.opt_present("si") { + true => 1000 * 1000, + false => 1024 * 1024, + }; + let KB = match matches.opt_present("si") { + true => 1000, + false => 1024, + }; + let convert_size = |size: u64| -> ~str { - if matches.opt_present("human-readable") { + if matches.opt_present("human-readable") || matches.opt_present("si") { if size > MB { format!("{:.1f}MB", (size as f64) / (MB as f64)) } else if size > KB { @@ -221,6 +232,11 @@ ers of 1000)."); } }; + let line_separator = match matches.opt_present("0") { + true => "\0", + false => "\n", + }; + let mut grand_total = 0; for path_str in strs.iter() { let path = Path::init(path_str.clone()); @@ -229,16 +245,24 @@ ers of 1000)."); let len = len.unwrap(); for (index, stat_arc) in iter.enumerate() { let stat = stat_arc.get(); - println!("{:<10} {}", convert_size(stat.size), stat.path.display()); + let size = match matches.opt_present("apparent-size") { + true => stat.size, + // C's stat is such that each block is assume to be 512 bytes + // See: http://linux.die.net/man/2/stat + false => stat.unstable.blocks * 512, + }; + print!("{:<10} {}", convert_size(size), stat.path.display()); + print(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 += stat.size; + grand_total += size; } } } if options.total { - println!("{:<10} total", convert_size(grand_total)); + print!("{:<10} total", convert_size(grand_total)); + print(line_separator); } } From c4db2f33299f348ed5c7daeb569b5963bf409237 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Wed, 11 Dec 2013 05:09:45 -0500 Subject: [PATCH 08/10] Adding partial support for formatting time --- du/du.rs | 137 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 112 insertions(+), 25 deletions(-) diff --git a/du/du.rs b/du/du.rs index 8a7d9cf42..2437352f0 100644 --- a/du/du.rs +++ b/du/du.rs @@ -20,6 +20,7 @@ use std::path::Path; use extra::arc::Arc; use extra::future::Future; use extra::getopts::groups; +use extra::time::Timespec; static VERSION: &'static str = "1.0.0"; @@ -81,12 +82,12 @@ fn main() { groups::optflag("", "apparent-size", "print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like"), - // // In main - // groups::optopt("B", "block-size", "scale sizes by SIZE before printing them. - // E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.", - // "SIZE"), - // // In main - // groups::optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'"), + // In main + groups::optopt("B", "block-size", "scale sizes by SIZE before printing them. + E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.", + "SIZE"), + // In main + groups::optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'"), // In main groups::optflag("c", "total", "produce a grand total"), // In task @@ -104,8 +105,8 @@ fn main() { groups::optflag("", "si", "like -h, but use powers of 1000 not 1024"), // In main groups::optflag("k", "", "like --block-size=1K"), - // // In task - // groups::optflag("l", "count-links", "count sizes many times if hard linked"), + // In task + groups::optflag("l", "count-links", "count sizes many times if hard linked"), // // In main groups::optflag("m", "", "like --block-size=1M"), // // In task @@ -114,7 +115,7 @@ fn main() { // groups::optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"), // // In main groups::optflag("0", "null", "end each output line with 0 byte rather than newline"), - // In main? + // In main groups::optflag("S", "separate-dirs", "do not include size of subdirectories"), // In main groups::optflag("s", "summarize", "display only a total for each argument"), @@ -124,19 +125,19 @@ fn main() { // groups::optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"), // // In task // groups::optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"), - // // In main + // In main groups::optopt("d", "max-depth", "print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize", "N"), - // // In main - // groups::optflag("", "time", "show time of the last modification of any file in the - // directory, or any of its subdirectories"), - // // In main - // groups::optopt("", "time", "show time as WORD instead of modification time: - // atime, access, use, ctime or status", "WORD"), - // // In main - // groups::optopt("", "time-style", "show times using style STYLE: - // full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE"), + // In main + groups::optflag("", "time", "show time of the last modification of any file in the + directory, or any of its subdirectories"), + // In main + groups::optopt("", "time", "show time as WORD instead of modification time: + atime, access, use, ctime or status", "WORD"), + // In main + groups::optopt("", "time-style", "show times using style STYLE: + full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE"), groups::optflag("", "help", "display this help and exit"), groups::optflag("", "version", "output version information and exit"), ]; @@ -214,12 +215,51 @@ ers of 1000)."); false => 1024, }; + let block_size = match matches.opt_str("block-size") { + Some(s) => { + let mut found_number = false; + let mut found_letter = false; + let mut numbers = ~[]; + let mut letters = ~[]; + for c in s.chars() { + if found_letter && c.is_digit() || !found_number && !c.is_digit() { + println!("du: invalid --block-size argument '{}'", s); + return + } else if c.is_digit() { + found_number = true; + numbers.push(c as u8); + } else if c.is_alphabetic() { + found_letter = true; + letters.push(c); + } + } + let number = std::uint::parse_bytes(numbers, 10).unwrap(); + let multiple = match std::str::from_chars(letters).as_slice() { + "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, + _ => { + println!("du: invalid --block-size argument '{}'", s); return + } + }; + number * multiple + }, + None => 1024 + }; + let convert_size = |size: u64| -> ~str { if matches.opt_present("human-readable") || matches.opt_present("si") { if size > MB { - format!("{:.1f}MB", (size as f64) / (MB as f64)) + format!("{:.1f}M", (size as f64) / (MB as f64)) } else if size > KB { - format!("{:.1f}KB", (size as f64) / (KB as f64)) + format!("{:.1f}K", (size as f64) / (KB as f64)) } else { format!("{}B", size) } @@ -228,10 +268,31 @@ ers of 1000)."); } else if matches.opt_present("m") { format!("{}", ((size as f64) / (MB as f64)).ceil()) } else { - format!("{}", ((size as f64) / (KB as f64)).ceil()) + format!("{}", ((size as f64) / (block_size as f64)).ceil()) } }; + let time_format_str = match matches.opt_str("time-style") { + Some(s) => { + match s.as_slice() { + "full-iso" => "%Y-%m-%d %H:%M:%S.%N %z", + "long-iso" => "%Y-%m-%d %H:%M", + "iso" => "%Y-%m-%d", + _ => { + println(" +du: invalid argument 'awdwa' for 'time style' +Valid arguments are: +- 'full-iso' +- 'long-iso' +- 'iso' +Try 'du --help' for more information."); + return + } + } + }, + None => "%Y-%m-%d %H:%M" + }; + let line_separator = match matches.opt_present("0") { true => "\0", false => "\n", @@ -239,19 +300,45 @@ ers of 1000)."); let mut grand_total = 0; for path_str in strs.iter() { - let path = Path::init(path_str.clone()); + let path = Path::new(path_str.clone()); 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 size = match matches.opt_present("apparent-size") { - true => stat.size, + true => stat.unstable.nlink * stat.size, // C's stat is such that each block is assume to be 512 bytes // See: http://linux.die.net/man/2/stat false => stat.unstable.blocks * 512, }; - print!("{:<10} {}", convert_size(size), stat.path.display()); + if matches.opt_present("time") { + let time_str = { + let (secs, nsecs) = { + let time = match matches.opt_str("time") { + Some(s) => match s.as_slice() { + "accessed" => stat.accessed, + "created" => stat.created, + "modified" => stat.modified, + _ => { + println("du: invalid argument 'modified' for '--time' + Valid arguments are: + - 'accessed', 'created', 'modified' + Try 'du --help' for more information."); + return + } + }, + None => stat.modified + }; + ((time / 1000) as i64, (time % 1000 * 1000) as i32) + }; + let time_spec = Timespec::new(secs, nsecs); + extra::time::at(time_spec).strftime(time_format_str) + }; + print!("{:<10} {:<30} {}", convert_size(size), time_str, stat.path.display()); + } else { + print!("{:<10} {}", convert_size(size), stat.path.display()); + } print(line_separator); if options.total && index == (len - 1) { // The last element will be the total size of the the path under From 882a6403f8544feefd57772dbad6989c2a63b9c9 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Wed, 11 Dec 2013 18:20:52 -0500 Subject: [PATCH 09/10] Fixing time format string --- du/du.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/du/du.rs b/du/du.rs index 2437352f0..d23ad539b 100644 --- a/du/du.rs +++ b/du/du.rs @@ -130,10 +130,8 @@ fn main() { only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize", "N"), // In main - groups::optflag("", "time", "show time of the last modification of any file in the - directory, or any of its subdirectories"), - // In main - groups::optopt("", "time", "show time as WORD instead of modification time: + groups::optflagopt("", "time", "show time of the last modification of any file in the + 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"), // In main groups::optopt("", "time-style", "show times using style STYLE: @@ -275,7 +273,7 @@ ers of 1000)."); let time_format_str = match matches.opt_str("time-style") { Some(s) => { match s.as_slice() { - "full-iso" => "%Y-%m-%d %H:%M:%S.%N %z", + "full-iso" => "%Y-%m-%d %H:%M:%S.%f %z", "long-iso" => "%Y-%m-%d %H:%M", "iso" => "%Y-%m-%d", _ => { @@ -330,7 +328,7 @@ Try 'du --help' for more information."); }, None => stat.modified }; - ((time / 1000) as i64, (time % 1000 * 1000) as i32) + ((time / 1000) as i64, (time % 1000 * 1000000) as i32) }; let time_spec = Timespec::new(secs, nsecs); extra::time::at(time_spec).strftime(time_format_str) From 63f6c9719b81b949fa6ce856b518fbf554844e46 Mon Sep 17 00:00:00 2001 From: Arcterus Date: Tue, 18 Feb 2014 17:10:32 -0800 Subject: [PATCH 10/10] Fix du, which closes #46, and id when not compiling on Linux --- du/du.rs | 105 ++++++++++++++++++++++++++++--------------------------- id/id.rs | 24 ++++++------- util.rs | 10 ++++++ 3 files changed, 76 insertions(+), 63 deletions(-) diff --git a/du/du.rs b/du/du.rs index d23ad539b..e0e2e9c11 100644 --- a/du/du.rs +++ b/du/du.rs @@ -1,4 +1,4 @@ -#[link(name="du", vers="1.0.0", author="Derek Chiang")]; +#[crate_id(name="du", vers="1.0.0", author="Derek Chiang")]; /* * This file is part of the uutils coreutils package. @@ -9,19 +9,24 @@ * file that was distributed with this source code. */ -extern mod extra; +#[feature(macro_rules)]; + +extern crate extra; +extern crate getopts; +extern crate sync; use std::os; -use std::io::stderr; use std::io::fs; use std::io::FileStat; use std::option::Option; use std::path::Path; -use extra::arc::Arc; -use extra::future::Future; -use extra::getopts::groups; use extra::time::Timespec; +use sync::{Arc, Future}; +#[path = "../util.rs"] +mod util; + +static NAME: &'static str = "du"; static VERSION: &'static str = "1.0.0"; struct Options { @@ -35,21 +40,21 @@ fn du(path: &Path, options_arc: Arc, depth: uint) -> ~[Arc] { let mut stats = ~[]; let mut futures = ~[]; let options = options_arc.get(); - let mut my_stat = path.stat(); + let mut my_stat = safe_unwrap!(path.stat()); - for f in fs::readdir(path).move_iter() { + for f in safe_unwrap!(fs::readdir(path)).move_iter() { match f.is_file() { true => { - let stat = f.stat(); + let stat = safe_unwrap!(f.stat()); my_stat.size += stat.size; my_stat.unstable.blocks += stat.unstable.blocks; if options.all { stats.push(Arc::new(stat)) - } + } } false => { let oa_clone = options_arc.clone(); - futures.push(do Future::spawn { du(&f, oa_clone, depth + 1) }) + futures.push(Future::spawn(proc() { du(&f, oa_clone, depth + 1) })) } } } @@ -77,88 +82,86 @@ fn main() { let program = args[0].clone(); let opts = ~[ // In task - groups::optflag("a", "all", " write counts for all files, not just directories"), + getopts::optflag("a", "all", " write counts for all files, not just directories"), // In main - groups::optflag("", "apparent-size", "print apparent sizes, rather than disk usage; + getopts::optflag("", "apparent-size", "print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like"), // In main - groups::optopt("B", "block-size", "scale sizes by SIZE before printing them. + getopts::optopt("B", "block-size", "scale sizes by SIZE before printing them. E.g., '-BM' prints sizes in units of 1,048,576 bytes. See SIZE format below.", "SIZE"), // In main - groups::optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'"), + getopts::optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'"), // In main - groups::optflag("c", "total", "produce a grand total"), + getopts::optflag("c", "total", "produce a grand total"), // In task - // groups::optflag("D", "dereference-args", "dereference only symlinks that are listed + // getopts::optflag("D", "dereference-args", "dereference only symlinks that are listed // on the command line"), // In main - // groups::optopt("", "files0-from", "summarize disk usage of the NUL-terminated file + // getopts::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 - // groups::optflag("H", "", "equivalent to --dereference-args (-D)"), + // getopts::optflag("H", "", "equivalent to --dereference-args (-D)"), // In main - groups::optflag("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"), + getopts::optflag("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"), // In main - groups::optflag("", "si", "like -h, but use powers of 1000 not 1024"), + getopts::optflag("", "si", "like -h, but use powers of 1000 not 1024"), // In main - groups::optflag("k", "", "like --block-size=1K"), + getopts::optflag("k", "", "like --block-size=1K"), // In task - groups::optflag("l", "count-links", "count sizes many times if hard linked"), + getopts::optflag("l", "count-links", "count sizes many times if hard linked"), // // In main - groups::optflag("m", "", "like --block-size=1M"), + getopts::optflag("m", "", "like --block-size=1M"), // // In task - // groups::optflag("L", "dereference", "dereference all symbolic links"), + // getopts::optflag("L", "dereference", "dereference all symbolic links"), // // In task - // groups::optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"), + // getopts::optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"), // // In main - groups::optflag("0", "null", "end each output line with 0 byte rather than newline"), + getopts::optflag("0", "null", "end each output line with 0 byte rather than newline"), // In main - groups::optflag("S", "separate-dirs", "do not include size of subdirectories"), + getopts::optflag("S", "separate-dirs", "do not include size of subdirectories"), // In main - groups::optflag("s", "summarize", "display only a total for each argument"), + getopts::optflag("s", "summarize", "display only a total for each argument"), // // In task - // groups::optflag("x", "one-file-system", "skip directories on different file systems"), + // getopts::optflag("x", "one-file-system", "skip directories on different file systems"), // // In task - // groups::optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"), + // getopts::optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"), // // In task - // groups::optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"), + // getopts::optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"), // In main - groups::optopt("d", "max-depth", "print the total for a directory (or file, with --all) + getopts::optopt("d", "max-depth", "print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize", "N"), // In main - groups::optflagopt("", "time", "show time of the last modification of any file in the + getopts::optflagopt("", "time", "show time of the last modification of any file in the 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"), // In main - groups::optopt("", "time-style", "show times using style STYLE: + getopts::optopt("", "time-style", "show times using style STYLE: full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE"), - groups::optflag("", "help", "display this help and exit"), - groups::optflag("", "version", "output version information and exit"), + getopts::optflag("", "help", "display this help and exit"), + getopts::optflag("", "version", "output version information and exit"), ]; - let matches = match groups::getopts(args.tail(), opts) { + let matches = match getopts::getopts(args.tail(), opts) { Ok(m) => m, Err(f) => { - writeln!(&mut stderr() as &mut Writer, - "Invalid options\n{}", f.to_err_msg()); - os::set_exit_status(1); + show_error!(1, "Invalid options\n{}", f.to_err_msg()); return } }; if matches.opt_present("help") { - println("du " + VERSION + " - estimate file space usage"); - println(""); - println("Usage:"); + println!("du {} - estimate file space usage", VERSION); + println!(""); + println!("Usage:"); println!(" {0:s} [OPTION]... [FILE]...", program); println!(" {0:s} [OPTION]... --files0-from=F", program); - println(""); - println(groups::usage("Summarize disk usage of each FILE, recursively for directories.", opts)); - println("Display values are in units of the first available SIZE from + println!(""); + println!("{}", getopts::usage("Summarize disk usage of each FILE, recursively for directories.", opts)); + println!("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 POSIXLY_CORRECT is set). @@ -277,7 +280,7 @@ ers of 1000)."); "long-iso" => "%Y-%m-%d %H:%M", "iso" => "%Y-%m-%d", _ => { - println(" + println!(" du: invalid argument 'awdwa' for 'time style' Valid arguments are: - 'full-iso' @@ -319,7 +322,7 @@ Try 'du --help' for more information."); "created" => stat.created, "modified" => stat.modified, _ => { - println("du: invalid argument 'modified' for '--time' + println!("du: invalid argument 'modified' for '--time' Valid arguments are: - 'accessed', 'created', 'modified' Try 'du --help' for more information."); @@ -337,7 +340,7 @@ Try 'du --help' for more information."); } else { print!("{:<10} {}", convert_size(size), stat.path.display()); } - print(line_separator); + print!("{}", 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. @@ -348,6 +351,6 @@ Try 'du --help' for more information."); if options.total { print!("{:<10} total", convert_size(grand_total)); - print(line_separator); + print!("{}", line_separator); } } diff --git a/id/id.rs b/id/id.rs index dff5b3ca2..f0169e8e6 100644 --- a/id/id.rs +++ b/id/id.rs @@ -41,27 +41,27 @@ struct c_group { #[cfg(not(target_os = "linux"))] mod audit { - pub use std::unstable::intrinsic::uninit; - use std::libc::{pid_t, c_uint, uint64_t, dev_t}; + pub use std::unstable::intrinsics::uninit; + use std::libc::{uid_t, pid_t, c_int, c_uint, uint64_t, dev_t}; - type au_id_t = uid_t; - type au_asid_t = pid_t; - type au_event_t = c_uint; - type au_emod_t = c_uint; - type au_class_t = c_int; + pub type au_id_t = uid_t; + pub type au_asid_t = pid_t; + pub type au_event_t = c_uint; + pub type au_emod_t = c_uint; + pub type au_class_t = c_int; - struct au_mask { + pub struct au_mask { am_success: c_uint, am_failure: c_uint } - type au_mask_t = au_mask; + pub type au_mask_t = au_mask; - struct au_tid_addr { + pub struct au_tid_addr { port: dev_t, } - type au_tid_addr_t = au_tid_addr; + pub type au_tid_addr_t = au_tid_addr; - struct c_auditinfo_addr { + pub struct c_auditinfo_addr { ai_auid: au_id_t, /* Audit user ID */ ai_mask: au_mask_t, /* Audit masks. */ ai_termid: au_tid_addr_t, /* Terminal ID. */ diff --git a/util.rs b/util.rs index 7e851ddf4..bbe2cc9b6 100644 --- a/util.rs +++ b/util.rs @@ -45,3 +45,13 @@ macro_rules! safe_writeln( } ) ) + +#[macro_export] +macro_rules! safe_unwrap( + ($exp:expr) => ( + match $exp { + Ok(m) => m, + Err(f) => crash!(1, "{}", f.to_str()) + } + ) +)