2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.52 2008/05/26 17:00:22 kientzle Exp $");
29 #ifdef HAVE_SYS_STAT_H
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
36 #include <sys/mkdev.h>
38 #ifdef MAJOR_IN_SYSMACROS
39 #include <sys/sysmacros.h>
45 #ifdef HAVE_LINUX_FS_H
46 #include <linux/fs.h> /* for Linux file flags */
48 #ifdef HAVE_LINUX_EXT2_FS_H
49 #include <linux/ext2_fs.h> /* for Linux file flags */
51 #ifdef HAVE_EXT2FS_EXT2_FS_H
53 * This must be after the include of linux/ext2_fs.h,
54 * otherwise the former will fail.
56 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
71 #include "archive_entry.h"
72 #include "archive_private.h"
73 #include "archive_entry_private.h"
76 #define max(a, b) ((a)>(b)?(a):(b))
78 /* Play games to come up with a suitable makedev() definition. */
81 #include <sys/netmgr.h>
82 #define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
84 /* There's a "makedev" macro. */
85 #define ae_makedev(maj, min) makedev((maj), (min))
86 #elif defined mkdev || defined _WIN32 || defined __WIN32__
88 #define ae_makedev(maj, min) mkdev((maj), (min))
90 /* There's a "makedev" function. */
91 #define ae_makedev(maj, min) makedev((maj), (min))
94 static void aes_clean(struct aes *);
95 static void aes_copy(struct aes *dest, struct aes *src);
96 static const char * aes_get_mbs(struct aes *);
97 static const wchar_t * aes_get_wcs(struct aes *);
98 static int aes_set_mbs(struct aes *, const char *mbs);
99 static int aes_copy_mbs(struct aes *, const char *mbs);
100 /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
101 static int aes_copy_wcs(struct aes *, const wchar_t *wcs);
102 static int aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
104 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
105 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
106 unsigned long *setp, unsigned long *clrp);
107 static const char *ae_strtofflags(const char *stringp,
108 unsigned long *setp, unsigned long *clrp);
109 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
110 const wchar_t *wname, int perm, int id);
111 static void append_id_w(wchar_t **wp, int id);
113 static int acl_special(struct archive_entry *entry,
114 int type, int permset, int tag);
115 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
116 int type, int permset, int tag, int id);
117 static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
118 static void next_field_w(const wchar_t **wp, const wchar_t **start,
119 const wchar_t **end, wchar_t *sep);
120 static int prefix_w(const wchar_t *start, const wchar_t *end,
121 const wchar_t *test);
123 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
124 int permset, int tag, int id, const wchar_t *name, size_t);
128 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
131 while ((*s1 = *s2) != L'\0')
137 static size_t wcslen(const wchar_t *s)
139 const wchar_t *p = s;
146 /* Good enough for simple equality testing, but not for sorting. */
147 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
150 #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
154 aes_clean(struct aes *aes)
157 free((wchar_t *)(uintptr_t)aes->aes_wcs);
160 archive_string_free(&(aes->aes_mbs));
161 archive_string_free(&(aes->aes_utf8));
166 aes_copy(struct aes *dest, struct aes *src)
170 dest->aes_set = src->aes_set;
171 archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
172 archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
174 if (src->aes_wcs != NULL) {
175 wp = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
178 __archive_errx(1, "No memory for aes_copy()");
179 wcscpy(wp, src->aes_wcs);
185 aes_get_utf8(struct aes *aes)
187 if (aes->aes_set & AES_SET_UTF8)
188 return (aes->aes_utf8.s);
189 if ((aes->aes_set & AES_SET_WCS)
190 && archive_strappend_w_utf8(&(aes->aes_utf8), aes->aes_wcs) != NULL) {
191 aes->aes_set |= AES_SET_UTF8;
192 return (aes->aes_utf8.s);
198 aes_get_mbs(struct aes *aes)
200 /* If we already have an MBS form, return that immediately. */
201 if (aes->aes_set & AES_SET_MBS)
202 return (aes->aes_mbs.s);
203 /* If there's a WCS form, try converting with the native locale. */
204 if ((aes->aes_set & AES_SET_WCS)
205 && archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) != NULL) {
206 aes->aes_set |= AES_SET_MBS;
207 return (aes->aes_mbs.s);
209 /* We'll use UTF-8 for MBS if all else fails. */
210 return (aes_get_utf8(aes));
213 static const wchar_t *
214 aes_get_wcs(struct aes *aes)
219 /* Return WCS form if we already have it. */
220 if (aes->aes_set & AES_SET_WCS)
221 return (aes->aes_wcs);
223 if (aes->aes_set & AES_SET_MBS) {
224 /* Try converting MBS to WCS using native locale. */
226 * No single byte will be more than one wide character,
227 * so this length estimate will always be big enough.
229 size_t wcs_length = aes->aes_mbs.length;
231 w = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
233 __archive_errx(1, "No memory for aes_get_wcs()");
234 r = mbstowcs(w, aes->aes_mbs.s, wcs_length);
237 aes->aes_set |= AES_SET_WCS;
238 return (aes->aes_wcs = w);
243 if (aes->aes_set & AES_SET_UTF8) {
244 /* Try converting UTF8 to WCS. */
245 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
246 aes->aes_set |= AES_SET_WCS;
247 return (aes->aes_wcs);
253 aes_set_mbs(struct aes *aes, const char *mbs)
255 return (aes_copy_mbs(aes, mbs));
259 aes_copy_mbs(struct aes *aes, const char *mbs)
265 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
266 archive_strcpy(&(aes->aes_mbs), mbs);
267 archive_string_empty(&(aes->aes_utf8));
269 free((wchar_t *)(uintptr_t)aes->aes_wcs);
276 * The 'update' form tries to proactively update all forms of
277 * this string (WCS and MBS) and returns an error if any of
278 * them fail. This is used by the 'pax' handler, for instance,
279 * to detect and report character-conversion failures early while
280 * still allowing clients to get potentially useful values from
281 * the more tolerant lazy conversions. (get_mbs and get_wcs will
282 * strive to give the user something useful, so you can get hopefully
283 * usable values even if some of the character conversions are failing.)
286 aes_update_utf8(struct aes *aes, const char *utf8)
290 return (1); /* Succeeded in clearing everything. */
293 /* Save the UTF8 string. */
294 archive_strcpy(&(aes->aes_utf8), utf8);
296 /* Empty the mbs and wcs strings. */
297 archive_string_empty(&(aes->aes_mbs));
299 free((wchar_t *)(uintptr_t)aes->aes_wcs);
303 aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
305 /* TODO: We should just do a direct UTF-8 to MBS conversion
306 * here. That would be faster, use less space, and give the
307 * same information. (If a UTF-8 to MBS conversion succeeds,
308 * then UTF-8->WCS and Unicode->MBS conversions will both
311 /* Try converting UTF8 to WCS, return false on failure. */
312 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
313 if (aes->aes_wcs == NULL)
315 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS; /* Both UTF8 and WCS set. */
317 /* Try converting WCS to MBS, return false on failure. */
318 if (archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) == NULL)
320 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
322 /* All conversions succeeded. */
327 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
329 return aes_copy_wcs_len(aes, wcs, wcs == NULL ? 0 : wcslen(wcs));
333 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
341 aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
342 archive_string_empty(&(aes->aes_mbs));
343 archive_string_empty(&(aes->aes_utf8));
345 free((wchar_t *)(uintptr_t)aes->aes_wcs);
348 w = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
350 __archive_errx(1, "No memory for aes_copy_wcs()");
351 wmemcpy(w, wcs, len);
357 /****************************************************************************
361 ****************************************************************************/
363 struct archive_entry *
364 archive_entry_clear(struct archive_entry *entry)
368 aes_clean(&entry->ae_fflags_text);
369 aes_clean(&entry->ae_gname);
370 aes_clean(&entry->ae_hardlink);
371 aes_clean(&entry->ae_pathname);
372 aes_clean(&entry->ae_symlink);
373 aes_clean(&entry->ae_uname);
374 archive_entry_acl_clear(entry);
375 archive_entry_xattr_clear(entry);
377 memset(entry, 0, sizeof(*entry));
381 struct archive_entry *
382 archive_entry_clone(struct archive_entry *entry)
384 struct archive_entry *entry2;
385 struct ae_acl *ap, *ap2;
388 /* Allocate new structure and copy over all of the fields. */
389 entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
392 memset(entry2, 0, sizeof(*entry2));
393 entry2->ae_stat = entry->ae_stat;
394 entry2->ae_fflags_set = entry->ae_fflags_set;
395 entry2->ae_fflags_clear = entry->ae_fflags_clear;
397 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
398 aes_copy(&entry2->ae_gname, &entry->ae_gname);
399 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
400 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
401 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
402 entry2->ae_hardlinkset = entry->ae_hardlinkset;
403 entry2->ae_symlinkset = entry->ae_symlinkset;
404 aes_copy(&entry2->ae_uname, &entry->ae_uname);
406 /* Copy ACL data over. */
407 ap = entry->acl_head;
409 ap2 = acl_new_entry(entry2,
410 ap->type, ap->permset, ap->tag, ap->id);
412 aes_copy(&ap2->name, &ap->name);
416 /* Copy xattr data over. */
417 xp = entry->xattr_head;
419 archive_entry_xattr_add_entry(entry2,
420 xp->name, xp->value, xp->size);
428 archive_entry_free(struct archive_entry *entry)
430 archive_entry_clear(entry);
434 struct archive_entry *
435 archive_entry_new(void)
437 struct archive_entry *entry;
439 entry = (struct archive_entry *)malloc(sizeof(*entry));
442 memset(entry, 0, sizeof(*entry));
447 * Functions for reading fields from an archive_entry.
451 archive_entry_atime(struct archive_entry *entry)
453 return (entry->ae_stat.aest_atime);
457 archive_entry_atime_nsec(struct archive_entry *entry)
459 return (entry->ae_stat.aest_atime_nsec);
463 archive_entry_ctime(struct archive_entry *entry)
465 return (entry->ae_stat.aest_ctime);
469 archive_entry_ctime_nsec(struct archive_entry *entry)
471 return (entry->ae_stat.aest_ctime_nsec);
475 archive_entry_dev(struct archive_entry *entry)
477 if (entry->ae_stat.aest_dev_is_broken_down)
478 return ae_makedev(entry->ae_stat.aest_devmajor,
479 entry->ae_stat.aest_devminor);
481 return (entry->ae_stat.aest_dev);
485 archive_entry_devmajor(struct archive_entry *entry)
487 if (entry->ae_stat.aest_dev_is_broken_down)
488 return (entry->ae_stat.aest_devmajor);
490 return major(entry->ae_stat.aest_dev);
494 archive_entry_devminor(struct archive_entry *entry)
496 if (entry->ae_stat.aest_dev_is_broken_down)
497 return (entry->ae_stat.aest_devminor);
499 return minor(entry->ae_stat.aest_dev);
503 archive_entry_filetype(struct archive_entry *entry)
505 return (AE_IFMT & entry->ae_stat.aest_mode);
509 archive_entry_fflags(struct archive_entry *entry,
510 unsigned long *set, unsigned long *clear)
512 *set = entry->ae_fflags_set;
513 *clear = entry->ae_fflags_clear;
517 * Note: if text was provided, this just returns that text. If you
518 * really need the text to be rebuilt in a canonical form, set the
519 * text, ask for the bitmaps, then set the bitmaps. (Setting the
520 * bitmaps clears any stored text.) This design is deliberate: if
521 * we're editing archives, we don't want to discard flags just because
522 * they aren't supported on the current system. The bitmap<->text
523 * conversions are platform-specific (see below).
526 archive_entry_fflags_text(struct archive_entry *entry)
531 f = aes_get_mbs(&entry->ae_fflags_text);
535 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
538 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
542 aes_copy_mbs(&entry->ae_fflags_text, p);
544 f = aes_get_mbs(&entry->ae_fflags_text);
549 archive_entry_gid(struct archive_entry *entry)
551 return (entry->ae_stat.aest_gid);
555 archive_entry_gname(struct archive_entry *entry)
557 return (aes_get_mbs(&entry->ae_gname));
561 archive_entry_gname_w(struct archive_entry *entry)
563 return (aes_get_wcs(&entry->ae_gname));
567 archive_entry_hardlink(struct archive_entry *entry)
569 if (!entry->ae_hardlinkset)
571 return (aes_get_mbs(&entry->ae_hardlink));
575 archive_entry_hardlink_w(struct archive_entry *entry)
577 if (!entry->ae_hardlinkset)
579 return (aes_get_wcs(&entry->ae_hardlink));
583 archive_entry_ino(struct archive_entry *entry)
585 return (entry->ae_stat.aest_ino);
589 archive_entry_mode(struct archive_entry *entry)
591 return (entry->ae_stat.aest_mode);
595 archive_entry_mtime(struct archive_entry *entry)
597 return (entry->ae_stat.aest_mtime);
601 archive_entry_mtime_nsec(struct archive_entry *entry)
603 return (entry->ae_stat.aest_mtime_nsec);
607 archive_entry_nlink(struct archive_entry *entry)
609 return (entry->ae_stat.aest_nlink);
613 archive_entry_pathname(struct archive_entry *entry)
615 return (aes_get_mbs(&entry->ae_pathname));
619 archive_entry_pathname_w(struct archive_entry *entry)
621 return (aes_get_wcs(&entry->ae_pathname));
625 archive_entry_rdev(struct archive_entry *entry)
627 if (entry->ae_stat.aest_rdev_is_broken_down)
628 return ae_makedev(entry->ae_stat.aest_rdevmajor,
629 entry->ae_stat.aest_rdevminor);
631 return (entry->ae_stat.aest_rdev);
635 archive_entry_rdevmajor(struct archive_entry *entry)
637 if (entry->ae_stat.aest_rdev_is_broken_down)
638 return (entry->ae_stat.aest_rdevmajor);
640 return major(entry->ae_stat.aest_rdev);
644 archive_entry_rdevminor(struct archive_entry *entry)
646 if (entry->ae_stat.aest_rdev_is_broken_down)
647 return (entry->ae_stat.aest_rdevminor);
649 return minor(entry->ae_stat.aest_rdev);
653 archive_entry_size(struct archive_entry *entry)
655 return (entry->ae_stat.aest_size);
659 archive_entry_sourcepath(struct archive_entry *entry)
661 return (aes_get_mbs(&entry->ae_sourcepath));
665 archive_entry_symlink(struct archive_entry *entry)
667 if (!entry->ae_symlinkset)
669 return (aes_get_mbs(&entry->ae_symlink));
673 archive_entry_symlink_w(struct archive_entry *entry)
675 if (!entry->ae_symlinkset)
677 return (aes_get_wcs(&entry->ae_symlink));
681 archive_entry_uid(struct archive_entry *entry)
683 return (entry->ae_stat.aest_uid);
687 archive_entry_uname(struct archive_entry *entry)
689 return (aes_get_mbs(&entry->ae_uname));
693 archive_entry_uname_w(struct archive_entry *entry)
695 return (aes_get_wcs(&entry->ae_uname));
699 * Functions to set archive_entry properties.
703 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
705 entry->stat_valid = 0;
706 entry->ae_stat.aest_mode &= ~AE_IFMT;
707 entry->ae_stat.aest_mode |= AE_IFMT & type;
711 archive_entry_set_fflags(struct archive_entry *entry,
712 unsigned long set, unsigned long clear)
714 aes_clean(&entry->ae_fflags_text);
715 entry->ae_fflags_set = set;
716 entry->ae_fflags_clear = clear;
720 archive_entry_copy_fflags_text(struct archive_entry *entry,
723 aes_copy_mbs(&entry->ae_fflags_text, flags);
724 return (ae_strtofflags(flags,
725 &entry->ae_fflags_set, &entry->ae_fflags_clear));
729 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
730 const wchar_t *flags)
732 aes_copy_wcs(&entry->ae_fflags_text, flags);
733 return (ae_wcstofflags(flags,
734 &entry->ae_fflags_set, &entry->ae_fflags_clear));
738 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
740 entry->stat_valid = 0;
741 entry->ae_stat.aest_gid = g;
745 archive_entry_set_gname(struct archive_entry *entry, const char *name)
747 aes_set_mbs(&entry->ae_gname, name);
751 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
753 aes_copy_mbs(&entry->ae_gname, name);
757 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
759 aes_copy_wcs(&entry->ae_gname, name);
763 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
765 return (aes_update_utf8(&entry->ae_gname, name));
769 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
771 entry->stat_valid = 0;
772 entry->ae_stat.aest_ino = ino;
776 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
778 aes_set_mbs(&entry->ae_hardlink, target);
780 entry->ae_hardlinkset = 1;
784 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
786 aes_copy_mbs(&entry->ae_hardlink, target);
788 entry->ae_hardlinkset = 1;
792 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
794 aes_copy_wcs(&entry->ae_hardlink, target);
796 entry->ae_hardlinkset = 1;
800 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
802 entry->stat_valid = 0;
803 entry->ae_stat.aest_atime = t;
804 entry->ae_stat.aest_atime_nsec = ns;
808 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
810 entry->stat_valid = 0;
811 entry->ae_stat.aest_ctime = t;
812 entry->ae_stat.aest_ctime_nsec = ns;
816 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
818 entry->stat_valid = 0;
819 entry->ae_stat.aest_dev_is_broken_down = 0;
820 entry->ae_stat.aest_dev = d;
824 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
826 entry->stat_valid = 0;
827 entry->ae_stat.aest_dev_is_broken_down = 1;
828 entry->ae_stat.aest_devmajor = m;
832 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
834 entry->stat_valid = 0;
835 entry->ae_stat.aest_dev_is_broken_down = 1;
836 entry->ae_stat.aest_devminor = m;
839 /* Set symlink if symlink is already set, else set hardlink. */
841 archive_entry_set_link(struct archive_entry *entry, const char *target)
843 if (entry->ae_symlinkset)
844 aes_set_mbs(&entry->ae_symlink, target);
846 aes_set_mbs(&entry->ae_hardlink, target);
849 /* Set symlink if symlink is already set, else set hardlink. */
851 archive_entry_copy_link(struct archive_entry *entry, const char *target)
853 if (entry->ae_symlinkset)
854 aes_copy_mbs(&entry->ae_symlink, target);
856 aes_copy_mbs(&entry->ae_hardlink, target);
859 /* Set symlink if symlink is already set, else set hardlink. */
861 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
863 if (entry->ae_symlinkset)
864 aes_copy_wcs(&entry->ae_symlink, target);
866 aes_copy_wcs(&entry->ae_hardlink, target);
870 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
872 if (entry->ae_symlinkset)
873 return (aes_update_utf8(&entry->ae_symlink, target));
875 return (aes_update_utf8(&entry->ae_hardlink, target));
879 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
881 entry->stat_valid = 0;
882 entry->ae_stat.aest_mode = m;
886 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
888 entry->stat_valid = 0;
889 entry->ae_stat.aest_mtime = m;
890 entry->ae_stat.aest_mtime_nsec = ns;
894 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
896 entry->stat_valid = 0;
897 entry->ae_stat.aest_nlink = nlink;
901 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
903 aes_set_mbs(&entry->ae_pathname, name);
907 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
909 aes_copy_mbs(&entry->ae_pathname, name);
913 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
915 aes_copy_wcs(&entry->ae_pathname, name);
919 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
921 return (aes_update_utf8(&entry->ae_pathname, name));
925 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
927 entry->stat_valid = 0;
928 entry->ae_stat.aest_mode &= AE_IFMT;
929 entry->ae_stat.aest_mode |= ~AE_IFMT & p;
933 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
935 entry->stat_valid = 0;
936 entry->ae_stat.aest_rdev = m;
937 entry->ae_stat.aest_rdev_is_broken_down = 0;
941 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
943 entry->stat_valid = 0;
944 entry->ae_stat.aest_rdev_is_broken_down = 1;
945 entry->ae_stat.aest_rdevmajor = m;
949 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
951 entry->stat_valid = 0;
952 entry->ae_stat.aest_rdev_is_broken_down = 1;
953 entry->ae_stat.aest_rdevminor = m;
957 archive_entry_set_size(struct archive_entry *entry, int64_t s)
959 entry->stat_valid = 0;
960 entry->ae_stat.aest_size = s;
964 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
966 aes_set_mbs(&entry->ae_sourcepath, path);
970 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
972 aes_set_mbs(&entry->ae_symlink, linkname);
973 if (linkname != NULL)
974 entry->ae_symlinkset = 1;
978 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
980 aes_copy_mbs(&entry->ae_symlink, linkname);
981 if (linkname != NULL)
982 entry->ae_symlinkset = 1;
986 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
988 aes_copy_wcs(&entry->ae_symlink, linkname);
989 if (linkname != NULL)
990 entry->ae_symlinkset = 1;
994 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
996 entry->stat_valid = 0;
997 entry->ae_stat.aest_uid = u;
1001 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1003 aes_set_mbs(&entry->ae_uname, name);
1007 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1009 aes_copy_mbs(&entry->ae_uname, name);
1013 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1015 aes_copy_wcs(&entry->ae_uname, name);
1019 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1021 return (aes_update_utf8(&entry->ae_uname, name));
1025 * ACL management. The following would, of course, be a lot simpler
1026 * if: 1) the last draft of POSIX.1e were a really thorough and
1027 * complete standard that addressed the needs of ACL archiving and 2)
1028 * everyone followed it faithfully. Alas, neither is true, so the
1029 * following is a lot more complex than might seem necessary to the
1034 archive_entry_acl_clear(struct archive_entry *entry)
1038 while (entry->acl_head != NULL) {
1039 ap = entry->acl_head->next;
1040 aes_clean(&entry->acl_head->name);
1041 free(entry->acl_head);
1042 entry->acl_head = ap;
1044 if (entry->acl_text_w != NULL) {
1045 free(entry->acl_text_w);
1046 entry->acl_text_w = NULL;
1048 entry->acl_p = NULL;
1049 entry->acl_state = 0; /* Not counting. */
1053 * Add a single ACL entry to the internal list of ACL data.
1056 archive_entry_acl_add_entry(struct archive_entry *entry,
1057 int type, int permset, int tag, int id, const char *name)
1061 if (acl_special(entry, type, permset, tag) == 0)
1063 ap = acl_new_entry(entry, type, permset, tag, id);
1068 if (name != NULL && *name != '\0')
1069 aes_copy_mbs(&ap->name, name);
1071 aes_clean(&ap->name);
1075 * As above, but with a wide-character name.
1078 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1079 int type, int permset, int tag, int id, const wchar_t *name)
1081 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
1085 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
1086 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
1090 if (acl_special(entry, type, permset, tag) == 0)
1092 ap = acl_new_entry(entry, type, permset, tag, id);
1097 if (name != NULL && *name != L'\0' && len > 0)
1098 aes_copy_wcs_len(&ap->name, name, len);
1100 aes_clean(&ap->name);
1104 * If this ACL entry is part of the standard POSIX permissions set,
1105 * store the permissions in the stat structure and return zero.
1108 acl_special(struct archive_entry *entry, int type, int permset, int tag)
1110 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
1112 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1113 entry->ae_stat.aest_mode &= ~0700;
1114 entry->ae_stat.aest_mode |= (permset & 7) << 6;
1116 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1117 entry->ae_stat.aest_mode &= ~0070;
1118 entry->ae_stat.aest_mode |= (permset & 7) << 3;
1120 case ARCHIVE_ENTRY_ACL_OTHER:
1121 entry->ae_stat.aest_mode &= ~0007;
1122 entry->ae_stat.aest_mode |= permset & 7;
1130 * Allocate and populate a new ACL entry with everything but the
1133 static struct ae_acl *
1134 acl_new_entry(struct archive_entry *entry,
1135 int type, int permset, int tag, int id)
1139 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
1140 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
1142 if (entry->acl_text_w != NULL) {
1143 free(entry->acl_text_w);
1144 entry->acl_text_w = NULL;
1147 /* XXX TODO: More sanity-checks on the arguments XXX */
1149 /* If there's a matching entry already in the list, overwrite it. */
1150 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
1151 if (ap->type == type && ap->tag == tag && ap->id == id) {
1152 ap->permset = permset;
1157 /* Add a new entry to the list. */
1158 ap = (struct ae_acl *)malloc(sizeof(*ap));
1161 memset(ap, 0, sizeof(*ap));
1162 ap->next = entry->acl_head;
1163 entry->acl_head = ap;
1167 ap->permset = permset;
1172 * Return a count of entries matching "want_type".
1175 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1181 ap = entry->acl_head;
1182 while (ap != NULL) {
1183 if ((ap->type & want_type) != 0)
1188 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1194 * Prepare for reading entries from the ACL data. Returns a count
1195 * of entries matching "want_type", or zero if there are no
1196 * non-extended ACL entries of that type.
1199 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1203 count = archive_entry_acl_count(entry, want_type);
1206 * If the only entries are the three standard ones,
1207 * then don't return any ACL data. (In this case,
1208 * client can just use chmod(2) to set permissions.)
1210 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1216 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1218 entry->acl_state = 0;
1219 entry->acl_p = entry->acl_head;
1224 * Return the next ACL entry in the list. Fake entries for the
1225 * standard permissions and include them in the returned list.
1229 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1230 int *permset, int *tag, int *id, const char **name)
1236 * The acl_state is either zero (no entries available), -1
1237 * (reading from list), or an entry type (retrieve that type
1238 * from ae_stat.aest_mode).
1240 if (entry->acl_state == 0)
1241 return (ARCHIVE_WARN);
1243 /* The first three access entries are special. */
1244 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1245 switch (entry->acl_state) {
1246 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1247 *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1248 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1249 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1250 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1251 return (ARCHIVE_OK);
1252 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1253 *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1254 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1255 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1256 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1257 return (ARCHIVE_OK);
1258 case ARCHIVE_ENTRY_ACL_OTHER:
1259 *permset = entry->ae_stat.aest_mode & 7;
1260 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1261 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1262 entry->acl_state = -1;
1263 entry->acl_p = entry->acl_head;
1264 return (ARCHIVE_OK);
1270 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1271 entry->acl_p = entry->acl_p->next;
1272 if (entry->acl_p == NULL) {
1273 entry->acl_state = 0;
1279 return (ARCHIVE_EOF); /* End of ACL entries. */
1281 *type = entry->acl_p->type;
1282 *permset = entry->acl_p->permset;
1283 *tag = entry->acl_p->tag;
1284 *id = entry->acl_p->id;
1285 *name = aes_get_mbs(&entry->acl_p->name);
1286 entry->acl_p = entry->acl_p->next;
1287 return (ARCHIVE_OK);
1291 * Generate a text version of the ACL. The flags parameter controls
1292 * the style of the generated ACL.
1295 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1299 const wchar_t *wname;
1300 const wchar_t *prefix;
1306 if (entry->acl_text_w != NULL) {
1307 free (entry->acl_text_w);
1308 entry->acl_text_w = NULL;
1314 ap = entry->acl_head;
1315 while (ap != NULL) {
1316 if ((ap->type & flags) != 0) {
1318 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1319 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1320 length += 8; /* "default:" */
1321 length += 5; /* tag name */
1322 length += 1; /* colon */
1323 wname = aes_get_wcs(&ap->name);
1325 length += wcslen(wname);
1327 length += sizeof(uid_t) * 3 + 1;
1328 length ++; /* colon */
1329 length += 3; /* rwx */
1330 length += 1; /* colon */
1331 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1332 length ++; /* newline */
1337 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1338 length += 10; /* "user::rwx\n" */
1339 length += 11; /* "group::rwx\n" */
1340 length += 11; /* "other::rwx\n" */
1346 /* Now, allocate the string and actually populate it. */
1347 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1349 __archive_errx(1, "No memory to generate the text version of the ACL");
1351 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1352 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1353 entry->ae_stat.aest_mode & 0700, -1);
1355 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1356 entry->ae_stat.aest_mode & 0070, -1);
1358 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1359 entry->ae_stat.aest_mode & 0007, -1);
1362 ap = entry->acl_head;
1363 while (ap != NULL) {
1364 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1365 wname = aes_get_wcs(&ap->name);
1367 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1371 append_entry_w(&wp, NULL, ap->tag, wname,
1380 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1381 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1382 prefix = L"default:";
1385 ap = entry->acl_head;
1387 while (ap != NULL) {
1388 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1389 wname = aes_get_wcs(&ap->name);
1392 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1396 append_entry_w(&wp, prefix, ap->tag,
1397 wname, ap->permset, id);
1404 return (entry->acl_text_w);
1408 append_id_w(wchar_t **wp, int id)
1413 append_id_w(wp, id / 10);
1414 *(*wp)++ = L"0123456789"[id % 10];
1418 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1419 const wchar_t *wname, int perm, int id)
1421 if (prefix != NULL) {
1422 wcscpy(*wp, prefix);
1426 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1430 case ARCHIVE_ENTRY_ACL_USER:
1431 wcscpy(*wp, L"user");
1433 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1437 case ARCHIVE_ENTRY_ACL_GROUP:
1438 wcscpy(*wp, L"group");
1440 case ARCHIVE_ENTRY_ACL_MASK:
1441 wcscpy(*wp, L"mask");
1445 case ARCHIVE_ENTRY_ACL_OTHER:
1446 wcscpy(*wp, L"other");
1453 if (wname != NULL) {
1456 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1457 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1458 append_id_w(wp, id);
1462 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1463 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1464 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1467 append_id_w(wp, id);
1473 * Parse a textual ACL. This automatically recognizes and supports
1474 * extensions described above. The 'type' argument is used to
1475 * indicate the type that should be used for any entries not
1476 * explicitly marked as "default:".
1479 __archive_entry_acl_parse_w(struct archive_entry *entry,
1480 const wchar_t *text, int default_type)
1483 const wchar_t *start;
1488 int type, tag, permset, id;
1492 while (text != NULL && *text != L'\0') {
1494 * Parse the fields out of the next entry,
1495 * advance 'text' to start of next entry.
1499 const wchar_t *start, *end;
1500 next_field_w(&text, &start, &end, &sep);
1502 field[fields].start = start;
1503 field[fields].end = end;
1506 } while (sep == L':');
1509 return (ARCHIVE_WARN);
1511 /* Check for a numeric ID in field 1 or 3. */
1513 isint_w(field[1].start, field[1].end, &id);
1514 /* Field 3 is optional. */
1515 if (id == -1 && fields > 3)
1516 isint_w(field[3].start, field[3].end, &id);
1518 /* Parse the permissions from field 2. */
1521 while (p < field[2].end) {
1524 permset |= ARCHIVE_ENTRY_ACL_READ;
1527 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1530 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1535 return (ARCHIVE_WARN);
1540 * Solaris extension: "defaultuser::rwx" is the
1541 * default ACL corresponding to "user::rwx", etc.
1543 if (field[0].end-field[0].start > 7
1544 && wmemcmp(field[0].start, L"default", 7) == 0) {
1545 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1546 field[0].start += 7;
1548 type = default_type;
1550 if (prefix_w(field[0].start, field[0].end, L"user")) {
1551 if (id != -1 || field[1].start < field[1].end)
1552 tag = ARCHIVE_ENTRY_ACL_USER;
1554 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1555 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1556 if (id != -1 || field[1].start < field[1].end)
1557 tag = ARCHIVE_ENTRY_ACL_GROUP;
1559 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1560 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1561 if (id != -1 || field[1].start < field[1].end)
1562 return (ARCHIVE_WARN);
1563 tag = ARCHIVE_ENTRY_ACL_OTHER;
1564 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1565 if (id != -1 || field[1].start < field[1].end)
1566 return (ARCHIVE_WARN);
1567 tag = ARCHIVE_ENTRY_ACL_MASK;
1569 return (ARCHIVE_WARN);
1571 /* Add entry to the internal list. */
1572 archive_entry_acl_add_entry_w_len(entry, type, permset,
1573 tag, id, field[1].start, field[1].end - field[1].start);
1575 return (ARCHIVE_OK);
1579 * extended attribute handling
1583 archive_entry_xattr_clear(struct archive_entry *entry)
1585 struct ae_xattr *xp;
1587 while (entry->xattr_head != NULL) {
1588 xp = entry->xattr_head->next;
1589 free(entry->xattr_head->name);
1590 free(entry->xattr_head->value);
1591 free(entry->xattr_head);
1592 entry->xattr_head = xp;
1595 entry->xattr_head = NULL;
1599 archive_entry_xattr_add_entry(struct archive_entry *entry,
1600 const char *name, const void *value, size_t size)
1602 struct ae_xattr *xp;
1604 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1607 if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1611 xp->name = strdup(name);
1612 if ((xp->value = malloc(size)) != NULL) {
1613 memcpy(xp->value, value, size);
1618 xp->next = entry->xattr_head;
1619 entry->xattr_head = xp;
1624 * returns number of the extended attribute entries
1627 archive_entry_xattr_count(struct archive_entry *entry)
1629 struct ae_xattr *xp;
1632 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1639 archive_entry_xattr_reset(struct archive_entry * entry)
1641 entry->xattr_p = entry->xattr_head;
1643 return archive_entry_xattr_count(entry);
1647 archive_entry_xattr_next(struct archive_entry * entry,
1648 const char **name, const void **value, size_t *size)
1650 if (entry->xattr_p) {
1651 *name = entry->xattr_p->name;
1652 *value = entry->xattr_p->value;
1653 *size = entry->xattr_p->size;
1655 entry->xattr_p = entry->xattr_p->next;
1657 return (ARCHIVE_OK);
1662 return (ARCHIVE_WARN);
1667 * end of xattr handling
1671 * Parse a string to a positive decimal integer. Returns true if
1672 * the string is non-empty and consists only of decimal digits,
1676 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1681 while (start < end) {
1682 if (*start < '0' || *start > '9')
1684 if (n > (INT_MAX / 10))
1697 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1698 * to point to just after the separator. *start points to the first
1699 * character of the matched text and *end just after the last
1700 * character of the matched identifier. In particular *end - *start
1701 * is the length of the field body, not including leading or trailing
1705 next_field_w(const wchar_t **wp, const wchar_t **start,
1706 const wchar_t **end, wchar_t *sep)
1708 /* Skip leading whitespace to find start of field. */
1709 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1714 /* Scan for the separator. */
1715 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1721 /* Trim trailing whitespace to locate end of field. */
1723 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1728 /* Adjust scanner location. */
1734 * Return true if the characters [start...end) are a prefix of 'test'.
1735 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1738 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1743 if (*start++ != *test++)
1746 while (start < end && *start++ == *test++)
1757 * Following code is modified from UC Berkeley sources, and
1758 * is subject to the following copyright notice.
1762 * Copyright (c) 1993
1763 * The Regents of the University of California. All rights reserved.
1765 * Redistribution and use in source and binary forms, with or without
1766 * modification, are permitted provided that the following conditions
1768 * 1. Redistributions of source code must retain the above copyright
1769 * notice, this list of conditions and the following disclaimer.
1770 * 2. Redistributions in binary form must reproduce the above copyright
1771 * notice, this list of conditions and the following disclaimer in the
1772 * documentation and/or other materials provided with the distribution.
1773 * 4. Neither the name of the University nor the names of its contributors
1774 * may be used to endorse or promote products derived from this software
1775 * without specific prior written permission.
1777 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1778 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1779 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1780 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1781 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1782 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1783 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1784 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1785 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1786 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1790 static struct flag {
1792 const wchar_t *wname;
1794 unsigned long clear;
1796 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1798 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1799 { "nosappend", L"nosappend", SF_APPEND, 0 },
1801 #ifdef EXT2_APPEND_FL /* 'a' */
1802 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1803 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1806 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1807 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1810 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1811 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1812 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1814 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1815 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1816 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1817 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1820 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1821 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1824 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1827 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1828 { "nouappend", L"nouappend", UF_APPEND, 0 },
1831 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1832 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1833 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1836 { "nodump", L"nodump", 0, UF_NODUMP},
1838 #ifdef EXT2_NODUMP_FL /* 'd' */
1839 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1842 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1845 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1846 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1848 #ifdef EXT2_COMPR_FL /* 'c' */
1849 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1852 #ifdef EXT2_NOATIME_FL /* 'A' */
1853 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1855 { NULL, NULL, 0, 0 }
1860 * Convert file flags to a comma-separated string. If no flags
1861 * are set, return the empty string.
1864 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1872 bits = bitset | bitclear;
1874 for (flag = flags; flag->name != NULL; flag++)
1875 if (bits & (flag->set | flag->clear)) {
1876 length += strlen(flag->name) + 1;
1877 bits &= ~(flag->set | flag->clear);
1882 string = (char *)malloc(length);
1887 for (flag = flags; flag->name != NULL; flag++) {
1888 if (bitset & flag->set || bitclear & flag->clear) {
1889 sp = flag->name + 2;
1890 } else if (bitset & flag->clear || bitclear & flag->set) {
1894 bitset &= ~(flag->set | flag->clear);
1895 bitclear &= ~(flag->set | flag->clear);
1898 while ((*dp++ = *sp++) != '\0')
1909 * Take string of arguments and return file flags. This
1910 * version works a little differently than strtofflags(3).
1911 * In particular, it always tests every token, skipping any
1912 * unrecognized tokens. It returns a pointer to the first
1913 * unrecognized token, or NULL if every token was recognized.
1914 * This version is also const-correct and does not modify the
1918 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
1920 const char *start, *end;
1922 unsigned long set, clear;
1928 /* Find start of first token. */
1929 while (*start == '\t' || *start == ' ' || *start == ',')
1931 while (*start != '\0') {
1932 /* Locate end of token. */
1934 while (*end != '\0' && *end != '\t' &&
1935 *end != ' ' && *end != ',')
1937 for (flag = flags; flag->name != NULL; flag++) {
1938 if (memcmp(start, flag->name, end - start) == 0) {
1939 /* Matched "noXXXX", so reverse the sense. */
1943 } else if (memcmp(start, flag->name + 2, end - start)
1945 /* Matched "XXXX", so don't reverse. */
1947 clear |= flag->clear;
1951 /* Ignore unknown flag names. */
1952 if (flag->name == NULL && failed == NULL)
1955 /* Find start of next token. */
1957 while (*start == '\t' || *start == ' ' || *start == ',')
1967 /* Return location of first failure. */
1973 * Take string of arguments and return file flags. This
1974 * version works a little differently than strtofflags(3).
1975 * In particular, it always tests every token, skipping any
1976 * unrecognized tokens. It returns a pointer to the first
1977 * unrecognized token, or NULL if every token was recognized.
1978 * This version is also const-correct and does not modify the
1981 static const wchar_t *
1982 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1984 const wchar_t *start, *end;
1986 unsigned long set, clear;
1987 const wchar_t *failed;
1992 /* Find start of first token. */
1993 while (*start == L'\t' || *start == L' ' || *start == L',')
1995 while (*start != L'\0') {
1996 /* Locate end of token. */
1998 while (*end != L'\0' && *end != L'\t' &&
1999 *end != L' ' && *end != L',')
2001 for (flag = flags; flag->wname != NULL; flag++) {
2002 if (wmemcmp(start, flag->wname, end - start) == 0) {
2003 /* Matched "noXXXX", so reverse the sense. */
2007 } else if (wmemcmp(start, flag->wname + 2, end - start)
2009 /* Matched "XXXX", so don't reverse. */
2011 clear |= flag->clear;
2015 /* Ignore unknown flag names. */
2016 if (flag->wname == NULL && failed == NULL)
2019 /* Find start of next token. */
2021 while (*start == L'\t' || *start == L' ' || *start == L',')
2031 /* Return location of first failure. */
2039 main(int argc, char **argv)
2041 struct archive_entry *entry = archive_entry_new();
2042 unsigned long set, clear;
2043 const wchar_t *remainder;
2045 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
2046 archive_entry_fflags(entry, &set, &clear);
2048 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
2050 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));