Import libarchive-2.5.4b.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_entry.h
blobf8601b9ee284ff296061add51910ad9871d5242e
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_entry.h,v 1.26 2008/03/14 23:00:53 kientzle Exp $
28 #ifndef ARCHIVE_ENTRY_H_INCLUDED
29 #define ARCHIVE_ENTRY_H_INCLUDED
32 * Note: archive_entry.h is for use outside of libarchive; the
33 * configuration headers (config.h, archive_platform.h, etc.) are
34 * purely internal. Do NOT use HAVE_XXX configuration macros to
35 * control the behavior of this header! If you must conditionalize,
36 * use predefined compiler and/or platform macros.
39 #include <sys/types.h>
40 #include <stddef.h> /* for wchar_t */
41 #include <time.h>
43 /* Get appropriate definitions of standard POSIX-style types. */
44 /* These should match the types used in 'struct stat' */
45 #ifdef _WIN32
46 #define __LA_UID_T unsigned int
47 #define __LA_GID_T unsigned int
48 #define __LA_DEV_T unsigned int
49 #define __LA_MODE_T unsigned short
50 #else
51 #include <unistd.h>
52 #define __LA_UID_T uid_t
53 #define __LA_GID_T gid_t
54 #define __LA_DEV_T dev_t
55 #define __LA_MODE_T mode_t
56 #endif
59 * XXX Is this defined for all Windows compilers? If so, in what
60 * header? It would be nice to remove the __LA_INO_T indirection and
61 * just use plain ino_t everywhere. Likewise for the other types just
62 * above.
64 #define __LA_INO_T ino_t
68 * On Windows, define LIBARCHIVE_STATIC if you're building or using a
69 * .lib. The default here assumes you're building a DLL. Only
70 * libarchive source should ever define __LIBARCHIVE_BUILD.
72 #if ((defined __WIN32__) || (defined _WIN32)) && (!defined LIBARCHIVE_STATIC)
73 # ifdef __LIBARCHIVE_BUILD
74 # ifdef __GNUC__
75 # define __LA_DECL __attribute__((dllexport)) extern
76 # else
77 # define __LA_DECL __declspec(dllexport)
78 # endif
79 # else
80 # ifdef __GNUC__
81 # define __LA_DECL __attribute__((dllimport)) extern
82 # else
83 # define __LA_DECL __declspec(dllimport)
84 # endif
85 # endif
86 #else
87 /* Static libraries on all platforms and shared libraries on non-Windows. */
88 # define __LA_DECL
89 #endif
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
96 * Description of an archive entry.
98 * You can think of this as "struct stat" with some text fields added in.
100 * TODO: Add "comment", "charset", and possibly other entries that are
101 * supported by "pax interchange" format. However, GNU, ustar, cpio,
102 * and other variants don't support these features, so they're not an
103 * excruciatingly high priority right now.
105 * TODO: "pax interchange" format allows essentially arbitrary
106 * key/value attributes to be attached to any entry. Supporting
107 * such extensions may make this library useful for special
108 * applications (e.g., a package manager could attach special
109 * package-management attributes to each entry).
111 struct archive_entry;
114 * File-type constants. These are returned from archive_entry_filetype()
115 * and passed to archive_entry_set_filetype().
117 * These values match S_XXX defines on every platform I've checked,
118 * including Windows, AIX, Linux, Solaris, and BSD. They're
119 * (re)defined here because platforms generally don't define the ones
120 * they don't support. For example, Windows doesn't define S_IFLNK or
121 * S_IFBLK. Instead of having a mass of conditional logic and system
122 * checks to define any S_XXX values that aren't supported locally,
123 * I've just defined a new set of such constants so that
124 * libarchive-based applications can manipulate and identify archive
125 * entries properly even if the hosting platform can't store them on
126 * disk.
128 * These values are also used directly within some portable formats,
129 * such as cpio. If you find a platform that varies from these, the
130 * correct solution is to leave these alone and translate from these
131 * portable values to platform-native values when entries are read from
132 * or written to disk.
134 #define AE_IFMT 0170000
135 #define AE_IFREG 0100000
136 #define AE_IFLNK 0120000
137 #define AE_IFSOCK 0140000
138 #define AE_IFCHR 0020000
139 #define AE_IFBLK 0060000
140 #define AE_IFDIR 0040000
141 #define AE_IFIFO 0010000
144 * Basic object manipulation
147 __LA_DECL struct archive_entry *archive_entry_clear(struct archive_entry *);
148 /* The 'clone' function does a deep copy; all of the strings are copied too. */
149 __LA_DECL struct archive_entry *archive_entry_clone(struct archive_entry *);
150 __LA_DECL void archive_entry_free(struct archive_entry *);
151 __LA_DECL struct archive_entry *archive_entry_new(void);
154 * Retrieve fields from an archive_entry.
157 __LA_DECL time_t archive_entry_atime(struct archive_entry *);
158 __LA_DECL long archive_entry_atime_nsec(struct archive_entry *);
159 __LA_DECL time_t archive_entry_ctime(struct archive_entry *);
160 __LA_DECL long archive_entry_ctime_nsec(struct archive_entry *);
161 __LA_DECL dev_t archive_entry_dev(struct archive_entry *);
162 __LA_DECL dev_t archive_entry_devmajor(struct archive_entry *);
163 __LA_DECL dev_t archive_entry_devminor(struct archive_entry *);
164 __LA_DECL __LA_MODE_T archive_entry_filetype(struct archive_entry *);
165 __LA_DECL void archive_entry_fflags(struct archive_entry *,
166 unsigned long * /* set */,
167 unsigned long * /* clear */);
168 __LA_DECL const char *archive_entry_fflags_text(struct archive_entry *);
169 __LA_DECL __LA_GID_T archive_entry_gid(struct archive_entry *);
170 __LA_DECL const char *archive_entry_gname(struct archive_entry *);
171 __LA_DECL const wchar_t *archive_entry_gname_w(struct archive_entry *);
172 __LA_DECL const char *archive_entry_hardlink(struct archive_entry *);
173 __LA_DECL const wchar_t *archive_entry_hardlink_w(struct archive_entry *);
174 __LA_DECL __LA_INO_T archive_entry_ino(struct archive_entry *);
175 __LA_DECL __LA_MODE_T archive_entry_mode(struct archive_entry *);
176 __LA_DECL time_t archive_entry_mtime(struct archive_entry *);
177 __LA_DECL long archive_entry_mtime_nsec(struct archive_entry *);
178 __LA_DECL unsigned int archive_entry_nlink(struct archive_entry *);
179 __LA_DECL const char *archive_entry_pathname(struct archive_entry *);
180 __LA_DECL const wchar_t *archive_entry_pathname_w(struct archive_entry *);
181 __LA_DECL dev_t archive_entry_rdev(struct archive_entry *);
182 __LA_DECL dev_t archive_entry_rdevmajor(struct archive_entry *);
183 __LA_DECL dev_t archive_entry_rdevminor(struct archive_entry *);
184 __LA_DECL const char *archive_entry_sourcepath(struct archive_entry *);
185 __LA_DECL int64_t archive_entry_size(struct archive_entry *);
186 __LA_DECL const char *archive_entry_strmode(struct archive_entry *);
187 __LA_DECL const char *archive_entry_symlink(struct archive_entry *);
188 __LA_DECL const wchar_t *archive_entry_symlink_w(struct archive_entry *);
189 __LA_DECL __LA_UID_T archive_entry_uid(struct archive_entry *);
190 __LA_DECL const char *archive_entry_uname(struct archive_entry *);
191 __LA_DECL const wchar_t *archive_entry_uname_w(struct archive_entry *);
194 * Set fields in an archive_entry.
196 * Note that string 'set' functions do not copy the string, only the pointer.
197 * In contrast, 'copy' functions do copy the object pointed to.
200 __LA_DECL void archive_entry_set_atime(struct archive_entry *, time_t, long);
201 __LA_DECL void archive_entry_set_ctime(struct archive_entry *, time_t, long);
202 __LA_DECL void archive_entry_set_dev(struct archive_entry *, dev_t);
203 __LA_DECL void archive_entry_set_devmajor(struct archive_entry *, dev_t);
204 __LA_DECL void archive_entry_set_devminor(struct archive_entry *, dev_t);
205 __LA_DECL void archive_entry_set_filetype(struct archive_entry *, unsigned int);
206 __LA_DECL void archive_entry_set_fflags(struct archive_entry *,
207 unsigned long /* set */, unsigned long /* clear */);
208 /* Returns pointer to start of first invalid token, or NULL if none. */
209 /* Note that all recognized tokens are processed, regardless. */
210 __LA_DECL const char *archive_entry_copy_fflags_text(struct archive_entry *,
211 const char *);
212 __LA_DECL const wchar_t *archive_entry_copy_fflags_text_w(struct archive_entry *,
213 const wchar_t *);
214 __LA_DECL void archive_entry_set_gid(struct archive_entry *, __LA_GID_T);
215 __LA_DECL void archive_entry_set_gname(struct archive_entry *, const char *);
216 __LA_DECL void archive_entry_copy_gname(struct archive_entry *, const char *);
217 __LA_DECL void archive_entry_copy_gname_w(struct archive_entry *, const wchar_t *);
218 __LA_DECL int archive_entry_update_gname_utf8(struct archive_entry *, const char *);
219 __LA_DECL void archive_entry_set_hardlink(struct archive_entry *, const char *);
220 __LA_DECL void archive_entry_copy_hardlink(struct archive_entry *, const char *);
221 __LA_DECL void archive_entry_copy_hardlink_w(struct archive_entry *, const wchar_t *);
222 __LA_DECL void archive_entry_set_ino(struct archive_entry *, unsigned long);
223 __LA_DECL void archive_entry_set_link(struct archive_entry *, const char *);
224 __LA_DECL void archive_entry_copy_link(struct archive_entry *, const char *);
225 __LA_DECL void archive_entry_copy_link_w(struct archive_entry *, const wchar_t *);
226 __LA_DECL int archive_entry_update_link_utf8(struct archive_entry *, const char *);
227 __LA_DECL void archive_entry_set_mode(struct archive_entry *, __LA_MODE_T);
228 __LA_DECL void archive_entry_set_mtime(struct archive_entry *, time_t, long);
229 __LA_DECL void archive_entry_set_nlink(struct archive_entry *, unsigned int);
230 __LA_DECL void archive_entry_set_pathname(struct archive_entry *, const char *);
231 __LA_DECL void archive_entry_copy_pathname(struct archive_entry *, const char *);
232 __LA_DECL void archive_entry_copy_pathname_w(struct archive_entry *, const wchar_t *);
233 __LA_DECL int archive_entry_update_pathname_utf8(struct archive_entry *, const char *);
234 __LA_DECL void archive_entry_set_perm(struct archive_entry *, __LA_MODE_T);
235 __LA_DECL void archive_entry_set_rdev(struct archive_entry *, dev_t);
236 __LA_DECL void archive_entry_set_rdevmajor(struct archive_entry *, dev_t);
237 __LA_DECL void archive_entry_set_rdevminor(struct archive_entry *, dev_t);
238 __LA_DECL void archive_entry_set_size(struct archive_entry *, int64_t);
239 __LA_DECL void archive_entry_copy_sourcepath(struct archive_entry *, const char *);
240 __LA_DECL void archive_entry_set_symlink(struct archive_entry *, const char *);
241 __LA_DECL void archive_entry_copy_symlink(struct archive_entry *, const char *);
242 __LA_DECL void archive_entry_copy_symlink_w(struct archive_entry *, const wchar_t *);
243 __LA_DECL void archive_entry_set_uid(struct archive_entry *, __LA_UID_T);
244 __LA_DECL void archive_entry_set_uname(struct archive_entry *, const char *);
245 __LA_DECL void archive_entry_copy_uname(struct archive_entry *, const char *);
246 __LA_DECL void archive_entry_copy_uname_w(struct archive_entry *, const wchar_t *);
247 __LA_DECL int archive_entry_update_uname_utf8(struct archive_entry *, const char *);
249 * Routines to bulk copy fields to/from a platform-native "struct
250 * stat." Libarchive used to just store a struct stat inside of each
251 * archive_entry object, but this created issues when trying to
252 * manipulate archives on systems different than the ones they were
253 * created on.
255 * TODO: On Linux, provide both stat32 and stat64 versions of these functions.
257 __LA_DECL const struct stat *archive_entry_stat(struct archive_entry *);
258 __LA_DECL void archive_entry_copy_stat(struct archive_entry *, const struct stat *);
261 * ACL routines. This used to simply store and return text-format ACL
262 * strings, but that proved insufficient for a number of reasons:
263 * = clients need control over uname/uid and gname/gid mappings
264 * = there are many different ACL text formats
265 * = would like to be able to read/convert archives containing ACLs
266 * on platforms that lack ACL libraries
268 * This last point, in particular, forces me to implement a reasonably
269 * complete set of ACL support routines.
271 * TODO: Extend this to support NFSv4/NTFS permissions. That should
272 * allow full ACL support on Mac OS, in particular, which uses
273 * POSIX.1e-style interfaces to manipulate NFSv4/NTFS permissions.
277 * Permission bits mimic POSIX.1e. Note that I've not followed POSIX.1e's
278 * "permset"/"perm" abstract type nonsense. A permset is just a simple
279 * bitmap, following long-standing Unix tradition.
281 #define ARCHIVE_ENTRY_ACL_EXECUTE 1
282 #define ARCHIVE_ENTRY_ACL_WRITE 2
283 #define ARCHIVE_ENTRY_ACL_READ 4
285 /* We need to be able to specify either or both of these. */
286 #define ARCHIVE_ENTRY_ACL_TYPE_ACCESS 256
287 #define ARCHIVE_ENTRY_ACL_TYPE_DEFAULT 512
289 /* Tag values mimic POSIX.1e */
290 #define ARCHIVE_ENTRY_ACL_USER 10001 /* Specified user. */
291 #define ARCHIVE_ENTRY_ACL_USER_OBJ 10002 /* User who owns the file. */
292 #define ARCHIVE_ENTRY_ACL_GROUP 10003 /* Specified group. */
293 #define ARCHIVE_ENTRY_ACL_GROUP_OBJ 10004 /* Group who owns the file. */
294 #define ARCHIVE_ENTRY_ACL_MASK 10005 /* Modify group access. */
295 #define ARCHIVE_ENTRY_ACL_OTHER 10006 /* Public. */
298 * Set the ACL by clearing it and adding entries one at a time.
299 * Unlike the POSIX.1e ACL routines, you must specify the type
300 * (access/default) for each entry. Internally, the ACL data is just
301 * a soup of entries. API calls here allow you to retrieve just the
302 * entries of interest. This design (which goes against the spirit of
303 * POSIX.1e) is useful for handling archive formats that combine
304 * default and access information in a single ACL list.
306 __LA_DECL void archive_entry_acl_clear(struct archive_entry *);
307 __LA_DECL void archive_entry_acl_add_entry(struct archive_entry *,
308 int /* type */, int /* permset */, int /* tag */,
309 int /* qual */, const char * /* name */);
310 __LA_DECL void archive_entry_acl_add_entry_w(struct archive_entry *,
311 int /* type */, int /* permset */, int /* tag */,
312 int /* qual */, const wchar_t * /* name */);
315 * To retrieve the ACL, first "reset", then repeatedly ask for the
316 * "next" entry. The want_type parameter allows you to request only
317 * access entries or only default entries.
319 __LA_DECL int archive_entry_acl_reset(struct archive_entry *, int /* want_type */);
320 __LA_DECL int archive_entry_acl_next(struct archive_entry *, int /* want_type */,
321 int * /* type */, int * /* permset */, int * /* tag */,
322 int * /* qual */, const char ** /* name */);
323 __LA_DECL int archive_entry_acl_next_w(struct archive_entry *, int /* want_type */,
324 int * /* type */, int * /* permset */, int * /* tag */,
325 int * /* qual */, const wchar_t ** /* name */);
328 * Construct a text-format ACL. The flags argument is a bitmask that
329 * can include any of the following:
331 * ARCHIVE_ENTRY_ACL_TYPE_ACCESS - Include access entries.
332 * ARCHIVE_ENTRY_ACL_TYPE_DEFAULT - Include default entries.
333 * ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID - Include extra numeric ID field in
334 * each ACL entry. (As used by 'star'.)
335 * ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT - Include "default:" before each
336 * default ACL entry.
338 #define ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID 1024
339 #define ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT 2048
340 __LA_DECL const wchar_t *archive_entry_acl_text_w(struct archive_entry *,
341 int /* flags */);
343 /* Return a count of entries matching 'want_type' */
344 __LA_DECL int archive_entry_acl_count(struct archive_entry *, int /* want_type */);
347 * Private ACL parser. This is private because it handles some
348 * very weird formats that clients should not be messing with.
349 * Clients should only deal with their platform-native formats.
350 * Because of the need to support many formats cleanly, new arguments
351 * are likely to get added on a regular basis. Clients who try to use
352 * this interface are likely to be surprised when it changes.
354 * You were warned!
356 * TODO: Move this declaration out of the public header and into
357 * a private header. Warnings above are silly.
359 __LA_DECL int __archive_entry_acl_parse_w(struct archive_entry *,
360 const wchar_t *, int /* type */);
363 * extended attributes
366 __LA_DECL void archive_entry_xattr_clear(struct archive_entry *);
367 __LA_DECL void archive_entry_xattr_add_entry(struct archive_entry *,
368 const char * /* name */, const void * /* value */,
369 size_t /* size */);
372 * To retrieve the xattr list, first "reset", then repeatedly ask for the
373 * "next" entry.
376 __LA_DECL int archive_entry_xattr_count(struct archive_entry *);
377 __LA_DECL int archive_entry_xattr_reset(struct archive_entry *);
378 __LA_DECL int archive_entry_xattr_next(struct archive_entry *,
379 const char ** /* name */, const void ** /* value */, size_t *);
382 * Utility to match up hardlinks.
384 * The 'struct archive_entry_linkresolver' is a cache of archive entries
385 * for files with multiple links. Here's how to use it:
386 * 1. Create a lookup object with archive_entry_linkresolver_new()
387 * 2. Tell it the archive format you're using.
388 * 3. Hand each archive_entry to archive_entry_linkify().
389 * That function will return 0, 1, or 2 entries that should
390 * be written.
391 * 4. Call archive_entry_linkify(resolver, NULL) until
392 * no more entries are returned.
393 * 5. Call archive_entry_link_resolver_free(resolver) to free resources.
395 * The entries returned have their hardlink and size fields updated
396 * appropriately. If an entry is passed in that does not refer to
397 * a file with multiple links, it is returned unchanged. The intention
398 * is that you should be able to simply filter all entries through
399 * this machine.
401 * To make things more efficient, be sure that each entry has a valid
402 * nlinks value. The hardlink cache uses this to track when all links
403 * have been found. If the nlinks value is zero, it will keep every
404 * name in the cache indefinitely, which can use a lot of memory.
406 * Note that archive_entry_size() is reset to zero if the file
407 * body should not be written to the archive. Pay attention!
409 __LA_DECL struct archive_entry_linkresolver;
412 * There are three different strategies for marking hardlinks.
413 * The descriptions below name them after the best-known
414 * formats that rely on each strategy:
416 * "Old cpio" is the simplest, it always returns any entry unmodified.
417 * As far as I know, only cpio formats use this. Old cpio archives
418 * store every link with the full body; the onus is on the dearchiver
419 * to detect and properly link the files as they are restored.
420 * "tar" is also pretty simple; it caches a copy the first time it sees
421 * any link. Subsequent appearances are modified to be hardlink
422 * references to the first one without any body. Used by all tar
423 * formats, although the newest tar formats permit the "old cpio" strategy
424 * as well. This strategy is very simple for the dearchiver,
425 * and reasonably straightforward for the archiver.
426 * "new cpio" is trickier. It stores the body only with the last
427 * occurrence. The complication is that we might not
428 * see every link to a particular file in a single session, so
429 * there's no easy way to know when we've seen the last occurrence.
430 * The solution here is to queue one link until we see the next.
431 * At the end of the session, you can enumerate any remaining
432 * entries by calling archive_entry_linkify(NULL) and store those
433 * bodies. If you have a file with three links l1, l2, and l3,
434 * you'll get the following behavior if you see all three links:
435 * linkify(l1) => NULL (the resolver stores l1 internally)
436 * linkify(l2) => l1 (resolver stores l2, you write l1)
437 * linkify(l3) => l2, l3 (all links seen, you can write both).
438 * If you only see l1 and l2, you'll get this behavior:
439 * linkify(l1) => NULL
440 * linkify(l2) => l1
441 * linkify(NULL) => l2 (at end, you retrieve remaining links)
442 * As the name suggests, this strategy is used by newer cpio variants.
443 * It's noticably more complex for the archiver, slightly more complex
444 * for the dearchiver than the tar strategy, but makes it straightforward
445 * to restore a file using any link by simply continuing to scan until
446 * you see a link that is stored with a body. In contrast, the tar
447 * strategy requires you to rescan the archive from the beginning to
448 * correctly extract an arbitrary link.
451 __LA_DECL struct archive_entry_linkresolver *archive_entry_linkresolver_new(void);
452 __LA_DECL void archive_entry_linkresolver_set_strategy(
453 struct archive_entry_linkresolver *, int /* format_code */);
454 __LA_DECL void archive_entry_linkresolver_free(struct archive_entry_linkresolver *);
455 __LA_DECL void archive_entry_linkify(struct archive_entry_linkresolver *,
456 struct archive_entry **, struct archive_entry **);
458 #ifdef __cplusplus
460 #endif
462 /* This is meaningless outside of this header. */
463 #undef __LA_DECL
465 #endif /* !ARCHIVE_ENTRY_H_INCLUDED */