blib/lib/File/Locate/Iterator.pm

filter: Statementstmt 87.7% 86 / 98 Branchbran 77.2% 51 / 66 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 83.3% 10 / 12 Podpod 100% 3 / 3 totaltotal 81.2% 156 / 192 CC 36 SCAR 31.1
CC
36
Cov
83%
new
32.3
default_database_file
6.0
_current
6.0
CRAP 22.3
↑ Summary blib/lib/MooseX/Iterator/Locate.pm »
1 # Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2017, 2018, 2019 Kevin Ryde.
2 #
3 # This file is part of File-Locate-Iterator.
4 #
5 # File-Locate-Iterator is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 3, or (at your option)
8 # any later version.
9 #
10 # File-Locate-Iterator is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with File-Locate-Iterator; see the file COPYING. Failing that, go to
17 # <http://www.gnu.org/licenses/>.
18  
19  
20 # Maybe:
21 # ignore_case globs and suffixes, not easily done to regexs
22 # glob_ignore_case
23 # suffix_ignore_case
24 #
25  
26  
27 package File::Locate::Iterator;
28 9 ▶use 5.006; # for qr//, and open anonymous handles
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 9 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 our @ISA = ('DynaLoader');
35  
36 9 our $VERSION = 28;
37  
38 # uncomment this to run the ### lines
39 #use Devel::Comments;
40  
41  
42 9 ▶if (eval { __PACKAGE__->bootstrap($VERSION) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { __PACKAGE__->bootstrap($File::Locate::Iterator::VERSION) } }) { } 9 0 9
43 ### FLI next() from XS ...
44 } else {
45 ### FLI next() in perl, XS didn't load: $@
46  
47 0 ▶ eval "\n#line ".(__LINE__+1)." \"".__FILE__."\"\n" . <<'HERE' or die;
Branch: 1 branch
BranchTrueFalseTotal
eval "\n#line 48 \"/home/cpancover/.cpan/build/File-Locate-Iterator-28-0/blib/lib/File/Locate/Iterator.pm\"\nuse strict;\nuse warnings;\nuse File::FnMatch;\n\nsub _UNEXPECTED_EOF {\n my (\$self) = \@_;\n undef \$self->{'entry'};\n croak 'Invalid database contents (unexpected EOF)';\n}\nsub _ERROR_READING {\n my (\$self) = \@_;\n undef \$self->{'entry'};\n croak 'Error reading database: ',\$!;\n}\nsub _BAD_SHARE {\n my (\$self, \$sharelen) = \@_;\n undef \$self->{'entry'};\n croak \"Invalid database contents (bad share length \$sharelen)\";\n}\nsub next {\n my (\$self) = \@_;\n ### FLI PP next()\n\n my \$sharelen = \$self->{'sharelen'};\n my \$entry = \$self->{'entry'};\n my \$regexp = \$self->{'regexp'};\n my \$globs = \$self->{'globs'};\n\n if (my \$mref = \$self->{'mref'}) {\n my \$pos = \$self->{'pos'};\n MREF_LOOP: for (;;) {\n #### pos in map: sprintf('%#x', \$pos)\n if (\$pos >= length (\$\$mref)) {\n undef \$self->{'entry'};\n return; # end of file\n }\n\n my (\$adjshare) = unpack 'c', substr (\$\$mref, \$pos++, 1);\n if (\$adjshare == -128) {\n #### 2byte pos: sprintf('%#X', \$pos)\n # print ord(substr (\$\$mref,\$pos,1)),\"\\n\";\n # print ord(substr (\$\$mref,\$pos+1,1)),\"\\n\";\n\n if (\$pos+2 > length (\$\$mref)) { goto &_UNEXPECTED_EOF; }\n\n # for perl 5.10 up could use 's>' for signed 16-bit big-endian,\n # instead of getting unsigned and stepping down\n (\$adjshare) = unpack 'n', substr (\$\$mref, \$pos, 2);\n if (\$adjshare >= 32768) { \$adjshare -= 65536; }\n\n \$pos += 2;\n }\n ### \$adjshare\n \$sharelen += \$adjshare;\n # print \"share now \$sharelen\\n\";\n if (\$sharelen < 0 || \$sharelen > length(\$entry)) {\n push \@_, \$sharelen; goto &_BAD_SHARE;\n }\n\n my \$end = index (\$\$mref, \"\\0\", \$pos);\n # print \"\$pos to \$end\\n\";\n if (\$end < 0) { goto &_UNEXPECTED_EOF; }\n\n \$entry = (substr(\$entry,0,\$sharelen)\n . substr (\$\$mref, \$pos, \$end-\$pos));\n \$pos = \$end + 1;\n\n if (\$regexp) {\n last if \$entry =~ \$regexp;\n } elsif (! \$globs) {\n last;\n }\n if (\$globs) {\n foreach my \$glob (\@\$globs) {\n last MREF_LOOP if File::FnMatch::fnmatch(\$glob,\$entry)\n }\n }\n }\n \$self->{'pos'} = \$pos;\n\n } else {\n local \$/ = \"\\0\"; # readline() to \\0\n\n my \$fh = \$self->{'fh'};\n ### pos tell(fh): sprintf('%#x',tell(\$fh))\n IO_LOOP: for (;;) {\n my \$adjshare;\n unless (my \$got = read \$fh, \$adjshare, 1) {\n if (defined \$got) {\n undef \$self->{'entry'};\n return; # EOF\n }\n goto &_ERROR_READING;\n }\n\n (\$adjshare) = unpack 'c', \$adjshare;\n if (\$adjshare == -128) {\n my \$got = read \$fh, \$adjshare, 2;\n if (! defined \$got) { goto &_ERROR_READING; }\n if (\$got != 2) { goto &_UNEXPECTED_EOF; }\n\n # for perl 5.10 up could use 's>' for signed 16-bit big-endian\n # pack, instead of getting unsigned and stepping down\n (\$adjshare) = unpack 'n', \$adjshare;\n if (\$adjshare >= 32768) { \$adjshare -= 65536; }\n }\n ### \$adjshare\n\n \$sharelen += \$adjshare;\n ### share now: \$sharelen\n if (\$sharelen < 0 || \$sharelen > length(\$entry)) {\n push \@_, \$sharelen; goto &_BAD_SHARE;\n }\n\n my \$part;\n {\n # perlfunc.pod of 5.10.0 for readline() says you can clear \$!\n # then check it afterwards for an error indication, but that's\n # wrong, \$! ends up set to EBADF when filling the PerlIO buffer,\n # which means if the readline crosses a 1024 byte boundary\n # (something in attempting a fast gets then falling back ...)\n\n \$part = readline \$fh;\n if (! defined \$part) { goto &_UNEXPECTED_EOF; }\n\n ### part: \$part\n chomp \$part or goto &_UNEXPECTED_EOF;\n }\n\n \$entry = substr(\$entry,0,\$sharelen) . \$part;\n\n if (\$regexp) {\n last if \$entry =~ \$regexp;\n } elsif (! \$globs) {\n last;\n }\n if (\$globs) {\n foreach my \$glob (\@\$globs) {\n last IO_LOOP if File::FnMatch::fnmatch(\$glob,\$entry)\n }\n }\n }\n }\n\n \$self->{'sharelen'} = \$sharelen;\n return (\$self->{'entry'} = \$entry);\n}\n\n1;\n\n" or die 0 0 0
48 use strict;
49 use warnings;
50 use File::FnMatch;
51  
52 sub _UNEXPECTED_EOF {
53 my ($self) = @_;
54 undef $self->{'entry'};
55 croak 'Invalid database contents (unexpected EOF)';
56 }
57 sub _ERROR_READING {
58 my ($self) = @_;
59 undef $self->{'entry'};
60 croak 'Error reading database: ',$!;
61 }
62 sub _BAD_SHARE {
63 my ($self, $sharelen) = @_;
64 undef $self->{'entry'};
65 croak "Invalid database contents (bad share length $sharelen)";
66 }
67 sub next {
68 my ($self) = @_;
69 ### FLI PP next()
70  
71 my $sharelen = $self->{'sharelen'};
72 my $entry = $self->{'entry'};
73 my $regexp = $self->{'regexp'};
74 my $globs = $self->{'globs'};
75  
76 if (my $mref = $self->{'mref'}) {
77 my $pos = $self->{'pos'};
78 MREF_LOOP: for (;;) {
79 #### pos in map: sprintf('%#x', $pos)
80 if ($pos >= length ($$mref)) {
81 undef $self->{'entry'};
82 return; # end of file
83 }
84  
85 my ($adjshare) = unpack 'c', substr ($$mref, $pos++, 1);
86 if ($adjshare == -128) {
87 #### 2byte pos: sprintf('%#X', $pos)
88 # print ord(substr ($$mref,$pos,1)),"\n";
89 # print ord(substr ($$mref,$pos+1,1)),"\n";
90  
91 if ($pos+2 > length ($$mref)) { goto &_UNEXPECTED_EOF; }
92  
93 # for perl 5.10 up could use 's>' for signed 16-bit big-endian,
94 # instead of getting unsigned and stepping down
95 ($adjshare) = unpack 'n', substr ($$mref, $pos, 2);
96 if ($adjshare >= 32768) { $adjshare -= 65536; }
97  
98 $pos += 2;
99 }
100 ### $adjshare
101 $sharelen += $adjshare;
102 # print "share now $sharelen\n";
103 if ($sharelen < 0 || $sharelen > length($entry)) {
104 push @_, $sharelen; goto &_BAD_SHARE;
105 }
106  
107 my $end = index ($$mref, "\0", $pos);
108 # print "$pos to $end\n";
109 if ($end < 0) { goto &_UNEXPECTED_EOF; }
110  
111 $entry = (substr($entry,0,$sharelen)
112 . substr ($$mref, $pos, $end-$pos));
113 $pos = $end + 1;
114  
115 if ($regexp) {
116 last if $entry =~ $regexp;
117 } elsif (! $globs) {
118 last;
119 }
120 if ($globs) {
121 foreach my $glob (@$globs) {
122 last MREF_LOOP if File::FnMatch::fnmatch($glob,$entry)
123 }
124 }
125 }
126 $self->{'pos'} = $pos;
127  
128 } else {
129 local $/ = "\0"; # readline() to \0
130  
131 my $fh = $self->{'fh'};
132 ### pos tell(fh): sprintf('%#x',tell($fh))
133 IO_LOOP: for (;;) {
134 my $adjshare;
135 unless (my $got = read $fh, $adjshare, 1) {
136 if (defined $got) {
137 undef $self->{'entry'};
138 return; # EOF
139 }
140 goto &_ERROR_READING;
141 }
142  
143 ($adjshare) = unpack 'c', $adjshare;
144 if ($adjshare == -128) {
145 my $got = read $fh, $adjshare, 2;
146 if (! defined $got) { goto &_ERROR_READING; }
147 if ($got != 2) { goto &_UNEXPECTED_EOF; }
148  
149 # for perl 5.10 up could use 's>' for signed 16-bit big-endian
150 # pack, instead of getting unsigned and stepping down
151 ($adjshare) = unpack 'n', $adjshare;
152 if ($adjshare >= 32768) { $adjshare -= 65536; }
153 }
154 ### $adjshare
155  
156 $sharelen += $adjshare;
157 ### share now: $sharelen
158 if ($sharelen < 0 || $sharelen > length($entry)) {
159 push @_, $sharelen; goto &_BAD_SHARE;
160 }
161  
162 my $part;
163 {
164 # perlfunc.pod of 5.10.0 for readline() says you can clear $!
165 # then check it afterwards for an error indication, but that's
166 # wrong, $! ends up set to EBADF when filling the PerlIO buffer,
167 # which means if the readline crosses a 1024 byte boundary
168 # (something in attempting a fast gets then falling back ...)
169  
170 $part = readline $fh;
171 if (! defined $part) { goto &_UNEXPECTED_EOF; }
172  
173 ### part: $part
174 chomp $part or goto &_UNEXPECTED_EOF;
175 }
176  
177 $entry = substr($entry,0,$sharelen) . $part;
178  
179 if ($regexp) {
180 last if $entry =~ $regexp;
181 } elsif (! $globs) {
182 last;
183 }
184 if ($globs) {
185 foreach my $glob (@$globs) {
186 last IO_LOOP if File::FnMatch::fnmatch($glob,$entry)
187 }
188 }
189 }
190 }
191  
192 $self->{'sharelen'} = $sharelen;
193 return ($self->{'entry'} = $entry);
194 }
195  
196 1;
197  
198 HERE
199 }
200  
201 9 ▶use constant default_use_mmap => 'if_sensible';
Subroutine BEGIN: called CC=1 SCAR=0.0
202 9 my $header = "\0LOCATE02\0";
203  
204  
205 # Default path these days is /var/cache/locate/locatedb.
206 #
207 # Back in findutils 4.1 it was $(localstatedir)/locatedb, but there seems to
208 # have been no way to ask about the location.
209 #
210 0 ▶sub default_database_file {
Subroutine default_database_file: not called CC=2 SCAR=17.9
Pod documented
211 # my ($class) = @_;
212 0 ▶ if (defined (my $env = $ENV{'LOCATE_PATH'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $env = $ENV{'LOCATE_PATH'})) { } 0 0 0
213 0 return $env;
214 } else {
215 0 return '/var/cache/locate/locatedb';
216 }
217 }
218  
219 # The fields, all meant to be private, are:
220 #
221 # regexp
222 # qr// regexp of all the 'regexp', 'regexps', 'suffix' and 'suffixes'
223 # parameters. If no such matches then no such field. When the field
224 # exists an entry must match the regexp or is skipped.
225 #
226 # globs
227 # arrayref of strings which are globs to fnmatch(). If no globs then no
228 # such field. When the field exists an entry must match at least one of
229 # the globs.
230 #
231 # mref
232 # Ref to a scalar which holds the database contents, or undef if using
233 # fh instead. It's either a ref to the 'database_str' parameter passed
234 # in, or a ref to a scalar created as an mmap of the file. The mmap one
235 # is shared among iterators through the File::Locate::Iterator::FileMap
236 # caching.
237 #
238 # fh
239 # When mref is undef, ref file handle which is to be read from,
240 # otherwise no such field. This can be either the 'database_fh'
241 # parameter or an opened anonymous handle of the 'database_file'
242 # parameter.
243 #
244 # When mmap is used the 'database_fh' is not held here. The mmap is
245 # made (or rather, looked up in the FileMap cache), and the handle is
246 # then no longer needed and can be closed or garbage collected in the
247 # caller.
248 #
249 # fh_start
250 # When fh is set, the tell($fh) position just after the $header in that
251 # fh. This is where to seek() back to for a $it->rewind. If tell()
252 # failed then this is -1 and $it->rewind is not possible.
253 #
254 # Normally fh_start is simply length($header) for a database starting at
255 # the start of the file, but a database_fh arg which is positioned at
256 # some offset into a file can be read and remembering an fh_start
257 # position lets $it->rewind work on it too.
258 #
259 # fm
260 # When using mmap, a File::Locate::Iterator::FileMap object which is the
261 # cache entry for the database file, otherwise no such field. This is
262 # hung onto to keep it alive while in use. $self->{'mref'} is
263 # $fm->mmapref in this case.
264 #
265 # pos
266 # When mref is not undef, an integer offset into the $$mref string which
267 # is the current read position. The file header is checked in new() so
268 # the initial value is length($header), ie. 10, the position of the
269 # first entry (or possibly EOF).
270 #
271 # entry
272 # String of the last database entry returned, or no such field before
273 # the first is read, or undef after EOF is hit. Might be undef instead
274 # of not existing if a hypothetical seek() goes back to the start of the
275 # file.
276 #
277 # sharelen
278 # Integer which is the number of leading bytes of 'entry' which the next
279 # entry will share with that previous entry. Initially 0.
280 #
281 # This is modified successively by the "adjshare" of each entry as each
282 # takes more or less of the preceding entry. An adjshare can range from
283 # -sharelen to take nothing at all of the previous entry, up to
284 # length($entry)-sharelen to increment up to take all of the previous
285 # entry.
286 #
287 76 ▶sub new {
Subroutine new: called CC=31 SCAR=34.8
Pod documented
288 76 my ($class, %options) = @_;
289 ### FLI new(): %options
290  
291 # delete 'regexp' field if it's undef, as the XS code wants no 'regexp'
292 # field for no regexps, not a field set to undef
293 76 my @regexps;
294 76 ▶ if (defined (my $regexp = delete $options{'regexp'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $regexp = delete $options{"regexp"})) 5 71 76
295 5 push @regexps, $regexp;
296 }
297 76 ▶ if (my $regexps = delete $options{'regexps'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regexps = delete $options{"regexps"}) 2 74 76
298 2 push @regexps, @$regexps;
299 }
300 76 ▶ foreach my $suffix (defined $options{'suffix'} ? $options{'suffix'} : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'suffix'} ? : 2 74 76
301 76 @{$options{'suffixes'}}) {
302 6 push @regexps, quotemeta($suffix) . '$';
303 }
304 ### @regexps
305  
306 # as per findutils locate.c locate() function, pattern with * ? or [ is a
307 # glob, anything else is a literal match
308 #
309 my @globs = (defined $options{'glob'} ? $options{'glob'} : (),
310 76 ▶ @{$options{'globs'} || []});
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'glob'} ? : 6 70 76
Condition100%2/2
$options{'globs'} || []
l!l
373
MC/DC100%1/1
$options{'globs'} || []
$options{'globs'}
Truth table100%2/2
A
A= $options{'globs'}
Aresult
11
00
311 76 @globs = grep { ($_ =~ /[[*?]/
312 0 ▶ || do { push @regexps, quotemeta($_); 0 })
Condition33%1/3
$_ =~ /[[*?]/ || do { push @regexps, quotemeta $_; 0 }
l!l&&r!l&&!r
1100
MC/DC0%0/2
$_ =~ /[[*?]/ || do { push @regexps, quotemeta $_; 0 }
$_ =~ /[[*?]/ do { push @regexps, quotemeta $_; 0 }
Truth table33%1/3
A || B
A= $_ =~ /[[*?]/B= do { push @regexps, quotemeta $_; 0 }
ABresult
1X1
011
000
313 } @globs;
314 ### @globs
315  
316 76 my $self = bless { entry => '',
317 sharelen => 0,
318 }, $class;
319  
320 76 ▶ if (@regexps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@regexps) 11 65 76
321 11 my $regexp = join ('|', @regexps);
322 11 $self->{'regexp'} = qr/$regexp/s;
323 }
324 76 ▶ if (@globs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@globs) 9 67 76
325 9 $self->{'globs'} = \@globs;
326 }
327  
328 ### regexp: $self->{'regexp'}
329 ### globs : $self->{'globs'}
330  
331 76 ▶ if (defined (my $ref = $options{'database_str_ref'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $ref = $options{'database_str_ref'})) { } 2 74 76
elsif (defined $options{'database_str'}) { } 18 56 74
332 2 $self->{'mref'} = $ref;
333  
334 } elsif (defined $options{'database_str'}) {
335 18 $self->{'mref'} = \$options{'database_str'};
336  
337 } else {
338 my $use_mmap = (defined $options{'use_mmap'}
339 56 ▶ ? $options{'use_mmap'}
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'use_mmap'} ? : 48 8 56
340 : $class->default_use_mmap);
341 ### $use_mmap
342 56 ▶ if ($use_mmap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_mmap) 33 23 56
343 33 ▶ if (! eval { require File::Locate::Iterator::FileMap }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require File::Locate::Iterator::FileMap } }) 0 33 33
344 ### FileMap not possible: $@
345 0 $use_mmap = 0;
346 }
347 }
348  
349 56 my $fh = $options{'database_fh'};
350 56 ▶ if (defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) { } 11 45 56
351 11 ▶ if ($use_mmap eq 'if_sensible'
Branch: 1 branch
BranchTrueFalseTotal
if ($use_mmap eq "if_sensible" and File::Locate::Iterator::FileMap::_have_mmap_layer($fh)) 0 11 11
Condition66%2/3
$use_mmap eq "if_sensible" and File::Locate::Iterator::FileMap::_have_mmap_layer($fh)
!ll&&!rl&&r
920
MC/DC0%0/2
$use_mmap eq "if_sensible" and File::Locate::Iterator::FileMap::_have_mmap_layer($fh)
$use_mmap eq "if_sensible" File::Locate::Iterator::FileMap::_have_mmap_layer($fh)
Truth table66%2/3
A and B
A= $use_mmap eq "if_sensible"B= File::Locate::Iterator::FileMap::_have_mmap_layer($fh)
ABresult
0X0
100
111
352 && File::Locate::Iterator::FileMap::_have_mmap_layer($fh)) {
353 ### already have mmap layer, not sensible to mmap again
354 0 $use_mmap = 0;
355 }
356 } else {
357 my $file = (defined $options{'database_file'}
358 45 ▶ ? $options{'database_file'}
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'database_file'} ? : 45 0 45
359 : $class->default_database_file);
360 ### open database_file: $file
361  
362 # Crib note: '<:raw' means without :perlio buffering, whereas
363 # binmode() preserves that buffering, assuming it's in the $ENV{'PERLIO'}
364 # defaults. Also :raw is not available in perl 5.6.
365 45 ▶ open $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or croak("Cannot open ${file}: $!") 45 0 45
366 or croak "Cannot open $file: $!";
367 45 ▶ binmode($fh)
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh or croak("Cannot set binary mode") 45 0 45
368 or croak "Cannot set binary mode";
369 }
370  
371 56 ▶ if ($use_mmap eq 'if_sensible') {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_mmap eq "if_sensible") 8 48 56
372 8 ▶ $use_mmap = (File::Locate::Iterator::FileMap::_mmap_size_excessive($fh)
Branch: 1 branch
BranchTrueFalseTotal
File::Locate::Iterator::FileMap::_mmap_size_excessive($fh) ? : 0 8 8
373 ? 0
374 : 'if_possible');
375 ### if_sensible after size check becomes: $use_mmap
376 }
377  
378 56 ▶ if ($use_mmap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_mmap) 33 23 56
379 ### attempt mmap: $fh, (-s $fh)
380  
381 # There's many ways an mmap can fail, just chuck an eval on FileMap /
382 # File::Map it to catch them all.
383 # - An ordinary readable file of length zero may fail per POSIX, and
384 # that's how it is in linux kernel post 2.6.12. However File::Map
385 # 0.20 takes care of returning an empty string for that.
386 # - A char special usually gives 0 for its length, even for instance
387 # linux kernel special files like /proc/meminfo. Char specials can
388 # often be mapped perfectly well, but without a length don't know
389 # how much to look at. For that reason "if_possible" restricts to
390 # ordinary files, though forced "use_mmap=>1" just goes ahead anyway.
391 #
392 33 ▶ if ($use_mmap eq 'if_possible') {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_mmap eq 'if_possible') { } 32 1 33
393 32 ▶ if (! -f $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $fh) { } 1 31 32
394 ### if_possible, not a plain file, consider not mmappable
395 } else {
396 31 ▶ if (! eval { $self->{'fm'}
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->{"fm"} = "File::Locate::Iterator::FileMap"->get($fh) } }) 2 29 31
397 = File::Locate::Iterator::FileMap->get($fh) }) {
398 ### mmap failed: $@
399 }
400 }
401 } else {
402 1 $self->{'fm'} = File::Locate::Iterator::FileMap->get($fh);
403 }
404 }
405 55 ▶ if ($self->{'fm'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'fm'}) { } 29 26 55
406 29 $self->{'mref'} = $self->{'fm'}->mmap_ref;
407 } else {
408 26 $self->{'fh'} = $fh;
409 }
410 }
411  
412 75 ▶ if (my $mref = $self->{'mref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mref = $self->{'mref'}) { } 49 26 75
413 49 ▶ unless ($$mref =~ /^\Q$header/o) { goto &_ERROR_BAD_HEADER }
Branch: 1 branch
BranchTrueFalseTotal
unless ($$mref =~ /^\Q$header\E/o) 3 46 49
414 46 $self->{'pos'} = length($header);
415 } else {
416 26 my $got = '';
417 26 read $self->{'fh'}, $got, length($header);
418 26 ▶ if ($got ne $header) { goto &_ERROR_BAD_HEADER }
Branch: 1 branch
BranchTrueFalseTotal
if ($got ne $header) 3 23 26
419 23 $self->{'fh_start'} = tell $self->{'fh'};
420 }
421  
422 69 return $self;
423 }
424 6 ▶sub _ERROR_BAD_HEADER {
Subroutine _ERROR_BAD_HEADER: called CC=1 SCAR=0.0
425 6 croak 'Invalid database contents (no LOCATE02 header)';
426 }
427  
428 12 ▶sub rewind {
Subroutine rewind: called CC=4 SCAR=14.0
Pod documented
429 12 my ($self) = @_;
430  
431 12 $self->{'sharelen'} = 0;
432 12 $self->{'entry'} = '';
433 12 ▶ if ($self->{'mref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'mref'}) { } 5 7 12
434 5 $self->{'pos'} = length($header);
435 } else {
436 7 ▶ $self->{'fh_start'} > 0
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh_start"} > 0 or croak("Cannot seek database") 7 0 7
437 or croak "Cannot seek database";
438 7 ▶ seek ($self->{'fh'}, $self->{'fh_start'}, 0)
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"fh"}, $self->{"fh_start"}, 0 or croak("Cannot seek database: $!") 7 0 7
439 or croak "Cannot seek database: $!";
440 }
441 }
442  
443 # return true if mmap is in use
444 # (an actual mmap, not the slightly similar 'database_str' option)
445 # this is meant for internal use as a diagnostic ...
446 12 ▶sub _using_mmap {
Subroutine _using_mmap: called CC=1 SCAR=0.0
447 12 my ($self) = @_;
448 12 return defined $self->{'fm'};
449 }
450  
451 # Not yet documented, likely worthwhile as long as it works properly.
452 # Return empty list for nothing yet? Same as next().
453 # Return empty list at EOF? At EOF 'entry' is undefed out.
454 #
455 # =item C<< $entry = $it->current >>
456 #
457 # Return the current entry from the database, meaning the same as the last
458 # call to C<next> returned. At the start of the database (before the first
459 # C<next>) or at end of the database the return is an empty list.
460 #
461 # while (defined $it->next) {
462 # ...
463 # print $it->current,"\n";
464 # }
465 #
466 0 ▶sub _current {
Subroutine _current: not called CC=2 SCAR=17.9
467 0 my ($self) = @_;
468 0 ▶ if (defined $self->{'entry'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'entry'}) { } 0 0 0
469 0 return $self->{'entry'};
470 } else {
471 0 return;
472 }
473 }
474  
475  
476 9 1;
477 __END__
↑ Summary blib/lib/MooseX/Iterator/Locate.pm »