2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 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.44 2007/07/15 19:10:34 kientzle Exp $");
29 #ifdef HAVE_SYS_STAT_H
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
36 #include <sys/mkdev.h>
37 # if !defined makedev && (defined mkdev || defined _WIN32 || defined __WIN32__)
38 # define makedev mkdev
41 #ifdef MAJOR_IN_SYSMACROS
42 #include <sys/sysmacros.h>
45 #ifdef HAVE_EXT2FS_EXT2_FS_H
46 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
51 #ifdef HAVE_LINUX_FS_H
52 #include <linux/fs.h> /* for Linux file flags */
54 #ifdef HAVE_LINUX_EXT2_FS_H
55 #include <linux/ext2_fs.h> /* for Linux file flags */
70 #include "archive_entry.h"
71 #include "archive_private.h"
72 #include "archive_entry_private.h"
75 #define max(a, b) ((a)>(b)?(a):(b))
77 static void aes_clean(struct aes *);
78 static void aes_copy(struct aes *dest, struct aes *src);
79 static const char * aes_get_mbs(struct aes *);
80 static const wchar_t * aes_get_wcs(struct aes *);
81 static void aes_set_mbs(struct aes *, const char *mbs);
82 static void aes_copy_mbs(struct aes *, const char *mbs);
83 /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
84 static void aes_copy_wcs(struct aes *, const wchar_t *wcs);
85 static void aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
87 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
88 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
89 unsigned long *setp, unsigned long *clrp);
90 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
91 const wchar_t *wname, int perm, int id);
92 static void append_id_w(wchar_t **wp, int id);
94 static int acl_special(struct archive_entry *entry,
95 int type, int permset, int tag);
96 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
97 int type, int permset, int tag, int id);
98 static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
99 static void next_field_w(const wchar_t **wp, const wchar_t **start,
100 const wchar_t **end, wchar_t *sep);
101 static int prefix_w(const wchar_t *start, const wchar_t *end,
102 const wchar_t *test);
104 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
105 int permset, int tag, int id, const wchar_t *name, size_t);
109 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
112 while ((*s1 = *s2) != L'\0')
118 static size_t wcslen(const wchar_t *s)
120 const wchar_t *p = s;
127 /* Good enough for simple equality testing, but not for sorting. */
128 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
131 #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
136 aes_clean(struct aes *aes)
138 if (aes->aes_mbs_alloc) {
139 free(aes->aes_mbs_alloc);
140 aes->aes_mbs_alloc = NULL;
142 if (aes->aes_wcs_alloc) {
143 free(aes->aes_wcs_alloc);
144 aes->aes_wcs_alloc = NULL;
146 memset(aes, 0, sizeof(*aes));
150 aes_copy(struct aes *dest, struct aes *src)
153 if (src->aes_mbs != NULL) {
154 dest->aes_mbs_alloc = strdup(src->aes_mbs);
155 dest->aes_mbs = dest->aes_mbs_alloc;
156 if (dest->aes_mbs == NULL)
157 __archive_errx(1, "No memory for aes_copy()");
160 if (src->aes_wcs != NULL) {
161 dest->aes_wcs_alloc = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
163 dest->aes_wcs = dest->aes_wcs_alloc;
164 if (dest->aes_wcs == NULL)
165 __archive_errx(1, "No memory for aes_copy()");
166 wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
171 aes_get_mbs(struct aes *aes)
173 if (aes->aes_mbs == NULL && aes->aes_wcs == NULL)
175 if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
177 * XXX Need to estimate the number of byte in the
178 * multi-byte form. Assume that, on average, wcs
179 * chars encode to no more than 3 bytes. There must
180 * be a better way... XXX
182 size_t mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
184 aes->aes_mbs_alloc = (char *)malloc(mbs_length);
185 aes->aes_mbs = aes->aes_mbs_alloc;
186 if (aes->aes_mbs == NULL)
187 __archive_errx(1, "No memory for aes_get_mbs()");
188 wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
189 aes->aes_mbs_alloc[mbs_length - 1] = 0;
191 return (aes->aes_mbs);
194 static const wchar_t *
195 aes_get_wcs(struct aes *aes)
197 if (aes->aes_wcs == NULL && aes->aes_mbs == NULL)
199 if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
201 * No single byte will be more than one wide character,
202 * so this length estimate will always be big enough.
204 size_t wcs_length = strlen(aes->aes_mbs);
207 = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
208 aes->aes_wcs = aes->aes_wcs_alloc;
209 if (aes->aes_wcs == NULL)
210 __archive_errx(1, "No memory for aes_get_wcs()");
211 mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
212 aes->aes_wcs_alloc[wcs_length] = 0;
214 return (aes->aes_wcs);
218 aes_set_mbs(struct aes *aes, const char *mbs)
220 if (aes->aes_mbs_alloc) {
221 free(aes->aes_mbs_alloc);
222 aes->aes_mbs_alloc = NULL;
224 if (aes->aes_wcs_alloc) {
225 free(aes->aes_wcs_alloc);
226 aes->aes_wcs_alloc = NULL;
233 aes_copy_mbs(struct aes *aes, const char *mbs)
235 if (aes->aes_mbs_alloc) {
236 free(aes->aes_mbs_alloc);
237 aes->aes_mbs_alloc = NULL;
239 if (aes->aes_wcs_alloc) {
240 free(aes->aes_wcs_alloc);
241 aes->aes_wcs_alloc = NULL;
243 aes->aes_mbs_alloc = (char *)malloc((strlen(mbs) + 1) * sizeof(char));
244 if (aes->aes_mbs_alloc == NULL)
245 __archive_errx(1, "No memory for aes_copy_mbs()");
246 strcpy(aes->aes_mbs_alloc, mbs);
247 aes->aes_mbs = aes->aes_mbs_alloc;
253 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
255 if (aes->aes_mbs_alloc) {
256 free(aes->aes_mbs_alloc);
257 aes->aes_mbs_alloc = NULL;
259 if (aes->aes_wcs_alloc) {
260 free(aes->aes_wcs_alloc);
261 aes->aes_wcs_alloc = NULL;
269 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
271 aes_copy_wcs_len(aes, wcs, wcslen(wcs));
275 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
277 if (aes->aes_mbs_alloc) {
278 free(aes->aes_mbs_alloc);
279 aes->aes_mbs_alloc = NULL;
281 if (aes->aes_wcs_alloc) {
282 free(aes->aes_wcs_alloc);
283 aes->aes_wcs_alloc = NULL;
286 aes->aes_wcs_alloc = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
287 if (aes->aes_wcs_alloc == NULL)
288 __archive_errx(1, "No memory for aes_copy_wcs()");
289 wmemcpy(aes->aes_wcs_alloc, wcs, len);
290 aes->aes_wcs_alloc[len] = L'\0';
291 aes->aes_wcs = aes->aes_wcs_alloc;
294 struct archive_entry *
295 archive_entry_clear(struct archive_entry *entry)
297 aes_clean(&entry->ae_fflags_text);
298 aes_clean(&entry->ae_gname);
299 aes_clean(&entry->ae_hardlink);
300 aes_clean(&entry->ae_pathname);
301 aes_clean(&entry->ae_symlink);
302 aes_clean(&entry->ae_uname);
303 archive_entry_acl_clear(entry);
304 archive_entry_xattr_clear(entry);
306 memset(entry, 0, sizeof(*entry));
310 struct archive_entry *
311 archive_entry_clone(struct archive_entry *entry)
313 struct archive_entry *entry2;
314 struct ae_acl *ap, *ap2;
317 /* Allocate new structure and copy over all of the fields. */
318 entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
321 memset(entry2, 0, sizeof(*entry2));
322 entry2->ae_stat = entry->ae_stat;
323 entry2->ae_fflags_set = entry->ae_fflags_set;
324 entry2->ae_fflags_clear = entry->ae_fflags_clear;
326 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
327 aes_copy(&entry2->ae_gname, &entry->ae_gname);
328 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
329 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
330 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
331 aes_copy(&entry2->ae_uname, &entry->ae_uname);
333 /* Copy ACL data over. */
334 ap = entry->acl_head;
336 ap2 = acl_new_entry(entry2,
337 ap->type, ap->permset, ap->tag, ap->id);
339 aes_copy(&ap2->name, &ap->name);
343 /* Copy xattr data over. */
344 xp = entry->xattr_head;
346 archive_entry_xattr_add_entry(entry2,
347 xp->name, xp->value, xp->size);
355 archive_entry_free(struct archive_entry *entry)
357 archive_entry_clear(entry);
361 struct archive_entry *
362 archive_entry_new(void)
364 struct archive_entry *entry;
366 entry = (struct archive_entry *)malloc(sizeof(*entry));
369 memset(entry, 0, sizeof(*entry));
374 * Functions for reading fields from an archive_entry.
378 archive_entry_atime(struct archive_entry *entry)
380 return (entry->ae_stat.aest_atime);
384 archive_entry_atime_nsec(struct archive_entry *entry)
386 return (entry->ae_stat.aest_atime_nsec);
390 archive_entry_ctime(struct archive_entry *entry)
392 return (entry->ae_stat.aest_ctime);
396 archive_entry_ctime_nsec(struct archive_entry *entry)
398 return (entry->ae_stat.aest_ctime_nsec);
402 archive_entry_dev(struct archive_entry *entry)
404 if (entry->ae_stat.aest_dev_is_broken_down)
405 return makedev(entry->ae_stat.aest_devmajor,
406 entry->ae_stat.aest_devminor);
408 return (entry->ae_stat.aest_dev);
412 archive_entry_devmajor(struct archive_entry *entry)
414 if (entry->ae_stat.aest_dev_is_broken_down)
415 return (entry->ae_stat.aest_devmajor);
417 return major(entry->ae_stat.aest_dev);
421 archive_entry_devminor(struct archive_entry *entry)
423 if (entry->ae_stat.aest_dev_is_broken_down)
424 return (entry->ae_stat.aest_devminor);
426 return minor(entry->ae_stat.aest_dev);
430 archive_entry_filetype(struct archive_entry *entry)
432 return (AE_IFMT & entry->ae_stat.aest_mode);
436 archive_entry_fflags(struct archive_entry *entry,
437 unsigned long *set, unsigned long *clear)
439 *set = entry->ae_fflags_set;
440 *clear = entry->ae_fflags_clear;
444 * Note: if text was provided, this just returns that text. If you
445 * really need the text to be rebuilt in a canonical form, set the
446 * text, ask for the bitmaps, then set the bitmaps. (Setting the
447 * bitmaps clears any stored text.) This design is deliberate: if
448 * we're editing archives, we don't want to discard flags just because
449 * they aren't supported on the current system. The bitmap<->text
450 * conversions are platform-specific (see below).
453 archive_entry_fflags_text(struct archive_entry *entry)
458 f = aes_get_mbs(&entry->ae_fflags_text);
462 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
465 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
469 aes_copy_mbs(&entry->ae_fflags_text, p);
471 f = aes_get_mbs(&entry->ae_fflags_text);
476 archive_entry_gid(struct archive_entry *entry)
478 return (entry->ae_stat.aest_gid);
482 archive_entry_gname(struct archive_entry *entry)
484 return (aes_get_mbs(&entry->ae_gname));
488 archive_entry_gname_w(struct archive_entry *entry)
490 return (aes_get_wcs(&entry->ae_gname));
494 archive_entry_hardlink(struct archive_entry *entry)
496 return (aes_get_mbs(&entry->ae_hardlink));
500 archive_entry_hardlink_w(struct archive_entry *entry)
502 return (aes_get_wcs(&entry->ae_hardlink));
506 archive_entry_ino(struct archive_entry *entry)
508 return (entry->ae_stat.aest_ino);
512 archive_entry_mode(struct archive_entry *entry)
514 return (entry->ae_stat.aest_mode);
518 archive_entry_mtime(struct archive_entry *entry)
520 return (entry->ae_stat.aest_mtime);
524 archive_entry_mtime_nsec(struct archive_entry *entry)
526 return (entry->ae_stat.aest_mtime_nsec);
530 archive_entry_nlink(struct archive_entry *entry)
532 return (entry->ae_stat.aest_nlink);
536 archive_entry_pathname(struct archive_entry *entry)
538 return (aes_get_mbs(&entry->ae_pathname));
542 archive_entry_pathname_w(struct archive_entry *entry)
544 return (aes_get_wcs(&entry->ae_pathname));
548 archive_entry_rdev(struct archive_entry *entry)
550 if (entry->ae_stat.aest_rdev_is_broken_down)
551 return makedev(entry->ae_stat.aest_rdevmajor,
552 entry->ae_stat.aest_rdevminor);
554 return (entry->ae_stat.aest_rdev);
558 archive_entry_rdevmajor(struct archive_entry *entry)
560 if (entry->ae_stat.aest_rdev_is_broken_down)
561 return (entry->ae_stat.aest_rdevmajor);
563 return major(entry->ae_stat.aest_rdev);
567 archive_entry_rdevminor(struct archive_entry *entry)
569 if (entry->ae_stat.aest_rdev_is_broken_down)
570 return (entry->ae_stat.aest_rdevminor);
572 return minor(entry->ae_stat.aest_rdev);
576 archive_entry_size(struct archive_entry *entry)
578 return (entry->ae_stat.aest_size);
582 archive_entry_symlink(struct archive_entry *entry)
584 return (aes_get_mbs(&entry->ae_symlink));
588 archive_entry_symlink_w(struct archive_entry *entry)
590 return (aes_get_wcs(&entry->ae_symlink));
594 archive_entry_uid(struct archive_entry *entry)
596 return (entry->ae_stat.aest_uid);
600 archive_entry_uname(struct archive_entry *entry)
602 return (aes_get_mbs(&entry->ae_uname));
606 archive_entry_uname_w(struct archive_entry *entry)
608 return (aes_get_wcs(&entry->ae_uname));
612 * Functions to set archive_entry properties.
616 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
618 entry->stat_valid = 0;
619 entry->ae_stat.aest_mode &= ~AE_IFMT;
620 entry->ae_stat.aest_mode |= AE_IFMT & type;
624 archive_entry_set_fflags(struct archive_entry *entry,
625 unsigned long set, unsigned long clear)
627 aes_clean(&entry->ae_fflags_text);
628 entry->ae_fflags_set = set;
629 entry->ae_fflags_clear = clear;
633 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
634 const wchar_t *flags)
636 aes_copy_wcs(&entry->ae_fflags_text, flags);
637 return (ae_wcstofflags(flags,
638 &entry->ae_fflags_set, &entry->ae_fflags_clear));
642 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
644 entry->stat_valid = 0;
645 entry->ae_stat.aest_gid = g;
649 archive_entry_set_gname(struct archive_entry *entry, const char *name)
651 aes_set_mbs(&entry->ae_gname, name);
655 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
657 aes_copy_mbs(&entry->ae_gname, name);
661 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
663 aes_copy_wcs(&entry->ae_gname, name);
667 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
669 entry->stat_valid = 0;
670 entry->ae_stat.aest_ino = ino;
674 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
676 aes_set_mbs(&entry->ae_hardlink, target);
680 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
682 aes_copy_mbs(&entry->ae_hardlink, target);
686 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
688 aes_copy_wcs(&entry->ae_hardlink, target);
692 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
694 entry->stat_valid = 0;
695 entry->ae_stat.aest_atime = t;
696 entry->ae_stat.aest_atime_nsec = ns;
700 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
702 entry->stat_valid = 0;
703 entry->ae_stat.aest_ctime = t;
704 entry->ae_stat.aest_ctime_nsec = ns;
708 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
710 entry->stat_valid = 0;
711 entry->ae_stat.aest_dev_is_broken_down = 0;
712 entry->ae_stat.aest_dev = d;
716 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
718 entry->stat_valid = 0;
719 entry->ae_stat.aest_dev_is_broken_down = 1;
720 entry->ae_stat.aest_devmajor = m;
724 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
726 entry->stat_valid = 0;
727 entry->ae_stat.aest_dev_is_broken_down = 1;
728 entry->ae_stat.aest_devminor = m;
731 /* Set symlink if symlink is already set, else set hardlink. */
733 archive_entry_set_link(struct archive_entry *entry, const char *target)
735 if (entry->ae_symlink.aes_mbs != NULL ||
736 entry->ae_symlink.aes_wcs != NULL)
737 aes_set_mbs(&entry->ae_symlink, target);
739 aes_set_mbs(&entry->ae_hardlink, target);
743 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
745 entry->stat_valid = 0;
746 entry->ae_stat.aest_mode = m;
750 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
752 entry->stat_valid = 0;
753 entry->ae_stat.aest_mtime = m;
754 entry->ae_stat.aest_mtime_nsec = ns;
758 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
760 entry->stat_valid = 0;
761 entry->ae_stat.aest_nlink = nlink;
765 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
767 aes_set_mbs(&entry->ae_pathname, name);
771 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
773 aes_copy_mbs(&entry->ae_pathname, name);
777 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
779 aes_copy_wcs(&entry->ae_pathname, name);
783 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
785 entry->stat_valid = 0;
786 entry->ae_stat.aest_rdev = m;
787 entry->ae_stat.aest_rdev_is_broken_down = 0;
791 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
793 entry->stat_valid = 0;
794 entry->ae_stat.aest_rdev_is_broken_down = 1;
795 entry->ae_stat.aest_rdevmajor = m;
799 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
801 entry->stat_valid = 0;
802 entry->ae_stat.aest_rdev_is_broken_down = 1;
803 entry->ae_stat.aest_rdevminor = m;
807 archive_entry_set_size(struct archive_entry *entry, int64_t s)
809 entry->stat_valid = 0;
810 entry->ae_stat.aest_size = s;
814 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
816 aes_set_mbs(&entry->ae_symlink, linkname);
820 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
822 aes_copy_mbs(&entry->ae_symlink, linkname);
826 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
828 aes_copy_wcs(&entry->ae_symlink, linkname);
832 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
834 entry->stat_valid = 0;
835 entry->ae_stat.aest_uid = u;
839 archive_entry_set_uname(struct archive_entry *entry, const char *name)
841 aes_set_mbs(&entry->ae_uname, name);
845 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
847 aes_copy_mbs(&entry->ae_uname, name);
851 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
853 aes_copy_wcs(&entry->ae_uname, name);
857 * ACL management. The following would, of course, be a lot simpler
858 * if: 1) the last draft of POSIX.1e were a really thorough and
859 * complete standard that addressed the needs of ACL archiving and 2)
860 * everyone followed it faithfully. Alas, neither is true, so the
861 * following is a lot more complex than might seem necessary to the
866 archive_entry_acl_clear(struct archive_entry *entry)
870 while (entry->acl_head != NULL) {
871 ap = entry->acl_head->next;
872 aes_clean(&entry->acl_head->name);
873 free(entry->acl_head);
874 entry->acl_head = ap;
876 if (entry->acl_text_w != NULL) {
877 free(entry->acl_text_w);
878 entry->acl_text_w = NULL;
881 entry->acl_state = 0; /* Not counting. */
885 * Add a single ACL entry to the internal list of ACL data.
888 archive_entry_acl_add_entry(struct archive_entry *entry,
889 int type, int permset, int tag, int id, const char *name)
893 if (acl_special(entry, type, permset, tag) == 0)
895 ap = acl_new_entry(entry, type, permset, tag, id);
900 if (name != NULL && *name != '\0')
901 aes_copy_mbs(&ap->name, name);
903 aes_clean(&ap->name);
907 * As above, but with a wide-character name.
910 archive_entry_acl_add_entry_w(struct archive_entry *entry,
911 int type, int permset, int tag, int id, const wchar_t *name)
913 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
917 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
918 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
922 if (acl_special(entry, type, permset, tag) == 0)
924 ap = acl_new_entry(entry, type, permset, tag, id);
929 if (name != NULL && *name != L'\0' && len > 0)
930 aes_copy_wcs_len(&ap->name, name, len);
932 aes_clean(&ap->name);
936 * If this ACL entry is part of the standard POSIX permissions set,
937 * store the permissions in the stat structure and return zero.
940 acl_special(struct archive_entry *entry, int type, int permset, int tag)
942 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
944 case ARCHIVE_ENTRY_ACL_USER_OBJ:
945 entry->ae_stat.aest_mode &= ~0700;
946 entry->ae_stat.aest_mode |= (permset & 7) << 6;
948 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
949 entry->ae_stat.aest_mode &= ~0070;
950 entry->ae_stat.aest_mode |= (permset & 7) << 3;
952 case ARCHIVE_ENTRY_ACL_OTHER:
953 entry->ae_stat.aest_mode &= ~0007;
954 entry->ae_stat.aest_mode |= permset & 7;
962 * Allocate and populate a new ACL entry with everything but the
965 static struct ae_acl *
966 acl_new_entry(struct archive_entry *entry,
967 int type, int permset, int tag, int id)
971 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
972 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
974 if (entry->acl_text_w != NULL) {
975 free(entry->acl_text_w);
976 entry->acl_text_w = NULL;
979 /* XXX TODO: More sanity-checks on the arguments XXX */
981 /* If there's a matching entry already in the list, overwrite it. */
982 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
983 if (ap->type == type && ap->tag == tag && ap->id == id) {
984 ap->permset = permset;
989 /* Add a new entry to the list. */
990 ap = (struct ae_acl *)malloc(sizeof(*ap));
993 memset(ap, 0, sizeof(*ap));
994 ap->next = entry->acl_head;
995 entry->acl_head = ap;
999 ap->permset = permset;
1004 * Return a count of entries matching "want_type".
1007 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1013 ap = entry->acl_head;
1014 while (ap != NULL) {
1015 if ((ap->type & want_type) != 0)
1020 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1026 * Prepare for reading entries from the ACL data. Returns a count
1027 * of entries matching "want_type", or zero if there are no
1028 * non-extended ACL entries of that type.
1031 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1035 count = archive_entry_acl_count(entry, want_type);
1038 * If the only entries are the three standard ones,
1039 * then don't return any ACL data. (In this case,
1040 * client can just use chmod(2) to set permissions.)
1042 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1048 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1050 entry->acl_state = 0;
1051 entry->acl_p = entry->acl_head;
1056 * Return the next ACL entry in the list. Fake entries for the
1057 * standard permissions and include them in the returned list.
1061 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1062 int *permset, int *tag, int *id, const char **name)
1068 * The acl_state is either zero (no entries available), -1
1069 * (reading from list), or an entry type (retrieve that type
1070 * from ae_stat.aest_mode).
1072 if (entry->acl_state == 0)
1073 return (ARCHIVE_WARN);
1075 /* The first three access entries are special. */
1076 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1077 switch (entry->acl_state) {
1078 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1079 *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1080 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1081 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1082 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1083 return (ARCHIVE_OK);
1084 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1085 *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1086 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1087 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1088 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1089 return (ARCHIVE_OK);
1090 case ARCHIVE_ENTRY_ACL_OTHER:
1091 *permset = entry->ae_stat.aest_mode & 7;
1092 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1093 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1094 entry->acl_state = -1;
1095 entry->acl_p = entry->acl_head;
1096 return (ARCHIVE_OK);
1102 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1103 entry->acl_p = entry->acl_p->next;
1104 if (entry->acl_p == NULL) {
1105 entry->acl_state = 0;
1106 return (ARCHIVE_EOF); /* End of ACL entries. */
1108 *type = entry->acl_p->type;
1109 *permset = entry->acl_p->permset;
1110 *tag = entry->acl_p->tag;
1111 *id = entry->acl_p->id;
1112 *name = aes_get_mbs(&entry->acl_p->name);
1113 entry->acl_p = entry->acl_p->next;
1114 return (ARCHIVE_OK);
1118 * Generate a text version of the ACL. The flags parameter controls
1119 * the style of the generated ACL.
1122 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1126 const wchar_t *wname;
1127 const wchar_t *prefix;
1133 if (entry->acl_text_w != NULL) {
1134 free (entry->acl_text_w);
1135 entry->acl_text_w = NULL;
1141 ap = entry->acl_head;
1142 while (ap != NULL) {
1143 if ((ap->type & flags) != 0) {
1145 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1146 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1147 length += 8; /* "default:" */
1148 length += 5; /* tag name */
1149 length += 1; /* colon */
1150 wname = aes_get_wcs(&ap->name);
1152 length += wcslen(wname);
1154 length += sizeof(uid_t) * 3 + 1;
1155 length ++; /* colon */
1156 length += 3; /* rwx */
1157 length += 1; /* colon */
1158 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1159 length ++; /* newline */
1164 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1165 length += 10; /* "user::rwx\n" */
1166 length += 11; /* "group::rwx\n" */
1167 length += 11; /* "other::rwx\n" */
1173 /* Now, allocate the string and actually populate it. */
1174 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1176 __archive_errx(1, "No memory to generate the text version of the ACL");
1178 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1179 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1180 entry->ae_stat.aest_mode & 0700, -1);
1182 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1183 entry->ae_stat.aest_mode & 0070, -1);
1185 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1186 entry->ae_stat.aest_mode & 0007, -1);
1189 ap = entry->acl_head;
1190 while (ap != NULL) {
1191 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1192 wname = aes_get_wcs(&ap->name);
1194 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1198 append_entry_w(&wp, NULL, ap->tag, wname,
1207 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1208 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1209 prefix = L"default:";
1212 ap = entry->acl_head;
1214 while (ap != NULL) {
1215 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1216 wname = aes_get_wcs(&ap->name);
1219 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1223 append_entry_w(&wp, prefix, ap->tag,
1224 wname, ap->permset, id);
1231 return (entry->acl_text_w);
1235 append_id_w(wchar_t **wp, int id)
1240 append_id_w(wp, id / 10);
1241 *(*wp)++ = L"0123456789"[id % 10];
1245 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1246 const wchar_t *wname, int perm, int id)
1248 if (prefix != NULL) {
1249 wcscpy(*wp, prefix);
1253 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1257 case ARCHIVE_ENTRY_ACL_USER:
1258 wcscpy(*wp, L"user");
1260 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1264 case ARCHIVE_ENTRY_ACL_GROUP:
1265 wcscpy(*wp, L"group");
1267 case ARCHIVE_ENTRY_ACL_MASK:
1268 wcscpy(*wp, L"mask");
1272 case ARCHIVE_ENTRY_ACL_OTHER:
1273 wcscpy(*wp, L"other");
1280 if (wname != NULL) {
1283 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1284 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1285 append_id_w(wp, id);
1289 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1290 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1291 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1294 append_id_w(wp, id);
1300 * Parse a textual ACL. This automatically recognizes and supports
1301 * extensions described above. The 'type' argument is used to
1302 * indicate the type that should be used for any entries not
1303 * explicitly marked as "default:".
1306 __archive_entry_acl_parse_w(struct archive_entry *entry,
1307 const wchar_t *text, int default_type)
1310 const wchar_t *start;
1315 int type, tag, permset, id;
1319 while (text != NULL && *text != L'\0') {
1321 * Parse the fields out of the next entry,
1322 * advance 'text' to start of next entry.
1326 const wchar_t *start, *end;
1327 next_field_w(&text, &start, &end, &sep);
1329 field[fields].start = start;
1330 field[fields].end = end;
1333 } while (sep == L':');
1336 return (ARCHIVE_WARN);
1338 /* Check for a numeric ID in field 1 or 3. */
1340 isint_w(field[1].start, field[1].end, &id);
1341 /* Field 3 is optional. */
1342 if (id == -1 && fields > 3)
1343 isint_w(field[3].start, field[3].end, &id);
1345 /* Parse the permissions from field 2. */
1348 while (p < field[2].end) {
1351 permset |= ARCHIVE_ENTRY_ACL_READ;
1354 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1357 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1362 return (ARCHIVE_WARN);
1367 * Solaris extension: "defaultuser::rwx" is the
1368 * default ACL corresponding to "user::rwx", etc.
1370 if (field[0].end-field[0].start > 7
1371 && wmemcmp(field[0].start, L"default", 7) == 0) {
1372 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1373 field[0].start += 7;
1375 type = default_type;
1377 if (prefix_w(field[0].start, field[0].end, L"user")) {
1378 if (id != -1 || field[1].start < field[1].end)
1379 tag = ARCHIVE_ENTRY_ACL_USER;
1381 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1382 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1383 if (id != -1 || field[1].start < field[1].end)
1384 tag = ARCHIVE_ENTRY_ACL_GROUP;
1386 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1387 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1388 if (id != -1 || field[1].start < field[1].end)
1389 return (ARCHIVE_WARN);
1390 tag = ARCHIVE_ENTRY_ACL_OTHER;
1391 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1392 if (id != -1 || field[1].start < field[1].end)
1393 return (ARCHIVE_WARN);
1394 tag = ARCHIVE_ENTRY_ACL_MASK;
1396 return (ARCHIVE_WARN);
1398 /* Add entry to the internal list. */
1399 archive_entry_acl_add_entry_w_len(entry, type, permset,
1400 tag, id, field[1].start, field[1].end - field[1].start);
1402 return (ARCHIVE_OK);
1406 * extended attribute handling
1410 archive_entry_xattr_clear(struct archive_entry *entry)
1412 struct ae_xattr *xp;
1414 while (entry->xattr_head != NULL) {
1415 xp = entry->xattr_head->next;
1416 free(entry->xattr_head->name);
1417 free(entry->xattr_head->value);
1418 free(entry->xattr_head);
1419 entry->xattr_head = xp;
1422 entry->xattr_head = NULL;
1426 archive_entry_xattr_add_entry(struct archive_entry *entry,
1427 const char *name, const void *value, size_t size)
1429 struct ae_xattr *xp;
1431 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1434 if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1438 xp->name = strdup(name);
1439 if ((xp->value = malloc(size)) != NULL) {
1440 memcpy(xp->value, value, size);
1445 xp->next = entry->xattr_head;
1446 entry->xattr_head = xp;
1451 * returns number of the extended attribute entries
1454 archive_entry_xattr_count(struct archive_entry *entry)
1456 struct ae_xattr *xp;
1459 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1466 archive_entry_xattr_reset(struct archive_entry * entry)
1468 entry->xattr_p = entry->xattr_head;
1470 return archive_entry_xattr_count(entry);
1474 archive_entry_xattr_next(struct archive_entry * entry,
1475 const char **name, const void **value, size_t *size)
1477 if (entry->xattr_p) {
1478 *name = entry->xattr_p->name;
1479 *value = entry->xattr_p->value;
1480 *size = entry->xattr_p->size;
1482 entry->xattr_p = entry->xattr_p->next;
1484 return (ARCHIVE_OK);
1489 return (ARCHIVE_WARN);
1494 * end of xattr handling
1498 * Parse a string to a positive decimal integer. Returns true if
1499 * the string is non-empty and consists only of decimal digits,
1503 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1508 while (start < end) {
1509 if (*start < '0' || *start > '9')
1511 if (n > (INT_MAX / 10))
1524 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1525 * to point to just after the separator. *start points to the first
1526 * character of the matched text and *end just after the last
1527 * character of the matched identifier. In particular *end - *start
1528 * is the length of the field body, not including leading or trailing
1532 next_field_w(const wchar_t **wp, const wchar_t **start,
1533 const wchar_t **end, wchar_t *sep)
1535 /* Skip leading whitespace to find start of field. */
1536 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1541 /* Scan for the separator. */
1542 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1548 /* Trim trailing whitespace to locate end of field. */
1550 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1555 /* Adjust scanner location. */
1561 * Return true if the characters [start...end) are a prefix of 'test'.
1562 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1565 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1570 if (*start++ != *test++)
1573 while (start < end && *start++ == *test++)
1584 * Following code is modified from UC Berkeley sources, and
1585 * is subject to the following copyright notice.
1589 * Copyright (c) 1993
1590 * The Regents of the University of California. All rights reserved.
1592 * Redistribution and use in source and binary forms, with or without
1593 * modification, are permitted provided that the following conditions
1595 * 1. Redistributions of source code must retain the above copyright
1596 * notice, this list of conditions and the following disclaimer.
1597 * 2. Redistributions in binary form must reproduce the above copyright
1598 * notice, this list of conditions and the following disclaimer in the
1599 * documentation and/or other materials provided with the distribution.
1600 * 4. Neither the name of the University nor the names of its contributors
1601 * may be used to endorse or promote products derived from this software
1602 * without specific prior written permission.
1604 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1605 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1606 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1607 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1608 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1609 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1610 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1611 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1612 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1613 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1617 static struct flag {
1619 const wchar_t *wname;
1621 unsigned long clear;
1623 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1625 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1626 { "nosappend", L"nosappend", SF_APPEND, 0 },
1628 #ifdef EXT2_APPEND_FL /* 'a' */
1629 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1630 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1633 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1634 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1637 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1638 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1639 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1641 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1642 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1643 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1644 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1647 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1648 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1651 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1654 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1655 { "nouappend", L"nouappend", UF_APPEND, 0 },
1658 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1659 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1660 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1663 { "nodump", L"nodump", 0, UF_NODUMP},
1665 #ifdef EXT2_NODUMP_FL /* 'd' */
1666 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1669 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1672 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1673 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1675 #ifdef EXT2_COMPR_FL /* 'c' */
1676 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1679 #ifdef EXT2_NOATIME_FL /* 'A' */
1680 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1682 { NULL, NULL, 0, 0 }
1687 * Convert file flags to a comma-separated string. If no flags
1688 * are set, return the empty string.
1691 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1699 bits = bitset | bitclear;
1701 for (flag = flags; flag->name != NULL; flag++)
1702 if (bits & (flag->set | flag->clear)) {
1703 length += strlen(flag->name) + 1;
1704 bits &= ~(flag->set | flag->clear);
1709 string = (char *)malloc(length);
1714 for (flag = flags; flag->name != NULL; flag++) {
1715 if (bitset & flag->set || bitclear & flag->clear) {
1716 sp = flag->name + 2;
1717 } else if (bitset & flag->clear || bitclear & flag->set) {
1721 bitset &= ~(flag->set | flag->clear);
1722 bitclear &= ~(flag->set | flag->clear);
1725 while ((*dp++ = *sp++) != '\0')
1736 * Take string of arguments and return file flags. This
1737 * version works a little differently than strtofflags(3).
1738 * In particular, it always tests every token, skipping any
1739 * unrecognized tokens. It returns a pointer to the first
1740 * unrecognized token, or NULL if every token was recognized.
1741 * This version is also const-correct and does not modify the
1745 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1747 const wchar_t *start, *end;
1749 unsigned long set, clear;
1750 const wchar_t *failed;
1755 /* Find start of first token. */
1756 while (*start == L'\t' || *start == L' ' || *start == L',')
1758 while (*start != L'\0') {
1759 /* Locate end of token. */
1761 while (*end != L'\0' && *end != L'\t' &&
1762 *end != L' ' && *end != L',')
1764 for (flag = flags; flag->wname != NULL; flag++) {
1765 if (wmemcmp(start, flag->wname, end - start) == 0) {
1766 /* Matched "noXXXX", so reverse the sense. */
1770 } else if (wmemcmp(start, flag->wname + 2, end - start)
1772 /* Matched "XXXX", so don't reverse. */
1774 clear |= flag->clear;
1778 /* Ignore unknown flag names. */
1779 if (flag->wname == NULL && failed == NULL)
1782 /* Find start of next token. */
1784 while (*start == L'\t' || *start == L' ' || *start == L',')
1794 /* Return location of first failure. */
1802 main(int argc, char **argv)
1804 struct archive_entry *entry = archive_entry_new();
1805 unsigned long set, clear;
1806 const wchar_t *remainder;
1808 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1809 archive_entry_fflags(entry, &set, &clear);
1811 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1813 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));