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.55 2008/12/23 05:01:43 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 */
49 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
50 * As the include guards don't agree, the order of include is important.
52 #ifdef HAVE_LINUX_EXT2_FS_H
53 #include <linux/ext2_fs.h> /* for Linux file flags */
55 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
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__) && !defined(__CYGWIN__))
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 if (aes->aes_wcs != NULL)
247 aes->aes_set |= AES_SET_WCS;
248 return (aes->aes_wcs);
254 aes_set_mbs(struct aes *aes, const char *mbs)
256 return (aes_copy_mbs(aes, mbs));
260 aes_copy_mbs(struct aes *aes, const char *mbs)
266 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
267 archive_strcpy(&(aes->aes_mbs), mbs);
268 archive_string_empty(&(aes->aes_utf8));
270 free((wchar_t *)(uintptr_t)aes->aes_wcs);
277 * The 'update' form tries to proactively update all forms of
278 * this string (WCS and MBS) and returns an error if any of
279 * them fail. This is used by the 'pax' handler, for instance,
280 * to detect and report character-conversion failures early while
281 * still allowing clients to get potentially useful values from
282 * the more tolerant lazy conversions. (get_mbs and get_wcs will
283 * strive to give the user something useful, so you can get hopefully
284 * usable values even if some of the character conversions are failing.)
287 aes_update_utf8(struct aes *aes, const char *utf8)
291 return (1); /* Succeeded in clearing everything. */
294 /* Save the UTF8 string. */
295 archive_strcpy(&(aes->aes_utf8), utf8);
297 /* Empty the mbs and wcs strings. */
298 archive_string_empty(&(aes->aes_mbs));
300 free((wchar_t *)(uintptr_t)aes->aes_wcs);
304 aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
306 /* TODO: We should just do a direct UTF-8 to MBS conversion
307 * here. That would be faster, use less space, and give the
308 * same information. (If a UTF-8 to MBS conversion succeeds,
309 * then UTF-8->WCS and Unicode->MBS conversions will both
312 /* Try converting UTF8 to WCS, return false on failure. */
313 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
314 if (aes->aes_wcs == NULL)
316 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS; /* Both UTF8 and WCS set. */
318 /* Try converting WCS to MBS, return false on failure. */
319 if (archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) == NULL)
321 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
323 /* All conversions succeeded. */
328 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
330 return aes_copy_wcs_len(aes, wcs, wcs == NULL ? 0 : wcslen(wcs));
334 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
342 aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
343 archive_string_empty(&(aes->aes_mbs));
344 archive_string_empty(&(aes->aes_utf8));
346 free((wchar_t *)(uintptr_t)aes->aes_wcs);
349 w = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
351 __archive_errx(1, "No memory for aes_copy_wcs()");
352 wmemcpy(w, wcs, len);
358 /****************************************************************************
362 ****************************************************************************/
364 struct archive_entry *
365 archive_entry_clear(struct archive_entry *entry)
369 aes_clean(&entry->ae_fflags_text);
370 aes_clean(&entry->ae_gname);
371 aes_clean(&entry->ae_hardlink);
372 aes_clean(&entry->ae_pathname);
373 aes_clean(&entry->ae_sourcepath);
374 aes_clean(&entry->ae_symlink);
375 aes_clean(&entry->ae_uname);
376 archive_entry_acl_clear(entry);
377 archive_entry_xattr_clear(entry);
379 memset(entry, 0, sizeof(*entry));
383 struct archive_entry *
384 archive_entry_clone(struct archive_entry *entry)
386 struct archive_entry *entry2;
387 struct ae_acl *ap, *ap2;
390 /* Allocate new structure and copy over all of the fields. */
391 entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
394 memset(entry2, 0, sizeof(*entry2));
395 entry2->ae_stat = entry->ae_stat;
396 entry2->ae_fflags_set = entry->ae_fflags_set;
397 entry2->ae_fflags_clear = entry->ae_fflags_clear;
399 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
400 aes_copy(&entry2->ae_gname, &entry->ae_gname);
401 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
402 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
403 aes_copy(&entry2->ae_sourcepath, &entry->ae_sourcepath);
404 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
405 entry2->ae_set = entry->ae_set;
406 aes_copy(&entry2->ae_uname, &entry->ae_uname);
408 /* Copy ACL data over. */
409 ap = entry->acl_head;
411 ap2 = acl_new_entry(entry2,
412 ap->type, ap->permset, ap->tag, ap->id);
414 aes_copy(&ap2->name, &ap->name);
418 /* Copy xattr data over. */
419 xp = entry->xattr_head;
421 archive_entry_xattr_add_entry(entry2,
422 xp->name, xp->value, xp->size);
430 archive_entry_free(struct archive_entry *entry)
432 archive_entry_clear(entry);
436 struct archive_entry *
437 archive_entry_new(void)
439 struct archive_entry *entry;
441 entry = (struct archive_entry *)malloc(sizeof(*entry));
444 memset(entry, 0, sizeof(*entry));
449 * Functions for reading fields from an archive_entry.
453 archive_entry_atime(struct archive_entry *entry)
455 return (entry->ae_stat.aest_atime);
459 archive_entry_atime_nsec(struct archive_entry *entry)
461 return (entry->ae_stat.aest_atime_nsec);
465 archive_entry_atime_is_set(struct archive_entry *entry)
467 return (entry->ae_set & AE_SET_ATIME);
471 archive_entry_birthtime(struct archive_entry *entry)
473 return (entry->ae_stat.aest_birthtime);
477 archive_entry_birthtime_nsec(struct archive_entry *entry)
479 return (entry->ae_stat.aest_birthtime_nsec);
483 archive_entry_birthtime_is_set(struct archive_entry *entry)
485 return (entry->ae_set & AE_SET_BIRTHTIME);
489 archive_entry_ctime(struct archive_entry *entry)
491 return (entry->ae_stat.aest_ctime);
495 archive_entry_ctime_is_set(struct archive_entry *entry)
497 return (entry->ae_set & AE_SET_CTIME);
501 archive_entry_ctime_nsec(struct archive_entry *entry)
503 return (entry->ae_stat.aest_ctime_nsec);
507 archive_entry_dev(struct archive_entry *entry)
509 if (entry->ae_stat.aest_dev_is_broken_down)
510 return ae_makedev(entry->ae_stat.aest_devmajor,
511 entry->ae_stat.aest_devminor);
513 return (entry->ae_stat.aest_dev);
517 archive_entry_devmajor(struct archive_entry *entry)
519 if (entry->ae_stat.aest_dev_is_broken_down)
520 return (entry->ae_stat.aest_devmajor);
522 return major(entry->ae_stat.aest_dev);
526 archive_entry_devminor(struct archive_entry *entry)
528 if (entry->ae_stat.aest_dev_is_broken_down)
529 return (entry->ae_stat.aest_devminor);
531 return minor(entry->ae_stat.aest_dev);
535 archive_entry_filetype(struct archive_entry *entry)
537 return (AE_IFMT & entry->ae_stat.aest_mode);
541 archive_entry_fflags(struct archive_entry *entry,
542 unsigned long *set, unsigned long *clear)
544 *set = entry->ae_fflags_set;
545 *clear = entry->ae_fflags_clear;
549 * Note: if text was provided, this just returns that text. If you
550 * really need the text to be rebuilt in a canonical form, set the
551 * text, ask for the bitmaps, then set the bitmaps. (Setting the
552 * bitmaps clears any stored text.) This design is deliberate: if
553 * we're editing archives, we don't want to discard flags just because
554 * they aren't supported on the current system. The bitmap<->text
555 * conversions are platform-specific (see below).
558 archive_entry_fflags_text(struct archive_entry *entry)
563 f = aes_get_mbs(&entry->ae_fflags_text);
567 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
570 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
574 aes_copy_mbs(&entry->ae_fflags_text, p);
576 f = aes_get_mbs(&entry->ae_fflags_text);
581 archive_entry_gid(struct archive_entry *entry)
583 return (entry->ae_stat.aest_gid);
587 archive_entry_gname(struct archive_entry *entry)
589 return (aes_get_mbs(&entry->ae_gname));
593 archive_entry_gname_w(struct archive_entry *entry)
595 return (aes_get_wcs(&entry->ae_gname));
599 archive_entry_hardlink(struct archive_entry *entry)
601 if (entry->ae_set & AE_SET_HARDLINK)
602 return (aes_get_mbs(&entry->ae_hardlink));
607 archive_entry_hardlink_w(struct archive_entry *entry)
609 if (entry->ae_set & AE_SET_HARDLINK)
610 return (aes_get_wcs(&entry->ae_hardlink));
615 archive_entry_ino(struct archive_entry *entry)
617 return (entry->ae_stat.aest_ino);
621 archive_entry_mode(struct archive_entry *entry)
623 return (entry->ae_stat.aest_mode);
627 archive_entry_mtime(struct archive_entry *entry)
629 return (entry->ae_stat.aest_mtime);
633 archive_entry_mtime_nsec(struct archive_entry *entry)
635 return (entry->ae_stat.aest_mtime_nsec);
639 archive_entry_mtime_is_set(struct archive_entry *entry)
641 return (entry->ae_set & AE_SET_MTIME);
645 archive_entry_nlink(struct archive_entry *entry)
647 return (entry->ae_stat.aest_nlink);
651 archive_entry_pathname(struct archive_entry *entry)
653 return (aes_get_mbs(&entry->ae_pathname));
657 archive_entry_pathname_w(struct archive_entry *entry)
659 return (aes_get_wcs(&entry->ae_pathname));
663 archive_entry_rdev(struct archive_entry *entry)
665 if (entry->ae_stat.aest_rdev_is_broken_down)
666 return ae_makedev(entry->ae_stat.aest_rdevmajor,
667 entry->ae_stat.aest_rdevminor);
669 return (entry->ae_stat.aest_rdev);
673 archive_entry_rdevmajor(struct archive_entry *entry)
675 if (entry->ae_stat.aest_rdev_is_broken_down)
676 return (entry->ae_stat.aest_rdevmajor);
678 return major(entry->ae_stat.aest_rdev);
682 archive_entry_rdevminor(struct archive_entry *entry)
684 if (entry->ae_stat.aest_rdev_is_broken_down)
685 return (entry->ae_stat.aest_rdevminor);
687 return minor(entry->ae_stat.aest_rdev);
691 archive_entry_size(struct archive_entry *entry)
693 return (entry->ae_stat.aest_size);
697 archive_entry_size_is_set(struct archive_entry *entry)
699 return (entry->ae_set & AE_SET_SIZE);
703 archive_entry_sourcepath(struct archive_entry *entry)
705 return (aes_get_mbs(&entry->ae_sourcepath));
709 archive_entry_symlink(struct archive_entry *entry)
711 if (entry->ae_set & AE_SET_SYMLINK)
712 return (aes_get_mbs(&entry->ae_symlink));
717 archive_entry_symlink_w(struct archive_entry *entry)
719 if (entry->ae_set & AE_SET_SYMLINK)
720 return (aes_get_wcs(&entry->ae_symlink));
725 archive_entry_uid(struct archive_entry *entry)
727 return (entry->ae_stat.aest_uid);
731 archive_entry_uname(struct archive_entry *entry)
733 return (aes_get_mbs(&entry->ae_uname));
737 archive_entry_uname_w(struct archive_entry *entry)
739 return (aes_get_wcs(&entry->ae_uname));
743 * Functions to set archive_entry properties.
747 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
749 entry->stat_valid = 0;
750 entry->ae_stat.aest_mode &= ~AE_IFMT;
751 entry->ae_stat.aest_mode |= AE_IFMT & type;
755 archive_entry_set_fflags(struct archive_entry *entry,
756 unsigned long set, unsigned long clear)
758 aes_clean(&entry->ae_fflags_text);
759 entry->ae_fflags_set = set;
760 entry->ae_fflags_clear = clear;
764 archive_entry_copy_fflags_text(struct archive_entry *entry,
767 aes_copy_mbs(&entry->ae_fflags_text, flags);
768 return (ae_strtofflags(flags,
769 &entry->ae_fflags_set, &entry->ae_fflags_clear));
773 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
774 const wchar_t *flags)
776 aes_copy_wcs(&entry->ae_fflags_text, flags);
777 return (ae_wcstofflags(flags,
778 &entry->ae_fflags_set, &entry->ae_fflags_clear));
782 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
784 entry->stat_valid = 0;
785 entry->ae_stat.aest_gid = g;
789 archive_entry_set_gname(struct archive_entry *entry, const char *name)
791 aes_set_mbs(&entry->ae_gname, name);
795 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
797 aes_copy_mbs(&entry->ae_gname, name);
801 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
803 aes_copy_wcs(&entry->ae_gname, name);
807 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
809 return (aes_update_utf8(&entry->ae_gname, name));
813 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
815 entry->stat_valid = 0;
816 entry->ae_stat.aest_ino = ino;
820 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
822 aes_set_mbs(&entry->ae_hardlink, target);
824 entry->ae_set |= AE_SET_HARDLINK;
826 entry->ae_set &= ~AE_SET_HARDLINK;
830 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
832 aes_copy_mbs(&entry->ae_hardlink, target);
834 entry->ae_set |= AE_SET_HARDLINK;
836 entry->ae_set &= ~AE_SET_HARDLINK;
840 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
842 aes_copy_wcs(&entry->ae_hardlink, target);
844 entry->ae_set |= AE_SET_HARDLINK;
846 entry->ae_set &= ~AE_SET_HARDLINK;
850 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
852 entry->stat_valid = 0;
853 entry->ae_set |= AE_SET_ATIME;
854 entry->ae_stat.aest_atime = t;
855 entry->ae_stat.aest_atime_nsec = ns;
859 archive_entry_unset_atime(struct archive_entry *entry)
861 archive_entry_set_atime(entry, 0, 0);
862 entry->ae_set &= ~AE_SET_ATIME;
866 archive_entry_set_birthtime(struct archive_entry *entry, time_t m, long ns)
868 entry->stat_valid = 0;
869 entry->ae_set |= AE_SET_BIRTHTIME;
870 entry->ae_stat.aest_birthtime = m;
871 entry->ae_stat.aest_birthtime_nsec = ns;
875 archive_entry_unset_birthtime(struct archive_entry *entry)
877 archive_entry_set_birthtime(entry, 0, 0);
878 entry->ae_set &= ~AE_SET_BIRTHTIME;
882 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
884 entry->stat_valid = 0;
885 entry->ae_set |= AE_SET_CTIME;
886 entry->ae_stat.aest_ctime = t;
887 entry->ae_stat.aest_ctime_nsec = ns;
891 archive_entry_unset_ctime(struct archive_entry *entry)
893 archive_entry_set_ctime(entry, 0, 0);
894 entry->ae_set &= ~AE_SET_CTIME;
898 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
900 entry->stat_valid = 0;
901 entry->ae_stat.aest_dev_is_broken_down = 0;
902 entry->ae_stat.aest_dev = d;
906 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
908 entry->stat_valid = 0;
909 entry->ae_stat.aest_dev_is_broken_down = 1;
910 entry->ae_stat.aest_devmajor = m;
914 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
916 entry->stat_valid = 0;
917 entry->ae_stat.aest_dev_is_broken_down = 1;
918 entry->ae_stat.aest_devminor = m;
921 /* Set symlink if symlink is already set, else set hardlink. */
923 archive_entry_set_link(struct archive_entry *entry, const char *target)
925 if (entry->ae_set & AE_SET_SYMLINK)
926 aes_set_mbs(&entry->ae_symlink, target);
928 aes_set_mbs(&entry->ae_hardlink, target);
931 /* Set symlink if symlink is already set, else set hardlink. */
933 archive_entry_copy_link(struct archive_entry *entry, const char *target)
935 if (entry->ae_set & AE_SET_SYMLINK)
936 aes_copy_mbs(&entry->ae_symlink, target);
938 aes_copy_mbs(&entry->ae_hardlink, target);
941 /* Set symlink if symlink is already set, else set hardlink. */
943 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
945 if (entry->ae_set & AE_SET_SYMLINK)
946 aes_copy_wcs(&entry->ae_symlink, target);
948 aes_copy_wcs(&entry->ae_hardlink, target);
952 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
954 if (entry->ae_set & AE_SET_SYMLINK)
955 return (aes_update_utf8(&entry->ae_symlink, target));
957 return (aes_update_utf8(&entry->ae_hardlink, target));
961 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
963 entry->stat_valid = 0;
964 entry->ae_stat.aest_mode = m;
968 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
970 entry->stat_valid = 0;
971 entry->ae_set |= AE_SET_MTIME;
972 entry->ae_stat.aest_mtime = m;
973 entry->ae_stat.aest_mtime_nsec = ns;
977 archive_entry_unset_mtime(struct archive_entry *entry)
979 archive_entry_set_mtime(entry, 0, 0);
980 entry->ae_set &= ~AE_SET_MTIME;
984 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
986 entry->stat_valid = 0;
987 entry->ae_stat.aest_nlink = nlink;
991 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
993 aes_set_mbs(&entry->ae_pathname, name);
997 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
999 aes_copy_mbs(&entry->ae_pathname, name);
1003 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
1005 aes_copy_wcs(&entry->ae_pathname, name);
1009 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
1011 return (aes_update_utf8(&entry->ae_pathname, name));
1015 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
1017 entry->stat_valid = 0;
1018 entry->ae_stat.aest_mode &= AE_IFMT;
1019 entry->ae_stat.aest_mode |= ~AE_IFMT & p;
1023 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
1025 entry->stat_valid = 0;
1026 entry->ae_stat.aest_rdev = m;
1027 entry->ae_stat.aest_rdev_is_broken_down = 0;
1031 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
1033 entry->stat_valid = 0;
1034 entry->ae_stat.aest_rdev_is_broken_down = 1;
1035 entry->ae_stat.aest_rdevmajor = m;
1039 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
1041 entry->stat_valid = 0;
1042 entry->ae_stat.aest_rdev_is_broken_down = 1;
1043 entry->ae_stat.aest_rdevminor = m;
1047 archive_entry_set_size(struct archive_entry *entry, int64_t s)
1049 entry->stat_valid = 0;
1050 entry->ae_stat.aest_size = s;
1051 entry->ae_set |= AE_SET_SIZE;
1055 archive_entry_unset_size(struct archive_entry *entry)
1057 archive_entry_set_size(entry, 0);
1058 entry->ae_set &= ~AE_SET_SIZE;
1062 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1064 aes_set_mbs(&entry->ae_sourcepath, path);
1068 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1070 aes_set_mbs(&entry->ae_symlink, linkname);
1071 if (linkname != NULL)
1072 entry->ae_set |= AE_SET_SYMLINK;
1074 entry->ae_set &= ~AE_SET_SYMLINK;
1078 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1080 aes_copy_mbs(&entry->ae_symlink, linkname);
1081 if (linkname != NULL)
1082 entry->ae_set |= AE_SET_SYMLINK;
1084 entry->ae_set &= ~AE_SET_SYMLINK;
1088 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1090 aes_copy_wcs(&entry->ae_symlink, linkname);
1091 if (linkname != NULL)
1092 entry->ae_set |= AE_SET_SYMLINK;
1094 entry->ae_set &= ~AE_SET_SYMLINK;
1098 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
1100 entry->stat_valid = 0;
1101 entry->ae_stat.aest_uid = u;
1105 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1107 aes_set_mbs(&entry->ae_uname, name);
1111 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1113 aes_copy_mbs(&entry->ae_uname, name);
1117 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1119 aes_copy_wcs(&entry->ae_uname, name);
1123 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1125 return (aes_update_utf8(&entry->ae_uname, name));
1129 * ACL management. The following would, of course, be a lot simpler
1130 * if: 1) the last draft of POSIX.1e were a really thorough and
1131 * complete standard that addressed the needs of ACL archiving and 2)
1132 * everyone followed it faithfully. Alas, neither is true, so the
1133 * following is a lot more complex than might seem necessary to the
1138 archive_entry_acl_clear(struct archive_entry *entry)
1142 while (entry->acl_head != NULL) {
1143 ap = entry->acl_head->next;
1144 aes_clean(&entry->acl_head->name);
1145 free(entry->acl_head);
1146 entry->acl_head = ap;
1148 if (entry->acl_text_w != NULL) {
1149 free(entry->acl_text_w);
1150 entry->acl_text_w = NULL;
1152 entry->acl_p = NULL;
1153 entry->acl_state = 0; /* Not counting. */
1157 * Add a single ACL entry to the internal list of ACL data.
1160 archive_entry_acl_add_entry(struct archive_entry *entry,
1161 int type, int permset, int tag, int id, const char *name)
1165 if (acl_special(entry, type, permset, tag) == 0)
1167 ap = acl_new_entry(entry, type, permset, tag, id);
1172 if (name != NULL && *name != '\0')
1173 aes_copy_mbs(&ap->name, name);
1175 aes_clean(&ap->name);
1179 * As above, but with a wide-character name.
1182 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1183 int type, int permset, int tag, int id, const wchar_t *name)
1185 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
1189 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
1190 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
1194 if (acl_special(entry, type, permset, tag) == 0)
1196 ap = acl_new_entry(entry, type, permset, tag, id);
1201 if (name != NULL && *name != L'\0' && len > 0)
1202 aes_copy_wcs_len(&ap->name, name, len);
1204 aes_clean(&ap->name);
1208 * If this ACL entry is part of the standard POSIX permissions set,
1209 * store the permissions in the stat structure and return zero.
1212 acl_special(struct archive_entry *entry, int type, int permset, int tag)
1214 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
1216 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1217 entry->ae_stat.aest_mode &= ~0700;
1218 entry->ae_stat.aest_mode |= (permset & 7) << 6;
1220 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1221 entry->ae_stat.aest_mode &= ~0070;
1222 entry->ae_stat.aest_mode |= (permset & 7) << 3;
1224 case ARCHIVE_ENTRY_ACL_OTHER:
1225 entry->ae_stat.aest_mode &= ~0007;
1226 entry->ae_stat.aest_mode |= permset & 7;
1234 * Allocate and populate a new ACL entry with everything but the
1237 static struct ae_acl *
1238 acl_new_entry(struct archive_entry *entry,
1239 int type, int permset, int tag, int id)
1243 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
1244 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
1246 if (entry->acl_text_w != NULL) {
1247 free(entry->acl_text_w);
1248 entry->acl_text_w = NULL;
1251 /* XXX TODO: More sanity-checks on the arguments XXX */
1253 /* If there's a matching entry already in the list, overwrite it. */
1254 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
1255 if (ap->type == type && ap->tag == tag && ap->id == id) {
1256 ap->permset = permset;
1261 /* Add a new entry to the list. */
1262 ap = (struct ae_acl *)malloc(sizeof(*ap));
1265 memset(ap, 0, sizeof(*ap));
1266 ap->next = entry->acl_head;
1267 entry->acl_head = ap;
1271 ap->permset = permset;
1276 * Return a count of entries matching "want_type".
1279 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1285 ap = entry->acl_head;
1286 while (ap != NULL) {
1287 if ((ap->type & want_type) != 0)
1292 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1298 * Prepare for reading entries from the ACL data. Returns a count
1299 * of entries matching "want_type", or zero if there are no
1300 * non-extended ACL entries of that type.
1303 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1307 count = archive_entry_acl_count(entry, want_type);
1310 * If the only entries are the three standard ones,
1311 * then don't return any ACL data. (In this case,
1312 * client can just use chmod(2) to set permissions.)
1314 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1320 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1322 entry->acl_state = 0;
1323 entry->acl_p = entry->acl_head;
1328 * Return the next ACL entry in the list. Fake entries for the
1329 * standard permissions and include them in the returned list.
1333 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1334 int *permset, int *tag, int *id, const char **name)
1340 * The acl_state is either zero (no entries available), -1
1341 * (reading from list), or an entry type (retrieve that type
1342 * from ae_stat.aest_mode).
1344 if (entry->acl_state == 0)
1345 return (ARCHIVE_WARN);
1347 /* The first three access entries are special. */
1348 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1349 switch (entry->acl_state) {
1350 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1351 *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1352 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1353 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1354 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1355 return (ARCHIVE_OK);
1356 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1357 *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1358 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1359 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1360 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1361 return (ARCHIVE_OK);
1362 case ARCHIVE_ENTRY_ACL_OTHER:
1363 *permset = entry->ae_stat.aest_mode & 7;
1364 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1365 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1366 entry->acl_state = -1;
1367 entry->acl_p = entry->acl_head;
1368 return (ARCHIVE_OK);
1374 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1375 entry->acl_p = entry->acl_p->next;
1376 if (entry->acl_p == NULL) {
1377 entry->acl_state = 0;
1383 return (ARCHIVE_EOF); /* End of ACL entries. */
1385 *type = entry->acl_p->type;
1386 *permset = entry->acl_p->permset;
1387 *tag = entry->acl_p->tag;
1388 *id = entry->acl_p->id;
1389 *name = aes_get_mbs(&entry->acl_p->name);
1390 entry->acl_p = entry->acl_p->next;
1391 return (ARCHIVE_OK);
1395 * Generate a text version of the ACL. The flags parameter controls
1396 * the style of the generated ACL.
1399 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1403 const wchar_t *wname;
1404 const wchar_t *prefix;
1410 if (entry->acl_text_w != NULL) {
1411 free (entry->acl_text_w);
1412 entry->acl_text_w = NULL;
1418 ap = entry->acl_head;
1419 while (ap != NULL) {
1420 if ((ap->type & flags) != 0) {
1422 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1423 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1424 length += 8; /* "default:" */
1425 length += 5; /* tag name */
1426 length += 1; /* colon */
1427 wname = aes_get_wcs(&ap->name);
1429 length += wcslen(wname);
1431 length += sizeof(uid_t) * 3 + 1;
1432 length ++; /* colon */
1433 length += 3; /* rwx */
1434 length += 1; /* colon */
1435 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1436 length ++; /* newline */
1441 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1442 length += 10; /* "user::rwx\n" */
1443 length += 11; /* "group::rwx\n" */
1444 length += 11; /* "other::rwx\n" */
1450 /* Now, allocate the string and actually populate it. */
1451 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1453 __archive_errx(1, "No memory to generate the text version of the ACL");
1455 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1456 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1457 entry->ae_stat.aest_mode & 0700, -1);
1459 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1460 entry->ae_stat.aest_mode & 0070, -1);
1462 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1463 entry->ae_stat.aest_mode & 0007, -1);
1466 ap = entry->acl_head;
1467 while (ap != NULL) {
1468 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1469 wname = aes_get_wcs(&ap->name);
1471 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1475 append_entry_w(&wp, NULL, ap->tag, wname,
1484 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1485 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1486 prefix = L"default:";
1489 ap = entry->acl_head;
1491 while (ap != NULL) {
1492 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1493 wname = aes_get_wcs(&ap->name);
1496 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1500 append_entry_w(&wp, prefix, ap->tag,
1501 wname, ap->permset, id);
1508 return (entry->acl_text_w);
1512 append_id_w(wchar_t **wp, int id)
1517 append_id_w(wp, id / 10);
1518 *(*wp)++ = L"0123456789"[id % 10];
1522 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1523 const wchar_t *wname, int perm, int id)
1525 if (prefix != NULL) {
1526 wcscpy(*wp, prefix);
1530 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1534 case ARCHIVE_ENTRY_ACL_USER:
1535 wcscpy(*wp, L"user");
1537 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1541 case ARCHIVE_ENTRY_ACL_GROUP:
1542 wcscpy(*wp, L"group");
1544 case ARCHIVE_ENTRY_ACL_MASK:
1545 wcscpy(*wp, L"mask");
1549 case ARCHIVE_ENTRY_ACL_OTHER:
1550 wcscpy(*wp, L"other");
1557 if (wname != NULL) {
1560 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1561 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1562 append_id_w(wp, id);
1566 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1567 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1568 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1571 append_id_w(wp, id);
1577 * Parse a textual ACL. This automatically recognizes and supports
1578 * extensions described above. The 'type' argument is used to
1579 * indicate the type that should be used for any entries not
1580 * explicitly marked as "default:".
1583 __archive_entry_acl_parse_w(struct archive_entry *entry,
1584 const wchar_t *text, int default_type)
1587 const wchar_t *start;
1592 int type, tag, permset, id;
1596 while (text != NULL && *text != L'\0') {
1598 * Parse the fields out of the next entry,
1599 * advance 'text' to start of next entry.
1603 const wchar_t *start, *end;
1604 next_field_w(&text, &start, &end, &sep);
1606 field[fields].start = start;
1607 field[fields].end = end;
1610 } while (sep == L':');
1613 return (ARCHIVE_WARN);
1615 /* Check for a numeric ID in field 1 or 3. */
1617 isint_w(field[1].start, field[1].end, &id);
1618 /* Field 3 is optional. */
1619 if (id == -1 && fields > 3)
1620 isint_w(field[3].start, field[3].end, &id);
1622 /* Parse the permissions from field 2. */
1625 while (p < field[2].end) {
1628 permset |= ARCHIVE_ENTRY_ACL_READ;
1631 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1634 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1639 return (ARCHIVE_WARN);
1644 * Solaris extension: "defaultuser::rwx" is the
1645 * default ACL corresponding to "user::rwx", etc.
1647 if (field[0].end-field[0].start > 7
1648 && wmemcmp(field[0].start, L"default", 7) == 0) {
1649 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1650 field[0].start += 7;
1652 type = default_type;
1654 if (prefix_w(field[0].start, field[0].end, L"user")) {
1655 if (id != -1 || field[1].start < field[1].end)
1656 tag = ARCHIVE_ENTRY_ACL_USER;
1658 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1659 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1660 if (id != -1 || field[1].start < field[1].end)
1661 tag = ARCHIVE_ENTRY_ACL_GROUP;
1663 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1664 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1665 if (id != -1 || field[1].start < field[1].end)
1666 return (ARCHIVE_WARN);
1667 tag = ARCHIVE_ENTRY_ACL_OTHER;
1668 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1669 if (id != -1 || field[1].start < field[1].end)
1670 return (ARCHIVE_WARN);
1671 tag = ARCHIVE_ENTRY_ACL_MASK;
1673 return (ARCHIVE_WARN);
1675 /* Add entry to the internal list. */
1676 archive_entry_acl_add_entry_w_len(entry, type, permset,
1677 tag, id, field[1].start, field[1].end - field[1].start);
1679 return (ARCHIVE_OK);
1683 * extended attribute handling
1687 archive_entry_xattr_clear(struct archive_entry *entry)
1689 struct ae_xattr *xp;
1691 while (entry->xattr_head != NULL) {
1692 xp = entry->xattr_head->next;
1693 free(entry->xattr_head->name);
1694 free(entry->xattr_head->value);
1695 free(entry->xattr_head);
1696 entry->xattr_head = xp;
1699 entry->xattr_head = NULL;
1703 archive_entry_xattr_add_entry(struct archive_entry *entry,
1704 const char *name, const void *value, size_t size)
1706 struct ae_xattr *xp;
1708 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1711 if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1715 xp->name = strdup(name);
1716 if ((xp->value = malloc(size)) != NULL) {
1717 memcpy(xp->value, value, size);
1722 xp->next = entry->xattr_head;
1723 entry->xattr_head = xp;
1728 * returns number of the extended attribute entries
1731 archive_entry_xattr_count(struct archive_entry *entry)
1733 struct ae_xattr *xp;
1736 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1743 archive_entry_xattr_reset(struct archive_entry * entry)
1745 entry->xattr_p = entry->xattr_head;
1747 return archive_entry_xattr_count(entry);
1751 archive_entry_xattr_next(struct archive_entry * entry,
1752 const char **name, const void **value, size_t *size)
1754 if (entry->xattr_p) {
1755 *name = entry->xattr_p->name;
1756 *value = entry->xattr_p->value;
1757 *size = entry->xattr_p->size;
1759 entry->xattr_p = entry->xattr_p->next;
1761 return (ARCHIVE_OK);
1766 return (ARCHIVE_WARN);
1771 * end of xattr handling
1775 * Parse a string to a positive decimal integer. Returns true if
1776 * the string is non-empty and consists only of decimal digits,
1780 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1785 while (start < end) {
1786 if (*start < '0' || *start > '9')
1788 if (n > (INT_MAX / 10))
1801 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1802 * to point to just after the separator. *start points to the first
1803 * character of the matched text and *end just after the last
1804 * character of the matched identifier. In particular *end - *start
1805 * is the length of the field body, not including leading or trailing
1809 next_field_w(const wchar_t **wp, const wchar_t **start,
1810 const wchar_t **end, wchar_t *sep)
1812 /* Skip leading whitespace to find start of field. */
1813 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1818 /* Scan for the separator. */
1819 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1825 /* Trim trailing whitespace to locate end of field. */
1827 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1832 /* Adjust scanner location. */
1838 * Return true if the characters [start...end) are a prefix of 'test'.
1839 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1842 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1847 if (*start++ != *test++)
1850 while (start < end && *start++ == *test++)
1861 * Following code is modified from UC Berkeley sources, and
1862 * is subject to the following copyright notice.
1866 * Copyright (c) 1993
1867 * The Regents of the University of California. All rights reserved.
1869 * Redistribution and use in source and binary forms, with or without
1870 * modification, are permitted provided that the following conditions
1872 * 1. Redistributions of source code must retain the above copyright
1873 * notice, this list of conditions and the following disclaimer.
1874 * 2. Redistributions in binary form must reproduce the above copyright
1875 * notice, this list of conditions and the following disclaimer in the
1876 * documentation and/or other materials provided with the distribution.
1877 * 4. Neither the name of the University nor the names of its contributors
1878 * may be used to endorse or promote products derived from this software
1879 * without specific prior written permission.
1881 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1882 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1883 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1884 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1885 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1886 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1887 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1888 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1889 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1890 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1894 static struct flag {
1896 const wchar_t *wname;
1898 unsigned long clear;
1900 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1902 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1903 { "nosappend", L"nosappend", SF_APPEND, 0 },
1905 #ifdef EXT2_APPEND_FL /* 'a' */
1906 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1907 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1910 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1911 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1914 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1915 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1916 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1918 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1919 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1920 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1921 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1924 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1925 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1928 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1931 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1932 { "nouappend", L"nouappend", UF_APPEND, 0 },
1935 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1936 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1937 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1940 { "nodump", L"nodump", 0, UF_NODUMP},
1942 #ifdef EXT2_NODUMP_FL /* 'd' */
1943 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1946 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1949 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1950 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1952 #ifdef EXT2_COMPR_FL /* 'c' */
1953 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1956 #ifdef EXT2_NOATIME_FL /* 'A' */
1957 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1959 { NULL, NULL, 0, 0 }
1964 * Convert file flags to a comma-separated string. If no flags
1965 * are set, return the empty string.
1968 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1976 bits = bitset | bitclear;
1978 for (flag = flags; flag->name != NULL; flag++)
1979 if (bits & (flag->set | flag->clear)) {
1980 length += strlen(flag->name) + 1;
1981 bits &= ~(flag->set | flag->clear);
1986 string = (char *)malloc(length);
1991 for (flag = flags; flag->name != NULL; flag++) {
1992 if (bitset & flag->set || bitclear & flag->clear) {
1993 sp = flag->name + 2;
1994 } else if (bitset & flag->clear || bitclear & flag->set) {
1998 bitset &= ~(flag->set | flag->clear);
1999 bitclear &= ~(flag->set | flag->clear);
2002 while ((*dp++ = *sp++) != '\0')
2013 * Take string of arguments and return file flags. This
2014 * version works a little differently than strtofflags(3).
2015 * In particular, it always tests every token, skipping any
2016 * unrecognized tokens. It returns a pointer to the first
2017 * unrecognized token, or NULL if every token was recognized.
2018 * This version is also const-correct and does not modify the
2022 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
2024 const char *start, *end;
2026 unsigned long set, clear;
2032 /* Find start of first token. */
2033 while (*start == '\t' || *start == ' ' || *start == ',')
2035 while (*start != '\0') {
2036 /* Locate end of token. */
2038 while (*end != '\0' && *end != '\t' &&
2039 *end != ' ' && *end != ',')
2041 for (flag = flags; flag->name != NULL; flag++) {
2042 if (memcmp(start, flag->name, end - start) == 0) {
2043 /* Matched "noXXXX", so reverse the sense. */
2047 } else if (memcmp(start, flag->name + 2, end - start)
2049 /* Matched "XXXX", so don't reverse. */
2051 clear |= flag->clear;
2055 /* Ignore unknown flag names. */
2056 if (flag->name == NULL && failed == NULL)
2059 /* Find start of next token. */
2061 while (*start == '\t' || *start == ' ' || *start == ',')
2071 /* Return location of first failure. */
2077 * Take string of arguments and return file flags. This
2078 * version works a little differently than strtofflags(3).
2079 * In particular, it always tests every token, skipping any
2080 * unrecognized tokens. It returns a pointer to the first
2081 * unrecognized token, or NULL if every token was recognized.
2082 * This version is also const-correct and does not modify the
2085 static const wchar_t *
2086 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
2088 const wchar_t *start, *end;
2090 unsigned long set, clear;
2091 const wchar_t *failed;
2096 /* Find start of first token. */
2097 while (*start == L'\t' || *start == L' ' || *start == L',')
2099 while (*start != L'\0') {
2100 /* Locate end of token. */
2102 while (*end != L'\0' && *end != L'\t' &&
2103 *end != L' ' && *end != L',')
2105 for (flag = flags; flag->wname != NULL; flag++) {
2106 if (wmemcmp(start, flag->wname, end - start) == 0) {
2107 /* Matched "noXXXX", so reverse the sense. */
2111 } else if (wmemcmp(start, flag->wname + 2, end - start)
2113 /* Matched "XXXX", so don't reverse. */
2115 clear |= flag->clear;
2119 /* Ignore unknown flag names. */
2120 if (flag->wname == NULL && failed == NULL)
2123 /* Find start of next token. */
2125 while (*start == L'\t' || *start == L' ' || *start == L',')
2135 /* Return location of first failure. */
2143 main(int argc, char **argv)
2145 struct archive_entry *entry = archive_entry_new();
2146 unsigned long set, clear;
2147 const wchar_t *remainder;
2149 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
2150 archive_entry_fflags(entry, &set, &clear);
2152 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
2154 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));