2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2016 Martin Matuska
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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$");
30 #ifdef HAVE_SYS_STAT_H
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
37 #include <sys/mkdev.h>
39 #elif MAJOR_IN_SYSMACROS
40 #include <sys/sysmacros.h>
49 #ifdef HAVE_LINUX_FS_H
50 #include <linux/fs.h> /* for Linux file flags */
53 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
54 * As the include guards don't agree, the order of include is important.
56 #ifdef HAVE_LINUX_EXT2_FS_H
57 #include <linux/ext2_fs.h> /* for Linux file flags */
59 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
60 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
75 #include "archive_acl_private.h"
76 #include "archive_entry.h"
77 #include "archive_entry_locale.h"
78 #include "archive_private.h"
79 #include "archive_entry_private.h"
81 #if !defined(HAVE_MAJOR) && !defined(major)
82 /* Replacement for major/minor/makedev. */
83 #define major(x) ((int)(0x00ff & ((x) >> 8)))
84 #define minor(x) ((int)(0xffff00ff & (x)))
85 #define makedev(maj,min) ((0xff00 & ((maj)<<8)) | (0xffff00ff & (min)))
88 /* Play games to come up with a suitable makedev() definition. */
91 #include <sys/netmgr.h>
92 #define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
94 /* There's a "makedev" macro. */
95 #define ae_makedev(maj, min) makedev((maj), (min))
96 #elif defined mkdev || ((defined _WIN32 || defined __WIN32__) && !defined(__CYGWIN__))
98 #define ae_makedev(maj, min) mkdev((maj), (min))
100 /* There's a "makedev" function. */
101 #define ae_makedev(maj, min) makedev((maj), (min))
105 * This adjustment is needed to support the following idiom for adding
106 * 1000ns to the stored time:
107 * archive_entry_set_atime(archive_entry_atime(),
108 * archive_entry_atime_nsec() + 1000)
109 * The additional if() here compensates for ambiguity in the C standard,
110 * which permits two possible interpretations of a % b when a is negative.
112 #define FIX_NS(t,ns) \
114 t += ns / 1000000000; \
116 if (ns < 0) { --t; ns += 1000000000; } \
119 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
120 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
121 unsigned long *setp, unsigned long *clrp);
122 static const char *ae_strtofflags(const char *stringp,
123 unsigned long *setp, unsigned long *clrp);
126 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
129 while ((*s1 = *s2) != L'\0')
135 static size_t wcslen(const wchar_t *s)
137 const wchar_t *p = s;
144 /* Good enough for simple equality testing, but not for sorting. */
145 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
148 /****************************************************************************
152 ****************************************************************************/
154 struct archive_entry *
155 archive_entry_clear(struct archive_entry *entry)
159 archive_mstring_clean(&entry->ae_fflags_text);
160 archive_mstring_clean(&entry->ae_gname);
161 archive_mstring_clean(&entry->ae_hardlink);
162 archive_mstring_clean(&entry->ae_pathname);
163 archive_mstring_clean(&entry->ae_sourcepath);
164 archive_mstring_clean(&entry->ae_symlink);
165 archive_mstring_clean(&entry->ae_uname);
166 archive_entry_copy_mac_metadata(entry, NULL, 0);
167 archive_acl_clear(&entry->acl);
168 archive_entry_xattr_clear(entry);
169 archive_entry_sparse_clear(entry);
171 entry->ae_symlink_type = AE_SYMLINK_TYPE_UNDEFINED;
172 memset(entry, 0, sizeof(*entry));
176 struct archive_entry *
177 archive_entry_clone(struct archive_entry *entry)
179 struct archive_entry *entry2;
181 struct ae_sparse *sp;
185 /* Allocate new structure and copy over all of the fields. */
186 /* TODO: Should we copy the archive over? Or require a new archive
188 entry2 = archive_entry_new2(entry->archive);
191 entry2->ae_stat = entry->ae_stat;
192 entry2->ae_fflags_set = entry->ae_fflags_set;
193 entry2->ae_fflags_clear = entry->ae_fflags_clear;
195 /* TODO: XXX If clone can have a different archive, what do we do here if
196 * character sets are different? XXX */
197 archive_mstring_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
198 archive_mstring_copy(&entry2->ae_gname, &entry->ae_gname);
199 archive_mstring_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
200 archive_mstring_copy(&entry2->ae_pathname, &entry->ae_pathname);
201 archive_mstring_copy(&entry2->ae_sourcepath, &entry->ae_sourcepath);
202 archive_mstring_copy(&entry2->ae_symlink, &entry->ae_symlink);
203 entry2->ae_set = entry->ae_set;
204 archive_mstring_copy(&entry2->ae_uname, &entry->ae_uname);
206 /* Copy symlink type */
207 entry2->ae_symlink_type = entry->ae_symlink_type;
209 /* Copy encryption status */
210 entry2->encryption = entry->encryption;
212 /* Copy ACL data over. */
213 archive_acl_copy(&entry2->acl, &entry->acl);
215 /* Copy Mac OS metadata. */
216 p = archive_entry_mac_metadata(entry, &s);
217 archive_entry_copy_mac_metadata(entry2, p, s);
219 /* Copy xattr data over. */
220 xp = entry->xattr_head;
222 archive_entry_xattr_add_entry(entry2,
223 xp->name, xp->value, xp->size);
227 /* Copy sparse data over. */
228 sp = entry->sparse_head;
230 archive_entry_sparse_add_entry(entry2,
231 sp->offset, sp->length);
239 archive_entry_free(struct archive_entry *entry)
241 archive_entry_clear(entry);
245 struct archive_entry *
246 archive_entry_new(void)
248 return archive_entry_new2(NULL);
251 struct archive_entry *
252 archive_entry_new2(struct archive *a)
254 struct archive_entry *entry;
256 entry = (struct archive_entry *)calloc(1, sizeof(*entry));
260 entry->ae_symlink_type = AE_SYMLINK_TYPE_UNDEFINED;
265 * Functions for reading fields from an archive_entry.
269 archive_entry_atime(struct archive_entry *entry)
271 return (entry->ae_stat.aest_atime);
275 archive_entry_atime_nsec(struct archive_entry *entry)
277 return (entry->ae_stat.aest_atime_nsec);
281 archive_entry_atime_is_set(struct archive_entry *entry)
283 return (entry->ae_set & AE_SET_ATIME);
287 archive_entry_birthtime(struct archive_entry *entry)
289 return (entry->ae_stat.aest_birthtime);
293 archive_entry_birthtime_nsec(struct archive_entry *entry)
295 return (entry->ae_stat.aest_birthtime_nsec);
299 archive_entry_birthtime_is_set(struct archive_entry *entry)
301 return (entry->ae_set & AE_SET_BIRTHTIME);
305 archive_entry_ctime(struct archive_entry *entry)
307 return (entry->ae_stat.aest_ctime);
311 archive_entry_ctime_is_set(struct archive_entry *entry)
313 return (entry->ae_set & AE_SET_CTIME);
317 archive_entry_ctime_nsec(struct archive_entry *entry)
319 return (entry->ae_stat.aest_ctime_nsec);
323 archive_entry_dev(struct archive_entry *entry)
325 if (entry->ae_stat.aest_dev_is_broken_down)
326 return ae_makedev(entry->ae_stat.aest_devmajor,
327 entry->ae_stat.aest_devminor);
329 return (entry->ae_stat.aest_dev);
333 archive_entry_dev_is_set(struct archive_entry *entry)
335 return (entry->ae_set & AE_SET_DEV);
339 archive_entry_devmajor(struct archive_entry *entry)
341 if (entry->ae_stat.aest_dev_is_broken_down)
342 return (entry->ae_stat.aest_devmajor);
344 return major(entry->ae_stat.aest_dev);
348 archive_entry_devminor(struct archive_entry *entry)
350 if (entry->ae_stat.aest_dev_is_broken_down)
351 return (entry->ae_stat.aest_devminor);
353 return minor(entry->ae_stat.aest_dev);
357 archive_entry_filetype(struct archive_entry *entry)
359 return (AE_IFMT & entry->acl.mode);
363 archive_entry_fflags(struct archive_entry *entry,
364 unsigned long *set, unsigned long *clear)
366 *set = entry->ae_fflags_set;
367 *clear = entry->ae_fflags_clear;
371 * Note: if text was provided, this just returns that text. If you
372 * really need the text to be rebuilt in a canonical form, set the
373 * text, ask for the bitmaps, then set the bitmaps. (Setting the
374 * bitmaps clears any stored text.) This design is deliberate: if
375 * we're editing archives, we don't want to discard flags just because
376 * they aren't supported on the current system. The bitmap<->text
377 * conversions are platform-specific (see below).
380 archive_entry_fflags_text(struct archive_entry *entry)
385 if (archive_mstring_get_mbs(entry->archive,
386 &entry->ae_fflags_text, &f) == 0) {
389 } else if (errno == ENOMEM)
390 __archive_errx(1, "No memory");
392 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
395 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
399 archive_mstring_copy_mbs(&entry->ae_fflags_text, p);
401 if (archive_mstring_get_mbs(entry->archive,
402 &entry->ae_fflags_text, &f) == 0)
405 __archive_errx(1, "No memory");
410 archive_entry_gid(struct archive_entry *entry)
412 return (entry->ae_stat.aest_gid);
416 archive_entry_gname(struct archive_entry *entry)
419 if (archive_mstring_get_mbs(entry->archive, &entry->ae_gname, &p) == 0)
422 __archive_errx(1, "No memory");
427 archive_entry_gname_utf8(struct archive_entry *entry)
430 if (archive_mstring_get_utf8(entry->archive, &entry->ae_gname, &p) == 0)
433 __archive_errx(1, "No memory");
439 archive_entry_gname_w(struct archive_entry *entry)
442 if (archive_mstring_get_wcs(entry->archive, &entry->ae_gname, &p) == 0)
445 __archive_errx(1, "No memory");
450 _archive_entry_gname_l(struct archive_entry *entry,
451 const char **p, size_t *len, struct archive_string_conv *sc)
453 return (archive_mstring_get_mbs_l(&entry->ae_gname, p, len, sc));
457 archive_entry_hardlink(struct archive_entry *entry)
460 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
462 if (archive_mstring_get_mbs(
463 entry->archive, &entry->ae_hardlink, &p) == 0)
466 __archive_errx(1, "No memory");
471 archive_entry_hardlink_utf8(struct archive_entry *entry)
474 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
476 if (archive_mstring_get_utf8(
477 entry->archive, &entry->ae_hardlink, &p) == 0)
480 __archive_errx(1, "No memory");
485 archive_entry_hardlink_w(struct archive_entry *entry)
488 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
490 if (archive_mstring_get_wcs(
491 entry->archive, &entry->ae_hardlink, &p) == 0)
494 __archive_errx(1, "No memory");
499 _archive_entry_hardlink_l(struct archive_entry *entry,
500 const char **p, size_t *len, struct archive_string_conv *sc)
502 if ((entry->ae_set & AE_SET_HARDLINK) == 0) {
507 return (archive_mstring_get_mbs_l(&entry->ae_hardlink, p, len, sc));
511 archive_entry_ino(struct archive_entry *entry)
513 return (entry->ae_stat.aest_ino);
517 archive_entry_ino_is_set(struct archive_entry *entry)
519 return (entry->ae_set & AE_SET_INO);
523 archive_entry_ino64(struct archive_entry *entry)
525 return (entry->ae_stat.aest_ino);
529 archive_entry_mode(struct archive_entry *entry)
531 return (entry->acl.mode);
535 archive_entry_mtime(struct archive_entry *entry)
537 return (entry->ae_stat.aest_mtime);
541 archive_entry_mtime_nsec(struct archive_entry *entry)
543 return (entry->ae_stat.aest_mtime_nsec);
547 archive_entry_mtime_is_set(struct archive_entry *entry)
549 return (entry->ae_set & AE_SET_MTIME);
553 archive_entry_nlink(struct archive_entry *entry)
555 return (entry->ae_stat.aest_nlink);
559 archive_entry_pathname(struct archive_entry *entry)
562 if (archive_mstring_get_mbs(
563 entry->archive, &entry->ae_pathname, &p) == 0)
566 __archive_errx(1, "No memory");
571 archive_entry_pathname_utf8(struct archive_entry *entry)
574 if (archive_mstring_get_utf8(
575 entry->archive, &entry->ae_pathname, &p) == 0)
578 __archive_errx(1, "No memory");
583 archive_entry_pathname_w(struct archive_entry *entry)
586 if (archive_mstring_get_wcs(
587 entry->archive, &entry->ae_pathname, &p) == 0)
590 __archive_errx(1, "No memory");
595 _archive_entry_pathname_l(struct archive_entry *entry,
596 const char **p, size_t *len, struct archive_string_conv *sc)
598 return (archive_mstring_get_mbs_l(&entry->ae_pathname, p, len, sc));
602 archive_entry_perm(struct archive_entry *entry)
604 return (~AE_IFMT & entry->acl.mode);
608 archive_entry_rdev(struct archive_entry *entry)
610 if (entry->ae_stat.aest_rdev_is_broken_down)
611 return ae_makedev(entry->ae_stat.aest_rdevmajor,
612 entry->ae_stat.aest_rdevminor);
614 return (entry->ae_stat.aest_rdev);
618 archive_entry_rdevmajor(struct archive_entry *entry)
620 if (entry->ae_stat.aest_rdev_is_broken_down)
621 return (entry->ae_stat.aest_rdevmajor);
623 return major(entry->ae_stat.aest_rdev);
627 archive_entry_rdevminor(struct archive_entry *entry)
629 if (entry->ae_stat.aest_rdev_is_broken_down)
630 return (entry->ae_stat.aest_rdevminor);
632 return minor(entry->ae_stat.aest_rdev);
636 archive_entry_size(struct archive_entry *entry)
638 return (entry->ae_stat.aest_size);
642 archive_entry_size_is_set(struct archive_entry *entry)
644 return (entry->ae_set & AE_SET_SIZE);
648 archive_entry_sourcepath(struct archive_entry *entry)
651 if (archive_mstring_get_mbs(
652 entry->archive, &entry->ae_sourcepath, &p) == 0)
655 __archive_errx(1, "No memory");
660 archive_entry_sourcepath_w(struct archive_entry *entry)
663 if (archive_mstring_get_wcs(
664 entry->archive, &entry->ae_sourcepath, &p) == 0)
670 archive_entry_symlink(struct archive_entry *entry)
673 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
675 if (archive_mstring_get_mbs(
676 entry->archive, &entry->ae_symlink, &p) == 0)
679 __archive_errx(1, "No memory");
684 archive_entry_symlink_type(struct archive_entry *entry)
686 return (entry->ae_symlink_type);
690 archive_entry_symlink_utf8(struct archive_entry *entry)
693 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
695 if (archive_mstring_get_utf8(
696 entry->archive, &entry->ae_symlink, &p) == 0)
699 __archive_errx(1, "No memory");
704 archive_entry_symlink_w(struct archive_entry *entry)
707 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
709 if (archive_mstring_get_wcs(
710 entry->archive, &entry->ae_symlink, &p) == 0)
713 __archive_errx(1, "No memory");
718 _archive_entry_symlink_l(struct archive_entry *entry,
719 const char **p, size_t *len, struct archive_string_conv *sc)
721 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
726 return (archive_mstring_get_mbs_l( &entry->ae_symlink, p, len, sc));
730 archive_entry_uid(struct archive_entry *entry)
732 return (entry->ae_stat.aest_uid);
736 archive_entry_uname(struct archive_entry *entry)
739 if (archive_mstring_get_mbs(entry->archive, &entry->ae_uname, &p) == 0)
742 __archive_errx(1, "No memory");
747 archive_entry_uname_utf8(struct archive_entry *entry)
750 if (archive_mstring_get_utf8(entry->archive, &entry->ae_uname, &p) == 0)
753 __archive_errx(1, "No memory");
758 archive_entry_uname_w(struct archive_entry *entry)
761 if (archive_mstring_get_wcs(entry->archive, &entry->ae_uname, &p) == 0)
764 __archive_errx(1, "No memory");
769 _archive_entry_uname_l(struct archive_entry *entry,
770 const char **p, size_t *len, struct archive_string_conv *sc)
772 return (archive_mstring_get_mbs_l(&entry->ae_uname, p, len, sc));
776 archive_entry_is_data_encrypted(struct archive_entry *entry)
778 return ((entry->encryption & AE_ENCRYPTION_DATA) == AE_ENCRYPTION_DATA);
782 archive_entry_is_metadata_encrypted(struct archive_entry *entry)
784 return ((entry->encryption & AE_ENCRYPTION_METADATA) == AE_ENCRYPTION_METADATA);
788 archive_entry_is_encrypted(struct archive_entry *entry)
790 return (entry->encryption & (AE_ENCRYPTION_DATA|AE_ENCRYPTION_METADATA));
794 * Functions to set archive_entry properties.
798 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
800 entry->stat_valid = 0;
801 entry->acl.mode &= ~AE_IFMT;
802 entry->acl.mode |= AE_IFMT & type;
806 archive_entry_set_fflags(struct archive_entry *entry,
807 unsigned long set, unsigned long clear)
809 archive_mstring_clean(&entry->ae_fflags_text);
810 entry->ae_fflags_set = set;
811 entry->ae_fflags_clear = clear;
815 archive_entry_copy_fflags_text(struct archive_entry *entry,
818 archive_mstring_copy_mbs(&entry->ae_fflags_text, flags);
819 return (ae_strtofflags(flags,
820 &entry->ae_fflags_set, &entry->ae_fflags_clear));
824 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
825 const wchar_t *flags)
827 archive_mstring_copy_wcs(&entry->ae_fflags_text, flags);
828 return (ae_wcstofflags(flags,
829 &entry->ae_fflags_set, &entry->ae_fflags_clear));
833 archive_entry_set_gid(struct archive_entry *entry, la_int64_t g)
835 entry->stat_valid = 0;
836 entry->ae_stat.aest_gid = g;
840 archive_entry_set_gname(struct archive_entry *entry, const char *name)
842 archive_mstring_copy_mbs(&entry->ae_gname, name);
846 archive_entry_set_gname_utf8(struct archive_entry *entry, const char *name)
848 archive_mstring_copy_utf8(&entry->ae_gname, name);
852 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
854 archive_mstring_copy_mbs(&entry->ae_gname, name);
858 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
860 archive_mstring_copy_wcs(&entry->ae_gname, name);
864 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
866 if (archive_mstring_update_utf8(entry->archive,
867 &entry->ae_gname, name) == 0)
870 __archive_errx(1, "No memory");
875 _archive_entry_copy_gname_l(struct archive_entry *entry,
876 const char *name, size_t len, struct archive_string_conv *sc)
878 return (archive_mstring_copy_mbs_len_l(&entry->ae_gname, name, len, sc));
882 archive_entry_set_ino(struct archive_entry *entry, la_int64_t ino)
884 entry->stat_valid = 0;
885 entry->ae_set |= AE_SET_INO;
886 entry->ae_stat.aest_ino = ino;
890 archive_entry_set_ino64(struct archive_entry *entry, la_int64_t ino)
892 entry->stat_valid = 0;
893 entry->ae_set |= AE_SET_INO;
894 entry->ae_stat.aest_ino = ino;
898 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
900 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
902 entry->ae_set |= AE_SET_HARDLINK;
904 entry->ae_set &= ~AE_SET_HARDLINK;
908 archive_entry_set_hardlink_utf8(struct archive_entry *entry, const char *target)
910 archive_mstring_copy_utf8(&entry->ae_hardlink, target);
912 entry->ae_set |= AE_SET_HARDLINK;
914 entry->ae_set &= ~AE_SET_HARDLINK;
918 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
920 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
922 entry->ae_set |= AE_SET_HARDLINK;
924 entry->ae_set &= ~AE_SET_HARDLINK;
928 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
930 archive_mstring_copy_wcs(&entry->ae_hardlink, target);
932 entry->ae_set |= AE_SET_HARDLINK;
934 entry->ae_set &= ~AE_SET_HARDLINK;
938 archive_entry_update_hardlink_utf8(struct archive_entry *entry, const char *target)
941 entry->ae_set |= AE_SET_HARDLINK;
943 entry->ae_set &= ~AE_SET_HARDLINK;
944 if (archive_mstring_update_utf8(entry->archive,
945 &entry->ae_hardlink, target) == 0)
948 __archive_errx(1, "No memory");
953 _archive_entry_copy_hardlink_l(struct archive_entry *entry,
954 const char *target, size_t len, struct archive_string_conv *sc)
958 r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
960 if (target != NULL && r == 0)
961 entry->ae_set |= AE_SET_HARDLINK;
963 entry->ae_set &= ~AE_SET_HARDLINK;
968 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
971 entry->stat_valid = 0;
972 entry->ae_set |= AE_SET_ATIME;
973 entry->ae_stat.aest_atime = t;
974 entry->ae_stat.aest_atime_nsec = ns;
978 archive_entry_unset_atime(struct archive_entry *entry)
980 archive_entry_set_atime(entry, 0, 0);
981 entry->ae_set &= ~AE_SET_ATIME;
985 archive_entry_set_birthtime(struct archive_entry *entry, time_t t, long ns)
988 entry->stat_valid = 0;
989 entry->ae_set |= AE_SET_BIRTHTIME;
990 entry->ae_stat.aest_birthtime = t;
991 entry->ae_stat.aest_birthtime_nsec = ns;
995 archive_entry_unset_birthtime(struct archive_entry *entry)
997 archive_entry_set_birthtime(entry, 0, 0);
998 entry->ae_set &= ~AE_SET_BIRTHTIME;
1002 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
1005 entry->stat_valid = 0;
1006 entry->ae_set |= AE_SET_CTIME;
1007 entry->ae_stat.aest_ctime = t;
1008 entry->ae_stat.aest_ctime_nsec = ns;
1012 archive_entry_unset_ctime(struct archive_entry *entry)
1014 archive_entry_set_ctime(entry, 0, 0);
1015 entry->ae_set &= ~AE_SET_CTIME;
1019 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
1021 entry->stat_valid = 0;
1022 entry->ae_set |= AE_SET_DEV;
1023 entry->ae_stat.aest_dev_is_broken_down = 0;
1024 entry->ae_stat.aest_dev = d;
1028 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
1030 entry->stat_valid = 0;
1031 entry->ae_set |= AE_SET_DEV;
1032 entry->ae_stat.aest_dev_is_broken_down = 1;
1033 entry->ae_stat.aest_devmajor = m;
1037 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
1039 entry->stat_valid = 0;
1040 entry->ae_set |= AE_SET_DEV;
1041 entry->ae_stat.aest_dev_is_broken_down = 1;
1042 entry->ae_stat.aest_devminor = m;
1045 /* Set symlink if symlink is already set, else set hardlink. */
1047 archive_entry_set_link(struct archive_entry *entry, const char *target)
1049 if (entry->ae_set & AE_SET_SYMLINK)
1050 archive_mstring_copy_mbs(&entry->ae_symlink, target);
1052 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
1056 archive_entry_set_link_utf8(struct archive_entry *entry, const char *target)
1058 if (entry->ae_set & AE_SET_SYMLINK)
1059 archive_mstring_copy_utf8(&entry->ae_symlink, target);
1061 archive_mstring_copy_utf8(&entry->ae_hardlink, target);
1064 /* Set symlink if symlink is already set, else set hardlink. */
1066 archive_entry_copy_link(struct archive_entry *entry, const char *target)
1068 if (entry->ae_set & AE_SET_SYMLINK)
1069 archive_mstring_copy_mbs(&entry->ae_symlink, target);
1071 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
1074 /* Set symlink if symlink is already set, else set hardlink. */
1076 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
1078 if (entry->ae_set & AE_SET_SYMLINK)
1079 archive_mstring_copy_wcs(&entry->ae_symlink, target);
1081 archive_mstring_copy_wcs(&entry->ae_hardlink, target);
1085 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
1088 if (entry->ae_set & AE_SET_SYMLINK)
1089 r = archive_mstring_update_utf8(entry->archive,
1090 &entry->ae_symlink, target);
1092 r = archive_mstring_update_utf8(entry->archive,
1093 &entry->ae_hardlink, target);
1096 if (errno == ENOMEM)
1097 __archive_errx(1, "No memory");
1102 _archive_entry_copy_link_l(struct archive_entry *entry,
1103 const char *target, size_t len, struct archive_string_conv *sc)
1107 if (entry->ae_set & AE_SET_SYMLINK)
1108 r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
1111 r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
1117 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
1119 entry->stat_valid = 0;
1120 entry->acl.mode = m;
1124 archive_entry_set_mtime(struct archive_entry *entry, time_t t, long ns)
1127 entry->stat_valid = 0;
1128 entry->ae_set |= AE_SET_MTIME;
1129 entry->ae_stat.aest_mtime = t;
1130 entry->ae_stat.aest_mtime_nsec = ns;
1134 archive_entry_unset_mtime(struct archive_entry *entry)
1136 archive_entry_set_mtime(entry, 0, 0);
1137 entry->ae_set &= ~AE_SET_MTIME;
1141 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
1143 entry->stat_valid = 0;
1144 entry->ae_stat.aest_nlink = nlink;
1148 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
1150 archive_mstring_copy_mbs(&entry->ae_pathname, name);
1154 archive_entry_set_pathname_utf8(struct archive_entry *entry, const char *name)
1156 archive_mstring_copy_utf8(&entry->ae_pathname, name);
1160 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
1162 archive_mstring_copy_mbs(&entry->ae_pathname, name);
1166 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
1168 archive_mstring_copy_wcs(&entry->ae_pathname, name);
1172 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
1174 if (archive_mstring_update_utf8(entry->archive,
1175 &entry->ae_pathname, name) == 0)
1177 if (errno == ENOMEM)
1178 __archive_errx(1, "No memory");
1183 _archive_entry_copy_pathname_l(struct archive_entry *entry,
1184 const char *name, size_t len, struct archive_string_conv *sc)
1186 return (archive_mstring_copy_mbs_len_l(&entry->ae_pathname,
1191 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
1193 entry->stat_valid = 0;
1194 entry->acl.mode &= AE_IFMT;
1195 entry->acl.mode |= ~AE_IFMT & p;
1199 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
1201 entry->stat_valid = 0;
1202 entry->ae_stat.aest_rdev = m;
1203 entry->ae_stat.aest_rdev_is_broken_down = 0;
1207 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
1209 entry->stat_valid = 0;
1210 entry->ae_stat.aest_rdev_is_broken_down = 1;
1211 entry->ae_stat.aest_rdevmajor = m;
1215 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
1217 entry->stat_valid = 0;
1218 entry->ae_stat.aest_rdev_is_broken_down = 1;
1219 entry->ae_stat.aest_rdevminor = m;
1223 archive_entry_set_size(struct archive_entry *entry, la_int64_t s)
1225 entry->stat_valid = 0;
1226 entry->ae_stat.aest_size = s;
1227 entry->ae_set |= AE_SET_SIZE;
1231 archive_entry_unset_size(struct archive_entry *entry)
1233 archive_entry_set_size(entry, 0);
1234 entry->ae_set &= ~AE_SET_SIZE;
1238 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1240 archive_mstring_copy_mbs(&entry->ae_sourcepath, path);
1244 archive_entry_copy_sourcepath_w(struct archive_entry *entry, const wchar_t *path)
1246 archive_mstring_copy_wcs(&entry->ae_sourcepath, path);
1250 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1252 archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
1253 if (linkname != NULL)
1254 entry->ae_set |= AE_SET_SYMLINK;
1256 entry->ae_set &= ~AE_SET_SYMLINK;
1260 archive_entry_set_symlink_type(struct archive_entry *entry, int type)
1262 entry->ae_symlink_type = type;
1266 archive_entry_set_symlink_utf8(struct archive_entry *entry, const char *linkname)
1268 archive_mstring_copy_utf8(&entry->ae_symlink, linkname);
1269 if (linkname != NULL)
1270 entry->ae_set |= AE_SET_SYMLINK;
1272 entry->ae_set &= ~AE_SET_SYMLINK;
1276 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1278 archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
1279 if (linkname != NULL)
1280 entry->ae_set |= AE_SET_SYMLINK;
1282 entry->ae_set &= ~AE_SET_SYMLINK;
1286 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1288 archive_mstring_copy_wcs(&entry->ae_symlink, linkname);
1289 if (linkname != NULL)
1290 entry->ae_set |= AE_SET_SYMLINK;
1292 entry->ae_set &= ~AE_SET_SYMLINK;
1296 archive_entry_update_symlink_utf8(struct archive_entry *entry, const char *linkname)
1298 if (linkname != NULL)
1299 entry->ae_set |= AE_SET_SYMLINK;
1301 entry->ae_set &= ~AE_SET_SYMLINK;
1302 if (archive_mstring_update_utf8(entry->archive,
1303 &entry->ae_symlink, linkname) == 0)
1305 if (errno == ENOMEM)
1306 __archive_errx(1, "No memory");
1311 _archive_entry_copy_symlink_l(struct archive_entry *entry,
1312 const char *linkname, size_t len, struct archive_string_conv *sc)
1316 r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
1318 if (linkname != NULL && r == 0)
1319 entry->ae_set |= AE_SET_SYMLINK;
1321 entry->ae_set &= ~AE_SET_SYMLINK;
1326 archive_entry_set_uid(struct archive_entry *entry, la_int64_t u)
1328 entry->stat_valid = 0;
1329 entry->ae_stat.aest_uid = u;
1333 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1335 archive_mstring_copy_mbs(&entry->ae_uname, name);
1339 archive_entry_set_uname_utf8(struct archive_entry *entry, const char *name)
1341 archive_mstring_copy_utf8(&entry->ae_uname, name);
1345 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1347 archive_mstring_copy_mbs(&entry->ae_uname, name);
1351 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1353 archive_mstring_copy_wcs(&entry->ae_uname, name);
1357 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1359 if (archive_mstring_update_utf8(entry->archive,
1360 &entry->ae_uname, name) == 0)
1362 if (errno == ENOMEM)
1363 __archive_errx(1, "No memory");
1368 archive_entry_set_is_data_encrypted(struct archive_entry *entry, char is_encrypted)
1371 entry->encryption |= AE_ENCRYPTION_DATA;
1373 entry->encryption &= ~AE_ENCRYPTION_DATA;
1378 archive_entry_set_is_metadata_encrypted(struct archive_entry *entry, char is_encrypted)
1381 entry->encryption |= AE_ENCRYPTION_METADATA;
1383 entry->encryption &= ~AE_ENCRYPTION_METADATA;
1388 _archive_entry_copy_uname_l(struct archive_entry *entry,
1389 const char *name, size_t len, struct archive_string_conv *sc)
1391 return (archive_mstring_copy_mbs_len_l(&entry->ae_uname,
1396 archive_entry_mac_metadata(struct archive_entry *entry, size_t *s)
1398 *s = entry->mac_metadata_size;
1399 return entry->mac_metadata;
1403 archive_entry_copy_mac_metadata(struct archive_entry *entry,
1404 const void *p, size_t s)
1406 free(entry->mac_metadata);
1407 if (p == NULL || s == 0) {
1408 entry->mac_metadata = NULL;
1409 entry->mac_metadata_size = 0;
1411 entry->mac_metadata_size = s;
1412 entry->mac_metadata = malloc(s);
1413 if (entry->mac_metadata == NULL)
1415 memcpy(entry->mac_metadata, p, s);
1420 * ACL management. The following would, of course, be a lot simpler
1421 * if: 1) the last draft of POSIX.1e were a really thorough and
1422 * complete standard that addressed the needs of ACL archiving and 2)
1423 * everyone followed it faithfully. Alas, neither is true, so the
1424 * following is a lot more complex than might seem necessary to the
1428 struct archive_acl *
1429 archive_entry_acl(struct archive_entry *entry)
1435 archive_entry_acl_clear(struct archive_entry *entry)
1437 archive_acl_clear(&entry->acl);
1441 * Add a single ACL entry to the internal list of ACL data.
1444 archive_entry_acl_add_entry(struct archive_entry *entry,
1445 int type, int permset, int tag, int id, const char *name)
1447 return archive_acl_add_entry(&entry->acl, type, permset, tag, id, name);
1451 * As above, but with a wide-character name.
1454 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1455 int type, int permset, int tag, int id, const wchar_t *name)
1457 return archive_acl_add_entry_w_len(&entry->acl,
1458 type, permset, tag, id, name, wcslen(name));
1462 * Return a bitmask of ACL types in an archive entry ACL list
1465 archive_entry_acl_types(struct archive_entry *entry)
1467 return (archive_acl_types(&entry->acl));
1471 * Return a count of entries matching "want_type".
1474 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1476 return archive_acl_count(&entry->acl, want_type);
1480 * Prepare for reading entries from the ACL data. Returns a count
1481 * of entries matching "want_type", or zero if there are no
1482 * non-extended ACL entries of that type.
1485 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1487 return archive_acl_reset(&entry->acl, want_type);
1491 * Return the next ACL entry in the list. Fake entries for the
1492 * standard permissions and include them in the returned list.
1495 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1496 int *permset, int *tag, int *id, const char **name)
1499 r = archive_acl_next(entry->archive, &entry->acl, want_type, type,
1500 permset, tag, id, name);
1501 if (r == ARCHIVE_FATAL && errno == ENOMEM)
1502 __archive_errx(1, "No memory");
1507 * Generate a text version of the ACL. The flags parameter controls
1508 * the style of the generated ACL.
1511 archive_entry_acl_to_text_w(struct archive_entry *entry, la_ssize_t *len,
1514 return (archive_acl_to_text_w(&entry->acl, len, flags,
1519 archive_entry_acl_to_text(struct archive_entry *entry, la_ssize_t *len,
1522 return (archive_acl_to_text_l(&entry->acl, len, flags, NULL));
1526 _archive_entry_acl_to_text_l(struct archive_entry *entry, ssize_t *len,
1527 int flags, struct archive_string_conv *sc)
1529 return (archive_acl_to_text_l(&entry->acl, len, flags, sc));
1536 archive_entry_acl_from_text_w(struct archive_entry *entry,
1537 const wchar_t *wtext, int type)
1539 return (archive_acl_from_text_w(&entry->acl, wtext, type));
1543 archive_entry_acl_from_text(struct archive_entry *entry,
1544 const char *text, int type)
1546 return (archive_acl_from_text_l(&entry->acl, text, type, NULL));
1550 _archive_entry_acl_from_text_l(struct archive_entry *entry, const char *text,
1551 int type, struct archive_string_conv *sc)
1553 return (archive_acl_from_text_l(&entry->acl, text, type, sc));
1558 archive_entry_acl_text_compat(int *flags)
1560 if ((*flags & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) == 0)
1563 /* ABI compat with old ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID */
1564 if ((*flags & OLD_ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID) != 0)
1565 *flags |= ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID;
1567 /* ABI compat with old ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT */
1568 if ((*flags & OLD_ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) != 0)
1569 *flags |= ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT;
1571 *flags |= ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA;
1578 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1580 free(entry->acl.acl_text_w);
1581 entry->acl.acl_text_w = NULL;
1582 if (archive_entry_acl_text_compat(&flags) == 0)
1583 entry->acl.acl_text_w = archive_acl_to_text_w(&entry->acl,
1584 NULL, flags, entry->archive);
1585 return (entry->acl.acl_text_w);
1590 archive_entry_acl_text(struct archive_entry *entry, int flags)
1592 free(entry->acl.acl_text);
1593 entry->acl.acl_text = NULL;
1594 if (archive_entry_acl_text_compat(&flags) == 0)
1595 entry->acl.acl_text = archive_acl_to_text_l(&entry->acl, NULL,
1598 return (entry->acl.acl_text);
1603 _archive_entry_acl_text_l(struct archive_entry *entry, int flags,
1604 const char **acl_text, size_t *len, struct archive_string_conv *sc)
1606 free(entry->acl.acl_text);
1607 entry->acl.acl_text = NULL;
1609 if (archive_entry_acl_text_compat(&flags) == 0)
1610 entry->acl.acl_text = archive_acl_to_text_l(&entry->acl,
1611 (ssize_t *)len, flags, sc);
1613 *acl_text = entry->acl.acl_text;
1619 * Following code is modified from UC Berkeley sources, and
1620 * is subject to the following copyright notice.
1624 * Copyright (c) 1993
1625 * The Regents of the University of California. All rights reserved.
1627 * Redistribution and use in source and binary forms, with or without
1628 * modification, are permitted provided that the following conditions
1630 * 1. Redistributions of source code must retain the above copyright
1631 * notice, this list of conditions and the following disclaimer.
1632 * 2. Redistributions in binary form must reproduce the above copyright
1633 * notice, this list of conditions and the following disclaimer in the
1634 * documentation and/or other materials provided with the distribution.
1635 * 4. Neither the name of the University nor the names of its contributors
1636 * may be used to endorse or promote products derived from this software
1637 * without specific prior written permission.
1639 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1640 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1641 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1642 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1643 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1644 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1645 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1646 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1647 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1648 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1653 * Supported file flags on FreeBSD and Mac OS:
1654 * sappnd,sappend SF_APPEND
1655 * arch,archived SF_ARCHIVED
1656 * schg,schange,simmutable SF_IMMUTABLE
1657 * sunlnk,sunlink SF_NOUNLINK (FreeBSD only)
1658 * uappnd,uappend UF_APPEND
1659 * compressed UF_COMPRESSED (Mac OS only)
1660 * hidden,uhidden UF_HIDDEN
1661 * uchg,uchange,uimmutable UF_IMMUTABLE
1663 * uunlnk,uunlink UF_NOUNLINK (FreeBSD only)
1664 * offline,uoffline UF_OFFLINE (FreeBSD only)
1666 * rdonly,urdonly,readonly UF_READONLY (FreeBSD only)
1667 * reparse,ureparse UF_REPARSE (FreeBSD only)
1668 * sparse,usparse UF_SPARSE (FreeBSD only)
1669 * system,usystem UF_SYSTEM (FreeBSD only)
1671 * See chflags(2) for more information
1673 * Supported file attributes on Linux:
1674 * a append only FS_APPEND_FL sappnd
1675 * A no atime updates FS_NOATIME_FL atime
1676 * c compress FS_COMPR_FL compress
1677 * C no copy on write FS_NOCOW_FL cow
1678 * d no dump FS_NODUMP_FL dump
1679 * D synchronous directory updates FS_DIRSYNC_FL dirsync
1680 * i immutable FS_IMMUTABLE_FL schg
1681 * j data journalling FS_JOURNAL_DATA_FL journal
1682 * P project hierarchy FS_PROJINHERIT_FL projinherit
1683 * s secure deletion FS_SECRM_FL securedeletion
1684 * S synchronous updates FS_SYNC_FL sync
1685 * t no tail-merging FS_NOTAIL_FL tail
1686 * T top of directory hierarchy FS_TOPDIR_FL topdir
1687 * u undeletable FS_UNRM_FL undel
1689 * See ioctl_iflags(2) for more information
1691 * Equivalent file flags supported on FreeBSD / Mac OS and Linux:
1692 * SF_APPEND FS_APPEND_FL sappnd
1693 * SF_IMMUTABLE FS_IMMUTABLE_FL schg
1694 * UF_NODUMP FS_NODUMP_FL nodump
1697 static const struct flag {
1699 const wchar_t *wname;
1701 unsigned long clear;
1703 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1705 { "nosappnd", L"nosappnd", SF_APPEND, 0},
1706 { "nosappend", L"nosappend", SF_APPEND, 0},
1708 #if defined(FS_APPEND_FL) /* 'a' */
1709 { "nosappnd", L"nosappnd", FS_APPEND_FL, 0},
1710 { "nosappend", L"nosappend", FS_APPEND_FL, 0},
1711 #elif defined(EXT2_APPEND_FL) /* 'a' */
1712 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0},
1713 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0},
1716 { "noarch", L"noarch", SF_ARCHIVED, 0},
1717 { "noarchived", L"noarchived", SF_ARCHIVED, 0},
1720 { "noschg", L"noschg", SF_IMMUTABLE, 0},
1721 { "noschange", L"noschange", SF_IMMUTABLE, 0},
1722 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0},
1724 #if defined(FS_IMMUTABLE_FL) /* 'i' */
1725 { "noschg", L"noschg", FS_IMMUTABLE_FL, 0},
1726 { "noschange", L"noschange", FS_IMMUTABLE_FL, 0},
1727 { "nosimmutable", L"nosimmutable", FS_IMMUTABLE_FL, 0},
1728 #elif defined(EXT2_IMMUTABLE_FL) /* 'i' */
1729 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0},
1730 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0},
1731 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0},
1734 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0},
1735 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0},
1738 { "nouappnd", L"nouappnd", UF_APPEND, 0},
1739 { "nouappend", L"nouappend", UF_APPEND, 0},
1742 { "nouchg", L"nouchg", UF_IMMUTABLE, 0},
1743 { "nouchange", L"nouchange", UF_IMMUTABLE, 0},
1744 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0},
1747 { "nodump", L"nodump", 0, UF_NODUMP},
1749 #if defined(FS_NODUMP_FL) /* 'd' */
1750 { "nodump", L"nodump", 0, FS_NODUMP_FL},
1751 #elif defined(EXT2_NODUMP_FL)
1752 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1755 { "noopaque", L"noopaque", UF_OPAQUE, 0},
1758 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0},
1759 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0},
1761 #ifdef UF_COMPRESSED
1763 { "nocompressed", L"nocompressed", UF_COMPRESSED, 0},
1766 { "nohidden", L"nohidden", UF_HIDDEN, 0},
1767 { "nouhidden", L"nouhidden", UF_HIDDEN, 0},
1769 #ifdef FILE_ATTRIBUTE_HIDDEN
1770 { "nohidden", L"nohidden", FILE_ATTRIBUTE_HIDDEN, 0},
1771 { "nouhidden", L"nouhidden", FILE_ATTRIBUTE_HIDDEN, 0},
1774 { "nooffline", L"nooffline", UF_OFFLINE, 0},
1775 { "nouoffline", L"nouoffline", UF_OFFLINE, 0},
1778 { "nordonly", L"nordonly", UF_READONLY, 0},
1779 { "nourdonly", L"nourdonly", UF_READONLY, 0},
1780 { "noreadonly", L"noreadonly", UF_READONLY, 0},
1782 #ifdef FILE_ATTRIBUTE_READONLY
1783 { "nordonly", L"nordonly", FILE_ATTRIBUTE_READONLY, 0},
1784 { "nourdonly", L"nourdonly", FILE_ATTRIBUTE_READONLY, 0},
1785 { "noreadonly", L"noreadonly", FILE_ATTRIBUTE_READONLY, 0},
1788 { "nosparse", L"nosparse", UF_SPARSE, 0},
1789 { "nousparse", L"nousparse", UF_SPARSE, 0},
1792 { "noreparse", L"noreparse", UF_REPARSE, 0},
1793 { "noureparse", L"noureparse", UF_REPARSE, 0},
1796 { "nosystem", L"nosystem", UF_SYSTEM, 0},
1797 { "nousystem", L"nousystem", UF_SYSTEM, 0},
1799 #ifdef FILE_ATTRIBUTE_SYSTEM
1800 { "nosystem", L"nosystem", FILE_ATTRIBUTE_SYSTEM, 0},
1801 { "nousystem", L"nousystem", FILE_ATTRIBUTE_SYSTEM, 0},
1803 #if defined(FS_UNRM_FL) /* 'u' */
1804 { "noundel", L"noundel", FS_UNRM_FL, 0},
1805 #elif defined(EXT2_UNRM_FL)
1806 { "noundel", L"noundel", EXT2_UNRM_FL, 0},
1809 #if defined(FS_COMPR_FL) /* 'c' */
1810 { "nocompress", L"nocompress", FS_COMPR_FL, 0},
1811 #elif defined(EXT2_COMPR_FL)
1812 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0},
1815 #if defined(FS_NOATIME_FL) /* 'A' */
1816 { "noatime", L"noatime", 0, FS_NOATIME_FL},
1817 #elif defined(EXT2_NOATIME_FL)
1818 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1820 #if defined(FS_DIRSYNC_FL) /* 'D' */
1821 { "nodirsync", L"nodirsync", FS_DIRSYNC_FL, 0},
1822 #elif defined(EXT2_DIRSYNC_FL)
1823 { "nodirsync", L"nodirsync", EXT2_DIRSYNC_FL, 0},
1825 #if defined(FS_JOURNAL_DATA_FL) /* 'j' */
1826 { "nojournal-data",L"nojournal-data", FS_JOURNAL_DATA_FL, 0},
1827 { "nojournal", L"nojournal", FS_JOURNAL_DATA_FL, 0},
1828 #elif defined(EXT3_JOURNAL_DATA_FL)
1829 { "nojournal-data",L"nojournal-data", EXT3_JOURNAL_DATA_FL, 0},
1830 { "nojournal", L"nojournal", EXT3_JOURNAL_DATA_FL, 0},
1832 #if defined(FS_SECRM_FL) /* 's' */
1833 { "nosecdel", L"nosecdel", FS_SECRM_FL, 0},
1834 { "nosecuredeletion",L"nosecuredeletion",FS_SECRM_FL, 0},
1835 #elif defined(EXT2_SECRM_FL)
1836 { "nosecdel", L"nosecdel", EXT2_SECRM_FL, 0},
1837 { "nosecuredeletion",L"nosecuredeletion",EXT2_SECRM_FL, 0},
1839 #if defined(FS_SYNC_FL) /* 'S' */
1840 { "nosync", L"nosync", FS_SYNC_FL, 0},
1841 #elif defined(EXT2_SYNC_FL)
1842 { "nosync", L"nosync", EXT2_SYNC_FL, 0},
1844 #if defined(FS_NOTAIL_FL) /* 't' */
1845 { "notail", L"notail", 0, FS_NOTAIL_FL},
1846 #elif defined(EXT2_NOTAIL_FL)
1847 { "notail", L"notail", 0, EXT2_NOTAIL_FL},
1849 #if defined(FS_TOPDIR_FL) /* 'T' */
1850 { "notopdir", L"notopdir", FS_TOPDIR_FL, 0},
1851 #elif defined(EXT2_TOPDIR_FL)
1852 { "notopdir", L"notopdir", EXT2_TOPDIR_FL, 0},
1854 #ifdef FS_NOCOW_FL /* 'C' */
1855 { "nocow", L"nocow", 0, FS_NOCOW_FL},
1857 #ifdef FS_PROJINHERIT_FL /* 'P' */
1858 { "noprojinherit",L"noprojinherit", FS_PROJINHERIT_FL, 0},
1865 * Convert file flags to a comma-separated string. If no flags
1866 * are set, return the empty string.
1869 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1874 const struct flag *flag;
1877 bits = bitset | bitclear;
1879 for (flag = flags; flag->name != NULL; flag++)
1880 if (bits & (flag->set | flag->clear)) {
1881 length += strlen(flag->name) + 1;
1882 bits &= ~(flag->set | flag->clear);
1887 string = (char *)malloc(length);
1892 for (flag = flags; flag->name != NULL; flag++) {
1893 if (bitset & flag->set || bitclear & flag->clear) {
1894 sp = flag->name + 2;
1895 } else if (bitset & flag->clear || bitclear & flag->set) {
1899 bitset &= ~(flag->set | flag->clear);
1900 bitclear &= ~(flag->set | flag->clear);
1903 while ((*dp++ = *sp++) != '\0')
1914 * Take string of arguments and return file flags. This
1915 * version works a little differently than strtofflags(3).
1916 * In particular, it always tests every token, skipping any
1917 * unrecognized tokens. It returns a pointer to the first
1918 * unrecognized token, or NULL if every token was recognized.
1919 * This version is also const-correct and does not modify the
1923 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
1925 const char *start, *end;
1926 const struct flag *flag;
1927 unsigned long set, clear;
1933 /* Find start of first token. */
1934 while (*start == '\t' || *start == ' ' || *start == ',')
1936 while (*start != '\0') {
1938 /* Locate end of token. */
1940 while (*end != '\0' && *end != '\t' &&
1941 *end != ' ' && *end != ',')
1943 length = end - start;
1944 for (flag = flags; flag->name != NULL; flag++) {
1945 size_t flag_length = strlen(flag->name);
1946 if (length == flag_length
1947 && memcmp(start, flag->name, length) == 0) {
1948 /* Matched "noXXXX", so reverse the sense. */
1952 } else if (length == flag_length - 2
1953 && memcmp(start, flag->name + 2, length) == 0) {
1954 /* Matched "XXXX", so don't reverse. */
1956 clear |= flag->clear;
1960 /* Ignore unknown flag names. */
1961 if (flag->name == NULL && failed == NULL)
1964 /* Find start of next token. */
1966 while (*start == '\t' || *start == ' ' || *start == ',')
1976 /* Return location of first failure. */
1982 * Take string of arguments and return file flags. This
1983 * version works a little differently than strtofflags(3).
1984 * In particular, it always tests every token, skipping any
1985 * unrecognized tokens. It returns a pointer to the first
1986 * unrecognized token, or NULL if every token was recognized.
1987 * This version is also const-correct and does not modify the
1990 static const wchar_t *
1991 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1993 const wchar_t *start, *end;
1994 const struct flag *flag;
1995 unsigned long set, clear;
1996 const wchar_t *failed;
2001 /* Find start of first token. */
2002 while (*start == L'\t' || *start == L' ' || *start == L',')
2004 while (*start != L'\0') {
2006 /* Locate end of token. */
2008 while (*end != L'\0' && *end != L'\t' &&
2009 *end != L' ' && *end != L',')
2011 length = end - start;
2012 for (flag = flags; flag->wname != NULL; flag++) {
2013 size_t flag_length = wcslen(flag->wname);
2014 if (length == flag_length
2015 && wmemcmp(start, flag->wname, length) == 0) {
2016 /* Matched "noXXXX", so reverse the sense. */
2020 } else if (length == flag_length - 2
2021 && wmemcmp(start, flag->wname + 2, length) == 0) {
2022 /* Matched "XXXX", so don't reverse. */
2024 clear |= flag->clear;
2028 /* Ignore unknown flag names. */
2029 if (flag->wname == NULL && failed == NULL)
2032 /* Find start of next token. */
2034 while (*start == L'\t' || *start == L' ' || *start == L',')
2044 /* Return location of first failure. */
2052 main(int argc, char **argv)
2054 struct archive_entry *entry = archive_entry_new();
2055 unsigned long set, clear;
2056 const wchar_t *remainder;
2058 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
2059 archive_entry_fflags(entry, &set, &clear);
2061 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
2063 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));