1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2016 Martin Matuska
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "archive_platform.h"
28
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35 #if MAJOR_IN_MKDEV
36 #include <sys/mkdev.h>
37 #define HAVE_MAJOR
38 #elif MAJOR_IN_SYSMACROS
39 #include <sys/sysmacros.h>
40 #define HAVE_MAJOR
41 #endif
42 #ifdef HAVE_ERRNO_H
43 #include <errno.h>
44 #endif
45 #ifdef HAVE_LIMITS_H
46 #include <limits.h>
47 #endif
48 #ifdef HAVE_LINUX_FS_H
49 #include <linux/fs.h> /* for Linux file flags */
50 #endif
51 /*
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.
54 */
55 #ifdef HAVE_LINUX_EXT2_FS_H
56 #include <linux/ext2_fs.h> /* for Linux file flags */
57 #endif
58 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
59 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
60 #endif
61 #include <stddef.h>
62 #include <stdio.h>
63 #ifdef HAVE_STDLIB_H
64 #include <stdlib.h>
65 #endif
66 #ifdef HAVE_STRING_H
67 #include <string.h>
68 #endif
69 #ifdef HAVE_WCHAR_H
70 #include <wchar.h>
71 #endif
72
73 #include "archive.h"
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"
79
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)))
85 #endif
86
87 /* Play games to come up with a suitable makedev() definition. */
88 #ifdef __QNXNTO__
89 /* QNX. <sigh> */
90 #include <sys/netmgr.h>
91 #define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
92 #elif defined makedev
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__))
96 /* Windows. <sigh> */
97 #define ae_makedev(maj, min) mkdev((maj), (min))
98 #else
99 /* There's a "makedev" function. */
100 #define ae_makedev(maj, min) makedev((maj), (min))
101 #endif
102
103 /*
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.
110 */
111 #define FIX_NS(t,ns) \
112 do { \
113 t += ns / 1000000000; \
114 ns %= 1000000000; \
115 if (ns < 0) { --t; ns += 1000000000; } \
116 } while (0)
117
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, size_t length,
122 unsigned long *setp, unsigned long *clrp);
123
124 #ifndef HAVE_WCSCPY
wcscpy(wchar_t * s1,const wchar_t * s2)125 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
126 {
127 wchar_t *dest = s1;
128 while ((*s1 = *s2) != L'\0')
129 ++s1, ++s2;
130 return dest;
131 }
132 #endif
133 #ifndef HAVE_WCSLEN
wcslen(const wchar_t * s)134 static size_t wcslen(const wchar_t *s)
135 {
136 const wchar_t *p = s;
137 while (*p != L'\0')
138 ++p;
139 return p - s;
140 }
141 #endif
142 #ifndef HAVE_WMEMCMP
143 /* Good enough for simple equality testing, but not for sorting. */
144 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
145 #endif
146
147 /****************************************************************************
148 *
149 * Public Interface
150 *
151 ****************************************************************************/
152
153 struct archive_entry *
archive_entry_clear(struct archive_entry * entry)154 archive_entry_clear(struct archive_entry *entry)
155 {
156 if (entry == NULL)
157 return (NULL);
158 archive_mstring_clean(&entry->ae_fflags_text);
159 archive_mstring_clean(&entry->ae_gname);
160 archive_mstring_clean(&entry->ae_linkname);
161 archive_mstring_clean(&entry->ae_pathname);
162 archive_mstring_clean(&entry->ae_sourcepath);
163 archive_mstring_clean(&entry->ae_uname);
164 archive_entry_copy_mac_metadata(entry, NULL, 0);
165 archive_acl_clear(&entry->acl);
166 archive_entry_xattr_clear(entry);
167 archive_entry_sparse_clear(entry);
168 free(entry->stat);
169 entry->ae_symlink_type = AE_SYMLINK_TYPE_UNDEFINED;
170 memset(entry, 0, sizeof(*entry));
171 return entry;
172 }
173
174 struct archive_entry *
archive_entry_clone(struct archive_entry * entry)175 archive_entry_clone(struct archive_entry *entry)
176 {
177 struct archive_entry *entry2;
178 struct ae_xattr *xp;
179 struct ae_sparse *sp;
180 size_t s;
181 const void *p;
182
183 /* Allocate new structure and copy over all of the fields. */
184 /* TODO: Should we copy the archive over? Or require a new archive
185 * as an argument? */
186 entry2 = archive_entry_new2(entry->archive);
187 if (entry2 == NULL)
188 return (NULL);
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;
192
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_linkname, &entry->ae_linkname);
198 archive_mstring_copy(&entry2->ae_pathname, &entry->ae_pathname);
199 archive_mstring_copy(&entry2->ae_sourcepath, &entry->ae_sourcepath);
200 entry2->ae_set = entry->ae_set;
201 archive_mstring_copy(&entry2->ae_uname, &entry->ae_uname);
202
203 /* Copy symlink type */
204 entry2->ae_symlink_type = entry->ae_symlink_type;
205
206 /* Copy encryption status */
207 entry2->encryption = entry->encryption;
208
209 /* Copy digests */
210 #define copy_digest(_e2, _e, _t) \
211 memcpy(_e2->digest._t, _e->digest._t, sizeof(_e2->digest._t))
212
213 copy_digest(entry2, entry, md5);
214 copy_digest(entry2, entry, rmd160);
215 copy_digest(entry2, entry, sha1);
216 copy_digest(entry2, entry, sha256);
217 copy_digest(entry2, entry, sha384);
218 copy_digest(entry2, entry, sha512);
219
220 #undef copy_digest
221
222 /* Copy ACL data over. */
223 archive_acl_copy(&entry2->acl, &entry->acl);
224
225 /* Copy Mac OS metadata. */
226 p = archive_entry_mac_metadata(entry, &s);
227 archive_entry_copy_mac_metadata(entry2, p, s);
228
229 /* Copy xattr data over. */
230 xp = entry->xattr_head;
231 while (xp != NULL) {
232 archive_entry_xattr_add_entry(entry2,
233 xp->name, xp->value, xp->size);
234 xp = xp->next;
235 }
236
237 /* Copy sparse data over. */
238 sp = entry->sparse_head;
239 while (sp != NULL) {
240 archive_entry_sparse_add_entry(entry2,
241 sp->offset, sp->length);
242 sp = sp->next;
243 }
244
245 return (entry2);
246 }
247
248 void
archive_entry_free(struct archive_entry * entry)249 archive_entry_free(struct archive_entry *entry)
250 {
251 archive_entry_clear(entry);
252 free(entry);
253 }
254
255 struct archive_entry *
archive_entry_new(void)256 archive_entry_new(void)
257 {
258 return archive_entry_new2(NULL);
259 }
260
261 struct archive_entry *
archive_entry_new2(struct archive * a)262 archive_entry_new2(struct archive *a)
263 {
264 struct archive_entry *entry;
265
266 entry = calloc(1, sizeof(*entry));
267 if (entry == NULL)
268 return (NULL);
269 entry->archive = a;
270 entry->ae_symlink_type = AE_SYMLINK_TYPE_UNDEFINED;
271 return (entry);
272 }
273
274 /*
275 * Functions for reading fields from an archive_entry.
276 */
277
278 __LA_TIME_T
archive_entry_atime(struct archive_entry * entry)279 archive_entry_atime(struct archive_entry *entry)
280 {
281 return (entry->ae_stat.aest_atime);
282 }
283
284 long
archive_entry_atime_nsec(struct archive_entry * entry)285 archive_entry_atime_nsec(struct archive_entry *entry)
286 {
287 return (entry->ae_stat.aest_atime_nsec);
288 }
289
290 int
archive_entry_atime_is_set(struct archive_entry * entry)291 archive_entry_atime_is_set(struct archive_entry *entry)
292 {
293 return (entry->ae_set & AE_SET_ATIME);
294 }
295
296 __LA_TIME_T
archive_entry_birthtime(struct archive_entry * entry)297 archive_entry_birthtime(struct archive_entry *entry)
298 {
299 return (entry->ae_stat.aest_birthtime);
300 }
301
302 long
archive_entry_birthtime_nsec(struct archive_entry * entry)303 archive_entry_birthtime_nsec(struct archive_entry *entry)
304 {
305 return (entry->ae_stat.aest_birthtime_nsec);
306 }
307
308 int
archive_entry_birthtime_is_set(struct archive_entry * entry)309 archive_entry_birthtime_is_set(struct archive_entry *entry)
310 {
311 return (entry->ae_set & AE_SET_BIRTHTIME);
312 }
313
314 __LA_TIME_T
archive_entry_ctime(struct archive_entry * entry)315 archive_entry_ctime(struct archive_entry *entry)
316 {
317 return (entry->ae_stat.aest_ctime);
318 }
319
320 int
archive_entry_ctime_is_set(struct archive_entry * entry)321 archive_entry_ctime_is_set(struct archive_entry *entry)
322 {
323 return (entry->ae_set & AE_SET_CTIME);
324 }
325
326 long
archive_entry_ctime_nsec(struct archive_entry * entry)327 archive_entry_ctime_nsec(struct archive_entry *entry)
328 {
329 return (entry->ae_stat.aest_ctime_nsec);
330 }
331
332 __LA_DEV_T
archive_entry_dev(struct archive_entry * entry)333 archive_entry_dev(struct archive_entry *entry)
334 {
335 if (entry->ae_stat.aest_dev_is_broken_down)
336 return ae_makedev(entry->ae_stat.aest_devmajor,
337 entry->ae_stat.aest_devminor);
338 else
339 return (entry->ae_stat.aest_dev);
340 }
341
342 int
archive_entry_dev_is_set(struct archive_entry * entry)343 archive_entry_dev_is_set(struct archive_entry *entry)
344 {
345 return (entry->ae_set & AE_SET_DEV);
346 }
347
348 __LA_DEV_T
archive_entry_devmajor(struct archive_entry * entry)349 archive_entry_devmajor(struct archive_entry *entry)
350 {
351 if (entry->ae_stat.aest_dev_is_broken_down)
352 return (entry->ae_stat.aest_devmajor);
353 else
354 return major(entry->ae_stat.aest_dev);
355 }
356
357 __LA_DEV_T
archive_entry_devminor(struct archive_entry * entry)358 archive_entry_devminor(struct archive_entry *entry)
359 {
360 if (entry->ae_stat.aest_dev_is_broken_down)
361 return (entry->ae_stat.aest_devminor);
362 else
363 return minor(entry->ae_stat.aest_dev);
364 }
365
366 __LA_MODE_T
archive_entry_filetype(struct archive_entry * entry)367 archive_entry_filetype(struct archive_entry *entry)
368 {
369 return (AE_IFMT & entry->acl.mode);
370 }
371
372 int
archive_entry_filetype_is_set(struct archive_entry * entry)373 archive_entry_filetype_is_set(struct archive_entry *entry)
374 {
375 return (entry->ae_set & AE_SET_FILETYPE);
376 }
377
378 void
archive_entry_fflags(struct archive_entry * entry,unsigned long * set,unsigned long * clear)379 archive_entry_fflags(struct archive_entry *entry,
380 unsigned long *set, unsigned long *clear)
381 {
382 *set = entry->ae_fflags_set;
383 *clear = entry->ae_fflags_clear;
384 }
385
386 /*
387 * Note: if text was provided, this just returns that text. If you
388 * really need the text to be rebuilt in a canonical form, set the
389 * text, ask for the bitmaps, then set the bitmaps. (Setting the
390 * bitmaps clears any stored text.) This design is deliberate: if
391 * we're editing archives, we don't want to discard flags just because
392 * they aren't supported on the current system. The bitmap<->text
393 * conversions are platform-specific (see below).
394 */
395 const char *
archive_entry_fflags_text(struct archive_entry * entry)396 archive_entry_fflags_text(struct archive_entry *entry)
397 {
398 const char *f;
399 char *p;
400
401 if (archive_mstring_get_mbs(entry->archive,
402 &entry->ae_fflags_text, &f) == 0) {
403 if (f != NULL)
404 return (f);
405 } else if (errno == ENOMEM)
406 __archive_errx(1, "No memory");
407
408 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
409 return (NULL);
410
411 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
412 if (p == NULL)
413 return (NULL);
414
415 archive_mstring_copy_mbs(&entry->ae_fflags_text, p);
416 free(p);
417 if (archive_mstring_get_mbs(entry->archive,
418 &entry->ae_fflags_text, &f) == 0)
419 return (f);
420 if (errno == ENOMEM)
421 __archive_errx(1, "No memory");
422 return (NULL);
423 }
424
425 la_int64_t
archive_entry_gid(struct archive_entry * entry)426 archive_entry_gid(struct archive_entry *entry)
427 {
428 return (entry->ae_stat.aest_gid);
429 }
430
431 int
archive_entry_gid_is_set(struct archive_entry * entry)432 archive_entry_gid_is_set(struct archive_entry *entry)
433 {
434 return (entry->ae_set & AE_SET_GID);
435 }
436
437 const char *
archive_entry_gname(struct archive_entry * entry)438 archive_entry_gname(struct archive_entry *entry)
439 {
440 const char *p;
441 if (archive_mstring_get_mbs(entry->archive, &entry->ae_gname, &p) == 0)
442 return (p);
443 if (errno == ENOMEM)
444 __archive_errx(1, "No memory");
445 return (NULL);
446 }
447
448 const char *
archive_entry_gname_utf8(struct archive_entry * entry)449 archive_entry_gname_utf8(struct archive_entry *entry)
450 {
451 const char *p;
452 if (archive_mstring_get_utf8(entry->archive, &entry->ae_gname, &p) == 0)
453 return (p);
454 if (errno == ENOMEM)
455 __archive_errx(1, "No memory");
456 return (NULL);
457 }
458
459
460 const wchar_t *
archive_entry_gname_w(struct archive_entry * entry)461 archive_entry_gname_w(struct archive_entry *entry)
462 {
463 const wchar_t *p;
464 if (archive_mstring_get_wcs(entry->archive, &entry->ae_gname, &p) == 0)
465 return (p);
466 if (errno == ENOMEM)
467 __archive_errx(1, "No memory");
468 return (NULL);
469 }
470
471 int
_archive_entry_gname_l(struct archive_entry * entry,const char ** p,size_t * len,struct archive_string_conv * sc)472 _archive_entry_gname_l(struct archive_entry *entry,
473 const char **p, size_t *len, struct archive_string_conv *sc)
474 {
475 return (archive_mstring_get_mbs_l(entry->archive, &entry->ae_gname, p, len, sc));
476 }
477
478 void
archive_entry_set_link_to_hardlink(struct archive_entry * entry)479 archive_entry_set_link_to_hardlink(struct archive_entry *entry)
480 {
481 if ((entry->ae_set & AE_SET_SYMLINK) != 0) {
482 entry->ae_set &= ~AE_SET_SYMLINK;
483 }
484 entry->ae_set |= AE_SET_HARDLINK;
485 }
486
487 const char *
archive_entry_hardlink(struct archive_entry * entry)488 archive_entry_hardlink(struct archive_entry *entry)
489 {
490 const char *p;
491 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
492 return (NULL);
493 if (archive_mstring_get_mbs(
494 entry->archive, &entry->ae_linkname, &p) == 0)
495 return (p);
496 if (errno == ENOMEM)
497 __archive_errx(1, "No memory");
498 return (NULL);
499 }
500
501 const char *
archive_entry_hardlink_utf8(struct archive_entry * entry)502 archive_entry_hardlink_utf8(struct archive_entry *entry)
503 {
504 const char *p;
505 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
506 return (NULL);
507 if (archive_mstring_get_utf8(
508 entry->archive, &entry->ae_linkname, &p) == 0)
509 return (p);
510 if (errno == ENOMEM)
511 __archive_errx(1, "No memory");
512 return (NULL);
513 }
514
515 const wchar_t *
archive_entry_hardlink_w(struct archive_entry * entry)516 archive_entry_hardlink_w(struct archive_entry *entry)
517 {
518 const wchar_t *p;
519 if ((entry->ae_set & AE_SET_HARDLINK) == 0)
520 return (NULL);
521 if (archive_mstring_get_wcs(
522 entry->archive, &entry->ae_linkname, &p) == 0)
523 return (p);
524 if (errno == ENOMEM)
525 __archive_errx(1, "No memory");
526 return (NULL);
527 }
528
529 int
archive_entry_hardlink_is_set(struct archive_entry * entry)530 archive_entry_hardlink_is_set(struct archive_entry *entry)
531 {
532 return (entry->ae_set & AE_SET_HARDLINK) != 0;
533 }
534
535 int
_archive_entry_hardlink_l(struct archive_entry * entry,const char ** p,size_t * len,struct archive_string_conv * sc)536 _archive_entry_hardlink_l(struct archive_entry *entry,
537 const char **p, size_t *len, struct archive_string_conv *sc)
538 {
539 if ((entry->ae_set & AE_SET_HARDLINK) == 0) {
540 *p = NULL;
541 *len = 0;
542 return (0);
543 }
544 return (archive_mstring_get_mbs_l(entry->archive, &entry->ae_linkname, p, len, sc));
545 }
546
547 la_int64_t
archive_entry_ino(struct archive_entry * entry)548 archive_entry_ino(struct archive_entry *entry)
549 {
550 return (entry->ae_stat.aest_ino);
551 }
552
553 int
archive_entry_ino_is_set(struct archive_entry * entry)554 archive_entry_ino_is_set(struct archive_entry *entry)
555 {
556 return (entry->ae_set & AE_SET_INO);
557 }
558
559 la_int64_t
archive_entry_ino64(struct archive_entry * entry)560 archive_entry_ino64(struct archive_entry *entry)
561 {
562 return (entry->ae_stat.aest_ino);
563 }
564
565 __LA_MODE_T
archive_entry_mode(struct archive_entry * entry)566 archive_entry_mode(struct archive_entry *entry)
567 {
568 return (entry->acl.mode);
569 }
570
571 __LA_TIME_T
archive_entry_mtime(struct archive_entry * entry)572 archive_entry_mtime(struct archive_entry *entry)
573 {
574 return (entry->ae_stat.aest_mtime);
575 }
576
577 long
archive_entry_mtime_nsec(struct archive_entry * entry)578 archive_entry_mtime_nsec(struct archive_entry *entry)
579 {
580 return (entry->ae_stat.aest_mtime_nsec);
581 }
582
583 int
archive_entry_mtime_is_set(struct archive_entry * entry)584 archive_entry_mtime_is_set(struct archive_entry *entry)
585 {
586 return (entry->ae_set & AE_SET_MTIME);
587 }
588
589 unsigned int
archive_entry_nlink(struct archive_entry * entry)590 archive_entry_nlink(struct archive_entry *entry)
591 {
592 return (entry->ae_stat.aest_nlink);
593 }
594
595 /* Instead, our caller could have chosen a specific encoding
596 * (archive_mstring_get_mbs, archive_mstring_get_utf8,
597 * archive_mstring_get_wcs). So we should try multiple
598 * encodings. Try mbs first because of history, even though
599 * utf8 might be better for pathname portability.
600 * Also omit wcs because of type mismatch (char * versus wchar *)
601 */
602 const char *
archive_entry_pathname(struct archive_entry * entry)603 archive_entry_pathname(struct archive_entry *entry)
604 {
605 const char *p;
606 if (archive_mstring_get_mbs(
607 entry->archive, &entry->ae_pathname, &p) == 0)
608 return (p);
609 #if HAVE_EILSEQ /*{*/
610 if (errno == EILSEQ) {
611 if (archive_mstring_get_utf8(
612 entry->archive, &entry->ae_pathname, &p) == 0)
613 return (p);
614 }
615 #endif /*}*/
616 if (errno == ENOMEM)
617 __archive_errx(1, "No memory");
618 return (NULL);
619 }
620
621 const char *
archive_entry_pathname_utf8(struct archive_entry * entry)622 archive_entry_pathname_utf8(struct archive_entry *entry)
623 {
624 const char *p;
625 if (archive_mstring_get_utf8(
626 entry->archive, &entry->ae_pathname, &p) == 0)
627 return (p);
628 if (errno == ENOMEM)
629 __archive_errx(1, "No memory");
630 return (NULL);
631 }
632
633 const wchar_t *
archive_entry_pathname_w(struct archive_entry * entry)634 archive_entry_pathname_w(struct archive_entry *entry)
635 {
636 const wchar_t *p;
637 if (archive_mstring_get_wcs(
638 entry->archive, &entry->ae_pathname, &p) == 0)
639 return (p);
640 if (errno == ENOMEM)
641 __archive_errx(1, "No memory");
642 return (NULL);
643 }
644
645 int
_archive_entry_pathname_l(struct archive_entry * entry,const char ** p,size_t * len,struct archive_string_conv * sc)646 _archive_entry_pathname_l(struct archive_entry *entry,
647 const char **p, size_t *len, struct archive_string_conv *sc)
648 {
649 return (archive_mstring_get_mbs_l(entry->archive, &entry->ae_pathname, p, len, sc));
650 }
651
652 __LA_MODE_T
archive_entry_perm(struct archive_entry * entry)653 archive_entry_perm(struct archive_entry *entry)
654 {
655 return (~AE_IFMT & entry->acl.mode);
656 }
657
658 int
archive_entry_perm_is_set(struct archive_entry * entry)659 archive_entry_perm_is_set(struct archive_entry *entry)
660 {
661 return (entry->ae_set & AE_SET_PERM);
662 }
663
664 int
archive_entry_rdev_is_set(struct archive_entry * entry)665 archive_entry_rdev_is_set(struct archive_entry *entry)
666 {
667 return (entry->ae_set & AE_SET_RDEV);
668 }
669
670 __LA_DEV_T
archive_entry_rdev(struct archive_entry * entry)671 archive_entry_rdev(struct archive_entry *entry)
672 {
673 if (archive_entry_rdev_is_set(entry)) {
674 if (entry->ae_stat.aest_rdev_is_broken_down)
675 return ae_makedev(entry->ae_stat.aest_rdevmajor,
676 entry->ae_stat.aest_rdevminor);
677 else
678 return (entry->ae_stat.aest_rdev);
679 } else {
680 return 0;
681 }
682 }
683
684 __LA_DEV_T
archive_entry_rdevmajor(struct archive_entry * entry)685 archive_entry_rdevmajor(struct archive_entry *entry)
686 {
687 if (archive_entry_rdev_is_set(entry)) {
688 if (entry->ae_stat.aest_rdev_is_broken_down)
689 return (entry->ae_stat.aest_rdevmajor);
690 else
691 return major(entry->ae_stat.aest_rdev);
692 } else {
693 return 0;
694 }
695 }
696
697 __LA_DEV_T
archive_entry_rdevminor(struct archive_entry * entry)698 archive_entry_rdevminor(struct archive_entry *entry)
699 {
700 if (archive_entry_rdev_is_set(entry)) {
701 if (entry->ae_stat.aest_rdev_is_broken_down)
702 return (entry->ae_stat.aest_rdevminor);
703 else
704 return minor(entry->ae_stat.aest_rdev);
705 } else {
706 return 0;
707 }
708 }
709
710 la_int64_t
archive_entry_size(struct archive_entry * entry)711 archive_entry_size(struct archive_entry *entry)
712 {
713 return (entry->ae_stat.aest_size);
714 }
715
716 int
archive_entry_size_is_set(struct archive_entry * entry)717 archive_entry_size_is_set(struct archive_entry *entry)
718 {
719 return (entry->ae_set & AE_SET_SIZE);
720 }
721
722 const char *
archive_entry_sourcepath(struct archive_entry * entry)723 archive_entry_sourcepath(struct archive_entry *entry)
724 {
725 const char *p;
726 if (archive_mstring_get_mbs(
727 entry->archive, &entry->ae_sourcepath, &p) == 0)
728 return (p);
729 if (errno == ENOMEM)
730 __archive_errx(1, "No memory");
731 return (NULL);
732 }
733
734 const wchar_t *
archive_entry_sourcepath_w(struct archive_entry * entry)735 archive_entry_sourcepath_w(struct archive_entry *entry)
736 {
737 const wchar_t *p;
738 if (archive_mstring_get_wcs(
739 entry->archive, &entry->ae_sourcepath, &p) == 0)
740 return (p);
741 return (NULL);
742 }
743
744 const char *
archive_entry_symlink(struct archive_entry * entry)745 archive_entry_symlink(struct archive_entry *entry)
746 {
747 const char *p;
748 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
749 return (NULL);
750 if (archive_mstring_get_mbs(
751 entry->archive, &entry->ae_linkname, &p) == 0)
752 return (p);
753 if (errno == ENOMEM)
754 __archive_errx(1, "No memory");
755 return (NULL);
756 }
757
758 void
archive_entry_set_link_to_symlink(struct archive_entry * entry)759 archive_entry_set_link_to_symlink(struct archive_entry *entry)
760 {
761 if ((entry->ae_set & AE_SET_HARDLINK) != 0) {
762 entry->ae_set &= ~AE_SET_HARDLINK;
763 }
764 entry->ae_set |= AE_SET_SYMLINK;
765 }
766
767 int
archive_entry_symlink_type(struct archive_entry * entry)768 archive_entry_symlink_type(struct archive_entry *entry)
769 {
770 return (entry->ae_symlink_type);
771 }
772
773 const char *
archive_entry_symlink_utf8(struct archive_entry * entry)774 archive_entry_symlink_utf8(struct archive_entry *entry)
775 {
776 const char *p;
777 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
778 return (NULL);
779 if (archive_mstring_get_utf8(
780 entry->archive, &entry->ae_linkname, &p) == 0)
781 return (p);
782 if (errno == ENOMEM)
783 __archive_errx(1, "No memory");
784 return (NULL);
785 }
786
787 const wchar_t *
archive_entry_symlink_w(struct archive_entry * entry)788 archive_entry_symlink_w(struct archive_entry *entry)
789 {
790 const wchar_t *p;
791 if ((entry->ae_set & AE_SET_SYMLINK) == 0)
792 return (NULL);
793 if (archive_mstring_get_wcs(
794 entry->archive, &entry->ae_linkname, &p) == 0)
795 return (p);
796 if (errno == ENOMEM)
797 __archive_errx(1, "No memory");
798 return (NULL);
799 }
800
801 int
_archive_entry_symlink_l(struct archive_entry * entry,const char ** p,size_t * len,struct archive_string_conv * sc)802 _archive_entry_symlink_l(struct archive_entry *entry,
803 const char **p, size_t *len, struct archive_string_conv *sc)
804 {
805 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
806 *p = NULL;
807 *len = 0;
808 return (0);
809 }
810 return (archive_mstring_get_mbs_l(entry->archive, &entry->ae_linkname, p, len, sc));
811 }
812
813 la_int64_t
archive_entry_uid(struct archive_entry * entry)814 archive_entry_uid(struct archive_entry *entry)
815 {
816 return (entry->ae_stat.aest_uid);
817 }
818
819 int
archive_entry_uid_is_set(struct archive_entry * entry)820 archive_entry_uid_is_set(struct archive_entry *entry)
821 {
822 return (entry->ae_set & AE_SET_UID);
823 }
824
825 const char *
archive_entry_uname(struct archive_entry * entry)826 archive_entry_uname(struct archive_entry *entry)
827 {
828 const char *p;
829 if (archive_mstring_get_mbs(entry->archive, &entry->ae_uname, &p) == 0)
830 return (p);
831 if (errno == ENOMEM)
832 __archive_errx(1, "No memory");
833 return (NULL);
834 }
835
836 const char *
archive_entry_uname_utf8(struct archive_entry * entry)837 archive_entry_uname_utf8(struct archive_entry *entry)
838 {
839 const char *p;
840 if (archive_mstring_get_utf8(entry->archive, &entry->ae_uname, &p) == 0)
841 return (p);
842 if (errno == ENOMEM)
843 __archive_errx(1, "No memory");
844 return (NULL);
845 }
846
847 const wchar_t *
archive_entry_uname_w(struct archive_entry * entry)848 archive_entry_uname_w(struct archive_entry *entry)
849 {
850 const wchar_t *p;
851 if (archive_mstring_get_wcs(entry->archive, &entry->ae_uname, &p) == 0)
852 return (p);
853 if (errno == ENOMEM)
854 __archive_errx(1, "No memory");
855 return (NULL);
856 }
857
858 int
_archive_entry_uname_l(struct archive_entry * entry,const char ** p,size_t * len,struct archive_string_conv * sc)859 _archive_entry_uname_l(struct archive_entry *entry,
860 const char **p, size_t *len, struct archive_string_conv *sc)
861 {
862 return (archive_mstring_get_mbs_l(entry->archive, &entry->ae_uname, p, len, sc));
863 }
864
865 int
archive_entry_is_data_encrypted(struct archive_entry * entry)866 archive_entry_is_data_encrypted(struct archive_entry *entry)
867 {
868 return ((entry->encryption & AE_ENCRYPTION_DATA) == AE_ENCRYPTION_DATA);
869 }
870
871 int
archive_entry_is_metadata_encrypted(struct archive_entry * entry)872 archive_entry_is_metadata_encrypted(struct archive_entry *entry)
873 {
874 return ((entry->encryption & AE_ENCRYPTION_METADATA) == AE_ENCRYPTION_METADATA);
875 }
876
877 int
archive_entry_is_encrypted(struct archive_entry * entry)878 archive_entry_is_encrypted(struct archive_entry *entry)
879 {
880 return (entry->encryption & (AE_ENCRYPTION_DATA|AE_ENCRYPTION_METADATA));
881 }
882
883 /*
884 * Functions to set archive_entry properties.
885 */
886
887 void
archive_entry_set_filetype(struct archive_entry * entry,unsigned int type)888 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
889 {
890 entry->stat_valid = 0;
891 entry->acl.mode &= ~AE_IFMT;
892 entry->acl.mode |= AE_IFMT & type;
893 entry->ae_set |= AE_SET_FILETYPE;
894 }
895
896 void
archive_entry_set_fflags(struct archive_entry * entry,unsigned long set,unsigned long clear)897 archive_entry_set_fflags(struct archive_entry *entry,
898 unsigned long set, unsigned long clear)
899 {
900 archive_mstring_clean(&entry->ae_fflags_text);
901 entry->ae_fflags_set = set;
902 entry->ae_fflags_clear = clear;
903 }
904
905 const char *
archive_entry_copy_fflags_text(struct archive_entry * entry,const char * flags)906 archive_entry_copy_fflags_text(struct archive_entry *entry,
907 const char *flags)
908 {
909 return archive_entry_copy_fflags_text_len(entry, flags, strlen(flags));
910 }
911
912 const char *
archive_entry_copy_fflags_text_len(struct archive_entry * entry,const char * flags,size_t flags_length)913 archive_entry_copy_fflags_text_len(struct archive_entry *entry,
914 const char *flags, size_t flags_length)
915 {
916 archive_mstring_copy_mbs_len(&entry->ae_fflags_text, flags, flags_length);
917 return (ae_strtofflags(flags, flags_length,
918 &entry->ae_fflags_set, &entry->ae_fflags_clear));
919 }
920
921 const wchar_t *
archive_entry_copy_fflags_text_w(struct archive_entry * entry,const wchar_t * flags)922 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
923 const wchar_t *flags)
924 {
925 archive_mstring_copy_wcs(&entry->ae_fflags_text, flags);
926 return (ae_wcstofflags(flags,
927 &entry->ae_fflags_set, &entry->ae_fflags_clear));
928 }
929
930 void
archive_entry_set_gid(struct archive_entry * entry,la_int64_t g)931 archive_entry_set_gid(struct archive_entry *entry, la_int64_t g)
932 {
933 if (g < 0) {
934 g = 0;
935 }
936 entry->stat_valid = 0;
937 entry->ae_stat.aest_gid = g;
938 entry->ae_set |= AE_SET_GID;
939 }
940
941 void
archive_entry_set_gname(struct archive_entry * entry,const char * name)942 archive_entry_set_gname(struct archive_entry *entry, const char *name)
943 {
944 archive_mstring_copy_mbs(&entry->ae_gname, name);
945 }
946
947 void
archive_entry_set_gname_utf8(struct archive_entry * entry,const char * name)948 archive_entry_set_gname_utf8(struct archive_entry *entry, const char *name)
949 {
950 archive_mstring_copy_utf8(&entry->ae_gname, name);
951 }
952
953 void
archive_entry_copy_gname(struct archive_entry * entry,const char * name)954 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
955 {
956 archive_mstring_copy_mbs(&entry->ae_gname, name);
957 }
958
959 void
archive_entry_copy_gname_w(struct archive_entry * entry,const wchar_t * name)960 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
961 {
962 archive_mstring_copy_wcs(&entry->ae_gname, name);
963 }
964
965 int
archive_entry_update_gname_utf8(struct archive_entry * entry,const char * name)966 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
967 {
968 if (archive_mstring_update_utf8(entry->archive,
969 &entry->ae_gname, name) == 0)
970 return (1);
971 if (errno == ENOMEM)
972 __archive_errx(1, "No memory");
973 return (0);
974 }
975
976 int
_archive_entry_copy_gname_l(struct archive_entry * entry,const char * name,size_t len,struct archive_string_conv * sc)977 _archive_entry_copy_gname_l(struct archive_entry *entry,
978 const char *name, size_t len, struct archive_string_conv *sc)
979 {
980 return (archive_mstring_copy_mbs_len_l(&entry->ae_gname, name, len, sc));
981 }
982
983 void
archive_entry_set_ino(struct archive_entry * entry,la_int64_t ino)984 archive_entry_set_ino(struct archive_entry *entry, la_int64_t ino)
985 {
986 if (ino < 0) {
987 entry->stat_valid = 0;
988 entry->ae_set &= ~AE_SET_INO;
989 return;
990 }
991 entry->stat_valid = 0;
992 entry->ae_set |= AE_SET_INO;
993 entry->ae_stat.aest_ino = ino;
994 }
995
996 void
archive_entry_set_ino64(struct archive_entry * entry,la_int64_t ino)997 archive_entry_set_ino64(struct archive_entry *entry, la_int64_t ino)
998 {
999 if (ino < 0) {
1000 entry->stat_valid = 0;
1001 entry->ae_set &= ~AE_SET_INO;
1002 return;
1003 }
1004 entry->stat_valid = 0;
1005 entry->ae_set |= AE_SET_INO;
1006 entry->ae_stat.aest_ino = ino;
1007 }
1008
1009 void
archive_entry_set_hardlink(struct archive_entry * entry,const char * target)1010 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
1011 {
1012 if (target == NULL) {
1013 entry->ae_set &= ~AE_SET_HARDLINK;
1014 if (entry->ae_set & AE_SET_SYMLINK) {
1015 return;
1016 }
1017 } else {
1018 entry->ae_set |= AE_SET_HARDLINK;
1019 }
1020 entry->ae_set &= ~AE_SET_SYMLINK;
1021 archive_mstring_copy_mbs(&entry->ae_linkname, target);
1022 }
1023
1024 void
archive_entry_set_hardlink_utf8(struct archive_entry * entry,const char * target)1025 archive_entry_set_hardlink_utf8(struct archive_entry *entry, const char *target)
1026 {
1027 if (target == NULL && (entry->ae_set & AE_SET_SYMLINK))
1028 return;
1029 archive_mstring_copy_utf8(&entry->ae_linkname, target);
1030 if (target != NULL)
1031 entry->ae_set |= AE_SET_HARDLINK;
1032 else
1033 entry->ae_set &= ~AE_SET_HARDLINK;
1034 }
1035
1036 void
archive_entry_copy_hardlink(struct archive_entry * entry,const char * target)1037 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
1038 {
1039 if (target == NULL && (entry->ae_set & AE_SET_SYMLINK))
1040 return;
1041 archive_mstring_copy_mbs(&entry->ae_linkname, target);
1042 if (target != NULL)
1043 entry->ae_set |= AE_SET_HARDLINK;
1044 else
1045 entry->ae_set &= ~AE_SET_HARDLINK;
1046 }
1047
1048 void
archive_entry_copy_hardlink_w(struct archive_entry * entry,const wchar_t * target)1049 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
1050 {
1051 if (target == NULL && (entry->ae_set & AE_SET_SYMLINK))
1052 return;
1053 archive_mstring_copy_wcs(&entry->ae_linkname, target);
1054 if (target != NULL)
1055 entry->ae_set |= AE_SET_HARDLINK;
1056 else
1057 entry->ae_set &= ~AE_SET_HARDLINK;
1058 }
1059
1060 int
archive_entry_update_hardlink_utf8(struct archive_entry * entry,const char * target)1061 archive_entry_update_hardlink_utf8(struct archive_entry *entry, const char *target)
1062 {
1063 if (target == NULL && (entry->ae_set & AE_SET_SYMLINK))
1064 return (0);
1065 if (target != NULL)
1066 entry->ae_set |= AE_SET_HARDLINK;
1067 else
1068 entry->ae_set &= ~AE_SET_HARDLINK;
1069 if (archive_mstring_update_utf8(entry->archive,
1070 &entry->ae_linkname, target) == 0)
1071 return (1);
1072 if (errno == ENOMEM)
1073 __archive_errx(1, "No memory");
1074 return (0);
1075 }
1076
1077 int
_archive_entry_copy_hardlink_l(struct archive_entry * entry,const char * target,size_t len,struct archive_string_conv * sc)1078 _archive_entry_copy_hardlink_l(struct archive_entry *entry,
1079 const char *target, size_t len, struct archive_string_conv *sc)
1080 {
1081 int r;
1082
1083 if (target == NULL && (entry->ae_set & AE_SET_SYMLINK))
1084 return (0);
1085 r = archive_mstring_copy_mbs_len_l(&entry->ae_linkname,
1086 target, len, sc);
1087 if (target != NULL && r == 0)
1088 entry->ae_set |= AE_SET_HARDLINK;
1089 else
1090 entry->ae_set &= ~AE_SET_HARDLINK;
1091 return (r);
1092 }
1093
1094 void
archive_entry_set_atime(struct archive_entry * entry,__LA_TIME_T t,long ns)1095 archive_entry_set_atime(struct archive_entry *entry, __LA_TIME_T t, long ns)
1096 {
1097 FIX_NS(t, ns);
1098 entry->stat_valid = 0;
1099 entry->ae_set |= AE_SET_ATIME;
1100 entry->ae_stat.aest_atime = t;
1101 entry->ae_stat.aest_atime_nsec = ns;
1102 }
1103
1104 void
archive_entry_unset_atime(struct archive_entry * entry)1105 archive_entry_unset_atime(struct archive_entry *entry)
1106 {
1107 archive_entry_set_atime(entry, 0, 0);
1108 entry->ae_set &= ~AE_SET_ATIME;
1109 }
1110
1111 void
archive_entry_set_birthtime(struct archive_entry * entry,__LA_TIME_T t,long ns)1112 archive_entry_set_birthtime(struct archive_entry *entry, __LA_TIME_T t, long ns)
1113 {
1114 FIX_NS(t, ns);
1115 entry->stat_valid = 0;
1116 entry->ae_set |= AE_SET_BIRTHTIME;
1117 entry->ae_stat.aest_birthtime = t;
1118 entry->ae_stat.aest_birthtime_nsec = ns;
1119 }
1120
1121 void
archive_entry_unset_birthtime(struct archive_entry * entry)1122 archive_entry_unset_birthtime(struct archive_entry *entry)
1123 {
1124 archive_entry_set_birthtime(entry, 0, 0);
1125 entry->ae_set &= ~AE_SET_BIRTHTIME;
1126 }
1127
1128 void
archive_entry_set_ctime(struct archive_entry * entry,__LA_TIME_T t,long ns)1129 archive_entry_set_ctime(struct archive_entry *entry, __LA_TIME_T t, long ns)
1130 {
1131 FIX_NS(t, ns);
1132 entry->stat_valid = 0;
1133 entry->ae_set |= AE_SET_CTIME;
1134 entry->ae_stat.aest_ctime = t;
1135 entry->ae_stat.aest_ctime_nsec = ns;
1136 }
1137
1138 void
archive_entry_unset_ctime(struct archive_entry * entry)1139 archive_entry_unset_ctime(struct archive_entry *entry)
1140 {
1141 archive_entry_set_ctime(entry, 0, 0);
1142 entry->ae_set &= ~AE_SET_CTIME;
1143 }
1144
1145 void
archive_entry_set_dev(struct archive_entry * entry,__LA_DEV_T d)1146 archive_entry_set_dev(struct archive_entry *entry, __LA_DEV_T d)
1147 {
1148 entry->stat_valid = 0;
1149 entry->ae_set |= AE_SET_DEV;
1150 entry->ae_stat.aest_dev_is_broken_down = 0;
1151 entry->ae_stat.aest_dev = d;
1152 }
1153
1154 void
archive_entry_set_devmajor(struct archive_entry * entry,__LA_DEV_T m)1155 archive_entry_set_devmajor(struct archive_entry *entry, __LA_DEV_T m)
1156 {
1157 entry->stat_valid = 0;
1158 entry->ae_set |= AE_SET_DEV;
1159 entry->ae_stat.aest_dev_is_broken_down = 1;
1160 entry->ae_stat.aest_devmajor = m;
1161 }
1162
1163 void
archive_entry_set_devminor(struct archive_entry * entry,__LA_DEV_T m)1164 archive_entry_set_devminor(struct archive_entry *entry, __LA_DEV_T m)
1165 {
1166 entry->stat_valid = 0;
1167 entry->ae_set |= AE_SET_DEV;
1168 entry->ae_stat.aest_dev_is_broken_down = 1;
1169 entry->ae_stat.aest_devminor = m;
1170 }
1171
1172 /* Set symlink if symlink is already set, else set hardlink. */
1173 void
archive_entry_set_link(struct archive_entry * entry,const char * target)1174 archive_entry_set_link(struct archive_entry *entry, const char *target)
1175 {
1176 archive_mstring_copy_mbs(&entry->ae_linkname, target);
1177 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
1178 entry->ae_set |= AE_SET_HARDLINK;
1179 }
1180 }
1181
1182 void
archive_entry_set_link_utf8(struct archive_entry * entry,const char * target)1183 archive_entry_set_link_utf8(struct archive_entry *entry, const char *target)
1184 {
1185 archive_mstring_copy_utf8(&entry->ae_linkname, target);
1186 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
1187 entry->ae_set |= AE_SET_HARDLINK;
1188 }
1189 }
1190
1191 /* Set symlink if symlink is already set, else set hardlink. */
1192 void
archive_entry_copy_link(struct archive_entry * entry,const char * target)1193 archive_entry_copy_link(struct archive_entry *entry, const char *target)
1194 {
1195 archive_mstring_copy_mbs(&entry->ae_linkname, target);
1196 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
1197 entry->ae_set |= AE_SET_HARDLINK;
1198 }
1199 }
1200
1201 /* Set symlink if symlink is already set, else set hardlink. */
1202 void
archive_entry_copy_link_w(struct archive_entry * entry,const wchar_t * target)1203 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
1204 {
1205 archive_mstring_copy_wcs(&entry->ae_linkname, target);
1206 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
1207 entry->ae_set |= AE_SET_HARDLINK;
1208 }
1209 }
1210
1211 int
archive_entry_update_link_utf8(struct archive_entry * entry,const char * target)1212 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
1213 {
1214 int r;
1215 r = archive_mstring_update_utf8(entry->archive,
1216 &entry->ae_linkname, target);
1217 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
1218 entry->ae_set |= AE_SET_HARDLINK;
1219 }
1220 if (r == 0)
1221 return (1);
1222 if (errno == ENOMEM)
1223 __archive_errx(1, "No memory");
1224 return (0);
1225 }
1226
1227 int
_archive_entry_copy_link_l(struct archive_entry * entry,const char * target,size_t len,struct archive_string_conv * sc)1228 _archive_entry_copy_link_l(struct archive_entry *entry,
1229 const char *target, size_t len, struct archive_string_conv *sc)
1230 {
1231 int r;
1232
1233 r = archive_mstring_copy_mbs_len_l(&entry->ae_linkname,
1234 target, len, sc);
1235 if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
1236 entry->ae_set |= AE_SET_HARDLINK;
1237 }
1238 return (r);
1239 }
1240
1241 void
archive_entry_set_mode(struct archive_entry * entry,__LA_MODE_T m)1242 archive_entry_set_mode(struct archive_entry *entry, __LA_MODE_T m)
1243 {
1244 entry->stat_valid = 0;
1245 entry->acl.mode = m;
1246 entry->ae_set |= AE_SET_PERM | AE_SET_FILETYPE;
1247 }
1248
1249 void
archive_entry_set_mtime(struct archive_entry * entry,__LA_TIME_T t,long ns)1250 archive_entry_set_mtime(struct archive_entry *entry, __LA_TIME_T t, long ns)
1251 {
1252 FIX_NS(t, ns);
1253 entry->stat_valid = 0;
1254 entry->ae_set |= AE_SET_MTIME;
1255 entry->ae_stat.aest_mtime = t;
1256 entry->ae_stat.aest_mtime_nsec = ns;
1257 }
1258
1259 void
archive_entry_unset_mtime(struct archive_entry * entry)1260 archive_entry_unset_mtime(struct archive_entry *entry)
1261 {
1262 archive_entry_set_mtime(entry, 0, 0);
1263 entry->ae_set &= ~AE_SET_MTIME;
1264 }
1265
1266 void
archive_entry_set_nlink(struct archive_entry * entry,unsigned int nlink)1267 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
1268 {
1269 entry->stat_valid = 0;
1270 entry->ae_stat.aest_nlink = nlink;
1271 }
1272
1273 void
archive_entry_set_pathname(struct archive_entry * entry,const char * name)1274 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
1275 {
1276 archive_mstring_copy_mbs(&entry->ae_pathname, name);
1277 }
1278
1279 void
archive_entry_set_pathname_utf8(struct archive_entry * entry,const char * name)1280 archive_entry_set_pathname_utf8(struct archive_entry *entry, const char *name)
1281 {
1282 archive_mstring_copy_utf8(&entry->ae_pathname, name);
1283 }
1284
1285 void
archive_entry_copy_pathname(struct archive_entry * entry,const char * name)1286 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
1287 {
1288 archive_mstring_copy_mbs(&entry->ae_pathname, name);
1289 }
1290
1291 void
archive_entry_copy_pathname_w(struct archive_entry * entry,const wchar_t * name)1292 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
1293 {
1294 archive_mstring_copy_wcs(&entry->ae_pathname, name);
1295 }
1296
1297 int
archive_entry_update_pathname_utf8(struct archive_entry * entry,const char * name)1298 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
1299 {
1300 if (archive_mstring_update_utf8(entry->archive,
1301 &entry->ae_pathname, name) == 0)
1302 return (1);
1303 if (errno == ENOMEM)
1304 __archive_errx(1, "No memory");
1305 return (0);
1306 }
1307
1308 int
_archive_entry_copy_pathname_l(struct archive_entry * entry,const char * name,size_t len,struct archive_string_conv * sc)1309 _archive_entry_copy_pathname_l(struct archive_entry *entry,
1310 const char *name, size_t len, struct archive_string_conv *sc)
1311 {
1312 return (archive_mstring_copy_mbs_len_l(&entry->ae_pathname,
1313 name, len, sc));
1314 }
1315
1316 void
archive_entry_set_perm(struct archive_entry * entry,__LA_MODE_T p)1317 archive_entry_set_perm(struct archive_entry *entry, __LA_MODE_T p)
1318 {
1319 entry->stat_valid = 0;
1320 entry->acl.mode &= AE_IFMT;
1321 entry->acl.mode |= ~AE_IFMT & p;
1322 entry->ae_set |= AE_SET_PERM;
1323 }
1324
1325 void
archive_entry_set_rdev(struct archive_entry * entry,__LA_DEV_T m)1326 archive_entry_set_rdev(struct archive_entry *entry, __LA_DEV_T m)
1327 {
1328 entry->stat_valid = 0;
1329 entry->ae_stat.aest_rdev = m;
1330 entry->ae_stat.aest_rdev_is_broken_down = 0;
1331 entry->ae_stat.aest_rdevmajor = 0;
1332 entry->ae_stat.aest_rdevminor = 0;
1333 entry->ae_set |= AE_SET_RDEV;
1334 }
1335
1336 void
archive_entry_set_rdevmajor(struct archive_entry * entry,__LA_DEV_T m)1337 archive_entry_set_rdevmajor(struct archive_entry *entry, __LA_DEV_T m)
1338 {
1339 entry->stat_valid = 0;
1340 entry->ae_stat.aest_rdev_is_broken_down = 1;
1341 entry->ae_stat.aest_rdev = 0;
1342 entry->ae_stat.aest_rdevmajor = m;
1343 entry->ae_set |= AE_SET_RDEV;
1344 }
1345
1346 void
archive_entry_set_rdevminor(struct archive_entry * entry,__LA_DEV_T m)1347 archive_entry_set_rdevminor(struct archive_entry *entry, __LA_DEV_T m)
1348 {
1349 entry->stat_valid = 0;
1350 entry->ae_stat.aest_rdev_is_broken_down = 1;
1351 entry->ae_stat.aest_rdev = 0;
1352 entry->ae_stat.aest_rdevminor = m;
1353 entry->ae_set |= AE_SET_RDEV;
1354 }
1355
1356 void
archive_entry_set_size(struct archive_entry * entry,la_int64_t s)1357 archive_entry_set_size(struct archive_entry *entry, la_int64_t s)
1358 {
1359 if (s < 0) {
1360 s = 0;
1361 }
1362 entry->stat_valid = 0;
1363 entry->ae_stat.aest_size = s;
1364 entry->ae_set |= AE_SET_SIZE;
1365 }
1366
1367 void
archive_entry_unset_size(struct archive_entry * entry)1368 archive_entry_unset_size(struct archive_entry *entry)
1369 {
1370 archive_entry_set_size(entry, 0);
1371 entry->ae_set &= ~AE_SET_SIZE;
1372 }
1373
1374 void
archive_entry_copy_sourcepath(struct archive_entry * entry,const char * path)1375 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1376 {
1377 archive_mstring_copy_mbs(&entry->ae_sourcepath, path);
1378 }
1379
1380 void
archive_entry_copy_sourcepath_w(struct archive_entry * entry,const wchar_t * path)1381 archive_entry_copy_sourcepath_w(struct archive_entry *entry, const wchar_t *path)
1382 {
1383 archive_mstring_copy_wcs(&entry->ae_sourcepath, path);
1384 }
1385
1386 void
archive_entry_set_symlink(struct archive_entry * entry,const char * linkname)1387 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1388 {
1389 if (linkname == NULL && (entry->ae_set & AE_SET_HARDLINK))
1390 return;
1391 archive_mstring_copy_mbs(&entry->ae_linkname, linkname);
1392 entry->ae_set &= ~AE_SET_HARDLINK;
1393 if (linkname == NULL)
1394 entry->ae_set &= ~AE_SET_SYMLINK;
1395 else
1396 entry->ae_set |= AE_SET_SYMLINK;
1397 }
1398
1399 void
archive_entry_set_symlink_type(struct archive_entry * entry,int type)1400 archive_entry_set_symlink_type(struct archive_entry *entry, int type)
1401 {
1402 entry->ae_symlink_type = type;
1403 }
1404
1405 void
archive_entry_set_symlink_utf8(struct archive_entry * entry,const char * linkname)1406 archive_entry_set_symlink_utf8(struct archive_entry *entry, const char *linkname)
1407 {
1408 if (linkname == NULL && (entry->ae_set & AE_SET_HARDLINK))
1409 return;
1410 archive_mstring_copy_utf8(&entry->ae_linkname, linkname);
1411 entry->ae_set &= ~AE_SET_HARDLINK;
1412 if (linkname == NULL)
1413 entry->ae_set &= ~AE_SET_SYMLINK;
1414 else
1415 entry->ae_set |= AE_SET_SYMLINK;
1416 }
1417
1418 void
archive_entry_copy_symlink(struct archive_entry * entry,const char * linkname)1419 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1420 {
1421 if (linkname == NULL && (entry->ae_set & AE_SET_HARDLINK))
1422 return;
1423 archive_mstring_copy_mbs(&entry->ae_linkname, linkname);
1424 entry->ae_set &= ~AE_SET_HARDLINK;
1425 if (linkname == NULL)
1426 entry->ae_set &= ~AE_SET_SYMLINK;
1427 else
1428 entry->ae_set |= AE_SET_SYMLINK;
1429 }
1430
1431 void
archive_entry_copy_symlink_w(struct archive_entry * entry,const wchar_t * linkname)1432 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1433 {
1434 if (linkname == NULL && (entry->ae_set & AE_SET_HARDLINK))
1435 return;
1436 archive_mstring_copy_wcs(&entry->ae_linkname, linkname);
1437 entry->ae_set &= ~AE_SET_HARDLINK;
1438 if (linkname == NULL)
1439 entry->ae_set &= ~AE_SET_SYMLINK;
1440 else
1441 entry->ae_set |= AE_SET_SYMLINK;
1442 }
1443
1444 int
archive_entry_update_symlink_utf8(struct archive_entry * entry,const char * linkname)1445 archive_entry_update_symlink_utf8(struct archive_entry *entry, const char *linkname)
1446 {
1447 if (linkname == NULL && (entry->ae_set & AE_SET_HARDLINK))
1448 return (0);
1449 entry->ae_set &= ~AE_SET_HARDLINK;
1450 if (linkname == NULL)
1451 entry->ae_set &= ~AE_SET_SYMLINK;
1452 else
1453 entry->ae_set |= AE_SET_SYMLINK;
1454 if (archive_mstring_update_utf8(entry->archive,
1455 &entry->ae_linkname, linkname) == 0)
1456 return (1);
1457 if (errno == ENOMEM)
1458 __archive_errx(1, "No memory");
1459 return (0);
1460 }
1461
1462 int
_archive_entry_copy_symlink_l(struct archive_entry * entry,const char * linkname,size_t len,struct archive_string_conv * sc)1463 _archive_entry_copy_symlink_l(struct archive_entry *entry,
1464 const char *linkname, size_t len, struct archive_string_conv *sc)
1465 {
1466 int r;
1467
1468 if (linkname == NULL && (entry->ae_set & AE_SET_HARDLINK))
1469 return (0);
1470 entry->ae_set &= ~AE_SET_HARDLINK;
1471 r = archive_mstring_copy_mbs_len_l(&entry->ae_linkname,
1472 linkname, len, sc);
1473 if (linkname == NULL || r != 0)
1474 entry->ae_set &= ~AE_SET_SYMLINK;
1475 else
1476 entry->ae_set |= AE_SET_SYMLINK;
1477 return (r);
1478 }
1479
1480 void
archive_entry_set_uid(struct archive_entry * entry,la_int64_t u)1481 archive_entry_set_uid(struct archive_entry *entry, la_int64_t u)
1482 {
1483 if (u < 0) {
1484 u = 0;
1485 }
1486 entry->stat_valid = 0;
1487 entry->ae_stat.aest_uid = u;
1488 entry->ae_set |= AE_SET_UID;
1489 }
1490
1491 void
archive_entry_set_uname(struct archive_entry * entry,const char * name)1492 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1493 {
1494 archive_mstring_copy_mbs(&entry->ae_uname, name);
1495 }
1496
1497 void
archive_entry_set_uname_utf8(struct archive_entry * entry,const char * name)1498 archive_entry_set_uname_utf8(struct archive_entry *entry, const char *name)
1499 {
1500 archive_mstring_copy_utf8(&entry->ae_uname, name);
1501 }
1502
1503 void
archive_entry_copy_uname(struct archive_entry * entry,const char * name)1504 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1505 {
1506 archive_mstring_copy_mbs(&entry->ae_uname, name);
1507 }
1508
1509 void
archive_entry_copy_uname_w(struct archive_entry * entry,const wchar_t * name)1510 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1511 {
1512 archive_mstring_copy_wcs(&entry->ae_uname, name);
1513 }
1514
1515 int
archive_entry_update_uname_utf8(struct archive_entry * entry,const char * name)1516 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1517 {
1518 if (archive_mstring_update_utf8(entry->archive,
1519 &entry->ae_uname, name) == 0)
1520 return (1);
1521 if (errno == ENOMEM)
1522 __archive_errx(1, "No memory");
1523 return (0);
1524 }
1525
1526 void
archive_entry_set_is_data_encrypted(struct archive_entry * entry,char is_encrypted)1527 archive_entry_set_is_data_encrypted(struct archive_entry *entry, char is_encrypted)
1528 {
1529 if (is_encrypted) {
1530 entry->encryption |= AE_ENCRYPTION_DATA;
1531 } else {
1532 entry->encryption &= ~AE_ENCRYPTION_DATA;
1533 }
1534 }
1535
1536 void
archive_entry_set_is_metadata_encrypted(struct archive_entry * entry,char is_encrypted)1537 archive_entry_set_is_metadata_encrypted(struct archive_entry *entry, char is_encrypted)
1538 {
1539 if (is_encrypted) {
1540 entry->encryption |= AE_ENCRYPTION_METADATA;
1541 } else {
1542 entry->encryption &= ~AE_ENCRYPTION_METADATA;
1543 }
1544 }
1545
1546 int
_archive_entry_copy_uname_l(struct archive_entry * entry,const char * name,size_t len,struct archive_string_conv * sc)1547 _archive_entry_copy_uname_l(struct archive_entry *entry,
1548 const char *name, size_t len, struct archive_string_conv *sc)
1549 {
1550 return (archive_mstring_copy_mbs_len_l(&entry->ae_uname,
1551 name, len, sc));
1552 }
1553
1554 const void *
archive_entry_mac_metadata(struct archive_entry * entry,size_t * s)1555 archive_entry_mac_metadata(struct archive_entry *entry, size_t *s)
1556 {
1557 *s = entry->mac_metadata_size;
1558 return entry->mac_metadata;
1559 }
1560
1561 void
archive_entry_copy_mac_metadata(struct archive_entry * entry,const void * p,size_t s)1562 archive_entry_copy_mac_metadata(struct archive_entry *entry,
1563 const void *p, size_t s)
1564 {
1565 void *metadata;
1566
1567 if (p == NULL || s == 0) {
1568 free(entry->mac_metadata);
1569 entry->mac_metadata = NULL;
1570 entry->mac_metadata_size = 0;
1571 } else {
1572 metadata = malloc(s);
1573 if (metadata == NULL)
1574 abort();
1575 memcpy(metadata, p, s);
1576 free(entry->mac_metadata);
1577 entry->mac_metadata = metadata;
1578 entry->mac_metadata_size = s;
1579 }
1580 }
1581
1582 /* Digest handling */
1583 const unsigned char *
archive_entry_digest(struct archive_entry * entry,int type)1584 archive_entry_digest(struct archive_entry *entry, int type)
1585 {
1586 switch (type) {
1587 case ARCHIVE_ENTRY_DIGEST_MD5:
1588 return entry->digest.md5;
1589 case ARCHIVE_ENTRY_DIGEST_RMD160:
1590 return entry->digest.rmd160;
1591 case ARCHIVE_ENTRY_DIGEST_SHA1:
1592 return entry->digest.sha1;
1593 case ARCHIVE_ENTRY_DIGEST_SHA256:
1594 return entry->digest.sha256;
1595 case ARCHIVE_ENTRY_DIGEST_SHA384:
1596 return entry->digest.sha384;
1597 case ARCHIVE_ENTRY_DIGEST_SHA512:
1598 return entry->digest.sha512;
1599 default:
1600 return NULL;
1601 }
1602 }
1603
1604 int
archive_entry_set_digest(struct archive_entry * entry,int type,const unsigned char * digest)1605 archive_entry_set_digest(struct archive_entry *entry, int type,
1606 const unsigned char *digest)
1607 {
1608 #define copy_digest(_e, _t, _d)\
1609 memcpy(_e->digest._t, _d, sizeof(_e->digest._t))
1610
1611 switch (type) {
1612 case ARCHIVE_ENTRY_DIGEST_MD5:
1613 copy_digest(entry, md5, digest);
1614 entry->mset_digest |= AE_MSET_DIGEST_MD5;
1615 break;
1616 case ARCHIVE_ENTRY_DIGEST_RMD160:
1617 copy_digest(entry, rmd160, digest);
1618 entry->mset_digest |= AE_MSET_DIGEST_RMD160;
1619 break;
1620 case ARCHIVE_ENTRY_DIGEST_SHA1:
1621 copy_digest(entry, sha1, digest);
1622 entry->mset_digest |= AE_MSET_DIGEST_SHA1;
1623 break;
1624 case ARCHIVE_ENTRY_DIGEST_SHA256:
1625 copy_digest(entry, sha256, digest);
1626 entry->mset_digest |= AE_MSET_DIGEST_SHA256;
1627 break;
1628 case ARCHIVE_ENTRY_DIGEST_SHA384:
1629 copy_digest(entry, sha384, digest);
1630 entry->mset_digest |= AE_MSET_DIGEST_SHA384;
1631 break;
1632 case ARCHIVE_ENTRY_DIGEST_SHA512:
1633 copy_digest(entry, sha512, digest);
1634 entry->mset_digest |= AE_MSET_DIGEST_SHA512;
1635 break;
1636 default:
1637 return ARCHIVE_WARN;
1638 }
1639
1640 return ARCHIVE_OK;
1641 #undef copy_digest
1642 }
1643
1644 /*
1645 * ACL management. The following would, of course, be a lot simpler
1646 * if: 1) the last draft of POSIX.1e were a really thorough and
1647 * complete standard that addressed the needs of ACL archiving and 2)
1648 * everyone followed it faithfully. Alas, neither is true, so the
1649 * following is a lot more complex than might seem necessary to the
1650 * uninitiated.
1651 */
1652
1653 struct archive_acl *
archive_entry_acl(struct archive_entry * entry)1654 archive_entry_acl(struct archive_entry *entry)
1655 {
1656 return &entry->acl;
1657 }
1658
1659 void
archive_entry_acl_clear(struct archive_entry * entry)1660 archive_entry_acl_clear(struct archive_entry *entry)
1661 {
1662 archive_acl_clear(&entry->acl);
1663 }
1664
1665 /*
1666 * Add a single ACL entry to the internal list of ACL data.
1667 */
1668 int
archive_entry_acl_add_entry(struct archive_entry * entry,int type,int permset,int tag,int id,const char * name)1669 archive_entry_acl_add_entry(struct archive_entry *entry,
1670 int type, int permset, int tag, int id, const char *name)
1671 {
1672 return archive_acl_add_entry(&entry->acl, type, permset, tag, id, name);
1673 }
1674
1675 /*
1676 * As above, but with a wide-character name.
1677 */
1678 int
archive_entry_acl_add_entry_w(struct archive_entry * entry,int type,int permset,int tag,int id,const wchar_t * name)1679 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1680 int type, int permset, int tag, int id, const wchar_t *name)
1681 {
1682 return archive_acl_add_entry_w_len(&entry->acl,
1683 type, permset, tag, id, name, wcslen(name));
1684 }
1685
1686 /*
1687 * Return a bitmask of ACL types in an archive entry ACL list
1688 */
1689 int
archive_entry_acl_types(struct archive_entry * entry)1690 archive_entry_acl_types(struct archive_entry *entry)
1691 {
1692 return (archive_acl_types(&entry->acl));
1693 }
1694
1695 /*
1696 * Return a count of entries matching "want_type".
1697 */
1698 int
archive_entry_acl_count(struct archive_entry * entry,int want_type)1699 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1700 {
1701 return archive_acl_count(&entry->acl, want_type);
1702 }
1703
1704 /*
1705 * Prepare for reading entries from the ACL data. Returns a count
1706 * of entries matching "want_type", or zero if there are no
1707 * non-extended ACL entries of that type.
1708 */
1709 int
archive_entry_acl_reset(struct archive_entry * entry,int want_type)1710 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1711 {
1712 return archive_acl_reset(&entry->acl, want_type);
1713 }
1714
1715 /*
1716 * Return the next ACL entry in the list. Fake entries for the
1717 * standard permissions and include them in the returned list.
1718 */
1719 int
archive_entry_acl_next(struct archive_entry * entry,int want_type,int * type,int * permset,int * tag,int * id,const char ** name)1720 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1721 int *permset, int *tag, int *id, const char **name)
1722 {
1723 int r;
1724 r = archive_acl_next(entry->archive, &entry->acl, want_type, type,
1725 permset, tag, id, name);
1726 if (r == ARCHIVE_FATAL && errno == ENOMEM)
1727 __archive_errx(1, "No memory");
1728 return (r);
1729 }
1730
1731 /*
1732 * Generate a text version of the ACL. The flags parameter controls
1733 * the style of the generated ACL.
1734 */
1735 wchar_t *
archive_entry_acl_to_text_w(struct archive_entry * entry,la_ssize_t * len,int flags)1736 archive_entry_acl_to_text_w(struct archive_entry *entry, la_ssize_t *len,
1737 int flags)
1738 {
1739 return (archive_acl_to_text_w(&entry->acl, len, flags,
1740 entry->archive));
1741 }
1742
1743 char *
archive_entry_acl_to_text(struct archive_entry * entry,la_ssize_t * len,int flags)1744 archive_entry_acl_to_text(struct archive_entry *entry, la_ssize_t *len,
1745 int flags)
1746 {
1747 return (archive_acl_to_text_l(&entry->acl, len, flags, NULL));
1748 }
1749
1750 char *
_archive_entry_acl_to_text_l(struct archive_entry * entry,ssize_t * len,int flags,struct archive_string_conv * sc)1751 _archive_entry_acl_to_text_l(struct archive_entry *entry, ssize_t *len,
1752 int flags, struct archive_string_conv *sc)
1753 {
1754 return (archive_acl_to_text_l(&entry->acl, len, flags, sc));
1755 }
1756
1757 /*
1758 * ACL text parser.
1759 */
1760 int
archive_entry_acl_from_text_w(struct archive_entry * entry,const wchar_t * wtext,int type)1761 archive_entry_acl_from_text_w(struct archive_entry *entry,
1762 const wchar_t *wtext, int type)
1763 {
1764 return (archive_acl_from_text_w(&entry->acl, wtext, type));
1765 }
1766
1767 int
archive_entry_acl_from_text(struct archive_entry * entry,const char * text,int type)1768 archive_entry_acl_from_text(struct archive_entry *entry,
1769 const char *text, int type)
1770 {
1771 return (archive_acl_from_text_l(&entry->acl, text, type, NULL));
1772 }
1773
1774 int
_archive_entry_acl_from_text_l(struct archive_entry * entry,const char * text,int type,struct archive_string_conv * sc)1775 _archive_entry_acl_from_text_l(struct archive_entry *entry, const char *text,
1776 int type, struct archive_string_conv *sc)
1777 {
1778 return (archive_acl_from_text_l(&entry->acl, text, type, sc));
1779 }
1780
1781 /* Deprecated */
1782 static int
archive_entry_acl_text_compat(int * flags)1783 archive_entry_acl_text_compat(int *flags)
1784 {
1785 if ((*flags & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) == 0)
1786 return (1);
1787
1788 /* ABI compat with old ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID */
1789 if ((*flags & OLD_ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID) != 0)
1790 *flags |= ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID;
1791
1792 /* ABI compat with old ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT */
1793 if ((*flags & OLD_ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) != 0)
1794 *flags |= ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT;
1795
1796 *flags |= ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA;
1797
1798 return (0);
1799 }
1800
1801 /* Deprecated */
1802 const wchar_t *
archive_entry_acl_text_w(struct archive_entry * entry,int flags)1803 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1804 {
1805 free(entry->acl.acl_text_w);
1806 entry->acl.acl_text_w = NULL;
1807 if (archive_entry_acl_text_compat(&flags) == 0)
1808 entry->acl.acl_text_w = archive_acl_to_text_w(&entry->acl,
1809 NULL, flags, entry->archive);
1810 return (entry->acl.acl_text_w);
1811 }
1812
1813 /* Deprecated */
1814 const char *
archive_entry_acl_text(struct archive_entry * entry,int flags)1815 archive_entry_acl_text(struct archive_entry *entry, int flags)
1816 {
1817 free(entry->acl.acl_text);
1818 entry->acl.acl_text = NULL;
1819 if (archive_entry_acl_text_compat(&flags) == 0)
1820 entry->acl.acl_text = archive_acl_to_text_l(&entry->acl, NULL,
1821 flags, NULL);
1822
1823 return (entry->acl.acl_text);
1824 }
1825
1826 /* Deprecated */
1827 int
_archive_entry_acl_text_l(struct archive_entry * entry,int flags,const char ** acl_text,size_t * len,struct archive_string_conv * sc)1828 _archive_entry_acl_text_l(struct archive_entry *entry, int flags,
1829 const char **acl_text, size_t *len, struct archive_string_conv *sc)
1830 {
1831 free(entry->acl.acl_text);
1832 entry->acl.acl_text = NULL;
1833
1834 if (archive_entry_acl_text_compat(&flags) == 0)
1835 entry->acl.acl_text = archive_acl_to_text_l(&entry->acl,
1836 (ssize_t *)len, flags, sc);
1837
1838 *acl_text = entry->acl.acl_text;
1839
1840 return (0);
1841 }
1842
1843 /*
1844 * Following code is modified from UC Berkeley sources, and
1845 * is subject to the following copyright notice.
1846 */
1847
1848 /*-
1849 * Copyright (c) 1993
1850 * The Regents of the University of California. All rights reserved.
1851 *
1852 * Redistribution and use in source and binary forms, with or without
1853 * modification, are permitted provided that the following conditions
1854 * are met:
1855 * 1. Redistributions of source code must retain the above copyright
1856 * notice, this list of conditions and the following disclaimer.
1857 * 2. Redistributions in binary form must reproduce the above copyright
1858 * notice, this list of conditions and the following disclaimer in the
1859 * documentation and/or other materials provided with the distribution.
1860 * 3. Neither the name of the University nor the names of its contributors
1861 * may be used to endorse or promote products derived from this software
1862 * without specific prior written permission.
1863 *
1864 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1865 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1866 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1867 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1868 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1869 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1870 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1871 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1872 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1873 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1874 * SUCH DAMAGE.
1875 */
1876
1877 /*
1878 * Supported file flags on FreeBSD and Mac OS:
1879 * sappnd,sappend SF_APPEND
1880 * arch,archived SF_ARCHIVED
1881 * schg,schange,simmutable SF_IMMUTABLE
1882 * sunlnk,sunlink SF_NOUNLINK (FreeBSD only)
1883 * uappnd,uappend UF_APPEND
1884 * compressed UF_COMPRESSED (Mac OS only)
1885 * hidden,uhidden UF_HIDDEN
1886 * uchg,uchange,uimmutable UF_IMMUTABLE
1887 * nodump UF_NODUMP
1888 * uunlnk,uunlink UF_NOUNLINK (FreeBSD only)
1889 * offline,uoffline UF_OFFLINE (FreeBSD only)
1890 * opaque UF_OPAQUE
1891 * rdonly,urdonly,readonly UF_READONLY (FreeBSD only)
1892 * reparse,ureparse UF_REPARSE (FreeBSD only)
1893 * sparse,usparse UF_SPARSE (FreeBSD only)
1894 * system,usystem UF_SYSTEM (FreeBSD only)
1895 *
1896 * See chflags(2) for more information
1897 *
1898 * Supported file attributes on Linux:
1899 * a append only FS_APPEND_FL sappnd
1900 * A no atime updates FS_NOATIME_FL atime
1901 * c compress FS_COMPR_FL compress
1902 * C no copy on write FS_NOCOW_FL cow
1903 * d no dump FS_NODUMP_FL dump
1904 * D synchronous directory updates FS_DIRSYNC_FL dirsync
1905 * i immutable FS_IMMUTABLE_FL schg
1906 * j data journalling FS_JOURNAL_DATA_FL journal
1907 * P project hierarchy FS_PROJINHERIT_FL projinherit
1908 * s secure deletion FS_SECRM_FL securedeletion
1909 * S synchronous updates FS_SYNC_FL sync
1910 * t no tail-merging FS_NOTAIL_FL tail
1911 * T top of directory hierarchy FS_TOPDIR_FL topdir
1912 * u undeletable FS_UNRM_FL undel
1913 *
1914 * See ioctl_iflags(2) for more information
1915 *
1916 * Equivalent file flags supported on FreeBSD / Mac OS and Linux:
1917 * SF_APPEND FS_APPEND_FL sappnd
1918 * SF_IMMUTABLE FS_IMMUTABLE_FL schg
1919 * UF_NODUMP FS_NODUMP_FL nodump
1920 */
1921
1922 static const struct flag {
1923 const char *name;
1924 const wchar_t *wname;
1925 unsigned long set;
1926 unsigned long clear;
1927 } fileflags[] = {
1928 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1929 #ifdef SF_APPEND
1930 { "nosappnd", L"nosappnd", SF_APPEND, 0},
1931 { "nosappend", L"nosappend", SF_APPEND, 0},
1932 #endif
1933 #if defined(FS_APPEND_FL) /* 'a' */
1934 { "nosappnd", L"nosappnd", FS_APPEND_FL, 0},
1935 { "nosappend", L"nosappend", FS_APPEND_FL, 0},
1936 #elif defined(EXT2_APPEND_FL) /* 'a' */
1937 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0},
1938 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0},
1939 #endif
1940 #ifdef SF_ARCHIVED
1941 { "noarch", L"noarch", SF_ARCHIVED, 0},
1942 { "noarchived", L"noarchived", SF_ARCHIVED, 0},
1943 #endif
1944 #ifdef SF_IMMUTABLE
1945 { "noschg", L"noschg", SF_IMMUTABLE, 0},
1946 { "noschange", L"noschange", SF_IMMUTABLE, 0},
1947 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0},
1948 #endif
1949 #if defined(FS_IMMUTABLE_FL) /* 'i' */
1950 { "noschg", L"noschg", FS_IMMUTABLE_FL, 0},
1951 { "noschange", L"noschange", FS_IMMUTABLE_FL, 0},
1952 { "nosimmutable", L"nosimmutable", FS_IMMUTABLE_FL, 0},
1953 #elif defined(EXT2_IMMUTABLE_FL) /* 'i' */
1954 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0},
1955 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0},
1956 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0},
1957 #endif
1958 #ifdef SF_NOUNLINK
1959 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0},
1960 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0},
1961 #endif
1962 #ifdef UF_APPEND
1963 { "nouappnd", L"nouappnd", UF_APPEND, 0},
1964 { "nouappend", L"nouappend", UF_APPEND, 0},
1965 #endif
1966 #ifdef UF_IMMUTABLE
1967 { "nouchg", L"nouchg", UF_IMMUTABLE, 0},
1968 { "nouchange", L"nouchange", UF_IMMUTABLE, 0},
1969 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0},
1970 #endif
1971 #ifdef UF_NODUMP
1972 { "nodump", L"nodump", 0, UF_NODUMP},
1973 #endif
1974 #if defined(FS_NODUMP_FL) /* 'd' */
1975 { "nodump", L"nodump", 0, FS_NODUMP_FL},
1976 #elif defined(EXT2_NODUMP_FL)
1977 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1978 #endif
1979 #ifdef UF_OPAQUE
1980 { "noopaque", L"noopaque", UF_OPAQUE, 0},
1981 #endif
1982 #ifdef UF_NOUNLINK
1983 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0},
1984 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0},
1985 #endif
1986 #ifdef UF_COMPRESSED
1987 /* Mac OS */
1988 { "nocompressed", L"nocompressed", UF_COMPRESSED, 0},
1989 #endif
1990 #ifdef UF_HIDDEN
1991 { "nohidden", L"nohidden", UF_HIDDEN, 0},
1992 { "nouhidden", L"nouhidden", UF_HIDDEN, 0},
1993 #endif
1994 #ifdef FILE_ATTRIBUTE_HIDDEN
1995 { "nohidden", L"nohidden", FILE_ATTRIBUTE_HIDDEN, 0},
1996 { "nouhidden", L"nouhidden", FILE_ATTRIBUTE_HIDDEN, 0},
1997 #endif
1998 #ifdef UF_OFFLINE
1999 { "nooffline", L"nooffline", UF_OFFLINE, 0},
2000 { "nouoffline", L"nouoffline", UF_OFFLINE, 0},
2001 #endif
2002 #ifdef UF_READONLY
2003 { "nordonly", L"nordonly", UF_READONLY, 0},
2004 { "nourdonly", L"nourdonly", UF_READONLY, 0},
2005 { "noreadonly", L"noreadonly", UF_READONLY, 0},
2006 #endif
2007 #ifdef FILE_ATTRIBUTE_READONLY
2008 { "nordonly", L"nordonly", FILE_ATTRIBUTE_READONLY, 0},
2009 { "nourdonly", L"nourdonly", FILE_ATTRIBUTE_READONLY, 0},
2010 { "noreadonly", L"noreadonly", FILE_ATTRIBUTE_READONLY, 0},
2011 #endif
2012 #ifdef UF_SPARSE
2013 { "nosparse", L"nosparse", UF_SPARSE, 0},
2014 { "nousparse", L"nousparse", UF_SPARSE, 0},
2015 #endif
2016 #ifdef UF_REPARSE
2017 { "noreparse", L"noreparse", UF_REPARSE, 0},
2018 { "noureparse", L"noureparse", UF_REPARSE, 0},
2019 #endif
2020 #ifdef UF_SYSTEM
2021 { "nosystem", L"nosystem", UF_SYSTEM, 0},
2022 { "nousystem", L"nousystem", UF_SYSTEM, 0},
2023 #endif
2024 #ifdef FILE_ATTRIBUTE_SYSTEM
2025 { "nosystem", L"nosystem", FILE_ATTRIBUTE_SYSTEM, 0},
2026 { "nousystem", L"nousystem", FILE_ATTRIBUTE_SYSTEM, 0},
2027 #endif
2028 #if defined(FS_UNRM_FL) /* 'u' */
2029 { "noundel", L"noundel", FS_UNRM_FL, 0},
2030 #elif defined(EXT2_UNRM_FL)
2031 { "noundel", L"noundel", EXT2_UNRM_FL, 0},
2032 #endif
2033
2034 #if defined(FS_COMPR_FL) /* 'c' */
2035 { "nocompress", L"nocompress", FS_COMPR_FL, 0},
2036 #elif defined(EXT2_COMPR_FL)
2037 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0},
2038 #endif
2039
2040 #if defined(FS_NOATIME_FL) /* 'A' */
2041 { "noatime", L"noatime", 0, FS_NOATIME_FL},
2042 #elif defined(EXT2_NOATIME_FL)
2043 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
2044 #endif
2045 #if defined(FS_DIRSYNC_FL) /* 'D' */
2046 { "nodirsync", L"nodirsync", FS_DIRSYNC_FL, 0},
2047 #elif defined(EXT2_DIRSYNC_FL)
2048 { "nodirsync", L"nodirsync", EXT2_DIRSYNC_FL, 0},
2049 #endif
2050 #if defined(FS_JOURNAL_DATA_FL) /* 'j' */
2051 { "nojournal-data",L"nojournal-data", FS_JOURNAL_DATA_FL, 0},
2052 { "nojournal", L"nojournal", FS_JOURNAL_DATA_FL, 0},
2053 #elif defined(EXT3_JOURNAL_DATA_FL)
2054 { "nojournal-data",L"nojournal-data", EXT3_JOURNAL_DATA_FL, 0},
2055 { "nojournal", L"nojournal", EXT3_JOURNAL_DATA_FL, 0},
2056 #endif
2057 #if defined(FS_SECRM_FL) /* 's' */
2058 { "nosecdel", L"nosecdel", FS_SECRM_FL, 0},
2059 { "nosecuredeletion",L"nosecuredeletion",FS_SECRM_FL, 0},
2060 #elif defined(EXT2_SECRM_FL)
2061 { "nosecdel", L"nosecdel", EXT2_SECRM_FL, 0},
2062 { "nosecuredeletion",L"nosecuredeletion",EXT2_SECRM_FL, 0},
2063 #endif
2064 #if defined(FS_SYNC_FL) /* 'S' */
2065 { "nosync", L"nosync", FS_SYNC_FL, 0},
2066 #elif defined(EXT2_SYNC_FL)
2067 { "nosync", L"nosync", EXT2_SYNC_FL, 0},
2068 #endif
2069 #if defined(FS_NOTAIL_FL) /* 't' */
2070 { "notail", L"notail", 0, FS_NOTAIL_FL},
2071 #elif defined(EXT2_NOTAIL_FL)
2072 { "notail", L"notail", 0, EXT2_NOTAIL_FL},
2073 #endif
2074 #if defined(FS_TOPDIR_FL) /* 'T' */
2075 { "notopdir", L"notopdir", FS_TOPDIR_FL, 0},
2076 #elif defined(EXT2_TOPDIR_FL)
2077 { "notopdir", L"notopdir", EXT2_TOPDIR_FL, 0},
2078 #endif
2079 #ifdef FS_NOCOW_FL /* 'C' */
2080 { "nocow", L"nocow", 0, FS_NOCOW_FL},
2081 #endif
2082 #ifdef FS_PROJINHERIT_FL /* 'P' */
2083 { "noprojinherit",L"noprojinherit", FS_PROJINHERIT_FL, 0},
2084 #endif
2085 { NULL, NULL, 0, 0}
2086 };
2087
2088 /*
2089 * fflagstostr --
2090 * Convert file flags to a comma-separated string. If no flags
2091 * are set, return the empty string.
2092 */
2093 static char *
ae_fflagstostr(unsigned long bitset,unsigned long bitclear)2094 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
2095 {
2096 char *string, *dp;
2097 const char *sp;
2098 unsigned long bits;
2099 const struct flag *flag;
2100 size_t length;
2101
2102 bits = bitset | bitclear;
2103 length = 0;
2104 for (flag = fileflags; flag->name != NULL; flag++)
2105 if (bits & (flag->set | flag->clear)) {
2106 length += strlen(flag->name) + 1;
2107 bits &= ~(flag->set | flag->clear);
2108 }
2109
2110 if (length == 0)
2111 return (NULL);
2112 string = malloc(length);
2113 if (string == NULL)
2114 return (NULL);
2115
2116 dp = string;
2117 for (flag = fileflags; flag->name != NULL; flag++) {
2118 if (bitset & flag->set || bitclear & flag->clear) {
2119 sp = flag->name + 2;
2120 } else if (bitset & flag->clear || bitclear & flag->set) {
2121 sp = flag->name;
2122 } else
2123 continue;
2124 bitset &= ~(flag->set | flag->clear);
2125 bitclear &= ~(flag->set | flag->clear);
2126 if (dp > string)
2127 *dp++ = ',';
2128 while ((*dp++ = *sp++) != '\0')
2129 ;
2130 dp--;
2131 }
2132
2133 *dp = '\0';
2134 return (string);
2135 }
2136
2137 /*
2138 * strtofflags --
2139 * Take string of arguments and return file flags. This
2140 * version works a little differently than strtofflags(3).
2141 * In particular, it always tests every token, skipping any
2142 * unrecognized tokens. It returns a pointer to the first
2143 * unrecognized token, or NULL if every token was recognized.
2144 * This version is also const-correct and does not modify the
2145 * provided string.
2146 */
2147 static const char *
ae_strtofflags(const char * s,size_t l,unsigned long * setp,unsigned long * clrp)2148 ae_strtofflags(const char *s, size_t l, unsigned long *setp, unsigned long *clrp)
2149 {
2150 const char *start, *end;
2151 const struct flag *flag;
2152 unsigned long set, clear;
2153 const char *failed;
2154
2155 set = clear = 0;
2156 start = s;
2157 failed = NULL;
2158 /* Find start of first token. */
2159 while (l > 0 && (*start == '\t' || *start == ' ' || *start == ',')) {
2160 start++;
2161 l--;
2162 }
2163 while (l > 0) {
2164 size_t length;
2165 /* Locate end of token. */
2166 end = start;
2167 while (l > 0 && *end != '\t' &&
2168 *end != ' ' && *end != ',') {
2169 end++;
2170 l--;
2171 }
2172 length = end - start;
2173 for (flag = fileflags; flag->name != NULL; flag++) {
2174 size_t flag_length = strlen(flag->name);
2175 if (length == flag_length
2176 && memcmp(start, flag->name, length) == 0) {
2177 /* Matched "noXXXX", so reverse the sense. */
2178 clear |= flag->set;
2179 set |= flag->clear;
2180 break;
2181 } else if (length == flag_length - 2
2182 && memcmp(start, flag->name + 2, length) == 0) {
2183 /* Matched "XXXX", so don't reverse. */
2184 set |= flag->set;
2185 clear |= flag->clear;
2186 break;
2187 }
2188 }
2189 /* Ignore unknown flag names. */
2190 if (flag->name == NULL && failed == NULL)
2191 failed = start;
2192
2193 /* Find start of next token. */
2194 start = end;
2195 while (l > 0 && (*start == '\t' || *start == ' ' || *start == ',')) {
2196 start++;
2197 l--;
2198 }
2199
2200 }
2201
2202 if (setp)
2203 *setp = set;
2204 if (clrp)
2205 *clrp = clear;
2206
2207 /* Return location of first failure. */
2208 return (failed);
2209 }
2210
2211 /*
2212 * wcstofflags --
2213 * Take string of arguments and return file flags. This
2214 * version works a little differently than strtofflags(3).
2215 * In particular, it always tests every token, skipping any
2216 * unrecognized tokens. It returns a pointer to the first
2217 * unrecognized token, or NULL if every token was recognized.
2218 * This version is also const-correct and does not modify the
2219 * provided string.
2220 */
2221 static const wchar_t *
ae_wcstofflags(const wchar_t * s,unsigned long * setp,unsigned long * clrp)2222 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
2223 {
2224 const wchar_t *start, *end;
2225 const struct flag *flag;
2226 unsigned long set, clear;
2227 const wchar_t *failed;
2228
2229 set = clear = 0;
2230 start = s;
2231 failed = NULL;
2232 /* Find start of first token. */
2233 while (*start == L'\t' || *start == L' ' || *start == L',')
2234 start++;
2235 while (*start != L'\0') {
2236 size_t length;
2237 /* Locate end of token. */
2238 end = start;
2239 while (*end != L'\0' && *end != L'\t' &&
2240 *end != L' ' && *end != L',')
2241 end++;
2242 length = end - start;
2243 for (flag = fileflags; flag->wname != NULL; flag++) {
2244 size_t flag_length = wcslen(flag->wname);
2245 if (length == flag_length
2246 && wmemcmp(start, flag->wname, length) == 0) {
2247 /* Matched "noXXXX", so reverse the sense. */
2248 clear |= flag->set;
2249 set |= flag->clear;
2250 break;
2251 } else if (length == flag_length - 2
2252 && wmemcmp(start, flag->wname + 2, length) == 0) {
2253 /* Matched "XXXX", so don't reverse. */
2254 set |= flag->set;
2255 clear |= flag->clear;
2256 break;
2257 }
2258 }
2259 /* Ignore unknown flag names. */
2260 if (flag->wname == NULL && failed == NULL)
2261 failed = start;
2262
2263 /* Find start of next token. */
2264 start = end;
2265 while (*start == L'\t' || *start == L' ' || *start == L',')
2266 start++;
2267
2268 }
2269
2270 if (setp)
2271 *setp = set;
2272 if (clrp)
2273 *clrp = clear;
2274
2275 /* Return location of first failure. */
2276 return (failed);
2277 }
2278
2279
2280 #ifdef TEST
2281 #include <stdio.h>
2282 int
main(int argc,char ** argv)2283 main(int argc, char **argv)
2284 {
2285 struct archive_entry *entry = archive_entry_new();
2286 unsigned long set, clear;
2287 const wchar_t *remainder;
2288
2289 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
2290 archive_entry_fflags(entry, &set, &clear);
2291
2292 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
2293
2294 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
2295 return (0);
2296 }
2297 #endif
2298