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: head/lib/libarchive/archive_entry.c 201096 2009-12-28 02:41:27Z kientzle $");
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 && f != NULL)
381 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
384 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
388 archive_mstring_copy_mbs(&entry->ae_fflags_text, p);
390 if (archive_mstring_get_mbs(entry->archive,
391 &entry->ae_fflags_text, &f) == 0)
397 archive_entry_gid(struct archive_entry *entry)
399 return (entry->ae_stat.aest_gid);
403 archive_entry_gname(struct archive_entry *entry)
406 if (archive_mstring_get_mbs(entry->archive, &entry->ae_gname, &p) == 0)
412 archive_entry_gname_w(struct archive_entry *entry)
415 if (archive_mstring_get_wcs(entry->archive, &entry->ae_gname, &p) == 0)
421 _archive_entry_gname_l(struct archive_entry *entry,
422 const char **p, size_t *len, struct archive_string_conv *sc)
424 return (archive_mstring_get_mbs_l(&entry->ae_gname, p, len, sc));
428 archive_entry_hardlink(struct archive_entry *entry)
431 if ((entry->ae_set & AE_SET_HARDLINK) && archive_mstring_get_mbs(
432 entry->archive, &entry->ae_hardlink, &p) == 0)
438 archive_entry_hardlink_w(struct archive_entry *entry)
441 if ((entry->ae_set & AE_SET_HARDLINK) && archive_mstring_get_wcs(
442 entry->archive, &entry->ae_hardlink, &p) == 0)
448 _archive_entry_hardlink_l(struct archive_entry *entry,
449 const char **p, size_t *len, struct archive_string_conv *sc)
451 if ((entry->ae_set & AE_SET_HARDLINK) == 0) {
456 return (archive_mstring_get_mbs_l(&entry->ae_hardlink, p, len, sc));
460 archive_entry_ino(struct archive_entry *entry)
462 return (entry->ae_stat.aest_ino);
466 archive_entry_ino_is_set(struct archive_entry *entry)
468 return (entry->ae_set & AE_SET_INO);
472 archive_entry_ino64(struct archive_entry *entry)
474 return (entry->ae_stat.aest_ino);
478 archive_entry_mode(struct archive_entry *entry)
480 return (entry->acl.mode);
484 archive_entry_mtime(struct archive_entry *entry)
486 return (entry->ae_stat.aest_mtime);
490 archive_entry_mtime_nsec(struct archive_entry *entry)
492 return (entry->ae_stat.aest_mtime_nsec);
496 archive_entry_mtime_is_set(struct archive_entry *entry)
498 return (entry->ae_set & AE_SET_MTIME);
502 archive_entry_nlink(struct archive_entry *entry)
504 return (entry->ae_stat.aest_nlink);
508 archive_entry_pathname(struct archive_entry *entry)
511 if (archive_mstring_get_mbs(
512 entry->archive, &entry->ae_pathname, &p) == 0)
518 archive_entry_pathname_w(struct archive_entry *entry)
521 if (archive_mstring_get_wcs(
522 entry->archive, &entry->ae_pathname, &p) == 0)
528 _archive_entry_pathname_l(struct archive_entry *entry,
529 const char **p, size_t *len, struct archive_string_conv *sc)
531 return (archive_mstring_get_mbs_l(&entry->ae_pathname, p, len, sc));
535 archive_entry_perm(struct archive_entry *entry)
537 return (~AE_IFMT & entry->acl.mode);
541 archive_entry_rdev(struct archive_entry *entry)
543 if (entry->ae_stat.aest_rdev_is_broken_down)
544 return ae_makedev(entry->ae_stat.aest_rdevmajor,
545 entry->ae_stat.aest_rdevminor);
547 return (entry->ae_stat.aest_rdev);
551 archive_entry_rdevmajor(struct archive_entry *entry)
553 if (entry->ae_stat.aest_rdev_is_broken_down)
554 return (entry->ae_stat.aest_rdevmajor);
556 return major(entry->ae_stat.aest_rdev);
560 archive_entry_rdevminor(struct archive_entry *entry)
562 if (entry->ae_stat.aest_rdev_is_broken_down)
563 return (entry->ae_stat.aest_rdevminor);
565 return minor(entry->ae_stat.aest_rdev);
569 archive_entry_size(struct archive_entry *entry)
571 return (entry->ae_stat.aest_size);
575 archive_entry_size_is_set(struct archive_entry *entry)
577 return (entry->ae_set & AE_SET_SIZE);
581 archive_entry_sourcepath(struct archive_entry *entry)
584 if (archive_mstring_get_mbs(
585 entry->archive, &entry->ae_sourcepath, &p) == 0)
591 archive_entry_sourcepath_w(struct archive_entry *entry)
594 if (archive_mstring_get_wcs(
595 entry->archive, &entry->ae_sourcepath, &p) == 0)
601 archive_entry_symlink(struct archive_entry *entry)
604 if ((entry->ae_set & AE_SET_SYMLINK) && archive_mstring_get_mbs(
605 entry->archive, &entry->ae_symlink, &p) == 0)
611 archive_entry_symlink_w(struct archive_entry *entry)
614 if ((entry->ae_set & AE_SET_SYMLINK) && archive_mstring_get_wcs(
615 entry->archive, &entry->ae_symlink, &p) == 0)
621 _archive_entry_symlink_l(struct archive_entry *entry,
622 const char **p, size_t *len, struct archive_string_conv *sc)
624 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
629 return (archive_mstring_get_mbs_l( &entry->ae_symlink, p, len, sc));
633 archive_entry_uid(struct archive_entry *entry)
635 return (entry->ae_stat.aest_uid);
639 archive_entry_uname(struct archive_entry *entry)
642 if (archive_mstring_get_mbs(entry->archive, &entry->ae_uname, &p) == 0)
648 archive_entry_uname_w(struct archive_entry *entry)
651 if (archive_mstring_get_wcs(entry->archive, &entry->ae_uname, &p) == 0)
657 _archive_entry_uname_l(struct archive_entry *entry,
658 const char **p, size_t *len, struct archive_string_conv *sc)
660 return (archive_mstring_get_mbs_l(&entry->ae_uname, p, len, sc));
664 * Functions to set archive_entry properties.
668 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
670 entry->stat_valid = 0;
671 entry->acl.mode &= ~AE_IFMT;
672 entry->acl.mode |= AE_IFMT & type;
676 archive_entry_set_fflags(struct archive_entry *entry,
677 unsigned long set, unsigned long clear)
679 archive_mstring_clean(&entry->ae_fflags_text);
680 entry->ae_fflags_set = set;
681 entry->ae_fflags_clear = clear;
685 archive_entry_copy_fflags_text(struct archive_entry *entry,
688 archive_mstring_copy_mbs(&entry->ae_fflags_text, flags);
689 return (ae_strtofflags(flags,
690 &entry->ae_fflags_set, &entry->ae_fflags_clear));
694 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
695 const wchar_t *flags)
697 archive_mstring_copy_wcs(&entry->ae_fflags_text, flags);
698 return (ae_wcstofflags(flags,
699 &entry->ae_fflags_set, &entry->ae_fflags_clear));
703 archive_entry_set_gid(struct archive_entry *entry, int64_t g)
705 entry->stat_valid = 0;
706 entry->ae_stat.aest_gid = g;
710 archive_entry_set_gname(struct archive_entry *entry, const char *name)
712 archive_mstring_copy_mbs(&entry->ae_gname, name);
716 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
718 archive_mstring_copy_mbs(&entry->ae_gname, name);
722 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
724 archive_mstring_copy_wcs(&entry->ae_gname, name);
728 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
730 if (archive_mstring_update_utf8(entry->archive,
731 &entry->ae_gname, name) == 0)
737 _archive_entry_copy_gname_l(struct archive_entry *entry,
738 const char *name, size_t len, struct archive_string_conv *sc)
740 return (archive_mstring_copy_mbs_len_l(&entry->ae_gname, name, len, sc));
744 archive_entry_set_ino(struct archive_entry *entry, int64_t ino)
746 entry->stat_valid = 0;
747 entry->ae_set |= AE_SET_INO;
748 entry->ae_stat.aest_ino = ino;
752 archive_entry_set_ino64(struct archive_entry *entry, int64_t ino)
754 entry->stat_valid = 0;
755 entry->ae_set |= AE_SET_INO;
756 entry->ae_stat.aest_ino = ino;
760 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
762 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
764 entry->ae_set |= AE_SET_HARDLINK;
766 entry->ae_set &= ~AE_SET_HARDLINK;
770 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
772 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
774 entry->ae_set |= AE_SET_HARDLINK;
776 entry->ae_set &= ~AE_SET_HARDLINK;
780 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
782 archive_mstring_copy_wcs(&entry->ae_hardlink, target);
784 entry->ae_set |= AE_SET_HARDLINK;
786 entry->ae_set &= ~AE_SET_HARDLINK;
790 archive_entry_update_hardlink_utf8(struct archive_entry *entry, const char *target)
793 entry->ae_set |= AE_SET_HARDLINK;
795 entry->ae_set &= ~AE_SET_HARDLINK;
796 if (archive_mstring_update_utf8(entry->archive,
797 &entry->ae_hardlink, target) == 0)
803 _archive_entry_copy_hardlink_l(struct archive_entry *entry,
804 const char *target, size_t len, struct archive_string_conv *sc)
808 r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
810 if (target != NULL && r == 0)
811 entry->ae_set |= AE_SET_HARDLINK;
813 entry->ae_set &= ~AE_SET_HARDLINK;
818 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
821 entry->stat_valid = 0;
822 entry->ae_set |= AE_SET_ATIME;
823 entry->ae_stat.aest_atime = t;
824 entry->ae_stat.aest_atime_nsec = ns;
828 archive_entry_unset_atime(struct archive_entry *entry)
830 archive_entry_set_atime(entry, 0, 0);
831 entry->ae_set &= ~AE_SET_ATIME;
835 archive_entry_set_birthtime(struct archive_entry *entry, time_t t, long ns)
838 entry->stat_valid = 0;
839 entry->ae_set |= AE_SET_BIRTHTIME;
840 entry->ae_stat.aest_birthtime = t;
841 entry->ae_stat.aest_birthtime_nsec = ns;
845 archive_entry_unset_birthtime(struct archive_entry *entry)
847 archive_entry_set_birthtime(entry, 0, 0);
848 entry->ae_set &= ~AE_SET_BIRTHTIME;
852 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
855 entry->stat_valid = 0;
856 entry->ae_set |= AE_SET_CTIME;
857 entry->ae_stat.aest_ctime = t;
858 entry->ae_stat.aest_ctime_nsec = ns;
862 archive_entry_unset_ctime(struct archive_entry *entry)
864 archive_entry_set_ctime(entry, 0, 0);
865 entry->ae_set &= ~AE_SET_CTIME;
869 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
871 entry->stat_valid = 0;
872 entry->ae_set |= AE_SET_DEV;
873 entry->ae_stat.aest_dev_is_broken_down = 0;
874 entry->ae_stat.aest_dev = d;
878 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
880 entry->stat_valid = 0;
881 entry->ae_set |= AE_SET_DEV;
882 entry->ae_stat.aest_dev_is_broken_down = 1;
883 entry->ae_stat.aest_devmajor = m;
887 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
889 entry->stat_valid = 0;
890 entry->ae_set |= AE_SET_DEV;
891 entry->ae_stat.aest_dev_is_broken_down = 1;
892 entry->ae_stat.aest_devminor = m;
895 /* Set symlink if symlink is already set, else set hardlink. */
897 archive_entry_set_link(struct archive_entry *entry, const char *target)
899 if (entry->ae_set & AE_SET_SYMLINK)
900 archive_mstring_copy_mbs(&entry->ae_symlink, target);
902 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
905 /* Set symlink if symlink is already set, else set hardlink. */
907 archive_entry_copy_link(struct archive_entry *entry, const char *target)
909 if (entry->ae_set & AE_SET_SYMLINK)
910 archive_mstring_copy_mbs(&entry->ae_symlink, target);
912 archive_mstring_copy_mbs(&entry->ae_hardlink, target);
915 /* Set symlink if symlink is already set, else set hardlink. */
917 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
919 if (entry->ae_set & AE_SET_SYMLINK)
920 archive_mstring_copy_wcs(&entry->ae_symlink, target);
922 archive_mstring_copy_wcs(&entry->ae_hardlink, target);
926 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
929 if (entry->ae_set & AE_SET_SYMLINK)
930 r = archive_mstring_update_utf8(entry->archive,
931 &entry->ae_symlink, target);
933 r = archive_mstring_update_utf8(entry->archive,
934 &entry->ae_hardlink, target);
935 return ((r == 0)? 1: 0);
939 _archive_entry_copy_link_l(struct archive_entry *entry,
940 const char *target, size_t len, struct archive_string_conv *sc)
944 if (entry->ae_set & AE_SET_SYMLINK)
945 r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
948 r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
954 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
956 entry->stat_valid = 0;
961 archive_entry_set_mtime(struct archive_entry *entry, time_t t, long ns)
964 entry->stat_valid = 0;
965 entry->ae_set |= AE_SET_MTIME;
966 entry->ae_stat.aest_mtime = t;
967 entry->ae_stat.aest_mtime_nsec = ns;
971 archive_entry_unset_mtime(struct archive_entry *entry)
973 archive_entry_set_mtime(entry, 0, 0);
974 entry->ae_set &= ~AE_SET_MTIME;
978 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
980 entry->stat_valid = 0;
981 entry->ae_stat.aest_nlink = nlink;
985 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
987 archive_mstring_copy_mbs(&entry->ae_pathname, name);
991 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
993 archive_mstring_copy_mbs(&entry->ae_pathname, name);
997 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
999 archive_mstring_copy_wcs(&entry->ae_pathname, name);
1003 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
1005 if (archive_mstring_update_utf8(entry->archive,
1006 &entry->ae_pathname, name) == 0)
1012 _archive_entry_copy_pathname_l(struct archive_entry *entry,
1013 const char *name, size_t len, struct archive_string_conv *sc)
1015 return (archive_mstring_copy_mbs_len_l(&entry->ae_pathname,
1020 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
1022 entry->stat_valid = 0;
1023 entry->acl.mode &= AE_IFMT;
1024 entry->acl.mode |= ~AE_IFMT & p;
1028 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
1030 entry->stat_valid = 0;
1031 entry->ae_stat.aest_rdev = m;
1032 entry->ae_stat.aest_rdev_is_broken_down = 0;
1036 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
1038 entry->stat_valid = 0;
1039 entry->ae_stat.aest_rdev_is_broken_down = 1;
1040 entry->ae_stat.aest_rdevmajor = m;
1044 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
1046 entry->stat_valid = 0;
1047 entry->ae_stat.aest_rdev_is_broken_down = 1;
1048 entry->ae_stat.aest_rdevminor = m;
1052 archive_entry_set_size(struct archive_entry *entry, int64_t s)
1054 entry->stat_valid = 0;
1055 entry->ae_stat.aest_size = s;
1056 entry->ae_set |= AE_SET_SIZE;
1060 archive_entry_unset_size(struct archive_entry *entry)
1062 archive_entry_set_size(entry, 0);
1063 entry->ae_set &= ~AE_SET_SIZE;
1067 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1069 archive_mstring_copy_mbs(&entry->ae_sourcepath, path);
1073 archive_entry_copy_sourcepath_w(struct archive_entry *entry, const wchar_t *path)
1075 archive_mstring_copy_wcs(&entry->ae_sourcepath, path);
1079 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1081 archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
1082 if (linkname != NULL)
1083 entry->ae_set |= AE_SET_SYMLINK;
1085 entry->ae_set &= ~AE_SET_SYMLINK;
1089 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1091 archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
1092 if (linkname != NULL)
1093 entry->ae_set |= AE_SET_SYMLINK;
1095 entry->ae_set &= ~AE_SET_SYMLINK;
1099 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1101 archive_mstring_copy_wcs(&entry->ae_symlink, linkname);
1102 if (linkname != NULL)
1103 entry->ae_set |= AE_SET_SYMLINK;
1105 entry->ae_set &= ~AE_SET_SYMLINK;
1109 archive_entry_update_symlink_utf8(struct archive_entry *entry, const char *linkname)
1111 if (linkname != NULL)
1112 entry->ae_set |= AE_SET_SYMLINK;
1114 entry->ae_set &= ~AE_SET_SYMLINK;
1115 if (archive_mstring_update_utf8(entry->archive,
1116 &entry->ae_symlink, linkname) == 0)
1122 _archive_entry_copy_symlink_l(struct archive_entry *entry,
1123 const char *linkname, size_t len, struct archive_string_conv *sc)
1127 r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
1129 if (linkname != NULL && r == 0)
1130 entry->ae_set |= AE_SET_SYMLINK;
1132 entry->ae_set &= ~AE_SET_SYMLINK;
1137 archive_entry_set_uid(struct archive_entry *entry, int64_t u)
1139 entry->stat_valid = 0;
1140 entry->ae_stat.aest_uid = u;
1144 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1146 archive_mstring_copy_mbs(&entry->ae_uname, name);
1150 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1152 archive_mstring_copy_mbs(&entry->ae_uname, name);
1156 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1158 archive_mstring_copy_wcs(&entry->ae_uname, name);
1162 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1164 if (archive_mstring_update_utf8(entry->archive,
1165 &entry->ae_uname, name) == 0)
1171 _archive_entry_copy_uname_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_uname,
1179 archive_entry_mac_metadata(struct archive_entry *entry, size_t *s)
1181 *s = entry->mac_metadata_size;
1182 return entry->mac_metadata;
1186 archive_entry_copy_mac_metadata(struct archive_entry *entry,
1187 const void *p, size_t s)
1189 free(entry->mac_metadata);
1190 if (p == NULL || s == 0) {
1191 entry->mac_metadata = NULL;
1192 entry->mac_metadata_size = 0;
1194 entry->mac_metadata_size = s;
1195 entry->mac_metadata = malloc(s);
1196 if (entry->mac_metadata == NULL)
1198 memcpy(entry->mac_metadata, p, s);
1203 * ACL management. The following would, of course, be a lot simpler
1204 * if: 1) the last draft of POSIX.1e were a really thorough and
1205 * complete standard that addressed the needs of ACL archiving and 2)
1206 * everyone followed it faithfully. Alas, neither is true, so the
1207 * following is a lot more complex than might seem necessary to the
1211 struct archive_acl *
1212 archive_entry_acl(struct archive_entry *entry)
1218 archive_entry_acl_clear(struct archive_entry *entry)
1220 archive_acl_clear(&entry->acl);
1224 * Add a single ACL entry to the internal list of ACL data.
1227 archive_entry_acl_add_entry(struct archive_entry *entry,
1228 int type, int permset, int tag, int id, const char *name)
1230 return archive_acl_add_entry(&entry->acl, type, permset, tag, id, name);
1234 * As above, but with a wide-character name.
1237 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1238 int type, int permset, int tag, int id, const wchar_t *name)
1240 return archive_acl_add_entry_w_len(&entry->acl,
1241 type, permset, tag, id, name, wcslen(name));
1245 * Return a count of entries matching "want_type".
1248 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1250 return archive_acl_count(&entry->acl, want_type);
1254 * Prepare for reading entries from the ACL data. Returns a count
1255 * of entries matching "want_type", or zero if there are no
1256 * non-extended ACL entries of that type.
1259 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1261 return archive_acl_reset(&entry->acl, want_type);
1265 * Return the next ACL entry in the list. Fake entries for the
1266 * standard permissions and include them in the returned list.
1269 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1270 int *permset, int *tag, int *id, const char **name)
1272 return archive_acl_next(entry->archive, &entry->acl, want_type, type, permset, tag, id, name);
1276 * Generate a text version of the ACL. The flags parameter controls
1277 * the style of the generated ACL.
1280 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1282 return archive_acl_text_w(entry->archive, &entry->acl, flags);
1286 archive_entry_acl_text(struct archive_entry *entry, int flags)
1289 if (archive_acl_text_l(&entry->acl, flags, &p, NULL, NULL) != 0
1296 _archive_entry_acl_text_l(struct archive_entry *entry, int flags,
1297 const char **acl_text, size_t *len, struct archive_string_conv *sc)
1299 return (archive_acl_text_l(&entry->acl, flags, acl_text, len, sc));
1303 * Following code is modified from UC Berkeley sources, and
1304 * is subject to the following copyright notice.
1308 * Copyright (c) 1993
1309 * The Regents of the University of California. All rights reserved.
1311 * Redistribution and use in source and binary forms, with or without
1312 * modification, are permitted provided that the following conditions
1314 * 1. Redistributions of source code must retain the above copyright
1315 * notice, this list of conditions and the following disclaimer.
1316 * 2. Redistributions in binary form must reproduce the above copyright
1317 * notice, this list of conditions and the following disclaimer in the
1318 * documentation and/or other materials provided with the distribution.
1319 * 4. Neither the name of the University nor the names of its contributors
1320 * may be used to endorse or promote products derived from this software
1321 * without specific prior written permission.
1323 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1324 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1325 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1326 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1327 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1328 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1329 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1330 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1331 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1332 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1336 static struct flag {
1338 const wchar_t *wname;
1340 unsigned long clear;
1342 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1344 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1345 { "nosappend", L"nosappend", SF_APPEND, 0 },
1347 #ifdef EXT2_APPEND_FL /* 'a' */
1348 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1349 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1352 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1353 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1356 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1357 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1358 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1360 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1361 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1362 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1363 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1366 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1367 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1370 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1373 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1374 { "nouappend", L"nouappend", UF_APPEND, 0 },
1377 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1378 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1379 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1382 { "nodump", L"nodump", 0, UF_NODUMP},
1384 #ifdef EXT2_NODUMP_FL /* 'd' */
1385 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1388 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1391 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1392 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1395 { "nouunlink", L"nouunlink", EXT2_UNRM_FL, 0},
1398 #ifdef EXT2_BTREE_FL
1399 { "nobtree", L"nobtree", EXT2_BTREE_FL, 0 },
1402 #ifdef EXT2_ECOMPR_FL
1403 { "nocomperr", L"nocomperr", EXT2_ECOMPR_FL, 0 },
1406 #ifdef EXT2_COMPR_FL /* 'c' */
1407 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1410 #ifdef EXT2_NOATIME_FL /* 'A' */
1411 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1414 #ifdef EXT2_DIRTY_FL
1415 { "nocompdirty",L"nocompdirty", EXT2_DIRTY_FL, 0},
1418 #ifdef EXT2_COMPRBLK_FL
1419 #ifdef EXT2_NOCOMPR_FL
1420 { "nocomprblk", L"nocomprblk", EXT2_COMPRBLK_FL, EXT2_NOCOMPR_FL},
1422 { "nocomprblk", L"nocomprblk", EXT2_COMPRBLK_FL, 0},
1425 #ifdef EXT2_DIRSYNC_FL
1426 { "nodirsync", L"nodirsync", EXT2_DIRSYNC_FL, 0},
1428 #ifdef EXT2_INDEX_FL
1429 { "nohashidx", L"nohashidx", EXT2_INDEX_FL, 0},
1431 #ifdef EXT2_IMAGIC_FL
1432 { "noimagic", L"noimagic", EXT2_IMAGIC_FL, 0},
1434 #ifdef EXT3_JOURNAL_DATA_FL
1435 { "nojournal", L"nojournal", EXT3_JOURNAL_DATA_FL, 0},
1437 #ifdef EXT2_SECRM_FL
1438 { "nosecuredeletion",L"nosecuredeletion",EXT2_SECRM_FL, 0},
1441 { "nosync", L"nosync", EXT2_SYNC_FL, 0},
1443 #ifdef EXT2_NOTAIL_FL
1444 { "notail", L"notail", 0, EXT2_NOTAIL_FL},
1446 #ifdef EXT2_TOPDIR_FL
1447 { "notopdir", L"notopdir", EXT2_TOPDIR_FL, 0},
1449 #ifdef EXT2_RESERVED_FL
1450 { "noreserved", L"noreserved", EXT2_RESERVED_FL, 0},
1453 { NULL, NULL, 0, 0 }
1458 * Convert file flags to a comma-separated string. If no flags
1459 * are set, return the empty string.
1462 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1470 bits = bitset | bitclear;
1472 for (flag = flags; flag->name != NULL; flag++)
1473 if (bits & (flag->set | flag->clear)) {
1474 length += strlen(flag->name) + 1;
1475 bits &= ~(flag->set | flag->clear);
1480 string = (char *)malloc(length);
1485 for (flag = flags; flag->name != NULL; flag++) {
1486 if (bitset & flag->set || bitclear & flag->clear) {
1487 sp = flag->name + 2;
1488 } else if (bitset & flag->clear || bitclear & flag->set) {
1492 bitset &= ~(flag->set | flag->clear);
1493 bitclear &= ~(flag->set | flag->clear);
1496 while ((*dp++ = *sp++) != '\0')
1507 * Take string of arguments and return file flags. This
1508 * version works a little differently than strtofflags(3).
1509 * In particular, it always tests every token, skipping any
1510 * unrecognized tokens. It returns a pointer to the first
1511 * unrecognized token, or NULL if every token was recognized.
1512 * This version is also const-correct and does not modify the
1516 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
1518 const char *start, *end;
1520 unsigned long set, clear;
1526 /* Find start of first token. */
1527 while (*start == '\t' || *start == ' ' || *start == ',')
1529 while (*start != '\0') {
1530 /* Locate end of token. */
1532 while (*end != '\0' && *end != '\t' &&
1533 *end != ' ' && *end != ',')
1535 for (flag = flags; flag->name != NULL; flag++) {
1536 if (memcmp(start, flag->name, end - start) == 0) {
1537 /* Matched "noXXXX", so reverse the sense. */
1541 } else if (memcmp(start, flag->name + 2, end - start)
1543 /* Matched "XXXX", so don't reverse. */
1545 clear |= flag->clear;
1549 /* Ignore unknown flag names. */
1550 if (flag->name == NULL && failed == NULL)
1553 /* Find start of next token. */
1555 while (*start == '\t' || *start == ' ' || *start == ',')
1565 /* Return location of first failure. */
1571 * Take string of arguments and return file flags. This
1572 * version works a little differently than strtofflags(3).
1573 * In particular, it always tests every token, skipping any
1574 * unrecognized tokens. It returns a pointer to the first
1575 * unrecognized token, or NULL if every token was recognized.
1576 * This version is also const-correct and does not modify the
1579 static const wchar_t *
1580 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1582 const wchar_t *start, *end;
1584 unsigned long set, clear;
1585 const wchar_t *failed;
1590 /* Find start of first token. */
1591 while (*start == L'\t' || *start == L' ' || *start == L',')
1593 while (*start != L'\0') {
1594 /* Locate end of token. */
1596 while (*end != L'\0' && *end != L'\t' &&
1597 *end != L' ' && *end != L',')
1599 for (flag = flags; flag->wname != NULL; flag++) {
1600 if (wmemcmp(start, flag->wname, end - start) == 0) {
1601 /* Matched "noXXXX", so reverse the sense. */
1605 } else if (wmemcmp(start, flag->wname + 2, end - start)
1607 /* Matched "XXXX", so don't reverse. */
1609 clear |= flag->clear;
1613 /* Ignore unknown flag names. */
1614 if (flag->wname == NULL && failed == NULL)
1617 /* Find start of next token. */
1619 while (*start == L'\t' || *start == L' ' || *start == L',')
1629 /* Return location of first failure. */
1637 main(int argc, char **argv)
1639 struct archive_entry *entry = archive_entry_new();
1640 unsigned long set, clear;
1641 const wchar_t *remainder;
1643 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1644 archive_entry_fflags(entry, &set, &clear);
1646 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1648 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));