Debian Codegraph @

README

raw
1 NAME
2 Archive::Tar::Wrapper - API wrapper around the 'tar' utility
3
4 SYNOPSIS
5 use Archive::Tar::Wrapper;
6
7 my $arch = Archive::Tar::Wrapper->new();
8
9 # Open a tarball, expand it into a temporary directory
10 $arch->read("archive.tgz");
11
12 # Iterate over all entries in the archive
13 $arch->list_reset(); # Reset Iterator
14
15 # Iterate through archive
16 while(my $entry = $arch->list_next()) {
17 my($tar_path, $phys_path) = @$entry;
18 print "$tar_path\n";
19 }
20
21 # Get a huge list with all entries
22 for my $entry (@{$arch->list_all()}) {
23 my($tar_path, $real_path) = @$entry;
24 print "Tarpath: $tar_path Tempfile: $real_path\n";
25 }
26
27 # Add a new entry
28 $arch->add($logic_path, $file_or_stringref);
29
30 # Remove an entry
31 $arch->remove($logic_path);
32
33 # Find the physical location of a temporary file
34 my($tmp_path) = $arch->locate($tar_path);
35
36 # Create a tarball
37 $arch->write($tarfile, $compress);
38
39 DESCRIPTION
40 Archive::Tar::Wrapper is an API wrapper around the "tar" command line
41 program. It never stores anything in memory, but works on temporary
42 directory structures on disk instead. It provides a mapping between the
43 logical paths in the tarball and the 'real' files in the temporary
44 directory on disk.
45
46 It differs from Archive::Tar in two ways:
47
48 * Archive::Tar::Wrapper almost doesn't hold anything in memory (see
49 "write" method), instead using disk as storage.
50
51 * Archive::Tar::Wrapper is 100% compliant with the platform's "tar"
52 utility because it uses it internally.
53
54 METHODS
55 new
56 my $arch = Archive::Tar::Wrapper->new();
57
58 Constructor for the "tar" wrapper class. Finds the "tar" executable by
59 searching "PATH" and returning the first hit. In case you want to use a
60 different tar executable, you can specify it as a parameter:
61
62 my $arch = Archive::Tar::Wrapper->new(tar => '/path/to/tar');
63
64 Since Archive::Tar::Wrapper creates temporary directories to store "tar"
65 data, the location of the temporary directory can be specified:
66
67 my $arch = Archive::Tar::Wrapper->new(tmpdir => '/path/to/tmpdir');
68
69 Tremendous performance increases can be achieved if the temporary
70 directory is located on a RAM disk. Check the "Using RAM Disks" in
71 Archive::Tar::Wrapper section for details.
72
73 Additional options can be passed to the "tar" command by using the
74 "tar_read_options" and "tar_write_options" parameters. Example:
75
76 my $arch = Archive::Tar::Wrapper->new(
77 tar_read_options => 'p'
78 );
79
80 will use "tar xfp archive.tgz" to extract the tarball instead of just
81 "tar xf archive.tgz". GNU tar supports even more options, these can be
82 passed in via
83
84 my $arch = Archive::Tar::Wrapper->new(
85 tar_gnu_read_options => ["--numeric-owner"],
86 );
87
88 Similarly, "tar_gnu_write_options" can be used to provide additional
89 options for GNU tar implementations. For example, the tar object
90
91 my $tar = Archive::Tar::Wrapper->new(
92 tar_gnu_write_options => ["--exclude=foo"],
93 );
94
95 will call the "tar" utility internally like
96
97 tar cf tarfile --exclude=foo ...
98
99 when the "write" method gets called.
100
101 By default, the "list_*()" functions will return only file entries:
102 directories will be suppressed. To have "list_*()" return directories as
103 well, use
104
105 my $arch = Archive::Tar::Wrapper->new(
106 dirs => 1
107 );
108
109 If more files are added to a tarball than the command line can handle,
110 Archive::Tar::Wrapper will switch from using the command
111
112 tar cfv tarfile file1 file2 file3 ...
113
114 to
115
116 tar cfv tarfile -T filelist
117
118 where "filelist" is a file containing all file to be added. The default
119 for this switch is 512, but it can be changed by setting the parameter
120 "max_cmd_line_args":
121
122 my $arch = Archive::Tar::Wrapper->new(
123 max_cmd_line_args => 1024
124 );
125
126 The allowable parameters are:
127
128 * "tar"
129
130 * "tmpdir"
131
132 * "tar_read_options"
133
134 * "tar_write_options"
135
136 * "tar_gnu_read_options"
137
138 * "tar_gnu_write_options"
139
140 * "max_cmd_line_args": defaults to 512
141
142 * "ramdisk"
143
144 Returns a new instance of the class.
145
146 read
147 $arch->read("archive.tgz");
148
149 read() opens the given tarball, expands it into a temporary directory
150 and returns 1 on success or "undef" on failure. The temporary directory
151 holding the tar data gets cleaned up when $arch goes out of scope.
152
153 "read" handles both compressed and uncompressed files. To find out if a
154 file is compressed or uncompressed, it tries to guess by extension, then
155 by checking the first couple of bytes in the tar file.
156
157 If only a limited number of files is needed from a tarball, they can be
158 specified after the tarball name:
159
160 $arch->read("archive.tgz", "path/file.dat", "path/sub/another.txt");
161
162 The file names are passed unmodified to the "tar" command, make sure
163 that the file paths match exactly what's in the tarball, otherwise
164 read() will fail.
165
166 list_reset
167 $arch->list_reset()
168
169 Resets the list iterator. To be used before the first call to
170 list_next().
171
172 tardir
173 $arch->tardir();
174
175 Return the directory the tarball was unpacked in. This is sometimes
176 useful to play dirty tricks on Archive::Tar::Wrapper by
177 mass-manipulating unpacked files before wrapping them back up into the
178 tarball.
179
180 is_compressed
181 Returns a string to identify if the tarball is compressed or not.
182
183 Expect as parameter a string with the path to the tarball.
184
185 Returns:
186
187 * a "z" character if the file is compressed with "gzip".
188
189 * a "j" character if the file is compressed with "bzip2".
190
191 * a "" character if the file is not compressed at all.
192
193 locate
194 $arch->locate($logic_path);
195
196 Finds the physical location of a file, specified by $logic_path, which
197 is the virtual path of the file within the tarball. Returns a path to
198 the temporary file Archive::Tar::Wrapper created to manipulate the
199 tarball on disk.
200
201 add
202 $arch->add($logic_path, $file_or_stringref, [$options]);
203
204 Add a new file to the tarball. $logic_path is the virtual path of the
205 file within the tarball. $file_or_stringref is either a scalar, in which
206 case it holds the physical path of a file on disk to be transferred
207 (i.e. copied) to the tarball, or it is a reference to a scalar, in which
208 case its content is interpreted to be the data of the file.
209
210 If no additional parameters are given, permissions and user/group id
211 settings of a file to be added are copied. If you want different
212 settings, specify them in the options hash:
213
214 $arch->add($logic_path, $stringref,
215 { perm => 0755, uid => 123, gid => 10 });
216
217 If $file_or_stringref is a reference to a Unicode string, the "binmode"
218 option has to be set to make sure the string gets written as proper
219 UTF-8 into the tar file:
220
221 $arch->add($logic_path, $stringref, { binmode => ":utf8" });
222
223 perm_cp
224 Copies the permissions from a file to another.
225
226 Expects as parameters:
227
228 1. string of the path to the file which permissions will be copied
229 from.
230
231 2. string of the path to the file which permissions will be copied to.
232
233 Returns 1 if everything works as expected.
234
235 perm_get
236 Gets the permissions from a file.
237
238 Expects as parameter the path to the source file.
239
240 Returns an array reference with only the permissions values, as returned
241 by "stat".
242
243 perm_set
244 Sets the permission on a file.
245
246 Expects as parameters:
247
248 1. The path to the file where the permissions should be applied to.
249
250 2. An array reference with the permissions (see "perm_set")
251
252 Returns 1 if everything goes fine.
253
254 Ignore errors here, as we can't change uid/gid unless we're the
255 superuser (see LIMITATIONS section).
256
257 remove
258 $arch->remove($logic_path);
259
260 Removes a file from the tarball. $logic_path is the virtual path of the
261 file within the tarball.
262
263 list_all
264 my $items = $arch->list_all();
265
266 Returns a reference to a (possibly huge) array of items in the tar file.
267 Each item is a reference to an array, containing two elements: the
268 relative path of the item in the tar file and the physical path to the
269 unpacked file or directory on disk.
270
271 To iterate over the list, the following construct can be used:
272
273 # Get a huge list with all entries
274 for my $entry (@{$arch->list_all()}) {
275 my($tar_path, $real_path) = @$entry;
276 print "Tarpath: $tar_path Tempfile: $real_path\n";
277 }
278
279 If the list of items in the tar file is big, use list_reset() and
280 list_next() instead of "list_all".
281
282 list_next
283 my ($tar_path, $phys_path, $type) = $arch->list_next();
284
285 Returns the next item in the tar file. It returns a list of three
286 scalars: the relative path of the item in the tar file, the physical
287 path to the unpacked file or directory on disk, and the type of the
288 entry (f=file, d=directory, l=symlink). Note that by default,
289 Archive::Tar::Wrapper won't display directories, unless the "dirs"
290 parameter is set when running the constructor.
291
292 write
293 $arch->write($tarfile, $compress);
294
295 Write out the tarball by tarring up all temporary files and directories
296 and store it in $tarfile on disk. If $compress holds a true value,
297 compression is used.
298
299 is_gnu
300 $arch->is_gnu();
301
302 Checks if the tar executable is a GNU tar by running 'tar --version' and
303 parsing the output for "GNU".
304
305 Returns true or false (in Perl terms).
306
307 is_bsd
308 $arch->is_bsd();
309
310 Same as is_gnu(), but for BSD.
311
312 ramdisk_mount
313 Mounts a RAM disk.
314
315 It executes the "mount" program under the hood to mount a RAM disk.
316
317 Expects as parameter a hash with options to mount the RAM disk, like:
318
319 * "size"
320
321 * "type" (most probably "tmpfs")
322
323 * "tmpdir"
324
325 Returns 1 if everything goes fine.
326
327 Be sure to check the "Using RAM Disks" in Archive::Tar::Wrapper for full
328 details on using RAM disks.
329
330 ramdisk_unmount
331 Unmounts the RAM disk already mounted with "ramdisk_mount".
332
333 Don't expect parameters and returns 1 if everything goes fine.
334
335 Be sure to check the "Using RAM Disks" in Archive::Tar::Wrapper for full
336 details on using RAM disks.
337
338 Using RAM Disks
339 On Linux, it's quite easy to create a RAM disk and achieve tremendous
340 speedups while untarring or modifying a tarball. You can either create
341 the RAM disk by hand by running
342
343 # mkdir -p /mnt/myramdisk
344 # mount -t tmpfs -o size=20m tmpfs /mnt/myramdisk
345
346 and then feeding the RAM disk as a temporary directory to
347 Archive::Tar::Wrapper, like
348
349 my $tar = Archive::Tar::Wrapper->new( tmpdir => '/mnt/myramdisk' );
350
351 or using Archive::Tar::Wrapper's built-in option "ramdisk":
352
353 my $tar = Archive::Tar::Wrapper->new(
354 ramdisk => {
355 type => 'tmpfs',
356 size => '20m', # 20 MB
357 },
358 );
359
360 Only drawback with the latter option is that creating the RAM disk needs
361 to be performed as root, which often isn't desirable for security
362 reasons. For this reason, Archive::Tar::Wrapper offers a utility
363 functions that mounts the RAM disk and returns the temporary directory
364 it's located in:
365
366 # Create new ramdisk (as root):
367 my $tmpdir = Archive::Tar::Wrapper->ramdisk_mount(
368 type => 'tmpfs',
369 size => '20m', # 20 MB
370 );
371
372 # Delete a ramdisk (as root):
373 Archive::Tar::Wrapper->ramdisk_unmount();
374
375 Optionally, the ramdisk_mount() command accepts a "tmpdir" parameter
376 pointing to a temporary directory for the RAM disk if you wish to set it
377 yourself instead of letting Archive::Tar::Wrapper create it
378 automatically.
379
380 KNOWN LIMITATIONS
381 * Currently, only "tar" programs supporting the "z" option (for
382 compressing/decompressing) are supported. Future version will use
383 "gzip" alternatively.
384
385 * Currently, you can't add empty directories to a tarball directly.
386 You could add a temporary file within a directory, and then remove()
387 the file.
388
389 * If you delete a file, the empty directories it was located in stay
390 in the tarball. You could try to locate() them and delete them. This
391 will be fixed, though.
392
393 * Filenames containing newlines are causing problems with the list
394 iterators. To be fixed.
395
396 * If you ask Archive::Tar::Wrapper to add a file to a tarball, it
397 copies it into a temporary directory and then calls the system tar
398 to wrap up that directory into a tarball.
399
400 This approach has limitations when it comes to file permissions: If
401 the file to be added belongs to a different user/group,
402 Archive::Tar::Wrapper will adjust the uid/gid/permissions of the
403 target file in the temporary directory to reflect the original
404 file's settings, to make sure the system tar will add it like that
405 to the tarball, just like a regular tar run on the original file
406 would. But this will fail of course if the original file's uid is
407 different from the current user's, unless the script is running with
408 superuser rights. The tar program by itself (without
409 Archive::Tar::Wrapper) works differently: It'll just make a note of
410 a file's uid/gid/permissions in the tarball (which it can do without
411 superuser rights) and upon extraction, it'll adjust the permissions
412 of newly generated files if the -p option is given (default for
413 superuser).
414
415 BUGS
416 Archive::Tar::Wrapper doesn't currently handle filenames with embedded
417 newlines.
418
419 Microsoft Windows support
420 Support on Microsoft Windows is limited.
421
422 Versions below Windows 10 will not be supported for desktops, and for
423 servers only Windows 2012 and above.
424
425 The GNU "tar.exe" program doesn't work properly with the current
426 interface of Archive::Tar::Wrapper.
427
428 You must use the "bsdtar.exe" and make sure it appears first in the
429 "PATH" environment variable than the GNU tar (if it is installed). See
430 <http://libarchive.org/> for details about how to download and install
431 "bsdtar.exe", or go to <http://gnuwin32.sourceforge.net/packages.html>
432 for a direct download. Be sure to look for the "bzip2" program package
433 to install it as well.
434
435 Windows 10 might come already with "bsdtar" program already installed.
436 Please search for that on the appropriate page (Microsoft keeps changing
437 the link to keep track of it here).
438
439 Having spaces in the path string to the tar program might be an issue
440 too. Although there is some effort in terms of workaround it, you best
441 might avoid it completely by installing in a different path than
442 "C:\Program Files". Installing both "bsdtar" and "bzip2" in
443 "C:\GnuWin32" will probably be enough when running the installers.
444
445 SEE ALSO
446 * Linux Gazette article from Ben Okopnik, issue 87
447 <https://linuxgazette.net/87/okopnik.html>.
448
449 BUGS
450 Please report any bugs or feature requests on the bugtracker website
451 <https://github.com/haarg/Archive-Tar-Wrapper/issues>
452
453 When submitting a bug or request, please include a test-file or a patch
454 to an existing test-file that illustrates the bug or desired feature.
455
456 AUTHORS
457 * Mike Schilli <[email protected]>
458
459 * Alceu Rodrigues de Freitas Junior <[email protected]>
460
461 CONTRIBUTORS
462 * Chris Weyl <[email protected]>
463
464 * David Cantrell <[email protected]>
465
466 * David Precious <[email protected]>
467
468 * Graham Knop <[email protected]>
469
470 * intrigeri <[email protected]>
471
472 * Kent Fredric <[email protected]>
473
474 * Mark Gardner <[email protected]>
475
476 * Mike Schilli <[email protected]>
477
478 * Mohammad S Anwar <[email protected]>
479
480 * Paulo Custodio <[email protected]>
481
482 * Randy Stauner <[email protected]>
483
484 * Sanko Robinson <[email protected]>
485
486 * Shoichi Kaji <[email protected]>
487
488 COPYRIGHT AND LICENSE
489 This software is Copyright (c) 2024 by Mike Schilli.
490
491 This is free software, licensed under:
492
493 The GNU General Public License, Version 3, June 2007
494