lib/TUI/StdDlg/FindFirstRec/Unix.pm

filter: Statementstmt 93.9% 219 / 233 Branchbran 60.3% 76 / 126 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 96.6% 29 / 30 Podpod 100% 3 / 3 totaltotal 82.0% 334 / 407 CC 67 SCAR 20.6
CC
67
Cov
82%
__ANON__
20.5
__ANON__
10.1
__ANON__
9.8
CRAP 7.9
« lib/TUI/StdDlg/FindFirstRec/Win32.pm ↑ Summary lib/TUI/Drivers/AttrPair.pm »
1 package TUI::StdDlg::FindFirstRec::Unix;
2 # ABSTRACT: A class implementing the behavior of findfirst/findnext for Unix
3
4 12 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 12 our $VERSION = '2.000002';
9 12 $VERSION =~ tr/_//d;
10 12 our $AUTHORITY = 'cpan:BRICKPOOL';
11
12 12 ▶use Hash::Util::FieldHash qw( fieldhash );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Scalar::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 refaddr
15 weaken
16 12 );
17 12 ▶use Fcntl qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
18 :mode
19 12 );
20
21 12 ▶use TUI::toolkit qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
22 :boolean
23 :utils
24 12 );
25 12 ▶use TUI::toolkit::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
26 Maybe
27 :is
28 :types
29 12 );
30
31 12 ▶use TUI::StdDlg::Const qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
32 MAXPATH
33 :_A_
34 12 );
35
36 PRIVATE: {
37 namespace::sweep->import( -also => [qw(
38 SPECIAL_BITS
39
40 reserved
41 size
42 attrib
43 wr_time
44 wr_date
45 name
46 12 ▶ )] ) if eval { require namespace::sweep };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require namespace::sweep } } 0 12 12
47
48 12 ▶ use constant SPECIAL_BITS => _A_SUBDIR | _A_HIDDEN | _A_SYSTEM;
Subroutine BEGIN: called CC=1 SCAR=0.0
49
50 # find_t offsets
51 12 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
52 12 reserved => 0,
53 size => 1,
54 attrib => 2,
55 wr_time => 3,
56 wr_date => 4,
57 name => 5,
58 12 };
59 }
60
61 # declare global variables
62 12 fieldhash my %REC_LIST;
63
64 # private attributes
65 12 ▶our %HAS; BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
66 %HAS = (
67 0 ▶ finfo => sub { die 'required' }, # weak_ref => 1
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 9 ▶ searchAttr => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 9 ▶ dirStream => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 9 ▶ searchDir => sub { './' },
Subroutine __ANON__: called CC=1 SCAR=0.0
71 9 ▶ wildcard => sub { '*' },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 )
73 12 }
74
75 # predeclare private methods
76 my (
77 12 $open,
78 $close,
79 $setParameters,
80 $setPath,
81 $matchEntry,
82 $attrMatch,
83 $wildcardMatch,
84 $cvtAttr,
85 $cvtTime,
86 );
87
88 11 ▶sub allocate { # $rec|undef ($fileinfo, $attrib, $pathname)
Subroutine allocate: called CC=5 SCAR=16.4
Pod documented
89 11 state $sig = signature(
90 method => 1,
91 pos => [
92 Maybe[ArrayLike],
93 PositiveOrZeroInt,
94 Str,
95 ],
96 );
97 11 my ( $class, $fileinfo, $attrib, $pathname ) = $sig->( @_ );
98
99 # The findfirst interface based on DOS call 0x4E doesn't provide a
100 # findclose function. The strategy here is the same as in Borland's RTL:
101 # a new object is created and stored internally in %REC_LIST, unless
102 # $fileinfo has already been passed to us before.
103 return undef
104 11 ▶ unless $fileinfo;
Branch: 1 branch
BranchTrueFalseTotal
unless $fileinfo 1 10 11
105
106 10 my $r = $REC_LIST{ $fileinfo };
107
108 # If $r is defined, we need to close the directory stream and reset the
109 # parameters. If $r is undef, we need to create a new FindFirstRec and store
110 # it in the field hash %REC_LIST and the global registry for retrieval.
111 10 ▶ if ( $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 1 9 10
112 1 $r->$close();
113 }
114 else {
115 $r = bless {
116 finfo => $fileinfo // $HAS{finfo}->(),
117 searchAttr => $HAS{searchAttr}->(),
118 dirStream => $HAS{dirStream}->(),
119 searchDir => $HAS{searchDir}->(),
120 9 ▶ wildcard => $HAS{wildcard}->(),
Condition33%1/3
$fileinfo // $TUI::StdDlg::FindFirstRec::Unix::HAS{"finfo"}->()
l!l&&r!l&&!r
900
MC/DC0%0/2
$fileinfo // $TUI::StdDlg::FindFirstRec::Unix::HAS{"finfo"}->()
$fileinfo $TUI::StdDlg::FindFirstRec::Unix::HAS{"finfo"}->()
Truth table33%1/3
A // B
A= $fileinfoB= $TUI::StdDlg::FindFirstRec::Unix::HAS{"finfo"}->()
ABresult
1X1
011
000
121 }, $class;
122 9 $REC_LIST{ $fileinfo } = $r;
123 9 weaken $r->{finfo};
124 }
125
126 # If pathname is a valid directory, make fileinfo point to the allocated
127 # FindFirstRec. Otherwise, return undef.
128 10 ▶ if ( $r->$setParameters( $attrib, $pathname ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->$setParameters($attrib, $pathname)) 10 0 10
129 # Connect fileinfo to FindFirstRec for compatibility with the original
130 # findfirst interface. This allows the caller to identify the corresponding
131 # FindFirstRec object using the fileinfo structure.
132 10 $fileinfo->[reserved] = refaddr $r;
133 10 return $r;
134 }
135
136 0 return undef;
137 } #/ sub allocate
138
139 9 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.2
140 9 my ( $self ) = @_;
141 9 ▶ assert ( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 9 9
142 9 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 9 9
143 9 $self->$close();
144 9 return;
145 }
146
147 23 ▶sub get { # $rec|undef ($fileinfo)
Subroutine get: called CC=2 SCAR=6.9
Pod documented
148 23 state $sig = signature(
149 method => 1,
150 pos => [
151 Maybe[ArrayLike],
152 ],
153 );
154 23 my ( $class, $fileinfo ) = $sig->( @_ );
155
156 return undef
157 23 ▶ unless $fileinfo;
Branch: 1 branch
BranchTrueFalseTotal
unless $fileinfo 1 22 23
158 22 return $REC_LIST{ $fileinfo };
159 }
160
161 28 ▶sub next { # $bool ()
Subroutine next: called CC=3 SCAR=11.0
Pod documented
162 28 state $sig = signature(
163 method => Object,
164 pos => [],
165 );
166 28 my ( $self ) = $sig->( @_ );
167
168 28 ▶ while ( defined( my $entry = readdir $self->{dirStream} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $self->{"dirStream"})) 37 3 40
169 37 ▶ return true
Branch: 1 branch
BranchTrueFalseTotal
if $self->$matchEntry($entry) 25 12 37
170 if $self->$matchEntry( $entry );
171 }
172
173 3 $self->$close();
174 3 return false;
175 }
176
177 10 ▶$open = sub { # $bool ()
Subroutine __ANON__: called CC=5 SCAR=16.6
178 10 my ( $self ) = @_;
179 10 ▶ assert ( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 10 10
180 10 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 10 10
181 10 ▶ assert ( !defined $self->{dirStream} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dirStream"} 0 10 10
182
183 10 my $dh;
184 return false
185 10 ▶ unless opendir $dh, $self->{searchDir};
Branch: 1 branch
BranchTrueFalseTotal
unless opendir $dh, $self->{"searchDir"} 0 10 10
186
187 10 $self->{dirStream} = $dh;
188 10 return true;
189 12 };
190
191 13 ▶$close = sub { # void ()
Subroutine __ANON__: called CC=4 SCAR=14.0
192 13 my ( $self ) = @_;
193 13 ▶ assert ( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 13 13
194 13 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 13 13
195
196 13 ▶ if ( defined $self->{dirStream} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"dirStream"}) 10 3 13
197 10 closedir $self->{dirStream};
198 10 $self->{dirStream} = undef;
199 }
200
201 13 return;
202 12 };
203
204 10 ▶$setParameters = sub { # $bool ($attrib, $pathname)
Subroutine __ANON__: called CC=7 SCAR=20.9
205 10 my ( $self, $attrib, $pathname ) = @_;
206 10 ▶ assert ( @_ == 3 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 10 10
207 10 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 10 10
208 10 ▶ assert ( is_PositiveOrZeroInt $attrib );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_PositiveOrZeroInt($attrib) 0 10 10
209 10 ▶ assert ( is_Str $pathname );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Str($pathname) 0 10 10
210
211 return false
212 10 ▶ if defined $self->{dirStream};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dirStream"} 0 10 10
213
214 10 $self->{searchAttr} = $attrib;
215
216 10 ▶ return $self->$setPath( $pathname )
Condition33%1/3
$self->$setPath($pathname) && $self->$open
!ll&&!rl&&r
0010
MC/DC0%0/2
$self->$setPath($pathname) && $self->$open
$self->$setPath($pathname) $self->$open
Truth table33%1/3
A && B
A= $self->$setPath($pathname)B= $self->$open
ABresult
0X0
100
111
217 && $self->$open();
218 12 };
219
220 10 ▶$setPath = sub { # $bool ($pathname)
Subroutine __ANON__: called CC=8 SCAR=22.9
221 10 my ( $self, $pathname ) = @_;
222 10 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 10 10
223 10 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 10 10
224 10 ▶ assert ( is_Str $pathname );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Str($pathname) 0 10 10
225
226 10 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
unless length $pathname 0 10 10
227 unless length $pathname;
228
229 10 my $searchDir = $pathname;
230
231 # Accept DOS separators on Unix, matching the intent of path_dos2unix().
232 10 $searchDir =~ tr{\\}{\/};
233
234 # Win32's FindFirst rejects paths ending with a separator. Legacy code may
235 # still pass such paths, so handle them gracefully by matching ".".
236 10 ▶ if ( substr( $searchDir, -1 ) eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($searchDir, -1) eq "/") 0 10 10
237 0 $self->{searchDir} = $searchDir;
238 0 $self->{wildcard} = '.';
239 0 return true;
240 }
241
242 10 my $lastSlash = rindex $searchDir, '/';
243
244 10 ▶ if ( $lastSlash < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastSlash < 0) { } 0 10 10
245 0 $self->{wildcard} = $searchDir;
246 0 $self->{searchDir} = './';
247 }
248 else {
249 10 $self->{wildcard} = substr $searchDir, $lastSlash + 1;
250 10 $self->{searchDir} = substr $searchDir, 0, $lastSlash + 1;
251 }
252
253 # '*.*' means any name, any extension in the 32-bit Borland-compatible model.
254 $self->{wildcard} = '*'
255 10 ▶ if $self->{wildcard} eq '*.*';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"wildcard"} eq "*.*" 1 9 10
256
257 10 return true;
258 12 };
259
260 37 ▶$matchEntry = sub { # $bool ($entry)
Subroutine __ANON__: called CC=7 SCAR=19.6
261 37 my ( $self, $entry ) = @_;
262 37 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 37 37
263 37 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 37 37
264 37 ▶ assert ( is_Str $entry );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Str($entry) 0 37 37
265
266 return false
267 37 ▶ unless $self->$wildcardMatch( $self->{wildcard}, $entry );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$wildcardMatch($self->{"wildcard"}, $entry) 6 31 37
268
269 31 my $path = $self->{searchDir} . $entry;
270 31 my @st = stat $path;
271 31 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
unless @st 0 31 31
272 unless @st;
273
274 31 my $fileAttr = $self->$cvtAttr( \@st, $entry );
275 31 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$attrMatch($fileAttr) 6 25 31
276 unless $self->$attrMatch( $fileAttr );
277
278 # Match found, fill finfo.
279 25 my $finfo = $self->{finfo};
280 25 $finfo->[size] = $st[7];
281 25 $finfo->[attrib] = $fileAttr;
282 25 $self->$cvtTime( \@st, $finfo );
283
284 25 my $name = substr $entry, 0, MAXPATH - 1;
285 25 $finfo->[name] = $name;
286
287 25 return true;
288 12 };
289
290 31 ▶$attrMatch = sub { # $bool ($attrib)
Subroutine __ANON__: called CC=6 SCAR=18.3
291 31 my ( $self, $attrib ) = @_;
292 31 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 31 31
293 31 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 31 31
294 31 ▶ assert ( is_PositiveOrZeroInt $attrib );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_PositiveOrZeroInt($attrib) 0 31 31
295 31 ▶ assert ( is_Int $self->{searchAttr} );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Int($self->{"searchAttr"}) 0 31 31
296
297 # Behaviour from the original _dos_findnext: if requested attribute word
298 # includes hidden, system, or subdirectory bits, return normal files and
299 # those with any of the requested attributes.
300 return !( $attrib & SPECIAL_BITS )
301 31 ▶ || ( $self->{searchAttr} & $attrib & SPECIAL_BITS );
Condition100%3/3
!($attrib & 22) || $self->{'searchAttr'} & $attrib & 22
l!l&&r!l&&!r
15106
MC/DC100%2/2
!($attrib & 22) || $self->{'searchAttr'} & $attrib & 22
!($attrib & 22) $self->{'searchAttr'} & $attrib & 22
Truth table100%3/3
not(A) || B
A= !($attrib & 22)B= $self->{'searchAttr'} & $attrib & 22
ABresult
1X1
011
000
302 12 };
303
304 37 ▶$wildcardMatch = sub { # $bool ($wildcard, $filename)
Subroutine __ANON__: called CC=15 SCAR=30.2
305 37 my ( $self, $wildcard, $filename ) = @_;
306 37 ▶ assert ( @_ == 3 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 37 37
307 37 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 37 37
308 37 ▶ assert ( is_Str $wildcard );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Str($wildcard) 0 37 37
309 37 ▶ assert ( is_Str $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Str($filename) 0 37 37
310
311 37 pos $wildcard = 0;
312
313 # https://stackoverflow.com/a/3300547
314 37 ▶ while ( pos( $wildcard ) < length $wildcard ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $wildcard < length $wildcard) 48 0 48
315 48 my $wc = substr $wildcard, pos( $wildcard ), 1;
316 48 pos( $wildcard )++;
317
318 48 SWITCH: for ( $wc ) {
319 48 ▶ $_ eq '?' and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "?" and 13 35 48
320 13 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
if $filename eq "" 3 10 13
321 if $filename eq '';
322 10 substr $filename, 0, 1, '';
323 10 last;
324 };
325 35 ▶ $_ eq '*' and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "*" and 31 4 35
326 31 ▶ return true
Branch: 1 branch
BranchTrueFalseTotal
if pos $wildcard == length $wildcard 31 0 31
327 if pos( $wildcard ) == length $wildcard;
328
329 0 my $rest = substr $wildcard, pos( $wildcard );
330 0 ▶ for ( my $i = 0; $i < length $filename; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $filename) 0 0 0
331 0 ▶ return true
Branch: 1 branch
BranchTrueFalseTotal
if $self->$wildcardMatch($rest, substr($filename, $i)) 0 0 0
332 if $self->$wildcardMatch( $rest, substr( $filename, $i ) );
333 }
334
335 0 return false;
336 };
337 DEFAULT: {
338 4 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
if $filename eq "" 0 4 4
339 if $filename eq '';
340 4 ▶ return false
Branch: 1 branch
BranchTrueFalseTotal
if substr($filename, 0, 1) ne $wc 3 1 4
341 if substr( $filename, 0, 1 ) ne $wc;
342 1 substr $filename, 0, 1, '';
343 }
344 }
345 }
346
347 0 return $filename eq '';
348 12 };
349
350 31 ▶$cvtAttr = sub { # $attr ($stat, $filename)
Subroutine __ANON__: called CC=9 SCAR=23.1
351 31 my ( $self, $st, $filename ) = @_;
352 31 ▶ assert ( @_ == 3 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 31 31
353 31 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 31 31
354 31 ▶ assert ( is_ArrayLike $st );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_ArrayLike($st) 0 31 31
355 31 ▶ assert ( is_Str $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Str($filename) 0 31 31
356
357 # Returns file attributes in find_t format.
358 31 my $mode = $st->[2];
359 31 my $attr = 0; # _A_NORMAL
360
361 31 ▶ if ( substr( $filename, 0, 1 ) eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($filename, 0, 1) eq ".") 9 22 31
362 9 $attr |= _A_HIDDEN;
363 }
364 31 ▶ if ( S_ISDIR( $mode ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (S_ISDIR($mode)) { } 16 15 31
elsif (not S_ISREG($mode)) { } 0 15 15
elsif (not $mode & 128) { } 0 15 15
365 16 $attr |= _A_SUBDIR;
366 }
367 elsif ( !S_ISREG( $mode ) ) { # If not a regular file
368 0 $attr |= _A_SYSTEM;
369 }
370 elsif ( !( $mode & S_IWUSR ) ) { # If no write access, innacurate.
371 0 $attr |= _A_RDONLY;
372 }
373 31 return $attr;
374 12 };
375
376 25 ▶$cvtTime = sub { # void ($stat, $fileinfo)
Subroutine __ANON__: called CC=5 SCAR=16.4
377 25 my ( $self, $st, $fileinfo ) = @_;
378 25 ▶ assert ( @_ == 3 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 25 25
379 25 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 25 25
380 25 ▶ assert ( is_ArrayLike $st );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_ArrayLike($st) 0 25 25
381 25 ▶ assert ( is_ArrayLike $fileinfo );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_ArrayLike($fileinfo) 0 25 25
382
383 # Updates fileinfo with the times in st.
384 25 my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime $st->[9];
385
386 25 my $fatDate = ( ( $year - 80 ) << 9 ) # Year-1980
387 | ( ( $mon + 1 ) << 5 ) # Month (1-12)
388 | $mday; # Day of the month (1–31)
389
390 25 my $fatTime = ( $hour << 11 ) # Hour (0-23)
391 | ( $min << 5 ) # Minutes (0-59)
392 | int( $sec / 2 ); # Seconds divided by 2
393
394 25 $fileinfo->[wr_date] = $fatDate;
395 25 $fileinfo->[wr_time] = $fatTime;
396
397 25 return;
398 12 };
399
400 12 1
401
402 __END__
« lib/TUI/StdDlg/FindFirstRec/Win32.pm ↑ Summary lib/TUI/Drivers/AttrPair.pm »