spl->critical section conversion.
[dragonfly.git] / contrib / libarchive / archive_entry.c
1 /*-
2  * Copyright (c) 2003-2004 Tim Kientzle
3  * All rights reserved.
4  *
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.
14  *
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.
25  */
26
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 $");
29
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>
40
41
42 #include "archive.h"
43 #include "archive_entry.h"
44
45 #undef max
46 #define max(a, b)       ((a)>(b)?(a):(b))
47
48 /*
49  * Handle wide character (i.e., Unicode) and non-wide character
50  * strings transparently.
51  *
52  */
53
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;
59 };
60
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 */
68 };
69
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);
78
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);
85
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);
94
95
96 /*
97  * Description of an archive entry.
98  *
99  * Basically, this is a "struct stat" with a few text fields added in.
100  *
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.
105  *
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.
113  *
114  * TODO: Design a good API for handling sparse files.
115  */
116 struct archive_entry {
117         /*
118          * Note that ae_stat.st_mode & S_IFMT  can be  0!
119          *
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.
124          */
125         struct stat ae_stat;
126
127         /*
128          * Use aes here so that we get transparent mbs<->wcs conversions.
129          */
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 */
138
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;
143 };
144
145 static void
146 aes_clean(struct aes *aes)
147 {
148         if (aes->aes_mbs_alloc) {
149                 free(aes->aes_mbs_alloc);
150                 aes->aes_mbs_alloc = NULL;
151         }
152         if (aes->aes_wcs_alloc) {
153                 free(aes->aes_wcs_alloc);
154                 aes->aes_wcs_alloc = NULL;
155         }
156         memset(aes, 0, sizeof(*aes));
157 }
158
159 static void
160 aes_copy(struct aes *dest, struct aes *src)
161 {
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;
166         }
167
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);
173         }
174 }
175
176 static const char *
177 aes_get_mbs(struct aes *aes)
178 {
179         if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
180                 /*
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
185                  */
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;
191         }
192         return (aes->aes_mbs);
193 }
194
195 static const wchar_t *
196 aes_get_wcs(struct aes *aes)
197 {
198         if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
199                 /*
200                  * No single byte will be more than one wide character,
201                  * so this length estimate will always be big enough.
202                  */
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;
209         }
210         return (aes->aes_wcs);
211 }
212
213 static void
214 aes_set_mbs(struct aes *aes, const char *mbs)
215 {
216         if (aes->aes_mbs_alloc) {
217                 free(aes->aes_mbs_alloc);
218                 aes->aes_mbs_alloc = NULL;
219         }
220         if (aes->aes_wcs_alloc) {
221                 free(aes->aes_wcs_alloc);
222                 aes->aes_wcs_alloc = NULL;
223         }
224         aes->aes_mbs = mbs;
225         aes->aes_wcs = NULL;
226 }
227
228 static void
229 aes_copy_mbs(struct aes *aes, const char *mbs)
230 {
231         if (aes->aes_mbs_alloc) {
232                 free(aes->aes_mbs_alloc);
233                 aes->aes_mbs_alloc = NULL;
234         }
235         if (aes->aes_wcs_alloc) {
236                 free(aes->aes_wcs_alloc);
237                 aes->aes_wcs_alloc = NULL;
238         }
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;
243 }
244
245 #if 0
246 static void
247 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
248 {
249         if (aes->aes_mbs_alloc) {
250                 free(aes->aes_mbs_alloc);
251                 aes->aes_mbs_alloc = NULL;
252         }
253         if (aes->aes_wcs_alloc) {
254                 free(aes->aes_wcs_alloc);
255                 aes->aes_wcs_alloc = NULL;
256         }
257         aes->aes_mbs = NULL;
258         aes->aes_wcs = wcs;
259 }
260 #endif
261
262 static void
263 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
264 {
265         if (aes->aes_mbs_alloc) {
266                 free(aes->aes_mbs_alloc);
267                 aes->aes_mbs_alloc = NULL;
268         }
269         if (aes->aes_wcs_alloc) {
270                 free(aes->aes_wcs_alloc);
271                 aes->aes_wcs_alloc = NULL;
272         }
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;
277 }
278
279 struct archive_entry *
280 archive_entry_clear(struct archive_entry *entry)
281 {
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;
291 }
292
293 struct archive_entry *
294 archive_entry_clone(struct archive_entry *entry)
295 {
296         struct archive_entry *entry2;
297
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;
306
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);
313
314         /* XXX TODO: Copy ACL data over as well. XXX */
315         return (entry2);
316 }
317
318 void
319 archive_entry_free(struct archive_entry *entry)
320 {
321         archive_entry_clear(entry);
322         free(entry);
323 }
324
325 struct archive_entry *
326 archive_entry_new(void)
327 {
328         struct archive_entry *entry;
329
330         entry = malloc(sizeof(*entry));
331         if(entry == NULL)
332                 return (NULL);
333         memset(entry, 0, sizeof(*entry));
334         return (entry);
335 }
336
337 /*
338  * Functions for reading fields from an archive_entry.
339  */
340
341 time_t
342 archive_entry_atime(struct archive_entry *entry)
343 {
344         return (entry->ae_stat.st_atime);
345 }
346
347 long
348 archive_entry_atime_nsec(struct archive_entry *entry)
349 {
350         (void)entry; /* entry can be unused here. */
351         return (ARCHIVE_STAT_ATIME_NANOS(&entry->ae_stat));
352 }
353
354 dev_t
355 archive_entry_dev(struct archive_entry *entry)
356 {
357         return (entry->ae_stat.st_dev);
358 }
359
360 void
361 archive_entry_fflags(struct archive_entry *entry,
362     unsigned long *set, unsigned long *clear)
363 {
364         *set = entry->ae_fflags_set;
365         *clear = entry->ae_fflags_clear;
366 }
367
368 /*
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).
376  */
377 const char *
378 archive_entry_fflags_text(struct archive_entry *entry)
379 {
380         const char *f;
381         char *p;
382
383         f = aes_get_mbs(&entry->ae_fflags_text);
384         if (f != NULL)
385                 return (f);
386
387         if (entry->ae_fflags_set == 0  &&  entry->ae_fflags_clear == 0)
388                 return (NULL);
389
390         p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
391         if (p == NULL)
392                 return (NULL);
393
394         aes_copy_mbs(&entry->ae_fflags_text, p);
395         free(p);
396         f = aes_get_mbs(&entry->ae_fflags_text);
397         return (f);
398 }
399
400 gid_t
401 archive_entry_gid(struct archive_entry *entry)
402 {
403         return (entry->ae_stat.st_gid);
404 }
405
406 const char *
407 archive_entry_gname(struct archive_entry *entry)
408 {
409         return (aes_get_mbs(&entry->ae_gname));
410 }
411
412 const char *
413 archive_entry_hardlink(struct archive_entry *entry)
414 {
415         return (aes_get_mbs(&entry->ae_hardlink));
416 }
417
418 ino_t
419 archive_entry_ino(struct archive_entry *entry)
420 {
421         return (entry->ae_stat.st_ino);
422 }
423
424 mode_t
425 archive_entry_mode(struct archive_entry *entry)
426 {
427         return (entry->ae_stat.st_mode);
428 }
429
430 time_t
431 archive_entry_mtime(struct archive_entry *entry)
432 {
433         return (entry->ae_stat.st_mtime);
434 }
435
436 long
437 archive_entry_mtime_nsec(struct archive_entry *entry)
438 {
439         (void)entry; /* entry can be unused here. */
440         return (ARCHIVE_STAT_MTIME_NANOS(&entry->ae_stat));
441 }
442
443 const char *
444 archive_entry_pathname(struct archive_entry *entry)
445 {
446         return (aes_get_mbs(&entry->ae_pathname));
447 }
448
449 const wchar_t *
450 archive_entry_pathname_w(struct archive_entry *entry)
451 {
452         return (aes_get_wcs(&entry->ae_pathname));
453 }
454
455 dev_t
456 archive_entry_rdev(struct archive_entry *entry)
457 {
458         return (entry->ae_stat.st_rdev);
459 }
460
461 dev_t
462 archive_entry_rdevmajor(struct archive_entry *entry)
463 {
464         return (major(entry->ae_stat.st_rdev));
465 }
466
467 dev_t
468 archive_entry_rdevminor(struct archive_entry *entry)
469 {
470         return (minor(entry->ae_stat.st_rdev));
471 }
472
473 int64_t
474 archive_entry_size(struct archive_entry *entry)
475 {
476         return (entry->ae_stat.st_size);
477 }
478
479 const struct stat *
480 archive_entry_stat(struct archive_entry *entry)
481 {
482         return (&entry->ae_stat);
483 }
484
485 const char *
486 archive_entry_symlink(struct archive_entry *entry)
487 {
488         return (aes_get_mbs(&entry->ae_symlink));
489 }
490
491 uid_t
492 archive_entry_uid(struct archive_entry *entry)
493 {
494         return (entry->ae_stat.st_uid);
495 }
496
497 const char *
498 archive_entry_uname(struct archive_entry *entry)
499 {
500         return (aes_get_mbs(&entry->ae_uname));
501 }
502
503 /*
504  * Functions to set archive_entry properties.
505  */
506
507 /*
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.
510  */
511 void
512 archive_entry_copy_stat(struct archive_entry *entry, const struct stat *st)
513 {
514         entry->ae_stat = *st;
515 }
516
517 void
518 archive_entry_set_fflags(struct archive_entry *entry,
519     unsigned long set, unsigned long clear)
520 {
521         aes_clean(&entry->ae_fflags_text);
522         entry->ae_fflags_set = set;
523         entry->ae_fflags_clear = clear;
524 }
525
526 const wchar_t *
527 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
528     const wchar_t *flags)
529 {
530         aes_copy_wcs(&entry->ae_fflags_text, flags);
531         return (ae_wcstofflags(flags,
532                     &entry->ae_fflags_set, &entry->ae_fflags_clear));
533 }
534
535 void
536 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
537 {
538         entry->ae_stat.st_gid = g;
539 }
540
541 void
542 archive_entry_set_gname(struct archive_entry *entry, const char *name)
543 {
544         aes_set_mbs(&entry->ae_gname, name);
545 }
546
547 void
548 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
549 {
550         aes_copy_wcs(&entry->ae_gname, name);
551 }
552
553 void
554 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
555 {
556         aes_set_mbs(&entry->ae_hardlink, target);
557 }
558
559 void
560 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
561 {
562         aes_copy_mbs(&entry->ae_hardlink, target);
563 }
564
565 void
566 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
567 {
568         aes_copy_wcs(&entry->ae_hardlink, target);
569 }
570
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)
574 {
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);
579 }
580
581 void
582 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
583 {
584         entry->ae_stat.st_mode = m;
585 }
586
587 void
588 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
589 {
590         entry->ae_stat.st_mtime = m;
591         ARCHIVE_STAT_SET_MTIME_NANOS(&entry->ae_stat, ns);
592 }
593
594 void
595 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
596 {
597         aes_set_mbs(&entry->ae_pathname, name);
598 }
599
600 void
601 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
602 {
603         aes_copy_mbs(&entry->ae_pathname, name);
604 }
605
606 void
607 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
608 {
609         aes_copy_wcs(&entry->ae_pathname, name);
610 }
611
612 void
613 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
614 {
615         dev_t d;
616
617         d = entry->ae_stat.st_rdev;
618         entry->ae_stat.st_rdev = makedev(major(m), minor(d));
619 }
620
621 void
622 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
623 {
624         dev_t d;
625
626         d = entry->ae_stat.st_rdev;
627         entry->ae_stat.st_rdev = makedev(major(d), minor(m));
628 }
629
630 void
631 archive_entry_set_size(struct archive_entry *entry, int64_t s)
632 {
633         entry->ae_stat.st_size = s;
634 }
635
636 void
637 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
638 {
639         aes_set_mbs(&entry->ae_symlink, linkname);
640 }
641
642 void
643 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
644 {
645         aes_copy_wcs(&entry->ae_symlink, linkname);
646 }
647
648 void
649 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
650 {
651         entry->ae_stat.st_uid = u;
652 }
653
654 void
655 archive_entry_set_uname(struct archive_entry *entry, const char *name)
656 {
657         aes_set_mbs(&entry->ae_uname, name);
658 }
659
660 void
661 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
662 {
663         aes_copy_wcs(&entry->ae_uname, name);
664 }
665
666 /*
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.
673  */
674
675 void
676 archive_entry_acl_clear(struct archive_entry *entry)
677 {
678         struct ae_acl   *ap;
679
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;
685         }
686         if (entry->acl_text_w != NULL) {
687                 free(entry->acl_text_w);
688                 entry->acl_text_w = NULL;
689         }
690         entry->acl_p = NULL;
691         entry->acl_state = 0; /* Not counting. */
692 }
693
694 /*
695  * Add a single ACL entry to the internal list of ACL data.
696  */
697 void
698 archive_entry_acl_add_entry(struct archive_entry *entry,
699     int type, int permset, int tag, int id, const char *name)
700 {
701         struct ae_acl *ap;
702
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;
709         }
710         if (name != NULL  &&  *name != '\0')
711                 aes_copy_mbs(&ap->name, name);
712         else
713                 aes_clean(&ap->name);
714 }
715
716 /*
717  * As above, but with a wide-character name.
718  */
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)
722 {
723         struct ae_acl *ap;
724
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;
731         }
732         if (name != NULL  &&  *name != L'\0')
733                 aes_copy_wcs(&ap->name, name);
734         else
735                 aes_clean(&ap->name);
736 }
737
738 /*
739  * If this ACL entry is part of the standard POSIX permissions set,
740  * store the permissions in the stat structure and return zero.
741  */
742 static int
743 acl_special(struct archive_entry *entry, int type, int permset, int tag)
744 {
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);
759                 }
760         }
761         return (1);
762 }
763
764 /*
765  * Allocate and populate a new ACL entry with everything but the
766  * name.
767  */
768 static struct ae_acl *
769 acl_new_entry(struct archive_entry *entry,
770     int type, int permset, int tag, int id)
771 {
772         struct ae_acl *ap;
773
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;
780         }
781
782         /* XXX TODO: More sanity-checks on the arguments XXX */
783
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);
789                 }
790         }
791
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);
802 }
803
804 /*
805  * Return a count of entries matching "want_type".
806  */
807 int
808 archive_entry_acl_count(struct archive_entry *entry, int want_type)
809 {
810         int count;
811         struct ae_acl *ap;
812
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;
819         }
820
821         if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
822                 count += 3;
823         return (count);
824 }
825
826 /*
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.
830  */
831 int
832 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
833 {
834         int count, cutoff;
835
836         count = archive_entry_acl_count(entry, want_type);
837
838         /*
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.)
842          */
843         if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
844                 cutoff = 3;
845         else
846                 cutoff = 0;
847
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);
854 }
855
856 /*
857  * Return the next ACL entry in the list.  Fake entries for the
858  * standard permissions and include them in the returned list.
859  */
860
861 int
862 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
863     int *permset, int *tag, int *id, const char **name)
864 {
865         *name = NULL;
866         *id = -1;
867
868         /*
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).
872          */
873         if (entry->acl_state == 0)
874                 return (ARCHIVE_WARN);
875
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;
900                 }
901         }
902
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);
908         }
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);
916 }
917
918 /*
919  * Generate a text version of the ACL.  The flags parameter controls
920  * the style of the generated ACL.
921  */
922 const wchar_t *
923 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
924 {
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;
933
934         if (entry->acl_text_w != NULL) {
935                 free (entry->acl_text_w);
936                 entry->acl_text_w = NULL;
937         }
938
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 */
959                 }
960                 ap = ap->next;
961         }
962
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" */
967         }
968
969         if (count == 0)
970                 return (NULL);
971
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;
985
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++;
998                         }
999                         ap = ap->next;
1000                 }
1001         }
1002
1003
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 ++;
1023                         }
1024                         ap = ap->next;
1025                 }
1026         }
1027
1028         return (entry->acl_text_w);
1029 }
1030
1031 static void
1032 append_id_w(wchar_t **wp, int id)
1033 {
1034         if (id > 9)
1035                 append_id_w(wp, id / 10);
1036         *(*wp)++ = L"0123456789"[id % 10];
1037 }
1038
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)
1042 {
1043         if (prefix != NULL) {
1044                 wcscpy(*wp, prefix);
1045                 *wp += wcslen(*wp);
1046         }
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;
1072         }
1073         *wp += wcslen(*wp);
1074         *(*wp)++ = L':';
1075         if (wname != NULL) {
1076                 wcscpy(*wp, wname);
1077                 *wp += wcslen(*wp);
1078         }
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);
1086         }
1087         **wp = L'\0';
1088 }
1089
1090 /*
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:".
1095  */
1096 int
1097 __archive_entry_acl_parse_w(struct archive_entry *entry,
1098     const wchar_t *text, int default_type)
1099 {
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;
1106
1107         name_start = name_end = NULL;
1108         namebuff = NULL;
1109         namebuff_length = 0;
1110
1111         while (text != NULL  &&  *text != L'\0') {
1112                 next_field_w(&text, &start, &end, &sep);
1113                 if (sep != L':')
1114                         goto fail;
1115
1116                 /*
1117                  * Solaris extension:  "defaultuser::rwx" is the
1118                  * default ACL corresponding to "user::rwx", etc.
1119                  */
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;
1125
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;
1162
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;
1180                         }
1181                 }
1182
1183                 /*
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.
1193                  */
1194                 if (sep == L':' && (tag == ARCHIVE_ENTRY_ACL_USER ||
1195                     tag == ARCHIVE_ENTRY_ACL_GROUP)) {
1196                         next_field_w(&text, &start, &end, &sep);
1197
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++;
1206                                 }
1207                         }
1208                 } else
1209                         id = -1; /* No id specified. */
1210
1211                 /* Skip any additional entries. */
1212                 while (sep == L':') {
1213                         next_field_w(&text, &start, &end, &sep);
1214                 }
1215
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));
1227                         }
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);
1232                 }
1233         }
1234         if (namebuff != NULL)
1235                 free(namebuff);
1236         return (ARCHIVE_OK);
1237
1238 fail:
1239         if (namebuff != NULL)
1240                 free(namebuff);
1241         return (ARCHIVE_WARN);
1242 }
1243
1244 /*
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.
1251  */
1252 static void
1253 next_field_w(const wchar_t **wp, const wchar_t **start,
1254     const wchar_t **end, wchar_t *sep)
1255 {
1256         /* Skip leading whitespace to find start of field. */
1257         while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1258                 (*wp)++;
1259         }
1260         *start = *wp;
1261
1262         /* Scan for the separator. */
1263         while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1264             **wp != L'\n') {
1265                 (*wp)++;
1266         }
1267         *sep = **wp;
1268
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)--;
1273         }
1274         (*end)++;
1275
1276         /* Adjust scanner location. */
1277         if (**wp != L'\0')
1278                 (*wp)++;
1279 }
1280
1281 static int
1282 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1283 {
1284         if (start == end)
1285                 return (0);
1286
1287         if (*start++ != *test++)
1288                 return (0);
1289
1290         while (start < end  &&  *start++ == *test++)
1291                 ;
1292
1293         if (start < end)
1294                 return (0);
1295
1296         return (1);
1297 }
1298
1299
1300 /*
1301  * Following code is modified from UC Berkeley sources, and
1302  * is subject to the following copyright notice.
1303  */
1304
1305 /*-
1306  * Copyright (c) 1993
1307  *      The Regents of the University of California.  All rights reserved.
1308  *
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.
1320  *
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.
1332  */
1333
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
1395
1396 #ifdef EXT2_NOATIME_FL                          /* 'A' */
1397         { "noatime",    L"noatime",             0,              EXT2_NOATIME_FL},
1398 #endif
1399         { NULL,         NULL,                   0,              0 }
1400 };
1401
1402 /*
1403  * fflagstostr --
1404  *      Convert file flags to a comma-separated string.  If no flags
1405  *      are set, return the empty string.
1406  */
1407 char *
1408 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1409 {
1410         char *string, *dp;
1411         const char *sp;
1412         unsigned long bits;
1413         struct flag *flag;
1414         int     length;
1415
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);
1422                 }
1423
1424         if (length == 0)
1425                 return (NULL);
1426         string = malloc(length);
1427         if (string == NULL)
1428                 return (NULL);
1429
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')
1443                         ;
1444                 dp--;
1445         }
1446
1447         *dp = '\0';
1448         return (string);
1449 }
1450
1451 /*
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.
1460  */
1461 const wchar_t *
1462 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1463 {
1464         const wchar_t *start, *end;
1465         struct flag *flag;
1466         unsigned long set, clear;
1467         const wchar_t *failed;
1468
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;
1493                         }
1494                 }
1495                 /* Ignore unknown flag names. */
1496                 if (flag->wname == NULL  &&  failed == NULL)
1497                         failed = start;
1498
1499                 /* Find start of next token. */
1500                 start = end;
1501                 while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
1502                         start++;
1503
1504         }
1505
1506         if (setp)
1507                 *setp = set;
1508         if (clrp)
1509                 *clrp = clear;
1510
1511         /* Return location of first failure. */
1512         return (failed);
1513 }
1514
1515
1516 #ifdef TEST
1517 #include <stdio.h>
1518 int
1519 main(int argc, char **argv)
1520 {
1521         struct archive_entry *entry = archive_entry_new();
1522         unsigned long set, clear;
1523         const wchar_t *remainder;
1524
1525         remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1526         archive_entry_fflags(entry, &set, &clear);
1527
1528         wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1529
1530         wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
1531         return (0);
1532 }
1533 #endif