lib/Archive/Tar.pm

filter: Statementstmt 79.7% 555 / 696 Branchbran 60.5% 275 / 454 Conditioncond 61.1% 126 / 206 MC/DCmcdc 40.5% 45 / 111 Subroutinesub 89.0% 49 / 55 Podpod 96.6% 29 / 30 totaltotal 69.5% 1079 / 1552 CC 314 SCAR 42.3
CC
314
Cov
71%
_make_special_file
213.5
_extract_file
131.9
_read_tar
120.6
CRAP 69.1
↑ Summary bin/ptargrep »
1 ### the gnu tar specification:
2 ### https://www.gnu.org/software/tar/manual/tar.html
3 ###
4 ### and the pax format spec, which tar derives from:
5 ### https://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
6  
7 package Archive::Tar;
8 17 require 5.005_03;
9  
10 17 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use File::Spec::Unix ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 17 ▶use Archive::Tar::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use Archive::Tar::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 17 require Exporter;
22  
23 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use vars qw[$DEBUG $error $VERSION $WARN $FOLLOW_SYMLINK $CHOWN $CHMOD
Subroutine BEGIN: called CC=1 SCAR=0.0
25 $DO_NOT_USE_PREFIX $HAS_PERLIO $HAS_IO_STRING $SAME_PERMISSIONS
26 $INSECURE_EXTRACT_MODE $ZERO_PAD_NUMBERS @ISA @EXPORT $RESOLVE_SYMLINK
27 $EXTRACT_BLOCK_SIZE $EXTRACT_HARDLINK $MAX_FILE_SIZE
28 17 ];
29  
30 17 @ISA = qw[Exporter];
31 17 @EXPORT = qw[ COMPRESS_GZIP COMPRESS_BZIP COMPRESS_XZ ];
32 17 $DEBUG = 0;
33 17 $WARN = 1;
34 17 $FOLLOW_SYMLINK = 0;
35 17 $VERSION = "3.14";
36 17 $CHOWN = 1;
37 17 $CHMOD = 1;
38 17 ▶$SAME_PERMISSIONS = $> == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$> == 0 ? : 0 17 17
39 17 $DO_NOT_USE_PREFIX = 0;
40 17 $INSECURE_EXTRACT_MODE = 0;
41 17 $ZERO_PAD_NUMBERS = 0;
42 17 ▶$RESOLVE_SYMLINK = $ENV{'PERL5_AT_RESOLVE_SYMLINK'} || 'speed';
Condition50%1/2
$ENV{'PERL5_AT_RESOLVE_SYMLINK'} || 'speed'
l!l
017
MC/DC0%0/1
$ENV{'PERL5_AT_RESOLVE_SYMLINK'} || 'speed'
$ENV{'PERL5_AT_RESOLVE_SYMLINK'}
Truth table50%1/2
A
A= $ENV{'PERL5_AT_RESOLVE_SYMLINK'}
Aresult
11
00
43 17 $EXTRACT_BLOCK_SIZE = 1024 * 1024 * 1024;
44 17 $EXTRACT_HARDLINK = 0;
45 17 $MAX_FILE_SIZE = 1024 * 1024 * 1024;
46  
47 17 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.5
48 17 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 17 $HAS_PERLIO = $Config::Config{useperlio};
50  
51 ### try and load IO::String anyway, so you can dynamically
52 ### switch between perlio and IO::String
53 17 ▶ $HAS_IO_STRING = eval {
Condition50%1/2
eval { do { require IO::String; 'IO::String'->import; 1 } } || 0
l!l
017
MC/DC0%0/1
eval { do { require IO::String; 'IO::String'->import; 1 } } || 0
eval { do { require IO::String; 'IO::String'->import; 1 } }
Truth table50%1/2
A
A= eval { do { require IO::String; 'IO::String'->import; 1 } }
Aresult
11
00
54 17 require IO::String;
55 0 IO::String->import;
56 0 1;
57 } || 0;
58 }
59  
60 =head1 NAME
61  
62 Archive::Tar - module for manipulations of tar archives
63  
64 =head1 SYNOPSIS
65  
66 use Archive::Tar;
67 my $tar = Archive::Tar->new;
68  
69 $tar->read('origin.tgz');
70 $tar->extract();
71  
72 $tar->add_files('file/foo.pl', 'docs/README');
73 $tar->add_data('file/baz.txt', 'This is the contents now');
74  
75 $tar->rename('oldname', 'new/file/name');
76 $tar->chown('/', 'root');
77 $tar->chown('/', 'root:root');
78 $tar->chmod('/tmp', '1777');
79  
80 $tar->write('files.tar'); # plain tar
81 $tar->write('files.tgz', COMPRESS_GZIP); # gzip compressed
82 $tar->write('files.tbz', COMPRESS_BZIP); # bzip2 compressed
83 $tar->write('files.txz', COMPRESS_XZ); # xz compressed
84  
85 =head1 DESCRIPTION
86  
87 Archive::Tar provides an object oriented mechanism for handling tar
88 files. It provides class methods for quick and easy files handling
89 while also allowing for the creation of tar file objects for custom
90 manipulation. If you have the IO::Zlib module installed,
91 Archive::Tar will also support compressed or gzipped tar files.
92  
93 An object of class Archive::Tar represents a .tar(.gz) archive full
94 of files and things.
95  
96 =head1 Object Methods
97  
98 =head2 Archive::Tar->new( [$file, $compressed] )
99  
100 Returns a new Tar object. If given any arguments, C<new()> calls the
101 C<read()> method automatically, passing on the arguments provided to
102 the C<read()> method.
103  
104 If C<new()> is invoked with arguments and the C<read()> method fails
105 for any reason, C<new()> returns undef.
106  
107 =cut
108  
109 17 my $tmpl = {
110 _data => [ ],
111 _file => 'Unknown',
112 };
113  
114 ### install get/set accessors for this object.
115 17 for my $key ( keys %$tmpl ) {
116 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
117 34 ▶ *{__PACKAGE__."::$key"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
118 773 my $self = shift;
119 773 ▶ $self->{$key} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 119 654 773
120 773 return $self->{$key};
121 }
122 34 }
123  
124 94 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
125 94 my $class = shift;
126 94 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 94 94
127  
128 ### copying $tmpl here since a shallow copy makes it use the
129 ### same aref, causing for files to remain in memory always.
130 94 my $obj = bless { _data => [ ], _file => 'Unknown', _error => '' }, $class;
131  
132 94 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 24 70 94
133 24 ▶ unless ( $obj->read( @_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->read(@_)) 0 24 24
134 0 $obj->_error(qq[No data could be read from file]);
135 0 return;
136 }
137 }
138  
139 94 return $obj;
140 }
141  
142 =head2 $tar->read ( $filename|$handle, [$compressed, {opt => 'val'}] )
143  
144 Read the given tar file into memory.
145 The first argument can either be the name of a file or a reference to
146 an already open filehandle (or an IO::Zlib object if it's compressed)
147  
148 The C<read> will I<replace> any previous content in C<$tar>!
149  
150 The second argument may be considered optional, but remains for
151 backwards compatibility. Archive::Tar now looks at the file
152 magic to determine what class should be used to open the file
153 and will transparently Do The Right Thing.
154  
155 Archive::Tar will warn if you try to pass a bzip2 / xz compressed file and the
156 IO::Uncompress::Bunzip2 / IO::Uncompress::UnXz are not available and simply return.
157  
158 Note that you can currently B<not> pass a C<gzip> compressed
159 filehandle, which is not opened with C<IO::Zlib>, a C<bzip2> compressed
160 filehandle, which is not opened with C<IO::Uncompress::Bunzip2>, a C<xz> compressed
161 filehandle, which is not opened with C<IO::Uncompress::UnXz>, nor a string
162 containing the full archive information (either compressed or
163 uncompressed). These are worth while features, but not currently
164 implemented. See the C<TODO> section.
165  
166 The third argument can be a hash reference with options. Note that
167 all options are case-sensitive.
168  
169 =over 4
170  
171 =item limit
172  
173 Do not read more than C<limit> files. This is useful if you have
174 very big archives, and are only interested in the first few files.
175  
176 =item filter
177  
178 Can be set to a regular expression. Only files with names that match
179 the expression will be read.
180  
181 =item md5
182  
183 Set to 1 and the md5sum of files will be returned (instead of file data)
184 my $iter = Archive::Tar->iter( $file, 1, {md5 => 1} );
185 while( my $f = $iter->() ) {
186 print $f->data . "\t" . $f->full_path . $/;
187 }
188  
189 =item extract
190  
191 If set to true, immediately extract entries when reading them. This
192 gives you the same memory break as the C<extract_archive> function.
193 Note however that entries will not be read into memory, but written
194 straight to disk. This means no C<Archive::Tar::File> objects are
195 created for you to inspect.
196  
197 =back
198  
199 All files are stored internally as C<Archive::Tar::File> objects.
200 Please consult the L<Archive::Tar::File> documentation for details.
201  
202 Returns the number of files read in scalar context, and a list of
203 C<Archive::Tar::File> objects in list context.
204  
205 =cut
206  
207 60 ▶sub read {
Subroutine read: called CC=7 SCAR=19.5
Pod documented
208 60 my $self = shift;
209 60 my $file = shift;
210 60 ▶ my $gzip = shift || 0;
Condition100%2/2
shift() || 0
l!l
654
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
211 60 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
1050
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
212  
213 60 ▶ unless( defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 58 2 60
214 2 $self->_error( qq[No file to read from!] );
215 2 return;
216 } else {
217 58 $self->_file( $file );
218 }
219  
220 58 ▶ my $handle = $self->_get_handle($file, $gzip, READ_ONLY->( ZLIB ) )
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $self->_get_handle($file, $gzip, sub { package Archive::Tar::Constant; use warnings; use strict; shift() ? 'rb' : 'r';}->(1)) or return 58 0 58
221 or return;
222  
223 58 ▶ my $data = $self->_read_tar( $handle, $opts ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->_read_tar($handle, $opts) or return 56 2 58
224  
225 56 $self->_data( $data );
226  
227 56 ▶ return wantarray ? @$data : scalar @$data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 51 56
228 }
229  
230 108 ▶sub _get_handle {
Subroutine _get_handle: called CC=20 SCAR=36.5
231 108 my $self = shift;
232 108 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 108 108
233 108 ▶ my $compress = shift || 0;
Condition100%2/2
shift() || 0
l!l
2484
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
234 108 ▶ my $mode = shift || READ_ONLY->( ZLIB ); # default to read only
Condition33%1/3
shift() || sub { package Archive::Tar::Constant; shift() ? 'rb' : 'r'; } ->(1)
l!l&&r!l&&!r
10800
MC/DC0%0/2
shift() || sub { package Archive::Tar::Constant; shift() ? 'rb' : 'r'; } ->(1)
shift() sub { package Archive::Tar::Constant; shift() ? 'rb' : 'r'; } ->(1)
Truth table33%1/3
A || B
A= shift()B= sub { package Archive::Tar::Constant; shift() ? 'rb' : 'r'; } ->(1)
ABresult
1X1
011
000
235  
236 ### Check if file is a file handle or IO glob
237 108 ▶ if ( ref $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) 0 108 108
238 0 ▶ return $file if eval{ *$file{IO} };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { *$file{"IO"} } } 0 0 0
239 0 ▶ return $file if eval{ $file->isa(q{IO::Handle}) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $file->isa("IO::Handle") } } 0 0 0
240 0 $file = q{}.$file;
241 }
242  
243 ### get a FH opened to the right class, so we can use it transparently
244 ### throughout the program
245 108 my $fh;
246 { ### reading magic only makes sense if we're opening a file for
247 ### reading. otherwise, just use what the user requested.
248 108 my $magic = '';
249 108 ▶ if( MODE_READ->($mode) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode)) 73 35 108
250 73 ▶ open my $tmp, '<', $file or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $tmp, "<", $file or do { $self->_error("Could not open '${file}' for reading: $!"); return } 73 0 73
251 0 $self->_error( qq[Could not open '$file' for reading: $!] );
252 0 return;
253 };
254  
255 ### read the first 6 bytes of the file to figure out which class to
256 ### use to open the file.
257 73 sysread( $tmp, $magic, 6 );
258 73 close $tmp;
259 }
260  
261 ### is it xz?
262 ### if you asked specifically for xz compression, or if we're in
263 ### read mode and the magic numbers add up, use xz
264 108 ▶ if( XZ and (
Branch: 2 branches
BranchTrueFalseTotal
if (do { $compress eq 'bzip2' or sub { package Archive::Tar::Constant; $_[0] =~ /^r/ ? 1 : 0; } ->($mode) and $magic =~ /^\x42\x5A\x68[\x30-\x39]/ }) { } 20 88 108
elsif ($compress or sub { package Archive::Tar::Constant; $_[0] =~ /^r/ ? 1 : 0; } ->($mode) or $magic =~ /^(?:\037\213|\037\235)/) { } 70 18 88
265 ($compress eq COMPRESS_XZ) or
266 ( MODE_READ->($mode) and $magic =~ XZ_MAGIC_NUM )
267 )
268 ) {
269 if( MODE_READ->($mode) ) {
270 $fh = IO::Uncompress::UnXz->new( $file ) or do {
271 $self->_error( qq[Could not read '$file': ] .
272 $IO::Uncompress::UnXz::UnXzError
273 );
274 return;
275 };
276 } else {
277 $fh = IO::Compress::Xz->new( $file ) or do {
278 $self->_error( qq[Could not write to '$file': ] .
279 $IO::Compress::Xz::XzError
280 );
281 return;
282 };
283 }
284  
285 ### is it bzip?
286 ### if you asked specifically for bzip compression, or if we're in
287 ### read mode and the magic numbers add up, use bzip
288 ▶ } elsif( BZIP and (
Condition100%6/6
$compress eq "bzip2" or sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode) and $magic =~ /^\x42\x5A\x68[\x30-\x39]/
l!l&&r!l&&!r
11988
sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode) and $magic =~ /^\x42\x5A\x68[\x30-\x39]/
!ll&&!rl&&r
27619
MC/DC100%3/3
$compress eq "bzip2" or sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode) and $magic =~ /^\x42\x5A\x68[\x30-\x39]/
$compress eq "bzip2" sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode) $magic =~ /^\x42\x5A\x68[\x30-\x39]/
Truth table100%4/4
A or B and C
A= $compress eq "bzip2"B= sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode)C= $magic =~ /^\x42\x5A\x68[\x30-\x39]/
ABCresult
1XX1
0111
00X0
0100
289 ($compress eq COMPRESS_BZIP) or
290 ( MODE_READ->($mode) and $magic =~ BZIP_MAGIC_NUM )
291 )
292 ) {
293  
294 ### different reader/writer modules, different error vars... sigh
295 20 ▶ if( MODE_READ->($mode) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sub { package Archive::Tar::Constant; $_[0] =~ /^r/ ? 1 : 0; } ->($mode)) { } 12 8 20
296 12 ▶ $fh = IO::Uncompress::Bunzip2->new( $file, MultiStream => 1 ) or do {
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Uncompress::Bunzip2"->new($file, "MultiStream", 1) or do { $self->_error("Could not read '${file}': " . $IO::Uncompress::Bunzip2::Bunzip2Error); return } 12 0 12
297 0 $self->_error( qq[Could not read '$file': ] .
298 $IO::Uncompress::Bunzip2::Bunzip2Error
299 );
300 0 return;
301 };
302  
303 } else {
304 8 ▶ $fh = IO::Compress::Bzip2->new( $file ) or do {
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Compress::Bzip2"->new($file) or do { $self->_error("Could not write to '${file}': " . $IO::Compress::Bzip2::Bzip2Error); return } 8 0 8
305 0 $self->_error( qq[Could not write to '$file': ] .
306 $IO::Compress::Bzip2::Bzip2Error
307 );
308 0 return;
309 };
310 }
311  
312 ### is it gzip?
313 ### if you asked for compression, if you wanted to read or the gzip
314 ### magic number is present (redundant with read)
315 ▶ } elsif( ZLIB and (
Condition83%5/6
$compress or sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode) or $magic =~ /^(?:\037\213|\037\235)/
l!l&&r!l&&!r
70018
$compress or sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode)
l!l&&r!l&&!r
135718
MC/DC66%2/3
$compress or sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode) or $magic =~ /^(?:\037\213|\037\235)/
$compress sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode) $magic =~ /^(?:\037\213|\037\235)/
Truth table75%3/4
A or B or C
A= $compressB= sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] =~ /^r/ ? 1 : 0;}->($mode)C= $magic =~ /^(?:\037\213|\037\235)/
ABCresult
1XX1
01X1
0011
0000
316 $compress or MODE_READ->($mode) or $magic =~ GZIP_MAGIC_NUM
317 )
318 ) {
319 70 $fh = IO::Zlib->new;
320  
321 70 ▶ unless( $fh->open( $file, $mode ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($file, $mode)) 0 70 70
322 0 $self->_error(qq[Could not create filehandle for '$file': $!]);
323 0 return;
324 }
325  
326 ### is it plain tar?
327 } else {
328 18 $fh = IO::File->new;
329  
330 18 ▶ unless( $fh->open( $file, $mode ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($file, $mode)) 0 18 18
331 0 $self->_error(qq[Could not create filehandle for '$file': $!]);
332 0 return;
333 }
334  
335 ### enable bin mode on tar archives
336 18 binmode $fh;
337 }
338 }
339  
340 108 return $fh;
341 }
342  
343  
344 91 ▶sub _read_tar {
Subroutine _read_tar: called CC=66 SCAR=47.9
345 91 my $self = shift;
346 91 ▶ my $handle = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $handle = shift() or return 91 0 91
347 91 ▶ my $opts = shift || {};
Condition50%1/2
shift() || {}
l!l
910
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
348  
349 91 ▶ my $count = $opts->{limit} || 0;
Condition100%2/2
$opts->{'limit'} || 0
l!l
3457
MC/DC100%1/1
$opts->{'limit'} || 0
$opts->{'limit'}
Truth table100%2/2
A
A= $opts->{'limit'}
Aresult
11
00
350 91 my $filter = $opts->{filter};
351 91 ▶ my $md5 = $opts->{md5} || 0; # cdrake
Condition50%1/2
$opts->{'md5'} || 0
l!l
091
MC/DC0%0/1
$opts->{'md5'} || 0
$opts->{'md5'}
Truth table50%1/2
A
A= $opts->{'md5'}
Aresult
11
00
352 91 my $filter_cb = $opts->{filter_cb};
353 91 ▶ my $extract = $opts->{extract} || 0;
Condition100%2/2
$opts->{'extract'} || 0
l!l
982
MC/DC100%1/1
$opts->{'extract'} || 0
$opts->{'extract'}
Truth table100%2/2
A
A= $opts->{'extract'}
Aresult
11
00
354  
355 ### set a cap on the amount of files to extract ###
356 91 my $limit = 0;
357 91 ▶ $limit = 1 if $count > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count > 0 34 57 91
358  
359 91 my $tarfile = [ ];
360 91 my $chunk;
361 91 my $read = 0;
362 91 my $real_name; # to set the name of a file when
363 # we're encountering @longlink
364 my $data;
365  
366 LOOP:
367 91 ▶ while( $handle->read( $chunk, HEAD ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->read($chunk, 512)) 736 71 807
368 ### IO::Zlib doesn't support this yet
369 736 my $offset;
370 736 ▶ if ( ref($handle) ne 'IO::Zlib' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handle ne 'IO::Zlib') { } 105 631 736
371 105 local $@;
372 105 ▶ $offset = eval { tell $handle } || 'unknown';
Condition50%1/2
eval { do { tell $handle } } || 'unknown'
l!l
1050
MC/DC0%0/1
eval { do { tell $handle } } || 'unknown'
eval { do { tell $handle } }
Truth table50%1/2
A
A= eval { do { tell $handle } }
Aresult
11
00
373 105 $@ = '';
374 }
375 else {
376 631 $offset = 'unknown';
377 }
378  
379 736 ▶ unless( $read++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($read++) 87 649 736
380 87 my $gzip = GZIP_MAGIC_NUM;
381 87 ▶ if( $chunk =~ /$gzip/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ /$gzip/) 0 87 87
382 0 $self->_error( qq[Cannot read compressed format in tar-mode] );
383 0 return;
384 }
385  
386 ### size is < HEAD, which means a corrupted file, as the minimum
387 ### length is _at least_ HEAD
388 87 ▶ if (length $chunk != HEAD) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $chunk != 512) 2 85 87
389 2 $self->_error( qq[Cannot read enough bytes from the tarfile] );
390 2 return;
391 }
392 }
393  
394 ### if we can't read in all bytes... ###
395 734 ▶ last if length $chunk != HEAD;
Branch: 1 branch
BranchTrueFalseTotal
if length $chunk != 512 3 731 734
396  
397 ### Apparently this should really be two blocks of 512 zeroes,
398 ### but GNU tar sometimes gets it wrong. See comment in the
399 ### source code (tar.c) to GNU cpio.
400 731 ▶ next if $chunk eq TAR_END;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" 434 297 731
401  
402 ### according to the posix spec, the last 12 bytes of the header are
403 ### null bytes, to pad it to a 512 byte block. That means if these
404 ### bytes are NOT null bytes, it's a corrupt header. See:
405 ### www.koders.com/c/fidCE473AD3D9F835D690259D60AD5654591D91D5BA.aspx
406 ### line 111
407 297 { my $nulls = join '', "\0" x 12;
408 297 ▶ unless( $nulls eq substr( $chunk, 500, 12 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nulls eq substr($chunk, 500, 12)) 0 297 297
409 0 $self->_error( qq[Invalid header block at offset $offset] );
410 0 next LOOP;
411 }
412 }
413  
414 ### pass the realname, so we can set it 'proper' right away
415 ### some of the heuristics are done on the name, so important
416 ### to set it ASAP
417 297 my $entry;
418 297 { my %extra_args = ();
419 297 ▶ $extra_args{'name'} = $$real_name if defined $real_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $real_name 63 234 297
420  
421 297 ▶ unless( $entry = Archive::Tar::File->new( chunk => $chunk,
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry = "Archive::Tar::File"->new("chunk", $chunk, %extra_args)) 0 297 297
422 %extra_args )
423 ) {
424 0 $self->_error( qq[Couldn't read chunk at offset $offset] );
425 0 next LOOP;
426 }
427 }
428  
429 ### ignore labels:
430 ### https://www.gnu.org/software/tar/manual/html_chapter/Media.html#SEC159
431 297 ▶ next if $entry->is_label;
Branch: 1 branch
BranchTrueFalseTotal
if $entry->is_label 0 297 297
432  
433 297 ▶ if( length $entry->type and ($entry->is_file || $entry->is_longlink) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $entry->type and $entry->is_file || $entry->is_longlink) 219 78 297
Condition66%4/6
length $entry->type and $entry->is_file || $entry->is_longlink
!ll&&!rl&&r
078219
$entry->is_file or $entry->is_longlink
l!l&&r!l&&!r
219078
MC/DC33%1/3
length $entry->type and $entry->is_file || $entry->is_longlink
length $entry->type $entry->is_file $entry->is_longlink
Truth table50%2/4
A and B or C
A= length $entry->typeB= $entry->is_fileC= $entry->is_longlink
ABCresult
0XX0
1000
11X1
1011
434  
435 219 ▶ if ( $entry->is_file && !$entry->validate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->is_file and not $entry->validate) 0 219 219
Condition33%1/3
$entry->is_file and not $entry->validate
!ll&&!rl&&r
02190
MC/DC0%0/2
$entry->is_file and not $entry->validate
$entry->is_file not $entry->validate
Truth table33%1/3
A and not(B)
A= $entry->is_fileB= not $entry->validate
ABresult
0X0
100
111
436 ### sometimes the chunk is rather fux0r3d and a whole 512
437 ### bytes ends up in the ->name area.
438 ### clean it up, if need be
439 0 my $name = $entry->name;
440 0 ▶ $name = substr($name, 0, 100) if length $name > 100;
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 100 0 0 0
441 0 $name =~ s/\n/ /g;
442  
443 0 $self->_error( $name . qq[: checksum error] );
444 0 next LOOP;
445 }
446  
447 219 my $block = BLOCK_SIZE->( $entry->size );
448  
449 219 ▶ if ( $MAX_FILE_SIZE && $entry->size > $MAX_FILE_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Archive::Tar::MAX_FILE_SIZE and $entry->size > $Archive::Tar::MAX_FILE_SIZE) 0 219 219
Condition33%1/3
$Archive::Tar::MAX_FILE_SIZE and $entry->size > $Archive::Tar::MAX_FILE_SIZE
!ll&&!rl&&r
02190
MC/DC0%0/2
$Archive::Tar::MAX_FILE_SIZE and $entry->size > $Archive::Tar::MAX_FILE_SIZE
$Archive::Tar::MAX_FILE_SIZE $entry->size > $Archive::Tar::MAX_FILE_SIZE
Truth table33%1/3
A and B
A= $Archive::Tar::MAX_FILE_SIZEB= $entry->size > $Archive::Tar::MAX_FILE_SIZE
ABresult
0X0
100
111
450 0 $self->_error( qq[Entry '] . $entry->full_path .
451 qq[' declared size ] . $entry->size .
452 qq[ bytes exceeds \$Archive::Tar::MAX_FILE_SIZE ] .
453 qq[($MAX_FILE_SIZE); refusing to allocate] );
454 0 next LOOP;
455 }
456  
457 219 $data = $entry->get_content_by_ref;
458  
459 219 my $skip = 0;
460 219 my $ctx; # cdrake
461 ### skip this entry if we're filtering
462  
463 219 ▶ if($md5) { # cdrake
Branch: 4 branches
BranchTrueFalseTotal
if ($md5) { } 0 219 219
elsif ($filter and not $entry->name =~ /$filter/) { } 22 197 219
elsif ($filter_cb and not &$filter_cb($entry)) { } 22 175 197
elsif ($entry->name eq 'pax_global_header' or $entry->type =~ /^(x|g)$/) { } 1 174 175
464 0 $ctx = Digest::MD5->new; # cdrake
465 0 $skip=5; # cdrake
466  
467 ▶ } elsif ($filter && $entry->name !~ $filter) {
Condition100%3/3
$filter and not $entry->name =~ /$filter/
!ll&&!rl&&r
194322
MC/DC100%2/2
$filter and not $entry->name =~ /$filter/
$filter not $entry->name =~ /$filter/
Truth table100%3/3
A and not(B)
A= $filterB= not $entry->name =~ /$filter/
ABresult
0X0
100
111
468 22 $skip = 1;
469  
470 ▶ } elsif ($filter_cb && ! $filter_cb->($entry)) {
Condition100%3/3
$filter_cb and not &$filter_cb($entry)
!ll&&!rl&&r
172322
MC/DC100%2/2
$filter_cb and not &$filter_cb($entry)
$filter_cb not &$filter_cb($entry)
Truth table100%3/3
A and not(B)
A= $filter_cbB= not &$filter_cb($entry)
ABresult
0X0
100
111
471 22 $skip = 2;
472  
473 ### skip this entry if it's a pax header. This is a special file added
474 ### by, among others, git-generated tarballs. It holds comments and is
475 ### not meant for extracting. See #38932: pax_global_header extracted
476 ▶ } elsif ( $entry->name eq PAX_HEADER or $entry->type =~ /^(x|g)$/ ) {
Condition66%2/3
$entry->name eq "pax_global_header" or $entry->type =~ /^(x|g)$/
l!l&&r!l&&!r
10174
MC/DC50%1/2
$entry->name eq "pax_global_header" or $entry->type =~ /^(x|g)$/
$entry->name eq "pax_global_header" $entry->type =~ /^(x|g)$/
Truth table66%2/3
A or B
A= $entry->name eq "pax_global_header"B= $entry->type =~ /^(x|g)$/
ABresult
1X1
011
000
477 1 $skip = 3;
478 }
479  
480 219 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) { } 45 174 219
481 #
482 # Since we're skipping, do not allocate memory for the
483 # whole file. Read it 64 BLOCKS at a time. Do not
484 # complete the skip yet because maybe what we read is a
485 # longlink and it won't get skipped after all
486 #
487 45 my $amt = $block;
488 45 my $fsz=$entry->size; # cdrake
489 45 ▶ while ($amt > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amt > 0) 45 45 90
490 45 $$data = '';
491 45 my $this = 64 * BLOCK;
492 45 ▶ $this = $amt if $this > $amt;
Branch: 1 branch
BranchTrueFalseTotal
if $this > $amt 45 0 45
493 45 ▶ if( $handle->read( $$data, $this ) < $this ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->read($$data, $this) < $this) 0 45 45
494 0 $self->_error( qq[Read error on tarfile (missing data) '].
495 $entry->full_path ."' at offset $offset" );
496 0 next LOOP;
497 }
498 45 $amt -= $this;
499 45 $fsz -= $this; # cdrake
500 45 ▶ substr ($$data, $fsz) = "" if ($fsz<0); # remove external junk prior to md5 # cdrake
Branch: 1 branch
BranchTrueFalseTotal
if $fsz < 0 45 0 45
501 45 ▶ $ctx->add($$data) if($skip==5); # cdrake
Branch: 1 branch
BranchTrueFalseTotal
if $skip == 5 0 45 45
502 }
503 45 ▶ $$data = $ctx->hexdigest if($skip==5 && !$entry->is_longlink && !$entry->is_unknown && !$entry->is_label ) ; # cdrake
Branch: 1 branch
BranchTrueFalseTotal
if $skip == 5 and not $entry->is_longlink and not $entry->is_unknown and not $entry->is_label 0 45 45
Condition33%3/9
$skip == 5 and not $entry->is_longlink and not $entry->is_unknown and not $entry->is_label
!ll&&!rl&&r
4500
$skip == 5 and not $entry->is_longlink and not $entry->is_unknown
!ll&&!rl&&r
4500
$skip == 5 and not $entry->is_longlink
!ll&&!rl&&r
4500
MC/DC0%0/4
$skip == 5 and not $entry->is_longlink and not $entry->is_unknown and not $entry->is_label
$skip == 5 not $entry->is_longlink not $entry->is_unknown not $entry->is_label
Truth table20%1/5
A and not(B) and not(C) and not(D)
A= $skip == 5B= not $entry->is_longlinkC= not $entry->is_unknownD= not $entry->is_label
ABCDresult
0XXX0
10XX0
110X0
11100
11111
504 } else {
505  
506 ### just read everything into memory
507 ### can't do lazy loading since IO::Zlib doesn't support 'seek'
508 ### this is because Compress::Zlib doesn't support it =/
509 ### this reads in the whole data in one read() call.
510 174 ▶ if ( $handle->read( $$data, $block ) < $block ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->read($$data, $block) < $block) 0 174 174
511 0 $self->_error( qq[Read error on tarfile (missing data) '].
512 $entry->full_path ."' at offset $offset" );
513 0 next LOOP;
514 }
515 ### throw away trailing garbage ###
516 174 ▶ substr ($$data, $entry->size) = "" if defined $$data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$data 174 0 174
517 }
518  
519 ### part II of the @LongLink munging -- need to do /after/
520 ### the checksum check.
521 219 ▶ if( $entry->is_longlink ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->is_longlink) 63 156 219
522 ### weird thing in tarfiles -- if the file is actually a
523 ### @LongLink, the data part seems to have a trailing ^@
524 ### (unprintable) char. to display, pipe output through less.
525 ### but that doesn't *always* happen.. so check if the last
526 ### character is a control character, and if so remove it
527 ### at any rate, we better remove that character here, or tests
528 ### like 'eq' and hash lookups based on names will SO not work
529 ### remove it by calculating the proper size, and then
530 ### tossing out everything that's longer than that size.
531  
532 ### count number of nulls
533 63 my $nulls = $$data =~ tr/\0/\0/;
534  
535 ### cut data + size by that many bytes
536 63 $entry->size( $entry->size - $nulls );
537 63 substr ($$data, $entry->size) = "";
538 }
539 }
540  
541 ### clean up of the entries.. posix tar /apparently/ has some
542 ### weird 'feature' that allows for filenames > 255 characters
543 ### they'll put a header in with as name '././@LongLink' and the
544 ### contents will be the name of the /next/ file in the archive
545 ### pretty crappy and kludgy if you ask me
546  
547 ### set the name for the next entry if this is a @LongLink;
548 ### this is one ugly hack =/ but needed for direct extraction
549 297 ▶ if( $entry->is_longlink ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($entry->is_longlink) { } 63 234 297
elsif (defined $real_name) { } 63 171 234
550 63 $real_name = $data;
551 63 next LOOP;
552 } elsif ( defined $real_name ) {
553 63 $entry->name( $$real_name );
554 63 $entry->prefix('');
555 63 undef $real_name;
556 }
557  
558 234 ▶ if ($filter && $entry->name !~ $filter) {
Branch: 3 branches
BranchTrueFalseTotal
if ($filter and not $entry->name =~ /$filter/) { } 20 214 234
elsif ($filter_cb and not &$filter_cb($entry)) { } 20 194 214
elsif ($entry->name eq 'pax_global_header' or $entry->type =~ /^(x|g)$/) { } 1 193 194
Condition100%3/3
$filter and not $entry->name =~ /$filter/
!ll&&!rl&&r
209520
MC/DC100%2/2
$filter and not $entry->name =~ /$filter/
$filter not $entry->name =~ /$filter/
Truth table100%3/3
A and not(B)
A= $filterB= not $entry->name =~ /$filter/
ABresult
0X0
100
111
559 20 next LOOP;
560  
561 ▶ } elsif ($filter_cb && ! $filter_cb->($entry)) {
Condition100%3/3
$filter_cb and not &$filter_cb($entry)
!ll&&!rl&&r
189520
MC/DC100%2/2
$filter_cb and not &$filter_cb($entry)
$filter_cb not &$filter_cb($entry)
Truth table100%3/3
A and not(B)
A= $filter_cbB= not &$filter_cb($entry)
ABresult
0X0
100
111
562 20 next LOOP;
563  
564 ### skip this entry if it's a pax header. This is a special file added
565 ### by, among others, git-generated tarballs. It holds comments and is
566 ### not meant for extracting. See #38932: pax_global_header extracted
567 ▶ } elsif ( $entry->name eq PAX_HEADER or $entry->type =~ /^(x|g)$/ ) {
Condition66%2/3
$entry->name eq "pax_global_header" or $entry->type =~ /^(x|g)$/
l!l&&r!l&&!r
10193
MC/DC50%1/2
$entry->name eq "pax_global_header" or $entry->type =~ /^(x|g)$/
$entry->name eq "pax_global_header" $entry->type =~ /^(x|g)$/
Truth table66%2/3
A or B
A= $entry->name eq "pax_global_header"B= $entry->type =~ /^(x|g)$/
ABresult
1X1
011
000
568 1 next LOOP;
569 }
570  
571 193 ▶ if ( $extract && !$entry->is_longlink
Branch: 1 branch
BranchTrueFalseTotal
if ($extract and not $entry->is_longlink and not $entry->is_unknown and not $entry->is_label) 9 184 193
Condition66%6/9
$extract and not $entry->is_longlink and not $entry->is_unknown and not $entry->is_label
!ll&&!rl&&r
18409
$extract and not $entry->is_longlink and not $entry->is_unknown
!ll&&!rl&&r
18409
$extract and not $entry->is_longlink
!ll&&!rl&&r
18409
MC/DC25%1/4
$extract and not $entry->is_longlink and not $entry->is_unknown and not $entry->is_label
$extract not $entry->is_longlink not $entry->is_unknown not $entry->is_label
Truth table40%2/5
A and not(B) and not(C) and not(D)
A= $extractB= not $entry->is_longlinkC= not $entry->is_unknownD= not $entry->is_label
ABCDresult
0XXX0
10XX0
110X0
11100
11111
572 && !$entry->is_unknown
573 && !$entry->is_label ) {
574 9 ▶ $self->_extract_file( $entry ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_extract_file($entry) or return 9 0 9
575 }
576  
577 ### Guard against tarfiles with garbage at the end
578 193 ▶ last LOOP if $entry->name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $entry->name eq "" 0 193 193
579  
580 ### push only the name on the rv if we're extracting
581 ### -- for extract_archive
582 193 ▶ push @$tarfile, ($extract ? $entry->name : $entry);
Branch: 1 branch
BranchTrueFalseTotal
$extract ? : 9 184 193
583  
584 193 ▶ if( $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) 21 172 193
585 21 ▶ $count-- unless $entry->is_longlink || $entry->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->is_longlink or $entry->is_dir 15 6 21
Condition66%2/3
$entry->is_longlink or $entry->is_dir
l!l&&r!l&&!r
0615
MC/DC50%1/2
$entry->is_longlink or $entry->is_dir
$entry->is_longlink $entry->is_dir
Truth table66%2/3
A or B
A= $entry->is_longlinkB= $entry->is_dir
ABresult
1X1
011
000
586 21 ▶ last LOOP unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 15 6 21
587 }
588 } continue {
589 716 undef $data;
590 }
591  
592 89 return $tarfile;
593 }
594  
595 =head2 $tar->contains_file( $filename )
596  
597 Check if the archive contains a certain file.
598 It will return true if the file is in the archive, false otherwise.
599  
600 Note however, that this function does an exact match using C<eq>
601 on the full path. So it cannot compensate for case-insensitive file-
602 systems or compare 2 paths to see if they would point to the same
603 underlying file.
604  
605 =cut
606  
607 2 ▶sub contains_file {
Subroutine contains_file: called CC=3 SCAR=11.0
Pod documented
608 2 my $self = shift;
609 2 my $full = shift;
610  
611 2 ▶ return unless defined $full;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $full 0 2 2
612  
613 ### don't warn if the entry isn't there.. that's what this function
614 ### is for after all.
615 2 local $WARN = 0;
616 2 ▶ return 1 if $self->_find_entry($full);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_find_entry($full) 1 1 2
617 1 return;
618 }
619  
620 =head2 $tar->extract( [@filenames] )
621  
622 Write files whose names are equivalent to any of the names in
623 C<@filenames> to disk, creating subdirectories as necessary. This
624 might not work too well under VMS.
625 Under MacPerl, the file's modification time will be converted to the
626 MacOS zero of time, and appropriate conversions will be done to the
627 path. However, the length of each element of the path is not
628 inspected to see whether it's longer than MacOS currently allows (32
629 characters).
630  
631 If C<extract> is called without a list of file names, the entire
632 contents of the archive are extracted.
633  
634 Returns a list of filenames extracted.
635  
636 =cut
637  
638 32 ▶sub extract {
Subroutine extract: called CC=10 SCAR=23.6
Pod documented
639 32 my $self = shift;
640 32 my @args = @_;
641 32 my @files;
642 my $hashmap;
643  
644 # use the speed optimization for all extracted files
645 32 ▶ local($self->{cwd}) = cwd() unless $self->{cwd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cwd"} 32 0 32
646  
647 ### you requested the extraction of only certain files
648 32 ▶ if( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 2 30 32
649 2 for my $file ( @args ) {
650  
651 ### it's already an object?
652 2 ▶ if( UNIVERSAL::isa( $file, 'Archive::Tar::File' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($file, 'Archive::Tar::File')) { } 1 1 2
653 1 push @files, $file;
654 1 next;
655  
656 ### go find it then
657 } else {
658  
659 # create hash-map once to speed up lookup
660 $hashmap = $hashmap || {
661 1 ▶ map { $_->full_path, $_ } @{$self->_data}
Condition50%1/2
$hashmap || {map({$_->full_path, $_;} @{$self->_data;})}
l!l
01
MC/DC0%0/1
$hashmap || {map({$_->full_path, $_;} @{$self->_data;})}
$hashmap
Truth table50%1/2
A
A= $hashmap
Aresult
11
00
662 };
663  
664 1 ▶ if (exists $hashmap->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hashmap->{$file}) { } 1 0 1
665 ### we found the file you're looking for
666 1 push @files, $hashmap->{$file};
667 } else {
668 0 return $self->_error(
669 qq[Could not find '$file' in archive] );
670 }
671 }
672 }
673  
674 ### just grab all the file items
675 } else {
676 30 @files = $self->get_files;
677 }
678  
679 ### nothing found? that's an error
680 32 ▶ unless( scalar @files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @files) 0 32 32
681 0 $self->_error( qq[No files found for ] . $self->_file );
682 0 return;
683 }
684  
685 ### now extract them
686 32 for my $entry ( @files ) {
687 111 ▶ unless( $self->_extract_file( $entry ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_extract_file($entry)) 3 108 111
688 3 $self->_error(q[Could not extract ']. $entry->full_path .q['] );
689 3 return;
690 }
691 }
692  
693 29 return @files;
694 }
695  
696 =head2 $tar->extract_file( $file, [$extract_path] )
697  
698 Write an entry, whose name is equivalent to the file name provided to
699 disk. Optionally takes a second parameter, which is the full native
700 path (including filename) the entry will be written to.
701  
702 For example:
703  
704 $tar->extract_file( 'name/in/archive', 'name/i/want/to/give/it' );
705  
706 $tar->extract_file( $at_file_object, 'name/i/want/to/give/it' );
707  
708 Returns true on success, false on failure.
709  
710 =cut
711  
712 167 ▶sub extract_file {
Subroutine extract_file: called CC=3 SCAR=11.2
Pod documented
713 167 my $self = shift;
714 167 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 167 167
715 167 my $alt = shift;
716  
717 167 ▶ my $entry = $self->_find_entry( $file )
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->_find_entry($file) or $self->_error("Could not find an entry for '${file}'"), return 167 0 167
718 or $self->_error( qq[Could not find an entry for '$file'] ), return;
719  
720 167 return $self->_extract_file( $entry, $alt );
721 }
722  
723 290 ▶sub _extract_file {
Subroutine _extract_file: called CC=59 SCAR=48.8
724 290 my $self = shift;
725 290 ▶ my $entry = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = shift() or return 290 0 290
726 290 my $alt = shift;
727  
728 ### you wanted an alternate extraction location ###
729 290 ▶ my $name = defined $alt ? $alt : $entry->full_path;
Branch: 1 branch
BranchTrueFalseTotal
defined $alt ? : 161 129 290
730  
731 ### splitpath takes a bool at the end to indicate
732 ### that it's splitting a dir
733 290 my ($vol,$dirs,$file);
734 290 ▶ if ( defined $alt ) { # It's a local-OS path
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alt) { } 161 129 290
735 161 ($vol,$dirs,$file) = File::Spec->splitpath( $alt,
736 $entry->is_dir );
737 } else {
738 129 ($vol,$dirs,$file) = File::Spec::Unix->splitpath( $name,
739 $entry->is_dir );
740 }
741  
742 290 my $dir;
743 ### is $name an absolute path? ###
744 290 ▶ if( $vol || File::Spec->file_name_is_absolute( $dirs ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vol or 'File::Spec'->file_name_is_absolute($dirs)) { } 80 210 290
Condition66%2/3
$vol or "File::Spec"->file_name_is_absolute($dirs)
l!l&&r!l&&!r
080210
MC/DC50%1/2
$vol or "File::Spec"->file_name_is_absolute($dirs)
$vol "File::Spec"->file_name_is_absolute($dirs)
Truth table66%2/3
A or B
A= $volB= "File::Spec"->file_name_is_absolute($dirs)
ABresult
1X1
011
000
745  
746 ### absolute names are not allowed to be in tarballs under
747 ### strict mode, so only allow it if a user tells us to do it
748 80 ▶ if( not defined $alt and not $INSECURE_EXTRACT_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $alt or $Archive::Tar::INSECURE_EXTRACT_MODE) 0 80 80
Condition33%1/3
defined $alt or $Archive::Tar::INSECURE_EXTRACT_MODE
l!l&&r!l&&!r
8000
MC/DC0%0/2
defined $alt or $Archive::Tar::INSECURE_EXTRACT_MODE
defined $alt $Archive::Tar::INSECURE_EXTRACT_MODE
Truth table33%1/3
A or B
A= defined $altB= $Archive::Tar::INSECURE_EXTRACT_MODE
ABresult
1X1
011
000
749 0 $self->_error(
750 q[Entry ']. $entry->full_path .q[' is an absolute path. ].
751 q[Not extracting absolute paths under SECURE EXTRACT MODE]
752 );
753 0 return;
754 }
755  
756 ### user asked us to, it's fine.
757 80 $dir = File::Spec->catpath( $vol, $dirs, "" );
758  
759 ### it's a relative path ###
760 } else {
761 my $cwd = (ref $self and defined $self->{cwd})
762 ? $self->{cwd}
763 210 ▶ : cwd();
Branch: 1 branch
BranchTrueFalseTotal
ref $self && defined $self->{'cwd'} ? : 111 99 210
Condition100%3/3
ref $self and defined $self->{"cwd"}
!ll&&!rl&&r
396111
MC/DC100%2/2
ref $self and defined $self->{"cwd"}
ref $self defined $self->{"cwd"}
Truth table100%3/3
A and B
A= ref $selfB= defined $self->{"cwd"}
ABresult
0X0
100
111
764  
765 210 ▶ my @dirs = defined $alt
Branch: 1 branch
BranchTrueFalseTotal
defined $alt ? : 81 129 210
766 ? File::Spec->splitdir( $dirs ) # It's a local-OS path
767 : File::Spec::Unix->splitdir( $dirs ); # it's UNIX-style, likely
768 # straight from the tarball
769  
770 210 ▶ if( not defined $alt and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $alt or $Archive::Tar::INSECURE_EXTRACT_MODE) 119 91 210
Condition100%3/3
defined $alt or $Archive::Tar::INSECURE_EXTRACT_MODE
l!l&&r!l&&!r
8110119
MC/DC100%2/2
defined $alt or $Archive::Tar::INSECURE_EXTRACT_MODE
defined $alt $Archive::Tar::INSECURE_EXTRACT_MODE
Truth table100%3/3
A or B
A= defined $altB= $Archive::Tar::INSECURE_EXTRACT_MODE
ABresult
1X1
011
000
771 not $INSECURE_EXTRACT_MODE
772 ) {
773  
774 ### paths that leave the current directory are not allowed under
775 ### strict mode, so only allow it if a user tells us to do this.
776 119 ▶ if( grep { $_ eq '..' } @dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "..";} @dirs) 1 118 119
777  
778 1 $self->_error(
779 q[Entry ']. $entry->full_path .q[' is attempting to leave ].
780 q[the current working directory. Not extracting under ].
781 q[SECURE EXTRACT MODE]
782 );
783 1 return;
784 }
785  
786 ### the archive may be asking us to extract into a symlink. This
787 ### is not sane and a possible security issue, as outlined here:
788 ### https://rt.cpan.org/Ticket/Display.html?id=30380
789 ### https://bugzilla.redhat.com/show_bug.cgi?id=295021
790 ### https://issues.rpath.com/browse/RPL-1716
791 118 my $full_path = $cwd;
792 118 for my $d ( @dirs ) {
793 108 $full_path = File::Spec->catdir( $full_path, $d );
794  
795 ### we've already checked this one, and it's safe. Move on.
796 108 ▶ next if ref $self and $self->{_link_cache}->{$full_path};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and $self->{"_link_cache"}{$full_path} 92 16 108
Condition100%3/3
ref $self and $self->{"_link_cache"}{$full_path}
!ll&&!rl&&r
31392
MC/DC100%2/2
ref $self and $self->{"_link_cache"}{$full_path}
ref $self $self->{"_link_cache"}{$full_path}
Truth table100%3/3
A and B
A= ref $selfB= $self->{"_link_cache"}{$full_path}
ABresult
0X0
100
111
797  
798 16 ▶ if( -l $full_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $full_path) 2 14 16
799 2 my $to = readlink $full_path;
800 2 my $diag = "symlinked directory ($full_path => $to)";
801  
802 2 $self->_error(
803 q[Entry ']. $entry->full_path .q[' is attempting to ].
804 qq[extract to a $diag. This is considered a security ].
805 q[vulnerability and not allowed under SECURE EXTRACT ].
806 q[MODE]
807 );
808 2 return;
809 }
810  
811 ### XXX keep a cache if possible, so the stats become cheaper:
812 14 ▶ $self->{_link_cache}->{$full_path} = 1 if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 11 3 14
813 }
814 }
815  
816 ### '.' is the directory delimiter on VMS, which has to be escaped
817 ### or changed to '_' on vms. vmsify is used, because older versions
818 ### of vmspath do not handle this properly.
819 ### Must not add a '/' to an empty directory though.
820 207 map { length() ? VMS::Filespec::vmsify($_.'/') : $_ } @dirs if ON_VMS;
821  
822 207 my ($cwd_vol,$cwd_dir,$cwd_file)
823 = File::Spec->splitpath( $cwd );
824 207 my @cwd = File::Spec->splitdir( $cwd_dir );
825 207 ▶ push @cwd, $cwd_file if length $cwd_file;
Branch: 1 branch
BranchTrueFalseTotal
if length $cwd_file 207 0 207
826  
827 ### We need to pass '' as the last element to catpath. Craig Berry
828 ### explains why (msgid <p0624083dc311ae541393@[172.16.52.1]>):
829 ### The root problem is that splitpath on UNIX always returns the
830 ### final path element as a file even if it is a directory, and of
831 ### course there is no way it can know the difference without checking
832 ### against the filesystem, which it is documented as not doing. When
833 ### you turn around and call catpath, on VMS you have to know which bits
834 ### are directory bits and which bits are file bits. In this case we
835 ### know the result should be a directory. I had thought you could omit
836 ### the file argument to catpath in such a case, but apparently on UNIX
837 ### you can't.
838 207 $dir = File::Spec->catpath(
839 $cwd_vol, File::Spec->catdir( @cwd, @dirs ), ''
840 );
841  
842 ### catdir() returns undef if the path is longer than 255 chars on
843 ### older VMS systems.
844 207 ▶ unless ( defined $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 207 207
845 0 ▶ $^W && $self->_error( qq[Could not compose a path for '$dirs'\n] );
Branch: 1 branch
BranchTrueFalseTotal
$^W and $self->_error("Could not compose a path for '${dirs}'\n") 0 0 0
846 0 return;
847 }
848  
849 }
850  
851 287 ▶ if( -e $dir && !-d _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir and not -d _) 0 287 287
Condition66%2/3
-e $dir and not -d _
!ll&&!rl&&r
142730
MC/DC0%0/2
-e $dir and not -d _
-e $dir not -d _
Truth table66%2/3
A and not(B)
A= -e $dirB= not -d _
ABresult
0X0
100
111
852 0 ▶ $^W && $self->_error( qq['$dir' exists, but it's not a directory!\n] );
Branch: 1 branch
BranchTrueFalseTotal
$^W and $self->_error("'${dir}' exists, but it's not a directory!\n") 0 0 0
853 0 return;
854 }
855  
856 287 ▶ unless ( -d _ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d _) 14 273 287
857 14 eval { File::Path::mkpath( $dir, 0, 0777 ) };
858 14 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 13 14
859 1 my $fp = $entry->full_path;
860 1 $self->_error(qq[Could not create directory '$dir' for '$fp': $@]);
861 1 return;
862 }
863  
864 ### XXX chown here? that might not be the same as in the archive
865 ### as we're only chown'ing to the owner of the file we're extracting
866 ### not to the owner of the directory itself, which may or may not
867 ### be another entry in the archive
868 ### Answer: no, gnu tar doesn't do it either, it'd be the wrong
869 ### way to go.
870 #if( $CHOWN && CAN_CHOWN ) {
871 # chown $entry->uid, $entry->gid, $dir or
872 # $self->_error( qq[Could not set uid/gid on '$dir'] );
873 #}
874 }
875  
876 ### we're done if we just needed to create a dir ###
877 286 ▶ return 1 if $entry->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $entry->is_dir 18 268 286
878  
879 268 my $full = File::Spec->catfile( $dir, $file );
880  
881 268 ▶ if( $entry->is_unknown ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->is_unknown) 0 268 268
882 0 $self->_error( qq[Unknown file type for file '$full'] );
883 0 return;
884 }
885  
886 ### If a file system already contains a block device with the same name as
887 ### the being extracted regular file, we would write the file's content
888 ### to the block device. So remove the existing file (block device) now.
889 ### If an archive contains multiple same-named entries, the last one
890 ### should replace the previous ones. So remove the old file now.
891 ### If the old entry is a symlink to a file outside of the CWD, the new
892 ### entry would create a file there. This is CVE-2018-12015
893 ### <https://rt.cpan.org/Ticket/Display.html?id=125523>.
894 268 ▶ if (-l $full || -e _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $full or -e _) 9 259 268
Condition66%2/3
-l $full or -e _
l!l&&r!l&&!r
09259
MC/DC50%1/2
-l $full or -e _
-l $full -e _
Truth table66%2/3
A or B
A= -l $fullB= -e _
ABresult
1X1
011
000
895 9 ▶ if (!unlink $full) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $full) 0 9 9
896 0 $self->_error( qq[Could not remove old file '$full': $!] );
897 0 return;
898 }
899 }
900 268 ▶ if( length $entry->type && $entry->is_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $entry->type and $entry->is_file) { } 260 8 268
Condition66%2/3
length $entry->type and $entry->is_file
!ll&&!rl&&r
08260
MC/DC50%1/2
length $entry->type and $entry->is_file
length $entry->type $entry->is_file
Truth table66%2/3
A and B
A= length $entry->typeB= $entry->is_file
ABresult
0X0
100
111
901 260 my $fh = IO::File->new;
902 260 ▶ $fh->open( $full, '>' ) or (
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($full, ">") or $self->_error("Could not open file '${full}': $!"), return 260 0 260
903 $self->_error( qq[Could not open file '$full': $!] ),
904 return
905 );
906  
907 260 ▶ if( $entry->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->size) 234 26 260
908 234 binmode $fh;
909 234 my $offset = 0;
910 234 my $content = $entry->get_content_by_ref();
911 234 ▶ while ($offset < $entry->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $entry->size) 16827 234 17061
912 16827 my $written
913 = syswrite $fh, $$content, $EXTRACT_BLOCK_SIZE, $offset;
914 16827 ▶ if (defined $written) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $written) { } 16827 0 16827
915 16827 $offset += $written;
916 } else {
917 0 $self->_error( qq[Could not write data to '$full': $!] );
918 0 return;
919 }
920 }
921 }
922  
923 260 ▶ close $fh or (
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $self->_error("Could not close file '${full}'"), return 260 0 260
924 $self->_error( qq[Could not close file '$full'] ),
925 return
926 );
927  
928 } else {
929 8 ▶ $self->_make_special_file( $entry, $full ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_make_special_file($entry, $full) or return 7 1 8
930 }
931  
932 ### only update the timestamp if it's not a symlink; that will change the
933 ### timestamp of the original. This addresses bug #33669: Could not update
934 ### timestamp warning on symlinks
935 267 ▶ if( not -l $full and not ( $entry->is_hardlink and ON_UNIX and $EXTRACT_HARDLINK ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-l $full or $entry->is_hardlink and !!1 and $Archive::Tar::EXTRACT_HARDLINK) 260 7 267
Condition50%4/8
-l $full or $entry->is_hardlink and !!1 and $Archive::Tar::EXTRACT_HARDLINK
l!l&&r!l&&!r
70260
$entry->is_hardlink and !!1 and $Archive::Tar::EXTRACT_HARDLINK
!ll&&!rl&&r
26000
$entry->is_hardlink and !!1
!ll
2600
MC/DC33%1/3
-l $full or $entry->is_hardlink and !!1 and $Archive::Tar::EXTRACT_HARDLINK
-l $full $entry->is_hardlink $Archive::Tar::EXTRACT_HARDLINK
Truth table50%2/4
A or B and C
A= -l $fullB= $entry->is_hardlinkC= $Archive::Tar::EXTRACT_HARDLINK
ABCresult
1XX1
0111
00X0
0100
936 260 ▶ utime time, $entry->mtime - TIME_OFFSET, $full or
Branch: 1 branch
BranchTrueFalseTotal
utime time, $entry->mtime - 0, $full or $self->_error("Could not update timestamp") 260 0 260
937 $self->_error( qq[Could not update timestamp] );
938 }
939  
940 267 ▶ if( $CHOWN && CAN_CHOWN->() and not -l $full and not ( $entry->is_hardlink and ON_UNIX and $EXTRACT_HARDLINK ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Archive::Tar::CHOWN and sub { package Archive::Tar::Constant; use warnings; use strict; $> == 0 and $^O ne 'MacOS' and $^O ne 'MSWin32';}->() and not -l $full and not $entry->is_hardlink && !!1 && $Archive::Tar::EXTRACT_HARDLINK) 0 267 267
Condition21%3/14
$Archive::Tar::CHOWN and sub { package Archive::Tar::Constant; use warnings; use strict; $> == 0 and $^O ne 'MacOS' and $^O ne 'MSWin32';}->() and not -l $full and not $entry->is_hardlink && !!1 && $Archive::Tar::EXTRACT_HARDLINK
!ll&&!rl&&r
26700
$Archive::Tar::CHOWN and sub { package Archive::Tar::Constant; use warnings; use strict; $> == 0 and $^O ne 'MacOS' and $^O ne 'MSWin32';}->() and not -l $full
!ll&&!rl&&r
26700
$Archive::Tar::CHOWN and sub { package Archive::Tar::Constant; use warnings; use strict; $> == 0 and $^O ne 'MacOS' and $^O ne 'MSWin32';}->()
!ll&&!rl&&r
02670
$entry->is_hardlink && 1 && $Archive::Tar::EXTRACT_HARDLINK
!ll&&!rl&&r
000
$entry->is_hardlink and !!1
!ll
00
MC/DC0%0/5
$Archive::Tar::CHOWN and sub { package Archive::Tar::Constant; use warnings; use strict; $> == 0 and $^O ne 'MacOS' and $^O ne 'MSWin32';}->() and not -l $full and not $entry->is_hardlink && !!1 && $Archive::Tar::EXTRACT_HARDLINK
$Archive::Tar::CHOWN sub { package Archive::Tar::Constant; use warnings; use strict; $> == 0 and $^O ne 'MacOS' and $^O ne 'MSWin32';}->() not -l $full $entry->is_hardlink $Archive::Tar::EXTRACT_HARDLINK
Truth table16%1/6
A and B and not(C) and not(D && E)
A= $Archive::Tar::CHOWNB= sub { package Archive::Tar::Constant; use warnings; use strict; $> == 0 and $^O ne 'MacOS' and $^O ne 'MSWin32';}->()C= not -l $fullD= $entry->is_hardlinkE= $Archive::Tar::EXTRACT_HARDLINK
ABCDEresult
0XXXX0
10XXX0
110XX0
111110
1110X1
111101
941 0 ▶ CORE::chown( $entry->uid, $entry->gid, $full ) or
Branch: 1 branch
BranchTrueFalseTotal
CORE::chown $entry->uid, $entry->gid, $full or $self->_error("Could not set uid/gid on '${full}'") 0 0 0
942 $self->_error( qq[Could not set uid/gid on '$full'] );
943 }
944  
945 ### only chmod if we're allowed to, but never chmod symlinks, since they'll
946 ### change the perms on the file they're linking too...
947 267 ▶ if( $CHMOD and not -l $full and not ( $entry->is_hardlink and ON_UNIX and $EXTRACT_HARDLINK ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Archive::Tar::CHMOD and not -l $full and not $entry->is_hardlink && !!1 && $Archive::Tar::EXTRACT_HARDLINK) 260 7 267
Condition54%6/11
$Archive::Tar::CHMOD and not -l $full and not $entry->is_hardlink && !!1 && $Archive::Tar::EXTRACT_HARDLINK
!ll&&!rl&&r
70260
$Archive::Tar::CHMOD and not -l $full
!ll&&!rl&&r
07260
$entry->is_hardlink && 1 && $Archive::Tar::EXTRACT_HARDLINK
!ll&&!rl&&r
26000
$entry->is_hardlink and !!1
!ll
2600
MC/DC25%1/4
$Archive::Tar::CHMOD and not -l $full and not $entry->is_hardlink && !!1 && $Archive::Tar::EXTRACT_HARDLINK
$Archive::Tar::CHMOD not -l $full $entry->is_hardlink $Archive::Tar::EXTRACT_HARDLINK
Truth table40%2/5
A and not(B) and not(C && D)
A= $Archive::Tar::CHMODB= not -l $fullC= $entry->is_hardlinkD= $Archive::Tar::EXTRACT_HARDLINK
ABCDresult
0XXX0
10XX0
11110
110X1
11101
948 260 my $mode = $entry->mode;
949 260 ▶ unless ($SAME_PERMISSIONS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Archive::Tar::SAME_PERMISSIONS) 260 0 260
950 260 $mode &= ~(oct(7000) | umask);
951 }
952 260 ▶ CORE::chmod( $mode, $full ) or
Branch: 1 branch
BranchTrueFalseTotal
CORE::chmod $mode, $full or $self->_error("Could not chown '${full}' to " . $entry->mode) 260 0 260
953 $self->_error( qq[Could not chown '$full' to ] . $entry->mode );
954 }
955  
956 267 return 1;
957 }
958  
959 8 ▶sub _make_special_file {
Subroutine _make_special_file: called CC=25 SCAR=53.6
960 8 my $self = shift;
961 8 ▶ my $entry = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = shift() or return 8 0 8
962 8 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 8 8
963  
964 8 my $err;
965  
966 8 ▶ if( $entry->is_symlink ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($entry->is_symlink) { } 8 0 8
elsif ($entry->is_hardlink) { } 0 0 0
elsif ($entry->is_fifo) { } 0 0 0
elsif ($entry->is_blockdev or $entry->is_chardev) { } 0 0 0
elsif ($entry->is_socket) { } 0 0 0
967 8 ▶ if( !$INSECURE_EXTRACT_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Archive::Tar::INSECURE_EXTRACT_MODE) 4 4 8
968 4 my $linkname = $entry->linkname;
969 4 ▶ if( File::Spec->file_name_is_absolute($linkname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($linkname)) 0 4 4
970 0 $self->_error( qq[Symlink '] . $entry->full_path .
971 qq[' has absolute target. Not extracting under SECURE EXTRACT MODE] );
972 0 return;
973 }
974 4 ▶ if( !defined _symlinks_resolver( $entry->full_path, $linkname, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _symlinks_resolver($entry->full_path, $linkname, 1)) 1 3 4
975 1 $self->_error( qq[Symlink '] . $entry->full_path .
976 qq[' target attempts traversal. Not extracting under SECURE EXTRACT MODE] );
977 1 return;
978 }
979 }
980 7 my $fail;
981 7 if( ON_UNIX ) {
982 7 ▶ symlink( $entry->linkname, $file ) or $fail++;
Branch: 1 branch
BranchTrueFalseTotal
symlink $entry->linkname, $file or ++$fail 7 0 7
983  
984 } else {
985 $self->_extract_special_file_as_plain_file( $entry, $file )
986 or $fail++;
987 }
988  
989 7 ▶ $err = qq[Making symbolic link '$file' to '] .
Branch: 1 branch
BranchTrueFalseTotal
if $fail 0 7 7
990 $entry->linkname .q[' failed] if $fail;
991  
992 } elsif ( $entry->is_hardlink ) {
993 0 ▶ if( !$INSECURE_EXTRACT_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Archive::Tar::INSECURE_EXTRACT_MODE) 0 0 0
994 0 my $linkname = $entry->linkname;
995 0 ▶ if( File::Spec->file_name_is_absolute($linkname) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($linkname)) 0 0 0
996 0 $self->_error( qq[Hardlink '] . $entry->full_path .
997 qq[' has absolute target '$linkname'. Not extracting ] .
998 qq[under SECURE EXTRACT MODE: extraction itself chmods ] .
999 qq[the shared inode.] );
1000 0 return;
1001 }
1002 0 ▶ if( !defined _symlinks_resolver( $entry->full_path, $linkname, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined _symlinks_resolver($entry->full_path, $linkname, 1)) 0 0 0
1003 0 $self->_error( qq[Hardlink '] . $entry->full_path .
1004 qq[' target '$linkname' attempts traversal. Not ] .
1005 qq[extracting under SECURE EXTRACT MODE: extraction ] .
1006 qq[itself chmods the shared inode.] );
1007 0 return;
1008 }
1009 }
1010 0 my $fail;
1011 0 ▶ if( ON_UNIX && $EXTRACT_HARDLINK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Archive::Tar::EXTRACT_HARDLINK) { } 0 0 0
1012 0 ▶ link( $entry->linkname, $file ) or $fail++;
Branch: 1 branch
BranchTrueFalseTotal
link $entry->linkname, $file or ++$fail 0 0 0
1013  
1014 } else {
1015 0 ▶ $self->_extract_special_file_as_plain_file( $entry, $file )
Branch: 1 branch
BranchTrueFalseTotal
$self->_extract_special_file_as_plain_file($entry, $file) or ++$fail 0 0 0
1016 or $fail++;
1017 }
1018  
1019 0 ▶ $err = qq[Making hard link from '] . $entry->linkname .
Branch: 1 branch
BranchTrueFalseTotal
if $fail 0 0 0
1020 qq[' to '$file' failed] if $fail;
1021  
1022 } elsif ( $entry->is_fifo ) {
1023 0 ▶ ON_UNIX && !system('mknod', $file, 'p') or
Branch: 1 branch
BranchTrueFalseTotal
system "mknod", $file, "p" and $err = q[Making fifo '] . $entry->name . q[' failed] 0 0 0
1024 $err = qq[Making fifo ']. $entry->name .qq[' failed];
1025  
1026 ▶ } elsif ( $entry->is_blockdev or $entry->is_chardev ) {
Condition0%0/3
$entry->is_blockdev or $entry->is_chardev
l!l&&r!l&&!r
000
MC/DC0%0/2
$entry->is_blockdev or $entry->is_chardev
$entry->is_blockdev $entry->is_chardev
Truth table0%0/3
A or B
A= $entry->is_blockdevB= $entry->is_chardev
ABresult
1X1
011
000
1027 0 ▶ my $mode = $entry->is_blockdev ? 'b' : 'c';
Branch: 1 branch
BranchTrueFalseTotal
$entry->is_blockdev ? : 0 0 0
1028  
1029 0 ▶ ON_UNIX && !system('mknod', $file, $mode,
Branch: 1 branch
BranchTrueFalseTotal
system "mknod", $file, $mode, $entry->devmajor, $entry->devminor and $err = q[Making block device '] . $entry->name . q[' (maj=] . $entry->devmajor . ' min=' . $entry->devminor . ') failed.' 0 0 0
1030 $entry->devmajor, $entry->devminor) or
1031 $err = qq[Making block device ']. $entry->name .qq[' (maj=] .
1032 $entry->devmajor . qq[ min=] . $entry->devminor .
1033 qq[) failed.];
1034  
1035 } elsif ( $entry->is_socket ) {
1036 ### the original doesn't do anything special for sockets.... ###
1037 0 1;
1038 }
1039  
1040 7 ▶ return $err ? $self->_error( $err ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 7 7
1041 }
1042  
1043 ### don't know how to make symlinks, let's just extract the file as
1044 ### a plain file
1045 0 ▶sub _extract_special_file_as_plain_file {
Subroutine _extract_special_file_as_plain_file: not called CC=5 SCAR=34.0
1046 0 my $self = shift;
1047 0 ▶ my $entry = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = shift() or return 0 0 0
1048 0 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
1049  
1050 0 my $err;
1051 TRY: {
1052 0 my $orig = $self->_find_entry( $entry->linkname, $entry );
1053  
1054 0 ▶ unless( $orig ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($orig) 0 0 0
1055 0 $err = qq[Could not find file '] . $entry->linkname .
1056 qq[' in memory.];
1057 0 last TRY;
1058 }
1059  
1060 ### clone the entry, make it appear as a normal file ###
1061 0 my $clone = $orig->clone;
1062 0 $clone->_downgrade_to_plainfile;
1063 0 ▶ $self->_extract_file( $clone, $file ) or last TRY;
Branch: 1 branch
BranchTrueFalseTotal
$self->_extract_file($clone, $file) or last TRY 0 0 0
1064  
1065 0 return 1;
1066 }
1067  
1068 0 return $self->_error($err);
1069 }
1070  
1071 =head2 $tar->list_files( [\@properties] )
1072  
1073 Returns a list of the names of all the files in the archive.
1074  
1075 If C<list_files()> is passed an array reference as its first argument
1076 it returns a list of hash references containing the requested
1077 properties of each file. The following list of properties is
1078 supported: name, size, mtime (last modified date), mode, uid, gid,
1079 linkname, uname, gname, devmajor, devminor, prefix.
1080  
1081 Passing an array reference containing only one element, 'name', is
1082 special cased to return a list of names rather than a list of hash
1083 references, making it equivalent to calling C<list_files> without
1084 arguments.
1085  
1086 =cut
1087  
1088 48 ▶sub list_files {
Subroutine list_files: called CC=7 SCAR=28.4
Pod documented
1089 48 my $self = shift;
1090 48 ▶ my $aref = shift || [ ];
Condition50%1/2
shift() || []
l!l
048
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1091  
1092 48 ▶ unless( $self->_data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_data) 0 48 48
1093 0 ▶ $self->read() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->read or return 0 0 0
1094 }
1095  
1096 48 ▶ if( @$aref == 0 or ( @$aref == 1 and $aref->[0] eq 'name' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$aref == 0 or @$aref == 1 and $aref->[0] eq 'name') { } 48 0 48
Condition16%1/6
@$aref == 0 or @$aref == 1 and $aref->[0] eq "name"
l!l&&r!l&&!r
4800
@$aref == 1 and $aref->[0] eq "name"
!ll&&!rl&&r
000
MC/DC0%0/3
@$aref == 0 or @$aref == 1 and $aref->[0] eq "name"
@$aref == 0 @$aref == 1 $aref->[0] eq "name"
Truth table25%1/4
A or B and C
A= @$aref == 0B= @$aref == 1C= $aref->[0] eq "name"
ABCresult
1XX1
0111
00X0
0100
1097 48 return map { $_->full_path } @{$self->_data};
1098 } else {
1099  
1100 #my @rv;
1101 #for my $obj ( @{$self->_data} ) {
1102 # push @rv, { map { $_ => $obj->$_() } @$aref };
1103 #}
1104 #return @rv;
1105  
1106 ### this does the same as the above.. just needs a +{ }
1107 ### to make sure perl doesn't confuse it for a block
1108 0 return map { my $o=$_;
1109 0 +{ map { $_ => $o->$_() } @$aref }
1110 0 } @{$self->_data};
1111 }
1112 }
1113  
1114 220 ▶sub _find_entry {
Subroutine _find_entry: called CC=14 SCAR=41.2
1115 220 my $self = shift;
1116 220 my $file = shift;
1117  
1118 220 ▶ unless( defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 220 220
1119 0 $self->_error( qq[No file specified] );
1120 0 return;
1121 }
1122  
1123 ### it's an object already
1124 220 ▶ return $file if UNIVERSAL::isa( $file, 'Archive::Tar::File' );
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($file, "Archive::Tar::File") 16 204 220
1125  
1126 seach_entry:
1127 204 ▶ if($self->_data){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_data) 204 0 204
1128 204 for my $entry ( @{$self->_data} ) {
1129 512 my $path = $entry->full_path;
1130 512 ▶ return $entry if $path eq $file;
Branch: 1 branch
BranchTrueFalseTotal
if $path eq $file 201 311 512
1131 }
1132 }
1133  
1134 3 ▶ if($Archive::Tar::RESOLVE_SYMLINK!~/none/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($Archive::Tar::RESOLVE_SYMLINK =~ /none/) 3 0 3
1135 3 ▶ if(my $link_entry = shift()){#fallback mode when symlinks are using relative notations ( ../a/./b/text.bin )
Branch: 1 branch
BranchTrueFalseTotal
if (my $link_entry = shift()) 0 3 3
1136 0 $file = _symlinks_resolver( $link_entry->name, $file );
1137 0 ▶ goto seach_entry if $self->_data;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_data 0 0 0
1138  
1139 #this will be slower than never, but won't failed!
1140  
1141 0 my $iterargs = $link_entry->{'_archive'};
1142 0 ▶ if($Archive::Tar::RESOLVE_SYMLINK=~/speed/ && @$iterargs==3){
Branch: 1 branch
BranchTrueFalseTotal
if ($Archive::Tar::RESOLVE_SYMLINK =~ /speed/ and @$iterargs == 3) 0 0 0
Condition0%0/3
$Archive::Tar::RESOLVE_SYMLINK =~ /speed/ and @$iterargs == 3
!ll&&!rl&&r
000
MC/DC0%0/2
$Archive::Tar::RESOLVE_SYMLINK =~ /speed/ and @$iterargs == 3
$Archive::Tar::RESOLVE_SYMLINK =~ /speed/ @$iterargs == 3
Truth table0%0/3
A and B
A= $Archive::Tar::RESOLVE_SYMLINK =~ /speed/B= @$iterargs == 3
ABresult
0X0
100
111
1143 #faster but whole archive will be read in memory
1144 #read whole archive and share data
1145 0 my $archive = Archive::Tar->new;
1146 0 $archive->read( @$iterargs );
1147 0 push @$iterargs, $archive; #take a trace for destruction
1148 0 ▶ if($archive->_data){
Branch: 1 branch
BranchTrueFalseTotal
if ($archive->_data) 0 0 0
1149 0 $self->_data( $archive->_data );
1150 0 goto seach_entry;
1151 }
1152 }#faster
1153  
1154 {#slower but lower memory usage
1155 # $iterargs = [$filename, $compressed, $opts];
1156 0 my $next = Archive::Tar->iter( @$iterargs );
1157 0 ▶ while(my $e = $next->()){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = &$next()) 0 0 0
1158 0 ▶ if($e->full_path eq $file){
Branch: 1 branch
BranchTrueFalseTotal
if ($e->full_path eq $file) 0 0 0
1159 0 undef $next;
1160 0 return $e;
1161 }
1162 }
1163 }#slower
1164 }
1165 }
1166  
1167 3 $self->_error( qq[No such file in archive: '$file'] );
1168 3 return;
1169 }
1170  
1171 =head2 $tar->get_files( [@filenames] )
1172  
1173 Returns the C<Archive::Tar::File> objects matching the filenames
1174 provided. If no filename list was passed, all C<Archive::Tar::File>
1175 objects in the current Tar object are returned.
1176  
1177 Please refer to the C<Archive::Tar::File> documentation on how to
1178 handle these objects.
1179  
1180 =cut
1181  
1182 114 ▶sub get_files {
Subroutine get_files: called CC=3 SCAR=11.0
Pod documented
1183 114 my $self = shift;
1184  
1185 114 ▶ return @{ $self->_data } unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 110 4 114
1186  
1187 4 my @list;
1188 4 for my $file ( @_ ) {
1189 4 push @list, grep { defined } $self->_find_entry( $file );
1190 }
1191  
1192 4 return @list;
1193 }
1194  
1195 =head2 $tar->get_content( $file )
1196  
1197 Return the content of the named file.
1198  
1199 =cut
1200  
1201 11 ▶sub get_content {
Subroutine get_content: called CC=2 SCAR=7.1
Pod documented
1202 11 my $self = shift;
1203 11 ▶ my $entry = $self->_find_entry( shift ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->_find_entry(shift()) or return 11 0 11
1204  
1205 11 return $entry->data;
1206 }
1207  
1208 =head2 $tar->replace_content( $file, $content )
1209  
1210 Make the string $content be the content for the file named $file.
1211  
1212 =cut
1213  
1214 1 ▶sub replace_content {
Subroutine replace_content: called CC=2 SCAR=7.1
Pod documented
1215 1 my $self = shift;
1216 1 ▶ my $entry = $self->_find_entry( shift ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->_find_entry(shift()) or return 1 0 1
1217  
1218 1 return $entry->replace_content( shift );
1219 }
1220  
1221 =head2 $tar->rename( $file, $new_name )
1222  
1223 Rename the file of the in-memory archive to $new_name.
1224  
1225 Note that you must specify a Unix path for $new_name, since per tar
1226 standard, all files in the archive must be Unix paths.
1227  
1228 Returns true on success and false on failure.
1229  
1230 =cut
1231  
1232 2 ▶sub rename {
Subroutine rename: called CC=4 SCAR=14.3
Pod documented
1233 2 my $self = shift;
1234 2 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 2 2
1235 2 ▶ my $new = shift; return unless defined $new;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 0 2 2
1236  
1237 2 ▶ my $entry = $self->_find_entry( $file ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->_find_entry($file) or return 2 0 2
1238  
1239 2 return $entry->rename( $new );
1240 }
1241  
1242 =head2 $tar->chmod( $file, $mode )
1243  
1244 Change mode of $file to $mode.
1245  
1246 Returns true on success and false on failure.
1247  
1248 =cut
1249  
1250 1 ▶sub chmod {
Subroutine chmod: called CC=5 SCAR=17.1
Pod documented
1251 1 my $self = shift;
1252 1 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 1 1
1253 1 ▶ my $mode = shift; return unless defined $mode && $mode =~ /^[0-7]{1,4}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode and $mode =~ /^[0-7]{1,4}$/ 0 1 1
Condition33%1/3
defined $mode and $mode =~ /^[0-7]{1,4}$/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $mode and $mode =~ /^[0-7]{1,4}$/
defined $mode $mode =~ /^[0-7]{1,4}$/
Truth table33%1/3
A and B
A= defined $modeB= $mode =~ /^[0-7]{1,4}$/
ABresult
0X0
100
111
1254 1 my @args = ("$mode");
1255  
1256 1 ▶ my $entry = $self->_find_entry( $file ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->_find_entry($file) or return 1 0 1
1257 1 my $x = $entry->chmod( @args );
1258 1 return $x;
1259 }
1260  
1261 =head2 $tar->chown( $file, $uname [, $gname] )
1262  
1263 Change owner $file to $uname and $gname.
1264  
1265 Returns true on success and false on failure.
1266  
1267 =cut
1268  
1269 2 ▶sub chown {
Subroutine chown: called CC=4 SCAR=14.1
Pod documented
1270 2 my $self = shift;
1271 2 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 2 2
1272 2 ▶ my $uname = shift; return unless defined $uname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uname 0 2 2
1273 2 my @args = ($uname);
1274 2 push(@args, shift);
1275  
1276 2 ▶ my $entry = $self->_find_entry( $file ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->_find_entry($file) or return 2 0 2
1277 2 my $x = $entry->chown( @args );
1278 2 return $x;
1279 }
1280  
1281 =head2 $tar->remove (@filenamelist)
1282  
1283 Removes any entries with names matching any of the given filenames
1284 from the in-memory archive. Returns a list of C<Archive::Tar::File>
1285 objects that remain.
1286  
1287 =cut
1288  
1289 1 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
1290 1 my $self = shift;
1291 1 my @list = @_;
1292  
1293 1 my %seen = map { $_->full_path => $_ } @{$self->_data};
1294 1 delete $seen{ $_ } for @list;
1295  
1296 1 $self->_data( [values %seen] );
1297  
1298 1 return values %seen;
1299 }
1300  
1301 =head2 $tar->clear
1302  
1303 C<clear> clears the current in-memory archive. This effectively gives
1304 you a 'blank' object, ready to be filled again. Note that C<clear>
1305 only has effect on the object, not the underlying tarfile.
1306  
1307 =cut
1308  
1309 2 ▶sub clear {
Subroutine clear: called CC=2 SCAR=7.0
Pod documented
1310 2 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 2 0 2
1311  
1312 2 $self->_data( [] );
1313 2 $self->_file( '' );
1314  
1315 2 return 1;
1316 }
1317  
1318  
1319 =head2 $tar->write ( [$file, $compressed, $prefix] )
1320  
1321 Write the in-memory archive to disk. The first argument can either
1322 be the name of a file or a reference to an already open filehandle (a
1323 GLOB reference).
1324  
1325 The second argument is used to indicate compression. You can
1326 compress using C<gzip>, C<bzip2> or C<xz>. If you pass a digit, it's assumed
1327 to be the C<gzip> compression level (between 1 and 9), but the use of
1328 constants is preferred:
1329  
1330 # write a gzip compressed file
1331 $tar->write( 'out.tgz', COMPRESS_GZIP );
1332  
1333 # write a bzip compressed file
1334 $tar->write( 'out.tbz', COMPRESS_BZIP );
1335  
1336 # write a xz compressed file
1337 $tar->write( 'out.txz', COMPRESS_XZ );
1338  
1339 Note that when you pass in a filehandle, the compression argument
1340 is ignored, as all files are printed verbatim to your filehandle.
1341 If you wish to enable compression with filehandles, use an
1342 C<IO::Zlib>, C<IO::Compress::Bzip2> or C<IO::Compress::Xz> filehandle instead.
1343  
1344 The third argument is an optional prefix. All files will be tucked
1345 away in the directory you specify as prefix. So if you have files
1346 'a' and 'b' in your archive, and you specify 'foo' as prefix, they
1347 will be written to the archive as 'foo/a' and 'foo/b'.
1348  
1349 If no arguments are given, C<write> returns the entire formatted
1350 archive as a string, which could be useful if you'd like to stuff the
1351 archive into a socket or a pipe to gzip or something.
1352  
1353  
1354 =cut
1355  
1356 38 ▶sub write {
Subroutine write: called CC=31 SCAR=39.4
Pod documented
1357 38 my $self = shift;
1358 38 ▶ my $file = shift; $file = '' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 3 35 38
1359 38 ▶ my $gzip = shift || 0;
Condition100%2/2
shift() || 0
l!l
1721
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1360 38 ▶ my $ext_prefix = shift; $ext_prefix = '' unless defined $ext_prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext_prefix 38 0 38
1361 38 my $dummy = '';
1362  
1363 ### only need a handle if we have a file to print to ###
1364 my $handle = length($file)
1365 ? ( $self->_get_handle($file, $gzip, WRITE_ONLY->($gzip) )
1366 or return )
1367 38 ▶ : $HAS_PERLIO ? do { open my $h, '>', \$dummy; $h }
Branch: 3 branches
BranchTrueFalseTotal
length $file ? : 35 3 38
$Archive::Tar::HAS_PERLIO ? : 3 0 3
$Archive::Tar::HAS_IO_STRING ? : 0 0 0
Condition50%1/2
$self->_get_handle($file, $gzip, sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] ? 'wb' . (shift()) : 'w';}->($gzip)) or return
l!l
350
MC/DC0%0/1
$self->_get_handle($file, $gzip, sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] ? 'wb' . (shift()) : 'w';}->($gzip)) or return
$self->_get_handle($file, $gzip, sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] ? 'wb' . (shift()) : 'w';}->($gzip))
Truth table50%1/2
A
A= $self->_get_handle($file, $gzip, sub { package Archive::Tar::Constant; use warnings; use strict; $_[0] ? 'wb' . (shift()) : 'w';}->($gzip))
Aresult
11
00
1368 : $HAS_IO_STRING ? IO::String->new
1369 : __PACKAGE__->no_string_support();
1370  
1371 ### Addresses: #41798: Nonempty $\ when writing a TAR file produces a
1372 ### corrupt TAR file. Must clear out $\ to make sure no garbage is
1373 ### printed to the archive
1374 38 local $\;
1375  
1376 38 for my $entry ( @{$self->_data} ) {
1377 ### entries to be written to the tarfile ###
1378 95 my @write_me;
1379  
1380 ### only now will we change the object to reflect the current state
1381 ### of the name and prefix fields -- this needs to be limited to
1382 ### write() only!
1383 95 my $clone = $entry->clone;
1384  
1385  
1386 ### so, if you don't want use to use the prefix, we'll stuff
1387 ### everything in the name field instead
1388 95 ▶ if( $DO_NOT_USE_PREFIX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Archive::Tar::DO_NOT_USE_PREFIX) { } 4 91 95
1389  
1390 ### you might have an extended prefix, if so, set it in the clone
1391 ### XXX is ::Unix right?
1392 4 ▶ $clone->name( length $ext_prefix
Branch: 1 branch
BranchTrueFalseTotal
length $ext_prefix ? : 0 4 4
1393 ? File::Spec::Unix->catdir( $ext_prefix,
1394 $clone->full_path)
1395 : $clone->full_path );
1396 4 $clone->prefix( '' );
1397  
1398 ### otherwise, we'll have to set it properly -- prefix part in the
1399 ### prefix and name part in the name field.
1400 } else {
1401  
1402 ### split them here, not before!
1403 91 my ($prefix,$name) = $clone->_prefix_and_file( $clone->full_path );
1404  
1405 ### you might have an extended prefix, if so, set it in the clone
1406 ### XXX is ::Unix right?
1407 91 ▶ $prefix = File::Spec::Unix->catdir( $ext_prefix, $prefix )
Branch: 1 branch
BranchTrueFalseTotal
if length $ext_prefix 0 91 91
1408 if length $ext_prefix;
1409  
1410 91 $clone->prefix( $prefix );
1411 91 $clone->name( $name );
1412 }
1413  
1414 ### names are too long, and will get truncated if we don't add a
1415 ### '@LongLink' file...
1416 95 ▶ my $make_longlink = ( length($clone->name) > NAME_LENGTH or
Condition100%5/5
length $clone->name > 100 || length $clone->prefix > 155 || 0
l!l
1580
length $clone->name > 100 or length $clone->prefix > 155
l!l&&r!l&&!r
8780
MC/DC100%2/2
length $clone->name > 100 || length $clone->prefix > 155 || 0
length $clone->name > 100 length $clone->prefix > 155
Truth table100%3/3
A or B
A= length $clone->name > 100B= length $clone->prefix > 155
ABresult
1X1
011
000
1417 length($clone->prefix) > PREFIX_LENGTH
1418 ) || 0;
1419  
1420 ### perhaps we need to make a longlink file?
1421 95 ▶ if( $make_longlink ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($make_longlink) 15 80 95
1422 15 my $longlink = Archive::Tar::File->new(
1423 data => LONGLINK_NAME,
1424 $clone->full_path,
1425 { type => LONGLINK }
1426 );
1427  
1428 15 ▶ unless( $longlink ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($longlink) 0 15 15
1429 0 $self->_error( qq[Could not create 'LongLink' entry for ] .
1430 qq[oversize file '] . $clone->full_path ."'" );
1431 0 return;
1432 };
1433  
1434 15 push @write_me, $longlink;
1435 }
1436  
1437 95 push @write_me, $clone;
1438  
1439 ### write the one, optionally 2 a::t::file objects to the handle
1440 95 for my $clone (@write_me) {
1441  
1442 ### if the file is a symlink, there are 2 options:
1443 ### either we leave the symlink intact, but then we don't write any
1444 ### data OR we follow the symlink, which means we actually make a
1445 ### copy. if we do the latter, we have to change the TYPE of the
1446 ### clone to 'FILE'
1447 110 ▶ my $link_ok = $clone->is_symlink && $Archive::Tar::FOLLOW_SYMLINK;
Condition66%2/3
$clone->is_symlink && $Archive::Tar::FOLLOW_SYMLINK
!ll&&!rl&&r
10820
MC/DC0%0/2
$clone->is_symlink && $Archive::Tar::FOLLOW_SYMLINK
$clone->is_symlink $Archive::Tar::FOLLOW_SYMLINK
Truth table66%2/3
A && B
A= $clone->is_symlinkB= $Archive::Tar::FOLLOW_SYMLINK
ABresult
0X0
100
111
1448 110 ▶ my $data_ok = !$clone->is_symlink && $clone->has_content;
Condition100%3/3
!$clone->is_symlink && $clone->has_content
!ll&&!rl&&r
22286
MC/DC100%2/2
!$clone->is_symlink && $clone->has_content
!$clone->is_symlink $clone->has_content
Truth table100%3/3
not(A) && B
A= !$clone->is_symlinkB= $clone->has_content
ABresult
0X0
100
111
1449  
1450 ### downgrade to a 'normal' file if it's a symlink we're going to
1451 ### treat as a regular file
1452 110 ▶ $clone->_downgrade_to_plainfile if $link_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $link_ok 0 110 110
1453  
1454 ### get the header for this block
1455 110 my $header = $self->_format_tar_entry( $clone );
1456 110 ▶ unless( $header ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header) 0 110 110
1457 0 $self->_error(q[Could not format header for: ] .
1458 $clone->full_path );
1459 0 return;
1460 }
1461  
1462 110 ▶ unless( print $handle $header ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $handle $header) 0 110 110
1463 0 $self->_error(q[Could not write header for: ] .
1464 $clone->full_path);
1465 0 return;
1466 }
1467  
1468 110 ▶ if( $link_ok or $data_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_ok or $data_ok) 86 24 110
Condition66%2/3
$link_ok or $data_ok
l!l&&r!l&&!r
08624
MC/DC50%1/2
$link_ok or $data_ok
$link_ok $data_ok
Truth table66%2/3
A or B
A= $link_okB= $data_ok
ABresult
1X1
011
000
1469 86 ▶ unless( print $handle $clone->data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $handle $clone->data) 0 86 86
1470 0 $self->_error(q[Could not write data for: ] .
1471 $clone->full_path);
1472 0 return;
1473 }
1474  
1475 ### pad the end of the clone if required ###
1476 86 ▶ print $handle TAR_PAD->( $clone->size ) if $clone->size % BLOCK
Branch: 1 branch
BranchTrueFalseTotal
if $clone->size % 512 86 0 86
1477 }
1478  
1479 } ### done writing these entries
1480 }
1481  
1482 ### write the end markers ###
1483 38 ▶ print $handle TAR_END x 2 or
Branch: 1 branch
BranchTrueFalseTotal
print $handle "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" or return $self->_error("Could not write tar end markers") 38 0 38
1484 return $self->_error( qq[Could not write tar end markers] );
1485  
1486 ### did you want it written to a file, or returned as a string? ###
1487 my $rv = length($file) ? 1
1488 : $HAS_PERLIO ? $dummy
1489 0 ▶ : do { seek $handle, 0, 0; local $/; <$handle> };
Branch: 2 branches
BranchTrueFalseTotal
length $file ? : 35 3 38
$Archive::Tar::HAS_PERLIO ? : 3 0 3
1490  
1491 ### make sure to close the handle if we created it
1492 38 ▶ if ( $file ne $handle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file ne $handle) 38 0 38
1493 38 ▶ unless( close $handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $handle) 0 38 38
1494 0 $self->_error( qq[Could not write tar] );
1495 0 return;
1496 }
1497 }
1498  
1499 38 return $rv;
1500 }
1501  
1502 110 ▶sub _format_tar_entry {
Subroutine _format_tar_entry: called CC=11 SCAR=25.0
1503 110 my $self = shift;
1504 110 ▶ my $entry = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = shift() or return 110 0 110
1505 110 ▶ my $ext_prefix = shift; $ext_prefix = '' unless defined $ext_prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext_prefix 110 0 110
1506 110 ▶ my $no_prefix = shift || 0;
Condition50%1/2
shift() || 0
l!l
0110
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1507  
1508 110 my $file = $entry->name;
1509 110 ▶ my $prefix = $entry->prefix; $prefix = '' unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 110 110
1510  
1511 ### remove the prefix from the file name
1512 ### not sure if this is still needed --kane
1513 ### no it's not -- Archive::Tar::File->_new_from_file will take care of
1514 ### this for us. Even worse, this would break if we tried to add a file
1515 ### like x/x.
1516 #if( length $prefix ) {
1517 # $file =~ s/^$match//;
1518 #}
1519  
1520 110 ▶ $prefix = File::Spec::Unix->catdir($ext_prefix, $prefix)
Branch: 1 branch
BranchTrueFalseTotal
if length $ext_prefix 0 110 110
1521 if length $ext_prefix;
1522  
1523 ### not sure why this is... ###
1524 110 my $l = PREFIX_LENGTH; # is ambiguous otherwise...
1525 110 ▶ substr ($prefix, 0, -$l) = "" if length $prefix >= PREFIX_LENGTH;
Branch: 1 branch
BranchTrueFalseTotal
if length $prefix >= 155 7 103 110
1526  
1527 110 ▶ my $f1 = "%06o"; my $f2 = $ZERO_PAD_NUMBERS ? "%011o" : "%11o";
Branch: 1 branch
BranchTrueFalseTotal
$Archive::Tar::ZERO_PAD_NUMBERS ? : 0 110 110
1528  
1529 ### this might be optimizable with a 'changed' flag in the file objects ###
1530 my $tar = pack (
1531 PACK,
1532 $file,
1533  
1534 330 (map { sprintf( $f1, $entry->$_() ) } qw[mode uid gid]),
1535 220 (map { sprintf( $f2, $entry->$_() ) } qw[size mtime]),
1536  
1537 "", # checksum field - space padded a bit down
1538  
1539 330 ▶ (map { $entry->$_() } qw[type linkname magic]),
Condition50%1/2
$entry->version || '00'
l!l
1100
MC/DC0%0/1
$entry->version || '00'
$entry->version
Truth table50%1/2
A
A= $entry->version
Aresult
11
00
1540  
1541 $entry->version || TAR_VERSION,
1542  
1543 220 (map { $entry->$_() } qw[uname gname]),
1544 110 ▶ (map { sprintf( $f1, $entry->$_() ) } qw[devmajor devminor]),
Branch: 1 branch
BranchTrueFalseTotal
$no_prefix ? : 0 110 110
1545  
1546 ($no_prefix ? '' : $prefix)
1547 );
1548  
1549 ### add the checksum ###
1550 110 ▶ my $checksum_fmt = $ZERO_PAD_NUMBERS ? "%06o\0" : "%06o\0";
Branch: 1 branch
BranchTrueFalseTotal
$Archive::Tar::ZERO_PAD_NUMBERS ? : 0 110 110
1551 110 substr($tar,148,7) = sprintf("%6o\0", unpack("%16C*",$tar));
1552  
1553 110 return $tar;
1554 }
1555  
1556 =head2 $tar->add_files( @filenamelist )
1557  
1558 Takes a list of filenames and adds them to the in-memory archive.
1559  
1560 The path to the file is automatically converted to a Unix like
1561 equivalent for use in the archive, and, if on MacOS, the file's
1562 modification time is converted from the MacOS epoch to the Unix epoch.
1563 So tar archives created on MacOS with B<Archive::Tar> can be read
1564 both with I<tar> on Unix and applications like I<suntar> or
1565 I<Stuffit Expander> on MacOS.
1566  
1567 Be aware that the file's type/creator and resource fork will be lost,
1568 which is usually what you want in cross-platform archives.
1569  
1570 Instead of a filename, you can also pass it an existing C<Archive::Tar::File>
1571 object from, for example, another archive. The object will be clone, and
1572 effectively be a copy of the original, not an alias.
1573  
1574 Returns a list of C<Archive::Tar::File> objects that were just added.
1575  
1576 =cut
1577  
1578 30 ▶sub add_files {
Subroutine add_files: called CC=8 SCAR=23.2
Pod documented
1579 30 my $self = shift;
1580 30 ▶ my @files = @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@files) = or return 30 0 30
1581  
1582 30 my @rv;
1583 30 for my $file ( @files ) {
1584  
1585 ### you passed an Archive::Tar::File object
1586 ### clone it so we don't accidentally have a reference to
1587 ### an object from another archive
1588 50 ▶ if( UNIVERSAL::isa( $file,'Archive::Tar::File' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($file, "Archive::Tar::File")) 28 22 50
1589 28 push @rv, $file->clone;
1590 28 next;
1591 }
1592  
1593 22 eval {
1594 22 ▶ if( utf8::is_utf8( $file )) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($file)) 0 22 22
1595 0 utf8::encode( $file );
1596 }
1597 };
1598  
1599 22 ▶ unless( -e $file || -l $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file or -l $file) 0 22 22
Condition33%1/3
-e $file or -l $file
l!l&&r!l&&!r
2200
MC/DC0%0/2
-e $file or -l $file
-e $file -l $file
Truth table33%1/3
A or B
A= -e $fileB= -l $file
ABresult
1X1
011
000
1600 0 $self->_error( qq[No such file: '$file'] );
1601 0 next;
1602 }
1603  
1604 22 my $obj = Archive::Tar::File->new( file => $file );
1605 22 ▶ unless( $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 0 22 22
1606 0 $self->_error( qq[Unable to add file: '$file'] );
1607 0 next;
1608 }
1609  
1610 22 push @rv, $obj;
1611 }
1612  
1613 30 push @{$self->{_data}}, @rv;
1614  
1615 30 return @rv;
1616 }
1617  
1618 =head2 $tar->add_data ( $filename, $data, [$opthashref] )
1619  
1620 Takes a filename, a scalar full of data and optionally a reference to
1621 a hash with specific options.
1622  
1623 Will add a file to the in-memory archive, with name C<$filename> and
1624 content C<$data>. Specific properties can be set using C<$opthashref>.
1625 The following list of properties is supported: name, size, mtime
1626 (last modified date), mode, uid, gid, linkname, uname, gname,
1627 devmajor, devminor, prefix, type. (On MacOS, the file's path and
1628 modification times are converted to Unix equivalents.)
1629  
1630 Valid values for the file type are the following constants defined by
1631 Archive::Tar::Constant:
1632  
1633 =over 4
1634  
1635 =item FILE
1636  
1637 Regular file.
1638  
1639 =item HARDLINK
1640  
1641 =item SYMLINK
1642  
1643 Hard and symbolic ("soft") links; linkname should specify target.
1644  
1645 =item CHARDEV
1646  
1647 =item BLOCKDEV
1648  
1649 Character and block devices. devmajor and devminor should specify the major
1650 and minor device numbers.
1651  
1652 =item DIR
1653  
1654 Directory.
1655  
1656 =item FIFO
1657  
1658 FIFO (named pipe).
1659  
1660 =item SOCKET
1661  
1662 Socket.
1663  
1664 =back
1665  
1666 Returns the C<Archive::Tar::File> object that was just added, or
1667 C<undef> on failure.
1668  
1669 =cut
1670  
1671 16 ▶sub add_data {
Subroutine add_data: called CC=2 SCAR=6.9
Pod documented
1672 16 my $self = shift;
1673 16 my ($file, $data, $opt) = @_;
1674  
1675 16 my $obj = Archive::Tar::File->new( data => $file, $data, $opt );
1676 16 ▶ unless( $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 1 15 16
1677 1 $self->_error( qq[Unable to add file: '$file'] );
1678 1 return;
1679 }
1680  
1681 15 push @{$self->{_data}}, $obj;
1682  
1683 15 return $obj;
1684 }
1685  
1686 =head2 $tar->error( [$BOOL] )
1687  
1688 Returns the current error string (usually, the last error reported).
1689 If a true value was specified, it will give the C<Carp::longmess>
1690 equivalent of the error, in effect giving you a stacktrace.
1691  
1692 For backwards compatibility, this error is also available as
1693 C<$Archive::Tar::error> although it is much recommended you use the
1694 method call instead.
1695  
1696 =cut
1697  
1698 {
1699 17 $error = '';
1700 17 my $longmess;
1701  
1702 16 ▶ sub _error {
Subroutine _error: called CC=4 SCAR=14.0
1703 16 my $self = shift;
1704 16 my $msg = $error = shift;
1705 16 $longmess = Carp::longmess($error);
1706 16 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 16 0 16
1707 16 $self->{_error} = $error;
1708 16 $self->{_longmess} = $longmess;
1709 }
1710  
1711 ### set Archive::Tar::WARN to 0 to disable printing
1712 ### of errors
1713 16 ▶ if( $WARN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Archive::Tar::WARN) 6 10 16
1714 6 ▶ carp $DEBUG ? $longmess : $msg;
Branch: 1 branch
BranchTrueFalseTotal
$Archive::Tar::DEBUG ? : 0 6 6
1715 }
1716  
1717 16 return;
1718 }
1719  
1720 22 ▶ sub error {
Subroutine error: called CC=4 SCAR=14.2
Pod documented
1721 22 my $self = shift;
1722 22 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 20 2 22
1723 20 ▶ return shift() ? $self->{_longmess} : $self->{_error};
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 20 20
1724 } else {
1725 2 ▶ return shift() ? $longmess : $error;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 2 2
1726 }
1727 }
1728 }
1729  
1730 =head2 $tar->setcwd( $cwd );
1731  
1732 C<Archive::Tar> needs to know the current directory, and it will run
1733 C<Cwd::cwd()> I<every> time it extracts a I<relative> entry from the
1734 tarfile and saves it in the file system. (As of version 1.30, however,
1735 C<Archive::Tar> will use the speed optimization described below
1736 automatically, so it's only relevant if you're using C<extract_file()>).
1737  
1738 Since C<Archive::Tar> doesn't change the current directory internally
1739 while it is extracting the items in a tarball, all calls to C<Cwd::cwd()>
1740 can be avoided if we can guarantee that the current directory doesn't
1741 get changed externally.
1742  
1743 To use this performance boost, set the current directory via
1744  
1745 use Cwd;
1746 $tar->setcwd( cwd() );
1747  
1748 once before calling a function like C<extract_file> and
1749 C<Archive::Tar> will use the current directory setting from then on
1750 and won't call C<Cwd::cwd()> internally.
1751  
1752 To switch back to the default behaviour, use
1753  
1754 $tar->setcwd( undef );
1755  
1756 and C<Archive::Tar> will call C<Cwd::cwd()> internally again.
1757  
1758 If you're using C<Archive::Tar>'s C<extract()> method, C<setcwd()> will
1759 be called for you.
1760  
1761 =cut
1762  
1763 17 ▶sub setcwd {
Subroutine setcwd: not called CC=1 SCAR=3.5
Pod documented
1764 0 my $self = shift;
1765 0 my $cwd = shift;
1766  
1767 0 $self->{cwd} = $cwd;
1768 }
1769  
1770 =head1 Class Methods
1771  
1772 =head2 Archive::Tar->create_archive($file, $compressed, @filelist)
1773  
1774 Creates a tar file from the list of files provided. The first
1775 argument can either be the name of the tar file to create or a
1776 reference to an open file handle (e.g. a GLOB reference).
1777  
1778 The second argument is used to indicate compression. You can
1779 compress using C<gzip>, C<bzip2> or C<xz>. If you pass a digit, it's assumed
1780 to be the C<gzip> compression level (between 1 and 9), but the use of
1781 constants is preferred:
1782  
1783 # write a gzip compressed file
1784 Archive::Tar->create_archive( 'out.tgz', COMPRESS_GZIP, @filelist );
1785  
1786 # write a bzip compressed file
1787 Archive::Tar->create_archive( 'out.tbz', COMPRESS_BZIP, @filelist );
1788  
1789 # write a xz compressed file
1790 Archive::Tar->create_archive( 'out.txz', COMPRESS_XZ, @filelist );
1791  
1792 Note that when you pass in a filehandle, the compression argument
1793 is ignored, as all files are printed verbatim to your filehandle.
1794 If you wish to enable compression with filehandles, use an
1795 C<IO::Zlib>, C<IO::Compress::Bzip2> or C<IO::Compress::Xz> filehandle instead.
1796  
1797 The remaining arguments list the files to be included in the tar file.
1798 These files must all exist. Any files which don't exist or can't be
1799 read are silently ignored.
1800  
1801 If the archive creation fails for any reason, C<create_archive> will
1802 return false. Please use the C<error> method to find the cause of the
1803 failure.
1804  
1805 Note that this method does not write C<on the fly> as it were; it
1806 still reads all the files into memory before writing out the archive.
1807 Consult the FAQ below if this is a problem.
1808  
1809 =cut
1810  
1811 9 ▶sub create_archive {
Subroutine create_archive: called CC=4 SCAR=14.1
Pod documented
1812 9 my $class = shift;
1813  
1814 9 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 9 9
1815 9 ▶ my $gzip = shift || 0;
Condition100%2/2
shift() || 0
l!l
63
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1816 9 my @files = @_;
1817  
1818 9 ▶ unless( @files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 0 9 9
1819 0 return $class->_error( qq[Cowardly refusing to create empty archive!] );
1820 }
1821  
1822 9 my $tar = $class->new;
1823 9 $tar->add_files( @files );
1824 9 return $tar->write( $file, $gzip );
1825 }
1826  
1827 =head2 Archive::Tar->iter( $filename, [ $compressed, {opt => $val} ] )
1828  
1829 Returns an iterator function that reads the tar file without loading
1830 it all in memory. Each time the function is called it will return the
1831 next file in the tarball. The files are returned as
1832 C<Archive::Tar::File> objects. The iterator function returns the
1833 empty list once it has exhausted the files contained.
1834  
1835 The second argument can be a hash reference with options, which are
1836 identical to the arguments passed to C<read()>.
1837  
1838 Example usage:
1839  
1840 my $next = Archive::Tar->iter( "example.tar.gz", 1, {filter => qr/\.pm$/} );
1841  
1842 while( my $f = $next->() ) {
1843 print $f->name, "\n";
1844  
1845 $f->extract or warn "Extraction failed";
1846  
1847 # ....
1848 }
1849  
1850 =cut
1851  
1852  
1853 15 ▶sub iter {
Subroutine iter: called CC=5 SCAR=16.4
Pod documented
1854 15 my $class = shift;
1855 15 my $filename = shift;
1856 15 ▶ return unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 15 15
1857 15 ▶ my $compressed = shift || 0;
Condition100%2/2
shift() || 0
l!l
114
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1858 15 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
114
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1859  
1860 ### get a handle to read from.
1861 15 ▶ my $handle = $class->_get_handle(
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $class->_get_handle($filename, $compressed, sub { package Archive::Tar::Constant; use warnings; use strict; shift() ? 'rb' : 'r';}->(1)) or return 15 0 15
1862 $filename,
1863 $compressed,
1864 READ_ONLY->( ZLIB )
1865 ) or return;
1866  
1867 15 my @data;
1868 15 my $CONSTRUCT_ARGS = [ $filename, $compressed, $opts ];
1869 35 ▶ return sub {
Subroutine __ANON__: called CC=10 SCAR=24.2
1870 35 ▶ return shift(@data) if @data; # more than one file returned?
Branch: 1 branch
BranchTrueFalseTotal
if @data 2 33 35
1871 33 ▶ return unless $handle; # handle exhausted?
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 0 33 33
1872  
1873 ### read data, should only return file
1874 33 my $tarfile = $class->_read_tar($handle, { %$opts, limit => 1 });
1875 33 ▶ @data = @$tarfile if ref $tarfile && ref $tarfile eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $tarfile and ref $tarfile eq "ARRAY" 33 0 33
Condition33%1/3
ref $tarfile and ref $tarfile eq "ARRAY"
!ll&&!rl&&r
0033
MC/DC0%0/2
ref $tarfile and ref $tarfile eq "ARRAY"
ref $tarfile ref $tarfile eq "ARRAY"
Truth table33%1/3
A and B
A= ref $tarfileB= ref $tarfile eq "ARRAY"
ABresult
0X0
100
111
1876 33 ▶ if($Archive::Tar::RESOLVE_SYMLINK!~/none/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($Archive::Tar::RESOLVE_SYMLINK =~ /none/) 33 0 33
1877 33 foreach(@data){
1878 #may refine this heuristic for ON_UNIX?
1879 20 ▶ if($_->linkname){
Branch: 1 branch
BranchTrueFalseTotal
if ($_->linkname) 1 19 20
1880 #is there a better slot to store/share it ?
1881 1 $_->{'_archive'} = $CONSTRUCT_ARGS;
1882 }
1883 }
1884 }
1885  
1886 ### return one piece of data
1887 33 ▶ return shift(@data) if @data;
Branch: 1 branch
BranchTrueFalseTotal
if @data 18 15 33
1888  
1889 ### data is exhausted, free the filehandle
1890 15 undef $handle;
1891 15 ▶ if(@$CONSTRUCT_ARGS == 4){
Branch: 1 branch
BranchTrueFalseTotal
if (@$CONSTRUCT_ARGS == 4) 0 15 15
1892 #free archive in memory
1893 0 undef $CONSTRUCT_ARGS->[-1];
1894 }
1895 15 return;
1896 15 };
1897 }
1898  
1899 =head2 Archive::Tar->list_archive($file, $compressed, [\@properties])
1900  
1901 Returns a list of the names of all the files in the archive. The
1902 first argument can either be the name of the tar file to list or a
1903 reference to an open file handle (e.g. a GLOB reference).
1904  
1905 If C<list_archive()> is passed an array reference as its third
1906 argument it returns a list of hash references containing the requested
1907 properties of each file. The following list of properties is
1908 supported: full_path, name, size, mtime (last modified date), mode,
1909 uid, gid, linkname, uname, gname, devmajor, devminor, prefix, type.
1910  
1911 See C<Archive::Tar::File> for details about supported properties.
1912  
1913 Passing an array reference containing only one element, 'name', is
1914 special cased to return a list of names rather than a list of hash
1915 references.
1916  
1917 =cut
1918  
1919 3 ▶sub list_archive {
Subroutine list_archive: called CC=4 SCAR=14.3
Pod documented
1920 3 my $class = shift;
1921 3 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 3 3
1922 3 ▶ my $gzip = shift || 0;
Condition50%1/2
shift() || 0
l!l
03
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1923  
1924 3 my $tar = $class->new($file, $gzip);
1925 3 ▶ return unless $tar;
Branch: 1 branch
BranchTrueFalseTotal
unless $tar 0 3 3
1926  
1927 3 return $tar->list_files( @_ );
1928 }
1929  
1930 =head2 Archive::Tar->extract_archive($file, $compressed)
1931  
1932 Extracts the contents of the tar file. The first argument can either
1933 be the name of the tar file to create or a reference to an open file
1934 handle (e.g. a GLOB reference). All relative paths in the tar file will
1935 be created underneath the current working directory.
1936  
1937 C<extract_archive> will return a list of files it extracted.
1938 If the archive extraction fails for any reason, C<extract_archive>
1939 will return false. Please use the C<error> method to find the cause
1940 of the failure.
1941  
1942 =cut
1943  
1944 9 ▶sub extract_archive {
Subroutine extract_archive: called CC=4 SCAR=14.0
Pod documented
1945 9 my $class = shift;
1946 9 ▶ my $file = shift; return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 9 9
1947 9 ▶ my $gzip = shift || 0;
Condition100%2/2
shift() || 0
l!l
63
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1948  
1949 9 ▶ my $tar = $class->new( ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $tar = $class->new or return 9 0 9
1950  
1951 9 return $tar->read( $file, $gzip, { extract => 1 } );
1952 }
1953  
1954 =head2 $bool = Archive::Tar->has_io_string
1955  
1956 Returns true if we currently have C<IO::String> support loaded.
1957  
1958 Either C<IO::String> or C<perlio> support is needed to support writing
1959 stringified archives. Currently, C<perlio> is the preferred method, if
1960 available.
1961  
1962 See the C<GLOBAL VARIABLES> section to see how to change this preference.
1963  
1964 =cut
1965  
1966 0 ▶sub has_io_string { return $HAS_IO_STRING; }
Subroutine has_io_string: not called CC=1 SCAR=6.9
Pod documented
1967  
1968 =head2 $bool = Archive::Tar->has_perlio
1969  
1970 Returns true if we currently have C<perlio> support loaded.
1971  
1972 This requires C<perl-5.8> or higher, compiled with C<perlio>
1973  
1974 Either C<IO::String> or C<perlio> support is needed to support writing
1975 stringified archives. Currently, C<perlio> is the preferred method, if
1976 available.
1977  
1978 See the C<GLOBAL VARIABLES> section to see how to change this preference.
1979  
1980 =cut
1981  
1982 0 ▶sub has_perlio { return $HAS_PERLIO; }
Subroutine has_perlio: not called CC=1 SCAR=6.9
Pod documented
1983  
1984 =head2 $bool = Archive::Tar->has_zlib_support
1985  
1986 Returns true if C<Archive::Tar> can extract C<zlib> compressed archives
1987  
1988 =cut
1989  
1990 6 ▶sub has_zlib_support { return ZLIB }
Subroutine has_zlib_support: called CC=1 SCAR=0.0
Pod documented
1991  
1992 =head2 $bool = Archive::Tar->has_bzip2_support
1993  
1994 Returns true if C<Archive::Tar> can extract C<bzip2> compressed archives
1995  
1996 =cut
1997  
1998 6 ▶sub has_bzip2_support { return BZIP }
Subroutine has_bzip2_support: called CC=1 SCAR=0.0
Pod documented
1999  
2000 =head2 $bool = Archive::Tar->has_xz_support
2001  
2002 Returns true if C<Archive::Tar> can extract C<xz> compressed archives
2003  
2004 =cut
2005  
2006 5 ▶sub has_xz_support { return XZ }
Subroutine has_xz_support: called CC=1 SCAR=0.0
Pod documented
2007  
2008 =head2 Archive::Tar->can_handle_compressed_files
2009  
2010 A simple checking routine, which will return true if C<Archive::Tar>
2011 is able to uncompress compressed archives on the fly with C<IO::Zlib>,
2012 C<IO::Compress::Bzip2> and C<IO::Compress::Xz> or false if not both are installed.
2013  
2014 You can use this as a shortcut to determine whether C<Archive::Tar>
2015 will do what you think before passing compressed archives to its
2016 C<read> method.
2017  
2018 =cut
2019  
2020 0 ▶sub can_handle_compressed_files { return ZLIB && BZIP ? 1 : 0 }
Subroutine can_handle_compressed_files: not called CC=1 SCAR=6.9
Pod documented
2021  
2022 0 ▶sub no_string_support {
Subroutine no_string_support: not called CC=1 SCAR=6.9
Pod undocumented
2023 0 croak("You have to install IO::String to support writing archives to strings");
2024 }
2025  
2026 4 ▶sub _symlinks_resolver{
Subroutine _symlinks_resolver: called CC=9 SCAR=23.3
2027 4 my ($src, $trg, $strict) = @_;
2028 4 my @src = split /[\/\\]/, $src;
2029 4 my @trg = split /[\/\\]/, $trg;
2030 4 pop @src; #strip out current object name
2031 4 ▶ if(@trg and $trg[0] eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if (@trg and $trg[0] eq "") 0 4 4
Condition33%1/3
@trg and $trg[0] eq ""
!ll&&!rl&&r
040
MC/DC0%0/2
@trg and $trg[0] eq ""
@trg $trg[0] eq ""
Truth table33%1/3
A and B
A= @trgB= $trg[0] eq ""
ABresult
0X0
100
111
2032 0 shift @trg;
2033 #restart path from scratch
2034 0 @src = ( );
2035 }
2036 4 foreach my $part ( @trg ){
2037 6 ▶ next if $part eq '.'; #ignore current
Branch: 1 branch
BranchTrueFalseTotal
if $part eq "." 0 6 6
2038 6 ▶ if($part eq '..'){
Branch: 1 branch
BranchTrueFalseTotal
if ($part eq '..') { } 3 3 6
2039 #got to parent
2040 3 ▶ return if $strict && !@src;
Branch: 1 branch
BranchTrueFalseTotal
if $strict and not @src 1 2 3
Condition66%2/3
$strict and not @src
!ll&&!rl&&r
021
MC/DC50%1/2
$strict and not @src
$strict not @src
Truth table66%2/3
A and not(B)
A= $strictB= not @src
ABresult
0X0
100
111
2041 2 pop @src;
2042 }
2043 else{
2044 #append it
2045 3 push @src, $part;
2046 }
2047 }
2048 3 my $path = join('/', @src);
2049 3 ▶ warn "_symlinks_resolver('$src','$trg') = $path" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Archive::Tar::DEBUG 0 3 3
2050 3 return $path;
2051 }
2052  
2053 17 1;
2054  
2055 __END__
↑ Summary bin/ptargrep »