1LIBARCHIVE(3) BSD Library Functions Manual LIBARCHIVE(3)
2
4 libarchive — functions for reading and writing streaming archives
5
7 Reading and Writing Streaming Archives Library (libarchive, -larchive)
8
10 The libarchive library provides a flexible interface for reading and
11 writing streaming archive files such as tar and cpio. The library is
12 inherently stream-oriented; readers serially iterate through the archive,
13 writers serially add things to the archive. In particular, note that
14 there is no built-in support for random access nor for in-place modifica‐
15 tion.
16
17 When reading an archive, the library automatically detects the format and
18 the compression. The library currently has read support for:
19 · old-style tar archives,
20 · most variants of the POSIX “ustar” format,
21 · the POSIX “pax interchange” format,
22 · GNU-format tar archives,
23 · most common cpio archive formats,
24 · ISO9660 CD images (with or without RockRidge extensions),
25 · Zip archives.
26 The library automatically detects archives compressed with gzip(1),
27 bzip2(1), or compress(1) and decompresses them transparently.
28
29 When writing an archive, you can specify the compression to be used and
30 the format to use. The library can write
31 · POSIX-standard “ustar” archives,
32 · POSIX “pax interchange format” archives,
33 · POSIX octet-oriented cpio archives,
34 · two different variants of shar archives.
35 Pax interchange format is an extension of the tar archive format that
36 eliminates essentially all of the limitations of historic tar formats in
37 a standard fashion that is supported by POSIX-compliant pax(1) implemen‐
38 tations on many systems as well as several newer implementations of
39 tar(1). Note that the default write format will suppress the pax
40 extended attributes for most entries; explicitly requesting pax format
41 will enable those attributes for all entries.
42
43 The read and write APIs are accessed through the archive_read_XXX() func‐
44 tions and the archive_write_XXX() functions, respectively, and either can
45 be used independently of the other.
46
47 The rest of this manual page provides an overview of the library opera‐
48 tion. More detailed information can be found in the individual manual
49 pages for each API or utility function.
50
52 To read an archive, you must first obtain an initialized struct archive
53 object from archive_read_new(). You can then modify this object for the
54 desired operations with the various archive_read_set_XXX() and
55 archive_read_support_XXX() functions. In particular, you will need to
56 invoke appropriate archive_read_support_XXX() functions to enable the
57 corresponding compression and format support. Note that these latter
58 functions perform two distinct operations: they cause the corresponding
59 support code to be linked into your program, and they enable the corre‐
60 sponding auto-detect code. Unless you have specific constraints, you
61 will generally want to invoke archive_read_support_compression_all() and
62 archive_read_support_format_all() to enable auto-detect for all formats
63 and compression types currently supported by the library.
64
65 Once you have prepared the struct archive object, you call
66 archive_read_open() to actually open the archive and prepare it for read‐
67 ing. There are several variants of this function; the most basic expects
68 you to provide pointers to several functions that can provide blocks of
69 bytes from the archive. There are convenience forms that allow you to
70 specify a filename, file descriptor, FILE * object, or a block of memory
71 from which to read the archive data. Note that the core library makes no
72 assumptions about the size of the blocks read; callback functions are
73 free to read whatever block size is most appropriate for the medium.
74
75 Each archive entry consists of a header followed by a certain amount of
76 data. You can obtain the next header with archive_read_next_header(),
77 which returns a pointer to an struct archive_entry structure with infor‐
78 mation about the current archive element. If the entry is a regular
79 file, then the header will be followed by the file data. You can use
80 archive_read_data() (which works much like the read(2) system call) to
81 read this data from the archive. You may prefer to use the higher-level
82 archive_read_data_skip(), which reads and discards the data for this
83 entry, archive_read_data_to_buffer(), which reads the data into an in-
84 memory buffer, archive_read_data_to_file(), which copies the data to the
85 provided file descriptor, or archive_read_extract(), which recreates the
86 specified entry on disk and copies data from the archive. In particular,
87 note that archive_read_extract() uses the struct archive_entry structure
88 that you provide it, which may differ from the entry just read from the
89 archive. In particular, many applications will want to override the
90 pathname, file permissions, or ownership.
91
92 Once you have finished reading data from the archive, you should call
93 archive_read_close() to close the archive, then call
94 archive_read_finish() to release all resources, including all memory
95 allocated by the library.
96
97 The archive_read(3) manual page provides more detailed calling informa‐
98 tion for this API.
99
101 You use a similar process to write an archive. The archive_write_new()
102 function creates an archive object useful for writing, the various
103 archive_write_set_XXX() functions are used to set parameters for writing
104 the archive, and archive_write_open() completes the setup and opens the
105 archive for writing.
106
107 Individual archive entries are written in a three-step process: You first
108 initialize a struct archive_entry structure with information about the
109 new entry. At a minimum, you should set the pathname of the entry and
110 provide a struct stat with a valid st_mode field, which specifies the
111 type of object and st_size field, which specifies the size of the data
112 portion of the object. The archive_write_header() function actually
113 writes the header data to the archive. You can then use
114 archive_write_data() to write the actual data.
115
116 After all entries have been written, use the archive_write_finish() func‐
117 tion to release all resources.
118
119 The archive_write(3) manual page provides more detailed calling informa‐
120 tion for this API.
121
123 Detailed descriptions of each function are provided by the corresponding
124 manual pages.
125
126 All of the functions utilize an opaque struct archive datatype that pro‐
127 vides access to the archive contents.
128
129 The struct archive_entry structure contains a complete description of a
130 single archive entry. It uses an opaque interface that is fully docu‐
131 mented in archive_entry(3).
132
133 Users familiar with historic formats should be aware that the newer vari‐
134 ants have eliminated most restrictions on the length of textual fields.
135 Clients should not assume that filenames, link names, user names, or
136 group names are limited in length. In particular, pax interchange format
137 can easily accommodate pathnames in arbitrary character sets that exceed
138 PATH_MAX.
139
141 Most functions return zero on success, non-zero on error. The return
142 value indicates the general severity of the error, ranging from
143 ARCHIVE_WARN, which indicates a minor problem that should probably be
144 reported to the user, to ARCHIVE_FATAL, which indicates a serious problem
145 that will prevent any further operations on this archive. On error, the
146 archive_errno() function can be used to retrieve a numeric error code
147 (see errno(2)). The archive_error_string() returns a textual error mes‐
148 sage suitable for display.
149
150 archive_read_new() and archive_write_new() return pointers to an allo‐
151 cated and initialized struct archive object.
152
153 archive_read_data() and archive_write_data() return a count of the number
154 of bytes actually read or written. A value of zero indicates the end of
155 the data for this entry. A negative value indicates an error, in which
156 case the archive_errno() and archive_error_string() functions can be used
157 to obtain more information.
158
160 There are character set conversions within the archive_entry(3) functions
161 that are impacted by the currently-selected locale.
162
164 tar(1), archive_entry(3), archive_read(3), archive_util(3),
165 archive_write(3), tar(5)
166
168 The libarchive library first appeared in FreeBSD 5.3.
169
171 The libarchive library was written by Tim Kientzle <kientzle@acm.org>.
172
174 Some archive formats support information that is not supported by struct
175 archive_entry. Such information cannot be fully archived or restored
176 using this library. This includes, for example, comments, character
177 sets, or the arbitrary key/value pairs that can appear in pax interchange
178 format archives.
179
180 Conversely, of course, not all of the information that can be stored in
181 an struct archive_entry is supported by all formats. For example, cpio
182 formats do not support nanosecond timestamps; old tar formats do not sup‐
183 port large device numbers.
184
185BSD August 19, 2006 BSD