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 #elif 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 #if !defined(HAVE_MAJOR) && !defined(major)
79 /* Replacement for major/minor/makedev. */
80 #define major(x) ((int)(0x00ff & ((x) >> 8)))
81 #define minor(x) ((int)(0xffff00ff & (x)))
82 #define makedev(maj,min) ((0xff00 & ((maj)<<8)) | (0xffff00ff & (min)))
85 /* Play games to come up with a suitable makedev() definition. */
88 #include <sys/netmgr.h>
89 #define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
91 /* There's a "makedev" macro. */
92 #define ae_makedev(maj, min) makedev((maj), (min))
93 #elif defined mkdev || ((defined _WIN32 || defined __WIN32__) && !defined(__CYGWIN__))
95 #define ae_makedev(maj, min) mkdev((maj), (min))
97 /* There's a "makedev" function. */
98 #define ae_makedev(maj, min) makedev((maj), (min))
101 static void aes_clean(struct aes *);
102 static void aes_copy(struct aes *dest, struct aes *src);
103 static const char * aes_get_mbs(struct aes *);
104 static const wchar_t * aes_get_wcs(struct aes *);
105 static int aes_set_mbs(struct aes *, const char *mbs);
106 static int aes_copy_mbs(struct aes *, const char *mbs);
107 /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
108 static int aes_copy_wcs(struct aes *, const wchar_t *wcs);
109 static int aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
111 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
112 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
113 unsigned long *setp, unsigned long *clrp);
114 static const char *ae_strtofflags(const char *stringp,
115 unsigned long *setp, unsigned long *clrp);
116 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
117 const wchar_t *wname, int perm, int id);
118 static void append_id_w(wchar_t **wp, int id);
120 static int acl_special(struct archive_entry *entry,
121 int type, int permset, int tag);
122 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
123 int type, int permset, int tag, int id);
124 static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
125 static int ismode_w(const wchar_t *start, const wchar_t *end, int *result);
126 static void next_field_w(const wchar_t **wp, const wchar_t **start,
127 const wchar_t **end, wchar_t *sep);
128 static int prefix_w(const wchar_t *start, const wchar_t *end,
129 const wchar_t *test);
131 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
132 int permset, int tag, int id, const wchar_t *name, size_t);
136 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
139 while ((*s1 = *s2) != L'\0')
145 static size_t wcslen(const wchar_t *s)
147 const wchar_t *p = s;
154 /* Good enough for simple equality testing, but not for sorting. */
155 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
158 #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
162 aes_clean(struct aes *aes)
165 free((wchar_t *)(uintptr_t)aes->aes_wcs);
168 archive_string_free(&(aes->aes_mbs));
169 archive_string_free(&(aes->aes_utf8));
174 aes_copy(struct aes *dest, struct aes *src)
178 dest->aes_set = src->aes_set;
179 archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
180 archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
182 if (src->aes_wcs != NULL) {
183 wp = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
186 __archive_errx(1, "No memory for aes_copy()");
187 wcscpy(wp, src->aes_wcs);
193 aes_get_utf8(struct aes *aes)
195 if (aes->aes_set & AES_SET_UTF8)
196 return (aes->aes_utf8.s);
197 if ((aes->aes_set & AES_SET_WCS)
198 && archive_strappend_w_utf8(&(aes->aes_utf8), aes->aes_wcs) != NULL) {
199 aes->aes_set |= AES_SET_UTF8;
200 return (aes->aes_utf8.s);
206 aes_get_mbs(struct aes *aes)
208 /* If we already have an MBS form, return that immediately. */
209 if (aes->aes_set & AES_SET_MBS)
210 return (aes->aes_mbs.s);
211 /* If there's a WCS form, try converting with the native locale. */
212 if ((aes->aes_set & AES_SET_WCS)
213 && archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) != NULL) {
214 aes->aes_set |= AES_SET_MBS;
215 return (aes->aes_mbs.s);
217 /* We'll use UTF-8 for MBS if all else fails. */
218 return (aes_get_utf8(aes));
221 static const wchar_t *
222 aes_get_wcs(struct aes *aes)
227 /* Return WCS form if we already have it. */
228 if (aes->aes_set & AES_SET_WCS)
229 return (aes->aes_wcs);
231 if (aes->aes_set & AES_SET_MBS) {
232 /* Try converting MBS to WCS using native locale. */
234 * No single byte will be more than one wide character,
235 * so this length estimate will always be big enough.
237 size_t wcs_length = aes->aes_mbs.length;
239 w = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
241 __archive_errx(1, "No memory for aes_get_wcs()");
242 r = mbstowcs(w, aes->aes_mbs.s, wcs_length);
243 if (r != (size_t)-1 && r != 0) {
245 aes->aes_set |= AES_SET_WCS;
246 return (aes->aes_wcs = w);
251 if (aes->aes_set & AES_SET_UTF8) {
252 /* Try converting UTF8 to WCS. */
253 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
254 if (aes->aes_wcs != NULL)
255 aes->aes_set |= AES_SET_WCS;
256 return (aes->aes_wcs);
262 aes_set_mbs(struct aes *aes, const char *mbs)
264 return (aes_copy_mbs(aes, mbs));
268 aes_copy_mbs(struct aes *aes, const char *mbs)
274 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
275 archive_strcpy(&(aes->aes_mbs), mbs);
276 archive_string_empty(&(aes->aes_utf8));
278 free((wchar_t *)(uintptr_t)aes->aes_wcs);
285 * The 'update' form tries to proactively update all forms of
286 * this string (WCS and MBS) and returns an error if any of
287 * them fail. This is used by the 'pax' handler, for instance,
288 * to detect and report character-conversion failures early while
289 * still allowing clients to get potentially useful values from
290 * the more tolerant lazy conversions. (get_mbs and get_wcs will
291 * strive to give the user something useful, so you can get hopefully
292 * usable values even if some of the character conversions are failing.)
295 aes_update_utf8(struct aes *aes, const char *utf8)
299 return (1); /* Succeeded in clearing everything. */
302 /* Save the UTF8 string. */
303 archive_strcpy(&(aes->aes_utf8), utf8);
305 /* Empty the mbs and wcs strings. */
306 archive_string_empty(&(aes->aes_mbs));
308 free((wchar_t *)(uintptr_t)aes->aes_wcs);
312 aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
314 /* TODO: We should just do a direct UTF-8 to MBS conversion
315 * here. That would be faster, use less space, and give the
316 * same information. (If a UTF-8 to MBS conversion succeeds,
317 * then UTF-8->WCS and Unicode->MBS conversions will both
320 /* Try converting UTF8 to WCS, return false on failure. */
321 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
322 if (aes->aes_wcs == NULL)
324 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS; /* Both UTF8 and WCS set. */
326 /* Try converting WCS to MBS, return false on failure. */
327 if (archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) == NULL)
329 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
331 /* All conversions succeeded. */
336 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
338 return aes_copy_wcs_len(aes, wcs, wcs == NULL ? 0 : wcslen(wcs));
342 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
350 aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
351 archive_string_empty(&(aes->aes_mbs));
352 archive_string_empty(&(aes->aes_utf8));
354 free((wchar_t *)(uintptr_t)aes->aes_wcs);
357 w = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
359 __archive_errx(1, "No memory for aes_copy_wcs()");
360 wmemcpy(w, wcs, len);
366 /****************************************************************************
370 ****************************************************************************/
372 struct archive_entry *
373 archive_entry_clear(struct archive_entry *entry)
377 aes_clean(&entry->ae_fflags_text);
378 aes_clean(&entry->ae_gname);
379 aes_clean(&entry->ae_hardlink);
380 aes_clean(&entry->ae_pathname);
381 aes_clean(&entry->ae_sourcepath);
382 aes_clean(&entry->ae_symlink);
383 aes_clean(&entry->ae_uname);
384 archive_entry_acl_clear(entry);
385 archive_entry_xattr_clear(entry);
387 memset(entry, 0, sizeof(*entry));
391 struct archive_entry *
392 archive_entry_clone(struct archive_entry *entry)
394 struct archive_entry *entry2;
395 struct ae_acl *ap, *ap2;
398 /* Allocate new structure and copy over all of the fields. */
399 entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
402 memset(entry2, 0, sizeof(*entry2));
403 entry2->ae_stat = entry->ae_stat;
404 entry2->ae_fflags_set = entry->ae_fflags_set;
405 entry2->ae_fflags_clear = entry->ae_fflags_clear;
407 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
408 aes_copy(&entry2->ae_gname, &entry->ae_gname);
409 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
410 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
411 aes_copy(&entry2->ae_sourcepath, &entry->ae_sourcepath);
412 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
413 entry2->ae_set = entry->ae_set;
414 aes_copy(&entry2->ae_uname, &entry->ae_uname);
416 /* Copy ACL data over. */
417 ap = entry->acl_head;
419 ap2 = acl_new_entry(entry2,
420 ap->type, ap->permset, ap->tag, ap->id);
422 aes_copy(&ap2->name, &ap->name);
426 /* Copy xattr data over. */
427 xp = entry->xattr_head;
429 archive_entry_xattr_add_entry(entry2,
430 xp->name, xp->value, xp->size);
438 archive_entry_free(struct archive_entry *entry)
440 archive_entry_clear(entry);
444 struct archive_entry *
445 archive_entry_new(void)
447 struct archive_entry *entry;
449 entry = (struct archive_entry *)malloc(sizeof(*entry));
452 memset(entry, 0, sizeof(*entry));
457 * Functions for reading fields from an archive_entry.
461 archive_entry_atime(struct archive_entry *entry)
463 return (entry->ae_stat.aest_atime);
467 archive_entry_atime_nsec(struct archive_entry *entry)
469 return (entry->ae_stat.aest_atime_nsec);
473 archive_entry_atime_is_set(struct archive_entry *entry)
475 return (entry->ae_set & AE_SET_ATIME);
479 archive_entry_birthtime(struct archive_entry *entry)
481 return (entry->ae_stat.aest_birthtime);
485 archive_entry_birthtime_nsec(struct archive_entry *entry)
487 return (entry->ae_stat.aest_birthtime_nsec);
491 archive_entry_birthtime_is_set(struct archive_entry *entry)
493 return (entry->ae_set & AE_SET_BIRTHTIME);
497 archive_entry_ctime(struct archive_entry *entry)
499 return (entry->ae_stat.aest_ctime);
503 archive_entry_ctime_is_set(struct archive_entry *entry)
505 return (entry->ae_set & AE_SET_CTIME);
509 archive_entry_ctime_nsec(struct archive_entry *entry)
511 return (entry->ae_stat.aest_ctime_nsec);
515 archive_entry_dev(struct archive_entry *entry)
517 if (entry->ae_stat.aest_dev_is_broken_down)
518 return ae_makedev(entry->ae_stat.aest_devmajor,
519 entry->ae_stat.aest_devminor);
521 return (entry->ae_stat.aest_dev);
525 archive_entry_devmajor(struct archive_entry *entry)
527 if (entry->ae_stat.aest_dev_is_broken_down)
528 return (entry->ae_stat.aest_devmajor);
530 return major(entry->ae_stat.aest_dev);
534 archive_entry_devminor(struct archive_entry *entry)
536 if (entry->ae_stat.aest_dev_is_broken_down)
537 return (entry->ae_stat.aest_devminor);
539 return minor(entry->ae_stat.aest_dev);
543 archive_entry_filetype(struct archive_entry *entry)
545 return (AE_IFMT & entry->ae_stat.aest_mode);
549 archive_entry_fflags(struct archive_entry *entry,
550 unsigned long *set, unsigned long *clear)
552 *set = entry->ae_fflags_set;
553 *clear = entry->ae_fflags_clear;
557 * Note: if text was provided, this just returns that text. If you
558 * really need the text to be rebuilt in a canonical form, set the
559 * text, ask for the bitmaps, then set the bitmaps. (Setting the
560 * bitmaps clears any stored text.) This design is deliberate: if
561 * we're editing archives, we don't want to discard flags just because
562 * they aren't supported on the current system. The bitmap<->text
563 * conversions are platform-specific (see below).
566 archive_entry_fflags_text(struct archive_entry *entry)
571 f = aes_get_mbs(&entry->ae_fflags_text);
575 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
578 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
582 aes_copy_mbs(&entry->ae_fflags_text, p);
584 f = aes_get_mbs(&entry->ae_fflags_text);
589 archive_entry_gid(struct archive_entry *entry)
591 return (entry->ae_stat.aest_gid);
595 archive_entry_gname(struct archive_entry *entry)
597 return (aes_get_mbs(&entry->ae_gname));
601 archive_entry_gname_w(struct archive_entry *entry)
603 return (aes_get_wcs(&entry->ae_gname));
607 archive_entry_hardlink(struct archive_entry *entry)
609 if (entry->ae_set & AE_SET_HARDLINK)
610 return (aes_get_mbs(&entry->ae_hardlink));
615 archive_entry_hardlink_w(struct archive_entry *entry)
617 if (entry->ae_set & AE_SET_HARDLINK)
618 return (aes_get_wcs(&entry->ae_hardlink));
623 archive_entry_ino(struct archive_entry *entry)
625 return (entry->ae_stat.aest_ino);
629 archive_entry_ino64(struct archive_entry *entry)
631 return (entry->ae_stat.aest_ino);
635 archive_entry_mode(struct archive_entry *entry)
637 return (entry->ae_stat.aest_mode);
641 archive_entry_mtime(struct archive_entry *entry)
643 return (entry->ae_stat.aest_mtime);
647 archive_entry_mtime_nsec(struct archive_entry *entry)
649 return (entry->ae_stat.aest_mtime_nsec);
653 archive_entry_mtime_is_set(struct archive_entry *entry)
655 return (entry->ae_set & AE_SET_MTIME);
659 archive_entry_nlink(struct archive_entry *entry)
661 return (entry->ae_stat.aest_nlink);
665 archive_entry_pathname(struct archive_entry *entry)
667 return (aes_get_mbs(&entry->ae_pathname));
671 archive_entry_pathname_w(struct archive_entry *entry)
673 return (aes_get_wcs(&entry->ae_pathname));
677 archive_entry_rdev(struct archive_entry *entry)
679 if (entry->ae_stat.aest_rdev_is_broken_down)
680 return ae_makedev(entry->ae_stat.aest_rdevmajor,
681 entry->ae_stat.aest_rdevminor);
683 return (entry->ae_stat.aest_rdev);
687 archive_entry_rdevmajor(struct archive_entry *entry)
689 if (entry->ae_stat.aest_rdev_is_broken_down)
690 return (entry->ae_stat.aest_rdevmajor);
692 return major(entry->ae_stat.aest_rdev);
696 archive_entry_rdevminor(struct archive_entry *entry)
698 if (entry->ae_stat.aest_rdev_is_broken_down)
699 return (entry->ae_stat.aest_rdevminor);
701 return minor(entry->ae_stat.aest_rdev);
705 archive_entry_size(struct archive_entry *entry)
707 return (entry->ae_stat.aest_size);
711 archive_entry_size_is_set(struct archive_entry *entry)
713 return (entry->ae_set & AE_SET_SIZE);
717 archive_entry_sourcepath(struct archive_entry *entry)
719 return (aes_get_mbs(&entry->ae_sourcepath));
723 archive_entry_symlink(struct archive_entry *entry)
725 if (entry->ae_set & AE_SET_SYMLINK)
726 return (aes_get_mbs(&entry->ae_symlink));
731 archive_entry_symlink_w(struct archive_entry *entry)
733 if (entry->ae_set & AE_SET_SYMLINK)
734 return (aes_get_wcs(&entry->ae_symlink));
739 archive_entry_uid(struct archive_entry *entry)
741 return (entry->ae_stat.aest_uid);
745 archive_entry_uname(struct archive_entry *entry)
747 return (aes_get_mbs(&entry->ae_uname));
751 archive_entry_uname_w(struct archive_entry *entry)
753 return (aes_get_wcs(&entry->ae_uname));
757 * Functions to set archive_entry properties.
761 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
763 entry->stat_valid = 0;
764 entry->ae_stat.aest_mode &= ~AE_IFMT;
765 entry->ae_stat.aest_mode |= AE_IFMT & type;
769 archive_entry_set_fflags(struct archive_entry *entry,
770 unsigned long set, unsigned long clear)
772 aes_clean(&entry->ae_fflags_text);
773 entry->ae_fflags_set = set;
774 entry->ae_fflags_clear = clear;
778 archive_entry_copy_fflags_text(struct archive_entry *entry,
781 aes_copy_mbs(&entry->ae_fflags_text, flags);
782 return (ae_strtofflags(flags,
783 &entry->ae_fflags_set, &entry->ae_fflags_clear));
787 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
788 const wchar_t *flags)
790 aes_copy_wcs(&entry->ae_fflags_text, flags);
791 return (ae_wcstofflags(flags,
792 &entry->ae_fflags_set, &entry->ae_fflags_clear));
796 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
798 entry->stat_valid = 0;
799 entry->ae_stat.aest_gid = g;
803 archive_entry_set_gname(struct archive_entry *entry, const char *name)
805 aes_set_mbs(&entry->ae_gname, name);
809 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
811 aes_copy_mbs(&entry->ae_gname, name);
815 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
817 aes_copy_wcs(&entry->ae_gname, name);
821 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
823 return (aes_update_utf8(&entry->ae_gname, name));
827 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
829 entry->stat_valid = 0;
830 entry->ae_stat.aest_ino = ino;
834 archive_entry_set_ino64(struct archive_entry *entry, int64_t ino)
836 entry->stat_valid = 0;
837 entry->ae_stat.aest_ino = ino;
841 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
843 aes_set_mbs(&entry->ae_hardlink, target);
845 entry->ae_set |= AE_SET_HARDLINK;
847 entry->ae_set &= ~AE_SET_HARDLINK;
851 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
853 aes_copy_mbs(&entry->ae_hardlink, target);
855 entry->ae_set |= AE_SET_HARDLINK;
857 entry->ae_set &= ~AE_SET_HARDLINK;
861 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
863 aes_copy_wcs(&entry->ae_hardlink, target);
865 entry->ae_set |= AE_SET_HARDLINK;
867 entry->ae_set &= ~AE_SET_HARDLINK;
871 archive_entry_update_hardlink_utf8(struct archive_entry *entry, const char *target)
874 entry->ae_set |= AE_SET_HARDLINK;
876 entry->ae_set &= ~AE_SET_HARDLINK;
877 return (aes_update_utf8(&entry->ae_hardlink, target));
881 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
883 entry->stat_valid = 0;
884 entry->ae_set |= AE_SET_ATIME;
885 entry->ae_stat.aest_atime = t;
886 entry->ae_stat.aest_atime_nsec = ns;
890 archive_entry_unset_atime(struct archive_entry *entry)
892 archive_entry_set_atime(entry, 0, 0);
893 entry->ae_set &= ~AE_SET_ATIME;
897 archive_entry_set_birthtime(struct archive_entry *entry, time_t m, long ns)
899 entry->stat_valid = 0;
900 entry->ae_set |= AE_SET_BIRTHTIME;
901 entry->ae_stat.aest_birthtime = m;
902 entry->ae_stat.aest_birthtime_nsec = ns;
906 archive_entry_unset_birthtime(struct archive_entry *entry)
908 archive_entry_set_birthtime(entry, 0, 0);
909 entry->ae_set &= ~AE_SET_BIRTHTIME;
913 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
915 entry->stat_valid = 0;
916 entry->ae_set |= AE_SET_CTIME;
917 entry->ae_stat.aest_ctime = t;
918 entry->ae_stat.aest_ctime_nsec = ns;
922 archive_entry_unset_ctime(struct archive_entry *entry)
924 archive_entry_set_ctime(entry, 0, 0);
925 entry->ae_set &= ~AE_SET_CTIME;
929 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
931 entry->stat_valid = 0;
932 entry->ae_stat.aest_dev_is_broken_down = 0;
933 entry->ae_stat.aest_dev = d;
937 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
939 entry->stat_valid = 0;
940 entry->ae_stat.aest_dev_is_broken_down = 1;
941 entry->ae_stat.aest_devmajor = m;
945 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
947 entry->stat_valid = 0;
948 entry->ae_stat.aest_dev_is_broken_down = 1;
949 entry->ae_stat.aest_devminor = m;
952 /* Set symlink if symlink is already set, else set hardlink. */
954 archive_entry_set_link(struct archive_entry *entry, const char *target)
956 if (entry->ae_set & AE_SET_SYMLINK)
957 aes_set_mbs(&entry->ae_symlink, target);
959 aes_set_mbs(&entry->ae_hardlink, target);
962 /* Set symlink if symlink is already set, else set hardlink. */
964 archive_entry_copy_link(struct archive_entry *entry, const char *target)
966 if (entry->ae_set & AE_SET_SYMLINK)
967 aes_copy_mbs(&entry->ae_symlink, target);
969 aes_copy_mbs(&entry->ae_hardlink, target);
972 /* Set symlink if symlink is already set, else set hardlink. */
974 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
976 if (entry->ae_set & AE_SET_SYMLINK)
977 aes_copy_wcs(&entry->ae_symlink, target);
979 aes_copy_wcs(&entry->ae_hardlink, target);
983 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
985 if (entry->ae_set & AE_SET_SYMLINK)
986 return (aes_update_utf8(&entry->ae_symlink, target));
988 return (aes_update_utf8(&entry->ae_hardlink, target));
992 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
994 entry->stat_valid = 0;
995 entry->ae_stat.aest_mode = m;
999 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
1001 entry->stat_valid = 0;
1002 entry->ae_set |= AE_SET_MTIME;
1003 entry->ae_stat.aest_mtime = m;
1004 entry->ae_stat.aest_mtime_nsec = ns;
1008 archive_entry_unset_mtime(struct archive_entry *entry)
1010 archive_entry_set_mtime(entry, 0, 0);
1011 entry->ae_set &= ~AE_SET_MTIME;
1015 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
1017 entry->stat_valid = 0;
1018 entry->ae_stat.aest_nlink = nlink;
1022 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
1024 aes_set_mbs(&entry->ae_pathname, name);
1028 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
1030 aes_copy_mbs(&entry->ae_pathname, name);
1034 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
1036 aes_copy_wcs(&entry->ae_pathname, name);
1040 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
1042 return (aes_update_utf8(&entry->ae_pathname, name));
1046 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
1048 entry->stat_valid = 0;
1049 entry->ae_stat.aest_mode &= AE_IFMT;
1050 entry->ae_stat.aest_mode |= ~AE_IFMT & p;
1054 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
1056 entry->stat_valid = 0;
1057 entry->ae_stat.aest_rdev = m;
1058 entry->ae_stat.aest_rdev_is_broken_down = 0;
1062 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
1064 entry->stat_valid = 0;
1065 entry->ae_stat.aest_rdev_is_broken_down = 1;
1066 entry->ae_stat.aest_rdevmajor = m;
1070 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
1072 entry->stat_valid = 0;
1073 entry->ae_stat.aest_rdev_is_broken_down = 1;
1074 entry->ae_stat.aest_rdevminor = m;
1078 archive_entry_set_size(struct archive_entry *entry, int64_t s)
1080 entry->stat_valid = 0;
1081 entry->ae_stat.aest_size = s;
1082 entry->ae_set |= AE_SET_SIZE;
1086 archive_entry_unset_size(struct archive_entry *entry)
1088 archive_entry_set_size(entry, 0);
1089 entry->ae_set &= ~AE_SET_SIZE;
1093 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1095 aes_set_mbs(&entry->ae_sourcepath, path);
1099 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1101 aes_set_mbs(&entry->ae_symlink, linkname);
1102 if (linkname != NULL)
1103 entry->ae_set |= AE_SET_SYMLINK;
1105 entry->ae_set &= ~AE_SET_SYMLINK;
1109 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1111 aes_copy_mbs(&entry->ae_symlink, linkname);
1112 if (linkname != NULL)
1113 entry->ae_set |= AE_SET_SYMLINK;
1115 entry->ae_set &= ~AE_SET_SYMLINK;
1119 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1121 aes_copy_wcs(&entry->ae_symlink, linkname);
1122 if (linkname != NULL)
1123 entry->ae_set |= AE_SET_SYMLINK;
1125 entry->ae_set &= ~AE_SET_SYMLINK;
1129 archive_entry_update_symlink_utf8(struct archive_entry *entry, const char *linkname)
1131 if (linkname != NULL)
1132 entry->ae_set |= AE_SET_SYMLINK;
1134 entry->ae_set &= ~AE_SET_SYMLINK;
1135 return (aes_update_utf8(&entry->ae_symlink, linkname));
1139 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
1141 entry->stat_valid = 0;
1142 entry->ae_stat.aest_uid = u;
1146 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1148 aes_set_mbs(&entry->ae_uname, name);
1152 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1154 aes_copy_mbs(&entry->ae_uname, name);
1158 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1160 aes_copy_wcs(&entry->ae_uname, name);
1164 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1166 return (aes_update_utf8(&entry->ae_uname, name));
1170 * ACL management. The following would, of course, be a lot simpler
1171 * if: 1) the last draft of POSIX.1e were a really thorough and
1172 * complete standard that addressed the needs of ACL archiving and 2)
1173 * everyone followed it faithfully. Alas, neither is true, so the
1174 * following is a lot more complex than might seem necessary to the
1179 archive_entry_acl_clear(struct archive_entry *entry)
1183 while (entry->acl_head != NULL) {
1184 ap = entry->acl_head->next;
1185 aes_clean(&entry->acl_head->name);
1186 free(entry->acl_head);
1187 entry->acl_head = ap;
1189 if (entry->acl_text_w != NULL) {
1190 free(entry->acl_text_w);
1191 entry->acl_text_w = NULL;
1193 entry->acl_p = NULL;
1194 entry->acl_state = 0; /* Not counting. */
1198 * Add a single ACL entry to the internal list of ACL data.
1201 archive_entry_acl_add_entry(struct archive_entry *entry,
1202 int type, int permset, int tag, int id, const char *name)
1206 if (acl_special(entry, type, permset, tag) == 0)
1208 ap = acl_new_entry(entry, type, permset, tag, id);
1213 if (name != NULL && *name != '\0')
1214 aes_copy_mbs(&ap->name, name);
1216 aes_clean(&ap->name);
1220 * As above, but with a wide-character name.
1223 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1224 int type, int permset, int tag, int id, const wchar_t *name)
1226 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
1230 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
1231 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
1235 if (acl_special(entry, type, permset, tag) == 0)
1237 ap = acl_new_entry(entry, type, permset, tag, id);
1242 if (name != NULL && *name != L'\0' && len > 0)
1243 aes_copy_wcs_len(&ap->name, name, len);
1245 aes_clean(&ap->name);
1249 * If this ACL entry is part of the standard POSIX permissions set,
1250 * store the permissions in the stat structure and return zero.
1253 acl_special(struct archive_entry *entry, int type, int permset, int tag)
1255 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
1257 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1258 entry->ae_stat.aest_mode &= ~0700;
1259 entry->ae_stat.aest_mode |= (permset & 7) << 6;
1261 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1262 entry->ae_stat.aest_mode &= ~0070;
1263 entry->ae_stat.aest_mode |= (permset & 7) << 3;
1265 case ARCHIVE_ENTRY_ACL_OTHER:
1266 entry->ae_stat.aest_mode &= ~0007;
1267 entry->ae_stat.aest_mode |= permset & 7;
1275 * Allocate and populate a new ACL entry with everything but the
1278 static struct ae_acl *
1279 acl_new_entry(struct archive_entry *entry,
1280 int type, int permset, int tag, int id)
1282 struct ae_acl *ap, *aq;
1284 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
1285 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
1287 if (entry->acl_text_w != NULL) {
1288 free(entry->acl_text_w);
1289 entry->acl_text_w = NULL;
1292 /* XXX TODO: More sanity-checks on the arguments XXX */
1294 /* If there's a matching entry already in the list, overwrite it. */
1295 ap = entry->acl_head;
1297 while (ap != NULL) {
1298 if (ap->type == type && ap->tag == tag && ap->id == id) {
1299 ap->permset = permset;
1306 /* Add a new entry to the end of the list. */
1307 ap = (struct ae_acl *)malloc(sizeof(*ap));
1310 memset(ap, 0, sizeof(*ap));
1312 entry->acl_head = ap;
1318 ap->permset = permset;
1323 * Return a count of entries matching "want_type".
1326 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1332 ap = entry->acl_head;
1333 while (ap != NULL) {
1334 if ((ap->type & want_type) != 0)
1339 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1345 * Prepare for reading entries from the ACL data. Returns a count
1346 * of entries matching "want_type", or zero if there are no
1347 * non-extended ACL entries of that type.
1350 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1354 count = archive_entry_acl_count(entry, want_type);
1357 * If the only entries are the three standard ones,
1358 * then don't return any ACL data. (In this case,
1359 * client can just use chmod(2) to set permissions.)
1361 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1367 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1369 entry->acl_state = 0;
1370 entry->acl_p = entry->acl_head;
1375 * Return the next ACL entry in the list. Fake entries for the
1376 * standard permissions and include them in the returned list.
1380 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1381 int *permset, int *tag, int *id, const char **name)
1387 * The acl_state is either zero (no entries available), -1
1388 * (reading from list), or an entry type (retrieve that type
1389 * from ae_stat.aest_mode).
1391 if (entry->acl_state == 0)
1392 return (ARCHIVE_WARN);
1394 /* The first three access entries are special. */
1395 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1396 switch (entry->acl_state) {
1397 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1398 *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1399 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1400 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1401 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1402 return (ARCHIVE_OK);
1403 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1404 *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1405 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1406 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1407 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1408 return (ARCHIVE_OK);
1409 case ARCHIVE_ENTRY_ACL_OTHER:
1410 *permset = entry->ae_stat.aest_mode & 7;
1411 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1412 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1413 entry->acl_state = -1;
1414 entry->acl_p = entry->acl_head;
1415 return (ARCHIVE_OK);
1421 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1422 entry->acl_p = entry->acl_p->next;
1423 if (entry->acl_p == NULL) {
1424 entry->acl_state = 0;
1430 return (ARCHIVE_EOF); /* End of ACL entries. */
1432 *type = entry->acl_p->type;
1433 *permset = entry->acl_p->permset;
1434 *tag = entry->acl_p->tag;
1435 *id = entry->acl_p->id;
1436 *name = aes_get_mbs(&entry->acl_p->name);
1437 entry->acl_p = entry->acl_p->next;
1438 return (ARCHIVE_OK);
1442 * Generate a text version of the ACL. The flags parameter controls
1443 * the style of the generated ACL.
1446 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1450 const wchar_t *wname;
1451 const wchar_t *prefix;
1457 if (entry->acl_text_w != NULL) {
1458 free (entry->acl_text_w);
1459 entry->acl_text_w = NULL;
1465 ap = entry->acl_head;
1466 while (ap != NULL) {
1467 if ((ap->type & flags) != 0) {
1469 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1470 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1471 length += 8; /* "default:" */
1472 length += 5; /* tag name */
1473 length += 1; /* colon */
1474 wname = aes_get_wcs(&ap->name);
1476 length += wcslen(wname);
1478 length += sizeof(uid_t) * 3 + 1;
1479 length ++; /* colon */
1480 length += 3; /* rwx */
1481 length += 1; /* colon */
1482 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1483 length ++; /* newline */
1488 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1489 length += 10; /* "user::rwx\n" */
1490 length += 11; /* "group::rwx\n" */
1491 length += 11; /* "other::rwx\n" */
1497 /* Now, allocate the string and actually populate it. */
1498 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1500 __archive_errx(1, "No memory to generate the text version of the ACL");
1502 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1503 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1504 entry->ae_stat.aest_mode & 0700, -1);
1506 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1507 entry->ae_stat.aest_mode & 0070, -1);
1509 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1510 entry->ae_stat.aest_mode & 0007, -1);
1513 ap = entry->acl_head;
1514 while (ap != NULL) {
1515 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1516 wname = aes_get_wcs(&ap->name);
1518 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1522 append_entry_w(&wp, NULL, ap->tag, wname,
1531 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1532 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1533 prefix = L"default:";
1536 ap = entry->acl_head;
1538 while (ap != NULL) {
1539 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1540 wname = aes_get_wcs(&ap->name);
1543 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1547 append_entry_w(&wp, prefix, ap->tag,
1548 wname, ap->permset, id);
1555 return (entry->acl_text_w);
1559 append_id_w(wchar_t **wp, int id)
1564 append_id_w(wp, id / 10);
1565 *(*wp)++ = L"0123456789"[id % 10];
1569 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1570 const wchar_t *wname, int perm, int id)
1572 if (prefix != NULL) {
1573 wcscpy(*wp, prefix);
1577 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1581 case ARCHIVE_ENTRY_ACL_USER:
1582 wcscpy(*wp, L"user");
1584 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1588 case ARCHIVE_ENTRY_ACL_GROUP:
1589 wcscpy(*wp, L"group");
1591 case ARCHIVE_ENTRY_ACL_MASK:
1592 wcscpy(*wp, L"mask");
1596 case ARCHIVE_ENTRY_ACL_OTHER:
1597 wcscpy(*wp, L"other");
1604 if (wname != NULL) {
1607 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1608 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1609 append_id_w(wp, id);
1613 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1614 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1615 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1618 append_id_w(wp, id);
1624 * Parse a textual ACL. This automatically recognizes and supports
1625 * extensions described above. The 'type' argument is used to
1626 * indicate the type that should be used for any entries not
1627 * explicitly marked as "default:".
1630 __archive_entry_acl_parse_w(struct archive_entry *entry,
1631 const wchar_t *text, int default_type)
1634 const wchar_t *start;
1639 int type, tag, permset, id;
1642 while (text != NULL && *text != L'\0') {
1644 * Parse the fields out of the next entry,
1645 * advance 'text' to start of next entry.
1649 const wchar_t *start, *end;
1650 next_field_w(&text, &start, &end, &sep);
1652 field[fields].start = start;
1653 field[fields].end = end;
1656 } while (sep == L':');
1658 /* Set remaining fields to blank. */
1659 for (n = fields; n < 4; ++n)
1660 field[n].start = field[n].end = NULL;
1662 /* Check for a numeric ID in field 1 or 3. */
1664 isint_w(field[1].start, field[1].end, &id);
1665 /* Field 3 is optional. */
1666 if (id == -1 && fields > 3)
1667 isint_w(field[3].start, field[3].end, &id);
1670 * Solaris extension: "defaultuser::rwx" is the
1671 * default ACL corresponding to "user::rwx", etc.
1673 if (field[0].end - field[0].start > 7
1674 && wmemcmp(field[0].start, L"default", 7) == 0) {
1675 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1676 field[0].start += 7;
1678 type = default_type;
1680 name.start = name.end = NULL;
1681 if (prefix_w(field[0].start, field[0].end, L"user")) {
1682 if (!ismode_w(field[2].start, field[2].end, &permset))
1683 return (ARCHIVE_WARN);
1684 if (id != -1 || field[1].start < field[1].end) {
1685 tag = ARCHIVE_ENTRY_ACL_USER;
1688 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1689 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1690 if (!ismode_w(field[2].start, field[2].end, &permset))
1691 return (ARCHIVE_WARN);
1692 if (id != -1 || field[1].start < field[1].end) {
1693 tag = ARCHIVE_ENTRY_ACL_GROUP;
1696 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1697 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1699 && field[1].start < field[1].end
1700 && ismode_w(field[1].start, field[1].end, &permset)) {
1701 /* This is Solaris-style "other:rwx" */
1702 } else if (fields == 3
1703 && field[1].start == field[1].end
1704 && field[2].start < field[2].end
1705 && ismode_w(field[2].start, field[2].end, &permset)) {
1706 /* This is FreeBSD-style "other::rwx" */
1708 return (ARCHIVE_WARN);
1709 tag = ARCHIVE_ENTRY_ACL_OTHER;
1710 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1712 && field[1].start < field[1].end
1713 && ismode_w(field[1].start, field[1].end, &permset)) {
1714 /* This is Solaris-style "mask:rwx" */
1715 } else if (fields == 3
1716 && field[1].start == field[1].end
1717 && field[2].start < field[2].end
1718 && ismode_w(field[2].start, field[2].end, &permset)) {
1719 /* This is FreeBSD-style "mask::rwx" */
1721 return (ARCHIVE_WARN);
1722 tag = ARCHIVE_ENTRY_ACL_MASK;
1724 return (ARCHIVE_WARN);
1726 /* Add entry to the internal list. */
1727 archive_entry_acl_add_entry_w_len(entry, type, permset,
1728 tag, id, name.start, name.end - name.start);
1730 return (ARCHIVE_OK);
1734 * Parse a string to a positive decimal integer. Returns true if
1735 * the string is non-empty and consists only of decimal digits,
1739 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1744 while (start < end) {
1745 if (*start < '0' || *start > '9')
1747 if (n > (INT_MAX / 10))
1760 * Parse a string as a mode field. Returns true if
1761 * the string is non-empty and consists only of mode characters,
1765 ismode_w(const wchar_t *start, const wchar_t *end, int *permset)
1776 *permset |= ARCHIVE_ENTRY_ACL_READ;
1779 *permset |= ARCHIVE_ENTRY_ACL_WRITE;
1782 *permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1794 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1795 * to point to just after the separator. *start points to the first
1796 * character of the matched text and *end just after the last
1797 * character of the matched identifier. In particular *end - *start
1798 * is the length of the field body, not including leading or trailing
1802 next_field_w(const wchar_t **wp, const wchar_t **start,
1803 const wchar_t **end, wchar_t *sep)
1805 /* Skip leading whitespace to find start of field. */
1806 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1811 /* Scan for the separator. */
1812 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1818 /* Trim trailing whitespace to locate end of field. */
1820 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1825 /* Adjust scanner location. */
1831 * Return true if the characters [start...end) are a prefix of 'test'.
1832 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1835 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1840 if (*start++ != *test++)
1843 while (start < end && *start++ == *test++)
1854 * Following code is modified from UC Berkeley sources, and
1855 * is subject to the following copyright notice.
1859 * Copyright (c) 1993
1860 * The Regents of the University of California. All rights reserved.
1862 * Redistribution and use in source and binary forms, with or without
1863 * modification, are permitted provided that the following conditions
1865 * 1. Redistributions of source code must retain the above copyright
1866 * notice, this list of conditions and the following disclaimer.
1867 * 2. Redistributions in binary form must reproduce the above copyright
1868 * notice, this list of conditions and the following disclaimer in the
1869 * documentation and/or other materials provided with the distribution.
1870 * 4. Neither the name of the University nor the names of its contributors
1871 * may be used to endorse or promote products derived from this software
1872 * without specific prior written permission.
1874 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1875 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1876 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1877 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1878 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1879 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1880 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1881 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1882 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1883 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1887 static struct flag {
1889 const wchar_t *wname;
1891 unsigned long clear;
1893 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1895 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1896 { "nosappend", L"nosappend", SF_APPEND, 0 },
1898 #ifdef EXT2_APPEND_FL /* 'a' */
1899 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1900 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1903 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1904 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1907 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1908 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1909 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1911 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1912 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1913 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1914 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1917 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1918 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1921 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1924 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1925 { "nouappend", L"nouappend", UF_APPEND, 0 },
1928 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1929 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1930 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1933 { "nodump", L"nodump", 0, UF_NODUMP},
1935 #ifdef EXT2_NODUMP_FL /* 'd' */
1936 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1939 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1942 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1943 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1946 { "nouunlink", L"nouunlink", EXT2_UNRM_FL, 0},
1949 #ifdef EXT2_BTREE_FL
1950 { "nobtree", L"nobtree", EXT2_BTREE_FL, 0 },
1953 #ifdef EXT2_ECOMPR_FL
1954 { "nocomperr", L"nocomperr", EXT2_ECOMPR_FL, 0 },
1957 #ifdef EXT2_COMPR_FL /* 'c' */
1958 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1961 #ifdef EXT2_NOATIME_FL /* 'A' */
1962 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1965 #ifdef EXT2_DIRTY_FL
1966 { "nocompdirty",L"nocompdirty", EXT2_DIRTY_FL, 0},
1969 #ifdef EXT2_COMPRBLK_FL
1970 #ifdef EXT2_NOCOMPR_FL
1971 { "nocomprblk", L"nocomprblk", EXT2_COMPRBLK_FL, EXT2_NOCOMPR_FL},
1973 { "nocomprblk", L"nocomprblk", EXT2_COMPRBLK_FL, 0},
1976 #ifdef EXT2_DIRSYNC_FL
1977 { "nodirsync", L"nodirsync", EXT2_DIRSYNC_FL, 0},
1979 #ifdef EXT2_INDEX_FL
1980 { "nohashidx", L"nohashidx", EXT2_INDEX_FL, 0},
1982 #ifdef EXT2_IMAGIC_FL
1983 { "noimagic", L"noimagic", EXT2_IMAGIC_FL, 0},
1985 #ifdef EXT3_JOURNAL_DATA_FL
1986 { "nojournal", L"nojournal", EXT3_JOURNAL_DATA_FL, 0},
1988 #ifdef EXT2_SECRM_FL
1989 { "nosecuredeletion",L"nosecuredeletion",EXT2_SECRM_FL, 0},
1992 { "nosync", L"nosync", EXT2_SYNC_FL, 0},
1994 #ifdef EXT2_NOTAIL_FL
1995 { "notail", L"notail", 0, EXT2_NOTAIL_FL},
1997 #ifdef EXT2_TOPDIR_FL
1998 { "notopdir", L"notopdir", EXT2_TOPDIR_FL, 0},
2000 #ifdef EXT2_RESERVED_FL
2001 { "noreserved", L"noreserved", EXT2_RESERVED_FL, 0},
2004 { NULL, NULL, 0, 0 }
2009 * Convert file flags to a comma-separated string. If no flags
2010 * are set, return the empty string.
2013 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
2021 bits = bitset | bitclear;
2023 for (flag = flags; flag->name != NULL; flag++)
2024 if (bits & (flag->set | flag->clear)) {
2025 length += strlen(flag->name) + 1;
2026 bits &= ~(flag->set | flag->clear);
2031 string = (char *)malloc(length);
2036 for (flag = flags; flag->name != NULL; flag++) {
2037 if (bitset & flag->set || bitclear & flag->clear) {
2038 sp = flag->name + 2;
2039 } else if (bitset & flag->clear || bitclear & flag->set) {
2043 bitset &= ~(flag->set | flag->clear);
2044 bitclear &= ~(flag->set | flag->clear);
2047 while ((*dp++ = *sp++) != '\0')
2058 * Take string of arguments and return file flags. This
2059 * version works a little differently than strtofflags(3).
2060 * In particular, it always tests every token, skipping any
2061 * unrecognized tokens. It returns a pointer to the first
2062 * unrecognized token, or NULL if every token was recognized.
2063 * This version is also const-correct and does not modify the
2067 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
2069 const char *start, *end;
2071 unsigned long set, clear;
2077 /* Find start of first token. */
2078 while (*start == '\t' || *start == ' ' || *start == ',')
2080 while (*start != '\0') {
2081 /* Locate end of token. */
2083 while (*end != '\0' && *end != '\t' &&
2084 *end != ' ' && *end != ',')
2086 for (flag = flags; flag->name != NULL; flag++) {
2087 if (memcmp(start, flag->name, end - start) == 0) {
2088 /* Matched "noXXXX", so reverse the sense. */
2092 } else if (memcmp(start, flag->name + 2, end - start)
2094 /* Matched "XXXX", so don't reverse. */
2096 clear |= flag->clear;
2100 /* Ignore unknown flag names. */
2101 if (flag->name == NULL && failed == NULL)
2104 /* Find start of next token. */
2106 while (*start == '\t' || *start == ' ' || *start == ',')
2116 /* Return location of first failure. */
2122 * Take string of arguments and return file flags. This
2123 * version works a little differently than strtofflags(3).
2124 * In particular, it always tests every token, skipping any
2125 * unrecognized tokens. It returns a pointer to the first
2126 * unrecognized token, or NULL if every token was recognized.
2127 * This version is also const-correct and does not modify the
2130 static const wchar_t *
2131 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
2133 const wchar_t *start, *end;
2135 unsigned long set, clear;
2136 const wchar_t *failed;
2141 /* Find start of first token. */
2142 while (*start == L'\t' || *start == L' ' || *start == L',')
2144 while (*start != L'\0') {
2145 /* Locate end of token. */
2147 while (*end != L'\0' && *end != L'\t' &&
2148 *end != L' ' && *end != L',')
2150 for (flag = flags; flag->wname != NULL; flag++) {
2151 if (wmemcmp(start, flag->wname, end - start) == 0) {
2152 /* Matched "noXXXX", so reverse the sense. */
2156 } else if (wmemcmp(start, flag->wname + 2, end - start)
2158 /* Matched "XXXX", so don't reverse. */
2160 clear |= flag->clear;
2164 /* Ignore unknown flag names. */
2165 if (flag->wname == NULL && failed == NULL)
2168 /* Find start of next token. */
2170 while (*start == L'\t' || *start == L' ' || *start == L',')
2180 /* Return location of first failure. */
2188 main(int argc, char **argv)
2190 struct archive_entry *entry = archive_entry_new();
2191 unsigned long set, clear;
2192 const wchar_t *remainder;
2194 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
2195 archive_entry_fflags(entry, &set, &clear);
2197 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
2199 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));