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:
parent
f0e25e5537
commit
be79a70572
1 changed files with 78 additions and 70 deletions
24
src/du/du.rs
24
src/du/du.rs
|
@ -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,10 @@ 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) => {
|
||||||
|
return Ok(Stat {
|
||||||
path: path,
|
path: path,
|
||||||
is_dir: metadata.is_dir(),
|
is_dir: metadata.is_dir(),
|
||||||
size: metadata.len(),
|
size: metadata.len(),
|
||||||
|
@ -65,6 +66,9 @@ 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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
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,7 +337,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() {
|
||||||
|
@ -397,6 +402,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));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue