1 ### the gnu tar specification:
2 ### http://www.gnu.org/software/tar/manual/tar.html
4 ### and the pax format spec, which tar derives from:
5 ### http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html
13 use Carp qw(carp croak);
15 use File::Spec::Unix ();
18 use Archive::Tar::File;
19 use Archive::Tar::Constant;
24 use vars qw[$DEBUG $error $VERSION $WARN $FOLLOW_SYMLINK $CHOWN $CHMOD
25 $DO_NOT_USE_PREFIX $HAS_PERLIO $HAS_IO_STRING $SAME_PERMISSIONS
26 $INSECURE_EXTRACT_MODE $ZERO_PAD_NUMBERS @ISA @EXPORT $RESOLVE_SYMLINK
30 @EXPORT = qw[ COMPRESS_GZIP COMPRESS_BZIP ];
37 $SAME_PERMISSIONS = $> == 0 ? 1 : 0;
38 $DO_NOT_USE_PREFIX = 0;
39 $INSECURE_EXTRACT_MODE = 0;
40 $ZERO_PAD_NUMBERS = 0;
41 $RESOLVE_SYMLINK = $ENV{'PERL5_AT_RESOLVE_SYMLINK'} || 'speed';
45 $HAS_PERLIO = $Config::Config{useperlio};
47 ### try and load IO::String anyway, so you can dynamically
48 ### switch between perlio and IO::String
49 $HAS_IO_STRING = eval {
58 Archive::Tar - module for manipulations of tar archives
63 my $tar = Archive::Tar->new;
65 $tar->read('origin.tgz');
68 $tar->add_files('file/foo.pl', 'docs/README');
69 $tar->add_data('file/baz.txt', 'This is the contents now');
71 $tar->rename('oldname', 'new/file/name');
72 $tar->chown('/', 'root');
73 $tar->chown('/', 'root:root');
74 $tar->chmod('/tmp', '1777');
76 $tar->write('files.tar'); # plain tar
77 $tar->write('files.tgz', COMPRESS_GZIP); # gzip compressed
78 $tar->write('files.tbz', COMPRESS_BZIP); # bzip2 compressed
82 Archive::Tar provides an object oriented mechanism for handling tar
83 files. It provides class methods for quick and easy files handling
84 while also allowing for the creation of tar file objects for custom
85 manipulation. If you have the IO::Zlib module installed,
86 Archive::Tar will also support compressed or gzipped tar files.
88 An object of class Archive::Tar represents a .tar(.gz) archive full
93 =head2 Archive::Tar->new( [$file, $compressed] )
95 Returns a new Tar object. If given any arguments, C<new()> calls the
96 C<read()> method automatically, passing on the arguments provided to
99 If C<new()> is invoked with arguments and the C<read()> method fails
100 for any reason, C<new()> returns undef.
109 ### install get/set accessors for this object.
110 for my $key ( keys %$tmpl ) {
112 *{__PACKAGE__."::$key"} = sub {
114 $self->{$key} = $_[0] if @_;
115 return $self->{$key};
121 $class = ref $class if ref $class;
123 ### copying $tmpl here since a shallow copy makes it use the
124 ### same aref, causing for files to remain in memory always.
125 my $obj = bless { _data => [ ], _file => 'Unknown', _error => '' }, $class;
128 unless ( $obj->read( @_ ) ) {
129 $obj->_error(qq[No data could be read from file]);
137 =head2 $tar->read ( $filename|$handle, [$compressed, {opt => 'val'}] )
139 Read the given tar file into memory.
140 The first argument can either be the name of a file or a reference to
141 an already open filehandle (or an IO::Zlib object if it's compressed)
143 The C<read> will I<replace> any previous content in C<$tar>!
145 The second argument may be considered optional, but remains for
146 backwards compatibility. Archive::Tar now looks at the file
147 magic to determine what class should be used to open the file
148 and will transparently Do The Right Thing.
150 Archive::Tar will warn if you try to pass a bzip2 compressed file and the
151 IO::Zlib / IO::Uncompress::Bunzip2 modules are not available and simply return.
153 Note that you can currently B<not> pass a C<gzip> compressed
154 filehandle, which is not opened with C<IO::Zlib>, a C<bzip2> compressed
155 filehandle, which is not opened with C<IO::Uncompress::Bunzip2>, nor a string
156 containing the full archive information (either compressed or
157 uncompressed). These are worth while features, but not currently
158 implemented. See the C<TODO> section.
160 The third argument can be a hash reference with options. Note that
161 all options are case-sensitive.
167 Do not read more than C<limit> files. This is useful if you have
168 very big archives, and are only interested in the first few files.
172 Can be set to a regular expression. Only files with names that match
173 the expression will be read.
177 Set to 1 and the md5sum of files will be returned (instead of file data)
178 my $iter = Archive::Tar->iter( $file, 1, {md5 => 1} );
179 while( my $f = $iter->() ) {
180 print $f->data . "\t" . $f->full_path . $/;
185 If set to true, immediately extract entries when reading them. This
186 gives you the same memory break as the C<extract_archive> function.
187 Note however that entries will not be read into memory, but written
188 straight to disk. This means no C<Archive::Tar::File> objects are
189 created for you to inspect.
193 All files are stored internally as C<Archive::Tar::File> objects.
194 Please consult the L<Archive::Tar::File> documentation for details.
196 Returns the number of files read in scalar context, and a list of
197 C<Archive::Tar::File> objects in list context.
204 my $gzip = shift || 0;
205 my $opts = shift || {};
207 unless( defined $file ) {
208 $self->_error( qq[No file to read from!] );
211 $self->_file( $file );
214 my $handle = $self->_get_handle($file, $gzip, READ_ONLY->( ZLIB ) )
217 my $data = $self->_read_tar( $handle, $opts ) or return;
219 $self->_data( $data );
221 return wantarray ? @$data : scalar @$data;
226 my $file = shift; return unless defined $file;
227 my $compress = shift || 0;
228 my $mode = shift || READ_ONLY->( ZLIB ); # default to read only
230 ### Check if file is a file handle or IO glob
232 return $file if eval{ *$file{IO} };
233 return $file if eval{ $file->isa(q{IO::Handle}) };
237 ### get a FH opened to the right class, so we can use it transparently
238 ### throughout the program
240 { ### reading magic only makes sense if we're opening a file for
241 ### reading. otherwise, just use what the user requested.
243 if( MODE_READ->($mode) ) {
244 open my $tmp, $file or do {
245 $self->_error( qq[Could not open '$file' for reading: $!] );
249 ### read the first 4 bites of the file to figure out which class to
250 ### use to open the file.
251 sysread( $tmp, $magic, 4 );
256 ### if you asked specifically for bzip compression, or if we're in
257 ### read mode and the magic numbers add up, use bzip
259 ($compress eq COMPRESS_BZIP) or
260 ( MODE_READ->($mode) and $magic =~ BZIP_MAGIC_NUM )
264 ### different reader/writer modules, different error vars... sigh
265 if( MODE_READ->($mode) ) {
266 $fh = IO::Uncompress::Bunzip2->new( $file ) or do {
267 $self->_error( qq[Could not read '$file': ] .
268 $IO::Uncompress::Bunzip2::Bunzip2Error
274 $fh = IO::Compress::Bzip2->new( $file ) or do {
275 $self->_error( qq[Could not write to '$file': ] .
276 $IO::Compress::Bzip2::Bzip2Error
283 ### if you asked for compression, if you wanted to read or the gzip
284 ### magic number is present (redundant with read)
286 $compress or MODE_READ->($mode) or $magic =~ GZIP_MAGIC_NUM
291 unless( $fh->open( $file, $mode ) ) {
292 $self->_error(qq[Could not create filehandle for '$file': $!]);
300 unless( $fh->open( $file, $mode ) ) {
301 $self->_error(qq[Could not create filehandle for '$file': $!]);
305 ### enable bin mode on tar archives
316 my $handle = shift or return;
317 my $opts = shift || {};
319 my $count = $opts->{limit} || 0;
320 my $filter = $opts->{filter};
321 my $md5 = $opts->{md5} || 0; # cdrake
322 my $filter_cb = $opts->{filter_cb};
323 my $extract = $opts->{extract} || 0;
325 ### set a cap on the amount of files to extract ###
327 $limit = 1 if $count > 0;
332 my $real_name; # to set the name of a file when
333 # we're encountering @longlink
337 while( $handle->read( $chunk, HEAD ) ) {
338 ### IO::Zlib doesn't support this yet
340 if ( ref($handle) ne 'IO::Zlib' ) {
342 $offset = eval { tell $handle } || 'unknown';
350 my $gzip = GZIP_MAGIC_NUM;
351 if( $chunk =~ /$gzip/ ) {
352 $self->_error( qq[Cannot read compressed format in tar-mode] );
356 ### size is < HEAD, which means a corrupted file, as the minimum
357 ### length is _at least_ HEAD
358 if (length $chunk != HEAD) {
359 $self->_error( qq[Cannot read enough bytes from the tarfile] );
364 ### if we can't read in all bytes... ###
365 last if length $chunk != HEAD;
367 ### Apparently this should really be two blocks of 512 zeroes,
368 ### but GNU tar sometimes gets it wrong. See comment in the
369 ### source code (tar.c) to GNU cpio.
370 next if $chunk eq TAR_END;
372 ### according to the posix spec, the last 12 bytes of the header are
373 ### null bytes, to pad it to a 512 byte block. That means if these
374 ### bytes are NOT null bytes, it's a corrupt header. See:
375 ### www.koders.com/c/fidCE473AD3D9F835D690259D60AD5654591D91D5BA.aspx
377 { my $nulls = join '', "\0" x 12;
378 unless( $nulls eq substr( $chunk, 500, 12 ) ) {
379 $self->_error( qq[Invalid header block at offset $offset] );
384 ### pass the realname, so we can set it 'proper' right away
385 ### some of the heuristics are done on the name, so important
388 { my %extra_args = ();
389 $extra_args{'name'} = $$real_name if defined $real_name;
391 unless( $entry = Archive::Tar::File->new( chunk => $chunk,
394 $self->_error( qq[Couldn't read chunk at offset $offset] );
400 ### http://www.gnu.org/software/tar/manual/html_chapter/Media.html#SEC159
401 next if $entry->is_label;
403 if( length $entry->type and ($entry->is_file || $entry->is_longlink) ) {
405 if ( $entry->is_file && !$entry->validate ) {
406 ### sometimes the chunk is rather fux0r3d and a whole 512
407 ### bytes ends up in the ->name area.
408 ### clean it up, if need be
409 my $name = $entry->name;
410 $name = substr($name, 0, 100) if length $name > 100;
413 $self->_error( $name . qq[: checksum error] );
417 my $block = BLOCK_SIZE->( $entry->size );
419 $data = $entry->get_content_by_ref;
423 ### skip this entry if we're filtering
426 $ctx = Digest::MD5->new; # cdrake
429 } elsif ($filter && $entry->name !~ $filter) {
432 } elsif ($filter_cb && ! $filter_cb->($entry)) {
435 ### skip this entry if it's a pax header. This is a special file added
436 ### by, among others, git-generated tarballs. It holds comments and is
437 ### not meant for extracting. See #38932: pax_global_header extracted
438 } elsif ( $entry->name eq PAX_HEADER or $entry->type =~ /^(x|g)$/ ) {
444 # Since we're skipping, do not allocate memory for the
445 # whole file. Read it 64 BLOCKS at a time. Do not
446 # complete the skip yet because maybe what we read is a
447 # longlink and it won't get skipped after all
450 my $fsz=$entry->size; # cdrake
453 my $this = 64 * BLOCK;
454 $this = $amt if $this > $amt;
455 if( $handle->read( $$data, $this ) < $this ) {
456 $self->_error( qq[Read error on tarfile (missing data) '].
457 $entry->full_path ."' at offset $offset" );
461 $fsz -= $this; # cdrake
462 substr ($$data, $fsz) = "" if ($fsz<0); # remove external junk prior to md5 # cdrake
463 $ctx->add($$data) if($skip==5); # cdrake
465 $$data = $ctx->hexdigest if($skip==5 && !$entry->is_longlink && !$entry->is_unknown && !$entry->is_label ) ; # cdrake
468 ### just read everything into memory
469 ### can't do lazy loading since IO::Zlib doesn't support 'seek'
470 ### this is because Compress::Zlib doesn't support it =/
471 ### this reads in the whole data in one read() call.
472 if ( $handle->read( $$data, $block ) < $block ) {
473 $self->_error( qq[Read error on tarfile (missing data) '].
474 $entry->full_path ."' at offset $offset" );
477 ### throw away trailing garbage ###
478 substr ($$data, $entry->size) = "" if defined $$data;
481 ### part II of the @LongLink munging -- need to do /after/
482 ### the checksum check.
483 if( $entry->is_longlink ) {
484 ### weird thing in tarfiles -- if the file is actually a
485 ### @LongLink, the data part seems to have a trailing ^@
486 ### (unprintable) char. to display, pipe output through less.
487 ### but that doesn't *always* happen.. so check if the last
488 ### character is a control character, and if so remove it
489 ### at any rate, we better remove that character here, or tests
490 ### like 'eq' and hash lookups based on names will SO not work
491 ### remove it by calculating the proper size, and then
492 ### tossing out everything that's longer than that size.
494 ### count number of nulls
495 my $nulls = $$data =~ tr/\0/\0/;
497 ### cut data + size by that many bytes
498 $entry->size( $entry->size - $nulls );
499 substr ($$data, $entry->size) = "";
503 ### clean up of the entries.. posix tar /apparently/ has some
504 ### weird 'feature' that allows for filenames > 255 characters
505 ### they'll put a header in with as name '././@LongLink' and the
506 ### contents will be the name of the /next/ file in the archive
507 ### pretty crappy and kludgy if you ask me
509 ### set the name for the next entry if this is a @LongLink;
510 ### this is one ugly hack =/ but needed for direct extraction
511 if( $entry->is_longlink ) {
514 } elsif ( defined $real_name ) {
515 $entry->name( $$real_name );
520 if ($filter && $entry->name !~ $filter) {
523 } elsif ($filter_cb && ! $filter_cb->($entry)) {
526 ### skip this entry if it's a pax header. This is a special file added
527 ### by, among others, git-generated tarballs. It holds comments and is
528 ### not meant for extracting. See #38932: pax_global_header extracted
529 } elsif ( $entry->name eq PAX_HEADER or $entry->type =~ /^(x|g)$/ ) {
533 if ( $extract && !$entry->is_longlink
534 && !$entry->is_unknown
535 && !$entry->is_label ) {
536 $self->_extract_file( $entry ) or return;
539 ### Guard against tarfiles with garbage at the end
540 last LOOP if $entry->name eq '';
542 ### push only the name on the rv if we're extracting
543 ### -- for extract_archive
544 push @$tarfile, ($extract ? $entry->name : $entry);
547 $count-- unless $entry->is_longlink || $entry->is_dir;
548 last LOOP unless $count;
557 =head2 $tar->contains_file( $filename )
559 Check if the archive contains a certain file.
560 It will return true if the file is in the archive, false otherwise.
562 Note however, that this function does an exact match using C<eq>
563 on the full path. So it cannot compensate for case-insensitive file-
564 systems or compare 2 paths to see if they would point to the same
573 return unless defined $full;
575 ### don't warn if the entry isn't there.. that's what this function
576 ### is for after all.
578 return 1 if $self->_find_entry($full);
582 =head2 $tar->extract( [@filenames] )
584 Write files whose names are equivalent to any of the names in
585 C<@filenames> to disk, creating subdirectories as necessary. This
586 might not work too well under VMS.
587 Under MacPerl, the file's modification time will be converted to the
588 MacOS zero of time, and appropriate conversions will be done to the
589 path. However, the length of each element of the path is not
590 inspected to see whether it's longer than MacOS currently allows (32
593 If C<extract> is called without a list of file names, the entire
594 contents of the archive are extracted.
596 Returns a list of filenames extracted.
605 # use the speed optimization for all extracted files
606 local($self->{cwd}) = cwd() unless $self->{cwd};
608 ### you requested the extraction of only certain files
610 for my $file ( @args ) {
612 ### it's already an object?
613 if( UNIVERSAL::isa( $file, 'Archive::Tar::File' ) ) {
621 for my $entry ( @{$self->_data} ) {
622 next unless $file eq $entry->full_path;
624 ### we found the file you're looking for
630 return $self->_error(
631 qq[Could not find '$file' in archive] );
636 ### just grab all the file items
638 @files = $self->get_files;
641 ### nothing found? that's an error
642 unless( scalar @files ) {
643 $self->_error( qq[No files found for ] . $self->_file );
648 for my $entry ( @files ) {
649 unless( $self->_extract_file( $entry ) ) {
650 $self->_error(q[Could not extract ']. $entry->full_path .q['] );
658 =head2 $tar->extract_file( $file, [$extract_path] )
660 Write an entry, whose name is equivalent to the file name provided to
661 disk. Optionally takes a second parameter, which is the full native
662 path (including filename) the entry will be written to.
666 $tar->extract_file( 'name/in/archive', 'name/i/want/to/give/it' );
668 $tar->extract_file( $at_file_object, 'name/i/want/to/give/it' );
670 Returns true on success, false on failure.
676 my $file = shift; return unless defined $file;
679 my $entry = $self->_find_entry( $file )
680 or $self->_error( qq[Could not find an entry for '$file'] ), return;
682 return $self->_extract_file( $entry, $alt );
687 my $entry = shift or return;
690 ### you wanted an alternate extraction location ###
691 my $name = defined $alt ? $alt : $entry->full_path;
693 ### splitpath takes a bool at the end to indicate
694 ### that it's splitting a dir
695 my ($vol,$dirs,$file);
696 if ( defined $alt ) { # It's a local-OS path
697 ($vol,$dirs,$file) = File::Spec->splitpath( $alt,
700 ($vol,$dirs,$file) = File::Spec::Unix->splitpath( $name,
705 ### is $name an absolute path? ###
706 if( $vol || File::Spec->file_name_is_absolute( $dirs ) ) {
708 ### absolute names are not allowed to be in tarballs under
709 ### strict mode, so only allow it if a user tells us to do it
710 if( not defined $alt and not $INSECURE_EXTRACT_MODE ) {
712 q[Entry ']. $entry->full_path .q[' is an absolute path. ].
713 q[Not extracting absolute paths under SECURE EXTRACT MODE]
718 ### user asked us to, it's fine.
719 $dir = File::Spec->catpath( $vol, $dirs, "" );
721 ### it's a relative path ###
723 my $cwd = (ref $self and defined $self->{cwd})
727 my @dirs = defined $alt
728 ? File::Spec->splitdir( $dirs ) # It's a local-OS path
729 : File::Spec::Unix->splitdir( $dirs ); # it's UNIX-style, likely
730 # straight from the tarball
732 if( not defined $alt and
733 not $INSECURE_EXTRACT_MODE
736 ### paths that leave the current directory are not allowed under
737 ### strict mode, so only allow it if a user tells us to do this.
738 if( grep { $_ eq '..' } @dirs ) {
741 q[Entry ']. $entry->full_path .q[' is attempting to leave ].
742 q[the current working directory. Not extracting under ].
743 q[SECURE EXTRACT MODE]
748 ### the archive may be asking us to extract into a symlink. This
749 ### is not sane and a possible security issue, as outlined here:
750 ### https://rt.cpan.org/Ticket/Display.html?id=30380
751 ### https://bugzilla.redhat.com/show_bug.cgi?id=295021
752 ### https://issues.rpath.com/browse/RPL-1716
753 my $full_path = $cwd;
754 for my $d ( @dirs ) {
755 $full_path = File::Spec->catdir( $full_path, $d );
757 ### we've already checked this one, and it's safe. Move on.
758 next if ref $self and $self->{_link_cache}->{$full_path};
760 if( -l $full_path ) {
761 my $to = readlink $full_path;
762 my $diag = "symlinked directory ($full_path => $to)";
765 q[Entry ']. $entry->full_path .q[' is attempting to ].
766 qq[extract to a $diag. This is considered a security ].
767 q[vulnerability and not allowed under SECURE EXTRACT ].
773 ### XXX keep a cache if possible, so the stats become cheaper:
774 $self->{_link_cache}->{$full_path} = 1 if ref $self;
778 ### '.' is the directory delimiter on VMS, which has to be escaped
779 ### or changed to '_' on vms. vmsify is used, because older versions
780 ### of vmspath do not handle this properly.
781 ### Must not add a '/' to an empty directory though.
782 map { length() ? VMS::Filespec::vmsify($_.'/') : $_ } @dirs if ON_VMS;
784 my ($cwd_vol,$cwd_dir,$cwd_file)
785 = File::Spec->splitpath( $cwd );
786 my @cwd = File::Spec->splitdir( $cwd_dir );
787 push @cwd, $cwd_file if length $cwd_file;
789 ### We need to pass '' as the last element to catpath. Craig Berry
790 ### explains why (msgid <p0624083dc311ae541393@[172.16.52.1]>):
791 ### The root problem is that splitpath on UNIX always returns the
792 ### final path element as a file even if it is a directory, and of
793 ### course there is no way it can know the difference without checking
794 ### against the filesystem, which it is documented as not doing. When
795 ### you turn around and call catpath, on VMS you have to know which bits
796 ### are directory bits and which bits are file bits. In this case we
797 ### know the result should be a directory. I had thought you could omit
798 ### the file argument to catpath in such a case, but apparently on UNIX
800 $dir = File::Spec->catpath(
801 $cwd_vol, File::Spec->catdir( @cwd, @dirs ), ''
804 ### catdir() returns undef if the path is longer than 255 chars on
805 ### older VMS systems.
806 unless ( defined $dir ) {
807 $^W && $self->_error( qq[Could not compose a path for '$dirs'\n] );
813 if( -e $dir && !-d _ ) {
814 $^W && $self->_error( qq['$dir' exists, but it's not a directory!\n] );
819 eval { File::Path::mkpath( $dir, 0, 0777 ) };
821 my $fp = $entry->full_path;
822 $self->_error(qq[Could not create directory '$dir' for '$fp': $@]);
826 ### XXX chown here? that might not be the same as in the archive
827 ### as we're only chown'ing to the owner of the file we're extracting
828 ### not to the owner of the directory itself, which may or may not
829 ### be another entry in the archive
830 ### Answer: no, gnu tar doesn't do it either, it'd be the wrong
832 #if( $CHOWN && CAN_CHOWN ) {
833 # chown $entry->uid, $entry->gid, $dir or
834 # $self->_error( qq[Could not set uid/gid on '$dir'] );
838 ### we're done if we just needed to create a dir ###
839 return 1 if $entry->is_dir;
841 my $full = File::Spec->catfile( $dir, $file );
843 if( $entry->is_unknown ) {
844 $self->_error( qq[Unknown file type for file '$full'] );
848 if( length $entry->type && $entry->is_file ) {
849 my $fh = IO::File->new;
850 $fh->open( '>' . $full ) or (
851 $self->_error( qq[Could not open file '$full': $!] ),
857 syswrite $fh, $entry->data or (
858 $self->_error( qq[Could not write data to '$full'] ),
864 $self->_error( qq[Could not close file '$full'] ),
869 $self->_make_special_file( $entry, $full ) or return;
872 ### only update the timestamp if it's not a symlink; that will change the
873 ### timestamp of the original. This addresses bug #33669: Could not update
874 ### timestamp warning on symlinks
876 utime time, $entry->mtime - TIME_OFFSET, $full or
877 $self->_error( qq[Could not update timestamp] );
880 if( $CHOWN && CAN_CHOWN->() and not -l $full ) {
881 chown $entry->uid, $entry->gid, $full or
882 $self->_error( qq[Could not set uid/gid on '$full'] );
885 ### only chmod if we're allowed to, but never chmod symlinks, since they'll
886 ### change the perms on the file they're linking too...
887 if( $CHMOD and not -l $full ) {
888 my $mode = $entry->mode;
889 unless ($SAME_PERMISSIONS) {
890 $mode &= ~(oct(7000) | umask);
892 chmod $mode, $full or
893 $self->_error( qq[Could not chown '$full' to ] . $entry->mode );
899 sub _make_special_file {
901 my $entry = shift or return;
902 my $file = shift; return unless defined $file;
906 if( $entry->is_symlink ) {
909 symlink( $entry->linkname, $file ) or $fail++;
912 $self->_extract_special_file_as_plain_file( $entry, $file )
916 $err = qq[Making symbolic link '$file' to '] .
917 $entry->linkname .q[' failed] if $fail;
919 } elsif ( $entry->is_hardlink ) {
922 link( $entry->linkname, $file ) or $fail++;
925 $self->_extract_special_file_as_plain_file( $entry, $file )
929 $err = qq[Making hard link from '] . $entry->linkname .
930 qq[' to '$file' failed] if $fail;
932 } elsif ( $entry->is_fifo ) {
933 ON_UNIX && !system('mknod', $file, 'p') or
934 $err = qq[Making fifo ']. $entry->name .qq[' failed];
936 } elsif ( $entry->is_blockdev or $entry->is_chardev ) {
937 my $mode = $entry->is_blockdev ? 'b' : 'c';
939 ON_UNIX && !system('mknod', $file, $mode,
940 $entry->devmajor, $entry->devminor) or
941 $err = qq[Making block device ']. $entry->name .qq[' (maj=] .
942 $entry->devmajor . qq[ min=] . $entry->devminor .
945 } elsif ( $entry->is_socket ) {
946 ### the original doesn't do anything special for sockets.... ###
950 return $err ? $self->_error( $err ) : 1;
953 ### don't know how to make symlinks, let's just extract the file as
955 sub _extract_special_file_as_plain_file {
957 my $entry = shift or return;
958 my $file = shift; return unless defined $file;
962 my $orig = $self->_find_entry( $entry->linkname, $entry );
965 $err = qq[Could not find file '] . $entry->linkname .
970 ### clone the entry, make it appear as a normal file ###
971 my $clone = $orig->clone;
972 $clone->_downgrade_to_plainfile;
973 $self->_extract_file( $clone, $file ) or last TRY;
978 return $self->_error($err);
981 =head2 $tar->list_files( [\@properties] )
983 Returns a list of the names of all the files in the archive.
985 If C<list_files()> is passed an array reference as its first argument
986 it returns a list of hash references containing the requested
987 properties of each file. The following list of properties is
988 supported: name, size, mtime (last modified date), mode, uid, gid,
989 linkname, uname, gname, devmajor, devminor, prefix.
991 Passing an array reference containing only one element, 'name', is
992 special cased to return a list of names rather than a list of hash
993 references, making it equivalent to calling C<list_files> without
1000 my $aref = shift || [ ];
1002 unless( $self->_data ) {
1003 $self->read() or return;
1006 if( @$aref == 0 or ( @$aref == 1 and $aref->[0] eq 'name' ) ) {
1007 return map { $_->full_path } @{$self->_data};
1011 #for my $obj ( @{$self->_data} ) {
1012 # push @rv, { map { $_ => $obj->$_() } @$aref };
1016 ### this does the same as the above.. just needs a +{ }
1017 ### to make sure perl doesn't confuse it for a block
1018 return map { my $o=$_;
1019 +{ map { $_ => $o->$_() } @$aref }
1028 unless( defined $file ) {
1029 $self->_error( qq[No file specified] );
1033 ### it's an object already
1034 return $file if UNIVERSAL::isa( $file, 'Archive::Tar::File' );
1038 for my $entry ( @{$self->_data} ) {
1039 my $path = $entry->full_path;
1040 return $entry if $path eq $file;
1044 if($Archive::Tar::RESOLVE_SYMLINK!~/none/){
1045 if(my $link_entry = shift()){#fallback mode when symlinks are using relative notations ( ../a/./b/text.bin )
1046 $file = _symlinks_resolver( $link_entry->name, $file );
1047 goto seach_entry if $self->_data;
1049 #this will be slower than never, but won't failed!
1051 my $iterargs = $link_entry->{'_archive'};
1052 if($Archive::Tar::RESOLVE_SYMLINK=~/speed/ && @$iterargs==3){
1053 #faster but whole archive will be read in memory
1054 #read whole archive and share data
1055 my $archive = Archive::Tar->new;
1056 $archive->read( @$iterargs );
1057 push @$iterargs, $archive; #take a trace for destruction
1058 if($archive->_data){
1059 $self->_data( $archive->_data );
1064 {#slower but lower memory usage
1065 # $iterargs = [$filename, $compressed, $opts];
1066 my $next = Archive::Tar->iter( @$iterargs );
1067 while(my $e = $next->()){
1068 if($e->full_path eq $file){
1077 $self->_error( qq[No such file in archive: '$file'] );
1081 =head2 $tar->get_files( [@filenames] )
1083 Returns the C<Archive::Tar::File> objects matching the filenames
1084 provided. If no filename list was passed, all C<Archive::Tar::File>
1085 objects in the current Tar object are returned.
1087 Please refer to the C<Archive::Tar::File> documentation on how to
1088 handle these objects.
1095 return @{ $self->_data } unless @_;
1098 for my $file ( @_ ) {
1099 push @list, grep { defined } $self->_find_entry( $file );
1105 =head2 $tar->get_content( $file )
1107 Return the content of the named file.
1113 my $entry = $self->_find_entry( shift ) or return;
1115 return $entry->data;
1118 =head2 $tar->replace_content( $file, $content )
1120 Make the string $content be the content for the file named $file.
1124 sub replace_content {
1126 my $entry = $self->_find_entry( shift ) or return;
1128 return $entry->replace_content( shift );
1131 =head2 $tar->rename( $file, $new_name )
1133 Rename the file of the in-memory archive to $new_name.
1135 Note that you must specify a Unix path for $new_name, since per tar
1136 standard, all files in the archive must be Unix paths.
1138 Returns true on success and false on failure.
1144 my $file = shift; return unless defined $file;
1145 my $new = shift; return unless defined $new;
1147 my $entry = $self->_find_entry( $file ) or return;
1149 return $entry->rename( $new );
1152 =head2 $tar->chmod( $file, $mode )
1154 Change mode of $file to $mode.
1156 Returns true on success and false on failure.
1162 my $file = shift; return unless defined $file;
1163 my $mode = shift; return unless defined $mode && $mode =~ /^[0-7]{1,4}$/;
1164 my @args = ("$mode");
1166 my $entry = $self->_find_entry( $file ) or return;
1167 my $x = $entry->chmod( @args );
1171 =head2 $tar->chown( $file, $uname [, $gname] )
1173 Change owner $file to $uname and $gname.
1175 Returns true on success and false on failure.
1181 my $file = shift; return unless defined $file;
1182 my $uname = shift; return unless defined $uname;
1183 my @args = ($uname);
1186 my $entry = $self->_find_entry( $file ) or return;
1187 my $x = $entry->chown( @args );
1191 =head2 $tar->remove (@filenamelist)
1193 Removes any entries with names matching any of the given filenames
1194 from the in-memory archive. Returns a list of C<Archive::Tar::File>
1195 objects that remain.
1203 my %seen = map { $_->full_path => $_ } @{$self->_data};
1204 delete $seen{ $_ } for @list;
1206 $self->_data( [values %seen] );
1208 return values %seen;
1213 C<clear> clears the current in-memory archive. This effectively gives
1214 you a 'blank' object, ready to be filled again. Note that C<clear>
1215 only has effect on the object, not the underlying tarfile.
1220 my $self = shift or return;
1229 =head2 $tar->write ( [$file, $compressed, $prefix] )
1231 Write the in-memory archive to disk. The first argument can either
1232 be the name of a file or a reference to an already open filehandle (a
1235 The second argument is used to indicate compression. You can either
1236 compress using C<gzip> or C<bzip2>. If you pass a digit, it's assumed
1237 to be the C<gzip> compression level (between 1 and 9), but the use of
1238 constants is preferred:
1240 # write a gzip compressed file
1241 $tar->write( 'out.tgz', COMPRESS_GZIP );
1243 # write a bzip compressed file
1244 $tar->write( 'out.tbz', COMPRESS_BZIP );
1246 Note that when you pass in a filehandle, the compression argument
1247 is ignored, as all files are printed verbatim to your filehandle.
1248 If you wish to enable compression with filehandles, use an
1249 C<IO::Zlib> or C<IO::Compress::Bzip2> filehandle instead.
1251 The third argument is an optional prefix. All files will be tucked
1252 away in the directory you specify as prefix. So if you have files
1253 'a' and 'b' in your archive, and you specify 'foo' as prefix, they
1254 will be written to the archive as 'foo/a' and 'foo/b'.
1256 If no arguments are given, C<write> returns the entire formatted
1257 archive as a string, which could be useful if you'd like to stuff the
1258 archive into a socket or a pipe to gzip or something.
1265 my $file = shift; $file = '' unless defined $file;
1266 my $gzip = shift || 0;
1267 my $ext_prefix = shift; $ext_prefix = '' unless defined $ext_prefix;
1270 ### only need a handle if we have a file to print to ###
1271 my $handle = length($file)
1272 ? ( $self->_get_handle($file, $gzip, WRITE_ONLY->($gzip) )
1274 : $HAS_PERLIO ? do { open my $h, '>', \$dummy; $h }
1275 : $HAS_IO_STRING ? IO::String->new
1276 : __PACKAGE__->no_string_support();
1278 ### Addresses: #41798: Nonempty $\ when writing a TAR file produces a
1279 ### corrupt TAR file. Must clear out $\ to make sure no garbage is
1280 ### printed to the archive
1283 for my $entry ( @{$self->_data} ) {
1284 ### entries to be written to the tarfile ###
1287 ### only now will we change the object to reflect the current state
1288 ### of the name and prefix fields -- this needs to be limited to
1290 my $clone = $entry->clone;
1293 ### so, if you don't want use to use the prefix, we'll stuff
1294 ### everything in the name field instead
1295 if( $DO_NOT_USE_PREFIX ) {
1297 ### you might have an extended prefix, if so, set it in the clone
1298 ### XXX is ::Unix right?
1299 $clone->name( length $ext_prefix
1300 ? File::Spec::Unix->catdir( $ext_prefix,
1302 : $clone->full_path );
1303 $clone->prefix( '' );
1305 ### otherwise, we'll have to set it properly -- prefix part in the
1306 ### prefix and name part in the name field.
1309 ### split them here, not before!
1310 my ($prefix,$name) = $clone->_prefix_and_file( $clone->full_path );
1312 ### you might have an extended prefix, if so, set it in the clone
1313 ### XXX is ::Unix right?
1314 $prefix = File::Spec::Unix->catdir( $ext_prefix, $prefix )
1315 if length $ext_prefix;
1317 $clone->prefix( $prefix );
1318 $clone->name( $name );
1321 ### names are too long, and will get truncated if we don't add a
1322 ### '@LongLink' file...
1323 my $make_longlink = ( length($clone->name) > NAME_LENGTH or
1324 length($clone->prefix) > PREFIX_LENGTH
1327 ### perhaps we need to make a longlink file?
1328 if( $make_longlink ) {
1329 my $longlink = Archive::Tar::File->new(
1330 data => LONGLINK_NAME,
1332 { type => LONGLINK }
1335 unless( $longlink ) {
1336 $self->_error( qq[Could not create 'LongLink' entry for ] .
1337 qq[oversize file '] . $clone->full_path ."'" );
1341 push @write_me, $longlink;
1344 push @write_me, $clone;
1346 ### write the one, optionally 2 a::t::file objects to the handle
1347 for my $clone (@write_me) {
1349 ### if the file is a symlink, there are 2 options:
1350 ### either we leave the symlink intact, but then we don't write any
1351 ### data OR we follow the symlink, which means we actually make a
1352 ### copy. if we do the latter, we have to change the TYPE of the
1354 my $link_ok = $clone->is_symlink && $Archive::Tar::FOLLOW_SYMLINK;
1355 my $data_ok = !$clone->is_symlink && $clone->has_content;
1357 ### downgrade to a 'normal' file if it's a symlink we're going to
1358 ### treat as a regular file
1359 $clone->_downgrade_to_plainfile if $link_ok;
1361 ### get the header for this block
1362 my $header = $self->_format_tar_entry( $clone );
1364 $self->_error(q[Could not format header for: ] .
1365 $clone->full_path );
1369 unless( print $handle $header ) {
1370 $self->_error(q[Could not write header for: ] .
1375 if( $link_ok or $data_ok ) {
1376 unless( print $handle $clone->data ) {
1377 $self->_error(q[Could not write data for: ] .
1382 ### pad the end of the clone if required ###
1383 print $handle TAR_PAD->( $clone->size ) if $clone->size % BLOCK
1386 } ### done writing these entries
1389 ### write the end markers ###
1390 print $handle TAR_END x 2 or
1391 return $self->_error( qq[Could not write tar end markers] );
1393 ### did you want it written to a file, or returned as a string? ###
1394 my $rv = length($file) ? 1
1395 : $HAS_PERLIO ? $dummy
1396 : do { seek $handle, 0, 0; local $/; <$handle> };
1398 ### make sure to close the handle if we created it
1399 if ( $file ne $handle ) {
1400 unless( close $handle ) {
1401 $self->_error( qq[Could not write tar] );
1409 sub _format_tar_entry {
1411 my $entry = shift or return;
1412 my $ext_prefix = shift; $ext_prefix = '' unless defined $ext_prefix;
1413 my $no_prefix = shift || 0;
1415 my $file = $entry->name;
1416 my $prefix = $entry->prefix; $prefix = '' unless defined $prefix;
1418 ### remove the prefix from the file name
1419 ### not sure if this is still needed --kane
1420 ### no it's not -- Archive::Tar::File->_new_from_file will take care of
1421 ### this for us. Even worse, this would break if we tried to add a file
1423 #if( length $prefix ) {
1424 # $file =~ s/^$match//;
1427 $prefix = File::Spec::Unix->catdir($ext_prefix, $prefix)
1428 if length $ext_prefix;
1430 ### not sure why this is... ###
1431 my $l = PREFIX_LENGTH; # is ambiguous otherwise...
1432 substr ($prefix, 0, -$l) = "" if length $prefix >= PREFIX_LENGTH;
1434 my $f1 = "%06o"; my $f2 = $ZERO_PAD_NUMBERS ? "%011o" : "%11o";
1436 ### this might be optimizable with a 'changed' flag in the file objects ###
1441 (map { sprintf( $f1, $entry->$_() ) } qw[mode uid gid]),
1442 (map { sprintf( $f2, $entry->$_() ) } qw[size mtime]),
1444 "", # checksum field - space padded a bit down
1446 (map { $entry->$_() } qw[type linkname magic]),
1448 $entry->version || TAR_VERSION,
1450 (map { $entry->$_() } qw[uname gname]),
1451 (map { sprintf( $f1, $entry->$_() ) } qw[devmajor devminor]),
1453 ($no_prefix ? '' : $prefix)
1456 ### add the checksum ###
1457 my $checksum_fmt = $ZERO_PAD_NUMBERS ? "%06o\0" : "%06o\0";
1458 substr($tar,148,7) = sprintf("%6o\0", unpack("%16C*",$tar));
1463 =head2 $tar->add_files( @filenamelist )
1465 Takes a list of filenames and adds them to the in-memory archive.
1467 The path to the file is automatically converted to a Unix like
1468 equivalent for use in the archive, and, if on MacOS, the file's
1469 modification time is converted from the MacOS epoch to the Unix epoch.
1470 So tar archives created on MacOS with B<Archive::Tar> can be read
1471 both with I<tar> on Unix and applications like I<suntar> or
1472 I<Stuffit Expander> on MacOS.
1474 Be aware that the file's type/creator and resource fork will be lost,
1475 which is usually what you want in cross-platform archives.
1477 Instead of a filename, you can also pass it an existing C<Archive::Tar::File>
1478 object from, for example, another archive. The object will be clone, and
1479 effectively be a copy of the original, not an alias.
1481 Returns a list of C<Archive::Tar::File> objects that were just added.
1487 my @files = @_ or return;
1490 for my $file ( @files ) {
1492 ### you passed an Archive::Tar::File object
1493 ### clone it so we don't accidentally have a reference to
1494 ### an object from another archive
1495 if( UNIVERSAL::isa( $file,'Archive::Tar::File' ) ) {
1496 push @rv, $file->clone;
1501 if( utf8::is_utf8( $file )) {
1502 utf8::encode( $file );
1506 unless( -e $file || -l $file ) {
1507 $self->_error( qq[No such file: '$file'] );
1511 my $obj = Archive::Tar::File->new( file => $file );
1513 $self->_error( qq[Unable to add file: '$file'] );
1520 push @{$self->{_data}}, @rv;
1525 =head2 $tar->add_data ( $filename, $data, [$opthashref] )
1527 Takes a filename, a scalar full of data and optionally a reference to
1528 a hash with specific options.
1530 Will add a file to the in-memory archive, with name C<$filename> and
1531 content C<$data>. Specific properties can be set using C<$opthashref>.
1532 The following list of properties is supported: name, size, mtime
1533 (last modified date), mode, uid, gid, linkname, uname, gname,
1534 devmajor, devminor, prefix, type. (On MacOS, the file's path and
1535 modification times are converted to Unix equivalents.)
1537 Valid values for the file type are the following constants defined by
1538 Archive::Tar::Constant:
1550 Hard and symbolic ("soft") links; linkname should specify target.
1556 Character and block devices. devmajor and devminor should specify the major
1557 and minor device numbers.
1573 Returns the C<Archive::Tar::File> object that was just added, or
1574 C<undef> on failure.
1580 my ($file, $data, $opt) = @_;
1582 my $obj = Archive::Tar::File->new( data => $file, $data, $opt );
1584 $self->_error( qq[Unable to add file: '$file'] );
1588 push @{$self->{_data}}, $obj;
1593 =head2 $tar->error( [$BOOL] )
1595 Returns the current error string (usually, the last error reported).
1596 If a true value was specified, it will give the C<Carp::longmess>
1597 equivalent of the error, in effect giving you a stacktrace.
1599 For backwards compatibility, this error is also available as
1600 C<$Archive::Tar::error> although it is much recommended you use the
1601 method call instead.
1611 my $msg = $error = shift;
1612 $longmess = Carp::longmess($error);
1614 $self->{_error} = $error;
1615 $self->{_longmess} = $longmess;
1618 ### set Archive::Tar::WARN to 0 to disable printing
1621 carp $DEBUG ? $longmess : $msg;
1630 return shift() ? $self->{_longmess} : $self->{_error};
1632 return shift() ? $longmess : $error;
1637 =head2 $tar->setcwd( $cwd );
1639 C<Archive::Tar> needs to know the current directory, and it will run
1640 C<Cwd::cwd()> I<every> time it extracts a I<relative> entry from the
1641 tarfile and saves it in the file system. (As of version 1.30, however,
1642 C<Archive::Tar> will use the speed optimization described below
1643 automatically, so it's only relevant if you're using C<extract_file()>).
1645 Since C<Archive::Tar> doesn't change the current directory internally
1646 while it is extracting the items in a tarball, all calls to C<Cwd::cwd()>
1647 can be avoided if we can guarantee that the current directory doesn't
1648 get changed externally.
1650 To use this performance boost, set the current directory via
1653 $tar->setcwd( cwd() );
1655 once before calling a function like C<extract_file> and
1656 C<Archive::Tar> will use the current directory setting from then on
1657 and won't call C<Cwd::cwd()> internally.
1659 To switch back to the default behaviour, use
1661 $tar->setcwd( undef );
1663 and C<Archive::Tar> will call C<Cwd::cwd()> internally again.
1665 If you're using C<Archive::Tar>'s C<extract()> method, C<setcwd()> will
1674 $self->{cwd} = $cwd;
1677 =head1 Class Methods
1679 =head2 Archive::Tar->create_archive($file, $compressed, @filelist)
1681 Creates a tar file from the list of files provided. The first
1682 argument can either be the name of the tar file to create or a
1683 reference to an open file handle (e.g. a GLOB reference).
1685 The second argument is used to indicate compression. You can either
1686 compress using C<gzip> or C<bzip2>. If you pass a digit, it's assumed
1687 to be the C<gzip> compression level (between 1 and 9), but the use of
1688 constants is preferred:
1690 # write a gzip compressed file
1691 Archive::Tar->create_archive( 'out.tgz', COMPRESS_GZIP, @filelist );
1693 # write a bzip compressed file
1694 Archive::Tar->create_archive( 'out.tbz', COMPRESS_BZIP, @filelist );
1696 Note that when you pass in a filehandle, the compression argument
1697 is ignored, as all files are printed verbatim to your filehandle.
1698 If you wish to enable compression with filehandles, use an
1699 C<IO::Zlib> or C<IO::Compress::Bzip2> filehandle instead.
1701 The remaining arguments list the files to be included in the tar file.
1702 These files must all exist. Any files which don't exist or can't be
1703 read are silently ignored.
1705 If the archive creation fails for any reason, C<create_archive> will
1706 return false. Please use the C<error> method to find the cause of the
1709 Note that this method does not write C<on the fly> as it were; it
1710 still reads all the files into memory before writing out the archive.
1711 Consult the FAQ below if this is a problem.
1715 sub create_archive {
1718 my $file = shift; return unless defined $file;
1719 my $gzip = shift || 0;
1723 return $class->_error( qq[Cowardly refusing to create empty archive!] );
1726 my $tar = $class->new;
1727 $tar->add_files( @files );
1728 return $tar->write( $file, $gzip );
1731 =head2 Archive::Tar->iter( $filename, [ $compressed, {opt => $val} ] )
1733 Returns an iterator function that reads the tar file without loading
1734 it all in memory. Each time the function is called it will return the
1735 next file in the tarball. The files are returned as
1736 C<Archive::Tar::File> objects. The iterator function returns the
1737 empty list once it has exhausted the files contained.
1739 The second argument can be a hash reference with options, which are
1740 identical to the arguments passed to C<read()>.
1744 my $next = Archive::Tar->iter( "example.tar.gz", 1, {filter => qr/\.pm$/} );
1746 while( my $f = $next->() ) {
1747 print $f->name, "\n";
1749 $f->extract or warn "Extraction failed";
1759 my $filename = shift or return;
1760 my $compressed = shift || 0;
1761 my $opts = shift || {};
1763 ### get a handle to read from.
1764 my $handle = $class->_get_handle(
1771 my $CONSTRUCT_ARGS = [ $filename, $compressed, $opts ];
1773 return shift(@data) if @data; # more than one file returned?
1774 return unless $handle; # handle exhausted?
1776 ### read data, should only return file
1777 my $tarfile = $class->_read_tar($handle, { %$opts, limit => 1 });
1778 @data = @$tarfile if ref $tarfile && ref $tarfile eq 'ARRAY';
1779 if($Archive::Tar::RESOLVE_SYMLINK!~/none/){
1781 #may refine this heuristic for ON_UNIX?
1783 #is there a better slot to store/share it ?
1784 $_->{'_archive'} = $CONSTRUCT_ARGS;
1789 ### return one piece of data
1790 return shift(@data) if @data;
1792 ### data is exhausted, free the filehandle
1794 if(@$CONSTRUCT_ARGS == 4){
1795 #free archive in memory
1796 undef $CONSTRUCT_ARGS->[-1];
1802 =head2 Archive::Tar->list_archive($file, $compressed, [\@properties])
1804 Returns a list of the names of all the files in the archive. The
1805 first argument can either be the name of the tar file to list or a
1806 reference to an open file handle (e.g. a GLOB reference).
1808 If C<list_archive()> is passed an array reference as its third
1809 argument it returns a list of hash references containing the requested
1810 properties of each file. The following list of properties is
1811 supported: full_path, name, size, mtime (last modified date), mode,
1812 uid, gid, linkname, uname, gname, devmajor, devminor, prefix, type.
1814 See C<Archive::Tar::File> for details about supported properties.
1816 Passing an array reference containing only one element, 'name', is
1817 special cased to return a list of names rather than a list of hash
1824 my $file = shift; return unless defined $file;
1825 my $gzip = shift || 0;
1827 my $tar = $class->new($file, $gzip);
1830 return $tar->list_files( @_ );
1833 =head2 Archive::Tar->extract_archive($file, $compressed)
1835 Extracts the contents of the tar file. The first argument can either
1836 be the name of the tar file to create or a reference to an open file
1837 handle (e.g. a GLOB reference). All relative paths in the tar file will
1838 be created underneath the current working directory.
1840 C<extract_archive> will return a list of files it extracted.
1841 If the archive extraction fails for any reason, C<extract_archive>
1842 will return false. Please use the C<error> method to find the cause
1847 sub extract_archive {
1849 my $file = shift; return unless defined $file;
1850 my $gzip = shift || 0;
1852 my $tar = $class->new( ) or return;
1854 return $tar->read( $file, $gzip, { extract => 1 } );
1857 =head2 $bool = Archive::Tar->has_io_string
1859 Returns true if we currently have C<IO::String> support loaded.
1861 Either C<IO::String> or C<perlio> support is needed to support writing
1862 stringified archives. Currently, C<perlio> is the preferred method, if
1865 See the C<GLOBAL VARIABLES> section to see how to change this preference.
1869 sub has_io_string { return $HAS_IO_STRING; }
1871 =head2 $bool = Archive::Tar->has_perlio
1873 Returns true if we currently have C<perlio> support loaded.
1875 This requires C<perl-5.8> or higher, compiled with C<perlio>
1877 Either C<IO::String> or C<perlio> support is needed to support writing
1878 stringified archives. Currently, C<perlio> is the preferred method, if
1881 See the C<GLOBAL VARIABLES> section to see how to change this preference.
1885 sub has_perlio { return $HAS_PERLIO; }
1887 =head2 $bool = Archive::Tar->has_zlib_support
1889 Returns true if C<Archive::Tar> can extract C<zlib> compressed archives
1893 sub has_zlib_support { return ZLIB }
1895 =head2 $bool = Archive::Tar->has_bzip2_support
1897 Returns true if C<Archive::Tar> can extract C<bzip2> compressed archives
1901 sub has_bzip2_support { return BZIP }
1903 =head2 Archive::Tar->can_handle_compressed_files
1905 A simple checking routine, which will return true if C<Archive::Tar>
1906 is able to uncompress compressed archives on the fly with C<IO::Zlib>
1907 and C<IO::Compress::Bzip2> or false if not both are installed.
1909 You can use this as a shortcut to determine whether C<Archive::Tar>
1910 will do what you think before passing compressed archives to its
1915 sub can_handle_compressed_files { return ZLIB && BZIP ? 1 : 0 }
1917 sub no_string_support {
1918 croak("You have to install IO::String to support writing archives to strings");
1921 sub _symlinks_resolver{
1922 my ($src, $trg) = @_;
1923 my @src = split /[\/\\]/, $src;
1924 my @trg = split /[\/\\]/, $trg;
1925 pop @src; #strip out current object name
1926 if(@trg and $trg[0] eq ''){
1928 #restart path from scratch
1931 foreach my $part ( @trg ){
1932 next if $part eq '.'; #ignore current
1942 my $path = join('/', @src);
1943 warn "_symlinks_resolver('$src','$trg') = $path" if $DEBUG;
1951 =head1 GLOBAL VARIABLES
1953 =head2 $Archive::Tar::FOLLOW_SYMLINK
1955 Set this variable to C<1> to make C<Archive::Tar> effectively make a
1956 copy of the file when extracting. Default is C<0>, which
1957 means the symlink stays intact. Of course, you will have to pack the
1958 file linked to as well.
1960 This option is checked when you write out the tarfile using C<write>
1961 or C<create_archive>.
1963 This works just like C</bin/tar>'s C<-h> option.
1965 =head2 $Archive::Tar::CHOWN
1967 By default, C<Archive::Tar> will try to C<chown> your files if it is
1968 able to. In some cases, this may not be desired. In that case, set
1969 this variable to C<0> to disable C<chown>-ing, even if it were
1972 The default is C<1>.
1974 =head2 $Archive::Tar::CHMOD
1976 By default, C<Archive::Tar> will try to C<chmod> your files to
1977 whatever mode was specified for the particular file in the archive.
1978 In some cases, this may not be desired. In that case, set this
1979 variable to C<0> to disable C<chmod>-ing.
1981 The default is C<1>.
1983 =head2 $Archive::Tar::SAME_PERMISSIONS
1985 When, C<$Archive::Tar::CHMOD> is enabled, this setting controls whether
1986 the permissions on files from the archive are used without modification
1987 of if they are filtered by removing any setid bits and applying the
1990 The default is C<1> for the root user and C<0> for normal users.
1992 =head2 $Archive::Tar::DO_NOT_USE_PREFIX
1994 By default, C<Archive::Tar> will try to put paths that are over
1995 100 characters in the C<prefix> field of your tar header, as
1996 defined per POSIX-standard. However, some (older) tar programs
1997 do not implement this spec. To retain compatibility with these older
1998 or non-POSIX compliant versions, you can set the C<$DO_NOT_USE_PREFIX>
1999 variable to a true value, and C<Archive::Tar> will use an alternate
2000 way of dealing with paths over 100 characters by using the
2001 C<GNU Extended Header> feature.
2003 Note that clients who do not support the C<GNU Extended Header>
2004 feature will not be able to read these archives. Such clients include
2005 tars on C<Solaris>, C<Irix> and C<AIX>.
2007 The default is C<0>.
2009 =head2 $Archive::Tar::DEBUG
2011 Set this variable to C<1> to always get the C<Carp::longmess> output
2012 of the warnings, instead of the regular C<carp>. This is the same
2013 message you would get by doing:
2019 =head2 $Archive::Tar::WARN
2021 Set this variable to C<0> if you do not want any warnings printed.
2022 Personally I recommend against doing this, but people asked for the
2023 option. Also, be advised that this is of course not threadsafe.
2027 =head2 $Archive::Tar::error
2029 Holds the last reported error. Kept for historical reasons, but its
2030 use is very much discouraged. Use the C<error()> method instead:
2032 warn $tar->error unless $tar->extract;
2034 Note that in older versions of this module, the C<error()> method
2035 would return an effectively global value even when called an instance
2036 method as above. This has since been fixed, and multiple instances of
2037 C<Archive::Tar> now have separate error strings.
2039 =head2 $Archive::Tar::INSECURE_EXTRACT_MODE
2041 This variable indicates whether C<Archive::Tar> should allow
2042 files to be extracted outside their current working directory.
2044 Allowing this could have security implications, as a malicious
2045 tar archive could alter or replace any file the extracting user
2046 has permissions to. Therefor, the default is to not allow
2047 insecure extractions.
2049 If you trust the archive, or have other reasons to allow the
2050 archive to write files outside your current working directory,
2051 set this variable to C<true>.
2053 Note that this is a backwards incompatible change from version
2056 =head2 $Archive::Tar::HAS_PERLIO
2058 This variable holds a boolean indicating if we currently have
2059 C<perlio> support loaded. This will be enabled for any perl
2060 greater than C<5.8> compiled with C<perlio>.
2062 If you feel strongly about disabling it, set this variable to
2063 C<false>. Note that you will then need C<IO::String> installed
2064 to support writing stringified archives.
2066 Don't change this variable unless you B<really> know what you're
2069 =head2 $Archive::Tar::HAS_IO_STRING
2071 This variable holds a boolean indicating if we currently have
2072 C<IO::String> support loaded. This will be enabled for any perl
2073 that has a loadable C<IO::String> module.
2075 If you feel strongly about disabling it, set this variable to
2076 C<false>. Note that you will then need C<perlio> support from
2077 your perl to be able to write stringified archives.
2079 Don't change this variable unless you B<really> know what you're
2082 =head2 $Archive::Tar::ZERO_PAD_NUMBERS
2084 This variable holds a boolean indicating if we will create
2085 zero padded numbers for C<size>, C<mtime> and C<checksum>.
2086 The default is C<0>, indicating that we will create space padded
2087 numbers. Added for compatibility with C<busybox> implementations.
2089 =head2 Tuning the way RESOLVE_SYMLINK will works
2091 You can tune the behaviour by setting the $Archive::Tar::RESOLVE_SYMLINK variable,
2092 or $ENV{PERL5_AT_RESOLVE_SYMLINK} before loading the module Archive::Tar.
2094 Values can be one of the following:
2097 Disable this mechanism and failed as it was in previous version (<1.88)
2101 this will read again the whole archive using read() so all entries
2105 If you prefer memory
2109 It won't work for terminal, pipe or sockets or every non seekable source.
2117 =item What's the minimum perl version required to run Archive::Tar?
2119 You will need perl version 5.005_03 or newer.
2121 =item Isn't Archive::Tar slow?
2123 Yes it is. It's pure perl, so it's a lot slower then your C</bin/tar>
2124 However, it's very portable. If speed is an issue, consider using
2125 C</bin/tar> instead.
2127 =item Isn't Archive::Tar heavier on memory than /bin/tar?
2129 Yes it is, see previous answer. Since C<Compress::Zlib> and therefore
2130 C<IO::Zlib> doesn't support C<seek> on their filehandles, there is little
2131 choice but to read the archive into memory.
2132 This is ok if you want to do in-memory manipulation of the archive.
2134 If you just want to extract, use the C<extract_archive> class method
2135 instead. It will optimize and write to disk immediately.
2137 Another option is to use the C<iter> class method to iterate over
2138 the files in the tarball without reading them all in memory at once.
2140 =item Can you lazy-load data instead?
2142 In some cases, yes. You can use the C<iter> class method to iterate
2143 over the files in the tarball without reading them all in memory at once.
2145 =item How much memory will an X kb tar file need?
2147 Probably more than X kb, since it will all be read into memory. If
2148 this is a problem, and you don't need to do in memory manipulation
2149 of the archive, consider using the C<iter> class method, or C</bin/tar>
2152 =item What do you do with unsupported filetypes in an archive?
2154 C<Unix> has a few filetypes that aren't supported on other platforms,
2155 like C<Win32>. If we encounter a C<hardlink> or C<symlink> we'll just
2156 try to make a copy of the original file, rather than throwing an error.
2158 This does require you to read the entire archive in to memory first,
2159 since otherwise we wouldn't know what data to fill the copy with.
2160 (This means that you cannot use the class methods, including C<iter>
2161 on archives that have incompatible filetypes and still expect things
2164 For other filetypes, like C<chardevs> and C<blockdevs> we'll warn that
2165 the extraction of this particular item didn't work.
2167 =item I'm using WinZip, or some other non-POSIX client, and files are not being extracted properly!
2169 By default, C<Archive::Tar> is in a completely POSIX-compatible
2170 mode, which uses the POSIX-specification of C<tar> to store files.
2171 For paths greater than 100 characters, this is done using the
2172 C<POSIX header prefix>. Non-POSIX-compatible clients may not support
2173 this part of the specification, and may only support the C<GNU Extended
2174 Header> functionality. To facilitate those clients, you can set the
2175 C<$Archive::Tar::DO_NOT_USE_PREFIX> variable to C<true>. See the
2176 C<GLOBAL VARIABLES> section for details on this variable.
2178 Note that GNU tar earlier than version 1.14 does not cope well with
2179 the C<POSIX header prefix>. If you use such a version, consider setting
2180 the C<$Archive::Tar::DO_NOT_USE_PREFIX> variable to C<true>.
2182 =item How do I extract only files that have property X from an archive?
2184 Sometimes, you might not wish to extract a complete archive, just
2185 the files that are relevant to you, based on some criteria.
2187 You can do this by filtering a list of C<Archive::Tar::File> objects
2188 based on your criteria. For example, to extract only files that have
2189 the string C<foo> in their title, you would use:
2192 grep { $_->full_path =~ /foo/ } $tar->get_files
2195 This way, you can filter on any attribute of the files in the archive.
2196 Consult the C<Archive::Tar::File> documentation on how to use these
2199 =item How do I access .tar.Z files?
2201 The C<Archive::Tar> module can optionally use C<Compress::Zlib> (via
2202 the C<IO::Zlib> module) to access tar files that have been compressed
2203 with C<gzip>. Unfortunately tar files compressed with the Unix C<compress>
2204 utility cannot be read by C<Compress::Zlib> and so cannot be directly
2205 accesses by C<Archive::Tar>.
2207 If the C<uncompress> or C<gunzip> programs are available, you can use
2208 one of these workarounds to read C<.tar.Z> files from C<Archive::Tar>
2210 Firstly with C<uncompress>
2214 open F, "uncompress -c $filename |";
2215 my $tar = Archive::Tar->new(*F);
2218 and this with C<gunzip>
2222 open F, "gunzip -c $filename |";
2223 my $tar = Archive::Tar->new(*F);
2226 Similarly, if the C<compress> program is available, you can use this to
2227 write a C<.tar.Z> file
2232 my $fh = new IO::File "| compress -c >$filename";
2233 my $tar = Archive::Tar->new();
2238 =item How do I handle Unicode strings?
2240 C<Archive::Tar> uses byte semantics for any files it reads from or writes
2241 to disk. This is not a problem if you only deal with files and never
2242 look at their content or work solely with byte strings. But if you use
2243 Unicode strings with character semantics, some additional steps need
2246 For example, if you add a Unicode string like
2249 $tar->add_data('file.txt', "Euro: \x{20AC}");
2251 then there will be a problem later when the tarfile gets written out
2252 to disk via C<$tar->write()>:
2254 Wide character in print at .../Archive/Tar.pm line 1014.
2256 The data was added as a Unicode string and when writing it out to disk,
2257 the C<:utf8> line discipline wasn't set by C<Archive::Tar>, so Perl
2258 tried to convert the string to ISO-8859 and failed. The written file
2259 now contains garbage.
2261 For this reason, Unicode strings need to be converted to UTF-8-encoded
2262 bytestrings before they are handed off to C<add_data()>:
2265 my $data = "Accented character: \x{20AC}";
2266 $data = encode('utf8', $data);
2268 $tar->add_data('file.txt', $data);
2270 A opposite problem occurs if you extract a UTF8-encoded file from a
2271 tarball. Using C<get_content()> on the C<Archive::Tar::File> object
2272 will return its content as a bytestring, not as a Unicode string.
2274 If you want it to be a Unicode string (because you want character
2275 semantics with operations like regular expression matching), you need
2276 to decode the UTF8-encoded content and have Perl convert it into
2280 my $data = $tar->get_content();
2282 # Make it a Unicode string
2283 $data = decode('utf8', $data);
2285 There is no easy way to provide this functionality in C<Archive::Tar>,
2286 because a tarball can contain many files, and each of which could be
2287 encoded in a different way.
2293 The AIX tar does not fill all unused space in the tar archive with 0x00.
2294 This sometimes leads to warning messages from C<Archive::Tar>.
2296 Invalid header block at offset nnn
2298 A fix for that problem is scheduled to be released in the following levels
2299 of AIX, all of which should be coming out in the 4th quarter of 2009:
2311 The IBM APAR number for this problem is IZ50240 (Reported component ID:
2312 5765G0300 / AIX 5.3). It is possible to get an ifix for that problem.
2313 If you need an ifix please contact your local IBM AIX support.
2319 =item Check if passed in handles are open for read/write
2321 Currently I don't know of any portable pure perl way to do this.
2322 Suggestions welcome.
2324 =item Allow archives to be passed in as string
2326 Currently, we only allow opened filehandles or filenames, but
2327 not strings. The internals would need some reworking to facilitate
2328 stringified archives.
2330 =item Facilitate processing an opened filehandle of a compressed archive
2332 Currently, we only support this if the filehandle is an IO::Zlib object.
2333 Environments, like apache, will present you with an opened filehandle
2334 to an uploaded file, which might be a compressed archive.
2342 =item The GNU tar specification
2344 C<http://www.gnu.org/software/tar/manual/tar.html>
2346 =item The PAX format specification
2348 The specification which tar derives from; C< http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html>
2350 =item A comparison of GNU and POSIX tar standards; C<http://www.delorie.com/gnu/docs/tar/tar_114.html>
2352 =item GNU tar intends to switch to POSIX compatibility
2354 GNU Tar authors have expressed their intention to become completely
2355 POSIX-compatible; C<http://www.gnu.org/software/tar/manual/html_node/Formats.html>
2357 =item A Comparison between various tar implementations
2359 Lists known issues and incompatibilities; C<http://gd.tuwien.ac.at/utils/archivers/star/README.otherbugs>
2365 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
2367 Please reports bugs to E<lt>bug-archive-tar@rt.cpan.orgE<gt>.
2369 =head1 ACKNOWLEDGEMENTS
2371 Thanks to Sean Burke, Chris Nandor, Chip Salzenberg, Tim Heaney, Gisle Aas,
2372 Rainer Tammer and especially Andrew Savige for their help and suggestions.
2376 This module is copyright (c) 2002 - 2009 Jos Boumans
2377 E<lt>kane@cpan.orgE<gt>. All rights reserved.
2379 This library is free software; you may redistribute and/or modify
2380 it under the same terms as Perl itself.