Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ use std::time::{Duration, UNIX_EPOCH};
use std::{error::Error, fmt::Display};
use uucore::display::{print_verbatim, Quotable};
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::parse_glob;
use uucore::parse_size::{parse_size_u64, ParseSizeError};
use uucore::{
crash, format_usage, help_about, help_section, help_usage, show, show_error, show_warning,
};
use uucore::{format_usage, help_about, help_section, help_usage, show, show_error, show_warning};
#[cfg(windows)]
use windows_sys::Win32::Foundation::HANDLE;
#[cfg(windows)]
Expand Down Expand Up @@ -255,22 +253,22 @@ fn get_file_info(path: &Path) -> Option<FileInfo> {
result
}

fn read_block_size(s: Option<&str>) -> u64 {
fn read_block_size(s: Option<&str>) -> UResult<u64> {
if let Some(s) = s {
parse_size_u64(s)
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(&e, s, options::BLOCK_SIZE)))
.map_err(|e| USimpleError::new(1, format_error_message(&e, s, options::BLOCK_SIZE)))
} else {
for env_var in ["DU_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
if let Ok(env_size) = env::var(env_var) {
if let Ok(v) = parse_size_u64(&env_size) {
return v;
return Ok(v);
}
}
}
if env::var("POSIXLY_CORRECT").is_ok() {
512
Ok(512)
} else {
1024
Ok(1024)
}
}
}
Expand Down Expand Up @@ -574,12 +572,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
matches
.get_one::<String>(options::BLOCK_SIZE)
.map(|s| s.as_str()),
);
)?;

let threshold = matches.get_one::<String>(options::THRESHOLD).map(|s| {
Threshold::from_str(s)
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(&e, s, options::THRESHOLD)))
});
let threshold = match matches.get_one::<String>(options::THRESHOLD) {
Some(s) => match Threshold::from_str(s) {
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) {
1000
Expand Down Expand Up @@ -991,13 +997,9 @@ mod test_du {

#[test]
fn test_read_block_size() {
let test_data = [
(Some("1024".to_string()), 1024),
(Some("K".to_string()), 1024),
(None, 1024),
];
let test_data = [Some("1024".to_string()), Some("K".to_string()), None];
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)));
}
}
}