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 ACL data over. */
205 archive_acl_copy(&entry2->acl, &entry->acl);
207 /* Copy Mac OS metadata. */
208 p = archive_entry_mac_metadata(entry, &s);
209 archive_entry_copy_mac_metadata(entry2, p, s);
211 /* Copy xattr data over. */
212 xp = entry->xattr_head;
214 archive_entry_xattr_add_entry(entry2,
215 xp->name, xp->value, xp->size);
219 /* Copy sparse data over. */
220 sp = entry->sparse_head;
222 archive_entry_sparse_add_entry(entry2,
223 sp->offset, sp->length);
231 archive_entry_free(struct archive_entry *entry)
233 archive_entry_clear(entry);
237 struct archive_entry *
238 archive_entry_new(void)
240 return archive_entry_new2(NULL);
243 struct archive_entry *
244 archive_entry_new2(struct archive *a)
246 struct archive_entry *entry;
248 entry = (struct archive_entry *)malloc(sizeof(*entry));
251 memset(entry, 0, sizeof(*entry));
257 * Functions for reading fields from an archive_entry.
261 archive_entry_atime(struct archive_entry *entry)
263 return (entry->ae_stat.aest_atime);
267 archive_entry_atime_nsec(struct archive_entry *entry)
269 return (entry->ae_stat.aest_atime_nsec);
273 archive_entry_atime_is_set(struct archive_entry *entry)
275 return (entry->ae_set & AE_SET_ATIME);
279 archive_entry_birthtime(struct archive_entry *entry)
281 return (entry->ae_stat.aest_birthtime);
285 archive_entry_birthtime_nsec(struct archive_entry *entry)
287 return (entry->ae_stat.aest_birthtime_nsec);
291 archive_entry_birthtime_is_set(struct archive_entry *entry)
293 return (entry->ae_set & AE_SET_BIRTHTIME);
297 archive_entry_ctime(struct archive_entry *entry)
299 return (entry->ae_stat.aest_ctime);
303 archive_entry_ctime_is_set(struct archive_entry *entry)
305 return (entry->ae_set & AE_SET_CTIME);
309 archive_entry_ctime_nsec(struct archive_entry *entry)
311 return (entry->ae_stat.aest_ctime_nsec);
315 archive_entry_dev(struct archive_entry *entry)
317 if (entry->ae_stat.aest_dev_is_broken_down)
318 return ae_makedev(entry->ae_stat.aest_devmajor,
319 entry->ae_stat.aest_devminor);
321 return (entry->ae_stat.aest_dev);
325 archive_entry_dev_is_set(struct archive_entry *entry)
327 return (entry->ae_set & AE_SET_DEV);
331 archive_entry_devmajor(struct archive_entry *entry)
333 if (entry->ae_stat.aest_dev_is_broken_down)
334 return (entry->ae_stat.aest_devmajor);
336 return major(entry->ae_stat.aest_dev);
340 archive_entry_devminor(struct archive_entry *entry)
342 if (entry->ae_stat.aest_dev_is_broken_down)
343 return (entry->ae_stat.aest_devminor);
345 return minor(entry->ae_stat.aest_dev);
349 archive_entry_filetype(struct archive_entry *entry)
351 return (AE_IFMT & entry->acl.mode);
355 archive_entry_fflags(struct archive_entry *entry,
356 unsigned long *set, unsigned long *clear)
358 *set = entry->ae_fflags_set;
359 *clear = entry->ae_fflags_clear;
363 * Note: if text was provided, this just returns that text. If you
364 * really need the text to be rebuilt in a canonical form, set the
365 * text, ask for the bitmaps, then set the bitmaps. (Setting the
366 * bitmaps clears any stored text.) This design is deliberate: if
367 * we're editing archives, we don't want to discard flags just because
368 * they aren't supported on the current system. The bitmap<->text
369 * conversions are platform-specific (see below).
372 archive_entry_fflags_text(struct archive_entry *entry)
377 if (archive_mstring_get_mbs(entry->archive,
378 &entry->ae_fflags_text, &f) == 0) {
381 } else if (errno == ENOMEM)
382 __archive_errx(1, "No memory");
384 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
387 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
391 archive_mstring_copy_mbs(&entry->ae_fflags_text, p);
393 if (archive_mstring_get_mbs(entry->archive,
394 &entry->ae_fflags_text, &f) == 0)
397 __archive_errx(1, "No memory");
402 archive_entry_gid(struct archive_entry *entry)
404 return (entry->ae_stat.aest_gid);
408 archive_entry_gname(struct archive_entry *entry)
411 if (archive_mstring_get_mbs(entry->archive, &entry->ae_gname, &p) == 0)
414 __archive_errx(1, "No memory");
419 archive_entry_gname_w(struct archive_entry *entry)
422 if (archive_mstring_get_wcs(entry->archive, &entry->ae_gname, &p) == 0)
425 __archive_errx(1, "No memory");
430 _archive_entry_gname_l(struct archive_entry *entry,
431 const char **p, size_t *len, struct archive_string_conv *sc)
433 return (archive_mstring_get_mbs_l(&entry->ae_gname, p, len, sc));
437 archive_entry_hardlink(struct archive_entry *entry)
440 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
442 if (archive_mstring_get_mbs(
443 entry->archive, &entry->ae_hardlink, &p) == 0)
446 __archive_errx(1, "No memory");
451 archive_entry_hardlink_w(struct archive_entry *entry)
454 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
456 if (archive_mstring_get_wcs(
457 entry->archive, &entry->ae_hardlink, &p) == 0)
460 __archive_errx(1, "No memory");
465 _archive_entry_hardlink_l(struct archive_entry *entry,
466 const char **p, size_t *len, struct archive_string_conv *sc)
468 if ((entry->ae_set & AE_SET_HARDLINK) == 0) {
473 return (archive_mstring_get_mbs_l(&entry->ae_hardlink, p, len, sc));
477 archive_entry_ino(struct archive_entry *entry)
479 return (entry->ae_stat.aest_ino);
483 archive_entry_ino_is_set(struct archive_entry *entry)
485 return (entry->ae_set & AE_SET_INO);
489 archive_entry_ino64(struct archive_entry *entry)
491 return (entry->ae_stat.aest_ino);
495 archive_entry_mode(struct archive_entry *entry)
497 return (entry->acl.mode);
501 archive_entry_mtime(struct archive_entry *entry)
503 return (entry->ae_stat.aest_mtime);
507 archive_entry_mtime_nsec(struct archive_entry *entry)
509 return (entry->ae_stat.aest_mtime_nsec);
513 archive_entry_mtime_is_set(struct archive_entry *entry)
515 return (entry->ae_set & AE_SET_MTIME);
519 archive_entry_nlink(struct archive_entry *entry)
521 return (entry->ae_stat.aest_nlink);
525 archive_entry_pathname(struct archive_entry *entry)
528 if (archive_mstring_get_mbs(
529 entry->archive, &entry->ae_pathname, &p) == 0)
532 __archive_errx(1, "No memory");
537 archive_entry_pathname_w(struct archive_entry *entry)
540 if (archive_mstring_get_wcs(
541 entry->archive, &entry->ae_pathname, &p) == 0)
544 __archive_errx(1, "No memory");
549 _archive_entry_pathname_l(struct archive_entry *entry,
550 const char **p, size_t *len, struct archive_string_conv *sc)
552 return (archive_mstring_get_mbs_l(&entry->ae_pathname, p, len, sc));
556 archive_entry_perm(struct archive_entry *entry)
558 return (~AE_IFMT & entry->acl.mode);
562 archive_entry_rdev(struct archive_entry *entry)
564 if (entry->ae_stat.aest_rdev_is_broken_down)
565 return ae_makedev(entry->ae_stat.aest_rdevmajor,
566 entry->ae_stat.aest_rdevminor);
568 return (entry->ae_stat.aest_rdev);
572 archive_entry_rdevmajor(struct archive_entry *entry)
574 if (entry->ae_stat.aest_rdev_is_broken_down)
575 return (entry->ae_stat.aest_rdevmajor);
577 return major(entry->ae_stat.aest_rdev);
581 archive_entry_rdevminor(struct archive_entry *entry)
583 if (entry->ae_stat.aest_rdev_is_broken_down)
584 return (entry->ae_stat.aest_rdevminor);
586 return minor(entry->ae_stat.aest_rdev);
590 archive_entry_size(struct archive_entry *entry)
592 return (entry->ae_stat.aest_size);
596 archive_entry_size_is_set(struct archive_entry *entry)
598 return (entry->ae_set & AE_SET_SIZE);
602 archive_entry_sourcepath(struct archive_entry *entry)
605 if (archive_mstring_get_mbs(
606 entry->archive, &entry->ae_sourcepath, &p) == 0)
609 __archive_errx(1, "No memory");
614 archive_entry_sourcepath_w(struct archive_entry *entry)
617 if (archive_mstring_get_wcs(
618 entry->archive, &entry->ae_sourcepath, &p) == 0)
624 archive_entry_symlink(struct archive_entry *entry)
627 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
629 if (archive_mstring_get_mbs(
630 entry->archive, &entry->ae_symlink, &p) == 0)
633 __archive_errx(1, "No memory");
638 archive_entry_symlink_w(struct archive_entry *entry)
641 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
643 if (archive_mstring_get_wcs(
644 entry->archive, &entry->ae_symlink, &p) == 0)
647 __archive_errx(1, "No memory");
652 _archive_entry_symlink_l(struct archive_entry *entry,
653 const char **p, size_t *len, struct archive_string_conv *sc)
655 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
660 return (archive_mstring_get_mbs_l( &entry->ae_symlink, p, len, sc));
664 archive_entry_uid(struct archive_entry *entry)
666 return (entry->ae_stat.aest_uid);
670 archive_entry_uname(struct archive_entry *entry)
673 if (archive_mstring_get_mbs(entry->archive, &entry->ae_uname, &p) == 0)
676 __archive_errx(1, "No memory");
681 archive_entry_uname_w(struct archive_entry *entry)
684 if (archive_mstring_get_wcs(entry->archive, &entry->ae_uname, &p) == 0)
687 __archive_errx(1, "No memory");
692 _archive_entry_uname_l(struct archive_entry *entry,
693 const char **p, size_t *len, struct archive_string_conv *sc)
695 return (archive_mstring_get_mbs_l(&entry->ae_uname, p, len, sc));
699 * Functions to set archive_entry properties.
703 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
705 entry->stat_valid = 0;
706 entry->acl.mode &= ~AE_IFMT;
707 entry->acl.mode |= AE_IFMT & type;
711 archive_entry_set_fflags(struct archive_entry *entry,
712 unsigned long set, unsigned long clear)
714 archive_mstring_clean(&entry->ae_fflags_text);
715 entry->ae_fflags_set = set;
716 entry->ae_fflags_clear = clear;
720 archive_entry_copy_fflags_text(struct archive_entry *entry,
723 archive_mstring_copy_mbs(&entry->ae_fflags_text, flags);
724 return (ae_strtofflags(flags,
725 &entry->ae_fflags_set, &entry->ae_fflags_clear));
729 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
730 const wchar_t *flags)
732 archive_mstring_copy_wcs(&entry->ae_fflags_text, flags);
733 return (ae_wcstofflags(flags,
734 &entry->ae_fflags_set, &entry->ae_fflags_clear));
738 archive_entry_set_gid(struct archive_entry *entry, int64_t g)
740 entry->stat_valid = 0;
741 entry->ae_stat.aest_gid = g;
745 archive_entry_set_gname(struct archive_entry *entry, const char *name)
747 archive_mstring_copy_mbs(&entry->ae_gname, name);
751 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
753 archive_mstring_copy_mbs(&entry->ae_gname, name);
757 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
759 archive_mstring_copy_wcs(&entry->ae_gname, name);
763 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
765 if (archive_mstring_update_utf8(entry->archive,
766 &entry->ae_gname, name) == 0)
769 __archive_errx(1, "No memory");
774 _archive_entry_copy_gname_l(struct archive_entry *entry,
775 const char *name, size_t len, struct archive_string_conv *sc)
777 return (archive_mstring_copy_mbs_len_l(&entry->ae_gname, name, len, sc));
781 archive_entry_set_ino(struct archive_entry *entry, int64_t ino)
783 entry->stat_valid = 0;
784 entry->ae_set |= AE_SET_INO;
785 entry->ae_stat.aest_ino = ino;
789 archive_entry_set_ino64(struct archive_entry *entry, int64_t ino)
791 entry->stat_valid = 0;
792 entry->ae_set |= AE_SET_INO;
793 entry->ae_stat.aest_ino = ino;
797 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
799 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
801 entry->ae_set |= AE_SET_HARDLINK;
803 entry->ae_set &= ~AE_SET_HARDLINK;
807 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
809 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
811 entry->ae_set |= AE_SET_HARDLINK;
813 entry->ae_set &= ~AE_SET_HARDLINK;
817 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
819 archive_mstring_copy_wcs(&entry->ae_hardlink, target);
821 entry->ae_set |= AE_SET_HARDLINK;
823 entry->ae_set &= ~AE_SET_HARDLINK;
827 archive_entry_update_hardlink_utf8(struct archive_entry *entry, const char *target)
830 entry->ae_set |= AE_SET_HARDLINK;
832 entry->ae_set &= ~AE_SET_HARDLINK;
833 if (archive_mstring_update_utf8(entry->archive,
834 &entry->ae_hardlink, target) == 0)
837 __archive_errx(1, "No memory");
842 _archive_entry_copy_hardlink_l(struct archive_entry *entry,
843 const char *target, size_t len, struct archive_string_conv *sc)
847 r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
849 if (target != NULL && r == 0)
850 entry->ae_set |= AE_SET_HARDLINK;
852 entry->ae_set &= ~AE_SET_HARDLINK;
857 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
860 entry->stat_valid = 0;
861 entry->ae_set |= AE_SET_ATIME;
862 entry->ae_stat.aest_atime = t;
863 entry->ae_stat.aest_atime_nsec = ns;
867 archive_entry_unset_atime(struct archive_entry *entry)
869 archive_entry_set_atime(entry, 0, 0);
870 entry->ae_set &= ~AE_SET_ATIME;
874 archive_entry_set_birthtime(struct archive_entry *entry, time_t t, long ns)
877 entry->stat_valid = 0;
878 entry->ae_set |= AE_SET_BIRTHTIME;
879 entry->ae_stat.aest_birthtime = t;
880 entry->ae_stat.aest_birthtime_nsec = ns;
884 archive_entry_unset_birthtime(struct archive_entry *entry)
886 archive_entry_set_birthtime(entry, 0, 0);
887 entry->ae_set &= ~AE_SET_BIRTHTIME;
891 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
894 entry->stat_valid = 0;
895 entry->ae_set |= AE_SET_CTIME;
896 entry->ae_stat.aest_ctime = t;
897 entry->ae_stat.aest_ctime_nsec = ns;
901 archive_entry_unset_ctime(struct archive_entry *entry)
903 archive_entry_set_ctime(entry, 0, 0);
904 entry->ae_set &= ~AE_SET_CTIME;
908 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
910 entry->stat_valid = 0;
911 entry->ae_set |= AE_SET_DEV;
912 entry->ae_stat.aest_dev_is_broken_down = 0;
913 entry->ae_stat.aest_dev = d;
917 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
919 entry->stat_valid = 0;
920 entry->ae_set |= AE_SET_DEV;
921 entry->ae_stat.aest_dev_is_broken_down = 1;
922 entry->ae_stat.aest_devmajor = m;
926 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
928 entry->stat_valid = 0;
929 entry->ae_set |= AE_SET_DEV;
930 entry->ae_stat.aest_dev_is_broken_down = 1;
931 entry->ae_stat.aest_devminor = m;
934 /* Set symlink if symlink is already set, else set hardlink. */
936 archive_entry_set_link(struct archive_entry *entry, const char *target)
938 if (entry->ae_set & AE_SET_SYMLINK)
939 archive_mstring_copy_mbs(&entry->ae_symlink, target);
941 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
944 /* Set symlink if symlink is already set, else set hardlink. */
946 archive_entry_copy_link(struct archive_entry *entry, const char *target)
948 if (entry->ae_set & AE_SET_SYMLINK)
949 archive_mstring_copy_mbs(&entry->ae_symlink, target);
951 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
954 /* Set symlink if symlink is already set, else set hardlink. */
956 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
958 if (entry->ae_set & AE_SET_SYMLINK)
959 archive_mstring_copy_wcs(&entry->ae_symlink, target);
961 archive_mstring_copy_wcs(&entry->ae_hardlink, target);
965 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
968 if (entry->ae_set & AE_SET_SYMLINK)
969 r = archive_mstring_update_utf8(entry->archive,
970 &entry->ae_symlink, target);
972 r = archive_mstring_update_utf8(entry->archive,
973 &entry->ae_hardlink, target);
977 __archive_errx(1, "No memory");
982 _archive_entry_copy_link_l(struct archive_entry *entry,
983 const char *target, size_t len, struct archive_string_conv *sc)
987 if (entry->ae_set & AE_SET_SYMLINK)
988 r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
991 r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
997 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
999 entry->stat_valid = 0;
1000 entry->acl.mode = m;
1004 archive_entry_set_mtime(struct archive_entry *entry, time_t t, long ns)
1007 entry->stat_valid = 0;
1008 entry->ae_set |= AE_SET_MTIME;
1009 entry->ae_stat.aest_mtime = t;
1010 entry->ae_stat.aest_mtime_nsec = ns;
1014 archive_entry_unset_mtime(struct archive_entry *entry)
1016 archive_entry_set_mtime(entry, 0, 0);
1017 entry->ae_set &= ~AE_SET_MTIME;
1021 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
1023 entry->stat_valid = 0;
1024 entry->ae_stat.aest_nlink = nlink;
1028 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
1030 archive_mstring_copy_mbs(&entry->ae_pathname, name);
1034 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
1036 archive_mstring_copy_mbs(&entry->ae_pathname, name);
1040 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
1042 archive_mstring_copy_wcs(&entry->ae_pathname, name);
1046 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
1048 if (archive_mstring_update_utf8(entry->archive,
1049 &entry->ae_pathname, name) == 0)
1051 if (errno == ENOMEM)
1052 __archive_errx(1, "No memory");
1057 _archive_entry_copy_pathname_l(struct archive_entry *entry,
1058 const char *name, size_t len, struct archive_string_conv *sc)
1060 return (archive_mstring_copy_mbs_len_l(&entry->ae_pathname,
1065 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
1067 entry->stat_valid = 0;
1068 entry->acl.mode &= AE_IFMT;
1069 entry->acl.mode |= ~AE_IFMT & p;
1073 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
1075 entry->stat_valid = 0;
1076 entry->ae_stat.aest_rdev = m;
1077 entry->ae_stat.aest_rdev_is_broken_down = 0;
1081 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
1083 entry->stat_valid = 0;
1084 entry->ae_stat.aest_rdev_is_broken_down = 1;
1085 entry->ae_stat.aest_rdevmajor = m;
1089 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
1091 entry->stat_valid = 0;
1092 entry->ae_stat.aest_rdev_is_broken_down = 1;
1093 entry->ae_stat.aest_rdevminor = m;
1097 archive_entry_set_size(struct archive_entry *entry, int64_t s)
1099 entry->stat_valid = 0;
1100 entry->ae_stat.aest_size = s;
1101 entry->ae_set |= AE_SET_SIZE;
1105 archive_entry_unset_size(struct archive_entry *entry)
1107 archive_entry_set_size(entry, 0);
1108 entry->ae_set &= ~AE_SET_SIZE;
1112 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1114 archive_mstring_copy_mbs(&entry->ae_sourcepath, path);
1118 archive_entry_copy_sourcepath_w(struct archive_entry *entry, const wchar_t *path)
1120 archive_mstring_copy_wcs(&entry->ae_sourcepath, path);
1124 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1126 archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
1127 if (linkname != NULL)
1128 entry->ae_set |= AE_SET_SYMLINK;
1130 entry->ae_set &= ~AE_SET_SYMLINK;
1134 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1136 archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
1137 if (linkname != NULL)
1138 entry->ae_set |= AE_SET_SYMLINK;
1140 entry->ae_set &= ~AE_SET_SYMLINK;
1144 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1146 archive_mstring_copy_wcs(&entry->ae_symlink, linkname);
1147 if (linkname != NULL)
1148 entry->ae_set |= AE_SET_SYMLINK;
1150 entry->ae_set &= ~AE_SET_SYMLINK;
1154 archive_entry_update_symlink_utf8(struct archive_entry *entry, const char *linkname)
1156 if (linkname != NULL)
1157 entry->ae_set |= AE_SET_SYMLINK;
1159 entry->ae_set &= ~AE_SET_SYMLINK;
1160 if (archive_mstring_update_utf8(entry->archive,
1161 &entry->ae_symlink, linkname) == 0)
1163 if (errno == ENOMEM)
1164 __archive_errx(1, "No memory");
1169 _archive_entry_copy_symlink_l(struct archive_entry *entry,
1170 const char *linkname, size_t len, struct archive_string_conv *sc)
1174 r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
1176 if (linkname != NULL && r == 0)
1177 entry->ae_set |= AE_SET_SYMLINK;
1179 entry->ae_set &= ~AE_SET_SYMLINK;
1184 archive_entry_set_uid(struct archive_entry *entry, int64_t u)
1186 entry->stat_valid = 0;
1187 entry->ae_stat.aest_uid = u;
1191 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1193 archive_mstring_copy_mbs(&entry->ae_uname, name);
1197 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1199 archive_mstring_copy_mbs(&entry->ae_uname, name);
1203 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1205 archive_mstring_copy_wcs(&entry->ae_uname, name);
1209 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1211 if (archive_mstring_update_utf8(entry->archive,
1212 &entry->ae_uname, name) == 0)
1214 if (errno == ENOMEM)
1215 __archive_errx(1, "No memory");
1220 _archive_entry_copy_uname_l(struct archive_entry *entry,
1221 const char *name, size_t len, struct archive_string_conv *sc)
1223 return (archive_mstring_copy_mbs_len_l(&entry->ae_uname,
1228 archive_entry_mac_metadata(struct archive_entry *entry, size_t *s)
1230 *s = entry->mac_metadata_size;
1231 return entry->mac_metadata;
1235 archive_entry_copy_mac_metadata(struct archive_entry *entry,
1236 const void *p, size_t s)
1238 free(entry->mac_metadata);
1239 if (p == NULL || s == 0) {
1240 entry->mac_metadata = NULL;
1241 entry->mac_metadata_size = 0;
1243 entry->mac_metadata_size = s;
1244 entry->mac_metadata = malloc(s);
1245 if (entry->mac_metadata == NULL)
1247 memcpy(entry->mac_metadata, p, s);
1252 * ACL management. The following would, of course, be a lot simpler
1253 * if: 1) the last draft of POSIX.1e were a really thorough and
1254 * complete standard that addressed the needs of ACL archiving and 2)
1255 * everyone followed it faithfully. Alas, neither is true, so the
1256 * following is a lot more complex than might seem necessary to the
1260 struct archive_acl *
1261 archive_entry_acl(struct archive_entry *entry)
1267 archive_entry_acl_clear(struct archive_entry *entry)
1269 archive_acl_clear(&entry->acl);
1273 * Add a single ACL entry to the internal list of ACL data.
1276 archive_entry_acl_add_entry(struct archive_entry *entry,
1277 int type, int permset, int tag, int id, const char *name)
1279 return archive_acl_add_entry(&entry->acl, type, permset, tag, id, name);
1283 * As above, but with a wide-character name.
1286 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1287 int type, int permset, int tag, int id, const wchar_t *name)
1289 return archive_acl_add_entry_w_len(&entry->acl,
1290 type, permset, tag, id, name, wcslen(name));
1294 * Return a count of entries matching "want_type".
1297 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1299 return archive_acl_count(&entry->acl, want_type);
1303 * Prepare for reading entries from the ACL data. Returns a count
1304 * of entries matching "want_type", or zero if there are no
1305 * non-extended ACL entries of that type.
1308 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1310 return archive_acl_reset(&entry->acl, want_type);
1314 * Return the next ACL entry in the list. Fake entries for the
1315 * standard permissions and include them in the returned list.
1318 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1319 int *permset, int *tag, int *id, const char **name)
1322 r = archive_acl_next(entry->archive, &entry->acl, want_type, type,
1323 permset, tag, id, name);
1324 if (r == ARCHIVE_FATAL && errno == ENOMEM)
1325 __archive_errx(1, "No memory");
1330 * Generate a text version of the ACL. The flags parameter controls
1331 * the style of the generated ACL.
1334 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1337 r = archive_acl_text_w(entry->archive, &entry->acl, flags);
1338 if (r == NULL && errno == ENOMEM)
1339 __archive_errx(1, "No memory");
1344 archive_entry_acl_text(struct archive_entry *entry, int flags)
1347 if (archive_acl_text_l(&entry->acl, flags, &p, NULL, NULL) != 0
1349 __archive_errx(1, "No memory");
1354 _archive_entry_acl_text_l(struct archive_entry *entry, int flags,
1355 const char **acl_text, size_t *len, struct archive_string_conv *sc)
1357 return (archive_acl_text_l(&entry->acl, flags, acl_text, len, sc));
1361 * Following code is modified from UC Berkeley sources, and
1362 * is subject to the following copyright notice.
1366 * Copyright (c) 1993
1367 * The Regents of the University of California. All rights reserved.
1369 * Redistribution and use in source and binary forms, with or without
1370 * modification, are permitted provided that the following conditions
1372 * 1. Redistributions of source code must retain the above copyright
1373 * notice, this list of conditions and the following disclaimer.
1374 * 2. Redistributions in binary form must reproduce the above copyright
1375 * notice, this list of conditions and the following disclaimer in the
1376 * documentation and/or other materials provided with the distribution.
1377 * 4. Neither the name of the University nor the names of its contributors
1378 * may be used to endorse or promote products derived from this software
1379 * without specific prior written permission.
1381 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1382 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1383 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1384 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1385 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1386 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1387 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1388 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1389 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1390 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1394 static struct flag {
1396 const wchar_t *wname;
1398 unsigned long clear;
1400 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1402 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1403 { "nosappend", L"nosappend", SF_APPEND, 0 },
1405 #ifdef EXT2_APPEND_FL /* 'a' */
1406 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1407 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1410 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1411 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1414 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1415 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1416 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1418 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1419 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1420 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1421 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1424 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1425 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1428 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1431 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1432 { "nouappend", L"nouappend", UF_APPEND, 0 },
1435 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1436 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1437 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1440 { "nodump", L"nodump", 0, UF_NODUMP},
1442 #ifdef EXT2_NODUMP_FL /* 'd' */
1443 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1446 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1449 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1450 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1452 #ifdef UF_COMPRESSED
1453 { "nocompressed",L"nocompressed", UF_COMPRESSED, 0 },
1456 { "nouunlink", L"nouunlink", EXT2_UNRM_FL, 0},
1459 #ifdef EXT2_BTREE_FL
1460 { "nobtree", L"nobtree", EXT2_BTREE_FL, 0 },
1463 #ifdef EXT2_ECOMPR_FL
1464 { "nocomperr", L"nocomperr", EXT2_ECOMPR_FL, 0 },
1467 #ifdef EXT2_COMPR_FL /* 'c' */
1468 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1471 #ifdef EXT2_NOATIME_FL /* 'A' */
1472 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1475 #ifdef EXT2_DIRTY_FL
1476 { "nocompdirty",L"nocompdirty", EXT2_DIRTY_FL, 0},
1479 #ifdef EXT2_COMPRBLK_FL
1480 #ifdef EXT2_NOCOMPR_FL
1481 { "nocomprblk", L"nocomprblk", EXT2_COMPRBLK_FL, EXT2_NOCOMPR_FL},
1483 { "nocomprblk", L"nocomprblk", EXT2_COMPRBLK_FL, 0},
1486 #ifdef EXT2_DIRSYNC_FL
1487 { "nodirsync", L"nodirsync", EXT2_DIRSYNC_FL, 0},
1489 #ifdef EXT2_INDEX_FL
1490 { "nohashidx", L"nohashidx", EXT2_INDEX_FL, 0},
1492 #ifdef EXT2_IMAGIC_FL
1493 { "noimagic", L"noimagic", EXT2_IMAGIC_FL, 0},
1495 #ifdef EXT3_JOURNAL_DATA_FL
1496 { "nojournal", L"nojournal", EXT3_JOURNAL_DATA_FL, 0},
1498 #ifdef EXT2_SECRM_FL
1499 { "nosecuredeletion",L"nosecuredeletion",EXT2_SECRM_FL, 0},
1502 { "nosync", L"nosync", EXT2_SYNC_FL, 0},
1504 #ifdef EXT2_NOTAIL_FL
1505 { "notail", L"notail", 0, EXT2_NOTAIL_FL},
1507 #ifdef EXT2_TOPDIR_FL
1508 { "notopdir", L"notopdir", EXT2_TOPDIR_FL, 0},
1510 #ifdef EXT2_RESERVED_FL
1511 { "noreserved", L"noreserved", EXT2_RESERVED_FL, 0},
1514 { NULL, NULL, 0, 0 }
1519 * Convert file flags to a comma-separated string. If no flags
1520 * are set, return the empty string.
1523 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1531 bits = bitset | bitclear;
1533 for (flag = flags; flag->name != NULL; flag++)
1534 if (bits & (flag->set | flag->clear)) {
1535 length += strlen(flag->name) + 1;
1536 bits &= ~(flag->set | flag->clear);
1541 string = (char *)malloc(length);
1546 for (flag = flags; flag->name != NULL; flag++) {
1547 if (bitset & flag->set || bitclear & flag->clear) {
1548 sp = flag->name + 2;
1549 } else if (bitset & flag->clear || bitclear & flag->set) {
1553 bitset &= ~(flag->set | flag->clear);
1554 bitclear &= ~(flag->set | flag->clear);
1557 while ((*dp++ = *sp++) != '\0')
1568 * Take string of arguments and return file flags. This
1569 * version works a little differently than strtofflags(3).
1570 * In particular, it always tests every token, skipping any
1571 * unrecognized tokens. It returns a pointer to the first
1572 * unrecognized token, or NULL if every token was recognized.
1573 * This version is also const-correct and does not modify the
1577 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
1579 const char *start, *end;
1581 unsigned long set, clear;
1587 /* Find start of first token. */
1588 while (*start == '\t' || *start == ' ' || *start == ',')
1590 while (*start != '\0') {
1591 /* Locate end of token. */
1593 while (*end != '\0' && *end != '\t' &&
1594 *end != ' ' && *end != ',')
1596 for (flag = flags; flag->name != NULL; flag++) {
1597 if (memcmp(start, flag->name, end - start) == 0) {
1598 /* Matched "noXXXX", so reverse the sense. */
1602 } else if (memcmp(start, flag->name + 2, end - start)
1604 /* Matched "XXXX", so don't reverse. */
1606 clear |= flag->clear;
1610 /* Ignore unknown flag names. */
1611 if (flag->name == NULL && failed == NULL)
1614 /* Find start of next token. */
1616 while (*start == '\t' || *start == ' ' || *start == ',')
1626 /* Return location of first failure. */
1632 * Take string of arguments and return file flags. This
1633 * version works a little differently than strtofflags(3).
1634 * In particular, it always tests every token, skipping any
1635 * unrecognized tokens. It returns a pointer to the first
1636 * unrecognized token, or NULL if every token was recognized.
1637 * This version is also const-correct and does not modify the
1640 static const wchar_t *
1641 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1643 const wchar_t *start, *end;
1645 unsigned long set, clear;
1646 const wchar_t *failed;
1651 /* Find start of first token. */
1652 while (*start == L'\t' || *start == L' ' || *start == L',')
1654 while (*start != L'\0') {
1655 /* Locate end of token. */
1657 while (*end != L'\0' && *end != L'\t' &&
1658 *end != L' ' && *end != L',')
1660 for (flag = flags; flag->wname != NULL; flag++) {
1661 if (wmemcmp(start, flag->wname, end - start) == 0) {
1662 /* Matched "noXXXX", so reverse the sense. */
1666 } else if (wmemcmp(start, flag->wname + 2, end - start)
1668 /* Matched "XXXX", so don't reverse. */
1670 clear |= flag->clear;
1674 /* Ignore unknown flag names. */
1675 if (flag->wname == NULL && failed == NULL)
1678 /* Find start of next token. */
1680 while (*start == L'\t' || *start == L' ' || *start == L',')
1690 /* Return location of first failure. */
1698 main(int argc, char **argv)
1700 struct archive_entry *entry = archive_entry_new();
1701 unsigned long set, clear;
1702 const wchar_t *remainder;
1704 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1705 archive_entry_fflags(entry, &set, &clear);
1707 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1709 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));