lib/Archive/Tar/File.pm

filter: Statementstmt 94.6% 194 / 205 Branchbran 63.1% 53 / 84 Conditioncond 64.5% 20 / 31 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 100% 39 / 39 Podpod 95.6% 22 / 23 totaltotal 83.3% 335 / 402 CC 54 SCAR 20.8
CC
54
Cov
83%
_filetype
35.4
_new_from_chunk
11.3
_new_from_file
9.6
CRAP 8.0
« bin/ptargrep ↑ Summary lib/Archive/Tar/Constant.pm »
1 package Archive::Tar::File;
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use File::Spec::Unix ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 ▶use Archive::Tar::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 18 ▶use vars qw[@ISA $VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 #@ISA = qw[Archive::Tar];
14 18 $VERSION = '3.14';
15  
16 ### set value to 1 to oct() it during the unpack ###
17  
18 18 my $tmpl = [
19 name => 0, # string A100
20 mode => 1, # octal A8
21 uid => 1, # octal A8
22 gid => 1, # octal A8
23 size => 0, # octal # cdrake - not *always* octal.. A12
24 mtime => 1, # octal A12
25 chksum => 1, # octal A8
26 type => 0, # character A1
27 linkname => 0, # string A100
28 magic => 0, # string A6
29 version => 0, # 2 bytes A2
30 uname => 0, # string A32
31 gname => 0, # string A32
32 devmajor => 1, # octal A8
33 devminor => 1, # octal A8
34 prefix => 0, # A155 x 12
35  
36 ### end UNPACK items ###
37 raw => 0, # the raw data chunk
38 data => 0, # the data associated with the file --
39 # This might be very memory intensive
40 ];
41  
42 ### install get/set accessors for this object.
43 18 ▶for ( my $i=0; $i<scalar @$tmpl ; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$tmpl) 324 18 342
44 324 my $key = $tmpl->[$i];
45 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 324 ▶ *{__PACKAGE__."::$key"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
47 34285 my $self = shift;
48 34285 ▶ $self->{$key} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 859 33426 34285
49  
50 ### just in case the key is not there or undef or something ###
51 34285 { local $^W = 0;
52 34285 return $self->{$key};
53 }
54 }
55 324 }
56  
57 =head1 NAME
58  
59 Archive::Tar::File - a subclass for in-memory extracted file from Archive::Tar
60  
61 =head1 SYNOPSIS
62  
63 my @items = $tar->get_files;
64  
65 print $_->name, ' ', $_->size, "\n" for @items;
66  
67 print $object->get_content;
68 $object->replace_content('new content');
69  
70 $object->rename( 'new/full/path/to/file.c' );
71  
72 =head1 DESCRIPTION
73  
74 Archive::Tar::File provides a neat little object layer for in-memory
75 extracted files. It's mostly used internally in Archive::Tar to tidy
76 up the code, but there's no reason users shouldn't use this API as
77 well.
78  
79 =head2 Accessors
80  
81 A lot of the methods in this package are accessors to the various
82 fields in the tar header:
83  
84 =over 4
85  
86 =item name
87  
88 The file's name
89  
90 =item mode
91  
92 The file's mode
93  
94 =item uid
95  
96 The user id owning the file
97  
98 =item gid
99  
100 The group id owning the file
101  
102 =item size
103  
104 File size in bytes
105  
106 =item mtime
107  
108 Modification time. Adjusted to mac-time on MacOS if required
109  
110 =item chksum
111  
112 Checksum field for the tar header
113  
114 =item type
115  
116 File type -- numeric, but comparable to exported constants -- see
117 Archive::Tar's documentation
118  
119 =item linkname
120  
121 If the file is a symlink, the file it's pointing to
122  
123 =item magic
124  
125 Tar magic string -- not useful for most users
126  
127 =item version
128  
129 Tar version string -- not useful for most users
130  
131 =item uname
132  
133 The user name that owns the file
134  
135 =item gname
136  
137 The group name that owns the file
138  
139 =item devmajor
140  
141 Device major number in case of a special file
142  
143 =item devminor
144  
145 Device minor number in case of a special file
146  
147 =item prefix
148  
149 Any directory to prefix to the extraction path, if any
150  
151 =item raw
152  
153 Raw tar header -- not useful for most users
154  
155 =back
156  
157 =head1 Methods
158  
159 =head2 Archive::Tar::File->new( file => $path )
160  
161 Returns a new Archive::Tar::File object from an existing file.
162  
163 Returns undef on failure.
164  
165 =head2 Archive::Tar::File->new( data => $path, $data, $opt )
166  
167 Returns a new Archive::Tar::File object from data.
168  
169 C<$path> defines the file name (which need not exist), C<$data> the
170 file contents, and C<$opt> is a reference to a hash of attributes
171 which may be used to override the default attributes (fields in the
172 tar header), which are described above in the Accessors section.
173  
174 Returns undef on failure.
175  
176 =head2 Archive::Tar::File->new( chunk => $chunk )
177  
178 Returns a new Archive::Tar::File object from a raw 512-byte tar
179 archive chunk.
180  
181 Returns undef on failure.
182  
183 =cut
184  
185 356 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
186 356 my $class = shift;
187 356 my $what = shift;
188  
189 356 ▶ my $obj = ($what eq 'chunk') ? __PACKAGE__->_new_from_chunk( @_ ) :
Branch: 3 branches
BranchTrueFalseTotal
$what eq 'chunk' ? : 297 59 356
$what eq 'file' ? : 22 37 59
$what eq 'data' ? : 37 0 37
190 ($what eq 'file' ) ? __PACKAGE__->_new_from_file( @_ ) :
191 ($what eq 'data' ) ? __PACKAGE__->_new_from_data( @_ ) :
192 undef;
193  
194 356 return $obj;
195 }
196  
197 ### copies the data, creates a clone ###
198 128 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
199 128 my $self = shift;
200 128 return bless { %$self }, ref $self;
201 }
202  
203 297 ▶sub _new_from_chunk {
Subroutine _new_from_chunk: called CC=10 SCAR=24.3
204 297 my $class = shift;
205 297 ▶ my $chunk = shift or return; # 512 bytes of tar header
Branch: 1 branch
BranchTrueFalseTotal
my $chunk = shift() or return 297 0 297
206 297 my %hash = @_;
207  
208 ### filter any arguments on defined-ness of values.
209 ### this allows overriding from what the tar-header is saying
210 ### about this tar-entry. Particularly useful for @LongLink files
211 297 my %args = map { $_ => $hash{$_} } grep { defined $hash{$_} } keys %hash;
212  
213 ### makes it start at 0 actually... :) ###
214 297 my $i = -1;
215 my %entry = map {
216 297 my ($s,$v)=($tmpl->[++$i],$tmpl->[++$i]); # cdrake
217 4752 ▶ ($_)=($_=~/^([^\0]*)/) unless($s eq 'size'); # cdrake
Branch: 1 branch
BranchTrueFalseTotal
unless $s eq "size" 4455 297 4752
218 4752 ▶ $s=> $v ? oct $_ : $_ # cdrake
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 2079 2673 4752
219 # $tmpl->[++$i] => $tmpl->[++$i] ? oct $_ : $_ # removed by cdrake - mucks up binary sizes >8gb
220 } unpack( UNPACK, $chunk ); # cdrake
221 # } map { /^([^\0]*)/ } unpack( UNPACK, $chunk ); # old - replaced now by cdrake
222  
223  
224 297 ▶ if(substr($entry{'size'}, 0, 1) eq "\x80") { # binary size extension for files >8gigs (> octal 77777777777777) # cdrake
Branch: 1 branch
BranchTrueFalseTotal
if (substr($entry{'size'}, 0, 1) eq "\200") { } 0 297 297
225 0 my @sz=unpack("aCSNN",$entry{'size'}); $entry{'size'}=$sz[4]+(2**32)*$sz[3]+$sz[2]*(2**64); # Use the low 80 bits (should use the upper 15 as well, but as at year 2011, that seems unlikely to ever be needed - the numbers are just too big...) # cdrake
226 } else { # cdrake
227 297 ($entry{'size'})=($entry{'size'}=~/^([^\0]*)/); $entry{'size'}=oct $entry{'size'}; # cdrake
228 } # cdrake
229  
230  
231 297 my $obj = bless { %entry, %args }, $class;
232  
233 ### magic is a filetype string.. it should have something like 'ustar' or
234 ### something similar... if the chunk is garbage, skip it
235 297 ▶ return unless $obj->magic !~ /\W/;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->magic =~ /\W/ 0 297 297
236  
237 ### store the original chunk ###
238 297 $obj->raw( $chunk );
239  
240 297 ▶ $obj->type(FILE) if ( (!length $obj->type) or ($obj->type =~ /\W/) );
Branch: 1 branch
BranchTrueFalseTotal
if not length $obj->type or $obj->type =~ /\W/ 0 297 297
Condition33%1/3
not length $obj->type or $obj->type =~ /\W/
l!l&&r!l&&!r
00297
MC/DC0%0/2
not length $obj->type or $obj->type =~ /\W/
not length $obj->type $obj->type =~ /\W/
Truth table33%1/3
not(A) or B
A= not length $obj->typeB= $obj->type =~ /\W/
ABresult
1X1
011
000
241 297 ▶ $obj->type(DIR) if ( ($obj->is_file) && ($obj->name =~ m|/$|) );
Branch: 1 branch
BranchTrueFalseTotal
if $obj->is_file and $obj->name =~ m[/$] 0 297 297
Condition66%2/3
$obj->is_file and $obj->name =~ m[/$]
!ll&&!rl&&r
782190
MC/DC0%0/2
$obj->is_file and $obj->name =~ m[/$]
$obj->is_file $obj->name =~ m[/$]
Truth table66%2/3
A and B
A= $obj->is_fileB= $obj->name =~ m[/$]
ABresult
0X0
100
111
242  
243  
244 297 return $obj;
245  
246 }
247  
248 22 ▶sub _new_from_file {
Subroutine _new_from_file: called CC=9 SCAR=22.6
249 22 my $class = shift;
250 22 my $path = shift;
251  
252 ### path has to at least exist
253 22 ▶ return unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 22 22
254  
255 22 my $type = __PACKAGE__->_filetype($path);
256 22 my $data = '';
257  
258 READ: {
259 22 ▶ unless ($type == DIR ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type == 5) 21 1 22
260 21 my $fh = IO::File->new;
261  
262 21 ▶ unless( $fh->open($path, 'r') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open($path, "r")) 0 21 21
263 ### dangling symlinks are fine, stop reading but continue
264 ### creating the object
265 0 ▶ last READ if $type == SYMLINK;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 2 0 0 0
266  
267 ### otherwise, return from this function --
268 ### anything that's *not* a symlink should be
269 ### resolvable
270 0 return;
271 }
272  
273 ### binmode needed to read files properly on win32 ###
274 21 binmode $fh;
275 21 $data = do { local $/; <$fh> };
276 21 close $fh;
277 }
278 }
279  
280 22 my @items = qw[mode uid gid size mtime];
281 22 my %hash = map { shift(@items), $_ } (lstat $path)[2,4,5,7,9];
282  
283 22 if (ON_VMS) {
284 ### VMS has two UID modes, traditional and POSIX. Normally POSIX is
285 ### not used. We currently do not have an easy way to see if we are in
286 ### POSIX mode. In traditional mode, the UID is actually the VMS UIC.
287 ### The VMS UIC has the upper 16 bits is the GID, which in many cases
288 ### the VMS UIC will be larger than 209715, the largest that TAR can
289 ### handle. So for now, assume it is traditional if the UID is larger
290 ### than 0x10000.
291  
292 if ($hash{uid} > 0x10000) {
293 $hash{uid} = $hash{uid} & 0xFFFF;
294 }
295  
296 ### The file length from stat() is the physical length of the file
297 ### However the amount of data read in may be more for some file types.
298 ### Fixed length files are read past the logical EOF to end of the block
299 ### containing. Other file types get expanded on read because record
300 ### delimiters are added.
301  
302 my $data_len = length $data;
303 $hash{size} = $data_len if $hash{size} < $data_len;
304  
305 }
306 ### you *must* set size == 0 on symlinks, or the next entry will be
307 ### though of as the contents of the symlink, which is wrong.
308 ### this fixes bug #7937
309 22 ▶ $hash{size} = 0 if ($type == DIR or $type == SYMLINK);
Branch: 1 branch
BranchTrueFalseTotal
if $type == 5 or $type == 2 1 21 22
Condition66%2/3
$type == 5 or $type == 2
l!l&&r!l&&!r
1021
MC/DC50%1/2
$type == 5 or $type == 2
$type == 5 $type == 2
Truth table66%2/3
A or B
A= $type == 5B= $type == 2
ABresult
1X1
011
000
310 22 $hash{mtime} -= TIME_OFFSET;
311  
312 ### strip the high bits off the mode, which we don't need to store
313 22 $hash{mode} = STRIP_MODE->( $hash{mode} );
314  
315  
316 ### probably requires some file path munging here ... ###
317 ### name and prefix are set later
318 my $obj = {
319 %hash,
320 name => '',
321 chksum => CHECK_SUM,
322 type => $type,
323 linkname => ($type == SYMLINK and CAN_READLINK)
324 ? readlink $path
325 : '',
326 magic => MAGIC,
327 version => TAR_VERSION,
328 uname => UNAME->( $hash{uid} ),
329 22 ▶ gname => GNAME->( $hash{gid} ),
Branch: 1 branch
BranchTrueFalseTotal
$type == 2 && 1 ? : 0 22 22
Condition50%1/2
$type == 2 and !!1
!ll
220
MC/DC0%0/1
$type == 2 and !!1
$type == 2
Truth table50%1/2
A
A= $type == 2
Aresult
00
11
330 devmajor => 0, # not handled
331 devminor => 0, # not handled
332 prefix => '',
333 data => $data,
334 };
335  
336 22 bless $obj, $class;
337  
338 ### fix up the prefix and file from the path
339 22 my($prefix,$file) = $obj->_prefix_and_file( $path );
340 22 $obj->prefix( $prefix );
341 22 $obj->name( $file );
342  
343 22 return $obj;
344 }
345  
346 37 ▶sub _new_from_data {
Subroutine _new_from_data: called CC=7 SCAR=19.6
347 37 my $class = shift;
348 37 ▶ my $path = shift; return unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 37 37
349 37 ▶ my $data = shift; return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 36 37
350 36 my $opt = shift;
351  
352 36 my $obj = {
353 data => $data,
354 name => '',
355 mode => MODE,
356 uid => UID,
357 gid => GID,
358 size => length $data,
359 mtime => time - TIME_OFFSET,
360 chksum => CHECK_SUM,
361 type => FILE,
362 linkname => '',
363 magic => MAGIC,
364 version => TAR_VERSION,
365 uname => UNAME->( UID ),
366 gname => GNAME->( GID ),
367 devminor => 0,
368 devmajor => 0,
369 prefix => '',
370 };
371  
372 ### overwrite with user options, if provided ###
373 36 ▶ if( $opt and ref $opt eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt and ref $opt eq "HASH") 24 12 36
Condition66%2/3
$opt and ref $opt eq "HASH"
!ll&&!rl&&r
12024
MC/DC50%1/2
$opt and ref $opt eq "HASH"
$opt ref $opt eq "HASH"
Truth table66%2/3
A and B
A= $optB= ref $opt eq "HASH"
ABresult
0X0
100
111
374 24 for my $key ( keys %$opt ) {
375  
376 ### don't write bogus options ###
377 35 ▶ next unless exists $obj->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{$key} 0 35 35
378 35 $obj->{$key} = $opt->{$key};
379 }
380 }
381  
382 36 bless $obj, $class;
383  
384 ### fix up the prefix and file from the path
385 36 my($prefix,$file) = $obj->_prefix_and_file( $path );
386 36 $obj->prefix( $prefix );
387 36 $obj->name( $file );
388  
389 36 return $obj;
390 }
391  
392 161 ▶sub _prefix_and_file {
Subroutine _prefix_and_file: called CC=3 SCAR=11.0
393 161 my $self = shift;
394 161 my $path = shift;
395  
396 161 my ($vol, $dirs, $file) = File::Spec->splitpath( $path, $self->is_dir );
397 161 my @dirs = File::Spec->splitdir( File::Spec->canonpath($dirs) );
398  
399 ### if it's a directory, then $file might be empty
400 161 ▶ $file = pop @dirs if $self->is_dir and not length $file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_dir and not length $file 22 139 161
Condition66%2/3
$self->is_dir and not length $file
!ll&&!rl&&r
139022
MC/DC50%1/2
$self->is_dir and not length $file
$self->is_dir not length $file
Truth table66%2/3
A and not(B)
A= $self->is_dirB= not length $file
ABresult
0X0
100
111
401  
402 ### splitting ../ gives you the relative path in native syntax
403 ### Remove the root (000000) directory
404 ### The volume from splitpath will also be in native syntax
405 161 if (ON_VMS) {
406 map { $_ = '..' if $_ eq '-'; $_ = '' if $_ eq '000000' } @dirs;
407 if (length($vol)) {
408 $vol = VMS::Filespec::unixify($vol);
409 unshift @dirs, $vol;
410 }
411 }
412  
413 161 my $prefix = File::Spec::Unix->catdir(@dirs);
414 161 return( $prefix, $file );
415 }
416  
417 22 ▶sub _filetype {
Subroutine _filetype: called CC=10 SCAR=35.7
418 22 my $self = shift;
419 22 my $file = shift;
420  
421 22 ▶ return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 22 22
422  
423 22 ▶ return SYMLINK if (-l $file); # Symlink
Branch: 1 branch
BranchTrueFalseTotal
if -l $file 0 22 22
424  
425 22 ▶ return FILE if (-f _); # Plain file
Branch: 1 branch
BranchTrueFalseTotal
if -f _ 21 1 22
426  
427 1 ▶ return DIR if (-d _); # Directory
Branch: 1 branch
BranchTrueFalseTotal
if -d _ 1 0 1
428  
429 0 ▶ return FIFO if (-p _); # Named pipe
Branch: 1 branch
BranchTrueFalseTotal
if -p _ 0 0 0
430  
431 0 ▶ return SOCKET if (-S _); # Socket
Branch: 1 branch
BranchTrueFalseTotal
if -S _ 0 0 0
432  
433 0 ▶ return BLOCKDEV if (-b _); # Block special
Branch: 1 branch
BranchTrueFalseTotal
if -b _ 0 0 0
434  
435 0 ▶ return CHARDEV if (-c _); # Character special
Branch: 1 branch
BranchTrueFalseTotal
if -c _ 0 0 0
436  
437 ### shouldn't happen, this is when making archives, not reading ###
438 0 ▶ return LONGLINK if ( $file eq LONGLINK_NAME );
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "././\@LongLink" 0 0 0
439  
440 0 return UNKNOWN; # Something else (like what?)
441  
442 }
443  
444 ### this method 'downgrades' a file to plain file -- this is used for
445 ### symlinks when FOLLOW_SYMLINKS is true.
446 1 ▶sub _downgrade_to_plainfile {
Subroutine _downgrade_to_plainfile: called CC=1 SCAR=0.0
447 1 my $entry = shift;
448 1 $entry->type( FILE );
449 1 $entry->mode( MODE );
450 1 $entry->linkname('');
451  
452 1 return 1;
453 }
454  
455 =head2 $bool = $file->extract( [ $alternative_name ] )
456  
457 Extract this object, optionally to an alternative name.
458  
459 See C<< Archive::Tar->extract_file >> for details.
460  
461 Returns true on success and false on failure.
462  
463 =cut
464  
465 3 ▶sub extract {
Subroutine extract: called CC=1 SCAR=0.0
Pod documented
466 3 my $self = shift;
467  
468 3 local $Carp::CarpLevel += 1;
469  
470 ### avoid circular use, so only require;
471 3 require Archive::Tar;
472 3 return Archive::Tar->_extract_file( $self, @_ );
473 }
474  
475 =head2 $path = $file->full_path
476  
477 Returns the full path from the tar header; this is basically a
478 concatenation of the C<prefix> and C<name> fields.
479  
480 =cut
481  
482 1451 ▶sub full_path {
Subroutine full_path: called CC=4 SCAR=13.9
Pod documented
483 1451 my $self = shift;
484  
485 ### if prefix field is empty
486 1451 ▶ return $self->name unless defined $self->prefix and length $self->prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->prefix and length $self->prefix 1223 228 1451
Condition66%2/3
defined $self->prefix and length $self->prefix
!ll&&!rl&&r
01223228
MC/DC50%1/2
defined $self->prefix and length $self->prefix
defined $self->prefix length $self->prefix
Truth table66%2/3
A and B
A= defined $self->prefixB= length $self->prefix
ABresult
0X0
100
111
487  
488 ### or otherwise, catfile'd
489 228 my $path = File::Spec::Unix->catfile( $self->prefix, $self->name );
490 228 ▶ $path .= "/" if $self->name =~ m{/$}; # Re-add trailing slash if necessary, as catfile() strips them off.
Branch: 1 branch
BranchTrueFalseTotal
if $self->name =~ m[/$] 2 226 228
491 228 return $path;
492 }
493  
494  
495 =head2 $bool = $file->validate
496  
497 Done by Archive::Tar internally when reading the tar file:
498 validate the header against the checksum to ensure integer tar file.
499  
500 Returns true on success, false on failure
501  
502 =cut
503  
504 220 ▶sub validate {
Subroutine validate: called CC=3 SCAR=11.3
Pod documented
505 220 my $self = shift;
506  
507 220 my $raw = $self->raw;
508  
509 ### don't know why this one is different from the one we /write/ ###
510 220 substr ($raw, 148, 8) = " ";
511  
512 ### bug #43513: [PATCH] Accept wrong checksums from SunOS and HP-UX tar
513 ### like GNU tar does. See here for details:
514 ### http://www.gnu.org/software/tar/manual/tar.html#SEC139
515 ### so we do both a signed AND unsigned validate. if one succeeds, that's
516 ### good enough
517 220 ▶ return ( (unpack ("%16C*", $raw) == $self->chksum)
Branch: 1 branch
BranchTrueFalseTotal
unpack('%16C*', $raw) == $self->chksum || unpack('%16c*', $raw) == $self->chksum ? : 220 0 220
Condition66%2/3
unpack("%16C*", $raw) == $self->chksum or unpack("%16c*", $raw) == $self->chksum
l!l&&r!l&&!r
21820
MC/DC0%0/2
unpack("%16C*", $raw) == $self->chksum or unpack("%16c*", $raw) == $self->chksum
unpack("%16C*", $raw) == $self->chksum unpack("%16c*", $raw) == $self->chksum
Truth table66%2/3
A or B
A= unpack("%16C*", $raw) == $self->chksumB= unpack("%16c*", $raw) == $self->chksum
ABresult
1X1
011
000
518 or (unpack ("%16c*", $raw) == $self->chksum)) ? 1 : 0;
519 }
520  
521 =head2 $bool = $file->has_content
522  
523 Returns a boolean to indicate whether the current object has content.
524 Some special files like directories and so on never will have any
525 content. This method is mainly to make sure you don't get warnings
526 for using uninitialized values when looking at an object's content.
527  
528 =cut
529  
530 113 ▶sub has_content {
Subroutine has_content: called CC=3 SCAR=11.0
Pod documented
531 113 my $self = shift;
532 113 ▶ return defined $self->data() && length $self->data() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->data && length $self->data ? : 88 25 113
Condition100%3/3
defined $self->data and length $self->data
!ll&&!rl&&r
17888
MC/DC100%2/2
defined $self->data and length $self->data
defined $self->data length $self->data
Truth table100%3/3
A and B
A= defined $self->dataB= length $self->data
ABresult
0X0
100
111
533 }
534  
535 =head2 $content = $file->get_content
536  
537 Returns the current content for the in-memory file
538  
539 =cut
540  
541 87 ▶sub get_content {
Subroutine get_content: called CC=1 SCAR=0.0
Pod documented
542 87 my $self = shift;
543 87 $self->data( );
544 }
545  
546 =head2 $cref = $file->get_content_by_ref
547  
548 Returns the current content for the in-memory file as a scalar
549 reference. Normal users won't need this, but it will save memory if
550 you are dealing with very large data files in your tar archive, since
551 it will pass the contents by reference, rather than make a copy of it
552 first.
553  
554 =cut
555  
556 458 ▶sub get_content_by_ref {
Subroutine get_content_by_ref: called CC=1 SCAR=0.0
Pod documented
557 458 my $self = shift;
558  
559 458 return \$self->{data};
560 }
561  
562 =head2 $bool = $file->replace_content( $content )
563  
564 Replace the current content of the file with the new content. This
565 only affects the in-memory archive, not the on-disk version until
566 you write it.
567  
568 Returns true on success, false on failure.
569  
570 =cut
571  
572 11 ▶sub replace_content {
Subroutine replace_content: called CC=2 SCAR=6.9
Pod documented
573 11 my $self = shift;
574 11 ▶ my $data = shift || '';
Condition100%2/2
shift() || ''
l!l
83
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
575  
576 11 $self->data( $data );
577 11 $self->size( length $data );
578 11 return 1;
579 }
580  
581 =head2 $bool = $file->rename( $new_name )
582  
583 Rename the current file to $new_name.
584  
585 Note that you must specify a Unix path for $new_name, since per tar
586 standard, all files in the archive must be Unix paths.
587  
588 Returns true on success and false on failure.
589  
590 =cut
591  
592 12 ▶sub rename {
Subroutine rename: called CC=2 SCAR=7.0
Pod documented
593 12 my $self = shift;
594 12 my $path = shift;
595  
596 12 ▶ return unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 12 12
597  
598 12 my ($prefix,$file) = $self->_prefix_and_file( $path );
599  
600 12 $self->name( $file );
601 12 $self->prefix( $prefix );
602  
603 12 return 1;
604 }
605  
606 =head2 $bool = $file->chmod( $mode )
607  
608 Change mode of $file to $mode. The mode can be a string or a number
609 which is interpreted as octal whether or not a leading 0 is given.
610  
611 Returns true on success and false on failure.
612  
613 =cut
614  
615 1 ▶sub chmod {
Subroutine chmod: called CC=3 SCAR=11.8
Pod documented
616 1 my $self = shift;
617 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
618 1 $self->{mode} = oct($mode);
619 1 return 1;
620 }
621  
622 =head2 $bool = $file->chown( $user [, $group])
623  
624 Change owner of $file to $user. If a $group is given that is changed
625 as well. You can also pass a single parameter with a colon separating the
626 use and group as in 'root:wheel'.
627  
628 Returns true on success and false on failure.
629  
630 =cut
631  
632 12 ▶sub chown {
Subroutine chown: called CC=5 SCAR=16.3
Pod documented
633 12 my $self = shift;
634 12 my $uname = shift;
635 12 ▶ return unless defined $uname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uname 0 12 12
636 12 my $gname;
637 12 ▶ if (-1 != index($uname, ':')) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($uname, ':') != -1) { } 0 12 12
638 0 ($uname, $gname) = split(/:/, $uname);
639 } else {
640 12 ▶ $gname = shift if @_ > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 0 7 5 12
641 }
642  
643 12 $self->uname( $uname );
644 12 ▶ $self->gname( $gname ) if $gname;
Branch: 1 branch
BranchTrueFalseTotal
if $gname 6 6 12
645 12 return 1;
646 }
647  
648 =head1 Convenience methods
649  
650 To quickly check the type of a C<Archive::Tar::File> object, you can
651 use the following methods:
652  
653 =over 4
654  
655 =item $file->is_file
656  
657 Returns true if the file is of type C<file>
658  
659 =item $file->is_dir
660  
661 Returns true if the file is of type C<dir>
662  
663 =item $file->is_hardlink
664  
665 Returns true if the file is of type C<hardlink>
666  
667 =item $file->is_symlink
668  
669 Returns true if the file is of type C<symlink>
670  
671 =item $file->is_chardev
672  
673 Returns true if the file is of type C<chardev>
674  
675 =item $file->is_blockdev
676  
677 Returns true if the file is of type C<blockdev>
678  
679 =item $file->is_fifo
680  
681 Returns true if the file is of type C<fifo>
682  
683 =item $file->is_socket
684  
685 Returns true if the file is of type C<socket>
686  
687 =item $file->is_longlink
688  
689 Returns true if the file is of type C<LongLink>.
690 Should not happen after a successful C<read>.
691  
692 =item $file->is_label
693  
694 Returns true if the file is of type C<Label>.
695 Should not happen after a successful C<read>.
696  
697 =item $file->is_unknown
698  
699 Returns true if the file type is C<unknown>
700  
701 =back
702  
703 =cut
704  
705 #stupid perl5.5.3 needs to warn if it's not numeric
706 1247 ▶sub is_file { local $^W; FILE == $_[0]->type }
Subroutine is_file: called CC=1 SCAR=0.0
Pod documented
707 1021 ▶sub is_dir { local $^W; DIR == $_[0]->type }
Subroutine is_dir: called CC=1 SCAR=0.0
Pod documented
708 524 ▶sub is_hardlink { local $^W; HARDLINK == $_[0]->type }
Subroutine is_hardlink: called CC=1 SCAR=0.0
Pod documented
709 232 ▶sub is_symlink { local $^W; SYMLINK == $_[0]->type }
Subroutine is_symlink: called CC=1 SCAR=0.0
Pod documented
710 4 ▶sub is_chardev { local $^W; CHARDEV == $_[0]->type }
Subroutine is_chardev: called CC=1 SCAR=0.0
Pod documented
711 4 ▶sub is_blockdev { local $^W; BLOCKDEV == $_[0]->type }
Subroutine is_blockdev: called CC=1 SCAR=0.0
Pod documented
712 4 ▶sub is_fifo { local $^W; FIFO == $_[0]->type }
Subroutine is_fifo: called CC=1 SCAR=0.0
Pod documented
713 4 ▶sub is_socket { local $^W; SOCKET == $_[0]->type }
Subroutine is_socket: called CC=1 SCAR=0.0
Pod documented
714 281 ▶sub is_unknown { local $^W; UNKNOWN == $_[0]->type }
Subroutine is_unknown: called CC=1 SCAR=0.0
Pod documented
715 628 ▶sub is_longlink { local $^W; LONGLINK eq $_[0]->type }
Subroutine is_longlink: called CC=1 SCAR=0.0
Pod documented
716 310 ▶sub is_label { local $^W; LABEL eq $_[0]->type }
Subroutine is_label: called CC=1 SCAR=0.0
Pod documented
717  
718 18 1;
« bin/ptargrep ↑ Summary lib/Archive/Tar/Constant.pm »