1 .\" Copyright (c) 2003-2007 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_compression_program ,
38 .Nm archive_read_support_format_all ,
39 .Nm archive_read_support_format_cpio ,
40 .Nm archive_read_support_format_empty ,
41 .Nm archive_read_support_format_iso9660 ,
42 .Nm archive_read_support_format_tar ,
43 .Nm archive_read_support_format_zip ,
44 .Nm archive_read_open ,
45 .Nm archive_read_open2 ,
46 .Nm archive_read_open_fd ,
47 .Nm archive_read_open_FILE ,
48 .Nm archive_read_open_filename ,
49 .Nm archive_read_open_memory ,
50 .Nm archive_read_next_header ,
51 .Nm archive_read_data ,
52 .Nm archive_read_data_block ,
53 .Nm archive_read_data_skip ,
54 .\" #if ARCHIVE_API_VERSION < 3
55 .Nm archive_read_data_into_buffer ,
57 .Nm archive_read_data_into_fd ,
58 .Nm archive_read_extract ,
59 .Nm archive_read_extract_set_progress_callback ,
60 .Nm archive_read_close ,
61 .Nm archive_read_finish
62 .Nd functions for reading streaming archives
66 .Fn archive_read_new "void"
68 .Fn archive_read_support_compression_all "struct archive *"
70 .Fn archive_read_support_compression_bzip2 "struct archive *"
72 .Fn archive_read_support_compression_compress "struct archive *"
74 .Fn archive_read_support_compression_gzip "struct archive *"
76 .Fn archive_read_support_compression_none "struct archive *"
78 .Fo archive_read_support_compression_program
79 .Fa "struct archive *"
83 .Fn archive_read_support_format_all "struct archive *"
85 .Fn archive_read_support_format_cpio "struct archive *"
87 .Fn archive_read_support_format_empty "struct archive *"
89 .Fn archive_read_support_format_iso9660 "struct archive *"
91 .Fn archive_read_support_format_tar "struct archive *"
93 .Fn archive_read_support_format_zip "struct archive *"
96 .Fa "struct archive *"
97 .Fa "void *client_data"
98 .Fa "archive_open_callback *"
99 .Fa "archive_read_callback *"
100 .Fa "archive_close_callback *"
103 .Fo archive_read_open2
104 .Fa "struct archive *"
105 .Fa "void *client_data"
106 .Fa "archive_open_callback *"
107 .Fa "archive_read_callback *"
108 .Fa "archive_skip_callback *"
109 .Fa "archive_close_callback *"
112 .Fn archive_read_open_FILE "struct archive *" "FILE *file"
114 .Fn archive_read_open_fd "struct archive *" "int fd" "size_t block_size"
116 .Fo archive_read_open_filename
117 .Fa "struct archive *"
118 .Fa "const char *filename"
119 .Fa "size_t block_size"
122 .Fn archive_read_open_memory "struct archive *" "void *buff" "size_t size"
124 .Fn archive_read_next_header "struct archive *" "struct archive_entry **"
126 .Fn archive_read_data "struct archive *" "void *buff" "size_t len"
128 .Fo archive_read_data_block
129 .Fa "struct archive *"
130 .Fa "const void **buff"
135 .Fn archive_read_data_skip "struct archive *"
136 .\" #if ARCHIVE_API_VERSION < 3
138 .Fn archive_read_data_into_buffer "struct archive *" "void *" "ssize_t len"
141 .Fn archive_read_data_into_fd "struct archive *" "int fd"
143 .Fo archive_read_extract
144 .Fa "struct archive *"
145 .Fa "struct archive_entry *"
149 .Fo archive_read_extract_set_progress_callback
150 .Fa "struct archive *"
151 .Fa "void (*func)(void *)"
152 .Fa "void *user_data"
155 .Fn archive_read_close "struct archive *"
157 .Fn archive_read_finish "struct archive *"
159 These functions provide a complete API for reading streaming archives.
160 The general process is to first create the
162 object, set options, initialize the reader, iterate over the archive
163 headers and associated data, then close the archive and release all
165 The following summary describes the functions in approximately the
166 order they would be used:
167 .Bl -tag -compact -width indent
168 .It Fn archive_read_new
169 Allocates and initializes a
171 object suitable for reading from an archive.
173 .Fn archive_read_support_compression_all ,
174 .Fn archive_read_support_compression_bzip2 ,
175 .Fn archive_read_support_compression_compress ,
176 .Fn archive_read_support_compression_gzip ,
177 .Fn archive_read_support_compression_none
179 Enables auto-detection code and decompression support for the
180 specified compression.
183 is always enabled by default.
185 .Fn archive_read_support_compression_all
186 enables all available decompression code.
187 .It Fn archive_read_support_compression_program
188 Data is fed through the specified external program before being dearchived.
189 Note that this disables automatic detection of the compression format,
190 so it makes no sense to specify this in conjunction with any other
191 decompression option.
193 .Fn archive_read_support_format_all ,
194 .Fn archive_read_support_format_cpio ,
195 .Fn archive_read_support_format_empty ,
196 .Fn archive_read_support_format_iso9660 ,
197 .Fn archive_read_support_format_tar ,
198 .Fn archive_read_support_format_zip
200 Enables support---including auto-detection code---for the
201 specified archive format.
203 .Fn archive_read_support_format_tar
204 enables support for a variety of standard tar formats, old-style tar,
205 ustar, pax interchange format, and many common variants.
207 .Fn archive_read_support_format_all
208 enables support for all available formats.
209 Only empty archives are supported by default.
210 .It Fn archive_read_open
212 .Fn archive_read_open2 ,
213 except that the skip callback is assumed to be
215 .It Fn archive_read_open2
216 Freeze the settings, open the archive, and prepare for reading entries.
217 This is the most generic version of this call, which accepts
218 four callback functions.
219 Most clients will want to use
220 .Fn archive_read_open_filename ,
221 .Fn archive_read_open_FILE ,
222 .Fn archive_read_open_fd ,
224 .Fn archive_read_open_memory
226 The library invokes the client-provided functions to obtain
227 raw bytes from the archive.
228 .It Fn archive_read_open_FILE
230 .Fn archive_read_open ,
231 except that it accepts a
234 This function should not be used with tape drives or other devices
235 that require strict I/O blocking.
236 .It Fn archive_read_open_fd
238 .Fn archive_read_open ,
239 except that it accepts a file descriptor and block size rather than
240 a set of function pointers.
241 Note that the file descriptor will not be automatically closed at
243 This function is safe for use with tape drives or other blocked devices.
244 .It Fn archive_read_open_file
245 This is a deprecated synonym for
246 .Fn archive_read_open_filename .
247 .It Fn archive_read_open_filename
249 .Fn archive_read_open ,
250 except that it accepts a simple filename and a block size.
251 A NULL filename represents standard input.
252 This function is safe for use with tape drives or other blocked devices.
253 .It Fn archive_read_open_memory
255 .Fn archive_read_open ,
256 except that it accepts a pointer and size of a block of
257 memory containing the archive data.
258 .It Fn archive_read_next_header
259 Read the header for the next entry and return a pointer to
261 .Tn struct archive_entry .
262 .It Fn archive_read_data
263 Read data associated with the header just read.
264 Internally, this is a convenience function that calls
265 .Fn archive_read_data_block
266 and fills any gaps with nulls so that callers see a single
267 continuous stream of data.
268 .It Fn archive_read_data_block
269 Return the next available block of data for this entry.
271 .Fn archive_read_data ,
273 .Fn archive_read_data_block
274 function avoids copying data and allows you to correctly handle
275 sparse files, as supported by some archive formats.
276 The library guarantees that offsets will increase and that blocks
278 Note that the blocks returned from this function can be much larger
279 than the block size read from disk, due to compression
280 and internal buffer optimizations.
281 .It Fn archive_read_data_skip
282 A convenience function that repeatedly calls
283 .Fn archive_read_data_block
284 to skip all of the data for this archive entry.
285 .\" #if ARCHIVE_API_VERSION < 3
286 .It Fn archive_read_data_into_buffer
287 This function is deprecated and will be removed.
289 .Fn archive_read_data
292 .It Fn archive_read_data_into_fd
293 A convenience function that repeatedly calls
294 .Fn archive_read_data_block
295 to copy the entire entry to the provided file descriptor.
296 .It Fn archive_read_extract , Fn archive_read_extract_set_skip_file
297 A convenience function that wraps the corresponding
298 .Xr archive_write_disk 3
301 .Fn archive_read_extract
302 creates a restore object using
303 .Xr archive_write_disk_new 3
305 .Xr archive_write_disk_set_standard_lookup 3 ,
306 then transparently invokes
307 .Xr archive_write_disk_set_options 3 ,
308 .Xr archive_write_header 3 ,
309 .Xr archive_write_data 3 ,
311 .Xr archive_write_finish_entry 3
312 to create the entry on disk and copy data into it.
315 argument is passed unmodified to
316 .Xr archive_write_disk_set_options 3 .
317 .It Fn archive_read_extract_set_progress_callback
318 Sets a pointer to a user-defined callback that can be used
319 for updating progress displays during extraction.
320 The progress function will be invoked during the extraction of large
322 The progress function will be invoked with the pointer provided to this call.
323 Generally, the data pointed to should include a reference to the archive
324 object and the archive_entry object so that various statistics
325 can be retrieved for the progress display.
326 .It Fn archive_read_close
327 Complete the archive and invoke the close callback.
328 .It Fn archive_read_finish
330 .Fn archive_read_close
331 if it was not invoked manually, then release all resources.
332 Note: In libarchive 1.x, this function was declared to return
334 which made it impossible to detect certain errors when
335 .Fn archive_read_close
336 was invoked implicitly from this function.
337 The declaration is corrected beginning with libarchive 2.0.
340 Note that the library determines most of the relevant information about
341 the archive by inspection.
342 In particular, it automatically detects
346 compression and transparently performs the appropriate decompression.
347 It also automatically detects the archive format.
349 A complete description of the
352 .Tn struct archive_entry
353 objects can be found in the overview manual page for
356 The callback functions must match the following prototypes:
357 .Bl -item -offset indent
360 .Fo archive_read_callback
361 .Fa "struct archive *"
362 .Fa "void *client_data"
363 .Fa "const void **buffer"
366 .\" #if ARCHIVE_API_VERSION < 2
368 .Fo archive_skip_callback
369 .Fa "struct archive *"
370 .Fa "void *client_data"
374 .\" .Ft typedef off_t
375 .\" .Fo archive_skip_callback
376 .\" .Fa "struct archive *"
377 .\" .Fa "void *client_data"
378 .\" .Fa "off_t request"
383 .Fn archive_open_callback "struct archive *" "void *client_data"
386 .Fn archive_close_callback "struct archive *" "void *client_data"
389 The open callback is invoked by
393 if the underlying file or data source is successfully
395 If the open fails, it should call
396 .Fn archive_set_error
397 to register an error code and message and return
400 The read callback is invoked whenever the library
401 requires raw bytes from the archive.
402 The read callback should read data into a buffer,
404 .Li const void **buffer
405 argument to point to the available data, and
406 return a count of the number of bytes available.
407 The library will invoke the read callback again
408 only after it has consumed this data.
409 The library imposes no constraints on the size
410 of the data blocks returned.
411 On end-of-file, the read callback should
413 On error, the read callback should invoke
414 .Fn archive_set_error
415 to register an error code and message and
418 The skip callback is invoked when the
419 library wants to ignore a block of data.
420 The return value is the number of bytes actually
421 skipped, which may differ from the request.
422 If the callback cannot skip data, it should return
424 If the skip callback is not provided (the
427 the library will invoke the read function
428 instead and simply discard the result.
429 A skip callback can provide significant
430 performance gains when reading uncompressed
431 archives from slow disk drives or other media
432 that can skip quickly.
434 The close callback is invoked by archive_close when
435 the archive processing is complete.
436 The callback should return
439 On failure, the callback should invoke
440 .Fn archive_set_error
441 to register an error code and message and
445 The following illustrates basic usage of the library.
447 the callback functions are simply wrappers around the standard
453 .Bd -literal -offset indent
455 list_archive(const char *name)
457 struct mydata *mydata;
459 struct archive_entry *entry;
461 mydata = malloc(sizeof(struct mydata));
462 a = archive_read_new();
464 archive_read_support_compression_all(a);
465 archive_read_support_format_all(a);
466 archive_read_open(a, mydata, myopen, myread, myclose);
467 while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
468 printf("%s\\n",archive_entry_pathname(entry));
469 archive_read_data_skip(a);
471 archive_read_finish(a);
476 myread(struct archive *a, void *client_data, const void **buff)
478 struct mydata *mydata = client_data;
480 *buff = mydata->buff;
481 return (read(mydata->fd, mydata->buff, 10240));
485 myopen(struct archive *a, void *client_data)
487 struct mydata *mydata = client_data;
489 mydata->fd = open(mydata->name, O_RDONLY);
490 return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
494 myclose(struct archive *a, void *client_data)
496 struct mydata *mydata = client_data;
504 Most functions return zero on success, non-zero on error.
505 The possible return codes include:
507 (the operation succeeded),
509 (the operation succeeded but a non-critical error was encountered),
511 (end-of-archive was encountered),
513 (the operation failed but can be retried),
516 (there was a fatal error; the archive should be closed immediately).
517 Detailed error codes and textual descriptions are available from the
520 .Fn archive_error_string
524 returns a pointer to a freshly allocated
531 .Fn archive_read_data
532 returns a count of bytes actually read or zero at the end of the entry.
538 is returned and an error code and textual description can be retrieved from the
541 .Fn archive_error_string
544 The library expects the client callbacks to behave similarly.
545 If there is an error, you can use
546 .Fn archive_set_error
547 to set an appropriate error code and description,
548 then return one of the non-zero values above.
549 (Note that the value eventually returned to the client may
550 not be the same; many errors that are not critical at the level
551 of basic I/O can prevent the archive from being properly read,
552 thus most I/O errors eventually cause
564 library first appeared in
570 library was written by
571 .An Tim Kientzle Aq kientzle@acm.org .
573 Many traditional archiver programs treat
574 empty files as valid empty archives.
575 For example, many implementations of
577 allow you to append entries to an empty file.
578 Of course, it is impossible to determine the format of an empty file
579 by inspecting the contents, so this library treats empty files as