lib/TUI/StdDlg/FindFirstRec/Unix.pm

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