Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive / archive_entry.c
blob6652b7a20b445aebd89fd6a590815dfab140292d
1 /*-
2 * Copyright (c) 2003-2004 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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry.c,v 1.25 2005/03/13 02:53:42 kientzle Exp $");
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #ifdef HAVE_EXT2FS_EXT2_FS_H
33 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
34 #endif
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wchar.h>
42 #include "archive.h"
43 #include "archive_entry.h"
45 #undef max
46 #define max(a, b) ((a)>(b)?(a):(b))
49 * Handle wide character (i.e., Unicode) and non-wide character
50 * strings transparently.
54 struct aes {
55 const char *aes_mbs;
56 char *aes_mbs_alloc;
57 const wchar_t *aes_wcs;
58 wchar_t *aes_wcs_alloc;
61 struct ae_acl {
62 struct ae_acl *next;
63 int type; /* E.g., access or default */
64 int tag; /* E.g., user/group/other/mask */
65 int permset; /* r/w/x bits */
66 int id; /* uid/gid for user/group */
67 struct aes name; /* uname/gname */
70 static void aes_clean(struct aes *);
71 static void aes_copy(struct aes *dest, struct aes *src);
72 static const char * aes_get_mbs(struct aes *);
73 static const wchar_t * aes_get_wcs(struct aes *);
74 static void aes_set_mbs(struct aes *, const char *mbs);
75 static void aes_copy_mbs(struct aes *, const char *mbs);
76 /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
77 static void aes_copy_wcs(struct aes *, const wchar_t *wcs);
79 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
80 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
81 unsigned long *setp, unsigned long *clrp);
82 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
83 const wchar_t *wname, int perm, int id);
84 static void append_id_w(wchar_t **wp, int id);
86 static int acl_special(struct archive_entry *entry,
87 int type, int permset, int tag);
88 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
89 int type, int permset, int tag, int id);
90 static void next_field_w(const wchar_t **wp, const wchar_t **start,
91 const wchar_t **end, wchar_t *sep);
92 static int prefix_w(const wchar_t *start, const wchar_t *end,
93 const wchar_t *test);
97 * Description of an archive entry.
99 * Basically, this is a "struct stat" with a few text fields added in.
101 * TODO: Add "comment", "charset", and possibly other entries
102 * that are supported by "pax interchange" format. However, GNU, ustar,
103 * cpio, and other variants don't support these features, so they're not an
104 * excruciatingly high priority right now.
106 * TODO: "pax interchange" format allows essentially arbitrary
107 * key/value attributes to be attached to any entry. Supporting
108 * such extensions may make this library useful for special
109 * applications (e.g., a package manager could attach special
110 * package-management attributes to each entry). There are tricky
111 * API issues involved, so this is not going to happen until
112 * there's a real demand for it.
114 * TODO: Design a good API for handling sparse files.
116 struct archive_entry {
118 * Note that ae_stat.st_mode & S_IFMT can be 0!
120 * This occurs when the actual file type of the object is not
121 * in the archive. For example, 'tar' archives store
122 * hardlinks without marking the type of the underlying
123 * object.
125 struct stat ae_stat;
128 * Use aes here so that we get transparent mbs<->wcs conversions.
130 struct aes ae_fflags_text; /* Text fflags per fflagstostr(3) */
131 unsigned long ae_fflags_set; /* Bitmap fflags */
132 unsigned long ae_fflags_clear;
133 struct aes ae_gname; /* Name of owning group */
134 struct aes ae_hardlink; /* Name of target for hardlink */
135 struct aes ae_pathname; /* Name of entry */
136 struct aes ae_symlink; /* symlink contents */
137 struct aes ae_uname; /* Name of owner */
139 struct ae_acl *acl_head;
140 struct ae_acl *acl_p;
141 int acl_state; /* See acl_next for details. */
142 wchar_t *acl_text_w;
145 static void
146 aes_clean(struct aes *aes)
148 if (aes->aes_mbs_alloc) {
149 free(aes->aes_mbs_alloc);
150 aes->aes_mbs_alloc = NULL;
152 if (aes->aes_wcs_alloc) {
153 free(aes->aes_wcs_alloc);
154 aes->aes_wcs_alloc = NULL;
156 memset(aes, 0, sizeof(*aes));
159 static void
160 aes_copy(struct aes *dest, struct aes *src)
162 *dest = *src;
163 if (src->aes_mbs != NULL) {
164 dest->aes_mbs_alloc = strdup(src->aes_mbs);
165 dest->aes_mbs = dest->aes_mbs_alloc;
168 if (src->aes_wcs != NULL) {
169 dest->aes_wcs_alloc = malloc((wcslen(src->aes_wcs) + 1)
170 * sizeof(wchar_t));
171 dest->aes_wcs = dest->aes_wcs_alloc;
172 wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
176 static const char *
177 aes_get_mbs(struct aes *aes)
179 if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
181 * XXX Need to estimate the number of byte in the
182 * multi-byte form. Assume that, on average, wcs
183 * chars encode to no more than 3 bytes. There must
184 * be a better way... XXX
186 int mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
187 aes->aes_mbs_alloc = malloc(mbs_length);
188 aes->aes_mbs = aes->aes_mbs_alloc;
189 wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
190 aes->aes_mbs_alloc[mbs_length - 1] = 0;
192 return (aes->aes_mbs);
195 static const wchar_t *
196 aes_get_wcs(struct aes *aes)
198 if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
200 * No single byte will be more than one wide character,
201 * so this length estimate will always be big enough.
203 int wcs_length = strlen(aes->aes_mbs);
204 aes->aes_wcs_alloc
205 = malloc((wcs_length + 1) * sizeof(wchar_t));
206 aes->aes_wcs = aes->aes_wcs_alloc;
207 mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
208 aes->aes_wcs_alloc[wcs_length] = 0;
210 return (aes->aes_wcs);
213 static void
214 aes_set_mbs(struct aes *aes, const char *mbs)
216 if (aes->aes_mbs_alloc) {
217 free(aes->aes_mbs_alloc);
218 aes->aes_mbs_alloc = NULL;
220 if (aes->aes_wcs_alloc) {
221 free(aes->aes_wcs_alloc);
222 aes->aes_wcs_alloc = NULL;
224 aes->aes_mbs = mbs;
225 aes->aes_wcs = NULL;
228 static void
229 aes_copy_mbs(struct aes *aes, const char *mbs)
231 if (aes->aes_mbs_alloc) {
232 free(aes->aes_mbs_alloc);
233 aes->aes_mbs_alloc = NULL;
235 if (aes->aes_wcs_alloc) {
236 free(aes->aes_wcs_alloc);
237 aes->aes_wcs_alloc = NULL;
239 aes->aes_mbs_alloc = malloc((strlen(mbs) + 1) * sizeof(char));
240 strcpy(aes->aes_mbs_alloc, mbs);
241 aes->aes_mbs = aes->aes_mbs_alloc;
242 aes->aes_wcs = NULL;
245 #if 0
246 static void
247 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
249 if (aes->aes_mbs_alloc) {
250 free(aes->aes_mbs_alloc);
251 aes->aes_mbs_alloc = NULL;
253 if (aes->aes_wcs_alloc) {
254 free(aes->aes_wcs_alloc);
255 aes->aes_wcs_alloc = NULL;
257 aes->aes_mbs = NULL;
258 aes->aes_wcs = wcs;
260 #endif
262 static void
263 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
265 if (aes->aes_mbs_alloc) {
266 free(aes->aes_mbs_alloc);
267 aes->aes_mbs_alloc = NULL;
269 if (aes->aes_wcs_alloc) {
270 free(aes->aes_wcs_alloc);
271 aes->aes_wcs_alloc = NULL;
273 aes->aes_mbs = NULL;
274 aes->aes_wcs_alloc = malloc((wcslen(wcs) + 1) * sizeof(wchar_t));
275 wcscpy(aes->aes_wcs_alloc, wcs);
276 aes->aes_wcs = aes->aes_wcs_alloc;
279 struct archive_entry *
280 archive_entry_clear(struct archive_entry *entry)
282 aes_clean(&entry->ae_fflags_text);
283 aes_clean(&entry->ae_gname);
284 aes_clean(&entry->ae_hardlink);
285 aes_clean(&entry->ae_pathname);
286 aes_clean(&entry->ae_symlink);
287 aes_clean(&entry->ae_uname);
288 archive_entry_acl_clear(entry);
289 memset(entry, 0, sizeof(*entry));
290 return entry;
293 struct archive_entry *
294 archive_entry_clone(struct archive_entry *entry)
296 struct archive_entry *entry2;
298 /* Allocate new structure and copy over all of the fields. */
299 entry2 = malloc(sizeof(*entry2));
300 if(entry2 == NULL)
301 return (NULL);
302 memset(entry2, 0, sizeof(*entry2));
303 entry2->ae_stat = entry->ae_stat;
304 entry2->ae_fflags_set = entry->ae_fflags_set;
305 entry2->ae_fflags_clear = entry->ae_fflags_clear;
307 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
308 aes_copy(&entry2->ae_gname, &entry->ae_gname);
309 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
310 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
311 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
312 aes_copy(&entry2->ae_uname, &entry->ae_uname);
314 /* XXX TODO: Copy ACL data over as well. XXX */
315 return (entry2);
318 void
319 archive_entry_free(struct archive_entry *entry)
321 archive_entry_clear(entry);
322 free(entry);
325 struct archive_entry *
326 archive_entry_new(void)
328 struct archive_entry *entry;
330 entry = malloc(sizeof(*entry));
331 if(entry == NULL)
332 return (NULL);
333 memset(entry, 0, sizeof(*entry));
334 return (entry);
338 * Functions for reading fields from an archive_entry.
341 time_t
342 archive_entry_atime(struct archive_entry *entry)
344 return (entry->ae_stat.st_atime);
347 long
348 archive_entry_atime_nsec(struct archive_entry *entry)
350 (void)entry; /* entry can be unused here. */
351 return (ARCHIVE_STAT_ATIME_NANOS(&entry->ae_stat));
354 dev_t
355 archive_entry_dev(struct archive_entry *entry)
357 return (entry->ae_stat.st_dev);
360 void
361 archive_entry_fflags(struct archive_entry *entry,
362 unsigned long *set, unsigned long *clear)
364 *set = entry->ae_fflags_set;
365 *clear = entry->ae_fflags_clear;
369 * Note: if text was provided, this just returns that text. If you
370 * really need the text to be rebuilt in a canonical form, set the
371 * text, ask for the bitmaps, then set the bitmaps. (Setting the
372 * bitmaps clears any stored text.) This design is deliberate: if
373 * we're editing archives, we don't want to discard flags just because
374 * they aren't supported on the current system. The bitmap<->text
375 * conversions are platform-specific (see below).
377 const char *
378 archive_entry_fflags_text(struct archive_entry *entry)
380 const char *f;
381 char *p;
383 f = aes_get_mbs(&entry->ae_fflags_text);
384 if (f != NULL)
385 return (f);
387 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
388 return (NULL);
390 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
391 if (p == NULL)
392 return (NULL);
394 aes_copy_mbs(&entry->ae_fflags_text, p);
395 free(p);
396 f = aes_get_mbs(&entry->ae_fflags_text);
397 return (f);
400 gid_t
401 archive_entry_gid(struct archive_entry *entry)
403 return (entry->ae_stat.st_gid);
406 const char *
407 archive_entry_gname(struct archive_entry *entry)
409 return (aes_get_mbs(&entry->ae_gname));
412 const char *
413 archive_entry_hardlink(struct archive_entry *entry)
415 return (aes_get_mbs(&entry->ae_hardlink));
418 ino_t
419 archive_entry_ino(struct archive_entry *entry)
421 return (entry->ae_stat.st_ino);
424 mode_t
425 archive_entry_mode(struct archive_entry *entry)
427 return (entry->ae_stat.st_mode);
430 time_t
431 archive_entry_mtime(struct archive_entry *entry)
433 return (entry->ae_stat.st_mtime);
436 long
437 archive_entry_mtime_nsec(struct archive_entry *entry)
439 (void)entry; /* entry can be unused here. */
440 return (ARCHIVE_STAT_MTIME_NANOS(&entry->ae_stat));
443 const char *
444 archive_entry_pathname(struct archive_entry *entry)
446 return (aes_get_mbs(&entry->ae_pathname));
449 const wchar_t *
450 archive_entry_pathname_w(struct archive_entry *entry)
452 return (aes_get_wcs(&entry->ae_pathname));
455 dev_t
456 archive_entry_rdev(struct archive_entry *entry)
458 return (entry->ae_stat.st_rdev);
461 dev_t
462 archive_entry_rdevmajor(struct archive_entry *entry)
464 return (major(entry->ae_stat.st_rdev));
467 dev_t
468 archive_entry_rdevminor(struct archive_entry *entry)
470 return (minor(entry->ae_stat.st_rdev));
473 int64_t
474 archive_entry_size(struct archive_entry *entry)
476 return (entry->ae_stat.st_size);
479 const struct stat *
480 archive_entry_stat(struct archive_entry *entry)
482 return (&entry->ae_stat);
485 const char *
486 archive_entry_symlink(struct archive_entry *entry)
488 return (aes_get_mbs(&entry->ae_symlink));
491 uid_t
492 archive_entry_uid(struct archive_entry *entry)
494 return (entry->ae_stat.st_uid);
497 const char *
498 archive_entry_uname(struct archive_entry *entry)
500 return (aes_get_mbs(&entry->ae_uname));
504 * Functions to set archive_entry properties.
508 * Note "copy" not "set" here. The "set" functions that accept a pointer
509 * only store the pointer; they don't copy the underlying object.
511 void
512 archive_entry_copy_stat(struct archive_entry *entry, const struct stat *st)
514 entry->ae_stat = *st;
517 void
518 archive_entry_set_fflags(struct archive_entry *entry,
519 unsigned long set, unsigned long clear)
521 aes_clean(&entry->ae_fflags_text);
522 entry->ae_fflags_set = set;
523 entry->ae_fflags_clear = clear;
526 const wchar_t *
527 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
528 const wchar_t *flags)
530 aes_copy_wcs(&entry->ae_fflags_text, flags);
531 return (ae_wcstofflags(flags,
532 &entry->ae_fflags_set, &entry->ae_fflags_clear));
535 void
536 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
538 entry->ae_stat.st_gid = g;
541 void
542 archive_entry_set_gname(struct archive_entry *entry, const char *name)
544 aes_set_mbs(&entry->ae_gname, name);
547 void
548 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
550 aes_copy_wcs(&entry->ae_gname, name);
553 void
554 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
556 aes_set_mbs(&entry->ae_hardlink, target);
559 void
560 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
562 aes_copy_mbs(&entry->ae_hardlink, target);
565 void
566 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
568 aes_copy_wcs(&entry->ae_hardlink, target);
571 /* Set symlink if symlink is already set, else set hardlink. */
572 void
573 archive_entry_set_link(struct archive_entry *entry, const char *target)
575 if (entry->ae_symlink.aes_mbs != NULL ||
576 entry->ae_symlink.aes_wcs != NULL)
577 aes_set_mbs(&entry->ae_symlink, target);
578 aes_set_mbs(&entry->ae_hardlink, target);
581 void
582 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
584 entry->ae_stat.st_mode = m;
587 void
588 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
590 entry->ae_stat.st_mtime = m;
591 ARCHIVE_STAT_SET_MTIME_NANOS(&entry->ae_stat, ns);
594 void
595 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
597 aes_set_mbs(&entry->ae_pathname, name);
600 void
601 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
603 aes_copy_mbs(&entry->ae_pathname, name);
606 void
607 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
609 aes_copy_wcs(&entry->ae_pathname, name);
612 void
613 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
615 dev_t d;
617 d = entry->ae_stat.st_rdev;
618 entry->ae_stat.st_rdev = makedev(major(m), minor(d));
621 void
622 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
624 dev_t d;
626 d = entry->ae_stat.st_rdev;
627 entry->ae_stat.st_rdev = makedev(major(d), minor(m));
630 void
631 archive_entry_set_size(struct archive_entry *entry, int64_t s)
633 entry->ae_stat.st_size = s;
636 void
637 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
639 aes_set_mbs(&entry->ae_symlink, linkname);
642 void
643 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
645 aes_copy_wcs(&entry->ae_symlink, linkname);
648 void
649 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
651 entry->ae_stat.st_uid = u;
654 void
655 archive_entry_set_uname(struct archive_entry *entry, const char *name)
657 aes_set_mbs(&entry->ae_uname, name);
660 void
661 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
663 aes_copy_wcs(&entry->ae_uname, name);
667 * ACL management. The following would, of course, be a lot simpler
668 * if: 1) the last draft of POSIX.1e were a really thorough and
669 * complete standard that addressed the needs of ACL archiving and 2)
670 * everyone followed it faithfully. Alas, neither is true, so the
671 * following is a lot more complex than might seem necessary to the
672 * uninitiated.
675 void
676 archive_entry_acl_clear(struct archive_entry *entry)
678 struct ae_acl *ap;
680 while (entry->acl_head != NULL) {
681 ap = entry->acl_head->next;
682 aes_clean(&entry->acl_head->name);
683 free(entry->acl_head);
684 entry->acl_head = ap;
686 if (entry->acl_text_w != NULL) {
687 free(entry->acl_text_w);
688 entry->acl_text_w = NULL;
690 entry->acl_p = NULL;
691 entry->acl_state = 0; /* Not counting. */
695 * Add a single ACL entry to the internal list of ACL data.
697 void
698 archive_entry_acl_add_entry(struct archive_entry *entry,
699 int type, int permset, int tag, int id, const char *name)
701 struct ae_acl *ap;
703 if (acl_special(entry, type, permset, tag) == 0)
704 return;
705 ap = acl_new_entry(entry, type, permset, tag, id);
706 if (ap == NULL) {
707 /* XXX Error XXX */
708 return;
710 if (name != NULL && *name != '\0')
711 aes_copy_mbs(&ap->name, name);
712 else
713 aes_clean(&ap->name);
717 * As above, but with a wide-character name.
719 void
720 archive_entry_acl_add_entry_w(struct archive_entry *entry,
721 int type, int permset, int tag, int id, const wchar_t *name)
723 struct ae_acl *ap;
725 if (acl_special(entry, type, permset, tag) == 0)
726 return;
727 ap = acl_new_entry(entry, type, permset, tag, id);
728 if (ap == NULL) {
729 /* XXX Error XXX */
730 return;
732 if (name != NULL && *name != L'\0')
733 aes_copy_wcs(&ap->name, name);
734 else
735 aes_clean(&ap->name);
739 * If this ACL entry is part of the standard POSIX permissions set,
740 * store the permissions in the stat structure and return zero.
742 static int
743 acl_special(struct archive_entry *entry, int type, int permset, int tag)
745 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
746 switch (tag) {
747 case ARCHIVE_ENTRY_ACL_USER_OBJ:
748 entry->ae_stat.st_mode &= ~0700;
749 entry->ae_stat.st_mode |= (permset & 7) << 6;
750 return (0);
751 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
752 entry->ae_stat.st_mode &= ~0070;
753 entry->ae_stat.st_mode |= (permset & 7) << 3;
754 return (0);
755 case ARCHIVE_ENTRY_ACL_OTHER:
756 entry->ae_stat.st_mode &= ~0007;
757 entry->ae_stat.st_mode |= permset & 7;
758 return (0);
761 return (1);
765 * Allocate and populate a new ACL entry with everything but the
766 * name.
768 static struct ae_acl *
769 acl_new_entry(struct archive_entry *entry,
770 int type, int permset, int tag, int id)
772 struct ae_acl *ap;
774 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
775 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
776 return (NULL);
777 if (entry->acl_text_w != NULL) {
778 free(entry->acl_text_w);
779 entry->acl_text_w = NULL;
782 /* XXX TODO: More sanity-checks on the arguments XXX */
784 /* If there's a matching entry already in the list, overwrite it. */
785 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
786 if (ap->type == type && ap->tag == tag && ap->id == id) {
787 ap->permset = permset;
788 return (ap);
792 /* Add a new entry to the list. */
793 ap = malloc(sizeof(*ap));
794 memset(ap, 0, sizeof(*ap));
795 ap->next = entry->acl_head;
796 entry->acl_head = ap;
797 ap->type = type;
798 ap->tag = tag;
799 ap->id = id;
800 ap->permset = permset;
801 return (ap);
805 * Return a count of entries matching "want_type".
808 archive_entry_acl_count(struct archive_entry *entry, int want_type)
810 int count;
811 struct ae_acl *ap;
813 count = 0;
814 ap = entry->acl_head;
815 while (ap != NULL) {
816 if ((ap->type & want_type) != 0)
817 count++;
818 ap = ap->next;
821 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
822 count += 3;
823 return (count);
827 * Prepare for reading entries from the ACL data. Returns a count
828 * of entries matching "want_type", or zero if there are no
829 * non-extended ACL entries of that type.
832 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
834 int count, cutoff;
836 count = archive_entry_acl_count(entry, want_type);
839 * If the only entries are the three standard ones,
840 * then don't return any ACL data. (In this case,
841 * client can just use chmod(2) to set permissions.)
843 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
844 cutoff = 3;
845 else
846 cutoff = 0;
848 if (count > cutoff)
849 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
850 else
851 entry->acl_state = 0;
852 entry->acl_p = entry->acl_head;
853 return (count);
857 * Return the next ACL entry in the list. Fake entries for the
858 * standard permissions and include them in the returned list.
862 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
863 int *permset, int *tag, int *id, const char **name)
865 *name = NULL;
866 *id = -1;
869 * The acl_state is either zero (no entries available), -1
870 * (reading from list), or an entry type (retrieve that type
871 * from ae_stat.st_mode).
873 if (entry->acl_state == 0)
874 return (ARCHIVE_WARN);
876 /* The first three access entries are special. */
877 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
878 switch (entry->acl_state) {
879 case ARCHIVE_ENTRY_ACL_USER_OBJ:
880 *permset = (entry->ae_stat.st_mode >> 6) & 7;
881 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
882 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
883 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
884 return (ARCHIVE_OK);
885 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
886 *permset = (entry->ae_stat.st_mode >> 3) & 7;
887 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
888 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
889 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
890 return (ARCHIVE_OK);
891 case ARCHIVE_ENTRY_ACL_OTHER:
892 *permset = entry->ae_stat.st_mode & 7;
893 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
894 *tag = ARCHIVE_ENTRY_ACL_OTHER;
895 entry->acl_state = -1;
896 entry->acl_p = entry->acl_head;
897 return (ARCHIVE_OK);
898 default:
899 break;
903 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
904 entry->acl_p = entry->acl_p->next;
905 if (entry->acl_p == NULL) {
906 entry->acl_state = 0;
907 return (ARCHIVE_WARN);
909 *type = entry->acl_p->type;
910 *permset = entry->acl_p->permset;
911 *tag = entry->acl_p->tag;
912 *id = entry->acl_p->id;
913 *name = aes_get_mbs(&entry->acl_p->name);
914 entry->acl_p = entry->acl_p->next;
915 return (ARCHIVE_OK);
919 * Generate a text version of the ACL. The flags parameter controls
920 * the style of the generated ACL.
922 const wchar_t *
923 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
925 int count;
926 int length;
927 const wchar_t *wname;
928 const wchar_t *prefix;
929 wchar_t separator;
930 struct ae_acl *ap;
931 int id;
932 wchar_t *wp;
934 if (entry->acl_text_w != NULL) {
935 free (entry->acl_text_w);
936 entry->acl_text_w = NULL;
939 separator = L',';
940 count = 0;
941 length = 0;
942 ap = entry->acl_head;
943 while (ap != NULL) {
944 if ((ap->type & flags) != 0) {
945 count++;
946 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
947 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
948 length += 8; /* "default:" */
949 length += 5; /* tag name */
950 length += 1; /* colon */
951 wname = aes_get_wcs(&ap->name);
952 if (wname != NULL)
953 length += wcslen(wname);
954 length ++; /* colon */
955 length += 3; /* rwx */
956 length += 1; /* colon */
957 length += max(sizeof(uid_t),sizeof(gid_t)) * 3 + 1;
958 length ++; /* newline */
960 ap = ap->next;
963 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
964 length += 10; /* "user::rwx\n" */
965 length += 11; /* "group::rwx\n" */
966 length += 11; /* "other::rwx\n" */
969 if (count == 0)
970 return (NULL);
972 /* Now, allocate the string and actually populate it. */
973 wp = entry->acl_text_w = malloc(length * sizeof(wchar_t));
974 count = 0;
975 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
976 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
977 entry->ae_stat.st_mode & 0700, -1);
978 *wp++ = ',';
979 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
980 entry->ae_stat.st_mode & 0070, -1);
981 *wp++ = ',';
982 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
983 entry->ae_stat.st_mode & 0007, -1);
984 count += 3;
986 ap = entry->acl_head;
987 while (ap != NULL) {
988 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
989 wname = aes_get_wcs(&ap->name);
990 *wp++ = separator;
991 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
992 id = ap->id;
993 else
994 id = -1;
995 append_entry_w(&wp, NULL, ap->tag, wname,
996 ap->permset, id);
997 count++;
999 ap = ap->next;
1004 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1005 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1006 prefix = L"default:";
1007 else
1008 prefix = NULL;
1009 ap = entry->acl_head;
1010 count = 0;
1011 while (ap != NULL) {
1012 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1013 wname = aes_get_wcs(&ap->name);
1014 if (count > 0)
1015 *wp++ = separator;
1016 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1017 id = ap->id;
1018 else
1019 id = -1;
1020 append_entry_w(&wp, prefix, ap->tag,
1021 wname, ap->permset, id);
1022 count ++;
1024 ap = ap->next;
1028 return (entry->acl_text_w);
1031 static void
1032 append_id_w(wchar_t **wp, int id)
1034 if (id > 9)
1035 append_id_w(wp, id / 10);
1036 *(*wp)++ = L"0123456789"[id % 10];
1039 static void
1040 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1041 const wchar_t *wname, int perm, int id)
1043 if (prefix != NULL) {
1044 wcscpy(*wp, prefix);
1045 *wp += wcslen(*wp);
1047 switch (tag) {
1048 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1049 wname = NULL;
1050 id = -1;
1051 /* FALL THROUGH */
1052 case ARCHIVE_ENTRY_ACL_USER:
1053 wcscpy(*wp, L"user");
1054 break;
1055 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1056 wname = NULL;
1057 id = -1;
1058 /* FALL THROUGH */
1059 case ARCHIVE_ENTRY_ACL_GROUP:
1060 wcscpy(*wp, L"group");
1061 break;
1062 case ARCHIVE_ENTRY_ACL_MASK:
1063 wcscpy(*wp, L"mask");
1064 wname = NULL;
1065 id = -1;
1066 break;
1067 case ARCHIVE_ENTRY_ACL_OTHER:
1068 wcscpy(*wp, L"other");
1069 wname = NULL;
1070 id = -1;
1071 break;
1073 *wp += wcslen(*wp);
1074 *(*wp)++ = L':';
1075 if (wname != NULL) {
1076 wcscpy(*wp, wname);
1077 *wp += wcslen(*wp);
1079 *(*wp)++ = L':';
1080 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1081 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1082 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1083 if (id != -1) {
1084 *(*wp)++ = L':';
1085 append_id_w(wp, id);
1087 **wp = L'\0';
1091 * Parse a textual ACL. This automatically recognizes and supports
1092 * extensions described above. The 'type' argument is used to
1093 * indicate the type that should be used for any entries not
1094 * explicitly marked as "default:".
1097 __archive_entry_acl_parse_w(struct archive_entry *entry,
1098 const wchar_t *text, int default_type)
1100 int type, tag, permset, id;
1101 const wchar_t *start, *end;
1102 const wchar_t *name_start, *name_end;
1103 wchar_t sep;
1104 wchar_t *namebuff;
1105 int namebuff_length;
1107 name_start = name_end = NULL;
1108 namebuff = NULL;
1109 namebuff_length = 0;
1111 while (text != NULL && *text != L'\0') {
1112 next_field_w(&text, &start, &end, &sep);
1113 if (sep != L':')
1114 goto fail;
1117 * Solaris extension: "defaultuser::rwx" is the
1118 * default ACL corresponding to "user::rwx", etc.
1120 if (end-start > 7 && wmemcmp(start, L"default", 7) == 0) {
1121 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1122 start += 7;
1123 } else
1124 type = default_type;
1126 if (prefix_w(start, end, L"user")) {
1127 next_field_w(&text, &start, &end, &sep);
1128 if (sep != L':')
1129 goto fail;
1130 if (end > start) {
1131 tag = ARCHIVE_ENTRY_ACL_USER;
1132 name_start = start;
1133 name_end = end;
1134 } else
1135 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1136 } else if (prefix_w(start, end, L"group")) {
1137 next_field_w(&text, &start, &end, &sep);
1138 if (sep != L':')
1139 goto fail;
1140 if (end > start) {
1141 tag = ARCHIVE_ENTRY_ACL_GROUP;
1142 name_start = start;
1143 name_end = end;
1144 } else
1145 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1146 } else if (prefix_w(start, end, L"other")) {
1147 next_field_w(&text, &start, &end, &sep);
1148 if (sep != L':')
1149 goto fail;
1150 if (end > start)
1151 goto fail;
1152 tag = ARCHIVE_ENTRY_ACL_OTHER;
1153 } else if (prefix_w(start, end, L"mask")) {
1154 next_field_w(&text, &start, &end, &sep);
1155 if (sep != L':')
1156 goto fail;
1157 if (end > start)
1158 goto fail;
1159 tag = ARCHIVE_ENTRY_ACL_MASK;
1160 } else
1161 goto fail;
1163 next_field_w(&text, &start, &end, &sep);
1164 permset = 0;
1165 while (start < end) {
1166 switch (*start++) {
1167 case 'r': case 'R':
1168 permset |= ARCHIVE_ENTRY_ACL_READ;
1169 break;
1170 case 'w': case 'W':
1171 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1172 break;
1173 case 'x': case 'X':
1174 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1175 break;
1176 case '-':
1177 break;
1178 default:
1179 goto fail;
1184 * Support star-compatible numeric UID/GID extension.
1185 * This extension adds a ":" followed by the numeric
1186 * ID so that "group:groupname:rwx", for example,
1187 * becomes "group:groupname:rwx:999", where 999 is the
1188 * numeric GID. This extension makes it possible, for
1189 * example, to correctly restore ACLs on a system that
1190 * might have a damaged passwd file or be disconnected
1191 * from a central NIS server. This extension is compatible
1192 * with POSIX.1e draft 17.
1194 if (sep == L':' && (tag == ARCHIVE_ENTRY_ACL_USER ||
1195 tag == ARCHIVE_ENTRY_ACL_GROUP)) {
1196 next_field_w(&text, &start, &end, &sep);
1198 id = 0;
1199 while (start < end && *start >= '0' && *start <= '9') {
1200 if (id > (INT_MAX / 10))
1201 id = INT_MAX;
1202 else {
1203 id *= 10;
1204 id += *start - '0';
1205 start++;
1208 } else
1209 id = -1; /* No id specified. */
1211 /* Skip any additional entries. */
1212 while (sep == L':') {
1213 next_field_w(&text, &start, &end, &sep);
1216 /* Add entry to the internal list. */
1217 if (name_end == name_start) {
1218 archive_entry_acl_add_entry_w(entry, type, permset,
1219 tag, id, NULL);
1220 } else {
1221 if (namebuff_length <= name_end - name_start) {
1222 if (namebuff != NULL)
1223 free(namebuff);
1224 namebuff_length = name_end - name_start + 256;
1225 namebuff =
1226 malloc(namebuff_length * sizeof(wchar_t));
1228 wmemcpy(namebuff, name_start, name_end - name_start);
1229 namebuff[name_end - name_start] = L'\0';
1230 archive_entry_acl_add_entry_w(entry, type,
1231 permset, tag, id, namebuff);
1234 if (namebuff != NULL)
1235 free(namebuff);
1236 return (ARCHIVE_OK);
1238 fail:
1239 if (namebuff != NULL)
1240 free(namebuff);
1241 return (ARCHIVE_WARN);
1245 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1246 * to point to just after the separator. *start points to the first
1247 * character of the matched text and *end just after the last
1248 * character of the matched identifier. In particular *end - *start
1249 * is the length of the field body, not including leading or trailing
1250 * whitespace.
1252 static void
1253 next_field_w(const wchar_t **wp, const wchar_t **start,
1254 const wchar_t **end, wchar_t *sep)
1256 /* Skip leading whitespace to find start of field. */
1257 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1258 (*wp)++;
1260 *start = *wp;
1262 /* Scan for the separator. */
1263 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1264 **wp != L'\n') {
1265 (*wp)++;
1267 *sep = **wp;
1269 /* Trim trailing whitespace to locate end of field. */
1270 *end = *wp - 1;
1271 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1272 (*end)--;
1274 (*end)++;
1276 /* Adjust scanner location. */
1277 if (**wp != L'\0')
1278 (*wp)++;
1281 static int
1282 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1284 if (start == end)
1285 return (0);
1287 if (*start++ != *test++)
1288 return (0);
1290 while (start < end && *start++ == *test++)
1293 if (start < end)
1294 return (0);
1296 return (1);
1301 * Following code is modified from UC Berkeley sources, and
1302 * is subject to the following copyright notice.
1306 * Copyright (c) 1993
1307 * The Regents of the University of California. All rights reserved.
1309 * Redistribution and use in source and binary forms, with or without
1310 * modification, are permitted provided that the following conditions
1311 * are met:
1312 * 1. Redistributions of source code must retain the above copyright
1313 * notice, this list of conditions and the following disclaimer.
1314 * 2. Redistributions in binary form must reproduce the above copyright
1315 * notice, this list of conditions and the following disclaimer in the
1316 * documentation and/or other materials provided with the distribution.
1317 * 4. Neither the name of the University nor the names of its contributors
1318 * may be used to endorse or promote products derived from this software
1319 * without specific prior written permission.
1321 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1322 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1323 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1324 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1325 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1326 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1327 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1328 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1329 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1330 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1331 * SUCH DAMAGE.
1334 static struct flag {
1335 const char *name;
1336 const wchar_t *wname;
1337 unsigned long set;
1338 unsigned long clear;
1339 } flags[] = {
1340 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1341 #ifdef SF_APPEND
1342 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1343 { "nosappend", L"nosappend", SF_APPEND, 0 },
1344 #endif
1345 #ifdef EXT2_APPEND_FL /* 'a' */
1346 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1347 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1348 #endif
1349 #ifdef SF_ARCHIVED
1350 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1351 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1352 #endif
1353 #ifdef SF_IMMUTABLE
1354 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1355 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1356 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1357 #endif
1358 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1359 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1360 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1361 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1362 #endif
1363 #ifdef SF_NOUNLINK
1364 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1365 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1366 #endif
1367 #ifdef SF_SNAPSHOT
1368 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1369 #endif
1370 #ifdef UF_APPEND
1371 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1372 { "nouappend", L"nouappend", UF_APPEND, 0 },
1373 #endif
1374 #ifdef UF_IMMUTABLE
1375 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1376 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1377 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1378 #endif
1379 #ifdef UF_NODUMP
1380 { "nodump", L"nodump", 0, UF_NODUMP},
1381 #endif
1382 #ifdef EXT2_NODUMP_FL /* 'd' */
1383 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1384 #endif
1385 #ifdef UF_OPAQUE
1386 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1387 #endif
1388 #ifdef UF_NOUNLINK
1389 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1390 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1391 #endif
1392 #ifdef EXT2_COMPR_FL /* 'c' */
1393 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1394 #endif
1396 #ifdef EXT2_NOATIME_FL /* 'A' */
1397 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1398 #endif
1399 { NULL, NULL, 0, 0 }
1403 * fflagstostr --
1404 * Convert file flags to a comma-separated string. If no flags
1405 * are set, return the empty string.
1407 char *
1408 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1410 char *string, *dp;
1411 const char *sp;
1412 unsigned long bits;
1413 struct flag *flag;
1414 int length;
1416 bits = bitset | bitclear;
1417 length = 0;
1418 for (flag = flags; flag->name != NULL; flag++)
1419 if (bits & (flag->set | flag->clear)) {
1420 length += strlen(flag->name) + 1;
1421 bits &= ~(flag->set | flag->clear);
1424 if (length == 0)
1425 return (NULL);
1426 string = malloc(length);
1427 if (string == NULL)
1428 return (NULL);
1430 dp = string;
1431 for (flag = flags; flag->name != NULL; flag++) {
1432 if (bitset & flag->set || bitclear & flag->clear) {
1433 sp = flag->name + 2;
1434 } else if (bitset & flag->clear || bitclear & flag->set) {
1435 sp = flag->name;
1436 } else
1437 continue;
1438 bitset &= ~(flag->set | flag->clear);
1439 bitclear &= ~(flag->set | flag->clear);
1440 if (dp > string)
1441 *dp++ = ',';
1442 while ((*dp++ = *sp++) != '\0')
1444 dp--;
1447 *dp = '\0';
1448 return (string);
1452 * wcstofflags --
1453 * Take string of arguments and return file flags. This
1454 * version works a little differently than strtofflags(3).
1455 * In particular, it always tests every token, skipping any
1456 * unrecognized tokens. It returns a pointer to the first
1457 * unrecognized token, or NULL if every token was recognized.
1458 * This version is also const-correct and does not modify the
1459 * provided string.
1461 const wchar_t *
1462 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1464 const wchar_t *start, *end;
1465 struct flag *flag;
1466 unsigned long set, clear;
1467 const wchar_t *failed;
1469 set = clear = 0;
1470 start = s;
1471 failed = NULL;
1472 /* Find start of first token. */
1473 while (*start == L'\t' || *start == L' ' || *start == L',')
1474 start++;
1475 while (*start != L'\0') {
1476 /* Locate end of token. */
1477 end = start;
1478 while (*end != L'\0' && *end != L'\t' &&
1479 *end != L' ' && *end != L',')
1480 end++;
1481 for (flag = flags; flag->wname != NULL; flag++) {
1482 if (wmemcmp(start, flag->wname, end - start) == 0) {
1483 /* Matched "noXXXX", so reverse the sense. */
1484 clear |= flag->set;
1485 set |= flag->clear;
1486 break;
1487 } else if (wmemcmp(start, flag->wname + 2, end - start)
1488 == 0) {
1489 /* Matched "XXXX", so don't reverse. */
1490 set |= flag->set;
1491 clear |= flag->clear;
1492 break;
1495 /* Ignore unknown flag names. */
1496 if (flag->wname == NULL && failed == NULL)
1497 failed = start;
1499 /* Find start of next token. */
1500 start = end;
1501 while (*start == L'\t' || *start == L' ' || *start == L',')
1502 start++;
1506 if (setp)
1507 *setp = set;
1508 if (clrp)
1509 *clrp = clear;
1511 /* Return location of first failure. */
1512 return (failed);
1516 #ifdef TEST
1517 #include <stdio.h>
1519 main(int argc, char **argv)
1521 struct archive_entry *entry = archive_entry_new();
1522 unsigned long set, clear;
1523 const wchar_t *remainder;
1525 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1526 archive_entry_fflags(entry, &set, &clear);
1528 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1530 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
1531 return (0);
1533 #endif