From 18ed99b9195512c86fac29be3577f4a1e8945991 Mon Sep 17 00:00:00 2001 From: Daniel Perrett Date: Thu, 5 Mar 2015 23:28:03 +0000 Subject: [PATCH 1/6] Added max-file-size and min-file-size options, Size.pm Conflicts: Makefile.PL --- Ack.pm | 4 ++++ ConfigLoader.pm | 4 ++++ MANIFEST | 2 ++ Makefile.PL | 3 ++- Size.pm | 50 +++++++++++++++++++++++++++++++++++++++++++++++ ack | 18 +++++++++++++++++ t/Util.pm | 4 ++++ t/config-loader.t | 30 ++++++++++++++++++++++++++++ t/lib/Size.t | 12 ++++++++++++ 9 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 Size.pm create mode 100644 t/lib/Size.t diff --git a/Ack.pm b/Ack.pm index 0fc88bd8..92006756 100644 --- a/Ack.pm +++ b/Ack.pm @@ -346,6 +346,10 @@ File inclusion/exclusion: filetype. --type=noX Exclude X files. See "ack --help-types" for supported filetypes. + --max-file-size=NUM, --max-size=NUM + Excludes files larger than this size (in bytes) + --min-file-size=NUM, --min-size=NUM + Excludes files smaller than this size (in bytes) File type specification: --type-set TYPE:FILTER:FILTERARGS diff --git a/ConfigLoader.pm b/ConfigLoader.pm index 2169e193..794bdf50 100644 --- a/ConfigLoader.pm +++ b/ConfigLoader.pm @@ -332,6 +332,10 @@ EOT => \$opt->{L}, 'm|max-count=i' => \$opt->{m}, 'match=s' => \$opt->{regex}, + 'max-size|max-file-size=i' + => \$opt->{max_file_size}, + 'min-size|min-file-size=i' + => \$opt->{min_file_size}, 'n|no-recurse' => \$opt->{n}, o => sub { $opt->{output} = '$&' }, 'output=s' => \$opt->{output}, diff --git a/MANIFEST b/MANIFEST index e5da5fc5..ae530a20 100644 --- a/MANIFEST +++ b/MANIFEST @@ -27,6 +27,7 @@ Match.pm MatchGroup.pm Resource.pm Resources.pm +Size.pm record-options squash @@ -141,6 +142,7 @@ t/lib/Match.t t/lib/MatchGroup.t t/lib/Resource.t t/lib/Resources.t +t/lib/Size.t t/home/.ackrc t/swamp/#emacs-workfile.pl# diff --git a/Makefile.PL b/Makefile.PL index 5e061231..8613f121 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -24,6 +24,7 @@ my %parms = ( 'ConfigLoader.pm' => '$(INST_LIBDIR)/App/Ack/ConfigLoader.pm', 'Filter.pm' => '$(INST_LIBDIR)/App/Ack/Filter.pm', 'Extension.pm' => '$(INST_LIBDIR)/App/Ack/Filter/Extension.pm', + 'Size.pm' => '$(INST_LIBDIR)/App/Ack/Filter/Size.pm', 'FirstLineMatch.pm' => '$(INST_LIBDIR)/App/Ack/Filter/FirstLineMatch.pm', 'Is.pm' => '$(INST_LIBDIR)/App/Ack/Filter/Is.pm', 'Match.pm' => '$(INST_LIBDIR)/App/Ack/Filter/Match.pm', @@ -95,7 +96,7 @@ ALL_PM = \ Ack.pm \ Resource.pm Resources.pm Basic.pm \ ConfigDefault.pm ConfigFinder.pm ConfigLoader.pm \ - Filter.pm Extension.pm FirstLineMatch.pm Is.pm Match.pm Default.pm Inverse.pm Collection.pm IsGroup.pm ExtensionGroup.pm MatchGroup.pm IsPath.pm IsPathGroup.pm + Filter.pm Extension.pm FirstLineMatch.pm Is.pm Match.pm Default.pm Inverse.pm Collection.pm IsGroup.pm ExtensionGroup.pm MatchGroup.pm IsPath.pm IsPathGroup.pm Size.pm TEST_VERBOSE=0 TEST_FILES=t/*.t t/lib/*.t diff --git a/Size.pm b/Size.pm new file mode 100644 index 00000000..c69b63cc --- /dev/null +++ b/Size.pm @@ -0,0 +1,50 @@ +package App::Ack::Filter::Size; + +use strict; +use warnings; +use base 'App::Ack::Filter'; + +use App::Ack::Filter (); + +sub new { + my ( $class, $min, $max ) = @_; + return bless { + min => $min, + max => $max, + }, $class; +} + +sub filter { + my ( $self, $resource ) = @_; + + my $min = $self->{'min'} || 0; + my $max = $self->{'max'}; + + my $file = $resource->name; + + return 1 if $file eq '-'; + + my $size = (-s $file) || 0; # paranoid? + + return 0 if $max and $size > $max; + return $size >= $min; +} + +sub inspect { + my ( $self ) = @_; + + my $min = $self->{'min'} || 0; + my $max = $self->{'max'} || '*'; + + return ref($self) . " - $min..$max"; +} + +sub to_string { + shift->inspect; +} + +BEGIN { + App::Ack::Filter->register_filter(size => __PACKAGE__); +} + +1; diff --git a/ack b/ack index 467078eb..9628dcf4 100644 --- a/ack +++ b/ack @@ -22,6 +22,7 @@ use App::Ack::Resource::Basic (); use App::Ack::Filter (); use App::Ack::Filter::Default; use App::Ack::Filter::Extension; +use App::Ack::Filter::Size; use App::Ack::Filter::FirstLineMatch; use App::Ack::Filter::Inverse; use App::Ack::Filter::Is; @@ -227,6 +228,8 @@ sub _compile_file_filter { return 0; } + return 0 unless App::Ack::Filter::Size->new($opt->{min_file_size}, $opt->{max_file_size})->filter($resource); + my $match_found = $direct_filters->filter($resource); # Don't bother invoking inverse filters unless we consider the current resource a match @@ -1410,6 +1413,21 @@ Print this manual page. No descending into subdirectories. +=item B<--max-file-size=I>, B<--max-size=I> + +The maximum size of files C is willing to search. + +This is useful for when you know you have a handful of extremely large files +which you do not need to search, but whose distinguishing feature is their size. + +If not set, or set to 0, then there is no maximum. + +=item B<--min-file-size=I>, B<--min-size=I> + +The minimum size of files C is willing to search. + +If not set, or set to 0, then there is no maximum. + =item B<-o> Show only the part of each line matching PATTERN (turns off text diff --git a/t/Util.pm b/t/Util.pm index a14acd61..78bc41f6 100644 --- a/t/Util.pm +++ b/t/Util.pm @@ -679,6 +679,10 @@ sub get_options { '--man', '--match', '--max-count', + '--max-file-size', + '--max-size', + '--min-file-size', + '--min-size', '--no-filename', '--no-recurse', '--nobreak', diff --git a/t/config-loader.t b/t/config-loader.t index 030a10e5..b75aeec0 100644 --- a/t/config-loader.t +++ b/t/config-loader.t @@ -36,6 +36,8 @@ my %defaults = ( l => undef, L => undef, m => undef, + max_file_size => undef, + min_file_size => undef, n => undef, output => undef, pager => undef, @@ -84,6 +86,34 @@ test_loader( '--before-context should set before_context' ); +test_loader( + argv => ['--max-size=1500'], + expected_opts => { %defaults, max_file_size => 1500 }, + expected_targets => [], + '--max-size should set max_file_size' +); + +test_loader( + argv => ['--max-file-size=1500'], + expected_opts => { %defaults, max_file_size => 1500 }, + expected_targets => [], + '--max-file-size should set max_file_size' +); + +test_loader( + argv => ['--min-size=1500'], + expected_opts => { %defaults, min_file_size => 1500 }, + expected_targets => [], + '--min-size should set min_file_size' +); + +test_loader( + argv => ['--min-file-size=1500'], + expected_opts => { %defaults, min_file_size => 1500 }, + expected_targets => [], + '--min-file-size should set min_file_size' +); + # XXX These tests should all be replicated to work off of the ack command line # tools instead of its internal APIs! do { diff --git a/t/lib/Size.t b/t/lib/Size.t new file mode 100644 index 00000000..4e0f7408 --- /dev/null +++ b/t/lib/Size.t @@ -0,0 +1,12 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 1; + +use App::Ack::Filter::Size; + +pass( 'App::Ack::Filter::Size loaded with nothing else loaded first' ); + +done_testing(); From aa6bda4fcde62a7338261d3ce5353a616728b81d Mon Sep 17 00:00:00 2001 From: Daniel Perrett Date: Fri, 6 Mar 2015 08:36:13 +0000 Subject: [PATCH 2/6] Added t/ack-size.t --- MANIFEST | 1 + t/ack-size.t | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 t/ack-size.t diff --git a/MANIFEST b/MANIFEST index ae530a20..bd7e209e 100644 --- a/MANIFEST +++ b/MANIFEST @@ -70,6 +70,7 @@ t/ack-print0.t t/ack-removed-options.t t/ack-show-types.t t/ack-s.t +t/ack-size.pl t/ack-type-del.t t/ack-type.t t/ack-v.t diff --git a/t/ack-size.t b/t/ack-size.t new file mode 100644 index 00000000..9850b375 --- /dev/null +++ b/t/ack-size.t @@ -0,0 +1,40 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 12; +use lib 't'; +use Util; + +prep_environment(); + +my ( $stdout, $stderr ); +my $help_types_output; + +# sanity check +( $stdout, $stderr ) = run_ack_with_stderr('--perl', '-f', 't/swamp'); +is( scalar(@{$stdout}), 11, 'Found initial 11 files' ); +is_empty_array( $stderr, 'Nothing in stderr' ); + +( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--max-file-size=0', '-f', 't/swamp'); +is( scalar(@{$stdout}), 11, 'Found initial 11 files (max of 0 has no effect)' ); +is_empty_array( $stderr, 'Nothing in stderr' ); + +( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--max-file-size=100', '-f', 't/swamp'); +is( scalar(@{$stdout}), 3, 'Found 3 files <= 100 bytes large' ); +is_empty_array( $stderr, 'Nothing in stderr' ); + +( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--max-file-size=101', '-f', 't/swamp'); +is( scalar(@{$stdout}), 3, 'Found 8 files >= 101 bytes large' ); +is_empty_array( $stderr, 'Nothing in stderr' ); + +( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--min-file-size=101', '--max-file-size=150', '-f', 't/swamp'); +is( scalar(@{$stdout}), 1, 'Found 1 file where 101 <= size <= 150' ); +is_empty_array( $stderr, 'Nothing in stderr' ); + +( $stdout, $stderr ) = run_ack_with_stderr('--perl', '--max-file-size=100', '--min-file-size=101', '-f', 't/swamp'); +is( scalar(@{$stdout}), 0, 'Found no files when max and min conflict' ); +is_empty_array( $stderr, 'Nothing in stderr' ); + +# done testing From 9bf5f9af5ec2d276c4cc6226f9909416e0a731c6 Mon Sep 17 00:00:00 2001 From: Daniel Perrett Date: Fri, 6 Mar 2015 17:44:12 +0000 Subject: [PATCH 3/6] Optimise Size code; allow units like 10k, 1.5MB, 2G --- ConfigLoader.pm | 9 +++++---- Size.pm | 18 ++++++++++++++++++ ack | 10 +++++++++- t/config-loader.t | 2 -- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/ConfigLoader.pm b/ConfigLoader.pm index 794bdf50..7c9b078b 100644 --- a/ConfigLoader.pm +++ b/ConfigLoader.pm @@ -7,6 +7,7 @@ use App::Ack (); use App::Ack::ConfigDefault (); use App::Ack::ConfigFinder (); use App::Ack::Filter; +use App::Ack::Filter::Size; use App::Ack::Filter::Default; use Carp 1.04 (); use Getopt::Long 2.35 (); @@ -332,10 +333,10 @@ EOT => \$opt->{L}, 'm|max-count=i' => \$opt->{m}, 'match=s' => \$opt->{regex}, - 'max-size|max-file-size=i' - => \$opt->{max_file_size}, - 'min-size|min-file-size=i' - => \$opt->{min_file_size}, + 'max-size|max-file-size=s' + => sub { $opt->{max_file_size} = App::Ack::Filter::Size::_parse_size ($_[1]) }, + 'min-size|min-file-size=s' + => sub { $opt->{min_file_size} = App::Ack::Filter::Size::_parse_size ($_[1]) }, 'n|no-recurse' => \$opt->{n}, o => sub { $opt->{output} = '$&' }, 'output=s' => \$opt->{output}, diff --git a/Size.pm b/Size.pm index c69b63cc..f1e52814 100644 --- a/Size.pm +++ b/Size.pm @@ -4,8 +4,26 @@ use strict; use warnings; use base 'App::Ack::Filter'; +use Carp 'croak'; + use App::Ack::Filter (); +sub _parse_size { + my $s = $_[0] || return 0; + + if ( $s =~ m/^\s*(\d+(?:\.\d+)?)(?:\s*([KMGT]?)B?)?\s*$/i ) { + my $n = $1; + if ($2) { + my $u = lc $2; + $n *= 1024 while $u =~ tr/tgmk/gmk/d; + } + return int $n; + } + else { + Carp::croak('Invalid size'); + } +} + sub new { my ( $class, $min, $max ) = @_; return bless { diff --git a/ack b/ack index 9628dcf4..ac0b2ac5 100644 --- a/ack +++ b/ack @@ -147,6 +147,14 @@ sub _compile_file_filter { } } + # For the usual case where the user has not set this, it is faster if we can + # we can reduce to a single boolean test before we even make the method call + # if both of min and max are 0, don't test, accept all files + + my $size_filter = ( $opt->{min_file_size} || $opt->{max_file_size} ) + ? App::Ack::Filter::Size->new($opt->{min_file_size}, $opt->{max_file_size}) + : 0; + my %is_member_of_starting_set = map { (get_file_id($_) => 1) } @{$start}; my @ignore_dir_filter = @{$opt->{idirs} || []}; @@ -228,7 +236,7 @@ sub _compile_file_filter { return 0; } - return 0 unless App::Ack::Filter::Size->new($opt->{min_file_size}, $opt->{max_file_size})->filter($resource); + return 0 if $size_filter && ! $size_filter->filter($resource); my $match_found = $direct_filters->filter($resource); diff --git a/t/config-loader.t b/t/config-loader.t index b75aeec0..3becd65b 100644 --- a/t/config-loader.t +++ b/t/config-loader.t @@ -36,8 +36,6 @@ my %defaults = ( l => undef, L => undef, m => undef, - max_file_size => undef, - min_file_size => undef, n => undef, output => undef, pager => undef, From c77b2ecdea4802f5497b0d95a955d2a16b1d7bdc Mon Sep 17 00:00:00 2001 From: Daniel Perrett Date: Fri, 6 Mar 2015 17:54:27 +0000 Subject: [PATCH 4/6] Use the same stat, i.e. -s _; no need to check for filename being "-" --- Size.pm | 4 +--- ack | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Size.pm b/Size.pm index f1e52814..5027500a 100644 --- a/Size.pm +++ b/Size.pm @@ -40,9 +40,7 @@ sub filter { my $file = $resource->name; - return 1 if $file eq '-'; - - my $size = (-s $file) || 0; # paranoid? + my $size = (-s _) || 0; # paranoid? return 0 if $max and $size > $max; return $size >= $min; diff --git a/ack b/ack index ac0b2ac5..8e1223fa 100644 --- a/ack +++ b/ack @@ -236,6 +236,11 @@ sub _compile_file_filter { return 0; } + # Warning: the size filter uses -s _: don't stat any other files + # or else you will break it. + # + # Also, it assumes we have a file name as named pipes are filtered out + # earlier. return 0 if $size_filter && ! $size_filter->filter($resource); my $match_found = $direct_filters->filter($resource); From 3652c49653372561fa13d52b8184cc0bfc3e239d Mon Sep 17 00:00:00 2001 From: Daniel Perrett Date: Mon, 9 Mar 2015 08:38:31 +0000 Subject: [PATCH 5/6] Respect KB/KiB distinction per petdance --- Size.pm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Size.pm b/Size.pm index 5027500a..45fff3c5 100644 --- a/Size.pm +++ b/Size.pm @@ -11,11 +11,12 @@ use App::Ack::Filter (); sub _parse_size { my $s = $_[0] || return 0; - if ( $s =~ m/^\s*(\d+(?:\.\d+)?)(?:\s*([KMGT]?)B?)?\s*$/i ) { + if ( $s =~ m/^\s*(\d+(?:\.\d+)?)(?:\s*([KMGT]?)(?:(i?)B)?)?\s*$/i ) { my $n = $1; if ($2) { my $u = lc $2; - $n *= 1024 while $u =~ tr/tgmk/gmk/d; + my $i = $3 ? 1024 : 1000; # 1KiB = 1024B; 1KB = 1000B + $n *= $i while $u =~ tr/tgmk/gmk/d; } return int $n; } From ea9f26b01ea220765f3a9caf0c66184bf644381a Mon Sep 17 00:00:00 2001 From: Daniel Perrett Date: Mon, 9 Mar 2015 09:04:59 +0000 Subject: [PATCH 6/6] Removed Size.pm - not a filter - per hoelzro --- ConfigLoader.pm | 5 ++-- MANIFEST | 2 -- Makefile.PL | 3 +-- Size.pm | 67 ----------------------------------------------- ack | 29 +++++++++++++++++--- t/config-loader.t | 2 ++ t/lib/Size.t | 12 --------- 7 files changed, 30 insertions(+), 90 deletions(-) delete mode 100644 Size.pm delete mode 100644 t/lib/Size.t diff --git a/ConfigLoader.pm b/ConfigLoader.pm index 7c9b078b..8166fbee 100644 --- a/ConfigLoader.pm +++ b/ConfigLoader.pm @@ -7,7 +7,6 @@ use App::Ack (); use App::Ack::ConfigDefault (); use App::Ack::ConfigFinder (); use App::Ack::Filter; -use App::Ack::Filter::Size; use App::Ack::Filter::Default; use Carp 1.04 (); use Getopt::Long 2.35 (); @@ -334,9 +333,9 @@ EOT 'm|max-count=i' => \$opt->{m}, 'match=s' => \$opt->{regex}, 'max-size|max-file-size=s' - => sub { $opt->{max_file_size} = App::Ack::Filter::Size::_parse_size ($_[1]) }, + => \$opt->{max_file_size}, 'min-size|min-file-size=s' - => sub { $opt->{min_file_size} = App::Ack::Filter::Size::_parse_size ($_[1]) }, + => \$opt->{min_file_size}, 'n|no-recurse' => \$opt->{n}, o => sub { $opt->{output} = '$&' }, 'output=s' => \$opt->{output}, diff --git a/MANIFEST b/MANIFEST index bd7e209e..d79bac32 100644 --- a/MANIFEST +++ b/MANIFEST @@ -27,7 +27,6 @@ Match.pm MatchGroup.pm Resource.pm Resources.pm -Size.pm record-options squash @@ -143,7 +142,6 @@ t/lib/Match.t t/lib/MatchGroup.t t/lib/Resource.t t/lib/Resources.t -t/lib/Size.t t/home/.ackrc t/swamp/#emacs-workfile.pl# diff --git a/Makefile.PL b/Makefile.PL index 8613f121..5e061231 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -24,7 +24,6 @@ my %parms = ( 'ConfigLoader.pm' => '$(INST_LIBDIR)/App/Ack/ConfigLoader.pm', 'Filter.pm' => '$(INST_LIBDIR)/App/Ack/Filter.pm', 'Extension.pm' => '$(INST_LIBDIR)/App/Ack/Filter/Extension.pm', - 'Size.pm' => '$(INST_LIBDIR)/App/Ack/Filter/Size.pm', 'FirstLineMatch.pm' => '$(INST_LIBDIR)/App/Ack/Filter/FirstLineMatch.pm', 'Is.pm' => '$(INST_LIBDIR)/App/Ack/Filter/Is.pm', 'Match.pm' => '$(INST_LIBDIR)/App/Ack/Filter/Match.pm', @@ -96,7 +95,7 @@ ALL_PM = \ Ack.pm \ Resource.pm Resources.pm Basic.pm \ ConfigDefault.pm ConfigFinder.pm ConfigLoader.pm \ - Filter.pm Extension.pm FirstLineMatch.pm Is.pm Match.pm Default.pm Inverse.pm Collection.pm IsGroup.pm ExtensionGroup.pm MatchGroup.pm IsPath.pm IsPathGroup.pm Size.pm + Filter.pm Extension.pm FirstLineMatch.pm Is.pm Match.pm Default.pm Inverse.pm Collection.pm IsGroup.pm ExtensionGroup.pm MatchGroup.pm IsPath.pm IsPathGroup.pm TEST_VERBOSE=0 TEST_FILES=t/*.t t/lib/*.t diff --git a/Size.pm b/Size.pm deleted file mode 100644 index 45fff3c5..00000000 --- a/Size.pm +++ /dev/null @@ -1,67 +0,0 @@ -package App::Ack::Filter::Size; - -use strict; -use warnings; -use base 'App::Ack::Filter'; - -use Carp 'croak'; - -use App::Ack::Filter (); - -sub _parse_size { - my $s = $_[0] || return 0; - - if ( $s =~ m/^\s*(\d+(?:\.\d+)?)(?:\s*([KMGT]?)(?:(i?)B)?)?\s*$/i ) { - my $n = $1; - if ($2) { - my $u = lc $2; - my $i = $3 ? 1024 : 1000; # 1KiB = 1024B; 1KB = 1000B - $n *= $i while $u =~ tr/tgmk/gmk/d; - } - return int $n; - } - else { - Carp::croak('Invalid size'); - } -} - -sub new { - my ( $class, $min, $max ) = @_; - return bless { - min => $min, - max => $max, - }, $class; -} - -sub filter { - my ( $self, $resource ) = @_; - - my $min = $self->{'min'} || 0; - my $max = $self->{'max'}; - - my $file = $resource->name; - - my $size = (-s _) || 0; # paranoid? - - return 0 if $max and $size > $max; - return $size >= $min; -} - -sub inspect { - my ( $self ) = @_; - - my $min = $self->{'min'} || 0; - my $max = $self->{'max'} || '*'; - - return ref($self) . " - $min..$max"; -} - -sub to_string { - shift->inspect; -} - -BEGIN { - App::Ack::Filter->register_filter(size => __PACKAGE__); -} - -1; diff --git a/ack b/ack index 8e1223fa..8665cd0d 100644 --- a/ack +++ b/ack @@ -22,7 +22,6 @@ use App::Ack::Resource::Basic (); use App::Ack::Filter (); use App::Ack::Filter::Default; use App::Ack::Filter::Extension; -use App::Ack::Filter::Size; use App::Ack::Filter::FirstLineMatch; use App::Ack::Filter::Inverse; use App::Ack::Filter::Is; @@ -99,6 +98,23 @@ MAIN: { main(); } +sub _parse_size { + my $s = $_[0] || return 0; + + if ( $s =~ m/^\s*(\d+(?:\.\d+)?)(?:\s*([KMGT]?)(?:(i?)B)?)?\s*$/i ) { + my $n = $1; + if ($2) { + my $u = lc $2; + my $i = $3 ? 1024 : 1000; # 1KiB = 1024B; 1KB = 1000B + $n *= $i while $u =~ tr/tgmk/gmk/d; + } + return int $n; + } + else { + Carp::croak('Invalid size'); + } +} + sub _compile_descend_filter { my ( $opt ) = @_; @@ -151,8 +167,13 @@ sub _compile_file_filter { # we can reduce to a single boolean test before we even make the method call # if both of min and max are 0, don't test, accept all files - my $size_filter = ( $opt->{min_file_size} || $opt->{max_file_size} ) - ? App::Ack::Filter::Size->new($opt->{min_file_size}, $opt->{max_file_size}) + my ( $min_file_size, $max_file_size ) = map { _parse_size( $opt->{"${_}_file_size"} ) } qw ( min max ); + my $size_filter = ( $min_file_size || $max_file_size ) + ? sub { + my $size = (-s _) || 0; # paranoid? + return 0 if $max_file_size and $size > $max_file_size; + return $size >= $min_file_size; + } : 0; my %is_member_of_starting_set = map { (get_file_id($_) => 1) } @{$start}; @@ -241,7 +262,7 @@ sub _compile_file_filter { # # Also, it assumes we have a file name as named pipes are filtered out # earlier. - return 0 if $size_filter && ! $size_filter->filter($resource); + return 0 if $size_filter && ! $size_filter->($resource); my $match_found = $direct_filters->filter($resource); diff --git a/t/config-loader.t b/t/config-loader.t index 3becd65b..b75aeec0 100644 --- a/t/config-loader.t +++ b/t/config-loader.t @@ -36,6 +36,8 @@ my %defaults = ( l => undef, L => undef, m => undef, + max_file_size => undef, + min_file_size => undef, n => undef, output => undef, pager => undef, diff --git a/t/lib/Size.t b/t/lib/Size.t deleted file mode 100644 index 4e0f7408..00000000 --- a/t/lib/Size.t +++ /dev/null @@ -1,12 +0,0 @@ -#!perl -T - -use strict; -use warnings; - -use Test::More tests => 1; - -use App::Ack::Filter::Size; - -pass( 'App::Ack::Filter::Size loaded with nothing else loaded first' ); - -done_testing();