2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
29 #ifdef HAVE_SYS_STAT_H
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
36 #include <sys/mkdev.h>
38 #elif MAJOR_IN_SYSMACROS
39 #include <sys/sysmacros.h>
48 #ifdef HAVE_LINUX_FS_H
49 #include <linux/fs.h> /* for Linux file flags */
52 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
53 * As the include guards don't agree, the order of include is important.
55 #ifdef HAVE_LINUX_EXT2_FS_H
56 #include <linux/ext2_fs.h> /* for Linux file flags */
58 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
59 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
74 #include "archive_acl_private.h"
75 #include "archive_entry.h"
76 #include "archive_entry_locale.h"
77 #include "archive_private.h"
78 #include "archive_entry_private.h"
80 #if !defined(HAVE_MAJOR) && !defined(major)
81 /* Replacement for major/minor/makedev. */
82 #define major(x) ((int)(0x00ff & ((x) >> 8)))
83 #define minor(x) ((int)(0xffff00ff & (x)))
84 #define makedev(maj,min) ((0xff00 & ((maj)<<8)) | (0xffff00ff & (min)))
87 /* Play games to come up with a suitable makedev() definition. */
90 #include <sys/netmgr.h>
91 #define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
93 /* There's a "makedev" macro. */
94 #define ae_makedev(maj, min) makedev((maj), (min))
95 #elif defined mkdev || ((defined _WIN32 || defined __WIN32__) && !defined(__CYGWIN__))
97 #define ae_makedev(maj, min) mkdev((maj), (min))
99 /* There's a "makedev" function. */
100 #define ae_makedev(maj, min) makedev((maj), (min))
104 * This adjustment is needed to support the following idiom for adding
105 * 1000ns to the stored time:
106 * archive_entry_set_atime(archive_entry_atime(),
107 * archive_entry_atime_nsec() + 1000)
108 * The additional if() here compensates for ambiguity in the C standard,
109 * which permits two possible interpretations of a % b when a is negative.
111 #define FIX_NS(t,ns) \
113 t += ns / 1000000000; \
115 if (ns < 0) { --t; ns += 1000000000; } \
118 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
119 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
120 unsigned long *setp, unsigned long *clrp);
121 static const char *ae_strtofflags(const char *stringp,
122 unsigned long *setp, unsigned long *clrp);
125 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
128 while ((*s1 = *s2) != L'\0')
134 static size_t wcslen(const wchar_t *s)
136 const wchar_t *p = s;
143 /* Good enough for simple equality testing, but not for sorting. */
144 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
147 /****************************************************************************
151 ****************************************************************************/
153 struct archive_entry *
154 archive_entry_clear(struct archive_entry *entry)
158 archive_mstring_clean(&entry->ae_fflags_text);
159 archive_mstring_clean(&entry->ae_gname);
160 archive_mstring_clean(&entry->ae_hardlink);
161 archive_mstring_clean(&entry->ae_pathname);
162 archive_mstring_clean(&entry->ae_sourcepath);
163 archive_mstring_clean(&entry->ae_symlink);
164 archive_mstring_clean(&entry->ae_uname);
165 archive_entry_copy_mac_metadata(entry, NULL, 0);
166 archive_acl_clear(&entry->acl);
167 archive_entry_xattr_clear(entry);
168 archive_entry_sparse_clear(entry);
170 memset(entry, 0, sizeof(*entry));
174 struct archive_entry *
175 archive_entry_clone(struct archive_entry *entry)
177 struct archive_entry *entry2;
179 struct ae_sparse *sp;
183 /* Allocate new structure and copy over all of the fields. */
184 /* TODO: Should we copy the archive over? Or require a new archive
186 entry2 = archive_entry_new2(entry->archive);
189 entry2->ae_stat = entry->ae_stat;
190 entry2->ae_fflags_set = entry->ae_fflags_set;
191 entry2->ae_fflags_clear = entry->ae_fflags_clear;
193 /* TODO: XXX If clone can have a different archive, what do we do here if
194 * character sets are different? XXX */
195 archive_mstring_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
196 archive_mstring_copy(&entry2->ae_gname, &entry->ae_gname);
197 archive_mstring_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
198 archive_mstring_copy(&entry2->ae_pathname, &entry->ae_pathname);
199 archive_mstring_copy(&entry2->ae_sourcepath, &entry->ae_sourcepath);
200 archive_mstring_copy(&entry2->ae_symlink, &entry->ae_symlink);
201 entry2->ae_set = entry->ae_set;
202 archive_mstring_copy(&entry2->ae_uname, &entry->ae_uname);
204 /* Copy encryption status */
205 entry2->encryption = entry->encryption;
207 /* Copy ACL data over. */
208 archive_acl_copy(&entry2->acl, &entry->acl);
210 /* Copy Mac OS metadata. */
211 p = archive_entry_mac_metadata(entry, &s);
212 archive_entry_copy_mac_metadata(entry2, p, s);
214 /* Copy xattr data over. */
215 xp = entry->xattr_head;
217 archive_entry_xattr_add_entry(entry2,
218 xp->name, xp->value, xp->size);
222 /* Copy sparse data over. */
223 sp = entry->sparse_head;
225 archive_entry_sparse_add_entry(entry2,
226 sp->offset, sp->length);
234 archive_entry_free(struct archive_entry *entry)
236 archive_entry_clear(entry);
240 struct archive_entry *
241 archive_entry_new(void)
243 return archive_entry_new2(NULL);
246 struct archive_entry *
247 archive_entry_new2(struct archive *a)
249 struct archive_entry *entry;
251 entry = (struct archive_entry *)calloc(1, sizeof(*entry));
259 * Functions for reading fields from an archive_entry.
263 archive_entry_atime(struct archive_entry *entry)
265 return (entry->ae_stat.aest_atime);
269 archive_entry_atime_nsec(struct archive_entry *entry)
271 return (entry->ae_stat.aest_atime_nsec);
275 archive_entry_atime_is_set(struct archive_entry *entry)
277 return (entry->ae_set & AE_SET_ATIME);
281 archive_entry_birthtime(struct archive_entry *entry)
283 return (entry->ae_stat.aest_birthtime);
287 archive_entry_birthtime_nsec(struct archive_entry *entry)
289 return (entry->ae_stat.aest_birthtime_nsec);
293 archive_entry_birthtime_is_set(struct archive_entry *entry)
295 return (entry->ae_set & AE_SET_BIRTHTIME);
299 archive_entry_ctime(struct archive_entry *entry)
301 return (entry->ae_stat.aest_ctime);
305 archive_entry_ctime_is_set(struct archive_entry *entry)
307 return (entry->ae_set & AE_SET_CTIME);
311 archive_entry_ctime_nsec(struct archive_entry *entry)
313 return (entry->ae_stat.aest_ctime_nsec);
317 archive_entry_dev(struct archive_entry *entry)
319 if (entry->ae_stat.aest_dev_is_broken_down)
320 return ae_makedev(entry->ae_stat.aest_devmajor,
321 entry->ae_stat.aest_devminor);
323 return (entry->ae_stat.aest_dev);
327 archive_entry_dev_is_set(struct archive_entry *entry)
329 return (entry->ae_set & AE_SET_DEV);
333 archive_entry_devmajor(struct archive_entry *entry)
335 if (entry->ae_stat.aest_dev_is_broken_down)
336 return (entry->ae_stat.aest_devmajor);
338 return major(entry->ae_stat.aest_dev);
342 archive_entry_devminor(struct archive_entry *entry)
344 if (entry->ae_stat.aest_dev_is_broken_down)
345 return (entry->ae_stat.aest_devminor);
347 return minor(entry->ae_stat.aest_dev);
351 archive_entry_filetype(struct archive_entry *entry)
353 return (AE_IFMT & entry->acl.mode);
357 archive_entry_fflags(struct archive_entry *entry,
358 unsigned long *set, unsigned long *clear)
360 *set = entry->ae_fflags_set;
361 *clear = entry->ae_fflags_clear;
365 * Note: if text was provided, this just returns that text. If you
366 * really need the text to be rebuilt in a canonical form, set the
367 * text, ask for the bitmaps, then set the bitmaps. (Setting the
368 * bitmaps clears any stored text.) This design is deliberate: if
369 * we're editing archives, we don't want to discard flags just because
370 * they aren't supported on the current system. The bitmap<->text
371 * conversions are platform-specific (see below).
374 archive_entry_fflags_text(struct archive_entry *entry)
379 if (archive_mstring_get_mbs(entry->archive,
380 &entry->ae_fflags_text, &f) == 0) {
383 } else if (errno == ENOMEM)
384 __archive_errx(1, "No memory");
386 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
389 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
393 archive_mstring_copy_mbs(&entry->ae_fflags_text, p);
395 if (archive_mstring_get_mbs(entry->archive,
396 &entry->ae_fflags_text, &f) == 0)
399 __archive_errx(1, "No memory");
404 archive_entry_gid(struct archive_entry *entry)
406 return (entry->ae_stat.aest_gid);
410 archive_entry_gname(struct archive_entry *entry)
413 if (archive_mstring_get_mbs(entry->archive, &entry->ae_gname, &p) == 0)
416 __archive_errx(1, "No memory");
421 archive_entry_gname_utf8(struct archive_entry *entry)
424 if (archive_mstring_get_utf8(entry->archive, &entry->ae_gname, &p) == 0)
427 __archive_errx(1, "No memory");
433 archive_entry_gname_w(struct archive_entry *entry)
436 if (archive_mstring_get_wcs(entry->archive, &entry->ae_gname, &p) == 0)
439 __archive_errx(1, "No memory");
444 _archive_entry_gname_l(struct archive_entry *entry,
445 const char **p, size_t *len, struct archive_string_conv *sc)
447 return (archive_mstring_get_mbs_l(&entry->ae_gname, p, len, sc));
451 archive_entry_hardlink(struct archive_entry *entry)
454 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
456 if (archive_mstring_get_mbs(
457 entry->archive, &entry->ae_hardlink, &p) == 0)
460 __archive_errx(1, "No memory");
465 archive_entry_hardlink_utf8(struct archive_entry *entry)
468 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
470 if (archive_mstring_get_utf8(
471 entry->archive, &entry->ae_hardlink, &p) == 0)
474 __archive_errx(1, "No memory");
479 archive_entry_hardlink_w(struct archive_entry *entry)
482 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
484 if (archive_mstring_get_wcs(
485 entry->archive, &entry->ae_hardlink, &p) == 0)
488 __archive_errx(1, "No memory");
493 _archive_entry_hardlink_l(struct archive_entry *entry,
494 const char **p, size_t *len, struct archive_string_conv *sc)
496 if ((entry->ae_set & AE_SET_HARDLINK) == 0) {
501 return (archive_mstring_get_mbs_l(&entry->ae_hardlink, p, len, sc));
505 archive_entry_ino(struct archive_entry *entry)
507 return (entry->ae_stat.aest_ino);
511 archive_entry_ino_is_set(struct archive_entry *entry)
513 return (entry->ae_set & AE_SET_INO);
517 archive_entry_ino64(struct archive_entry *entry)
519 return (entry->ae_stat.aest_ino);
523 archive_entry_mode(struct archive_entry *entry)
525 return (entry->acl.mode);
529 archive_entry_mtime(struct archive_entry *entry)
531 return (entry->ae_stat.aest_mtime);
535 archive_entry_mtime_nsec(struct archive_entry *entry)
537 return (entry->ae_stat.aest_mtime_nsec);
541 archive_entry_mtime_is_set(struct archive_entry *entry)
543 return (entry->ae_set & AE_SET_MTIME);
547 archive_entry_nlink(struct archive_entry *entry)
549 return (entry->ae_stat.aest_nlink);
553 archive_entry_pathname(struct archive_entry *entry)
556 if (archive_mstring_get_mbs(
557 entry->archive, &entry->ae_pathname, &p) == 0)
560 __archive_errx(1, "No memory");
565 archive_entry_pathname_utf8(struct archive_entry *entry)
568 if (archive_mstring_get_utf8(
569 entry->archive, &entry->ae_pathname, &p) == 0)
572 __archive_errx(1, "No memory");
577 archive_entry_pathname_w(struct archive_entry *entry)
580 if (archive_mstring_get_wcs(
581 entry->archive, &entry->ae_pathname, &p) == 0)
584 __archive_errx(1, "No memory");
589 _archive_entry_pathname_l(struct archive_entry *entry,
590 const char **p, size_t *len, struct archive_string_conv *sc)
592 return (archive_mstring_get_mbs_l(&entry->ae_pathname, p, len, sc));
596 archive_entry_perm(struct archive_entry *entry)
598 return (~AE_IFMT & entry->acl.mode);
602 archive_entry_rdev(struct archive_entry *entry)
604 if (entry->ae_stat.aest_rdev_is_broken_down)
605 return ae_makedev(entry->ae_stat.aest_rdevmajor,
606 entry->ae_stat.aest_rdevminor);
608 return (entry->ae_stat.aest_rdev);
612 archive_entry_rdevmajor(struct archive_entry *entry)
614 if (entry->ae_stat.aest_rdev_is_broken_down)
615 return (entry->ae_stat.aest_rdevmajor);
617 return major(entry->ae_stat.aest_rdev);
621 archive_entry_rdevminor(struct archive_entry *entry)
623 if (entry->ae_stat.aest_rdev_is_broken_down)
624 return (entry->ae_stat.aest_rdevminor);
626 return minor(entry->ae_stat.aest_rdev);
630 archive_entry_size(struct archive_entry *entry)
632 return (entry->ae_stat.aest_size);
636 archive_entry_size_is_set(struct archive_entry *entry)
638 return (entry->ae_set & AE_SET_SIZE);
642 archive_entry_sourcepath(struct archive_entry *entry)
645 if (archive_mstring_get_mbs(
646 entry->archive, &entry->ae_sourcepath, &p) == 0)
649 __archive_errx(1, "No memory");
654 archive_entry_sourcepath_w(struct archive_entry *entry)
657 if (archive_mstring_get_wcs(
658 entry->archive, &entry->ae_sourcepath, &p) == 0)
664 archive_entry_symlink(struct archive_entry *entry)
667 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
669 if (archive_mstring_get_mbs(
670 entry->archive, &entry->ae_symlink, &p) == 0)
673 __archive_errx(1, "No memory");
678 archive_entry_symlink_utf8(struct archive_entry *entry)
681 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
683 if (archive_mstring_get_utf8(
684 entry->archive, &entry->ae_symlink, &p) == 0)
687 __archive_errx(1, "No memory");
692 archive_entry_symlink_w(struct archive_entry *entry)
695 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
697 if (archive_mstring_get_wcs(
698 entry->archive, &entry->ae_symlink, &p) == 0)
701 __archive_errx(1, "No memory");
706 _archive_entry_symlink_l(struct archive_entry *entry,
707 const char **p, size_t *len, struct archive_string_conv *sc)
709 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
714 return (archive_mstring_get_mbs_l( &entry->ae_symlink, p, len, sc));
718 archive_entry_uid(struct archive_entry *entry)
720 return (entry->ae_stat.aest_uid);
724 archive_entry_uname(struct archive_entry *entry)
727 if (archive_mstring_get_mbs(entry->archive, &entry->ae_uname, &p) == 0)
730 __archive_errx(1, "No memory");
735 archive_entry_uname_utf8(struct archive_entry *entry)
738 if (archive_mstring_get_utf8(entry->archive, &entry->ae_uname, &p) == 0)
741 __archive_errx(1, "No memory");
746 archive_entry_uname_w(struct archive_entry *entry)
749 if (archive_mstring_get_wcs(entry->archive, &entry->ae_uname, &p) == 0)
752 __archive_errx(1, "No memory");
757 _archive_entry_uname_l(struct archive_entry *entry,
758 const char **p, size_t *len, struct archive_string_conv *sc)
760 return (archive_mstring_get_mbs_l(&entry->ae_uname, p, len, sc));
764 archive_entry_is_data_encrypted(struct archive_entry *entry)
766 return ((entry->encryption & AE_ENCRYPTION_DATA) == AE_ENCRYPTION_DATA);
770 archive_entry_is_metadata_encrypted(struct archive_entry *entry)
772 return ((entry->encryption & AE_ENCRYPTION_METADATA) == AE_ENCRYPTION_METADATA);
776 archive_entry_is_encrypted(struct archive_entry *entry)
778 return (entry->encryption & (AE_ENCRYPTION_DATA|AE_ENCRYPTION_METADATA));
782 * Functions to set archive_entry properties.
786 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
788 entry->stat_valid = 0;
789 entry->acl.mode &= ~AE_IFMT;
790 entry->acl.mode |= AE_IFMT & type;
794 archive_entry_set_fflags(struct archive_entry *entry,
795 unsigned long set, unsigned long clear)
797 archive_mstring_clean(&entry->ae_fflags_text);
798 entry->ae_fflags_set = set;
799 entry->ae_fflags_clear = clear;
803 archive_entry_copy_fflags_text(struct archive_entry *entry,
806 archive_mstring_copy_mbs(&entry->ae_fflags_text, flags);
807 return (ae_strtofflags(flags,
808 &entry->ae_fflags_set, &entry->ae_fflags_clear));
812 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
813 const wchar_t *flags)
815 archive_mstring_copy_wcs(&entry->ae_fflags_text, flags);
816 return (ae_wcstofflags(flags,
817 &entry->ae_fflags_set, &entry->ae_fflags_clear));
821 archive_entry_set_gid(struct archive_entry *entry, int64_t g)
823 entry->stat_valid = 0;
824 entry->ae_stat.aest_gid = g;
828 archive_entry_set_gname(struct archive_entry *entry, const char *name)
830 archive_mstring_copy_mbs(&entry->ae_gname, name);
834 archive_entry_set_gname_utf8(struct archive_entry *entry, const char *name)
836 archive_mstring_copy_utf8(&entry->ae_gname, name);
840 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
842 archive_mstring_copy_mbs(&entry->ae_gname, name);
846 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
848 archive_mstring_copy_wcs(&entry->ae_gname, name);
852 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
854 if (archive_mstring_update_utf8(entry->archive,
855 &entry->ae_gname, name) == 0)
858 __archive_errx(1, "No memory");
863 _archive_entry_copy_gname_l(struct archive_entry *entry,
864 const char *name, size_t len, struct archive_string_conv *sc)
866 return (archive_mstring_copy_mbs_len_l(&entry->ae_gname, name, len, sc));
870 archive_entry_set_ino(struct archive_entry *entry, int64_t ino)
872 entry->stat_valid = 0;
873 entry->ae_set |= AE_SET_INO;
874 entry->ae_stat.aest_ino = ino;
878 archive_entry_set_ino64(struct archive_entry *entry, int64_t ino)
880 entry->stat_valid = 0;
881 entry->ae_set |= AE_SET_INO;
882 entry->ae_stat.aest_ino = ino;
886 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
888 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
890 entry->ae_set |= AE_SET_HARDLINK;
892 entry->ae_set &= ~AE_SET_HARDLINK;
896 archive_entry_set_hardlink_utf8(struct archive_entry *entry, const char *target)
898 archive_mstring_copy_utf8(&entry->ae_hardlink, target);
900 entry->ae_set |= AE_SET_HARDLINK;
902 entry->ae_set &= ~AE_SET_HARDLINK;
906 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
908 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
910 entry->ae_set |= AE_SET_HARDLINK;
912 entry->ae_set &= ~AE_SET_HARDLINK;
916 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
918 archive_mstring_copy_wcs(&entry->ae_hardlink, target);
920 entry->ae_set |= AE_SET_HARDLINK;
922 entry->ae_set &= ~AE_SET_HARDLINK;
926 archive_entry_update_hardlink_utf8(struct archive_entry *entry, const char *target)
929 entry->ae_set |= AE_SET_HARDLINK;
931 entry->ae_set &= ~AE_SET_HARDLINK;
932 if (archive_mstring_update_utf8(entry->archive,
933 &entry->ae_hardlink, target) == 0)
936 __archive_errx(1, "No memory");
941 _archive_entry_copy_hardlink_l(struct archive_entry *entry,
942 const char *target, size_t len, struct archive_string_conv *sc)
946 r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
948 if (target != NULL && r == 0)
949 entry->ae_set |= AE_SET_HARDLINK;
951 entry->ae_set &= ~AE_SET_HARDLINK;
956 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
959 entry->stat_valid = 0;
960 entry->ae_set |= AE_SET_ATIME;
961 entry->ae_stat.aest_atime = t;
962 entry->ae_stat.aest_atime_nsec = ns;
966 archive_entry_unset_atime(struct archive_entry *entry)
968 archive_entry_set_atime(entry, 0, 0);
969 entry->ae_set &= ~AE_SET_ATIME;
973 archive_entry_set_birthtime(struct archive_entry *entry, time_t t, long ns)
976 entry->stat_valid = 0;
977 entry->ae_set |= AE_SET_BIRTHTIME;
978 entry->ae_stat.aest_birthtime = t;
979 entry->ae_stat.aest_birthtime_nsec = ns;
983 archive_entry_unset_birthtime(struct archive_entry *entry)
985 archive_entry_set_birthtime(entry, 0, 0);
986 entry->ae_set &= ~AE_SET_BIRTHTIME;
990 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
993 entry->stat_valid = 0;
994 entry->ae_set |= AE_SET_CTIME;
995 entry->ae_stat.aest_ctime = t;
996 entry->ae_stat.aest_ctime_nsec = ns;
1000 archive_entry_unset_ctime(struct archive_entry *entry)
1002 archive_entry_set_ctime(entry, 0, 0);
1003 entry->ae_set &= ~AE_SET_CTIME;
1007 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
1009 entry->stat_valid = 0;
1010 entry->ae_set |= AE_SET_DEV;
1011 entry->ae_stat.aest_dev_is_broken_down = 0;
1012 entry->ae_stat.aest_dev = d;
1016 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
1018 entry->stat_valid = 0;
1019 entry->ae_set |= AE_SET_DEV;
1020 entry->ae_stat.aest_dev_is_broken_down = 1;
1021 entry->ae_stat.aest_devmajor = m;
1025 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
1027 entry->stat_valid = 0;
1028 entry->ae_set |= AE_SET_DEV;
1029 entry->ae_stat.aest_dev_is_broken_down = 1;
1030 entry->ae_stat.aest_devminor = m;
1033 /* Set symlink if symlink is already set, else set hardlink. */
1035 archive_entry_set_link(struct archive_entry *entry, const char *target)
1037 if (entry->ae_set & AE_SET_SYMLINK)
1038 archive_mstring_copy_mbs(&entry->ae_symlink, target);
1040 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
1044 archive_entry_set_link_utf8(struct archive_entry *entry, const char *target)
1046 if (entry->ae_set & AE_SET_SYMLINK)
1047 archive_mstring_copy_utf8(&entry->ae_symlink, target);
1049 archive_mstring_copy_utf8(&entry->ae_hardlink, target);
1052 /* Set symlink if symlink is already set, else set hardlink. */
1054 archive_entry_copy_link(struct archive_entry *entry, const char *target)
1056 if (entry->ae_set & AE_SET_SYMLINK)
1057 archive_mstring_copy_mbs(&entry->ae_symlink, target);
1059 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
1062 /* Set symlink if symlink is already set, else set hardlink. */
1064 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
1066 if (entry->ae_set & AE_SET_SYMLINK)
1067 archive_mstring_copy_wcs(&entry->ae_symlink, target);
1069 archive_mstring_copy_wcs(&entry->ae_hardlink, target);
1073 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
1076 if (entry->ae_set & AE_SET_SYMLINK)
1077 r = archive_mstring_update_utf8(entry->archive,
1078 &entry->ae_symlink, target);
1080 r = archive_mstring_update_utf8(entry->archive,
1081 &entry->ae_hardlink, target);
1084 if (errno == ENOMEM)
1085 __archive_errx(1, "No memory");
1090 _archive_entry_copy_link_l(struct archive_entry *entry,
1091 const char *target, size_t len, struct archive_string_conv *sc)
1095 if (entry->ae_set & AE_SET_SYMLINK)
1096 r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
1099 r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
1105 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
1107 entry->stat_valid = 0;
1108 entry->acl.mode = m;
1112 archive_entry_set_mtime(struct archive_entry *entry, time_t t, long ns)
1115 entry->stat_valid = 0;
1116 entry->ae_set |= AE_SET_MTIME;
1117 entry->ae_stat.aest_mtime = t;
1118 entry->ae_stat.aest_mtime_nsec = ns;
1122 archive_entry_unset_mtime(struct archive_entry *entry)
1124 archive_entry_set_mtime(entry, 0, 0);
1125 entry->ae_set &= ~AE_SET_MTIME;
1129 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
1131 entry->stat_valid = 0;
1132 entry->ae_stat.aest_nlink = nlink;
1136 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
1138 archive_mstring_copy_mbs(&entry->ae_pathname, name);
1142 archive_entry_set_pathname_utf8(struct archive_entry *entry, const char *name)
1144 archive_mstring_copy_utf8(&entry->ae_pathname, name);
1148 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
1150 archive_mstring_copy_mbs(&entry->ae_pathname, name);
1154 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
1156 archive_mstring_copy_wcs(&entry->ae_pathname, name);
1160 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
1162 if (archive_mstring_update_utf8(entry->archive,
1163 &entry->ae_pathname, name) == 0)
1165 if (errno == ENOMEM)
1166 __archive_errx(1, "No memory");
1171 _archive_entry_copy_pathname_l(struct archive_entry *entry,
1172 const char *name, size_t len, struct archive_string_conv *sc)
1174 return (archive_mstring_copy_mbs_len_l(&entry->ae_pathname,
1179 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
1181 entry->stat_valid = 0;
1182 entry->acl.mode &= AE_IFMT;
1183 entry->acl.mode |= ~AE_IFMT & p;
1187 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
1189 entry->stat_valid = 0;
1190 entry->ae_stat.aest_rdev = m;
1191 entry->ae_stat.aest_rdev_is_broken_down = 0;
1195 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
1197 entry->stat_valid = 0;
1198 entry->ae_stat.aest_rdev_is_broken_down = 1;
1199 entry->ae_stat.aest_rdevmajor = m;
1203 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
1205 entry->stat_valid = 0;
1206 entry->ae_stat.aest_rdev_is_broken_down = 1;
1207 entry->ae_stat.aest_rdevminor = m;
1211 archive_entry_set_size(struct archive_entry *entry, int64_t s)
1213 entry->stat_valid = 0;
1214 entry->ae_stat.aest_size = s;
1215 entry->ae_set |= AE_SET_SIZE;
1219 archive_entry_unset_size(struct archive_entry *entry)
1221 archive_entry_set_size(entry, 0);
1222 entry->ae_set &= ~AE_SET_SIZE;
1226 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1228 archive_mstring_copy_mbs(&entry->ae_sourcepath, path);
1232 archive_entry_copy_sourcepath_w(struct archive_entry *entry, const wchar_t *path)
1234 archive_mstring_copy_wcs(&entry->ae_sourcepath, path);
1238 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1240 archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
1241 if (linkname != NULL)
1242 entry->ae_set |= AE_SET_SYMLINK;
1244 entry->ae_set &= ~AE_SET_SYMLINK;
1248 archive_entry_set_symlink_utf8(struct archive_entry *entry, const char *linkname)
1250 archive_mstring_copy_utf8(&entry->ae_symlink, linkname);
1251 if (linkname != NULL)
1252 entry->ae_set |= AE_SET_SYMLINK;
1254 entry->ae_set &= ~AE_SET_SYMLINK;
1258 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1260 archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
1261 if (linkname != NULL)
1262 entry->ae_set |= AE_SET_SYMLINK;
1264 entry->ae_set &= ~AE_SET_SYMLINK;
1268 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1270 archive_mstring_copy_wcs(&entry->ae_symlink, linkname);
1271 if (linkname != NULL)
1272 entry->ae_set |= AE_SET_SYMLINK;
1274 entry->ae_set &= ~AE_SET_SYMLINK;
1278 archive_entry_update_symlink_utf8(struct archive_entry *entry, const char *linkname)
1280 if (linkname != NULL)
1281 entry->ae_set |= AE_SET_SYMLINK;
1283 entry->ae_set &= ~AE_SET_SYMLINK;
1284 if (archive_mstring_update_utf8(entry->archive,
1285 &entry->ae_symlink, linkname) == 0)
1287 if (errno == ENOMEM)
1288 __archive_errx(1, "No memory");
1293 _archive_entry_copy_symlink_l(struct archive_entry *entry,
1294 const char *linkname, size_t len, struct archive_string_conv *sc)
1298 r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
1300 if (linkname != NULL && r == 0)
1301 entry->ae_set |= AE_SET_SYMLINK;
1303 entry->ae_set &= ~AE_SET_SYMLINK;
1308 archive_entry_set_uid(struct archive_entry *entry, int64_t u)
1310 entry->stat_valid = 0;
1311 entry->ae_stat.aest_uid = u;
1315 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1317 archive_mstring_copy_mbs(&entry->ae_uname, name);
1321 archive_entry_set_uname_utf8(struct archive_entry *entry, const char *name)
1323 archive_mstring_copy_utf8(&entry->ae_uname, name);
1327 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1329 archive_mstring_copy_mbs(&entry->ae_uname, name);
1333 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1335 archive_mstring_copy_wcs(&entry->ae_uname, name);
1339 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1341 if (archive_mstring_update_utf8(entry->archive,
1342 &entry->ae_uname, name) == 0)
1344 if (errno == ENOMEM)
1345 __archive_errx(1, "No memory");
1350 archive_entry_set_is_data_encrypted(struct archive_entry *entry, char is_encrypted)
1353 entry->encryption |= AE_ENCRYPTION_DATA;
1355 entry->encryption &= ~AE_ENCRYPTION_DATA;
1360 archive_entry_set_is_metadata_encrypted(struct archive_entry *entry, char is_encrypted)
1363 entry->encryption |= AE_ENCRYPTION_METADATA;
1365 entry->encryption &= ~AE_ENCRYPTION_METADATA;
1370 _archive_entry_copy_uname_l(struct archive_entry *entry,
1371 const char *name, size_t len, struct archive_string_conv *sc)
1373 return (archive_mstring_copy_mbs_len_l(&entry->ae_uname,
1378 archive_entry_mac_metadata(struct archive_entry *entry, size_t *s)
1380 *s = entry->mac_metadata_size;
1381 return entry->mac_metadata;
1385 archive_entry_copy_mac_metadata(struct archive_entry *entry,
1386 const void *p, size_t s)
1388 free(entry->mac_metadata);
1389 if (p == NULL || s == 0) {
1390 entry->mac_metadata = NULL;
1391 entry->mac_metadata_size = 0;
1393 entry->mac_metadata_size = s;
1394 entry->mac_metadata = malloc(s);
1395 if (entry->mac_metadata == NULL)
1397 memcpy(entry->mac_metadata, p, s);
1402 * ACL management. The following would, of course, be a lot simpler
1403 * if: 1) the last draft of POSIX.1e were a really thorough and
1404 * complete standard that addressed the needs of ACL archiving and 2)
1405 * everyone followed it faithfully. Alas, neither is true, so the
1406 * following is a lot more complex than might seem necessary to the
1410 struct archive_acl *
1411 archive_entry_acl(struct archive_entry *entry)
1417 archive_entry_acl_clear(struct archive_entry *entry)
1419 archive_acl_clear(&entry->acl);
1423 * Add a single ACL entry to the internal list of ACL data.
1426 archive_entry_acl_add_entry(struct archive_entry *entry,
1427 int type, int permset, int tag, int id, const char *name)
1429 return archive_acl_add_entry(&entry->acl, type, permset, tag, id, name);
1433 * As above, but with a wide-character name.
1436 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1437 int type, int permset, int tag, int id, const wchar_t *name)
1439 return archive_acl_add_entry_w_len(&entry->acl,
1440 type, permset, tag, id, name, wcslen(name));
1444 * Return a bitmask of ACL types in an archive entry ACL list
1447 archive_entry_acl_types(struct archive_entry *entry)
1449 return ((&entry->acl)->acl_types);
1453 * Return a count of entries matching "want_type".
1456 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1458 return archive_acl_count(&entry->acl, want_type);
1462 * Prepare for reading entries from the ACL data. Returns a count
1463 * of entries matching "want_type", or zero if there are no
1464 * non-extended ACL entries of that type.
1467 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1469 return archive_acl_reset(&entry->acl, want_type);
1473 * Return the next ACL entry in the list. Fake entries for the
1474 * standard permissions and include them in the returned list.
1477 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1478 int *permset, int *tag, int *id, const char **name)
1481 r = archive_acl_next(entry->archive, &entry->acl, want_type, type,
1482 permset, tag, id, name);
1483 if (r == ARCHIVE_FATAL && errno == ENOMEM)
1484 __archive_errx(1, "No memory");
1489 * Generate a text version of the ACL. The flags parameter controls
1490 * the style of the generated ACL.
1493 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1496 r = archive_acl_text_w(entry->archive, &entry->acl, flags);
1497 if (r == NULL && errno == ENOMEM)
1498 __archive_errx(1, "No memory");
1503 archive_entry_acl_text(struct archive_entry *entry, int flags)
1506 if (archive_acl_text_l(&entry->acl, flags, &p, NULL, NULL) != 0
1508 __archive_errx(1, "No memory");
1513 _archive_entry_acl_text_l(struct archive_entry *entry, int flags,
1514 const char **acl_text, size_t *len, struct archive_string_conv *sc)
1516 return (archive_acl_text_l(&entry->acl, flags, acl_text, len, sc));
1520 * Following code is modified from UC Berkeley sources, and
1521 * is subject to the following copyright notice.
1525 * Copyright (c) 1993
1526 * The Regents of the University of California. All rights reserved.
1528 * Redistribution and use in source and binary forms, with or without
1529 * modification, are permitted provided that the following conditions
1531 * 1. Redistributions of source code must retain the above copyright
1532 * notice, this list of conditions and the following disclaimer.
1533 * 2. Redistributions in binary form must reproduce the above copyright
1534 * notice, this list of conditions and the following disclaimer in the
1535 * documentation and/or other materials provided with the distribution.
1536 * 4. Neither the name of the University nor the names of its contributors
1537 * may be used to endorse or promote products derived from this software
1538 * without specific prior written permission.
1540 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1541 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1542 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1543 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1544 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1545 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1546 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1547 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1548 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1549 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1553 static struct flag {
1555 const wchar_t *wname;
1557 unsigned long clear;
1559 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1561 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1562 { "nosappend", L"nosappend", SF_APPEND, 0 },
1564 #ifdef EXT2_APPEND_FL /* 'a' */
1565 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1566 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1569 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1570 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1573 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1574 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1575 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1577 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1578 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1579 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1580 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1583 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1584 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1587 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1590 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1591 { "nouappend", L"nouappend", UF_APPEND, 0 },
1594 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1595 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1596 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1599 { "nodump", L"nodump", 0, UF_NODUMP},
1601 #ifdef EXT2_NODUMP_FL /* 'd' */
1602 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1605 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1608 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1609 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1611 #ifdef UF_COMPRESSED
1612 { "nocompressed",L"nocompressed", UF_COMPRESSED, 0 },
1615 { "nouunlink", L"nouunlink", EXT2_UNRM_FL, 0},
1618 #ifdef EXT2_BTREE_FL
1619 { "nobtree", L"nobtree", EXT2_BTREE_FL, 0 },
1622 #ifdef EXT2_ECOMPR_FL
1623 { "nocomperr", L"nocomperr", EXT2_ECOMPR_FL, 0 },
1626 #ifdef EXT2_COMPR_FL /* 'c' */
1627 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1630 #ifdef EXT2_NOATIME_FL /* 'A' */
1631 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1634 #ifdef EXT2_DIRTY_FL
1635 { "nocompdirty",L"nocompdirty", EXT2_DIRTY_FL, 0},
1638 #ifdef EXT2_COMPRBLK_FL
1639 #ifdef EXT2_NOCOMPR_FL
1640 { "nocomprblk", L"nocomprblk", EXT2_COMPRBLK_FL, EXT2_NOCOMPR_FL},
1642 { "nocomprblk", L"nocomprblk", EXT2_COMPRBLK_FL, 0},
1645 #ifdef EXT2_DIRSYNC_FL
1646 { "nodirsync", L"nodirsync", EXT2_DIRSYNC_FL, 0},
1648 #ifdef EXT2_INDEX_FL
1649 { "nohashidx", L"nohashidx", EXT2_INDEX_FL, 0},
1651 #ifdef EXT2_IMAGIC_FL
1652 { "noimagic", L"noimagic", EXT2_IMAGIC_FL, 0},
1654 #ifdef EXT3_JOURNAL_DATA_FL
1655 { "nojournal", L"nojournal", EXT3_JOURNAL_DATA_FL, 0},
1657 #ifdef EXT2_SECRM_FL
1658 { "nosecuredeletion",L"nosecuredeletion",EXT2_SECRM_FL, 0},
1661 { "nosync", L"nosync", EXT2_SYNC_FL, 0},
1663 #ifdef EXT2_NOTAIL_FL
1664 { "notail", L"notail", 0, EXT2_NOTAIL_FL},
1666 #ifdef EXT2_TOPDIR_FL
1667 { "notopdir", L"notopdir", EXT2_TOPDIR_FL, 0},
1669 #ifdef EXT2_RESERVED_FL
1670 { "noreserved", L"noreserved", EXT2_RESERVED_FL, 0},
1673 { NULL, NULL, 0, 0 }
1678 * Convert file flags to a comma-separated string. If no flags
1679 * are set, return the empty string.
1682 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1690 bits = bitset | bitclear;
1692 for (flag = flags; flag->name != NULL; flag++)
1693 if (bits & (flag->set | flag->clear)) {
1694 length += strlen(flag->name) + 1;
1695 bits &= ~(flag->set | flag->clear);
1700 string = (char *)malloc(length);
1705 for (flag = flags; flag->name != NULL; flag++) {
1706 if (bitset & flag->set || bitclear & flag->clear) {
1707 sp = flag->name + 2;
1708 } else if (bitset & flag->clear || bitclear & flag->set) {
1712 bitset &= ~(flag->set | flag->clear);
1713 bitclear &= ~(flag->set | flag->clear);
1716 while ((*dp++ = *sp++) != '\0')
1727 * Take string of arguments and return file flags. This
1728 * version works a little differently than strtofflags(3).
1729 * In particular, it always tests every token, skipping any
1730 * unrecognized tokens. It returns a pointer to the first
1731 * unrecognized token, or NULL if every token was recognized.
1732 * This version is also const-correct and does not modify the
1736 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
1738 const char *start, *end;
1740 unsigned long set, clear;
1746 /* Find start of first token. */
1747 while (*start == '\t' || *start == ' ' || *start == ',')
1749 while (*start != '\0') {
1751 /* Locate end of token. */
1753 while (*end != '\0' && *end != '\t' &&
1754 *end != ' ' && *end != ',')
1756 length = end - start;
1757 for (flag = flags; flag->name != NULL; flag++) {
1758 size_t flag_length = strlen(flag->name);
1759 if (length == flag_length
1760 && memcmp(start, flag->name, length) == 0) {
1761 /* Matched "noXXXX", so reverse the sense. */
1765 } else if (length == flag_length - 2
1766 && memcmp(start, flag->name + 2, length) == 0) {
1767 /* Matched "XXXX", so don't reverse. */
1769 clear |= flag->clear;
1773 /* Ignore unknown flag names. */
1774 if (flag->name == NULL && failed == NULL)
1777 /* Find start of next token. */
1779 while (*start == '\t' || *start == ' ' || *start == ',')
1789 /* Return location of first failure. */
1795 * Take string of arguments and return file flags. This
1796 * version works a little differently than strtofflags(3).
1797 * In particular, it always tests every token, skipping any
1798 * unrecognized tokens. It returns a pointer to the first
1799 * unrecognized token, or NULL if every token was recognized.
1800 * This version is also const-correct and does not modify the
1803 static const wchar_t *
1804 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1806 const wchar_t *start, *end;
1808 unsigned long set, clear;
1809 const wchar_t *failed;
1814 /* Find start of first token. */
1815 while (*start == L'\t' || *start == L' ' || *start == L',')
1817 while (*start != L'\0') {
1819 /* Locate end of token. */
1821 while (*end != L'\0' && *end != L'\t' &&
1822 *end != L' ' && *end != L',')
1824 length = end - start;
1825 for (flag = flags; flag->wname != NULL; flag++) {
1826 size_t flag_length = wcslen(flag->wname);
1827 if (length == flag_length
1828 && wmemcmp(start, flag->wname, length) == 0) {
1829 /* Matched "noXXXX", so reverse the sense. */
1833 } else if (length == flag_length - 2
1834 && wmemcmp(start, flag->wname + 2, length) == 0) {
1835 /* Matched "XXXX", so don't reverse. */
1837 clear |= flag->clear;
1841 /* Ignore unknown flag names. */
1842 if (flag->wname == NULL && failed == NULL)
1845 /* Find start of next token. */
1847 while (*start == L'\t' || *start == L' ' || *start == L',')
1857 /* Return location of first failure. */
1865 main(int argc, char **argv)
1867 struct archive_entry *entry = archive_entry_new();
1868 unsigned long set, clear;
1869 const wchar_t *remainder;
1871 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1872 archive_entry_fflags(entry, &set, &clear);
1874 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1876 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));