| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | ############################################################################## | ||||||||||||||||||||||||||||
| 3 | # Tool for using regular expressions against the contents of files in a tar | ||||||||||||||||||||||||||||
| 4 | # archive. See 'ptargrep --help' for more documentation. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | BEGIN { pop @INC if $INC[-1] eq '.' } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Pod::Usage qw(pod2usage); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Getopt::Long qw(GetOptions); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Archive::Tar qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use File::Path qw(mkpath); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | my(%opt, $pattern); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | if(!GetOptions(\%opt, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 'basename|b', | ||||||||||||||||||||||||||||
| 20 | 'ignore-case|i', | ||||||||||||||||||||||||||||
| 21 | 'list-only|l', | ||||||||||||||||||||||||||||
| 22 | 'verbose|v', | ||||||||||||||||||||||||||||
| 23 | 'help|?', | ||||||||||||||||||||||||||||
| 24 | )) { | ||||||||||||||||||||||||||||
| 25 | 0 | pod2usage(-exitval => 1, -verbose => 0); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | pod2usage(-exitstatus => 0, -verbose => 2) if $opt{help}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | pod2usage(-exitval => 1, -verbose => 0, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | -message => "No pattern specified", | ||||||||||||||||||||||||||||
| 33 | ) unless @ARGV; | ||||||||||||||||||||||||||||
| 34 | 1 | make_pattern( shift(@ARGV) ); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | pod2usage(-exitval => 1, -verbose => 0, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | -message => "No tar files specified", | ||||||||||||||||||||||||||||
| 38 | ) unless @ARGV; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | process_archive($_) foreach @ARGV; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | exit 0; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub make_pattern { | ||||||||||||||||||||||||||
|
Subroutine
make_pattern: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 46 | 1 | my($pat) = @_; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | if($opt{'ignore-case'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $pattern = qr{(?im)$pat}; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||
| 52 | 1 | $pattern = qr{(?m)$pat}; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub process_archive { | ||||||||||||||||||||||||||
|
Subroutine
process_archive: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 58 | 1 | my($filename) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | _log("Processing archive: $filename"); | |||||||||||||||||||||||||||
| 61 | 1 | my $next = Archive::Tar->iter($filename); | |||||||||||||||||||||||||||
| 62 | 1 | ▶ | while( my $f = $next->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | next unless $f->is_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | match_file($f) if $f->size > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub match_file { | ||||||||||||||||||||||||||
|
Subroutine
match_file: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||
| 70 | 1 | my($f) = @_; | |||||||||||||||||||||||||||
| 71 | 1 | my $path = $f->name; | |||||||||||||||||||||||||||
| 72 | 1 | my $prefix = $f->prefix; | |||||||||||||||||||||||||||
| 73 | 1 | ▶ | if (defined $prefix) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | $path = File::Spec->catfile($prefix, $path); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | _log("filename: %s (%d bytes)", $path, $f->size); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | my $body = $f->get_content(); | |||||||||||||||||||||||||||
| 80 | 1 | ▶ | if($body !~ $pattern) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | _log(" no match"); | |||||||||||||||||||||||||||
| 82 | 0 | return; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | if($opt{'list-only'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | print $path, "\n"; | |||||||||||||||||||||||||||
| 87 | 1 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | save_file($path, $body); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub save_file { | ||||||||||||||||||||||||||
|
Subroutine
save_file: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 95 | 0 | my($path, $body) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | _log(" found match - extracting"); | |||||||||||||||||||||||||||
| 98 | 0 | my($fh); | |||||||||||||||||||||||||||
| 99 | 0 | my($dir, $file) = $path =~ m{\A(?:(.*)/)?([^/]+)\z}; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | if($dir and not $opt{basename}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$dir and not $opt{"basename"}
MC/DC0%0/2
$dir and not $opt{"basename"}
$dir
not $opt{"basename"}
Truth table0%0/3
A and not(B)
A= $dirB= not $opt{"basename"}
| |||||||||||||||||||||||||||||
| 101 | 0 | _log(" writing to $dir/$file"); | |||||||||||||||||||||||||||
| 102 | 0 | $dir =~ s{\A/}{./}; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | mkpath($dir) unless -d $dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | open $fh, '>', "$dir/$file" or die "open($dir/$file): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||
| 107 | 0 | _log(" writing to ./$file"); | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | open $fh, '>', $file or die "open($file): $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 0 | print $fh $body; | |||||||||||||||||||||||||||
| 111 | 0 | close($fh); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 2 | ▶ | sub _log { | ||||||||||||||||||||||||||
|
Subroutine
_log: called CC=2 SCAR=10.5
| |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | return unless $opt{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | my($format, @args) = @_; | |||||||||||||||||||||||||||
| 118 | 0 | warn sprintf($format, @args) . "\n"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||