1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 21:47:46 +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,8 +91,9 @@ 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()) {
Ok(this_stat) => {
if this_stat.is_dir { if this_stat.is_dir {
futures.push(du(this_stat, options, depth + 1)); futures.push(du(this_stat, options, depth + 1));
} else { } else {
@ -103,6 +104,11 @@ fn du(mut my_stat: Stat, options: &Options, depth: usize) -> Box<DoubleEndedIter
} }
} }
} }
Err(error) => show_error!("{}", error),
},
Err(error) => show_error!("{}", error),
}
}
} }
stats.extend( stats.extend(
@ -327,7 +333,9 @@ 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) {
Ok(stat) => {
let iter = du(stat, &options, 0).into_iter();
let (_, len) = iter.size_hint(); let (_, len) = iter.size_hint();
let len = len.unwrap(); let len = len.unwrap();
for (index, stat) in iter.enumerate() { for (index, stat) in iter.enumerate() {
@ -390,6 +398,9 @@ Try '{} --help' for more information.",
} }
} }
} }
Err(error) => show_error!("{}", error),
}
}
if options.total { if options.total {
print!("{}\ttotal", convert_size(grand_total)); print!("{}\ttotal", convert_size(grand_total));