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$");
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 int ismode_w(const wchar_t *start, const wchar_t *end, int *result);
119 static void next_field_w(const wchar_t **wp, const wchar_t **start,
120 const wchar_t **end, wchar_t *sep);
121 static int prefix_w(const wchar_t *start, const wchar_t *end,
122 const wchar_t *test);
124 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
125 int permset, int tag, int id, const wchar_t *name, size_t);
129 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
132 while ((*s1 = *s2) != L'\0')
138 static size_t wcslen(const wchar_t *s)
140 const wchar_t *p = s;
147 /* Good enough for simple equality testing, but not for sorting. */
148 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
151 #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
155 aes_clean(struct aes *aes)
158 free((wchar_t *)(uintptr_t)aes->aes_wcs);
161 archive_string_free(&(aes->aes_mbs));
162 archive_string_free(&(aes->aes_utf8));
167 aes_copy(struct aes *dest, struct aes *src)
171 dest->aes_set = src->aes_set;
172 archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
173 archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
175 if (src->aes_wcs != NULL) {
176 wp = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
179 __archive_errx(1, "No memory for aes_copy()");
180 wcscpy(wp, src->aes_wcs);
186 aes_get_utf8(struct aes *aes)
188 if (aes->aes_set & AES_SET_UTF8)
189 return (aes->aes_utf8.s);
190 if ((aes->aes_set & AES_SET_WCS)
191 && archive_strappend_w_utf8(&(aes->aes_utf8), aes->aes_wcs) != NULL) {
192 aes->aes_set |= AES_SET_UTF8;
193 return (aes->aes_utf8.s);
199 aes_get_mbs(struct aes *aes)
201 /* If we already have an MBS form, return that immediately. */
202 if (aes->aes_set & AES_SET_MBS)
203 return (aes->aes_mbs.s);
204 /* If there's a WCS form, try converting with the native locale. */
205 if ((aes->aes_set & AES_SET_WCS)
206 && archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) != NULL) {
207 aes->aes_set |= AES_SET_MBS;
208 return (aes->aes_mbs.s);
210 /* We'll use UTF-8 for MBS if all else fails. */
211 return (aes_get_utf8(aes));
214 static const wchar_t *
215 aes_get_wcs(struct aes *aes)
220 /* Return WCS form if we already have it. */
221 if (aes->aes_set & AES_SET_WCS)
222 return (aes->aes_wcs);
224 if (aes->aes_set & AES_SET_MBS) {
225 /* Try converting MBS to WCS using native locale. */
227 * No single byte will be more than one wide character,
228 * so this length estimate will always be big enough.
230 size_t wcs_length = aes->aes_mbs.length;
232 w = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
234 __archive_errx(1, "No memory for aes_get_wcs()");
235 r = mbstowcs(w, aes->aes_mbs.s, wcs_length);
238 aes->aes_set |= AES_SET_WCS;
239 return (aes->aes_wcs = w);
244 if (aes->aes_set & AES_SET_UTF8) {
245 /* Try converting UTF8 to WCS. */
246 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
247 if (aes->aes_wcs != NULL)
248 aes->aes_set |= AES_SET_WCS;
249 return (aes->aes_wcs);
255 aes_set_mbs(struct aes *aes, const char *mbs)
257 return (aes_copy_mbs(aes, mbs));
261 aes_copy_mbs(struct aes *aes, const char *mbs)
267 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
268 archive_strcpy(&(aes->aes_mbs), mbs);
269 archive_string_empty(&(aes->aes_utf8));
271 free((wchar_t *)(uintptr_t)aes->aes_wcs);
278 * The 'update' form tries to proactively update all forms of
279 * this string (WCS and MBS) and returns an error if any of
280 * them fail. This is used by the 'pax' handler, for instance,
281 * to detect and report character-conversion failures early while
282 * still allowing clients to get potentially useful values from
283 * the more tolerant lazy conversions. (get_mbs and get_wcs will
284 * strive to give the user something useful, so you can get hopefully
285 * usable values even if some of the character conversions are failing.)
288 aes_update_utf8(struct aes *aes, const char *utf8)
292 return (1); /* Succeeded in clearing everything. */
295 /* Save the UTF8 string. */
296 archive_strcpy(&(aes->aes_utf8), utf8);
298 /* Empty the mbs and wcs strings. */
299 archive_string_empty(&(aes->aes_mbs));
301 free((wchar_t *)(uintptr_t)aes->aes_wcs);
305 aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
307 /* TODO: We should just do a direct UTF-8 to MBS conversion
308 * here. That would be faster, use less space, and give the
309 * same information. (If a UTF-8 to MBS conversion succeeds,
310 * then UTF-8->WCS and Unicode->MBS conversions will both
313 /* Try converting UTF8 to WCS, return false on failure. */
314 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
315 if (aes->aes_wcs == NULL)
317 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS; /* Both UTF8 and WCS set. */
319 /* Try converting WCS to MBS, return false on failure. */
320 if (archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) == NULL)
322 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
324 /* All conversions succeeded. */
329 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
331 return aes_copy_wcs_len(aes, wcs, wcs == NULL ? 0 : wcslen(wcs));
335 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
343 aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
344 archive_string_empty(&(aes->aes_mbs));
345 archive_string_empty(&(aes->aes_utf8));
347 free((wchar_t *)(uintptr_t)aes->aes_wcs);
350 w = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
352 __archive_errx(1, "No memory for aes_copy_wcs()");
353 wmemcpy(w, wcs, len);
359 /****************************************************************************
363 ****************************************************************************/
365 struct archive_entry *
366 archive_entry_clear(struct archive_entry *entry)
370 aes_clean(&entry->ae_fflags_text);
371 aes_clean(&entry->ae_gname);
372 aes_clean(&entry->ae_hardlink);
373 aes_clean(&entry->ae_pathname);
374 aes_clean(&entry->ae_sourcepath);
375 aes_clean(&entry->ae_symlink);
376 aes_clean(&entry->ae_uname);
377 archive_entry_acl_clear(entry);
378 archive_entry_xattr_clear(entry);
380 memset(entry, 0, sizeof(*entry));
384 struct archive_entry *
385 archive_entry_clone(struct archive_entry *entry)
387 struct archive_entry *entry2;
388 struct ae_acl *ap, *ap2;
391 /* Allocate new structure and copy over all of the fields. */
392 entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
395 memset(entry2, 0, sizeof(*entry2));
396 entry2->ae_stat = entry->ae_stat;
397 entry2->ae_fflags_set = entry->ae_fflags_set;
398 entry2->ae_fflags_clear = entry->ae_fflags_clear;
400 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
401 aes_copy(&entry2->ae_gname, &entry->ae_gname);
402 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
403 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
404 aes_copy(&entry2->ae_sourcepath, &entry->ae_sourcepath);
405 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
406 entry2->ae_set = entry->ae_set;
407 aes_copy(&entry2->ae_uname, &entry->ae_uname);
409 /* Copy ACL data over. */
410 ap = entry->acl_head;
412 ap2 = acl_new_entry(entry2,
413 ap->type, ap->permset, ap->tag, ap->id);
415 aes_copy(&ap2->name, &ap->name);
419 /* Copy xattr data over. */
420 xp = entry->xattr_head;
422 archive_entry_xattr_add_entry(entry2,
423 xp->name, xp->value, xp->size);
431 archive_entry_free(struct archive_entry *entry)
433 archive_entry_clear(entry);
437 struct archive_entry *
438 archive_entry_new(void)
440 struct archive_entry *entry;
442 entry = (struct archive_entry *)malloc(sizeof(*entry));
445 memset(entry, 0, sizeof(*entry));
450 * Functions for reading fields from an archive_entry.
454 archive_entry_atime(struct archive_entry *entry)
456 return (entry->ae_stat.aest_atime);
460 archive_entry_atime_nsec(struct archive_entry *entry)
462 return (entry->ae_stat.aest_atime_nsec);
466 archive_entry_atime_is_set(struct archive_entry *entry)
468 return (entry->ae_set & AE_SET_ATIME);
472 archive_entry_birthtime(struct archive_entry *entry)
474 return (entry->ae_stat.aest_birthtime);
478 archive_entry_birthtime_nsec(struct archive_entry *entry)
480 return (entry->ae_stat.aest_birthtime_nsec);
484 archive_entry_birthtime_is_set(struct archive_entry *entry)
486 return (entry->ae_set & AE_SET_BIRTHTIME);
490 archive_entry_ctime(struct archive_entry *entry)
492 return (entry->ae_stat.aest_ctime);
496 archive_entry_ctime_is_set(struct archive_entry *entry)
498 return (entry->ae_set & AE_SET_CTIME);
502 archive_entry_ctime_nsec(struct archive_entry *entry)
504 return (entry->ae_stat.aest_ctime_nsec);
508 archive_entry_dev(struct archive_entry *entry)
510 if (entry->ae_stat.aest_dev_is_broken_down)
511 return ae_makedev(entry->ae_stat.aest_devmajor,
512 entry->ae_stat.aest_devminor);
514 return (entry->ae_stat.aest_dev);
518 archive_entry_devmajor(struct archive_entry *entry)
520 if (entry->ae_stat.aest_dev_is_broken_down)
521 return (entry->ae_stat.aest_devmajor);
523 return major(entry->ae_stat.aest_dev);
527 archive_entry_devminor(struct archive_entry *entry)
529 if (entry->ae_stat.aest_dev_is_broken_down)
530 return (entry->ae_stat.aest_devminor);
532 return minor(entry->ae_stat.aest_dev);
536 archive_entry_filetype(struct archive_entry *entry)
538 return (AE_IFMT & entry->ae_stat.aest_mode);
542 archive_entry_fflags(struct archive_entry *entry,
543 unsigned long *set, unsigned long *clear)
545 *set = entry->ae_fflags_set;
546 *clear = entry->ae_fflags_clear;
550 * Note: if text was provided, this just returns that text. If you
551 * really need the text to be rebuilt in a canonical form, set the
552 * text, ask for the bitmaps, then set the bitmaps. (Setting the
553 * bitmaps clears any stored text.) This design is deliberate: if
554 * we're editing archives, we don't want to discard flags just because
555 * they aren't supported on the current system. The bitmap<->text
556 * conversions are platform-specific (see below).
559 archive_entry_fflags_text(struct archive_entry *entry)
564 f = aes_get_mbs(&entry->ae_fflags_text);
568 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
571 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
575 aes_copy_mbs(&entry->ae_fflags_text, p);
577 f = aes_get_mbs(&entry->ae_fflags_text);
582 archive_entry_gid(struct archive_entry *entry)
584 return (entry->ae_stat.aest_gid);
588 archive_entry_gname(struct archive_entry *entry)
590 return (aes_get_mbs(&entry->ae_gname));
594 archive_entry_gname_w(struct archive_entry *entry)
596 return (aes_get_wcs(&entry->ae_gname));
600 archive_entry_hardlink(struct archive_entry *entry)
602 if (entry->ae_set & AE_SET_HARDLINK)
603 return (aes_get_mbs(&entry->ae_hardlink));
608 archive_entry_hardlink_w(struct archive_entry *entry)
610 if (entry->ae_set & AE_SET_HARDLINK)
611 return (aes_get_wcs(&entry->ae_hardlink));
616 archive_entry_ino(struct archive_entry *entry)
618 return (entry->ae_stat.aest_ino);
622 archive_entry_mode(struct archive_entry *entry)
624 return (entry->ae_stat.aest_mode);
628 archive_entry_mtime(struct archive_entry *entry)
630 return (entry->ae_stat.aest_mtime);
634 archive_entry_mtime_nsec(struct archive_entry *entry)
636 return (entry->ae_stat.aest_mtime_nsec);
640 archive_entry_mtime_is_set(struct archive_entry *entry)
642 return (entry->ae_set & AE_SET_MTIME);
646 archive_entry_nlink(struct archive_entry *entry)
648 return (entry->ae_stat.aest_nlink);
652 archive_entry_pathname(struct archive_entry *entry)
654 return (aes_get_mbs(&entry->ae_pathname));
658 archive_entry_pathname_w(struct archive_entry *entry)
660 return (aes_get_wcs(&entry->ae_pathname));
664 archive_entry_rdev(struct archive_entry *entry)
666 if (entry->ae_stat.aest_rdev_is_broken_down)
667 return ae_makedev(entry->ae_stat.aest_rdevmajor,
668 entry->ae_stat.aest_rdevminor);
670 return (entry->ae_stat.aest_rdev);
674 archive_entry_rdevmajor(struct archive_entry *entry)
676 if (entry->ae_stat.aest_rdev_is_broken_down)
677 return (entry->ae_stat.aest_rdevmajor);
679 return major(entry->ae_stat.aest_rdev);
683 archive_entry_rdevminor(struct archive_entry *entry)
685 if (entry->ae_stat.aest_rdev_is_broken_down)
686 return (entry->ae_stat.aest_rdevminor);
688 return minor(entry->ae_stat.aest_rdev);
692 archive_entry_size(struct archive_entry *entry)
694 return (entry->ae_stat.aest_size);
698 archive_entry_size_is_set(struct archive_entry *entry)
700 return (entry->ae_set & AE_SET_SIZE);
704 archive_entry_sourcepath(struct archive_entry *entry)
706 return (aes_get_mbs(&entry->ae_sourcepath));
710 archive_entry_symlink(struct archive_entry *entry)
712 if (entry->ae_set & AE_SET_SYMLINK)
713 return (aes_get_mbs(&entry->ae_symlink));
718 archive_entry_symlink_w(struct archive_entry *entry)
720 if (entry->ae_set & AE_SET_SYMLINK)
721 return (aes_get_wcs(&entry->ae_symlink));
726 archive_entry_uid(struct archive_entry *entry)
728 return (entry->ae_stat.aest_uid);
732 archive_entry_uname(struct archive_entry *entry)
734 return (aes_get_mbs(&entry->ae_uname));
738 archive_entry_uname_w(struct archive_entry *entry)
740 return (aes_get_wcs(&entry->ae_uname));
744 * Functions to set archive_entry properties.
748 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
750 entry->stat_valid = 0;
751 entry->ae_stat.aest_mode &= ~AE_IFMT;
752 entry->ae_stat.aest_mode |= AE_IFMT & type;
756 archive_entry_set_fflags(struct archive_entry *entry,
757 unsigned long set, unsigned long clear)
759 aes_clean(&entry->ae_fflags_text);
760 entry->ae_fflags_set = set;
761 entry->ae_fflags_clear = clear;
765 archive_entry_copy_fflags_text(struct archive_entry *entry,
768 aes_copy_mbs(&entry->ae_fflags_text, flags);
769 return (ae_strtofflags(flags,
770 &entry->ae_fflags_set, &entry->ae_fflags_clear));
774 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
775 const wchar_t *flags)
777 aes_copy_wcs(&entry->ae_fflags_text, flags);
778 return (ae_wcstofflags(flags,
779 &entry->ae_fflags_set, &entry->ae_fflags_clear));
783 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
785 entry->stat_valid = 0;
786 entry->ae_stat.aest_gid = g;
790 archive_entry_set_gname(struct archive_entry *entry, const char *name)
792 aes_set_mbs(&entry->ae_gname, name);
796 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
798 aes_copy_mbs(&entry->ae_gname, name);
802 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
804 aes_copy_wcs(&entry->ae_gname, name);
808 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
810 return (aes_update_utf8(&entry->ae_gname, name));
814 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
816 entry->stat_valid = 0;
817 entry->ae_stat.aest_ino = ino;
821 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
823 aes_set_mbs(&entry->ae_hardlink, target);
825 entry->ae_set |= AE_SET_HARDLINK;
827 entry->ae_set &= ~AE_SET_HARDLINK;
831 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
833 aes_copy_mbs(&entry->ae_hardlink, target);
835 entry->ae_set |= AE_SET_HARDLINK;
837 entry->ae_set &= ~AE_SET_HARDLINK;
841 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
843 aes_copy_wcs(&entry->ae_hardlink, target);
845 entry->ae_set |= AE_SET_HARDLINK;
847 entry->ae_set &= ~AE_SET_HARDLINK;
851 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
853 entry->stat_valid = 0;
854 entry->ae_set |= AE_SET_ATIME;
855 entry->ae_stat.aest_atime = t;
856 entry->ae_stat.aest_atime_nsec = ns;
860 archive_entry_unset_atime(struct archive_entry *entry)
862 archive_entry_set_atime(entry, 0, 0);
863 entry->ae_set &= ~AE_SET_ATIME;
867 archive_entry_set_birthtime(struct archive_entry *entry, time_t m, long ns)
869 entry->stat_valid = 0;
870 entry->ae_set |= AE_SET_BIRTHTIME;
871 entry->ae_stat.aest_birthtime = m;
872 entry->ae_stat.aest_birthtime_nsec = ns;
876 archive_entry_unset_birthtime(struct archive_entry *entry)
878 archive_entry_set_birthtime(entry, 0, 0);
879 entry->ae_set &= ~AE_SET_BIRTHTIME;
883 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
885 entry->stat_valid = 0;
886 entry->ae_set |= AE_SET_CTIME;
887 entry->ae_stat.aest_ctime = t;
888 entry->ae_stat.aest_ctime_nsec = ns;
892 archive_entry_unset_ctime(struct archive_entry *entry)
894 archive_entry_set_ctime(entry, 0, 0);
895 entry->ae_set &= ~AE_SET_CTIME;
899 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
901 entry->stat_valid = 0;
902 entry->ae_stat.aest_dev_is_broken_down = 0;
903 entry->ae_stat.aest_dev = d;
907 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
909 entry->stat_valid = 0;
910 entry->ae_stat.aest_dev_is_broken_down = 1;
911 entry->ae_stat.aest_devmajor = m;
915 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
917 entry->stat_valid = 0;
918 entry->ae_stat.aest_dev_is_broken_down = 1;
919 entry->ae_stat.aest_devminor = m;
922 /* Set symlink if symlink is already set, else set hardlink. */
924 archive_entry_set_link(struct archive_entry *entry, const char *target)
926 if (entry->ae_set & AE_SET_SYMLINK)
927 aes_set_mbs(&entry->ae_symlink, target);
929 aes_set_mbs(&entry->ae_hardlink, target);
932 /* Set symlink if symlink is already set, else set hardlink. */
934 archive_entry_copy_link(struct archive_entry *entry, const char *target)
936 if (entry->ae_set & AE_SET_SYMLINK)
937 aes_copy_mbs(&entry->ae_symlink, target);
939 aes_copy_mbs(&entry->ae_hardlink, target);
942 /* Set symlink if symlink is already set, else set hardlink. */
944 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
946 if (entry->ae_set & AE_SET_SYMLINK)
947 aes_copy_wcs(&entry->ae_symlink, target);
949 aes_copy_wcs(&entry->ae_hardlink, target);
953 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
955 if (entry->ae_set & AE_SET_SYMLINK)
956 return (aes_update_utf8(&entry->ae_symlink, target));
958 return (aes_update_utf8(&entry->ae_hardlink, target));
962 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
964 entry->stat_valid = 0;
965 entry->ae_stat.aest_mode = m;
969 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
971 entry->stat_valid = 0;
972 entry->ae_set |= AE_SET_MTIME;
973 entry->ae_stat.aest_mtime = m;
974 entry->ae_stat.aest_mtime_nsec = ns;
978 archive_entry_unset_mtime(struct archive_entry *entry)
980 archive_entry_set_mtime(entry, 0, 0);
981 entry->ae_set &= ~AE_SET_MTIME;
985 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
987 entry->stat_valid = 0;
988 entry->ae_stat.aest_nlink = nlink;
992 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
994 aes_set_mbs(&entry->ae_pathname, name);
998 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
1000 aes_copy_mbs(&entry->ae_pathname, name);
1004 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
1006 aes_copy_wcs(&entry->ae_pathname, name);
1010 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
1012 return (aes_update_utf8(&entry->ae_pathname, name));
1016 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
1018 entry->stat_valid = 0;
1019 entry->ae_stat.aest_mode &= AE_IFMT;
1020 entry->ae_stat.aest_mode |= ~AE_IFMT & p;
1024 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
1026 entry->stat_valid = 0;
1027 entry->ae_stat.aest_rdev = m;
1028 entry->ae_stat.aest_rdev_is_broken_down = 0;
1032 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
1034 entry->stat_valid = 0;
1035 entry->ae_stat.aest_rdev_is_broken_down = 1;
1036 entry->ae_stat.aest_rdevmajor = m;
1040 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
1042 entry->stat_valid = 0;
1043 entry->ae_stat.aest_rdev_is_broken_down = 1;
1044 entry->ae_stat.aest_rdevminor = m;
1048 archive_entry_set_size(struct archive_entry *entry, int64_t s)
1050 entry->stat_valid = 0;
1051 entry->ae_stat.aest_size = s;
1052 entry->ae_set |= AE_SET_SIZE;
1056 archive_entry_unset_size(struct archive_entry *entry)
1058 archive_entry_set_size(entry, 0);
1059 entry->ae_set &= ~AE_SET_SIZE;
1063 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1065 aes_set_mbs(&entry->ae_sourcepath, path);
1069 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1071 aes_set_mbs(&entry->ae_symlink, linkname);
1072 if (linkname != NULL)
1073 entry->ae_set |= AE_SET_SYMLINK;
1075 entry->ae_set &= ~AE_SET_SYMLINK;
1079 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1081 aes_copy_mbs(&entry->ae_symlink, linkname);
1082 if (linkname != NULL)
1083 entry->ae_set |= AE_SET_SYMLINK;
1085 entry->ae_set &= ~AE_SET_SYMLINK;
1089 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1091 aes_copy_wcs(&entry->ae_symlink, linkname);
1092 if (linkname != NULL)
1093 entry->ae_set |= AE_SET_SYMLINK;
1095 entry->ae_set &= ~AE_SET_SYMLINK;
1099 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
1101 entry->stat_valid = 0;
1102 entry->ae_stat.aest_uid = u;
1106 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1108 aes_set_mbs(&entry->ae_uname, name);
1112 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1114 aes_copy_mbs(&entry->ae_uname, name);
1118 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1120 aes_copy_wcs(&entry->ae_uname, name);
1124 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1126 return (aes_update_utf8(&entry->ae_uname, name));
1130 * ACL management. The following would, of course, be a lot simpler
1131 * if: 1) the last draft of POSIX.1e were a really thorough and
1132 * complete standard that addressed the needs of ACL archiving and 2)
1133 * everyone followed it faithfully. Alas, neither is true, so the
1134 * following is a lot more complex than might seem necessary to the
1139 archive_entry_acl_clear(struct archive_entry *entry)
1143 while (entry->acl_head != NULL) {
1144 ap = entry->acl_head->next;
1145 aes_clean(&entry->acl_head->name);
1146 free(entry->acl_head);
1147 entry->acl_head = ap;
1149 if (entry->acl_text_w != NULL) {
1150 free(entry->acl_text_w);
1151 entry->acl_text_w = NULL;
1153 entry->acl_p = NULL;
1154 entry->acl_state = 0; /* Not counting. */
1158 * Add a single ACL entry to the internal list of ACL data.
1161 archive_entry_acl_add_entry(struct archive_entry *entry,
1162 int type, int permset, int tag, int id, const char *name)
1166 if (acl_special(entry, type, permset, tag) == 0)
1168 ap = acl_new_entry(entry, type, permset, tag, id);
1173 if (name != NULL && *name != '\0')
1174 aes_copy_mbs(&ap->name, name);
1176 aes_clean(&ap->name);
1180 * As above, but with a wide-character name.
1183 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1184 int type, int permset, int tag, int id, const wchar_t *name)
1186 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
1190 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
1191 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
1195 if (acl_special(entry, type, permset, tag) == 0)
1197 ap = acl_new_entry(entry, type, permset, tag, id);
1202 if (name != NULL && *name != L'\0' && len > 0)
1203 aes_copy_wcs_len(&ap->name, name, len);
1205 aes_clean(&ap->name);
1209 * If this ACL entry is part of the standard POSIX permissions set,
1210 * store the permissions in the stat structure and return zero.
1213 acl_special(struct archive_entry *entry, int type, int permset, int tag)
1215 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
1217 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1218 entry->ae_stat.aest_mode &= ~0700;
1219 entry->ae_stat.aest_mode |= (permset & 7) << 6;
1221 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1222 entry->ae_stat.aest_mode &= ~0070;
1223 entry->ae_stat.aest_mode |= (permset & 7) << 3;
1225 case ARCHIVE_ENTRY_ACL_OTHER:
1226 entry->ae_stat.aest_mode &= ~0007;
1227 entry->ae_stat.aest_mode |= permset & 7;
1235 * Allocate and populate a new ACL entry with everything but the
1238 static struct ae_acl *
1239 acl_new_entry(struct archive_entry *entry,
1240 int type, int permset, int tag, int id)
1242 struct ae_acl *ap, *aq;
1244 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
1245 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
1247 if (entry->acl_text_w != NULL) {
1248 free(entry->acl_text_w);
1249 entry->acl_text_w = NULL;
1252 /* XXX TODO: More sanity-checks on the arguments XXX */
1254 /* If there's a matching entry already in the list, overwrite it. */
1255 ap = entry->acl_head;
1257 while (ap != NULL) {
1258 if (ap->type == type && ap->tag == tag && ap->id == id) {
1259 ap->permset = permset;
1266 /* Add a new entry to the end of the list. */
1267 ap = (struct ae_acl *)malloc(sizeof(*ap));
1270 memset(ap, 0, sizeof(*ap));
1272 entry->acl_head = ap;
1278 ap->permset = permset;
1283 * Return a count of entries matching "want_type".
1286 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1292 ap = entry->acl_head;
1293 while (ap != NULL) {
1294 if ((ap->type & want_type) != 0)
1299 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1305 * Prepare for reading entries from the ACL data. Returns a count
1306 * of entries matching "want_type", or zero if there are no
1307 * non-extended ACL entries of that type.
1310 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1314 count = archive_entry_acl_count(entry, want_type);
1317 * If the only entries are the three standard ones,
1318 * then don't return any ACL data. (In this case,
1319 * client can just use chmod(2) to set permissions.)
1321 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1327 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1329 entry->acl_state = 0;
1330 entry->acl_p = entry->acl_head;
1335 * Return the next ACL entry in the list. Fake entries for the
1336 * standard permissions and include them in the returned list.
1340 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1341 int *permset, int *tag, int *id, const char **name)
1347 * The acl_state is either zero (no entries available), -1
1348 * (reading from list), or an entry type (retrieve that type
1349 * from ae_stat.aest_mode).
1351 if (entry->acl_state == 0)
1352 return (ARCHIVE_WARN);
1354 /* The first three access entries are special. */
1355 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1356 switch (entry->acl_state) {
1357 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1358 *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1359 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1360 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1361 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1362 return (ARCHIVE_OK);
1363 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1364 *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1365 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1366 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1367 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1368 return (ARCHIVE_OK);
1369 case ARCHIVE_ENTRY_ACL_OTHER:
1370 *permset = entry->ae_stat.aest_mode & 7;
1371 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1372 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1373 entry->acl_state = -1;
1374 entry->acl_p = entry->acl_head;
1375 return (ARCHIVE_OK);
1381 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1382 entry->acl_p = entry->acl_p->next;
1383 if (entry->acl_p == NULL) {
1384 entry->acl_state = 0;
1390 return (ARCHIVE_EOF); /* End of ACL entries. */
1392 *type = entry->acl_p->type;
1393 *permset = entry->acl_p->permset;
1394 *tag = entry->acl_p->tag;
1395 *id = entry->acl_p->id;
1396 *name = aes_get_mbs(&entry->acl_p->name);
1397 entry->acl_p = entry->acl_p->next;
1398 return (ARCHIVE_OK);
1402 * Generate a text version of the ACL. The flags parameter controls
1403 * the style of the generated ACL.
1406 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1410 const wchar_t *wname;
1411 const wchar_t *prefix;
1417 if (entry->acl_text_w != NULL) {
1418 free (entry->acl_text_w);
1419 entry->acl_text_w = NULL;
1425 ap = entry->acl_head;
1426 while (ap != NULL) {
1427 if ((ap->type & flags) != 0) {
1429 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1430 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1431 length += 8; /* "default:" */
1432 length += 5; /* tag name */
1433 length += 1; /* colon */
1434 wname = aes_get_wcs(&ap->name);
1436 length += wcslen(wname);
1438 length += sizeof(uid_t) * 3 + 1;
1439 length ++; /* colon */
1440 length += 3; /* rwx */
1441 length += 1; /* colon */
1442 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1443 length ++; /* newline */
1448 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1449 length += 10; /* "user::rwx\n" */
1450 length += 11; /* "group::rwx\n" */
1451 length += 11; /* "other::rwx\n" */
1457 /* Now, allocate the string and actually populate it. */
1458 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1460 __archive_errx(1, "No memory to generate the text version of the ACL");
1462 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1463 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1464 entry->ae_stat.aest_mode & 0700, -1);
1466 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1467 entry->ae_stat.aest_mode & 0070, -1);
1469 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1470 entry->ae_stat.aest_mode & 0007, -1);
1473 ap = entry->acl_head;
1474 while (ap != NULL) {
1475 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1476 wname = aes_get_wcs(&ap->name);
1478 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1482 append_entry_w(&wp, NULL, ap->tag, wname,
1491 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1492 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1493 prefix = L"default:";
1496 ap = entry->acl_head;
1498 while (ap != NULL) {
1499 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1500 wname = aes_get_wcs(&ap->name);
1503 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1507 append_entry_w(&wp, prefix, ap->tag,
1508 wname, ap->permset, id);
1515 return (entry->acl_text_w);
1519 append_id_w(wchar_t **wp, int id)
1524 append_id_w(wp, id / 10);
1525 *(*wp)++ = L"0123456789"[id % 10];
1529 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1530 const wchar_t *wname, int perm, int id)
1532 if (prefix != NULL) {
1533 wcscpy(*wp, prefix);
1537 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1541 case ARCHIVE_ENTRY_ACL_USER:
1542 wcscpy(*wp, L"user");
1544 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1548 case ARCHIVE_ENTRY_ACL_GROUP:
1549 wcscpy(*wp, L"group");
1551 case ARCHIVE_ENTRY_ACL_MASK:
1552 wcscpy(*wp, L"mask");
1556 case ARCHIVE_ENTRY_ACL_OTHER:
1557 wcscpy(*wp, L"other");
1564 if (wname != NULL) {
1567 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1568 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1569 append_id_w(wp, id);
1573 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1574 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1575 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1578 append_id_w(wp, id);
1584 * Parse a textual ACL. This automatically recognizes and supports
1585 * extensions described above. The 'type' argument is used to
1586 * indicate the type that should be used for any entries not
1587 * explicitly marked as "default:".
1590 __archive_entry_acl_parse_w(struct archive_entry *entry,
1591 const wchar_t *text, int default_type)
1594 const wchar_t *start;
1599 int type, tag, permset, id;
1602 while (text != NULL && *text != L'\0') {
1604 * Parse the fields out of the next entry,
1605 * advance 'text' to start of next entry.
1609 const wchar_t *start, *end;
1610 next_field_w(&text, &start, &end, &sep);
1612 field[fields].start = start;
1613 field[fields].end = end;
1616 } while (sep == L':');
1618 /* Check for a numeric ID in field 1 or 3. */
1620 isint_w(field[1].start, field[1].end, &id);
1621 /* Field 3 is optional. */
1622 if (id == -1 && fields > 3)
1623 isint_w(field[3].start, field[3].end, &id);
1626 * Solaris extension: "defaultuser::rwx" is the
1627 * default ACL corresponding to "user::rwx", etc.
1629 if (field[0].end-field[0].start > 7
1630 && wmemcmp(field[0].start, L"default", 7) == 0) {
1631 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1632 field[0].start += 7;
1634 type = default_type;
1636 name.start = name.end = NULL;
1637 if (prefix_w(field[0].start, field[0].end, L"user")) {
1638 if (!ismode_w(field[2].start, field[2].end, &permset))
1639 return (ARCHIVE_WARN);
1640 if (id != -1 || field[1].start < field[1].end) {
1641 tag = ARCHIVE_ENTRY_ACL_USER;
1644 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1645 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1646 if (!ismode_w(field[2].start, field[2].end, &permset))
1647 return (ARCHIVE_WARN);
1648 if (id != -1 || field[1].start < field[1].end) {
1649 tag = ARCHIVE_ENTRY_ACL_GROUP;
1652 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1653 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1655 && field[1].start < field[1].end
1656 && ismode_w(field[1].start, field[2].end, &permset)) {
1657 /* This is Solaris-style "other:rwx" */
1658 } else if (fields == 3
1659 && field[1].start == field[1].end
1660 && field[2].start < field[2].end
1661 && ismode_w(field[2].start, field[2].end, &permset)) {
1662 /* This is FreeBSD-style "other::rwx" */
1664 return (ARCHIVE_WARN);
1665 tag = ARCHIVE_ENTRY_ACL_OTHER;
1666 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1668 && field[1].start < field[1].end
1669 && ismode_w(field[1].start, field[1].end, &permset)) {
1670 /* This is Solaris-style "mask:rwx" */
1671 } else if (fields == 3
1672 && field[1].start == field[1].end
1673 && field[2].start < field[2].end
1674 && ismode_w(field[2].start, field[2].end, &permset)) {
1675 /* This is FreeBSD-style "mask::rwx" */
1677 return (ARCHIVE_WARN);
1678 tag = ARCHIVE_ENTRY_ACL_MASK;
1680 return (ARCHIVE_WARN);
1682 /* Add entry to the internal list. */
1683 archive_entry_acl_add_entry_w_len(entry, type, permset,
1684 tag, id, name.start, name.end - name.start);
1686 return (ARCHIVE_OK);
1690 * extended attribute handling
1694 archive_entry_xattr_clear(struct archive_entry *entry)
1696 struct ae_xattr *xp;
1698 while (entry->xattr_head != NULL) {
1699 xp = entry->xattr_head->next;
1700 free(entry->xattr_head->name);
1701 free(entry->xattr_head->value);
1702 free(entry->xattr_head);
1703 entry->xattr_head = xp;
1706 entry->xattr_head = NULL;
1710 archive_entry_xattr_add_entry(struct archive_entry *entry,
1711 const char *name, const void *value, size_t size)
1713 struct ae_xattr *xp;
1715 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1718 if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1722 xp->name = strdup(name);
1723 if ((xp->value = malloc(size)) != NULL) {
1724 memcpy(xp->value, value, size);
1729 xp->next = entry->xattr_head;
1730 entry->xattr_head = xp;
1735 * returns number of the extended attribute entries
1738 archive_entry_xattr_count(struct archive_entry *entry)
1740 struct ae_xattr *xp;
1743 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1750 archive_entry_xattr_reset(struct archive_entry * entry)
1752 entry->xattr_p = entry->xattr_head;
1754 return archive_entry_xattr_count(entry);
1758 archive_entry_xattr_next(struct archive_entry * entry,
1759 const char **name, const void **value, size_t *size)
1761 if (entry->xattr_p) {
1762 *name = entry->xattr_p->name;
1763 *value = entry->xattr_p->value;
1764 *size = entry->xattr_p->size;
1766 entry->xattr_p = entry->xattr_p->next;
1768 return (ARCHIVE_OK);
1773 return (ARCHIVE_WARN);
1778 * end of xattr handling
1782 * Parse a string to a positive decimal integer. Returns true if
1783 * the string is non-empty and consists only of decimal digits,
1787 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1792 while (start < end) {
1793 if (*start < '0' || *start > '9')
1795 if (n > (INT_MAX / 10))
1808 * Parse a string as a mode field. Returns true if
1809 * the string is non-empty and consists only of mode characters,
1813 ismode_w(const wchar_t *start, const wchar_t *end, int *permset)
1822 *permset |= ARCHIVE_ENTRY_ACL_READ;
1825 *permset |= ARCHIVE_ENTRY_ACL_WRITE;
1828 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1840 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1841 * to point to just after the separator. *start points to the first
1842 * character of the matched text and *end just after the last
1843 * character of the matched identifier. In particular *end - *start
1844 * is the length of the field body, not including leading or trailing
1848 next_field_w(const wchar_t **wp, const wchar_t **start,
1849 const wchar_t **end, wchar_t *sep)
1851 /* Skip leading whitespace to find start of field. */
1852 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1857 /* Scan for the separator. */
1858 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1864 /* Trim trailing whitespace to locate end of field. */
1866 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1871 /* Adjust scanner location. */
1877 * Return true if the characters [start...end) are a prefix of 'test'.
1878 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1881 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1886 if (*start++ != *test++)
1889 while (start < end && *start++ == *test++)
1900 * Following code is modified from UC Berkeley sources, and
1901 * is subject to the following copyright notice.
1905 * Copyright (c) 1993
1906 * The Regents of the University of California. All rights reserved.
1908 * Redistribution and use in source and binary forms, with or without
1909 * modification, are permitted provided that the following conditions
1911 * 1. Redistributions of source code must retain the above copyright
1912 * notice, this list of conditions and the following disclaimer.
1913 * 2. Redistributions in binary form must reproduce the above copyright
1914 * notice, this list of conditions and the following disclaimer in the
1915 * documentation and/or other materials provided with the distribution.
1916 * 4. Neither the name of the University nor the names of its contributors
1917 * may be used to endorse or promote products derived from this software
1918 * without specific prior written permission.
1920 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1921 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1922 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1923 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1924 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1925 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1926 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1927 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1928 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1929 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1933 static struct flag {
1935 const wchar_t *wname;
1937 unsigned long clear;
1939 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1941 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1942 { "nosappend", L"nosappend", SF_APPEND, 0 },
1944 #ifdef EXT2_APPEND_FL /* 'a' */
1945 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1946 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1949 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1950 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1953 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1954 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1955 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1957 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1958 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1959 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1960 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1963 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1964 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1967 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1970 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1971 { "nouappend", L"nouappend", UF_APPEND, 0 },
1974 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1975 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1976 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1979 { "nodump", L"nodump", 0, UF_NODUMP},
1981 #ifdef EXT2_NODUMP_FL /* 'd' */
1982 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1985 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1988 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1989 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1991 #ifdef EXT2_COMPR_FL /* 'c' */
1992 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1995 #ifdef EXT2_NOATIME_FL /* 'A' */
1996 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1998 { NULL, NULL, 0, 0 }
2003 * Convert file flags to a comma-separated string. If no flags
2004 * are set, return the empty string.
2007 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
2015 bits = bitset | bitclear;
2017 for (flag = flags; flag->name != NULL; flag++)
2018 if (bits & (flag->set | flag->clear)) {
2019 length += strlen(flag->name) + 1;
2020 bits &= ~(flag->set | flag->clear);
2025 string = (char *)malloc(length);
2030 for (flag = flags; flag->name != NULL; flag++) {
2031 if (bitset & flag->set || bitclear & flag->clear) {
2032 sp = flag->name + 2;
2033 } else if (bitset & flag->clear || bitclear & flag->set) {
2037 bitset &= ~(flag->set | flag->clear);
2038 bitclear &= ~(flag->set | flag->clear);
2041 while ((*dp++ = *sp++) != '\0')
2052 * Take string of arguments and return file flags. This
2053 * version works a little differently than strtofflags(3).
2054 * In particular, it always tests every token, skipping any
2055 * unrecognized tokens. It returns a pointer to the first
2056 * unrecognized token, or NULL if every token was recognized.
2057 * This version is also const-correct and does not modify the
2061 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
2063 const char *start, *end;
2065 unsigned long set, clear;
2071 /* Find start of first token. */
2072 while (*start == '\t' || *start == ' ' || *start == ',')
2074 while (*start != '\0') {
2075 /* Locate end of token. */
2077 while (*end != '\0' && *end != '\t' &&
2078 *end != ' ' && *end != ',')
2080 for (flag = flags; flag->name != NULL; flag++) {
2081 if (memcmp(start, flag->name, end - start) == 0) {
2082 /* Matched "noXXXX", so reverse the sense. */
2086 } else if (memcmp(start, flag->name + 2, end - start)
2088 /* Matched "XXXX", so don't reverse. */
2090 clear |= flag->clear;
2094 /* Ignore unknown flag names. */
2095 if (flag->name == NULL && failed == NULL)
2098 /* Find start of next token. */
2100 while (*start == '\t' || *start == ' ' || *start == ',')
2110 /* Return location of first failure. */
2116 * Take string of arguments and return file flags. This
2117 * version works a little differently than strtofflags(3).
2118 * In particular, it always tests every token, skipping any
2119 * unrecognized tokens. It returns a pointer to the first
2120 * unrecognized token, or NULL if every token was recognized.
2121 * This version is also const-correct and does not modify the
2124 static const wchar_t *
2125 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
2127 const wchar_t *start, *end;
2129 unsigned long set, clear;
2130 const wchar_t *failed;
2135 /* Find start of first token. */
2136 while (*start == L'\t' || *start == L' ' || *start == L',')
2138 while (*start != L'\0') {
2139 /* Locate end of token. */
2141 while (*end != L'\0' && *end != L'\t' &&
2142 *end != L' ' && *end != L',')
2144 for (flag = flags; flag->wname != NULL; flag++) {
2145 if (wmemcmp(start, flag->wname, end - start) == 0) {
2146 /* Matched "noXXXX", so reverse the sense. */
2150 } else if (wmemcmp(start, flag->wname + 2, end - start)
2152 /* Matched "XXXX", so don't reverse. */
2154 clear |= flag->clear;
2158 /* Ignore unknown flag names. */
2159 if (flag->wname == NULL && failed == NULL)
2162 /* Find start of next token. */
2164 while (*start == L'\t' || *start == L' ' || *start == L',')
2174 /* Return location of first failure. */
2182 main(int argc, char **argv)
2184 struct archive_entry *entry = archive_entry_new();
2185 unsigned long set, clear;
2186 const wchar_t *remainder;
2188 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
2189 archive_entry_fflags(entry, &set, &clear);
2191 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
2193 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));