1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 05:27:45 +00:00

Merge pull request #1157 from bootandy/master

Fix edge case for du on mac
This commit is contained in:
Alex Lyon 2018-03-15 12:26:42 -07:00 committed by GitHub
commit 3015a19230
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,7 +16,7 @@ extern crate uucore;
use std::fs; use std::fs;
use std::iter; use std::iter;
use std::io::{stderr, Write}; use std::io::{stderr, Result, Write};
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
use std::path::PathBuf; use std::path::PathBuf;
use time::Timespec; use time::Timespec;
@ -54,9 +54,9 @@ struct Stat {
} }
impl Stat { impl Stat {
fn new(path: PathBuf) -> Stat { fn new(path: PathBuf) -> Result<Stat> {
let metadata = safe_unwrap!(fs::symlink_metadata(&path)); let metadata = fs::symlink_metadata(&path)?;
Stat { Ok(Stat {
path: path, path: path,
is_dir: metadata.is_dir(), is_dir: metadata.is_dir(),
size: metadata.len(), size: metadata.len(),
@ -65,7 +65,7 @@ impl Stat {
created: metadata.mtime() as u64, created: metadata.mtime() as u64,
accessed: metadata.atime() as u64, accessed: metadata.atime() as u64,
modified: metadata.mtime() as u64, modified: metadata.mtime() as u64,
} })
} }
} }
@ -91,16 +91,22 @@ fn du(mut my_stat: Stat, options: &Options, depth: usize) -> Box<DoubleEndedIter
}; };
for f in read.into_iter() { for f in read.into_iter() {
let entry = crash_if_err!(1, f); match f {
let this_stat = Stat::new(entry.path()); Ok(entry) => match Stat::new(entry.path()) {
if this_stat.is_dir { Ok(this_stat) => {
futures.push(du(this_stat, options, depth + 1)); if this_stat.is_dir {
} else { futures.push(du(this_stat, options, depth + 1));
my_stat.size += this_stat.size; } else {
my_stat.blocks += this_stat.blocks; my_stat.size += this_stat.size;
if options.all { my_stat.blocks += this_stat.blocks;
stats.push(this_stat); if options.all {
} stats.push(this_stat);
}
}
}
Err(error) => show_error!("{}", error),
},
Err(error) => show_error!("{}", error),
} }
} }
} }
@ -327,67 +333,72 @@ Try '{} --help' for more information.",
let mut grand_total = 0; let mut grand_total = 0;
for path_str in strs.into_iter() { for path_str in strs.into_iter() {
let path = PathBuf::from(path_str); let path = PathBuf::from(path_str);
let iter = du(Stat::new(path), &options, 0).into_iter(); match Stat::new(path) {
let (_, len) = iter.size_hint(); Ok(stat) => {
let len = len.unwrap(); let iter = du(stat, &options, 0).into_iter();
for (index, stat) in iter.enumerate() { let (_, len) = iter.size_hint();
let size = if matches.opt_present("apparent-size") { let len = len.unwrap();
stat.nlink * stat.size for (index, stat) in iter.enumerate() {
} else { let size = if matches.opt_present("apparent-size") {
// C's stat is such that each block is assume to be 512 bytes stat.nlink * stat.size
// See: http://linux.die.net/man/2/stat } else {
stat.blocks * 512 // C's stat is such that each block is assume to be 512 bytes
}; // See: http://linux.die.net/man/2/stat
if matches.opt_present("time") { stat.blocks * 512
let tm = { };
let (secs, nsecs) = { if matches.opt_present("time") {
let time = match matches.opt_str("time") { let tm = {
Some(s) => match &s[..] { let (secs, nsecs) = {
"accessed" => stat.accessed, let time = match matches.opt_str("time") {
"created" => stat.created, Some(s) => match &s[..] {
"modified" => stat.modified, "accessed" => stat.accessed,
_ => { "created" => stat.created,
show_error!( "modified" => stat.modified,
"invalid argument 'modified' for '--time' _ => {
show_error!(
"invalid argument 'modified' for '--time'
Valid arguments are: Valid arguments are:
- 'accessed', 'created', 'modified' - 'accessed', 'created', 'modified'
Try '{} --help' for more information.", Try '{} --help' for more information.",
NAME NAME
); );
return 1; return 1;
} }
}, },
None => stat.modified, None => stat.modified,
};
((time / 1000) as i64, (time % 1000 * 1000000) as i32)
};
time::at(Timespec::new(secs, nsecs))
}; };
((time / 1000) as i64, (time % 1000 * 1000000) as i32) if !summarize || (summarize && index == len - 1) {
}; let time_str = tm.strftime(time_format_str).unwrap();
time::at(Timespec::new(secs, nsecs)) print!(
}; "{}\t{}\t{}{}",
if !summarize || (summarize && index == len - 1) { convert_size(size),
let time_str = tm.strftime(time_format_str).unwrap(); time_str,
print!( stat.path.display(),
"{}\t{}\t{}{}", line_separator
convert_size(size), );
time_str, }
stat.path.display(), } else {
line_separator if !summarize || (summarize && index == len - 1) {
); print!(
} "{}\t{}{}",
} else { convert_size(size),
if !summarize || (summarize && index == len - 1) { stat.path.display(),
print!( line_separator
"{}\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;
}
} }
} }
if options.total && index == (len - 1) { Err(error) => show_error!("{}", error),
// 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;
}
} }
} }