2 * Copyright (c) 2003-2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/lib/libarchive/archive.h.in,v 1.19 2004/10/18 05:31:01 kientzle Exp $
29 #ifndef ARCHIVE_H_INCLUDED
30 #define ARCHIVE_H_INCLUDED
33 * If ARCHIVE_API_VERSION != archive_api_version(), then the library you
34 * were linked with is using an incompatible API. This is almost
35 * certainly a fatal problem.
37 * ARCHIVE_API_FEATURE is incremented with each significant feature
38 * addition, so you can test (at compile or run time) if a particular
39 * feature is implemented. It's no big deal if ARCHIVE_API_FEATURE !=
40 * archive_api_feature(), as long as both are high enough to include
41 * the features you're relying on. Specific values of FEATURE are
44 * 1 - Version tests are available.
45 * 2 - archive_{read,write}_close available separately from _finish.
47 #define ARCHIVE_API_VERSION @ARCHIVE_API_VERSION@
48 int archive_api_version(void);
49 #define ARCHIVE_API_FEATURE @ARCHIVE_API_FEATURE@
50 int archive_api_feature(void);
51 /* Textual name/version of the library. */
52 const char * archive_version(void);
54 #include <sys/types.h> /* Linux requires this for off_t */
55 #include <inttypes.h> /* For int64_t */
56 #include <unistd.h> /* For ssize_t and size_t */
58 #define ARCHIVE_BYTES_PER_RECORD 512
59 #define ARCHIVE_DEFAULT_BYTES_PER_BLOCK 10240
61 /* Declare our basic types. */
66 * Error codes: Use archive_errno() and archive_error_string()
67 * to retrieve details. Unless specified otherwise, all functions
68 * that return 'int' use these codes.
70 #define ARCHIVE_EOF 1 /* Found end of archive. */
71 #define ARCHIVE_OK 0 /* Operation was successful. */
72 #define ARCHIVE_RETRY (-10) /* Retry might succeed. */
73 #define ARCHIVE_WARN (-20) /* Partial sucess. */
74 #define ARCHIVE_FATAL (-30) /* No more operations are possible. */
77 * As far as possible, archive_errno returns standard platform errno codes.
78 * Of course, the details vary by platform, so the actual definitions
79 * here are stored in "archive_platform.h". The symbols are listed here
80 * for reference; as a rule, clients should not need to know the exact
81 * platform-dependent error code.
83 /* Unrecognized or invalid file format. */
84 /* #define ARCHIVE_ERRNO_FILE_FORMAT */
85 /* Illegal usage of the library. */
86 /* #define ARCHIVE_ERRNO_PROGRAMMER_ERROR */
87 /* Unknown or unclassified error. */
88 /* #define ARCHIVE_ERRNO_MISC */
91 * Callbacks are invoked to automatically read/write/open/close the archive.
92 * You can provide your own for complex tasks (like breaking archives
93 * across multiple tapes) or use standard ones built into the library.
96 /* Returns pointer and size of next block of data from archive. */
97 typedef ssize_t archive_read_callback(struct archive *, void *_client_data,
98 const void **_buffer);
99 /* Returns size actually written, zero on EOF, -1 on error. */
100 typedef ssize_t archive_write_callback(struct archive *, void *_client_data,
101 void *_buffer, size_t _length);
102 typedef int archive_open_callback(struct archive *, void *_client_data);
103 typedef int archive_close_callback(struct archive *, void *_client_data);
106 * Codes for archive_compression.
108 #define ARCHIVE_COMPRESSION_NONE 0
109 #define ARCHIVE_COMPRESSION_GZIP 1
110 #define ARCHIVE_COMPRESSION_BZIP2 2
111 #define ARCHIVE_COMPRESSION_COMPRESS 3
114 * Codes returned by archive_format.
116 * Top 16 bits identifies the format family (e.g., "tar"); lower
117 * 16 bits indicate the variant. This is updated by read_next_header.
118 * Note that the lower 16 bits will often vary from entry to entry.
120 #define ARCHIVE_FORMAT_BASE_MASK 0xff0000U
121 #define ARCHIVE_FORMAT_CPIO 0x10000
122 #define ARCHIVE_FORMAT_CPIO_POSIX (ARCHIVE_FORMAT_CPIO | 1)
123 #define ARCHIVE_FORMAT_SHAR 0x20000
124 #define ARCHIVE_FORMAT_SHAR_BASE (ARCHIVE_FORMAT_SHAR | 1)
125 #define ARCHIVE_FORMAT_SHAR_DUMP (ARCHIVE_FORMAT_SHAR | 2)
126 #define ARCHIVE_FORMAT_TAR 0x30000
127 #define ARCHIVE_FORMAT_TAR_USTAR (ARCHIVE_FORMAT_TAR | 1)
128 #define ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE (ARCHIVE_FORMAT_TAR | 2)
129 #define ARCHIVE_FORMAT_TAR_PAX_RESTRICTED (ARCHIVE_FORMAT_TAR | 3)
130 #define ARCHIVE_FORMAT_TAR_GNUTAR (ARCHIVE_FORMAT_TAR | 4)
133 * Basic outline for reading an archive:
134 * 1) Ask archive_read_new for an archive reader object.
135 * 2) Update any global properties as appropriate.
136 * In particular, you'll certainly want to call appropriate
137 * archive_read_support_XXX functions.
138 * 3) Call archive_read_open_XXX to open the archive
139 * 4) Repeatedly call archive_read_next_header to get information about
140 * successive archive entries. Call archive_read_data to extract
141 * data for entries of interest.
142 * 5) Call archive_read_finish to end processing.
144 struct archive *archive_read_new(void);
147 * The archive_read_support_XXX calls enable auto-detect for this
148 * archive handle. They also link in the necessary support code.
149 * For example, if you don't want bzlib linked in, don't invoke
150 * support_compression_bzip2(). The "all" functions provide the
153 int archive_read_support_compression_all(struct archive *);
154 int archive_read_support_compression_bzip2(struct archive *);
155 int archive_read_support_compression_compress(struct archive *);
156 int archive_read_support_compression_gzip(struct archive *);
157 int archive_read_support_compression_none(struct archive *);
159 int archive_read_support_format_all(struct archive *);
160 int archive_read_support_format_cpio(struct archive *);
161 int archive_read_support_format_gnutar(struct archive *);
162 int archive_read_support_format_tar(struct archive *);
165 /* Open the archive using callbacks for archive I/O. */
166 int archive_read_open(struct archive *, void *_client_data,
167 archive_open_callback *, archive_read_callback *,
168 archive_close_callback *);
171 * The archive_read_open_file function is a convenience function built
172 * on archive_read_open that uses a canned callback suitable for
173 * common situations. Note that a NULL filename indicates stdin.
175 int archive_read_open_file(struct archive *, const char *_file,
177 int archive_read_open_fd(struct archive *, int _fd,
180 /* Parses and returns next entry header. */
181 int archive_read_next_header(struct archive *,
182 struct archive_entry **);
185 * Retrieve the byte offset in UNCOMPRESSED data where last-read
188 int64_t archive_read_header_position(struct archive *);
190 /* Read data from the body of an entry. Similar to read(2). */
191 ssize_t archive_read_data(struct archive *, void *, size_t);
193 * A zero-copy version of archive_read_data that also exposes the file offset
194 * of each returned block. Note that the client has no way to specify
195 * the desired size of the block. The API does gaurantee that offsets will
196 * be strictly increasing and that returned blocks will not overlap.
198 int archive_read_data_block(struct archive *a,
199 const void **buff, size_t *size, off_t *offset);
202 * Some convenience functions that are built on archive_read_data:
203 * 'skip': skips entire entry
204 * 'into_buffer': writes data into memory buffer that you provide
205 * 'into_fd': writes data to specified filedes
207 int archive_read_data_skip(struct archive *);
208 int archive_read_data_into_buffer(struct archive *, void *buffer,
210 int archive_read_data_into_fd(struct archive *, int fd);
213 * Convenience function to recreate the current entry (whose header
214 * has just been read) on disk.
216 * This does quite a bit more than just copy data to disk. It also:
217 * - Creates intermediate directories as required.
218 * - Manages directory permissions: non-writable directories will
219 * be initially created with write permission enabled; when the
220 * archive is closed, dir permissions are edited to the values specified
222 * - Checks hardlinks: hardlinks will not be extracted unless the
223 * linked-to file was also extracted within the same session. (TODO)
226 /* The "flags" argument selects optional behavior, 'OR' the flags you want. */
227 /* TODO: The 'Default' comments here are not quite correct; clean this up. */
228 #define ARCHIVE_EXTRACT_OWNER (1) /* Default: owner/group not restored */
229 #define ARCHIVE_EXTRACT_PERM (2) /* Default: restore perm only for reg file*/
230 #define ARCHIVE_EXTRACT_TIME (4) /* Default: mod time not restored */
231 #define ARCHIVE_EXTRACT_NO_OVERWRITE (8) /* Default: Replace files on disk */
232 #define ARCHIVE_EXTRACT_UNLINK (16) /* Default: don't unlink existing files */
233 #define ARCHIVE_EXTRACT_ACL (32) /* Default: don't restore ACLs */
234 #define ARCHIVE_EXTRACT_FFLAGS (64) /* Default: don't restore fflags */
236 int archive_read_extract(struct archive *, struct archive_entry *,
238 void archive_read_extract_set_progress_callback(struct archive *,
239 void (*_progress_func)(void *), void *_user_data);
241 /* Close the file and release most resources. */
242 int archive_read_close(struct archive *);
243 /* Release all resources and destroy the object. */
244 /* Note that archive_read_finish will call archive_read_close for you. */
245 void archive_read_finish(struct archive *);
248 * To create an archive:
249 * 1) Ask archive_write_new for a archive writer object.
250 * 2) Set any global properties. In particular, you should register
251 * open/write/close callbacks.
252 * 3) Call archive_write_open to open the file
254 * - construct an appropriate struct archive_entry structure
255 * - archive_write_header to write the header
256 * - archive_write_data to write the entry data
257 * 5) archive_write_close to close the output
258 * 6) archive_write_finish to cleanup the writer and release resources
260 struct archive *archive_write_new(void);
261 int archive_write_set_bytes_per_block(struct archive *,
262 int bytes_per_block);
263 /* XXX This is badly misnamed; suggestions appreciated. XXX */
264 int archive_write_set_bytes_in_last_block(struct archive *,
265 int bytes_in_last_block);
267 int archive_write_set_compression_bzip2(struct archive *);
268 int archive_write_set_compression_gzip(struct archive *);
269 int archive_write_set_compression_none(struct archive *);
270 /* A convenience function to set the format based on the code or name. */
271 int archive_write_set_format(struct archive *, int format_code);
272 int archive_write_set_format_by_name(struct archive *,
274 /* To minimize link pollution, use one or more of the following. */
275 int archive_write_set_format_cpio(struct archive *);
276 /* TODO: int archive_write_set_format_old_tar(struct archive *); */
277 int archive_write_set_format_pax(struct archive *);
278 int archive_write_set_format_pax_restricted(struct archive *);
279 int archive_write_set_format_shar(struct archive *);
280 int archive_write_set_format_shar_dump(struct archive *);
281 int archive_write_set_format_ustar(struct archive *);
282 int archive_write_open(struct archive *, void *,
283 archive_open_callback *, archive_write_callback *,
284 archive_close_callback *);
285 int archive_write_open_fd(struct archive *, int _fd);
286 int archive_write_open_file(struct archive *, const char *_file);
289 * Note that the library will truncate writes beyond the size provided
290 * to archive_write_header or pad if the provided data is short.
292 int archive_write_header(struct archive *,
293 struct archive_entry *);
294 /* TODO: should be ssize_t, but that might require .so version bump? */
295 int archive_write_data(struct archive *, const void *, size_t);
296 int archive_write_close(struct archive *);
297 void archive_write_finish(struct archive *);
300 * Accessor functions to read/set various information in
301 * the struct archive object:
303 /* Bytes written after compression or read before decompression. */
304 int64_t archive_position_compressed(struct archive *);
305 /* Bytes written to compressor or read from decompressor. */
306 int64_t archive_position_uncompressed(struct archive *);
308 const char *archive_compression_name(struct archive *);
309 int archive_compression(struct archive *);
310 int archive_errno(struct archive *);
311 const char *archive_error_string(struct archive *);
312 const char *archive_format_name(struct archive *);
313 int archive_format(struct archive *);
314 void archive_set_error(struct archive *, int _err, const char *fmt, ...);
316 #endif /* !ARCHIVE_H_INCLUDED */