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.25 2005/03/13 02:53:42 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(struct archive_entry
*entry
, const char *name
)
603 aes_copy_mbs(&entry
->ae_pathname
, name
);
607 archive_entry_copy_pathname_w(struct archive_entry
*entry
, const wchar_t *name
)
609 aes_copy_wcs(&entry
->ae_pathname
, name
);
613 archive_entry_set_rdevmajor(struct archive_entry
*entry
, dev_t m
)
617 d
= entry
->ae_stat
.st_rdev
;
618 entry
->ae_stat
.st_rdev
= makedev(major(m
), minor(d
));
622 archive_entry_set_rdevminor(struct archive_entry
*entry
, dev_t m
)
626 d
= entry
->ae_stat
.st_rdev
;
627 entry
->ae_stat
.st_rdev
= makedev(major(d
), minor(m
));
631 archive_entry_set_size(struct archive_entry
*entry
, int64_t s
)
633 entry
->ae_stat
.st_size
= s
;
637 archive_entry_set_symlink(struct archive_entry
*entry
, const char *linkname
)
639 aes_set_mbs(&entry
->ae_symlink
, linkname
);
643 archive_entry_copy_symlink_w(struct archive_entry
*entry
, const wchar_t *linkname
)
645 aes_copy_wcs(&entry
->ae_symlink
, linkname
);
649 archive_entry_set_uid(struct archive_entry
*entry
, uid_t u
)
651 entry
->ae_stat
.st_uid
= u
;
655 archive_entry_set_uname(struct archive_entry
*entry
, const char *name
)
657 aes_set_mbs(&entry
->ae_uname
, name
);
661 archive_entry_copy_uname_w(struct archive_entry
*entry
, const wchar_t *name
)
663 aes_copy_wcs(&entry
->ae_uname
, name
);
667 * ACL management. The following would, of course, be a lot simpler
668 * if: 1) the last draft of POSIX.1e were a really thorough and
669 * complete standard that addressed the needs of ACL archiving and 2)
670 * everyone followed it faithfully. Alas, neither is true, so the
671 * following is a lot more complex than might seem necessary to the
676 archive_entry_acl_clear(struct archive_entry
*entry
)
680 while (entry
->acl_head
!= NULL
) {
681 ap
= entry
->acl_head
->next
;
682 aes_clean(&entry
->acl_head
->name
);
683 free(entry
->acl_head
);
684 entry
->acl_head
= ap
;
686 if (entry
->acl_text_w
!= NULL
) {
687 free(entry
->acl_text_w
);
688 entry
->acl_text_w
= NULL
;
691 entry
->acl_state
= 0; /* Not counting. */
695 * Add a single ACL entry to the internal list of ACL data.
698 archive_entry_acl_add_entry(struct archive_entry
*entry
,
699 int type
, int permset
, int tag
, int id
, const char *name
)
703 if (acl_special(entry
, type
, permset
, tag
) == 0)
705 ap
= acl_new_entry(entry
, type
, permset
, tag
, id
);
710 if (name
!= NULL
&& *name
!= '\0')
711 aes_copy_mbs(&ap
->name
, name
);
713 aes_clean(&ap
->name
);
717 * As above, but with a wide-character name.
720 archive_entry_acl_add_entry_w(struct archive_entry
*entry
,
721 int type
, int permset
, int tag
, int id
, const wchar_t *name
)
725 if (acl_special(entry
, type
, permset
, tag
) == 0)
727 ap
= acl_new_entry(entry
, type
, permset
, tag
, id
);
732 if (name
!= NULL
&& *name
!= L
'\0')
733 aes_copy_wcs(&ap
->name
, name
);
735 aes_clean(&ap
->name
);
739 * If this ACL entry is part of the standard POSIX permissions set,
740 * store the permissions in the stat structure and return zero.
743 acl_special(struct archive_entry
*entry
, int type
, int permset
, int tag
)
745 if (type
== ARCHIVE_ENTRY_ACL_TYPE_ACCESS
) {
747 case ARCHIVE_ENTRY_ACL_USER_OBJ
:
748 entry
->ae_stat
.st_mode
&= ~0700;
749 entry
->ae_stat
.st_mode
|= (permset
& 7) << 6;
751 case ARCHIVE_ENTRY_ACL_GROUP_OBJ
:
752 entry
->ae_stat
.st_mode
&= ~0070;
753 entry
->ae_stat
.st_mode
|= (permset
& 7) << 3;
755 case ARCHIVE_ENTRY_ACL_OTHER
:
756 entry
->ae_stat
.st_mode
&= ~0007;
757 entry
->ae_stat
.st_mode
|= permset
& 7;
765 * Allocate and populate a new ACL entry with everything but the
768 static struct ae_acl
*
769 acl_new_entry(struct archive_entry
*entry
,
770 int type
, int permset
, int tag
, int id
)
774 if (type
!= ARCHIVE_ENTRY_ACL_TYPE_ACCESS
&&
775 type
!= ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
)
777 if (entry
->acl_text_w
!= NULL
) {
778 free(entry
->acl_text_w
);
779 entry
->acl_text_w
= NULL
;
782 /* XXX TODO: More sanity-checks on the arguments XXX */
784 /* If there's a matching entry already in the list, overwrite it. */
785 for (ap
= entry
->acl_head
; ap
!= NULL
; ap
= ap
->next
) {
786 if (ap
->type
== type
&& ap
->tag
== tag
&& ap
->id
== id
) {
787 ap
->permset
= permset
;
792 /* Add a new entry to the list. */
793 ap
= malloc(sizeof(*ap
));
794 memset(ap
, 0, sizeof(*ap
));
795 ap
->next
= entry
->acl_head
;
796 entry
->acl_head
= ap
;
800 ap
->permset
= permset
;
805 * Return a count of entries matching "want_type".
808 archive_entry_acl_count(struct archive_entry
*entry
, int want_type
)
814 ap
= entry
->acl_head
;
816 if ((ap
->type
& want_type
) != 0)
821 if (count
> 0 && ((want_type
& ARCHIVE_ENTRY_ACL_TYPE_ACCESS
) != 0))
827 * Prepare for reading entries from the ACL data. Returns a count
828 * of entries matching "want_type", or zero if there are no
829 * non-extended ACL entries of that type.
832 archive_entry_acl_reset(struct archive_entry
*entry
, int want_type
)
836 count
= archive_entry_acl_count(entry
, want_type
);
839 * If the only entries are the three standard ones,
840 * then don't return any ACL data. (In this case,
841 * client can just use chmod(2) to set permissions.)
843 if ((want_type
& ARCHIVE_ENTRY_ACL_TYPE_ACCESS
) != 0)
849 entry
->acl_state
= ARCHIVE_ENTRY_ACL_USER_OBJ
;
851 entry
->acl_state
= 0;
852 entry
->acl_p
= entry
->acl_head
;
857 * Return the next ACL entry in the list. Fake entries for the
858 * standard permissions and include them in the returned list.
862 archive_entry_acl_next(struct archive_entry
*entry
, int want_type
, int *type
,
863 int *permset
, int *tag
, int *id
, const char **name
)
869 * The acl_state is either zero (no entries available), -1
870 * (reading from list), or an entry type (retrieve that type
871 * from ae_stat.st_mode).
873 if (entry
->acl_state
== 0)
874 return (ARCHIVE_WARN
);
876 /* The first three access entries are special. */
877 if ((want_type
& ARCHIVE_ENTRY_ACL_TYPE_ACCESS
) != 0) {
878 switch (entry
->acl_state
) {
879 case ARCHIVE_ENTRY_ACL_USER_OBJ
:
880 *permset
= (entry
->ae_stat
.st_mode
>> 6) & 7;
881 *type
= ARCHIVE_ENTRY_ACL_TYPE_ACCESS
;
882 *tag
= ARCHIVE_ENTRY_ACL_USER_OBJ
;
883 entry
->acl_state
= ARCHIVE_ENTRY_ACL_GROUP_OBJ
;
885 case ARCHIVE_ENTRY_ACL_GROUP_OBJ
:
886 *permset
= (entry
->ae_stat
.st_mode
>> 3) & 7;
887 *type
= ARCHIVE_ENTRY_ACL_TYPE_ACCESS
;
888 *tag
= ARCHIVE_ENTRY_ACL_GROUP_OBJ
;
889 entry
->acl_state
= ARCHIVE_ENTRY_ACL_OTHER
;
891 case ARCHIVE_ENTRY_ACL_OTHER
:
892 *permset
= entry
->ae_stat
.st_mode
& 7;
893 *type
= ARCHIVE_ENTRY_ACL_TYPE_ACCESS
;
894 *tag
= ARCHIVE_ENTRY_ACL_OTHER
;
895 entry
->acl_state
= -1;
896 entry
->acl_p
= entry
->acl_head
;
903 while (entry
->acl_p
!= NULL
&& (entry
->acl_p
->type
& want_type
) == 0)
904 entry
->acl_p
= entry
->acl_p
->next
;
905 if (entry
->acl_p
== NULL
) {
906 entry
->acl_state
= 0;
907 return (ARCHIVE_WARN
);
909 *type
= entry
->acl_p
->type
;
910 *permset
= entry
->acl_p
->permset
;
911 *tag
= entry
->acl_p
->tag
;
912 *id
= entry
->acl_p
->id
;
913 *name
= aes_get_mbs(&entry
->acl_p
->name
);
914 entry
->acl_p
= entry
->acl_p
->next
;
919 * Generate a text version of the ACL. The flags parameter controls
920 * the style of the generated ACL.
923 archive_entry_acl_text_w(struct archive_entry
*entry
, int flags
)
927 const wchar_t *wname
;
928 const wchar_t *prefix
;
934 if (entry
->acl_text_w
!= NULL
) {
935 free (entry
->acl_text_w
);
936 entry
->acl_text_w
= NULL
;
942 ap
= entry
->acl_head
;
944 if ((ap
->type
& flags
) != 0) {
946 if ((flags
& ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT
) &&
947 (ap
->type
& ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
))
948 length
+= 8; /* "default:" */
949 length
+= 5; /* tag name */
950 length
+= 1; /* colon */
951 wname
= aes_get_wcs(&ap
->name
);
953 length
+= wcslen(wname
);
954 length
++; /* colon */
955 length
+= 3; /* rwx */
956 length
+= 1; /* colon */
957 length
+= max(sizeof(uid_t
),sizeof(gid_t
)) * 3 + 1;
958 length
++; /* newline */
963 if (count
> 0 && ((flags
& ARCHIVE_ENTRY_ACL_TYPE_ACCESS
) != 0)) {
964 length
+= 10; /* "user::rwx\n" */
965 length
+= 11; /* "group::rwx\n" */
966 length
+= 11; /* "other::rwx\n" */
972 /* Now, allocate the string and actually populate it. */
973 wp
= entry
->acl_text_w
= malloc(length
* sizeof(wchar_t));
975 if ((flags
& ARCHIVE_ENTRY_ACL_TYPE_ACCESS
) != 0) {
976 append_entry_w(&wp
, NULL
, ARCHIVE_ENTRY_ACL_USER_OBJ
, NULL
,
977 entry
->ae_stat
.st_mode
& 0700, -1);
979 append_entry_w(&wp
, NULL
, ARCHIVE_ENTRY_ACL_GROUP_OBJ
, NULL
,
980 entry
->ae_stat
.st_mode
& 0070, -1);
982 append_entry_w(&wp
, NULL
, ARCHIVE_ENTRY_ACL_OTHER
, NULL
,
983 entry
->ae_stat
.st_mode
& 0007, -1);
986 ap
= entry
->acl_head
;
988 if ((ap
->type
& ARCHIVE_ENTRY_ACL_TYPE_ACCESS
) != 0) {
989 wname
= aes_get_wcs(&ap
->name
);
991 if (flags
& ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID
)
995 append_entry_w(&wp
, NULL
, ap
->tag
, wname
,
1004 if ((flags
& ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
) != 0) {
1005 if (flags
& ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT
)
1006 prefix
= L
"default:";
1009 ap
= entry
->acl_head
;
1011 while (ap
!= NULL
) {
1012 if ((ap
->type
& ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
) != 0) {
1013 wname
= aes_get_wcs(&ap
->name
);
1016 if (flags
& ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID
)
1020 append_entry_w(&wp
, prefix
, ap
->tag
,
1021 wname
, ap
->permset
, id
);
1028 return (entry
->acl_text_w
);
1032 append_id_w(wchar_t **wp
, int id
)
1035 append_id_w(wp
, id
/ 10);
1036 *(*wp
)++ = L
"0123456789"[id
% 10];
1040 append_entry_w(wchar_t **wp
, const wchar_t *prefix
, int tag
,
1041 const wchar_t *wname
, int perm
, int id
)
1043 if (prefix
!= NULL
) {
1044 wcscpy(*wp
, prefix
);
1048 case ARCHIVE_ENTRY_ACL_USER_OBJ
:
1052 case ARCHIVE_ENTRY_ACL_USER
:
1053 wcscpy(*wp
, L
"user");
1055 case ARCHIVE_ENTRY_ACL_GROUP_OBJ
:
1059 case ARCHIVE_ENTRY_ACL_GROUP
:
1060 wcscpy(*wp
, L
"group");
1062 case ARCHIVE_ENTRY_ACL_MASK
:
1063 wcscpy(*wp
, L
"mask");
1067 case ARCHIVE_ENTRY_ACL_OTHER
:
1068 wcscpy(*wp
, L
"other");
1075 if (wname
!= NULL
) {
1080 *(*wp
)++ = (perm
& 0444) ? L
'r' : L
'-';
1081 *(*wp
)++ = (perm
& 0222) ? L
'w' : L
'-';
1082 *(*wp
)++ = (perm
& 0111) ? L
'x' : L
'-';
1085 append_id_w(wp
, id
);
1091 * Parse a textual ACL. This automatically recognizes and supports
1092 * extensions described above. The 'type' argument is used to
1093 * indicate the type that should be used for any entries not
1094 * explicitly marked as "default:".
1097 __archive_entry_acl_parse_w(struct archive_entry
*entry
,
1098 const wchar_t *text
, int default_type
)
1100 int type
, tag
, permset
, id
;
1101 const wchar_t *start
, *end
;
1102 const wchar_t *name_start
, *name_end
;
1105 int namebuff_length
;
1107 name_start
= name_end
= NULL
;
1109 namebuff_length
= 0;
1111 while (text
!= NULL
&& *text
!= L
'\0') {
1112 next_field_w(&text
, &start
, &end
, &sep
);
1117 * Solaris extension: "defaultuser::rwx" is the
1118 * default ACL corresponding to "user::rwx", etc.
1120 if (end
-start
> 7 && wmemcmp(start
, L
"default", 7) == 0) {
1121 type
= ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
;
1124 type
= default_type
;
1126 if (prefix_w(start
, end
, L
"user")) {
1127 next_field_w(&text
, &start
, &end
, &sep
);
1131 tag
= ARCHIVE_ENTRY_ACL_USER
;
1135 tag
= ARCHIVE_ENTRY_ACL_USER_OBJ
;
1136 } else if (prefix_w(start
, end
, L
"group")) {
1137 next_field_w(&text
, &start
, &end
, &sep
);
1141 tag
= ARCHIVE_ENTRY_ACL_GROUP
;
1145 tag
= ARCHIVE_ENTRY_ACL_GROUP_OBJ
;
1146 } else if (prefix_w(start
, end
, L
"other")) {
1147 next_field_w(&text
, &start
, &end
, &sep
);
1152 tag
= ARCHIVE_ENTRY_ACL_OTHER
;
1153 } else if (prefix_w(start
, end
, L
"mask")) {
1154 next_field_w(&text
, &start
, &end
, &sep
);
1159 tag
= ARCHIVE_ENTRY_ACL_MASK
;
1163 next_field_w(&text
, &start
, &end
, &sep
);
1165 while (start
< end
) {
1168 permset
|= ARCHIVE_ENTRY_ACL_READ
;
1171 permset
|= ARCHIVE_ENTRY_ACL_WRITE
;
1174 permset
|= ARCHIVE_ENTRY_ACL_EXECUTE
;
1184 * Support star-compatible numeric UID/GID extension.
1185 * This extension adds a ":" followed by the numeric
1186 * ID so that "group:groupname:rwx", for example,
1187 * becomes "group:groupname:rwx:999", where 999 is the
1188 * numeric GID. This extension makes it possible, for
1189 * example, to correctly restore ACLs on a system that
1190 * might have a damaged passwd file or be disconnected
1191 * from a central NIS server. This extension is compatible
1192 * with POSIX.1e draft 17.
1194 if (sep
== L
':' && (tag
== ARCHIVE_ENTRY_ACL_USER
||
1195 tag
== ARCHIVE_ENTRY_ACL_GROUP
)) {
1196 next_field_w(&text
, &start
, &end
, &sep
);
1199 while (start
< end
&& *start
>= '0' && *start
<= '9') {
1200 if (id
> (INT_MAX
/ 10))
1209 id
= -1; /* No id specified. */
1211 /* Skip any additional entries. */
1212 while (sep
== L
':') {
1213 next_field_w(&text
, &start
, &end
, &sep
);
1216 /* Add entry to the internal list. */
1217 if (name_end
== name_start
) {
1218 archive_entry_acl_add_entry_w(entry
, type
, permset
,
1221 if (namebuff_length
<= name_end
- name_start
) {
1222 if (namebuff
!= NULL
)
1224 namebuff_length
= name_end
- name_start
+ 256;
1226 malloc(namebuff_length
* sizeof(wchar_t));
1228 wmemcpy(namebuff
, name_start
, name_end
- name_start
);
1229 namebuff
[name_end
- name_start
] = L
'\0';
1230 archive_entry_acl_add_entry_w(entry
, type
,
1231 permset
, tag
, id
, namebuff
);
1234 if (namebuff
!= NULL
)
1236 return (ARCHIVE_OK
);
1239 if (namebuff
!= NULL
)
1241 return (ARCHIVE_WARN
);
1245 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1246 * to point to just after the separator. *start points to the first
1247 * character of the matched text and *end just after the last
1248 * character of the matched identifier. In particular *end - *start
1249 * is the length of the field body, not including leading or trailing
1253 next_field_w(const wchar_t **wp
, const wchar_t **start
,
1254 const wchar_t **end
, wchar_t *sep
)
1256 /* Skip leading whitespace to find start of field. */
1257 while (**wp
== L
' ' || **wp
== L
'\t' || **wp
== L
'\n') {
1262 /* Scan for the separator. */
1263 while (**wp
!= L
'\0' && **wp
!= L
',' && **wp
!= L
':' &&
1269 /* Trim trailing whitespace to locate end of field. */
1271 while (**end
== L
' ' || **end
== L
'\t' || **end
== L
'\n') {
1276 /* Adjust scanner location. */
1282 prefix_w(const wchar_t *start
, const wchar_t *end
, const wchar_t *test
)
1287 if (*start
++ != *test
++)
1290 while (start
< end
&& *start
++ == *test
++)
1301 * Following code is modified from UC Berkeley sources, and
1302 * is subject to the following copyright notice.
1306 * Copyright (c) 1993
1307 * The Regents of the University of California. All rights reserved.
1309 * Redistribution and use in source and binary forms, with or without
1310 * modification, are permitted provided that the following conditions
1312 * 1. Redistributions of source code must retain the above copyright
1313 * notice, this list of conditions and the following disclaimer.
1314 * 2. Redistributions in binary form must reproduce the above copyright
1315 * notice, this list of conditions and the following disclaimer in the
1316 * documentation and/or other materials provided with the distribution.
1317 * 4. Neither the name of the University nor the names of its contributors
1318 * may be used to endorse or promote products derived from this software
1319 * without specific prior written permission.
1321 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1322 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1323 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1324 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1325 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1326 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1327 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1328 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1329 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1330 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1334 static struct flag
{
1336 const wchar_t *wname
;
1338 unsigned long clear
;
1340 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1342 { "nosappnd", L
"nosappnd", SF_APPEND
, 0 },
1343 { "nosappend", L
"nosappend", SF_APPEND
, 0 },
1345 #ifdef EXT2_APPEND_FL /* 'a' */
1346 { "nosappnd", L
"nosappnd", EXT2_APPEND_FL
, 0 },
1347 { "nosappend", L
"nosappend", EXT2_APPEND_FL
, 0 },
1350 { "noarch", L
"noarch", SF_ARCHIVED
, 0 },
1351 { "noarchived", L
"noarchived", SF_ARCHIVED
, 0 },
1354 { "noschg", L
"noschg", SF_IMMUTABLE
, 0 },
1355 { "noschange", L
"noschange", SF_IMMUTABLE
, 0 },
1356 { "nosimmutable", L
"nosimmutable", SF_IMMUTABLE
, 0 },
1358 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1359 { "noschg", L
"noschg", EXT2_IMMUTABLE_FL
, 0 },
1360 { "noschange", L
"noschange", EXT2_IMMUTABLE_FL
, 0 },
1361 { "nosimmutable", L
"nosimmutable", EXT2_IMMUTABLE_FL
, 0 },
1364 { "nosunlnk", L
"nosunlnk", SF_NOUNLINK
, 0 },
1365 { "nosunlink", L
"nosunlink", SF_NOUNLINK
, 0 },
1368 { "nosnapshot", L
"nosnapshot", SF_SNAPSHOT
, 0 },
1371 { "nouappnd", L
"nouappnd", UF_APPEND
, 0 },
1372 { "nouappend", L
"nouappend", UF_APPEND
, 0 },
1375 { "nouchg", L
"nouchg", UF_IMMUTABLE
, 0 },
1376 { "nouchange", L
"nouchange", UF_IMMUTABLE
, 0 },
1377 { "nouimmutable", L
"nouimmutable", UF_IMMUTABLE
, 0 },
1380 { "nodump", L
"nodump", 0, UF_NODUMP
},
1382 #ifdef EXT2_NODUMP_FL /* 'd' */
1383 { "nodump", L
"nodump", 0, EXT2_NODUMP_FL
},
1386 { "noopaque", L
"noopaque", UF_OPAQUE
, 0 },
1389 { "nouunlnk", L
"nouunlnk", UF_NOUNLINK
, 0 },
1390 { "nouunlink", L
"nouunlink", UF_NOUNLINK
, 0 },
1392 #ifdef EXT2_COMPR_FL /* 'c' */
1393 { "nocompress", L
"nocompress", EXT2_COMPR_FL
, 0 },
1396 #ifdef EXT2_NOATIME_FL /* 'A' */
1397 { "noatime", L
"noatime", 0, EXT2_NOATIME_FL
},
1399 { NULL
, NULL
, 0, 0 }
1404 * Convert file flags to a comma-separated string. If no flags
1405 * are set, return the empty string.
1408 ae_fflagstostr(unsigned long bitset
, unsigned long bitclear
)
1416 bits
= bitset
| bitclear
;
1418 for (flag
= flags
; flag
->name
!= NULL
; flag
++)
1419 if (bits
& (flag
->set
| flag
->clear
)) {
1420 length
+= strlen(flag
->name
) + 1;
1421 bits
&= ~(flag
->set
| flag
->clear
);
1426 string
= malloc(length
);
1431 for (flag
= flags
; flag
->name
!= NULL
; flag
++) {
1432 if (bitset
& flag
->set
|| bitclear
& flag
->clear
) {
1433 sp
= flag
->name
+ 2;
1434 } else if (bitset
& flag
->clear
|| bitclear
& flag
->set
) {
1438 bitset
&= ~(flag
->set
| flag
->clear
);
1439 bitclear
&= ~(flag
->set
| flag
->clear
);
1442 while ((*dp
++ = *sp
++) != '\0')
1453 * Take string of arguments and return file flags. This
1454 * version works a little differently than strtofflags(3).
1455 * In particular, it always tests every token, skipping any
1456 * unrecognized tokens. It returns a pointer to the first
1457 * unrecognized token, or NULL if every token was recognized.
1458 * This version is also const-correct and does not modify the
1462 ae_wcstofflags(const wchar_t *s
, unsigned long *setp
, unsigned long *clrp
)
1464 const wchar_t *start
, *end
;
1466 unsigned long set
, clear
;
1467 const wchar_t *failed
;
1472 /* Find start of first token. */
1473 while (*start
== L
'\t' || *start
== L
' ' || *start
== L
',')
1475 while (*start
!= L
'\0') {
1476 /* Locate end of token. */
1478 while (*end
!= L
'\0' && *end
!= L
'\t' &&
1479 *end
!= L
' ' && *end
!= L
',')
1481 for (flag
= flags
; flag
->wname
!= NULL
; flag
++) {
1482 if (wmemcmp(start
, flag
->wname
, end
- start
) == 0) {
1483 /* Matched "noXXXX", so reverse the sense. */
1487 } else if (wmemcmp(start
, flag
->wname
+ 2, end
- start
)
1489 /* Matched "XXXX", so don't reverse. */
1491 clear
|= flag
->clear
;
1495 /* Ignore unknown flag names. */
1496 if (flag
->wname
== NULL
&& failed
== NULL
)
1499 /* Find start of next token. */
1501 while (*start
== L
'\t' || *start
== L
' ' || *start
== L
',')
1511 /* Return location of first failure. */
1519 main(int argc
, char **argv
)
1521 struct archive_entry
*entry
= archive_entry_new();
1522 unsigned long set
, clear
;
1523 const wchar_t *remainder
;
1525 remainder
= archive_entry_copy_fflags_text_w(entry
, L
"nosappnd dump archive,,,,,,,");
1526 archive_entry_fflags(entry
, &set
, &clear
);
1528 wprintf(L
"set=0x%lX clear=0x%lX remainder='%ls'\n", set
, clear
, remainder
);
1530 wprintf(L
"new flags='%s'\n", archive_entry_fflags_text(entry
));