.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Archive::Tar::Wrapper 3pm"
.TH Archive::Tar::Wrapper 3pm 2024-04-19 "perl v5.38.2" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
Archive::Tar::Wrapper \- API wrapper around the 'tar' utility
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 1
\& use Archive::Tar::Wrapper;
\&
\& my $arch = Archive::Tar::Wrapper\->new();
\&
\& # Open a tarball, expand it into a temporary directory
\& $arch\->read("archive.tgz");
\&
\& # Iterate over all entries in the archive
\& $arch\->list_reset(); # Reset Iterator
\&
\& # Iterate through archive
\& while(my $entry = $arch\->list_next()) {
\& my($tar_path, $phys_path) = @$entry;
\& print "$tar_path\en";
\& }
\&
\& # Get a huge list with all entries
\& for my $entry (@{$arch\->list_all()}) {
\& my($tar_path, $real_path) = @$entry;
\& print "Tarpath: $tar_path Tempfile: $real_path\en";
\& }
\&
\& # Add a new entry
\& $arch\->add($logic_path, $file_or_stringref);
\&
\& # Remove an entry
\& $arch\->remove($logic_path);
\&
\& # Find the physical location of a temporary file
\& my($tmp_path) = $arch\->locate($tar_path);
\&
\& # Create a tarball
\& $arch\->write($tarfile, $compress);
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
\&\fBArchive::Tar::Wrapper\fR is an API wrapper around the \f(CW\*(C`tar\*(C'\fR command line
program. It never stores anything in memory, but works on temporary
directory structures on disk instead. It provides a mapping between
the logical paths in the tarball and the 'real' files in the temporary
directory on disk.
.PP
It differs from Archive::Tar in two ways:
.IP \(bu 4
\&\fBArchive::Tar::Wrapper\fR almost doesn't hold anything in memory (see \f(CW\*(C`write\*(C'\fR method),
instead using disk as storage.
.IP \(bu 4
\&\fBArchive::Tar::Wrapper\fR is 100% compliant with the platform's \f(CW\*(C`tar\*(C'\fR
utility because it uses it internally.
.SH METHODS
.IX Header "METHODS"
.SS new
.IX Subsection "new"
.Vb 1
\& my $arch = Archive::Tar::Wrapper\->new();
.Ve
.PP
Constructor for the \f(CW\*(C`tar\*(C'\fR wrapper class. Finds the \f(CW\*(C`tar\*(C'\fR executable
by searching \f(CW\*(C`PATH\*(C'\fR and returning the first hit. In case you want
to use a different tar executable, you can specify it as a parameter:
.PP
.Vb 1
\& my $arch = Archive::Tar::Wrapper\->new(tar => \*(Aq/path/to/tar\*(Aq);
.Ve
.PP
Since \fBArchive::Tar::Wrapper\fR creates temporary directories to store
\&\f(CW\*(C`tar\*(C'\fR data, the location of the temporary directory can be specified:
.PP
.Vb 1
\& my $arch = Archive::Tar::Wrapper\->new(tmpdir => \*(Aq/path/to/tmpdir\*(Aq);
.Ve
.PP
Tremendous performance increases can be achieved if the temporary
directory is located on a RAM disk. Check the "Using RAM Disks" in Archive::Tar::Wrapper
section for details.
.PP
Additional options can be passed to the \f(CW\*(C`tar\*(C'\fR command by using the
\&\f(CW\*(C`tar_read_options\*(C'\fR and \f(CW\*(C`tar_write_options\*(C'\fR parameters. Example:
.PP
.Vb 3
\& my $arch = Archive::Tar::Wrapper\->new(
\& tar_read_options => \*(Aqp\*(Aq
\& );
.Ve
.PP
will use \f(CW\*(C`tar xfp archive.tgz\*(C'\fR to extract the tarball instead of just
\&\f(CW\*(C`tar xf archive.tgz\*(C'\fR. GNU tar supports even more options, these can
be passed in via
.PP
.Vb 3
\& my $arch = Archive::Tar::Wrapper\->new(
\& tar_gnu_read_options => ["\-\-numeric\-owner"],
\& );
.Ve
.PP
Similarly, \f(CW\*(C`tar_gnu_write_options\*(C'\fR can be used to provide additional
options for GNU tar implementations. For example, the tar object
.PP
.Vb 3
\& my $tar = Archive::Tar::Wrapper\->new(
\& tar_gnu_write_options => ["\-\-exclude=foo"],
\& );
.Ve
.PP
will call the \f(CW\*(C`tar\*(C'\fR utility internally like
.PP
.Vb 1
\& tar cf tarfile \-\-exclude=foo ...
.Ve
.PP
when the \f(CW\*(C`write\*(C'\fR method gets called.
.PP
By default, the \f(CW\*(C`list_*()\*(C'\fR functions will return only file entries:
directories will be suppressed. To have \f(CW\*(C`list_*()\*(C'\fR return directories
as well, use
.PP
.Vb 3
\& my $arch = Archive::Tar::Wrapper\->new(
\& dirs => 1
\& );
.Ve
.PP
If more files are added to a tarball than the command line can handle,
\&\fBArchive::Tar::Wrapper\fR will switch from using the command
.PP
.Vb 1
\& tar cfv tarfile file1 file2 file3 ...
.Ve
.PP
to
.PP
.Vb 1
\& tar cfv tarfile \-T filelist
.Ve
.PP
where \f(CW\*(C`filelist\*(C'\fR is a file containing all file to be added. The default
for this switch is 512, but it can be changed by setting the parameter
\&\f(CW\*(C`max_cmd_line_args\*(C'\fR:
.PP
.Vb 3
\& my $arch = Archive::Tar::Wrapper\->new(
\& max_cmd_line_args => 1024
\& );
.Ve
.PP
The expectable parameters are:
.IP \(bu 4
tar
.IP \(bu 4
tmpdir
.IP \(bu 4
tar_read_options
.IP \(bu 4
tar_write_options
.IP \(bu 4
tar_gnu_read_options
.IP \(bu 4
tar_gnu_write_options
.IP \(bu 4
max_cmd_line_args: defaults to 512
.IP \(bu 4
ramdisk
.PP
Returns a new instance of the class.
.SS read
.IX Subsection "read"
.Vb 1
\& $arch\->read("archive.tgz");
.Ve
.PP
\&\f(CWread()\fR opens the given tarball, expands it into a temporary directory
and returns 1 on success or \f(CW\*(C`undef\*(C'\fR on failure.
The temporary directory holding the tar data gets cleaned up when \f(CW$arch\fR
goes out of scope.
.PP
\&\f(CW\*(C`read\*(C'\fR handles both compressed and uncompressed files. To find out if
a file is compressed or uncompressed, it tries to guess by extension,
then by checking the first couple of bytes in the tarfile.
.PP
If only a limited number of files is needed from a tarball, they
can be specified after the tarball name:
.PP
.Vb 1
\& $arch\->read("archive.tgz", "path/file.dat", "path/sub/another.txt");
.Ve
.PP
The file names are passed unmodified to the \f(CW\*(C`tar\*(C'\fR command, make sure
that the file paths match exactly what's in the tarball, otherwise
\&\f(CWread()\fR will fail.
.SS list_reset
.IX Subsection "list_reset"
.Vb 1
\& $arch\->list_reset()
.Ve
.PP
Resets the list iterator. To be used before the first call to \f(CWlist_next()\fR.
.SS tardir
.IX Subsection "tardir"
.Vb 1
\& $arch\->tardir();
.Ve
.PP
Return the directory the tarball was unpacked in. This is sometimes useful
to play dirty tricks on \fBArchive::Tar::Wrapper\fR by mass-manipulating
unpacked files before wrapping them back up into the tarball.
.SS is_compressed
.IX Subsection "is_compressed"
Returns a string to identify if the tarball is compressed or not.
.PP
Expect as parameter a string with the path to the tarball.
.PP
Returns:
.IP \(bu 4
a "z" character if the file is compressed with gzip.
.IP \(bu 4
a "j" character if the file is compressed with bzip2.
.IP \(bu 4
a "" character if the file is not compressed at all.
.SS locate
.IX Subsection "locate"
.Vb 1
\& $arch\->locate($logic_path);
.Ve
.PP
Finds the physical location of a file, specified by \f(CW$logic_path\fR, which
is the virtual path of the file within the tarball. Returns a path to
the temporary file \fBArchive::Tar::Wrapper\fR created to manipulate the
tarball on disk.
.SS add
.IX Subsection "add"
.Vb 1
\& $arch\->add($logic_path, $file_or_stringref, [$options]);
.Ve
.PP
Add a new file to the tarball. \f(CW$logic_path\fR is the virtual path
of the file within the tarball. \f(CW$file_or_stringref\fR is either
a scalar, in which case it holds the physical path of a file
on disk to be transferred (i.e. copied) to the tarball, or it is
a reference to a scalar, in which case its content is interpreted
to be the data of the file.
.PP
If no additional parameters are given, permissions and user/group
id settings of a file to be added are copied. If you want different
settings, specify them in the options hash:
.PP
.Vb 2
\& $arch\->add($logic_path, $stringref,
\& { perm => 0755, uid => 123, gid => 10 });
.Ve
.PP
If \f(CW$file_or_stringref\fR is a reference to a Unicode string, the \f(CW\*(C`binmode\*(C'\fR
option has to be set to make sure the string gets written as proper UTF\-8
into the tarfile:
.PP
.Vb 1
\& $arch\->add($logic_path, $stringref, { binmode => ":utf8" });
.Ve
.SS perm_cp
.IX Subsection "perm_cp"
Copies the permissions from a file to another.
.PP
Expects as parameters:
.IP 1. 4
string of the path to the file which permissions will be copied from.
.IP 2. 4
string of the path to the file which permissions will be copied to.
.PP
Returns 1 if everything works as expected.
.SS perm_get
.IX Subsection "perm_get"
Gets the permissions from a file.
.PP
Expects as parameter the path to the source file.
.PP
Returns an array reference with only the permissions values, as returned by \f(CW\*(C`stat\*(C'\fR.
.SS perm_set
.IX Subsection "perm_set"
Sets the permission on a file.
.PP
Expects as parameters:
.IP 1. 4
The path to the file where the permissions should be applied to.
.IP 2. 4
An array reference with the permissions (see \f(CW\*(C`perm_set\*(C'\fR)
.PP
Returns 1 if everything goes fine.
.PP
Ignore errors here, as we can't change uid/gid unless we're the superuser (see LIMITATIONS section).
.SS remove
.IX Subsection "remove"
.Vb 1
\& $arch\->remove($logic_path);
.Ve
.PP
Removes a file from the tarball. \f(CW$logic_path\fR is the virtual path
of the file within the tarball.
.SS list_all
.IX Subsection "list_all"
.Vb 1
\& my $items = $arch\->list_all();
.Ve
.PP
Returns a reference to a (possibly huge) array of items in the
tarfile. Each item is a reference to an array, containing two
elements: the relative path of the item in the tarfile and the
physical path to the unpacked file or directory on disk.
.PP
To iterate over the list, the following construct can be used:
.PP
.Vb 5
\& # Get a huge list with all entries
\& for my $entry (@{$arch\->list_all()}) {
\& my($tar_path, $real_path) = @$entry;
\& print "Tarpath: $tar_path Tempfile: $real_path\en";
\& }
.Ve
.PP
If the list of items in the tarfile is big, use \f(CWlist_reset()\fR and
\&\f(CWlist_next()\fR instead of \f(CW\*(C`list_all\*(C'\fR.
.SS list_next
.IX Subsection "list_next"
.Vb 1
\& my ($tar_path, $phys_path, $type) = $arch\->list_next();
.Ve
.PP
Returns the next item in the tarfile. It returns a list of three scalars:
the relative path of the item in the tarfile, the physical path
to the unpacked file or directory on disk, and the type of the entry
(f=file, d=directory, l=symlink). Note that by default,
\&\fBArchive::Tar::Wrapper\fR won't display directories, unless the \f(CW\*(C`dirs\*(C'\fR
parameter is set when running the constructor.
.SS write
.IX Subsection "write"
.Vb 1
\& $arch\->write($tarfile, $compress);
.Ve
.PP
Write out the tarball by tarring up all temporary files and directories
and store it in \f(CW$tarfile\fR on disk. If \f(CW$compress\fR holds a true value,
compression is used.
.SS is_gnu
.IX Subsection "is_gnu"
.Vb 1
\& $arch\->is_gnu();
.Ve
.PP
Checks if the tar executable is a GNU tar by running 'tar \-\-version'
and parsing the output for "GNU".
.PP
Returns true or false (in Perl terms).
.SS is_bsd
.IX Subsection "is_bsd"
.Vb 1
\& $arch\->is_bsd();
.Ve
.PP
Same as \f(CWis_gnu()\fR, but for BSD.
.SS ramdisk_mount
.IX Subsection "ramdisk_mount"
Mounts a RAM disk.
.PP
It executes the \f(CW\*(C`mount\*(C'\fR program under the hood to mount a RAM disk.
.PP
Expects as parameter a hash with options to mount the RAM disk, like:
.IP \(bu 4
size
.IP \(bu 4
type (most probably \f(CW\*(C`tmpfs\*(C'\fR)
.IP \(bu 4
tmpdir
.PP
Returns 1 if everything goes fine.
.PP
Be sure to check the "Using RAM Disks" in Archive::Tar::Wrapper for full details on using RAM disks.
.SS ramdisk_unmount
.IX Subsection "ramdisk_unmount"
Unmounts the RAM disk already mounted with \f(CW\*(C`ramdisk_mount\*(C'\fR.
.PP
Don't expect parameters and returns 1 if everything goes fine.
.PP
Be sure to check the "Using RAM Disks" in Archive::Tar::Wrapper for full details on using RAM disks.
.SH "Using RAM Disks"
.IX Header "Using RAM Disks"
On Linux, it's quite easy to create a RAM disk and achieve tremendous
speedups while untarring or modifying a tarball. You can either
create the RAM disk by hand by running
.PP
.Vb 2
\& # mkdir \-p /mnt/myramdisk
\& # mount \-t tmpfs \-o size=20m tmpfs /mnt/myramdisk
.Ve
.PP
and then feeding the ramdisk as a temporary directory to
\&\fBArchive::Tar::Wrapper\fR, like
.PP
.Vb 1
\& my $tar = Archive::Tar::Wrapper\->new( tmpdir => \*(Aq/mnt/myramdisk\*(Aq );
.Ve
.PP
or using \fBArchive::Tar::Wrapper\fR's built-in option \f(CW\*(C`ramdisk\*(C'\fR:
.PP
.Vb 6
\& my $tar = Archive::Tar::Wrapper\->new(
\& ramdisk => {
\& type => \*(Aqtmpfs\*(Aq,
\& size => \*(Aq20m\*(Aq, # 20 MB
\& },
\& );
.Ve
.PP
Only drawback with the latter option is that creating the RAM disk needs
to be performed as root, which often isn't desirable for security reasons.
For this reason, \fBArchive::Tar::Wrapper\fR offers a utility functions that
mounts the RAM disk and returns the temporary directory it's located in:
.PP
.Vb 5
\& # Create new ramdisk (as root):
\& my $tmpdir = Archive::Tar::Wrapper\->ramdisk_mount(
\& type => \*(Aqtmpfs\*(Aq,
\& size => \*(Aq20m\*(Aq, # 20 MB
\& );
\&
\& # Delete a ramdisk (as root):
\& Archive::Tar::Wrapper\->ramdisk_unmount();
.Ve
.PP
Optionally, the \f(CWramdisk_mount()\fR command accepts a \f(CW\*(C`tmpdir\*(C'\fR parameter
pointing to a temporary directory for the RAM disk if you wish to set it
yourself instead of letting \fBArchive::Tar::Wrapper\fR create it automatically.
.SH "KNOWN LIMITATIONS"
.IX Header "KNOWN LIMITATIONS"
.IP \(bu 4
Currently, only \f(CW\*(C`tar\*(C'\fR programs supporting the \f(CW\*(C`z\*(C'\fR option (for
compressing/decompressing) are supported. Future version will use
\&\f(CW\*(C`gzip\*(C'\fR alternatively.
.IP \(bu 4
Currently, you can't add empty directories to a tarball directly.
You could add a temporary file within a directory, and then
\&\f(CWremove()\fR the file.
.IP \(bu 4
If you delete a file, the empty directories it was located in
stay in the tarball. You could try to \f(CWlocate()\fR them and delete
them. This will be fixed, though.
.IP \(bu 4
Filenames containing newlines are causing problems with the list
iterators. To be fixed.
.IP \(bu 4
If you ask \fBArchive::Tar::Wrapper\fR to add a file to a tarball, it copies it
into a temporary directory and then calls the system tar to wrap up that
directory into a tarball.
.Sp
This approach has limitations when it comes to file permissions: If the file to
be added belongs to a different user/group, \fBArchive::Tar::Wrapper\fR will adjust
the uid/gid/permissions of the target file in the temporary directory to
reflect the original file's settings, to make sure the system tar will add it
like that to the tarball, just like a regular tar run on the original file
would. But this will fail of course if the original file's uid is different
from the current user's, unless the script is running with superuser rights.
The tar program by itself (without \fBArchive::Tar::Wrapper\fR) works differently:
It'll just make a note of a file's uid/gid/permissions in the tarball (which it
can do without superuser rights) and upon extraction, it'll adjust the
permissions of newly generated files if the \-p option is given (default for
superuser).
.SH BUGS
.IX Header "BUGS"
\&\fBArchive::Tar::Wrapper\fR doesn't currently handle filenames with embedded
newlines.
.SS "Microsoft Windows support"
.IX Subsection "Microsoft Windows support"
Support on Microsoft Windows is limited.
.PP
Versions below Windows 10 will not be supported for desktops, and for servers
only Windows 2012 and above.
.PP
The GNU \f(CW\*(C`tar.exe\*(C'\fR program doesn't work properly with the current interface of
\&\fBArchive::Tar::Wrapper\fR.
.PP
You must use the \f(CW\*(C`bsdtar.exe\*(C'\fR and make sure it appears first in the \f(CW\*(C`PATH\*(C'\fR
environment variable than the GNU tar (if it is installed). See
for details about how to download and
install \f(CW\*(C`bsdtar.exe\*(C'\fR, or go to
for a direct download. Be sure to look for the \f(CW\*(C`bzip2\*(C'\fR program package to
install it as well.
.PP
Windows 10 might come already with \f(CW\*(C`bsdtar\*(C'\fR program already installed. Please
search for that on the appropriate page (Microsoft keeps changing the link to
keep track of it here).
.PP
Having spaces in the path string to the tar program might be an issue too.
Although there is some effort in terms of workaround it, you best might avoid it
completely by installing in a different path than \f(CW\*(C`C:\eProgram Files\*(C'\fR.
Installing both \f(CW\*(C`bsdtar\*(C'\fR and \f(CW\*(C`bzip2\*(C'\fR in \f(CW\*(C`C:\eGnuWin32\*(C'\fR will probably be enough
when running the installers.
.SH LEGALESE
.IX Header "LEGALESE"
This software is copyright (c) 2005 of Mike Schilli.
.PP
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
.PP
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
.PP
You should have received a copy of the GNU General Public License along with
Archive-Tar-Wrapper. If not, see .
.SH "SEE ALSO"
.IX Header "SEE ALSO"
.IP \(bu 4
Linux Gazette article from Ben Okopnik, issue 87 .
.SH AUTHOR
.IX Header "AUTHOR"
2005, Mike Schilli
.SH MAINTAINER
.IX Header "MAINTAINER"
2018, Alceu Rodrigues de Freitas Junior