Archive-Tar
view release on metacpan or search on metacpan
3.10 25/05/2026 (Stig Palmquist)
- Added MAX_FILE_SIZE setting, defaulting to 1GB, for
extracting files
3.08 22/05/2026 (Stig Palmquist)
- Validate symlink and hardlink linkname in SECURE MODE
3.06 10/05/2026
- Hardlinks not extracted by default, added EXTRACT_HARDLINK flag
- If hardlinks are extracted, they are now subject to the same rules
as symlinks with regards to chown and chmod
3.04 25/02/2025 (Dan Church, Arne Johannessen && SISYPHUS)
- Fix handling filenames with trailing whitespace
- Allow --format=ustar option for ptar
- GH#21402 Symlink tests on Windows
3.02 13/04/2023 (Manfred Stock)
- Test block sizes up to (2**31 - 1) bytes only (fix for 32bit perls)
- Don't match on message from exception in symlink test
- Improve formatting of $Archive::Tar::RESOLVE_SYMLINK documentation
1.93_02 22/10/2013 (XLAT)
- [rt.cpan.org #78030] symlinks resolution on MSWin32
1.92 18/09/2013 (David Steinbrunner)
- typo fixes
1.90 05/09/2012 (Tom Jones)
- documentation fixes
1.88 01/06/2012 (Markus Prosch)
- resolved chown won't work on symlinks
1.86 24/05/2012 (Mark Allen)
- don't use tell on IO::Zlib handles RT#64339
1.84 02/03/2012 (HMBRAND)
- ptar now supports -T option [rt#75473]
- ptar now supports dashless options [rt#75475]
- auto-encode filenames marked as UTF-8 [rt#75474]
1.82 21/11/2011 (CDRAKE)
- Adjustments to handle files >8gb (>0777777777777 octal)
- Feature to return the MD5SUM of files in the archive
1.80 13/10/2011
- patch from Rocky Bernstein to add file chown() method [rt#71221]
1.78 08/09/2011
- patch from Rocky Bernstein to add chown() method [rt#70623]
- blead patch from Alexandr Ciornii to resolve [perl#78708]
1.76 07/01/2011
- upstream blead patches from Peter Acklam
1.74 18/12/2010
- Skip extracting pax extended headers, reported as
RT #64038
1.72 18/11/2010
1.07 17/10/2003:
This release holds some bugfixes over the 1.06 release:
- Quell some annoying warnings about binmode on unopened filehandles
- Add tests for binary files included in a tarball
1.06 15/10/2003:
This release holds some bugfixes and new features over the 1.05 release.
- The chown() code somehow didn't make it into the 1.05 release
- Patch _get_handle() to treat all IO::File handles as binary.
This should make win32 users happy
New feature added:
- A method called 'contains_file' that will tell you if a certain file
is already in the archive.
1.05 23/8/2003:
This release holds some bugfixes and new features over the 1.04 release.
- The Test::Harness that came with perl 5.6.0 was buggy, require
a higher version in the Makefile.PL
- Add a global variable $CHOWN that controls whether Archive::Tar
should attempt to chown() files or not when it can.
1.04 27/7/2003:
This release hold a bugfix over the 1.03 release:
- NULL-byte padding was done also on files that had no real content,
like symlinks, thus ending up with a number of bytes not dividable
by 512.
1.03 26/6/2003:
lib/Archive/Tar.pm view on Meta::CPAN
use Archive::Tar;
my $tar = Archive::Tar->new;
$tar->read('origin.tgz');
$tar->extract();
$tar->add_files('file/foo.pl', 'docs/README');
$tar->add_data('file/baz.txt', 'This is the contents now');
$tar->rename('oldname', 'new/file/name');
$tar->chown('/', 'root');
$tar->chown('/', 'root:root');
$tar->chmod('/tmp', '1777');
$tar->write('files.tar'); # plain tar
$tar->write('files.tgz', COMPRESS_GZIP); # gzip compressed
$tar->write('files.tbz', COMPRESS_BZIP); # bzip2 compressed
$tar->write('files.txz', COMPRESS_XZ); # xz compressed
=head1 DESCRIPTION
Archive::Tar provides an object oriented mechanism for handling tar
lib/Archive/Tar.pm view on Meta::CPAN
}
unless ( -d _ ) {
eval { File::Path::mkpath( $dir, 0, 0777 ) };
if( $@ ) {
my $fp = $entry->full_path;
$self->_error(qq[Could not create directory '$dir' for '$fp': $@]);
return;
}
### XXX chown here? that might not be the same as in the archive
### as we're only chown'ing to the owner of the file we're extracting
### not to the owner of the directory itself, which may or may not
### be another entry in the archive
### Answer: no, gnu tar doesn't do it either, it'd be the wrong
### way to go.
#if( $CHOWN && CAN_CHOWN ) {
# chown $entry->uid, $entry->gid, $dir or
# $self->_error( qq[Could not set uid/gid on '$dir'] );
#}
}
### we're done if we just needed to create a dir ###
return 1 if $entry->is_dir;
my $full = File::Spec->catfile( $dir, $file );
if( $entry->is_unknown ) {
lib/Archive/Tar.pm view on Meta::CPAN
### only update the timestamp if it's not a symlink; that will change the
### timestamp of the original. This addresses bug #33669: Could not update
### timestamp warning on symlinks
if( not -l $full and not ( $entry->is_hardlink and ON_UNIX and $EXTRACT_HARDLINK ) ) {
utime time, $entry->mtime - TIME_OFFSET, $full or
$self->_error( qq[Could not update timestamp] );
}
if( $CHOWN && CAN_CHOWN->() and not -l $full and not ( $entry->is_hardlink and ON_UNIX and $EXTRACT_HARDLINK ) ) {
CORE::chown( $entry->uid, $entry->gid, $full ) or
$self->_error( qq[Could not set uid/gid on '$full'] );
}
### only chmod if we're allowed to, but never chmod symlinks, since they'll
### change the perms on the file they're linking too...
if( $CHMOD and not -l $full and not ( $entry->is_hardlink and ON_UNIX and $EXTRACT_HARDLINK ) ) {
my $mode = $entry->mode;
unless ($SAME_PERMISSIONS) {
$mode &= ~(oct(7000) | umask);
}
CORE::chmod( $mode, $full ) or
$self->_error( qq[Could not chown '$full' to ] . $entry->mode );
}
return 1;
}
sub _make_special_file {
my $self = shift;
my $entry = shift or return;
my $file = shift; return unless defined $file;
lib/Archive/Tar.pm view on Meta::CPAN
my $self = shift;
my $file = shift; return unless defined $file;
my $mode = shift; return unless defined $mode && $mode =~ /^[0-7]{1,4}$/;
my @args = ("$mode");
my $entry = $self->_find_entry( $file ) or return;
my $x = $entry->chmod( @args );
return $x;
}
=head2 $tar->chown( $file, $uname [, $gname] )
Change owner $file to $uname and $gname.
Returns true on success and false on failure.
=cut
sub chown {
my $self = shift;
my $file = shift; return unless defined $file;
my $uname = shift; return unless defined $uname;
my @args = ($uname);
push(@args, shift);
my $entry = $self->_find_entry( $file ) or return;
my $x = $entry->chown( @args );
return $x;
}
=head2 $tar->remove (@filenamelist)
Removes any entries with names matching any of the given filenames
from the in-memory archive. Returns a list of C<Archive::Tar::File>
objects that remain.
=cut
lib/Archive/Tar.pm view on Meta::CPAN
means the symlink stays intact. Of course, you will have to pack the
file linked to as well.
This option is checked when you write out the tarfile using C<write>
or C<create_archive>.
This works just like C</bin/tar>'s C<-h> option.
=head2 $Archive::Tar::CHOWN
By default, C<Archive::Tar> will try to C<chown> your files if it is
able to. In some cases, this may not be desired. In that case, set
this variable to C<0> to disable C<chown>-ing, even if it were
possible.
The default is C<1>.
=head2 $Archive::Tar::CHMOD
By default, C<Archive::Tar> will try to C<chmod> your files to
whatever mode was specified for the particular file in the archive.
In some cases, this may not be desired. In that case, set this
variable to C<0> to disable C<chmod>-ing.
lib/Archive/Tar/File.pm view on Meta::CPAN
=cut
sub chmod {
my $self = shift;
my $mode = shift; return unless defined $mode && $mode =~ /^[0-7]{1,4}$/;
$self->{mode} = oct($mode);
return 1;
}
=head2 $bool = $file->chown( $user [, $group])
Change owner of $file to $user. If a $group is given that is changed
as well. You can also pass a single parameter with a colon separating the
use and group as in 'root:wheel'.
Returns true on success and false on failure.
=cut
sub chown {
my $self = shift;
my $uname = shift;
return unless defined $uname;
my $gname;
if (-1 != index($uname, ':')) {
($uname, $gname) = split(/:/, $uname);
} else {
$gname = shift if @_ > 0;
}
t/03_file.t view on Meta::CPAN
" get_content_by_ref ok" );
is( $obj->has_content, length($contents) ? 1 : 0,
" has_content ok" );
ok( $obj->replace_content( $replace_contents ),
" replace_content ok" );
ok( $obj->get_content eq $replace_contents, " get_content ok" );
ok( $obj->replace_content( $contents ), " replace_content ok" );
ok( $obj->get_content eq $contents, " get_content ok" );
ok( $obj->rename( $rename_path ), " rename ok" );
ok( $obj->chown( 'root' ), " chown 1 arg ok" );
is( $obj->uname, 'root', " chown to root ok" );
ok( $obj->chown( 'rocky', 'perl'), " chown 2 args ok" );
is( $obj->uname, 'rocky', " chown to rocky ok" );
is( $obj->gname, 'perl', " chown to rocky:perl ok" );
is( $obj->name, $rename_file, " name '$file' ok" );
is( $obj->prefix, $rename_dir, " prefix '$dir' ok" );
ok( $obj->rename( $unix_path ), " rename ok" );
is( $obj->name, $file, " name '$file' ok" );
is( $obj->prefix, $dir, " prefix '$dir' ok" );
### clone tests ###
my $clone = $obj->clone;
isnt( $obj, $clone, "Clone is different object" );
is_deeply( $obj, $clone, " Clone holds same data" );
t/04_resolved_issues.t view on Meta::CPAN
### matter
my $in_file = basename($0);
my $out_file = '../' . $in_file . "_$$";
ok( $tar->add_files( $in_file ),
" Added '$in_file'" );
ok( $tar->chmod( $in_file, '1777'),
" chmod 177 $in_file" );
ok( $tar->chown( $in_file, 'root' ),
" chown to root" );
ok( $tar->chown( $in_file, 'root', 'root' ),
" chown to root:root" );
ok( $tar->rename( $in_file, $out_file ),
" Renamed to '$out_file'" );
### first, test with strict extract permissions on
{ local $Archive::Tar::INSECURE_EXTRACT_MODE = 0;
### we quell the error on STDERR
local $Archive::Tar::WARN = 0;
local $Archive::Tar::WARN = 0;
( run in 0.914 second using v1.01-cache-2.11-cpan-71847e10f99 )