2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
41 #include "archive_entry.h"
42 #include "archive_entry_locale.h"
43 #include "archive_private.h"
44 #include "archive_write_private.h"
47 struct sparse_block *next;
54 uint64_t entry_bytes_remaining;
55 uint64_t entry_padding;
56 struct archive_string l_url_encoded_name;
57 struct archive_string pax_header;
58 struct archive_string sparse_map;
59 size_t sparse_map_padding;
60 struct sparse_block *sparse_list;
61 struct sparse_block *sparse_tail;
62 struct archive_string_conv *sconv_utf8;
66 static void add_pax_attr(struct archive_string *, const char *key,
68 static void add_pax_attr_int(struct archive_string *,
69 const char *key, int64_t value);
70 static void add_pax_attr_time(struct archive_string *,
71 const char *key, int64_t sec,
73 static ssize_t archive_write_pax_data(struct archive_write *,
74 const void *, size_t);
75 static int archive_write_pax_close(struct archive_write *);
76 static int archive_write_pax_free(struct archive_write *);
77 static int archive_write_pax_finish_entry(struct archive_write *);
78 static int archive_write_pax_header(struct archive_write *,
79 struct archive_entry *);
80 static int archive_write_pax_options(struct archive_write *,
81 const char *, const char *);
82 static char *base64_encode(const char *src, size_t len);
83 static char *build_gnu_sparse_name(char *dest, const char *src);
84 static char *build_pax_attribute_name(char *dest, const char *src);
85 static char *build_ustar_entry_name(char *dest, const char *src,
86 size_t src_length, const char *insert);
87 static char *format_int(char *dest, int64_t);
88 static int has_non_ASCII(const char *);
89 static void sparse_list_clear(struct pax *);
90 static int sparse_list_add(struct pax *, int64_t, int64_t);
91 static char *url_encode(const char *in);
94 * Set output format to 'restricted pax' format.
96 * This is the same as normal 'pax', but tries to suppress
97 * the pax header whenever possible. This is the default for
98 * bsdtar, for instance.
101 archive_write_set_format_pax_restricted(struct archive *_a)
103 struct archive_write *a = (struct archive_write *)_a;
106 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
107 ARCHIVE_STATE_NEW, "archive_write_set_format_pax_restricted");
109 r = archive_write_set_format_pax(&a->archive);
110 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
111 a->archive.archive_format_name = "restricted POSIX pax interchange";
116 * Set output format to 'pax' format.
119 archive_write_set_format_pax(struct archive *_a)
121 struct archive_write *a = (struct archive_write *)_a;
124 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
125 ARCHIVE_STATE_NEW, "archive_write_set_format_pax");
127 if (a->format_free != NULL)
130 pax = (struct pax *)malloc(sizeof(*pax));
132 archive_set_error(&a->archive, ENOMEM,
133 "Can't allocate pax data");
134 return (ARCHIVE_FATAL);
136 memset(pax, 0, sizeof(*pax));
137 a->format_data = pax;
138 a->format_name = "pax";
139 a->format_options = archive_write_pax_options;
140 a->format_write_header = archive_write_pax_header;
141 a->format_write_data = archive_write_pax_data;
142 a->format_close = archive_write_pax_close;
143 a->format_free = archive_write_pax_free;
144 a->format_finish_entry = archive_write_pax_finish_entry;
145 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
146 a->archive.archive_format_name = "POSIX pax interchange";
151 archive_write_pax_options(struct archive_write *a, const char *key,
154 struct pax *pax = (struct pax *)a->format_data;
155 int ret = ARCHIVE_FAILED;
157 if (strcmp(key, "hdrcharset") == 0) {
159 * The character-set we can use are defined in
160 * IEEE Std 1003.1-2001
162 if (val == NULL || val[0] == 0)
163 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
164 "pax: hdrcharset option needs a character-set name");
165 else if (strcmp(val, "BINARY") == 0 ||
166 strcmp(val, "binary") == 0) {
168 * Specify binary mode. We will not convert
169 * filenames, uname and gname to any charsets.
173 } else if (strcmp(val, "UTF-8") == 0) {
175 * Specify UTF-8 character-set to be used for
176 * filenames. This is almost the test that
177 * running platform supports the string conversion.
178 * Especially libarchive_test needs this trick for
181 pax->sconv_utf8 = archive_string_conversion_to_charset(
182 &(a->archive), "UTF-8", 0);
183 if (pax->sconv_utf8 == NULL)
188 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
189 "pax: invalid charset name");
193 /* Note: The "warn" return is just to inform the options
194 * supervisor that we didn't handle it. It will generate
195 * a suitable error if no one used this option. */
196 return (ARCHIVE_WARN);
200 * Note: This code assumes that 'nanos' has the same sign as 'sec',
201 * which implies that sec=-1, nanos=200000000 represents -1.2 seconds
202 * and not -0.8 seconds. This is a pretty pedantic point, as we're
203 * unlikely to encounter many real files created before Jan 1, 1970,
204 * much less ones with timestamps recorded to sub-second resolution.
207 add_pax_attr_time(struct archive_string *as, const char *key,
208 int64_t sec, unsigned long nanos)
213 * Note that each byte contributes fewer than 3 base-10
214 * digits, so this will always be big enough.
216 char tmp[1 + 3*sizeof(sec) + 1 + 3*sizeof(nanos)];
218 tmp[sizeof(tmp) - 1] = 0;
219 t = tmp + sizeof(tmp) - 1;
221 /* Skip trailing zeros in the fractional part. */
222 for (digit = 0, i = 10; i > 0 && digit == 0; i--) {
227 /* Only format the fraction if it's non-zero. */
230 *--t = "0123456789"[digit];
237 t = format_int(t, sec);
239 add_pax_attr(as, key, t);
243 format_int(char *t, int64_t i)
248 ui = (i == INT64_MIN) ? (uint64_t)(INT64_MAX) + 1 : (uint64_t)(-i);
253 *--t = "0123456789"[ui % 10];
261 add_pax_attr_int(struct archive_string *as, const char *key, int64_t value)
263 char tmp[1 + 3 * sizeof(value)];
265 tmp[sizeof(tmp) - 1] = 0;
266 add_pax_attr(as, key, format_int(tmp + sizeof(tmp) - 1, value));
270 * Add a key/value attribute to the pax header. This function handles
271 * the length field and various other syntactic requirements.
274 add_pax_attr(struct archive_string *as, const char *key, const char *value)
276 int digits, i, len, next_ten;
277 char tmp[1 + 3 * sizeof(int)]; /* < 3 base-10 digits per byte */
280 * PAX attributes have the following layout:
281 * <len> <space> <key> <=> <value> <nl>
283 len = 1 + (int)strlen(key) + 1 + (int)strlen(value) + 1;
286 * The <len> field includes the length of the <len> field, so
287 * computing the correct length is tricky. I start by
288 * counting the number of base-10 digits in 'len' and
289 * computing the next higher power of 10.
297 next_ten = next_ten * 10;
300 * For example, if string without the length field is 99
301 * chars, then adding the 2 digit length "99" will force the
302 * total length past 100, requiring an extra digit. The next
303 * statement adjusts for this effect.
305 if (len + digits >= next_ten)
308 /* Now, we have the right length so we can build the line. */
309 tmp[sizeof(tmp) - 1] = 0; /* Null-terminate the work area. */
310 archive_strcat(as, format_int(tmp + sizeof(tmp) - 1, len + digits));
311 archive_strappend_char(as, ' ');
312 archive_strcat(as, key);
313 archive_strappend_char(as, '=');
314 archive_strcat(as, value);
315 archive_strappend_char(as, '\n');
319 archive_write_pax_header_xattrs(struct archive_write *a,
320 struct pax *pax, struct archive_entry *entry)
322 struct archive_string s;
323 int i = archive_entry_xattr_reset(entry);
329 char *url_encoded_name = NULL, *encoded_name = NULL;
333 archive_entry_xattr_next(entry, &name, &value, &size);
334 url_encoded_name = url_encode(name);
335 if (url_encoded_name != NULL) {
336 /* Convert narrow-character to UTF-8. */
337 r = archive_strcpy_l(&(pax->l_url_encoded_name),
338 url_encoded_name, pax->sconv_utf8);
339 free(url_encoded_name); /* Done with this. */
341 encoded_name = pax->l_url_encoded_name.s;
342 else if (errno == ENOMEM) {
343 archive_set_error(&a->archive, ENOMEM,
344 "Can't allocate memory for Linkname");
345 return (ARCHIVE_FATAL);
349 encoded_value = base64_encode((const char *)value, size);
351 if (encoded_name != NULL && encoded_value != NULL) {
352 archive_string_init(&s);
353 archive_strcpy(&s, "LIBARCHIVE.xattr.");
354 archive_strcat(&s, encoded_name);
355 add_pax_attr(&(pax->pax_header), s.s, encoded_value);
356 archive_string_free(&s);
364 get_entry_hardlink(struct archive_write *a, struct archive_entry *entry,
365 const char **name, size_t *length, struct archive_string_conv *sc)
369 r = archive_entry_hardlink_l(entry, name, length, sc);
371 if (errno == ENOMEM) {
372 archive_set_error(&a->archive, ENOMEM,
373 "Can't allocate memory for Linkname");
374 return (ARCHIVE_FATAL);
376 return (ARCHIVE_WARN);
382 get_entry_pathname(struct archive_write *a, struct archive_entry *entry,
383 const char **name, size_t *length, struct archive_string_conv *sc)
387 r = archive_entry_pathname_l(entry, name, length, sc);
389 if (errno == ENOMEM) {
390 archive_set_error(&a->archive, ENOMEM,
391 "Can't allocate memory for Pathname");
392 return (ARCHIVE_FATAL);
394 return (ARCHIVE_WARN);
400 get_entry_uname(struct archive_write *a, struct archive_entry *entry,
401 const char **name, size_t *length, struct archive_string_conv *sc)
405 r = archive_entry_uname_l(entry, name, length, sc);
407 if (errno == ENOMEM) {
408 archive_set_error(&a->archive, ENOMEM,
409 "Can't allocate memory for Uname");
410 return (ARCHIVE_FATAL);
412 return (ARCHIVE_WARN);
418 get_entry_gname(struct archive_write *a, struct archive_entry *entry,
419 const char **name, size_t *length, struct archive_string_conv *sc)
423 r = archive_entry_gname_l(entry, name, length, sc);
425 if (errno == ENOMEM) {
426 archive_set_error(&a->archive, ENOMEM,
427 "Can't allocate memory for Gname");
428 return (ARCHIVE_FATAL);
430 return (ARCHIVE_WARN);
436 get_entry_symlink(struct archive_write *a, struct archive_entry *entry,
437 const char **name, size_t *length, struct archive_string_conv *sc)
441 r = archive_entry_symlink_l(entry, name, length, sc);
443 if (errno == ENOMEM) {
444 archive_set_error(&a->archive, ENOMEM,
445 "Can't allocate memory for Linkname");
446 return (ARCHIVE_FATAL);
448 return (ARCHIVE_WARN);
454 * TODO: Consider adding 'comment' and 'charset' fields to
455 * archive_entry so that clients can specify them. Also, consider
456 * adding generic key/value tags so clients can add arbitrary
459 * TODO: Break up this 700-line function!!!! Yowza!
462 archive_write_pax_header(struct archive_write *a,
463 struct archive_entry *entry_original)
465 struct archive_entry *entry_main;
468 int need_extension, r, ret;
470 uint64_t sparse_total, real_size;
472 const char *hardlink;
473 const char *path = NULL, *linkpath = NULL;
474 const char *uname = NULL, *gname = NULL;
475 const void *mac_metadata;
476 size_t mac_metadata_size;
477 struct archive_string_conv *sconv;
478 size_t hardlink_length, path_length, linkpath_length;
479 size_t uname_length, gname_length;
483 char ustar_entry_name[256];
484 char pax_entry_name[256];
485 char gnu_sparse_name[256];
486 struct archive_string entry_name;
490 pax = (struct pax *)a->format_data;
493 if (archive_entry_pathname(entry_original) == NULL) {
494 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
495 "Can't record entry in tar file without pathname");
496 return (ARCHIVE_FAILED);
500 * Choose a header encoding.
503 sconv = NULL;/* Binary mode. */
505 /* Header encoding is UTF-8. */
506 if (pax->sconv_utf8 == NULL) {
507 /* Initialize the string conversion object
509 pax->sconv_utf8 = archive_string_conversion_to_charset(
510 &(a->archive), "UTF-8", 1);
511 if (pax->sconv_utf8 == NULL)
512 /* Couldn't allocate memory */
513 return (ARCHIVE_FAILED);
515 sconv = pax->sconv_utf8;
518 r = get_entry_hardlink(a, entry_original, &hardlink,
519 &hardlink_length, sconv);
520 if (r == ARCHIVE_FATAL)
522 else if (r != ARCHIVE_OK) {
523 r = get_entry_hardlink(a, entry_original, &hardlink,
524 &hardlink_length, NULL);
525 if (r == ARCHIVE_FATAL)
527 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
528 "Can't translate linkname '%s' to %s", hardlink,
529 archive_string_conversion_charset_name(sconv));
531 sconv = NULL;/* The header charset switches to binary mode. */
534 /* Make sure this is a type of entry that we can handle here */
535 if (hardlink == NULL) {
536 switch (archive_entry_filetype(entry_original)) {
546 * Ensure a trailing '/'. Modify the original
547 * entry so the client sees the change.
549 #if defined(_WIN32) && !defined(__CYGWIN__)
552 wp = archive_entry_pathname_w(entry_original);
553 if (wp != NULL && wp[wcslen(wp) -1] != L'/') {
554 struct archive_wstring ws;
556 archive_string_init(&ws);
557 path_length = wcslen(wp);
558 if (archive_wstring_ensure(&ws,
559 path_length + 2) == NULL) {
560 archive_set_error(&a->archive, ENOMEM,
561 "Can't allocate pax data");
562 archive_wstring_free(&ws);
563 return(ARCHIVE_FATAL);
565 /* Should we keep '\' ? */
566 if (wp[path_length -1] == L'\\')
568 archive_wstrncpy(&ws, wp, path_length);
569 archive_wstrappend_wchar(&ws, L'/');
570 archive_entry_copy_pathname_w(
571 entry_original, ws.s);
572 archive_wstring_free(&ws);
576 p = archive_entry_pathname(entry_original);
578 * On Windows, this is a backup operation just in
579 * case getting WCS failed. On POSIX, this is a
582 if (p != NULL && p[strlen(p) - 1] != '/') {
583 struct archive_string as;
585 archive_string_init(&as);
586 path_length = strlen(p);
587 if (archive_string_ensure(&as,
588 path_length + 2) == NULL) {
589 archive_set_error(&a->archive, ENOMEM,
590 "Can't allocate pax data");
591 archive_string_free(&as);
592 return(ARCHIVE_FATAL);
594 #if defined(_WIN32) && !defined(__CYGWIN__)
595 /* NOTE: This might break the pathname
596 * if the current code page is CP932 and
597 * the pathname includes a character '\'
598 * as a part of its multibyte pathname. */
599 if (p[strlen(p) -1] == '\\')
603 archive_strncpy(&as, p, path_length);
604 archive_strappend_char(&as, '/');
605 archive_entry_copy_pathname(
606 entry_original, as.s);
607 archive_string_free(&as);
612 archive_set_error(&a->archive,
613 ARCHIVE_ERRNO_FILE_FORMAT,
614 "tar format cannot archive socket");
615 return (ARCHIVE_FAILED);
617 archive_set_error(&a->archive,
618 ARCHIVE_ERRNO_FILE_FORMAT,
619 "tar format cannot archive this (type=0%lo)",
621 archive_entry_filetype(entry_original));
622 return (ARCHIVE_FAILED);
627 * If Mac OS metadata blob is here, recurse to write that
628 * as a separate entry. This is really a pretty poor design:
629 * In particular, it doubles the overhead for long filenames.
630 * TODO: Help Apple folks design something better and figure
631 * out how to transition from this legacy format.
633 * Note that this code is present on every platform; clients
634 * on non-Mac are unlikely to ever provide this data, but
635 * applications that copy entries from one archive to another
636 * should not lose data just because the local filesystem
640 archive_entry_mac_metadata(entry_original, &mac_metadata_size);
641 if (mac_metadata != NULL) {
645 struct archive_entry *extra = archive_entry_new2(&a->archive);
647 oname = archive_entry_pathname(entry_original);
648 name_length = strlen(oname);
649 name = malloc(name_length + 3);
650 if (name == NULL || extra == NULL) {
651 /* XXX error message */
652 archive_entry_free(extra);
654 return (ARCHIVE_FAILED);
657 /* Find last '/'; strip trailing '/' characters */
658 bname = strrchr(name, '/');
659 while (bname != NULL && bname[1] == '\0') {
661 bname = strrchr(name, '/');
664 memmove(name + 2, name, name_length + 1);
665 memmove(name, "._", 2);
668 memmove(bname + 2, bname, strlen(bname) + 1);
669 memmove(bname, "._", 2);
671 archive_entry_copy_pathname(extra, name);
674 archive_entry_set_size(extra, mac_metadata_size);
675 archive_entry_set_filetype(extra, AE_IFREG);
676 archive_entry_set_perm(extra,
677 archive_entry_perm(entry_original));
678 archive_entry_set_mtime(extra,
679 archive_entry_mtime(entry_original),
680 archive_entry_mtime_nsec(entry_original));
681 archive_entry_set_gid(extra,
682 archive_entry_gid(entry_original));
683 archive_entry_set_gname(extra,
684 archive_entry_gname(entry_original));
685 archive_entry_set_uid(extra,
686 archive_entry_uid(entry_original));
687 archive_entry_set_uname(extra,
688 archive_entry_uname(entry_original));
690 /* Recurse to write the special copyfile entry. */
691 r = archive_write_pax_header(a, extra);
692 archive_entry_free(extra);
693 if (r < ARCHIVE_WARN)
697 r = (int)archive_write_pax_data(a, mac_metadata,
699 if (r < ARCHIVE_WARN)
703 r = archive_write_pax_finish_entry(a);
704 if (r < ARCHIVE_WARN)
710 /* Copy entry so we can modify it as needed. */
711 #if defined(_WIN32) && !defined(__CYGWIN__)
712 /* Make sure the path separators in pahtname, hardlink and symlink
713 * are all slash '/', not the Windows path separator '\'. */
714 entry_main = __la_win_entry_in_posix_pathseparator(entry_original);
715 if (entry_main == entry_original)
716 entry_main = archive_entry_clone(entry_original);
718 entry_main = archive_entry_clone(entry_original);
720 if (entry_main == NULL) {
721 archive_set_error(&a->archive, ENOMEM,
722 "Can't allocate pax data");
723 return(ARCHIVE_FATAL);
725 archive_string_empty(&(pax->pax_header)); /* Blank our work area. */
726 archive_string_empty(&(pax->sparse_map));
728 sparse_list_clear(pax);
730 if (hardlink == NULL &&
731 archive_entry_filetype(entry_main) == AE_IFREG)
732 sparse_count = archive_entry_sparse_reset(entry_main);
736 int64_t offset, length, last_offset = 0;
737 /* Get the last entry of sparse block. */
738 while (archive_entry_sparse_next(
739 entry_main, &offset, &length) == ARCHIVE_OK)
740 last_offset = offset + length;
742 /* If the last sparse block does not reach the end of file,
743 * We have to add a empty sparse block as the last entry to
744 * manage storing file data. */
745 if (last_offset < archive_entry_size(entry_main))
746 archive_entry_sparse_add_entry(entry_main,
747 archive_entry_size(entry_main), 0);
748 sparse_count = archive_entry_sparse_reset(entry_main);
752 * First, check the name fields and see if any of them
753 * require binary coding. If any of them does, then all of
756 r = get_entry_pathname(a, entry_main, &path, &path_length, sconv);
757 if (r == ARCHIVE_FATAL)
759 else if (r != ARCHIVE_OK) {
760 r = get_entry_pathname(a, entry_main, &path,
762 if (r == ARCHIVE_FATAL)
764 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
765 "Can't translate pathname '%s' to %s", path,
766 archive_string_conversion_charset_name(sconv));
768 sconv = NULL;/* The header charset switches to binary mode. */
770 r = get_entry_uname(a, entry_main, &uname, &uname_length, sconv);
771 if (r == ARCHIVE_FATAL)
773 else if (r != ARCHIVE_OK) {
774 r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL);
775 if (r == ARCHIVE_FATAL)
777 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
778 "Can't translate uname '%s' to %s", uname,
779 archive_string_conversion_charset_name(sconv));
781 sconv = NULL;/* The header charset switches to binary mode. */
783 r = get_entry_gname(a, entry_main, &gname, &gname_length, sconv);
784 if (r == ARCHIVE_FATAL)
786 else if (r != ARCHIVE_OK) {
787 r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL);
788 if (r == ARCHIVE_FATAL)
790 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
791 "Can't translate gname '%s' to %s", gname,
792 archive_string_conversion_charset_name(sconv));
794 sconv = NULL;/* The header charset switches to binary mode. */
797 linkpath_length = hardlink_length;
798 if (linkpath == NULL) {
799 r = get_entry_symlink(a, entry_main, &linkpath,
800 &linkpath_length, sconv);
801 if (r == ARCHIVE_FATAL)
803 else if (r != ARCHIVE_OK) {
804 r = get_entry_symlink(a, entry_main, &linkpath,
805 &linkpath_length, NULL);
806 if (r == ARCHIVE_FATAL)
808 archive_set_error(&a->archive,
809 ARCHIVE_ERRNO_FILE_FORMAT,
810 "Can't translate linkname '%s' to %s", linkpath,
811 archive_string_conversion_charset_name(sconv));
817 /* If any string conversions failed, get all attributes
819 if (sconv == NULL && !pax->opt_binary) {
820 if (hardlink != NULL) {
821 r = get_entry_hardlink(a, entry_main, &hardlink,
822 &hardlink_length, NULL);
823 if (r == ARCHIVE_FATAL)
826 linkpath_length = hardlink_length;
828 r = get_entry_pathname(a, entry_main, &path,
830 if (r == ARCHIVE_FATAL)
832 r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL);
833 if (r == ARCHIVE_FATAL)
835 r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL);
836 if (r == ARCHIVE_FATAL)
840 /* Store the header encoding first, to be nice to readers. */
842 add_pax_attr(&(pax->pax_header), "hdrcharset", "BINARY");
846 * If name is too long, or has non-ASCII characters, add
847 * 'path' to pax extended attrs. (Note that an unconvertible
848 * name must have non-ASCII characters.)
850 if (has_non_ASCII(path)) {
851 /* We have non-ASCII characters. */
852 add_pax_attr(&(pax->pax_header), "path", path);
853 archive_entry_set_pathname(entry_main,
854 build_ustar_entry_name(ustar_entry_name,
855 path, path_length, NULL));
858 /* We have an all-ASCII path; we'd like to just store
859 * it in the ustar header if it will fit. Yes, this
860 * duplicates some of the logic in
861 * archive_write_set_format_ustar.c
863 if (path_length <= 100) {
864 /* Fits in the old 100-char tar name field. */
866 /* Find largest suffix that will fit. */
867 /* Note: strlen() > 100, so strlen() - 100 - 1 >= 0 */
868 suffix = strchr(path + path_length - 100 - 1, '/');
869 /* Don't attempt an empty prefix. */
871 suffix = strchr(suffix + 1, '/');
872 /* We can put it in the ustar header if it's
873 * all ASCII and it's either <= 100 characters
874 * or can be split at a '/' into a prefix <=
875 * 155 chars and a suffix <= 100 chars. (Note
876 * the strchr() above will return NULL exactly
877 * when the path can't be split.)
879 if (suffix == NULL /* Suffix > 100 chars. */
880 || suffix[1] == '\0' /* empty suffix */
881 || suffix - path > 155) /* Prefix > 155 chars */
883 add_pax_attr(&(pax->pax_header), "path", path);
884 archive_entry_set_pathname(entry_main,
885 build_ustar_entry_name(ustar_entry_name,
886 path, path_length, NULL));
892 if (linkpath != NULL) {
893 /* If link name is too long or has non-ASCII characters, add
894 * 'linkpath' to pax extended attrs. */
895 if (linkpath_length > 100 || has_non_ASCII(linkpath)) {
896 add_pax_attr(&(pax->pax_header), "linkpath", linkpath);
897 if (linkpath_length > 100) {
898 if (hardlink != NULL)
899 archive_entry_set_hardlink(entry_main,
900 "././@LongHardLink");
902 archive_entry_set_symlink(entry_main,
908 /* Save a pathname since it will be renamed if `entry_main` has
910 archive_string_init(&entry_name);
911 archive_strcpy(&entry_name, archive_entry_pathname(entry_main));
913 /* If file size is too large, add 'size' to pax extended attrs. */
914 if (archive_entry_size(entry_main) >= (((int64_t)1) << 33)) {
915 add_pax_attr_int(&(pax->pax_header), "size",
916 archive_entry_size(entry_main));
920 /* If numeric GID is too large, add 'gid' to pax extended attrs. */
921 if ((unsigned int)archive_entry_gid(entry_main) >= (1 << 18)) {
922 add_pax_attr_int(&(pax->pax_header), "gid",
923 archive_entry_gid(entry_main));
927 /* If group name is too large or has non-ASCII characters, add
928 * 'gname' to pax extended attrs. */
930 if (gname_length > 31 || has_non_ASCII(gname)) {
931 add_pax_attr(&(pax->pax_header), "gname", gname);
936 /* If numeric UID is too large, add 'uid' to pax extended attrs. */
937 if ((unsigned int)archive_entry_uid(entry_main) >= (1 << 18)) {
938 add_pax_attr_int(&(pax->pax_header), "uid",
939 archive_entry_uid(entry_main));
943 /* Add 'uname' to pax extended attrs if necessary. */
945 if (uname_length > 31 || has_non_ASCII(uname)) {
946 add_pax_attr(&(pax->pax_header), "uname", uname);
952 * POSIX/SUSv3 doesn't provide a standard key for large device
953 * numbers. I use the same keys here that Joerg Schilling
954 * used for 'star.' (Which, somewhat confusingly, are called
955 * "devXXX" even though they code "rdev" values.) No doubt,
956 * other implementations use other keys. Note that there's no
957 * reason we can't write the same information into a number of
960 * Of course, this is only needed for block or char device entries.
962 if (archive_entry_filetype(entry_main) == AE_IFBLK
963 || archive_entry_filetype(entry_main) == AE_IFCHR) {
965 * If rdevmajor is too large, add 'SCHILY.devmajor' to
966 * extended attributes.
968 int rdevmajor, rdevminor;
969 rdevmajor = archive_entry_rdevmajor(entry_main);
970 rdevminor = archive_entry_rdevminor(entry_main);
971 if (rdevmajor >= (1 << 18)) {
972 add_pax_attr_int(&(pax->pax_header), "SCHILY.devmajor",
975 * Non-strict formatting below means we don't
976 * have to truncate here. Not truncating improves
977 * the chance that some more modern tar archivers
978 * (such as GNU tar 1.13) can restore the full
979 * value even if they don't understand the pax
980 * extended attributes. See my rant below about
981 * file size fields for additional details.
983 /* archive_entry_set_rdevmajor(entry_main,
984 rdevmajor & ((1 << 18) - 1)); */
989 * If devminor is too large, add 'SCHILY.devminor' to
990 * extended attributes.
992 if (rdevminor >= (1 << 18)) {
993 add_pax_attr_int(&(pax->pax_header), "SCHILY.devminor",
995 /* Truncation is not necessary here, either. */
996 /* archive_entry_set_rdevminor(entry_main,
997 rdevminor & ((1 << 18) - 1)); */
1003 * Technically, the mtime field in the ustar header can
1004 * support 33 bits, but many platforms use signed 32-bit time
1005 * values. The cutoff of 0x7fffffff here is a compromise.
1006 * Yes, this check is duplicated just below; this helps to
1007 * avoid writing an mtime attribute just to handle a
1008 * high-resolution timestamp in "restricted pax" mode.
1010 if (!need_extension &&
1011 ((archive_entry_mtime(entry_main) < 0)
1012 || (archive_entry_mtime(entry_main) >= 0x7fffffff)))
1015 /* I use a star-compatible file flag attribute. */
1016 p = archive_entry_fflags_text(entry_main);
1017 if (!need_extension && p != NULL && *p != '\0')
1020 /* If there are non-trivial ACL entries, we need an extension. */
1021 if (!need_extension && archive_entry_acl_count(entry_original,
1022 ARCHIVE_ENTRY_ACL_TYPE_ACCESS) > 0)
1025 /* If there are non-trivial ACL entries, we need an extension. */
1026 if (!need_extension && archive_entry_acl_count(entry_original,
1027 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) > 0)
1030 /* If there are extended attributes, we need an extension */
1031 if (!need_extension && archive_entry_xattr_count(entry_original) > 0)
1034 /* If there are sparse info, we need an extension */
1035 if (!need_extension && sparse_count > 0)
1039 * The following items are handled differently in "pax
1040 * restricted" format. In particular, in "pax restricted"
1041 * format they won't be added unless need_extension is
1042 * already set (we're already generating an extended header, so
1043 * may as well include these).
1045 if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED ||
1048 if (archive_entry_mtime(entry_main) < 0 ||
1049 archive_entry_mtime(entry_main) >= 0x7fffffff ||
1050 archive_entry_mtime_nsec(entry_main) != 0)
1051 add_pax_attr_time(&(pax->pax_header), "mtime",
1052 archive_entry_mtime(entry_main),
1053 archive_entry_mtime_nsec(entry_main));
1055 if (archive_entry_ctime(entry_main) != 0 ||
1056 archive_entry_ctime_nsec(entry_main) != 0)
1057 add_pax_attr_time(&(pax->pax_header), "ctime",
1058 archive_entry_ctime(entry_main),
1059 archive_entry_ctime_nsec(entry_main));
1061 if (archive_entry_atime(entry_main) != 0 ||
1062 archive_entry_atime_nsec(entry_main) != 0)
1063 add_pax_attr_time(&(pax->pax_header), "atime",
1064 archive_entry_atime(entry_main),
1065 archive_entry_atime_nsec(entry_main));
1067 /* Store birth/creationtime only if it's earlier than mtime */
1068 if (archive_entry_birthtime_is_set(entry_main) &&
1069 archive_entry_birthtime(entry_main)
1070 < archive_entry_mtime(entry_main))
1071 add_pax_attr_time(&(pax->pax_header),
1072 "LIBARCHIVE.creationtime",
1073 archive_entry_birthtime(entry_main),
1074 archive_entry_birthtime_nsec(entry_main));
1076 /* I use a star-compatible file flag attribute. */
1077 p = archive_entry_fflags_text(entry_main);
1078 if (p != NULL && *p != '\0')
1079 add_pax_attr(&(pax->pax_header), "SCHILY.fflags", p);
1081 /* I use star-compatible ACL attributes. */
1082 r = archive_entry_acl_text_l(entry_original,
1083 ARCHIVE_ENTRY_ACL_TYPE_ACCESS |
1084 ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID,
1085 &p, NULL, pax->sconv_utf8);
1087 if (errno == ENOMEM) {
1088 archive_set_error(&a->archive, ENOMEM,
1089 "Can't allocate memory for "
1091 return (ARCHIVE_FATAL);
1093 archive_set_error(&a->archive,
1094 ARCHIVE_ERRNO_FILE_FORMAT,
1095 "Can't translate ACL.access to UTF-8");
1097 } else if (p != NULL && *p != '\0') {
1098 add_pax_attr(&(pax->pax_header),
1099 "SCHILY.acl.access", p);
1101 r = archive_entry_acl_text_l(entry_original,
1102 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT |
1103 ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID,
1104 &p, NULL, pax->sconv_utf8);
1106 if (errno == ENOMEM) {
1107 archive_set_error(&a->archive, ENOMEM,
1108 "Can't allocate memory for "
1110 return (ARCHIVE_FATAL);
1112 archive_set_error(&a->archive,
1113 ARCHIVE_ERRNO_FILE_FORMAT,
1114 "Can't translate ACL.default to UTF-8");
1116 } else if (p != NULL && *p != '\0') {
1117 add_pax_attr(&(pax->pax_header),
1118 "SCHILY.acl.default", p);
1121 /* We use GNU-tar-compatible sparse attributes. */
1122 if (sparse_count > 0) {
1123 int64_t soffset, slength;
1125 add_pax_attr_int(&(pax->pax_header),
1126 "GNU.sparse.major", 1);
1127 add_pax_attr_int(&(pax->pax_header),
1128 "GNU.sparse.minor", 0);
1129 add_pax_attr(&(pax->pax_header),
1130 "GNU.sparse.name", entry_name.s);
1131 add_pax_attr_int(&(pax->pax_header),
1132 "GNU.sparse.realsize",
1133 archive_entry_size(entry_main));
1135 /* Rename the file name which will be used for
1136 * ustar header to a special name, which GNU
1137 * PAX Format 1.0 requires */
1138 archive_entry_set_pathname(entry_main,
1139 build_gnu_sparse_name(gnu_sparse_name,
1143 * - Make a sparse map, which will precede a file data.
1144 * - Get the total size of available data of sparse.
1146 archive_string_sprintf(&(pax->sparse_map), "%d\n",
1148 while (archive_entry_sparse_next(entry_main,
1149 &soffset, &slength) == ARCHIVE_OK) {
1150 archive_string_sprintf(&(pax->sparse_map),
1154 sparse_total += slength;
1155 if (sparse_list_add(pax, soffset, slength)
1157 archive_set_error(&a->archive,
1159 "Can't allocate memory");
1160 archive_entry_free(entry_main);
1161 archive_string_free(&entry_name);
1162 return (ARCHIVE_FATAL);
1167 /* Store extended attributes */
1168 if (archive_write_pax_header_xattrs(a, pax, entry_original)
1170 archive_entry_free(entry_main);
1171 archive_string_free(&entry_name);
1172 return (ARCHIVE_FATAL);
1176 /* Only regular files have data. */
1177 if (archive_entry_filetype(entry_main) != AE_IFREG)
1178 archive_entry_set_size(entry_main, 0);
1181 * Pax-restricted does not store data for hardlinks, in order
1182 * to improve compatibility with ustar.
1184 if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE &&
1186 archive_entry_set_size(entry_main, 0);
1189 * XXX Full pax interchange format does permit a hardlink
1190 * entry to have data associated with it. I'm not supporting
1191 * that here because the client expects me to tell them whether
1192 * or not this format expects data for hardlinks. If I
1193 * don't check here, then every pax archive will end up with
1194 * duplicated data for hardlinks. Someday, there may be
1195 * need to select this behavior, in which case the following
1196 * will need to be revisited. XXX
1198 if (hardlink != NULL)
1199 archive_entry_set_size(entry_main, 0);
1201 /* Save a real file size. */
1202 real_size = archive_entry_size(entry_main);
1204 * Overwrite a file size by the total size of sparse blocks and
1205 * the size of sparse map info. That file size is the length of
1206 * the data, which we will exactly store into an archive file.
1208 if (archive_strlen(&(pax->sparse_map))) {
1209 size_t mapsize = archive_strlen(&(pax->sparse_map));
1210 pax->sparse_map_padding = 0x1ff & (-(ssize_t)mapsize);
1211 archive_entry_set_size(entry_main,
1212 mapsize + pax->sparse_map_padding + sparse_total);
1215 /* Format 'ustar' header for main entry.
1217 * The trouble with file size: If the reader can't understand
1218 * the file size, they may not be able to locate the next
1219 * entry and the rest of the archive is toast. Pax-compliant
1220 * readers are supposed to ignore the file size in the main
1221 * header, so the question becomes how to maximize portability
1222 * for readers that don't support pax attribute extensions.
1223 * For maximum compatibility, I permit numeric extensions in
1224 * the main header so that the file size stored will always be
1225 * correct, even if it's in a format that only some
1226 * implementations understand. The technique used here is:
1228 * a) If possible, follow the standard exactly. This handles
1229 * files up to 8 gigabytes minus 1.
1231 * b) If that fails, try octal but omit the field terminator.
1232 * That handles files up to 64 gigabytes minus 1.
1234 * c) Otherwise, use base-256 extensions. That handles files
1235 * up to 2^63 in this implementation, with the potential to
1236 * go up to 2^94. That should hold us for a while. ;-)
1238 * The non-strict formatter uses similar logic for other
1239 * numeric fields, though they're less critical.
1241 if (__archive_write_format_header_ustar(a, ustarbuff, entry_main, -1, 0,
1242 NULL) == ARCHIVE_FATAL)
1243 return (ARCHIVE_FATAL);
1245 /* If we built any extended attributes, write that entry first. */
1246 if (archive_strlen(&(pax->pax_header)) > 0) {
1247 struct archive_entry *pax_attr_entry;
1252 pax_attr_entry = archive_entry_new2(&a->archive);
1254 archive_entry_set_pathname(pax_attr_entry,
1255 build_pax_attribute_name(pax_entry_name, p));
1256 archive_entry_set_size(pax_attr_entry,
1257 archive_strlen(&(pax->pax_header)));
1258 /* Copy uid/gid (but clip to ustar limits). */
1259 uid = archive_entry_uid(entry_main);
1261 uid = (1 << 18) - 1;
1262 archive_entry_set_uid(pax_attr_entry, uid);
1263 gid = archive_entry_gid(entry_main);
1265 gid = (1 << 18) - 1;
1266 archive_entry_set_gid(pax_attr_entry, gid);
1267 /* Copy mode over (but not setuid/setgid bits) */
1268 mode = archive_entry_mode(entry_main);
1278 archive_entry_set_mode(pax_attr_entry, mode);
1280 /* Copy uname/gname. */
1281 archive_entry_set_uname(pax_attr_entry,
1282 archive_entry_uname(entry_main));
1283 archive_entry_set_gname(pax_attr_entry,
1284 archive_entry_gname(entry_main));
1286 /* Copy mtime, but clip to ustar limits. */
1287 s = archive_entry_mtime(entry_main);
1288 if (s < 0) { s = 0; }
1289 if (s >= 0x7fffffff) { s = 0x7fffffff; }
1290 archive_entry_set_mtime(pax_attr_entry, s, 0);
1292 /* Standard ustar doesn't support atime. */
1293 archive_entry_set_atime(pax_attr_entry, 0, 0);
1295 /* Standard ustar doesn't support ctime. */
1296 archive_entry_set_ctime(pax_attr_entry, 0, 0);
1298 r = __archive_write_format_header_ustar(a, paxbuff,
1299 pax_attr_entry, 'x', 1, NULL);
1301 archive_entry_free(pax_attr_entry);
1303 /* Note that the 'x' header shouldn't ever fail to format */
1304 if (r < ARCHIVE_WARN) {
1305 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1306 "archive_write_pax_header: "
1307 "'x' header failed?! This can't happen.\n");
1308 return (ARCHIVE_FATAL);
1311 r = __archive_write_output(a, paxbuff, 512);
1312 if (r != ARCHIVE_OK) {
1313 sparse_list_clear(pax);
1314 pax->entry_bytes_remaining = 0;
1315 pax->entry_padding = 0;
1316 return (ARCHIVE_FATAL);
1319 pax->entry_bytes_remaining = archive_strlen(&(pax->pax_header));
1320 pax->entry_padding =
1321 0x1ff & (-(int64_t)pax->entry_bytes_remaining);
1323 r = __archive_write_output(a, pax->pax_header.s,
1324 archive_strlen(&(pax->pax_header)));
1325 if (r != ARCHIVE_OK) {
1326 /* If a write fails, we're pretty much toast. */
1327 return (ARCHIVE_FATAL);
1329 /* Pad out the end of the entry. */
1330 r = __archive_write_nulls(a, (size_t)pax->entry_padding);
1331 if (r != ARCHIVE_OK) {
1332 /* If a write fails, we're pretty much toast. */
1333 return (ARCHIVE_FATAL);
1335 pax->entry_bytes_remaining = pax->entry_padding = 0;
1338 /* Write the header for main entry. */
1339 r = __archive_write_output(a, ustarbuff, 512);
1340 if (r != ARCHIVE_OK)
1344 * Inform the client of the on-disk size we're using, so
1345 * they can avoid unnecessarily writing a body for something
1346 * that we're just going to ignore.
1348 archive_entry_set_size(entry_original, real_size);
1349 if (pax->sparse_list == NULL && real_size > 0) {
1350 /* This is not a sparse file but we handle its data as
1351 * a sparse block. */
1352 sparse_list_add(pax, 0, real_size);
1353 sparse_total = real_size;
1355 pax->entry_padding = 0x1ff & (-(int64_t)sparse_total);
1356 archive_entry_free(entry_main);
1357 archive_string_free(&entry_name);
1363 * We need a valid name for the regular 'ustar' entry. This routine
1364 * tries to hack something more-or-less reasonable.
1366 * The approach here tries to preserve leading dir names. We do so by
1367 * working with four sections:
1368 * 1) "prefix" directory names,
1369 * 2) "suffix" directory names,
1370 * 3) inserted dir name (optional),
1373 * These sections must satisfy the following requirements:
1374 * * Parts 1 & 2 together form an initial portion of the dir name.
1375 * * Part 3 is specified by the caller. (It should not contain a leading
1377 * * Part 4 forms an initial portion of the base filename.
1378 * * The filename must be <= 99 chars to fit the ustar 'name' field.
1379 * * Parts 2, 3, 4 together must be <= 99 chars to fit the ustar 'name' fld.
1380 * * Part 1 must be <= 155 chars to fit the ustar 'prefix' field.
1381 * * If the original name ends in a '/', the new name must also end in a '/'
1382 * * Trailing '/.' sequences may be stripped.
1384 * Note: Recall that the ustar format does not store the '/' separating
1385 * parts 1 & 2, but does store the '/' separating parts 2 & 3.
1388 build_ustar_entry_name(char *dest, const char *src, size_t src_length,
1391 const char *prefix, *prefix_end;
1392 const char *suffix, *suffix_end;
1393 const char *filename, *filename_end;
1395 int need_slash = 0; /* Was there a trailing slash? */
1396 size_t suffix_length = 99;
1397 size_t insert_length;
1399 /* Length of additional dir element to be added. */
1403 /* +2 here allows for '/' before and after the insert. */
1404 insert_length = strlen(insert) + 2;
1406 /* Step 0: Quick bailout in a common case. */
1407 if (src_length < 100 && insert == NULL) {
1408 strncpy(dest, src, src_length);
1409 dest[src_length] = '\0';
1413 /* Step 1: Locate filename and enforce the length restriction. */
1414 filename_end = src + src_length;
1415 /* Remove trailing '/' chars and '/.' pairs. */
1417 if (filename_end > src && filename_end[-1] == '/') {
1419 need_slash = 1; /* Remember to restore trailing '/'. */
1422 if (filename_end > src + 1 && filename_end[-1] == '.'
1423 && filename_end[-2] == '/') {
1425 need_slash = 1; /* "foo/." will become "foo/" */
1432 /* Find start of filename. */
1433 filename = filename_end - 1;
1434 while ((filename > src) && (*filename != '/'))
1436 if ((*filename == '/') && (filename < filename_end - 1))
1438 /* Adjust filename_end so that filename + insert fits in 99 chars. */
1439 suffix_length -= insert_length;
1440 if (filename_end > filename + suffix_length)
1441 filename_end = filename + suffix_length;
1442 /* Calculate max size for "suffix" section (#3 above). */
1443 suffix_length -= filename_end - filename;
1445 /* Step 2: Locate the "prefix" section of the dirname, including
1448 prefix_end = prefix + 155;
1449 if (prefix_end > filename)
1450 prefix_end = filename;
1451 while (prefix_end > prefix && *prefix_end != '/')
1453 if ((prefix_end < filename) && (*prefix_end == '/'))
1456 /* Step 3: Locate the "suffix" section of the dirname,
1457 * including trailing '/'. */
1458 suffix = prefix_end;
1459 suffix_end = suffix + suffix_length; /* Enforce limit. */
1460 if (suffix_end > filename)
1461 suffix_end = filename;
1462 if (suffix_end < suffix)
1463 suffix_end = suffix;
1464 while (suffix_end > suffix && *suffix_end != '/')
1466 if ((suffix_end < filename) && (*suffix_end == '/'))
1469 /* Step 4: Build the new name. */
1470 /* The OpenBSD strlcpy function is safer, but less portable. */
1471 /* Rather than maintain two versions, just use the strncpy version. */
1473 if (prefix_end > prefix) {
1474 strncpy(p, prefix, prefix_end - prefix);
1475 p += prefix_end - prefix;
1477 if (suffix_end > suffix) {
1478 strncpy(p, suffix, suffix_end - suffix);
1479 p += suffix_end - suffix;
1481 if (insert != NULL) {
1482 /* Note: assume insert does not have leading or trailing '/' */
1484 p += strlen(insert);
1487 strncpy(p, filename, filename_end - filename);
1488 p += filename_end - filename;
1497 * The ustar header for the pax extended attributes must have a
1498 * reasonable name: SUSv3 requires 'dirname'/PaxHeader.'pid'/'filename'
1499 * where 'pid' is the PID of the archiving process. Unfortunately,
1500 * that makes testing a pain since the output varies for each run,
1501 * so I'm sticking with the simpler 'dirname'/PaxHeader/'filename'
1502 * for now. (Someday, I'll make this settable. Then I can use the
1503 * SUS recommendation as default and test harnesses can override it
1504 * to get predictable results.)
1506 * Joerg Schilling has argued that this is unnecessary because, in
1507 * practice, if the pax extended attributes get extracted as regular
1508 * files, no one is going to bother reading those attributes to
1509 * manually restore them. Based on this, 'star' uses
1510 * /tmp/PaxHeader/'basename' as the ustar header name. This is a
1511 * tempting argument, in part because it's simpler than the SUSv3
1512 * recommendation, but I'm not entirely convinced. I'm also
1513 * uncomfortable with the fact that "/tmp" is a Unix-ism.
1515 * The following routine leverages build_ustar_entry_name() above and
1516 * so is simpler than you might think. It just needs to provide the
1517 * additional path element and handle a few pathological cases).
1520 build_pax_attribute_name(char *dest, const char *src)
1525 /* Handle the null filename case. */
1526 if (src == NULL || *src == '\0') {
1527 strcpy(dest, "PaxHeader/blank");
1531 /* Prune final '/' and other unwanted final elements. */
1532 p = src + strlen(src);
1534 /* Ends in "/", remove the '/' */
1535 if (p > src && p[-1] == '/') {
1539 /* Ends in "/.", remove the '.' */
1540 if (p > src + 1 && p[-1] == '.'
1548 /* Pathological case: After above, there was nothing left.
1549 * This includes "/." "/./." "/.//./." etc. */
1551 strcpy(dest, "/PaxHeader/rootdir");
1555 /* Convert unadorned "." into a suitable filename. */
1556 if (*src == '.' && p == src + 1) {
1557 strcpy(dest, "PaxHeader/currentdir");
1562 * TODO: Push this string into the 'pax' structure to avoid
1563 * recomputing it every time. That will also open the door
1564 * to having clients override it.
1566 #if HAVE_GETPID && 0 /* Disable this for now; see above comment. */
1567 sprintf(buff, "PaxHeader.%d", getpid());
1569 /* If the platform can't fetch the pid, don't include it. */
1570 strcpy(buff, "PaxHeader");
1572 /* General case: build a ustar-compatible name adding
1574 build_ustar_entry_name(dest, src, p - src, buff);
1580 * GNU PAX Format 1.0 requires the special name, which pattern is:
1581 * <dir>/GNUSparseFile.<pid>/<original file name>
1583 * This function is used for only Sparse file, a file type of which
1587 build_gnu_sparse_name(char *dest, const char *src)
1592 /* Handle the null filename case. */
1593 if (src == NULL || *src == '\0') {
1594 strcpy(dest, "GNUSparseFile/blank");
1598 /* Prune final '/' and other unwanted final elements. */
1599 p = src + strlen(src);
1601 /* Ends in "/", remove the '/' */
1602 if (p > src && p[-1] == '/') {
1606 /* Ends in "/.", remove the '.' */
1607 if (p > src + 1 && p[-1] == '.'
1615 #if HAVE_GETPID && 0 /* Disable this as pax attribute name. */
1616 sprintf(buff, "GNUSparseFile.%d", getpid());
1618 /* If the platform can't fetch the pid, don't include it. */
1619 strcpy(buff, "GNUSparseFile");
1621 /* General case: build a ustar-compatible name adding
1622 * "/GNUSparseFile/". */
1623 build_ustar_entry_name(dest, src, p - src, buff);
1628 /* Write two null blocks for the end of archive */
1630 archive_write_pax_close(struct archive_write *a)
1632 return (__archive_write_nulls(a, 512 * 2));
1636 archive_write_pax_free(struct archive_write *a)
1640 pax = (struct pax *)a->format_data;
1642 return (ARCHIVE_OK);
1644 archive_string_free(&pax->pax_header);
1645 archive_string_free(&pax->sparse_map);
1646 archive_string_free(&pax->l_url_encoded_name);
1647 sparse_list_clear(pax);
1649 a->format_data = NULL;
1650 return (ARCHIVE_OK);
1654 archive_write_pax_finish_entry(struct archive_write *a)
1660 pax = (struct pax *)a->format_data;
1661 remaining = pax->entry_bytes_remaining;
1662 if (remaining == 0) {
1663 while (pax->sparse_list) {
1664 struct sparse_block *sb;
1665 if (!pax->sparse_list->is_hole)
1666 remaining += pax->sparse_list->remaining;
1667 sb = pax->sparse_list->next;
1668 free(pax->sparse_list);
1669 pax->sparse_list = sb;
1672 ret = __archive_write_nulls(a, (size_t)(remaining + pax->entry_padding));
1673 pax->entry_bytes_remaining = pax->entry_padding = 0;
1678 archive_write_pax_data(struct archive_write *a, const void *buff, size_t s)
1685 pax = (struct pax *)a->format_data;
1688 * According to GNU PAX format 1.0, write a sparse map
1691 if (archive_strlen(&(pax->sparse_map))) {
1692 ret = __archive_write_output(a, pax->sparse_map.s,
1693 archive_strlen(&(pax->sparse_map)));
1694 if (ret != ARCHIVE_OK)
1696 ret = __archive_write_nulls(a, pax->sparse_map_padding);
1697 if (ret != ARCHIVE_OK)
1699 archive_string_empty(&(pax->sparse_map));
1704 const unsigned char *p;
1706 while (pax->sparse_list != NULL &&
1707 pax->sparse_list->remaining == 0) {
1708 struct sparse_block *sb = pax->sparse_list->next;
1709 free(pax->sparse_list);
1710 pax->sparse_list = sb;
1713 if (pax->sparse_list == NULL)
1716 p = ((const unsigned char *)buff) + total;
1718 if (ws > pax->sparse_list->remaining)
1719 ws = (size_t)pax->sparse_list->remaining;
1721 if (pax->sparse_list->is_hole) {
1722 /* Current block is hole thus we do not write
1724 pax->sparse_list->remaining -= ws;
1729 ret = __archive_write_output(a, p, ws);
1730 pax->sparse_list->remaining -= ws;
1732 if (ret != ARCHIVE_OK)
1739 has_non_ASCII(const char *_p)
1741 const unsigned char *p = (const unsigned char *)_p;
1745 while (*p != '\0' && *p < 128)
1747 return (*p != '\0');
1751 * Used by extended attribute support; encodes the name
1752 * so that there will be no '=' characters in the result.
1755 url_encode(const char *in)
1762 for (s = in; *s != '\0'; s++) {
1763 if (*s < 33 || *s > 126 || *s == '%' || *s == '=')
1769 out = (char *)malloc(out_len + 1);
1773 for (s = in, d = out; *s != '\0'; s++) {
1774 /* encode any non-printable ASCII character or '%' or '=' */
1775 if (*s < 33 || *s > 126 || *s == '%' || *s == '=') {
1776 /* URL encoding is '%' followed by two hex digits */
1778 *d++ = "0123456789ABCDEF"[0x0f & (*s >> 4)];
1779 *d++ = "0123456789ABCDEF"[0x0f & *s];
1789 * Encode a sequence of bytes into a C string using base-64 encoding.
1791 * Returns a null-terminated C string allocated with malloc(); caller
1792 * is responsible for freeing the result.
1795 base64_encode(const char *s, size_t len)
1797 static const char digits[64] =
1798 { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
1799 'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
1800 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
1801 't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
1806 /* 3 bytes becomes 4 chars, but round up and allow for trailing NUL */
1807 out = (char *)malloc((len * 4 + 2) / 3 + 1);
1812 /* Convert each group of 3 bytes into 4 characters. */
1814 v = (((int)s[0] << 16) & 0xff0000)
1815 | (((int)s[1] << 8) & 0xff00)
1816 | (((int)s[2]) & 0x00ff);
1819 *d++ = digits[(v >> 18) & 0x3f];
1820 *d++ = digits[(v >> 12) & 0x3f];
1821 *d++ = digits[(v >> 6) & 0x3f];
1822 *d++ = digits[(v) & 0x3f];
1824 /* Handle final group of 1 byte (2 chars) or 2 bytes (3 chars). */
1828 v = (((int)s[0] << 16) & 0xff0000);
1829 *d++ = digits[(v >> 18) & 0x3f];
1830 *d++ = digits[(v >> 12) & 0x3f];
1833 v = (((int)s[0] << 16) & 0xff0000)
1834 | (((int)s[1] << 8) & 0xff00);
1835 *d++ = digits[(v >> 18) & 0x3f];
1836 *d++ = digits[(v >> 12) & 0x3f];
1837 *d++ = digits[(v >> 6) & 0x3f];
1840 /* Add trailing NUL character so output is a valid C string. */
1846 sparse_list_clear(struct pax *pax)
1848 while (pax->sparse_list != NULL) {
1849 struct sparse_block *sb = pax->sparse_list;
1850 pax->sparse_list = sb->next;
1853 pax->sparse_tail = NULL;
1857 _sparse_list_add_block(struct pax *pax, int64_t offset, int64_t length,
1860 struct sparse_block *sb;
1862 sb = (struct sparse_block *)malloc(sizeof(*sb));
1864 return (ARCHIVE_FATAL);
1866 sb->is_hole = is_hole;
1867 sb->offset = offset;
1868 sb->remaining = length;
1869 if (pax->sparse_list == NULL || pax->sparse_tail == NULL)
1870 pax->sparse_list = pax->sparse_tail = sb;
1872 pax->sparse_tail->next = sb;
1873 pax->sparse_tail = sb;
1875 return (ARCHIVE_OK);
1879 sparse_list_add(struct pax *pax, int64_t offset, int64_t length)
1881 int64_t last_offset;
1884 if (pax->sparse_tail == NULL)
1887 last_offset = pax->sparse_tail->offset +
1888 pax->sparse_tail->remaining;
1890 if (last_offset < offset) {
1891 /* Add a hole block. */
1892 r = _sparse_list_add_block(pax, last_offset,
1893 offset - last_offset, 1);
1894 if (r != ARCHIVE_OK)
1897 /* Add data block. */
1898 return (_sparse_list_add_block(pax, offset, length, 0));