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