2 * Copyright (c) 2003-2004 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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry.c,v 1.23 2004/08/08 07:39:19 kientzle Exp $");
31 #include <sys/types.h>
32 #ifdef HAVE_EXT2FS_EXT2_FS_H
33 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
43 #include "archive_entry.h"
46 #define max(a, b) ((a)>(b)?(a):(b))
49 * Handle wide character (i.e., Unicode) and non-wide character
50 * strings transparently.
57 const wchar_t *aes_wcs;
58 wchar_t *aes_wcs_alloc;
63 int type; /* E.g., access or default */
64 int tag; /* E.g., user/group/other/mask */
65 int permset; /* r/w/x bits */
66 int id; /* uid/gid for user/group */
67 struct aes name; /* uname/gname */
70 static void aes_clean(struct aes *);
71 static void aes_copy(struct aes *dest, struct aes *src);
72 static const char * aes_get_mbs(struct aes *);
73 static const wchar_t * aes_get_wcs(struct aes *);
74 static void aes_set_mbs(struct aes *, const char *mbs);
75 static void aes_copy_mbs(struct aes *, const char *mbs);
76 /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
77 static void aes_copy_wcs(struct aes *, const wchar_t *wcs);
79 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
80 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
81 unsigned long *setp, unsigned long *clrp);
82 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
83 const wchar_t *wname, int perm, int id);
84 static void append_id_w(wchar_t **wp, int id);
86 static int acl_special(struct archive_entry *entry,
87 int type, int permset, int tag);
88 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
89 int type, int permset, int tag, int id);
90 static void next_field_w(const wchar_t **wp, const wchar_t **start,
91 const wchar_t **end, wchar_t *sep);
92 static int prefix_w(const wchar_t *start, const wchar_t *end,
97 * Description of an archive entry.
99 * Basically, this is a "struct stat" with a few text fields added in.
101 * TODO: Add "comment", "charset", and possibly other entries
102 * that are supported by "pax interchange" format. However, GNU, ustar,
103 * cpio, and other variants don't support these features, so they're not an
104 * excruciatingly high priority right now.
106 * TODO: "pax interchange" format allows essentially arbitrary
107 * key/value attributes to be attached to any entry. Supporting
108 * such extensions may make this library useful for special
109 * applications (e.g., a package manager could attach special
110 * package-management attributes to each entry). There are tricky
111 * API issues involved, so this is not going to happen until
112 * there's a real demand for it.
114 * TODO: Design a good API for handling sparse files.
116 struct archive_entry {
118 * Note that ae_stat.st_mode & S_IFMT can be 0!
120 * This occurs when the actual file type of the object is not
121 * in the archive. For example, 'tar' archives store
122 * hardlinks without marking the type of the underlying
128 * Use aes here so that we get transparent mbs<->wcs conversions.
130 struct aes ae_fflags_text; /* Text fflags per fflagstostr(3) */
131 unsigned long ae_fflags_set; /* Bitmap fflags */
132 unsigned long ae_fflags_clear;
133 struct aes ae_gname; /* Name of owning group */
134 struct aes ae_hardlink; /* Name of target for hardlink */
135 struct aes ae_pathname; /* Name of entry */
136 struct aes ae_symlink; /* symlink contents */
137 struct aes ae_uname; /* Name of owner */
139 struct ae_acl *acl_head;
140 struct ae_acl *acl_p;
141 int acl_state; /* See acl_next for details. */
146 aes_clean(struct aes *aes)
148 if (aes->aes_mbs_alloc) {
149 free(aes->aes_mbs_alloc);
150 aes->aes_mbs_alloc = NULL;
152 if (aes->aes_wcs_alloc) {
153 free(aes->aes_wcs_alloc);
154 aes->aes_wcs_alloc = NULL;
156 memset(aes, 0, sizeof(*aes));
160 aes_copy(struct aes *dest, struct aes *src)
163 if (src->aes_mbs != NULL) {
164 dest->aes_mbs_alloc = strdup(src->aes_mbs);
165 dest->aes_mbs = dest->aes_mbs_alloc;
168 if (src->aes_wcs != NULL) {
169 dest->aes_wcs_alloc = malloc((wcslen(src->aes_wcs) + 1)
171 dest->aes_wcs = dest->aes_wcs_alloc;
172 wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
177 aes_get_mbs(struct aes *aes)
179 if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
181 * XXX Need to estimate the number of byte in the
182 * multi-byte form. Assume that, on average, wcs
183 * chars encode to no more than 3 bytes. There must
184 * be a better way... XXX
186 int mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
187 aes->aes_mbs_alloc = malloc(mbs_length);
188 aes->aes_mbs = aes->aes_mbs_alloc;
189 wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
190 aes->aes_mbs_alloc[mbs_length - 1] = 0;
192 return (aes->aes_mbs);
195 static const wchar_t *
196 aes_get_wcs(struct aes *aes)
198 if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
200 * No single byte will be more than one wide character,
201 * so this length estimate will always be big enough.
203 int wcs_length = strlen(aes->aes_mbs);
205 = malloc((wcs_length + 1) * sizeof(wchar_t));
206 aes->aes_wcs = aes->aes_wcs_alloc;
207 mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
208 aes->aes_wcs_alloc[wcs_length] = 0;
210 return (aes->aes_wcs);
214 aes_set_mbs(struct aes *aes, const char *mbs)
216 if (aes->aes_mbs_alloc) {
217 free(aes->aes_mbs_alloc);
218 aes->aes_mbs_alloc = NULL;
220 if (aes->aes_wcs_alloc) {
221 free(aes->aes_wcs_alloc);
222 aes->aes_wcs_alloc = NULL;
229 aes_copy_mbs(struct aes *aes, const char *mbs)
231 if (aes->aes_mbs_alloc) {
232 free(aes->aes_mbs_alloc);
233 aes->aes_mbs_alloc = NULL;
235 if (aes->aes_wcs_alloc) {
236 free(aes->aes_wcs_alloc);
237 aes->aes_wcs_alloc = NULL;
239 aes->aes_mbs_alloc = malloc((strlen(mbs) + 1) * sizeof(char));
240 strcpy(aes->aes_mbs_alloc, mbs);
241 aes->aes_mbs = aes->aes_mbs_alloc;
247 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
249 if (aes->aes_mbs_alloc) {
250 free(aes->aes_mbs_alloc);
251 aes->aes_mbs_alloc = NULL;
253 if (aes->aes_wcs_alloc) {
254 free(aes->aes_wcs_alloc);
255 aes->aes_wcs_alloc = NULL;
263 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
265 if (aes->aes_mbs_alloc) {
266 free(aes->aes_mbs_alloc);
267 aes->aes_mbs_alloc = NULL;
269 if (aes->aes_wcs_alloc) {
270 free(aes->aes_wcs_alloc);
271 aes->aes_wcs_alloc = NULL;
274 aes->aes_wcs_alloc = malloc((wcslen(wcs) + 1) * sizeof(wchar_t));
275 wcscpy(aes->aes_wcs_alloc, wcs);
276 aes->aes_wcs = aes->aes_wcs_alloc;
279 struct archive_entry *
280 archive_entry_clear(struct archive_entry *entry)
282 aes_clean(&entry->ae_fflags_text);
283 aes_clean(&entry->ae_gname);
284 aes_clean(&entry->ae_hardlink);
285 aes_clean(&entry->ae_pathname);
286 aes_clean(&entry->ae_symlink);
287 aes_clean(&entry->ae_uname);
288 archive_entry_acl_clear(entry);
289 memset(entry, 0, sizeof(*entry));
293 struct archive_entry *
294 archive_entry_clone(struct archive_entry *entry)
296 struct archive_entry *entry2;
298 /* Allocate new structure and copy over all of the fields. */
299 entry2 = malloc(sizeof(*entry2));
302 memset(entry2, 0, sizeof(*entry2));
303 entry2->ae_stat = entry->ae_stat;
304 entry2->ae_fflags_set = entry->ae_fflags_set;
305 entry2->ae_fflags_clear = entry->ae_fflags_clear;
307 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
308 aes_copy(&entry2->ae_gname, &entry->ae_gname);
309 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
310 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
311 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
312 aes_copy(&entry2->ae_uname, &entry->ae_uname);
314 /* XXX TODO: Copy ACL data over as well. XXX */
319 archive_entry_free(struct archive_entry *entry)
321 archive_entry_clear(entry);
325 struct archive_entry *
326 archive_entry_new(void)
328 struct archive_entry *entry;
330 entry = malloc(sizeof(*entry));
333 memset(entry, 0, sizeof(*entry));
338 * Functions for reading fields from an archive_entry.
342 archive_entry_atime(struct archive_entry *entry)
344 return (entry->ae_stat.st_atime);
348 archive_entry_atime_nsec(struct archive_entry *entry)
350 (void)entry; /* entry can be unused here. */
351 return (ARCHIVE_STAT_ATIME_NANOS(&entry->ae_stat));
355 archive_entry_dev(struct archive_entry *entry)
357 return (entry->ae_stat.st_dev);
361 archive_entry_fflags(struct archive_entry *entry,
362 unsigned long *set, unsigned long *clear)
364 *set = entry->ae_fflags_set;
365 *clear = entry->ae_fflags_clear;
369 * Note: if text was provided, this just returns that text. If you
370 * really need the text to be rebuilt in a canonical form, set the
371 * text, ask for the bitmaps, then set the bitmaps. (Setting the
372 * bitmaps clears any stored text.) This design is deliberate: if
373 * we're editing archives, we don't want to discard flags just because
374 * they aren't supported on the current system. The bitmap<->text
375 * conversions are platform-specific (see below).
378 archive_entry_fflags_text(struct archive_entry *entry)
383 f = aes_get_mbs(&entry->ae_fflags_text);
387 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
390 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
394 aes_copy_mbs(&entry->ae_fflags_text, p);
396 f = aes_get_mbs(&entry->ae_fflags_text);
401 archive_entry_gid(struct archive_entry *entry)
403 return (entry->ae_stat.st_gid);
407 archive_entry_gname(struct archive_entry *entry)
409 return (aes_get_mbs(&entry->ae_gname));
413 archive_entry_hardlink(struct archive_entry *entry)
415 return (aes_get_mbs(&entry->ae_hardlink));
419 archive_entry_ino(struct archive_entry *entry)
421 return (entry->ae_stat.st_ino);
425 archive_entry_mode(struct archive_entry *entry)
427 return (entry->ae_stat.st_mode);
431 archive_entry_mtime(struct archive_entry *entry)
433 return (entry->ae_stat.st_mtime);
437 archive_entry_mtime_nsec(struct archive_entry *entry)
439 (void)entry; /* entry can be unused here. */
440 return (ARCHIVE_STAT_MTIME_NANOS(&entry->ae_stat));
444 archive_entry_pathname(struct archive_entry *entry)
446 return (aes_get_mbs(&entry->ae_pathname));
450 archive_entry_pathname_w(struct archive_entry *entry)
452 return (aes_get_wcs(&entry->ae_pathname));
456 archive_entry_rdev(struct archive_entry *entry)
458 return (entry->ae_stat.st_rdev);
462 archive_entry_rdevmajor(struct archive_entry *entry)
464 return (major(entry->ae_stat.st_rdev));
468 archive_entry_rdevminor(struct archive_entry *entry)
470 return (minor(entry->ae_stat.st_rdev));
474 archive_entry_size(struct archive_entry *entry)
476 return (entry->ae_stat.st_size);
480 archive_entry_stat(struct archive_entry *entry)
482 return (&entry->ae_stat);
486 archive_entry_symlink(struct archive_entry *entry)
488 return (aes_get_mbs(&entry->ae_symlink));
492 archive_entry_uid(struct archive_entry *entry)
494 return (entry->ae_stat.st_uid);
498 archive_entry_uname(struct archive_entry *entry)
500 return (aes_get_mbs(&entry->ae_uname));
504 * Functions to set archive_entry properties.
508 * Note "copy" not "set" here. The "set" functions that accept a pointer
509 * only store the pointer; they don't copy the underlying object.
512 archive_entry_copy_stat(struct archive_entry *entry, const struct stat *st)
514 entry->ae_stat = *st;
518 archive_entry_set_fflags(struct archive_entry *entry,
519 unsigned long set, unsigned long clear)
521 aes_clean(&entry->ae_fflags_text);
522 entry->ae_fflags_set = set;
523 entry->ae_fflags_clear = clear;
527 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
528 const wchar_t *flags)
530 aes_copy_wcs(&entry->ae_fflags_text, flags);
531 return (ae_wcstofflags(flags,
532 &entry->ae_fflags_set, &entry->ae_fflags_clear));
536 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
538 entry->ae_stat.st_gid = g;
542 archive_entry_set_gname(struct archive_entry *entry, const char *name)
544 aes_set_mbs(&entry->ae_gname, name);
548 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
550 aes_copy_wcs(&entry->ae_gname, name);
554 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
556 aes_set_mbs(&entry->ae_hardlink, target);
560 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
562 aes_copy_mbs(&entry->ae_hardlink, target);
566 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
568 aes_copy_wcs(&entry->ae_hardlink, target);
571 /* Set symlink if symlink is already set, else set hardlink. */
573 archive_entry_set_link(struct archive_entry *entry, const char *target)
575 if (entry->ae_symlink.aes_mbs != NULL ||
576 entry->ae_symlink.aes_wcs != NULL)
577 aes_set_mbs(&entry->ae_symlink, target);
578 aes_set_mbs(&entry->ae_hardlink, target);
582 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
584 entry->ae_stat.st_mode = m;
588 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
590 entry->ae_stat.st_mtime = m;
591 ARCHIVE_STAT_SET_MTIME_NANOS(&entry->ae_stat, ns);
595 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
597 aes_set_mbs(&entry->ae_pathname, name);
601 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
603 aes_copy_wcs(&entry->ae_pathname, name);
607 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
611 d = entry->ae_stat.st_rdev;
612 entry->ae_stat.st_rdev = makedev(m, minor(d));
616 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
620 d = entry->ae_stat.st_rdev;
621 entry->ae_stat.st_rdev = makedev( major(d), m);
625 archive_entry_set_size(struct archive_entry *entry, int64_t s)
627 entry->ae_stat.st_size = s;
631 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
633 aes_set_mbs(&entry->ae_symlink, linkname);
637 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
639 aes_copy_wcs(&entry->ae_symlink, linkname);
643 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
645 entry->ae_stat.st_uid = u;
649 archive_entry_set_uname(struct archive_entry *entry, const char *name)
651 aes_set_mbs(&entry->ae_uname, name);
655 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
657 aes_copy_wcs(&entry->ae_uname, name);
661 * ACL management. The following would, of course, be a lot simpler
662 * if: 1) the last draft of POSIX.1e were a really thorough and
663 * complete standard that addressed the needs of ACL archiving and 2)
664 * everyone followed it faithfully. Alas, neither is true, so the
665 * following is a lot more complex than might seem necessary to the
670 archive_entry_acl_clear(struct archive_entry *entry)
674 while (entry->acl_head != NULL) {
675 ap = entry->acl_head->next;
676 aes_clean(&entry->acl_head->name);
677 free(entry->acl_head);
678 entry->acl_head = ap;
680 if (entry->acl_text_w != NULL) {
681 free(entry->acl_text_w);
682 entry->acl_text_w = NULL;
685 entry->acl_state = 0; /* Not counting. */
689 * Add a single ACL entry to the internal list of ACL data.
692 archive_entry_acl_add_entry(struct archive_entry *entry,
693 int type, int permset, int tag, int id, const char *name)
697 if (acl_special(entry, type, permset, tag) == 0)
699 ap = acl_new_entry(entry, type, permset, tag, id);
704 if (name != NULL && *name != '\0')
705 aes_copy_mbs(&ap->name, name);
707 aes_clean(&ap->name);
711 * As above, but with a wide-character name.
714 archive_entry_acl_add_entry_w(struct archive_entry *entry,
715 int type, int permset, int tag, int id, const wchar_t *name)
719 if (acl_special(entry, type, permset, tag) == 0)
721 ap = acl_new_entry(entry, type, permset, tag, id);
726 if (name != NULL && *name != L'\0')
727 aes_copy_wcs(&ap->name, name);
729 aes_clean(&ap->name);
733 * If this ACL entry is part of the standard POSIX permissions set,
734 * store the permissions in the stat structure and return zero.
737 acl_special(struct archive_entry *entry, int type, int permset, int tag)
739 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
741 case ARCHIVE_ENTRY_ACL_USER_OBJ:
742 entry->ae_stat.st_mode &= ~0700;
743 entry->ae_stat.st_mode |= (permset & 7) << 6;
745 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
746 entry->ae_stat.st_mode &= ~0070;
747 entry->ae_stat.st_mode |= (permset & 7) << 3;
749 case ARCHIVE_ENTRY_ACL_OTHER:
750 entry->ae_stat.st_mode &= ~0007;
751 entry->ae_stat.st_mode |= permset & 7;
759 * Allocate and populate a new ACL entry with everything but the
762 static struct ae_acl *
763 acl_new_entry(struct archive_entry *entry,
764 int type, int permset, int tag, int id)
768 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
769 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
771 if (entry->acl_text_w != NULL) {
772 free(entry->acl_text_w);
773 entry->acl_text_w = NULL;
776 /* XXX TODO: More sanity-checks on the arguments XXX */
778 /* If there's a matching entry already in the list, overwrite it. */
779 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
780 if (ap->type == type && ap->tag == tag && ap->id == id) {
781 ap->permset = permset;
786 /* Add a new entry to the list. */
787 ap = malloc(sizeof(*ap));
788 memset(ap, 0, sizeof(*ap));
789 ap->next = entry->acl_head;
790 entry->acl_head = ap;
794 ap->permset = permset;
799 * Return a count of entries matching "want_type".
802 archive_entry_acl_count(struct archive_entry *entry, int want_type)
808 ap = entry->acl_head;
810 if ((ap->type & want_type) != 0)
815 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
821 * Prepare for reading entries from the ACL data. Returns a count
822 * of entries matching "want_type", or zero if there are no
823 * non-extended ACL entries of that type.
826 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
830 count = archive_entry_acl_count(entry, want_type);
833 * If the only entries are the three standard ones,
834 * then don't return any ACL data. (In this case,
835 * client can just use chmod(2) to set permissions.)
837 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
843 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
845 entry->acl_state = 0;
846 entry->acl_p = entry->acl_head;
851 * Return the next ACL entry in the list. Fake entries for the
852 * standard permissions and include them in the returned list.
856 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
857 int *permset, int *tag, int *id, const char **name)
863 * The acl_state is either zero (no entries available), -1
864 * (reading from list), or an entry type (retrieve that type
865 * from ae_stat.st_mode).
867 if (entry->acl_state == 0)
868 return (ARCHIVE_WARN);
870 /* The first three access entries are special. */
871 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
872 switch (entry->acl_state) {
873 case ARCHIVE_ENTRY_ACL_USER_OBJ:
874 *permset = (entry->ae_stat.st_mode >> 6) & 7;
875 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
876 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
877 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
879 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
880 *permset = (entry->ae_stat.st_mode >> 3) & 7;
881 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
882 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
883 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
885 case ARCHIVE_ENTRY_ACL_OTHER:
886 *permset = entry->ae_stat.st_mode & 7;
887 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
888 *tag = ARCHIVE_ENTRY_ACL_OTHER;
889 entry->acl_state = -1;
890 entry->acl_p = entry->acl_head;
897 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
898 entry->acl_p = entry->acl_p->next;
899 if (entry->acl_p == NULL) {
900 entry->acl_state = 0;
901 return (ARCHIVE_WARN);
903 *type = entry->acl_p->type;
904 *permset = entry->acl_p->permset;
905 *tag = entry->acl_p->tag;
906 *id = entry->acl_p->id;
907 *name = aes_get_mbs(&entry->acl_p->name);
908 entry->acl_p = entry->acl_p->next;
913 * Generate a text version of the ACL. The flags parameter controls
914 * the style of the generated ACL.
917 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
921 const wchar_t *wname;
922 const wchar_t *prefix;
928 if (entry->acl_text_w != NULL) {
929 free (entry->acl_text_w);
930 entry->acl_text_w = NULL;
936 ap = entry->acl_head;
938 if ((ap->type & flags) != 0) {
940 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
941 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
942 length += 8; /* "default:" */
943 length += 5; /* tag name */
944 length += 1; /* colon */
945 wname = aes_get_wcs(&ap->name);
947 length += wcslen(wname);
948 length ++; /* colon */
949 length += 3; /* rwx */
950 length += 1; /* colon */
951 length += max(sizeof(uid_t),sizeof(gid_t)) * 3 + 1;
952 length ++; /* newline */
957 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
958 length += 10; /* "user::rwx\n" */
959 length += 11; /* "group::rwx\n" */
960 length += 11; /* "other::rwx\n" */
966 /* Now, allocate the string and actually populate it. */
967 wp = entry->acl_text_w = malloc(length * sizeof(wchar_t));
969 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
970 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
971 entry->ae_stat.st_mode & 0700, -1);
973 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
974 entry->ae_stat.st_mode & 0070, -1);
976 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
977 entry->ae_stat.st_mode & 0007, -1);
980 ap = entry->acl_head;
982 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
983 wname = aes_get_wcs(&ap->name);
985 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
989 append_entry_w(&wp, NULL, ap->tag, wname,
998 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
999 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1000 prefix = L"default:";
1003 ap = entry->acl_head;
1005 while (ap != NULL) {
1006 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1007 wname = aes_get_wcs(&ap->name);
1010 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1014 append_entry_w(&wp, prefix, ap->tag,
1015 wname, ap->permset, id);
1022 return (entry->acl_text_w);
1026 append_id_w(wchar_t **wp, int id)
1029 append_id_w(wp, id / 10);
1030 *(*wp)++ = L"0123456789"[id % 10];
1034 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1035 const wchar_t *wname, int perm, int id)
1037 if (prefix != NULL) {
1038 wcscpy(*wp, prefix);
1042 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1046 case ARCHIVE_ENTRY_ACL_USER:
1047 wcscpy(*wp, L"user");
1049 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1053 case ARCHIVE_ENTRY_ACL_GROUP:
1054 wcscpy(*wp, L"group");
1056 case ARCHIVE_ENTRY_ACL_MASK:
1057 wcscpy(*wp, L"mask");
1061 case ARCHIVE_ENTRY_ACL_OTHER:
1062 wcscpy(*wp, L"other");
1069 if (wname != NULL) {
1074 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1075 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1076 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1079 append_id_w(wp, id);
1085 * Parse a textual ACL. This automatically recognizes and supports
1086 * extensions described above. The 'type' argument is used to
1087 * indicate the type that should be used for any entries not
1088 * explicitly marked as "default:".
1091 __archive_entry_acl_parse_w(struct archive_entry *entry,
1092 const wchar_t *text, int default_type)
1094 int type, tag, permset, id;
1095 const wchar_t *start, *end;
1096 const wchar_t *name_start, *name_end;
1099 int namebuff_length;
1101 name_start = name_end = NULL;
1103 namebuff_length = 0;
1105 while (text != NULL && *text != L'\0') {
1106 next_field_w(&text, &start, &end, &sep);
1111 * Solaris extension: "defaultuser::rwx" is the
1112 * default ACL corresponding to "user::rwx", etc.
1114 if (end-start > 7 && wmemcmp(start, L"default", 7) == 0) {
1115 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1118 type = default_type;
1120 if (prefix_w(start, end, L"user")) {
1121 next_field_w(&text, &start, &end, &sep);
1125 tag = ARCHIVE_ENTRY_ACL_USER;
1129 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1130 } else if (prefix_w(start, end, L"group")) {
1131 next_field_w(&text, &start, &end, &sep);
1135 tag = ARCHIVE_ENTRY_ACL_GROUP;
1139 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1140 } else if (prefix_w(start, end, L"other")) {
1141 next_field_w(&text, &start, &end, &sep);
1146 tag = ARCHIVE_ENTRY_ACL_OTHER;
1147 } else if (prefix_w(start, end, L"mask")) {
1148 next_field_w(&text, &start, &end, &sep);
1153 tag = ARCHIVE_ENTRY_ACL_MASK;
1157 next_field_w(&text, &start, &end, &sep);
1159 while (start < end) {
1162 permset |= ARCHIVE_ENTRY_ACL_READ;
1165 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1168 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1178 * Support star-compatible numeric UID/GID extension.
1179 * This extension adds a ":" followed by the numeric
1180 * ID so that "group:groupname:rwx", for example,
1181 * becomes "group:groupname:rwx:999", where 999 is the
1182 * numeric GID. This extension makes it possible, for
1183 * example, to correctly restore ACLs on a system that
1184 * might have a damaged passwd file or be disconnected
1185 * from a central NIS server. This extension is compatible
1186 * with POSIX.1e draft 17.
1188 if (sep == L':' && (tag == ARCHIVE_ENTRY_ACL_USER ||
1189 tag == ARCHIVE_ENTRY_ACL_GROUP)) {
1190 next_field_w(&text, &start, &end, &sep);
1193 while (start < end && *start >= '0' && *start <= '9') {
1194 if (id > (INT_MAX / 10))
1203 id = -1; /* No id specified. */
1205 /* Skip any additional entries. */
1206 while (sep == L':') {
1207 next_field_w(&text, &start, &end, &sep);
1210 /* Add entry to the internal list. */
1211 if (name_end == name_start) {
1212 archive_entry_acl_add_entry_w(entry, type, permset,
1215 if (namebuff_length <= name_end - name_start) {
1216 if (namebuff != NULL)
1218 namebuff_length = name_end - name_start + 256;
1220 malloc(namebuff_length * sizeof(wchar_t));
1222 wmemcpy(namebuff, name_start, name_end - name_start);
1223 namebuff[name_end - name_start] = L'\0';
1224 archive_entry_acl_add_entry_w(entry, type,
1225 permset, tag, id, namebuff);
1228 if (namebuff != NULL)
1230 return (ARCHIVE_OK);
1233 if (namebuff != NULL)
1235 return (ARCHIVE_WARN);
1239 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1240 * to point to just after the separator. *start points to the first
1241 * character of the matched text and *end just after the last
1242 * character of the matched identifier. In particular *end - *start
1243 * is the length of the field body, not including leading or trailing
1247 next_field_w(const wchar_t **wp, const wchar_t **start,
1248 const wchar_t **end, wchar_t *sep)
1250 /* Skip leading whitespace to find start of field. */
1251 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1256 /* Scan for the separator. */
1257 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1263 /* Trim trailing whitespace to locate end of field. */
1265 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1270 /* Adjust scanner location. */
1276 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1281 if (*start++ != *test++)
1284 while (start < end && *start++ == *test++)
1295 * Following code is modified from UC Berkeley sources, and
1296 * is subject to the following copyright notice.
1300 * Copyright (c) 1993
1301 * The Regents of the University of California. All rights reserved.
1303 * Redistribution and use in source and binary forms, with or without
1304 * modification, are permitted provided that the following conditions
1306 * 1. Redistributions of source code must retain the above copyright
1307 * notice, this list of conditions and the following disclaimer.
1308 * 2. Redistributions in binary form must reproduce the above copyright
1309 * notice, this list of conditions and the following disclaimer in the
1310 * documentation and/or other materials provided with the distribution.
1311 * 4. Neither the name of the University nor the names of its contributors
1312 * may be used to endorse or promote products derived from this software
1313 * without specific prior written permission.
1315 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1316 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1317 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1318 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1319 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1320 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1321 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1322 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1323 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1324 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1328 static struct flag {
1330 const wchar_t *wname;
1332 unsigned long clear;
1334 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1336 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1337 { "nosappend", L"nosappend", SF_APPEND, 0 },
1339 #ifdef EXT2_APPEND_FL /* 'a' */
1340 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1341 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1344 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1345 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1348 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1349 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1350 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1352 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1353 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1354 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1355 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1358 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1359 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1362 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1365 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1366 { "nouappend", L"nouappend", UF_APPEND, 0 },
1369 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1370 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1371 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1374 { "nodump", L"nodump", 0, UF_NODUMP},
1376 #ifdef EXT2_NODUMP_FL /* 'd' */
1377 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1380 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1383 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1384 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1386 #ifdef EXT2_COMPR_FL /* 'c' */
1387 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1390 #ifdef EXT2_NOATIME_FL /* 'A' */
1391 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1393 { NULL, NULL, 0, 0 }
1398 * Convert file flags to a comma-separated string. If no flags
1399 * are set, return the empty string.
1402 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1410 bits = bitset | bitclear;
1412 for (flag = flags; flag->name != NULL; flag++)
1413 if (bits & (flag->set | flag->clear)) {
1414 length += strlen(flag->name) + 1;
1415 bits &= ~(flag->set | flag->clear);
1420 string = malloc(length);
1425 for (flag = flags; flag->name != NULL; flag++) {
1426 if (bitset & flag->set || bitclear & flag->clear) {
1427 sp = flag->name + 2;
1428 } else if (bitset & flag->clear || bitclear & flag->set) {
1432 bitset &= ~(flag->set | flag->clear);
1433 bitclear &= ~(flag->set | flag->clear);
1436 while ((*dp++ = *sp++) != '\0')
1447 * Take string of arguments and return file flags. This
1448 * version works a little differently than strtofflags(3).
1449 * In particular, it always tests every token, skipping any
1450 * unrecognized tokens. It returns a pointer to the first
1451 * unrecognized token, or NULL if every token was recognized.
1452 * This version is also const-correct and does not modify the
1456 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1458 const wchar_t *start, *end;
1460 unsigned long set, clear;
1461 const wchar_t *failed;
1466 /* Find start of first token. */
1467 while (*start == L'\t' || *start == L' ' || *start == L',')
1469 while (*start != L'\0') {
1470 /* Locate end of token. */
1472 while (*end != L'\0' && *end != L'\t' &&
1473 *end != L' ' && *end != L',')
1475 for (flag = flags; flag->wname != NULL; flag++) {
1476 if (wmemcmp(start, flag->wname, end - start) == 0) {
1477 /* Matched "noXXXX", so reverse the sense. */
1481 } else if (wmemcmp(start, flag->wname + 2, end - start)
1483 /* Matched "XXXX", so don't reverse. */
1485 clear |= flag->clear;
1489 /* Ignore unknown flag names. */
1490 if (flag->wname == NULL && failed == NULL)
1493 /* Find start of next token. */
1495 while (*start == L'\t' || *start == L' ' || *start == L',')
1505 /* Return location of first failure. */
1513 main(int argc, char **argv)
1515 struct archive_entry *entry = archive_entry_new();
1516 unsigned long set, clear;
1517 const wchar_t *remainder;
1519 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1520 archive_entry_fflags(entry, &set, &clear);
1522 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1524 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));