Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive / archive.h.in
1 /*-
2  * Copyright (c) 2003-2004 Tim Kientzle
3  * All rights reserved.
4  *
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  *    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.
14  *
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.
25  *
26  * $FreeBSD: src/lib/libarchive/archive.h.in,v 1.22 2005/01/25 06:07:28 kientzle Exp $
27  */
28
29 #ifndef ARCHIVE_H_INCLUDED
30 #define ARCHIVE_H_INCLUDED
31
32 /*
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.
36  *
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
42  * documented here:
43  *
44  *    1 - Version tests are available.
45  *    2 - archive_{read,write}_close available separately from _finish.
46  */
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);
53
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 */
57
58 #define ARCHIVE_BYTES_PER_RECORD          512
59 #define ARCHIVE_DEFAULT_BYTES_PER_BLOCK 10240
60
61 /* Declare our basic types. */
62 struct archive;
63 struct archive_entry;
64
65 /*
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.
69  */
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. */
75
76 /*
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.
82  */
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 */
89
90 /*
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.
94  */
95
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);
104
105 /*
106  * Codes for archive_compression.
107  */
108 #define ARCHIVE_COMPRESSION_NONE        0
109 #define ARCHIVE_COMPRESSION_GZIP        1
110 #define ARCHIVE_COMPRESSION_BZIP2       2
111 #define ARCHIVE_COMPRESSION_COMPRESS    3
112
113 /*
114  * Codes returned by archive_format.
115  *
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.
119  */
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)
131 #define ARCHIVE_FORMAT_ISO9660                  0x40000
132 #define ARCHIVE_FORMAT_ISO9660_ROCKRIDGE        (ARCHIVE_FORMAT_ISO9660 | 1)
133 #define ARCHIVE_FORMAT_ZIP                      0x50000
134
135 /*-
136  * Basic outline for reading an archive:
137  *   1) Ask archive_read_new for an archive reader object.
138  *   2) Update any global properties as appropriate.
139  *      In particular, you'll certainly want to call appropriate
140  *      archive_read_support_XXX functions.
141  *   3) Call archive_read_open_XXX to open the archive
142  *   4) Repeatedly call archive_read_next_header to get information about
143  *      successive archive entries.  Call archive_read_data to extract
144  *      data for entries of interest.
145  *   5) Call archive_read_finish to end processing.
146  */
147 struct archive  *archive_read_new(void);
148
149 /*
150  * The archive_read_support_XXX calls enable auto-detect for this
151  * archive handle.  They also link in the necessary support code.
152  * For example, if you don't want bzlib linked in, don't invoke
153  * support_compression_bzip2().  The "all" functions provide the
154  * obvious shorthand.
155  */
156 int              archive_read_support_compression_all(struct archive *);
157 int              archive_read_support_compression_bzip2(struct archive *);
158 int              archive_read_support_compression_compress(struct archive *);
159 int              archive_read_support_compression_gzip(struct archive *);
160 int              archive_read_support_compression_none(struct archive *);
161
162 int              archive_read_support_format_all(struct archive *);
163 int              archive_read_support_format_cpio(struct archive *);
164 int              archive_read_support_format_gnutar(struct archive *);
165 int              archive_read_support_format_iso9660(struct archive *);
166 int              archive_read_support_format_tar(struct archive *);
167 int              archive_read_support_format_zip(struct archive *);
168
169
170 /* Open the archive using callbacks for archive I/O. */
171 int              archive_read_open(struct archive *, void *_client_data,
172                      archive_open_callback *, archive_read_callback *,
173                      archive_close_callback *);
174
175 /*
176  * The archive_read_open_file function is a convenience function built
177  * on archive_read_open that uses a canned callback suitable for
178  * common situations.  Note that a NULL filename indicates stdin.
179  */
180 int              archive_read_open_file(struct archive *, const char *_file,
181                      size_t _block_size);
182 int              archive_read_open_fd(struct archive *, int _fd,
183                      size_t _block_size);
184
185 /* Parses and returns next entry header. */
186 int              archive_read_next_header(struct archive *,
187                      struct archive_entry **);
188
189 /*
190  * Retrieve the byte offset in UNCOMPRESSED data where last-read
191  * header started.
192  */
193 int64_t          archive_read_header_position(struct archive *);
194
195 /* Read data from the body of an entry.  Similar to read(2). */
196 ssize_t          archive_read_data(struct archive *, void *, size_t);
197 /*
198  * A zero-copy version of archive_read_data that also exposes the file offset
199  * of each returned block.  Note that the client has no way to specify
200  * the desired size of the block.  The API does gaurantee that offsets will
201  * be strictly increasing and that returned blocks will not overlap.
202  */
203 int              archive_read_data_block(struct archive *a,
204                     const void **buff, size_t *size, off_t *offset);
205
206 /*-
207  * Some convenience functions that are built on archive_read_data:
208  *  'skip': skips entire entry
209  *  'into_buffer': writes data into memory buffer that you provide
210  *  'into_fd': writes data to specified filedes
211  */
212 int              archive_read_data_skip(struct archive *);
213 int              archive_read_data_into_buffer(struct archive *, void *buffer,
214                      ssize_t len);
215 int              archive_read_data_into_fd(struct archive *, int fd);
216
217 /*-
218  * Convenience function to recreate the current entry (whose header
219  * has just been read) on disk.
220  *
221  * This does quite a bit more than just copy data to disk. It also:
222  *  - Creates intermediate directories as required.
223  *  - Manages directory permissions:  non-writable directories will
224  *    be initially created with write permission enabled; when the
225  *    archive is closed, dir permissions are edited to the values specified
226  *    in the archive.
227  *  - Checks hardlinks:  hardlinks will not be extracted unless the
228  *    linked-to file was also extracted within the same session. (TODO)
229  */
230
231 /* The "flags" argument selects optional behavior, 'OR' the flags you want. */
232 /* TODO: The 'Default' comments here are not quite correct; clean this up. */
233 #define ARCHIVE_EXTRACT_OWNER   (1) /* Default: owner/group not restored */
234 #define ARCHIVE_EXTRACT_PERM    (2) /* Default: restore perm only for reg file*/
235 #define ARCHIVE_EXTRACT_TIME    (4) /* Default: mod time not restored */
236 #define ARCHIVE_EXTRACT_NO_OVERWRITE (8) /* Default: Replace files on disk */
237 #define ARCHIVE_EXTRACT_UNLINK  (16) /* Default: don't unlink existing files */
238 #define ARCHIVE_EXTRACT_ACL     (32) /* Default: don't restore ACLs */
239 #define ARCHIVE_EXTRACT_FFLAGS  (64) /* Default: don't restore fflags */
240
241 int              archive_read_extract(struct archive *, struct archive_entry *,
242                      int flags);
243 void             archive_read_extract_set_progress_callback(struct archive *,
244                      void (*_progress_func)(void *), void *_user_data);
245
246 /* Close the file and release most resources. */
247 int              archive_read_close(struct archive *);
248 /* Release all resources and destroy the object. */
249 /* Note that archive_read_finish will call archive_read_close for you. */
250 void             archive_read_finish(struct archive *);
251
252 /*-
253  * To create an archive:
254  *   1) Ask archive_write_new for a archive writer object.
255  *   2) Set any global properties.  In particular, you should register
256  *      open/write/close callbacks.
257  *   3) Call archive_write_open to open the file
258  *   4) For each entry:
259  *      - construct an appropriate struct archive_entry structure
260  *      - archive_write_header to write the header
261  *      - archive_write_data to write the entry data
262  *   5) archive_write_close to close the output
263  *   6) archive_write_finish to cleanup the writer and release resources
264  */
265 struct archive  *archive_write_new(void);
266 int              archive_write_set_bytes_per_block(struct archive *,
267                      int bytes_per_block);
268 /* XXX This is badly misnamed; suggestions appreciated. XXX */
269 int              archive_write_set_bytes_in_last_block(struct archive *,
270                      int bytes_in_last_block);
271
272 int              archive_write_set_compression_bzip2(struct archive *);
273 int              archive_write_set_compression_gzip(struct archive *);
274 int              archive_write_set_compression_none(struct archive *);
275 /* A convenience function to set the format based on the code or name. */
276 int              archive_write_set_format(struct archive *, int format_code);
277 int              archive_write_set_format_by_name(struct archive *,
278                      const char *name);
279 /* To minimize link pollution, use one or more of the following. */
280 int              archive_write_set_format_cpio(struct archive *);
281 /* TODO: int archive_write_set_format_old_tar(struct archive *); */
282 int              archive_write_set_format_pax(struct archive *);
283 int              archive_write_set_format_pax_restricted(struct archive *);
284 int              archive_write_set_format_shar(struct archive *);
285 int              archive_write_set_format_shar_dump(struct archive *);
286 int              archive_write_set_format_ustar(struct archive *);
287 int              archive_write_open(struct archive *, void *,
288                      archive_open_callback *, archive_write_callback *,
289                      archive_close_callback *);
290 int              archive_write_open_fd(struct archive *, int _fd);
291 int              archive_write_open_file(struct archive *, const char *_file);
292
293 /*
294  * Note that the library will truncate writes beyond the size provided
295  * to archive_write_header or pad if the provided data is short.
296  */
297 int              archive_write_header(struct archive *,
298                      struct archive_entry *);
299 /* TODO: should be ssize_t, but that might require .so version bump? */
300 int              archive_write_data(struct archive *, const void *, size_t);
301 int              archive_write_close(struct archive *);
302 void             archive_write_finish(struct archive *);
303
304 /*
305  * Accessor functions to read/set various information in
306  * the struct archive object:
307  */
308 /* Bytes written after compression or read before decompression. */
309 int64_t          archive_position_compressed(struct archive *);
310 /* Bytes written to compressor or read from decompressor. */
311 int64_t          archive_position_uncompressed(struct archive *);
312
313 const char      *archive_compression_name(struct archive *);
314 int              archive_compression(struct archive *);
315 int              archive_errno(struct archive *);
316 const char      *archive_error_string(struct archive *);
317 const char      *archive_format_name(struct archive *);
318 int              archive_format(struct archive *);
319 void             archive_set_error(struct archive *, int _err, const char *fmt, ...);
320
321 #endif /* !ARCHIVE_H_INCLUDED */