Import libarchive-2.2.7.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_entry.c
1 /*-
2  * Copyright (c) 2003-2007 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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry.c,v 1.44 2007/07/15 19:10:34 kientzle Exp $");
28
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35 #ifdef MAJOR_IN_MKDEV
36 #include <sys/mkdev.h>
37 # if !defined makedev && (defined mkdev || defined _WIN32 || defined __WIN32__)
38 #  define makedev mkdev
39 # endif
40 #else
41 #ifdef MAJOR_IN_SYSMACROS
42 #include <sys/sysmacros.h>
43 #endif
44 #endif
45 #ifdef HAVE_EXT2FS_EXT2_FS_H
46 #include <ext2fs/ext2_fs.h>     /* for Linux file flags */
47 #endif
48 #ifdef HAVE_LIMITS_H
49 #include <limits.h>
50 #endif
51 #ifdef HAVE_LINUX_FS_H
52 #include <linux/fs.h>   /* for Linux file flags */
53 #endif
54 #ifdef HAVE_LINUX_EXT2_FS_H
55 #include <linux/ext2_fs.h>      /* for Linux file flags */
56 #endif
57 #include <stddef.h>
58 #include <stdio.h>
59 #ifdef HAVE_STDLIB_H
60 #include <stdlib.h>
61 #endif
62 #ifdef HAVE_STRING_H
63 #include <string.h>
64 #endif
65 #ifdef HAVE_WCHAR_H
66 #include <wchar.h>
67 #endif
68
69 #include "archive.h"
70 #include "archive_entry.h"
71 #include "archive_private.h"
72 #include "archive_entry_private.h"
73
74 #undef max
75 #define max(a, b)       ((a)>(b)?(a):(b))
76
77 static void     aes_clean(struct aes *);
78 static void     aes_copy(struct aes *dest, struct aes *src);
79 static const char *     aes_get_mbs(struct aes *);
80 static const wchar_t *  aes_get_wcs(struct aes *);
81 static void     aes_set_mbs(struct aes *, const char *mbs);
82 static void     aes_copy_mbs(struct aes *, const char *mbs);
83 /* static void  aes_set_wcs(struct aes *, const wchar_t *wcs); */
84 static void     aes_copy_wcs(struct aes *, const wchar_t *wcs);
85 static void     aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
86
87 static char *    ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
88 static const wchar_t    *ae_wcstofflags(const wchar_t *stringp,
89                     unsigned long *setp, unsigned long *clrp);
90 static void     append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
91                     const wchar_t *wname, int perm, int id);
92 static void     append_id_w(wchar_t **wp, int id);
93
94 static int      acl_special(struct archive_entry *entry,
95                     int type, int permset, int tag);
96 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
97                     int type, int permset, int tag, int id);
98 static int      isint_w(const wchar_t *start, const wchar_t *end, int *result);
99 static void     next_field_w(const wchar_t **wp, const wchar_t **start,
100                     const wchar_t **end, wchar_t *sep);
101 static int      prefix_w(const wchar_t *start, const wchar_t *end,
102                     const wchar_t *test);
103 static void
104 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
105                     int permset, int tag, int id, const wchar_t *name, size_t);
106
107
108 #ifndef HAVE_WCSCPY
109 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
110 {
111         wchar_t *dest = s1;
112         while ((*s1 = *s2) != L'\0')
113                 ++s1, ++s2;
114         return dest;
115 }
116 #endif
117 #ifndef HAVE_WCSLEN
118 static size_t wcslen(const wchar_t *s)
119 {
120         const wchar_t *p = s;
121         while (*p != L'\0')
122                 ++p;
123         return p - s;
124 }
125 #endif
126 #ifndef HAVE_WMEMCMP
127 /* Good enough for simple equality testing, but not for sorting. */
128 #define wmemcmp(a,b,i)  memcmp((a), (b), (i) * sizeof(wchar_t))
129 #endif
130 #ifndef HAVE_WMEMCPY
131 #define wmemcpy(a,b,i)  (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
132 #endif
133
134
135 static void
136 aes_clean(struct aes *aes)
137 {
138         if (aes->aes_mbs_alloc) {
139                 free(aes->aes_mbs_alloc);
140                 aes->aes_mbs_alloc = NULL;
141         }
142         if (aes->aes_wcs_alloc) {
143                 free(aes->aes_wcs_alloc);
144                 aes->aes_wcs_alloc = NULL;
145         }
146         memset(aes, 0, sizeof(*aes));
147 }
148
149 static void
150 aes_copy(struct aes *dest, struct aes *src)
151 {
152         *dest = *src;
153         if (src->aes_mbs != NULL) {
154                 dest->aes_mbs_alloc = strdup(src->aes_mbs);
155                 dest->aes_mbs = dest->aes_mbs_alloc;
156                 if (dest->aes_mbs == NULL)
157                         __archive_errx(1, "No memory for aes_copy()");
158         }
159
160         if (src->aes_wcs != NULL) {
161                 dest->aes_wcs_alloc = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
162                     * sizeof(wchar_t));
163                 dest->aes_wcs = dest->aes_wcs_alloc;
164                 if (dest->aes_wcs == NULL)
165                         __archive_errx(1, "No memory for aes_copy()");
166                 wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
167         }
168 }
169
170 static const char *
171 aes_get_mbs(struct aes *aes)
172 {
173         if (aes->aes_mbs == NULL && aes->aes_wcs == NULL)
174                 return NULL;
175         if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
176                 /*
177                  * XXX Need to estimate the number of byte in the
178                  * multi-byte form.  Assume that, on average, wcs
179                  * chars encode to no more than 3 bytes.  There must
180                  * be a better way... XXX
181                  */
182                 size_t mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
183
184                 aes->aes_mbs_alloc = (char *)malloc(mbs_length);
185                 aes->aes_mbs = aes->aes_mbs_alloc;
186                 if (aes->aes_mbs == NULL)
187                         __archive_errx(1, "No memory for aes_get_mbs()");
188                 wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
189                 aes->aes_mbs_alloc[mbs_length - 1] = 0;
190         }
191         return (aes->aes_mbs);
192 }
193
194 static const wchar_t *
195 aes_get_wcs(struct aes *aes)
196 {
197         if (aes->aes_wcs == NULL && aes->aes_mbs == NULL)
198                 return NULL;
199         if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
200                 /*
201                  * No single byte will be more than one wide character,
202                  * so this length estimate will always be big enough.
203                  */
204                 size_t wcs_length = strlen(aes->aes_mbs);
205
206                 aes->aes_wcs_alloc
207                     = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
208                 aes->aes_wcs = aes->aes_wcs_alloc;
209                 if (aes->aes_wcs == NULL)
210                         __archive_errx(1, "No memory for aes_get_wcs()");
211                 mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
212                 aes->aes_wcs_alloc[wcs_length] = 0;
213         }
214         return (aes->aes_wcs);
215 }
216
217 static void
218 aes_set_mbs(struct aes *aes, const char *mbs)
219 {
220         if (aes->aes_mbs_alloc) {
221                 free(aes->aes_mbs_alloc);
222                 aes->aes_mbs_alloc = NULL;
223         }
224         if (aes->aes_wcs_alloc) {
225                 free(aes->aes_wcs_alloc);
226                 aes->aes_wcs_alloc = NULL;
227         }
228         aes->aes_mbs = mbs;
229         aes->aes_wcs = NULL;
230 }
231
232 static void
233 aes_copy_mbs(struct aes *aes, const char *mbs)
234 {
235         if (aes->aes_mbs_alloc) {
236                 free(aes->aes_mbs_alloc);
237                 aes->aes_mbs_alloc = NULL;
238         }
239         if (aes->aes_wcs_alloc) {
240                 free(aes->aes_wcs_alloc);
241                 aes->aes_wcs_alloc = NULL;
242         }
243         aes->aes_mbs_alloc = (char *)malloc((strlen(mbs) + 1) * sizeof(char));
244         if (aes->aes_mbs_alloc == NULL)
245                 __archive_errx(1, "No memory for aes_copy_mbs()");
246         strcpy(aes->aes_mbs_alloc, mbs);
247         aes->aes_mbs = aes->aes_mbs_alloc;
248         aes->aes_wcs = NULL;
249 }
250
251 #if 0
252 static void
253 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
254 {
255         if (aes->aes_mbs_alloc) {
256                 free(aes->aes_mbs_alloc);
257                 aes->aes_mbs_alloc = NULL;
258         }
259         if (aes->aes_wcs_alloc) {
260                 free(aes->aes_wcs_alloc);
261                 aes->aes_wcs_alloc = NULL;
262         }
263         aes->aes_mbs = NULL;
264         aes->aes_wcs = wcs;
265 }
266 #endif
267
268 static void
269 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
270 {
271         aes_copy_wcs_len(aes, wcs, wcslen(wcs));
272 }
273
274 static void
275 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
276 {
277         if (aes->aes_mbs_alloc) {
278                 free(aes->aes_mbs_alloc);
279                 aes->aes_mbs_alloc = NULL;
280         }
281         if (aes->aes_wcs_alloc) {
282                 free(aes->aes_wcs_alloc);
283                 aes->aes_wcs_alloc = NULL;
284         }
285         aes->aes_mbs = NULL;
286         aes->aes_wcs_alloc = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
287         if (aes->aes_wcs_alloc == NULL)
288                 __archive_errx(1, "No memory for aes_copy_wcs()");
289         wmemcpy(aes->aes_wcs_alloc, wcs, len);
290         aes->aes_wcs_alloc[len] = L'\0';
291         aes->aes_wcs = aes->aes_wcs_alloc;
292 }
293
294 struct archive_entry *
295 archive_entry_clear(struct archive_entry *entry)
296 {
297         aes_clean(&entry->ae_fflags_text);
298         aes_clean(&entry->ae_gname);
299         aes_clean(&entry->ae_hardlink);
300         aes_clean(&entry->ae_pathname);
301         aes_clean(&entry->ae_symlink);
302         aes_clean(&entry->ae_uname);
303         archive_entry_acl_clear(entry);
304         archive_entry_xattr_clear(entry);
305         free(entry->stat);
306         memset(entry, 0, sizeof(*entry));
307         return entry;
308 }
309
310 struct archive_entry *
311 archive_entry_clone(struct archive_entry *entry)
312 {
313         struct archive_entry *entry2;
314         struct ae_acl *ap, *ap2;
315         struct ae_xattr *xp;
316
317         /* Allocate new structure and copy over all of the fields. */
318         entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
319         if (entry2 == NULL)
320                 return (NULL);
321         memset(entry2, 0, sizeof(*entry2));
322         entry2->ae_stat = entry->ae_stat;
323         entry2->ae_fflags_set = entry->ae_fflags_set;
324         entry2->ae_fflags_clear = entry->ae_fflags_clear;
325
326         aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
327         aes_copy(&entry2->ae_gname, &entry->ae_gname);
328         aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
329         aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
330         aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
331         aes_copy(&entry2->ae_uname, &entry->ae_uname);
332
333         /* Copy ACL data over. */
334         ap = entry->acl_head;
335         while (ap != NULL) {
336                 ap2 = acl_new_entry(entry2,
337                     ap->type, ap->permset, ap->tag, ap->id);
338                 if (ap2 != NULL)
339                         aes_copy(&ap2->name, &ap->name);
340                 ap = ap->next;
341         }
342
343         /* Copy xattr data over. */
344         xp = entry->xattr_head;
345         while (xp != NULL) {
346                 archive_entry_xattr_add_entry(entry2,
347                     xp->name, xp->value, xp->size);
348                 xp = xp->next;
349         }
350
351         return (entry2);
352 }
353
354 void
355 archive_entry_free(struct archive_entry *entry)
356 {
357         archive_entry_clear(entry);
358         free(entry);
359 }
360
361 struct archive_entry *
362 archive_entry_new(void)
363 {
364         struct archive_entry *entry;
365
366         entry = (struct archive_entry *)malloc(sizeof(*entry));
367         if (entry == NULL)
368                 return (NULL);
369         memset(entry, 0, sizeof(*entry));
370         return (entry);
371 }
372
373 /*
374  * Functions for reading fields from an archive_entry.
375  */
376
377 time_t
378 archive_entry_atime(struct archive_entry *entry)
379 {
380         return (entry->ae_stat.aest_atime);
381 }
382
383 long
384 archive_entry_atime_nsec(struct archive_entry *entry)
385 {
386         return (entry->ae_stat.aest_atime_nsec);
387 }
388
389 time_t
390 archive_entry_ctime(struct archive_entry *entry)
391 {
392         return (entry->ae_stat.aest_ctime);
393 }
394
395 long
396 archive_entry_ctime_nsec(struct archive_entry *entry)
397 {
398         return (entry->ae_stat.aest_ctime_nsec);
399 }
400
401 dev_t
402 archive_entry_dev(struct archive_entry *entry)
403 {
404         if (entry->ae_stat.aest_dev_is_broken_down)
405                 return makedev(entry->ae_stat.aest_devmajor,
406                     entry->ae_stat.aest_devminor);
407         else
408                 return (entry->ae_stat.aest_dev);
409 }
410
411 dev_t
412 archive_entry_devmajor(struct archive_entry *entry)
413 {
414         if (entry->ae_stat.aest_dev_is_broken_down)
415                 return (entry->ae_stat.aest_devmajor);
416         else
417                 return major(entry->ae_stat.aest_dev);
418 }
419
420 dev_t
421 archive_entry_devminor(struct archive_entry *entry)
422 {
423         if (entry->ae_stat.aest_dev_is_broken_down)
424                 return (entry->ae_stat.aest_devminor);
425         else
426                 return minor(entry->ae_stat.aest_dev);
427 }
428
429 mode_t
430 archive_entry_filetype(struct archive_entry *entry)
431 {
432         return (AE_IFMT & entry->ae_stat.aest_mode);
433 }
434
435 void
436 archive_entry_fflags(struct archive_entry *entry,
437     unsigned long *set, unsigned long *clear)
438 {
439         *set = entry->ae_fflags_set;
440         *clear = entry->ae_fflags_clear;
441 }
442
443 /*
444  * Note: if text was provided, this just returns that text.  If you
445  * really need the text to be rebuilt in a canonical form, set the
446  * text, ask for the bitmaps, then set the bitmaps.  (Setting the
447  * bitmaps clears any stored text.)  This design is deliberate: if
448  * we're editing archives, we don't want to discard flags just because
449  * they aren't supported on the current system.  The bitmap<->text
450  * conversions are platform-specific (see below).
451  */
452 const char *
453 archive_entry_fflags_text(struct archive_entry *entry)
454 {
455         const char *f;
456         char *p;
457
458         f = aes_get_mbs(&entry->ae_fflags_text);
459         if (f != NULL)
460                 return (f);
461
462         if (entry->ae_fflags_set == 0  &&  entry->ae_fflags_clear == 0)
463                 return (NULL);
464
465         p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
466         if (p == NULL)
467                 return (NULL);
468
469         aes_copy_mbs(&entry->ae_fflags_text, p);
470         free(p);
471         f = aes_get_mbs(&entry->ae_fflags_text);
472         return (f);
473 }
474
475 gid_t
476 archive_entry_gid(struct archive_entry *entry)
477 {
478         return (entry->ae_stat.aest_gid);
479 }
480
481 const char *
482 archive_entry_gname(struct archive_entry *entry)
483 {
484         return (aes_get_mbs(&entry->ae_gname));
485 }
486
487 const wchar_t *
488 archive_entry_gname_w(struct archive_entry *entry)
489 {
490         return (aes_get_wcs(&entry->ae_gname));
491 }
492
493 const char *
494 archive_entry_hardlink(struct archive_entry *entry)
495 {
496         return (aes_get_mbs(&entry->ae_hardlink));
497 }
498
499 const wchar_t *
500 archive_entry_hardlink_w(struct archive_entry *entry)
501 {
502         return (aes_get_wcs(&entry->ae_hardlink));
503 }
504
505 ino_t
506 archive_entry_ino(struct archive_entry *entry)
507 {
508         return (entry->ae_stat.aest_ino);
509 }
510
511 mode_t
512 archive_entry_mode(struct archive_entry *entry)
513 {
514         return (entry->ae_stat.aest_mode);
515 }
516
517 time_t
518 archive_entry_mtime(struct archive_entry *entry)
519 {
520         return (entry->ae_stat.aest_mtime);
521 }
522
523 long
524 archive_entry_mtime_nsec(struct archive_entry *entry)
525 {
526         return (entry->ae_stat.aest_mtime_nsec);
527 }
528
529 unsigned int
530 archive_entry_nlink(struct archive_entry *entry)
531 {
532         return (entry->ae_stat.aest_nlink);
533 }
534
535 const char *
536 archive_entry_pathname(struct archive_entry *entry)
537 {
538         return (aes_get_mbs(&entry->ae_pathname));
539 }
540
541 const wchar_t *
542 archive_entry_pathname_w(struct archive_entry *entry)
543 {
544         return (aes_get_wcs(&entry->ae_pathname));
545 }
546
547 dev_t
548 archive_entry_rdev(struct archive_entry *entry)
549 {
550         if (entry->ae_stat.aest_rdev_is_broken_down)
551                 return makedev(entry->ae_stat.aest_rdevmajor,
552                     entry->ae_stat.aest_rdevminor);
553         else
554                 return (entry->ae_stat.aest_rdev);
555 }
556
557 dev_t
558 archive_entry_rdevmajor(struct archive_entry *entry)
559 {
560         if (entry->ae_stat.aest_rdev_is_broken_down)
561                 return (entry->ae_stat.aest_rdevmajor);
562         else
563                 return major(entry->ae_stat.aest_rdev);
564 }
565
566 dev_t
567 archive_entry_rdevminor(struct archive_entry *entry)
568 {
569         if (entry->ae_stat.aest_rdev_is_broken_down)
570                 return (entry->ae_stat.aest_rdevminor);
571         else
572                 return minor(entry->ae_stat.aest_rdev);
573 }
574
575 int64_t
576 archive_entry_size(struct archive_entry *entry)
577 {
578         return (entry->ae_stat.aest_size);
579 }
580
581 const char *
582 archive_entry_symlink(struct archive_entry *entry)
583 {
584         return (aes_get_mbs(&entry->ae_symlink));
585 }
586
587 const wchar_t *
588 archive_entry_symlink_w(struct archive_entry *entry)
589 {
590         return (aes_get_wcs(&entry->ae_symlink));
591 }
592
593 uid_t
594 archive_entry_uid(struct archive_entry *entry)
595 {
596         return (entry->ae_stat.aest_uid);
597 }
598
599 const char *
600 archive_entry_uname(struct archive_entry *entry)
601 {
602         return (aes_get_mbs(&entry->ae_uname));
603 }
604
605 const wchar_t *
606 archive_entry_uname_w(struct archive_entry *entry)
607 {
608         return (aes_get_wcs(&entry->ae_uname));
609 }
610
611 /*
612  * Functions to set archive_entry properties.
613  */
614
615 void
616 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
617 {
618         entry->stat_valid = 0;
619         entry->ae_stat.aest_mode &= ~AE_IFMT;
620         entry->ae_stat.aest_mode |= AE_IFMT & type;
621 }
622
623 void
624 archive_entry_set_fflags(struct archive_entry *entry,
625     unsigned long set, unsigned long clear)
626 {
627         aes_clean(&entry->ae_fflags_text);
628         entry->ae_fflags_set = set;
629         entry->ae_fflags_clear = clear;
630 }
631
632 const wchar_t *
633 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
634     const wchar_t *flags)
635 {
636         aes_copy_wcs(&entry->ae_fflags_text, flags);
637         return (ae_wcstofflags(flags,
638                     &entry->ae_fflags_set, &entry->ae_fflags_clear));
639 }
640
641 void
642 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
643 {
644         entry->stat_valid = 0;
645         entry->ae_stat.aest_gid = g;
646 }
647
648 void
649 archive_entry_set_gname(struct archive_entry *entry, const char *name)
650 {
651         aes_set_mbs(&entry->ae_gname, name);
652 }
653
654 void
655 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
656 {
657         aes_copy_mbs(&entry->ae_gname, name);
658 }
659
660 void
661 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
662 {
663         aes_copy_wcs(&entry->ae_gname, name);
664 }
665
666 void
667 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
668 {
669         entry->stat_valid = 0;
670         entry->ae_stat.aest_ino = ino;
671 }
672
673 void
674 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
675 {
676         aes_set_mbs(&entry->ae_hardlink, target);
677 }
678
679 void
680 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
681 {
682         aes_copy_mbs(&entry->ae_hardlink, target);
683 }
684
685 void
686 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
687 {
688         aes_copy_wcs(&entry->ae_hardlink, target);
689 }
690
691 void
692 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
693 {
694         entry->stat_valid = 0;
695         entry->ae_stat.aest_atime = t;
696         entry->ae_stat.aest_atime_nsec = ns;
697 }
698
699 void
700 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
701 {
702         entry->stat_valid = 0;
703         entry->ae_stat.aest_ctime = t;
704         entry->ae_stat.aest_ctime_nsec = ns;
705 }
706
707 void
708 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
709 {
710         entry->stat_valid = 0;
711         entry->ae_stat.aest_dev_is_broken_down = 0;
712         entry->ae_stat.aest_dev = d;
713 }
714
715 void
716 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
717 {
718         entry->stat_valid = 0;
719         entry->ae_stat.aest_dev_is_broken_down = 1;
720         entry->ae_stat.aest_devmajor = m;
721 }
722
723 void
724 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
725 {
726         entry->stat_valid = 0;
727         entry->ae_stat.aest_dev_is_broken_down = 1;
728         entry->ae_stat.aest_devminor = m;
729 }
730
731 /* Set symlink if symlink is already set, else set hardlink. */
732 void
733 archive_entry_set_link(struct archive_entry *entry, const char *target)
734 {
735         if (entry->ae_symlink.aes_mbs != NULL ||
736             entry->ae_symlink.aes_wcs != NULL)
737                 aes_set_mbs(&entry->ae_symlink, target);
738         else
739                 aes_set_mbs(&entry->ae_hardlink, target);
740 }
741
742 void
743 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
744 {
745         entry->stat_valid = 0;
746         entry->ae_stat.aest_mode = m;
747 }
748
749 void
750 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
751 {
752         entry->stat_valid = 0;
753         entry->ae_stat.aest_mtime = m;
754         entry->ae_stat.aest_mtime_nsec = ns;
755 }
756
757 void
758 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
759 {
760         entry->stat_valid = 0;
761         entry->ae_stat.aest_nlink = nlink;
762 }
763
764 void
765 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
766 {
767         aes_set_mbs(&entry->ae_pathname, name);
768 }
769
770 void
771 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
772 {
773         aes_copy_mbs(&entry->ae_pathname, name);
774 }
775
776 void
777 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
778 {
779         aes_copy_wcs(&entry->ae_pathname, name);
780 }
781
782 void
783 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
784 {
785         entry->stat_valid = 0;
786         entry->ae_stat.aest_mode &= AE_IFMT;
787         entry->ae_stat.aest_mode |= ~AE_IFMT & p;
788 }
789
790 void
791 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
792 {
793         entry->stat_valid = 0;
794         entry->ae_stat.aest_rdev = m;
795         entry->ae_stat.aest_rdev_is_broken_down = 0;
796 }
797
798 void
799 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
800 {
801         entry->stat_valid = 0;
802         entry->ae_stat.aest_rdev_is_broken_down = 1;
803         entry->ae_stat.aest_rdevmajor = m;
804 }
805
806 void
807 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
808 {
809         entry->stat_valid = 0;
810         entry->ae_stat.aest_rdev_is_broken_down = 1;
811         entry->ae_stat.aest_rdevminor = m;
812 }
813
814 void
815 archive_entry_set_size(struct archive_entry *entry, int64_t s)
816 {
817         entry->stat_valid = 0;
818         entry->ae_stat.aest_size = s;
819 }
820
821 void
822 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
823 {
824         aes_set_mbs(&entry->ae_symlink, linkname);
825 }
826
827 void
828 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
829 {
830         aes_copy_mbs(&entry->ae_symlink, linkname);
831 }
832
833 void
834 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
835 {
836         aes_copy_wcs(&entry->ae_symlink, linkname);
837 }
838
839 void
840 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
841 {
842         entry->stat_valid = 0;
843         entry->ae_stat.aest_uid = u;
844 }
845
846 void
847 archive_entry_set_uname(struct archive_entry *entry, const char *name)
848 {
849         aes_set_mbs(&entry->ae_uname, name);
850 }
851
852 void
853 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
854 {
855         aes_copy_mbs(&entry->ae_uname, name);
856 }
857
858 void
859 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
860 {
861         aes_copy_wcs(&entry->ae_uname, name);
862 }
863
864 /*
865  * ACL management.  The following would, of course, be a lot simpler
866  * if: 1) the last draft of POSIX.1e were a really thorough and
867  * complete standard that addressed the needs of ACL archiving and 2)
868  * everyone followed it faithfully.  Alas, neither is true, so the
869  * following is a lot more complex than might seem necessary to the
870  * uninitiated.
871  */
872
873 void
874 archive_entry_acl_clear(struct archive_entry *entry)
875 {
876         struct ae_acl   *ap;
877
878         while (entry->acl_head != NULL) {
879                 ap = entry->acl_head->next;
880                 aes_clean(&entry->acl_head->name);
881                 free(entry->acl_head);
882                 entry->acl_head = ap;
883         }
884         if (entry->acl_text_w != NULL) {
885                 free(entry->acl_text_w);
886                 entry->acl_text_w = NULL;
887         }
888         entry->acl_p = NULL;
889         entry->acl_state = 0; /* Not counting. */
890 }
891
892 /*
893  * Add a single ACL entry to the internal list of ACL data.
894  */
895 void
896 archive_entry_acl_add_entry(struct archive_entry *entry,
897     int type, int permset, int tag, int id, const char *name)
898 {
899         struct ae_acl *ap;
900
901         if (acl_special(entry, type, permset, tag) == 0)
902                 return;
903         ap = acl_new_entry(entry, type, permset, tag, id);
904         if (ap == NULL) {
905                 /* XXX Error XXX */
906                 return;
907         }
908         if (name != NULL  &&  *name != '\0')
909                 aes_copy_mbs(&ap->name, name);
910         else
911                 aes_clean(&ap->name);
912 }
913
914 /*
915  * As above, but with a wide-character name.
916  */
917 void
918 archive_entry_acl_add_entry_w(struct archive_entry *entry,
919     int type, int permset, int tag, int id, const wchar_t *name)
920 {
921         archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
922 }
923
924 void
925 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
926     int type, int permset, int tag, int id, const wchar_t *name, size_t len)
927 {
928         struct ae_acl *ap;
929
930         if (acl_special(entry, type, permset, tag) == 0)
931                 return;
932         ap = acl_new_entry(entry, type, permset, tag, id);
933         if (ap == NULL) {
934                 /* XXX Error XXX */
935                 return;
936         }
937         if (name != NULL  &&  *name != L'\0' && len > 0)
938                 aes_copy_wcs_len(&ap->name, name, len);
939         else
940                 aes_clean(&ap->name);
941 }
942
943 /*
944  * If this ACL entry is part of the standard POSIX permissions set,
945  * store the permissions in the stat structure and return zero.
946  */
947 static int
948 acl_special(struct archive_entry *entry, int type, int permset, int tag)
949 {
950         if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
951                 switch (tag) {
952                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
953                         entry->ae_stat.aest_mode &= ~0700;
954                         entry->ae_stat.aest_mode |= (permset & 7) << 6;
955                         return (0);
956                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
957                         entry->ae_stat.aest_mode &= ~0070;
958                         entry->ae_stat.aest_mode |= (permset & 7) << 3;
959                         return (0);
960                 case ARCHIVE_ENTRY_ACL_OTHER:
961                         entry->ae_stat.aest_mode &= ~0007;
962                         entry->ae_stat.aest_mode |= permset & 7;
963                         return (0);
964                 }
965         }
966         return (1);
967 }
968
969 /*
970  * Allocate and populate a new ACL entry with everything but the
971  * name.
972  */
973 static struct ae_acl *
974 acl_new_entry(struct archive_entry *entry,
975     int type, int permset, int tag, int id)
976 {
977         struct ae_acl *ap;
978
979         if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
980             type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
981                 return (NULL);
982         if (entry->acl_text_w != NULL) {
983                 free(entry->acl_text_w);
984                 entry->acl_text_w = NULL;
985         }
986
987         /* XXX TODO: More sanity-checks on the arguments XXX */
988
989         /* If there's a matching entry already in the list, overwrite it. */
990         for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
991                 if (ap->type == type && ap->tag == tag && ap->id == id) {
992                         ap->permset = permset;
993                         return (ap);
994                 }
995         }
996
997         /* Add a new entry to the list. */
998         ap = (struct ae_acl *)malloc(sizeof(*ap));
999         if (ap == NULL)
1000                 return (NULL);
1001         memset(ap, 0, sizeof(*ap));
1002         ap->next = entry->acl_head;
1003         entry->acl_head = ap;
1004         ap->type = type;
1005         ap->tag = tag;
1006         ap->id = id;
1007         ap->permset = permset;
1008         return (ap);
1009 }
1010
1011 /*
1012  * Return a count of entries matching "want_type".
1013  */
1014 int
1015 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1016 {
1017         int count;
1018         struct ae_acl *ap;
1019
1020         count = 0;
1021         ap = entry->acl_head;
1022         while (ap != NULL) {
1023                 if ((ap->type & want_type) != 0)
1024                         count++;
1025                 ap = ap->next;
1026         }
1027
1028         if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1029                 count += 3;
1030         return (count);
1031 }
1032
1033 /*
1034  * Prepare for reading entries from the ACL data.  Returns a count
1035  * of entries matching "want_type", or zero if there are no
1036  * non-extended ACL entries of that type.
1037  */
1038 int
1039 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1040 {
1041         int count, cutoff;
1042
1043         count = archive_entry_acl_count(entry, want_type);
1044
1045         /*
1046          * If the only entries are the three standard ones,
1047          * then don't return any ACL data.  (In this case,
1048          * client can just use chmod(2) to set permissions.)
1049          */
1050         if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1051                 cutoff = 3;
1052         else
1053                 cutoff = 0;
1054
1055         if (count > cutoff)
1056                 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1057         else
1058                 entry->acl_state = 0;
1059         entry->acl_p = entry->acl_head;
1060         return (count);
1061 }
1062
1063 /*
1064  * Return the next ACL entry in the list.  Fake entries for the
1065  * standard permissions and include them in the returned list.
1066  */
1067
1068 int
1069 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1070     int *permset, int *tag, int *id, const char **name)
1071 {
1072         *name = NULL;
1073         *id = -1;
1074
1075         /*
1076          * The acl_state is either zero (no entries available), -1
1077          * (reading from list), or an entry type (retrieve that type
1078          * from ae_stat.aest_mode).
1079          */
1080         if (entry->acl_state == 0)
1081                 return (ARCHIVE_WARN);
1082
1083         /* The first three access entries are special. */
1084         if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1085                 switch (entry->acl_state) {
1086                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1087                         *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1088                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1089                         *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1090                         entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1091                         return (ARCHIVE_OK);
1092                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1093                         *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1094                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1095                         *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1096                         entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1097                         return (ARCHIVE_OK);
1098                 case ARCHIVE_ENTRY_ACL_OTHER:
1099                         *permset = entry->ae_stat.aest_mode & 7;
1100                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1101                         *tag = ARCHIVE_ENTRY_ACL_OTHER;
1102                         entry->acl_state = -1;
1103                         entry->acl_p = entry->acl_head;
1104                         return (ARCHIVE_OK);
1105                 default:
1106                         break;
1107                 }
1108         }
1109
1110         while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1111                 entry->acl_p = entry->acl_p->next;
1112         if (entry->acl_p == NULL) {
1113                 entry->acl_state = 0;
1114                 return (ARCHIVE_EOF); /* End of ACL entries. */
1115         }
1116         *type = entry->acl_p->type;
1117         *permset = entry->acl_p->permset;
1118         *tag = entry->acl_p->tag;
1119         *id = entry->acl_p->id;
1120         *name = aes_get_mbs(&entry->acl_p->name);
1121         entry->acl_p = entry->acl_p->next;
1122         return (ARCHIVE_OK);
1123 }
1124
1125 /*
1126  * Generate a text version of the ACL.  The flags parameter controls
1127  * the style of the generated ACL.
1128  */
1129 const wchar_t *
1130 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1131 {
1132         int count;
1133         int length;
1134         const wchar_t *wname;
1135         const wchar_t *prefix;
1136         wchar_t separator;
1137         struct ae_acl *ap;
1138         int id;
1139         wchar_t *wp;
1140
1141         if (entry->acl_text_w != NULL) {
1142                 free (entry->acl_text_w);
1143                 entry->acl_text_w = NULL;
1144         }
1145
1146         separator = L',';
1147         count = 0;
1148         length = 0;
1149         ap = entry->acl_head;
1150         while (ap != NULL) {
1151                 if ((ap->type & flags) != 0) {
1152                         count++;
1153                         if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1154                             (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1155                                 length += 8; /* "default:" */
1156                         length += 5; /* tag name */
1157                         length += 1; /* colon */
1158                         wname = aes_get_wcs(&ap->name);
1159                         if (wname != NULL)
1160                                 length += wcslen(wname);
1161                         else
1162                                 length += sizeof(uid_t) * 3 + 1;
1163                         length ++; /* colon */
1164                         length += 3; /* rwx */
1165                         length += 1; /* colon */
1166                         length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1167                         length ++; /* newline */
1168                 }
1169                 ap = ap->next;
1170         }
1171
1172         if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1173                 length += 10; /* "user::rwx\n" */
1174                 length += 11; /* "group::rwx\n" */
1175                 length += 11; /* "other::rwx\n" */
1176         }
1177
1178         if (count == 0)
1179                 return (NULL);
1180
1181         /* Now, allocate the string and actually populate it. */
1182         wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1183         if (wp == NULL)
1184                 __archive_errx(1, "No memory to generate the text version of the ACL");
1185         count = 0;
1186         if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1187                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1188                     entry->ae_stat.aest_mode & 0700, -1);
1189                 *wp++ = ',';
1190                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1191                     entry->ae_stat.aest_mode & 0070, -1);
1192                 *wp++ = ',';
1193                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1194                     entry->ae_stat.aest_mode & 0007, -1);
1195                 count += 3;
1196
1197                 ap = entry->acl_head;
1198                 while (ap != NULL) {
1199                         if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1200                                 wname = aes_get_wcs(&ap->name);
1201                                 *wp++ = separator;
1202                                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1203                                         id = ap->id;
1204                                 else
1205                                         id = -1;
1206                                 append_entry_w(&wp, NULL, ap->tag, wname,
1207                                     ap->permset, id);
1208                                 count++;
1209                         }
1210                         ap = ap->next;
1211                 }
1212         }
1213
1214
1215         if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1216                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1217                         prefix = L"default:";
1218                 else
1219                         prefix = NULL;
1220                 ap = entry->acl_head;
1221                 count = 0;
1222                 while (ap != NULL) {
1223                         if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1224                                 wname = aes_get_wcs(&ap->name);
1225                                 if (count > 0)
1226                                         *wp++ = separator;
1227                                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1228                                         id = ap->id;
1229                                 else
1230                                         id = -1;
1231                                 append_entry_w(&wp, prefix, ap->tag,
1232                                     wname, ap->permset, id);
1233                                 count ++;
1234                         }
1235                         ap = ap->next;
1236                 }
1237         }
1238
1239         return (entry->acl_text_w);
1240 }
1241
1242 static void
1243 append_id_w(wchar_t **wp, int id)
1244 {
1245         if (id < 0)
1246                 id = 0;
1247         if (id > 9)
1248                 append_id_w(wp, id / 10);
1249         *(*wp)++ = L"0123456789"[id % 10];
1250 }
1251
1252 static void
1253 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1254     const wchar_t *wname, int perm, int id)
1255 {
1256         if (prefix != NULL) {
1257                 wcscpy(*wp, prefix);
1258                 *wp += wcslen(*wp);
1259         }
1260         switch (tag) {
1261         case ARCHIVE_ENTRY_ACL_USER_OBJ:
1262                 wname = NULL;
1263                 id = -1;
1264                 /* FALLTHROUGH */
1265         case ARCHIVE_ENTRY_ACL_USER:
1266                 wcscpy(*wp, L"user");
1267                 break;
1268         case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1269                 wname = NULL;
1270                 id = -1;
1271                 /* FALLTHROUGH */
1272         case ARCHIVE_ENTRY_ACL_GROUP:
1273                 wcscpy(*wp, L"group");
1274                 break;
1275         case ARCHIVE_ENTRY_ACL_MASK:
1276                 wcscpy(*wp, L"mask");
1277                 wname = NULL;
1278                 id = -1;
1279                 break;
1280         case ARCHIVE_ENTRY_ACL_OTHER:
1281                 wcscpy(*wp, L"other");
1282                 wname = NULL;
1283                 id = -1;
1284                 break;
1285         }
1286         *wp += wcslen(*wp);
1287         *(*wp)++ = L':';
1288         if (wname != NULL) {
1289                 wcscpy(*wp, wname);
1290                 *wp += wcslen(*wp);
1291         } else if (tag == ARCHIVE_ENTRY_ACL_USER
1292             || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1293                 append_id_w(wp, id);
1294                 id = -1;
1295         }
1296         *(*wp)++ = L':';
1297         *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1298         *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1299         *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1300         if (id != -1) {
1301                 *(*wp)++ = L':';
1302                 append_id_w(wp, id);
1303         }
1304         **wp = L'\0';
1305 }
1306
1307 /*
1308  * Parse a textual ACL.  This automatically recognizes and supports
1309  * extensions described above.  The 'type' argument is used to
1310  * indicate the type that should be used for any entries not
1311  * explicitly marked as "default:".
1312  */
1313 int
1314 __archive_entry_acl_parse_w(struct archive_entry *entry,
1315     const wchar_t *text, int default_type)
1316 {
1317         struct {
1318                 const wchar_t *start;
1319                 const wchar_t *end;
1320         } field[4];
1321
1322         int fields;
1323         int type, tag, permset, id;
1324         const wchar_t *p;
1325         wchar_t sep;
1326
1327         while (text != NULL  &&  *text != L'\0') {
1328                 /*
1329                  * Parse the fields out of the next entry,
1330                  * advance 'text' to start of next entry.
1331                  */
1332                 fields = 0;
1333                 do {
1334                         const wchar_t *start, *end;
1335                         next_field_w(&text, &start, &end, &sep);
1336                         if (fields < 4) {
1337                                 field[fields].start = start;
1338                                 field[fields].end = end;
1339                         }
1340                         ++fields;
1341                 } while (sep == L':');
1342
1343                 if (fields < 3)
1344                         return (ARCHIVE_WARN);
1345
1346                 /* Check for a numeric ID in field 1 or 3. */
1347                 id = -1;
1348                 isint_w(field[1].start, field[1].end, &id);
1349                 /* Field 3 is optional. */
1350                 if (id == -1 && fields > 3)
1351                         isint_w(field[3].start, field[3].end, &id);
1352
1353                 /* Parse the permissions from field 2. */
1354                 permset = 0;
1355                 p = field[2].start;
1356                 while (p < field[2].end) {
1357                         switch (*p++) {
1358                         case 'r': case 'R':
1359                                 permset |= ARCHIVE_ENTRY_ACL_READ;
1360                                 break;
1361                         case 'w': case 'W':
1362                                 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1363                                 break;
1364                         case 'x': case 'X':
1365                                 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1366                                 break;
1367                         case '-':
1368                                 break;
1369                         default:
1370                                 return (ARCHIVE_WARN);
1371                         }
1372                 }
1373
1374                 /*
1375                  * Solaris extension:  "defaultuser::rwx" is the
1376                  * default ACL corresponding to "user::rwx", etc.
1377                  */
1378                 if (field[0].end-field[0].start > 7
1379                     && wmemcmp(field[0].start, L"default", 7) == 0) {
1380                         type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1381                         field[0].start += 7;
1382                 } else
1383                         type = default_type;
1384
1385                 if (prefix_w(field[0].start, field[0].end, L"user")) {
1386                         if (id != -1 || field[1].start < field[1].end)
1387                                 tag = ARCHIVE_ENTRY_ACL_USER;
1388                         else
1389                                 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1390                 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1391                         if (id != -1 || field[1].start < field[1].end)
1392                                 tag = ARCHIVE_ENTRY_ACL_GROUP;
1393                         else
1394                                 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1395                 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1396                         if (id != -1 || field[1].start < field[1].end)
1397                                 return (ARCHIVE_WARN);
1398                         tag = ARCHIVE_ENTRY_ACL_OTHER;
1399                 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1400                         if (id != -1 || field[1].start < field[1].end)
1401                                 return (ARCHIVE_WARN);
1402                         tag = ARCHIVE_ENTRY_ACL_MASK;
1403                 } else
1404                         return (ARCHIVE_WARN);
1405
1406                 /* Add entry to the internal list. */
1407                 archive_entry_acl_add_entry_w_len(entry, type, permset,
1408                     tag, id, field[1].start, field[1].end - field[1].start);
1409         }
1410         return (ARCHIVE_OK);
1411 }
1412
1413 /*
1414  * extended attribute handling
1415  */
1416
1417 void
1418 archive_entry_xattr_clear(struct archive_entry *entry)
1419 {
1420         struct ae_xattr *xp;
1421
1422         while (entry->xattr_head != NULL) {
1423                 xp = entry->xattr_head->next;
1424                 free(entry->xattr_head->name);
1425                 free(entry->xattr_head->value);
1426                 free(entry->xattr_head);
1427                 entry->xattr_head = xp;
1428         }
1429
1430         entry->xattr_head = NULL;
1431 }
1432
1433 void
1434 archive_entry_xattr_add_entry(struct archive_entry *entry,
1435         const char *name, const void *value, size_t size)
1436 {
1437         struct ae_xattr *xp;
1438
1439         for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1440                 ;
1441
1442         if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1443                 /* XXX Error XXX */
1444                 return;
1445
1446         xp->name = strdup(name);
1447         if ((xp->value = malloc(size)) != NULL) {
1448                 memcpy(xp->value, value, size);
1449                 xp->size = size;
1450         } else
1451                 xp->size = 0;
1452
1453         xp->next = entry->xattr_head;
1454         entry->xattr_head = xp;
1455 }
1456
1457
1458 /*
1459  * returns number of the extended attribute entries
1460  */
1461 int
1462 archive_entry_xattr_count(struct archive_entry *entry)
1463 {
1464         struct ae_xattr *xp;
1465         int count = 0;
1466
1467         for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1468                 count++;
1469
1470         return count;
1471 }
1472
1473 int
1474 archive_entry_xattr_reset(struct archive_entry * entry)
1475 {
1476         entry->xattr_p = entry->xattr_head;
1477
1478         return archive_entry_xattr_count(entry);
1479 }
1480
1481 int
1482 archive_entry_xattr_next(struct archive_entry * entry,
1483         const char **name, const void **value, size_t *size)
1484 {
1485         if (entry->xattr_p) {
1486                 *name = entry->xattr_p->name;
1487                 *value = entry->xattr_p->value;
1488                 *size = entry->xattr_p->size;
1489
1490                 entry->xattr_p = entry->xattr_p->next;
1491
1492                 return (ARCHIVE_OK);
1493         } else {
1494                 *name = NULL;
1495                 *name = NULL;
1496                 *size = (size_t)0;
1497                 return (ARCHIVE_WARN);
1498         }
1499 }
1500
1501 /*
1502  * end of xattr handling
1503  */
1504
1505 /*
1506  * Parse a string to a positive decimal integer.  Returns true if
1507  * the string is non-empty and consists only of decimal digits,
1508  * false otherwise.
1509  */
1510 static int
1511 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1512 {
1513         int n = 0;
1514         if (start >= end)
1515                 return (0);
1516         while (start < end) {
1517                 if (*start < '0' || *start > '9')
1518                         return (0);
1519                 if (n > (INT_MAX / 10))
1520                         n = INT_MAX;
1521                 else {
1522                         n *= 10;
1523                         n += *start - '0';
1524                 }
1525                 start++;
1526         }
1527         *result = n;
1528         return (1);
1529 }
1530
1531 /*
1532  * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]".  *wp is updated
1533  * to point to just after the separator.  *start points to the first
1534  * character of the matched text and *end just after the last
1535  * character of the matched identifier.  In particular *end - *start
1536  * is the length of the field body, not including leading or trailing
1537  * whitespace.
1538  */
1539 static void
1540 next_field_w(const wchar_t **wp, const wchar_t **start,
1541     const wchar_t **end, wchar_t *sep)
1542 {
1543         /* Skip leading whitespace to find start of field. */
1544         while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1545                 (*wp)++;
1546         }
1547         *start = *wp;
1548
1549         /* Scan for the separator. */
1550         while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1551             **wp != L'\n') {
1552                 (*wp)++;
1553         }
1554         *sep = **wp;
1555
1556         /* Trim trailing whitespace to locate end of field. */
1557         *end = *wp - 1;
1558         while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1559                 (*end)--;
1560         }
1561         (*end)++;
1562
1563         /* Adjust scanner location. */
1564         if (**wp != L'\0')
1565                 (*wp)++;
1566 }
1567
1568 /*
1569  * Return true if the characters [start...end) are a prefix of 'test'.
1570  * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1571  */
1572 static int
1573 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1574 {
1575         if (start == end)
1576                 return (0);
1577
1578         if (*start++ != *test++)
1579                 return (0);
1580
1581         while (start < end  &&  *start++ == *test++)
1582                 ;
1583
1584         if (start < end)
1585                 return (0);
1586
1587         return (1);
1588 }
1589
1590
1591 /*
1592  * Following code is modified from UC Berkeley sources, and
1593  * is subject to the following copyright notice.
1594  */
1595
1596 /*-
1597  * Copyright (c) 1993
1598  *      The Regents of the University of California.  All rights reserved.
1599  *
1600  * Redistribution and use in source and binary forms, with or without
1601  * modification, are permitted provided that the following conditions
1602  * are met:
1603  * 1. Redistributions of source code must retain the above copyright
1604  *    notice, this list of conditions and the following disclaimer.
1605  * 2. Redistributions in binary form must reproduce the above copyright
1606  *    notice, this list of conditions and the following disclaimer in the
1607  *    documentation and/or other materials provided with the distribution.
1608  * 4. Neither the name of the University nor the names of its contributors
1609  *    may be used to endorse or promote products derived from this software
1610  *    without specific prior written permission.
1611  *
1612  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1613  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1614  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1615  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1616  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1617  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1618  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1619  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1620  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1621  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1622  * SUCH DAMAGE.
1623  */
1624
1625 static struct flag {
1626         const char      *name;
1627         const wchar_t   *wname;
1628         unsigned long    set;
1629         unsigned long    clear;
1630 } flags[] = {
1631         /* Preferred (shorter) names per flag first, all prefixed by "no" */
1632 #ifdef SF_APPEND
1633         { "nosappnd",   L"nosappnd",            SF_APPEND,      0 },
1634         { "nosappend",  L"nosappend",           SF_APPEND,      0 },
1635 #endif
1636 #ifdef  EXT2_APPEND_FL                          /* 'a' */
1637         { "nosappnd",   L"nosappnd",            EXT2_APPEND_FL, 0 },
1638         { "nosappend",  L"nosappend",           EXT2_APPEND_FL, 0 },
1639 #endif
1640 #ifdef SF_ARCHIVED
1641         { "noarch",     L"noarch",              SF_ARCHIVED,    0 },
1642         { "noarchived", L"noarchived",          SF_ARCHIVED,    0 },
1643 #endif
1644 #ifdef SF_IMMUTABLE
1645         { "noschg",     L"noschg",              SF_IMMUTABLE,   0 },
1646         { "noschange",  L"noschange",           SF_IMMUTABLE,   0 },
1647         { "nosimmutable",       L"nosimmutable",        SF_IMMUTABLE,   0 },
1648 #endif
1649 #ifdef EXT2_IMMUTABLE_FL                        /* 'i' */
1650         { "noschg",     L"noschg",              EXT2_IMMUTABLE_FL,      0 },
1651         { "noschange",  L"noschange",           EXT2_IMMUTABLE_FL,      0 },
1652         { "nosimmutable",       L"nosimmutable",        EXT2_IMMUTABLE_FL,      0 },
1653 #endif
1654 #ifdef SF_NOUNLINK
1655         { "nosunlnk",   L"nosunlnk",            SF_NOUNLINK,    0 },
1656         { "nosunlink",  L"nosunlink",           SF_NOUNLINK,    0 },
1657 #endif
1658 #ifdef SF_SNAPSHOT
1659         { "nosnapshot", L"nosnapshot",  SF_SNAPSHOT,    0 },
1660 #endif
1661 #ifdef UF_APPEND
1662         { "nouappnd",   L"nouappnd",            UF_APPEND,      0 },
1663         { "nouappend",  L"nouappend",           UF_APPEND,      0 },
1664 #endif
1665 #ifdef UF_IMMUTABLE
1666         { "nouchg",     L"nouchg",              UF_IMMUTABLE,   0 },
1667         { "nouchange",  L"nouchange",           UF_IMMUTABLE,   0 },
1668         { "nouimmutable",       L"nouimmutable",        UF_IMMUTABLE,   0 },
1669 #endif
1670 #ifdef UF_NODUMP
1671         { "nodump",     L"nodump",              0,              UF_NODUMP},
1672 #endif
1673 #ifdef EXT2_NODUMP_FL                           /* 'd' */
1674         { "nodump",     L"nodump",              0,              EXT2_NODUMP_FL},
1675 #endif
1676 #ifdef UF_OPAQUE
1677         { "noopaque",   L"noopaque",            UF_OPAQUE,      0 },
1678 #endif
1679 #ifdef UF_NOUNLINK
1680         { "nouunlnk",   L"nouunlnk",            UF_NOUNLINK,    0 },
1681         { "nouunlink",  L"nouunlink",           UF_NOUNLINK,    0 },
1682 #endif
1683 #ifdef EXT2_COMPR_FL                            /* 'c' */
1684         { "nocompress", L"nocompress",          EXT2_COMPR_FL,  0 },
1685 #endif
1686
1687 #ifdef EXT2_NOATIME_FL                          /* 'A' */
1688         { "noatime",    L"noatime",             0,              EXT2_NOATIME_FL},
1689 #endif
1690         { NULL,         NULL,                   0,              0 }
1691 };
1692
1693 /*
1694  * fflagstostr --
1695  *      Convert file flags to a comma-separated string.  If no flags
1696  *      are set, return the empty string.
1697  */
1698 char *
1699 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1700 {
1701         char *string, *dp;
1702         const char *sp;
1703         unsigned long bits;
1704         struct flag *flag;
1705         size_t  length;
1706
1707         bits = bitset | bitclear;
1708         length = 0;
1709         for (flag = flags; flag->name != NULL; flag++)
1710                 if (bits & (flag->set | flag->clear)) {
1711                         length += strlen(flag->name) + 1;
1712                         bits &= ~(flag->set | flag->clear);
1713                 }
1714
1715         if (length == 0)
1716                 return (NULL);
1717         string = (char *)malloc(length);
1718         if (string == NULL)
1719                 return (NULL);
1720
1721         dp = string;
1722         for (flag = flags; flag->name != NULL; flag++) {
1723                 if (bitset & flag->set || bitclear & flag->clear) {
1724                         sp = flag->name + 2;
1725                 } else if (bitset & flag->clear  ||  bitclear & flag->set) {
1726                         sp = flag->name;
1727                 } else
1728                         continue;
1729                 bitset &= ~(flag->set | flag->clear);
1730                 bitclear &= ~(flag->set | flag->clear);
1731                 if (dp > string)
1732                         *dp++ = ',';
1733                 while ((*dp++ = *sp++) != '\0')
1734                         ;
1735                 dp--;
1736         }
1737
1738         *dp = '\0';
1739         return (string);
1740 }
1741
1742 /*
1743  * wcstofflags --
1744  *      Take string of arguments and return file flags.  This
1745  *      version works a little differently than strtofflags(3).
1746  *      In particular, it always tests every token, skipping any
1747  *      unrecognized tokens.  It returns a pointer to the first
1748  *      unrecognized token, or NULL if every token was recognized.
1749  *      This version is also const-correct and does not modify the
1750  *      provided string.
1751  */
1752 const wchar_t *
1753 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1754 {
1755         const wchar_t *start, *end;
1756         struct flag *flag;
1757         unsigned long set, clear;
1758         const wchar_t *failed;
1759
1760         set = clear = 0;
1761         start = s;
1762         failed = NULL;
1763         /* Find start of first token. */
1764         while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
1765                 start++;
1766         while (*start != L'\0') {
1767                 /* Locate end of token. */
1768                 end = start;
1769                 while (*end != L'\0'  &&  *end != L'\t'  &&
1770                     *end != L' '  &&  *end != L',')
1771                         end++;
1772                 for (flag = flags; flag->wname != NULL; flag++) {
1773                         if (wmemcmp(start, flag->wname, end - start) == 0) {
1774                                 /* Matched "noXXXX", so reverse the sense. */
1775                                 clear |= flag->set;
1776                                 set |= flag->clear;
1777                                 break;
1778                         } else if (wmemcmp(start, flag->wname + 2, end - start)
1779                             == 0) {
1780                                 /* Matched "XXXX", so don't reverse. */
1781                                 set |= flag->set;
1782                                 clear |= flag->clear;
1783                                 break;
1784                         }
1785                 }
1786                 /* Ignore unknown flag names. */
1787                 if (flag->wname == NULL  &&  failed == NULL)
1788                         failed = start;
1789
1790                 /* Find start of next token. */
1791                 start = end;
1792                 while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
1793                         start++;
1794
1795         }
1796
1797         if (setp)
1798                 *setp = set;
1799         if (clrp)
1800                 *clrp = clear;
1801
1802         /* Return location of first failure. */
1803         return (failed);
1804 }
1805
1806
1807 #ifdef TEST
1808 #include <stdio.h>
1809 int
1810 main(int argc, char **argv)
1811 {
1812         struct archive_entry *entry = archive_entry_new();
1813         unsigned long set, clear;
1814         const wchar_t *remainder;
1815
1816         remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1817         archive_entry_fflags(entry, &set, &clear);
1818
1819         wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1820
1821         wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
1822         return (0);
1823 }
1824 #endif