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>
42 #ifdef HAVE_EXT2FS_EXT2_FS_H
43 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
48 #ifdef HAVE_LINUX_FS_H
49 #include <linux/fs.h> /* for Linux file flags */
51 #ifdef HAVE_LINUX_EXT2_FS_H
52 #include <linux/ext2_fs.h> /* for Linux file flags */
67 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
70 while ((*s1 = *s2) != L'\0')
76 static size_t wcslen(const wchar_t *s)
85 /* Good enough for simple equality testing, but not for sorting. */
86 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
89 #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
93 #include "archive_entry.h"
94 #include "archive_private.h"
97 #define max(a, b) ((a)>(b)?(a):(b))
100 * Handle wide character (i.e., Unicode) and non-wide character
101 * strings transparently.
108 const wchar_t *aes_wcs;
109 wchar_t *aes_wcs_alloc;
114 int type; /* E.g., access or default */
115 int tag; /* E.g., user/group/other/mask */
116 int permset; /* r/w/x bits */
117 int id; /* uid/gid for user/group */
118 struct aes name; /* uname/gname */
122 struct ae_xattr *next;
129 static void aes_clean(struct aes *);
130 static void aes_copy(struct aes *dest, struct aes *src);
131 static const char * aes_get_mbs(struct aes *);
132 static const wchar_t * aes_get_wcs(struct aes *);
133 static void aes_set_mbs(struct aes *, const char *mbs);
134 static void aes_copy_mbs(struct aes *, const char *mbs);
135 /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
136 static void aes_copy_wcs(struct aes *, const wchar_t *wcs);
137 static void aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
139 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
140 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
141 unsigned long *setp, unsigned long *clrp);
142 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
143 const wchar_t *wname, int perm, int id);
144 static void append_id_w(wchar_t **wp, int id);
146 static int acl_special(struct archive_entry *entry,
147 int type, int permset, int tag);
148 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
149 int type, int permset, int tag, int id);
150 static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
151 static void next_field_w(const wchar_t **wp, const wchar_t **start,
152 const wchar_t **end, wchar_t *sep);
153 static int prefix_w(const wchar_t *start, const wchar_t *end,
154 const wchar_t *test);
156 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
157 int permset, int tag, int id, const wchar_t *name, size_t);
161 * Description of an archive entry.
163 * Basically, this is a "struct stat" with a few text fields added in.
165 * TODO: Add "comment", "charset", and possibly other entries
166 * that are supported by "pax interchange" format. However, GNU, ustar,
167 * cpio, and other variants don't support these features, so they're not an
168 * excruciatingly high priority right now.
170 * TODO: "pax interchange" format allows essentially arbitrary
171 * key/value attributes to be attached to any entry. Supporting
172 * such extensions may make this library useful for special
173 * applications (e.g., a package manager could attach special
174 * package-management attributes to each entry). There are tricky
175 * API issues involved, so this is not going to happen until
176 * there's a real demand for it.
178 * TODO: Design a good API for handling sparse files.
180 struct archive_entry {
182 * Note that ae_stat.st_mode & S_IFMT can be 0!
184 * This occurs when the actual file type of the object is not
185 * in the archive. For example, 'tar' archives store
186 * hardlinks without marking the type of the underlying
192 * Use aes here so that we get transparent mbs<->wcs conversions.
194 struct aes ae_fflags_text; /* Text fflags per fflagstostr(3) */
195 unsigned long ae_fflags_set; /* Bitmap fflags */
196 unsigned long ae_fflags_clear;
197 struct aes ae_gname; /* Name of owning group */
198 struct aes ae_hardlink; /* Name of target for hardlink */
199 struct aes ae_pathname; /* Name of entry */
200 struct aes ae_symlink; /* symlink contents */
201 struct aes ae_uname; /* Name of owner */
203 struct ae_acl *acl_head;
204 struct ae_acl *acl_p;
205 int acl_state; /* See acl_next for details. */
208 struct ae_xattr *xattr_head;
209 struct ae_xattr *xattr_p;
213 aes_clean(struct aes *aes)
215 if (aes->aes_mbs_alloc) {
216 free(aes->aes_mbs_alloc);
217 aes->aes_mbs_alloc = NULL;
219 if (aes->aes_wcs_alloc) {
220 free(aes->aes_wcs_alloc);
221 aes->aes_wcs_alloc = NULL;
223 memset(aes, 0, sizeof(*aes));
227 aes_copy(struct aes *dest, struct aes *src)
230 if (src->aes_mbs != NULL) {
231 dest->aes_mbs_alloc = strdup(src->aes_mbs);
232 dest->aes_mbs = dest->aes_mbs_alloc;
233 if (dest->aes_mbs == NULL)
234 __archive_errx(1, "No memory for aes_copy()");
237 if (src->aes_wcs != NULL) {
238 dest->aes_wcs_alloc = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
240 dest->aes_wcs = dest->aes_wcs_alloc;
241 if (dest->aes_wcs == NULL)
242 __archive_errx(1, "No memory for aes_copy()");
243 wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
248 aes_get_mbs(struct aes *aes)
250 if (aes->aes_mbs == NULL && aes->aes_wcs == NULL)
252 if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
254 * XXX Need to estimate the number of byte in the
255 * multi-byte form. Assume that, on average, wcs
256 * chars encode to no more than 3 bytes. There must
257 * be a better way... XXX
259 size_t mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
261 aes->aes_mbs_alloc = (char *)malloc(mbs_length);
262 aes->aes_mbs = aes->aes_mbs_alloc;
263 if (aes->aes_mbs == NULL)
264 __archive_errx(1, "No memory for aes_get_mbs()");
265 wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
266 aes->aes_mbs_alloc[mbs_length - 1] = 0;
268 return (aes->aes_mbs);
271 static const wchar_t *
272 aes_get_wcs(struct aes *aes)
274 if (aes->aes_wcs == NULL && aes->aes_mbs == NULL)
276 if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
278 * No single byte will be more than one wide character,
279 * so this length estimate will always be big enough.
281 size_t wcs_length = strlen(aes->aes_mbs);
284 = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
285 aes->aes_wcs = aes->aes_wcs_alloc;
286 if (aes->aes_wcs == NULL)
287 __archive_errx(1, "No memory for aes_get_wcs()");
288 mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
289 aes->aes_wcs_alloc[wcs_length] = 0;
291 return (aes->aes_wcs);
295 aes_set_mbs(struct aes *aes, const char *mbs)
297 if (aes->aes_mbs_alloc) {
298 free(aes->aes_mbs_alloc);
299 aes->aes_mbs_alloc = NULL;
301 if (aes->aes_wcs_alloc) {
302 free(aes->aes_wcs_alloc);
303 aes->aes_wcs_alloc = NULL;
310 aes_copy_mbs(struct aes *aes, const char *mbs)
312 if (aes->aes_mbs_alloc) {
313 free(aes->aes_mbs_alloc);
314 aes->aes_mbs_alloc = NULL;
316 if (aes->aes_wcs_alloc) {
317 free(aes->aes_wcs_alloc);
318 aes->aes_wcs_alloc = NULL;
320 aes->aes_mbs_alloc = (char *)malloc((strlen(mbs) + 1) * sizeof(char));
321 if (aes->aes_mbs_alloc == NULL)
322 __archive_errx(1, "No memory for aes_copy_mbs()");
323 strcpy(aes->aes_mbs_alloc, mbs);
324 aes->aes_mbs = aes->aes_mbs_alloc;
330 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
332 if (aes->aes_mbs_alloc) {
333 free(aes->aes_mbs_alloc);
334 aes->aes_mbs_alloc = NULL;
336 if (aes->aes_wcs_alloc) {
337 free(aes->aes_wcs_alloc);
338 aes->aes_wcs_alloc = NULL;
346 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
348 aes_copy_wcs_len(aes, wcs, wcslen(wcs));
352 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
354 if (aes->aes_mbs_alloc) {
355 free(aes->aes_mbs_alloc);
356 aes->aes_mbs_alloc = NULL;
358 if (aes->aes_wcs_alloc) {
359 free(aes->aes_wcs_alloc);
360 aes->aes_wcs_alloc = NULL;
363 aes->aes_wcs_alloc = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
364 if (aes->aes_wcs_alloc == NULL)
365 __archive_errx(1, "No memory for aes_copy_wcs()");
366 wmemcpy(aes->aes_wcs_alloc, wcs, len);
367 aes->aes_wcs_alloc[len] = L'\0';
368 aes->aes_wcs = aes->aes_wcs_alloc;
371 struct archive_entry *
372 archive_entry_clear(struct archive_entry *entry)
374 aes_clean(&entry->ae_fflags_text);
375 aes_clean(&entry->ae_gname);
376 aes_clean(&entry->ae_hardlink);
377 aes_clean(&entry->ae_pathname);
378 aes_clean(&entry->ae_symlink);
379 aes_clean(&entry->ae_uname);
380 archive_entry_acl_clear(entry);
381 archive_entry_xattr_clear(entry);
382 memset(entry, 0, sizeof(*entry));
386 struct archive_entry *
387 archive_entry_clone(struct archive_entry *entry)
389 struct archive_entry *entry2;
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_symlink, &entry->ae_symlink);
405 aes_copy(&entry2->ae_uname, &entry->ae_uname);
407 /* XXX TODO: Copy ACL data over as well. XXX */
408 /* XXX TODO: Copy xattr data over as well. XXX */
413 archive_entry_free(struct archive_entry *entry)
415 archive_entry_clear(entry);
419 struct archive_entry *
420 archive_entry_new(void)
422 struct archive_entry *entry;
424 entry = (struct archive_entry *)malloc(sizeof(*entry));
427 memset(entry, 0, sizeof(*entry));
432 * Functions for reading fields from an archive_entry.
436 archive_entry_atime(struct archive_entry *entry)
438 return (entry->ae_stat.st_atime);
442 archive_entry_atime_nsec(struct archive_entry *entry)
444 (void)entry; /* entry can be unused here. */
445 return (ARCHIVE_STAT_ATIME_NANOS(&entry->ae_stat));
449 archive_entry_ctime(struct archive_entry *entry)
451 return (entry->ae_stat.st_ctime);
455 archive_entry_ctime_nsec(struct archive_entry *entry)
457 (void)entry; /* entry can be unused here. */
458 return (ARCHIVE_STAT_CTIME_NANOS(&entry->ae_stat));
462 archive_entry_dev(struct archive_entry *entry)
464 return (entry->ae_stat.st_dev);
468 archive_entry_fflags(struct archive_entry *entry,
469 unsigned long *set, unsigned long *clear)
471 *set = entry->ae_fflags_set;
472 *clear = entry->ae_fflags_clear;
476 * Note: if text was provided, this just returns that text. If you
477 * really need the text to be rebuilt in a canonical form, set the
478 * text, ask for the bitmaps, then set the bitmaps. (Setting the
479 * bitmaps clears any stored text.) This design is deliberate: if
480 * we're editing archives, we don't want to discard flags just because
481 * they aren't supported on the current system. The bitmap<->text
482 * conversions are platform-specific (see below).
485 archive_entry_fflags_text(struct archive_entry *entry)
490 f = aes_get_mbs(&entry->ae_fflags_text);
494 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
497 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
501 aes_copy_mbs(&entry->ae_fflags_text, p);
503 f = aes_get_mbs(&entry->ae_fflags_text);
508 archive_entry_gid(struct archive_entry *entry)
510 return (entry->ae_stat.st_gid);
514 archive_entry_gname(struct archive_entry *entry)
516 return (aes_get_mbs(&entry->ae_gname));
520 archive_entry_gname_w(struct archive_entry *entry)
522 return (aes_get_wcs(&entry->ae_gname));
526 archive_entry_hardlink(struct archive_entry *entry)
528 return (aes_get_mbs(&entry->ae_hardlink));
532 archive_entry_hardlink_w(struct archive_entry *entry)
534 return (aes_get_wcs(&entry->ae_hardlink));
538 archive_entry_ino(struct archive_entry *entry)
540 return (entry->ae_stat.st_ino);
544 archive_entry_mode(struct archive_entry *entry)
546 return (entry->ae_stat.st_mode);
550 archive_entry_mtime(struct archive_entry *entry)
552 return (entry->ae_stat.st_mtime);
556 archive_entry_mtime_nsec(struct archive_entry *entry)
558 (void)entry; /* entry can be unused here. */
559 return (ARCHIVE_STAT_MTIME_NANOS(&entry->ae_stat));
563 archive_entry_pathname(struct archive_entry *entry)
565 return (aes_get_mbs(&entry->ae_pathname));
569 archive_entry_pathname_w(struct archive_entry *entry)
571 return (aes_get_wcs(&entry->ae_pathname));
575 archive_entry_rdev(struct archive_entry *entry)
577 return (entry->ae_stat.st_rdev);
581 archive_entry_rdevmajor(struct archive_entry *entry)
583 return (major(entry->ae_stat.st_rdev));
587 archive_entry_rdevminor(struct archive_entry *entry)
589 return (minor(entry->ae_stat.st_rdev));
593 archive_entry_size(struct archive_entry *entry)
595 return (entry->ae_stat.st_size);
599 archive_entry_stat(struct archive_entry *entry)
601 return (&entry->ae_stat);
605 archive_entry_symlink(struct archive_entry *entry)
607 return (aes_get_mbs(&entry->ae_symlink));
611 archive_entry_symlink_w(struct archive_entry *entry)
613 return (aes_get_wcs(&entry->ae_symlink));
617 archive_entry_uid(struct archive_entry *entry)
619 return (entry->ae_stat.st_uid);
623 archive_entry_uname(struct archive_entry *entry)
625 return (aes_get_mbs(&entry->ae_uname));
629 archive_entry_uname_w(struct archive_entry *entry)
631 return (aes_get_wcs(&entry->ae_uname));
635 * Functions to set archive_entry properties.
639 * Note "copy" not "set" here. The "set" functions that accept a pointer
640 * only store the pointer; they don't copy the underlying object.
643 archive_entry_copy_stat(struct archive_entry *entry, const struct stat *st)
645 entry->ae_stat = *st;
649 archive_entry_set_fflags(struct archive_entry *entry,
650 unsigned long set, unsigned long clear)
652 aes_clean(&entry->ae_fflags_text);
653 entry->ae_fflags_set = set;
654 entry->ae_fflags_clear = clear;
658 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
659 const wchar_t *flags)
661 aes_copy_wcs(&entry->ae_fflags_text, flags);
662 return (ae_wcstofflags(flags,
663 &entry->ae_fflags_set, &entry->ae_fflags_clear));
667 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
669 entry->ae_stat.st_gid = g;
673 archive_entry_set_gname(struct archive_entry *entry, const char *name)
675 aes_set_mbs(&entry->ae_gname, name);
679 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
681 aes_copy_wcs(&entry->ae_gname, name);
685 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
687 aes_set_mbs(&entry->ae_hardlink, target);
691 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
693 aes_copy_mbs(&entry->ae_hardlink, target);
697 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
699 aes_copy_wcs(&entry->ae_hardlink, target);
703 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
705 entry->ae_stat.st_atime = t;
706 ARCHIVE_STAT_SET_ATIME_NANOS(&entry->ae_stat, ns);
710 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
712 entry->ae_stat.st_ctime = t;
713 ARCHIVE_STAT_SET_CTIME_NANOS(&entry->ae_stat, ns);
716 /* Set symlink if symlink is already set, else set hardlink. */
718 archive_entry_set_link(struct archive_entry *entry, const char *target)
720 if (entry->ae_symlink.aes_mbs != NULL ||
721 entry->ae_symlink.aes_wcs != NULL)
722 aes_set_mbs(&entry->ae_symlink, target);
724 aes_set_mbs(&entry->ae_hardlink, target);
728 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
730 entry->ae_stat.st_mode = m;
734 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
736 entry->ae_stat.st_mtime = m;
737 ARCHIVE_STAT_SET_MTIME_NANOS(&entry->ae_stat, ns);
741 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
743 aes_set_mbs(&entry->ae_pathname, name);
747 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
749 aes_copy_mbs(&entry->ae_pathname, name);
753 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
755 aes_copy_wcs(&entry->ae_pathname, name);
759 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
763 d = entry->ae_stat.st_rdev;
764 entry->ae_stat.st_rdev = makedev(major(m), minor(d));
768 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
772 d = entry->ae_stat.st_rdev;
773 entry->ae_stat.st_rdev = makedev(major(d), minor(m));
777 archive_entry_set_size(struct archive_entry *entry, int64_t s)
779 entry->ae_stat.st_size = s;
783 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
785 aes_set_mbs(&entry->ae_symlink, linkname);
789 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
791 aes_copy_mbs(&entry->ae_symlink, linkname);
795 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
797 aes_copy_wcs(&entry->ae_symlink, linkname);
801 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
803 entry->ae_stat.st_uid = u;
807 archive_entry_set_uname(struct archive_entry *entry, const char *name)
809 aes_set_mbs(&entry->ae_uname, name);
813 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
815 aes_copy_wcs(&entry->ae_uname, name);
819 * ACL management. The following would, of course, be a lot simpler
820 * if: 1) the last draft of POSIX.1e were a really thorough and
821 * complete standard that addressed the needs of ACL archiving and 2)
822 * everyone followed it faithfully. Alas, neither is true, so the
823 * following is a lot more complex than might seem necessary to the
828 archive_entry_acl_clear(struct archive_entry *entry)
832 while (entry->acl_head != NULL) {
833 ap = entry->acl_head->next;
834 aes_clean(&entry->acl_head->name);
835 free(entry->acl_head);
836 entry->acl_head = ap;
838 if (entry->acl_text_w != NULL) {
839 free(entry->acl_text_w);
840 entry->acl_text_w = NULL;
843 entry->acl_state = 0; /* Not counting. */
847 * Add a single ACL entry to the internal list of ACL data.
850 archive_entry_acl_add_entry(struct archive_entry *entry,
851 int type, int permset, int tag, int id, const char *name)
855 if (acl_special(entry, type, permset, tag) == 0)
857 ap = acl_new_entry(entry, type, permset, tag, id);
862 if (name != NULL && *name != '\0')
863 aes_copy_mbs(&ap->name, name);
865 aes_clean(&ap->name);
869 * As above, but with a wide-character name.
872 archive_entry_acl_add_entry_w(struct archive_entry *entry,
873 int type, int permset, int tag, int id, const wchar_t *name)
875 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
879 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
880 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
884 if (acl_special(entry, type, permset, tag) == 0)
886 ap = acl_new_entry(entry, type, permset, tag, id);
891 if (name != NULL && *name != L'\0' && len > 0)
892 aes_copy_wcs_len(&ap->name, name, len);
894 aes_clean(&ap->name);
898 * If this ACL entry is part of the standard POSIX permissions set,
899 * store the permissions in the stat structure and return zero.
902 acl_special(struct archive_entry *entry, int type, int permset, int tag)
904 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
906 case ARCHIVE_ENTRY_ACL_USER_OBJ:
907 entry->ae_stat.st_mode &= ~0700;
908 entry->ae_stat.st_mode |= (permset & 7) << 6;
910 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
911 entry->ae_stat.st_mode &= ~0070;
912 entry->ae_stat.st_mode |= (permset & 7) << 3;
914 case ARCHIVE_ENTRY_ACL_OTHER:
915 entry->ae_stat.st_mode &= ~0007;
916 entry->ae_stat.st_mode |= permset & 7;
924 * Allocate and populate a new ACL entry with everything but the
927 static struct ae_acl *
928 acl_new_entry(struct archive_entry *entry,
929 int type, int permset, int tag, int id)
933 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
934 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
936 if (entry->acl_text_w != NULL) {
937 free(entry->acl_text_w);
938 entry->acl_text_w = NULL;
941 /* XXX TODO: More sanity-checks on the arguments XXX */
943 /* If there's a matching entry already in the list, overwrite it. */
944 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
945 if (ap->type == type && ap->tag == tag && ap->id == id) {
946 ap->permset = permset;
951 /* Add a new entry to the list. */
952 ap = (struct ae_acl *)malloc(sizeof(*ap));
955 memset(ap, 0, sizeof(*ap));
956 ap->next = entry->acl_head;
957 entry->acl_head = ap;
961 ap->permset = permset;
966 * Return a count of entries matching "want_type".
969 archive_entry_acl_count(struct archive_entry *entry, int want_type)
975 ap = entry->acl_head;
977 if ((ap->type & want_type) != 0)
982 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
988 * Prepare for reading entries from the ACL data. Returns a count
989 * of entries matching "want_type", or zero if there are no
990 * non-extended ACL entries of that type.
993 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
997 count = archive_entry_acl_count(entry, want_type);
1000 * If the only entries are the three standard ones,
1001 * then don't return any ACL data. (In this case,
1002 * client can just use chmod(2) to set permissions.)
1004 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1010 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1012 entry->acl_state = 0;
1013 entry->acl_p = entry->acl_head;
1018 * Return the next ACL entry in the list. Fake entries for the
1019 * standard permissions and include them in the returned list.
1023 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1024 int *permset, int *tag, int *id, const char **name)
1030 * The acl_state is either zero (no entries available), -1
1031 * (reading from list), or an entry type (retrieve that type
1032 * from ae_stat.st_mode).
1034 if (entry->acl_state == 0)
1035 return (ARCHIVE_WARN);
1037 /* The first three access entries are special. */
1038 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1039 switch (entry->acl_state) {
1040 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1041 *permset = (entry->ae_stat.st_mode >> 6) & 7;
1042 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1043 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1044 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1045 return (ARCHIVE_OK);
1046 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1047 *permset = (entry->ae_stat.st_mode >> 3) & 7;
1048 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1049 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1050 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1051 return (ARCHIVE_OK);
1052 case ARCHIVE_ENTRY_ACL_OTHER:
1053 *permset = entry->ae_stat.st_mode & 7;
1054 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1055 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1056 entry->acl_state = -1;
1057 entry->acl_p = entry->acl_head;
1058 return (ARCHIVE_OK);
1064 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1065 entry->acl_p = entry->acl_p->next;
1066 if (entry->acl_p == NULL) {
1067 entry->acl_state = 0;
1068 return (ARCHIVE_EOF); /* End of ACL entries. */
1070 *type = entry->acl_p->type;
1071 *permset = entry->acl_p->permset;
1072 *tag = entry->acl_p->tag;
1073 *id = entry->acl_p->id;
1074 *name = aes_get_mbs(&entry->acl_p->name);
1075 entry->acl_p = entry->acl_p->next;
1076 return (ARCHIVE_OK);
1080 * Generate a text version of the ACL. The flags parameter controls
1081 * the style of the generated ACL.
1084 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1088 const wchar_t *wname;
1089 const wchar_t *prefix;
1095 if (entry->acl_text_w != NULL) {
1096 free (entry->acl_text_w);
1097 entry->acl_text_w = NULL;
1103 ap = entry->acl_head;
1104 while (ap != NULL) {
1105 if ((ap->type & flags) != 0) {
1107 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1108 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1109 length += 8; /* "default:" */
1110 length += 5; /* tag name */
1111 length += 1; /* colon */
1112 wname = aes_get_wcs(&ap->name);
1114 length += wcslen(wname);
1116 length += sizeof(uid_t) * 3 + 1;
1117 length ++; /* colon */
1118 length += 3; /* rwx */
1119 length += 1; /* colon */
1120 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1121 length ++; /* newline */
1126 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1127 length += 10; /* "user::rwx\n" */
1128 length += 11; /* "group::rwx\n" */
1129 length += 11; /* "other::rwx\n" */
1135 /* Now, allocate the string and actually populate it. */
1136 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1138 __archive_errx(1, "No memory to generate the text version of the ACL");
1140 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1141 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1142 entry->ae_stat.st_mode & 0700, -1);
1144 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1145 entry->ae_stat.st_mode & 0070, -1);
1147 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1148 entry->ae_stat.st_mode & 0007, -1);
1151 ap = entry->acl_head;
1152 while (ap != NULL) {
1153 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1154 wname = aes_get_wcs(&ap->name);
1156 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1160 append_entry_w(&wp, NULL, ap->tag, wname,
1169 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1170 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1171 prefix = L"default:";
1174 ap = entry->acl_head;
1176 while (ap != NULL) {
1177 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1178 wname = aes_get_wcs(&ap->name);
1181 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1185 append_entry_w(&wp, prefix, ap->tag,
1186 wname, ap->permset, id);
1193 return (entry->acl_text_w);
1197 append_id_w(wchar_t **wp, int id)
1202 append_id_w(wp, id / 10);
1203 *(*wp)++ = L"0123456789"[id % 10];
1207 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1208 const wchar_t *wname, int perm, int id)
1210 if (prefix != NULL) {
1211 wcscpy(*wp, prefix);
1215 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1219 case ARCHIVE_ENTRY_ACL_USER:
1220 wcscpy(*wp, L"user");
1222 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1226 case ARCHIVE_ENTRY_ACL_GROUP:
1227 wcscpy(*wp, L"group");
1229 case ARCHIVE_ENTRY_ACL_MASK:
1230 wcscpy(*wp, L"mask");
1234 case ARCHIVE_ENTRY_ACL_OTHER:
1235 wcscpy(*wp, L"other");
1242 if (wname != NULL) {
1245 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1246 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1247 append_id_w(wp, id);
1251 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1252 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1253 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1256 append_id_w(wp, id);
1262 * Parse a textual ACL. This automatically recognizes and supports
1263 * extensions described above. The 'type' argument is used to
1264 * indicate the type that should be used for any entries not
1265 * explicitly marked as "default:".
1268 __archive_entry_acl_parse_w(struct archive_entry *entry,
1269 const wchar_t *text, int default_type)
1272 const wchar_t *start;
1277 int type, tag, permset, id;
1281 while (text != NULL && *text != L'\0') {
1283 * Parse the fields out of the next entry,
1284 * advance 'text' to start of next entry.
1288 const wchar_t *start, *end;
1289 next_field_w(&text, &start, &end, &sep);
1291 field[fields].start = start;
1292 field[fields].end = end;
1295 } while (sep == L':');
1298 return (ARCHIVE_WARN);
1300 /* Check for a numeric ID in field 1 or 3. */
1302 isint_w(field[1].start, field[1].end, &id);
1303 /* Field 3 is optional. */
1304 if (id == -1 && fields > 3)
1305 isint_w(field[3].start, field[3].end, &id);
1307 /* Parse the permissions from field 2. */
1310 while (p < field[2].end) {
1313 permset |= ARCHIVE_ENTRY_ACL_READ;
1316 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1319 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1324 return (ARCHIVE_WARN);
1329 * Solaris extension: "defaultuser::rwx" is the
1330 * default ACL corresponding to "user::rwx", etc.
1332 if (field[0].end-field[0].start > 7
1333 && wmemcmp(field[0].start, L"default", 7) == 0) {
1334 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1335 field[0].start += 7;
1337 type = default_type;
1339 if (prefix_w(field[0].start, field[0].end, L"user")) {
1340 if (id != -1 || field[1].start < field[1].end)
1341 tag = ARCHIVE_ENTRY_ACL_USER;
1343 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1344 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1345 if (id != -1 || field[1].start < field[1].end)
1346 tag = ARCHIVE_ENTRY_ACL_GROUP;
1348 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1349 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1350 if (id != -1 || field[1].start < field[1].end)
1351 return (ARCHIVE_WARN);
1352 tag = ARCHIVE_ENTRY_ACL_OTHER;
1353 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1354 if (id != -1 || field[1].start < field[1].end)
1355 return (ARCHIVE_WARN);
1356 tag = ARCHIVE_ENTRY_ACL_MASK;
1358 return (ARCHIVE_WARN);
1360 /* Add entry to the internal list. */
1361 archive_entry_acl_add_entry_w_len(entry, type, permset,
1362 tag, id, field[1].start, field[1].end - field[1].start);
1364 return (ARCHIVE_OK);
1368 * extended attribute handling
1372 archive_entry_xattr_clear(struct archive_entry *entry)
1374 struct ae_xattr *xp;
1376 while (entry->xattr_head != NULL) {
1377 xp = entry->xattr_head->next;
1378 free(entry->xattr_head->name);
1379 free(entry->xattr_head->value);
1380 free(entry->xattr_head);
1381 entry->xattr_head = xp;
1384 entry->xattr_head = NULL;
1388 archive_entry_xattr_add_entry(struct archive_entry *entry,
1389 const char *name, const void *value, size_t size)
1391 struct ae_xattr *xp;
1393 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1396 if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1400 xp->name = strdup(name);
1401 if ((xp->value = malloc(size)) != NULL) {
1402 memcpy(xp->value, value, size);
1407 xp->next = entry->xattr_head;
1408 entry->xattr_head = xp;
1413 * returns number of the extended attribute entries
1416 archive_entry_xattr_count(struct archive_entry *entry)
1418 struct ae_xattr *xp;
1421 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1428 archive_entry_xattr_reset(struct archive_entry * entry)
1430 entry->xattr_p = entry->xattr_head;
1432 return archive_entry_xattr_count(entry);
1436 archive_entry_xattr_next(struct archive_entry * entry,
1437 const char **name, const void **value, size_t *size)
1439 if (entry->xattr_p) {
1440 *name = entry->xattr_p->name;
1441 *value = entry->xattr_p->value;
1442 *size = entry->xattr_p->size;
1444 entry->xattr_p = entry->xattr_p->next;
1446 return (ARCHIVE_OK);
1451 return (ARCHIVE_WARN);
1456 * end of xattr handling
1460 * Parse a string to a positive decimal integer. Returns true if
1461 * the string is non-empty and consists only of decimal digits,
1465 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1470 while (start < end) {
1471 if (*start < '0' || *start > '9')
1473 if (n > (INT_MAX / 10))
1486 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1487 * to point to just after the separator. *start points to the first
1488 * character of the matched text and *end just after the last
1489 * character of the matched identifier. In particular *end - *start
1490 * is the length of the field body, not including leading or trailing
1494 next_field_w(const wchar_t **wp, const wchar_t **start,
1495 const wchar_t **end, wchar_t *sep)
1497 /* Skip leading whitespace to find start of field. */
1498 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1503 /* Scan for the separator. */
1504 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1510 /* Trim trailing whitespace to locate end of field. */
1512 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1517 /* Adjust scanner location. */
1523 * Return true if the characters [start...end) are a prefix of 'test'.
1524 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1527 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1532 if (*start++ != *test++)
1535 while (start < end && *start++ == *test++)
1546 * Following code is modified from UC Berkeley sources, and
1547 * is subject to the following copyright notice.
1551 * Copyright (c) 1993
1552 * The Regents of the University of California. All rights reserved.
1554 * Redistribution and use in source and binary forms, with or without
1555 * modification, are permitted provided that the following conditions
1557 * 1. Redistributions of source code must retain the above copyright
1558 * notice, this list of conditions and the following disclaimer.
1559 * 2. Redistributions in binary form must reproduce the above copyright
1560 * notice, this list of conditions and the following disclaimer in the
1561 * documentation and/or other materials provided with the distribution.
1562 * 4. Neither the name of the University nor the names of its contributors
1563 * may be used to endorse or promote products derived from this software
1564 * without specific prior written permission.
1566 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1567 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1568 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1569 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1570 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1571 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1572 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1573 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1574 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1575 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1579 static struct flag {
1581 const wchar_t *wname;
1583 unsigned long clear;
1585 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1587 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1588 { "nosappend", L"nosappend", SF_APPEND, 0 },
1590 #ifdef EXT2_APPEND_FL /* 'a' */
1591 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1592 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1595 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1596 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1599 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1600 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1601 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1603 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1604 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1605 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1606 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1609 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1610 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1613 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1616 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1617 { "nouappend", L"nouappend", UF_APPEND, 0 },
1620 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1621 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1622 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1625 { "nodump", L"nodump", 0, UF_NODUMP},
1627 #ifdef EXT2_NODUMP_FL /* 'd' */
1628 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1631 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1634 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1635 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1637 #ifdef EXT2_COMPR_FL /* 'c' */
1638 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1641 #ifdef EXT2_NOATIME_FL /* 'A' */
1642 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1644 { NULL, NULL, 0, 0 }
1649 * Convert file flags to a comma-separated string. If no flags
1650 * are set, return the empty string.
1653 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1661 bits = bitset | bitclear;
1663 for (flag = flags; flag->name != NULL; flag++)
1664 if (bits & (flag->set | flag->clear)) {
1665 length += strlen(flag->name) + 1;
1666 bits &= ~(flag->set | flag->clear);
1671 string = (char *)malloc(length);
1676 for (flag = flags; flag->name != NULL; flag++) {
1677 if (bitset & flag->set || bitclear & flag->clear) {
1678 sp = flag->name + 2;
1679 } else if (bitset & flag->clear || bitclear & flag->set) {
1683 bitset &= ~(flag->set | flag->clear);
1684 bitclear &= ~(flag->set | flag->clear);
1687 while ((*dp++ = *sp++) != '\0')
1698 * Take string of arguments and return file flags. This
1699 * version works a little differently than strtofflags(3).
1700 * In particular, it always tests every token, skipping any
1701 * unrecognized tokens. It returns a pointer to the first
1702 * unrecognized token, or NULL if every token was recognized.
1703 * This version is also const-correct and does not modify the
1707 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1709 const wchar_t *start, *end;
1711 unsigned long set, clear;
1712 const wchar_t *failed;
1717 /* Find start of first token. */
1718 while (*start == L'\t' || *start == L' ' || *start == L',')
1720 while (*start != L'\0') {
1721 /* Locate end of token. */
1723 while (*end != L'\0' && *end != L'\t' &&
1724 *end != L' ' && *end != L',')
1726 for (flag = flags; flag->wname != NULL; flag++) {
1727 if (wmemcmp(start, flag->wname, end - start) == 0) {
1728 /* Matched "noXXXX", so reverse the sense. */
1732 } else if (wmemcmp(start, flag->wname + 2, end - start)
1734 /* Matched "XXXX", so don't reverse. */
1736 clear |= flag->clear;
1740 /* Ignore unknown flag names. */
1741 if (flag->wname == NULL && failed == NULL)
1744 /* Find start of next token. */
1746 while (*start == L'\t' || *start == L' ' || *start == L',')
1756 /* Return location of first failure. */
1764 main(int argc, char **argv)
1766 struct archive_entry *entry = archive_entry_new();
1767 unsigned long set, clear;
1768 const wchar_t *remainder;
1770 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1771 archive_entry_fflags(entry, &set, &clear);
1773 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1775 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));