Import libarchive 2.2.3 fixing a few memory leaks and other fixes.
[dragonfly/port-amd64.git] / contrib / libarchive-2 / libarchive / archive_entry.c
blob908987d420ceb0597afa01274acddacaf57e4be7
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry.c,v 1.42 2007/04/14 02:37:22 kientzle Exp $");
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
69 #include "archive.h"
70 #include "archive_entry.h"
71 #include "archive_private.h"
72 #include "archive_entry_private.h"
74 #undef max
75 #define max(a, b) ((a)>(b)?(a):(b))
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);
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);
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);
108 #ifndef HAVE_WCSCPY
109 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
111 wchar_t *dest = s1;
112 while ((*s1 = *s2) != L'\0')
113 ++s1, ++s2;
114 return dest;
116 #endif
117 #ifndef HAVE_WCSLEN
118 static size_t wcslen(const wchar_t *s)
120 const wchar_t *p = s;
121 while (*p != L'\0')
122 ++p;
123 return p - s;
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
135 static void
136 aes_clean(struct aes *aes)
138 if (aes->aes_mbs_alloc) {
139 free(aes->aes_mbs_alloc);
140 aes->aes_mbs_alloc = NULL;
142 if (aes->aes_wcs_alloc) {
143 free(aes->aes_wcs_alloc);
144 aes->aes_wcs_alloc = NULL;
146 memset(aes, 0, sizeof(*aes));
149 static void
150 aes_copy(struct aes *dest, struct aes *src)
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()");
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);
170 static const char *
171 aes_get_mbs(struct aes *aes)
173 if (aes->aes_mbs == NULL && aes->aes_wcs == NULL)
174 return NULL;
175 if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
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
182 size_t mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
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;
191 return (aes->aes_mbs);
194 static const wchar_t *
195 aes_get_wcs(struct aes *aes)
197 if (aes->aes_wcs == NULL && aes->aes_mbs == NULL)
198 return NULL;
199 if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
201 * No single byte will be more than one wide character,
202 * so this length estimate will always be big enough.
204 size_t wcs_length = strlen(aes->aes_mbs);
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;
214 return (aes->aes_wcs);
217 static void
218 aes_set_mbs(struct aes *aes, const char *mbs)
220 if (aes->aes_mbs_alloc) {
221 free(aes->aes_mbs_alloc);
222 aes->aes_mbs_alloc = NULL;
224 if (aes->aes_wcs_alloc) {
225 free(aes->aes_wcs_alloc);
226 aes->aes_wcs_alloc = NULL;
228 aes->aes_mbs = mbs;
229 aes->aes_wcs = NULL;
232 static void
233 aes_copy_mbs(struct aes *aes, const char *mbs)
235 if (aes->aes_mbs_alloc) {
236 free(aes->aes_mbs_alloc);
237 aes->aes_mbs_alloc = NULL;
239 if (aes->aes_wcs_alloc) {
240 free(aes->aes_wcs_alloc);
241 aes->aes_wcs_alloc = NULL;
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;
251 #if 0
252 static void
253 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
255 if (aes->aes_mbs_alloc) {
256 free(aes->aes_mbs_alloc);
257 aes->aes_mbs_alloc = NULL;
259 if (aes->aes_wcs_alloc) {
260 free(aes->aes_wcs_alloc);
261 aes->aes_wcs_alloc = NULL;
263 aes->aes_mbs = NULL;
264 aes->aes_wcs = wcs;
266 #endif
268 static void
269 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
271 aes_copy_wcs_len(aes, wcs, wcslen(wcs));
274 static void
275 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
277 if (aes->aes_mbs_alloc) {
278 free(aes->aes_mbs_alloc);
279 aes->aes_mbs_alloc = NULL;
281 if (aes->aes_wcs_alloc) {
282 free(aes->aes_wcs_alloc);
283 aes->aes_wcs_alloc = NULL;
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;
294 struct archive_entry *
295 archive_entry_clear(struct archive_entry *entry)
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;
310 struct archive_entry *
311 archive_entry_clone(struct archive_entry *entry)
313 struct archive_entry *entry2;
314 struct ae_acl *ap, *ap2;
315 struct ae_xattr *xp;
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;
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);
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;
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;
351 return (entry2);
354 void
355 archive_entry_free(struct archive_entry *entry)
357 archive_entry_clear(entry);
358 free(entry);
361 struct archive_entry *
362 archive_entry_new(void)
364 struct archive_entry *entry;
366 entry = (struct archive_entry *)malloc(sizeof(*entry));
367 if (entry == NULL)
368 return (NULL);
369 memset(entry, 0, sizeof(*entry));
370 return (entry);
374 * Functions for reading fields from an archive_entry.
377 time_t
378 archive_entry_atime(struct archive_entry *entry)
380 return (entry->ae_stat.aest_atime);
383 long
384 archive_entry_atime_nsec(struct archive_entry *entry)
386 return (entry->ae_stat.aest_atime_nsec);
389 time_t
390 archive_entry_ctime(struct archive_entry *entry)
392 return (entry->ae_stat.aest_ctime);
395 long
396 archive_entry_ctime_nsec(struct archive_entry *entry)
398 return (entry->ae_stat.aest_ctime_nsec);
401 dev_t
402 archive_entry_dev(struct archive_entry *entry)
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);
411 dev_t
412 archive_entry_devmajor(struct archive_entry *entry)
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);
420 dev_t
421 archive_entry_devminor(struct archive_entry *entry)
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);
429 mode_t
430 archive_entry_filetype(struct archive_entry *entry)
432 return (AE_IFMT & entry->ae_stat.aest_mode);
435 void
436 archive_entry_fflags(struct archive_entry *entry,
437 unsigned long *set, unsigned long *clear)
439 *set = entry->ae_fflags_set;
440 *clear = entry->ae_fflags_clear;
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).
452 const char *
453 archive_entry_fflags_text(struct archive_entry *entry)
455 const char *f;
456 char *p;
458 f = aes_get_mbs(&entry->ae_fflags_text);
459 if (f != NULL)
460 return (f);
462 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
463 return (NULL);
465 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
466 if (p == NULL)
467 return (NULL);
469 aes_copy_mbs(&entry->ae_fflags_text, p);
470 free(p);
471 f = aes_get_mbs(&entry->ae_fflags_text);
472 return (f);
475 gid_t
476 archive_entry_gid(struct archive_entry *entry)
478 return (entry->ae_stat.aest_gid);
481 const char *
482 archive_entry_gname(struct archive_entry *entry)
484 return (aes_get_mbs(&entry->ae_gname));
487 const wchar_t *
488 archive_entry_gname_w(struct archive_entry *entry)
490 return (aes_get_wcs(&entry->ae_gname));
493 const char *
494 archive_entry_hardlink(struct archive_entry *entry)
496 return (aes_get_mbs(&entry->ae_hardlink));
499 const wchar_t *
500 archive_entry_hardlink_w(struct archive_entry *entry)
502 return (aes_get_wcs(&entry->ae_hardlink));
505 ino_t
506 archive_entry_ino(struct archive_entry *entry)
508 return (entry->ae_stat.aest_ino);
511 mode_t
512 archive_entry_mode(struct archive_entry *entry)
514 return (entry->ae_stat.aest_mode);
517 time_t
518 archive_entry_mtime(struct archive_entry *entry)
520 return (entry->ae_stat.aest_mtime);
523 long
524 archive_entry_mtime_nsec(struct archive_entry *entry)
526 return (entry->ae_stat.aest_mtime_nsec);
529 unsigned int
530 archive_entry_nlink(struct archive_entry *entry)
532 return (entry->ae_stat.aest_nlink);
535 const char *
536 archive_entry_pathname(struct archive_entry *entry)
538 return (aes_get_mbs(&entry->ae_pathname));
541 const wchar_t *
542 archive_entry_pathname_w(struct archive_entry *entry)
544 return (aes_get_wcs(&entry->ae_pathname));
547 dev_t
548 archive_entry_rdev(struct archive_entry *entry)
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);
557 dev_t
558 archive_entry_rdevmajor(struct archive_entry *entry)
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);
566 dev_t
567 archive_entry_rdevminor(struct archive_entry *entry)
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);
575 int64_t
576 archive_entry_size(struct archive_entry *entry)
578 return (entry->ae_stat.aest_size);
581 const char *
582 archive_entry_symlink(struct archive_entry *entry)
584 return (aes_get_mbs(&entry->ae_symlink));
587 const wchar_t *
588 archive_entry_symlink_w(struct archive_entry *entry)
590 return (aes_get_wcs(&entry->ae_symlink));
593 uid_t
594 archive_entry_uid(struct archive_entry *entry)
596 return (entry->ae_stat.aest_uid);
599 const char *
600 archive_entry_uname(struct archive_entry *entry)
602 return (aes_get_mbs(&entry->ae_uname));
605 const wchar_t *
606 archive_entry_uname_w(struct archive_entry *entry)
608 return (aes_get_wcs(&entry->ae_uname));
612 * Functions to set archive_entry properties.
615 void
616 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
618 entry->stat_valid = 0;
619 entry->ae_stat.aest_mode &= ~AE_IFMT;
620 entry->ae_stat.aest_mode |= AE_IFMT & type;
623 void
624 archive_entry_set_fflags(struct archive_entry *entry,
625 unsigned long set, unsigned long clear)
627 aes_clean(&entry->ae_fflags_text);
628 entry->ae_fflags_set = set;
629 entry->ae_fflags_clear = clear;
632 const wchar_t *
633 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
634 const wchar_t *flags)
636 aes_copy_wcs(&entry->ae_fflags_text, flags);
637 return (ae_wcstofflags(flags,
638 &entry->ae_fflags_set, &entry->ae_fflags_clear));
641 void
642 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
644 entry->stat_valid = 0;
645 entry->ae_stat.aest_gid = g;
648 void
649 archive_entry_set_gname(struct archive_entry *entry, const char *name)
651 aes_set_mbs(&entry->ae_gname, name);
654 void
655 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
657 aes_copy_wcs(&entry->ae_gname, name);
660 void
661 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
663 entry->stat_valid = 0;
664 entry->ae_stat.aest_ino = ino;
667 void
668 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
670 aes_set_mbs(&entry->ae_hardlink, target);
673 void
674 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
676 aes_copy_mbs(&entry->ae_hardlink, target);
679 void
680 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
682 aes_copy_wcs(&entry->ae_hardlink, target);
685 void
686 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
688 entry->stat_valid = 0;
689 entry->ae_stat.aest_atime = t;
690 entry->ae_stat.aest_atime_nsec = ns;
693 void
694 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
696 entry->stat_valid = 0;
697 entry->ae_stat.aest_ctime = t;
698 entry->ae_stat.aest_ctime_nsec = ns;
701 void
702 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
704 entry->stat_valid = 0;
705 entry->ae_stat.aest_dev_is_broken_down = 0;
706 entry->ae_stat.aest_dev = d;
709 void
710 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
712 entry->stat_valid = 0;
713 entry->ae_stat.aest_dev_is_broken_down = 1;
714 entry->ae_stat.aest_devmajor = m;
717 void
718 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
720 entry->stat_valid = 0;
721 entry->ae_stat.aest_dev_is_broken_down = 1;
722 entry->ae_stat.aest_devminor = m;
725 /* Set symlink if symlink is already set, else set hardlink. */
726 void
727 archive_entry_set_link(struct archive_entry *entry, const char *target)
729 if (entry->ae_symlink.aes_mbs != NULL ||
730 entry->ae_symlink.aes_wcs != NULL)
731 aes_set_mbs(&entry->ae_symlink, target);
732 else
733 aes_set_mbs(&entry->ae_hardlink, target);
736 void
737 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
739 entry->stat_valid = 0;
740 entry->ae_stat.aest_mode = m;
743 void
744 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
746 entry->stat_valid = 0;
747 entry->ae_stat.aest_mtime = m;
748 entry->ae_stat.aest_mtime_nsec = ns;
751 void
752 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
754 entry->stat_valid = 0;
755 entry->ae_stat.aest_nlink = nlink;
758 void
759 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
761 aes_set_mbs(&entry->ae_pathname, name);
764 void
765 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
767 aes_copy_mbs(&entry->ae_pathname, name);
770 void
771 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
773 aes_copy_wcs(&entry->ae_pathname, name);
776 void
777 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
779 entry->stat_valid = 0;
780 entry->ae_stat.aest_rdev = m;
781 entry->ae_stat.aest_rdev_is_broken_down = 0;
784 void
785 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
787 entry->stat_valid = 0;
788 entry->ae_stat.aest_rdev_is_broken_down = 1;
789 entry->ae_stat.aest_rdevmajor = m;
792 void
793 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
795 entry->stat_valid = 0;
796 entry->ae_stat.aest_rdev_is_broken_down = 1;
797 entry->ae_stat.aest_rdevminor = m;
800 void
801 archive_entry_set_size(struct archive_entry *entry, int64_t s)
803 entry->stat_valid = 0;
804 entry->ae_stat.aest_size = s;
807 void
808 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
810 aes_set_mbs(&entry->ae_symlink, linkname);
813 void
814 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
816 aes_copy_mbs(&entry->ae_symlink, linkname);
819 void
820 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
822 aes_copy_wcs(&entry->ae_symlink, linkname);
825 void
826 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
828 entry->stat_valid = 0;
829 entry->ae_stat.aest_uid = u;
832 void
833 archive_entry_set_uname(struct archive_entry *entry, const char *name)
835 aes_set_mbs(&entry->ae_uname, name);
838 void
839 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
841 aes_copy_wcs(&entry->ae_uname, name);
845 * ACL management. The following would, of course, be a lot simpler
846 * if: 1) the last draft of POSIX.1e were a really thorough and
847 * complete standard that addressed the needs of ACL archiving and 2)
848 * everyone followed it faithfully. Alas, neither is true, so the
849 * following is a lot more complex than might seem necessary to the
850 * uninitiated.
853 void
854 archive_entry_acl_clear(struct archive_entry *entry)
856 struct ae_acl *ap;
858 while (entry->acl_head != NULL) {
859 ap = entry->acl_head->next;
860 aes_clean(&entry->acl_head->name);
861 free(entry->acl_head);
862 entry->acl_head = ap;
864 if (entry->acl_text_w != NULL) {
865 free(entry->acl_text_w);
866 entry->acl_text_w = NULL;
868 entry->acl_p = NULL;
869 entry->acl_state = 0; /* Not counting. */
873 * Add a single ACL entry to the internal list of ACL data.
875 void
876 archive_entry_acl_add_entry(struct archive_entry *entry,
877 int type, int permset, int tag, int id, const char *name)
879 struct ae_acl *ap;
881 if (acl_special(entry, type, permset, tag) == 0)
882 return;
883 ap = acl_new_entry(entry, type, permset, tag, id);
884 if (ap == NULL) {
885 /* XXX Error XXX */
886 return;
888 if (name != NULL && *name != '\0')
889 aes_copy_mbs(&ap->name, name);
890 else
891 aes_clean(&ap->name);
895 * As above, but with a wide-character name.
897 void
898 archive_entry_acl_add_entry_w(struct archive_entry *entry,
899 int type, int permset, int tag, int id, const wchar_t *name)
901 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
904 void
905 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
906 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
908 struct ae_acl *ap;
910 if (acl_special(entry, type, permset, tag) == 0)
911 return;
912 ap = acl_new_entry(entry, type, permset, tag, id);
913 if (ap == NULL) {
914 /* XXX Error XXX */
915 return;
917 if (name != NULL && *name != L'\0' && len > 0)
918 aes_copy_wcs_len(&ap->name, name, len);
919 else
920 aes_clean(&ap->name);
924 * If this ACL entry is part of the standard POSIX permissions set,
925 * store the permissions in the stat structure and return zero.
927 static int
928 acl_special(struct archive_entry *entry, int type, int permset, int tag)
930 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
931 switch (tag) {
932 case ARCHIVE_ENTRY_ACL_USER_OBJ:
933 entry->ae_stat.aest_mode &= ~0700;
934 entry->ae_stat.aest_mode |= (permset & 7) << 6;
935 return (0);
936 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
937 entry->ae_stat.aest_mode &= ~0070;
938 entry->ae_stat.aest_mode |= (permset & 7) << 3;
939 return (0);
940 case ARCHIVE_ENTRY_ACL_OTHER:
941 entry->ae_stat.aest_mode &= ~0007;
942 entry->ae_stat.aest_mode |= permset & 7;
943 return (0);
946 return (1);
950 * Allocate and populate a new ACL entry with everything but the
951 * name.
953 static struct ae_acl *
954 acl_new_entry(struct archive_entry *entry,
955 int type, int permset, int tag, int id)
957 struct ae_acl *ap;
959 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
960 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
961 return (NULL);
962 if (entry->acl_text_w != NULL) {
963 free(entry->acl_text_w);
964 entry->acl_text_w = NULL;
967 /* XXX TODO: More sanity-checks on the arguments XXX */
969 /* If there's a matching entry already in the list, overwrite it. */
970 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
971 if (ap->type == type && ap->tag == tag && ap->id == id) {
972 ap->permset = permset;
973 return (ap);
977 /* Add a new entry to the list. */
978 ap = (struct ae_acl *)malloc(sizeof(*ap));
979 if (ap == NULL)
980 return (NULL);
981 memset(ap, 0, sizeof(*ap));
982 ap->next = entry->acl_head;
983 entry->acl_head = ap;
984 ap->type = type;
985 ap->tag = tag;
986 ap->id = id;
987 ap->permset = permset;
988 return (ap);
992 * Return a count of entries matching "want_type".
995 archive_entry_acl_count(struct archive_entry *entry, int want_type)
997 int count;
998 struct ae_acl *ap;
1000 count = 0;
1001 ap = entry->acl_head;
1002 while (ap != NULL) {
1003 if ((ap->type & want_type) != 0)
1004 count++;
1005 ap = ap->next;
1008 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1009 count += 3;
1010 return (count);
1014 * Prepare for reading entries from the ACL data. Returns a count
1015 * of entries matching "want_type", or zero if there are no
1016 * non-extended ACL entries of that type.
1019 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1021 int count, cutoff;
1023 count = archive_entry_acl_count(entry, want_type);
1026 * If the only entries are the three standard ones,
1027 * then don't return any ACL data. (In this case,
1028 * client can just use chmod(2) to set permissions.)
1030 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1031 cutoff = 3;
1032 else
1033 cutoff = 0;
1035 if (count > cutoff)
1036 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1037 else
1038 entry->acl_state = 0;
1039 entry->acl_p = entry->acl_head;
1040 return (count);
1044 * Return the next ACL entry in the list. Fake entries for the
1045 * standard permissions and include them in the returned list.
1049 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1050 int *permset, int *tag, int *id, const char **name)
1052 *name = NULL;
1053 *id = -1;
1056 * The acl_state is either zero (no entries available), -1
1057 * (reading from list), or an entry type (retrieve that type
1058 * from ae_stat.aest_mode).
1060 if (entry->acl_state == 0)
1061 return (ARCHIVE_WARN);
1063 /* The first three access entries are special. */
1064 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1065 switch (entry->acl_state) {
1066 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1067 *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1068 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1069 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1070 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1071 return (ARCHIVE_OK);
1072 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1073 *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1074 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1075 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1076 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1077 return (ARCHIVE_OK);
1078 case ARCHIVE_ENTRY_ACL_OTHER:
1079 *permset = entry->ae_stat.aest_mode & 7;
1080 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1081 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1082 entry->acl_state = -1;
1083 entry->acl_p = entry->acl_head;
1084 return (ARCHIVE_OK);
1085 default:
1086 break;
1090 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1091 entry->acl_p = entry->acl_p->next;
1092 if (entry->acl_p == NULL) {
1093 entry->acl_state = 0;
1094 return (ARCHIVE_EOF); /* End of ACL entries. */
1096 *type = entry->acl_p->type;
1097 *permset = entry->acl_p->permset;
1098 *tag = entry->acl_p->tag;
1099 *id = entry->acl_p->id;
1100 *name = aes_get_mbs(&entry->acl_p->name);
1101 entry->acl_p = entry->acl_p->next;
1102 return (ARCHIVE_OK);
1106 * Generate a text version of the ACL. The flags parameter controls
1107 * the style of the generated ACL.
1109 const wchar_t *
1110 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1112 int count;
1113 int length;
1114 const wchar_t *wname;
1115 const wchar_t *prefix;
1116 wchar_t separator;
1117 struct ae_acl *ap;
1118 int id;
1119 wchar_t *wp;
1121 if (entry->acl_text_w != NULL) {
1122 free (entry->acl_text_w);
1123 entry->acl_text_w = NULL;
1126 separator = L',';
1127 count = 0;
1128 length = 0;
1129 ap = entry->acl_head;
1130 while (ap != NULL) {
1131 if ((ap->type & flags) != 0) {
1132 count++;
1133 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1134 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1135 length += 8; /* "default:" */
1136 length += 5; /* tag name */
1137 length += 1; /* colon */
1138 wname = aes_get_wcs(&ap->name);
1139 if (wname != NULL)
1140 length += wcslen(wname);
1141 else
1142 length += sizeof(uid_t) * 3 + 1;
1143 length ++; /* colon */
1144 length += 3; /* rwx */
1145 length += 1; /* colon */
1146 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1147 length ++; /* newline */
1149 ap = ap->next;
1152 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1153 length += 10; /* "user::rwx\n" */
1154 length += 11; /* "group::rwx\n" */
1155 length += 11; /* "other::rwx\n" */
1158 if (count == 0)
1159 return (NULL);
1161 /* Now, allocate the string and actually populate it. */
1162 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1163 if (wp == NULL)
1164 __archive_errx(1, "No memory to generate the text version of the ACL");
1165 count = 0;
1166 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1167 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1168 entry->ae_stat.aest_mode & 0700, -1);
1169 *wp++ = ',';
1170 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1171 entry->ae_stat.aest_mode & 0070, -1);
1172 *wp++ = ',';
1173 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1174 entry->ae_stat.aest_mode & 0007, -1);
1175 count += 3;
1177 ap = entry->acl_head;
1178 while (ap != NULL) {
1179 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1180 wname = aes_get_wcs(&ap->name);
1181 *wp++ = separator;
1182 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1183 id = ap->id;
1184 else
1185 id = -1;
1186 append_entry_w(&wp, NULL, ap->tag, wname,
1187 ap->permset, id);
1188 count++;
1190 ap = ap->next;
1195 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1196 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1197 prefix = L"default:";
1198 else
1199 prefix = NULL;
1200 ap = entry->acl_head;
1201 count = 0;
1202 while (ap != NULL) {
1203 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1204 wname = aes_get_wcs(&ap->name);
1205 if (count > 0)
1206 *wp++ = separator;
1207 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1208 id = ap->id;
1209 else
1210 id = -1;
1211 append_entry_w(&wp, prefix, ap->tag,
1212 wname, ap->permset, id);
1213 count ++;
1215 ap = ap->next;
1219 return (entry->acl_text_w);
1222 static void
1223 append_id_w(wchar_t **wp, int id)
1225 if (id < 0)
1226 id = 0;
1227 if (id > 9)
1228 append_id_w(wp, id / 10);
1229 *(*wp)++ = L"0123456789"[id % 10];
1232 static void
1233 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1234 const wchar_t *wname, int perm, int id)
1236 if (prefix != NULL) {
1237 wcscpy(*wp, prefix);
1238 *wp += wcslen(*wp);
1240 switch (tag) {
1241 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1242 wname = NULL;
1243 id = -1;
1244 /* FALLTHROUGH */
1245 case ARCHIVE_ENTRY_ACL_USER:
1246 wcscpy(*wp, L"user");
1247 break;
1248 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1249 wname = NULL;
1250 id = -1;
1251 /* FALLTHROUGH */
1252 case ARCHIVE_ENTRY_ACL_GROUP:
1253 wcscpy(*wp, L"group");
1254 break;
1255 case ARCHIVE_ENTRY_ACL_MASK:
1256 wcscpy(*wp, L"mask");
1257 wname = NULL;
1258 id = -1;
1259 break;
1260 case ARCHIVE_ENTRY_ACL_OTHER:
1261 wcscpy(*wp, L"other");
1262 wname = NULL;
1263 id = -1;
1264 break;
1266 *wp += wcslen(*wp);
1267 *(*wp)++ = L':';
1268 if (wname != NULL) {
1269 wcscpy(*wp, wname);
1270 *wp += wcslen(*wp);
1271 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1272 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1273 append_id_w(wp, id);
1274 id = -1;
1276 *(*wp)++ = L':';
1277 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1278 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1279 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1280 if (id != -1) {
1281 *(*wp)++ = L':';
1282 append_id_w(wp, id);
1284 **wp = L'\0';
1288 * Parse a textual ACL. This automatically recognizes and supports
1289 * extensions described above. The 'type' argument is used to
1290 * indicate the type that should be used for any entries not
1291 * explicitly marked as "default:".
1294 __archive_entry_acl_parse_w(struct archive_entry *entry,
1295 const wchar_t *text, int default_type)
1297 struct {
1298 const wchar_t *start;
1299 const wchar_t *end;
1300 } field[4];
1302 int fields;
1303 int type, tag, permset, id;
1304 const wchar_t *p;
1305 wchar_t sep;
1307 while (text != NULL && *text != L'\0') {
1309 * Parse the fields out of the next entry,
1310 * advance 'text' to start of next entry.
1312 fields = 0;
1313 do {
1314 const wchar_t *start, *end;
1315 next_field_w(&text, &start, &end, &sep);
1316 if (fields < 4) {
1317 field[fields].start = start;
1318 field[fields].end = end;
1320 ++fields;
1321 } while (sep == L':');
1323 if (fields < 3)
1324 return (ARCHIVE_WARN);
1326 /* Check for a numeric ID in field 1 or 3. */
1327 id = -1;
1328 isint_w(field[1].start, field[1].end, &id);
1329 /* Field 3 is optional. */
1330 if (id == -1 && fields > 3)
1331 isint_w(field[3].start, field[3].end, &id);
1333 /* Parse the permissions from field 2. */
1334 permset = 0;
1335 p = field[2].start;
1336 while (p < field[2].end) {
1337 switch (*p++) {
1338 case 'r': case 'R':
1339 permset |= ARCHIVE_ENTRY_ACL_READ;
1340 break;
1341 case 'w': case 'W':
1342 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1343 break;
1344 case 'x': case 'X':
1345 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1346 break;
1347 case '-':
1348 break;
1349 default:
1350 return (ARCHIVE_WARN);
1355 * Solaris extension: "defaultuser::rwx" is the
1356 * default ACL corresponding to "user::rwx", etc.
1358 if (field[0].end-field[0].start > 7
1359 && wmemcmp(field[0].start, L"default", 7) == 0) {
1360 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1361 field[0].start += 7;
1362 } else
1363 type = default_type;
1365 if (prefix_w(field[0].start, field[0].end, L"user")) {
1366 if (id != -1 || field[1].start < field[1].end)
1367 tag = ARCHIVE_ENTRY_ACL_USER;
1368 else
1369 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1370 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1371 if (id != -1 || field[1].start < field[1].end)
1372 tag = ARCHIVE_ENTRY_ACL_GROUP;
1373 else
1374 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1375 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1376 if (id != -1 || field[1].start < field[1].end)
1377 return (ARCHIVE_WARN);
1378 tag = ARCHIVE_ENTRY_ACL_OTHER;
1379 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1380 if (id != -1 || field[1].start < field[1].end)
1381 return (ARCHIVE_WARN);
1382 tag = ARCHIVE_ENTRY_ACL_MASK;
1383 } else
1384 return (ARCHIVE_WARN);
1386 /* Add entry to the internal list. */
1387 archive_entry_acl_add_entry_w_len(entry, type, permset,
1388 tag, id, field[1].start, field[1].end - field[1].start);
1390 return (ARCHIVE_OK);
1394 * extended attribute handling
1397 void
1398 archive_entry_xattr_clear(struct archive_entry *entry)
1400 struct ae_xattr *xp;
1402 while (entry->xattr_head != NULL) {
1403 xp = entry->xattr_head->next;
1404 free(entry->xattr_head->name);
1405 free(entry->xattr_head->value);
1406 free(entry->xattr_head);
1407 entry->xattr_head = xp;
1410 entry->xattr_head = NULL;
1413 void
1414 archive_entry_xattr_add_entry(struct archive_entry *entry,
1415 const char *name, const void *value, size_t size)
1417 struct ae_xattr *xp;
1419 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1422 if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1423 /* XXX Error XXX */
1424 return;
1426 xp->name = strdup(name);
1427 if ((xp->value = malloc(size)) != NULL) {
1428 memcpy(xp->value, value, size);
1429 xp->size = size;
1430 } else
1431 xp->size = 0;
1433 xp->next = entry->xattr_head;
1434 entry->xattr_head = xp;
1439 * returns number of the extended attribute entries
1442 archive_entry_xattr_count(struct archive_entry *entry)
1444 struct ae_xattr *xp;
1445 int count = 0;
1447 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1448 count++;
1450 return count;
1454 archive_entry_xattr_reset(struct archive_entry * entry)
1456 entry->xattr_p = entry->xattr_head;
1458 return archive_entry_xattr_count(entry);
1462 archive_entry_xattr_next(struct archive_entry * entry,
1463 const char **name, const void **value, size_t *size)
1465 if (entry->xattr_p) {
1466 *name = entry->xattr_p->name;
1467 *value = entry->xattr_p->value;
1468 *size = entry->xattr_p->size;
1470 entry->xattr_p = entry->xattr_p->next;
1472 return (ARCHIVE_OK);
1473 } else {
1474 *name = NULL;
1475 *name = NULL;
1476 *size = (size_t)0;
1477 return (ARCHIVE_WARN);
1482 * end of xattr handling
1486 * Parse a string to a positive decimal integer. Returns true if
1487 * the string is non-empty and consists only of decimal digits,
1488 * false otherwise.
1490 static int
1491 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1493 int n = 0;
1494 if (start >= end)
1495 return (0);
1496 while (start < end) {
1497 if (*start < '0' || *start > '9')
1498 return (0);
1499 if (n > (INT_MAX / 10))
1500 n = INT_MAX;
1501 else {
1502 n *= 10;
1503 n += *start - '0';
1505 start++;
1507 *result = n;
1508 return (1);
1512 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1513 * to point to just after the separator. *start points to the first
1514 * character of the matched text and *end just after the last
1515 * character of the matched identifier. In particular *end - *start
1516 * is the length of the field body, not including leading or trailing
1517 * whitespace.
1519 static void
1520 next_field_w(const wchar_t **wp, const wchar_t **start,
1521 const wchar_t **end, wchar_t *sep)
1523 /* Skip leading whitespace to find start of field. */
1524 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1525 (*wp)++;
1527 *start = *wp;
1529 /* Scan for the separator. */
1530 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1531 **wp != L'\n') {
1532 (*wp)++;
1534 *sep = **wp;
1536 /* Trim trailing whitespace to locate end of field. */
1537 *end = *wp - 1;
1538 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1539 (*end)--;
1541 (*end)++;
1543 /* Adjust scanner location. */
1544 if (**wp != L'\0')
1545 (*wp)++;
1549 * Return true if the characters [start...end) are a prefix of 'test'.
1550 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1552 static int
1553 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1555 if (start == end)
1556 return (0);
1558 if (*start++ != *test++)
1559 return (0);
1561 while (start < end && *start++ == *test++)
1564 if (start < end)
1565 return (0);
1567 return (1);
1572 * Following code is modified from UC Berkeley sources, and
1573 * is subject to the following copyright notice.
1577 * Copyright (c) 1993
1578 * The Regents of the University of California. All rights reserved.
1580 * Redistribution and use in source and binary forms, with or without
1581 * modification, are permitted provided that the following conditions
1582 * are met:
1583 * 1. Redistributions of source code must retain the above copyright
1584 * notice, this list of conditions and the following disclaimer.
1585 * 2. Redistributions in binary form must reproduce the above copyright
1586 * notice, this list of conditions and the following disclaimer in the
1587 * documentation and/or other materials provided with the distribution.
1588 * 4. Neither the name of the University nor the names of its contributors
1589 * may be used to endorse or promote products derived from this software
1590 * without specific prior written permission.
1592 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1593 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1594 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1595 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1596 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1597 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1598 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1599 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1600 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1601 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1602 * SUCH DAMAGE.
1605 static struct flag {
1606 const char *name;
1607 const wchar_t *wname;
1608 unsigned long set;
1609 unsigned long clear;
1610 } flags[] = {
1611 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1612 #ifdef SF_APPEND
1613 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1614 { "nosappend", L"nosappend", SF_APPEND, 0 },
1615 #endif
1616 #ifdef EXT2_APPEND_FL /* 'a' */
1617 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1618 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1619 #endif
1620 #ifdef SF_ARCHIVED
1621 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1622 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1623 #endif
1624 #ifdef SF_IMMUTABLE
1625 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1626 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1627 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1628 #endif
1629 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1630 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1631 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1632 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1633 #endif
1634 #ifdef SF_NOUNLINK
1635 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1636 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1637 #endif
1638 #ifdef SF_SNAPSHOT
1639 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1640 #endif
1641 #ifdef UF_APPEND
1642 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1643 { "nouappend", L"nouappend", UF_APPEND, 0 },
1644 #endif
1645 #ifdef UF_IMMUTABLE
1646 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1647 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1648 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1649 #endif
1650 #ifdef UF_NODUMP
1651 { "nodump", L"nodump", 0, UF_NODUMP},
1652 #endif
1653 #ifdef EXT2_NODUMP_FL /* 'd' */
1654 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1655 #endif
1656 #ifdef UF_OPAQUE
1657 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1658 #endif
1659 #ifdef UF_NOUNLINK
1660 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1661 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1662 #endif
1663 #ifdef EXT2_COMPR_FL /* 'c' */
1664 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1665 #endif
1667 #ifdef EXT2_NOATIME_FL /* 'A' */
1668 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1669 #endif
1670 { NULL, NULL, 0, 0 }
1674 * fflagstostr --
1675 * Convert file flags to a comma-separated string. If no flags
1676 * are set, return the empty string.
1678 char *
1679 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1681 char *string, *dp;
1682 const char *sp;
1683 unsigned long bits;
1684 struct flag *flag;
1685 size_t length;
1687 bits = bitset | bitclear;
1688 length = 0;
1689 for (flag = flags; flag->name != NULL; flag++)
1690 if (bits & (flag->set | flag->clear)) {
1691 length += strlen(flag->name) + 1;
1692 bits &= ~(flag->set | flag->clear);
1695 if (length == 0)
1696 return (NULL);
1697 string = (char *)malloc(length);
1698 if (string == NULL)
1699 return (NULL);
1701 dp = string;
1702 for (flag = flags; flag->name != NULL; flag++) {
1703 if (bitset & flag->set || bitclear & flag->clear) {
1704 sp = flag->name + 2;
1705 } else if (bitset & flag->clear || bitclear & flag->set) {
1706 sp = flag->name;
1707 } else
1708 continue;
1709 bitset &= ~(flag->set | flag->clear);
1710 bitclear &= ~(flag->set | flag->clear);
1711 if (dp > string)
1712 *dp++ = ',';
1713 while ((*dp++ = *sp++) != '\0')
1715 dp--;
1718 *dp = '\0';
1719 return (string);
1723 * wcstofflags --
1724 * Take string of arguments and return file flags. This
1725 * version works a little differently than strtofflags(3).
1726 * In particular, it always tests every token, skipping any
1727 * unrecognized tokens. It returns a pointer to the first
1728 * unrecognized token, or NULL if every token was recognized.
1729 * This version is also const-correct and does not modify the
1730 * provided string.
1732 const wchar_t *
1733 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1735 const wchar_t *start, *end;
1736 struct flag *flag;
1737 unsigned long set, clear;
1738 const wchar_t *failed;
1740 set = clear = 0;
1741 start = s;
1742 failed = NULL;
1743 /* Find start of first token. */
1744 while (*start == L'\t' || *start == L' ' || *start == L',')
1745 start++;
1746 while (*start != L'\0') {
1747 /* Locate end of token. */
1748 end = start;
1749 while (*end != L'\0' && *end != L'\t' &&
1750 *end != L' ' && *end != L',')
1751 end++;
1752 for (flag = flags; flag->wname != NULL; flag++) {
1753 if (wmemcmp(start, flag->wname, end - start) == 0) {
1754 /* Matched "noXXXX", so reverse the sense. */
1755 clear |= flag->set;
1756 set |= flag->clear;
1757 break;
1758 } else if (wmemcmp(start, flag->wname + 2, end - start)
1759 == 0) {
1760 /* Matched "XXXX", so don't reverse. */
1761 set |= flag->set;
1762 clear |= flag->clear;
1763 break;
1766 /* Ignore unknown flag names. */
1767 if (flag->wname == NULL && failed == NULL)
1768 failed = start;
1770 /* Find start of next token. */
1771 start = end;
1772 while (*start == L'\t' || *start == L' ' || *start == L',')
1773 start++;
1777 if (setp)
1778 *setp = set;
1779 if (clrp)
1780 *clrp = clear;
1782 /* Return location of first failure. */
1783 return (failed);
1787 #ifdef TEST
1788 #include <stdio.h>
1790 main(int argc, char **argv)
1792 struct archive_entry *entry = archive_entry_new();
1793 unsigned long set, clear;
1794 const wchar_t *remainder;
1796 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1797 archive_entry_fflags(entry, &set, &clear);
1799 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1801 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
1802 return (0);
1804 #endif