lib/Archive/Probe.pm

filter: Statementstmt 75.1% 245 / 326 Branchbran 56.5% 104 / 184 Conditioncond 20.8% 5 / 24 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 87.1% 34 / 39 Podpod 100% 7 / 7 totaltotal 66.6% 397 / 596 CC 107 SCAR 39.3
CC
107
Cov
66%
extract
342.0
__ANON__
20.0
__ANON__
20.0
CRAP 50.9
↑ Summary
1 package Archive::Probe;
2 #
3 # This class searches and extracts files matching given pattern within
4 # deeply nested archive files. Mixed archive types are supported.
5 # Pre-requisite: unrar, 7za should be in PATH
6 # Get free unrar from: http://www.rarlab.com/rar_add.htm
7 # Get free 7za from: http://www.7-zip.org
8 # Author: JustinZhang <fgz@cpan.org>
9 # Creation Date: 2013-05-06
10 #
11 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use File::Spec::Functions qw(catdir catfile devnull path);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 8 our $VERSION = "0.86";
21  
22 8 my %_CMD_LOC_FOR = ();
23  
24 =pod
25  
26 =head1 NAME
27  
28 Archive::Probe - A generic library to search file within archive
29  
30 =head1 SYNOPSIS
31  
32 use Archive::Probe;
33  
34 my $tmpdir = '<temp_dir>';
35 my $base = '<directory_or_archive_file>';
36 my $probe = Archive::Probe->new();
37 $probe->working_dir($tmpdir);
38 $probe->add_pattern(
39 '<your_pattern_here>',
40 sub {
41 my ($pattern, $file_ref) = @_;
42  
43 # do something with result files
44 });
45 $probe->search($base, 1);
46  
47 # or use it as generic archive extractor
48 use Archive::Probe;
49  
50 my $archive = '<path_to_your_achive>';
51 my $dest_dir = '<path_to_dest>';
52 $probe->extract($archive, $dest_dir, 1);
53  
54 =head1 DESCRIPTION
55  
56 Archive::Probe is a generic utility to search or extract archives.
57  
58 It facilitates searching of particular file by name or content inside
59 deeply nested archive with mixed types. It can also extract embedded
60 archive inside the master archive recursively. It is built on top of
61 common archive tools such as 7zip, unrar, unzip and tar. It supports
62 common archive types such as .tar, .tgz, .bz2, .rar, .zip .7z and Java
63 archive such as .jar, .war, .ear. If the target archive file contains
64 another archive file of same or other type, this module extracts the
65 embedded archive to fulfill the inquiry. The level of embedding is
66 unlimited. This module depends on unzip, unrar, 7za and tar which are
67 assumed to be present in PATH. The 7za is part of 7zip utility. It is
68 preferred tool to deal with .zip archive it runs faster and handles meta
69 character better than unzip. The 7zip is open source software and you
70 download and install it from www.7-zip.org or install the binary package
71 p7zip with your favorite package management software. The unrar is
72 freeware which can be downloaded from http://www.rarlab.com/rar_add.htm.
73  
74 =cut
75  
76 =head1 METHODS
77  
78 =head2 constructor new()
79  
80 Creates a new C<Archive::Probe> object.
81  
82 =cut
83  
84 8 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
85 8 my $self = shift;
86  
87 8 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
88 8 return bless {}, $class;
89 }
90  
91 =head2 add_pattern($pattern, $callback)
92  
93 Register a file pattern to search with in the archive file(s) and the
94 callback code to handle the matched files. The callback will be passed
95 two arguments:
96  
97 =over 4
98  
99 =item $pattern
100  
101 This is the pattern of files to be searched.
102  
103 =item $callback
104  
105 This is the callback to examine the search result. The array reference
106 to the files matched the pattern is passed to the callback. If you want
107 to examine the content of the matched files, then you set the second
108 argument of the C<search()> method to true.
109  
110 =back
111  
112 =cut
113  
114 8 ▶sub add_pattern {
Subroutine add_pattern: called CC=4 SCAR=14.0
Pod documented
115 8 my ($self, $pattern, $callback) = @_;
116  
117 # validate pattern and callback
118 8 ▶ confess("Pattern is mandatory\n") unless $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern 0 8 8
119 8 ▶ confess("Code reference is expected\n") unless ref($callback) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 8 8
120  
121 8 my $pattern_map = $self->_search_pattern();
122 8 ▶ if (!$pattern_map) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern_map) 3 5 8
123 3 $pattern_map = {};
124 3 $self->_search_pattern($pattern_map);
125 }
126  
127 8 $pattern_map->{$pattern} = [$callback];
128 }
129  
130 =head2 search($base, $extract_matched)
131  
132 Search files of interest under 'base' and invoke the callback.
133 It requires two arguments:
134  
135 =over 4
136  
137 =item $base
138  
139 This is the directory containing the archive file(s) or the archive file
140 itself.
141  
142 =item $extract_matched
143  
144 Extract or copy the matched files to the working directory
145 if this parameter evaluates to true. This is useful when you need search
146 files based on their content not just by name.
147  
148 =back
149  
150 =cut
151  
152 4 ▶sub search {
Subroutine search: called CC=11 SCAR=24.3
Pod documented
153 4 my ($self, $base, $do_extract) = @_;
154
155 4 my @queue = ();
156 4 push @queue, $base;
157  
158 4 ▶ while (my $path = shift @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = shift @queue) 20 4 24
159 20 ▶ if (-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 8 12 20
elsif (-f $path) { } 12 0 12
160 8 ▶ opendir(my $dh, $path) or do {
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $path or do { carp("Can't read directory due to: $!\n"); next } 8 0 8
161 0 carp("Can't read directory due to: $!\n");
162 0 next;
163 };
164  
165 8 ▶ while (my $entry = readdir($dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 32 8 40
166 32 ▶ next if $entry eq '.' || $entry eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "." or $entry eq ".." 16 16 32
Condition100%3/3
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
8816
MC/DC100%2/2
$entry eq "." or $entry eq ".."
$entry eq "." $entry eq ".."
Truth table100%3/3
A or B
A= $entry eq "."B= $entry eq ".."
ABresult
1X1
011
000
167 16 push @queue, catfile($path, $entry);
168 }
169 8 closedir($dh);
170 }
171 elsif (-f $path) {
172 12 my $new_base = $base;
173 12 ▶ $new_base = dirname($base) if $base eq $path;
Branch: 1 branch
BranchTrueFalseTotal
if $base eq $path 0 12 12
174 # Test if the file matches regestered pattern
175 12 $self->_match($do_extract, $new_base, '', $path);
176 12 ▶ if ($self->_is_archive_file($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_archive_file($path)) 10 2 12
177 10 my $ctx = $self->_strip_dir($new_base, $path) ;
178 10 ▶ $ctx .= '__' if $ctx ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $ctx ne "" 10 0 10
179 10 $self->_search_in_archive(
180 $do_extract,
181 $new_base,
182 $ctx,
183 $path
184 );
185 }
186 }
187 }
188  
189 # check search result & invoke callback
190 4 $self->_callback();
191 }
192  
193 =head2 extract($base, $to_dir, $recursive, $flat)
194  
195 Extract archive to given destination directory.
196 It requires three arguments:
197  
198 =over 4
199  
200 =item $base
201  
202 This is the path to the archive file or the base archive directory.
203  
204 =item $to_dir
205  
206 The destination directory.
207  
208 =item $recursive
209  
210 Recursively extract all embedded archive files in the master archive if
211 this parameter evaluates to true. It defaults to true.
212  
213 =item $flat
214  
215 If this parameter evaluates to true, C<Archive::Probe> extracts embedded
216 archives under the same folder as their containing folder in recursive
217 mode. Otherwise, it extracts the content of embedded archives into their
218 own directories to avoid files with same name from different embedded
219 archive being overwritten. Default is false.
220  
221 =item return value
222  
223 The return value of this method evaluates to true if the archive is
224 extacted successfully. Otherwise, it evaluates to false.
225  
226 =back
227  
228 =cut
229  
230 0 ▶sub extract {
Subroutine extract: not called CC=18 SCAR=58.3
Pod documented
231 0 my ($self, $base, $to_dir, $recursive, $flat) = @_;
232
233 0 ▶ $recursive = 1 unless defined($recursive);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $recursive 0 0 0
234 0 my @queue = ();
235 0 my %searched_for = ();
236 0 push @queue, $base;
237  
238 0 ▶ while (my $path = shift @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = shift @queue) 0 0 0
239 0 ▶ if (-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
elsif ($self->_is_archive_file($path)) { } 0 0 0
240 # search archives in this directory
241 0 my $ret = opendir(my $dh, $path);
242 0 ▶ if (!$ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 0 0
243 0 carp("Can't read directory due to: $!\n");
244 0 next;
245 }
246  
247 0 ▶ while (my $entry = readdir($dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 0 0 0
248 0 ▶ next if $entry eq '.' || $entry eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "." or $entry eq ".." 0 0 0
Condition0%0/3
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$entry eq "." or $entry eq ".."
$entry eq "." $entry eq ".."
Truth table0%0/3
A or B
A= $entry eq "."B= $entry eq ".."
ABresult
1X1
011
000
249 0 my $f = catfile($path, $entry);
250 0 ▶ if (-d $f ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $f) { } 0 0 0
elsif (-f $f and $self->_is_archive_file($f)) { } 0 0 0
251 0 push @queue, $f;
252 }
253 ▶ elsif (-f $f && $self->_is_archive_file($f)) {
Condition0%0/3
-f $f and $self->_is_archive_file($f)
!ll&&!rl&&r
000
MC/DC0%0/2
-f $f and $self->_is_archive_file($f)
-f $f $self->_is_archive_file($f)
Truth table0%0/3
A and B
A= -f $fB= $self->_is_archive_file($f)
ABresult
0X0
100
111
254 0 ▶ push @queue, $f unless $searched_for{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $searched_for{$f} 0 0 0
255 }
256 }
257 0 closedir($dh);
258 }
259 elsif ($self->_is_archive_file($path)) {
260 0 $searched_for{$path} = 1;
261 # extract archive and find any embedded archives
262 # if recursive extraction is required
263 0 my $dest_dir = $to_dir;
264 0 ▶ if (index($path, $to_dir) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($path, $to_dir) != -1) 0 0 0
265 0 ▶ if ($flat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flat) { } 0 0 0
266 0 $dest_dir = dirname($path);
267 }
268 else {
269 0 $dest_dir = catdir(
270 dirname($path),
271 basename($path) . "__"
272 );
273 }
274 }
275 0 my $ret = $self->_extract_archive_file($path, "", $dest_dir);
276 0 ▶ if ($ret && $recursive) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret and $recursive) { } 0 0 0
elsif (not $ret) { } 0 0 0
Condition0%0/3
$ret and $recursive
!ll&&!rl&&r
000
MC/DC0%0/2
$ret and $recursive
$ret $recursive
Truth table0%0/3
A and B
A= $retB= $recursive
ABresult
0X0
100
111
277 0 push @queue, $dest_dir;
278 }
279 elsif (!$ret) {
280 0 return 0;
281 }
282 }
283 }
284 0 return 1;
285 }
286  
287 =head2 reset_matches()
288  
289 Reset the matched files list.
290  
291 =cut
292  
293 4 ▶sub reset_matches {
Subroutine reset_matches: called CC=2 SCAR=6.9
Pod documented
294 4 my ($self) = @_;
295  
296 4 my $patterns = $self->_search_pattern();
297 4 foreach my $pat (keys(%$patterns)) {
298 10 undef($patterns->{$pat}[1]);
299 }
300 }
301  
302 =head1 ACCESSORS
303  
304 =head2 working_dir([$directory])
305  
306 Set or get the working directory where the temporary files will be created.
307  
308 =cut
309  
310 24 ▶sub working_dir {
Subroutine working_dir: called CC=2 SCAR=6.9
Pod documented
311 24 my ($self, $value) = @_;
312  
313 24 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 4 20 24
314 4 my $oldval = $self->{working_dir};
315 4 $self->{working_dir} = $value;
316 4 return $oldval;
317 }
318  
319 20 return $self->{working_dir};
320 }
321  
322 =head2 show_extracting_output([BOOL])
323  
324 Enable or disable the output of command line archive tool.
325  
326 =cut
327  
328 13 ▶sub show_extracting_output {
Subroutine show_extracting_output: called CC=2 SCAR=9.2
Pod documented
329 13 my ($self, $value) = @_;
330  
331 13 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 13 13
332 0 my $oldval = $self->{show_extracting_output};
333 0 $self->{show_extracting_output} = $value;
334 0 return $oldval;
335 }
336  
337 13 return $self->{show_extracting_output};
338 }
339  
340 12 ▶sub _extract_matched {
Subroutine _extract_matched: called CC=7 SCAR=20.7
341 12 my ($self, $base_dir, $ctx, $file, $do_extract) = @_;
342  
343 12 my $dest;
344 12 my $work_dir = $self->working_dir();
345 # extract the matched file here
346 12 ▶ if ($ctx ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx ne '') { } 11 1 12
347 # parent file location = $base_dir + substr($ctx, 0, -2)
348 11 my $parent = catfile($base_dir, substr($ctx, 0, -2));
349 11 my $extract_dir = catdir($work_dir, $ctx);
350 11 ▶ if ($do_extract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_extract) 11 0 11
351 11 my $ret = $self->_extract_archive_file(
352 $parent,
353 $file,
354 $extract_dir
355 );
356 11 ▶ if (!$ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 11 11
357 0 carp("$file can not be extracted from $parent, ignored\n");
358 0 return;
359 }
360 }
361 11 $dest = catfile($extract_dir, $file);
362 }
363 else {
364 # matched files are unarchived
365 # copy to working directory as-is
366 # create absent local dir first
367 1 my $local_path = $self->_strip_dir($base_dir, $file);
368 1 $dest = catfile($work_dir, $local_path);
369  
370 1 ▶ if ($do_extract) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_extract) 1 0 1
371 1 my $dir2 = catdir($work_dir, $self->_dir_name($local_path));
372 1 ▶ mkpath($dir2) unless -d $dir2;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir2 1 0 1
373 1 my $ret = copy($file, $dest);
374 1 ▶ if (!$ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 1 1
375 0 carp("Can't copy file $file to $dest due to: $!\n");
376 0 return;
377 }
378 }
379 }
380 12 return $dest;
381 }
382  
383 97 ▶sub _match {
Subroutine _match: called CC=5 SCAR=16.1
384 97 my ($self, $do_extract, $base_dir, $ctx, $file) = @_;
385  
386 97 my $matches = 0;
387 97 my $part = $self->_strip_dir(catdir($base_dir, $ctx), $file);
388 97 my $patterns = $self->_search_pattern();
389 97 foreach my $pat (keys(%$patterns)) {
390 217 ▶ if ($part =~ /$pat/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /$pat/) 12 205 217
391 12 $matches ++;
392 12 my $dest = $self->_extract_matched(
393 $base_dir,
394 $ctx,
395 $file,
396 $do_extract
397 );
398 # do not add file to matched list if extract fails
399 12 ▶ next unless $dest;
Branch: 1 branch
BranchTrueFalseTotal
unless $dest 0 12 12
400  
401 12 my $pat_ref = $patterns->{$pat};
402 12 ▶ if (!defined($pat_ref->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pat_ref->[1]) { } 10 2 12
403 10 $pat_ref->[1] = [$dest];
404 }
405 else {
406 2 push @{$pat_ref->[1]}, $dest;
407 }
408 }
409 }
410 97 return $matches;
411 }
412  
413 4 ▶sub _callback {
Subroutine _callback: called CC=4 SCAR=14.5
414 4 my ($self) = @_;
415  
416 4 my $patterns = $self->_search_pattern();
417 4 foreach my $pat (keys(%$patterns)) {
418 10 my $pat_ref = $patterns->{$pat};
419 10 ▶ if (ref($pat_ref->[0]) eq 'CODE' && defined($pat_ref->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pat_ref->[0] eq "CODE" and defined $pat_ref->[1]) 10 0 10
Condition33%1/3
ref $pat_ref->[0] eq "CODE" and defined $pat_ref->[1]
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $pat_ref->[0] eq "CODE" and defined $pat_ref->[1]
ref $pat_ref->[0] eq "CODE" defined $pat_ref->[1]
Truth table33%1/3
A and B
A= ref $pat_ref->[0] eq "CODE"B= defined $pat_ref->[1]
ABresult
0X0
100
111
420 10 $pat_ref->[0]->($pat, $pat_ref->[1]);
421 }
422 }
423 4 $self->_cleanup();
424 }
425  
426 12 ▶sub _search_in_archive {
Subroutine _search_in_archive: called CC=8 SCAR=29.6
427 12 my ($self, $do_extract, $base_dir, $ctx, $file) = @_;
428  
429 12 ▶ if ($file =~ /\.zip$|\.jar$|\.war$|\.ear$/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($file =~ /\.zip$|\.jar$|\.war$|\.ear$/) { } 3 9 12
elsif ($file =~ /\.7z$/) { } 0 9 9
elsif ($file =~ /\.rar$/) { } 4 5 9
elsif ($file =~ /\.tgz$|\.tar\.gz$|\.tar\.Z$/) { } 3 2 5
elsif ($file =~ /\.bz2$/) { } 1 1 2
elsif ($file =~ /\.tar$/) { } 1 0 1
430 3 ▶ if ($self->_is_cmd_avail('7za')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_cmd_avail('7za')) { } 0 3 3
431 $self->_peek_archive(
432 $do_extract,
433 $base_dir,
434 $ctx,
435 $file,
436 '7za l',
437 '(-+)\s+(-+)\s+(-+)\s+(-+)\s+(-+)',
438 '---+',
439 '',
440 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
441 0 my ($entry, undef, undef, undef, undef, $file_pos) = @_;
442 0 my (undef, undef, $a, undef) = split(' ', $entry, 4);
443 0 ▶ return if $a =~ /^D/;
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /^D/ 0 0 0
444 0 ▶ if ($file_pos && $file_pos < length($entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_pos and $file_pos < length $entry) 0 0 0
Condition0%0/3
$file_pos and $file_pos < length $entry
!ll&&!rl&&r
000
MC/DC0%0/2
$file_pos and $file_pos < length $entry
$file_pos $file_pos < length $entry
Truth table0%0/3
A and B
A= $file_posB= $file_pos < length $entry
ABresult
0X0
100
111
445 0 my $f = substr($entry, $file_pos);
446 0 return $f;
447 }
448 0 return;
449 }
450 0 );
451 }
452 else {
453 $self->_peek_archive(
454 $do_extract,
455 $base_dir,
456 $ctx,
457 $file,
458 "unzip -l",
459 "--------",
460 "--------",
461 '',
462 78 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
463 78 my ($entry) = @_;
464 78 my (undef, undef, undef, $f) = split(' ', $entry, 4);
465 78 return $f;
466 }
467 3 );
468  
469 }
470 }
471 elsif ($file =~ /\.7z$/) {
472 $self->_peek_archive(
473 $do_extract,
474 $base_dir,
475 $ctx,
476 $file,
477 '7za l',
478 '(-+)\s+(-+)\s+(-+)\s+(-+)\s+(-+)',
479 '---+',
480 '',
481 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
482 0 my ($entry, undef, undef, undef, undef, $file_pos_7z) = @_;
483 0 my (undef, undef, $a, undef) = split(' ', $entry, 4);
484 0 ▶ return if $a =~ /^D/;
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /^D/ 0 0 0
485 0 ▶ if ($file_pos_7z && $file_pos_7z < length($entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_pos_7z and $file_pos_7z < length $entry) 0 0 0
Condition0%0/3
$file_pos_7z and $file_pos_7z < length $entry
!ll&&!rl&&r
000
MC/DC0%0/2
$file_pos_7z and $file_pos_7z < length $entry
$file_pos_7z $file_pos_7z < length $entry
Truth table0%0/3
A and B
A= $file_pos_7zB= $file_pos_7z < length $entry
ABresult
0X0
100
111
486 0 my $f = substr($entry, $file_pos_7z);
487 0 return $f;
488 }
489 0 return;
490 }
491 0 );
492 }
493 elsif ($file =~ /\.rar$/) {
494 $self->_peek_archive(
495 $do_extract,
496 $base_dir,
497 $ctx,
498 $file,
499 "unrar vb",
500 '',
501 '',
502 '',
503 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
504 0 my ($entry) = @_;
505 0 return $entry;
506 }
507 4 );
508 }
509 elsif ($file =~ /\.tgz$|\.tar\.gz$|\.tar\.Z$/) {
510 $self->_peek_archive(
511 $do_extract,
512 $base_dir,
513 $ctx,
514 $file,
515 "tar -tzf",
516 '',
517 '',
518 '\/$',
519 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
520 5 my ($entry) = @_;
521 5 return $entry;
522 }
523 3 );
524 }
525 elsif ($file =~ /\.bz2$/) {
526 $self->_peek_archive(
527 $do_extract,
528 $base_dir,
529 $ctx,
530 $file,
531 "tar -tjf",
532 '',
533 '',
534 '\/$',
535 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
536 1 my ($entry) = @_;
537 1 return $entry;
538 }
539 1 );
540 }
541 elsif ($file =~ /\.tar$/) {
542 $self->_peek_archive(
543 $do_extract,
544 $base_dir,
545 $ctx,
546 $file,
547 "tar -tf",
548 '',
549 '',
550 '\/$',
551 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
552 1 my ($entry) = @_;
553 1 return $entry;
554 }
555 1 );
556 }
557 else {
558 0 carp("Archive file $file is not supported\n");
559 }
560 }
561  
562 12 ▶sub _peek_archive {
Subroutine _peek_archive: called CC=16 SCAR=28.1
563 12 my ($self,
564 $do_extract,
565 $base_dir,
566 $ctx,
567 $file,
568 $list_cmd,
569 $begin_pat,
570 $end_pat,
571 $ignore_pat,
572 $sub
573 ) = @_;
574  
575 # stop peeking if archive tool is not available
576 12 my ($ar_cmd) = split(/\s+/, $list_cmd);
577 12 ▶ if (!$self->_is_cmd_avail($ar_cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_cmd_avail($ar_cmd)) 4 8 12
578 4 carp("$ar_cmd not in PATH, archive $file ignored\n");
579 4 return;
580 }
581
582 8 my $tmpdir = $self->working_dir();
583 8 my $lst_file = $self->_get_list_file();
584 8 my $cmd = join(" ", $list_cmd, $self->_escape($file));
585 8 my $cmd_shell = "$cmd > $lst_file 2>&1";
586 8 my $ret = system($cmd_shell);
587 8 ▶ if ($ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret != 0) 0 8 8
588 0 carp("Can't run $cmd\n");
589 0 return;
590 }
591 8 $ret = open(my $fh, q{<}, "$lst_file");
592 8 ▶ if (!$ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 8 8
593 0 carp("Can't open file $lst_file due to: $!\n");
594 0 return;
595 }
596  
597 8 my @col_indexes;
598 8 my $file_list_begin = 0;
599 8 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 97 5 102
600 97 chomp;
601 97 my $line = $_;
602 97 ▶ if ($begin_pat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin_pat) 90 7 97
603 90 ▶ if (! $file_list_begin) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_list_begin) 9 81 90
604 # determine if the start of file list and
605 # calculate start position of each column
606 9 my @captures = $line =~ /$begin_pat/g;
607 9 ▶ if (@captures) {
Branch: 1 branch
BranchTrueFalseTotal
if (@captures) 3 6 9
608 3 my $pos = 0;
609 3 $file_list_begin = 1;
610 3 foreach my $cap (@captures) {
611 6 push @col_indexes, index($line, $cap, $pos);
612 6 $pos += length($cap);
613 }
614 }
615 9 next;
616 }
617 }
618  
619 88 ▶ if ($ignore_pat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore_pat) 7 81 88
620 7 ▶ next if /$ignore_pat/;
Branch: 1 branch
BranchTrueFalseTotal
if /$ignore_pat/ 0 7 7
621 }
622  
623 88 ▶ if ($end_pat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_pat) 81 7 88
624 81 ▶ last if /$end_pat/;
Branch: 1 branch
BranchTrueFalseTotal
if /$end_pat/ 3 78 81
625 }
626  
627 85 my $f = $sub->($line, @col_indexes);
628 # ignore empty line, usually directory
629 85 ▶ next unless $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 85 85
630 85 $self->_match($do_extract, $base_dir, $ctx, $f);
631 85 ▶ if ($self->_is_archive_file($f)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_archive_file($f)) 2 83 85
632 2 my $extract_dir = catdir($tmpdir, $ctx);
633 2 my $ret = $self->_extract_archive_file($file, $f, $extract_dir);
634 2 ▶ if ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) { } 2 0 2
635 2 my $new_ctx = catfile($ctx, $f . '__');
636 2 $self->_search_in_archive(
637 $do_extract,
638 $tmpdir,
639 $new_ctx,
640 catfile($extract_dir, $f)
641 );
642 }
643 else {
644 0 carp("$f can not be extracted from $file, ignored\n");
645 }
646 }
647 }
648 8 close($fh);
649 }
650  
651 13 ▶sub _extract_archive_file {
Subroutine _extract_archive_file: called CC=12 SCAR=28.5
652 13 my ($self, $parent, $file, $extract_dir) = @_;
653  
654 13 ▶ mkpath($extract_dir) unless -d $extract_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $extract_dir 6 7 13
655 13 my $cmd = "";
656 13 ▶ if ($parent =~ /\.zip$|\.jar$|\.war$|\.ear$/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($parent =~ /\.zip$|\.jar$|\.war$|\.ear$/) { } 8 5 13
elsif ($parent =~ /\.7z$/) { } 0 5 5
elsif ($parent =~ /\.rar$/) { } 0 5 5
elsif ($parent =~ /\.tgz$|\.tar\.gz$|\.tar\.Z$/) { } 3 2 5
elsif ($parent =~ /\.bz2$/) { } 1 1 2
elsif ($parent =~ /\.tar$/) { } 1 0 1
657 8 ▶ if ($self->_is_cmd_avail('7za')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_cmd_avail('7za')) { } 0 8 8
658 # specify dummy password to make 7za fail fast
659 # instead of waiting for user input password when
660 # the zip file is password-protected
661 0 $cmd = $self->_build_cmd(
662 '7za x -y -pxxx',
663 $extract_dir,
664 $parent,
665 $file
666 );
667 }
668 else {
669 8 ▶ if ($^O !~ /bsd$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $^O =~ /bsd$/i) { } 8 0 8
670 # specify dummy password to make unzip fail fast
671 # instead of waiting for user input password when
672 # the zip file is password-protected
673 8 $cmd = $self->_build_cmd(
674 'unzip -P xxx -o',
675 $extract_dir,
676 $parent,
677 $file
678 );
679 }
680 else {
681 # FreeBSD and its derivatives do NOT support -P
682 0 ▶ if ($file !~ /[;<>\\\*\|`&\$!#\(\)\[\]\{\}:'"]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file =~ /[;<>\\\*\|`&\$!#\(\)\[\]\{\}:'"]/) { } 0 0 0
683 0 $cmd = $self->_build_cmd(
684 'unzip -o',
685 $extract_dir,
686 $parent,
687 $file
688 );
689 }
690 else {
691 # extract all files if the matched
692 # file has shell meta-char in the name
693 0 $cmd = $self->_build_cmd(
694 'unzip -o',
695 $extract_dir,
696 $parent,
697 ''
698 );
699 }
700 }
701 }
702 }
703 elsif ($parent =~ /\.7z$/) {
704 # specify dummy password to make 7za fail fast
705 # instead of waiting for user input password when
706 # the zip file is password-protected
707 0 $cmd = $self->_build_cmd(
708 '7za x -y -pxxx',
709 $extract_dir,
710 $parent,
711 $file
712 );
713 }
714 elsif ($parent =~ /\.rar$/) {
715 0 $cmd = $self->_build_cmd(
716 'unrar x -o+',
717 $extract_dir,
718 $parent,
719 $file
720 );
721 }
722 elsif ($parent =~ /\.tgz$|\.tar\.gz$|\.tar\.Z$/) {
723 # The "-o" avoid to restore the owner as it could be root
724 3 $cmd = $self->_build_cmd(
725 'tar -xzof',
726 $extract_dir,
727 $parent,
728 $file
729 );
730 }
731 elsif ($parent =~ /\.bz2$/) {
732 # The "-o" avoid to restore the owner as it could be root
733 1 $cmd = $self->_build_cmd(
734 'tar -xjof',
735 $extract_dir,
736 $parent,
737 $file
738 );
739 }
740 elsif ($parent =~ /\.tar$/) {
741 # The "-o" avoid to restore the owner as it could be root
742 1 $cmd = $self->_build_cmd(
743 'tar -xof',
744 $extract_dir,
745 $parent,
746 $file
747 );
748 }
749 13 my $cmd_shell = sprintf("%s 2>%s 1>&2", $cmd, devnull());
750 13 ▶ $cmd_shell = "$cmd 1>&2" if $self->show_extracting_output();
Branch: 1 branch
BranchTrueFalseTotal
if $self->show_extracting_output 0 13 13
751 13 my $ret = system($cmd_shell);
752 13 return $ret == 0;
753 }
754  
755 13 ▶sub _build_cmd {
Subroutine _build_cmd: called CC=2 SCAR=7.4
756 13 my ($self, $extract_cmd, $dir, $parent, $file) = @_;
757  
758 13 my $chdir_cmd = q[cd];
759 13 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 13 13
760 0 $chdir_cmd = q[cd /d];
761 }
762 13 return sprintf(
763 "%s %s && %s %s %s",
764 $chdir_cmd,
765 $self->_escape($dir),
766 $extract_cmd,
767 $self->_escape($parent),
768 $self->_escape($file)
769 );
770 }
771  
772 34 ▶sub _is_cmd_avail {
Subroutine _is_cmd_avail: called CC=8 SCAR=25.4
773 34 my ($self, $cmd) = @_;
774  
775 34 ▶ if (!exists $_CMD_LOC_FOR{$cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_CMD_LOC_FOR{$cmd}) 12 22 34
776 12 my @path = path();
777 12 foreach my $p (@path) {
778 112 my $fp = catfile($p, $cmd);
779 112 ▶ if (-f $fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fp) { } 4 108 112
780 4 $_CMD_LOC_FOR{$cmd} = $fp;
781 4 return 1;
782 }
783 else {
784 108 ▶ if($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 108 108
785 # try to append .exe to the name
786 0 my $fp_win = $fp . ".exe";
787 0 ▶ if (-f $fp_win) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fp_win) 0 0 0
788 0 $_CMD_LOC_FOR{$cmd} = $fp_win;
789 0 return 1;
790 }
791 # try to append .bat to the name
792 0 $fp_win = $fp . ".bat";
793 0 ▶ if (-f $fp_win) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fp_win) 0 0 0
794 0 $_CMD_LOC_FOR{$cmd} = $fp_win;
795 0 return 1;
796 }
797 }
798 }
799 }
800 # executable not found, won't try again
801 8 $_CMD_LOC_FOR{$cmd} = "";
802 }
803 30 ▶ return $_CMD_LOC_FOR{$cmd} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_CMD_LOC_FOR{$cmd} ? : 8 22 30
804 }
805  
806 118 ▶sub _strip_dir {
Subroutine _strip_dir: called CC=4 SCAR=14.0
807 118 my ($self, $base_dir, $path) = @_;
808  
809 118 my $dir1 = $base_dir;
810 118 my $path1 = $path;
811  
812 118 my $path_sep = '/';
813 118 ▶ $path_sep = '\\' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 118 118
814  
815 118 ▶ $dir1 .= $path_sep unless substr($dir1, -1, 1) eq $path_sep;
Branch: 1 branch
BranchTrueFalseTotal
unless substr($dir1, -1, 1) eq $path_sep 118 0 118
816 118 ▶ if (index($path1, $dir1) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($path1, $dir1) == 0) 33 85 118
817 33 $path1 = substr($path1, length($dir1));
818 }
819 118 return $path1;
820 }
821  
822 47 ▶sub _escape {
Subroutine _escape: called CC=3 SCAR=12.7
823 47 my ($self, $str) = @_;
824  
825 47 my $ret = $str;
826 47 ▶ if ($^O ne 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne 'MSWin32') { } 47 0 47
827 47 $ret =~ s/([ ;<>\\\*\|`&\$!#\(\)\[\]\{\}:'"])/\\$1/g;
828 }
829 else {
830 0 ▶ $ret = qq["$ret"] if $ret =~ /[ &#*\|\[\]\(\)\{\}\=;!+,`~']/;
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ /[ &#*\|\[\]\(\)\{\}\=;!+,`~']/ 0 0 0
831 }
832 47 return $ret;
833 }
834  
835 97 ▶sub _is_archive_file {
Subroutine _is_archive_file: called CC=1 SCAR=0.0
836 97 my ($self, $file) = @_;
837  
838 97 return $file =~ /\.(zip|jar|war|ear|7z|rar|tgz|bz2|tar|tar\.gz|tar\.Z)$/
839 }
840  
841 15 ▶sub _property {
Subroutine _property: called CC=3 SCAR=11.0
842 15 my ($self, $attr, $value) = @_;
843  
844 15 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 3 12 15
845 3 my $oldval = $self->{$attr};
846 3 $self->{$attr} = $value;
847 $self->{_properties_with_value} = {}
848 3 ▶ if(!exists $self->{_properties_with_value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_properties_with_value"} 3 0 3
849 3 $self->{_properties_with_value}{$attr} = 1;
850 3 return $oldval;
851 }
852  
853 12 return $self->{$attr};
854 }
855  
856 0 ▶sub _remove_property {
Subroutine _remove_property: not called CC=1 SCAR=6.9
857 0 my ($self, $attr) = @_;
858  
859 0 $self->{$attr} = undef;
860 }
861  
862 116 ▶sub _search_pattern {
Subroutine _search_pattern: called CC=2 SCAR=6.9
863 116 my ($self, $value) = @_;
864  
865 116 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 3 113 116
866 3 my $oldval = $self->{search_pattern};
867 3 $self->{search_pattern} = $value;
868 3 return $oldval;
869 }
870  
871 113 return $self->{search_pattern};
872 }
873  
874 1 ▶sub _dir_name {
Subroutine _dir_name: called CC=3 SCAR=11.6
875 1 my ($self, $path) = @_;
876  
877 1 my $path_sep = '/';
878 1 ▶ $path_sep = '\\' if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 1 1
879 1 my $idx = rindex($path, $path_sep);
880 1 ▶ if ($idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx > 0) { } 1 0 1
881 1 return substr($path, 0, $idx);
882 }
883 else {
884 0 return '';
885 }
886 }
887  
888 8 ▶sub _get_list_file {
Subroutine _get_list_file: called CC=2 SCAR=6.9
889 8 my ($self) = @_;
890  
891 8 my (undef, $lst) = tempfile();
892 8 my $files = $self->_property('archive_lst_files');
893 8 ▶ if (!defined($files)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $files) 3 5 8
894 3 $files = [];
895 3 $self->_property('archive_lst_files', $files);
896 }
897 8 push @$files, $lst;
898 8 return $lst;
899 }
900  
901 4 ▶sub _cleanup {
Subroutine _cleanup: called CC=2 SCAR=6.9
902 4 my ($self) = @_;
903  
904 4 my $files = $self->_property('archive_lst_files');
905 4 foreach my $f (@$files) {
906 10 unlink($f);
907 }
908 }
909  
910 8 1;
911  
912 =pod
913  
914 =head1 HOW IT WORKS
915  
916 C<Archive::Probe> provides plumbing boiler code to search files in nested
917 archive files. It does the heavy lifting to extract mininal files necessary
918 to fulfill the inquiry.
919  
920 =head1 SOURCE AVAILABILITY
921  
922 This code is hosted on Github
923  
924 https://github.com/schnell18/archive-probe
925  
926 =head1 BUG REPORTS
927  
928 Please report bugs or other issues to E<lt>fgz@rt.cpan.orgE<gt>.
929  
930 =head1 AUTHOR
931  
932 This module is developed by Justin Zhang E<lt>fgz@cpan.orgE<gt>.
933  
934 =head1 COPYRIGHT
935  
936 Copyright (C) 2013 by Justin Zhang
937  
938 This library is free software; you may redistribute and/or modify it
939 under the same terms as Perl itself.
940  
941 =cut
942  
943 # vim: set ai nu nobk expandtab sw=4 ts=4 tw=72 :
↑ Summary