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
if ($file eq '-') {
use IO::Handle;
$file = IO::Handle->new();
$file->fdopen(fileno(STDOUT),"w");
}
my $tar = Archive::Tar->new;
$tar->add_files(@files);
if( $opts->{C} ) {
for my $f ($tar->get_files) {
$f->mode($f->mode & ~022); # chmod go-w
}
}
$tar->write($file, $compress);
} else {
if ($file eq '-') {
use IO::Handle;
$file = IO::Handle->new();
$file->fdopen(fileno(STDIN),"r");
}
lib/Archive/Tar.pm view on Meta::CPAN
$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
files. It provides class methods for quick and easy files handling
lib/Archive/Tar.pm view on Meta::CPAN
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
$err = qq[Making symbolic link '$file' to '] .
$entry->linkname .q[' failed] if $fail;
} elsif ( $entry->is_hardlink ) {
if( !$INSECURE_EXTRACT_MODE ) {
my $linkname = $entry->linkname;
if( File::Spec->file_name_is_absolute($linkname) ) {
$self->_error( qq[Hardlink '] . $entry->full_path .
qq[' has absolute target '$linkname'. Not extracting ] .
qq[under SECURE EXTRACT MODE: extraction itself chmods ] .
qq[the shared inode.] );
return;
}
if( grep { $_ eq '..' } File::Spec->splitdir($linkname) ) {
$self->_error( qq[Hardlink '] . $entry->full_path .
qq[' target '$linkname' attempts traversal. Not ] .
qq[extracting under SECURE EXTRACT MODE: extraction ] .
qq[itself chmods the shared inode.] );
return;
}
}
my $fail;
if( ON_UNIX && $EXTRACT_HARDLINK ) {
link( $entry->linkname, $file ) or $fail++;
} else {
$self->_extract_special_file_as_plain_file( $entry, $file )
or $fail++;
lib/Archive/Tar.pm view on Meta::CPAN
sub rename {
my $self = shift;
my $file = shift; return unless defined $file;
my $new = shift; return unless defined $new;
my $entry = $self->_find_entry( $file ) or return;
return $entry->rename( $new );
}
=head2 $tar->chmod( $file, $mode )
Change mode of $file to $mode.
Returns true on success and false on failure.
=cut
sub chmod {
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
lib/Archive/Tar.pm view on Meta::CPAN
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.
The default is C<1>.
=head2 $Archive::Tar::SAME_PERMISSIONS
When, C<$Archive::Tar::CHMOD> is enabled, this setting controls whether
the permissions on files from the archive are used without modification
of if they are filtered by removing any setid bits and applying the
current umask.
lib/Archive/Tar/File.pm view on Meta::CPAN
return unless defined $path;
my ($prefix,$file) = $self->_prefix_and_file( $path );
$self->name( $file );
$self->prefix( $prefix );
return 1;
}
=head2 $bool = $file->chmod( $mode )
Change mode of $file to $mode. The mode can be a string or a number
which is interpreted as octal whether or not a leading 0 is given.
Returns true on success and false on failure.
=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
t/02_methods.t view on Meta::CPAN
my $TBZ_FILE = File::Spec->catfile( @ROOT, 'foo.tbz' );
my $TXZ_FILE = File::Spec->catfile( @ROOT, 'foo.txz' );
my $OUT_TAR_FILE = File::Spec->catfile( @ROOT, 'out.tar' );
my $OUT_TGZ_FILE = File::Spec->catfile( @ROOT, 'out.tgz' );
my $OUT_TBZ_FILE = File::Spec->catfile( @ROOT, 'out.tbz' );
my $OUT_TXZ_FILE = File::Spec->catfile( @ROOT, 'out.txz' );
my $COMPRESS_FILE = 'copy';
$^O eq 'VMS' and $COMPRESS_FILE .= '.';
copy( File::Basename::basename($0), $COMPRESS_FILE );
chmod 0644, $COMPRESS_FILE;
### done setting up environment ###
### check for zlib/bzip2/xz support
{ for my $meth ( qw[has_zlib_support has_bzip2_support has_xz_support] ) {
can_ok( $Class, $meth );
}
}
t/04_resolved_issues.t view on Meta::CPAN
isa_ok( $tar, $Class, " Object" );
### absolute paths are already taken care of. Only relative paths
### 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'" );
( run in 2.336 seconds using v1.01-cache-2.11-cpan-fe3c2283af0 )