Add our READMEs.
[dragonfly/vkernel-mp.git] / contrib / libarchive-2.1 / libarchive / archive_entry.c
blobc96b549b6d27720687b5b3110f14cf616bfd0ec4
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry.c,v 1.42 2007/04/14 02:37:22 kientzle Exp $");
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35 #ifdef MAJOR_IN_MKDEV
36 #include <sys/mkdev.h>
37 # if !defined makedev && (defined mkdev || defined _WIN32 || defined __WIN32__)
38 # define makedev mkdev
39 # endif
40 #else
41 #ifdef MAJOR_IN_SYSMACROS
42 #include <sys/sysmacros.h>
43 #endif
44 #endif
45 #ifdef HAVE_EXT2FS_EXT2_FS_H
46 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
47 #endif
48 #ifdef HAVE_LIMITS_H
49 #include <limits.h>
50 #endif
51 #ifdef HAVE_LINUX_FS_H
52 #include <linux/fs.h> /* for Linux file flags */
53 #endif
54 #ifdef HAVE_LINUX_EXT2_FS_H
55 #include <linux/ext2_fs.h> /* for Linux file flags */
56 #endif
57 #include <stddef.h>
58 #include <stdio.h>
59 #ifdef HAVE_STDLIB_H
60 #include <stdlib.h>
61 #endif
62 #ifdef HAVE_STRING_H
63 #include <string.h>
64 #endif
65 #ifdef HAVE_WCHAR_H
66 #include <wchar.h>
67 #endif
69 #ifndef HAVE_WCSCPY
70 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
72 wchar_t *dest = s1;
73 while ((*s1 = *s2) != L'\0')
74 ++s1, ++s2;
75 return dest;
77 #endif
78 #ifndef HAVE_WCSLEN
79 static size_t wcslen(const wchar_t *s)
81 const wchar_t *p = s;
82 while (*p != L'\0')
83 ++p;
84 return p - s;
86 #endif
87 #ifndef HAVE_WMEMCMP
88 /* Good enough for simple equality testing, but not for sorting. */
89 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
90 #endif
91 #ifndef HAVE_WMEMCPY
92 #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
93 #endif
95 #include "archive.h"
96 #include "archive_entry.h"
97 #include "archive_private.h"
99 #undef max
100 #define max(a, b) ((a)>(b)?(a):(b))
103 * Handle wide character (i.e., Unicode) and non-wide character
104 * strings transparently.
108 struct aes {
109 const char *aes_mbs;
110 char *aes_mbs_alloc;
111 const wchar_t *aes_wcs;
112 wchar_t *aes_wcs_alloc;
115 struct ae_acl {
116 struct ae_acl *next;
117 int type; /* E.g., access or default */
118 int tag; /* E.g., user/group/other/mask */
119 int permset; /* r/w/x bits */
120 int id; /* uid/gid for user/group */
121 struct aes name; /* uname/gname */
124 struct ae_xattr {
125 struct ae_xattr *next;
127 char *name;
128 void *value;
129 size_t size;
132 static void aes_clean(struct aes *);
133 static void aes_copy(struct aes *dest, struct aes *src);
134 static const char * aes_get_mbs(struct aes *);
135 static const wchar_t * aes_get_wcs(struct aes *);
136 static void aes_set_mbs(struct aes *, const char *mbs);
137 static void aes_copy_mbs(struct aes *, const char *mbs);
138 /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
139 static void aes_copy_wcs(struct aes *, const wchar_t *wcs);
140 static void aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
142 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
143 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
144 unsigned long *setp, unsigned long *clrp);
145 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
146 const wchar_t *wname, int perm, int id);
147 static void append_id_w(wchar_t **wp, int id);
149 static int acl_special(struct archive_entry *entry,
150 int type, int permset, int tag);
151 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
152 int type, int permset, int tag, int id);
153 static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
154 static void next_field_w(const wchar_t **wp, const wchar_t **start,
155 const wchar_t **end, wchar_t *sep);
156 static int prefix_w(const wchar_t *start, const wchar_t *end,
157 const wchar_t *test);
158 static void
159 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
160 int permset, int tag, int id, const wchar_t *name, size_t);
164 * Description of an archive entry.
166 * Basically, this is a "struct stat" with a few text fields added in.
168 * TODO: Add "comment", "charset", and possibly other entries
169 * that are supported by "pax interchange" format. However, GNU, ustar,
170 * cpio, and other variants don't support these features, so they're not an
171 * excruciatingly high priority right now.
173 * TODO: "pax interchange" format allows essentially arbitrary
174 * key/value attributes to be attached to any entry. Supporting
175 * such extensions may make this library useful for special
176 * applications (e.g., a package manager could attach special
177 * package-management attributes to each entry). There are tricky
178 * API issues involved, so this is not going to happen until
179 * there's a real demand for it.
181 * TODO: Design a good API for handling sparse files.
183 struct archive_entry {
185 * Note that ae_stat.st_mode & S_IFMT can be 0!
187 * This occurs when the actual file type of the object is not
188 * in the archive. For example, 'tar' archives store
189 * hardlinks without marking the type of the underlying
190 * object.
192 struct stat ae_stat;
195 * Use aes here so that we get transparent mbs<->wcs conversions.
197 struct aes ae_fflags_text; /* Text fflags per fflagstostr(3) */
198 unsigned long ae_fflags_set; /* Bitmap fflags */
199 unsigned long ae_fflags_clear;
200 struct aes ae_gname; /* Name of owning group */
201 struct aes ae_hardlink; /* Name of target for hardlink */
202 struct aes ae_pathname; /* Name of entry */
203 struct aes ae_symlink; /* symlink contents */
204 struct aes ae_uname; /* Name of owner */
206 struct ae_acl *acl_head;
207 struct ae_acl *acl_p;
208 int acl_state; /* See acl_next for details. */
209 wchar_t *acl_text_w;
211 struct ae_xattr *xattr_head;
212 struct ae_xattr *xattr_p;
215 static void
216 aes_clean(struct aes *aes)
218 if (aes->aes_mbs_alloc) {
219 free(aes->aes_mbs_alloc);
220 aes->aes_mbs_alloc = NULL;
222 if (aes->aes_wcs_alloc) {
223 free(aes->aes_wcs_alloc);
224 aes->aes_wcs_alloc = NULL;
226 memset(aes, 0, sizeof(*aes));
229 static void
230 aes_copy(struct aes *dest, struct aes *src)
232 *dest = *src;
233 if (src->aes_mbs != NULL) {
234 dest->aes_mbs_alloc = strdup(src->aes_mbs);
235 dest->aes_mbs = dest->aes_mbs_alloc;
236 if (dest->aes_mbs == NULL)
237 __archive_errx(1, "No memory for aes_copy()");
240 if (src->aes_wcs != NULL) {
241 dest->aes_wcs_alloc = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
242 * sizeof(wchar_t));
243 dest->aes_wcs = dest->aes_wcs_alloc;
244 if (dest->aes_wcs == NULL)
245 __archive_errx(1, "No memory for aes_copy()");
246 wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
250 static const char *
251 aes_get_mbs(struct aes *aes)
253 if (aes->aes_mbs == NULL && aes->aes_wcs == NULL)
254 return NULL;
255 if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
257 * XXX Need to estimate the number of byte in the
258 * multi-byte form. Assume that, on average, wcs
259 * chars encode to no more than 3 bytes. There must
260 * be a better way... XXX
262 size_t mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
264 aes->aes_mbs_alloc = (char *)malloc(mbs_length);
265 aes->aes_mbs = aes->aes_mbs_alloc;
266 if (aes->aes_mbs == NULL)
267 __archive_errx(1, "No memory for aes_get_mbs()");
268 wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
269 aes->aes_mbs_alloc[mbs_length - 1] = 0;
271 return (aes->aes_mbs);
274 static const wchar_t *
275 aes_get_wcs(struct aes *aes)
277 if (aes->aes_wcs == NULL && aes->aes_mbs == NULL)
278 return NULL;
279 if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
281 * No single byte will be more than one wide character,
282 * so this length estimate will always be big enough.
284 size_t wcs_length = strlen(aes->aes_mbs);
286 aes->aes_wcs_alloc
287 = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
288 aes->aes_wcs = aes->aes_wcs_alloc;
289 if (aes->aes_wcs == NULL)
290 __archive_errx(1, "No memory for aes_get_wcs()");
291 mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
292 aes->aes_wcs_alloc[wcs_length] = 0;
294 return (aes->aes_wcs);
297 static void
298 aes_set_mbs(struct aes *aes, const char *mbs)
300 if (aes->aes_mbs_alloc) {
301 free(aes->aes_mbs_alloc);
302 aes->aes_mbs_alloc = NULL;
304 if (aes->aes_wcs_alloc) {
305 free(aes->aes_wcs_alloc);
306 aes->aes_wcs_alloc = NULL;
308 aes->aes_mbs = mbs;
309 aes->aes_wcs = NULL;
312 static void
313 aes_copy_mbs(struct aes *aes, const char *mbs)
315 if (aes->aes_mbs_alloc) {
316 free(aes->aes_mbs_alloc);
317 aes->aes_mbs_alloc = NULL;
319 if (aes->aes_wcs_alloc) {
320 free(aes->aes_wcs_alloc);
321 aes->aes_wcs_alloc = NULL;
323 aes->aes_mbs_alloc = (char *)malloc((strlen(mbs) + 1) * sizeof(char));
324 if (aes->aes_mbs_alloc == NULL)
325 __archive_errx(1, "No memory for aes_copy_mbs()");
326 strcpy(aes->aes_mbs_alloc, mbs);
327 aes->aes_mbs = aes->aes_mbs_alloc;
328 aes->aes_wcs = NULL;
331 #if 0
332 static void
333 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
335 if (aes->aes_mbs_alloc) {
336 free(aes->aes_mbs_alloc);
337 aes->aes_mbs_alloc = NULL;
339 if (aes->aes_wcs_alloc) {
340 free(aes->aes_wcs_alloc);
341 aes->aes_wcs_alloc = NULL;
343 aes->aes_mbs = NULL;
344 aes->aes_wcs = wcs;
346 #endif
348 static void
349 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
351 aes_copy_wcs_len(aes, wcs, wcslen(wcs));
354 static void
355 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
357 if (aes->aes_mbs_alloc) {
358 free(aes->aes_mbs_alloc);
359 aes->aes_mbs_alloc = NULL;
361 if (aes->aes_wcs_alloc) {
362 free(aes->aes_wcs_alloc);
363 aes->aes_wcs_alloc = NULL;
365 aes->aes_mbs = NULL;
366 aes->aes_wcs_alloc = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
367 if (aes->aes_wcs_alloc == NULL)
368 __archive_errx(1, "No memory for aes_copy_wcs()");
369 wmemcpy(aes->aes_wcs_alloc, wcs, len);
370 aes->aes_wcs_alloc[len] = L'\0';
371 aes->aes_wcs = aes->aes_wcs_alloc;
374 struct archive_entry *
375 archive_entry_clear(struct archive_entry *entry)
377 aes_clean(&entry->ae_fflags_text);
378 aes_clean(&entry->ae_gname);
379 aes_clean(&entry->ae_hardlink);
380 aes_clean(&entry->ae_pathname);
381 aes_clean(&entry->ae_symlink);
382 aes_clean(&entry->ae_uname);
383 archive_entry_acl_clear(entry);
384 archive_entry_xattr_clear(entry);
385 memset(entry, 0, sizeof(*entry));
386 return entry;
389 struct archive_entry *
390 archive_entry_clone(struct archive_entry *entry)
392 struct archive_entry *entry2;
394 /* Allocate new structure and copy over all of the fields. */
395 entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
396 if (entry2 == NULL)
397 return (NULL);
398 memset(entry2, 0, sizeof(*entry2));
399 entry2->ae_stat = entry->ae_stat;
400 entry2->ae_fflags_set = entry->ae_fflags_set;
401 entry2->ae_fflags_clear = entry->ae_fflags_clear;
403 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
404 aes_copy(&entry2->ae_gname, &entry->ae_gname);
405 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
406 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
407 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
408 aes_copy(&entry2->ae_uname, &entry->ae_uname);
410 /* XXX TODO: Copy ACL data over as well. XXX */
411 /* XXX TODO: Copy xattr data over as well. XXX */
412 return (entry2);
415 void
416 archive_entry_free(struct archive_entry *entry)
418 archive_entry_clear(entry);
419 free(entry);
422 struct archive_entry *
423 archive_entry_new(void)
425 struct archive_entry *entry;
427 entry = (struct archive_entry *)malloc(sizeof(*entry));
428 if (entry == NULL)
429 return (NULL);
430 memset(entry, 0, sizeof(*entry));
431 return (entry);
435 * Functions for reading fields from an archive_entry.
438 time_t
439 archive_entry_atime(struct archive_entry *entry)
441 return (entry->ae_stat.st_atime);
444 long
445 archive_entry_atime_nsec(struct archive_entry *entry)
447 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
448 return (entry->ae_stat.st_atimespec.tv_nsec);
449 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
450 return (entry->ae_stat.st_atim.tv_nsec);
451 #else
452 (void)entry; /* UNUSED */
453 return (0);
454 #endif
457 time_t
458 archive_entry_ctime(struct archive_entry *entry)
460 return (entry->ae_stat.st_ctime);
463 long
464 archive_entry_ctime_nsec(struct archive_entry *entry)
466 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
467 return (entry->ae_stat.st_ctimespec.tv_nsec);
468 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
469 return (entry->ae_stat.st_ctim.tv_nsec);
470 #else
471 (void)entry; /* UNUSED */
472 return (0);
473 #endif
476 dev_t
477 archive_entry_dev(struct archive_entry *entry)
479 return (entry->ae_stat.st_dev);
482 mode_t
483 archive_entry_filetype(struct archive_entry *entry)
485 return (S_IFMT & entry->ae_stat.st_mode);
488 void
489 archive_entry_fflags(struct archive_entry *entry,
490 unsigned long *set, unsigned long *clear)
492 *set = entry->ae_fflags_set;
493 *clear = entry->ae_fflags_clear;
497 * Note: if text was provided, this just returns that text. If you
498 * really need the text to be rebuilt in a canonical form, set the
499 * text, ask for the bitmaps, then set the bitmaps. (Setting the
500 * bitmaps clears any stored text.) This design is deliberate: if
501 * we're editing archives, we don't want to discard flags just because
502 * they aren't supported on the current system. The bitmap<->text
503 * conversions are platform-specific (see below).
505 const char *
506 archive_entry_fflags_text(struct archive_entry *entry)
508 const char *f;
509 char *p;
511 f = aes_get_mbs(&entry->ae_fflags_text);
512 if (f != NULL)
513 return (f);
515 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
516 return (NULL);
518 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
519 if (p == NULL)
520 return (NULL);
522 aes_copy_mbs(&entry->ae_fflags_text, p);
523 free(p);
524 f = aes_get_mbs(&entry->ae_fflags_text);
525 return (f);
528 gid_t
529 archive_entry_gid(struct archive_entry *entry)
531 return (entry->ae_stat.st_gid);
534 const char *
535 archive_entry_gname(struct archive_entry *entry)
537 return (aes_get_mbs(&entry->ae_gname));
540 const wchar_t *
541 archive_entry_gname_w(struct archive_entry *entry)
543 return (aes_get_wcs(&entry->ae_gname));
546 const char *
547 archive_entry_hardlink(struct archive_entry *entry)
549 return (aes_get_mbs(&entry->ae_hardlink));
552 const wchar_t *
553 archive_entry_hardlink_w(struct archive_entry *entry)
555 return (aes_get_wcs(&entry->ae_hardlink));
558 ino_t
559 archive_entry_ino(struct archive_entry *entry)
561 return (entry->ae_stat.st_ino);
564 mode_t
565 archive_entry_mode(struct archive_entry *entry)
567 return (entry->ae_stat.st_mode);
570 time_t
571 archive_entry_mtime(struct archive_entry *entry)
573 return (entry->ae_stat.st_mtime);
576 long
577 archive_entry_mtime_nsec(struct archive_entry *entry)
579 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
580 return (entry->ae_stat.st_mtimespec.tv_nsec);
581 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
582 return (entry->ae_stat.st_mtim.tv_nsec);
583 #else
584 (void)entry; /* UNUSED */
585 return (0);
586 #endif
589 unsigned int
590 archive_entry_nlink(struct archive_entry *entry)
592 return (entry->ae_stat.st_nlink);
595 const char *
596 archive_entry_pathname(struct archive_entry *entry)
598 return (aes_get_mbs(&entry->ae_pathname));
601 const wchar_t *
602 archive_entry_pathname_w(struct archive_entry *entry)
604 return (aes_get_wcs(&entry->ae_pathname));
607 dev_t
608 archive_entry_rdev(struct archive_entry *entry)
610 return (entry->ae_stat.st_rdev);
613 dev_t
614 archive_entry_rdevmajor(struct archive_entry *entry)
616 return (major(entry->ae_stat.st_rdev));
619 dev_t
620 archive_entry_rdevminor(struct archive_entry *entry)
622 return (minor(entry->ae_stat.st_rdev));
625 int64_t
626 archive_entry_size(struct archive_entry *entry)
628 return (entry->ae_stat.st_size);
631 const struct stat *
632 archive_entry_stat(struct archive_entry *entry)
634 return (&entry->ae_stat);
637 const char *
638 archive_entry_symlink(struct archive_entry *entry)
640 return (aes_get_mbs(&entry->ae_symlink));
643 const wchar_t *
644 archive_entry_symlink_w(struct archive_entry *entry)
646 return (aes_get_wcs(&entry->ae_symlink));
649 uid_t
650 archive_entry_uid(struct archive_entry *entry)
652 return (entry->ae_stat.st_uid);
655 const char *
656 archive_entry_uname(struct archive_entry *entry)
658 return (aes_get_mbs(&entry->ae_uname));
661 const wchar_t *
662 archive_entry_uname_w(struct archive_entry *entry)
664 return (aes_get_wcs(&entry->ae_uname));
668 * Functions to set archive_entry properties.
672 * Note "copy" not "set" here. The "set" functions that accept a pointer
673 * only store the pointer; they don't copy the underlying object.
675 void
676 archive_entry_copy_stat(struct archive_entry *entry, const struct stat *st)
678 entry->ae_stat = *st;
681 void
682 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
684 entry->ae_stat.st_mode &= ~AE_IFMT;
685 entry->ae_stat.st_mode |= AE_IFMT & type;
688 void
689 archive_entry_set_fflags(struct archive_entry *entry,
690 unsigned long set, unsigned long clear)
692 aes_clean(&entry->ae_fflags_text);
693 entry->ae_fflags_set = set;
694 entry->ae_fflags_clear = clear;
697 const wchar_t *
698 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
699 const wchar_t *flags)
701 aes_copy_wcs(&entry->ae_fflags_text, flags);
702 return (ae_wcstofflags(flags,
703 &entry->ae_fflags_set, &entry->ae_fflags_clear));
706 void
707 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
709 entry->ae_stat.st_gid = g;
712 void
713 archive_entry_set_gname(struct archive_entry *entry, const char *name)
715 aes_set_mbs(&entry->ae_gname, name);
718 void
719 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
721 aes_copy_wcs(&entry->ae_gname, name);
724 void
725 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
727 entry->ae_stat.st_ino = ino;
730 void
731 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
733 aes_set_mbs(&entry->ae_hardlink, target);
736 void
737 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
739 aes_copy_mbs(&entry->ae_hardlink, target);
742 void
743 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
745 aes_copy_wcs(&entry->ae_hardlink, target);
748 void
749 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
751 entry->ae_stat.st_atime = t;
752 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
753 entry->ae_stat.st_atimespec.tv_nsec = ns;
754 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
755 entry->ae_stat.st_atim.tv_nsec = ns;
756 #else
757 (void)ns; /* UNUSED */
758 #endif
761 void
762 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
764 entry->ae_stat.st_ctime = t;
765 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
766 entry->ae_stat.st_ctimespec.tv_nsec = ns;
767 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
768 entry->ae_stat.st_ctim.tv_nsec = ns;
769 #else
770 (void)ns; /* UNUSED */
771 #endif
774 void
775 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
777 entry->ae_stat.st_dev = d;
780 void
781 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
783 dev_t d;
785 d = entry->ae_stat.st_dev;
786 entry->ae_stat.st_dev = makedev(major(m), minor(d));
789 void
790 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
792 dev_t d;
794 d = entry->ae_stat.st_dev;
795 entry->ae_stat.st_dev = makedev(major(d), minor(m));
798 /* Set symlink if symlink is already set, else set hardlink. */
799 void
800 archive_entry_set_link(struct archive_entry *entry, const char *target)
802 if (entry->ae_symlink.aes_mbs != NULL ||
803 entry->ae_symlink.aes_wcs != NULL)
804 aes_set_mbs(&entry->ae_symlink, target);
805 else
806 aes_set_mbs(&entry->ae_hardlink, target);
809 void
810 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
812 entry->ae_stat.st_mode = m;
815 void
816 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
818 entry->ae_stat.st_mtime = m;
819 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
820 entry->ae_stat.st_mtimespec.tv_nsec = ns;
821 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
822 entry->ae_stat.st_mtim.tv_nsec = ns;
823 #else
824 (void)ns; /* UNUSED */
825 #endif
828 void
829 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
831 entry->ae_stat.st_nlink = nlink;
834 void
835 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
837 aes_set_mbs(&entry->ae_pathname, name);
840 void
841 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
843 aes_copy_mbs(&entry->ae_pathname, name);
846 void
847 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
849 aes_copy_wcs(&entry->ae_pathname, name);
852 void
853 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
855 entry->ae_stat.st_rdev = m;
858 void
859 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
861 dev_t d;
863 d = entry->ae_stat.st_rdev;
864 entry->ae_stat.st_rdev = makedev(major(m), minor(d));
867 void
868 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
870 dev_t d;
872 d = entry->ae_stat.st_rdev;
873 entry->ae_stat.st_rdev = makedev(major(d), minor(m));
876 void
877 archive_entry_set_size(struct archive_entry *entry, int64_t s)
879 entry->ae_stat.st_size = s;
882 void
883 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
885 aes_set_mbs(&entry->ae_symlink, linkname);
888 void
889 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
891 aes_copy_mbs(&entry->ae_symlink, linkname);
894 void
895 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
897 aes_copy_wcs(&entry->ae_symlink, linkname);
900 void
901 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
903 entry->ae_stat.st_uid = u;
906 void
907 archive_entry_set_uname(struct archive_entry *entry, const char *name)
909 aes_set_mbs(&entry->ae_uname, name);
912 void
913 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
915 aes_copy_wcs(&entry->ae_uname, name);
919 * ACL management. The following would, of course, be a lot simpler
920 * if: 1) the last draft of POSIX.1e were a really thorough and
921 * complete standard that addressed the needs of ACL archiving and 2)
922 * everyone followed it faithfully. Alas, neither is true, so the
923 * following is a lot more complex than might seem necessary to the
924 * uninitiated.
927 void
928 archive_entry_acl_clear(struct archive_entry *entry)
930 struct ae_acl *ap;
932 while (entry->acl_head != NULL) {
933 ap = entry->acl_head->next;
934 aes_clean(&entry->acl_head->name);
935 free(entry->acl_head);
936 entry->acl_head = ap;
938 if (entry->acl_text_w != NULL) {
939 free(entry->acl_text_w);
940 entry->acl_text_w = NULL;
942 entry->acl_p = NULL;
943 entry->acl_state = 0; /* Not counting. */
947 * Add a single ACL entry to the internal list of ACL data.
949 void
950 archive_entry_acl_add_entry(struct archive_entry *entry,
951 int type, int permset, int tag, int id, const char *name)
953 struct ae_acl *ap;
955 if (acl_special(entry, type, permset, tag) == 0)
956 return;
957 ap = acl_new_entry(entry, type, permset, tag, id);
958 if (ap == NULL) {
959 /* XXX Error XXX */
960 return;
962 if (name != NULL && *name != '\0')
963 aes_copy_mbs(&ap->name, name);
964 else
965 aes_clean(&ap->name);
969 * As above, but with a wide-character name.
971 void
972 archive_entry_acl_add_entry_w(struct archive_entry *entry,
973 int type, int permset, int tag, int id, const wchar_t *name)
975 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
978 void
979 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
980 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
982 struct ae_acl *ap;
984 if (acl_special(entry, type, permset, tag) == 0)
985 return;
986 ap = acl_new_entry(entry, type, permset, tag, id);
987 if (ap == NULL) {
988 /* XXX Error XXX */
989 return;
991 if (name != NULL && *name != L'\0' && len > 0)
992 aes_copy_wcs_len(&ap->name, name, len);
993 else
994 aes_clean(&ap->name);
998 * If this ACL entry is part of the standard POSIX permissions set,
999 * store the permissions in the stat structure and return zero.
1001 static int
1002 acl_special(struct archive_entry *entry, int type, int permset, int tag)
1004 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
1005 switch (tag) {
1006 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1007 entry->ae_stat.st_mode &= ~0700;
1008 entry->ae_stat.st_mode |= (permset & 7) << 6;
1009 return (0);
1010 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1011 entry->ae_stat.st_mode &= ~0070;
1012 entry->ae_stat.st_mode |= (permset & 7) << 3;
1013 return (0);
1014 case ARCHIVE_ENTRY_ACL_OTHER:
1015 entry->ae_stat.st_mode &= ~0007;
1016 entry->ae_stat.st_mode |= permset & 7;
1017 return (0);
1020 return (1);
1024 * Allocate and populate a new ACL entry with everything but the
1025 * name.
1027 static struct ae_acl *
1028 acl_new_entry(struct archive_entry *entry,
1029 int type, int permset, int tag, int id)
1031 struct ae_acl *ap;
1033 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
1034 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
1035 return (NULL);
1036 if (entry->acl_text_w != NULL) {
1037 free(entry->acl_text_w);
1038 entry->acl_text_w = NULL;
1041 /* XXX TODO: More sanity-checks on the arguments XXX */
1043 /* If there's a matching entry already in the list, overwrite it. */
1044 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
1045 if (ap->type == type && ap->tag == tag && ap->id == id) {
1046 ap->permset = permset;
1047 return (ap);
1051 /* Add a new entry to the list. */
1052 ap = (struct ae_acl *)malloc(sizeof(*ap));
1053 if (ap == NULL)
1054 return (NULL);
1055 memset(ap, 0, sizeof(*ap));
1056 ap->next = entry->acl_head;
1057 entry->acl_head = ap;
1058 ap->type = type;
1059 ap->tag = tag;
1060 ap->id = id;
1061 ap->permset = permset;
1062 return (ap);
1066 * Return a count of entries matching "want_type".
1069 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1071 int count;
1072 struct ae_acl *ap;
1074 count = 0;
1075 ap = entry->acl_head;
1076 while (ap != NULL) {
1077 if ((ap->type & want_type) != 0)
1078 count++;
1079 ap = ap->next;
1082 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1083 count += 3;
1084 return (count);
1088 * Prepare for reading entries from the ACL data. Returns a count
1089 * of entries matching "want_type", or zero if there are no
1090 * non-extended ACL entries of that type.
1093 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1095 int count, cutoff;
1097 count = archive_entry_acl_count(entry, want_type);
1100 * If the only entries are the three standard ones,
1101 * then don't return any ACL data. (In this case,
1102 * client can just use chmod(2) to set permissions.)
1104 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1105 cutoff = 3;
1106 else
1107 cutoff = 0;
1109 if (count > cutoff)
1110 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1111 else
1112 entry->acl_state = 0;
1113 entry->acl_p = entry->acl_head;
1114 return (count);
1118 * Return the next ACL entry in the list. Fake entries for the
1119 * standard permissions and include them in the returned list.
1123 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1124 int *permset, int *tag, int *id, const char **name)
1126 *name = NULL;
1127 *id = -1;
1130 * The acl_state is either zero (no entries available), -1
1131 * (reading from list), or an entry type (retrieve that type
1132 * from ae_stat.st_mode).
1134 if (entry->acl_state == 0)
1135 return (ARCHIVE_WARN);
1137 /* The first three access entries are special. */
1138 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1139 switch (entry->acl_state) {
1140 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1141 *permset = (entry->ae_stat.st_mode >> 6) & 7;
1142 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1143 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1144 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1145 return (ARCHIVE_OK);
1146 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1147 *permset = (entry->ae_stat.st_mode >> 3) & 7;
1148 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1149 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1150 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1151 return (ARCHIVE_OK);
1152 case ARCHIVE_ENTRY_ACL_OTHER:
1153 *permset = entry->ae_stat.st_mode & 7;
1154 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1155 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1156 entry->acl_state = -1;
1157 entry->acl_p = entry->acl_head;
1158 return (ARCHIVE_OK);
1159 default:
1160 break;
1164 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1165 entry->acl_p = entry->acl_p->next;
1166 if (entry->acl_p == NULL) {
1167 entry->acl_state = 0;
1168 return (ARCHIVE_EOF); /* End of ACL entries. */
1170 *type = entry->acl_p->type;
1171 *permset = entry->acl_p->permset;
1172 *tag = entry->acl_p->tag;
1173 *id = entry->acl_p->id;
1174 *name = aes_get_mbs(&entry->acl_p->name);
1175 entry->acl_p = entry->acl_p->next;
1176 return (ARCHIVE_OK);
1180 * Generate a text version of the ACL. The flags parameter controls
1181 * the style of the generated ACL.
1183 const wchar_t *
1184 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1186 int count;
1187 int length;
1188 const wchar_t *wname;
1189 const wchar_t *prefix;
1190 wchar_t separator;
1191 struct ae_acl *ap;
1192 int id;
1193 wchar_t *wp;
1195 if (entry->acl_text_w != NULL) {
1196 free (entry->acl_text_w);
1197 entry->acl_text_w = NULL;
1200 separator = L',';
1201 count = 0;
1202 length = 0;
1203 ap = entry->acl_head;
1204 while (ap != NULL) {
1205 if ((ap->type & flags) != 0) {
1206 count++;
1207 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1208 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1209 length += 8; /* "default:" */
1210 length += 5; /* tag name */
1211 length += 1; /* colon */
1212 wname = aes_get_wcs(&ap->name);
1213 if (wname != NULL)
1214 length += wcslen(wname);
1215 else
1216 length += sizeof(uid_t) * 3 + 1;
1217 length ++; /* colon */
1218 length += 3; /* rwx */
1219 length += 1; /* colon */
1220 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1221 length ++; /* newline */
1223 ap = ap->next;
1226 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1227 length += 10; /* "user::rwx\n" */
1228 length += 11; /* "group::rwx\n" */
1229 length += 11; /* "other::rwx\n" */
1232 if (count == 0)
1233 return (NULL);
1235 /* Now, allocate the string and actually populate it. */
1236 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1237 if (wp == NULL)
1238 __archive_errx(1, "No memory to generate the text version of the ACL");
1239 count = 0;
1240 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1241 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1242 entry->ae_stat.st_mode & 0700, -1);
1243 *wp++ = ',';
1244 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1245 entry->ae_stat.st_mode & 0070, -1);
1246 *wp++ = ',';
1247 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1248 entry->ae_stat.st_mode & 0007, -1);
1249 count += 3;
1251 ap = entry->acl_head;
1252 while (ap != NULL) {
1253 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1254 wname = aes_get_wcs(&ap->name);
1255 *wp++ = separator;
1256 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1257 id = ap->id;
1258 else
1259 id = -1;
1260 append_entry_w(&wp, NULL, ap->tag, wname,
1261 ap->permset, id);
1262 count++;
1264 ap = ap->next;
1269 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1270 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1271 prefix = L"default:";
1272 else
1273 prefix = NULL;
1274 ap = entry->acl_head;
1275 count = 0;
1276 while (ap != NULL) {
1277 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1278 wname = aes_get_wcs(&ap->name);
1279 if (count > 0)
1280 *wp++ = separator;
1281 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1282 id = ap->id;
1283 else
1284 id = -1;
1285 append_entry_w(&wp, prefix, ap->tag,
1286 wname, ap->permset, id);
1287 count ++;
1289 ap = ap->next;
1293 return (entry->acl_text_w);
1296 static void
1297 append_id_w(wchar_t **wp, int id)
1299 if (id < 0)
1300 id = 0;
1301 if (id > 9)
1302 append_id_w(wp, id / 10);
1303 *(*wp)++ = L"0123456789"[id % 10];
1306 static void
1307 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1308 const wchar_t *wname, int perm, int id)
1310 if (prefix != NULL) {
1311 wcscpy(*wp, prefix);
1312 *wp += wcslen(*wp);
1314 switch (tag) {
1315 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1316 wname = NULL;
1317 id = -1;
1318 /* FALLTHROUGH */
1319 case ARCHIVE_ENTRY_ACL_USER:
1320 wcscpy(*wp, L"user");
1321 break;
1322 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1323 wname = NULL;
1324 id = -1;
1325 /* FALLTHROUGH */
1326 case ARCHIVE_ENTRY_ACL_GROUP:
1327 wcscpy(*wp, L"group");
1328 break;
1329 case ARCHIVE_ENTRY_ACL_MASK:
1330 wcscpy(*wp, L"mask");
1331 wname = NULL;
1332 id = -1;
1333 break;
1334 case ARCHIVE_ENTRY_ACL_OTHER:
1335 wcscpy(*wp, L"other");
1336 wname = NULL;
1337 id = -1;
1338 break;
1340 *wp += wcslen(*wp);
1341 *(*wp)++ = L':';
1342 if (wname != NULL) {
1343 wcscpy(*wp, wname);
1344 *wp += wcslen(*wp);
1345 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1346 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1347 append_id_w(wp, id);
1348 id = -1;
1350 *(*wp)++ = L':';
1351 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1352 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1353 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1354 if (id != -1) {
1355 *(*wp)++ = L':';
1356 append_id_w(wp, id);
1358 **wp = L'\0';
1362 * Parse a textual ACL. This automatically recognizes and supports
1363 * extensions described above. The 'type' argument is used to
1364 * indicate the type that should be used for any entries not
1365 * explicitly marked as "default:".
1368 __archive_entry_acl_parse_w(struct archive_entry *entry,
1369 const wchar_t *text, int default_type)
1371 struct {
1372 const wchar_t *start;
1373 const wchar_t *end;
1374 } field[4];
1376 int fields;
1377 int type, tag, permset, id;
1378 const wchar_t *p;
1379 wchar_t sep;
1381 while (text != NULL && *text != L'\0') {
1383 * Parse the fields out of the next entry,
1384 * advance 'text' to start of next entry.
1386 fields = 0;
1387 do {
1388 const wchar_t *start, *end;
1389 next_field_w(&text, &start, &end, &sep);
1390 if (fields < 4) {
1391 field[fields].start = start;
1392 field[fields].end = end;
1394 ++fields;
1395 } while (sep == L':');
1397 if (fields < 3)
1398 return (ARCHIVE_WARN);
1400 /* Check for a numeric ID in field 1 or 3. */
1401 id = -1;
1402 isint_w(field[1].start, field[1].end, &id);
1403 /* Field 3 is optional. */
1404 if (id == -1 && fields > 3)
1405 isint_w(field[3].start, field[3].end, &id);
1407 /* Parse the permissions from field 2. */
1408 permset = 0;
1409 p = field[2].start;
1410 while (p < field[2].end) {
1411 switch (*p++) {
1412 case 'r': case 'R':
1413 permset |= ARCHIVE_ENTRY_ACL_READ;
1414 break;
1415 case 'w': case 'W':
1416 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1417 break;
1418 case 'x': case 'X':
1419 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1420 break;
1421 case '-':
1422 break;
1423 default:
1424 return (ARCHIVE_WARN);
1429 * Solaris extension: "defaultuser::rwx" is the
1430 * default ACL corresponding to "user::rwx", etc.
1432 if (field[0].end-field[0].start > 7
1433 && wmemcmp(field[0].start, L"default", 7) == 0) {
1434 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1435 field[0].start += 7;
1436 } else
1437 type = default_type;
1439 if (prefix_w(field[0].start, field[0].end, L"user")) {
1440 if (id != -1 || field[1].start < field[1].end)
1441 tag = ARCHIVE_ENTRY_ACL_USER;
1442 else
1443 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1444 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1445 if (id != -1 || field[1].start < field[1].end)
1446 tag = ARCHIVE_ENTRY_ACL_GROUP;
1447 else
1448 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1449 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1450 if (id != -1 || field[1].start < field[1].end)
1451 return (ARCHIVE_WARN);
1452 tag = ARCHIVE_ENTRY_ACL_OTHER;
1453 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1454 if (id != -1 || field[1].start < field[1].end)
1455 return (ARCHIVE_WARN);
1456 tag = ARCHIVE_ENTRY_ACL_MASK;
1457 } else
1458 return (ARCHIVE_WARN);
1460 /* Add entry to the internal list. */
1461 archive_entry_acl_add_entry_w_len(entry, type, permset,
1462 tag, id, field[1].start, field[1].end - field[1].start);
1464 return (ARCHIVE_OK);
1468 * extended attribute handling
1471 void
1472 archive_entry_xattr_clear(struct archive_entry *entry)
1474 struct ae_xattr *xp;
1476 while (entry->xattr_head != NULL) {
1477 xp = entry->xattr_head->next;
1478 free(entry->xattr_head->name);
1479 free(entry->xattr_head->value);
1480 free(entry->xattr_head);
1481 entry->xattr_head = xp;
1484 entry->xattr_head = NULL;
1487 void
1488 archive_entry_xattr_add_entry(struct archive_entry *entry,
1489 const char *name, const void *value, size_t size)
1491 struct ae_xattr *xp;
1493 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1496 if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1497 /* XXX Error XXX */
1498 return;
1500 xp->name = strdup(name);
1501 if ((xp->value = malloc(size)) != NULL) {
1502 memcpy(xp->value, value, size);
1503 xp->size = size;
1504 } else
1505 xp->size = 0;
1507 xp->next = entry->xattr_head;
1508 entry->xattr_head = xp;
1513 * returns number of the extended attribute entries
1516 archive_entry_xattr_count(struct archive_entry *entry)
1518 struct ae_xattr *xp;
1519 int count = 0;
1521 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1522 count++;
1524 return count;
1528 archive_entry_xattr_reset(struct archive_entry * entry)
1530 entry->xattr_p = entry->xattr_head;
1532 return archive_entry_xattr_count(entry);
1536 archive_entry_xattr_next(struct archive_entry * entry,
1537 const char **name, const void **value, size_t *size)
1539 if (entry->xattr_p) {
1540 *name = entry->xattr_p->name;
1541 *value = entry->xattr_p->value;
1542 *size = entry->xattr_p->size;
1544 entry->xattr_p = entry->xattr_p->next;
1546 return (ARCHIVE_OK);
1547 } else {
1548 *name = NULL;
1549 *name = NULL;
1550 *size = (size_t)0;
1551 return (ARCHIVE_WARN);
1556 * end of xattr handling
1560 * Parse a string to a positive decimal integer. Returns true if
1561 * the string is non-empty and consists only of decimal digits,
1562 * false otherwise.
1564 static int
1565 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1567 int n = 0;
1568 if (start >= end)
1569 return (0);
1570 while (start < end) {
1571 if (*start < '0' || *start > '9')
1572 return (0);
1573 if (n > (INT_MAX / 10))
1574 n = INT_MAX;
1575 else {
1576 n *= 10;
1577 n += *start - '0';
1579 start++;
1581 *result = n;
1582 return (1);
1586 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1587 * to point to just after the separator. *start points to the first
1588 * character of the matched text and *end just after the last
1589 * character of the matched identifier. In particular *end - *start
1590 * is the length of the field body, not including leading or trailing
1591 * whitespace.
1593 static void
1594 next_field_w(const wchar_t **wp, const wchar_t **start,
1595 const wchar_t **end, wchar_t *sep)
1597 /* Skip leading whitespace to find start of field. */
1598 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1599 (*wp)++;
1601 *start = *wp;
1603 /* Scan for the separator. */
1604 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1605 **wp != L'\n') {
1606 (*wp)++;
1608 *sep = **wp;
1610 /* Trim trailing whitespace to locate end of field. */
1611 *end = *wp - 1;
1612 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1613 (*end)--;
1615 (*end)++;
1617 /* Adjust scanner location. */
1618 if (**wp != L'\0')
1619 (*wp)++;
1623 * Return true if the characters [start...end) are a prefix of 'test'.
1624 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1626 static int
1627 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1629 if (start == end)
1630 return (0);
1632 if (*start++ != *test++)
1633 return (0);
1635 while (start < end && *start++ == *test++)
1638 if (start < end)
1639 return (0);
1641 return (1);
1646 * Following code is modified from UC Berkeley sources, and
1647 * is subject to the following copyright notice.
1651 * Copyright (c) 1993
1652 * The Regents of the University of California. All rights reserved.
1654 * Redistribution and use in source and binary forms, with or without
1655 * modification, are permitted provided that the following conditions
1656 * are met:
1657 * 1. Redistributions of source code must retain the above copyright
1658 * notice, this list of conditions and the following disclaimer.
1659 * 2. Redistributions in binary form must reproduce the above copyright
1660 * notice, this list of conditions and the following disclaimer in the
1661 * documentation and/or other materials provided with the distribution.
1662 * 4. Neither the name of the University nor the names of its contributors
1663 * may be used to endorse or promote products derived from this software
1664 * without specific prior written permission.
1666 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1667 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1668 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1669 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1670 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1671 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1672 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1673 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1674 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1675 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1676 * SUCH DAMAGE.
1679 static struct flag {
1680 const char *name;
1681 const wchar_t *wname;
1682 unsigned long set;
1683 unsigned long clear;
1684 } flags[] = {
1685 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1686 #ifdef SF_APPEND
1687 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1688 { "nosappend", L"nosappend", SF_APPEND, 0 },
1689 #endif
1690 #ifdef EXT2_APPEND_FL /* 'a' */
1691 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1692 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1693 #endif
1694 #ifdef SF_ARCHIVED
1695 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1696 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1697 #endif
1698 #ifdef SF_IMMUTABLE
1699 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1700 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1701 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1702 #endif
1703 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1704 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1705 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1706 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1707 #endif
1708 #ifdef SF_NOUNLINK
1709 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1710 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1711 #endif
1712 #ifdef SF_SNAPSHOT
1713 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1714 #endif
1715 #ifdef UF_APPEND
1716 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1717 { "nouappend", L"nouappend", UF_APPEND, 0 },
1718 #endif
1719 #ifdef UF_IMMUTABLE
1720 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1721 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1722 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1723 #endif
1724 #ifdef UF_NODUMP
1725 { "nodump", L"nodump", 0, UF_NODUMP},
1726 #endif
1727 #ifdef EXT2_NODUMP_FL /* 'd' */
1728 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1729 #endif
1730 #ifdef UF_OPAQUE
1731 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1732 #endif
1733 #ifdef UF_NOUNLINK
1734 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1735 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1736 #endif
1737 #ifdef EXT2_COMPR_FL /* 'c' */
1738 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1739 #endif
1741 #ifdef EXT2_NOATIME_FL /* 'A' */
1742 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1743 #endif
1744 { NULL, NULL, 0, 0 }
1748 * fflagstostr --
1749 * Convert file flags to a comma-separated string. If no flags
1750 * are set, return the empty string.
1752 char *
1753 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1755 char *string, *dp;
1756 const char *sp;
1757 unsigned long bits;
1758 struct flag *flag;
1759 size_t length;
1761 bits = bitset | bitclear;
1762 length = 0;
1763 for (flag = flags; flag->name != NULL; flag++)
1764 if (bits & (flag->set | flag->clear)) {
1765 length += strlen(flag->name) + 1;
1766 bits &= ~(flag->set | flag->clear);
1769 if (length == 0)
1770 return (NULL);
1771 string = (char *)malloc(length);
1772 if (string == NULL)
1773 return (NULL);
1775 dp = string;
1776 for (flag = flags; flag->name != NULL; flag++) {
1777 if (bitset & flag->set || bitclear & flag->clear) {
1778 sp = flag->name + 2;
1779 } else if (bitset & flag->clear || bitclear & flag->set) {
1780 sp = flag->name;
1781 } else
1782 continue;
1783 bitset &= ~(flag->set | flag->clear);
1784 bitclear &= ~(flag->set | flag->clear);
1785 if (dp > string)
1786 *dp++ = ',';
1787 while ((*dp++ = *sp++) != '\0')
1789 dp--;
1792 *dp = '\0';
1793 return (string);
1797 * wcstofflags --
1798 * Take string of arguments and return file flags. This
1799 * version works a little differently than strtofflags(3).
1800 * In particular, it always tests every token, skipping any
1801 * unrecognized tokens. It returns a pointer to the first
1802 * unrecognized token, or NULL if every token was recognized.
1803 * This version is also const-correct and does not modify the
1804 * provided string.
1806 const wchar_t *
1807 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1809 const wchar_t *start, *end;
1810 struct flag *flag;
1811 unsigned long set, clear;
1812 const wchar_t *failed;
1814 set = clear = 0;
1815 start = s;
1816 failed = NULL;
1817 /* Find start of first token. */
1818 while (*start == L'\t' || *start == L' ' || *start == L',')
1819 start++;
1820 while (*start != L'\0') {
1821 /* Locate end of token. */
1822 end = start;
1823 while (*end != L'\0' && *end != L'\t' &&
1824 *end != L' ' && *end != L',')
1825 end++;
1826 for (flag = flags; flag->wname != NULL; flag++) {
1827 if (wmemcmp(start, flag->wname, end - start) == 0) {
1828 /* Matched "noXXXX", so reverse the sense. */
1829 clear |= flag->set;
1830 set |= flag->clear;
1831 break;
1832 } else if (wmemcmp(start, flag->wname + 2, end - start)
1833 == 0) {
1834 /* Matched "XXXX", so don't reverse. */
1835 set |= flag->set;
1836 clear |= flag->clear;
1837 break;
1840 /* Ignore unknown flag names. */
1841 if (flag->wname == NULL && failed == NULL)
1842 failed = start;
1844 /* Find start of next token. */
1845 start = end;
1846 while (*start == L'\t' || *start == L' ' || *start == L',')
1847 start++;
1851 if (setp)
1852 *setp = set;
1853 if (clrp)
1854 *clrp = clear;
1856 /* Return location of first failure. */
1857 return (failed);
1861 #ifdef TEST
1862 #include <stdio.h>
1864 main(int argc, char **argv)
1866 struct archive_entry *entry = archive_entry_new();
1867 unsigned long set, clear;
1868 const wchar_t *remainder;
1870 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1871 archive_entry_fflags(entry, &set, &clear);
1873 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1875 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
1876 return (0);
1878 #endif