| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | BEGIN { pop @INC if $INC[-1] eq '.' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use File::Find; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Getopt::Std; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Archive::Tar; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # Allow (and ignore) --format=ustar, for compatibility with GNU tar | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | for (my $i = 0; $i < @ARGV; ++$i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 17 | ▶ | last if $ARGV[$i] eq '--'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 17 | ▶ | splice @ARGV, $i--, 1 if $ARGV[$i] eq '--format=ustar'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 17 | ▶ | splice @ARGV, $i--, 2 if $i < $#ARGV | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$i < $#ARGV and $ARGV[$i] eq "--format" and $ARGV[$i + 1] eq "ustar"
$i < $#ARGV and $ARGV[$i] eq "--format"
MC/DC66%2/3
$i < $#ARGV and $ARGV[$i] eq "--format" and $ARGV[$i + 1] eq "ustar"
$i < $#ARGV
$ARGV[$i] eq "--format"
$ARGV[$i + 1] eq "ustar"
Truth table75%3/4
A and B and C
A= $i < $#ARGVB= $ARGV[$i] eq "--format"C= $ARGV[$i + 1] eq "ustar"
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | && $ARGV[$i] eq '--format' && $ARGV[$i + 1] eq 'ustar'; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # Allow historic support for dashless bundled options | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # tar cvf file.tar | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # is valid (GNU) tar style | ||||||||||||||||||||||||||||||||||||||||||
| 22 | @ARGV && $ARGV[0] =~ m/^[DdcvzthxIC]+[fT]?$/ and | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | ▶ | unshift @ARGV, map { "-$_" } split m// => shift @ARGV; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@ARGV and $ARGV[0] =~ /^[DdcvzthxIC]+[fT]?$/
MC/DC50%1/2
@ARGV and $ARGV[0] =~ /^[DdcvzthxIC]+[fT]?$/
@ARGV
$ARGV[0] =~ /^[DdcvzthxIC]+[fT]?$/
Truth table66%2/3
A and B
A= @ARGVB= $ARGV[0] =~ /^[DdcvzthxIC]+[fT]?$/
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | my $opts = {}; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 5 | ▶ | getopts('Ddcvzthxf:ICT:', $opts) or die usage(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | ### show the help message ### | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 5 | ▶ | die usage() if $opts->{h}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | ### enable debugging (undocumented feature) | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | local $Archive::Tar::DEBUG = 1 if $opts->{d}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | ### enable insecure extracting. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 5 | ▶ | local $Archive::Tar::INSECURE_EXTRACT_MODE = 1 if $opts->{I}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | ### sanity checks ### | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | ▶ | unless ( 1 == grep { defined $opts->{$_} } qw[x t c] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | die "You need exactly one of 'x', 't' or 'c' options: " . usage(); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | my $compress = $opts->{z} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | my $verbose = $opts->{v} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | ▶ | my $file = $opts->{f} ? $opts->{f} : 'default.tar'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | my $tar = Archive::Tar->new(); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | ▶ | if( $opts->{c} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | my @files; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | my @src = @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | if( $opts->{T} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if( $opts->{T} eq "-" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | chomp( @src = <STDIN> ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | } elsif( open my $fh, "<", $opts->{T} ) { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | chomp( @src = <$fh> ); | |||||||||||||||||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | die "$0: $opts->{T}: $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | find( sub { push @files, $File::Find::name; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | print $File::Find::name.$/ if $verbose }, @src ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | if ($file eq '-') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $file = IO::Handle->new(); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $file->fdopen(fileno(STDOUT),"w"); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 3 | my $tar = Archive::Tar->new; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | $tar->add_files(@files); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | ▶ | if( $opts->{C} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | for my $f ($tar->get_files) { | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $f->mode($f->mode & ~022); # chmod go-w | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | $tar->write($file, $compress); | |||||||||||||||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | if ($file eq '-') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $file = IO::Handle->new(); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $file->fdopen(fileno(STDIN),"r"); | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | ### print the files we're finding? | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | my $print = $verbose || $opts->{'t'} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
$verbose || $opts->{'t'} || 0
$verbose or $opts->{"t"}
MC/DC50%1/2
$verbose || $opts->{'t'} || 0
$verbose
$opts->{"t"}
Truth table66%2/3
A or B
A= $verboseB= $opts->{"t"}
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | my $iter = Archive::Tar->iter( $file ); | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | while( my $f = $iter->() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | ▶ | print $f->full_path . $/ if $print; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | ### data dumper output | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | print Dumper( $f ) if $opts->{'D'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | ### extract it | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | $f->extract if $opts->{'x'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | ### pod & usage in one | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $usage .= << '=cut'; | |||||||||||||||||||||||||||||||||||||||||
| 102 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | ptar - a tar-like program written in perl | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | ptar is a small, tar look-alike program that uses the perl module | ||||||||||||||||||||||||||||||||||||||||||
| 111 | Archive::Tar to extract, create and list tar archives. | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | ptar -c [-v] [-z] [-C] [-f ARCHIVE_FILE | -] FILE FILE ... | ||||||||||||||||||||||||||||||||||||||||||
| 116 | ptar -c [-v] [-z] [-C] [-T index | -] [-f ARCHIVE_FILE | -] | ||||||||||||||||||||||||||||||||||||||||||
| 117 | ptar -x [-v] [-z] [-f ARCHIVE_FILE | -] | ||||||||||||||||||||||||||||||||||||||||||
| 118 | ptar -t [-z] [-f ARCHIVE_FILE | -] | ||||||||||||||||||||||||||||||||||||||||||
| 119 | ptar -h | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | c Create ARCHIVE_FILE or STDOUT (-) from FILE | ||||||||||||||||||||||||||||||||||||||||||
| 124 | x Extract from ARCHIVE_FILE or STDIN (-) | ||||||||||||||||||||||||||||||||||||||||||
| 125 | t List the contents of ARCHIVE_FILE or STDIN (-) | ||||||||||||||||||||||||||||||||||||||||||
| 126 | f Name of the ARCHIVE_FILE to use. Default is './default.tar' | ||||||||||||||||||||||||||||||||||||||||||
| 127 | z Read/Write zlib compressed ARCHIVE_FILE (not always available) | ||||||||||||||||||||||||||||||||||||||||||
| 128 | v Print filenames as they are added or extracted from ARCHIVE_FILE | ||||||||||||||||||||||||||||||||||||||||||
| 129 | h Prints this help message | ||||||||||||||||||||||||||||||||||||||||||
| 130 | C CPAN mode - drop 022 from permissions | ||||||||||||||||||||||||||||||||||||||||||
| 131 | T get names to create from file | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | L<tar(1)>, L<Archive::Tar>. | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | ### strip the pod directives | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | $usage =~ s/=pod\n//g; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $usage =~ s/=head1 //g; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | ### add some newlines | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $usage .= $/.$/; | |||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | return $usage; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||