blib/lib/App/Ack/File.pm

filter: Statementstmt 87.8% 65 / 74 Branchbran 72.2% 26 / 36 Conditioncond 66.6% 12 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 13 / 13 Podpod 88.8% 8 / 9 totaltotal 79.5% 128 / 161 CC 25 SCAR 16.8
CC
25
Cov
80%
may_be_present
9.6
firstliney
7.3
reset
5.5
CRAP 5.4
« blib/lib/App/Ack.pm ↑ Summary blib/lib/App/Ack/ConfigLoader.pm »
1 package App::Ack::File;
2  
3 585 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 585 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 585 ▶use App::Ack ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 585 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 App::Ack::File
12  
13 =head1 DESCRIPTION
14  
15 Abstracts a file from the filesystem.
16  
17 =head1 METHODS
18  
19 =head2 new( $filename )
20  
21 Opens the file specified by I<$filename> and returns a filehandle and
22 a flag that says whether it could be binary.
23  
24 If there's a failure, it throws a warning and returns an empty list.
25  
26 =cut
27  
28 17505 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
29 17505 my $class = shift;
30 17505 my $filename = shift;
31  
32 17505 my $self = bless {
33 filename => $filename,
34 fh => undef,
35 }, $class;
36  
37 17505 ▶ if ( $self->{filename} eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filename"} eq "-") 5 17500 17505
38 5 $self->{fh} = *STDIN;
39 }
40  
41 17505 return $self;
42 }
43  
44  
45 =head2 $file->name()
46  
47 Returns the name of the file.
48  
49 =cut
50  
51 37850 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
52 37850 return $_[0]->{filename};
53 }
54  
55  
56 =head2 $file->basename()
57  
58 Returns the basename (the last component the path)
59 of the file.
60  
61 =cut
62  
63 24520 ▶sub basename {
Subroutine basename: called CC=2 SCAR=7.1
Pod documented
64 24520 my ( $self ) = @_;
65  
66 24520 ▶ return $self->{basename} //= (File::Spec->splitpath($self->name))[2];
Condition66%2/3
$self->{"basename"} //= ("File::Spec"->splitpath($self->name))[2]
l!l&&r!l&&!r
11546129740
MC/DC0%0/2
$self->{"basename"} //= ("File::Spec"->splitpath($self->name))[2]
$self->{"basename"} ("File::Spec"->splitpath($self->name))[2]
Truth table66%2/3
A //= B
A= $self->{"basename"}B= ("File::Spec"->splitpath($self->name))[2]
ABresult
1X1
011
000
67 }
68  
69  
70 =head2 $file->open()
71  
72 Opens a filehandle for reading this file and returns it, or returns
73 undef if the operation fails (the error is in C<$!>). Instead of calling
74 C<close $fh>, C<$file-E<gt>close> should be called.
75  
76 =cut
77  
78 6570 ▶sub open {
Subroutine open: called CC=3 SCAR=11.0
Pod documented
79 6570 my ( $self ) = @_;
80  
81 6570 ▶ if ( !$self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}) 4607 1963 6570
82 4607 ▶ if ( open $self->{fh}, '<', $self->{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::open $self->{'fh'}, '<', $self->{'filename'}) { } 4600 7 4607
83 # Do nothing.
84 }
85 else {
86 7 $self->{fh} = undef;
87 }
88 }
89  
90 6570 return $self->{fh};
91 }
92  
93  
94 3395 ▶sub may_be_present {
Subroutine may_be_present: called CC=9 SCAR=22.7
Pod undocumented
95 3395 my $self = shift;
96 3395 my $regex = shift;
97  
98 # Tells if the file needs a line-by-line scan. This is a big
99 # optimization because if you can tell from the outset that the pattern
100 # is not found in the file at all, then there's no need to do the
101 # line-by-line iteration.
102  
103 # Slurp up an entire file up to 10M, see if there are any matches
104 # in it, and if so, let us know so we can iterate over it directly.
105  
106 # The $regex may be undef if it had a "$" in it, and is therefore unsuitable for this heuristic.
107  
108 3395 my $may_be_present = 1;
109 3395 ▶ if ( $regex && $self->open() && -f $self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex and $self->open and -f $self->{"fh"}) 3304 91 3395
Condition100%6/6
$regex and $self->open and -f $self->{"fh"}
!ll&&!rl&&r
8653304
$regex and $self->open
!ll&&!rl&&r
8333309
MC/DC100%3/3
$regex and $self->open and -f $self->{"fh"}
$regex $self->open -f $self->{"fh"}
Truth table100%4/4
A and B and C
A= $regexB= $self->openC= -f $self->{"fh"}
ABCresult
0XX0
10X0
1100
1111
110 3304 my $buffer;
111 3304 my $size = 10_000_000;
112 3304 my $rc = sysread( $self->{fh}, $buffer, $size );
113 3304 ▶ if ( !defined($rc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rc) { } 0 3304 3304
114 0 ▶ if ( $App::Ack::report_bad_filenames ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Ack::report_bad_filenames) 0 0 0
115 0 App::Ack::warn( $self->name . ": $!" );
116 }
117 0 $may_be_present = 0;
118 }
119 else {
120 # If we read all 10M, then we need to scan the rest.
121 # If there are any carriage returns, our results are flaky, so scan the rest.
122 3304 ▶ if ( ($rc == $size) || (index($buffer,"\r") >= 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == $size or (index($buffer, "\r") != -1)) { } 36 3268 3304
Condition66%2/3
$rc == $size or (index($buffer, "\r") != -1)
l!l&&r!l&&!r
0363268
MC/DC50%1/2
$rc == $size or (index($buffer, "\r") != -1)
$rc == $size (index($buffer, "\r") != -1)
Truth table66%2/3
A or B
A= $rc == $sizeB= (index($buffer, "\r") != -1)
ABresult
1X1
011
000
123 36 $may_be_present = 1;
124 }
125 else {
126 3268 ▶ if ( $buffer !~ /$regex/o ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer =~ /$regex/o) 2533 735 3268
127 2533 $may_be_present = 0;
128 }
129 }
130 }
131 }
132  
133 3395 return $may_be_present;
134 }
135  
136  
137 =head2 $file->reset()
138  
139 Resets the file back to the beginning. This is only called if
140 C<may_be_present()> is true, but not always if C<may_be_present()>
141 is true.
142  
143 =cut
144  
145 2063 ▶sub reset {
Subroutine reset: called CC=5 SCAR=17.0
Pod documented
146 2063 my $self = shift;
147  
148 2063 ▶ if ( defined($self->{fh}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fh"}) 2016 47 2063
149 2016 ▶ return unless -f $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"fh"} 5 2011 2016
150  
151 2011 ▶ if ( !seek( $self->{fh}, 0, 0 ) && $App::Ack::report_bad_filenames ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not seek($self->{"fh"}, 0, 0) and $App::Ack::report_bad_filenames) 0 2011 2011
Condition33%1/3
not seek($self->{"fh"}, 0, 0) and $App::Ack::report_bad_filenames
!ll&&!rl&&r
201100
MC/DC0%0/2
not seek($self->{"fh"}, 0, 0) and $App::Ack::report_bad_filenames
not seek($self->{"fh"}, 0, 0) $App::Ack::report_bad_filenames
Truth table33%1/3
not(A) and B
A= not seek($self->{"fh"}, 0, 0)B= $App::Ack::report_bad_filenames
ABresult
0X0
100
111
152 0 App::Ack::warn( "$self->{filename}: $!" );
153 }
154 }
155  
156 2058 return;
157 }
158  
159  
160 =head2 $file->close()
161  
162 Close the file.
163  
164 =cut
165  
166 1471 ▶sub close {
Subroutine close: called CC=4 SCAR=15.0
Pod documented
167 1471 my $self = shift;
168  
169 1471 ▶ if ( $self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"}) 1470 1 1471
170 1470 ▶ if ( !close($self->{fh}) && $App::Ack::report_bad_filenames ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not CORE::close $self->{"fh"} and $App::Ack::report_bad_filenames) 0 1470 1470
Condition33%1/3
not CORE::close $self->{"fh"} and $App::Ack::report_bad_filenames
!ll&&!rl&&r
147000
MC/DC0%0/2
not CORE::close $self->{"fh"} and $App::Ack::report_bad_filenames
not CORE::close $self->{"fh"} $App::Ack::report_bad_filenames
Truth table33%1/3
not(A) and B
A= not CORE::close $self->{"fh"}B= $App::Ack::report_bad_filenames
ABresult
0X0
100
111
171 0 App::Ack::warn( $self->name() . ": $!" );
172 }
173 1470 $self->{fh} = undef;
174 }
175  
176 1471 return;
177 }
178  
179  
180 =head2 $file->clone()
181  
182 Clones this file.
183  
184 =cut
185  
186 200 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
187 200 my ( $self ) = @_;
188  
189 200 return __PACKAGE__->new($self->name);
190 }
191  
192  
193 =head2 $file->firstliney()
194  
195 Returns the first line of a file (or first 250 characters, whichever
196 comes first).
197  
198 =cut
199  
200 1201 ▶sub firstliney {
Subroutine firstliney: called CC=6 SCAR=19.9
Pod documented
201 1201 my ( $self ) = @_;
202  
203 1201 ▶ if ( !exists $self->{firstliney} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"firstliney"}) 1201 0 1201
204 1201 my $fh = $self->open();
205 1201 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fh) { } 0 1201 1201
206 0 ▶ if ( $App::Ack::report_bad_filenames ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Ack::report_bad_filenames) 0 0 0
207 0 App::Ack::warn( $self->name . ': ' . $! );
208 }
209 0 $self->{firstliney} = '';
210 }
211 else {
212 1201 my $buffer;
213 1201 my $rc = sysread( $fh, $buffer, 250 );
214 1201 ▶ if ( $rc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) { } 1190 11 1201
215 1190 $buffer =~ s/[\r\n].*//s;
216 }
217 else {
218 11 ▶ if ( !defined($rc) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 11 11
219 0 App::Ack::warn( $self->name . ': ' . $! );
220 }
221 11 $buffer = '';
222 }
223 1201 $self->{firstliney} = $buffer;
224 1201 $self->reset;
225 }
226 }
227  
228 1201 return $self->{firstliney};
229 }
230  
231 585 1;
« blib/lib/App/Ack.pm ↑ Summary blib/lib/App/Ack/ConfigLoader.pm »