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::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,17 +54,21 @@ 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)); match fs::symlink_metadata(&path) {
Stat { Ok(metadata) => {
path: path, return Ok(Stat {
is_dir: metadata.is_dir(), path: path,
size: metadata.len(), is_dir: metadata.is_dir(),
blocks: metadata.blocks() as u64, size: metadata.len(),
nlink: metadata.nlink() as u64, blocks: metadata.blocks() as u64,
created: metadata.mtime() as u64, nlink: metadata.nlink() as u64,
accessed: metadata.atime() as u64, created: metadata.mtime() as u64,
modified: 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() { for f in read.into_iter() {
match f { match f {
Ok(entry) => match fs::symlink_metadata(entry.path()) { Ok(entry) => match Stat::new(entry.path()) {
Ok(_) => { Ok(this_stat) => {
let this_stat = Stat::new(entry.path());
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 {
@ -334,67 +337,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;
}
} }
} }