pkgsrc - initial commit
[pkgsrc.git] / archivers / libarchive / files / doc / man / archive_read.3
1 .TH archive_read 3 "August 19, 2006" ""
2 .SH NAME
3 \fBarchive_read_new\fP,
4 \fBarchive_read_support_compression_all\fP,
5 \fBarchive_read_support_compression_bzip2\fP,
6 \fBarchive_read_support_compression_compress\fP,
7 \fBarchive_read_support_compression_gzip\fP,
8 \fBarchive_read_support_compression_none\fP,
9 \fBarchive_read_support_compression_program\fP,
10 \fBarchive_read_support_format_all\fP,
11 \fBarchive_read_support_format_cpio\fP,
12 \fBarchive_read_support_format_empty\fP,
13 \fBarchive_read_support_format_iso9660\fP,
14 \fBarchive_read_support_format_tar\fP,
15 \fBarchive_read_support_format_zip\fP,
16 \fBarchive_read_open\fP,
17 \fBarchive_read_open2\fP,
18 \fBarchive_read_open_fd\fP,
19 \fBarchive_read_open_FILE\fP,
20 \fBarchive_read_open_filename\fP,
21 \fBarchive_read_open_memory\fP,
22 \fBarchive_read_next_header\fP,
23 \fBarchive_read_data\fP,
24 \fBarchive_read_data_block\fP,
25 \fBarchive_read_data_skip\fP,
26 \fBarchive_read_data_into_buffer\fP,
27 \fBarchive_read_data_into_fd\fP,
28 \fBarchive_read_extract\fP,
29 \fBarchive_read_extract2\fP,
30 \fBarchive_read_extract_set_progress_callback\fP,
31 \fBarchive_read_close\fP,
32 \fBarchive_read_finish\fP
33 \- functions for reading streaming archives
34 .SH SYNOPSIS
35 \fB#include <archive.h>\fP
36 .br
37 \fIstruct archive *\fP
38 .RE
39 .nh
40 \fBarchive_read_new\fP
41 .hy
42 ("void");
43 \fIint\fP
44 .RE
45 .nh
46 \fBarchive_read_support_compression_all\fP
47 .hy
48 ("struct archive *");
49 \fIint\fP
50 .RE
51 .nh
52 \fBarchive_read_support_compression_bzip2\fP
53 .hy
54 ("struct archive *");
55 \fIint\fP
56 .RE
57 .nh
58 \fBarchive_read_support_compression_compress\fP
59 .hy
60 ("struct archive *");
61 \fIint\fP
62 .RE
63 .nh
64 \fBarchive_read_support_compression_gzip\fP
65 .hy
66 ("struct archive *");
67 \fIint\fP
68 .RE
69 .nh
70 \fBarchive_read_support_compression_none\fP
71 .hy
72 ("struct archive *");
73 \fIint\fP
74 .RE
75 Fo archive_read_support_compression_program
76 Fa "struct archive *"
77 Fa "const char *cmd"
78 Fc
79 \fIint\fP
80 .RE
81 .nh
82 \fBarchive_read_support_format_all\fP
83 .hy
84 ("struct archive *");
85 \fIint\fP
86 .RE
87 .nh
88 \fBarchive_read_support_format_cpio\fP
89 .hy
90 ("struct archive *");
91 \fIint\fP
92 .RE
93 .nh
94 \fBarchive_read_support_format_empty\fP
95 .hy
96 ("struct archive *");
97 \fIint\fP
98 .RE
99 .nh
100 \fBarchive_read_support_format_iso9660\fP
101 .hy
102 ("struct archive *");
103 \fIint\fP
104 .RE
105 .nh
106 \fBarchive_read_support_format_tar\fP
107 .hy
108 ("struct archive *");
109 \fIint\fP
110 .RE
111 .nh
112 \fBarchive_read_support_format_zip\fP
113 .hy
114 ("struct archive *");
115 \fIint\fP
116 .RE
117 Fo archive_read_open
118 Fa "struct archive *"
119 Fa "void *client_data"
120 Fa "archive_open_callback *"
121 Fa "archive_read_callback *"
122 Fa "archive_close_callback *"
123 Fc
124 \fIint\fP
125 .RE
126 Fo archive_read_open2
127 Fa "struct archive *"
128 Fa "void *client_data"
129 Fa "archive_open_callback *"
130 Fa "archive_read_callback *"
131 Fa "archive_skip_callback *"
132 Fa "archive_close_callback *"
133 Fc
134 \fIint\fP
135 .RE
136 .nh
137 \fBarchive_read_open_FILE\fP
138 .hy
139 ("struct archive *" "FILE *file");
140 \fIint\fP
141 .RE
142 .nh
143 \fBarchive_read_open_fd\fP
144 .hy
145 ("struct archive *" "int fd" "size_t block_size");
146 \fIint\fP
147 .RE
148 Fo archive_read_open_filename
149 Fa "struct archive *"
150 Fa "const char *filename"
151 Fa "size_t block_size"
152 Fc
153 \fIint\fP
154 .RE
155 .nh
156 \fBarchive_read_open_memory\fP
157 .hy
158 ("struct archive *" "void *buff" "size_t size");
159 \fIint\fP
160 .RE
161 .nh
162 \fBarchive_read_next_header\fP
163 .hy
164 ("struct archive *" "struct archive_entry **");
165 \fIssize_t\fP
166 .RE
167 .nh
168 \fBarchive_read_data\fP
169 .hy
170 ("struct archive *" "void *buff" "size_t len");
171 \fIint\fP
172 .RE
173 Fo archive_read_data_block
174 Fa "struct archive *"
175 Fa "const void **buff"
176 Fa "size_t *len"
177 Fa "off_t *offset"
178 Fc
179 \fIint\fP
180 .RE
181 .nh
182 \fBarchive_read_data_skip\fP
183 .hy
184 ("struct archive *");
185 \fIint\fP
186 .RE
187 .nh
188 \fBarchive_read_data_into_buffer\fP
189 .hy
190 ("struct archive *" "void *" "ssize_t len");
191 \fIint\fP
192 .RE
193 .nh
194 \fBarchive_read_data_into_fd\fP
195 .hy
196 ("struct archive *" "int fd");
197 \fIint\fP
198 .RE
199 Fo archive_read_extract
200 Fa "struct archive *"
201 Fa "struct archive_entry *"
202 Fa "int flags"
203 Fc
204 \fIint\fP
205 .RE
206 Fo archive_read_extract2
207 Fa "struct archive *src"
208 Fa "struct archive_entry *"
209 Fa "struct archive *dest"
210 Fc
211 \fIvoid\fP
212 .RE
213 Fo archive_read_extract_set_progress_callback
214 Fa "struct archive *"
215 Fa "void (*func)(void *)"
216 Fa "void *user_data"
217 Fc
218 \fIint\fP
219 .RE
220 .nh
221 \fBarchive_read_close\fP
222 .hy
223 ("struct archive *");
224 \fIint\fP
225 .RE
226 .nh
227 \fBarchive_read_finish\fP
228 .hy
229 ("struct archive *");
230 .SH DESCRIPTION
231 These functions provide a complete API for reading streaming archives.
232 The general process is to first create the
233 Tn struct archive
234 object, set options, initialize the reader, iterate over the archive
235 headers and associated data, then close the archive and release all
236 resources.
237 The following summary describes the functions in approximately the
238 order they would be used:
239 .TP
240 .nh
241 \fBarchive_read_new\fP
242 .hy
243 ();
244 Allocates and initializes a
245 Tn struct archive
246 object suitable for reading from an archive.
247 .TP
248 Xo
249 .nh
250 \fBarchive_read_support_compression_all\fP
251 .hy
252 (,);
253 .nh
254 \fBarchive_read_support_compression_bzip2\fP
255 .hy
256 (,);
257 .nh
258 \fBarchive_read_support_compression_compress\fP
259 .hy
260 (,);
261 .nh
262 \fBarchive_read_support_compression_gzip\fP
263 .hy
264 (,);
265 .nh
266 \fBarchive_read_support_compression_none\fP
267 .hy
268 ();
269 Xc
270 Enables auto-detection code and decompression support for the
271 specified compression.
272 Note that
273 ``none''
274 is always enabled by default.
275 For convenience,
276 .nh
277 \fBarchive_read_support_compression_all\fP
278 .hy
279 ();
280 enables all available decompression code.
281 .TP
282 .nh
283 \fBarchive_read_support_compression_program\fP
284 .hy
285 ();
286 Data is fed through the specified external program before being dearchived.
287 Note that this disables automatic detection of the compression format,
288 so it makes no sense to specify this in conjunction with any other
289 decompression option.
290 .TP
291 Xo
292 .nh
293 \fBarchive_read_support_format_all\fP
294 .hy
295 (,);
296 .nh
297 \fBarchive_read_support_format_cpio\fP
298 .hy
299 (,);
300 .nh
301 \fBarchive_read_support_format_empty\fP
302 .hy
303 (,);
304 .nh
305 \fBarchive_read_support_format_iso9660\fP
306 .hy
307 (,);
308 .nh
309 \fBarchive_read_support_format_tar\fP
310 .hy
311 (,);
312 .nh
313 \fBarchive_read_support_format_zip\fP
314 .hy
315 ();
316 Xc
317 Enables support---including auto-detection code---for the
318 specified archive format.
319 For example,
320 .nh
321 \fBarchive_read_support_format_tar\fP
322 .hy
323 ();
324 enables support for a variety of standard tar formats, old-style tar,
325 ustar, pax interchange format, and many common variants.
326 For convenience,
327 .nh
328 \fBarchive_read_support_format_all\fP
329 .hy
330 ();
331 enables support for all available formats.
332 Only empty archives are supported by default.
333 .TP
334 .nh
335 \fBarchive_read_open\fP
336 .hy
337 ();
338 The same as
339 .nh
340 \fBarchive_read_open2\fP
341 .hy
342 (,);
343 except that the skip callback is assumed to be
344 .BR NULL.
345 .TP
346 .nh
347 \fBarchive_read_open2\fP
348 .hy
349 ();
350 Freeze the settings, open the archive, and prepare for reading entries.
351 This is the most generic version of this call, which accepts
352 four callback functions.
353 Most clients will want to use
354 .nh
355 \fBarchive_read_open_filename\fP
356 .hy
357 (,);
358 .nh
359 \fBarchive_read_open_FILE\fP
360 .hy
361 (,);
362 .nh
363 \fBarchive_read_open_fd\fP
364 .hy
365 (,);
366 or
367 .nh
368 \fBarchive_read_open_memory\fP
369 .hy
370 ();
371 instead.
372 The library invokes the client-provided functions to obtain
373 raw bytes from the archive.
374 .TP
375 .nh
376 \fBarchive_read_open_FILE\fP
377 .hy
378 ();
379 Like
380 .nh
381 \fBarchive_read_open\fP
382 .hy
383 (,);
384 except that it accepts a
385 \fI"FILE *"\fP
386 .RE
387 pointer.
388 This function should not be used with tape drives or other devices
389 that require strict I/O blocking.
390 .TP
391 .nh
392 \fBarchive_read_open_fd\fP
393 .hy
394 ();
395 Like
396 .nh
397 \fBarchive_read_open\fP
398 .hy
399 (,);
400 except that it accepts a file descriptor and block size rather than
401 a set of function pointers.
402 Note that the file descriptor will not be automatically closed at
403 end-of-archive.
404 This function is safe for use with tape drives or other blocked devices.
405 .TP
406 .nh
407 \fBarchive_read_open_file\fP
408 .hy
409 ();
410 This is a deprecated synonym for
411 .nh
412 \fBarchive_read_open_filename\fP
413 .hy
414 (.);
415 .TP
416 .nh
417 \fBarchive_read_open_filename\fP
418 .hy
419 ();
420 Like
421 .nh
422 \fBarchive_read_open\fP
423 .hy
424 (,);
425 except that it accepts a simple filename and a block size.
426 A NULL filename represents standard input.
427 This function is safe for use with tape drives or other blocked devices.
428 .TP
429 .nh
430 \fBarchive_read_open_memory\fP
431 .hy
432 ();
433 Like
434 .nh
435 \fBarchive_read_open\fP
436 .hy
437 (,);
438 except that it accepts a pointer and size of a block of
439 memory containing the archive data.
440 .TP
441 .nh
442 \fBarchive_read_next_header\fP
443 .hy
444 ();
445 Read the header for the next entry and return a pointer to
446 a
447 Tn struct archive_entry.
448 .TP
449 .nh
450 \fBarchive_read_data\fP
451 .hy
452 ();
453 Read data associated with the header just read.
454 Internally, this is a convenience function that calls
455 .nh
456 \fBarchive_read_data_block\fP
457 .hy
458 ();
459 and fills any gaps with nulls so that callers see a single
460 continuous stream of data.
461 .TP
462 .nh
463 \fBarchive_read_data_block\fP
464 .hy
465 ();
466 Return the next available block of data for this entry.
467 Unlike
468 .nh
469 \fBarchive_read_data\fP
470 .hy
471 (,);
472 the
473 .nh
474 \fBarchive_read_data_block\fP
475 .hy
476 ();
477 function avoids copying data and allows you to correctly handle
478 sparse files, as supported by some archive formats.
479 The library guarantees that offsets will increase and that blocks
480 will not overlap.
481 Note that the blocks returned from this function can be much larger
482 than the block size read from disk, due to compression
483 and internal buffer optimizations.
484 .TP
485 .nh
486 \fBarchive_read_data_skip\fP
487 .hy
488 ();
489 A convenience function that repeatedly calls
490 .nh
491 \fBarchive_read_data_block\fP
492 .hy
493 ();
494 to skip all of the data for this archive entry.
495 .TP
496 .nh
497 \fBarchive_read_data_into_buffer\fP
498 .hy
499 ();
500 This function is deprecated and will be removed.
501 Use
502 .nh
503 \fBarchive_read_data\fP
504 .hy
505 ();
506 instead.
507 .TP
508 .nh
509 \fBarchive_read_data_into_fd\fP
510 .hy
511 ();
512 A convenience function that repeatedly calls
513 .nh
514 \fBarchive_read_data_block\fP
515 .hy
516 ();
517 to copy the entire entry to the provided file descriptor.
518 .TP
519 .nh
520 \fBarchive_read_extract\fP
521 .hy
522 (, .nh);
523 \fBarchive_read_extract_set_skip_file\fP
524 .hy
525 ();
526 A convenience function that wraps the corresponding
527 \fBarchive_write_disk\fP(3)
528 interfaces.
529 The first call to
530 .nh
531 \fBarchive_read_extract\fP
532 .hy
533 ();
534 creates a restore object using
535 \fBarchive_write_disk_new\fP(3)
536 and
537 \fBarchive_write_disk_set_standard_lookup\fP(3),
538 then transparently invokes
539 \fBarchive_write_disk_set_options\fP(3),
540 \fBarchive_write_header\fP(3),
541 \fBarchive_write_data\fP(3),
542 and
543 \fBarchive_write_finish_entry\fP(3)
544 to create the entry on disk and copy data into it.
545 The
546 \fIflags\fP
547 argument is passed unmodified to
548 \fBarchive_write_disk_set_options\fP(3).
549 .TP
550 .nh
551 \fBarchive_read_extract2\fP
552 .hy
553 ();
554 This is another version of
555 .nh
556 \fBarchive_read_extract\fP
557 .hy
558 ();
559 that allows you to provide your own restore object.
560 In particular, this allows you to override the standard lookup functions
561 using
562 \fBarchive_write_disk_set_group_lookup\fP(3),
563 and
564 \fBarchive_write_disk_set_user_lookup\fP(3).
565 Note that
566 .nh
567 \fBarchive_read_extract2\fP
568 .hy
569 ();
570 does not accept a
571 \fIflags\fP
572 argument; you should use
573 .nh
574 \fBarchive_write_disk_set_options\fP
575 .hy
576 ();
577 to set the restore options yourself.
578 .TP
579 .nh
580 \fBarchive_read_extract_set_progress_callback\fP
581 .hy
582 ();
583 Sets a pointer to a user-defined callback that can be used
584 for updating progress displays during extraction.
585 The progress function will be invoked during the extraction of large
586 regular files.
587 The progress function will be invoked with the pointer provided to this call.
588 Generally, the data pointed to should include a reference to the archive
589 object and the archive_entry object so that various statistics
590 can be retrieved for the progress display.
591 .TP
592 .nh
593 \fBarchive_read_close\fP
594 .hy
595 ();
596 Complete the archive and invoke the close callback.
597 .TP
598 .nh
599 \fBarchive_read_finish\fP
600 .hy
601 ();
602 Invokes
603 .nh
604 \fBarchive_read_close\fP
605 .hy
606 ();
607 if it was not invoked manually, then release all resources.
608 Note: In libarchive 1.x, this function was declared to return
609 \fIvoid,\fP
610 .RE
611 which made it impossible to detect certain errors when
612 .nh
613 \fBarchive_read_close\fP
614 .hy
615 ();
616 was invoked implicitly from this function.
617 The declaration is corrected beginning with libarchive 2.0.
618 Note that the library determines most of the relevant information about
619 the archive by inspection.
620 In particular, it automatically detects
621 \fBgzip\fP(1)
622 or
623 \fBbzip2\fP(1)
624 compression and transparently performs the appropriate decompression.
625 It also automatically detects the archive format.
626 A complete description of the
627 Tn struct archive
628 and
629 Tn struct archive_entry
630 objects can be found in the overview manual page for
631 \fBlibarchive\fP(3).
632 .SH CLIENT CALLBACKS
633 The callback functions must match the following prototypes:
634 .IP
635 \fItypedef ssize_t\fP
636 .RE
637 Fo archive_read_callback
638 Fa "struct archive *"
639 Fa "void *client_data"
640 Fa "const void **buffer"
641 Fc
642 .IP
643 \fItypedef int\fP
644 .RE
645 Fo archive_skip_callback
646 Fa "struct archive *"
647 Fa "void *client_data"
648 Fa "size_t request"
649 Fc
650 .IP
651 \fItypedef int\fP
652 .RE
653 .nh
654 \fBarchive_open_callback\fP
655 .hy
656 ("struct archive *" "void *client_data");
657 .IP
658 \fItypedef int\fP
659 .RE
660 .nh
661 \fBarchive_close_callback\fP
662 .hy
663 ("struct archive *" "void *client_data");
664 The open callback is invoked by
665 .nh
666 \fBarchive_open\fP
667 .hy
668 (.);
669 It should return
670 \fBARCHIVE_OK\fP
671 if the underlying file or data source is successfully
672 opened.
673 If the open fails, it should call
674 .nh
675 \fBarchive_set_error\fP
676 .hy
677 ();
678 to register an error code and message and return
679 \fBARCHIVE_FATAL\fP.
680 The read callback is invoked whenever the library
681 requires raw bytes from the archive.
682 The read callback should read data into a buffer,
683 set the
684 .RS
685 const void **buffer
686 .RE
687 argument to point to the available data, and
688 return a count of the number of bytes available.
689 The library will invoke the read callback again
690 only after it has consumed this data.
691 The library imposes no constraints on the size
692 of the data blocks returned.
693 On end-of-file, the read callback should
694 return zero.
695 On error, the read callback should invoke
696 .nh
697 \fBarchive_set_error\fP
698 .hy
699 ();
700 to register an error code and message and
701 return -1.
702 The skip callback is invoked when the
703 library wants to ignore a block of data.
704 The return value is the number of bytes actually
705 skipped, which may differ from the request.
706 If the callback cannot skip data, it should return
707 zero.
708 If the skip callback is not provided (the
709 function pointer is
710 .BR NULL ),
711 the library will invoke the read function
712 instead and simply discard the result.
713 A skip callback can provide significant
714 performance gains when reading uncompressed
715 archives from slow disk drives or other media
716 that can skip quickly.
717 The close callback is invoked by archive_close when
718 the archive processing is complete.
719 The callback should return
720 \fBARCHIVE_OK\fP
721 on success.
722 On failure, the callback should invoke
723 .nh
724 \fBarchive_set_error\fP
725 .hy
726 ();
727 to register an error code and message and
728 return
729 \fBARCHIVE_FATAL.\fP
730 .SH EXAMPLE
731 The following illustrates basic usage of the library.
732 In this example,
733 the callback functions are simply wrappers around the standard
734 \fBopen\fP(2),
735 \fBread\fP(2),
736 and
737 \fBclose\fP(2)
738 system calls.
739 .RS
740 void
741 list_archive(const char *name)
742 {
743   struct mydata *mydata;
744   struct archive *a;
745   struct archive_entry *entry;
746   mydata = malloc(sizeof(struct mydata));
747   a = archive_read_new();
748   mydata->name = name;
749   archive_read_support_compression_all(a);
750   archive_read_support_format_all(a);
751   archive_read_open(a, mydata, myopen, myread, myclose);
752   while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
753     printf("%s\\n",archive_entry_pathname(entry));
754     archive_read_data_skip(a);
755   }
756   archive_read_finish(a);
757   free(mydata);
758 }
759 ssize_t
760 myread(struct archive *a, void *client_data, const void **buff)
761 {
762   struct mydata *mydata = client_data;
763   *buff = mydata->buff;
764   return (read(mydata->fd, mydata->buff, 10240));
765 }
766 int
767 myopen(struct archive *a, void *client_data)
768 {
769   struct mydata *mydata = client_data;
770   mydata->fd = open(mydata->name, O_RDONLY);
771   return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
772 }
773 int
774 myclose(struct archive *a, void *client_data)
775 {
776   struct mydata *mydata = client_data;
777   if (mydata->fd > 0)
778     close(mydata->fd);
779   return (ARCHIVE_OK);
780 }
781 .RE
782 .SH RETURN VALUES
783 Most functions return zero on success, non-zero on error.
784 The possible return codes include:
785 \fBARCHIVE_OK\fP
786 (the operation succeeded),
787 \fBARCHIVE_WARN\fP
788 (the operation succeeded but a non-critical error was encountered),
789 \fBARCHIVE_EOF\fP
790 (end-of-archive was encountered),
791 \fBARCHIVE_RETRY\fP
792 (the operation failed but can be retried),
793 and
794 \fBARCHIVE_FATAL\fP
795 (there was a fatal error; the archive should be closed immediately).
796 Detailed error codes and textual descriptions are available from the
797 .nh
798 \fBarchive_errno\fP
799 .hy
800 ();
801 and
802 .nh
803 \fBarchive_error_string\fP
804 .hy
805 ();
806 functions.
807 .nh
808 \fBarchive_read_new\fP
809 .hy
810 ();
811 returns a pointer to a freshly allocated
812 Tn struct archive
813 object.
814 It returns
815 .BR NULL
816 on error.
817 .nh
818 \fBarchive_read_data\fP
819 .hy
820 ();
821 returns a count of bytes actually read or zero at the end of the entry.
822 On error, a value of
823 \fBARCHIVE_FATAL\fP,
824 \fBARCHIVE_WARN\fP,
825 or
826 \fBARCHIVE_RETRY\fP
827 is returned and an error code and textual description can be retrieved from the
828 .nh
829 \fBarchive_errno\fP
830 .hy
831 ();
832 and
833 .nh
834 \fBarchive_error_string\fP
835 .hy
836 ();
837 functions.
838 The library expects the client callbacks to behave similarly.
839 If there is an error, you can use
840 .nh
841 \fBarchive_set_error\fP
842 .hy
843 ();
844 to set an appropriate error code and description,
845 then return one of the non-zero values above.
846 (Note that the value eventually returned to the client may
847 not be the same; many errors that are not critical at the level
848 of basic I/O can prevent the archive from being properly read,
849 thus most I/O errors eventually cause
850 \fBARCHIVE_FATAL\fP
851 to be returned.)
852 .SH SEE ALSO
853 \fBtar\fP(1),
854 \fBarchive\fP(3),
855 \fBarchive_util\fP(3),
856 \fBtar\fP(5)
857 .SH HISTORY
858 The
859 \fBlibarchive\fP
860 library first appeared in
861 FreeBSD 5.3.
862 .SH AUTHORS
863 -nosplit
864 The
865 \fBlibarchive\fP
866 library was written by
867 Tim Kientzle <kientzle@acm.org.>
868 .SH BUGS
869 Many traditional archiver programs treat
870 empty files as valid empty archives.
871 For example, many implementations of
872 \fBtar\fP(1)
873 allow you to append entries to an empty file.
874 Of course, it is impossible to determine the format of an empty file
875 by inspecting the contents, so this library treats empty files as
876 having a special
877 ``empty''
878 format.