1ARCHIVE_READ(3)          BSD Library Functions Manual          ARCHIVE_READ(3)
2

NAME

4     archive_read — functions for reading streaming archives
5

LIBRARY

7     Streaming Archive Library (libarchive, -larchive)
8

SYNOPSIS

10     #include <archive.h>
11

DESCRIPTION

13     These functions provide a complete API for reading streaming archives.
14     The general process is to first create the struct archive object, set
15     options, initialize the reader, iterate over the archive headers and
16     associated data, then close the archive and release all resources.
17
18   Create archive object
19     See archive_read_new(3).
20
21     To read an archive, you must first obtain an initialized struct archive
22     object from archive_read_new().
23
24   Enable filters and formats
25     See archive_read_filter(3) and archive_read_format(3).
26
27     You can then modify this object for the desired operations with the vari‐
28     ous archive_read_set_XXX() and archive_read_support_XXX() functions.  In
29     particular, you will need to invoke appropriate
30     archive_read_support_XXX() functions to enable the corresponding compres‐
31     sion and format support.  Note that these latter functions perform two
32     distinct operations: they cause the corresponding support code to be
33     linked into your program, and they enable the corresponding auto-detect
34     code.  Unless you have specific constraints, you will generally want to
35     invoke archive_read_support_filter_all() and
36     archive_read_support_format_all() to enable auto-detect for all formats
37     and compression types currently supported by the library.
38
39   Set options
40     See archive_read_set_options(3).
41
42   Open archive
43     See archive_read_open(3).
44
45     Once you have prepared the struct archive object, you call
46     archive_read_open() to actually open the archive and prepare it for read‐
47     ing.  There are several variants of this function; the most basic expects
48     you to provide pointers to several functions that can provide blocks of
49     bytes from the archive.  There are convenience forms that allow you to
50     specify a filename, file descriptor, FILE * object, or a block of memory
51     from which to read the archive data.  Note that the core library makes no
52     assumptions about the size of the blocks read; callback functions are
53     free to read whatever block size is most appropriate for the medium.
54
55   Consume archive
56     See archive_read_header(3), archive_read_data(3) and
57     archive_read_extract(3).
58
59     Each archive entry consists of a header followed by a certain amount of
60     data.  You can obtain the next header with archive_read_next_header(),
61     which returns a pointer to an struct archive_entry structure with infor‐
62     mation about the current archive element.  If the entry is a regular
63     file, then the header will be followed by the file data.  You can use
64     archive_read_data() (which works much like the read(2) system call) to
65     read this data from the archive, or archive_read_data_block() which pro‐
66     vides a slightly more efficient interface.  You may prefer to use the
67     higher-level archive_read_data_skip(), which reads and discards the data
68     for this entry, archive_read_data_into_fd(), which copies the data to the
69     provided file descriptor, or archive_read_extract(), which recreates the
70     specified entry on disk and copies data from the archive.  In particular,
71     note that archive_read_extract() uses the struct archive_entry structure
72     that you provide it, which may differ from the entry just read from the
73     archive.  In particular, many applications will want to override the
74     pathname, file permissions, or ownership.
75
76   Release resources
77     See archive_read_free(3).
78
79     Once you have finished reading data from the archive, you should call
80     archive_read_close() to close the archive, then call archive_read_free()
81     to release all resources, including all memory allocated by the library.
82

EXAMPLE

84     The following illustrates basic usage of the library.  In this example,
85     the callback functions are simply wrappers around the standard open(2),
86     read(2), and close(2) system calls.
87
88           void
89           list_archive(const char *name)
90           {
91             struct mydata *mydata;
92             struct archive *a;
93             struct archive_entry *entry;
94
95             mydata = malloc(sizeof(struct mydata));
96             a = archive_read_new();
97             mydata->name = name;
98             archive_read_support_filter_all(a);
99             archive_read_support_format_all(a);
100             archive_read_open(a, mydata, myopen, myread, myclose);
101             while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
102               printf("%s\n",archive_entry_pathname(entry));
103               archive_read_data_skip(a);
104             }
105             archive_read_free(a);
106             free(mydata);
107           }
108
109           la_ssize_t
110           myread(struct archive *a, void *client_data, const void **buff)
111           {
112             struct mydata *mydata = client_data;
113
114             *buff = mydata->buff;
115             return (read(mydata->fd, mydata->buff, 10240));
116           }
117
118           int
119           myopen(struct archive *a, void *client_data)
120           {
121             struct mydata *mydata = client_data;
122
123             mydata->fd = open(mydata->name, O_RDONLY);
124             return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
125           }
126
127           int
128           myclose(struct archive *a, void *client_data)
129           {
130             struct mydata *mydata = client_data;
131
132             if (mydata->fd > 0)
133               close(mydata->fd);
134             return (ARCHIVE_OK);
135           }
136

SEE ALSO

138     tar(1), libarchive(3), archive_read_new(3), archive_read_data(3),
139     archive_read_extract(3), archive_read_filter(3), archive_read_format(3),
140     archive_read_header(3), archive_read_open(3),
141     archive_read_set_options(3), archive_util(3), tar(5)
142

HISTORY

144     The libarchive library first appeared in FreeBSD 5.3.
145

AUTHORS

147     The libarchive library was written by Tim Kientzle <kientzle@acm.org>.
148

BUGS

150     Many traditional archiver programs treat empty files as valid empty ar‐
151     chives.  For example, many implementations of tar(1) allow you to append
152     entries to an empty file.  Of course, it is impossible to determine the
153     format of an empty file by inspecting the contents, so this library
154     treats empty files as having a special “empty” format.
155
156BSD                            February 2, 2012                            BSD
Impressum