File Coverage

bin/ptar
Criterion Covered Total %
statement 61 77 79.2
branch 30 48 62.5
condition 9 11 81.8
subroutine 10 11 90.9
pod n/a
total 110 147 74.8


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 5     5   26583 use strict;
  5         10  
  5         201  
3 5     5   25 use warnings;
  5         9  
  5         418  
4              
5 5 50   5   154 BEGIN { pop @INC if $INC[-1] eq '.' }
6 5     5   30 use File::Find;
  5         8  
  5         567  
7 5     5   2708 use Getopt::Std;
  5         13084  
  5         409  
8 5     5   3819 use Archive::Tar;
  5         20  
  5         427  
9 5     5   3521 use Data::Dumper;
  5         48637  
  5         3216  
10              
11             # Allow (and ignore) --format=ustar, for compatibility with GNU tar
12 5         1004651 for (my $i = 0; $i < @ARGV; ++$i) {
13 17 50       53 last if $ARGV[$i] eq '--';
14 17 100       44 splice @ARGV, $i--, 1 if $ARGV[$i] eq '--format=ustar';
15 17 100 100     97 splice @ARGV, $i--, 2 if $i < $#ARGV
      66        
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 100 66     53 unshift @ARGV, map { "-$_" } split m// => shift @ARGV;
  3         8  
24 5         30 my $opts = {};
25 5 50       70 getopts('Ddcvzthxf:ICT:', $opts) or die usage();
26              
27             ### show the help message ###
28 5 50       524 die usage() if $opts->{h};
29              
30             ### enable debugging (undocumented feature)
31 5 50       19 local $Archive::Tar::DEBUG = 1 if $opts->{d};
32              
33             ### enable insecure extracting.
34 5 50       19 local $Archive::Tar::INSECURE_EXTRACT_MODE = 1 if $opts->{I};
35              
36             ### sanity checks ###
37 5 50       15 unless ( 1 == grep { defined $opts->{$_} } qw[x t c] ) {
  15         47  
38 0         0 die "You need exactly one of 'x', 't' or 'c' options: " . usage();
39             }
40              
41 5 50       26 my $compress = $opts->{z} ? 1 : 0;
42 5 100       19 my $verbose = $opts->{v} ? 1 : 0;
43 5 50       26 my $file = $opts->{f} ? $opts->{f} : 'default.tar';
44 5         61 my $tar = Archive::Tar->new();
45              
46 5 100       20 if( $opts->{c} ) {
47 3         70 my @files;
48 3         55 my @src = @ARGV;
49 3 50       44 if( $opts->{T} ) {
50 0 0       0 if( $opts->{T} eq "-" ) {
    0          
51 0         0 chomp( @src = );
52             } elsif( open my $fh, "<", $opts->{T} ) {
53 0         0 chomp( @src = <$fh> );
54             } else {
55 0         0 die "$0: $opts->{T}: $!\n";
56             }
57             }
58              
59 3     3   12 find( sub { push @files, $File::Find::name;
60 3 100       390 print $File::Find::name.$/ if $verbose }, @src );
  3         64  
61              
62 3 50       22 if ($file eq '-') {
63 5     5   55 use IO::Handle;
  5         11  
  5         723  
64 0         0 $file = IO::Handle->new();
65 0         0 $file->fdopen(fileno(STDOUT),"w");
66             }
67              
68 3         14 my $tar = Archive::Tar->new;
69 3         16 $tar->add_files(@files);
70 3 50       9 if( $opts->{C} ) {
71 0         0 for my $f ($tar->get_files) {
72 0         0 $f->mode($f->mode & ~022); # chmod go-w
73             }
74             }
75 3         27 $tar->write($file, $compress);
76             } else {
77 2 50       145 if ($file eq '-') {
78 5     5   33 use IO::Handle;
  5         13  
  5         11596  
79 0         0 $file = IO::Handle->new();
80 0         0 $file->fdopen(fileno(STDIN),"r");
81             }
82              
83             ### print the files we're finding?
84 2   100     49 my $print = $verbose || $opts->{'t'} || 0;
85              
86 2         18 my $iter = Archive::Tar->iter( $file );
87              
88 2         8 while( my $f = $iter->() ) {
89 2 100       10 print $f->full_path . $/ if $print;
90              
91             ### data dumper output
92 2 50       10 print Dumper( $f ) if $opts->{'D'};
93              
94             ### extract it
95 2 100       24 $f->extract if $opts->{'x'};
96             }
97             }
98              
99             ### pod & usage in one
100             sub usage {
101 0     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, L.
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