1 .\" Copyright (c) 2003-2006 Tim Kientzle
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .Nm archive_read_new ,
32 .Nm archive_read_support_compression_all ,
33 .Nm archive_read_support_compression_bzip2 ,
34 .Nm archive_read_support_compression_compress ,
35 .Nm archive_read_support_compression_gzip ,
36 .Nm archive_read_support_compression_none ,
37 .Nm archive_read_support_format_all ,
38 .Nm archive_read_support_format_cpio ,
39 .Nm archive_read_support_format_iso9660 ,
40 .Nm archive_read_support_format_tar ,
41 .Nm archive_read_support_format_zip ,
42 .Nm archive_read_open ,
43 .Nm archive_read_open2 ,
44 .Nm archive_read_open_fd ,
45 .Nm archive_read_open_FILE ,
46 .Nm archive_read_open_filename ,
47 .Nm archive_read_open_memory ,
48 .Nm archive_read_next_header ,
49 .Nm archive_read_data ,
50 .Nm archive_read_data_block ,
51 .Nm archive_read_data_skip ,
52 .Nm archive_read_data_into_buffer ,
53 .Nm archive_read_data_into_fd ,
54 .Nm archive_read_extract ,
55 .Nm archive_read_extract_set_progress_callback ,
56 .Nm archive_read_close ,
57 .Nm archive_read_finish
58 .Nd functions for reading streaming archives
62 .Fn archive_read_new "void"
64 .Fn archive_read_support_compression_all "struct archive *"
66 .Fn archive_read_support_compression_bzip2 "struct archive *"
68 .Fn archive_read_support_compression_compress "struct archive *"
70 .Fn archive_read_support_compression_gzip "struct archive *"
72 .Fn archive_read_support_compression_none "struct archive *"
74 .Fn archive_read_support_format_all "struct archive *"
76 .Fn archive_read_support_format_cpio "struct archive *"
78 .Fn archive_read_support_format_iso9660 "struct archive *"
80 .Fn archive_read_support_format_tar "struct archive *"
82 .Fn archive_read_support_format_zip "struct archive *"
84 .Fn archive_read_open "struct archive *" "void *client_data" "archive_open_callback *" "archive_read_callback *" "archive_close_callback *"
86 .Fn archive_read_open2 "struct archive *" "void *client_data" "archive_open_callback *" "archive_read_callback *" "archive_skip_callback *" "archive_close_callback *"
88 .Fn archive_read_open_FILE "struct archive *" "FILE *file"
90 .Fn archive_read_open_fd "struct archive *" "int fd" "size_t block_size"
92 .Fn archive_read_open_filename "struct archive *" "const char *filename" "size_t block_size"
94 .Fn archive_read_open_memory "struct archive *" "void *buff" "size_t size"
96 .Fn archive_read_next_header "struct archive *" "struct archive_entry **"
98 .Fn archive_read_data "struct archive *" "void *buff" "size_t len"
100 .Fn archive_read_data_block "struct archive *" "const void **buff" "size_t *len" "off_t *offset"
102 .Fn archive_read_data_skip "struct archive *"
104 .Fn archive_read_data_into_buffer "struct archive *" "void *" "ssize_t len"
106 .Fn archive_read_data_into_fd "struct archive *" "int fd"
108 .Fn archive_read_extract "struct archive *" "struct archive_entry *" "int flags"
110 .Fn archive_read_extract_set_progress_callback "struct archive *" "void (*func)(void *)" "void *user_data"
112 .Fn archive_read_close "struct archive *"
114 .Fn archive_read_finish "struct archive *"
116 These functions provide a complete API for reading streaming archives.
117 The general process is to first create the
119 object, set options, initialize the reader, iterate over the archive
120 headers and associated data, then close the archive and release all
122 The following summary describes the functions in approximately the
123 order they would be used:
124 .Bl -tag -compact -width indent
125 .It Fn archive_read_new
126 Allocates and initializes a
128 object suitable for reading from an archive.
129 .It Fn archive_read_support_compression_all , Fn archive_read_support_compression_bzip2 , Fn archive_read_support_compression_compress , Fn archive_read_support_compression_gzip , Fn archive_read_support_compression_none
130 Enables auto-detection code and decompression support for the
131 specified compression.
134 is always enabled by default.
136 .Fn archive_read_support_compression_all
137 enables all available decompression code.
138 .It Fn archive_read_support_format_all , Fn archive_read_support_format_cpio , Fn archive_read_support_format_iso9660 , Fn archive_read_support_format_tar, Fn archive_read_support_format_zip
139 Enables support---including auto-detection code---for the
140 specified archive format.
142 .Fn archive_read_support_format_tar
143 enables support for a variety of standard tar formats, old-style tar,
144 ustar, pax interchange format, and many common variants.
146 .Fn archive_read_support_format_all
147 enables support for all available formats.
148 Note that there is no default.
149 .It Fn archive_read_open
151 .Fn archive_read_open2 ,
152 except that the skip callback is assumed to be
154 .It Fn archive_read_open2
155 Freeze the settings, open the archive, and prepare for reading entries.
156 This is the most generic version of this call, which accepts
157 four callback functions.
158 Most clients will want to use
159 .Fn archive_read_open_filename ,
160 .Fn archive_read_open_FILE ,
161 .Fn archive_read_open_fd ,
163 .Fn archive_read_open_memory
165 The library invokes the client-provided functions to obtain
166 raw bytes from the archive.
167 .It Fn archive_read_open_FILE
169 .Fn archive_read_open ,
170 except that it accepts a
173 This function should not be used with tape drives or other devices
174 that require strict I/O blocking.
175 .It Fn archive_read_open_fd
177 .Fn archive_read_open ,
178 except that it accepts a file descriptor and block size rather than
179 a set of function pointers.
180 Note that the file descriptor will not be automatically closed at
182 This function is safe for use with tape drives or other blocked devices.
183 .It Fn archive_read_open_file
184 This is a deprecated synonym for
185 .Fn archive_read_open_filename .
186 .It Fn archive_read_open_filename
188 .Fn archive_read_open ,
189 except that it accepts a simple filename and a block size.
190 A NULL filename represents standard input.
191 This function is safe for use with tape drives or other blocked devices.
192 .It Fn archive_read_open_memory
194 .Fn archive_read_open ,
195 except that it accepts a pointer and size of a block of
196 memory containing the archive data.
197 .It Fn archive_read_next_header
198 Read the header for the next entry and return a pointer to
200 .Tn struct archive_entry .
201 .It Fn archive_read_data
202 Read data associated with the header just read.
203 Internally, this is a convenience function that calls
204 .Fn archive_read_data_block
205 and fills any gaps with nulls so that callers see a single
206 continuous stream of data.
207 .It Fn archive_read_data_block
208 Return the next available block of data for this entry.
210 .Fn archive_read_data ,
212 .Fn archive_read_data_block
213 function avoids copying data and allows you to correctly handle
214 sparse files, as supported by some archive formats.
215 The library guarantees that offsets will increase and that blocks
217 Note that the blocks returned from this function can be much larger
218 than the block size read from disk, due to compression
219 and internal buffer optimizations.
220 .It Fn archive_read_data_skip
221 A convenience function that repeatedly calls
222 .Fn archive_read_data_block
223 to skip all of the data for this archive entry.
224 .It Fn archive_read_data_into_buffer
225 A convenience function that repeatedly calls
226 .Fn archive_read_data_block
227 to copy the entire entry into the client-supplied buffer.
228 Note that the client is responsible for sizing the buffer appropriately.
229 .It Fn archive_read_data_into_fd
230 A convenience function that repeatedly calls
231 .Fn archive_read_data_block
232 to copy the entire entry to the provided file descriptor.
233 .It Fn archive_read_extract_set_skip_file
234 This function records the device and inode numbers
235 of a file that should not be restored.
236 This is a convenience that prevents
237 .Fn archive_read_extract
238 from restoring a file over the archive itself.
239 .It Fn archive_read_extract
240 A convenience function that recreates the specified object on
241 disk and reads the entry data into that object.
242 The filename, permissions, and other critical information
243 are taken from the provided
248 argument modifies how the object is recreated.
249 It consists of a bitwise OR of one or more of the following values:
250 .Bl -tag -compact -width "indent"
251 .It Cm ARCHIVE_EXTRACT_OWNER
252 The user and group IDs should be set on the restored file.
253 By default, the user and group IDs are not restored.
254 .It Cm ARCHIVE_EXTRACT_PERM
255 The permissions (mode bits) should be restored for all objects.
256 By default, permissions are only restored for regular files.
257 .It Cm ARCHIVE_EXTRACT_TIME
258 The timestamps (mtime, ctime, and atime) should be restored.
259 By default, they are ignored.
260 Note that restoring of atime is not currently supported.
261 .It Cm ARCHIVE_EXTRACT_NO_OVERWRITE
262 Existing files on disk will not be overwritten.
263 By default, existing regular files are truncated and overwritten;
264 existing directories will have their permissions updated;
265 other pre-existing objects are unlinked and recreated from scratch.
266 .It Cm ARCHIVE_EXTRACT_UNLINK
267 Existing files on disk will be unlinked and recreated from scratch.
268 By default, existing files are truncated and rewritten, but
269 the file is not recreated.
270 In particular, the default behavior does not break existing hard links.
271 .It Cm ARCHIVE_EXTRACT_ACL
272 Attempt to restore ACLs.
273 By default, extended ACLs are ignored.
274 .It Cm ARCHIVE_EXTRACT_FFLAGS
275 Attempt to restore extended file flags.
276 By default, file flags are ignored.
278 Note that not all attributes are set immediately;
279 some attributes are cached in memory and written to disk only
280 when the archive is closed.
281 (For example, read-only directories are initially created
282 writable so that files within those directories can be
284 The final permissions are set when the archive is closed.)
285 .It Fn archive_read_extract_set_progress_callback
286 Sets a pointer to a user-defined callback that can be used
287 for updating progress displays during extraction.
288 The progress function will be invoked during the extraction of large
290 The progress function will be invoked with the pointer provided to this call.
291 Generally, the data pointed to should include a reference to the archive
292 object and the archive_entry object so that various statistics
293 can be retrieved for the progress display.
294 .It Fn archive_read_close
295 Complete the archive and invoke the close callback.
296 .It Fn archive_read_finish
298 .Fn archive_read_close
299 if it was not invoked manually, then release all resources.
302 Note that the library determines most of the relevant information about
303 the archive by inspection.
304 In particular, it automatically detects
308 compression and transparently performs the appropriate decompression.
309 It also automatically detects the archive format.
311 A complete description of the
314 .Tn struct archive_entry
315 objects can be found in the overview manual page for
318 The callback functions must match the following prototypes:
319 .Bl -item -offset indent
322 .Fn archive_read_callback "struct archive *" "void *client_data" "const void **buffer"
325 .Fn archive_skip_callback "struct archive *" "void *client_data" "size_t request"
328 .Fn archive_open_callback "struct archive *" "void *client_data"
331 .Fn archive_close_callback "struct archive *" "void *client_data"
334 The open callback is invoked by
338 if the underlying file or data source is successfully
340 If the open fails, it should call
341 .Fn archive_set_error
342 to register an error code and message and return
345 The read callback is invoked whenever the library
346 requires raw bytes from the archive.
347 The read callback should read data into a buffer,
349 .Li const void **buffer
350 argument to point to the available data, and
351 return a count of the number of bytes available.
352 The library will invoke the read callback again
353 only after it has consumed this data.
354 The library imposes no constraints on the size
355 of the data blocks returned.
356 On end-of-file, the read callback should
358 On error, the read callback should invoke
359 .Fn archive_set_error
360 to register an error code and message and
363 The skip callback is invoked when the
364 library wants to ignore a block of data.
365 The return value is the number of bytes actually
366 skipped, which may differ from the request.
367 If the callback cannot skip data, it should return
369 If the skip callback is not provided (the
372 the library will invoke the read function
373 instead and simply discard the result.
374 A skip callback can provide significant
375 performance gains when reading uncompressed
376 archives from slow disk drives or other media
377 that can skip quickly.
379 The close callback is invoked by archive_close when
380 the archive processing is complete.
381 The callback should return
384 On failure, the callback should invoke
385 .Fn archive_set_error
386 to register an error code and message and
390 The following illustrates basic usage of the library.
392 the callback functions are simply wrappers around the standard
398 .Bd -literal -offset indent
400 list_archive(const char *name)
402 struct mydata *mydata;
404 struct archive_entry *entry;
406 mydata = malloc(sizeof(struct mydata));
407 a = archive_read_new();
409 archive_read_support_compression_all(a);
410 archive_read_support_format_all(a);
411 archive_read_open(a, mydata, myopen, myread, myclose);
412 while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
413 printf("%s\\n",archive_entry_pathname(entry));
414 archive_read_data_skip(a);
416 archive_read_finish(a);
421 myread(struct archive *a, void *client_data, const void **buff)
423 struct mydata *mydata = client_data;
425 *buff = mydata->buff;
426 return (read(mydata->fd, mydata->buff, 10240));
430 myopen(struct archive *a, void *client_data)
432 struct mydata *mydata = client_data;
434 mydata->fd = open(mydata->name, O_RDONLY);
435 return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
439 myclose(struct archive *a, void *client_data)
441 struct mydata *mydata = client_data;
449 Most functions return zero on success, non-zero on error.
450 The possible return codes include:
452 (the operation succeeded),
454 (the operation succeeded but a non-critical error was encountered),
456 (end-of-archive was encountered),
458 (the operation failed but can be retried),
461 (there was a fatal error; the archive should be closed immediately).
462 Detailed error codes and textual descriptions are available from the
465 .Fn archive_error_string
469 returns a pointer to a freshly allocated
476 .Fn archive_read_data
477 returns a count of bytes actually read or zero at the end of the entry.
483 is returned and an error code and textual description can be retrieved from the
486 .Fn archive_error_string
489 The library expects the client callbacks to behave similarly.
490 If there is an error, you can use
491 .Fn archive_set_error
492 to set an appropriate error code and description,
493 then return one of the non-zero values above.
494 (Note that the value eventually returned to the client may
495 not be the same; many errors that are not critical at the level
496 of basic I/O can prevent the archive from being properly read,
497 thus most I/O errors eventually cause
509 library first appeared in
515 library was written by
516 .An Tim Kientzle Aq kientzle@acm.org .
518 Directories are actually extracted in two distinct phases.
519 Directories are created during
520 .Fn archive_read_extract ,
521 but final permissions are not set until
522 .Fn archive_read_close .
523 This separation is necessary to correctly handle borderline
524 cases such as a non-writable directory containing
525 files, but can cause unexpected results.
526 In particular, directory permissions are not fully
527 restored until the archive is closed.
530 to change the current directory between calls to
531 .Fn archive_read_extract
533 .Fn archive_read_close ,
534 you may confuse the permission-setting logic with
535 the result that directory permissions are restored