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_set_bytes_per_block ,
33 .Nm archive_read_support_compression_all ,
34 .Nm archive_read_support_compression_bzip2 ,
35 .Nm archive_read_support_compression_compress ,
36 .Nm archive_read_support_compression_gzip ,
37 .Nm archive_read_support_compression_none ,
38 .Nm archive_read_support_format_all ,
39 .Nm archive_read_support_format_cpio ,
40 .Nm archive_read_support_format_iso9660 ,
41 .Nm archive_read_support_format_tar ,
42 .Nm archive_read_support_format_zip ,
43 .Nm archive_read_open ,
44 .Nm archive_read_open2 ,
45 .Nm archive_read_open_fd ,
46 .Nm archive_read_open_file ,
47 .Nm archive_read_next_header ,
48 .Nm archive_read_data ,
49 .Nm archive_read_data_block ,
50 .Nm archive_read_data_skip ,
51 .Nm archive_read_data_into_buffer ,
52 .Nm archive_read_data_into_fd ,
53 .Nm archive_read_extract ,
54 .Nm archive_read_extract_set_progress_callback ,
55 .Nm archive_read_close ,
56 .Nm archive_read_finish
57 .Nd functions for reading streaming archives
61 .Fn archive_read_new "void"
63 .Fn archive_read_set_bytes_per_block "struct archive *" "int"
65 .Fn archive_read_support_compression_all "struct archive *"
67 .Fn archive_read_support_compression_bzip2 "struct archive *"
69 .Fn archive_read_support_compression_compress "struct archive *"
71 .Fn archive_read_support_compression_gzip "struct archive *"
73 .Fn archive_read_support_compression_none "struct archive *"
75 .Fn archive_read_support_format_all "struct archive *"
77 .Fn archive_read_support_format_cpio "struct archive *"
79 .Fn archive_read_support_format_iso9660 "struct archive *"
81 .Fn archive_read_support_format_tar "struct archive *"
83 .Fn archive_read_support_format_zip "struct archive *"
85 .Fn archive_read_open "struct archive *" "void *client_data" "archive_open_callback *" "archive_read_callback *" "archive_close_callback *"
87 .Fn archive_read_open2 "struct archive *" "void *client_data" "archive_open_callback *" "archive_read_callback *" "archive_skip_callback *" "archive_close_callback *"
89 .Fn archive_read_open_fd "struct archive *" "int fd" "size_t block_size"
91 .Fn archive_read_open_file "struct archive *" "const char *filename" "size_t block_size"
93 .Fn archive_read_next_header "struct archive *" "struct archive_entry **"
95 .Fn archive_read_data "struct archive *" "void *buff" "size_t len"
97 .Fn archive_read_data_block "struct archive *" "const void **buff" "size_t *len" "off_t *offset"
99 .Fn archive_read_data_skip "struct archive *"
101 .Fn archive_read_data_into_buffer "struct archive *" "void *" "ssize_t len"
103 .Fn archive_read_data_into_fd "struct archive *" "int fd"
105 .Fn archive_read_extract "struct archive *" "struct archive_entry *" "int flags"
107 .Fn archive_read_extract_set_progress_callback "struct archive *" "void (*func)(void *)" "void *user_data"
109 .Fn archive_read_close "struct archive *"
111 .Fn archive_read_finish "struct archive *"
113 These functions provide a complete API for reading streaming archives.
114 The general process is to first create the
116 object, set options, initialize the reader, iterate over the archive
117 headers and associated data, then close the archive and release all
119 The following summary describes the functions in approximately the
120 order they would be used:
121 .Bl -tag -compact -width indent
122 .It Fn archive_read_new
123 Allocates and initializes a
125 object suitable for reading from an archive.
126 .It Fn archive_read_set_bytes_per_block
127 Sets the block size used for reading the archive data.
128 This controls the size that will be used when invoking the read
130 The default is 20 records or 10240 bytes for tar formats.
131 .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
132 Enables auto-detection code and decompression support for the
133 specified compression.
136 is always enabled by default.
138 .Fn archive_read_support_compression_all
139 enables all available decompression code.
140 .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
141 Enables support---including auto-detection code---for the
142 specified archive format.
144 .Fn archive_read_support_format_tar
145 enables support for a variety of standard tar formats, old-style tar,
146 ustar, pax interchange format, and many common variants.
148 .Fn archive_read_support_format_all
149 enables support for all available formats.
150 Note that there is no default.
151 .It Fn archive_read_open
153 .Fn archive_read_open2 ,
154 except that the skip callback is assumed to be
156 .It Fn archive_read_open2
157 Freeze the settings, open the archive, and prepare for reading entries.
158 This is the most generic version of this call, which accepts
159 four callback functions.
160 Most clients will want to use
161 .Fn archive_read_open_file
163 .Fn archive_read_open_fd
165 The library invokes the client-provided functions to obtain
166 raw bytes from the archive.
167 .It Fn archive_read_open_fd
169 .Fn archive_read_open ,
170 except that it accepts a file descriptor and block size rather than
171 a set of function pointers.
172 Note that the file descriptor will not be automatically closed at
174 This function is safe for use with tape drives or other blocked devices.
175 .It Fn archive_read_open_file
177 .Fn archive_read_open ,
178 except that it accepts a simple filename and a block size.
179 A NULL filename represents standard input.
180 This function is safe for use with tape drives or other blocked devices.
181 .It Fn archive_read_next_header
182 Read the header for the next entry and return a pointer to
184 .Tn struct archive_entry .
185 .It Fn archive_read_data
186 Read data associated with the header just read.
187 Internally, this is a convenience function that calls
188 .Fn archive_read_data_block
189 and fills any gaps with nulls so that callers see a single
190 continuous stream of data.
191 .It Fn archive_read_data_block
192 Return the next available block of data for this entry.
194 .Fn archive_read_data ,
196 .Fn archive_read_data_block
197 function avoids copying data and allows you to correctly handle
198 sparse files, as supported by some archive formats.
199 The library guarantees that offsets will increase and that blocks
201 Note that the blocks returned from this function can be much larger
202 than the block size read from disk, due to compression
203 and internal buffer optimizations.
204 .It Fn archive_read_data_skip
205 A convenience function that repeatedly calls
206 .Fn archive_read_data_block
207 to skip all of the data for this archive entry.
208 .It Fn archive_read_data_into_buffer
209 A convenience function that repeatedly calls
210 .Fn archive_read_data_block
211 to copy the entire entry into the client-supplied buffer.
212 Note that the client is responsible for sizing the buffer appropriately.
213 .It Fn archive_read_data_into_fd
214 A convenience function that repeatedly calls
215 .Fn archive_read_data_block
216 to copy the entire entry to the provided file descriptor.
217 .It Fn archive_read_extract_set_skip_file
218 This function records the device and inode numbers
219 of a file that should not be restored.
220 This is a convenience that prevents
221 .Fn archive_read_extract
222 from restoring a file over the archive itself.
223 .It Fn archive_read_extract
224 A convenience function that recreates the specified object on
225 disk and reads the entry data into that object.
226 The filename, permissions, and other critical information
227 are taken from the provided
232 argument modifies how the object is recreated.
233 It consists of a bitwise OR of one or more of the following values:
234 .Bl -tag -compact -width "indent"
235 .It Cm ARCHIVE_EXTRACT_OWNER
236 The user and group IDs should be set on the restored file.
237 By default, the user and group IDs are not restored.
238 .It Cm ARCHIVE_EXTRACT_PERM
239 The permissions (mode bits) should be restored for all objects.
240 By default, permissions are only restored for regular files.
241 .It Cm ARCHIVE_EXTRACT_TIME
242 The timestamps (mtime, ctime, and atime) should be restored.
243 By default, they are ignored.
244 Note that restoring of atime is not currently supported.
245 .It Cm ARCHIVE_EXTRACT_NO_OVERWRITE
246 Existing files on disk will not be overwritten.
247 By default, existing regular files are truncated and overwritten;
248 existing directories will have their permissions updated;
249 other pre-existing objects are unlinked and recreated from scratch.
250 .It Cm ARCHIVE_EXTRACT_UNLINK
251 Existing files on disk will be unlinked and recreated from scratch.
252 By default, existing files are truncated and rewritten, but
253 the file is not recreated.
254 In particular, the default behavior does not break existing hard links.
255 .It Cm ARCHIVE_EXTRACT_ACL
256 Attempt to restore ACLs.
257 By default, extended ACLs are ignored.
258 .It Cm ARCHIVE_EXTRACT_FFLAGS
259 Attempt to restore extended file flags.
260 By default, file flags are ignored.
262 Note that not all attributes are set immediately;
263 some attributes are cached in memory and written to disk only
264 when the archive is closed.
265 (For example, read-only directories are initially created
266 writable so that files within those directories can be
268 The final permissions are set when the archive is closed.)
269 .It Fn archive_read_extract_set_progress_callback
270 Sets a pointer to a user-defined callback that can be used
271 for updating progress displays during extraction.
272 The progress function will be invoked during the extraction of large
274 The progress function will be invoked with the pointer provided to this call.
275 Generally, the data pointed to should include a reference to the archive
276 object and the archive_entry object so that various statistics
277 can be retrieved for the progress display.
278 .It Fn archive_read_close
279 Complete the archive and invoke the close callback.
280 .It Fn archive_read_finish
282 .Fn archive_read_close
283 if it was not invoked manually, then release all resources.
286 Note that the library determines most of the relevant information about
287 the archive by inspection.
288 In particular, it automatically detects
292 compression and transparently performs the appropriate decompression.
293 It also automatically detects the archive format.
295 A complete description of the
298 .Tn struct archive_entry
299 objects can be found in the overview manual page for
302 The callback functions must match the following prototypes:
303 .Bl -item -offset indent
306 .Fn archive_read_callback "struct archive *" "void *client_data" "const void **buffer"
309 .Fn archive_skip_callback "struct archive *" "void *client_data" "size_t request"
312 .Fn archive_open_callback "struct archive *" "void *client_data"
315 .Fn archive_close_callback "struct archive *" "void *client_data"
318 The open callback is invoked by
322 if the underlying file or data source is successfully
324 If the open fails, it should call
325 .Fn archive_set_error
326 to register an error code and message and return
329 The read callback is invoked whenever the library
330 requires raw bytes from the archive.
331 The read callback should read data into a buffer,
333 .Li const void **buffer
334 argument to point to the available data, and
335 return a count of the number of bytes available.
336 The library will invoke the read callback again
337 only after it has consumed this data.
338 The library imposes no constraints on the size
339 of the data blocks returned.
340 On end-of-file, the read callback should
342 On error, the read callback should invoke
343 .Fn archive_set_error
344 to register an error code and message and
347 The skip callback is invoked when the
348 library wants to ignore a block of data.
349 The return value is the number of bytes actually
350 skipped, which may differ from the request.
351 If the callback cannot skip data, it should return
353 If the skip callback is not provided (the
356 the library will invoke the read function
357 instead and simply discard the result.
358 A skip callback can provide significant
359 performance gains when reading uncompressed
360 archives from slow disk drives or other media
361 that can skip quickly.
363 The close callback is invoked by archive_close when
364 the archive processing is complete.
365 The callback should return
368 On failure, the callback should invoke
369 .Fn archive_set_error
370 to register an error code and message and
374 The following illustrates basic usage of the library.
376 the callback functions are simply wrappers around the standard
382 .Bd -literal -offset indent
384 list_archive(const char *name)
386 struct mydata *mydata;
388 struct archive_entry *entry;
390 mydata = malloc(sizeof(struct mydata));
391 a = archive_read_new();
393 archive_read_support_compression_all(a);
394 archive_read_support_format_all(a);
395 archive_read_open(a, mydata, myopen, myread, myclose);
396 while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
397 printf("%s\\n",archive_entry_pathname(entry));
398 archive_read_data_skip(a);
400 archive_read_finish(a);
405 myread(struct archive *a, void *client_data, const void **buff)
407 struct mydata *mydata = client_data;
409 *buff = mydata->buff;
410 return (read(mydata->fd, mydata->buff, 10240));
414 myopen(struct archive *a, void *client_data)
416 struct mydata *mydata = client_data;
418 mydata->fd = open(mydata->name, O_RDONLY);
419 return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
423 myclose(struct archive *a, void *client_data)
425 struct mydata *mydata = client_data;
433 Most functions return zero on success, non-zero on error.
434 The possible return codes include:
436 (the operation succeeded),
438 (the operation succeeded but a non-critical error was encountered),
440 (end-of-archive was encountered),
442 (the operation failed but can be retried),
445 (there was a fatal error; the archive should be closed immediately).
446 Detailed error codes and textual descriptions are available from the
449 .Fn archive_error_string
453 returns a pointer to a freshly allocated
460 .Fn archive_read_data
461 returns a count of bytes actually read or zero at the end of the entry.
467 is returned and an error code and textual description can be retrieved from the
470 .Fn archive_error_string
473 The library expects the client callbacks to behave similarly.
474 If there is an error, you can use
475 .Fn archive_set_error
476 to set an appropriate error code and description,
477 then return one of the non-zero values above.
478 (Note that the value eventually returned to the client may
479 not be the same; many errors that are not critical at the level
480 of basic I/O can prevent the archive from being properly read,
481 thus most I/O errors eventually cause
493 library first appeared in
499 library was written by
500 .An Tim Kientzle Aq kientzle@acm.org .
502 Directories are actually extracted in two distinct phases.
503 Directories are created during
504 .Fn archive_read_extract ,
505 but final permissions are not set until
506 .Fn archive_read_close .
507 This separation is necessary to correctly handle borderline
508 cases such as a non-writable directory containing
509 files, but can cause unexpected results.
510 In particular, directory permissions are not fully
511 restored until the archive is closed.
514 to change the current directory between calls to
515 .Fn archive_read_extract
517 .Fn archive_read_close ,
518 you may confuse the permission-setting logic with
519 the result that directory permissions are restored