mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 20:17:45 +00:00
du: Refactor threshold handling
This commit is contained in:
parent
fa12b46c51
commit
da7b02cf9d
1 changed files with 22 additions and 20 deletions
|
@ -599,11 +599,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
|
|
||||||
let block_size = u64::try_from(read_block_size(matches.value_of(options::BLOCK_SIZE))).unwrap();
|
let block_size = u64::try_from(read_block_size(matches.value_of(options::BLOCK_SIZE))).unwrap();
|
||||||
|
|
||||||
let threshold = match matches.value_of(options::THRESHOLD) {
|
let threshold = matches.value_of(options::THRESHOLD).map(|s| {
|
||||||
Some(s) => Threshold::from_str(s)
|
Threshold::from_str(s)
|
||||||
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(e, s, options::THRESHOLD))),
|
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(e, s, options::THRESHOLD)))
|
||||||
None => Threshold(None, false),
|
});
|
||||||
};
|
|
||||||
|
|
||||||
let multiplier: u64 = if matches.is_present(options::SI) {
|
let multiplier: u64 = if matches.is_present(options::SI) {
|
||||||
1000
|
1000
|
||||||
|
@ -673,7 +672,8 @@ Try '{} --help' for more information.",
|
||||||
// See: http://linux.die.net/man/2/stat
|
// See: http://linux.die.net/man/2/stat
|
||||||
stat.blocks * 512
|
stat.blocks * 512
|
||||||
};
|
};
|
||||||
if threshold.should_exclude(size) {
|
|
||||||
|
if threshold.map_or(false, |threshold| threshold.should_exclude(size)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -743,31 +743,33 @@ Try '{} --help' for more information.",
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Threshold(Option<u64>, bool);
|
#[derive(Clone, Copy)]
|
||||||
|
enum Threshold {
|
||||||
|
Lower(u64),
|
||||||
|
Upper(u64),
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for Threshold {
|
impl FromStr for Threshold {
|
||||||
type Err = ParseSizeError;
|
type Err = ParseSizeError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
|
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
|
||||||
let offset = if s.starts_with('-') || s.starts_with('+') {
|
let offset = if s.starts_with(&['-', '+'][..]) { 1 } else { 0 };
|
||||||
1
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
};
|
|
||||||
let sz = parse_size(&s[offset..])?;
|
|
||||||
|
|
||||||
Ok(Threshold(
|
let size = u64::try_from(parse_size(&s[offset..])?).unwrap();
|
||||||
Some(u64::try_from(sz).unwrap()),
|
|
||||||
s.starts_with('-'),
|
if s.starts_with('-') {
|
||||||
))
|
Ok(Threshold::Upper(size))
|
||||||
|
} else {
|
||||||
|
Ok(Threshold::Lower(size))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Threshold {
|
impl Threshold {
|
||||||
fn should_exclude(&self, sz: u64) -> bool {
|
fn should_exclude(&self, size: u64) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
Threshold(Some(th), is_upper) => (is_upper && sz > th) || (!is_upper && sz < th),
|
Threshold::Upper(threshold) => size > threshold,
|
||||||
Threshold(None, _) => false,
|
Threshold::Lower(threshold) => size < threshold,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue