1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry.c,v 1.55 2008/12/23 05:01:43 kientzle Exp $");
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 #ifdef MAJOR_IN_MKDEV
36 #include <sys/mkdev.h>
37 #else
38 #ifdef MAJOR_IN_SYSMACROS
39 #include <sys/sysmacros.h>
40 #endif
41 #endif
42 #ifdef HAVE_LIMITS_H
43 #include <limits.h>
44 #endif
45 #ifdef HAVE_LINUX_FS_H
46 #include <linux/fs.h> /* for Linux file flags */
47 #endif
48 /*
49 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
50 * As the include guards don't agree, the order of include is important.
51 */
52 #ifdef HAVE_LINUX_EXT2_FS_H
53 #include <linux/ext2_fs.h> /* for Linux file flags */
54 #endif
55 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
56 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
57 #endif
58 #include <stddef.h>
59 #include <stdio.h>
60 #ifdef HAVE_STDLIB_H
61 #include <stdlib.h>
62 #endif
63 #ifdef HAVE_STRING_H
64 #include <string.h>
65 #endif
66 #ifdef HAVE_WCHAR_H
67 #include <wchar.h>
68 #endif
69
70 #include "archive.h"
71 #include "archive_entry.h"
72 #include "archive_private.h"
73 #include "archive_entry_private.h"
74
75 #undef max
76 #define max(a, b) ((a)>(b)?(a):(b))
77
78 /* Play games to come up with a suitable makedev() definition. */
79 #ifdef __QNXNTO__
80 /* QNX. <sigh> */
81 #include <sys/netmgr.h>
82 #define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
83 #elif defined makedev
84 /* There's a "makedev" macro. */
85 #define ae_makedev(maj, min) makedev((maj), (min))
86 #elif defined mkdev || ((defined _WIN32 || defined __WIN32__) && !defined(__CYGWIN__))
87 /* Windows. <sigh> */
88 #define ae_makedev(maj, min) mkdev((maj), (min))
89 #else
90 /* There's a "makedev" function. */
91 #define ae_makedev(maj, min) makedev((maj), (min))
92 #endif
93
94 static void aes_clean(struct aes *);
95 static void aes_copy(struct aes *dest, struct aes *src);
96 static const char * aes_get_mbs(struct aes *);
97 static const wchar_t * aes_get_wcs(struct aes *);
98 static int aes_set_mbs(struct aes *, const char *mbs);
99 static int aes_copy_mbs(struct aes *, const char *mbs);
100 /* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
101 static int aes_copy_wcs(struct aes *, const wchar_t *wcs);
102 static int aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
103
104 static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
105 static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
106 unsigned long *setp, unsigned long *clrp);
107 static const char *ae_strtofflags(const char *stringp,
108 unsigned long *setp, unsigned long *clrp);
109 static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
110 const wchar_t *wname, int perm, int id);
111 static void append_id_w(wchar_t **wp, int id);
112
113 static int acl_special(struct archive_entry *entry,
114 int type, int permset, int tag);
115 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
116 int type, int permset, int tag, int id);
117 static int isint_w(const wchar_t *start, const wchar_t *end, int *result);
118 static void next_field_w(const wchar_t **wp, const wchar_t **start,
119 const wchar_t **end, wchar_t *sep);
120 static int prefix_w(const wchar_t *start, const wchar_t *end,
121 const wchar_t *test);
122 static void
123 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
124 int permset, int tag, int id, const wchar_t *name, size_t);
125
126
127 #ifndef HAVE_WCSCPY
wcscpy(wchar_t * s1,const wchar_t * s2)128 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
129 {
130 wchar_t *dest = s1;
131 while ((*s1 = *s2) != L'\0')
132 ++s1, ++s2;
133 return dest;
134 }
135 #endif
136 #ifndef HAVE_WCSLEN
wcslen(const wchar_t * s)137 static size_t wcslen(const wchar_t *s)
138 {
139 const wchar_t *p = s;
140 while (*p != L'\0')
141 ++p;
142 return p - s;
143 }
144 #endif
145 #ifndef HAVE_WMEMCMP
146 /* Good enough for simple equality testing, but not for sorting. */
147 #define wmemcmp(a,b,i) memcmp((a), (b), (i) * sizeof(wchar_t))
148 #endif
149 #ifndef HAVE_WMEMCPY
150 #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
151 #endif
152
153 static void
aes_clean(struct aes * aes)154 aes_clean(struct aes *aes)
155 {
156 if (aes->aes_wcs) {
157 free((wchar_t *)(uintptr_t)aes->aes_wcs);
158 aes->aes_wcs = NULL;
159 }
160 archive_string_free(&(aes->aes_mbs));
161 archive_string_free(&(aes->aes_utf8));
162 aes->aes_set = 0;
163 }
164
165 static void
aes_copy(struct aes * dest,struct aes * src)166 aes_copy(struct aes *dest, struct aes *src)
167 {
168 wchar_t *wp;
169
170 dest->aes_set = src->aes_set;
171 archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
172 archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
173
174 if (src->aes_wcs != NULL) {
175 wp = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
176 * sizeof(wchar_t));
177 if (wp == NULL)
178 __archive_errx(1, "No memory for aes_copy()");
179 wcscpy(wp, src->aes_wcs);
180 dest->aes_wcs = wp;
181 }
182 }
183
184 static const char *
aes_get_utf8(struct aes * aes)185 aes_get_utf8(struct aes *aes)
186 {
187 if (aes->aes_set & AES_SET_UTF8)
188 return (aes->aes_utf8.s);
189 if ((aes->aes_set & AES_SET_WCS)
190 && archive_strappend_w_utf8(&(aes->aes_utf8), aes->aes_wcs) != NULL) {
191 aes->aes_set |= AES_SET_UTF8;
192 return (aes->aes_utf8.s);
193 }
194 return (NULL);
195 }
196
197 static const char *
aes_get_mbs(struct aes * aes)198 aes_get_mbs(struct aes *aes)
199 {
200 /* If we already have an MBS form, return that immediately. */
201 if (aes->aes_set & AES_SET_MBS)
202 return (aes->aes_mbs.s);
203 /* If there's a WCS form, try converting with the native locale. */
204 if ((aes->aes_set & AES_SET_WCS)
205 && archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) != NULL) {
206 aes->aes_set |= AES_SET_MBS;
207 return (aes->aes_mbs.s);
208 }
209 /* We'll use UTF-8 for MBS if all else fails. */
210 return (aes_get_utf8(aes));
211 }
212
213 static const wchar_t *
aes_get_wcs(struct aes * aes)214 aes_get_wcs(struct aes *aes)
215 {
216 wchar_t *w;
217 int r;
218
219 /* Return WCS form if we already have it. */
220 if (aes->aes_set & AES_SET_WCS)
221 return (aes->aes_wcs);
222
223 if (aes->aes_set & AES_SET_MBS) {
224 /* Try converting MBS to WCS using native locale. */
225 /*
226 * No single byte will be more than one wide character,
227 * so this length estimate will always be big enough.
228 */
229 size_t wcs_length = aes->aes_mbs.length;
230
231 w = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
232 if (w == NULL)
233 __archive_errx(1, "No memory for aes_get_wcs()");
234 r = mbstowcs(w, aes->aes_mbs.s, wcs_length);
235 if (r > 0) {
236 w[r] = 0;
237 aes->aes_set |= AES_SET_WCS;
238 return (aes->aes_wcs = w);
239 }
240 free(w);
241 }
242
243 if (aes->aes_set & AES_SET_UTF8) {
244 /* Try converting UTF8 to WCS. */
245 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
246 if (aes->aes_wcs != NULL)
247 aes->aes_set |= AES_SET_WCS;
248 return (aes->aes_wcs);
249 }
250 return (NULL);
251 }
252
253 static int
aes_set_mbs(struct aes * aes,const char * mbs)254 aes_set_mbs(struct aes *aes, const char *mbs)
255 {
256 return (aes_copy_mbs(aes, mbs));
257 }
258
259 static int
aes_copy_mbs(struct aes * aes,const char * mbs)260 aes_copy_mbs(struct aes *aes, const char *mbs)
261 {
262 if (mbs == NULL) {
263 aes->aes_set = 0;
264 return (0);
265 }
266 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
267 archive_strcpy(&(aes->aes_mbs), mbs);
268 archive_string_empty(&(aes->aes_utf8));
269 if (aes->aes_wcs) {
270 free((wchar_t *)(uintptr_t)aes->aes_wcs);
271 aes->aes_wcs = NULL;
272 }
273 return (0);
274 }
275
276 /*
277 * The 'update' form tries to proactively update all forms of
278 * this string (WCS and MBS) and returns an error if any of
279 * them fail. This is used by the 'pax' handler, for instance,
280 * to detect and report character-conversion failures early while
281 * still allowing clients to get potentially useful values from
282 * the more tolerant lazy conversions. (get_mbs and get_wcs will
283 * strive to give the user something useful, so you can get hopefully
284 * usable values even if some of the character conversions are failing.)
285 */
286 static int
aes_update_utf8(struct aes * aes,const char * utf8)287 aes_update_utf8(struct aes *aes, const char *utf8)
288 {
289 if (utf8 == NULL) {
290 aes->aes_set = 0;
291 return (1); /* Succeeded in clearing everything. */
292 }
293
294 /* Save the UTF8 string. */
295 archive_strcpy(&(aes->aes_utf8), utf8);
296
297 /* Empty the mbs and wcs strings. */
298 archive_string_empty(&(aes->aes_mbs));
299 if (aes->aes_wcs) {
300 free((wchar_t *)(uintptr_t)aes->aes_wcs);
301 aes->aes_wcs = NULL;
302 }
303
304 aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
305
306 /* TODO: We should just do a direct UTF-8 to MBS conversion
307 * here. That would be faster, use less space, and give the
308 * same information. (If a UTF-8 to MBS conversion succeeds,
309 * then UTF-8->WCS and Unicode->MBS conversions will both
310 * succeed.) */
311
312 /* Try converting UTF8 to WCS, return false on failure. */
313 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
314 if (aes->aes_wcs == NULL)
315 return (0);
316 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS; /* Both UTF8 and WCS set. */
317
318 /* Try converting WCS to MBS, return false on failure. */
319 if (archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) == NULL)
320 return (0);
321 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
322
323 /* All conversions succeeded. */
324 return (1);
325 }
326
327 static int
aes_copy_wcs(struct aes * aes,const wchar_t * wcs)328 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
329 {
330 return aes_copy_wcs_len(aes, wcs, wcs == NULL ? 0 : wcslen(wcs));
331 }
332
333 static int
aes_copy_wcs_len(struct aes * aes,const wchar_t * wcs,size_t len)334 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
335 {
336 wchar_t *w;
337
338 if (wcs == NULL) {
339 aes->aes_set = 0;
340 return (0);
341 }
342 aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
343 archive_string_empty(&(aes->aes_mbs));
344 archive_string_empty(&(aes->aes_utf8));
345 if (aes->aes_wcs) {
346 free((wchar_t *)(uintptr_t)aes->aes_wcs);
347 aes->aes_wcs = NULL;
348 }
349 w = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
350 if (w == NULL)
351 __archive_errx(1, "No memory for aes_copy_wcs()");
352 wmemcpy(w, wcs, len);
353 w[len] = L'\0';
354 aes->aes_wcs = w;
355 return (0);
356 }
357
358 /****************************************************************************
359 *
360 * Public Interface
361 *
362 ****************************************************************************/
363
364 struct archive_entry *
archive_entry_clear(struct archive_entry * entry)365 archive_entry_clear(struct archive_entry *entry)
366 {
367 if (entry == NULL)
368 return (NULL);
369 aes_clean(&entry->ae_fflags_text);
370 aes_clean(&entry->ae_gname);
371 aes_clean(&entry->ae_hardlink);
372 aes_clean(&entry->ae_pathname);
373 aes_clean(&entry->ae_sourcepath);
374 aes_clean(&entry->ae_symlink);
375 aes_clean(&entry->ae_uname);
376 archive_entry_acl_clear(entry);
377 archive_entry_xattr_clear(entry);
378 free(entry->stat);
379 memset(entry, 0, sizeof(*entry));
380 return entry;
381 }
382
383 struct archive_entry *
archive_entry_clone(struct archive_entry * entry)384 archive_entry_clone(struct archive_entry *entry)
385 {
386 struct archive_entry *entry2;
387 struct ae_acl *ap, *ap2;
388 struct ae_xattr *xp;
389
390 /* Allocate new structure and copy over all of the fields. */
391 entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
392 if (entry2 == NULL)
393 return (NULL);
394 memset(entry2, 0, sizeof(*entry2));
395 entry2->ae_stat = entry->ae_stat;
396 entry2->ae_fflags_set = entry->ae_fflags_set;
397 entry2->ae_fflags_clear = entry->ae_fflags_clear;
398
399 aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
400 aes_copy(&entry2->ae_gname, &entry->ae_gname);
401 aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
402 aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
403 aes_copy(&entry2->ae_sourcepath, &entry->ae_sourcepath);
404 aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
405 entry2->ae_set = entry->ae_set;
406 aes_copy(&entry2->ae_uname, &entry->ae_uname);
407
408 /* Copy ACL data over. */
409 ap = entry->acl_head;
410 while (ap != NULL) {
411 ap2 = acl_new_entry(entry2,
412 ap->type, ap->permset, ap->tag, ap->id);
413 if (ap2 != NULL)
414 aes_copy(&ap2->name, &ap->name);
415 ap = ap->next;
416 }
417
418 /* Copy xattr data over. */
419 xp = entry->xattr_head;
420 while (xp != NULL) {
421 archive_entry_xattr_add_entry(entry2,
422 xp->name, xp->value, xp->size);
423 xp = xp->next;
424 }
425
426 return (entry2);
427 }
428
429 void
archive_entry_free(struct archive_entry * entry)430 archive_entry_free(struct archive_entry *entry)
431 {
432 archive_entry_clear(entry);
433 free(entry);
434 }
435
436 struct archive_entry *
archive_entry_new(void)437 archive_entry_new(void)
438 {
439 struct archive_entry *entry;
440
441 entry = (struct archive_entry *)malloc(sizeof(*entry));
442 if (entry == NULL)
443 return (NULL);
444 memset(entry, 0, sizeof(*entry));
445 return (entry);
446 }
447
448 /*
449 * Functions for reading fields from an archive_entry.
450 */
451
452 time_t
archive_entry_atime(struct archive_entry * entry)453 archive_entry_atime(struct archive_entry *entry)
454 {
455 return (entry->ae_stat.aest_atime);
456 }
457
458 long
archive_entry_atime_nsec(struct archive_entry * entry)459 archive_entry_atime_nsec(struct archive_entry *entry)
460 {
461 return (entry->ae_stat.aest_atime_nsec);
462 }
463
464 int
archive_entry_atime_is_set(struct archive_entry * entry)465 archive_entry_atime_is_set(struct archive_entry *entry)
466 {
467 return (entry->ae_set & AE_SET_ATIME);
468 }
469
470 time_t
archive_entry_birthtime(struct archive_entry * entry)471 archive_entry_birthtime(struct archive_entry *entry)
472 {
473 return (entry->ae_stat.aest_birthtime);
474 }
475
476 long
archive_entry_birthtime_nsec(struct archive_entry * entry)477 archive_entry_birthtime_nsec(struct archive_entry *entry)
478 {
479 return (entry->ae_stat.aest_birthtime_nsec);
480 }
481
482 int
archive_entry_birthtime_is_set(struct archive_entry * entry)483 archive_entry_birthtime_is_set(struct archive_entry *entry)
484 {
485 return (entry->ae_set & AE_SET_BIRTHTIME);
486 }
487
488 time_t
archive_entry_ctime(struct archive_entry * entry)489 archive_entry_ctime(struct archive_entry *entry)
490 {
491 return (entry->ae_stat.aest_ctime);
492 }
493
494 int
archive_entry_ctime_is_set(struct archive_entry * entry)495 archive_entry_ctime_is_set(struct archive_entry *entry)
496 {
497 return (entry->ae_set & AE_SET_CTIME);
498 }
499
500 long
archive_entry_ctime_nsec(struct archive_entry * entry)501 archive_entry_ctime_nsec(struct archive_entry *entry)
502 {
503 return (entry->ae_stat.aest_ctime_nsec);
504 }
505
506 dev_t
archive_entry_dev(struct archive_entry * entry)507 archive_entry_dev(struct archive_entry *entry)
508 {
509 if (entry->ae_stat.aest_dev_is_broken_down)
510 return ae_makedev(entry->ae_stat.aest_devmajor,
511 entry->ae_stat.aest_devminor);
512 else
513 return (entry->ae_stat.aest_dev);
514 }
515
516 dev_t
archive_entry_devmajor(struct archive_entry * entry)517 archive_entry_devmajor(struct archive_entry *entry)
518 {
519 if (entry->ae_stat.aest_dev_is_broken_down)
520 return (entry->ae_stat.aest_devmajor);
521 else
522 return major(entry->ae_stat.aest_dev);
523 }
524
525 dev_t
archive_entry_devminor(struct archive_entry * entry)526 archive_entry_devminor(struct archive_entry *entry)
527 {
528 if (entry->ae_stat.aest_dev_is_broken_down)
529 return (entry->ae_stat.aest_devminor);
530 else
531 return minor(entry->ae_stat.aest_dev);
532 }
533
534 mode_t
archive_entry_filetype(struct archive_entry * entry)535 archive_entry_filetype(struct archive_entry *entry)
536 {
537 return (AE_IFMT & entry->ae_stat.aest_mode);
538 }
539
540 void
archive_entry_fflags(struct archive_entry * entry,unsigned long * set,unsigned long * clear)541 archive_entry_fflags(struct archive_entry *entry,
542 unsigned long *set, unsigned long *clear)
543 {
544 *set = entry->ae_fflags_set;
545 *clear = entry->ae_fflags_clear;
546 }
547
548 /*
549 * Note: if text was provided, this just returns that text. If you
550 * really need the text to be rebuilt in a canonical form, set the
551 * text, ask for the bitmaps, then set the bitmaps. (Setting the
552 * bitmaps clears any stored text.) This design is deliberate: if
553 * we're editing archives, we don't want to discard flags just because
554 * they aren't supported on the current system. The bitmap<->text
555 * conversions are platform-specific (see below).
556 */
557 const char *
archive_entry_fflags_text(struct archive_entry * entry)558 archive_entry_fflags_text(struct archive_entry *entry)
559 {
560 const char *f;
561 char *p;
562
563 f = aes_get_mbs(&entry->ae_fflags_text);
564 if (f != NULL)
565 return (f);
566
567 if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
568 return (NULL);
569
570 p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
571 if (p == NULL)
572 return (NULL);
573
574 aes_copy_mbs(&entry->ae_fflags_text, p);
575 free(p);
576 f = aes_get_mbs(&entry->ae_fflags_text);
577 return (f);
578 }
579
580 gid_t
archive_entry_gid(struct archive_entry * entry)581 archive_entry_gid(struct archive_entry *entry)
582 {
583 return (entry->ae_stat.aest_gid);
584 }
585
586 const char *
archive_entry_gname(struct archive_entry * entry)587 archive_entry_gname(struct archive_entry *entry)
588 {
589 return (aes_get_mbs(&entry->ae_gname));
590 }
591
592 const wchar_t *
archive_entry_gname_w(struct archive_entry * entry)593 archive_entry_gname_w(struct archive_entry *entry)
594 {
595 return (aes_get_wcs(&entry->ae_gname));
596 }
597
598 const char *
archive_entry_hardlink(struct archive_entry * entry)599 archive_entry_hardlink(struct archive_entry *entry)
600 {
601 if (entry->ae_set & AE_SET_HARDLINK)
602 return (aes_get_mbs(&entry->ae_hardlink));
603 return (NULL);
604 }
605
606 const wchar_t *
archive_entry_hardlink_w(struct archive_entry * entry)607 archive_entry_hardlink_w(struct archive_entry *entry)
608 {
609 if (entry->ae_set & AE_SET_HARDLINK)
610 return (aes_get_wcs(&entry->ae_hardlink));
611 return (NULL);
612 }
613
614 ino_t
archive_entry_ino(struct archive_entry * entry)615 archive_entry_ino(struct archive_entry *entry)
616 {
617 return (entry->ae_stat.aest_ino);
618 }
619
620 mode_t
archive_entry_mode(struct archive_entry * entry)621 archive_entry_mode(struct archive_entry *entry)
622 {
623 return (entry->ae_stat.aest_mode);
624 }
625
626 time_t
archive_entry_mtime(struct archive_entry * entry)627 archive_entry_mtime(struct archive_entry *entry)
628 {
629 return (entry->ae_stat.aest_mtime);
630 }
631
632 long
archive_entry_mtime_nsec(struct archive_entry * entry)633 archive_entry_mtime_nsec(struct archive_entry *entry)
634 {
635 return (entry->ae_stat.aest_mtime_nsec);
636 }
637
638 int
archive_entry_mtime_is_set(struct archive_entry * entry)639 archive_entry_mtime_is_set(struct archive_entry *entry)
640 {
641 return (entry->ae_set & AE_SET_MTIME);
642 }
643
644 unsigned int
archive_entry_nlink(struct archive_entry * entry)645 archive_entry_nlink(struct archive_entry *entry)
646 {
647 return (entry->ae_stat.aest_nlink);
648 }
649
650 const char *
archive_entry_pathname(struct archive_entry * entry)651 archive_entry_pathname(struct archive_entry *entry)
652 {
653 return (aes_get_mbs(&entry->ae_pathname));
654 }
655
656 const wchar_t *
archive_entry_pathname_w(struct archive_entry * entry)657 archive_entry_pathname_w(struct archive_entry *entry)
658 {
659 return (aes_get_wcs(&entry->ae_pathname));
660 }
661
662 dev_t
archive_entry_rdev(struct archive_entry * entry)663 archive_entry_rdev(struct archive_entry *entry)
664 {
665 if (entry->ae_stat.aest_rdev_is_broken_down)
666 return ae_makedev(entry->ae_stat.aest_rdevmajor,
667 entry->ae_stat.aest_rdevminor);
668 else
669 return (entry->ae_stat.aest_rdev);
670 }
671
672 dev_t
archive_entry_rdevmajor(struct archive_entry * entry)673 archive_entry_rdevmajor(struct archive_entry *entry)
674 {
675 if (entry->ae_stat.aest_rdev_is_broken_down)
676 return (entry->ae_stat.aest_rdevmajor);
677 else
678 return major(entry->ae_stat.aest_rdev);
679 }
680
681 dev_t
archive_entry_rdevminor(struct archive_entry * entry)682 archive_entry_rdevminor(struct archive_entry *entry)
683 {
684 if (entry->ae_stat.aest_rdev_is_broken_down)
685 return (entry->ae_stat.aest_rdevminor);
686 else
687 return minor(entry->ae_stat.aest_rdev);
688 }
689
690 int64_t
archive_entry_size(struct archive_entry * entry)691 archive_entry_size(struct archive_entry *entry)
692 {
693 return (entry->ae_stat.aest_size);
694 }
695
696 int
archive_entry_size_is_set(struct archive_entry * entry)697 archive_entry_size_is_set(struct archive_entry *entry)
698 {
699 return (entry->ae_set & AE_SET_SIZE);
700 }
701
702 const char *
archive_entry_sourcepath(struct archive_entry * entry)703 archive_entry_sourcepath(struct archive_entry *entry)
704 {
705 return (aes_get_mbs(&entry->ae_sourcepath));
706 }
707
708 const char *
archive_entry_symlink(struct archive_entry * entry)709 archive_entry_symlink(struct archive_entry *entry)
710 {
711 if (entry->ae_set & AE_SET_SYMLINK)
712 return (aes_get_mbs(&entry->ae_symlink));
713 return (NULL);
714 }
715
716 const wchar_t *
archive_entry_symlink_w(struct archive_entry * entry)717 archive_entry_symlink_w(struct archive_entry *entry)
718 {
719 if (entry->ae_set & AE_SET_SYMLINK)
720 return (aes_get_wcs(&entry->ae_symlink));
721 return (NULL);
722 }
723
724 uid_t
archive_entry_uid(struct archive_entry * entry)725 archive_entry_uid(struct archive_entry *entry)
726 {
727 return (entry->ae_stat.aest_uid);
728 }
729
730 const char *
archive_entry_uname(struct archive_entry * entry)731 archive_entry_uname(struct archive_entry *entry)
732 {
733 return (aes_get_mbs(&entry->ae_uname));
734 }
735
736 const wchar_t *
archive_entry_uname_w(struct archive_entry * entry)737 archive_entry_uname_w(struct archive_entry *entry)
738 {
739 return (aes_get_wcs(&entry->ae_uname));
740 }
741
742 /*
743 * Functions to set archive_entry properties.
744 */
745
746 void
archive_entry_set_filetype(struct archive_entry * entry,unsigned int type)747 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
748 {
749 entry->stat_valid = 0;
750 entry->ae_stat.aest_mode &= ~AE_IFMT;
751 entry->ae_stat.aest_mode |= AE_IFMT & type;
752 }
753
754 void
archive_entry_set_fflags(struct archive_entry * entry,unsigned long set,unsigned long clear)755 archive_entry_set_fflags(struct archive_entry *entry,
756 unsigned long set, unsigned long clear)
757 {
758 aes_clean(&entry->ae_fflags_text);
759 entry->ae_fflags_set = set;
760 entry->ae_fflags_clear = clear;
761 }
762
763 const char *
archive_entry_copy_fflags_text(struct archive_entry * entry,const char * flags)764 archive_entry_copy_fflags_text(struct archive_entry *entry,
765 const char *flags)
766 {
767 aes_copy_mbs(&entry->ae_fflags_text, flags);
768 return (ae_strtofflags(flags,
769 &entry->ae_fflags_set, &entry->ae_fflags_clear));
770 }
771
772 const wchar_t *
archive_entry_copy_fflags_text_w(struct archive_entry * entry,const wchar_t * flags)773 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
774 const wchar_t *flags)
775 {
776 aes_copy_wcs(&entry->ae_fflags_text, flags);
777 return (ae_wcstofflags(flags,
778 &entry->ae_fflags_set, &entry->ae_fflags_clear));
779 }
780
781 void
archive_entry_set_gid(struct archive_entry * entry,gid_t g)782 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
783 {
784 entry->stat_valid = 0;
785 entry->ae_stat.aest_gid = g;
786 }
787
788 void
archive_entry_set_gname(struct archive_entry * entry,const char * name)789 archive_entry_set_gname(struct archive_entry *entry, const char *name)
790 {
791 aes_set_mbs(&entry->ae_gname, name);
792 }
793
794 void
archive_entry_copy_gname(struct archive_entry * entry,const char * name)795 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
796 {
797 aes_copy_mbs(&entry->ae_gname, name);
798 }
799
800 void
archive_entry_copy_gname_w(struct archive_entry * entry,const wchar_t * name)801 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
802 {
803 aes_copy_wcs(&entry->ae_gname, name);
804 }
805
806 int
archive_entry_update_gname_utf8(struct archive_entry * entry,const char * name)807 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
808 {
809 return (aes_update_utf8(&entry->ae_gname, name));
810 }
811
812 void
archive_entry_set_ino(struct archive_entry * entry,unsigned long ino)813 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
814 {
815 entry->stat_valid = 0;
816 entry->ae_stat.aest_ino = ino;
817 }
818
819 void
archive_entry_set_hardlink(struct archive_entry * entry,const char * target)820 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
821 {
822 aes_set_mbs(&entry->ae_hardlink, target);
823 if (target != NULL)
824 entry->ae_set |= AE_SET_HARDLINK;
825 else
826 entry->ae_set &= ~AE_SET_HARDLINK;
827 }
828
829 void
archive_entry_copy_hardlink(struct archive_entry * entry,const char * target)830 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
831 {
832 aes_copy_mbs(&entry->ae_hardlink, target);
833 if (target != NULL)
834 entry->ae_set |= AE_SET_HARDLINK;
835 else
836 entry->ae_set &= ~AE_SET_HARDLINK;
837 }
838
839 void
archive_entry_copy_hardlink_w(struct archive_entry * entry,const wchar_t * target)840 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
841 {
842 aes_copy_wcs(&entry->ae_hardlink, target);
843 if (target != NULL)
844 entry->ae_set |= AE_SET_HARDLINK;
845 else
846 entry->ae_set &= ~AE_SET_HARDLINK;
847 }
848
849 void
archive_entry_set_atime(struct archive_entry * entry,time_t t,long ns)850 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
851 {
852 entry->stat_valid = 0;
853 entry->ae_set |= AE_SET_ATIME;
854 entry->ae_stat.aest_atime = t;
855 entry->ae_stat.aest_atime_nsec = ns;
856 }
857
858 void
archive_entry_unset_atime(struct archive_entry * entry)859 archive_entry_unset_atime(struct archive_entry *entry)
860 {
861 archive_entry_set_atime(entry, 0, 0);
862 entry->ae_set &= ~AE_SET_ATIME;
863 }
864
865 void
archive_entry_set_birthtime(struct archive_entry * entry,time_t m,long ns)866 archive_entry_set_birthtime(struct archive_entry *entry, time_t m, long ns)
867 {
868 entry->stat_valid = 0;
869 entry->ae_set |= AE_SET_BIRTHTIME;
870 entry->ae_stat.aest_birthtime = m;
871 entry->ae_stat.aest_birthtime_nsec = ns;
872 }
873
874 void
archive_entry_unset_birthtime(struct archive_entry * entry)875 archive_entry_unset_birthtime(struct archive_entry *entry)
876 {
877 archive_entry_set_birthtime(entry, 0, 0);
878 entry->ae_set &= ~AE_SET_BIRTHTIME;
879 }
880
881 void
archive_entry_set_ctime(struct archive_entry * entry,time_t t,long ns)882 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
883 {
884 entry->stat_valid = 0;
885 entry->ae_set |= AE_SET_CTIME;
886 entry->ae_stat.aest_ctime = t;
887 entry->ae_stat.aest_ctime_nsec = ns;
888 }
889
890 void
archive_entry_unset_ctime(struct archive_entry * entry)891 archive_entry_unset_ctime(struct archive_entry *entry)
892 {
893 archive_entry_set_ctime(entry, 0, 0);
894 entry->ae_set &= ~AE_SET_CTIME;
895 }
896
897 void
archive_entry_set_dev(struct archive_entry * entry,dev_t d)898 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
899 {
900 entry->stat_valid = 0;
901 entry->ae_stat.aest_dev_is_broken_down = 0;
902 entry->ae_stat.aest_dev = d;
903 }
904
905 void
archive_entry_set_devmajor(struct archive_entry * entry,dev_t m)906 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
907 {
908 entry->stat_valid = 0;
909 entry->ae_stat.aest_dev_is_broken_down = 1;
910 entry->ae_stat.aest_devmajor = m;
911 }
912
913 void
archive_entry_set_devminor(struct archive_entry * entry,dev_t m)914 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
915 {
916 entry->stat_valid = 0;
917 entry->ae_stat.aest_dev_is_broken_down = 1;
918 entry->ae_stat.aest_devminor = m;
919 }
920
921 /* Set symlink if symlink is already set, else set hardlink. */
922 void
archive_entry_set_link(struct archive_entry * entry,const char * target)923 archive_entry_set_link(struct archive_entry *entry, const char *target)
924 {
925 if (entry->ae_set & AE_SET_SYMLINK)
926 aes_set_mbs(&entry->ae_symlink, target);
927 else
928 aes_set_mbs(&entry->ae_hardlink, target);
929 }
930
931 /* Set symlink if symlink is already set, else set hardlink. */
932 void
archive_entry_copy_link(struct archive_entry * entry,const char * target)933 archive_entry_copy_link(struct archive_entry *entry, const char *target)
934 {
935 if (entry->ae_set & AE_SET_SYMLINK)
936 aes_copy_mbs(&entry->ae_symlink, target);
937 else
938 aes_copy_mbs(&entry->ae_hardlink, target);
939 }
940
941 /* Set symlink if symlink is already set, else set hardlink. */
942 void
archive_entry_copy_link_w(struct archive_entry * entry,const wchar_t * target)943 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
944 {
945 if (entry->ae_set & AE_SET_SYMLINK)
946 aes_copy_wcs(&entry->ae_symlink, target);
947 else
948 aes_copy_wcs(&entry->ae_hardlink, target);
949 }
950
951 int
archive_entry_update_link_utf8(struct archive_entry * entry,const char * target)952 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
953 {
954 if (entry->ae_set & AE_SET_SYMLINK)
955 return (aes_update_utf8(&entry->ae_symlink, target));
956 else
957 return (aes_update_utf8(&entry->ae_hardlink, target));
958 }
959
960 void
archive_entry_set_mode(struct archive_entry * entry,mode_t m)961 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
962 {
963 entry->stat_valid = 0;
964 entry->ae_stat.aest_mode = m;
965 }
966
967 void
archive_entry_set_mtime(struct archive_entry * entry,time_t m,long ns)968 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
969 {
970 entry->stat_valid = 0;
971 entry->ae_set |= AE_SET_MTIME;
972 entry->ae_stat.aest_mtime = m;
973 entry->ae_stat.aest_mtime_nsec = ns;
974 }
975
976 void
archive_entry_unset_mtime(struct archive_entry * entry)977 archive_entry_unset_mtime(struct archive_entry *entry)
978 {
979 archive_entry_set_mtime(entry, 0, 0);
980 entry->ae_set &= ~AE_SET_MTIME;
981 }
982
983 void
archive_entry_set_nlink(struct archive_entry * entry,unsigned int nlink)984 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
985 {
986 entry->stat_valid = 0;
987 entry->ae_stat.aest_nlink = nlink;
988 }
989
990 void
archive_entry_set_pathname(struct archive_entry * entry,const char * name)991 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
992 {
993 aes_set_mbs(&entry->ae_pathname, name);
994 }
995
996 void
archive_entry_copy_pathname(struct archive_entry * entry,const char * name)997 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
998 {
999 aes_copy_mbs(&entry->ae_pathname, name);
1000 }
1001
1002 void
archive_entry_copy_pathname_w(struct archive_entry * entry,const wchar_t * name)1003 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
1004 {
1005 aes_copy_wcs(&entry->ae_pathname, name);
1006 }
1007
1008 int
archive_entry_update_pathname_utf8(struct archive_entry * entry,const char * name)1009 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
1010 {
1011 return (aes_update_utf8(&entry->ae_pathname, name));
1012 }
1013
1014 void
archive_entry_set_perm(struct archive_entry * entry,mode_t p)1015 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
1016 {
1017 entry->stat_valid = 0;
1018 entry->ae_stat.aest_mode &= AE_IFMT;
1019 entry->ae_stat.aest_mode |= ~AE_IFMT & p;
1020 }
1021
1022 void
archive_entry_set_rdev(struct archive_entry * entry,dev_t m)1023 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
1024 {
1025 entry->stat_valid = 0;
1026 entry->ae_stat.aest_rdev = m;
1027 entry->ae_stat.aest_rdev_is_broken_down = 0;
1028 }
1029
1030 void
archive_entry_set_rdevmajor(struct archive_entry * entry,dev_t m)1031 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
1032 {
1033 entry->stat_valid = 0;
1034 entry->ae_stat.aest_rdev_is_broken_down = 1;
1035 entry->ae_stat.aest_rdevmajor = m;
1036 }
1037
1038 void
archive_entry_set_rdevminor(struct archive_entry * entry,dev_t m)1039 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
1040 {
1041 entry->stat_valid = 0;
1042 entry->ae_stat.aest_rdev_is_broken_down = 1;
1043 entry->ae_stat.aest_rdevminor = m;
1044 }
1045
1046 void
archive_entry_set_size(struct archive_entry * entry,int64_t s)1047 archive_entry_set_size(struct archive_entry *entry, int64_t s)
1048 {
1049 entry->stat_valid = 0;
1050 entry->ae_stat.aest_size = s;
1051 entry->ae_set |= AE_SET_SIZE;
1052 }
1053
1054 void
archive_entry_unset_size(struct archive_entry * entry)1055 archive_entry_unset_size(struct archive_entry *entry)
1056 {
1057 archive_entry_set_size(entry, 0);
1058 entry->ae_set &= ~AE_SET_SIZE;
1059 }
1060
1061 void
archive_entry_copy_sourcepath(struct archive_entry * entry,const char * path)1062 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1063 {
1064 aes_set_mbs(&entry->ae_sourcepath, path);
1065 }
1066
1067 void
archive_entry_set_symlink(struct archive_entry * entry,const char * linkname)1068 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1069 {
1070 aes_set_mbs(&entry->ae_symlink, linkname);
1071 if (linkname != NULL)
1072 entry->ae_set |= AE_SET_SYMLINK;
1073 else
1074 entry->ae_set &= ~AE_SET_SYMLINK;
1075 }
1076
1077 void
archive_entry_copy_symlink(struct archive_entry * entry,const char * linkname)1078 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1079 {
1080 aes_copy_mbs(&entry->ae_symlink, linkname);
1081 if (linkname != NULL)
1082 entry->ae_set |= AE_SET_SYMLINK;
1083 else
1084 entry->ae_set &= ~AE_SET_SYMLINK;
1085 }
1086
1087 void
archive_entry_copy_symlink_w(struct archive_entry * entry,const wchar_t * linkname)1088 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1089 {
1090 aes_copy_wcs(&entry->ae_symlink, linkname);
1091 if (linkname != NULL)
1092 entry->ae_set |= AE_SET_SYMLINK;
1093 else
1094 entry->ae_set &= ~AE_SET_SYMLINK;
1095 }
1096
1097 void
archive_entry_set_uid(struct archive_entry * entry,uid_t u)1098 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
1099 {
1100 entry->stat_valid = 0;
1101 entry->ae_stat.aest_uid = u;
1102 }
1103
1104 void
archive_entry_set_uname(struct archive_entry * entry,const char * name)1105 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1106 {
1107 aes_set_mbs(&entry->ae_uname, name);
1108 }
1109
1110 void
archive_entry_copy_uname(struct archive_entry * entry,const char * name)1111 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1112 {
1113 aes_copy_mbs(&entry->ae_uname, name);
1114 }
1115
1116 void
archive_entry_copy_uname_w(struct archive_entry * entry,const wchar_t * name)1117 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1118 {
1119 aes_copy_wcs(&entry->ae_uname, name);
1120 }
1121
1122 int
archive_entry_update_uname_utf8(struct archive_entry * entry,const char * name)1123 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1124 {
1125 return (aes_update_utf8(&entry->ae_uname, name));
1126 }
1127
1128 /*
1129 * ACL management. The following would, of course, be a lot simpler
1130 * if: 1) the last draft of POSIX.1e were a really thorough and
1131 * complete standard that addressed the needs of ACL archiving and 2)
1132 * everyone followed it faithfully. Alas, neither is true, so the
1133 * following is a lot more complex than might seem necessary to the
1134 * uninitiated.
1135 */
1136
1137 void
archive_entry_acl_clear(struct archive_entry * entry)1138 archive_entry_acl_clear(struct archive_entry *entry)
1139 {
1140 struct ae_acl *ap;
1141
1142 while (entry->acl_head != NULL) {
1143 ap = entry->acl_head->next;
1144 aes_clean(&entry->acl_head->name);
1145 free(entry->acl_head);
1146 entry->acl_head = ap;
1147 }
1148 if (entry->acl_text_w != NULL) {
1149 free(entry->acl_text_w);
1150 entry->acl_text_w = NULL;
1151 }
1152 entry->acl_p = NULL;
1153 entry->acl_state = 0; /* Not counting. */
1154 }
1155
1156 /*
1157 * Add a single ACL entry to the internal list of ACL data.
1158 */
1159 void
archive_entry_acl_add_entry(struct archive_entry * entry,int type,int permset,int tag,int id,const char * name)1160 archive_entry_acl_add_entry(struct archive_entry *entry,
1161 int type, int permset, int tag, int id, const char *name)
1162 {
1163 struct ae_acl *ap;
1164
1165 if (acl_special(entry, type, permset, tag) == 0)
1166 return;
1167 ap = acl_new_entry(entry, type, permset, tag, id);
1168 if (ap == NULL) {
1169 /* XXX Error XXX */
1170 return;
1171 }
1172 if (name != NULL && *name != '\0')
1173 aes_copy_mbs(&ap->name, name);
1174 else
1175 aes_clean(&ap->name);
1176 }
1177
1178 /*
1179 * As above, but with a wide-character name.
1180 */
1181 void
archive_entry_acl_add_entry_w(struct archive_entry * entry,int type,int permset,int tag,int id,const wchar_t * name)1182 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1183 int type, int permset, int tag, int id, const wchar_t *name)
1184 {
1185 archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
1186 }
1187
1188 void
archive_entry_acl_add_entry_w_len(struct archive_entry * entry,int type,int permset,int tag,int id,const wchar_t * name,size_t len)1189 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
1190 int type, int permset, int tag, int id, const wchar_t *name, size_t len)
1191 {
1192 struct ae_acl *ap;
1193
1194 if (acl_special(entry, type, permset, tag) == 0)
1195 return;
1196 ap = acl_new_entry(entry, type, permset, tag, id);
1197 if (ap == NULL) {
1198 /* XXX Error XXX */
1199 return;
1200 }
1201 if (name != NULL && *name != L'\0' && len > 0)
1202 aes_copy_wcs_len(&ap->name, name, len);
1203 else
1204 aes_clean(&ap->name);
1205 }
1206
1207 /*
1208 * If this ACL entry is part of the standard POSIX permissions set,
1209 * store the permissions in the stat structure and return zero.
1210 */
1211 static int
acl_special(struct archive_entry * entry,int type,int permset,int tag)1212 acl_special(struct archive_entry *entry, int type, int permset, int tag)
1213 {
1214 if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
1215 switch (tag) {
1216 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1217 entry->ae_stat.aest_mode &= ~0700;
1218 entry->ae_stat.aest_mode |= (permset & 7) << 6;
1219 return (0);
1220 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1221 entry->ae_stat.aest_mode &= ~0070;
1222 entry->ae_stat.aest_mode |= (permset & 7) << 3;
1223 return (0);
1224 case ARCHIVE_ENTRY_ACL_OTHER:
1225 entry->ae_stat.aest_mode &= ~0007;
1226 entry->ae_stat.aest_mode |= permset & 7;
1227 return (0);
1228 }
1229 }
1230 return (1);
1231 }
1232
1233 /*
1234 * Allocate and populate a new ACL entry with everything but the
1235 * name.
1236 */
1237 static struct ae_acl *
acl_new_entry(struct archive_entry * entry,int type,int permset,int tag,int id)1238 acl_new_entry(struct archive_entry *entry,
1239 int type, int permset, int tag, int id)
1240 {
1241 struct ae_acl *ap;
1242
1243 if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
1244 type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
1245 return (NULL);
1246 if (entry->acl_text_w != NULL) {
1247 free(entry->acl_text_w);
1248 entry->acl_text_w = NULL;
1249 }
1250
1251 /* XXX TODO: More sanity-checks on the arguments XXX */
1252
1253 /* If there's a matching entry already in the list, overwrite it. */
1254 for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
1255 if (ap->type == type && ap->tag == tag && ap->id == id) {
1256 ap->permset = permset;
1257 return (ap);
1258 }
1259 }
1260
1261 /* Add a new entry to the list. */
1262 ap = (struct ae_acl *)malloc(sizeof(*ap));
1263 if (ap == NULL)
1264 return (NULL);
1265 memset(ap, 0, sizeof(*ap));
1266 ap->next = entry->acl_head;
1267 entry->acl_head = ap;
1268 ap->type = type;
1269 ap->tag = tag;
1270 ap->id = id;
1271 ap->permset = permset;
1272 return (ap);
1273 }
1274
1275 /*
1276 * Return a count of entries matching "want_type".
1277 */
1278 int
archive_entry_acl_count(struct archive_entry * entry,int want_type)1279 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1280 {
1281 int count;
1282 struct ae_acl *ap;
1283
1284 count = 0;
1285 ap = entry->acl_head;
1286 while (ap != NULL) {
1287 if ((ap->type & want_type) != 0)
1288 count++;
1289 ap = ap->next;
1290 }
1291
1292 if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1293 count += 3;
1294 return (count);
1295 }
1296
1297 /*
1298 * Prepare for reading entries from the ACL data. Returns a count
1299 * of entries matching "want_type", or zero if there are no
1300 * non-extended ACL entries of that type.
1301 */
1302 int
archive_entry_acl_reset(struct archive_entry * entry,int want_type)1303 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1304 {
1305 int count, cutoff;
1306
1307 count = archive_entry_acl_count(entry, want_type);
1308
1309 /*
1310 * If the only entries are the three standard ones,
1311 * then don't return any ACL data. (In this case,
1312 * client can just use chmod(2) to set permissions.)
1313 */
1314 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1315 cutoff = 3;
1316 else
1317 cutoff = 0;
1318
1319 if (count > cutoff)
1320 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1321 else
1322 entry->acl_state = 0;
1323 entry->acl_p = entry->acl_head;
1324 return (count);
1325 }
1326
1327 /*
1328 * Return the next ACL entry in the list. Fake entries for the
1329 * standard permissions and include them in the returned list.
1330 */
1331
1332 int
archive_entry_acl_next(struct archive_entry * entry,int want_type,int * type,int * permset,int * tag,int * id,const char ** name)1333 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1334 int *permset, int *tag, int *id, const char **name)
1335 {
1336 *name = NULL;
1337 *id = -1;
1338
1339 /*
1340 * The acl_state is either zero (no entries available), -1
1341 * (reading from list), or an entry type (retrieve that type
1342 * from ae_stat.aest_mode).
1343 */
1344 if (entry->acl_state == 0)
1345 return (ARCHIVE_WARN);
1346
1347 /* The first three access entries are special. */
1348 if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1349 switch (entry->acl_state) {
1350 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1351 *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1352 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1353 *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1354 entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1355 return (ARCHIVE_OK);
1356 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1357 *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1358 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1359 *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1360 entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1361 return (ARCHIVE_OK);
1362 case ARCHIVE_ENTRY_ACL_OTHER:
1363 *permset = entry->ae_stat.aest_mode & 7;
1364 *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1365 *tag = ARCHIVE_ENTRY_ACL_OTHER;
1366 entry->acl_state = -1;
1367 entry->acl_p = entry->acl_head;
1368 return (ARCHIVE_OK);
1369 default:
1370 break;
1371 }
1372 }
1373
1374 while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1375 entry->acl_p = entry->acl_p->next;
1376 if (entry->acl_p == NULL) {
1377 entry->acl_state = 0;
1378 *type = 0;
1379 *permset = 0;
1380 *tag = 0;
1381 *id = -1;
1382 *name = NULL;
1383 return (ARCHIVE_EOF); /* End of ACL entries. */
1384 }
1385 *type = entry->acl_p->type;
1386 *permset = entry->acl_p->permset;
1387 *tag = entry->acl_p->tag;
1388 *id = entry->acl_p->id;
1389 *name = aes_get_mbs(&entry->acl_p->name);
1390 entry->acl_p = entry->acl_p->next;
1391 return (ARCHIVE_OK);
1392 }
1393
1394 /*
1395 * Generate a text version of the ACL. The flags parameter controls
1396 * the style of the generated ACL.
1397 */
1398 const wchar_t *
archive_entry_acl_text_w(struct archive_entry * entry,int flags)1399 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1400 {
1401 int count;
1402 size_t length;
1403 const wchar_t *wname;
1404 const wchar_t *prefix;
1405 wchar_t separator;
1406 struct ae_acl *ap;
1407 int id;
1408 wchar_t *wp;
1409
1410 if (entry->acl_text_w != NULL) {
1411 free (entry->acl_text_w);
1412 entry->acl_text_w = NULL;
1413 }
1414
1415 separator = L',';
1416 count = 0;
1417 length = 0;
1418 ap = entry->acl_head;
1419 while (ap != NULL) {
1420 if ((ap->type & flags) != 0) {
1421 count++;
1422 if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1423 (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1424 length += 8; /* "default:" */
1425 length += 5; /* tag name */
1426 length += 1; /* colon */
1427 wname = aes_get_wcs(&ap->name);
1428 if (wname != NULL)
1429 length += wcslen(wname);
1430 else
1431 length += sizeof(uid_t) * 3 + 1;
1432 length ++; /* colon */
1433 length += 3; /* rwx */
1434 length += 1; /* colon */
1435 length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1436 length ++; /* newline */
1437 }
1438 ap = ap->next;
1439 }
1440
1441 if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1442 length += 10; /* "user::rwx\n" */
1443 length += 11; /* "group::rwx\n" */
1444 length += 11; /* "other::rwx\n" */
1445 }
1446
1447 if (count == 0)
1448 return (NULL);
1449
1450 /* Now, allocate the string and actually populate it. */
1451 wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1452 if (wp == NULL)
1453 __archive_errx(1, "No memory to generate the text version of the ACL");
1454 count = 0;
1455 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1456 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1457 entry->ae_stat.aest_mode & 0700, -1);
1458 *wp++ = ',';
1459 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1460 entry->ae_stat.aest_mode & 0070, -1);
1461 *wp++ = ',';
1462 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1463 entry->ae_stat.aest_mode & 0007, -1);
1464 count += 3;
1465
1466 ap = entry->acl_head;
1467 while (ap != NULL) {
1468 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1469 wname = aes_get_wcs(&ap->name);
1470 *wp++ = separator;
1471 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1472 id = ap->id;
1473 else
1474 id = -1;
1475 append_entry_w(&wp, NULL, ap->tag, wname,
1476 ap->permset, id);
1477 count++;
1478 }
1479 ap = ap->next;
1480 }
1481 }
1482
1483
1484 if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1485 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1486 prefix = L"default:";
1487 else
1488 prefix = NULL;
1489 ap = entry->acl_head;
1490 count = 0;
1491 while (ap != NULL) {
1492 if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1493 wname = aes_get_wcs(&ap->name);
1494 if (count > 0)
1495 *wp++ = separator;
1496 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1497 id = ap->id;
1498 else
1499 id = -1;
1500 append_entry_w(&wp, prefix, ap->tag,
1501 wname, ap->permset, id);
1502 count ++;
1503 }
1504 ap = ap->next;
1505 }
1506 }
1507
1508 return (entry->acl_text_w);
1509 }
1510
1511 static void
append_id_w(wchar_t ** wp,int id)1512 append_id_w(wchar_t **wp, int id)
1513 {
1514 if (id < 0)
1515 id = 0;
1516 if (id > 9)
1517 append_id_w(wp, id / 10);
1518 *(*wp)++ = L"0123456789"[id % 10];
1519 }
1520
1521 static void
append_entry_w(wchar_t ** wp,const wchar_t * prefix,int tag,const wchar_t * wname,int perm,int id)1522 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1523 const wchar_t *wname, int perm, int id)
1524 {
1525 if (prefix != NULL) {
1526 wcscpy(*wp, prefix);
1527 *wp += wcslen(*wp);
1528 }
1529 switch (tag) {
1530 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1531 wname = NULL;
1532 id = -1;
1533 /* FALLTHROUGH */
1534 case ARCHIVE_ENTRY_ACL_USER:
1535 wcscpy(*wp, L"user");
1536 break;
1537 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1538 wname = NULL;
1539 id = -1;
1540 /* FALLTHROUGH */
1541 case ARCHIVE_ENTRY_ACL_GROUP:
1542 wcscpy(*wp, L"group");
1543 break;
1544 case ARCHIVE_ENTRY_ACL_MASK:
1545 wcscpy(*wp, L"mask");
1546 wname = NULL;
1547 id = -1;
1548 break;
1549 case ARCHIVE_ENTRY_ACL_OTHER:
1550 wcscpy(*wp, L"other");
1551 wname = NULL;
1552 id = -1;
1553 break;
1554 }
1555 *wp += wcslen(*wp);
1556 *(*wp)++ = L':';
1557 if (wname != NULL) {
1558 wcscpy(*wp, wname);
1559 *wp += wcslen(*wp);
1560 } else if (tag == ARCHIVE_ENTRY_ACL_USER
1561 || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1562 append_id_w(wp, id);
1563 id = -1;
1564 }
1565 *(*wp)++ = L':';
1566 *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1567 *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1568 *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1569 if (id != -1) {
1570 *(*wp)++ = L':';
1571 append_id_w(wp, id);
1572 }
1573 **wp = L'\0';
1574 }
1575
1576 /*
1577 * Parse a textual ACL. This automatically recognizes and supports
1578 * extensions described above. The 'type' argument is used to
1579 * indicate the type that should be used for any entries not
1580 * explicitly marked as "default:".
1581 */
1582 int
__archive_entry_acl_parse_w(struct archive_entry * entry,const wchar_t * text,int default_type)1583 __archive_entry_acl_parse_w(struct archive_entry *entry,
1584 const wchar_t *text, int default_type)
1585 {
1586 struct {
1587 const wchar_t *start;
1588 const wchar_t *end;
1589 } field[4];
1590
1591 int fields;
1592 int type, tag, permset, id;
1593 const wchar_t *p;
1594 wchar_t sep;
1595
1596 while (text != NULL && *text != L'\0') {
1597 /*
1598 * Parse the fields out of the next entry,
1599 * advance 'text' to start of next entry.
1600 */
1601 fields = 0;
1602 do {
1603 const wchar_t *start, *end;
1604 next_field_w(&text, &start, &end, &sep);
1605 if (fields < 4) {
1606 field[fields].start = start;
1607 field[fields].end = end;
1608 }
1609 ++fields;
1610 } while (sep == L':');
1611
1612 if (fields < 3)
1613 return (ARCHIVE_WARN);
1614
1615 /* Check for a numeric ID in field 1 or 3. */
1616 id = -1;
1617 isint_w(field[1].start, field[1].end, &id);
1618 /* Field 3 is optional. */
1619 if (id == -1 && fields > 3)
1620 isint_w(field[3].start, field[3].end, &id);
1621
1622 /* Parse the permissions from field 2. */
1623 permset = 0;
1624 p = field[2].start;
1625 while (p < field[2].end) {
1626 switch (*p++) {
1627 case 'r': case 'R':
1628 permset |= ARCHIVE_ENTRY_ACL_READ;
1629 break;
1630 case 'w': case 'W':
1631 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1632 break;
1633 case 'x': case 'X':
1634 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1635 break;
1636 case '-':
1637 break;
1638 default:
1639 return (ARCHIVE_WARN);
1640 }
1641 }
1642
1643 /*
1644 * Solaris extension: "defaultuser::rwx" is the
1645 * default ACL corresponding to "user::rwx", etc.
1646 */
1647 if (field[0].end-field[0].start > 7
1648 && wmemcmp(field[0].start, L"default", 7) == 0) {
1649 type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1650 field[0].start += 7;
1651 } else
1652 type = default_type;
1653
1654 if (prefix_w(field[0].start, field[0].end, L"user")) {
1655 if (id != -1 || field[1].start < field[1].end)
1656 tag = ARCHIVE_ENTRY_ACL_USER;
1657 else
1658 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1659 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1660 if (id != -1 || field[1].start < field[1].end)
1661 tag = ARCHIVE_ENTRY_ACL_GROUP;
1662 else
1663 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1664 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1665 if (id != -1 || field[1].start < field[1].end)
1666 return (ARCHIVE_WARN);
1667 tag = ARCHIVE_ENTRY_ACL_OTHER;
1668 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1669 if (id != -1 || field[1].start < field[1].end)
1670 return (ARCHIVE_WARN);
1671 tag = ARCHIVE_ENTRY_ACL_MASK;
1672 } else
1673 return (ARCHIVE_WARN);
1674
1675 /* Add entry to the internal list. */
1676 archive_entry_acl_add_entry_w_len(entry, type, permset,
1677 tag, id, field[1].start, field[1].end - field[1].start);
1678 }
1679 return (ARCHIVE_OK);
1680 }
1681
1682 /*
1683 * extended attribute handling
1684 */
1685
1686 void
archive_entry_xattr_clear(struct archive_entry * entry)1687 archive_entry_xattr_clear(struct archive_entry *entry)
1688 {
1689 struct ae_xattr *xp;
1690
1691 while (entry->xattr_head != NULL) {
1692 xp = entry->xattr_head->next;
1693 free(entry->xattr_head->name);
1694 free(entry->xattr_head->value);
1695 free(entry->xattr_head);
1696 entry->xattr_head = xp;
1697 }
1698
1699 entry->xattr_head = NULL;
1700 }
1701
1702 void
archive_entry_xattr_add_entry(struct archive_entry * entry,const char * name,const void * value,size_t size)1703 archive_entry_xattr_add_entry(struct archive_entry *entry,
1704 const char *name, const void *value, size_t size)
1705 {
1706 struct ae_xattr *xp;
1707
1708 if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1709 __archive_errx(1, "Out of memory");
1710
1711 if ((xp->name = strdup(name)) == NULL)
1712 __archive_errx(1, "Out of memory");
1713
1714 if ((xp->value = malloc(size)) != NULL) {
1715 memcpy(xp->value, value, size);
1716 xp->size = size;
1717 } else
1718 xp->size = 0;
1719
1720 xp->next = entry->xattr_head;
1721 entry->xattr_head = xp;
1722 }
1723
1724
1725 /*
1726 * returns number of the extended attribute entries
1727 */
1728 int
archive_entry_xattr_count(struct archive_entry * entry)1729 archive_entry_xattr_count(struct archive_entry *entry)
1730 {
1731 struct ae_xattr *xp;
1732 int count = 0;
1733
1734 for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1735 count++;
1736
1737 return count;
1738 }
1739
1740 int
archive_entry_xattr_reset(struct archive_entry * entry)1741 archive_entry_xattr_reset(struct archive_entry * entry)
1742 {
1743 entry->xattr_p = entry->xattr_head;
1744
1745 return archive_entry_xattr_count(entry);
1746 }
1747
1748 int
archive_entry_xattr_next(struct archive_entry * entry,const char ** name,const void ** value,size_t * size)1749 archive_entry_xattr_next(struct archive_entry * entry,
1750 const char **name, const void **value, size_t *size)
1751 {
1752 if (entry->xattr_p) {
1753 *name = entry->xattr_p->name;
1754 *value = entry->xattr_p->value;
1755 *size = entry->xattr_p->size;
1756
1757 entry->xattr_p = entry->xattr_p->next;
1758
1759 return (ARCHIVE_OK);
1760 } else {
1761 *name = NULL;
1762 *value = NULL;
1763 *size = (size_t)0;
1764 return (ARCHIVE_WARN);
1765 }
1766 }
1767
1768 /*
1769 * end of xattr handling
1770 */
1771
1772 /*
1773 * Parse a string to a positive decimal integer. Returns true if
1774 * the string is non-empty and consists only of decimal digits,
1775 * false otherwise.
1776 */
1777 static int
isint_w(const wchar_t * start,const wchar_t * end,int * result)1778 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1779 {
1780 int n = 0;
1781 if (start >= end)
1782 return (0);
1783 while (start < end) {
1784 if (*start < '0' || *start > '9')
1785 return (0);
1786 if (n > (INT_MAX / 10))
1787 n = INT_MAX;
1788 else {
1789 n *= 10;
1790 n += *start - '0';
1791 }
1792 start++;
1793 }
1794 *result = n;
1795 return (1);
1796 }
1797
1798 /*
1799 * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
1800 * to point to just after the separator. *start points to the first
1801 * character of the matched text and *end just after the last
1802 * character of the matched identifier. In particular *end - *start
1803 * is the length of the field body, not including leading or trailing
1804 * whitespace.
1805 */
1806 static void
next_field_w(const wchar_t ** wp,const wchar_t ** start,const wchar_t ** end,wchar_t * sep)1807 next_field_w(const wchar_t **wp, const wchar_t **start,
1808 const wchar_t **end, wchar_t *sep)
1809 {
1810 /* Skip leading whitespace to find start of field. */
1811 while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1812 (*wp)++;
1813 }
1814 *start = *wp;
1815
1816 /* Scan for the separator. */
1817 while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1818 **wp != L'\n') {
1819 (*wp)++;
1820 }
1821 *sep = **wp;
1822
1823 /* Trim trailing whitespace to locate end of field. */
1824 *end = *wp - 1;
1825 while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1826 (*end)--;
1827 }
1828 (*end)++;
1829
1830 /* Adjust scanner location. */
1831 if (**wp != L'\0')
1832 (*wp)++;
1833 }
1834
1835 /*
1836 * Return true if the characters [start...end) are a prefix of 'test'.
1837 * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1838 */
1839 static int
prefix_w(const wchar_t * start,const wchar_t * end,const wchar_t * test)1840 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1841 {
1842 if (start == end)
1843 return (0);
1844
1845 if (*start++ != *test++)
1846 return (0);
1847
1848 while (start < end && *start++ == *test++)
1849 ;
1850
1851 if (start < end)
1852 return (0);
1853
1854 return (1);
1855 }
1856
1857
1858 /*
1859 * Following code is modified from UC Berkeley sources, and
1860 * is subject to the following copyright notice.
1861 */
1862
1863 /*-
1864 * Copyright (c) 1993
1865 * The Regents of the University of California. All rights reserved.
1866 *
1867 * Redistribution and use in source and binary forms, with or without
1868 * modification, are permitted provided that the following conditions
1869 * are met:
1870 * 1. Redistributions of source code must retain the above copyright
1871 * notice, this list of conditions and the following disclaimer.
1872 * 2. Redistributions in binary form must reproduce the above copyright
1873 * notice, this list of conditions and the following disclaimer in the
1874 * documentation and/or other materials provided with the distribution.
1875 * 4. Neither the name of the University nor the names of its contributors
1876 * may be used to endorse or promote products derived from this software
1877 * without specific prior written permission.
1878 *
1879 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1880 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1881 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1882 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1883 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1884 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1885 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1886 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1887 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1888 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1889 * SUCH DAMAGE.
1890 */
1891
1892 static struct flag {
1893 const char *name;
1894 const wchar_t *wname;
1895 unsigned long set;
1896 unsigned long clear;
1897 } flags[] = {
1898 /* Preferred (shorter) names per flag first, all prefixed by "no" */
1899 #ifdef SF_APPEND
1900 { "nosappnd", L"nosappnd", SF_APPEND, 0 },
1901 { "nosappend", L"nosappend", SF_APPEND, 0 },
1902 #endif
1903 #ifdef EXT2_APPEND_FL /* 'a' */
1904 { "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
1905 { "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
1906 #endif
1907 #ifdef SF_ARCHIVED
1908 { "noarch", L"noarch", SF_ARCHIVED, 0 },
1909 { "noarchived", L"noarchived", SF_ARCHIVED, 0 },
1910 #endif
1911 #ifdef SF_IMMUTABLE
1912 { "noschg", L"noschg", SF_IMMUTABLE, 0 },
1913 { "noschange", L"noschange", SF_IMMUTABLE, 0 },
1914 { "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
1915 #endif
1916 #ifdef EXT2_IMMUTABLE_FL /* 'i' */
1917 { "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
1918 { "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
1919 { "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
1920 #endif
1921 #ifdef SF_NOUNLINK
1922 { "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
1923 { "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
1924 #endif
1925 #ifdef SF_SNAPSHOT
1926 { "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
1927 #endif
1928 #ifdef UF_APPEND
1929 { "nouappnd", L"nouappnd", UF_APPEND, 0 },
1930 { "nouappend", L"nouappend", UF_APPEND, 0 },
1931 #endif
1932 #ifdef UF_IMMUTABLE
1933 { "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
1934 { "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
1935 { "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
1936 #endif
1937 #ifdef UF_NODUMP
1938 { "nodump", L"nodump", 0, UF_NODUMP},
1939 #endif
1940 #ifdef EXT2_NODUMP_FL /* 'd' */
1941 { "nodump", L"nodump", 0, EXT2_NODUMP_FL},
1942 #endif
1943 #ifdef UF_OPAQUE
1944 { "noopaque", L"noopaque", UF_OPAQUE, 0 },
1945 #endif
1946 #ifdef UF_NOUNLINK
1947 { "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
1948 { "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
1949 #endif
1950 #ifdef EXT2_COMPR_FL /* 'c' */
1951 { "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
1952 #endif
1953
1954 #ifdef EXT2_NOATIME_FL /* 'A' */
1955 { "noatime", L"noatime", 0, EXT2_NOATIME_FL},
1956 #endif
1957 { NULL, NULL, 0, 0 }
1958 };
1959
1960 /*
1961 * fflagstostr --
1962 * Convert file flags to a comma-separated string. If no flags
1963 * are set, return the empty string.
1964 */
1965 static char *
ae_fflagstostr(unsigned long bitset,unsigned long bitclear)1966 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1967 {
1968 char *string, *dp;
1969 const char *sp;
1970 unsigned long bits;
1971 struct flag *flag;
1972 size_t length;
1973
1974 bits = bitset | bitclear;
1975 length = 0;
1976 for (flag = flags; flag->name != NULL; flag++)
1977 if (bits & (flag->set | flag->clear)) {
1978 length += strlen(flag->name) + 1;
1979 bits &= ~(flag->set | flag->clear);
1980 }
1981
1982 if (length == 0)
1983 return (NULL);
1984 string = (char *)malloc(length);
1985 if (string == NULL)
1986 return (NULL);
1987
1988 dp = string;
1989 for (flag = flags; flag->name != NULL; flag++) {
1990 if (bitset & flag->set || bitclear & flag->clear) {
1991 sp = flag->name + 2;
1992 } else if (bitset & flag->clear || bitclear & flag->set) {
1993 sp = flag->name;
1994 } else
1995 continue;
1996 bitset &= ~(flag->set | flag->clear);
1997 bitclear &= ~(flag->set | flag->clear);
1998 if (dp > string)
1999 *dp++ = ',';
2000 while ((*dp++ = *sp++) != '\0')
2001 ;
2002 dp--;
2003 }
2004
2005 *dp = '\0';
2006 return (string);
2007 }
2008
2009 /*
2010 * strtofflags --
2011 * Take string of arguments and return file flags. This
2012 * version works a little differently than strtofflags(3).
2013 * In particular, it always tests every token, skipping any
2014 * unrecognized tokens. It returns a pointer to the first
2015 * unrecognized token, or NULL if every token was recognized.
2016 * This version is also const-correct and does not modify the
2017 * provided string.
2018 */
2019 static const char *
ae_strtofflags(const char * s,unsigned long * setp,unsigned long * clrp)2020 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
2021 {
2022 const char *start, *end;
2023 struct flag *flag;
2024 unsigned long set, clear;
2025 const char *failed;
2026
2027 set = clear = 0;
2028 start = s;
2029 failed = NULL;
2030 /* Find start of first token. */
2031 while (*start == '\t' || *start == ' ' || *start == ',')
2032 start++;
2033 while (*start != '\0') {
2034 /* Locate end of token. */
2035 end = start;
2036 while (*end != '\0' && *end != '\t' &&
2037 *end != ' ' && *end != ',')
2038 end++;
2039 for (flag = flags; flag->name != NULL; flag++) {
2040 if (memcmp(start, flag->name, end - start) == 0) {
2041 /* Matched "noXXXX", so reverse the sense. */
2042 clear |= flag->set;
2043 set |= flag->clear;
2044 break;
2045 } else if (memcmp(start, flag->name + 2, end - start)
2046 == 0) {
2047 /* Matched "XXXX", so don't reverse. */
2048 set |= flag->set;
2049 clear |= flag->clear;
2050 break;
2051 }
2052 }
2053 /* Ignore unknown flag names. */
2054 if (flag->name == NULL && failed == NULL)
2055 failed = start;
2056
2057 /* Find start of next token. */
2058 start = end;
2059 while (*start == '\t' || *start == ' ' || *start == ',')
2060 start++;
2061
2062 }
2063
2064 if (setp)
2065 *setp = set;
2066 if (clrp)
2067 *clrp = clear;
2068
2069 /* Return location of first failure. */
2070 return (failed);
2071 }
2072
2073 /*
2074 * wcstofflags --
2075 * Take string of arguments and return file flags. This
2076 * version works a little differently than strtofflags(3).
2077 * In particular, it always tests every token, skipping any
2078 * unrecognized tokens. It returns a pointer to the first
2079 * unrecognized token, or NULL if every token was recognized.
2080 * This version is also const-correct and does not modify the
2081 * provided string.
2082 */
2083 static const wchar_t *
ae_wcstofflags(const wchar_t * s,unsigned long * setp,unsigned long * clrp)2084 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
2085 {
2086 const wchar_t *start, *end;
2087 struct flag *flag;
2088 unsigned long set, clear;
2089 const wchar_t *failed;
2090
2091 set = clear = 0;
2092 start = s;
2093 failed = NULL;
2094 /* Find start of first token. */
2095 while (*start == L'\t' || *start == L' ' || *start == L',')
2096 start++;
2097 while (*start != L'\0') {
2098 /* Locate end of token. */
2099 end = start;
2100 while (*end != L'\0' && *end != L'\t' &&
2101 *end != L' ' && *end != L',')
2102 end++;
2103 for (flag = flags; flag->wname != NULL; flag++) {
2104 if (wmemcmp(start, flag->wname, end - start) == 0) {
2105 /* Matched "noXXXX", so reverse the sense. */
2106 clear |= flag->set;
2107 set |= flag->clear;
2108 break;
2109 } else if (wmemcmp(start, flag->wname + 2, end - start)
2110 == 0) {
2111 /* Matched "XXXX", so don't reverse. */
2112 set |= flag->set;
2113 clear |= flag->clear;
2114 break;
2115 }
2116 }
2117 /* Ignore unknown flag names. */
2118 if (flag->wname == NULL && failed == NULL)
2119 failed = start;
2120
2121 /* Find start of next token. */
2122 start = end;
2123 while (*start == L'\t' || *start == L' ' || *start == L',')
2124 start++;
2125
2126 }
2127
2128 if (setp)
2129 *setp = set;
2130 if (clrp)
2131 *clrp = clear;
2132
2133 /* Return location of first failure. */
2134 return (failed);
2135 }
2136
2137
2138 #ifdef TEST
2139 #include <stdio.h>
2140 int
main(int argc,char ** argv)2141 main(int argc, char **argv)
2142 {
2143 struct archive_entry *entry = archive_entry_new();
2144 unsigned long set, clear;
2145 const wchar_t *remainder;
2146
2147 remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
2148 archive_entry_fflags(entry, &set, &clear);
2149
2150 wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
2151
2152 wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
2153 return (0);
2154 }
2155 #endif
2156