2 * Copyright (c) 2003-2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
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: src/lib/libarchive/archive_write_set_format_pax.c,v 1.26 2005/04/23 17:46:51 kientzle Exp $");
38 #include "archive_entry.h"
39 #include "archive_private.h"
42 uint64_t entry_bytes_remaining
;
43 uint64_t entry_padding
;
44 struct archive_string pax_header
;
48 static void add_pax_attr(struct archive_string
*, const char *key
,
50 static void add_pax_attr_int(struct archive_string
*,
51 const char *key
, int64_t value
);
52 static void add_pax_attr_time(struct archive_string
*,
53 const char *key
, int64_t sec
,
55 static void add_pax_attr_w(struct archive_string
*,
56 const char *key
, const wchar_t *wvalue
);
57 static int archive_write_pax_data(struct archive
*,
58 const void *, size_t);
59 static int archive_write_pax_finish(struct archive
*);
60 static int archive_write_pax_finish_entry(struct archive
*);
61 static int archive_write_pax_header(struct archive
*,
62 struct archive_entry
*);
63 static char *build_pax_attribute_name(char *dest
, const char *src
);
64 static char *build_ustar_entry_name(char *dest
, const char *src
, const char *insert
);
65 static char *format_int(char *dest
, int64_t);
66 static int write_nulls(struct archive
*, size_t);
69 * Set output format to 'restricted pax' format.
71 * This is the same as normal 'pax', but tries to suppress
72 * the pax header whenever possible. This is the default for
73 * bsdtar, for instance.
76 archive_write_set_format_pax_restricted(struct archive
*a
)
79 r
= archive_write_set_format_pax(a
);
80 a
->archive_format
= ARCHIVE_FORMAT_TAR_PAX_RESTRICTED
;
81 a
->archive_format_name
= "restricted POSIX pax interchange";
86 * Set output format to 'pax' format.
89 archive_write_set_format_pax(struct archive
*a
)
93 if (a
->format_finish
!= NULL
)
94 (a
->format_finish
)(a
);
96 pax
= malloc(sizeof(*pax
));
98 archive_set_error(a
, ENOMEM
, "Can't allocate pax data");
99 return (ARCHIVE_FATAL
);
101 memset(pax
, 0, sizeof(*pax
));
102 a
->format_data
= pax
;
104 a
->pad_uncompressed
= 1;
105 a
->format_write_header
= archive_write_pax_header
;
106 a
->format_write_data
= archive_write_pax_data
;
107 a
->format_finish
= archive_write_pax_finish
;
108 a
->format_finish_entry
= archive_write_pax_finish_entry
;
109 a
->archive_format
= ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE
;
110 a
->archive_format_name
= "POSIX pax interchange";
115 * Note: This code assumes that 'nanos' has the same sign as 'sec',
116 * which implies that sec=-1, nanos=200000000 represents -1.2 seconds
117 * and not -0.8 seconds. This is a pretty pedantic point, as we're
118 * unlikely to encounter many real files created before Jan 1, 1970,
119 * much less ones with timestamps recorded to sub-second resolution.
122 add_pax_attr_time(struct archive_string
*as
, const char *key
,
123 int64_t sec
, unsigned long nanos
)
128 * Note that each byte contributes fewer than 3 base-10
129 * digits, so this will always be big enough.
131 char tmp
[1 + 3*sizeof(sec
) + 1 + 3*sizeof(nanos
)];
133 tmp
[sizeof(tmp
) - 1] = 0;
134 t
= tmp
+ sizeof(tmp
) - 1;
136 /* Skip trailing zeros in the fractional part. */
137 for(digit
= 0, i
= 10; i
> 0 && digit
== 0; i
--) {
142 /* Only format the fraction if it's non-zero. */
145 *--t
= "0123456789"[digit
];
152 t
= format_int(t
, sec
);
154 add_pax_attr(as
, key
, t
);
158 format_int(char *t
, int64_t i
)
169 *--t
= "0123456789"[i
% 10];
177 add_pax_attr_int(struct archive_string
*as
, const char *key
, int64_t value
)
179 char tmp
[1 + 3 * sizeof(value
)];
181 tmp
[sizeof(tmp
) - 1] = 0;
182 add_pax_attr(as
, key
, format_int(tmp
+ sizeof(tmp
) - 1, value
));
186 add_pax_attr_w(struct archive_string
*as
, const char *key
, const wchar_t *wval
)
191 char *utf8_value
, *p
;
194 for (wp
= wval
; *wp
!= L
'\0'; ) {
198 else if (wc
<= 0x7ff)
200 else if (wc
<= 0xffff)
202 else if (wc
<= 0x1fffff)
204 else if (wc
<= 0x3ffffff)
206 else if (wc
<= 0x7fffffff)
208 /* Ignore larger values; UTF-8 can't encode them. */
211 utf8_value
= malloc(utf8len
+ 1);
212 for (wp
= wval
, p
= utf8_value
; *wp
!= L
'\0'; ) {
216 } else if (wc
<= 0x7ff) {
217 p
[0] = 0xc0 | ((wc
>> 6) & 0x1f);
218 p
[1] = 0x80 | (wc
& 0x3f);
220 } else if (wc
<= 0xffff) {
221 p
[0] = 0xe0 | ((wc
>> 12) & 0x0f);
222 p
[1] = 0x80 | ((wc
>> 6) & 0x3f);
223 p
[2] = 0x80 | (wc
& 0x3f);
225 } else if (wc
<= 0x1fffff) {
226 p
[0] = 0xf0 | ((wc
>> 18) & 0x07);
227 p
[1] = 0x80 | ((wc
>> 12) & 0x3f);
228 p
[2] = 0x80 | ((wc
>> 6) & 0x3f);
229 p
[3] = 0x80 | (wc
& 0x3f);
231 } else if (wc
<= 0x3ffffff) {
232 p
[0] = 0xf8 | ((wc
>> 24) & 0x03);
233 p
[1] = 0x80 | ((wc
>> 18) & 0x3f);
234 p
[2] = 0x80 | ((wc
>> 12) & 0x3f);
235 p
[3] = 0x80 | ((wc
>> 6) & 0x3f);
236 p
[4] = 0x80 | (wc
& 0x3f);
238 } else if (wc
<= 0x7fffffff) {
239 p
[0] = 0xfc | ((wc
>> 30) & 0x01);
240 p
[1] = 0x80 | ((wc
>> 24) & 0x3f);
241 p
[1] = 0x80 | ((wc
>> 18) & 0x3f);
242 p
[2] = 0x80 | ((wc
>> 12) & 0x3f);
243 p
[3] = 0x80 | ((wc
>> 6) & 0x3f);
244 p
[4] = 0x80 | (wc
& 0x3f);
247 /* Ignore larger values; UTF-8 can't encode them. */
250 add_pax_attr(as
, key
, utf8_value
);
255 * Add a key/value attribute to the pax header. This function handles
256 * the length field and various other syntactic requirements.
259 add_pax_attr(struct archive_string
*as
, const char *key
, const char *value
)
261 int digits
, i
, len
, next_ten
;
262 char tmp
[1 + 3 * sizeof(int)]; /* < 3 base-10 digits per byte */
265 * PAX attributes have the following layout:
266 * <len> <space> <key> <=> <value> <nl>
268 len
= 1 + strlen(key
) + 1 + strlen(value
) + 1;
271 * The <len> field includes the length of the <len> field, so
272 * computing the correct length is tricky. I start by
273 * counting the number of base-10 digits in 'len' and
274 * computing the next higher power of 10.
282 next_ten
= next_ten
* 10;
285 * For example, if string without the length field is 99
286 * chars, then adding the 2 digit length "99" will force the
287 * total length past 100, requiring an extra digit. The next
288 * statement adjusts for this effect.
290 if (len
+ digits
>= next_ten
)
293 /* Now, we have the right length so we can build the line. */
294 tmp
[sizeof(tmp
) - 1] = 0; /* Null-terminate the work area. */
295 archive_strcat(as
, format_int(tmp
+ sizeof(tmp
) - 1, len
+ digits
));
296 archive_strappend_char(as
, ' ');
297 archive_strcat(as
, key
);
298 archive_strappend_char(as
, '=');
299 archive_strcat(as
, value
);
300 archive_strappend_char(as
, '\n');
304 * TODO: Consider adding 'comment' and 'charset' fields to
305 * archive_entry so that clients can specify them. Also, consider
306 * adding generic key/value tags so clients can add arbitrary
310 archive_write_pax_header(struct archive
*a
,
311 struct archive_entry
*entry_original
)
313 struct archive_entry
*entry_main
;
314 const char *linkname
, *p
;
315 const char *hardlink
;
316 const wchar_t *wp
, *wp2
;
317 const char *suffix_start
;
318 int need_extension
, r
, ret
;
320 const struct stat
*st_main
, *st_original
;
324 char ustar_entry_name
[256];
325 char pax_entry_name
[256];
328 pax
= a
->format_data
;
331 st_original
= archive_entry_stat(entry_original
);
333 hardlink
= archive_entry_hardlink(entry_original
);
335 /* Make sure this is a type of entry that we can handle here */
336 if (hardlink
== NULL
) {
337 switch (st_original
->st_mode
& S_IFMT
) {
346 archive_set_error(a
, ARCHIVE_ERRNO_FILE_FORMAT
,
347 "tar format cannot archive socket");
348 return (ARCHIVE_WARN
);
350 archive_set_error(a
, ARCHIVE_ERRNO_FILE_FORMAT
,
351 "tar format cannot archive this (mode=0%lo)",
352 (unsigned long)st_original
->st_mode
);
353 return (ARCHIVE_WARN
);
357 /* Copy entry so we can modify it as needed. */
358 entry_main
= archive_entry_clone(entry_original
);
359 archive_string_empty(&(pax
->pax_header
)); /* Blank our work area. */
360 st_main
= archive_entry_stat(entry_main
);
363 * Determining whether or not the name is too big is ugly
364 * because of the rules for dividing names between 'name' and
365 * 'prefix' fields. Here, I pick out the longest possible
366 * suffix, then test whether the remaining prefix is too long.
368 wp
= archive_entry_pathname_w(entry_main
);
369 p
= archive_entry_pathname(entry_main
);
370 if (strlen(p
) <= 100) /* Short enough for just 'name' field */
371 suffix_start
= p
; /* Record a zero-length prefix */
373 /* Find the largest suffix that fits in 'name' field. */
374 suffix_start
= strchr(p
+ strlen(p
) - 100 - 1, '/');
376 /* Find non-ASCII character, if any. */
378 while (*wp2
!= L
'\0' && *wp2
< 128)
382 * If name is too long, or has non-ASCII characters, add
383 * 'path' to pax extended attrs.
385 if (suffix_start
== NULL
|| suffix_start
- p
> 155 || *wp2
!= L
'\0') {
386 add_pax_attr_w(&(pax
->pax_header
), "path", wp
);
387 archive_entry_set_pathname(entry_main
,
388 build_ustar_entry_name(ustar_entry_name
, p
, NULL
));
392 /* If link name is too long, add 'linkpath' to pax extended attrs. */
394 if (linkname
== NULL
)
395 linkname
= archive_entry_symlink(entry_main
);
397 if (linkname
!= NULL
&& strlen(linkname
) > 100) {
398 add_pax_attr(&(pax
->pax_header
), "linkpath", linkname
);
399 if (hardlink
!= NULL
)
400 archive_entry_set_hardlink(entry_main
,
401 "././@LongHardLink");
403 archive_entry_set_symlink(entry_main
,
408 /* If file size is too large, add 'size' to pax extended attrs. */
409 if (st_main
->st_size
>= (((int64_t)1) << 33)) {
410 add_pax_attr_int(&(pax
->pax_header
), "size", st_main
->st_size
);
414 /* If numeric GID is too large, add 'gid' to pax extended attrs. */
415 if (st_main
->st_gid
>= (1 << 18)) {
416 add_pax_attr_int(&(pax
->pax_header
), "gid", st_main
->st_gid
);
420 /* If group name is too large, add 'gname' to pax extended attrs. */
421 /* TODO: If gname has non-ASCII characters, use pax attribute. */
422 p
= archive_entry_gname(entry_main
);
423 if (p
!= NULL
&& strlen(p
) > 31) {
424 add_pax_attr(&(pax
->pax_header
), "gname", p
);
425 archive_entry_set_gname(entry_main
, NULL
);
429 /* If numeric UID is too large, add 'uid' to pax extended attrs. */
430 if (st_main
->st_uid
>= (1 << 18)) {
431 add_pax_attr_int(&(pax
->pax_header
), "uid", st_main
->st_uid
);
435 /* If user name is too large, add 'uname' to pax extended attrs. */
436 /* TODO: If uname has non-ASCII characters, use pax attribute. */
437 p
= archive_entry_uname(entry_main
);
438 if (p
!= NULL
&& strlen(p
) > 31) {
439 add_pax_attr(&(pax
->pax_header
), "uname", p
);
440 archive_entry_set_uname(entry_main
, NULL
);
445 * POSIX/SUSv3 doesn't provide a standard key for large device
446 * numbers. I use the same keys here that Joerg Schilling
447 * used for 'star.' (Which, somewhat confusingly, are called
448 * "devXXX" even though they code "rdev" values.) No doubt,
449 * other implementations use other keys. Note that there's no
450 * reason we can't write the same information into a number of
453 * Of course, this is only needed for block or char device entries.
455 if (S_ISBLK(st_main
->st_mode
) ||
456 S_ISCHR(st_main
->st_mode
)) {
458 * If rdevmajor is too large, add 'SCHILY.devmajor' to
459 * extended attributes.
461 dev_t rdevmajor
, rdevminor
;
462 rdevmajor
= major(st_main
->st_rdev
);
463 rdevminor
= minor(st_main
->st_rdev
);
464 if (rdevmajor
>= (1 << 18)) {
465 add_pax_attr_int(&(pax
->pax_header
), "SCHILY.devmajor",
468 * Non-strict formatting below means we don't
469 * have to truncate here. Not truncating improves
470 * the chance that some more modern tar archivers
471 * (such as GNU tar 1.13) can restore the full
472 * value even if they don't understand the pax
473 * extended attributes. See my rant below about
474 * file size fields for additional details.
476 /* archive_entry_set_rdevmajor(entry_main,
477 rdevmajor & ((1 << 18) - 1)); */
482 * If devminor is too large, add 'SCHILY.devminor' to
483 * extended attributes.
485 if (rdevminor
>= (1 << 18)) {
486 add_pax_attr_int(&(pax
->pax_header
), "SCHILY.devminor",
488 /* Truncation is not necessary here, either. */
489 /* archive_entry_set_rdevminor(entry_main,
490 rdevminor & ((1 << 18) - 1)); */
496 * Technically, the mtime field in the ustar header can
497 * support 33 bits, but many platforms use signed 32-bit time
498 * values. The cutoff of 0x7fffffff here is a compromise.
499 * Yes, this check is duplicated just below; this helps to
500 * avoid writing an mtime attribute just to handle a
501 * high-resolution timestamp in "restricted pax" mode.
503 if (!need_extension
&&
504 ((st_main
->st_mtime
< 0) || (st_main
->st_mtime
>= 0x7fffffff)))
507 /* I use a star-compatible file flag attribute. */
508 p
= archive_entry_fflags_text(entry_main
);
509 if (!need_extension
&& p
!= NULL
&& *p
!= '\0')
512 /* If there are non-trivial ACL entries, we need an extension. */
513 if (!need_extension
&& archive_entry_acl_count(entry_original
,
514 ARCHIVE_ENTRY_ACL_TYPE_ACCESS
) > 0)
517 /* If there are non-trivial ACL entries, we need an extension. */
518 if (!need_extension
&& archive_entry_acl_count(entry_original
,
519 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
) > 0)
523 * The following items are handled differently in "pax
524 * restricted" format. In particular, in "pax restricted"
525 * format they won't be added unless need_extension is
526 * already set (we're already generating an extended header, so
527 * may as well include these).
529 if (a
->archive_format
!= ARCHIVE_FORMAT_TAR_PAX_RESTRICTED
||
532 if (st_main
->st_mtime
< 0 ||
533 st_main
->st_mtime
>= 0x7fffffff ||
534 ARCHIVE_STAT_MTIME_NANOS(st_main
) != 0)
535 add_pax_attr_time(&(pax
->pax_header
), "mtime",
537 ARCHIVE_STAT_MTIME_NANOS(st_main
));
539 if (st_main
->st_ctime
!= 0 ||
540 ARCHIVE_STAT_CTIME_NANOS(st_main
) != 0)
541 add_pax_attr_time(&(pax
->pax_header
), "ctime",
543 ARCHIVE_STAT_CTIME_NANOS(st_main
));
545 if (st_main
->st_atime
!= 0 ||
546 ARCHIVE_STAT_ATIME_NANOS(st_main
) != 0)
547 add_pax_attr_time(&(pax
->pax_header
), "atime",
549 ARCHIVE_STAT_ATIME_NANOS(st_main
));
551 /* I use a star-compatible file flag attribute. */
552 p
= archive_entry_fflags_text(entry_main
);
553 if (p
!= NULL
&& *p
!= '\0')
554 add_pax_attr(&(pax
->pax_header
), "SCHILY.fflags", p
);
556 /* I use star-compatible ACL attributes. */
557 wp
= archive_entry_acl_text_w(entry_original
,
558 ARCHIVE_ENTRY_ACL_TYPE_ACCESS
|
559 ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID
);
560 if (wp
!= NULL
&& *wp
!= L
'\0')
561 add_pax_attr_w(&(pax
->pax_header
),
562 "SCHILY.acl.access", wp
);
563 wp
= archive_entry_acl_text_w(entry_original
,
564 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
|
565 ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID
);
566 if (wp
!= NULL
&& *wp
!= L
'\0')
567 add_pax_attr_w(&(pax
->pax_header
),
568 "SCHILY.acl.default", wp
);
570 /* Include star-compatible metadata info. */
571 /* Note: "SCHILY.dev{major,minor}" are NOT the
572 * major/minor portions of "SCHILY.dev". */
573 add_pax_attr_int(&(pax
->pax_header
), "SCHILY.dev",
575 add_pax_attr_int(&(pax
->pax_header
), "SCHILY.ino",
577 add_pax_attr_int(&(pax
->pax_header
), "SCHILY.nlink",
581 /* Only regular files have data. */
582 if (!S_ISREG(archive_entry_mode(entry_main
)))
583 archive_entry_set_size(entry_main
, 0);
586 * Pax-restricted does not store data for hardlinks, in order
587 * to improve compatibility with ustar.
589 if (a
->archive_format
!= ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE
&&
591 archive_entry_set_size(entry_main
, 0);
594 * XXX Full pax interchange format does permit a hardlink
595 * entry to have data associated with it. I'm not supporting
596 * that here because the client expects me to tell them whether
597 * or not this format expects data for hardlinks. If I
598 * don't check here, then every pax archive will end up with
599 * duplicated data for hardlinks. Someday, there may be
600 * need to select this behavior, in which case the following
601 * will need to be revisited. XXX
603 if (hardlink
!= NULL
)
604 archive_entry_set_size(entry_main
, 0);
606 /* Format 'ustar' header for main entry.
608 * The trouble with file size: If the reader can't understand
609 * the file size, they may not be able to locate the next
610 * entry and the rest of the archive is toast. Pax-compliant
611 * readers are supposed to ignore the file size in the main
612 * header, so the question becomes how to maximize portability
613 * for readers that don't support pax attribute extensions.
614 * For maximum compatibility, I permit numeric extensions in
615 * the main header so that the file size stored will always be
616 * correct, even if it's in a format that only some
617 * implementations understand. The technique used here is:
619 * a) If possible, follow the standard exactly. This handles
620 * files up to 8 gigabytes minus 1.
622 * b) If that fails, try octal but omit the field terminator.
623 * That handles files up to 64 gigabytes minus 1.
625 * c) Otherwise, use base-256 extensions. That handles files
626 * up to 2^63 in this implementation, with the potential to
627 * go up to 2^94. That should hold us for a while. ;-)
629 * The non-strict formatter uses similar logic for other
630 * numeric fields, though they're less critical.
632 __archive_write_format_header_ustar(a
, ustarbuff
, entry_main
, -1, 0);
634 /* If we built any extended attributes, write that entry first. */
636 if (archive_strlen(&(pax
->pax_header
)) > 0) {
638 struct archive_entry
*pax_attr_entry
;
640 memset(&st
, 0, sizeof(st
));
641 pax_attr_entry
= archive_entry_new();
642 p
= archive_entry_pathname(entry_main
);
643 archive_entry_set_pathname(pax_attr_entry
,
644 build_pax_attribute_name(pax_entry_name
, p
));
645 st
.st_size
= archive_strlen(&(pax
->pax_header
));
646 st
.st_uid
= st_main
->st_uid
;
647 if (st
.st_uid
>= 1 << 18)
648 st
.st_uid
= (1 << 18) - 1;
649 st
.st_gid
= st_main
->st_gid
;
650 if (st
.st_gid
>= 1 << 18)
651 st
.st_gid
= (1 << 18) - 1;
652 st
.st_mode
= st_main
->st_mode
;
653 archive_entry_copy_stat(pax_attr_entry
, &st
);
655 archive_entry_set_uname(pax_attr_entry
,
656 archive_entry_uname(entry_main
));
657 archive_entry_set_gname(pax_attr_entry
,
658 archive_entry_gname(entry_main
));
660 ret
= __archive_write_format_header_ustar(a
, paxbuff
,
661 pax_attr_entry
, 'x', 1);
663 archive_entry_free(pax_attr_entry
);
665 /* Note that the 'x' header shouldn't ever fail to format */
667 const char *msg
= "archive_write_pax_header: "
668 "'x' header failed?! This can't happen.\n";
669 write(2, msg
, strlen(msg
));
672 r
= (a
->compression_write
)(a
, paxbuff
, 512);
673 if (r
!= ARCHIVE_OK
) {
674 pax
->entry_bytes_remaining
= 0;
675 pax
->entry_padding
= 0;
676 return (ARCHIVE_FATAL
);
679 pax
->entry_bytes_remaining
= archive_strlen(&(pax
->pax_header
));
680 pax
->entry_padding
= 0x1ff & (- pax
->entry_bytes_remaining
);
682 r
= (a
->compression_write
)(a
, pax
->pax_header
.s
,
683 archive_strlen(&(pax
->pax_header
)));
684 if (r
!= ARCHIVE_OK
) {
685 /* If a write fails, we're pretty much toast. */
686 return (ARCHIVE_FATAL
);
688 /* Pad out the end of the entry. */
689 r
= write_nulls(a
, pax
->entry_padding
);
690 if (r
!= ARCHIVE_OK
) {
691 /* If a write fails, we're pretty much toast. */
692 return (ARCHIVE_FATAL
);
694 pax
->entry_bytes_remaining
= pax
->entry_padding
= 0;
697 /* Write the header for main entry. */
698 r
= (a
->compression_write
)(a
, ustarbuff
, 512);
703 * Inform the client of the on-disk size we're using, so
704 * they can avoid unnecessarily writing a body for something
705 * that we're just going to ignore.
707 archive_entry_set_size(entry_original
, archive_entry_size(entry_main
));
708 pax
->entry_bytes_remaining
= archive_entry_size(entry_main
);
709 pax
->entry_padding
= 0x1ff & (- pax
->entry_bytes_remaining
);
710 archive_entry_free(entry_main
);
716 * We need a valid name for the regular 'ustar' entry. This routine
717 * tries to hack something more-or-less reasonable.
719 * The approach here tries to preserve leading dir names. We do so by
720 * working with four sections:
721 * 1) "prefix" directory names,
722 * 2) "suffix" directory names,
723 * 3) inserted dir name (optional),
726 * These sections must satisfy the following requirements:
727 * * Parts 1 & 2 together form an initial portion of the dir name.
728 * * Part 3 is specified by the caller. (It should not contain a leading
730 * * Part 4 forms an initial portion of the base filename.
731 * * The filename must be <= 99 chars to fit the ustar 'name' field.
732 * * Parts 2, 3, 4 together must be <= 99 chars to fit the ustar 'name' fld.
733 * * Part 1 must be <= 155 chars to fit the ustar 'prefix' field.
734 * * If the original name ends in a '/', the new name must also end in a '/'
735 * * Trailing '/.' sequences may be stripped.
737 * Note: Recall that the ustar format does not store the '/' separating
738 * parts 1 & 2, but does store the '/' separating parts 2 & 3.
741 build_ustar_entry_name(char *dest
, const char *src
, const char *insert
)
743 const char *prefix
, *prefix_end
;
744 const char *suffix
, *suffix_end
;
745 const char *filename
, *filename_end
;
748 int need_slash
= 0; /* Was there a trailing slash? */
749 size_t suffix_length
= 99;
752 /* Length of additional dir element to be added. */
756 /* +2 here allows for '/' before and after the insert. */
757 insert_length
= strlen(insert
) + 2;
759 /* Step 0: Quick bailout in a common case. */
761 if (s
< 100 && insert
== NULL
) {
766 /* Step 1: Locate filename and enforce the length restriction. */
767 filename_end
= src
+ s
;
768 /* Remove trailing '/' chars and '/.' pairs. */
770 if (filename_end
> src
&& filename_end
[-1] == '/') {
772 need_slash
= 1; /* Remember to restore trailing '/'. */
775 if (filename_end
> src
+ 1 && filename_end
[-1] == '.'
776 && filename_end
[-2] == '/') {
778 need_slash
= 1; /* "foo/." will become "foo/" */
785 /* Find start of filename. */
786 filename
= filename_end
- 1;
787 while ((filename
> src
) && (*filename
!= '/'))
789 if ((*filename
== '/') && (filename
< filename_end
- 1))
791 /* Adjust filename_end so that filename + insert fits in 99 chars. */
792 suffix_length
-= insert_length
;
793 if (filename_end
> filename
+ suffix_length
)
794 filename_end
= filename
+ suffix_length
;
795 /* Calculate max size for "suffix" section (#3 above). */
796 suffix_length
-= filename_end
- filename
;
798 /* Step 2: Locate the "prefix" section of the dirname, including
801 prefix_end
= prefix
+ 155;
802 if (prefix_end
> filename
)
803 prefix_end
= filename
;
804 while (prefix_end
> prefix
&& *prefix_end
!= '/')
806 if ((prefix_end
< filename
) && (*prefix_end
== '/'))
809 /* Step 3: Locate the "suffix" section of the dirname,
810 * including trailing '/'. */
812 suffix_end
= suffix
+ suffix_length
; /* Enforce limit. */
813 if (suffix_end
> filename
)
814 suffix_end
= filename
;
815 if (suffix_end
< suffix
)
817 while (suffix_end
> suffix
&& *suffix_end
!= '/')
819 if ((suffix_end
< filename
) && (*suffix_end
== '/'))
822 /* Step 4: Build the new name. */
823 /* The OpenBSD strlcpy function is safer, but less portable. */
824 /* Rather than maintain two versions, just use the strncpy version. */
826 if (prefix_end
> prefix
) {
827 strncpy(p
, prefix
, prefix_end
- prefix
);
828 p
+= prefix_end
- prefix
;
830 if (suffix_end
> suffix
) {
831 strncpy(p
, suffix
, suffix_end
- suffix
);
832 p
+= suffix_end
- suffix
;
834 if (insert
!= NULL
) {
835 if (prefix_end
> prefix
|| suffix_end
> suffix
)
841 strncpy(p
, filename
, filename_end
- filename
);
842 p
+= filename_end
- filename
;
851 * The ustar header for the pax extended attributes must have a
852 * reasonable name: SUSv3 suggests 'dirname'/PaxHeader/'filename'
854 * Joerg Schiling has argued that this is unnecessary because, in practice,
855 * if the pax extended attributes get extracted as regular files, noone is
856 * going to bother reading those attributes to manually restore them.
857 * Based on this, 'star' uses /tmp/PaxHeader/'basename' as the ustar header
858 * name. This is a tempting argument, but I'm not entirely convinced.
859 * I'm also uncomfortable with the fact that "/tmp" is a Unix-ism.
861 * The following routine implements the SUSv3 recommendation, and is
862 * much simpler because build_ustar_entry_name() above already does
863 * most of the work (we just need to give it an extra path element to
864 * insert and handle a few pathological cases).
867 build_pax_attribute_name(char *dest
, const char *src
)
871 /* Handle the null filename case. */
872 if (src
== NULL
|| *src
== '\0') {
873 strcpy(dest
, "PaxHeader/blank");
877 /* Prune final '/' and other unwanted final elements. */
878 p
= dest
+ strlen(dest
);
880 /* Ends in "/", remove the '/' */
881 if (p
> dest
&& p
[-1] == '/') {
885 /* Ends in "/.", remove the '.' */
886 if (p
> dest
+ 1 && p
[-1] == '.'
894 /* Pathological case: After above, there was nothing left. */
896 strcpy(dest
, "/PaxHeader/rootdir");
900 /* Convert unadorned "." into "dot" */
901 if (*src
== '.' && src
[1] == '\0') {
902 strcpy(dest
, "PaxHeader/currentdir");
906 /* General case: build a ustar-compatible name adding "/PaxHeader/". */
907 build_ustar_entry_name(dest
, src
, "PaxHeader");
912 /* Write two null blocks for the end of archive */
914 archive_write_pax_finish(struct archive
*a
)
920 pax
= a
->format_data
;
921 if (pax
->written
&& a
->compression_write
!= NULL
)
922 r
= write_nulls(a
, 512 * 2);
923 archive_string_free(&pax
->pax_header
);
925 a
->format_data
= NULL
;
930 archive_write_pax_finish_entry(struct archive
*a
)
935 pax
= a
->format_data
;
936 ret
= write_nulls(a
, pax
->entry_bytes_remaining
+ pax
->entry_padding
);
937 pax
->entry_bytes_remaining
= pax
->entry_padding
= 0;
942 write_nulls(struct archive
*a
, size_t padding
)
946 while (padding
> 0) {
947 to_write
= padding
< a
->null_length
? padding
: a
->null_length
;
948 ret
= (a
->compression_write
)(a
, a
->nulls
, to_write
);
949 if (ret
!= ARCHIVE_OK
)
957 archive_write_pax_data(struct archive
*a
, const void *buff
, size_t s
)
962 pax
= a
->format_data
;
964 if (s
> pax
->entry_bytes_remaining
)
965 s
= pax
->entry_bytes_remaining
;
967 ret
= (a
->compression_write
)(a
, buff
, s
);
968 pax
->entry_bytes_remaining
-= s
;