Skip to content
Myr

Add exact size support to the --size filter (#669)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Platon Pronko · committed by David Peter · parent 2338b3b · Browse files · 4 files+20 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+20 added−7 removed

  • tests:+7 added−0 removed
  • other:+13 added−7 removed

4 files+20 added−7 removed

modified:doc/fd.1+2 added
177 unmodified lines
178.IP '+'179file size must be greater than or equal to this180.IP '-'181file size must be less than or equal to this182.P183If neither '+' nor '-' is specified, file size must be exactly equal to this.184.IP 'NUM'185The numeric size (e.g. 500)186.IP 'UNIT'187The units for NUM. They are not case-sensitive.
139 unmodified lines
modified:src/filter/size.rs+9 added-6 removed
1use lazy_static::lazy_static;2use regex::Regex;34lazy_static! {5 static ref SIZE_CAPTURES: Regex = Regex::new(r"(?i)^([+-])(\d+)(b|[kmgt]i?b?)$").unwrap();5 static ref SIZE_CAPTURES: Regex = Regex::new(r"(?i)^([+-]?)(\d+)(b|[kmgt]i?b?)$").unwrap();6}78#[derive(Clone, Copy, Debug, PartialEq)]9pub enum SizeFilter {10 Max(u64),11 Min(u64),12 Equals(u64),13}1415// SI prefixes (powers of 10)16const KILO: u64 = 1000;
32 unmodified lines
49 _ => return None,50 };5152 let size = quantity * multiplier;52 Some(match limit_kind {53 "+" => SizeFilter::Min(size),54 _ => SizeFilter::Max(size),55 })53 match limit_kind {54 "+" => Some(SizeFilter::Min(size)),55 "-" => Some(SizeFilter::Max(size)),56 "" => Some(SizeFilter::Equals(size)),57 _ => None58 }59 }6061 pub fn is_within(&self, size: u64) -> bool {62 match *self {63 SizeFilter::Max(limit) => size <= limit,64 SizeFilter::Min(limit) => size >= limit,65 SizeFilter::Equals(limit) => size == limit,66 }67 }68}69
103 unmodified lines
173 }174175 // Invalid parse data176 gen_size_filter_failure! {173 ensure_missing_symbol_returns_none: "10M",177 ensure_missing_number_returns_none: "+g",178 ensure_missing_unit_returns_none: "+18",179 ensure_bad_format_returns_none_1: "$10M",180 ensure_bad_format_returns_none_2: "badval",
32 unmodified lines
modified:src/app.rs+2 added-1 removed
388 unmodified lines
389 .help("Limit results based on the size of files.")390 .long_help(391 "Limit results based on the size of files using the format <+-><NUM><UNIT>.\n \392 '+': file size must be greater than or equal to this\n \393 '-': file size must be less than or equal to this\n \393 '-': file size must be less than or equal to this\n\394 If neither '+' nor '-' is specified, file size must be exactly equal to this.\n \395 'NUM': The numeric size (e.g. 500)\n \396 'UNIT': The units for NUM. They are not case-sensitive.\n\397 Allowed unit values:\n \398 'b': bytes\n \
184 unmodified lines
modified:tests/tests.rs+7 added
1385 unmodified lines
1386 );13871388 // Zero sized files.1389 te.assert_output(&["", "--size", "-0B"], "0_bytes.foo");1390 te.assert_output(&["", "--size", "0B"], "0_bytes.foo");1391 te.assert_output(&["", "--size=0B"], "0_bytes.foo");1392 te.assert_output(&["", "-S", "0B"], "0_bytes.foo");13931394 // Files with 2 bytes or more.1395 te.assert_output(1396 &["", "--size", "+2B"],
8 unmodified lines
14051406 // Files with size between 1 byte and 11 bytes.1407 te.assert_output(&["", "--size", "+1B", "--size", "-11B"], "11_bytes.foo");14081409 // Files with size equal 11 bytes.1410 te.assert_output(&["", "--size", "11B"], "11_bytes.foo");14111412 // Files with size between 1 byte and 30 bytes.1413 te.assert_output(1414 &["", "--size", "+1B", "--size", "-30B"],1415 "11_bytes.foo
23 unmodified lines
1439 te.assert_output(&["", "--size", "+3k", "--size", "-3ki"], "3_kilobytes.foo");14401441 // Files with size equal 4 kibibytes.1442 te.assert_output(&["", "--size", "+4ki", "--size", "-4ki"], "4_kibibytes.foo");1443 te.assert_output(&["", "--size", "4ki"], "4_kibibytes.foo");1444}14451446#[cfg(test)]1447fn create_file_with_modified<P: AsRef<Path>>(path: P, duration_in_secs: u64) {
226 unmodified lines