Tar File Manual¶
This project provides functionality to read and write a variety of tar archive
formats. Currently supported are the POSIX
ustar, PAX
(ustar with a few new
entry types), GNU
, and v7 (very old) formats.
This project is rather low level and is focused exclusively on reading and
writing physical tar file entries using streams. Therefore, it contains no
functionality to automatically build archives from a set of files on the
filesystem or write the contents of a file to the filesystem. Additionally,
there are no smarts that read multiple physical entries and combine them into a
single logical entry (e.g., with PAX
extended headers or GNU
long link/path
name support). This approach is taken to both improve portability (the CL
spec
has no facility for things like detecting symlinks or modifying/reading file
owners, etc.) and properly separate concerns.
For a higher level library that reads and writes logical entries, as well as includes file system integration, see cl-tar.
This project is a fork of Nathan Froyd's archive library. Much code remains, but the non-portable bits have been stripped, better support for multiple archive types (and autodetection) has been added, better blocking support added (tar readers/writers are supposed to read/write in multiples of 512 bytes), cpio support removed, and a test suite added, along with other miscellaneous fixes and improvements.
One major user visible difference between this library and the original archive
is that there is no need to discard entries when you are finished with them and
support has been added for seeking within a stream (using FILE-POSITION
under
the hood). This means you can do something like iterate over all entries in one
go and then get a stream containing the contents of an arbitrary entry.
However, care must be taken if you hop around in a tar file as it will only
work if the underlying stream containing the tar archive is
seekable. Typically, streams backed by files are seekable, but all other
input streams (including stdin) are not. Therefore if there is any chance
your code that uses tar-file
will read files from non-seekable streams, you
should process entries sequentially and completely before moving on to the next
one. Alternatively, you can set the blocking factor high enough that the entire
file is read into memory at once (yikes!).
Opening and Closing Tar Files¶
-
[variable]
:latin-1
The default encoding to use for strings read from/written to tar headers. Must be recognized by Babel.
-
[macro]
(tar-file-var pathname-or-stream &key (direction :input)
(if-exists nil) (if-does-not-exist nil) (type :auto) (compression :auto)
(blocking-factor 20) (header-encoding '\*default-header-encoding\*)) &body
body
Bind
TAR-FILE-VAR
to a newly openedtar-file
, backed byPATHNAME-OR-STREAM
. IfPATHNAME-OR-STREAM
evaluates to a stream, that stream is used directly, otherwise, it is opened viaOPEN
. IfPATHNAME-OR-STREAM
is a stream, that stream is not closed upon exiting the body of the macro.DIRECTION
must be either:INPUT
or:OUTPUT
.IF-EXISTS
andIF-DOES-NOT-EXIST
are passed toOPEN
ifPATHNAME-OR-STREAM
is not a stream.See
open-tar-file
for a description ofTYPE
,BLOCKING-FACTOR
,HEADER-ENCODING
, andCOMPRESSION
.
-
[function]
stream &key (direction :input) (type :auto)
(blocking-factor 20) (header-encoding \*default-header-encoding\*) (compression
:auto)
Create a
tar-file
object backed bySTREAM
. TheSTREAM
should not be read from or written to any more.DIRECTION
is either:INPUT
or:OUTPUT
.BLOCKING-FACTOR
is an integer that specifies how many 512-byte blocks should be read from or written toSTREAM
at any one time.TYPE
is eitherAUTO
or a class designator for a subclass oftar-file
. If:AUTO
, the appropriate class will be determined by looking at the first tar header.HEADER-ENCODING
is an encoding specifier recognized by Babel.COMPRESSION
determines what compression scheme is used, if any. It can be either:AUTO
(the default),NIL
(no compression), or:GZIP
. If:AUTO
, the compression type is determined using thePATHNAME
of the stream (for:OUTPUT
) or by peeking at the stream for magic numbers (for:INPUT
).
-
[generic-function]
tar-file
Perform any necessary processing for finalizing
TAR-FILE
. This function must be called prior to callingclose-tar-file
.
-
[generic-function]
tar-file
Closes the stream associated with
TAR-FILE
and the tar-file itself. Further operations on the tar-file are undefined.Does
NOT
close the underlyingSTREAM
that backed theTAR-FILE
.
Tar Archive Types¶
There are three different types of tar archives that this system can handle, with a common base class.
-
[class]
Base class of a tar file.
- [class]
A ustar tar file.
- [class]
A gnu tar file.
- [class]
A v7 tar file.
Entries¶
-
[macro]
(entry tar-file &optional result) &body body
Iterate over the entries in
tar-file
. For each entry,entry
is bound to anentry
representing the entry.RESULT
is used as inDOTIMES
.
-
[generic-function]
tar-file
Return the next entry in
TAR-FILE
orNIL
if there is no next entry
-
[class]
Base class for all entries in a tar file.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
has associated data that can be read usingmake-entry-stream
.
-
[generic-function]
entry
Returns a new binary stream that contains
ENTRY
's data.
-
[generic-function]
entry
Return the name of the
ENTRY
(a string).
-
[generic-function]
entry
Return the size of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the uname of the
ENTRY
(a string).
-
[generic-function]
entry
Return the gname of the
ENTRY
(a string).
-
[generic-function]
entry
Return the mode of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the mtime of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the uid of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the gid of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the linkname of the
ENTRY
(a string).
-
[generic-function]
entry
Return the major device of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the minor device of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the prefix of the
ENTRY
(a string).
-
[generic-function]
entry
Return the atime of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the ctime of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the offset of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the offset of the first sparse block of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the numbytes of the first sparse block of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the offset of the second sparse block of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the numbytes of the second sparse block of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the offset of the third sparse block of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the numbytes of the third sparse block of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the offset of the fourth sparse block of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the numbytes of the fourth sparse block of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the isextended field of the
ENTRY
(an integer).
-
[generic-function]
entry
Return the realsize of the
ENTRY
(an integer).
-
[generic-function]
entry name &optional default
Get the
NAME
attribute fromENTRY
.
-
[generic-function]
entry
Return a list of attribute names contained within
ENTRY
.
-
[macro]
(name value entry &optional result) &body body
Given a
PAX
entry
with attributes, executeBODY
for every attribute, withname
bound to the attribute name andVALUE
bound to the attribute value.
File Entry¶
-
[class]
A regular file.
-
[generic-function]
tar-file name &rest args &key uname gname mode mtime
uid gid size data prefix
Write a
file-entry
toTAR-FILE
.DATA
can be eitherNIL
(no data is written), an octet vector (written as is), a string (encoded usingUTF-8
and written), or aPATHNAME
(opened, read, and written to the archive).
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
denotes a regular file.
Symbolic Link Entry ¶
- [class]
A symbolic link.
-
[generic-function]
tar-file name &rest args &key uname gname mode mtime
uid gid linkname prefix
Write a
symbolic-link-entry
toTAR-FILE
.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
denotes a symbolic link.
Hard Link Entry¶
- [class]
A hard link.
-
[generic-function]
tar-file name &rest args &key uname gname mode mtime
uid gid linkname prefix
Write a
hard-link-entry
toTAR-FILE
.
Character Device Entry ¶
-
[class]
A character device.
-
[generic-function]
tar-file name &rest args &key uname gname mode mtime
uid gid devmajor devminor prefix
Write a
character-device-entry
toTAR-FILE
.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
denotes a character device.
Block Device Entry ¶
-
[class]
A block device.
-
[generic-function]
tar-file name &rest args &key uname gname mode mtime
uid gid devmajor devminor prefix
Write a
block-device-entry
toTAR-FILE
.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
denotes a block device.
Fifo Entry¶
-
[class]
A
FIFO
.
-
[generic-function]
tar-file name &rest args &key uname gname mode mtime
uid gid prefix
Write a
fifo-entry
toTAR-FILE
.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
denotes a fifo.
Directory Entry¶
-
[class]
A directory.
-
[generic-function]
tar-file name &rest args &key uname gname mode mtime
uid gid size prefix
Write a
directory-entry
toTAR-FILE
.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
denotes a directory.
Pax Extended Attributes Entry ¶
-
[class]
Extended attributes for the subsequent record.
-
[generic-function]
tar-file name &rest args &key attributes
Write a
pax-extended-attributes-entry
toTAR-FILE
.ATTRIBUTES
must be either a hash table mapping strings to strings or an alist mapping strings to strings. If it is an alist, ordering is preserved.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
containsPAX
extended attributes.
Pax Global Attributes Entry ¶
-
[class]
Extended attributes for all subsequent records.
-
[generic-function]
tar-file name &rest args &key attributes
Write a
pax-global-attributes-entry
toTAR-FILE
.ATTRIBUTES
must be either a hash table mapping strings to strings or an alist mapping strings to strings. If it is an alist, ordering is preserved.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
containsPAX
global attributes.
Gnu Long Link Name Entry ¶
-
[generic-function]
tar-file name &rest args &key data
Write a
gnu-long-link-name-entry
toTAR-FILE
.DATA
must be either a string (which is thenUTF-8
encoded) or a byte vector.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
contains aGNU
long link name.
Gnu Long Name Entry ¶
-
[generic-function]
tar-file name &rest args &key data
Write a
gnu-long-name-entry
toTAR-FILE
.DATA
must be either a string (which is thenUTF-8
encoded) or a byte vector.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
contains aGNU
long name.
Gnu Directory Dump Entry ¶
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
contains aGNU
directory dump.
Gnu-Sparse-File Entry ¶
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
contains aGNU
sparse file.
Gnu Volume Header Name Entry ¶
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
contains aGNU
volume header name.
Unknown Entry¶
-
[class]
An unknown entry.
-
[generic-function]
entry
Returns non-
NIL
ifENTRY
is unknown.
Conditions¶
-
[condition]
All errors signaled are of this type.
-
[condition]
Signaled when the checksum in a tar header is invalid.