Archiv...Wrapper(3pm)
π Archive::Tar::Wrapper(3pm) π
okay bestie, welcome to the Archive::Tar::Wrapper(3pm) page, you're gonna love her, she's literally the girlboss of tarball handling π
β¨ NAME β¨
Archive::Tar::Wrapper β it's an API wrapper around the tar utility. that's it, that's the whole vibe. π
πΈ SYNOPSIS πΈ
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\n";
}
# 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\n";
}
# 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);
π¦ DESCRIPTION π¦
so Archive::Tar::Wrapper is an API wrapper around the tar command line program. and here's the tea: it never stores anything in memory. like, not ever. it works on temporary directory structures on disk instead. it's giving "I don't do RAM, I do disk" energy β¨
what she does is provide a mapping between the logical paths inside the tarball and the 'real' files sitting in that temporary directory on disk. iykyk.
now, how is she different from Archive::Tar? two ways, bestie:
Archive::Tar::Wrapperalmost doesn't hold anything in memory (okay except thewritemethod, see that section), and uses disk as storage instead. πArchive::Tar::Wrapperis 100% compliant with your platform'starutility, because she uses it internally. no cap. π
π METHODS π
π new
my $arch = Archive::Tar::Wrapper->new();
constructor for the tar wrapper class. she finds the tar executable by searching PATH and returning the first hit. iconic behaviour.
but say you wanna use a different tar executable? you can absolutely specify it as a parameter, bestie:
my $arch = Archive::Tar::Wrapper->new(tar => '/path/to/tar');
and since Archive::Tar::Wrapper creates temporary directories to store tar data, you can also tell her where that temp directory should live:
my $arch = Archive::Tar::Wrapper->new(tmpdir => '/path/to/tmpdir');
π pro tip from the original page: 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 the details.
you can pass additional options to the tar command using the tar_read_options and tar_write_options parameters. like:
my $arch = Archive::Tar::Wrapper->new(
tar_read_options => 'p'
);
that one will use tar xfp archive.tgz to extract the tarball instead of just tar xf archive.tgz. periodt.
GNU tar supports even more options, and you can pass those in via:
my $arch = Archive::Tar::Wrapper->new(
tar_gnu_read_options => ["--numeric-owner"],
);
similarly, tar_gnu_write_options is there for extra options for GNU tar implementations. for example, this tar object:
my $tar = Archive::Tar::Wrapper->new(
tar_gnu_write_options => ["--exclude=foo"],
);
will call the tar utility internally like:
tar cf tarfile --exclude=foo ...
...when the write method gets called. slay.
by default, the list_*() functions return only file entries β directories get suppressed, sorry girls. but if you want list_*() to return directories too, use:
my $arch = Archive::Tar::Wrapper->new(
dirs => 1
);
now, if more files get added to a tarball than the command line can handle, Archive::Tar::Wrapper switches from using:
tar cfv tarfile file1 file2 file3 ...
to:
tar cfv tarfile -T filelist
where filelist is a file containing all the files to be added. the default for this switch is 512, but you can change it by setting the parameter max_cmd_line_args:
my $arch = Archive::Tar::Wrapper->new(
max_cmd_line_args => 1024
);
the allowable parameters are, in order:
tartmpdirtar_read_optionstar_write_optionstar_gnu_read_optionstar_gnu_write_optionsmax_cmd_line_args: defaults to 512ramdisk
returns a new instance of the class. π«Ά
π¦ read
$arch->read("archive.tgz");
read() opens the given tarball, expands it into a temporary directory, and returns 1 on success or undef on failure. the temporary directory holding the tar data gets cleaned up when $arch goes out of scope. she's tidy like that π
read handles both compressed and uncompressed files. to figure out which is which, she tries to guess by extension, and then by checking the first couple of bytes in the tar file. detective energy π
if you only need a limited number of files from a tarball, you can list them right after the tarball name:
$arch->read("archive.tgz", "path/file.dat", "path/sub/another.txt");
β οΈ okay important, bestie: the file names are passed unmodified to thetarcommand. make sure the file paths match exactly what's in the tarball, otherwiseread()will fail. no fixing, no guessing, exact match only. π
π§ list_reset
$arch->list_reset()
resets the list iterator. use it before the first call to list_next(). that's the whole job, and she does it beautifully β¨
πΈ tardir
$arch->tardir();
returns the directory the tarball was unpacked in. this is sometimes useful to play dirty tricks on Archive::Tar::Wrapper by mass-manipulating unpacked files before wrapping them back up into the tarball. chaotic, love that for you π
π is_compressed
returns a string to identify if the tarball is compressed or not.
expects as parameter a string with the path to the tarball.
returns:
- a
"z"character if the file is compressed withgzip. - a
"j"character if the file is compressed withbzip2. - a
""character if the file is not compressed at all.
π locate
$arch->locate($logic_path);
finds the physical location of a file, specified by $logic_path, which is the virtual path of the file within the tarball. returns a path to the temporary file Archive::Tar::Wrapper created to manipulate the tarball on disk. she's basically your personal GPS πΊοΈβ¨
π add
$arch->add($logic_path, $file_or_stringref, [$options]);
add a new file to the tarball. $logic_path is the virtual path of the file within the tarball. $file_or_stringref 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's a reference to a scalar, in which case its content is interpreted to be the data of the file. two modes, both iconic π
if no additional parameters are given, permissions and user/group id settings of a file to be added are copied over. but if you want different settings, just specify them in the options hash:
$arch->add($logic_path, $stringref,
{ perm => 0755, uid => 123, gid => 10 });
and if $file_or_stringref is a reference to a Unicode string, the binmode option has to be set to make sure the string gets written as proper UTF-8 into the tar file:
$arch->add($logic_path, $stringref, { binmode => ":utf8" });
π perm_cp
copies the permissions from a file to another.
expects as parameters:
- string of the path to the file which permissions will be copied from.
- string of the path to the file which permissions will be copied to.
returns 1 if everything works as expected.
β perm_get
gets the permissions from a file.
expects as parameter the path to the source file.
returns an array reference with only the permissions values, as returned by stat.
π perm_set
sets the permission on a file.
expects as parameters:
- the path to the file where the permissions should be applied to.
- an array reference with the permissions (see
"perm_set")
returns 1 if everything goes fine.
β οΈ ignore errors here, bestie, because we can't change uid/gid unless we're the superuser (see LIMITATIONS section).
π remove
$arch->remove($logic_path);
removes a file from the tarball. $logic_path is the virtual path of the file within the tarball. bye girl πβ¨
π¦ list_all
my $items = $arch->list_all();
returns a reference to a (possibly huge) array of items in the tar file. each item is a reference to an array containing two elements: the relative path of the item in the tar file, and the physical path to the unpacked file or directory on disk.
to iterate over the list, you can use this construct:
# 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\n";
}
π‘ if the list of items in the tar file is big, uselist_reset()andlist_next()instead oflist_all. your RAM will thank you π
π§ list_next
my ($tar_path, $phys_path, $type) = $arch->list_next();
returns the next item in the tar file. it returns a list of three scalars: the relative path of the item in the tar file, the physical path to the unpacked file or directory on disk, and the type of the entry (f=file, d=directory, l=symlink). ngl the type field is a little sneaky detail that people forget about.
note that by default, Archive::Tar::Wrapper won't display directories, unless the dirs parameter is set when running the constructor. π
π write
$arch->write($tarfile, $compress);
write out the tarball by tarring up all temporary files and directories and store it in $tarfile on disk. if $compress holds a true value, compression is used. that's the moment she eats and leaves no crumbs π₯
π is_gnu
$arch->is_gnu();
checks if the tar executable is a GNU tar by running tar --version and parsing the output for "GNU".
returns true or false (in Perl terms). β¨
πΈ is_bsd
$arch->is_bsd();
same as is_gnu(), but for BSD. she's inclusive like that π
π ramdisk_mount
mounts a RAM disk.
it executes the mount program under the hood to mount a RAM disk.
expects as parameter a hash with options to mount the RAM disk, like:
sizetype(most probablytmpfs)tmpdir
returns 1 if everything goes fine.
π‘ be sure to check the "Using RAM Disks" in Archive::Tar::Wrapper for full details on using RAM disks.
π ramdisk_unmount
unmounts the RAM disk already mounted with ramdisk_mount.
don't expect parameters and returns 1 if everything goes fine.
π‘ be sure to check the "Using RAM Disks" in Archive::Tar::Wrapper for full details on using RAM disks.
π Using RAM Disks π
okay bestie, listen up, because this one is genuinely a glow-up. On Linux, it is so easy to spin up a RAM disk, and the speedup you get while untarring or modifying a tarball is, like, unreal. π
You can either make the RAM disk by hand, like a grown-up:
# mkdir -p /mnt/myramdisk
# mount -t tmpfs -o size=20m tmpfs /mnt/myramdisk
and then hand that RAM disk over to Archive::Tar::Wrapper as a temporary directory, like:
my $tar = Archive::Tar::Wrapper->new( tmpdir => '/mnt/myramdisk' );
or you can just use Archive::Tar::Wrapper's built-in ramdisk option and let it cook:
my $tar = Archive::Tar::Wrapper->new(
ramdisk => {
type => 'tmpfs',
size => '20m', # 20 MB
},
);
The only drawback with that second option? Creating the RAM disk has to be done as root, and ngl that's often not the vibe for security reasons. π So for that exact reason, Archive::Tar::Wrapper offers a couple of utility functions that mount the RAM disk and hand you back the temporary directory it lives in:
# Create new ramdisk (as root):
my $tmpdir = Archive::Tar::Wrapper->ramdisk_mount(
type => 'tmpfs',
size => '20m', # 20 MB
);
# Delete a ramdisk (as root):
Archive::Tar::Wrapper->ramdisk_unmount();
Optionally, the ramdisk_mount() command accepts a tmpdir parameter pointing to a temporary directory for the RAM disk, in case you wanna set it yourself instead of letting Archive::Tar::Wrapper create it automatically. β¨
π KNOWN LIMITATIONS π
ik, ik, she's not perfect. here's the tea:
- Currently, only
tarprograms supporting thezoption (for compressing/decompressing) are supported. Future version will usegzipalternatively. - Currently, you can't add empty directories to a tarball directly. You could add a temporary file within a directory, and then
remove()the file. π« - If you delete a file, the empty directories it was located in stay in the tarball. You could try to
locate()them and delete them. This will be fixed, though. - Filenames containing newlines are causing problems with the list iterators. To be fixed.
- If you ask
Archive::Tar::Wrapperto add a file to a tarball, it copies it into a temporary directory and then calls the systemtarto wrap up that directory into a tarball.
This approach has limitations when it comes to file permissions: If the file to be added belongs to a different user/group, Archive::Tar::Wrapper 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 Archive::Tar::Wrapper) 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).
π BUGS π
Archive::Tar::Wrapper doesn't currently handle filenames with embedded newlines. fr fr, that's the whole bug.
πͺ Microsoft Windows support πͺ
Support on Microsoft Windows is limited. π
Versions below Windows 10 will not be supported for desktops, and for servers only Windows 2012 and above.
The GNU tar.exe program doesn't work properly with the current interface of Archive::Tar::Wrapper.
You must use the bsdtar.exe and make sure it appears first in the PATH environment variable than the GNU tar (if it is installed). See http://libarchive.org/ for details about how to download and install bsdtar.exe, or go to http://gnuwin32.sourceforge.net/packages.html for a direct download. Be sure to look for the bzip2 program package to install it as well.
Windows 10 might come already with bsdtar program already installed. Please search for that on the appropriate page (Microsoft keeps changing the link to keep track of it here). π
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 C:\Program Files. Installing both bsdtar and bzip2 in C:\GnuWin32 will probably be enough when running the installers. β¨
π SEE ALSO π
- Linux Gazette article from Ben Okopnik, issue 87
https://linuxgazette.net/87/okopnik.html.
π BUGS π
Please report any bugs or feature requests on the bugtracker website https://github.com/haarg/Archive-Tar-Wrapper/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. bestie, receipts help everyone. π«Ά
π AUTHORS π
- Mike Schilli
<cpan@perlmeister.com> - Alceu Rodrigues de Freitas Junior
<glasswalk3r@yahoo.com.br>
π CONTRIBUTORS π
- Chris Weyl
<cweyl@alumni.drew.edu> - David Cantrell
<david@cantrell.org.uk> - David Precious
<davidp@preshweb.co.uk> - Graham Knop
<haarg@haarg.org> - intrigeri
<intrigeri@boum.org> - Kent Fredric
<kentfredric@gmail.com> - Mark Gardner
<mjg+github@phoenixtrap.com> - Mike Schilli
<github@perlmeister.com> - Mohammad S Anwar
<mohammad.anwar@yahoo.com> - Paulo Custodio
<pauloscustodio@gmail.com> - Randy Stauner
<randy@magnificent-tears.com> - Sanko Robinson
<sanko@cpan.org> - Shoichi Kaji
<skaji@cpan.org>
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2024 by Mike Schilli.
This is free software, licensed under:
The GNU General Public License, Version 3, June 2007
and that's a wrap, bestie!! π this page was served up by perl v5.40.0, dated 2024-09-01, from the Archiv...Wrapper(3pm) page in the User Contributed Perl Documentation. slay, go tar something. πβ¨π¦
π¦ where this page comes from
- ubuntu resolute: libarchive-tar-wrapper-perl 0.42-1 Β· GPL-3.0-or-later
- ubuntu stonking: libarchive-tar-wrapper-perl 0.42-1 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms