Import libarchive 2.2.3 fixing a few memory leaks and other fixes.
[dragonfly/port-amd64.git] / contrib / libarchive-2 / libarchive / archive.h.in
blob7a085d0a449344d390729a2ba125a8a6603f251d
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * $FreeBSD: src/lib/libarchive/archive.h.in,v 1.43 2007/05/02 05:29:55 cperciva Exp $
28 #ifndef ARCHIVE_H_INCLUDED
29 #define ARCHIVE_H_INCLUDED
32 * This header file corresponds to:
33 * Library version @ARCHIVE_VERSION@
34 * Shared library version @SHLIB_MAJOR@
37 #include <sys/types.h> /* Linux requires this for off_t */
38 @ARCHIVE_H_INCLUDE_INTTYPES_H@
39 #include <stdio.h> /* For FILE * */
40 #ifndef _WIN32
41 #include <unistd.h> /* For ssize_t and size_t */
42 #else
43 typedef long ssize_t;
44 typedef unsigned int uid_t;
45 typedef unsigned int gid_t;
46 typedef unsigned short mode_t;
47 #endif
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
55 * If ARCHIVE_API_VERSION != archive_api_version(), then the library you
56 * were linked with is using an incompatible API to the one you were
57 * compiled with. This is almost certainly a fatal problem.
59 * ARCHIVE_API_FEATURE is incremented with each significant feature
60 * addition, so you can test (at compile or run time) if a particular
61 * feature is implemented. It's no big deal if ARCHIVE_API_FEATURE !=
62 * archive_api_feature(), as long as both are high enough to include
63 * the features you're relying on. Specific values of FEATURE are
64 * documented here:
66 * 1 - Version tests are available.
67 * 2 - archive_{read,write}_close available separately from _finish.
68 * 3 - open_memory, open_memory2, open_FILE, open_fd available
69 * 5 - archive_write_disk interface available
71 #define ARCHIVE_API_VERSION @ARCHIVE_API_MAJOR@
72 int archive_api_version(void);
73 #define ARCHIVE_API_FEATURE @ARCHIVE_API_MINOR@
74 int archive_api_feature(void);
75 /* Textual name/version of the library. */
76 #define ARCHIVE_LIBRARY_VERSION "libarchive @ARCHIVE_VERSION@"
77 const char * archive_version(void);
79 #define ARCHIVE_BYTES_PER_RECORD 512
80 #define ARCHIVE_DEFAULT_BYTES_PER_BLOCK 10240
82 /* Declare our basic types. */
83 struct archive;
84 struct archive_entry;
87 * Error codes: Use archive_errno() and archive_error_string()
88 * to retrieve details. Unless specified otherwise, all functions
89 * that return 'int' use these codes.
91 #define ARCHIVE_EOF 1 /* Found end of archive. */
92 #define ARCHIVE_OK 0 /* Operation was successful. */
93 #define ARCHIVE_RETRY (-10) /* Retry might succeed. */
94 #define ARCHIVE_WARN (-20) /* Partial success. */
95 /* For example, if write_header "fails", then you can't push data. */
96 #define ARCHIVE_FAILED (-25) /* Current operation cannot complete. */
97 #define ARCHIVE_FATAL (-30) /* No more operations are possible. */
100 * As far as possible, archive_errno returns standard platform errno codes.
101 * Of course, the details vary by platform, so the actual definitions
102 * here are stored in "archive_platform.h". The symbols are listed here
103 * for reference; as a rule, clients should not need to know the exact
104 * platform-dependent error code.
106 /* Unrecognized or invalid file format. */
107 /* #define ARCHIVE_ERRNO_FILE_FORMAT */
108 /* Illegal usage of the library. */
109 /* #define ARCHIVE_ERRNO_PROGRAMMER_ERROR */
110 /* Unknown or unclassified error. */
111 /* #define ARCHIVE_ERRNO_MISC */
114 * Callbacks are invoked to automatically read/skip/write/open/close the
115 * archive. You can provide your own for complex tasks (like breaking
116 * archives across multiple tapes) or use standard ones built into the
117 * library.
120 /* Returns pointer and size of next block of data from archive. */
121 typedef ssize_t archive_read_callback(struct archive *, void *_client_data,
122 const void **_buffer);
123 /* Skips at most request bytes from archive and returns the skipped amount */
124 #if ARCHIVE_API_VERSION < 2
125 typedef ssize_t archive_skip_callback(struct archive *, void *_client_data,
126 size_t request);
127 #else
128 typedef off_t archive_skip_callback(struct archive *, void *_client_data,
129 off_t request);
130 #endif
131 /* Returns size actually written, zero on EOF, -1 on error. */
132 typedef ssize_t archive_write_callback(struct archive *, void *_client_data,
133 const void *_buffer, size_t _length);
134 typedef int archive_open_callback(struct archive *, void *_client_data);
135 typedef int archive_close_callback(struct archive *, void *_client_data);
138 * Codes for archive_compression.
140 #define ARCHIVE_COMPRESSION_NONE 0
141 #define ARCHIVE_COMPRESSION_GZIP 1
142 #define ARCHIVE_COMPRESSION_BZIP2 2
143 #define ARCHIVE_COMPRESSION_COMPRESS 3
144 #define ARCHIVE_COMPRESSION_PROGRAM 4
147 * Codes returned by archive_format.
149 * Top 16 bits identifies the format family (e.g., "tar"); lower
150 * 16 bits indicate the variant. This is updated by read_next_header.
151 * Note that the lower 16 bits will often vary from entry to entry.
152 * In some cases, this variation occurs as libarchive learns more about
153 * the archive (for example, later entries might utilize extensions that
154 * weren't necessary earlier in the archive; in this case, libarchive
155 * will change the format code to indicate the extended format that
156 * was used). In other cases, it's because different tools have
157 * modified the archive and so different parts of the archive
158 * actually have slightly different formts. (Both tar and cpio store
159 * format codes in each entry, so it is quite possible for each
160 * entry to be in a different format.)
162 #define ARCHIVE_FORMAT_BASE_MASK 0xff0000
163 #define ARCHIVE_FORMAT_CPIO 0x10000
164 #define ARCHIVE_FORMAT_CPIO_POSIX (ARCHIVE_FORMAT_CPIO | 1)
165 #define ARCHIVE_FORMAT_CPIO_BIN_LE (ARCHIVE_FORMAT_CPIO | 2)
166 #define ARCHIVE_FORMAT_CPIO_BIN_BE (ARCHIVE_FORMAT_CPIO | 3)
167 #define ARCHIVE_FORMAT_CPIO_SVR4_NOCRC (ARCHIVE_FORMAT_CPIO | 4)
168 #define ARCHIVE_FORMAT_CPIO_SVR4_CRC (ARCHIVE_FORMAT_CPIO | 5)
169 #define ARCHIVE_FORMAT_SHAR 0x20000
170 #define ARCHIVE_FORMAT_SHAR_BASE (ARCHIVE_FORMAT_SHAR | 1)
171 #define ARCHIVE_FORMAT_SHAR_DUMP (ARCHIVE_FORMAT_SHAR | 2)
172 #define ARCHIVE_FORMAT_TAR 0x30000
173 #define ARCHIVE_FORMAT_TAR_USTAR (ARCHIVE_FORMAT_TAR | 1)
174 #define ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE (ARCHIVE_FORMAT_TAR | 2)
175 #define ARCHIVE_FORMAT_TAR_PAX_RESTRICTED (ARCHIVE_FORMAT_TAR | 3)
176 #define ARCHIVE_FORMAT_TAR_GNUTAR (ARCHIVE_FORMAT_TAR | 4)
177 #define ARCHIVE_FORMAT_ISO9660 0x40000
178 #define ARCHIVE_FORMAT_ISO9660_ROCKRIDGE (ARCHIVE_FORMAT_ISO9660 | 1)
179 #define ARCHIVE_FORMAT_ZIP 0x50000
180 #define ARCHIVE_FORMAT_EMPTY 0x60000
181 #define ARCHIVE_FORMAT_AR 0x70000
182 #define ARCHIVE_FORMAT_AR_GNU (ARCHIVE_FORMAT_AR | 1)
183 #define ARCHIVE_FORMAT_AR_BSD (ARCHIVE_FORMAT_AR | 2)
186 * Basic outline for reading an archive:
187 * 1) Ask archive_read_new for an archive reader object.
188 * 2) Update any global properties as appropriate.
189 * In particular, you'll certainly want to call appropriate
190 * archive_read_support_XXX functions.
191 * 3) Call archive_read_open_XXX to open the archive
192 * 4) Repeatedly call archive_read_next_header to get information about
193 * successive archive entries. Call archive_read_data to extract
194 * data for entries of interest.
195 * 5) Call archive_read_finish to end processing.
197 struct archive *archive_read_new(void);
200 * The archive_read_support_XXX calls enable auto-detect for this
201 * archive handle. They also link in the necessary support code.
202 * For example, if you don't want bzlib linked in, don't invoke
203 * support_compression_bzip2(). The "all" functions provide the
204 * obvious shorthand.
206 int archive_read_support_compression_all(struct archive *);
207 int archive_read_support_compression_bzip2(struct archive *);
208 int archive_read_support_compression_compress(struct archive *);
209 int archive_read_support_compression_gzip(struct archive *);
210 int archive_read_support_compression_none(struct archive *);
211 int archive_read_support_compression_program(struct archive *,
212 const char *command);
214 int archive_read_support_format_all(struct archive *);
215 int archive_read_support_format_ar(struct archive *);
216 int archive_read_support_format_cpio(struct archive *);
217 int archive_read_support_format_empty(struct archive *);
218 int archive_read_support_format_gnutar(struct archive *);
219 int archive_read_support_format_iso9660(struct archive *);
220 int archive_read_support_format_tar(struct archive *);
221 int archive_read_support_format_zip(struct archive *);
224 /* Open the archive using callbacks for archive I/O. */
225 int archive_read_open(struct archive *, void *_client_data,
226 archive_open_callback *, archive_read_callback *,
227 archive_close_callback *);
228 int archive_read_open2(struct archive *, void *_client_data,
229 archive_open_callback *, archive_read_callback *,
230 archive_skip_callback *, archive_close_callback *);
233 * A variety of shortcuts that invoke archive_read_open() with
234 * canned callbacks suitable for common situations. The ones that
235 * accept a block size handle tape blocking correctly.
237 /* Use this if you know the filename. Note: NULL indicates stdin. */
238 int archive_read_open_filename(struct archive *,
239 const char *_filename, size_t _block_size);
240 /* archive_read_open_file() is a deprecated synonym for ..._open_filename(). */
241 int archive_read_open_file(struct archive *,
242 const char *_filename, size_t _block_size);
243 /* Read an archive that's stored in memory. */
244 int archive_read_open_memory(struct archive *,
245 void * buff, size_t size);
246 /* A more involved version that is only used for internal testing. */
247 int archive_read_open_memory2(struct archive *a, void *buff,
248 size_t size, size_t read_size);
249 /* Read an archive that's already open, using the file descriptor. */
250 int archive_read_open_fd(struct archive *, int _fd,
251 size_t _block_size);
252 /* Read an archive that's already open, using a FILE *. */
253 /* Note: DO NOT use this with tape drives. */
254 int archive_read_open_FILE(struct archive *, FILE *_file);
256 /* Parses and returns next entry header. */
257 int archive_read_next_header(struct archive *,
258 struct archive_entry **);
261 * Retrieve the byte offset in UNCOMPRESSED data where last-read
262 * header started.
264 int64_t archive_read_header_position(struct archive *);
266 /* Read data from the body of an entry. Similar to read(2). */
267 ssize_t archive_read_data(struct archive *, void *, size_t);
269 * A zero-copy version of archive_read_data that also exposes the file offset
270 * of each returned block. Note that the client has no way to specify
271 * the desired size of the block. The API does guarantee that offsets will
272 * be strictly increasing and that returned blocks will not overlap.
274 int archive_read_data_block(struct archive *a,
275 const void **buff, size_t *size, off_t *offset);
278 * Some convenience functions that are built on archive_read_data:
279 * 'skip': skips entire entry
280 * 'into_buffer': writes data into memory buffer that you provide
281 * 'into_fd': writes data to specified filedes
283 int archive_read_data_skip(struct archive *);
284 int archive_read_data_into_buffer(struct archive *, void *buffer,
285 ssize_t len);
286 int archive_read_data_into_fd(struct archive *, int fd);
289 * Convenience function to recreate the current entry (whose header
290 * has just been read) on disk.
292 * This does quite a bit more than just copy data to disk. It also:
293 * - Creates intermediate directories as required.
294 * - Manages directory permissions: non-writable directories will
295 * be initially created with write permission enabled; when the
296 * archive is closed, dir permissions are edited to the values specified
297 * in the archive.
298 * - Checks hardlinks: hardlinks will not be extracted unless the
299 * linked-to file was also extracted within the same session. (TODO)
302 /* The "flags" argument selects optional behavior, 'OR' the flags you want. */
304 /* Default: Do not try to set owner/group. */
305 #define ARCHIVE_EXTRACT_OWNER (1)
306 /* Default: Do obey umask, do not restore SUID/SGID/SVTX bits. */
307 #define ARCHIVE_EXTRACT_PERM (2)
308 /* Default: Do not restore mtime/atime. */
309 #define ARCHIVE_EXTRACT_TIME (4)
310 /* Default: Replace existing files. */
311 #define ARCHIVE_EXTRACT_NO_OVERWRITE (8)
312 /* Default: Try create first, unlink only if create fails with EEXIST. */
313 #define ARCHIVE_EXTRACT_UNLINK (16)
314 /* Default: Do not restore ACLs. */
315 #define ARCHIVE_EXTRACT_ACL (32)
316 /* Default: Do not restore fflags. */
317 #define ARCHIVE_EXTRACT_FFLAGS (64)
318 /* Default: Do not restore xattrs. */
319 #define ARCHIVE_EXTRACT_XATTR (128)
320 /* Default: Do not try to guard against extracts redirected by symlinks. */
321 /* Note: With ARCHIVE_EXTRACT_UNLINK, will remove any intermediate symlink. */
322 #define ARCHIVE_EXTRACT_SECURE_SYMLINKS (256)
323 /* Default: Do not reject entries with '..' as path elements. */
324 #define ARCHIVE_EXTRACT_SECURE_NODOTDOT (512)
325 /* Default: Create parent directories as needed. */
326 #define ARCHIVE_EXTRACT_NO_AUTODIR (1024)
327 /* Default: Overwrite files, even if one on disk is newer. */
328 #define ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER (2048)
330 int archive_read_extract(struct archive *, struct archive_entry *,
331 int flags);
332 void archive_read_extract_set_progress_callback(struct archive *,
333 void (*_progress_func)(void *), void *_user_data);
335 /* Record the dev/ino of a file that will not be written. This is
336 * generally set to the dev/ino of the archive being read. */
337 void archive_read_extract_set_skip_file(struct archive *,
338 dev_t, ino_t);
340 /* Close the file and release most resources. */
341 int archive_read_close(struct archive *);
342 /* Release all resources and destroy the object. */
343 /* Note that archive_read_finish will call archive_read_close for you. */
344 #if ARCHIVE_API_VERSION > 1
345 int archive_read_finish(struct archive *);
346 #else
347 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
348 /* Erroneously declared to return void in libarchive 1.x */
349 void archive_read_finish(struct archive *);
350 #endif
353 * To create an archive:
354 * 1) Ask archive_write_new for a archive writer object.
355 * 2) Set any global properties. In particular, you should set
356 * the compression and format to use.
357 * 3) Call archive_write_open to open the file (most people
358 * will use archive_write_open_file or archive_write_open_fd,
359 * which provide convenient canned I/O callbacks for you).
360 * 4) For each entry:
361 * - construct an appropriate struct archive_entry structure
362 * - archive_write_header to write the header
363 * - archive_write_data to write the entry data
364 * 5) archive_write_close to close the output
365 * 6) archive_write_finish to cleanup the writer and release resources
367 struct archive *archive_write_new(void);
368 int archive_write_set_bytes_per_block(struct archive *,
369 int bytes_per_block);
370 int archive_write_get_bytes_per_block(struct archive *);
371 /* XXX This is badly misnamed; suggestions appreciated. XXX */
372 int archive_write_set_bytes_in_last_block(struct archive *,
373 int bytes_in_last_block);
374 int archive_write_get_bytes_in_last_block(struct archive *);
376 /* The dev/ino of a file that won't be archived. This is used
377 * to avoid recursively adding an archive to itself. */
378 int archive_write_set_skip_file(struct archive *, dev_t, ino_t);
380 int archive_write_set_compression_bzip2(struct archive *);
381 int archive_write_set_compression_gzip(struct archive *);
382 int archive_write_set_compression_none(struct archive *);
383 int archive_write_set_compression_program(struct archive *,
384 const char *cmd);
385 /* A convenience function to set the format based on the code or name. */
386 int archive_write_set_format(struct archive *, int format_code);
387 int archive_write_set_format_by_name(struct archive *,
388 const char *name);
389 /* To minimize link pollution, use one or more of the following. */
390 int archive_write_set_format_ar_bsd(struct archive *);
391 int archive_write_set_format_ar_svr4(struct archive *);
392 int archive_write_set_format_cpio(struct archive *);
393 /* TODO: int archive_write_set_format_old_tar(struct archive *); */
394 int archive_write_set_format_pax(struct archive *);
395 int archive_write_set_format_pax_restricted(struct archive *);
396 int archive_write_set_format_shar(struct archive *);
397 int archive_write_set_format_shar_dump(struct archive *);
398 int archive_write_set_format_ustar(struct archive *);
399 int archive_write_open(struct archive *, void *,
400 archive_open_callback *, archive_write_callback *,
401 archive_close_callback *);
402 int archive_write_open_fd(struct archive *, int _fd);
403 int archive_write_open_filename(struct archive *, const char *_file);
404 /* A deprecated synonym for archive_write_open_filename() */
405 int archive_write_open_file(struct archive *, const char *_file);
406 int archive_write_open_FILE(struct archive *, FILE *);
407 /* _buffSize is the size of the buffer, _used refers to a variable that
408 * will be updated after each write into the buffer. */
409 int archive_write_open_memory(struct archive *,
410 void *_buffer, size_t _buffSize, size_t *_used);
413 * Note that the library will truncate writes beyond the size provided
414 * to archive_write_header or pad if the provided data is short.
416 int archive_write_header(struct archive *,
417 struct archive_entry *);
418 #if ARCHIVE_API_VERSION > 1
419 ssize_t archive_write_data(struct archive *, const void *, size_t);
420 #else
421 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
422 /* This was erroneously declared to return "int" in libarchive 1.x. */
423 int archive_write_data(struct archive *, const void *, size_t);
424 #endif
425 ssize_t archive_write_data_block(struct archive *, const void *, size_t, off_t);
426 int archive_write_finish_entry(struct archive *);
427 int archive_write_close(struct archive *);
428 #if ARCHIVE_API_VERSION > 1
429 int archive_write_finish(struct archive *);
430 #else
431 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
432 /* Return value was incorrect in libarchive 1.x. */
433 void archive_write_finish(struct archive *);
434 #endif
437 * To create objects on disk:
438 * 1) Ask archive_write_disk_new for a new archive_write_disk object.
439 * 2) Set any global properties. In particular, you should set
440 * the compression and format to use.
441 * 3) For each entry:
442 * - construct an appropriate struct archive_entry structure
443 * - archive_write_header to create the file/dir/etc on disk
444 * - archive_write_data to write the entry data
445 * 4) archive_write_finish to cleanup the writer and release resources
447 * In particular, you can use this in conjunction with archive_read()
448 * to pull entries out of an archive and create them on disk.
450 struct archive *archive_write_disk_new(void);
451 /* This file will not be overwritten. */
452 int archive_write_disk_set_skip_file(struct archive *,
453 dev_t, ino_t);
454 /* Set flags to control how the next item gets created. */
455 int archive_write_disk_set_options(struct archive *,
456 int flags);
458 * The lookup functions are given uname/uid (or gname/gid) pairs and
459 * return a uid (gid) suitable for this system. These are used for
460 * restoring ownership and for setting ACLs. The default functions
461 * are naive, they just return the uid/gid. These are small, so reasonable
462 * for applications that don't need to preserve ownership; they
463 * are probably also appropriate for applications that are doing
464 * same-system backup and restore.
467 * The "standard" lookup functions use common system calls to lookup
468 * the uname/gname, falling back to the uid/gid if the names can't be
469 * found. They cache lookups and are reasonably fast, but can be very
470 * large, so they are not used unless you ask for them. In
471 * particular, these match the specifications of POSIX "pax" and old
472 * POSIX "tar".
474 int archive_write_disk_set_standard_lookup(struct archive *);
476 * If neither the default (naive) nor the standard (big) functions suit
477 * your needs, you can write your own and register them. Be sure to
478 * include a cleanup function if you have allocated private data.
480 int archive_write_disk_set_group_lookup(struct archive *,
481 void *private_data,
482 gid_t (*loookup)(void *, const char *gname, gid_t gid),
483 void (*cleanup)(void *));
484 int archive_write_disk_set_user_lookup(struct archive *,
485 void *private_data,
486 uid_t (*)(void *, const char *uname, uid_t uid),
487 void (*cleanup)(void *));
490 * Accessor functions to read/set various information in
491 * the struct archive object:
493 /* Bytes written after compression or read before decompression. */
494 int64_t archive_position_compressed(struct archive *);
495 /* Bytes written to compressor or read from decompressor. */
496 int64_t archive_position_uncompressed(struct archive *);
498 const char *archive_compression_name(struct archive *);
499 int archive_compression(struct archive *);
500 int archive_errno(struct archive *);
501 const char *archive_error_string(struct archive *);
502 const char *archive_format_name(struct archive *);
503 int archive_format(struct archive *);
504 void archive_clear_error(struct archive *);
505 void archive_set_error(struct archive *, int _err, const char *fmt, ...);
506 void archive_copy_error(struct archive *dest, struct archive *src);
508 #ifdef __cplusplus
510 #endif
512 #endif /* !ARCHIVE_H_INCLUDED */