mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
du: use common error methods with show! instead of set_exit_code
This commit is contained in:
parent
6747e7261e
commit
88a62c4922
2 changed files with 9 additions and 25 deletions
|
@ -20,7 +20,7 @@ use std::fs::File;
|
||||||
use std::fs::Metadata;
|
use std::fs::Metadata;
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::io::{ErrorKind, Result};
|
use std::io::Result;
|
||||||
use std::iter;
|
use std::iter;
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
use std::os::unix::fs::MetadataExt;
|
use std::os::unix::fs::MetadataExt;
|
||||||
|
@ -34,7 +34,8 @@ use std::str::FromStr;
|
||||||
use std::time::{Duration, UNIX_EPOCH};
|
use std::time::{Duration, UNIX_EPOCH};
|
||||||
use std::{error::Error, fmt::Display};
|
use std::{error::Error, fmt::Display};
|
||||||
use uucore::display::{print_verbatim, Quotable};
|
use uucore::display::{print_verbatim, Quotable};
|
||||||
use uucore::error::{set_exit_code, UError, UResult};
|
use uucore::error::FromIo;
|
||||||
|
use uucore::error::{UError, UResult};
|
||||||
use uucore::format_usage;
|
use uucore::format_usage;
|
||||||
use uucore::parse_size::{parse_size, ParseSizeError};
|
use uucore::parse_size::{parse_size, ParseSizeError};
|
||||||
use uucore::InvalidEncodingHandling;
|
use uucore::InvalidEncodingHandling;
|
||||||
|
@ -102,7 +103,6 @@ const UNITS: [(char, u32); 6] = [('E', 6), ('P', 5), ('T', 4), ('G', 3), ('M', 2
|
||||||
|
|
||||||
struct Options {
|
struct Options {
|
||||||
all: bool,
|
all: bool,
|
||||||
util_name: String,
|
|
||||||
max_depth: Option<usize>,
|
max_depth: Option<usize>,
|
||||||
total: bool,
|
total: bool,
|
||||||
separate_dirs: bool,
|
separate_dirs: bool,
|
||||||
|
@ -309,13 +309,9 @@ fn du(
|
||||||
let read = match fs::read_dir(&my_stat.path) {
|
let read = match fs::read_dir(&my_stat.path) {
|
||||||
Ok(read) => read,
|
Ok(read) => read,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!(
|
show!(
|
||||||
"{}: cannot read directory {}: {}",
|
e.map_err_context(|| format!("cannot read directory {}", my_stat.path.quote()))
|
||||||
options.util_name,
|
|
||||||
my_stat.path.quote(),
|
|
||||||
e
|
|
||||||
);
|
);
|
||||||
set_exit_code(1);
|
|
||||||
return Box::new(iter::once(my_stat));
|
return Box::new(iter::once(my_stat));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -368,18 +364,9 @@ fn du(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(error) => match error.kind() {
|
Err(e) => show!(
|
||||||
ErrorKind::PermissionDenied => {
|
e.map_err_context(|| format!("cannot access {}", entry.path().quote()))
|
||||||
let description = format!("cannot access {}", entry.path().quote());
|
),
|
||||||
let error_message = "Permission denied";
|
|
||||||
show_error_custom_description!(description, "{}", error_message);
|
|
||||||
set_exit_code(1);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
set_exit_code(1);
|
|
||||||
show_error!("cannot access {}: {}", entry.path().quote(), error);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(error) => show_error!("{}", error),
|
Err(error) => show_error!("{}", error),
|
||||||
|
@ -567,7 +554,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
|
|
||||||
let options = Options {
|
let options = Options {
|
||||||
all: matches.is_present(options::ALL),
|
all: matches.is_present(options::ALL),
|
||||||
util_name: uucore::util_name().to_owned(),
|
|
||||||
max_depth,
|
max_depth,
|
||||||
total: matches.is_present(options::TOTAL),
|
total: matches.is_present(options::TOTAL),
|
||||||
separate_dirs: matches.is_present(options::SEPARATE_DIRS),
|
separate_dirs: matches.is_present(options::SEPARATE_DIRS),
|
||||||
|
|
|
@ -435,9 +435,7 @@ fn test_du_no_permission() {
|
||||||
ts.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).succeeds();
|
ts.ccmd("chmod").arg("-r").arg(SUB_DIR_LINKS).succeeds();
|
||||||
|
|
||||||
let result = ts.ucmd().arg(SUB_DIR_LINKS).fails();
|
let result = ts.ucmd().arg(SUB_DIR_LINKS).fails();
|
||||||
result.stderr_contains(
|
result.stderr_contains("du: cannot read directory 'subdir/links': Permission denied");
|
||||||
"du: cannot read directory 'subdir/links': Permission denied (os error 13)",
|
|
||||||
);
|
|
||||||
|
|
||||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue