1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-02 05:57:46 +00:00

Refactor Stat::new to return Result

This was to remove the double call to fs::symlink_metadata
This commit is contained in:
bootandy 2018-03-14 16:26:22 -04:00
parent f0e25e5537
commit be79a70572

View file

@ -16,7 +16,7 @@ extern crate uucore;
use std::fs;
use std::iter;
use std::io::{stderr, Write};
use std::io::{stderr, Result, Write};
use std::os::unix::fs::MetadataExt;
use std::path::PathBuf;
use time::Timespec;
@ -54,9 +54,10 @@ struct Stat {
}
impl Stat {
fn new(path: PathBuf) -> Stat {
let metadata = safe_unwrap!(fs::symlink_metadata(&path));
Stat {
fn new(path: PathBuf) -> Result<Stat> {
match fs::symlink_metadata(&path) {
Ok(metadata) => {
return Ok(Stat {
path: path,
is_dir: metadata.is_dir(),
size: metadata.len(),
@ -65,6 +66,9 @@ impl Stat {
created: metadata.mtime() as u64,
accessed: metadata.atime() as u64,
modified: metadata.mtime() as u64,
})
}
Err(e) => Err(e),
}
}
}
@ -92,9 +96,8 @@ fn du(mut my_stat: Stat, options: &Options, depth: usize) -> Box<DoubleEndedIter
for f in read.into_iter() {
match f {
Ok(entry) => match fs::symlink_metadata(entry.path()) {
Ok(_) => {
let this_stat = Stat::new(entry.path());
Ok(entry) => match Stat::new(entry.path()) {
Ok(this_stat) => {
if this_stat.is_dir {
futures.push(du(this_stat, options, depth + 1));
} else {
@ -334,7 +337,9 @@ Try '{} --help' for more information.",
let mut grand_total = 0;
for path_str in strs.into_iter() {
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 = len.unwrap();
for (index, stat) in iter.enumerate() {
@ -397,6 +402,9 @@ Try '{} --help' for more information.",
}
}
}
Err(error) => show_error!("{}", error),
}
}
if options.total {
print!("{}\ttotal", convert_size(grand_total));