1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3232153Smm * Copyright (c) 2010-2012 Michihiro NAKAJIMA 4228753Smm * All rights reserved. 5228753Smm * 6228753Smm * Redistribution and use in source and binary forms, with or without 7228753Smm * modification, are permitted provided that the following conditions 8228753Smm * are met: 9228753Smm * 1. Redistributions of source code must retain the above copyright 10228753Smm * notice, this list of conditions and the following disclaimer. 11228753Smm * 2. Redistributions in binary form must reproduce the above copyright 12228753Smm * notice, this list of conditions and the following disclaimer in the 13228753Smm * documentation and/or other materials provided with the distribution. 14228753Smm * 15228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25228753Smm */ 26228753Smm 27228753Smm#include "archive_platform.h" 28228763Smm__FBSDID("$FreeBSD$"); 29228753Smm 30228753Smm#ifdef HAVE_ERRNO_H 31228753Smm#include <errno.h> 32228753Smm#endif 33228753Smm#ifdef HAVE_STDLIB_H 34228753Smm#include <stdlib.h> 35228753Smm#endif 36228753Smm#ifdef HAVE_STRING_H 37228753Smm#include <string.h> 38228753Smm#endif 39228753Smm 40228753Smm#include "archive.h" 41228753Smm#include "archive_entry.h" 42232153Smm#include "archive_entry_locale.h" 43228753Smm#include "archive_private.h" 44228753Smm#include "archive_write_private.h" 45228753Smm 46232153Smmstruct sparse_block { 47232153Smm struct sparse_block *next; 48232153Smm int is_hole; 49232153Smm uint64_t offset; 50232153Smm uint64_t remaining; 51232153Smm}; 52232153Smm 53228753Smmstruct pax { 54228753Smm uint64_t entry_bytes_remaining; 55228753Smm uint64_t entry_padding; 56232153Smm struct archive_string l_url_encoded_name; 57228753Smm struct archive_string pax_header; 58232153Smm struct archive_string sparse_map; 59232153Smm size_t sparse_map_padding; 60232153Smm struct sparse_block *sparse_list; 61232153Smm struct sparse_block *sparse_tail; 62232153Smm struct archive_string_conv *sconv_utf8; 63232153Smm int opt_binary; 64228753Smm}; 65228753Smm 66228753Smmstatic void add_pax_attr(struct archive_string *, const char *key, 67228753Smm const char *value); 68228753Smmstatic void add_pax_attr_int(struct archive_string *, 69228753Smm const char *key, int64_t value); 70228753Smmstatic void add_pax_attr_time(struct archive_string *, 71228753Smm const char *key, int64_t sec, 72228753Smm unsigned long nanos); 73228753Smmstatic ssize_t archive_write_pax_data(struct archive_write *, 74228753Smm const void *, size_t); 75232153Smmstatic int archive_write_pax_close(struct archive_write *); 76232153Smmstatic int archive_write_pax_free(struct archive_write *); 77228753Smmstatic int archive_write_pax_finish_entry(struct archive_write *); 78228753Smmstatic int archive_write_pax_header(struct archive_write *, 79228753Smm struct archive_entry *); 80232153Smmstatic int archive_write_pax_options(struct archive_write *, 81232153Smm const char *, const char *); 82228753Smmstatic char *base64_encode(const char *src, size_t len); 83232153Smmstatic char *build_gnu_sparse_name(char *dest, const char *src); 84228753Smmstatic char *build_pax_attribute_name(char *dest, const char *src); 85228753Smmstatic char *build_ustar_entry_name(char *dest, const char *src, 86228753Smm size_t src_length, const char *insert); 87228753Smmstatic char *format_int(char *dest, int64_t); 88232153Smmstatic int has_non_ASCII(const char *); 89232153Smmstatic void sparse_list_clear(struct pax *); 90232153Smmstatic int sparse_list_add(struct pax *, int64_t, int64_t); 91228753Smmstatic char *url_encode(const char *in); 92228753Smm 93228753Smm/* 94228753Smm * Set output format to 'restricted pax' format. 95228753Smm * 96228753Smm * This is the same as normal 'pax', but tries to suppress 97228753Smm * the pax header whenever possible. This is the default for 98228753Smm * bsdtar, for instance. 99228753Smm */ 100228753Smmint 101228753Smmarchive_write_set_format_pax_restricted(struct archive *_a) 102228753Smm{ 103228753Smm struct archive_write *a = (struct archive_write *)_a; 104228753Smm int r; 105232153Smm 106232153Smm archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, 107232153Smm ARCHIVE_STATE_NEW, "archive_write_set_format_pax_restricted"); 108232153Smm 109228753Smm r = archive_write_set_format_pax(&a->archive); 110228753Smm a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED; 111228753Smm a->archive.archive_format_name = "restricted POSIX pax interchange"; 112228753Smm return (r); 113228753Smm} 114228753Smm 115228753Smm/* 116228753Smm * Set output format to 'pax' format. 117228753Smm */ 118228753Smmint 119228753Smmarchive_write_set_format_pax(struct archive *_a) 120228753Smm{ 121228753Smm struct archive_write *a = (struct archive_write *)_a; 122228753Smm struct pax *pax; 123228753Smm 124232153Smm archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, 125232153Smm ARCHIVE_STATE_NEW, "archive_write_set_format_pax"); 126228753Smm 127232153Smm if (a->format_free != NULL) 128232153Smm (a->format_free)(a); 129232153Smm 130228753Smm pax = (struct pax *)malloc(sizeof(*pax)); 131228753Smm if (pax == NULL) { 132232153Smm archive_set_error(&a->archive, ENOMEM, 133232153Smm "Can't allocate pax data"); 134228753Smm return (ARCHIVE_FATAL); 135228753Smm } 136228753Smm memset(pax, 0, sizeof(*pax)); 137228753Smm a->format_data = pax; 138228753Smm a->format_name = "pax"; 139232153Smm a->format_options = archive_write_pax_options; 140228753Smm a->format_write_header = archive_write_pax_header; 141228753Smm a->format_write_data = archive_write_pax_data; 142232153Smm a->format_close = archive_write_pax_close; 143232153Smm a->format_free = archive_write_pax_free; 144228753Smm a->format_finish_entry = archive_write_pax_finish_entry; 145228753Smm a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; 146228753Smm a->archive.archive_format_name = "POSIX pax interchange"; 147228753Smm return (ARCHIVE_OK); 148228753Smm} 149228753Smm 150232153Smmstatic int 151232153Smmarchive_write_pax_options(struct archive_write *a, const char *key, 152232153Smm const char *val) 153232153Smm{ 154232153Smm struct pax *pax = (struct pax *)a->format_data; 155232153Smm int ret = ARCHIVE_FAILED; 156232153Smm 157232153Smm if (strcmp(key, "hdrcharset") == 0) { 158232153Smm /* 159232153Smm * The character-set we can use are defined in 160232153Smm * IEEE Std 1003.1-2001 161232153Smm */ 162232153Smm if (val == NULL || val[0] == 0) 163232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 164232153Smm "pax: hdrcharset option needs a character-set name"); 165232153Smm else if (strcmp(val, "BINARY") == 0 || 166232153Smm strcmp(val, "binary") == 0) { 167232153Smm /* 168232153Smm * Specify binary mode. We will not convert 169232153Smm * filenames, uname and gname to any charsets. 170232153Smm */ 171232153Smm pax->opt_binary = 1; 172232153Smm ret = ARCHIVE_OK; 173232153Smm } else if (strcmp(val, "UTF-8") == 0) { 174232153Smm /* 175232153Smm * Specify UTF-8 character-set to be used for 176232153Smm * filenames. This is almost the test that 177232153Smm * running platform supports the string conversion. 178232153Smm * Especially libarchive_test needs this trick for 179232153Smm * its test. 180232153Smm */ 181232153Smm pax->sconv_utf8 = archive_string_conversion_to_charset( 182232153Smm &(a->archive), "UTF-8", 0); 183232153Smm if (pax->sconv_utf8 == NULL) 184232153Smm ret = ARCHIVE_FATAL; 185232153Smm else 186232153Smm ret = ARCHIVE_OK; 187232153Smm } else 188232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 189232153Smm "pax: invalid charset name"); 190232153Smm return (ret); 191232153Smm } 192232153Smm 193232153Smm /* Note: The "warn" return is just to inform the options 194232153Smm * supervisor that we didn't handle it. It will generate 195232153Smm * a suitable error if no one used this option. */ 196232153Smm return (ARCHIVE_WARN); 197232153Smm} 198232153Smm 199228753Smm/* 200228753Smm * Note: This code assumes that 'nanos' has the same sign as 'sec', 201228753Smm * which implies that sec=-1, nanos=200000000 represents -1.2 seconds 202228753Smm * and not -0.8 seconds. This is a pretty pedantic point, as we're 203228753Smm * unlikely to encounter many real files created before Jan 1, 1970, 204228753Smm * much less ones with timestamps recorded to sub-second resolution. 205228753Smm */ 206228753Smmstatic void 207228753Smmadd_pax_attr_time(struct archive_string *as, const char *key, 208228753Smm int64_t sec, unsigned long nanos) 209228753Smm{ 210228753Smm int digit, i; 211228753Smm char *t; 212228753Smm /* 213228753Smm * Note that each byte contributes fewer than 3 base-10 214228753Smm * digits, so this will always be big enough. 215228753Smm */ 216228753Smm char tmp[1 + 3*sizeof(sec) + 1 + 3*sizeof(nanos)]; 217228753Smm 218228753Smm tmp[sizeof(tmp) - 1] = 0; 219228753Smm t = tmp + sizeof(tmp) - 1; 220228753Smm 221228753Smm /* Skip trailing zeros in the fractional part. */ 222228753Smm for (digit = 0, i = 10; i > 0 && digit == 0; i--) { 223228753Smm digit = nanos % 10; 224228753Smm nanos /= 10; 225228753Smm } 226228753Smm 227228753Smm /* Only format the fraction if it's non-zero. */ 228228753Smm if (i > 0) { 229228753Smm while (i > 0) { 230228753Smm *--t = "0123456789"[digit]; 231228753Smm digit = nanos % 10; 232228753Smm nanos /= 10; 233228753Smm i--; 234228753Smm } 235228753Smm *--t = '.'; 236228753Smm } 237228753Smm t = format_int(t, sec); 238228753Smm 239228753Smm add_pax_attr(as, key, t); 240228753Smm} 241228753Smm 242228753Smmstatic char * 243228753Smmformat_int(char *t, int64_t i) 244228753Smm{ 245232153Smm uint64_t ui; 246228753Smm 247232153Smm if (i < 0) 248232153Smm ui = (i == INT64_MIN) ? (uint64_t)(INT64_MAX) + 1 : (uint64_t)(-i); 249232153Smm else 250232153Smm ui = i; 251228753Smm 252228753Smm do { 253232153Smm *--t = "0123456789"[ui % 10]; 254232153Smm } while (ui /= 10); 255232153Smm if (i < 0) 256228753Smm *--t = '-'; 257228753Smm return (t); 258228753Smm} 259228753Smm 260228753Smmstatic void 261228753Smmadd_pax_attr_int(struct archive_string *as, const char *key, int64_t value) 262228753Smm{ 263228753Smm char tmp[1 + 3 * sizeof(value)]; 264228753Smm 265228753Smm tmp[sizeof(tmp) - 1] = 0; 266228753Smm add_pax_attr(as, key, format_int(tmp + sizeof(tmp) - 1, value)); 267228753Smm} 268228753Smm 269228753Smm/* 270228753Smm * Add a key/value attribute to the pax header. This function handles 271228753Smm * the length field and various other syntactic requirements. 272228753Smm */ 273228753Smmstatic void 274228753Smmadd_pax_attr(struct archive_string *as, const char *key, const char *value) 275228753Smm{ 276228753Smm int digits, i, len, next_ten; 277228753Smm char tmp[1 + 3 * sizeof(int)]; /* < 3 base-10 digits per byte */ 278228753Smm 279228753Smm /*- 280228753Smm * PAX attributes have the following layout: 281228753Smm * <len> <space> <key> <=> <value> <nl> 282228753Smm */ 283228753Smm len = 1 + (int)strlen(key) + 1 + (int)strlen(value) + 1; 284228753Smm 285228753Smm /* 286228753Smm * The <len> field includes the length of the <len> field, so 287228753Smm * computing the correct length is tricky. I start by 288228753Smm * counting the number of base-10 digits in 'len' and 289228753Smm * computing the next higher power of 10. 290228753Smm */ 291228753Smm next_ten = 1; 292228753Smm digits = 0; 293228753Smm i = len; 294228753Smm while (i > 0) { 295228753Smm i = i / 10; 296228753Smm digits++; 297228753Smm next_ten = next_ten * 10; 298228753Smm } 299228753Smm /* 300228753Smm * For example, if string without the length field is 99 301228753Smm * chars, then adding the 2 digit length "99" will force the 302228753Smm * total length past 100, requiring an extra digit. The next 303228753Smm * statement adjusts for this effect. 304228753Smm */ 305228753Smm if (len + digits >= next_ten) 306228753Smm digits++; 307228753Smm 308228753Smm /* Now, we have the right length so we can build the line. */ 309228753Smm tmp[sizeof(tmp) - 1] = 0; /* Null-terminate the work area. */ 310228753Smm archive_strcat(as, format_int(tmp + sizeof(tmp) - 1, len + digits)); 311228753Smm archive_strappend_char(as, ' '); 312228753Smm archive_strcat(as, key); 313228753Smm archive_strappend_char(as, '='); 314228753Smm archive_strcat(as, value); 315228753Smm archive_strappend_char(as, '\n'); 316228753Smm} 317228753Smm 318232153Smmstatic int 319232153Smmarchive_write_pax_header_xattrs(struct archive_write *a, 320232153Smm struct pax *pax, struct archive_entry *entry) 321228753Smm{ 322228753Smm struct archive_string s; 323228753Smm int i = archive_entry_xattr_reset(entry); 324228753Smm 325228753Smm while (i--) { 326228753Smm const char *name; 327228753Smm const void *value; 328228753Smm char *encoded_value; 329228753Smm char *url_encoded_name = NULL, *encoded_name = NULL; 330228753Smm size_t size; 331232153Smm int r; 332228753Smm 333228753Smm archive_entry_xattr_next(entry, &name, &value, &size); 334228753Smm url_encoded_name = url_encode(name); 335228753Smm if (url_encoded_name != NULL) { 336232153Smm /* Convert narrow-character to UTF-8. */ 337238856Smm r = archive_strcpy_l(&(pax->l_url_encoded_name), 338232153Smm url_encoded_name, pax->sconv_utf8); 339228753Smm free(url_encoded_name); /* Done with this. */ 340232153Smm if (r == 0) 341232153Smm encoded_name = pax->l_url_encoded_name.s; 342232153Smm else if (errno == ENOMEM) { 343232153Smm archive_set_error(&a->archive, ENOMEM, 344232153Smm "Can't allocate memory for Linkname"); 345232153Smm return (ARCHIVE_FATAL); 346232153Smm } 347228753Smm } 348228753Smm 349228753Smm encoded_value = base64_encode((const char *)value, size); 350228753Smm 351228753Smm if (encoded_name != NULL && encoded_value != NULL) { 352228753Smm archive_string_init(&s); 353228753Smm archive_strcpy(&s, "LIBARCHIVE.xattr."); 354228753Smm archive_strcat(&s, encoded_name); 355228753Smm add_pax_attr(&(pax->pax_header), s.s, encoded_value); 356228753Smm archive_string_free(&s); 357228753Smm } 358228753Smm free(encoded_value); 359228753Smm } 360232153Smm return (ARCHIVE_OK); 361228753Smm} 362228753Smm 363232153Smmstatic int 364232153Smmget_entry_hardlink(struct archive_write *a, struct archive_entry *entry, 365232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 366232153Smm{ 367232153Smm int r; 368232153Smm 369232153Smm r = archive_entry_hardlink_l(entry, name, length, sc); 370232153Smm if (r != 0) { 371232153Smm if (errno == ENOMEM) { 372232153Smm archive_set_error(&a->archive, ENOMEM, 373232153Smm "Can't allocate memory for Linkname"); 374232153Smm return (ARCHIVE_FATAL); 375232153Smm } 376232153Smm return (ARCHIVE_WARN); 377232153Smm } 378232153Smm return (ARCHIVE_OK); 379232153Smm} 380232153Smm 381232153Smmstatic int 382232153Smmget_entry_pathname(struct archive_write *a, struct archive_entry *entry, 383232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 384232153Smm{ 385232153Smm int r; 386232153Smm 387232153Smm r = archive_entry_pathname_l(entry, name, length, sc); 388232153Smm if (r != 0) { 389232153Smm if (errno == ENOMEM) { 390232153Smm archive_set_error(&a->archive, ENOMEM, 391232153Smm "Can't allocate memory for Pathname"); 392232153Smm return (ARCHIVE_FATAL); 393232153Smm } 394232153Smm return (ARCHIVE_WARN); 395232153Smm } 396232153Smm return (ARCHIVE_OK); 397232153Smm} 398232153Smm 399232153Smmstatic int 400232153Smmget_entry_uname(struct archive_write *a, struct archive_entry *entry, 401232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 402232153Smm{ 403232153Smm int r; 404232153Smm 405232153Smm r = archive_entry_uname_l(entry, name, length, sc); 406232153Smm if (r != 0) { 407232153Smm if (errno == ENOMEM) { 408232153Smm archive_set_error(&a->archive, ENOMEM, 409232153Smm "Can't allocate memory for Uname"); 410232153Smm return (ARCHIVE_FATAL); 411232153Smm } 412232153Smm return (ARCHIVE_WARN); 413232153Smm } 414232153Smm return (ARCHIVE_OK); 415232153Smm} 416232153Smm 417232153Smmstatic int 418232153Smmget_entry_gname(struct archive_write *a, struct archive_entry *entry, 419232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 420232153Smm{ 421232153Smm int r; 422232153Smm 423232153Smm r = archive_entry_gname_l(entry, name, length, sc); 424232153Smm if (r != 0) { 425232153Smm if (errno == ENOMEM) { 426232153Smm archive_set_error(&a->archive, ENOMEM, 427232153Smm "Can't allocate memory for Gname"); 428232153Smm return (ARCHIVE_FATAL); 429232153Smm } 430232153Smm return (ARCHIVE_WARN); 431232153Smm } 432232153Smm return (ARCHIVE_OK); 433232153Smm} 434232153Smm 435232153Smmstatic int 436232153Smmget_entry_symlink(struct archive_write *a, struct archive_entry *entry, 437232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 438232153Smm{ 439232153Smm int r; 440232153Smm 441232153Smm r = archive_entry_symlink_l(entry, name, length, sc); 442232153Smm if (r != 0) { 443232153Smm if (errno == ENOMEM) { 444232153Smm archive_set_error(&a->archive, ENOMEM, 445232153Smm "Can't allocate memory for Linkname"); 446232153Smm return (ARCHIVE_FATAL); 447232153Smm } 448232153Smm return (ARCHIVE_WARN); 449232153Smm } 450232153Smm return (ARCHIVE_OK); 451232153Smm} 452232153Smm 453228753Smm/* 454228753Smm * TODO: Consider adding 'comment' and 'charset' fields to 455228753Smm * archive_entry so that clients can specify them. Also, consider 456228753Smm * adding generic key/value tags so clients can add arbitrary 457228753Smm * key/value data. 458232153Smm * 459232153Smm * TODO: Break up this 700-line function!!!! Yowza! 460228753Smm */ 461228753Smmstatic int 462228753Smmarchive_write_pax_header(struct archive_write *a, 463228753Smm struct archive_entry *entry_original) 464228753Smm{ 465228753Smm struct archive_entry *entry_main; 466228753Smm const char *p; 467228753Smm const char *suffix; 468228753Smm int need_extension, r, ret; 469232153Smm int sparse_count; 470232153Smm uint64_t sparse_total, real_size; 471228753Smm struct pax *pax; 472228753Smm const char *hardlink; 473228753Smm const char *path = NULL, *linkpath = NULL; 474228753Smm const char *uname = NULL, *gname = NULL; 475232153Smm const void *mac_metadata; 476232153Smm size_t mac_metadata_size; 477232153Smm struct archive_string_conv *sconv; 478232153Smm size_t hardlink_length, path_length, linkpath_length; 479232153Smm size_t uname_length, gname_length; 480228753Smm 481228753Smm char paxbuff[512]; 482228753Smm char ustarbuff[512]; 483228753Smm char ustar_entry_name[256]; 484228753Smm char pax_entry_name[256]; 485232153Smm char gnu_sparse_name[256]; 486232153Smm struct archive_string entry_name; 487228753Smm 488228753Smm ret = ARCHIVE_OK; 489228753Smm need_extension = 0; 490228753Smm pax = (struct pax *)a->format_data; 491228753Smm 492232153Smm /* Sanity check. */ 493232153Smm if (archive_entry_pathname(entry_original) == NULL) { 494232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 495232153Smm "Can't record entry in tar file without pathname"); 496232153Smm return (ARCHIVE_FAILED); 497232153Smm } 498228753Smm 499232153Smm /* 500232153Smm * Choose a header encoding. 501232153Smm */ 502232153Smm if (pax->opt_binary) 503232153Smm sconv = NULL;/* Binary mode. */ 504232153Smm else { 505232153Smm /* Header encoding is UTF-8. */ 506232153Smm if (pax->sconv_utf8 == NULL) { 507232153Smm /* Initialize the string conversion object 508232153Smm * we must need */ 509232153Smm pax->sconv_utf8 = archive_string_conversion_to_charset( 510232153Smm &(a->archive), "UTF-8", 1); 511232153Smm if (pax->sconv_utf8 == NULL) 512232153Smm /* Couldn't allocate memory */ 513232153Smm return (ARCHIVE_FAILED); 514232153Smm } 515232153Smm sconv = pax->sconv_utf8; 516232153Smm } 517232153Smm 518232153Smm r = get_entry_hardlink(a, entry_original, &hardlink, 519232153Smm &hardlink_length, sconv); 520232153Smm if (r == ARCHIVE_FATAL) 521232153Smm return (r); 522232153Smm else if (r != ARCHIVE_OK) { 523232153Smm r = get_entry_hardlink(a, entry_original, &hardlink, 524232153Smm &hardlink_length, NULL); 525232153Smm if (r == ARCHIVE_FATAL) 526232153Smm return (r); 527232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 528232153Smm "Can't translate linkname '%s' to %s", hardlink, 529232153Smm archive_string_conversion_charset_name(sconv)); 530232153Smm ret = ARCHIVE_WARN; 531232153Smm sconv = NULL;/* The header charset switches to binary mode. */ 532232153Smm } 533232153Smm 534228753Smm /* Make sure this is a type of entry that we can handle here */ 535228753Smm if (hardlink == NULL) { 536228753Smm switch (archive_entry_filetype(entry_original)) { 537228753Smm case AE_IFBLK: 538228753Smm case AE_IFCHR: 539228753Smm case AE_IFIFO: 540228753Smm case AE_IFLNK: 541228753Smm case AE_IFREG: 542228753Smm break; 543228753Smm case AE_IFDIR: 544232153Smm { 545228753Smm /* 546228753Smm * Ensure a trailing '/'. Modify the original 547228753Smm * entry so the client sees the change. 548228753Smm */ 549232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 550232153Smm const wchar_t *wp; 551232153Smm 552232153Smm wp = archive_entry_pathname_w(entry_original); 553232153Smm if (wp != NULL && wp[wcslen(wp) -1] != L'/') { 554232153Smm struct archive_wstring ws; 555232153Smm 556232153Smm archive_string_init(&ws); 557232153Smm path_length = wcslen(wp); 558232153Smm if (archive_wstring_ensure(&ws, 559232153Smm path_length + 2) == NULL) { 560228753Smm archive_set_error(&a->archive, ENOMEM, 561232153Smm "Can't allocate pax data"); 562232153Smm archive_wstring_free(&ws); 563228753Smm return(ARCHIVE_FATAL); 564228753Smm } 565232153Smm /* Should we keep '\' ? */ 566232153Smm if (wp[path_length -1] == L'\\') 567232153Smm path_length--; 568232153Smm archive_wstrncpy(&ws, wp, path_length); 569232153Smm archive_wstrappend_wchar(&ws, L'/'); 570232153Smm archive_entry_copy_pathname_w( 571232153Smm entry_original, ws.s); 572232153Smm archive_wstring_free(&ws); 573232153Smm p = NULL; 574232153Smm } else 575232153Smm#endif 576232153Smm p = archive_entry_pathname(entry_original); 577232153Smm /* 578232153Smm * On Windows, this is a backup operation just in 579232153Smm * case getting WCS failed. On POSIX, this is a 580232153Smm * normal operation. 581232153Smm */ 582232153Smm if (p != NULL && p[strlen(p) - 1] != '/') { 583232153Smm struct archive_string as; 584232153Smm 585232153Smm archive_string_init(&as); 586232153Smm path_length = strlen(p); 587232153Smm if (archive_string_ensure(&as, 588232153Smm path_length + 2) == NULL) { 589232153Smm archive_set_error(&a->archive, ENOMEM, 590232153Smm "Can't allocate pax data"); 591232153Smm archive_string_free(&as); 592232153Smm return(ARCHIVE_FATAL); 593232153Smm } 594232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 595232153Smm /* NOTE: This might break the pathname 596232153Smm * if the current code page is CP932 and 597232153Smm * the pathname includes a character '\' 598232153Smm * as a part of its multibyte pathname. */ 599232153Smm if (p[strlen(p) -1] == '\\') 600232153Smm path_length--; 601232153Smm else 602232153Smm#endif 603232153Smm archive_strncpy(&as, p, path_length); 604232153Smm archive_strappend_char(&as, '/'); 605232153Smm archive_entry_copy_pathname( 606232153Smm entry_original, as.s); 607232153Smm archive_string_free(&as); 608228753Smm } 609228753Smm break; 610232153Smm } 611228753Smm case AE_IFSOCK: 612228753Smm archive_set_error(&a->archive, 613228753Smm ARCHIVE_ERRNO_FILE_FORMAT, 614228753Smm "tar format cannot archive socket"); 615232153Smm return (ARCHIVE_FAILED); 616228753Smm default: 617228753Smm archive_set_error(&a->archive, 618228753Smm ARCHIVE_ERRNO_FILE_FORMAT, 619228753Smm "tar format cannot archive this (type=0%lo)", 620232153Smm (unsigned long) 621232153Smm archive_entry_filetype(entry_original)); 622232153Smm return (ARCHIVE_FAILED); 623228753Smm } 624228753Smm } 625228753Smm 626232153Smm /* 627232153Smm * If Mac OS metadata blob is here, recurse to write that 628232153Smm * as a separate entry. This is really a pretty poor design: 629232153Smm * In particular, it doubles the overhead for long filenames. 630232153Smm * TODO: Help Apple folks design something better and figure 631232153Smm * out how to transition from this legacy format. 632232153Smm * 633232153Smm * Note that this code is present on every platform; clients 634232153Smm * on non-Mac are unlikely to ever provide this data, but 635232153Smm * applications that copy entries from one archive to another 636232153Smm * should not lose data just because the local filesystem 637232153Smm * can't store it. 638232153Smm */ 639232153Smm mac_metadata = 640232153Smm archive_entry_mac_metadata(entry_original, &mac_metadata_size); 641232153Smm if (mac_metadata != NULL) { 642232153Smm const char *oname; 643232153Smm char *name, *bname; 644232153Smm size_t name_length; 645232153Smm struct archive_entry *extra = archive_entry_new2(&a->archive); 646232153Smm 647232153Smm oname = archive_entry_pathname(entry_original); 648232153Smm name_length = strlen(oname); 649232153Smm name = malloc(name_length + 3); 650248616Smm if (name == NULL || extra == NULL) { 651232153Smm /* XXX error message */ 652248616Smm archive_entry_free(extra); 653248616Smm free(name); 654232153Smm return (ARCHIVE_FAILED); 655232153Smm } 656232153Smm strcpy(name, oname); 657232153Smm /* Find last '/'; strip trailing '/' characters */ 658232153Smm bname = strrchr(name, '/'); 659232153Smm while (bname != NULL && bname[1] == '\0') { 660232153Smm *bname = '\0'; 661232153Smm bname = strrchr(name, '/'); 662232153Smm } 663232153Smm if (bname == NULL) { 664232153Smm memmove(name + 2, name, name_length + 1); 665232153Smm memmove(name, "._", 2); 666232153Smm } else { 667232153Smm bname += 1; 668232153Smm memmove(bname + 2, bname, strlen(bname) + 1); 669232153Smm memmove(bname, "._", 2); 670232153Smm } 671232153Smm archive_entry_copy_pathname(extra, name); 672232153Smm free(name); 673232153Smm 674232153Smm archive_entry_set_size(extra, mac_metadata_size); 675232153Smm archive_entry_set_filetype(extra, AE_IFREG); 676232153Smm archive_entry_set_perm(extra, 677232153Smm archive_entry_perm(entry_original)); 678232153Smm archive_entry_set_mtime(extra, 679232153Smm archive_entry_mtime(entry_original), 680232153Smm archive_entry_mtime_nsec(entry_original)); 681232153Smm archive_entry_set_gid(extra, 682232153Smm archive_entry_gid(entry_original)); 683232153Smm archive_entry_set_gname(extra, 684232153Smm archive_entry_gname(entry_original)); 685232153Smm archive_entry_set_uid(extra, 686232153Smm archive_entry_uid(entry_original)); 687232153Smm archive_entry_set_uname(extra, 688232153Smm archive_entry_uname(entry_original)); 689232153Smm 690232153Smm /* Recurse to write the special copyfile entry. */ 691232153Smm r = archive_write_pax_header(a, extra); 692248616Smm archive_entry_free(extra); 693232153Smm if (r < ARCHIVE_WARN) 694232153Smm return (r); 695232153Smm if (r < ret) 696232153Smm ret = r; 697248616Smm r = (int)archive_write_pax_data(a, mac_metadata, 698248616Smm mac_metadata_size); 699232153Smm if (r < ARCHIVE_WARN) 700232153Smm return (r); 701232153Smm if (r < ret) 702232153Smm ret = r; 703232153Smm r = archive_write_pax_finish_entry(a); 704232153Smm if (r < ARCHIVE_WARN) 705232153Smm return (r); 706232153Smm if (r < ret) 707232153Smm ret = r; 708232153Smm } 709232153Smm 710228753Smm /* Copy entry so we can modify it as needed. */ 711232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 712232153Smm /* Make sure the path separators in pahtname, hardlink and symlink 713232153Smm * are all slash '/', not the Windows path separator '\'. */ 714232153Smm entry_main = __la_win_entry_in_posix_pathseparator(entry_original); 715232153Smm if (entry_main == entry_original) 716232153Smm entry_main = archive_entry_clone(entry_original); 717232153Smm#else 718228753Smm entry_main = archive_entry_clone(entry_original); 719232153Smm#endif 720232153Smm if (entry_main == NULL) { 721232153Smm archive_set_error(&a->archive, ENOMEM, 722232153Smm "Can't allocate pax data"); 723232153Smm return(ARCHIVE_FATAL); 724232153Smm } 725228753Smm archive_string_empty(&(pax->pax_header)); /* Blank our work area. */ 726232153Smm archive_string_empty(&(pax->sparse_map)); 727232153Smm sparse_total = 0; 728232153Smm sparse_list_clear(pax); 729228753Smm 730232153Smm if (hardlink == NULL && 731232153Smm archive_entry_filetype(entry_main) == AE_IFREG) 732232153Smm sparse_count = archive_entry_sparse_reset(entry_main); 733232153Smm else 734232153Smm sparse_count = 0; 735232153Smm if (sparse_count) { 736232153Smm int64_t offset, length, last_offset = 0; 737232153Smm /* Get the last entry of sparse block. */ 738232153Smm while (archive_entry_sparse_next( 739232153Smm entry_main, &offset, &length) == ARCHIVE_OK) 740232153Smm last_offset = offset + length; 741232153Smm 742232153Smm /* If the last sparse block does not reach the end of file, 743232153Smm * We have to add a empty sparse block as the last entry to 744232153Smm * manage storing file data. */ 745232153Smm if (last_offset < archive_entry_size(entry_main)) 746232153Smm archive_entry_sparse_add_entry(entry_main, 747232153Smm archive_entry_size(entry_main), 0); 748232153Smm sparse_count = archive_entry_sparse_reset(entry_main); 749232153Smm } 750232153Smm 751228753Smm /* 752228753Smm * First, check the name fields and see if any of them 753228753Smm * require binary coding. If any of them does, then all of 754228753Smm * them do. 755228753Smm */ 756232153Smm r = get_entry_pathname(a, entry_main, &path, &path_length, sconv); 757232153Smm if (r == ARCHIVE_FATAL) 758232153Smm return (r); 759232153Smm else if (r != ARCHIVE_OK) { 760232153Smm r = get_entry_pathname(a, entry_main, &path, 761232153Smm &path_length, NULL); 762232153Smm if (r == ARCHIVE_FATAL) 763232153Smm return (r); 764228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 765232153Smm "Can't translate pathname '%s' to %s", path, 766232153Smm archive_string_conversion_charset_name(sconv)); 767228753Smm ret = ARCHIVE_WARN; 768232153Smm sconv = NULL;/* The header charset switches to binary mode. */ 769228753Smm } 770232153Smm r = get_entry_uname(a, entry_main, &uname, &uname_length, sconv); 771232153Smm if (r == ARCHIVE_FATAL) 772232153Smm return (r); 773232153Smm else if (r != ARCHIVE_OK) { 774232153Smm r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL); 775232153Smm if (r == ARCHIVE_FATAL) 776232153Smm return (r); 777228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 778232153Smm "Can't translate uname '%s' to %s", uname, 779232153Smm archive_string_conversion_charset_name(sconv)); 780228753Smm ret = ARCHIVE_WARN; 781232153Smm sconv = NULL;/* The header charset switches to binary mode. */ 782228753Smm } 783232153Smm r = get_entry_gname(a, entry_main, &gname, &gname_length, sconv); 784232153Smm if (r == ARCHIVE_FATAL) 785232153Smm return (r); 786232153Smm else if (r != ARCHIVE_OK) { 787232153Smm r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL); 788232153Smm if (r == ARCHIVE_FATAL) 789232153Smm return (r); 790228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 791232153Smm "Can't translate gname '%s' to %s", gname, 792232153Smm archive_string_conversion_charset_name(sconv)); 793228753Smm ret = ARCHIVE_WARN; 794232153Smm sconv = NULL;/* The header charset switches to binary mode. */ 795228753Smm } 796228753Smm linkpath = hardlink; 797232153Smm linkpath_length = hardlink_length; 798232153Smm if (linkpath == NULL) { 799232153Smm r = get_entry_symlink(a, entry_main, &linkpath, 800232153Smm &linkpath_length, sconv); 801232153Smm if (r == ARCHIVE_FATAL) 802232153Smm return (r); 803232153Smm else if (r != ARCHIVE_OK) { 804232153Smm r = get_entry_symlink(a, entry_main, &linkpath, 805232153Smm &linkpath_length, NULL); 806232153Smm if (r == ARCHIVE_FATAL) 807232153Smm return (r); 808232153Smm archive_set_error(&a->archive, 809232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 810232153Smm "Can't translate linkname '%s' to %s", linkpath, 811232153Smm archive_string_conversion_charset_name(sconv)); 812232153Smm ret = ARCHIVE_WARN; 813232153Smm sconv = NULL; 814232153Smm } 815228753Smm } 816232153Smm 817232153Smm /* If any string conversions failed, get all attributes 818232153Smm * in binary-mode. */ 819232153Smm if (sconv == NULL && !pax->opt_binary) { 820232153Smm if (hardlink != NULL) { 821232153Smm r = get_entry_hardlink(a, entry_main, &hardlink, 822232153Smm &hardlink_length, NULL); 823232153Smm if (r == ARCHIVE_FATAL) 824232153Smm return (r); 825232153Smm linkpath = hardlink; 826232153Smm linkpath_length = hardlink_length; 827232153Smm } 828232153Smm r = get_entry_pathname(a, entry_main, &path, 829232153Smm &path_length, NULL); 830232153Smm if (r == ARCHIVE_FATAL) 831232153Smm return (r); 832232153Smm r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL); 833232153Smm if (r == ARCHIVE_FATAL) 834232153Smm return (r); 835232153Smm r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL); 836232153Smm if (r == ARCHIVE_FATAL) 837232153Smm return (r); 838228753Smm } 839228753Smm 840228753Smm /* Store the header encoding first, to be nice to readers. */ 841232153Smm if (sconv == NULL) 842232153Smm add_pax_attr(&(pax->pax_header), "hdrcharset", "BINARY"); 843228753Smm 844228753Smm 845228753Smm /* 846228753Smm * If name is too long, or has non-ASCII characters, add 847228753Smm * 'path' to pax extended attrs. (Note that an unconvertible 848228753Smm * name must have non-ASCII characters.) 849228753Smm */ 850232153Smm if (has_non_ASCII(path)) { 851228753Smm /* We have non-ASCII characters. */ 852232153Smm add_pax_attr(&(pax->pax_header), "path", path); 853228753Smm archive_entry_set_pathname(entry_main, 854228753Smm build_ustar_entry_name(ustar_entry_name, 855232153Smm path, path_length, NULL)); 856228753Smm need_extension = 1; 857228753Smm } else { 858228753Smm /* We have an all-ASCII path; we'd like to just store 859228753Smm * it in the ustar header if it will fit. Yes, this 860228753Smm * duplicates some of the logic in 861232153Smm * archive_write_set_format_ustar.c 862228753Smm */ 863232153Smm if (path_length <= 100) { 864228753Smm /* Fits in the old 100-char tar name field. */ 865228753Smm } else { 866228753Smm /* Find largest suffix that will fit. */ 867228753Smm /* Note: strlen() > 100, so strlen() - 100 - 1 >= 0 */ 868232153Smm suffix = strchr(path + path_length - 100 - 1, '/'); 869228753Smm /* Don't attempt an empty prefix. */ 870228753Smm if (suffix == path) 871228753Smm suffix = strchr(suffix + 1, '/'); 872228753Smm /* We can put it in the ustar header if it's 873228753Smm * all ASCII and it's either <= 100 characters 874228753Smm * or can be split at a '/' into a prefix <= 875228753Smm * 155 chars and a suffix <= 100 chars. (Note 876228753Smm * the strchr() above will return NULL exactly 877228753Smm * when the path can't be split.) 878228753Smm */ 879228753Smm if (suffix == NULL /* Suffix > 100 chars. */ 880228753Smm || suffix[1] == '\0' /* empty suffix */ 881228753Smm || suffix - path > 155) /* Prefix > 155 chars */ 882228753Smm { 883232153Smm add_pax_attr(&(pax->pax_header), "path", path); 884228753Smm archive_entry_set_pathname(entry_main, 885228753Smm build_ustar_entry_name(ustar_entry_name, 886232153Smm path, path_length, NULL)); 887228753Smm need_extension = 1; 888228753Smm } 889228753Smm } 890228753Smm } 891228753Smm 892228753Smm if (linkpath != NULL) { 893228753Smm /* If link name is too long or has non-ASCII characters, add 894228753Smm * 'linkpath' to pax extended attrs. */ 895232153Smm if (linkpath_length > 100 || has_non_ASCII(linkpath)) { 896232153Smm add_pax_attr(&(pax->pax_header), "linkpath", linkpath); 897232153Smm if (linkpath_length > 100) { 898228753Smm if (hardlink != NULL) 899228753Smm archive_entry_set_hardlink(entry_main, 900228753Smm "././@LongHardLink"); 901228753Smm else 902228753Smm archive_entry_set_symlink(entry_main, 903228753Smm "././@LongSymLink"); 904228753Smm } 905228753Smm need_extension = 1; 906228753Smm } 907228753Smm } 908232153Smm /* Save a pathname since it will be renamed if `entry_main` has 909232153Smm * sparse blocks. */ 910232153Smm archive_string_init(&entry_name); 911232153Smm archive_strcpy(&entry_name, archive_entry_pathname(entry_main)); 912228753Smm 913228753Smm /* If file size is too large, add 'size' to pax extended attrs. */ 914228753Smm if (archive_entry_size(entry_main) >= (((int64_t)1) << 33)) { 915228753Smm add_pax_attr_int(&(pax->pax_header), "size", 916228753Smm archive_entry_size(entry_main)); 917228753Smm need_extension = 1; 918228753Smm } 919228753Smm 920228753Smm /* If numeric GID is too large, add 'gid' to pax extended attrs. */ 921228753Smm if ((unsigned int)archive_entry_gid(entry_main) >= (1 << 18)) { 922228753Smm add_pax_attr_int(&(pax->pax_header), "gid", 923228753Smm archive_entry_gid(entry_main)); 924228753Smm need_extension = 1; 925228753Smm } 926228753Smm 927228753Smm /* If group name is too large or has non-ASCII characters, add 928228753Smm * 'gname' to pax extended attrs. */ 929228753Smm if (gname != NULL) { 930232153Smm if (gname_length > 31 || has_non_ASCII(gname)) { 931232153Smm add_pax_attr(&(pax->pax_header), "gname", gname); 932228753Smm need_extension = 1; 933228753Smm } 934228753Smm } 935228753Smm 936228753Smm /* If numeric UID is too large, add 'uid' to pax extended attrs. */ 937228753Smm if ((unsigned int)archive_entry_uid(entry_main) >= (1 << 18)) { 938228753Smm add_pax_attr_int(&(pax->pax_header), "uid", 939228753Smm archive_entry_uid(entry_main)); 940228753Smm need_extension = 1; 941228753Smm } 942228753Smm 943228753Smm /* Add 'uname' to pax extended attrs if necessary. */ 944228753Smm if (uname != NULL) { 945232153Smm if (uname_length > 31 || has_non_ASCII(uname)) { 946232153Smm add_pax_attr(&(pax->pax_header), "uname", uname); 947228753Smm need_extension = 1; 948228753Smm } 949228753Smm } 950228753Smm 951228753Smm /* 952228753Smm * POSIX/SUSv3 doesn't provide a standard key for large device 953228753Smm * numbers. I use the same keys here that Joerg Schilling 954228753Smm * used for 'star.' (Which, somewhat confusingly, are called 955228753Smm * "devXXX" even though they code "rdev" values.) No doubt, 956228753Smm * other implementations use other keys. Note that there's no 957228753Smm * reason we can't write the same information into a number of 958228753Smm * different keys. 959228753Smm * 960228753Smm * Of course, this is only needed for block or char device entries. 961228753Smm */ 962228753Smm if (archive_entry_filetype(entry_main) == AE_IFBLK 963228753Smm || archive_entry_filetype(entry_main) == AE_IFCHR) { 964228753Smm /* 965228753Smm * If rdevmajor is too large, add 'SCHILY.devmajor' to 966228753Smm * extended attributes. 967228753Smm */ 968232153Smm int rdevmajor, rdevminor; 969228753Smm rdevmajor = archive_entry_rdevmajor(entry_main); 970228753Smm rdevminor = archive_entry_rdevminor(entry_main); 971228753Smm if (rdevmajor >= (1 << 18)) { 972228753Smm add_pax_attr_int(&(pax->pax_header), "SCHILY.devmajor", 973228753Smm rdevmajor); 974228753Smm /* 975228753Smm * Non-strict formatting below means we don't 976228753Smm * have to truncate here. Not truncating improves 977228753Smm * the chance that some more modern tar archivers 978228753Smm * (such as GNU tar 1.13) can restore the full 979228753Smm * value even if they don't understand the pax 980228753Smm * extended attributes. See my rant below about 981228753Smm * file size fields for additional details. 982228753Smm */ 983228753Smm /* archive_entry_set_rdevmajor(entry_main, 984228753Smm rdevmajor & ((1 << 18) - 1)); */ 985228753Smm need_extension = 1; 986228753Smm } 987228753Smm 988228753Smm /* 989228753Smm * If devminor is too large, add 'SCHILY.devminor' to 990228753Smm * extended attributes. 991228753Smm */ 992228753Smm if (rdevminor >= (1 << 18)) { 993228753Smm add_pax_attr_int(&(pax->pax_header), "SCHILY.devminor", 994228753Smm rdevminor); 995228753Smm /* Truncation is not necessary here, either. */ 996228753Smm /* archive_entry_set_rdevminor(entry_main, 997228753Smm rdevminor & ((1 << 18) - 1)); */ 998228753Smm need_extension = 1; 999228753Smm } 1000228753Smm } 1001228753Smm 1002228753Smm /* 1003228753Smm * Technically, the mtime field in the ustar header can 1004228753Smm * support 33 bits, but many platforms use signed 32-bit time 1005228753Smm * values. The cutoff of 0x7fffffff here is a compromise. 1006228753Smm * Yes, this check is duplicated just below; this helps to 1007228753Smm * avoid writing an mtime attribute just to handle a 1008228753Smm * high-resolution timestamp in "restricted pax" mode. 1009228753Smm */ 1010228753Smm if (!need_extension && 1011228753Smm ((archive_entry_mtime(entry_main) < 0) 1012228753Smm || (archive_entry_mtime(entry_main) >= 0x7fffffff))) 1013228753Smm need_extension = 1; 1014228753Smm 1015228753Smm /* I use a star-compatible file flag attribute. */ 1016228753Smm p = archive_entry_fflags_text(entry_main); 1017228753Smm if (!need_extension && p != NULL && *p != '\0') 1018228753Smm need_extension = 1; 1019228753Smm 1020228753Smm /* If there are non-trivial ACL entries, we need an extension. */ 1021228753Smm if (!need_extension && archive_entry_acl_count(entry_original, 1022228753Smm ARCHIVE_ENTRY_ACL_TYPE_ACCESS) > 0) 1023228753Smm need_extension = 1; 1024228753Smm 1025228753Smm /* If there are non-trivial ACL entries, we need an extension. */ 1026228753Smm if (!need_extension && archive_entry_acl_count(entry_original, 1027228753Smm ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) > 0) 1028228753Smm need_extension = 1; 1029228753Smm 1030228753Smm /* If there are extended attributes, we need an extension */ 1031228753Smm if (!need_extension && archive_entry_xattr_count(entry_original) > 0) 1032228753Smm need_extension = 1; 1033228753Smm 1034232153Smm /* If there are sparse info, we need an extension */ 1035232153Smm if (!need_extension && sparse_count > 0) 1036232153Smm need_extension = 1; 1037232153Smm 1038228753Smm /* 1039228753Smm * The following items are handled differently in "pax 1040228753Smm * restricted" format. In particular, in "pax restricted" 1041228753Smm * format they won't be added unless need_extension is 1042228753Smm * already set (we're already generating an extended header, so 1043228753Smm * may as well include these). 1044228753Smm */ 1045228753Smm if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED || 1046228753Smm need_extension) { 1047228753Smm 1048228753Smm if (archive_entry_mtime(entry_main) < 0 || 1049228753Smm archive_entry_mtime(entry_main) >= 0x7fffffff || 1050228753Smm archive_entry_mtime_nsec(entry_main) != 0) 1051228753Smm add_pax_attr_time(&(pax->pax_header), "mtime", 1052228753Smm archive_entry_mtime(entry_main), 1053228753Smm archive_entry_mtime_nsec(entry_main)); 1054228753Smm 1055228753Smm if (archive_entry_ctime(entry_main) != 0 || 1056228753Smm archive_entry_ctime_nsec(entry_main) != 0) 1057228753Smm add_pax_attr_time(&(pax->pax_header), "ctime", 1058228753Smm archive_entry_ctime(entry_main), 1059228753Smm archive_entry_ctime_nsec(entry_main)); 1060228753Smm 1061228753Smm if (archive_entry_atime(entry_main) != 0 || 1062228753Smm archive_entry_atime_nsec(entry_main) != 0) 1063228753Smm add_pax_attr_time(&(pax->pax_header), "atime", 1064228753Smm archive_entry_atime(entry_main), 1065228753Smm archive_entry_atime_nsec(entry_main)); 1066228753Smm 1067228753Smm /* Store birth/creationtime only if it's earlier than mtime */ 1068228753Smm if (archive_entry_birthtime_is_set(entry_main) && 1069228753Smm archive_entry_birthtime(entry_main) 1070228753Smm < archive_entry_mtime(entry_main)) 1071228753Smm add_pax_attr_time(&(pax->pax_header), 1072228753Smm "LIBARCHIVE.creationtime", 1073228753Smm archive_entry_birthtime(entry_main), 1074228753Smm archive_entry_birthtime_nsec(entry_main)); 1075228753Smm 1076228753Smm /* I use a star-compatible file flag attribute. */ 1077228753Smm p = archive_entry_fflags_text(entry_main); 1078228753Smm if (p != NULL && *p != '\0') 1079228753Smm add_pax_attr(&(pax->pax_header), "SCHILY.fflags", p); 1080228753Smm 1081228753Smm /* I use star-compatible ACL attributes. */ 1082232153Smm r = archive_entry_acl_text_l(entry_original, 1083228753Smm ARCHIVE_ENTRY_ACL_TYPE_ACCESS | 1084232153Smm ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID, 1085232153Smm &p, NULL, pax->sconv_utf8); 1086232153Smm if (r != 0) { 1087232153Smm if (errno == ENOMEM) { 1088232153Smm archive_set_error(&a->archive, ENOMEM, 1089232153Smm "Can't allocate memory for " 1090232153Smm "ACL.access"); 1091232153Smm return (ARCHIVE_FATAL); 1092232153Smm } 1093232153Smm archive_set_error(&a->archive, 1094232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 1095232153Smm "Can't translate ACL.access to UTF-8"); 1096232153Smm ret = ARCHIVE_WARN; 1097232153Smm } else if (p != NULL && *p != '\0') { 1098232153Smm add_pax_attr(&(pax->pax_header), 1099232153Smm "SCHILY.acl.access", p); 1100232153Smm } 1101232153Smm r = archive_entry_acl_text_l(entry_original, 1102228753Smm ARCHIVE_ENTRY_ACL_TYPE_DEFAULT | 1103232153Smm ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID, 1104232153Smm &p, NULL, pax->sconv_utf8); 1105232153Smm if (r != 0) { 1106232153Smm if (errno == ENOMEM) { 1107232153Smm archive_set_error(&a->archive, ENOMEM, 1108232153Smm "Can't allocate memory for " 1109232153Smm "ACL.default"); 1110232153Smm return (ARCHIVE_FATAL); 1111232153Smm } 1112232153Smm archive_set_error(&a->archive, 1113232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 1114232153Smm "Can't translate ACL.default to UTF-8"); 1115232153Smm ret = ARCHIVE_WARN; 1116232153Smm } else if (p != NULL && *p != '\0') { 1117232153Smm add_pax_attr(&(pax->pax_header), 1118232153Smm "SCHILY.acl.default", p); 1119232153Smm } 1120228753Smm 1121232153Smm /* We use GNU-tar-compatible sparse attributes. */ 1122232153Smm if (sparse_count > 0) { 1123232153Smm int64_t soffset, slength; 1124228753Smm 1125232153Smm add_pax_attr_int(&(pax->pax_header), 1126232153Smm "GNU.sparse.major", 1); 1127232153Smm add_pax_attr_int(&(pax->pax_header), 1128232153Smm "GNU.sparse.minor", 0); 1129232153Smm add_pax_attr(&(pax->pax_header), 1130232153Smm "GNU.sparse.name", entry_name.s); 1131232153Smm add_pax_attr_int(&(pax->pax_header), 1132232153Smm "GNU.sparse.realsize", 1133232153Smm archive_entry_size(entry_main)); 1134232153Smm 1135232153Smm /* Rename the file name which will be used for 1136232153Smm * ustar header to a special name, which GNU 1137232153Smm * PAX Format 1.0 requires */ 1138232153Smm archive_entry_set_pathname(entry_main, 1139232153Smm build_gnu_sparse_name(gnu_sparse_name, 1140232153Smm entry_name.s)); 1141232153Smm 1142232153Smm /* 1143232153Smm * - Make a sparse map, which will precede a file data. 1144232153Smm * - Get the total size of available data of sparse. 1145232153Smm */ 1146232153Smm archive_string_sprintf(&(pax->sparse_map), "%d\n", 1147232153Smm sparse_count); 1148232153Smm while (archive_entry_sparse_next(entry_main, 1149232153Smm &soffset, &slength) == ARCHIVE_OK) { 1150232153Smm archive_string_sprintf(&(pax->sparse_map), 1151232153Smm "%jd\n%jd\n", 1152232153Smm (intmax_t)soffset, 1153232153Smm (intmax_t)slength); 1154232153Smm sparse_total += slength; 1155232153Smm if (sparse_list_add(pax, soffset, slength) 1156232153Smm != ARCHIVE_OK) { 1157232153Smm archive_set_error(&a->archive, 1158232153Smm ENOMEM, 1159232153Smm "Can't allocate memory"); 1160232153Smm archive_entry_free(entry_main); 1161232153Smm archive_string_free(&entry_name); 1162232153Smm return (ARCHIVE_FATAL); 1163232153Smm } 1164232153Smm } 1165232153Smm } 1166232153Smm 1167228753Smm /* Store extended attributes */ 1168232153Smm if (archive_write_pax_header_xattrs(a, pax, entry_original) 1169232153Smm == ARCHIVE_FATAL) { 1170232153Smm archive_entry_free(entry_main); 1171232153Smm archive_string_free(&entry_name); 1172232153Smm return (ARCHIVE_FATAL); 1173232153Smm } 1174228753Smm } 1175228753Smm 1176228753Smm /* Only regular files have data. */ 1177228753Smm if (archive_entry_filetype(entry_main) != AE_IFREG) 1178228753Smm archive_entry_set_size(entry_main, 0); 1179228753Smm 1180228753Smm /* 1181228753Smm * Pax-restricted does not store data for hardlinks, in order 1182228753Smm * to improve compatibility with ustar. 1183228753Smm */ 1184228753Smm if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE && 1185228753Smm hardlink != NULL) 1186228753Smm archive_entry_set_size(entry_main, 0); 1187228753Smm 1188228753Smm /* 1189228753Smm * XXX Full pax interchange format does permit a hardlink 1190228753Smm * entry to have data associated with it. I'm not supporting 1191228753Smm * that here because the client expects me to tell them whether 1192228753Smm * or not this format expects data for hardlinks. If I 1193228753Smm * don't check here, then every pax archive will end up with 1194228753Smm * duplicated data for hardlinks. Someday, there may be 1195228753Smm * need to select this behavior, in which case the following 1196228753Smm * will need to be revisited. XXX 1197228753Smm */ 1198228753Smm if (hardlink != NULL) 1199228753Smm archive_entry_set_size(entry_main, 0); 1200228753Smm 1201232153Smm /* Save a real file size. */ 1202232153Smm real_size = archive_entry_size(entry_main); 1203232153Smm /* 1204232153Smm * Overwrite a file size by the total size of sparse blocks and 1205232153Smm * the size of sparse map info. That file size is the length of 1206232153Smm * the data, which we will exactly store into an archive file. 1207232153Smm */ 1208232153Smm if (archive_strlen(&(pax->sparse_map))) { 1209232153Smm size_t mapsize = archive_strlen(&(pax->sparse_map)); 1210232153Smm pax->sparse_map_padding = 0x1ff & (-(ssize_t)mapsize); 1211232153Smm archive_entry_set_size(entry_main, 1212232153Smm mapsize + pax->sparse_map_padding + sparse_total); 1213232153Smm } 1214232153Smm 1215228753Smm /* Format 'ustar' header for main entry. 1216228753Smm * 1217228753Smm * The trouble with file size: If the reader can't understand 1218228753Smm * the file size, they may not be able to locate the next 1219228753Smm * entry and the rest of the archive is toast. Pax-compliant 1220228753Smm * readers are supposed to ignore the file size in the main 1221228753Smm * header, so the question becomes how to maximize portability 1222228753Smm * for readers that don't support pax attribute extensions. 1223228753Smm * For maximum compatibility, I permit numeric extensions in 1224228753Smm * the main header so that the file size stored will always be 1225228753Smm * correct, even if it's in a format that only some 1226228753Smm * implementations understand. The technique used here is: 1227228753Smm * 1228228753Smm * a) If possible, follow the standard exactly. This handles 1229228753Smm * files up to 8 gigabytes minus 1. 1230228753Smm * 1231228753Smm * b) If that fails, try octal but omit the field terminator. 1232228753Smm * That handles files up to 64 gigabytes minus 1. 1233228753Smm * 1234228753Smm * c) Otherwise, use base-256 extensions. That handles files 1235228753Smm * up to 2^63 in this implementation, with the potential to 1236228753Smm * go up to 2^94. That should hold us for a while. ;-) 1237228753Smm * 1238228753Smm * The non-strict formatter uses similar logic for other 1239228753Smm * numeric fields, though they're less critical. 1240228753Smm */ 1241232153Smm if (__archive_write_format_header_ustar(a, ustarbuff, entry_main, -1, 0, 1242232153Smm NULL) == ARCHIVE_FATAL) 1243232153Smm return (ARCHIVE_FATAL); 1244228753Smm 1245228753Smm /* If we built any extended attributes, write that entry first. */ 1246228753Smm if (archive_strlen(&(pax->pax_header)) > 0) { 1247228753Smm struct archive_entry *pax_attr_entry; 1248228753Smm time_t s; 1249232153Smm int64_t uid, gid; 1250232153Smm int mode; 1251228753Smm 1252232153Smm pax_attr_entry = archive_entry_new2(&a->archive); 1253232153Smm p = entry_name.s; 1254228753Smm archive_entry_set_pathname(pax_attr_entry, 1255228753Smm build_pax_attribute_name(pax_entry_name, p)); 1256228753Smm archive_entry_set_size(pax_attr_entry, 1257228753Smm archive_strlen(&(pax->pax_header))); 1258228753Smm /* Copy uid/gid (but clip to ustar limits). */ 1259228753Smm uid = archive_entry_uid(entry_main); 1260232153Smm if (uid >= 1 << 18) 1261232153Smm uid = (1 << 18) - 1; 1262228753Smm archive_entry_set_uid(pax_attr_entry, uid); 1263228753Smm gid = archive_entry_gid(entry_main); 1264232153Smm if (gid >= 1 << 18) 1265232153Smm gid = (1 << 18) - 1; 1266228753Smm archive_entry_set_gid(pax_attr_entry, gid); 1267228753Smm /* Copy mode over (but not setuid/setgid bits) */ 1268228753Smm mode = archive_entry_mode(entry_main); 1269228753Smm#ifdef S_ISUID 1270228753Smm mode &= ~S_ISUID; 1271228753Smm#endif 1272228753Smm#ifdef S_ISGID 1273228753Smm mode &= ~S_ISGID; 1274228753Smm#endif 1275228753Smm#ifdef S_ISVTX 1276228753Smm mode &= ~S_ISVTX; 1277228753Smm#endif 1278228753Smm archive_entry_set_mode(pax_attr_entry, mode); 1279228753Smm 1280228753Smm /* Copy uname/gname. */ 1281228753Smm archive_entry_set_uname(pax_attr_entry, 1282228753Smm archive_entry_uname(entry_main)); 1283228753Smm archive_entry_set_gname(pax_attr_entry, 1284228753Smm archive_entry_gname(entry_main)); 1285228753Smm 1286228753Smm /* Copy mtime, but clip to ustar limits. */ 1287228753Smm s = archive_entry_mtime(entry_main); 1288228753Smm if (s < 0) { s = 0; } 1289228753Smm if (s >= 0x7fffffff) { s = 0x7fffffff; } 1290228753Smm archive_entry_set_mtime(pax_attr_entry, s, 0); 1291228753Smm 1292228753Smm /* Standard ustar doesn't support atime. */ 1293228753Smm archive_entry_set_atime(pax_attr_entry, 0, 0); 1294228753Smm 1295228753Smm /* Standard ustar doesn't support ctime. */ 1296228753Smm archive_entry_set_ctime(pax_attr_entry, 0, 0); 1297228753Smm 1298228753Smm r = __archive_write_format_header_ustar(a, paxbuff, 1299232153Smm pax_attr_entry, 'x', 1, NULL); 1300228753Smm 1301228753Smm archive_entry_free(pax_attr_entry); 1302228753Smm 1303228753Smm /* Note that the 'x' header shouldn't ever fail to format */ 1304232153Smm if (r < ARCHIVE_WARN) { 1305232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1306232153Smm "archive_write_pax_header: " 1307232153Smm "'x' header failed?! This can't happen.\n"); 1308232153Smm return (ARCHIVE_FATAL); 1309232153Smm } else if (r < ret) 1310232153Smm ret = r; 1311232153Smm r = __archive_write_output(a, paxbuff, 512); 1312228753Smm if (r != ARCHIVE_OK) { 1313232153Smm sparse_list_clear(pax); 1314228753Smm pax->entry_bytes_remaining = 0; 1315228753Smm pax->entry_padding = 0; 1316228753Smm return (ARCHIVE_FATAL); 1317228753Smm } 1318228753Smm 1319228753Smm pax->entry_bytes_remaining = archive_strlen(&(pax->pax_header)); 1320232153Smm pax->entry_padding = 1321232153Smm 0x1ff & (-(int64_t)pax->entry_bytes_remaining); 1322228753Smm 1323232153Smm r = __archive_write_output(a, pax->pax_header.s, 1324228753Smm archive_strlen(&(pax->pax_header))); 1325228753Smm if (r != ARCHIVE_OK) { 1326228753Smm /* If a write fails, we're pretty much toast. */ 1327228753Smm return (ARCHIVE_FATAL); 1328228753Smm } 1329228753Smm /* Pad out the end of the entry. */ 1330238856Smm r = __archive_write_nulls(a, (size_t)pax->entry_padding); 1331228753Smm if (r != ARCHIVE_OK) { 1332228753Smm /* If a write fails, we're pretty much toast. */ 1333228753Smm return (ARCHIVE_FATAL); 1334228753Smm } 1335228753Smm pax->entry_bytes_remaining = pax->entry_padding = 0; 1336228753Smm } 1337228753Smm 1338228753Smm /* Write the header for main entry. */ 1339232153Smm r = __archive_write_output(a, ustarbuff, 512); 1340228753Smm if (r != ARCHIVE_OK) 1341228753Smm return (r); 1342228753Smm 1343228753Smm /* 1344228753Smm * Inform the client of the on-disk size we're using, so 1345228753Smm * they can avoid unnecessarily writing a body for something 1346228753Smm * that we're just going to ignore. 1347228753Smm */ 1348232153Smm archive_entry_set_size(entry_original, real_size); 1349232153Smm if (pax->sparse_list == NULL && real_size > 0) { 1350232153Smm /* This is not a sparse file but we handle its data as 1351232153Smm * a sparse block. */ 1352232153Smm sparse_list_add(pax, 0, real_size); 1353232153Smm sparse_total = real_size; 1354232153Smm } 1355232153Smm pax->entry_padding = 0x1ff & (-(int64_t)sparse_total); 1356228753Smm archive_entry_free(entry_main); 1357232153Smm archive_string_free(&entry_name); 1358228753Smm 1359228753Smm return (ret); 1360228753Smm} 1361228753Smm 1362228753Smm/* 1363228753Smm * We need a valid name for the regular 'ustar' entry. This routine 1364228753Smm * tries to hack something more-or-less reasonable. 1365228753Smm * 1366228753Smm * The approach here tries to preserve leading dir names. We do so by 1367228753Smm * working with four sections: 1368228753Smm * 1) "prefix" directory names, 1369228753Smm * 2) "suffix" directory names, 1370228753Smm * 3) inserted dir name (optional), 1371228753Smm * 4) filename. 1372228753Smm * 1373228753Smm * These sections must satisfy the following requirements: 1374228753Smm * * Parts 1 & 2 together form an initial portion of the dir name. 1375228753Smm * * Part 3 is specified by the caller. (It should not contain a leading 1376228753Smm * or trailing '/'.) 1377228753Smm * * Part 4 forms an initial portion of the base filename. 1378228753Smm * * The filename must be <= 99 chars to fit the ustar 'name' field. 1379228753Smm * * Parts 2, 3, 4 together must be <= 99 chars to fit the ustar 'name' fld. 1380228753Smm * * Part 1 must be <= 155 chars to fit the ustar 'prefix' field. 1381228753Smm * * If the original name ends in a '/', the new name must also end in a '/' 1382228753Smm * * Trailing '/.' sequences may be stripped. 1383228753Smm * 1384228753Smm * Note: Recall that the ustar format does not store the '/' separating 1385228753Smm * parts 1 & 2, but does store the '/' separating parts 2 & 3. 1386228753Smm */ 1387228753Smmstatic char * 1388228753Smmbuild_ustar_entry_name(char *dest, const char *src, size_t src_length, 1389228753Smm const char *insert) 1390228753Smm{ 1391228753Smm const char *prefix, *prefix_end; 1392228753Smm const char *suffix, *suffix_end; 1393228753Smm const char *filename, *filename_end; 1394228753Smm char *p; 1395228753Smm int need_slash = 0; /* Was there a trailing slash? */ 1396228753Smm size_t suffix_length = 99; 1397228753Smm size_t insert_length; 1398228753Smm 1399228753Smm /* Length of additional dir element to be added. */ 1400228753Smm if (insert == NULL) 1401228753Smm insert_length = 0; 1402228753Smm else 1403228753Smm /* +2 here allows for '/' before and after the insert. */ 1404228753Smm insert_length = strlen(insert) + 2; 1405228753Smm 1406228753Smm /* Step 0: Quick bailout in a common case. */ 1407228753Smm if (src_length < 100 && insert == NULL) { 1408228753Smm strncpy(dest, src, src_length); 1409228753Smm dest[src_length] = '\0'; 1410228753Smm return (dest); 1411228753Smm } 1412228753Smm 1413228753Smm /* Step 1: Locate filename and enforce the length restriction. */ 1414228753Smm filename_end = src + src_length; 1415228753Smm /* Remove trailing '/' chars and '/.' pairs. */ 1416228753Smm for (;;) { 1417228753Smm if (filename_end > src && filename_end[-1] == '/') { 1418228753Smm filename_end --; 1419228753Smm need_slash = 1; /* Remember to restore trailing '/'. */ 1420228753Smm continue; 1421228753Smm } 1422228753Smm if (filename_end > src + 1 && filename_end[-1] == '.' 1423228753Smm && filename_end[-2] == '/') { 1424228753Smm filename_end -= 2; 1425228753Smm need_slash = 1; /* "foo/." will become "foo/" */ 1426228753Smm continue; 1427228753Smm } 1428228753Smm break; 1429228753Smm } 1430228753Smm if (need_slash) 1431228753Smm suffix_length--; 1432228753Smm /* Find start of filename. */ 1433228753Smm filename = filename_end - 1; 1434228753Smm while ((filename > src) && (*filename != '/')) 1435228753Smm filename --; 1436228753Smm if ((*filename == '/') && (filename < filename_end - 1)) 1437228753Smm filename ++; 1438228753Smm /* Adjust filename_end so that filename + insert fits in 99 chars. */ 1439228753Smm suffix_length -= insert_length; 1440228753Smm if (filename_end > filename + suffix_length) 1441228753Smm filename_end = filename + suffix_length; 1442228753Smm /* Calculate max size for "suffix" section (#3 above). */ 1443228753Smm suffix_length -= filename_end - filename; 1444228753Smm 1445228753Smm /* Step 2: Locate the "prefix" section of the dirname, including 1446228753Smm * trailing '/'. */ 1447228753Smm prefix = src; 1448228753Smm prefix_end = prefix + 155; 1449228753Smm if (prefix_end > filename) 1450228753Smm prefix_end = filename; 1451228753Smm while (prefix_end > prefix && *prefix_end != '/') 1452228753Smm prefix_end--; 1453228753Smm if ((prefix_end < filename) && (*prefix_end == '/')) 1454228753Smm prefix_end++; 1455228753Smm 1456228753Smm /* Step 3: Locate the "suffix" section of the dirname, 1457228753Smm * including trailing '/'. */ 1458228753Smm suffix = prefix_end; 1459228753Smm suffix_end = suffix + suffix_length; /* Enforce limit. */ 1460228753Smm if (suffix_end > filename) 1461228753Smm suffix_end = filename; 1462228753Smm if (suffix_end < suffix) 1463228753Smm suffix_end = suffix; 1464228753Smm while (suffix_end > suffix && *suffix_end != '/') 1465228753Smm suffix_end--; 1466228753Smm if ((suffix_end < filename) && (*suffix_end == '/')) 1467228753Smm suffix_end++; 1468228753Smm 1469228753Smm /* Step 4: Build the new name. */ 1470228753Smm /* The OpenBSD strlcpy function is safer, but less portable. */ 1471228753Smm /* Rather than maintain two versions, just use the strncpy version. */ 1472228753Smm p = dest; 1473228753Smm if (prefix_end > prefix) { 1474228753Smm strncpy(p, prefix, prefix_end - prefix); 1475228753Smm p += prefix_end - prefix; 1476228753Smm } 1477228753Smm if (suffix_end > suffix) { 1478228753Smm strncpy(p, suffix, suffix_end - suffix); 1479228753Smm p += suffix_end - suffix; 1480228753Smm } 1481228753Smm if (insert != NULL) { 1482228753Smm /* Note: assume insert does not have leading or trailing '/' */ 1483228753Smm strcpy(p, insert); 1484228753Smm p += strlen(insert); 1485228753Smm *p++ = '/'; 1486228753Smm } 1487228753Smm strncpy(p, filename, filename_end - filename); 1488228753Smm p += filename_end - filename; 1489228753Smm if (need_slash) 1490228753Smm *p++ = '/'; 1491228753Smm *p = '\0'; 1492228753Smm 1493228753Smm return (dest); 1494228753Smm} 1495228753Smm 1496228753Smm/* 1497228753Smm * The ustar header for the pax extended attributes must have a 1498228753Smm * reasonable name: SUSv3 requires 'dirname'/PaxHeader.'pid'/'filename' 1499228753Smm * where 'pid' is the PID of the archiving process. Unfortunately, 1500228753Smm * that makes testing a pain since the output varies for each run, 1501228753Smm * so I'm sticking with the simpler 'dirname'/PaxHeader/'filename' 1502228753Smm * for now. (Someday, I'll make this settable. Then I can use the 1503228753Smm * SUS recommendation as default and test harnesses can override it 1504228753Smm * to get predictable results.) 1505228753Smm * 1506228753Smm * Joerg Schilling has argued that this is unnecessary because, in 1507228753Smm * practice, if the pax extended attributes get extracted as regular 1508232153Smm * files, no one is going to bother reading those attributes to 1509228753Smm * manually restore them. Based on this, 'star' uses 1510228753Smm * /tmp/PaxHeader/'basename' as the ustar header name. This is a 1511228753Smm * tempting argument, in part because it's simpler than the SUSv3 1512228753Smm * recommendation, but I'm not entirely convinced. I'm also 1513228753Smm * uncomfortable with the fact that "/tmp" is a Unix-ism. 1514228753Smm * 1515228753Smm * The following routine leverages build_ustar_entry_name() above and 1516228753Smm * so is simpler than you might think. It just needs to provide the 1517228753Smm * additional path element and handle a few pathological cases). 1518228753Smm */ 1519228753Smmstatic char * 1520228753Smmbuild_pax_attribute_name(char *dest, const char *src) 1521228753Smm{ 1522228753Smm char buff[64]; 1523228753Smm const char *p; 1524228753Smm 1525228753Smm /* Handle the null filename case. */ 1526228753Smm if (src == NULL || *src == '\0') { 1527228753Smm strcpy(dest, "PaxHeader/blank"); 1528228753Smm return (dest); 1529228753Smm } 1530228753Smm 1531228753Smm /* Prune final '/' and other unwanted final elements. */ 1532228753Smm p = src + strlen(src); 1533228753Smm for (;;) { 1534228753Smm /* Ends in "/", remove the '/' */ 1535228753Smm if (p > src && p[-1] == '/') { 1536228753Smm --p; 1537228753Smm continue; 1538228753Smm } 1539228753Smm /* Ends in "/.", remove the '.' */ 1540228753Smm if (p > src + 1 && p[-1] == '.' 1541228753Smm && p[-2] == '/') { 1542228753Smm --p; 1543228753Smm continue; 1544228753Smm } 1545228753Smm break; 1546228753Smm } 1547228753Smm 1548228753Smm /* Pathological case: After above, there was nothing left. 1549228753Smm * This includes "/." "/./." "/.//./." etc. */ 1550228753Smm if (p == src) { 1551228753Smm strcpy(dest, "/PaxHeader/rootdir"); 1552228753Smm return (dest); 1553228753Smm } 1554228753Smm 1555228753Smm /* Convert unadorned "." into a suitable filename. */ 1556228753Smm if (*src == '.' && p == src + 1) { 1557228753Smm strcpy(dest, "PaxHeader/currentdir"); 1558228753Smm return (dest); 1559228753Smm } 1560228753Smm 1561228753Smm /* 1562228753Smm * TODO: Push this string into the 'pax' structure to avoid 1563228753Smm * recomputing it every time. That will also open the door 1564228753Smm * to having clients override it. 1565228753Smm */ 1566228753Smm#if HAVE_GETPID && 0 /* Disable this for now; see above comment. */ 1567228753Smm sprintf(buff, "PaxHeader.%d", getpid()); 1568228753Smm#else 1569228753Smm /* If the platform can't fetch the pid, don't include it. */ 1570228753Smm strcpy(buff, "PaxHeader"); 1571228753Smm#endif 1572232153Smm /* General case: build a ustar-compatible name adding 1573232153Smm * "/PaxHeader/". */ 1574228753Smm build_ustar_entry_name(dest, src, p - src, buff); 1575228753Smm 1576228753Smm return (dest); 1577228753Smm} 1578228753Smm 1579232153Smm/* 1580232153Smm * GNU PAX Format 1.0 requires the special name, which pattern is: 1581232153Smm * <dir>/GNUSparseFile.<pid>/<original file name> 1582232153Smm * 1583232153Smm * This function is used for only Sparse file, a file type of which 1584232153Smm * is regular file. 1585232153Smm */ 1586232153Smmstatic char * 1587232153Smmbuild_gnu_sparse_name(char *dest, const char *src) 1588228753Smm{ 1589232153Smm char buff[64]; 1590232153Smm const char *p; 1591228753Smm 1592232153Smm /* Handle the null filename case. */ 1593232153Smm if (src == NULL || *src == '\0') { 1594232153Smm strcpy(dest, "GNUSparseFile/blank"); 1595232153Smm return (dest); 1596232153Smm } 1597228753Smm 1598232153Smm /* Prune final '/' and other unwanted final elements. */ 1599232153Smm p = src + strlen(src); 1600232153Smm for (;;) { 1601232153Smm /* Ends in "/", remove the '/' */ 1602232153Smm if (p > src && p[-1] == '/') { 1603232153Smm --p; 1604232153Smm continue; 1605232153Smm } 1606232153Smm /* Ends in "/.", remove the '.' */ 1607232153Smm if (p > src + 1 && p[-1] == '.' 1608232153Smm && p[-2] == '/') { 1609232153Smm --p; 1610232153Smm continue; 1611232153Smm } 1612232153Smm break; 1613232153Smm } 1614232153Smm 1615232153Smm#if HAVE_GETPID && 0 /* Disable this as pax attribute name. */ 1616232153Smm sprintf(buff, "GNUSparseFile.%d", getpid()); 1617232153Smm#else 1618232153Smm /* If the platform can't fetch the pid, don't include it. */ 1619232153Smm strcpy(buff, "GNUSparseFile"); 1620232153Smm#endif 1621232153Smm /* General case: build a ustar-compatible name adding 1622232153Smm * "/GNUSparseFile/". */ 1623232153Smm build_ustar_entry_name(dest, src, p - src, buff); 1624232153Smm 1625232153Smm return (dest); 1626228753Smm} 1627228753Smm 1628232153Smm/* Write two null blocks for the end of archive */ 1629228753Smmstatic int 1630232153Smmarchive_write_pax_close(struct archive_write *a) 1631228753Smm{ 1632232153Smm return (__archive_write_nulls(a, 512 * 2)); 1633232153Smm} 1634232153Smm 1635232153Smmstatic int 1636232153Smmarchive_write_pax_free(struct archive_write *a) 1637232153Smm{ 1638228753Smm struct pax *pax; 1639228753Smm 1640228753Smm pax = (struct pax *)a->format_data; 1641228753Smm if (pax == NULL) 1642228753Smm return (ARCHIVE_OK); 1643228753Smm 1644228753Smm archive_string_free(&pax->pax_header); 1645232153Smm archive_string_free(&pax->sparse_map); 1646232153Smm archive_string_free(&pax->l_url_encoded_name); 1647232153Smm sparse_list_clear(pax); 1648228753Smm free(pax); 1649228753Smm a->format_data = NULL; 1650228753Smm return (ARCHIVE_OK); 1651228753Smm} 1652228753Smm 1653228753Smmstatic int 1654228753Smmarchive_write_pax_finish_entry(struct archive_write *a) 1655228753Smm{ 1656228753Smm struct pax *pax; 1657232153Smm uint64_t remaining; 1658228753Smm int ret; 1659228753Smm 1660228753Smm pax = (struct pax *)a->format_data; 1661232153Smm remaining = pax->entry_bytes_remaining; 1662232153Smm if (remaining == 0) { 1663232153Smm while (pax->sparse_list) { 1664232153Smm struct sparse_block *sb; 1665232153Smm if (!pax->sparse_list->is_hole) 1666232153Smm remaining += pax->sparse_list->remaining; 1667232153Smm sb = pax->sparse_list->next; 1668232153Smm free(pax->sparse_list); 1669232153Smm pax->sparse_list = sb; 1670232153Smm } 1671232153Smm } 1672238856Smm ret = __archive_write_nulls(a, (size_t)(remaining + pax->entry_padding)); 1673228753Smm pax->entry_bytes_remaining = pax->entry_padding = 0; 1674228753Smm return (ret); 1675228753Smm} 1676228753Smm 1677232153Smmstatic ssize_t 1678232153Smmarchive_write_pax_data(struct archive_write *a, const void *buff, size_t s) 1679228753Smm{ 1680232153Smm struct pax *pax; 1681232153Smm size_t ws; 1682232153Smm size_t total; 1683228753Smm int ret; 1684228753Smm 1685232153Smm pax = (struct pax *)a->format_data; 1686232153Smm 1687232153Smm /* 1688232153Smm * According to GNU PAX format 1.0, write a sparse map 1689232153Smm * before the body. 1690232153Smm */ 1691232153Smm if (archive_strlen(&(pax->sparse_map))) { 1692232153Smm ret = __archive_write_output(a, pax->sparse_map.s, 1693232153Smm archive_strlen(&(pax->sparse_map))); 1694228753Smm if (ret != ARCHIVE_OK) 1695228753Smm return (ret); 1696232153Smm ret = __archive_write_nulls(a, pax->sparse_map_padding); 1697232153Smm if (ret != ARCHIVE_OK) 1698232153Smm return (ret); 1699232153Smm archive_string_empty(&(pax->sparse_map)); 1700228753Smm } 1701228753Smm 1702232153Smm total = 0; 1703232153Smm while (total < s) { 1704232153Smm const unsigned char *p; 1705228753Smm 1706232153Smm while (pax->sparse_list != NULL && 1707232153Smm pax->sparse_list->remaining == 0) { 1708232153Smm struct sparse_block *sb = pax->sparse_list->next; 1709232153Smm free(pax->sparse_list); 1710232153Smm pax->sparse_list = sb; 1711232153Smm } 1712228753Smm 1713232153Smm if (pax->sparse_list == NULL) 1714232153Smm return (total); 1715232153Smm 1716232153Smm p = ((const unsigned char *)buff) + total; 1717232153Smm ws = s - total; 1718232153Smm if (ws > pax->sparse_list->remaining) 1719238856Smm ws = (size_t)pax->sparse_list->remaining; 1720232153Smm 1721232153Smm if (pax->sparse_list->is_hole) { 1722232153Smm /* Current block is hole thus we do not write 1723232153Smm * the body. */ 1724232153Smm pax->sparse_list->remaining -= ws; 1725232153Smm total += ws; 1726232153Smm continue; 1727232153Smm } 1728232153Smm 1729232153Smm ret = __archive_write_output(a, p, ws); 1730232153Smm pax->sparse_list->remaining -= ws; 1731232153Smm total += ws; 1732232153Smm if (ret != ARCHIVE_OK) 1733232153Smm return (ret); 1734232153Smm } 1735232153Smm return (total); 1736228753Smm} 1737228753Smm 1738228753Smmstatic int 1739232153Smmhas_non_ASCII(const char *_p) 1740228753Smm{ 1741232153Smm const unsigned char *p = (const unsigned char *)_p; 1742232153Smm 1743232153Smm if (p == NULL) 1744228753Smm return (1); 1745232153Smm while (*p != '\0' && *p < 128) 1746232153Smm p++; 1747232153Smm return (*p != '\0'); 1748228753Smm} 1749228753Smm 1750228753Smm/* 1751228753Smm * Used by extended attribute support; encodes the name 1752228753Smm * so that there will be no '=' characters in the result. 1753228753Smm */ 1754228753Smmstatic char * 1755228753Smmurl_encode(const char *in) 1756228753Smm{ 1757228753Smm const char *s; 1758228753Smm char *d; 1759228753Smm int out_len = 0; 1760228753Smm char *out; 1761228753Smm 1762228753Smm for (s = in; *s != '\0'; s++) { 1763228753Smm if (*s < 33 || *s > 126 || *s == '%' || *s == '=') 1764228753Smm out_len += 3; 1765228753Smm else 1766228753Smm out_len++; 1767228753Smm } 1768228753Smm 1769228753Smm out = (char *)malloc(out_len + 1); 1770228753Smm if (out == NULL) 1771228753Smm return (NULL); 1772228753Smm 1773228753Smm for (s = in, d = out; *s != '\0'; s++) { 1774228753Smm /* encode any non-printable ASCII character or '%' or '=' */ 1775228753Smm if (*s < 33 || *s > 126 || *s == '%' || *s == '=') { 1776228753Smm /* URL encoding is '%' followed by two hex digits */ 1777228753Smm *d++ = '%'; 1778228753Smm *d++ = "0123456789ABCDEF"[0x0f & (*s >> 4)]; 1779228753Smm *d++ = "0123456789ABCDEF"[0x0f & *s]; 1780228753Smm } else { 1781228753Smm *d++ = *s; 1782228753Smm } 1783228753Smm } 1784228753Smm *d = '\0'; 1785228753Smm return (out); 1786228753Smm} 1787228753Smm 1788228753Smm/* 1789228753Smm * Encode a sequence of bytes into a C string using base-64 encoding. 1790228753Smm * 1791228753Smm * Returns a null-terminated C string allocated with malloc(); caller 1792228753Smm * is responsible for freeing the result. 1793228753Smm */ 1794228753Smmstatic char * 1795228753Smmbase64_encode(const char *s, size_t len) 1796228753Smm{ 1797228753Smm static const char digits[64] = 1798228753Smm { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', 1799228753Smm 'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d', 1800228753Smm 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s', 1801228753Smm 't','u','v','w','x','y','z','0','1','2','3','4','5','6','7', 1802228753Smm '8','9','+','/' }; 1803228753Smm int v; 1804228753Smm char *d, *out; 1805228753Smm 1806228753Smm /* 3 bytes becomes 4 chars, but round up and allow for trailing NUL */ 1807228753Smm out = (char *)malloc((len * 4 + 2) / 3 + 1); 1808228753Smm if (out == NULL) 1809228753Smm return (NULL); 1810228753Smm d = out; 1811228753Smm 1812228753Smm /* Convert each group of 3 bytes into 4 characters. */ 1813228753Smm while (len >= 3) { 1814228753Smm v = (((int)s[0] << 16) & 0xff0000) 1815228753Smm | (((int)s[1] << 8) & 0xff00) 1816228753Smm | (((int)s[2]) & 0x00ff); 1817228753Smm s += 3; 1818228753Smm len -= 3; 1819228753Smm *d++ = digits[(v >> 18) & 0x3f]; 1820228753Smm *d++ = digits[(v >> 12) & 0x3f]; 1821228753Smm *d++ = digits[(v >> 6) & 0x3f]; 1822228753Smm *d++ = digits[(v) & 0x3f]; 1823228753Smm } 1824228753Smm /* Handle final group of 1 byte (2 chars) or 2 bytes (3 chars). */ 1825228753Smm switch (len) { 1826228753Smm case 0: break; 1827228753Smm case 1: 1828228753Smm v = (((int)s[0] << 16) & 0xff0000); 1829228753Smm *d++ = digits[(v >> 18) & 0x3f]; 1830228753Smm *d++ = digits[(v >> 12) & 0x3f]; 1831228753Smm break; 1832228753Smm case 2: 1833228753Smm v = (((int)s[0] << 16) & 0xff0000) 1834228753Smm | (((int)s[1] << 8) & 0xff00); 1835228753Smm *d++ = digits[(v >> 18) & 0x3f]; 1836228753Smm *d++ = digits[(v >> 12) & 0x3f]; 1837228753Smm *d++ = digits[(v >> 6) & 0x3f]; 1838228753Smm break; 1839228753Smm } 1840228753Smm /* Add trailing NUL character so output is a valid C string. */ 1841228753Smm *d = '\0'; 1842228753Smm return (out); 1843228753Smm} 1844232153Smm 1845232153Smmstatic void 1846232153Smmsparse_list_clear(struct pax *pax) 1847232153Smm{ 1848232153Smm while (pax->sparse_list != NULL) { 1849232153Smm struct sparse_block *sb = pax->sparse_list; 1850232153Smm pax->sparse_list = sb->next; 1851232153Smm free(sb); 1852232153Smm } 1853232153Smm pax->sparse_tail = NULL; 1854232153Smm} 1855232153Smm 1856232153Smmstatic int 1857232153Smm_sparse_list_add_block(struct pax *pax, int64_t offset, int64_t length, 1858232153Smm int is_hole) 1859232153Smm{ 1860232153Smm struct sparse_block *sb; 1861232153Smm 1862232153Smm sb = (struct sparse_block *)malloc(sizeof(*sb)); 1863232153Smm if (sb == NULL) 1864232153Smm return (ARCHIVE_FATAL); 1865232153Smm sb->next = NULL; 1866232153Smm sb->is_hole = is_hole; 1867232153Smm sb->offset = offset; 1868232153Smm sb->remaining = length; 1869248616Smm if (pax->sparse_list == NULL || pax->sparse_tail == NULL) 1870232153Smm pax->sparse_list = pax->sparse_tail = sb; 1871232153Smm else { 1872232153Smm pax->sparse_tail->next = sb; 1873232153Smm pax->sparse_tail = sb; 1874232153Smm } 1875232153Smm return (ARCHIVE_OK); 1876232153Smm} 1877232153Smm 1878232153Smmstatic int 1879232153Smmsparse_list_add(struct pax *pax, int64_t offset, int64_t length) 1880232153Smm{ 1881232153Smm int64_t last_offset; 1882232153Smm int r; 1883232153Smm 1884232153Smm if (pax->sparse_tail == NULL) 1885232153Smm last_offset = 0; 1886232153Smm else { 1887232153Smm last_offset = pax->sparse_tail->offset + 1888232153Smm pax->sparse_tail->remaining; 1889232153Smm } 1890232153Smm if (last_offset < offset) { 1891232153Smm /* Add a hole block. */ 1892232153Smm r = _sparse_list_add_block(pax, last_offset, 1893232153Smm offset - last_offset, 1); 1894232153Smm if (r != ARCHIVE_OK) 1895232153Smm return (r); 1896232153Smm } 1897232153Smm /* Add data block. */ 1898232153Smm return (_sparse_list_add_block(pax, offset, length, 0)); 1899232153Smm} 1900232153Smm 1901