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>
38 #ifdef MAJOR_IN_SYSMACROS
39 #include <sys/sysmacros.h>
42 #ifdef HAVE_EXT2FS_EXT2_FS_H
43 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
48 #ifdef HAVE_LINUX_FS_H
49 #include <linux/fs.h> /* for Linux file flags */
51 #ifdef HAVE_LINUX_EXT2_FS_H
52 #include <linux/ext2_fs.h> /* for Linux file flags */
67 #include "archive_entry.h"
68 #include "archive_private.h"
69 #include "archive_entry_private.h"
72 #define max(a, b) ((a)>(b)?(a):(b))
74 /* Play games to come up with a suitable makedev() definition. */
77 #include <sys/netmgr.h>
78 #define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
80 /* There's a "makedev" macro. */
81 #define ae_makedev(maj, min) makedev((maj), (min))
82 #elif defined mkdev || defined _WIN32 || defined __WIN32__
84 #define ae_makedev(maj, min) mkdev((maj), (min))
86 /* There's a "makedev" function. */
87 #define ae_makedev(maj, min) makedev((maj), (min))
90 static void aes_clean(struct aes *);
91 static void aes_copy(struct aes *dest, struct aes *src);
92 static const char * aes_get_mbs(struct aes *);
93 static const wchar_t * aes_get_wcs(struct aes *);
94 static void aes_set_mbs(struct aes *, const char *mbs);
95 static void aes_copy_mbs(struct aes *, const char *mbs);
96 /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
97 static void aes_copy_wcs(struct aes *, const wchar_t *wcs);
98 static void aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
100 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
101 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
102 unsigned long *setp, unsigned long *clrp);
103 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
104 const wchar_t *wname, int perm, int id);
105 static void append_id_w(wchar_t **wp, int id);
107 static int acl_special(struct archive_entry *entry,
108 int type, int permset, int tag);
109 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
110 int type, int permset, int tag, int id);
111 static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
112 static void next_field_w(const wchar_t **wp, const wchar_t **start,
113 const wchar_t **end, wchar_t *sep);
114 static int prefix_w(const wchar_t *start, const wchar_t *end,
115 const wchar_t *test);
117 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
118 int permset, int tag, int id, const wchar_t *name, size_t);
122 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
125 while ((*s1 = *s2) != L'\0')
131 static size_t wcslen(const wchar_t *s)
133 const wchar_t *p = s;
140 /* Good enough for simple equality testing, but not for sorting. */
141 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
144 #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
149 aes_clean(struct aes *aes)
151 if (aes->aes_mbs_alloc) {
152 free(aes->aes_mbs_alloc);
153 aes->aes_mbs_alloc = NULL;
155 if (aes->aes_wcs_alloc) {
156 free(aes->aes_wcs_alloc);
157 aes->aes_wcs_alloc = NULL;
159 memset(aes, 0, sizeof(*aes));
163 aes_copy(struct aes *dest, struct aes *src)
166 if (src->aes_mbs != NULL) {
167 dest->aes_mbs_alloc = strdup(src->aes_mbs);
168 dest->aes_mbs = dest->aes_mbs_alloc;
169 if (dest->aes_mbs == NULL)
170 __archive_errx(1, "No memory for aes_copy()");
173 if (src->aes_wcs != NULL) {
174 dest->aes_wcs_alloc = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
176 dest->aes_wcs = dest->aes_wcs_alloc;
177 if (dest->aes_wcs == NULL)
178 __archive_errx(1, "No memory for aes_copy()");
179 wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
184 aes_get_mbs(struct aes *aes)
186 if (aes->aes_mbs == NULL && aes->aes_wcs == NULL)
188 if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
190 * XXX Need to estimate the number of byte in the
191 * multi-byte form. Assume that, on average, wcs
192 * chars encode to no more than 3 bytes. There must
193 * be a better way... XXX
195 size_t mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
197 aes->aes_mbs_alloc = (char *)malloc(mbs_length);
198 aes->aes_mbs = aes->aes_mbs_alloc;
199 if (aes->aes_mbs == NULL)
200 __archive_errx(1, "No memory for aes_get_mbs()");
201 wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
202 aes->aes_mbs_alloc[mbs_length - 1] = 0;
204 return (aes->aes_mbs);
207 static const wchar_t *
208 aes_get_wcs(struct aes *aes)
210 if (aes->aes_wcs == NULL && aes->aes_mbs == NULL)
212 if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
214 * No single byte will be more than one wide character,
215 * so this length estimate will always be big enough.
217 size_t wcs_length = strlen(aes->aes_mbs);
220 = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
221 aes->aes_wcs = aes->aes_wcs_alloc;
222 if (aes->aes_wcs == NULL)
223 __archive_errx(1, "No memory for aes_get_wcs()");
224 mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
225 aes->aes_wcs_alloc[wcs_length] = 0;
227 return (aes->aes_wcs);
231 aes_set_mbs(struct aes *aes, const char *mbs)
233 if (aes->aes_mbs_alloc) {
234 free(aes->aes_mbs_alloc);
235 aes->aes_mbs_alloc = NULL;
237 if (aes->aes_wcs_alloc) {
238 free(aes->aes_wcs_alloc);
239 aes->aes_wcs_alloc = NULL;
246 aes_copy_mbs(struct aes *aes, const char *mbs)
248 if (aes->aes_mbs_alloc) {
249 free(aes->aes_mbs_alloc);
250 aes->aes_mbs_alloc = NULL;
252 if (aes->aes_wcs_alloc) {
253 free(aes->aes_wcs_alloc);
254 aes->aes_wcs_alloc = NULL;
256 aes->aes_mbs_alloc = (char *)malloc((strlen(mbs) + 1) * sizeof(char));
257 if (aes->aes_mbs_alloc == NULL)
258 __archive_errx(1, "No memory for aes_copy_mbs()");
259 strcpy(aes->aes_mbs_alloc, mbs);
260 aes->aes_mbs = aes->aes_mbs_alloc;
266 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
268 if (aes->aes_mbs_alloc) {
269 free(aes->aes_mbs_alloc);
270 aes->aes_mbs_alloc = NULL;
272 if (aes->aes_wcs_alloc) {
273 free(aes->aes_wcs_alloc);
274 aes->aes_wcs_alloc = NULL;
282 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
284 aes_copy_wcs_len(aes, wcs, wcslen(wcs));
288 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
290 if (aes->aes_mbs_alloc) {
291 free(aes->aes_mbs_alloc);
292 aes->aes_mbs_alloc = NULL;
294 if (aes->aes_wcs_alloc) {
295 free(aes->aes_wcs_alloc);
296 aes->aes_wcs_alloc = NULL;
299 aes->aes_wcs_alloc = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
300 if (aes->aes_wcs_alloc == NULL)
301 __archive_errx(1, "No memory for aes_copy_wcs()");
302 wmemcpy(aes->aes_wcs_alloc, wcs, len);
303 aes->aes_wcs_alloc[len] = L'\0';
304 aes->aes_wcs = aes->aes_wcs_alloc;
307 struct archive_entry *
308 archive_entry_clear(struct archive_entry *entry)
310 aes_clean(&entry->ae_fflags_text);
311 aes_clean(&entry->ae_gname);
312 aes_clean(&entry->ae_hardlink);
313 aes_clean(&entry->ae_pathname);
314 aes_clean(&entry->ae_symlink);
315 aes_clean(&entry->ae_uname);
316 archive_entry_acl_clear(entry);
317 archive_entry_xattr_clear(entry);
319 memset(entry, 0, sizeof(*entry));
323 struct archive_entry *
324 archive_entry_clone(struct archive_entry *entry)
326 struct archive_entry *entry2;
327 struct ae_acl *ap, *ap2;
330 /* Allocate new structure and copy over all of the fields. */
331 entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
334 memset(entry2, 0, sizeof(*entry2));
335 entry2->ae_stat = entry->ae_stat;
336 entry2->ae_fflags_set = entry->ae_fflags_set;
337 entry2->ae_fflags_clear = entry->ae_fflags_clear;
339 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
340 aes_copy(&entry2->ae_gname, &entry->ae_gname);
341 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
342 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
343 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
344 aes_copy(&entry2->ae_uname, &entry->ae_uname);
346 /* Copy ACL data over. */
347 ap = entry->acl_head;
349 ap2 = acl_new_entry(entry2,
350 ap->type, ap->permset, ap->tag, ap->id);
352 aes_copy(&ap2->name, &ap->name);
356 /* Copy xattr data over. */
357 xp = entry->xattr_head;
359 archive_entry_xattr_add_entry(entry2,
360 xp->name, xp->value, xp->size);
368 archive_entry_free(struct archive_entry *entry)
370 archive_entry_clear(entry);
374 struct archive_entry *
375 archive_entry_new(void)
377 struct archive_entry *entry;
379 entry = (struct archive_entry *)malloc(sizeof(*entry));
382 memset(entry, 0, sizeof(*entry));
387 * Functions for reading fields from an archive_entry.
391 archive_entry_atime(struct archive_entry *entry)
393 return (entry->ae_stat.aest_atime);
397 archive_entry_atime_nsec(struct archive_entry *entry)
399 return (entry->ae_stat.aest_atime_nsec);
403 archive_entry_ctime(struct archive_entry *entry)
405 return (entry->ae_stat.aest_ctime);
409 archive_entry_ctime_nsec(struct archive_entry *entry)
411 return (entry->ae_stat.aest_ctime_nsec);
415 archive_entry_dev(struct archive_entry *entry)
417 if (entry->ae_stat.aest_dev_is_broken_down)
418 return ae_makedev(entry->ae_stat.aest_devmajor,
419 entry->ae_stat.aest_devminor);
421 return (entry->ae_stat.aest_dev);
425 archive_entry_devmajor(struct archive_entry *entry)
427 if (entry->ae_stat.aest_dev_is_broken_down)
428 return (entry->ae_stat.aest_devmajor);
430 return major(entry->ae_stat.aest_dev);
434 archive_entry_devminor(struct archive_entry *entry)
436 if (entry->ae_stat.aest_dev_is_broken_down)
437 return (entry->ae_stat.aest_devminor);
439 return minor(entry->ae_stat.aest_dev);
443 archive_entry_filetype(struct archive_entry *entry)
445 return (AE_IFMT & entry->ae_stat.aest_mode);
449 archive_entry_fflags(struct archive_entry *entry,
450 unsigned long *set, unsigned long *clear)
452 *set = entry->ae_fflags_set;
453 *clear = entry->ae_fflags_clear;
457 * Note: if text was provided, this just returns that text. If you
458 * really need the text to be rebuilt in a canonical form, set the
459 * text, ask for the bitmaps, then set the bitmaps. (Setting the
460 * bitmaps clears any stored text.) This design is deliberate: if
461 * we're editing archives, we don't want to discard flags just because
462 * they aren't supported on the current system. The bitmap<->text
463 * conversions are platform-specific (see below).
466 archive_entry_fflags_text(struct archive_entry *entry)
471 f = aes_get_mbs(&entry->ae_fflags_text);
475 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
478 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
482 aes_copy_mbs(&entry->ae_fflags_text, p);
484 f = aes_get_mbs(&entry->ae_fflags_text);
489 archive_entry_gid(struct archive_entry *entry)
491 return (entry->ae_stat.aest_gid);
495 archive_entry_gname(struct archive_entry *entry)
497 return (aes_get_mbs(&entry->ae_gname));
501 archive_entry_gname_w(struct archive_entry *entry)
503 return (aes_get_wcs(&entry->ae_gname));
507 archive_entry_hardlink(struct archive_entry *entry)
509 return (aes_get_mbs(&entry->ae_hardlink));
513 archive_entry_hardlink_w(struct archive_entry *entry)
515 return (aes_get_wcs(&entry->ae_hardlink));
519 archive_entry_ino(struct archive_entry *entry)
521 return (entry->ae_stat.aest_ino);
525 archive_entry_mode(struct archive_entry *entry)
527 return (entry->ae_stat.aest_mode);
531 archive_entry_mtime(struct archive_entry *entry)
533 return (entry->ae_stat.aest_mtime);
537 archive_entry_mtime_nsec(struct archive_entry *entry)
539 return (entry->ae_stat.aest_mtime_nsec);
543 archive_entry_nlink(struct archive_entry *entry)
545 return (entry->ae_stat.aest_nlink);
549 archive_entry_pathname(struct archive_entry *entry)
551 return (aes_get_mbs(&entry->ae_pathname));
555 archive_entry_pathname_w(struct archive_entry *entry)
557 return (aes_get_wcs(&entry->ae_pathname));
561 archive_entry_rdev(struct archive_entry *entry)
563 if (entry->ae_stat.aest_rdev_is_broken_down)
564 return ae_makedev(entry->ae_stat.aest_rdevmajor,
565 entry->ae_stat.aest_rdevminor);
567 return (entry->ae_stat.aest_rdev);
571 archive_entry_rdevmajor(struct archive_entry *entry)
573 if (entry->ae_stat.aest_rdev_is_broken_down)
574 return (entry->ae_stat.aest_rdevmajor);
576 return major(entry->ae_stat.aest_rdev);
580 archive_entry_rdevminor(struct archive_entry *entry)
582 if (entry->ae_stat.aest_rdev_is_broken_down)
583 return (entry->ae_stat.aest_rdevminor);
585 return minor(entry->ae_stat.aest_rdev);
589 archive_entry_size(struct archive_entry *entry)
591 return (entry->ae_stat.aest_size);
595 archive_entry_symlink(struct archive_entry *entry)
597 return (aes_get_mbs(&entry->ae_symlink));
601 archive_entry_symlink_w(struct archive_entry *entry)
603 return (aes_get_wcs(&entry->ae_symlink));
607 archive_entry_uid(struct archive_entry *entry)
609 return (entry->ae_stat.aest_uid);
613 archive_entry_uname(struct archive_entry *entry)
615 return (aes_get_mbs(&entry->ae_uname));
619 archive_entry_uname_w(struct archive_entry *entry)
621 return (aes_get_wcs(&entry->ae_uname));
625 * Functions to set archive_entry properties.
629 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
631 entry->stat_valid = 0;
632 entry->ae_stat.aest_mode &= ~AE_IFMT;
633 entry->ae_stat.aest_mode |= AE_IFMT & type;
637 archive_entry_set_fflags(struct archive_entry *entry,
638 unsigned long set, unsigned long clear)
640 aes_clean(&entry->ae_fflags_text);
641 entry->ae_fflags_set = set;
642 entry->ae_fflags_clear = clear;
646 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
647 const wchar_t *flags)
649 aes_copy_wcs(&entry->ae_fflags_text, flags);
650 return (ae_wcstofflags(flags,
651 &entry->ae_fflags_set, &entry->ae_fflags_clear));
655 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
657 entry->stat_valid = 0;
658 entry->ae_stat.aest_gid = g;
662 archive_entry_set_gname(struct archive_entry *entry, const char *name)
664 aes_set_mbs(&entry->ae_gname, name);
668 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
670 aes_copy_mbs(&entry->ae_gname, name);
674 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
676 aes_copy_wcs(&entry->ae_gname, name);
680 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
682 entry->stat_valid = 0;
683 entry->ae_stat.aest_ino = ino;
687 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
689 aes_set_mbs(&entry->ae_hardlink, target);
693 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
695 aes_copy_mbs(&entry->ae_hardlink, target);
699 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
701 aes_copy_wcs(&entry->ae_hardlink, target);
705 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
707 entry->stat_valid = 0;
708 entry->ae_stat.aest_atime = t;
709 entry->ae_stat.aest_atime_nsec = ns;
713 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
715 entry->stat_valid = 0;
716 entry->ae_stat.aest_ctime = t;
717 entry->ae_stat.aest_ctime_nsec = ns;
721 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
723 entry->stat_valid = 0;
724 entry->ae_stat.aest_dev_is_broken_down = 0;
725 entry->ae_stat.aest_dev = d;
729 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
731 entry->stat_valid = 0;
732 entry->ae_stat.aest_dev_is_broken_down = 1;
733 entry->ae_stat.aest_devmajor = m;
737 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
739 entry->stat_valid = 0;
740 entry->ae_stat.aest_dev_is_broken_down = 1;
741 entry->ae_stat.aest_devminor = m;
744 /* Set symlink if symlink is already set, else set hardlink. */
746 archive_entry_set_link(struct archive_entry *entry, const char *target)
748 if (entry->ae_symlink.aes_mbs != NULL ||
749 entry->ae_symlink.aes_wcs != NULL)
750 aes_set_mbs(&entry->ae_symlink, target);
752 aes_set_mbs(&entry->ae_hardlink, target);
756 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
758 entry->stat_valid = 0;
759 entry->ae_stat.aest_mode = m;
763 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
765 entry->stat_valid = 0;
766 entry->ae_stat.aest_mtime = m;
767 entry->ae_stat.aest_mtime_nsec = ns;
771 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
773 entry->stat_valid = 0;
774 entry->ae_stat.aest_nlink = nlink;
778 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
780 aes_set_mbs(&entry->ae_pathname, name);
784 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
786 aes_copy_mbs(&entry->ae_pathname, name);
790 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
792 aes_copy_wcs(&entry->ae_pathname, name);
796 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
798 entry->stat_valid = 0;
799 entry->ae_stat.aest_mode &= AE_IFMT;
800 entry->ae_stat.aest_mode |= ~AE_IFMT & p;
804 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
806 entry->stat_valid = 0;
807 entry->ae_stat.aest_rdev = m;
808 entry->ae_stat.aest_rdev_is_broken_down = 0;
812 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
814 entry->stat_valid = 0;
815 entry->ae_stat.aest_rdev_is_broken_down = 1;
816 entry->ae_stat.aest_rdevmajor = m;
820 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
822 entry->stat_valid = 0;
823 entry->ae_stat.aest_rdev_is_broken_down = 1;
824 entry->ae_stat.aest_rdevminor = m;
828 archive_entry_set_size(struct archive_entry *entry, int64_t s)
830 entry->stat_valid = 0;
831 entry->ae_stat.aest_size = s;
835 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
837 aes_set_mbs(&entry->ae_symlink, linkname);
841 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
843 aes_copy_mbs(&entry->ae_symlink, linkname);
847 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
849 aes_copy_wcs(&entry->ae_symlink, linkname);
853 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
855 entry->stat_valid = 0;
856 entry->ae_stat.aest_uid = u;
860 archive_entry_set_uname(struct archive_entry *entry, const char *name)
862 aes_set_mbs(&entry->ae_uname, name);
866 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
868 aes_copy_mbs(&entry->ae_uname, name);
872 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
874 aes_copy_wcs(&entry->ae_uname, name);
878 * ACL management. The following would, of course, be a lot simpler
879 * if: 1) the last draft of POSIX.1e were a really thorough and
880 * complete standard that addressed the needs of ACL archiving and 2)
881 * everyone followed it faithfully. Alas, neither is true, so the
882 * following is a lot more complex than might seem necessary to the
887 archive_entry_acl_clear(struct archive_entry *entry)
891 while (entry->acl_head != NULL) {
892 ap = entry->acl_head->next;
893 aes_clean(&entry->acl_head->name);
894 free(entry->acl_head);
895 entry->acl_head = ap;
897 if (entry->acl_text_w != NULL) {
898 free(entry->acl_text_w);
899 entry->acl_text_w = NULL;
902 entry->acl_state = 0; /* Not counting. */
906 * Add a single ACL entry to the internal list of ACL data.
909 archive_entry_acl_add_entry(struct archive_entry *entry,
910 int type, int permset, int tag, int id, const char *name)
914 if (acl_special(entry, type, permset, tag) == 0)
916 ap = acl_new_entry(entry, type, permset, tag, id);
921 if (name != NULL && *name != '\0')
922 aes_copy_mbs(&ap->name, name);
924 aes_clean(&ap->name);
928 * As above, but with a wide-character name.
931 archive_entry_acl_add_entry_w(struct archive_entry *entry,
932 int type, int permset, int tag, int id, const wchar_t *name)
934 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
938 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
939 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
943 if (acl_special(entry, type, permset, tag) == 0)
945 ap = acl_new_entry(entry, type, permset, tag, id);
950 if (name != NULL && *name != L'\0' && len > 0)
951 aes_copy_wcs_len(&ap->name, name, len);
953 aes_clean(&ap->name);
957 * If this ACL entry is part of the standard POSIX permissions set,
958 * store the permissions in the stat structure and return zero.
961 acl_special(struct archive_entry *entry, int type, int permset, int tag)
963 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
965 case ARCHIVE_ENTRY_ACL_USER_OBJ:
966 entry->ae_stat.aest_mode &= ~0700;
967 entry->ae_stat.aest_mode |= (permset & 7) << 6;
969 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
970 entry->ae_stat.aest_mode &= ~0070;
971 entry->ae_stat.aest_mode |= (permset & 7) << 3;
973 case ARCHIVE_ENTRY_ACL_OTHER:
974 entry->ae_stat.aest_mode &= ~0007;
975 entry->ae_stat.aest_mode |= permset & 7;
983 * Allocate and populate a new ACL entry with everything but the
986 static struct ae_acl *
987 acl_new_entry(struct archive_entry *entry,
988 int type, int permset, int tag, int id)
992 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
993 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
995 if (entry->acl_text_w != NULL) {
996 free(entry->acl_text_w);
997 entry->acl_text_w = NULL;
1000 /* XXX TODO: More sanity-checks on the arguments XXX */
1002 /* If there's a matching entry already in the list, overwrite it. */
1003 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
1004 if (ap->type == type && ap->tag == tag && ap->id == id) {
1005 ap->permset = permset;
1010 /* Add a new entry to the list. */
1011 ap = (struct ae_acl *)malloc(sizeof(*ap));
1014 memset(ap, 0, sizeof(*ap));
1015 ap->next = entry->acl_head;
1016 entry->acl_head = ap;
1020 ap->permset = permset;
1025 * Return a count of entries matching "want_type".
1028 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1034 ap = entry->acl_head;
1035 while (ap != NULL) {
1036 if ((ap->type & want_type) != 0)
1041 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1047 * Prepare for reading entries from the ACL data. Returns a count
1048 * of entries matching "want_type", or zero if there are no
1049 * non-extended ACL entries of that type.
1052 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1056 count = archive_entry_acl_count(entry, want_type);
1059 * If the only entries are the three standard ones,
1060 * then don't return any ACL data. (In this case,
1061 * client can just use chmod(2) to set permissions.)
1063 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1069 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1071 entry->acl_state = 0;
1072 entry->acl_p = entry->acl_head;
1077 * Return the next ACL entry in the list. Fake entries for the
1078 * standard permissions and include them in the returned list.
1082 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1083 int *permset, int *tag, int *id, const char **name)
1089 * The acl_state is either zero (no entries available), -1
1090 * (reading from list), or an entry type (retrieve that type
1091 * from ae_stat.aest_mode).
1093 if (entry->acl_state == 0)
1094 return (ARCHIVE_WARN);
1096 /* The first three access entries are special. */
1097 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1098 switch (entry->acl_state) {
1099 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1100 *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1101 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1102 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1103 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1104 return (ARCHIVE_OK);
1105 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1106 *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1107 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1108 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1109 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1110 return (ARCHIVE_OK);
1111 case ARCHIVE_ENTRY_ACL_OTHER:
1112 *permset = entry->ae_stat.aest_mode & 7;
1113 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1114 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1115 entry->acl_state = -1;
1116 entry->acl_p = entry->acl_head;
1117 return (ARCHIVE_OK);
1123 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1124 entry->acl_p = entry->acl_p->next;
1125 if (entry->acl_p == NULL) {
1126 entry->acl_state = 0;
1127 return (ARCHIVE_EOF); /* End of ACL entries. */
1129 *type = entry->acl_p->type;
1130 *permset = entry->acl_p->permset;
1131 *tag = entry->acl_p->tag;
1132 *id = entry->acl_p->id;
1133 *name = aes_get_mbs(&entry->acl_p->name);
1134 entry->acl_p = entry->acl_p->next;
1135 return (ARCHIVE_OK);
1139 * Generate a text version of the ACL. The flags parameter controls
1140 * the style of the generated ACL.
1143 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1147 const wchar_t *wname;
1148 const wchar_t *prefix;
1154 if (entry->acl_text_w != NULL) {
1155 free (entry->acl_text_w);
1156 entry->acl_text_w = NULL;
1162 ap = entry->acl_head;
1163 while (ap != NULL) {
1164 if ((ap->type & flags) != 0) {
1166 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1167 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1168 length += 8; /* "default:" */
1169 length += 5; /* tag name */
1170 length += 1; /* colon */
1171 wname = aes_get_wcs(&ap->name);
1173 length += wcslen(wname);
1175 length += sizeof(uid_t) * 3 + 1;
1176 length ++; /* colon */
1177 length += 3; /* rwx */
1178 length += 1; /* colon */
1179 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1180 length ++; /* newline */
1185 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1186 length += 10; /* "user::rwx\n" */
1187 length += 11; /* "group::rwx\n" */
1188 length += 11; /* "other::rwx\n" */
1194 /* Now, allocate the string and actually populate it. */
1195 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1197 __archive_errx(1, "No memory to generate the text version of the ACL");
1199 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1200 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1201 entry->ae_stat.aest_mode & 0700, -1);
1203 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1204 entry->ae_stat.aest_mode & 0070, -1);
1206 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1207 entry->ae_stat.aest_mode & 0007, -1);
1210 ap = entry->acl_head;
1211 while (ap != NULL) {
1212 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1213 wname = aes_get_wcs(&ap->name);
1215 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1219 append_entry_w(&wp, NULL, ap->tag, wname,
1228 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1229 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1230 prefix = L"default:";
1233 ap = entry->acl_head;
1235 while (ap != NULL) {
1236 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1237 wname = aes_get_wcs(&ap->name);
1240 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1244 append_entry_w(&wp, prefix, ap->tag,
1245 wname, ap->permset, id);
1252 return (entry->acl_text_w);
1256 append_id_w(wchar_t **wp, int id)
1261 append_id_w(wp, id / 10);
1262 *(*wp)++ = L"0123456789"[id % 10];
1266 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1267 const wchar_t *wname, int perm, int id)
1269 if (prefix != NULL) {
1270 wcscpy(*wp, prefix);
1274 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1278 case ARCHIVE_ENTRY_ACL_USER:
1279 wcscpy(*wp, L"user");
1281 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1285 case ARCHIVE_ENTRY_ACL_GROUP:
1286 wcscpy(*wp, L"group");
1288 case ARCHIVE_ENTRY_ACL_MASK:
1289 wcscpy(*wp, L"mask");
1293 case ARCHIVE_ENTRY_ACL_OTHER:
1294 wcscpy(*wp, L"other");
1301 if (wname != NULL) {
1304 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1305 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1306 append_id_w(wp, id);
1310 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1311 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1312 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1315 append_id_w(wp, id);
1321 * Parse a textual ACL. This automatically recognizes and supports
1322 * extensions described above. The 'type' argument is used to
1323 * indicate the type that should be used for any entries not
1324 * explicitly marked as "default:".
1327 __archive_entry_acl_parse_w(struct archive_entry *entry,
1328 const wchar_t *text, int default_type)
1331 const wchar_t *start;
1336 int type, tag, permset, id;
1340 while (text != NULL && *text != L'\0') {
1342 * Parse the fields out of the next entry,
1343 * advance 'text' to start of next entry.
1347 const wchar_t *start, *end;
1348 next_field_w(&text, &start, &end, &sep);
1350 field[fields].start = start;
1351 field[fields].end = end;
1354 } while (sep == L':');
1357 return (ARCHIVE_WARN);
1359 /* Check for a numeric ID in field 1 or 3. */
1361 isint_w(field[1].start, field[1].end, &id);
1362 /* Field 3 is optional. */
1363 if (id == -1 && fields > 3)
1364 isint_w(field[3].start, field[3].end, &id);
1366 /* Parse the permissions from field 2. */
1369 while (p < field[2].end) {
1372 permset |= ARCHIVE_ENTRY_ACL_READ;
1375 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1378 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1383 return (ARCHIVE_WARN);
1388 * Solaris extension: "defaultuser::rwx" is the
1389 * default ACL corresponding to "user::rwx", etc.
1391 if (field[0].end-field[0].start > 7
1392 && wmemcmp(field[0].start, L"default", 7) == 0) {
1393 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1394 field[0].start += 7;
1396 type = default_type;
1398 if (prefix_w(field[0].start, field[0].end, L"user")) {
1399 if (id != -1 || field[1].start < field[1].end)
1400 tag = ARCHIVE_ENTRY_ACL_USER;
1402 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1403 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1404 if (id != -1 || field[1].start < field[1].end)
1405 tag = ARCHIVE_ENTRY_ACL_GROUP;
1407 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1408 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1409 if (id != -1 || field[1].start < field[1].end)
1410 return (ARCHIVE_WARN);
1411 tag = ARCHIVE_ENTRY_ACL_OTHER;
1412 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1413 if (id != -1 || field[1].start < field[1].end)
1414 return (ARCHIVE_WARN);
1415 tag = ARCHIVE_ENTRY_ACL_MASK;
1417 return (ARCHIVE_WARN);
1419 /* Add entry to the internal list. */
1420 archive_entry_acl_add_entry_w_len(entry, type, permset,
1421 tag, id, field[1].start, field[1].end - field[1].start);
1423 return (ARCHIVE_OK);
1427 * extended attribute handling
1431 archive_entry_xattr_clear(struct archive_entry *entry)
1433 struct ae_xattr *xp;
1435 while (entry->xattr_head != NULL) {
1436 xp = entry->xattr_head->next;
1437 free(entry->xattr_head->name);
1438 free(entry->xattr_head->value);
1439 free(entry->xattr_head);
1440 entry->xattr_head = xp;
1443 entry->xattr_head = NULL;
1447 archive_entry_xattr_add_entry(struct archive_entry *entry,
1448 const char *name, const void *value, size_t size)
1450 struct ae_xattr *xp;
1452 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1455 if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1459 xp->name = strdup(name);
1460 if ((xp->value = malloc(size)) != NULL) {
1461 memcpy(xp->value, value, size);
1466 xp->next = entry->xattr_head;
1467 entry->xattr_head = xp;
1472 * returns number of the extended attribute entries
1475 archive_entry_xattr_count(struct archive_entry *entry)
1477 struct ae_xattr *xp;
1480 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1487 archive_entry_xattr_reset(struct archive_entry * entry)
1489 entry->xattr_p = entry->xattr_head;
1491 return archive_entry_xattr_count(entry);
1495 archive_entry_xattr_next(struct archive_entry * entry,
1496 const char **name, const void **value, size_t *size)
1498 if (entry->xattr_p) {
1499 *name = entry->xattr_p->name;
1500 *value = entry->xattr_p->value;
1501 *size = entry->xattr_p->size;
1503 entry->xattr_p = entry->xattr_p->next;
1505 return (ARCHIVE_OK);
1510 return (ARCHIVE_WARN);
1515 * end of xattr handling
1519 * Parse a string to a positive decimal integer. Returns true if
1520 * the string is non-empty and consists only of decimal digits,
1524 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1529 while (start < end) {
1530 if (*start < '0' || *start > '9')
1532 if (n > (INT_MAX / 10))
1545 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1546 * to point to just after the separator. *start points to the first
1547 * character of the matched text and *end just after the last
1548 * character of the matched identifier. In particular *end - *start
1549 * is the length of the field body, not including leading or trailing
1553 next_field_w(const wchar_t **wp, const wchar_t **start,
1554 const wchar_t **end, wchar_t *sep)
1556 /* Skip leading whitespace to find start of field. */
1557 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1562 /* Scan for the separator. */
1563 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1569 /* Trim trailing whitespace to locate end of field. */
1571 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1576 /* Adjust scanner location. */
1582 * Return true if the characters [start...end) are a prefix of 'test'.
1583 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1586 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1591 if (*start++ != *test++)
1594 while (start < end && *start++ == *test++)
1605 * Following code is modified from UC Berkeley sources, and
1606 * is subject to the following copyright notice.
1610 * Copyright (c) 1993
1611 * The Regents of the University of California. All rights reserved.
1613 * Redistribution and use in source and binary forms, with or without
1614 * modification, are permitted provided that the following conditions
1616 * 1. Redistributions of source code must retain the above copyright
1617 * notice, this list of conditions and the following disclaimer.
1618 * 2. Redistributions in binary form must reproduce the above copyright
1619 * notice, this list of conditions and the following disclaimer in the
1620 * documentation and/or other materials provided with the distribution.
1621 * 4. Neither the name of the University nor the names of its contributors
1622 * may be used to endorse or promote products derived from this software
1623 * without specific prior written permission.
1625 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1626 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1627 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1628 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1629 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1630 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1631 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1632 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1633 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1634 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1638 static struct flag {
1640 const wchar_t *wname;
1642 unsigned long clear;
1644 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1646 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1647 { "nosappend", L"nosappend", SF_APPEND, 0 },
1649 #ifdef EXT2_APPEND_FL /* 'a' */
1650 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1651 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1654 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1655 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1658 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1659 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1660 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1662 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1663 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1664 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1665 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1668 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1669 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1672 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1675 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1676 { "nouappend", L"nouappend", UF_APPEND, 0 },
1679 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1680 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1681 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1684 { "nodump", L"nodump", 0, UF_NODUMP},
1686 #ifdef EXT2_NODUMP_FL /* 'd' */
1687 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1690 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1693 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1694 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1696 #ifdef EXT2_COMPR_FL /* 'c' */
1697 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1700 #ifdef EXT2_NOATIME_FL /* 'A' */
1701 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1703 { NULL, NULL, 0, 0 }
1708 * Convert file flags to a comma-separated string. If no flags
1709 * are set, return the empty string.
1712 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1720 bits = bitset | bitclear;
1722 for (flag = flags; flag->name != NULL; flag++)
1723 if (bits & (flag->set | flag->clear)) {
1724 length += strlen(flag->name) + 1;
1725 bits &= ~(flag->set | flag->clear);
1730 string = (char *)malloc(length);
1735 for (flag = flags; flag->name != NULL; flag++) {
1736 if (bitset & flag->set || bitclear & flag->clear) {
1737 sp = flag->name + 2;
1738 } else if (bitset & flag->clear || bitclear & flag->set) {
1742 bitset &= ~(flag->set | flag->clear);
1743 bitclear &= ~(flag->set | flag->clear);
1746 while ((*dp++ = *sp++) != '\0')
1757 * Take string of arguments and return file flags. This
1758 * version works a little differently than strtofflags(3).
1759 * In particular, it always tests every token, skipping any
1760 * unrecognized tokens. It returns a pointer to the first
1761 * unrecognized token, or NULL if every token was recognized.
1762 * This version is also const-correct and does not modify the
1766 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1768 const wchar_t *start, *end;
1770 unsigned long set, clear;
1771 const wchar_t *failed;
1776 /* Find start of first token. */
1777 while (*start == L'\t' || *start == L' ' || *start == L',')
1779 while (*start != L'\0') {
1780 /* Locate end of token. */
1782 while (*end != L'\0' && *end != L'\t' &&
1783 *end != L' ' && *end != L',')
1785 for (flag = flags; flag->wname != NULL; flag++) {
1786 if (wmemcmp(start, flag->wname, end - start) == 0) {
1787 /* Matched "noXXXX", so reverse the sense. */
1791 } else if (wmemcmp(start, flag->wname + 2, end - start)
1793 /* Matched "XXXX", so don't reverse. */
1795 clear |= flag->clear;
1799 /* Ignore unknown flag names. */
1800 if (flag->wname == NULL && failed == NULL)
1803 /* Find start of next token. */
1805 while (*start == L'\t' || *start == L' ' || *start == L',')
1815 /* Return location of first failure. */
1823 main(int argc, char **argv)
1825 struct archive_entry *entry = archive_entry_new();
1826 unsigned long set, clear;
1827 const wchar_t *remainder;
1829 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1830 archive_entry_fflags(entry, &set, &clear);
1832 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1834 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));