mirror of
https://github.com/uutils/coreutils
synced 2024-12-16 00:02:50 +00:00
du: remove crash! macro
This commit is contained in:
parent
44d105d015
commit
e11878e7ba
1 changed files with 28 additions and 21 deletions
|
@ -31,13 +31,11 @@ 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::FromIo;
|
use uucore::error::FromIo;
|
||||||
use uucore::error::{set_exit_code, UError, UResult};
|
use uucore::error::{set_exit_code, UError, UResult, USimpleError};
|
||||||
use uucore::line_ending::LineEnding;
|
use uucore::line_ending::LineEnding;
|
||||||
use uucore::parse_glob;
|
use uucore::parse_glob;
|
||||||
use uucore::parse_size::{parse_size_u64, ParseSizeError};
|
use uucore::parse_size::{parse_size_u64, ParseSizeError};
|
||||||
use uucore::{
|
use uucore::{format_usage, help_about, help_section, help_usage, show, show_error, show_warning};
|
||||||
crash, format_usage, help_about, help_section, help_usage, show, show_error, show_warning,
|
|
||||||
};
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use windows_sys::Win32::Foundation::HANDLE;
|
use windows_sys::Win32::Foundation::HANDLE;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
|
@ -255,22 +253,27 @@ fn get_file_info(path: &Path) -> Option<FileInfo> {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_block_size(s: Option<&str>) -> u64 {
|
fn read_block_size(s: Option<&str>) -> UResult<u64> {
|
||||||
if let Some(s) = s {
|
if let Some(s) = s {
|
||||||
parse_size_u64(s)
|
match parse_size_u64(s) {
|
||||||
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(&e, s, options::BLOCK_SIZE)))
|
Ok(x) => Ok(x),
|
||||||
|
Err(e) => Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
format_error_message(&e, s, options::BLOCK_SIZE),
|
||||||
|
)),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
for env_var in ["DU_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
|
for env_var in ["DU_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
|
||||||
if let Ok(env_size) = env::var(env_var) {
|
if let Ok(env_size) = env::var(env_var) {
|
||||||
if let Ok(v) = parse_size_u64(&env_size) {
|
if let Ok(v) = parse_size_u64(&env_size) {
|
||||||
return v;
|
return Ok(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if env::var("POSIXLY_CORRECT").is_ok() {
|
if env::var("POSIXLY_CORRECT").is_ok() {
|
||||||
512
|
Ok(512)
|
||||||
} else {
|
} else {
|
||||||
1024
|
Ok(1024)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -574,12 +577,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
matches
|
matches
|
||||||
.get_one::<String>(options::BLOCK_SIZE)
|
.get_one::<String>(options::BLOCK_SIZE)
|
||||||
.map(|s| s.as_str()),
|
.map(|s| s.as_str()),
|
||||||
);
|
)?;
|
||||||
|
|
||||||
let threshold = matches.get_one::<String>(options::THRESHOLD).map(|s| {
|
let threshold = match matches.get_one::<String>(options::THRESHOLD) {
|
||||||
Threshold::from_str(s)
|
Some(s) => match Threshold::from_str(s) {
|
||||||
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(&e, s, options::THRESHOLD)))
|
Ok(t) => Some(t),
|
||||||
});
|
Err(e) => {
|
||||||
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
format_error_message(&e, s, options::THRESHOLD),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
let multiplier: u64 = if matches.get_flag(options::SI) {
|
let multiplier: u64 = if matches.get_flag(options::SI) {
|
||||||
1000
|
1000
|
||||||
|
@ -991,13 +1002,9 @@ mod test_du {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_read_block_size() {
|
fn test_read_block_size() {
|
||||||
let test_data = [
|
let test_data = [Some("1024".to_string()), Some("K".to_string()), None];
|
||||||
(Some("1024".to_string()), 1024),
|
|
||||||
(Some("K".to_string()), 1024),
|
|
||||||
(None, 1024),
|
|
||||||
];
|
|
||||||
for it in &test_data {
|
for it in &test_data {
|
||||||
assert_eq!(read_block_size(it.0.as_deref()), it.1);
|
assert!(matches!(read_block_size(it.as_deref()), Ok(1024)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue