1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3232153Smm * Copyright (c) 2010-2012 Michihiro NAKAJIMA 4313570Smm * Copyright (c) 2016 Martin Matuska 5228753Smm * All rights reserved. 6228753Smm * 7228753Smm * Redistribution and use in source and binary forms, with or without 8228753Smm * modification, are permitted provided that the following conditions 9228753Smm * are met: 10228753Smm * 1. Redistributions of source code must retain the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer. 12228753Smm * 2. Redistributions in binary form must reproduce the above copyright 13228753Smm * notice, this list of conditions and the following disclaimer in the 14228753Smm * documentation and/or other materials provided with the distribution. 15228753Smm * 16228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 17228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 20228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26228753Smm */ 27228753Smm 28228753Smm#include "archive_platform.h" 29228763Smm__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/archive_write_set_format_pax.c 358088 2020-02-19 01:50:47Z mm $"); 30228753Smm 31228753Smm#ifdef HAVE_ERRNO_H 32228753Smm#include <errno.h> 33228753Smm#endif 34228753Smm#ifdef HAVE_STDLIB_H 35228753Smm#include <stdlib.h> 36228753Smm#endif 37228753Smm#ifdef HAVE_STRING_H 38228753Smm#include <string.h> 39228753Smm#endif 40228753Smm 41228753Smm#include "archive.h" 42228753Smm#include "archive_entry.h" 43232153Smm#include "archive_entry_locale.h" 44228753Smm#include "archive_private.h" 45228753Smm#include "archive_write_private.h" 46358088Smm#include "archive_write_set_format_private.h" 47228753Smm 48232153Smmstruct sparse_block { 49232153Smm struct sparse_block *next; 50232153Smm int is_hole; 51232153Smm uint64_t offset; 52232153Smm uint64_t remaining; 53232153Smm}; 54232153Smm 55228753Smmstruct pax { 56228753Smm uint64_t entry_bytes_remaining; 57228753Smm uint64_t entry_padding; 58232153Smm struct archive_string l_url_encoded_name; 59228753Smm struct archive_string pax_header; 60232153Smm struct archive_string sparse_map; 61232153Smm size_t sparse_map_padding; 62232153Smm struct sparse_block *sparse_list; 63232153Smm struct sparse_block *sparse_tail; 64232153Smm struct archive_string_conv *sconv_utf8; 65232153Smm int opt_binary; 66313570Smm 67313570Smm unsigned flags; 68313570Smm#define WRITE_SCHILY_XATTR (1 << 0) 69313570Smm#define WRITE_LIBARCHIVE_XATTR (1 << 1) 70228753Smm}; 71228753Smm 72228753Smmstatic void add_pax_attr(struct archive_string *, const char *key, 73228753Smm const char *value); 74313570Smmstatic void add_pax_attr_binary(struct archive_string *, 75313570Smm const char *key, 76313570Smm const char *value, size_t value_len); 77228753Smmstatic void add_pax_attr_int(struct archive_string *, 78228753Smm const char *key, int64_t value); 79228753Smmstatic void add_pax_attr_time(struct archive_string *, 80228753Smm const char *key, int64_t sec, 81228753Smm unsigned long nanos); 82313570Smmstatic int add_pax_acl(struct archive_write *, 83313570Smm struct archive_entry *, struct pax *, int); 84228753Smmstatic ssize_t archive_write_pax_data(struct archive_write *, 85228753Smm const void *, size_t); 86232153Smmstatic int archive_write_pax_close(struct archive_write *); 87232153Smmstatic int archive_write_pax_free(struct archive_write *); 88228753Smmstatic int archive_write_pax_finish_entry(struct archive_write *); 89228753Smmstatic int archive_write_pax_header(struct archive_write *, 90228753Smm struct archive_entry *); 91232153Smmstatic int archive_write_pax_options(struct archive_write *, 92232153Smm const char *, const char *); 93228753Smmstatic char *base64_encode(const char *src, size_t len); 94232153Smmstatic char *build_gnu_sparse_name(char *dest, const char *src); 95228753Smmstatic char *build_pax_attribute_name(char *dest, const char *src); 96228753Smmstatic char *build_ustar_entry_name(char *dest, const char *src, 97228753Smm size_t src_length, const char *insert); 98228753Smmstatic char *format_int(char *dest, int64_t); 99232153Smmstatic int has_non_ASCII(const char *); 100232153Smmstatic void sparse_list_clear(struct pax *); 101232153Smmstatic int sparse_list_add(struct pax *, int64_t, int64_t); 102228753Smmstatic char *url_encode(const char *in); 103228753Smm 104228753Smm/* 105228753Smm * Set output format to 'restricted pax' format. 106228753Smm * 107228753Smm * This is the same as normal 'pax', but tries to suppress 108228753Smm * the pax header whenever possible. This is the default for 109228753Smm * bsdtar, for instance. 110228753Smm */ 111228753Smmint 112228753Smmarchive_write_set_format_pax_restricted(struct archive *_a) 113228753Smm{ 114228753Smm struct archive_write *a = (struct archive_write *)_a; 115228753Smm int r; 116232153Smm 117232153Smm archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, 118232153Smm ARCHIVE_STATE_NEW, "archive_write_set_format_pax_restricted"); 119232153Smm 120228753Smm r = archive_write_set_format_pax(&a->archive); 121228753Smm a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED; 122228753Smm a->archive.archive_format_name = "restricted POSIX pax interchange"; 123228753Smm return (r); 124228753Smm} 125228753Smm 126228753Smm/* 127228753Smm * Set output format to 'pax' format. 128228753Smm */ 129228753Smmint 130228753Smmarchive_write_set_format_pax(struct archive *_a) 131228753Smm{ 132228753Smm struct archive_write *a = (struct archive_write *)_a; 133228753Smm struct pax *pax; 134228753Smm 135232153Smm archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, 136232153Smm ARCHIVE_STATE_NEW, "archive_write_set_format_pax"); 137228753Smm 138232153Smm if (a->format_free != NULL) 139232153Smm (a->format_free)(a); 140232153Smm 141311041Smm pax = (struct pax *)calloc(1, sizeof(*pax)); 142228753Smm if (pax == NULL) { 143232153Smm archive_set_error(&a->archive, ENOMEM, 144232153Smm "Can't allocate pax data"); 145228753Smm return (ARCHIVE_FATAL); 146228753Smm } 147313570Smm pax->flags = WRITE_LIBARCHIVE_XATTR | WRITE_SCHILY_XATTR; 148313570Smm 149228753Smm a->format_data = pax; 150228753Smm a->format_name = "pax"; 151232153Smm a->format_options = archive_write_pax_options; 152228753Smm a->format_write_header = archive_write_pax_header; 153228753Smm a->format_write_data = archive_write_pax_data; 154232153Smm a->format_close = archive_write_pax_close; 155232153Smm a->format_free = archive_write_pax_free; 156228753Smm a->format_finish_entry = archive_write_pax_finish_entry; 157228753Smm a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; 158228753Smm a->archive.archive_format_name = "POSIX pax interchange"; 159228753Smm return (ARCHIVE_OK); 160228753Smm} 161228753Smm 162232153Smmstatic int 163232153Smmarchive_write_pax_options(struct archive_write *a, const char *key, 164232153Smm const char *val) 165232153Smm{ 166232153Smm struct pax *pax = (struct pax *)a->format_data; 167232153Smm int ret = ARCHIVE_FAILED; 168232153Smm 169232153Smm if (strcmp(key, "hdrcharset") == 0) { 170232153Smm /* 171232153Smm * The character-set we can use are defined in 172232153Smm * IEEE Std 1003.1-2001 173232153Smm */ 174232153Smm if (val == NULL || val[0] == 0) 175232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 176232153Smm "pax: hdrcharset option needs a character-set name"); 177232153Smm else if (strcmp(val, "BINARY") == 0 || 178232153Smm strcmp(val, "binary") == 0) { 179232153Smm /* 180232153Smm * Specify binary mode. We will not convert 181232153Smm * filenames, uname and gname to any charsets. 182232153Smm */ 183232153Smm pax->opt_binary = 1; 184232153Smm ret = ARCHIVE_OK; 185232153Smm } else if (strcmp(val, "UTF-8") == 0) { 186232153Smm /* 187232153Smm * Specify UTF-8 character-set to be used for 188232153Smm * filenames. This is almost the test that 189232153Smm * running platform supports the string conversion. 190232153Smm * Especially libarchive_test needs this trick for 191232153Smm * its test. 192232153Smm */ 193232153Smm pax->sconv_utf8 = archive_string_conversion_to_charset( 194232153Smm &(a->archive), "UTF-8", 0); 195232153Smm if (pax->sconv_utf8 == NULL) 196232153Smm ret = ARCHIVE_FATAL; 197232153Smm else 198232153Smm ret = ARCHIVE_OK; 199232153Smm } else 200232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 201232153Smm "pax: invalid charset name"); 202232153Smm return (ret); 203358088Smm } else if (strcmp(key, "xattrheader") == 0) { 204358088Smm if (val == NULL || val[0] == 0) { 205358088Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 206358088Smm "pax: xattrheader requires a value"); 207358088Smm } else if (strcmp(val, "ALL") == 0 || 208358088Smm strcmp(val, "all") == 0) { 209358088Smm pax->flags |= WRITE_LIBARCHIVE_XATTR | WRITE_SCHILY_XATTR; 210358088Smm ret = ARCHIVE_OK; 211358088Smm } else if (strcmp(val, "SCHILY") == 0 || 212358088Smm strcmp(val, "schily") == 0) { 213358088Smm pax->flags |= WRITE_SCHILY_XATTR; 214358088Smm pax->flags &= ~WRITE_LIBARCHIVE_XATTR; 215358088Smm ret = ARCHIVE_OK; 216358088Smm } else if (strcmp(val, "LIBARCHIVE") == 0 || 217358088Smm strcmp(val, "libarchive") == 0) { 218358088Smm pax->flags |= WRITE_LIBARCHIVE_XATTR; 219358088Smm pax->flags &= ~WRITE_SCHILY_XATTR; 220358088Smm ret = ARCHIVE_OK; 221358088Smm } else 222358088Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 223358088Smm "pax: invalid xattr header name"); 224358088Smm return (ret); 225232153Smm } 226232153Smm 227232153Smm /* Note: The "warn" return is just to inform the options 228232153Smm * supervisor that we didn't handle it. It will generate 229232153Smm * a suitable error if no one used this option. */ 230232153Smm return (ARCHIVE_WARN); 231232153Smm} 232232153Smm 233228753Smm/* 234228753Smm * Note: This code assumes that 'nanos' has the same sign as 'sec', 235228753Smm * which implies that sec=-1, nanos=200000000 represents -1.2 seconds 236228753Smm * and not -0.8 seconds. This is a pretty pedantic point, as we're 237228753Smm * unlikely to encounter many real files created before Jan 1, 1970, 238228753Smm * much less ones with timestamps recorded to sub-second resolution. 239228753Smm */ 240228753Smmstatic void 241228753Smmadd_pax_attr_time(struct archive_string *as, const char *key, 242228753Smm int64_t sec, unsigned long nanos) 243228753Smm{ 244228753Smm int digit, i; 245228753Smm char *t; 246228753Smm /* 247228753Smm * Note that each byte contributes fewer than 3 base-10 248228753Smm * digits, so this will always be big enough. 249228753Smm */ 250228753Smm char tmp[1 + 3*sizeof(sec) + 1 + 3*sizeof(nanos)]; 251228753Smm 252228753Smm tmp[sizeof(tmp) - 1] = 0; 253228753Smm t = tmp + sizeof(tmp) - 1; 254228753Smm 255228753Smm /* Skip trailing zeros in the fractional part. */ 256228753Smm for (digit = 0, i = 10; i > 0 && digit == 0; i--) { 257228753Smm digit = nanos % 10; 258228753Smm nanos /= 10; 259228753Smm } 260228753Smm 261228753Smm /* Only format the fraction if it's non-zero. */ 262228753Smm if (i > 0) { 263228753Smm while (i > 0) { 264228753Smm *--t = "0123456789"[digit]; 265228753Smm digit = nanos % 10; 266228753Smm nanos /= 10; 267228753Smm i--; 268228753Smm } 269228753Smm *--t = '.'; 270228753Smm } 271228753Smm t = format_int(t, sec); 272228753Smm 273228753Smm add_pax_attr(as, key, t); 274228753Smm} 275228753Smm 276228753Smmstatic char * 277228753Smmformat_int(char *t, int64_t i) 278228753Smm{ 279232153Smm uint64_t ui; 280228753Smm 281232153Smm if (i < 0) 282232153Smm ui = (i == INT64_MIN) ? (uint64_t)(INT64_MAX) + 1 : (uint64_t)(-i); 283232153Smm else 284232153Smm ui = i; 285228753Smm 286228753Smm do { 287232153Smm *--t = "0123456789"[ui % 10]; 288232153Smm } while (ui /= 10); 289232153Smm if (i < 0) 290228753Smm *--t = '-'; 291228753Smm return (t); 292228753Smm} 293228753Smm 294228753Smmstatic void 295228753Smmadd_pax_attr_int(struct archive_string *as, const char *key, int64_t value) 296228753Smm{ 297228753Smm char tmp[1 + 3 * sizeof(value)]; 298228753Smm 299228753Smm tmp[sizeof(tmp) - 1] = 0; 300228753Smm add_pax_attr(as, key, format_int(tmp + sizeof(tmp) - 1, value)); 301228753Smm} 302228753Smm 303228753Smm/* 304228753Smm * Add a key/value attribute to the pax header. This function handles 305228753Smm * the length field and various other syntactic requirements. 306228753Smm */ 307228753Smmstatic void 308228753Smmadd_pax_attr(struct archive_string *as, const char *key, const char *value) 309228753Smm{ 310313570Smm add_pax_attr_binary(as, key, value, strlen(value)); 311313570Smm} 312313570Smm 313313570Smm/* 314313570Smm * Add a key/value attribute to the pax header. This function handles 315313570Smm * binary values. 316313570Smm */ 317313570Smmstatic void 318313570Smmadd_pax_attr_binary(struct archive_string *as, const char *key, 319313570Smm const char *value, size_t value_len) 320313570Smm{ 321228753Smm int digits, i, len, next_ten; 322228753Smm char tmp[1 + 3 * sizeof(int)]; /* < 3 base-10 digits per byte */ 323228753Smm 324228753Smm /*- 325228753Smm * PAX attributes have the following layout: 326228753Smm * <len> <space> <key> <=> <value> <nl> 327228753Smm */ 328313570Smm len = 1 + (int)strlen(key) + 1 + (int)value_len + 1; 329228753Smm 330228753Smm /* 331228753Smm * The <len> field includes the length of the <len> field, so 332228753Smm * computing the correct length is tricky. I start by 333228753Smm * counting the number of base-10 digits in 'len' and 334228753Smm * computing the next higher power of 10. 335228753Smm */ 336228753Smm next_ten = 1; 337228753Smm digits = 0; 338228753Smm i = len; 339228753Smm while (i > 0) { 340228753Smm i = i / 10; 341228753Smm digits++; 342228753Smm next_ten = next_ten * 10; 343228753Smm } 344228753Smm /* 345228753Smm * For example, if string without the length field is 99 346228753Smm * chars, then adding the 2 digit length "99" will force the 347228753Smm * total length past 100, requiring an extra digit. The next 348228753Smm * statement adjusts for this effect. 349228753Smm */ 350228753Smm if (len + digits >= next_ten) 351228753Smm digits++; 352228753Smm 353228753Smm /* Now, we have the right length so we can build the line. */ 354228753Smm tmp[sizeof(tmp) - 1] = 0; /* Null-terminate the work area. */ 355228753Smm archive_strcat(as, format_int(tmp + sizeof(tmp) - 1, len + digits)); 356228753Smm archive_strappend_char(as, ' '); 357228753Smm archive_strcat(as, key); 358228753Smm archive_strappend_char(as, '='); 359313570Smm archive_array_append(as, value, value_len); 360228753Smm archive_strappend_char(as, '\n'); 361228753Smm} 362228753Smm 363313570Smmstatic void 364313570Smmarchive_write_pax_header_xattr(struct pax *pax, const char *encoded_name, 365313570Smm const void *value, size_t value_len) 366313570Smm{ 367313570Smm struct archive_string s; 368313570Smm char *encoded_value; 369313570Smm 370313570Smm if (pax->flags & WRITE_LIBARCHIVE_XATTR) { 371313570Smm encoded_value = base64_encode((const char *)value, value_len); 372313570Smm 373313570Smm if (encoded_name != NULL && encoded_value != NULL) { 374313570Smm archive_string_init(&s); 375313570Smm archive_strcpy(&s, "LIBARCHIVE.xattr."); 376313570Smm archive_strcat(&s, encoded_name); 377313570Smm add_pax_attr(&(pax->pax_header), s.s, encoded_value); 378313570Smm archive_string_free(&s); 379313570Smm } 380313570Smm free(encoded_value); 381313570Smm } 382313570Smm if (pax->flags & WRITE_SCHILY_XATTR) { 383313570Smm archive_string_init(&s); 384313570Smm archive_strcpy(&s, "SCHILY.xattr."); 385313570Smm archive_strcat(&s, encoded_name); 386313570Smm add_pax_attr_binary(&(pax->pax_header), s.s, value, value_len); 387313570Smm archive_string_free(&s); 388313570Smm } 389313570Smm} 390313570Smm 391232153Smmstatic int 392232153Smmarchive_write_pax_header_xattrs(struct archive_write *a, 393232153Smm struct pax *pax, struct archive_entry *entry) 394228753Smm{ 395228753Smm int i = archive_entry_xattr_reset(entry); 396228753Smm 397228753Smm while (i--) { 398228753Smm const char *name; 399228753Smm const void *value; 400228753Smm char *url_encoded_name = NULL, *encoded_name = NULL; 401228753Smm size_t size; 402232153Smm int r; 403228753Smm 404228753Smm archive_entry_xattr_next(entry, &name, &value, &size); 405228753Smm url_encoded_name = url_encode(name); 406228753Smm if (url_encoded_name != NULL) { 407232153Smm /* Convert narrow-character to UTF-8. */ 408238856Smm r = archive_strcpy_l(&(pax->l_url_encoded_name), 409232153Smm url_encoded_name, pax->sconv_utf8); 410228753Smm free(url_encoded_name); /* Done with this. */ 411232153Smm if (r == 0) 412232153Smm encoded_name = pax->l_url_encoded_name.s; 413232153Smm else if (errno == ENOMEM) { 414232153Smm archive_set_error(&a->archive, ENOMEM, 415232153Smm "Can't allocate memory for Linkname"); 416232153Smm return (ARCHIVE_FATAL); 417232153Smm } 418228753Smm } 419228753Smm 420313570Smm archive_write_pax_header_xattr(pax, encoded_name, 421313570Smm value, size); 422228753Smm 423228753Smm } 424232153Smm return (ARCHIVE_OK); 425228753Smm} 426228753Smm 427232153Smmstatic int 428232153Smmget_entry_hardlink(struct archive_write *a, struct archive_entry *entry, 429232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 430232153Smm{ 431232153Smm int r; 432232153Smm 433232153Smm r = archive_entry_hardlink_l(entry, name, length, sc); 434232153Smm if (r != 0) { 435232153Smm if (errno == ENOMEM) { 436232153Smm archive_set_error(&a->archive, ENOMEM, 437232153Smm "Can't allocate memory for Linkname"); 438232153Smm return (ARCHIVE_FATAL); 439232153Smm } 440232153Smm return (ARCHIVE_WARN); 441232153Smm } 442232153Smm return (ARCHIVE_OK); 443232153Smm} 444232153Smm 445232153Smmstatic int 446232153Smmget_entry_pathname(struct archive_write *a, struct archive_entry *entry, 447232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 448232153Smm{ 449232153Smm int r; 450232153Smm 451232153Smm r = archive_entry_pathname_l(entry, name, length, sc); 452232153Smm if (r != 0) { 453232153Smm if (errno == ENOMEM) { 454232153Smm archive_set_error(&a->archive, ENOMEM, 455232153Smm "Can't allocate memory for Pathname"); 456232153Smm return (ARCHIVE_FATAL); 457232153Smm } 458232153Smm return (ARCHIVE_WARN); 459232153Smm } 460232153Smm return (ARCHIVE_OK); 461232153Smm} 462232153Smm 463232153Smmstatic int 464232153Smmget_entry_uname(struct archive_write *a, struct archive_entry *entry, 465232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 466232153Smm{ 467232153Smm int r; 468232153Smm 469232153Smm r = archive_entry_uname_l(entry, name, length, sc); 470232153Smm if (r != 0) { 471232153Smm if (errno == ENOMEM) { 472232153Smm archive_set_error(&a->archive, ENOMEM, 473232153Smm "Can't allocate memory for Uname"); 474232153Smm return (ARCHIVE_FATAL); 475232153Smm } 476232153Smm return (ARCHIVE_WARN); 477232153Smm } 478232153Smm return (ARCHIVE_OK); 479232153Smm} 480232153Smm 481232153Smmstatic int 482232153Smmget_entry_gname(struct archive_write *a, struct archive_entry *entry, 483232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 484232153Smm{ 485232153Smm int r; 486232153Smm 487232153Smm r = archive_entry_gname_l(entry, name, length, sc); 488232153Smm if (r != 0) { 489232153Smm if (errno == ENOMEM) { 490232153Smm archive_set_error(&a->archive, ENOMEM, 491232153Smm "Can't allocate memory for Gname"); 492232153Smm return (ARCHIVE_FATAL); 493232153Smm } 494232153Smm return (ARCHIVE_WARN); 495232153Smm } 496232153Smm return (ARCHIVE_OK); 497232153Smm} 498232153Smm 499232153Smmstatic int 500232153Smmget_entry_symlink(struct archive_write *a, struct archive_entry *entry, 501232153Smm const char **name, size_t *length, struct archive_string_conv *sc) 502232153Smm{ 503232153Smm int r; 504232153Smm 505232153Smm r = archive_entry_symlink_l(entry, name, length, sc); 506232153Smm if (r != 0) { 507232153Smm if (errno == ENOMEM) { 508232153Smm archive_set_error(&a->archive, ENOMEM, 509232153Smm "Can't allocate memory for Linkname"); 510232153Smm return (ARCHIVE_FATAL); 511232153Smm } 512232153Smm return (ARCHIVE_WARN); 513232153Smm } 514232153Smm return (ARCHIVE_OK); 515232153Smm} 516232153Smm 517313570Smm/* Add ACL to pax header */ 518313570Smmstatic int 519313570Smmadd_pax_acl(struct archive_write *a, 520313570Smm struct archive_entry *entry, struct pax *pax, int flags) 521313570Smm{ 522313570Smm char *p; 523313570Smm const char *attr; 524313570Smm int acl_types; 525313570Smm 526313570Smm acl_types = archive_entry_acl_types(entry); 527313570Smm 528313570Smm if ((acl_types & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) 529313570Smm attr = "SCHILY.acl.ace"; 530313570Smm else if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) 531313570Smm attr = "SCHILY.acl.access"; 532313570Smm else if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) 533313570Smm attr = "SCHILY.acl.default"; 534313570Smm else 535313570Smm return (ARCHIVE_FATAL); 536313570Smm 537313570Smm p = archive_entry_acl_to_text_l(entry, NULL, flags, pax->sconv_utf8); 538313570Smm if (p == NULL) { 539313570Smm if (errno == ENOMEM) { 540313570Smm archive_set_error(&a->archive, ENOMEM, "%s %s", 541313570Smm "Can't allocate memory for ", attr); 542313570Smm return (ARCHIVE_FATAL); 543313570Smm } 544313570Smm archive_set_error(&a->archive, 545313570Smm ARCHIVE_ERRNO_FILE_FORMAT, "%s %s %s", 546313570Smm "Can't translate ", attr, " to UTF-8"); 547313570Smm return(ARCHIVE_WARN); 548342360Smm } 549342360Smm 550342360Smm if (*p != '\0') { 551313570Smm add_pax_attr(&(pax->pax_header), 552313570Smm attr, p); 553313570Smm } 554342360Smm free(p); 555313570Smm return(ARCHIVE_OK); 556313570Smm} 557313570Smm 558228753Smm/* 559228753Smm * TODO: Consider adding 'comment' and 'charset' fields to 560228753Smm * archive_entry so that clients can specify them. Also, consider 561228753Smm * adding generic key/value tags so clients can add arbitrary 562228753Smm * key/value data. 563232153Smm * 564232153Smm * TODO: Break up this 700-line function!!!! Yowza! 565228753Smm */ 566228753Smmstatic int 567228753Smmarchive_write_pax_header(struct archive_write *a, 568228753Smm struct archive_entry *entry_original) 569228753Smm{ 570228753Smm struct archive_entry *entry_main; 571228753Smm const char *p; 572228753Smm const char *suffix; 573228753Smm int need_extension, r, ret; 574313570Smm int acl_types; 575232153Smm int sparse_count; 576232153Smm uint64_t sparse_total, real_size; 577228753Smm struct pax *pax; 578228753Smm const char *hardlink; 579228753Smm const char *path = NULL, *linkpath = NULL; 580228753Smm const char *uname = NULL, *gname = NULL; 581232153Smm const void *mac_metadata; 582232153Smm size_t mac_metadata_size; 583232153Smm struct archive_string_conv *sconv; 584232153Smm size_t hardlink_length, path_length, linkpath_length; 585232153Smm size_t uname_length, gname_length; 586228753Smm 587228753Smm char paxbuff[512]; 588228753Smm char ustarbuff[512]; 589228753Smm char ustar_entry_name[256]; 590228753Smm char pax_entry_name[256]; 591232153Smm char gnu_sparse_name[256]; 592232153Smm struct archive_string entry_name; 593228753Smm 594228753Smm ret = ARCHIVE_OK; 595228753Smm need_extension = 0; 596228753Smm pax = (struct pax *)a->format_data; 597228753Smm 598232153Smm /* Sanity check. */ 599232153Smm if (archive_entry_pathname(entry_original) == NULL) { 600232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 601232153Smm "Can't record entry in tar file without pathname"); 602232153Smm return (ARCHIVE_FAILED); 603232153Smm } 604228753Smm 605232153Smm /* 606232153Smm * Choose a header encoding. 607232153Smm */ 608232153Smm if (pax->opt_binary) 609232153Smm sconv = NULL;/* Binary mode. */ 610232153Smm else { 611232153Smm /* Header encoding is UTF-8. */ 612232153Smm if (pax->sconv_utf8 == NULL) { 613232153Smm /* Initialize the string conversion object 614232153Smm * we must need */ 615232153Smm pax->sconv_utf8 = archive_string_conversion_to_charset( 616232153Smm &(a->archive), "UTF-8", 1); 617232153Smm if (pax->sconv_utf8 == NULL) 618232153Smm /* Couldn't allocate memory */ 619232153Smm return (ARCHIVE_FAILED); 620232153Smm } 621232153Smm sconv = pax->sconv_utf8; 622232153Smm } 623232153Smm 624232153Smm r = get_entry_hardlink(a, entry_original, &hardlink, 625232153Smm &hardlink_length, sconv); 626232153Smm if (r == ARCHIVE_FATAL) 627232153Smm return (r); 628232153Smm else if (r != ARCHIVE_OK) { 629232153Smm r = get_entry_hardlink(a, entry_original, &hardlink, 630232153Smm &hardlink_length, NULL); 631232153Smm if (r == ARCHIVE_FATAL) 632232153Smm return (r); 633232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 634232153Smm "Can't translate linkname '%s' to %s", hardlink, 635232153Smm archive_string_conversion_charset_name(sconv)); 636232153Smm ret = ARCHIVE_WARN; 637232153Smm sconv = NULL;/* The header charset switches to binary mode. */ 638232153Smm } 639232153Smm 640228753Smm /* Make sure this is a type of entry that we can handle here */ 641228753Smm if (hardlink == NULL) { 642228753Smm switch (archive_entry_filetype(entry_original)) { 643228753Smm case AE_IFBLK: 644228753Smm case AE_IFCHR: 645228753Smm case AE_IFIFO: 646228753Smm case AE_IFLNK: 647228753Smm case AE_IFREG: 648228753Smm break; 649228753Smm case AE_IFDIR: 650232153Smm { 651228753Smm /* 652228753Smm * Ensure a trailing '/'. Modify the original 653228753Smm * entry so the client sees the change. 654228753Smm */ 655232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 656232153Smm const wchar_t *wp; 657232153Smm 658232153Smm wp = archive_entry_pathname_w(entry_original); 659232153Smm if (wp != NULL && wp[wcslen(wp) -1] != L'/') { 660232153Smm struct archive_wstring ws; 661232153Smm 662232153Smm archive_string_init(&ws); 663232153Smm path_length = wcslen(wp); 664232153Smm if (archive_wstring_ensure(&ws, 665232153Smm path_length + 2) == NULL) { 666228753Smm archive_set_error(&a->archive, ENOMEM, 667232153Smm "Can't allocate pax data"); 668232153Smm archive_wstring_free(&ws); 669228753Smm return(ARCHIVE_FATAL); 670228753Smm } 671232153Smm /* Should we keep '\' ? */ 672232153Smm if (wp[path_length -1] == L'\\') 673232153Smm path_length--; 674232153Smm archive_wstrncpy(&ws, wp, path_length); 675232153Smm archive_wstrappend_wchar(&ws, L'/'); 676232153Smm archive_entry_copy_pathname_w( 677232153Smm entry_original, ws.s); 678232153Smm archive_wstring_free(&ws); 679232153Smm p = NULL; 680232153Smm } else 681232153Smm#endif 682232153Smm p = archive_entry_pathname(entry_original); 683232153Smm /* 684232153Smm * On Windows, this is a backup operation just in 685232153Smm * case getting WCS failed. On POSIX, this is a 686232153Smm * normal operation. 687232153Smm */ 688342360Smm if (p != NULL && p[0] != '\0' && p[strlen(p) - 1] != '/') { 689232153Smm struct archive_string as; 690232153Smm 691232153Smm archive_string_init(&as); 692232153Smm path_length = strlen(p); 693232153Smm if (archive_string_ensure(&as, 694232153Smm path_length + 2) == NULL) { 695232153Smm archive_set_error(&a->archive, ENOMEM, 696232153Smm "Can't allocate pax data"); 697232153Smm archive_string_free(&as); 698232153Smm return(ARCHIVE_FATAL); 699232153Smm } 700232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 701232153Smm /* NOTE: This might break the pathname 702232153Smm * if the current code page is CP932 and 703232153Smm * the pathname includes a character '\' 704232153Smm * as a part of its multibyte pathname. */ 705232153Smm if (p[strlen(p) -1] == '\\') 706232153Smm path_length--; 707232153Smm else 708232153Smm#endif 709232153Smm archive_strncpy(&as, p, path_length); 710232153Smm archive_strappend_char(&as, '/'); 711232153Smm archive_entry_copy_pathname( 712232153Smm entry_original, as.s); 713232153Smm archive_string_free(&as); 714228753Smm } 715228753Smm break; 716232153Smm } 717358088Smm default: /* AE_IFSOCK and unknown */ 718358088Smm __archive_write_entry_filetype_unsupported( 719358088Smm &a->archive, entry_original, "pax"); 720232153Smm return (ARCHIVE_FAILED); 721228753Smm } 722228753Smm } 723228753Smm 724232153Smm /* 725232153Smm * If Mac OS metadata blob is here, recurse to write that 726232153Smm * as a separate entry. This is really a pretty poor design: 727232153Smm * In particular, it doubles the overhead for long filenames. 728232153Smm * TODO: Help Apple folks design something better and figure 729232153Smm * out how to transition from this legacy format. 730232153Smm * 731232153Smm * Note that this code is present on every platform; clients 732232153Smm * on non-Mac are unlikely to ever provide this data, but 733232153Smm * applications that copy entries from one archive to another 734232153Smm * should not lose data just because the local filesystem 735232153Smm * can't store it. 736232153Smm */ 737232153Smm mac_metadata = 738232153Smm archive_entry_mac_metadata(entry_original, &mac_metadata_size); 739232153Smm if (mac_metadata != NULL) { 740232153Smm const char *oname; 741232153Smm char *name, *bname; 742232153Smm size_t name_length; 743232153Smm struct archive_entry *extra = archive_entry_new2(&a->archive); 744232153Smm 745232153Smm oname = archive_entry_pathname(entry_original); 746232153Smm name_length = strlen(oname); 747232153Smm name = malloc(name_length + 3); 748248616Smm if (name == NULL || extra == NULL) { 749232153Smm /* XXX error message */ 750248616Smm archive_entry_free(extra); 751248616Smm free(name); 752232153Smm return (ARCHIVE_FAILED); 753232153Smm } 754232153Smm strcpy(name, oname); 755232153Smm /* Find last '/'; strip trailing '/' characters */ 756232153Smm bname = strrchr(name, '/'); 757232153Smm while (bname != NULL && bname[1] == '\0') { 758232153Smm *bname = '\0'; 759232153Smm bname = strrchr(name, '/'); 760232153Smm } 761232153Smm if (bname == NULL) { 762232153Smm memmove(name + 2, name, name_length + 1); 763232153Smm memmove(name, "._", 2); 764232153Smm } else { 765232153Smm bname += 1; 766232153Smm memmove(bname + 2, bname, strlen(bname) + 1); 767232153Smm memmove(bname, "._", 2); 768232153Smm } 769232153Smm archive_entry_copy_pathname(extra, name); 770232153Smm free(name); 771232153Smm 772232153Smm archive_entry_set_size(extra, mac_metadata_size); 773232153Smm archive_entry_set_filetype(extra, AE_IFREG); 774232153Smm archive_entry_set_perm(extra, 775232153Smm archive_entry_perm(entry_original)); 776232153Smm archive_entry_set_mtime(extra, 777232153Smm archive_entry_mtime(entry_original), 778232153Smm archive_entry_mtime_nsec(entry_original)); 779232153Smm archive_entry_set_gid(extra, 780232153Smm archive_entry_gid(entry_original)); 781232153Smm archive_entry_set_gname(extra, 782232153Smm archive_entry_gname(entry_original)); 783232153Smm archive_entry_set_uid(extra, 784232153Smm archive_entry_uid(entry_original)); 785232153Smm archive_entry_set_uname(extra, 786232153Smm archive_entry_uname(entry_original)); 787232153Smm 788232153Smm /* Recurse to write the special copyfile entry. */ 789232153Smm r = archive_write_pax_header(a, extra); 790248616Smm archive_entry_free(extra); 791232153Smm if (r < ARCHIVE_WARN) 792232153Smm return (r); 793232153Smm if (r < ret) 794232153Smm ret = r; 795248616Smm r = (int)archive_write_pax_data(a, mac_metadata, 796248616Smm mac_metadata_size); 797232153Smm if (r < ARCHIVE_WARN) 798232153Smm return (r); 799232153Smm if (r < ret) 800232153Smm ret = r; 801232153Smm r = archive_write_pax_finish_entry(a); 802232153Smm if (r < ARCHIVE_WARN) 803232153Smm return (r); 804232153Smm if (r < ret) 805232153Smm ret = r; 806232153Smm } 807232153Smm 808228753Smm /* Copy entry so we can modify it as needed. */ 809232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 810311041Smm /* Make sure the path separators in pathname, hardlink and symlink 811232153Smm * are all slash '/', not the Windows path separator '\'. */ 812232153Smm entry_main = __la_win_entry_in_posix_pathseparator(entry_original); 813232153Smm if (entry_main == entry_original) 814232153Smm entry_main = archive_entry_clone(entry_original); 815232153Smm#else 816228753Smm entry_main = archive_entry_clone(entry_original); 817232153Smm#endif 818232153Smm if (entry_main == NULL) { 819232153Smm archive_set_error(&a->archive, ENOMEM, 820232153Smm "Can't allocate pax data"); 821232153Smm return(ARCHIVE_FATAL); 822232153Smm } 823228753Smm archive_string_empty(&(pax->pax_header)); /* Blank our work area. */ 824232153Smm archive_string_empty(&(pax->sparse_map)); 825232153Smm sparse_total = 0; 826232153Smm sparse_list_clear(pax); 827228753Smm 828232153Smm if (hardlink == NULL && 829232153Smm archive_entry_filetype(entry_main) == AE_IFREG) 830232153Smm sparse_count = archive_entry_sparse_reset(entry_main); 831232153Smm else 832232153Smm sparse_count = 0; 833232153Smm if (sparse_count) { 834232153Smm int64_t offset, length, last_offset = 0; 835232153Smm /* Get the last entry of sparse block. */ 836232153Smm while (archive_entry_sparse_next( 837232153Smm entry_main, &offset, &length) == ARCHIVE_OK) 838232153Smm last_offset = offset + length; 839232153Smm 840232153Smm /* If the last sparse block does not reach the end of file, 841232153Smm * We have to add a empty sparse block as the last entry to 842232153Smm * manage storing file data. */ 843232153Smm if (last_offset < archive_entry_size(entry_main)) 844232153Smm archive_entry_sparse_add_entry(entry_main, 845232153Smm archive_entry_size(entry_main), 0); 846232153Smm sparse_count = archive_entry_sparse_reset(entry_main); 847232153Smm } 848232153Smm 849228753Smm /* 850228753Smm * First, check the name fields and see if any of them 851228753Smm * require binary coding. If any of them does, then all of 852228753Smm * them do. 853228753Smm */ 854232153Smm r = get_entry_pathname(a, entry_main, &path, &path_length, sconv); 855358088Smm if (r == ARCHIVE_FATAL) { 856358088Smm archive_entry_free(entry_main); 857232153Smm return (r); 858358088Smm } else if (r != ARCHIVE_OK) { 859232153Smm r = get_entry_pathname(a, entry_main, &path, 860232153Smm &path_length, NULL); 861358088Smm if (r == ARCHIVE_FATAL) { 862358088Smm archive_entry_free(entry_main); 863232153Smm return (r); 864358088Smm } 865228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 866232153Smm "Can't translate pathname '%s' to %s", path, 867232153Smm archive_string_conversion_charset_name(sconv)); 868228753Smm ret = ARCHIVE_WARN; 869232153Smm sconv = NULL;/* The header charset switches to binary mode. */ 870228753Smm } 871232153Smm r = get_entry_uname(a, entry_main, &uname, &uname_length, sconv); 872358088Smm if (r == ARCHIVE_FATAL) { 873358088Smm archive_entry_free(entry_main); 874232153Smm return (r); 875358088Smm } else if (r != ARCHIVE_OK) { 876232153Smm r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL); 877358088Smm if (r == ARCHIVE_FATAL) { 878358088Smm archive_entry_free(entry_main); 879232153Smm return (r); 880358088Smm } 881228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 882232153Smm "Can't translate uname '%s' to %s", uname, 883232153Smm archive_string_conversion_charset_name(sconv)); 884228753Smm ret = ARCHIVE_WARN; 885232153Smm sconv = NULL;/* The header charset switches to binary mode. */ 886228753Smm } 887232153Smm r = get_entry_gname(a, entry_main, &gname, &gname_length, sconv); 888358088Smm if (r == ARCHIVE_FATAL) { 889358088Smm archive_entry_free(entry_main); 890232153Smm return (r); 891358088Smm } else if (r != ARCHIVE_OK) { 892232153Smm r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL); 893358088Smm if (r == ARCHIVE_FATAL) { 894358088Smm archive_entry_free(entry_main); 895232153Smm return (r); 896358088Smm } 897228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 898232153Smm "Can't translate gname '%s' to %s", gname, 899232153Smm archive_string_conversion_charset_name(sconv)); 900228753Smm ret = ARCHIVE_WARN; 901232153Smm sconv = NULL;/* The header charset switches to binary mode. */ 902228753Smm } 903228753Smm linkpath = hardlink; 904232153Smm linkpath_length = hardlink_length; 905232153Smm if (linkpath == NULL) { 906232153Smm r = get_entry_symlink(a, entry_main, &linkpath, 907232153Smm &linkpath_length, sconv); 908358088Smm if (r == ARCHIVE_FATAL) { 909358088Smm archive_entry_free(entry_main); 910232153Smm return (r); 911358088Smm } else if (r != ARCHIVE_OK) { 912232153Smm r = get_entry_symlink(a, entry_main, &linkpath, 913232153Smm &linkpath_length, NULL); 914358088Smm if (r == ARCHIVE_FATAL) { 915358088Smm archive_entry_free(entry_main); 916232153Smm return (r); 917358088Smm } 918232153Smm archive_set_error(&a->archive, 919232153Smm ARCHIVE_ERRNO_FILE_FORMAT, 920232153Smm "Can't translate linkname '%s' to %s", linkpath, 921232153Smm archive_string_conversion_charset_name(sconv)); 922232153Smm ret = ARCHIVE_WARN; 923232153Smm sconv = NULL; 924232153Smm } 925228753Smm } 926232153Smm 927232153Smm /* If any string conversions failed, get all attributes 928232153Smm * in binary-mode. */ 929232153Smm if (sconv == NULL && !pax->opt_binary) { 930232153Smm if (hardlink != NULL) { 931232153Smm r = get_entry_hardlink(a, entry_main, &hardlink, 932232153Smm &hardlink_length, NULL); 933358088Smm if (r == ARCHIVE_FATAL) { 934358088Smm archive_entry_free(entry_main); 935232153Smm return (r); 936358088Smm } 937232153Smm linkpath = hardlink; 938232153Smm linkpath_length = hardlink_length; 939232153Smm } 940232153Smm r = get_entry_pathname(a, entry_main, &path, 941232153Smm &path_length, NULL); 942358088Smm if (r == ARCHIVE_FATAL) { 943358088Smm archive_entry_free(entry_main); 944232153Smm return (r); 945358088Smm } 946232153Smm r = get_entry_uname(a, entry_main, &uname, &uname_length, NULL); 947358088Smm if (r == ARCHIVE_FATAL) { 948358088Smm archive_entry_free(entry_main); 949232153Smm return (r); 950358088Smm } 951232153Smm r = get_entry_gname(a, entry_main, &gname, &gname_length, NULL); 952358088Smm if (r == ARCHIVE_FATAL) { 953358088Smm archive_entry_free(entry_main); 954232153Smm return (r); 955358088Smm } 956228753Smm } 957228753Smm 958228753Smm /* Store the header encoding first, to be nice to readers. */ 959232153Smm if (sconv == NULL) 960232153Smm add_pax_attr(&(pax->pax_header), "hdrcharset", "BINARY"); 961228753Smm 962228753Smm 963228753Smm /* 964228753Smm * If name is too long, or has non-ASCII characters, add 965228753Smm * 'path' to pax extended attrs. (Note that an unconvertible 966228753Smm * name must have non-ASCII characters.) 967228753Smm */ 968232153Smm if (has_non_ASCII(path)) { 969228753Smm /* We have non-ASCII characters. */ 970232153Smm add_pax_attr(&(pax->pax_header), "path", path); 971228753Smm archive_entry_set_pathname(entry_main, 972228753Smm build_ustar_entry_name(ustar_entry_name, 973232153Smm path, path_length, NULL)); 974228753Smm need_extension = 1; 975228753Smm } else { 976228753Smm /* We have an all-ASCII path; we'd like to just store 977228753Smm * it in the ustar header if it will fit. Yes, this 978228753Smm * duplicates some of the logic in 979232153Smm * archive_write_set_format_ustar.c 980228753Smm */ 981232153Smm if (path_length <= 100) { 982228753Smm /* Fits in the old 100-char tar name field. */ 983228753Smm } else { 984228753Smm /* Find largest suffix that will fit. */ 985228753Smm /* Note: strlen() > 100, so strlen() - 100 - 1 >= 0 */ 986232153Smm suffix = strchr(path + path_length - 100 - 1, '/'); 987228753Smm /* Don't attempt an empty prefix. */ 988228753Smm if (suffix == path) 989228753Smm suffix = strchr(suffix + 1, '/'); 990228753Smm /* We can put it in the ustar header if it's 991228753Smm * all ASCII and it's either <= 100 characters 992228753Smm * or can be split at a '/' into a prefix <= 993228753Smm * 155 chars and a suffix <= 100 chars. (Note 994228753Smm * the strchr() above will return NULL exactly 995228753Smm * when the path can't be split.) 996228753Smm */ 997228753Smm if (suffix == NULL /* Suffix > 100 chars. */ 998228753Smm || suffix[1] == '\0' /* empty suffix */ 999228753Smm || suffix - path > 155) /* Prefix > 155 chars */ 1000228753Smm { 1001232153Smm add_pax_attr(&(pax->pax_header), "path", path); 1002228753Smm archive_entry_set_pathname(entry_main, 1003228753Smm build_ustar_entry_name(ustar_entry_name, 1004232153Smm path, path_length, NULL)); 1005228753Smm need_extension = 1; 1006228753Smm } 1007228753Smm } 1008228753Smm } 1009228753Smm 1010228753Smm if (linkpath != NULL) { 1011228753Smm /* If link name is too long or has non-ASCII characters, add 1012228753Smm * 'linkpath' to pax extended attrs. */ 1013232153Smm if (linkpath_length > 100 || has_non_ASCII(linkpath)) { 1014232153Smm add_pax_attr(&(pax->pax_header), "linkpath", linkpath); 1015232153Smm if (linkpath_length > 100) { 1016228753Smm if (hardlink != NULL) 1017228753Smm archive_entry_set_hardlink(entry_main, 1018228753Smm "././@LongHardLink"); 1019228753Smm else 1020228753Smm archive_entry_set_symlink(entry_main, 1021228753Smm "././@LongSymLink"); 1022228753Smm } 1023228753Smm need_extension = 1; 1024228753Smm } 1025228753Smm } 1026232153Smm /* Save a pathname since it will be renamed if `entry_main` has 1027232153Smm * sparse blocks. */ 1028232153Smm archive_string_init(&entry_name); 1029232153Smm archive_strcpy(&entry_name, archive_entry_pathname(entry_main)); 1030228753Smm 1031228753Smm /* If file size is too large, add 'size' to pax extended attrs. */ 1032228753Smm if (archive_entry_size(entry_main) >= (((int64_t)1) << 33)) { 1033228753Smm add_pax_attr_int(&(pax->pax_header), "size", 1034228753Smm archive_entry_size(entry_main)); 1035228753Smm need_extension = 1; 1036228753Smm } 1037228753Smm 1038228753Smm /* If numeric GID is too large, add 'gid' to pax extended attrs. */ 1039228753Smm if ((unsigned int)archive_entry_gid(entry_main) >= (1 << 18)) { 1040228753Smm add_pax_attr_int(&(pax->pax_header), "gid", 1041228753Smm archive_entry_gid(entry_main)); 1042228753Smm need_extension = 1; 1043228753Smm } 1044228753Smm 1045228753Smm /* If group name is too large or has non-ASCII characters, add 1046228753Smm * 'gname' to pax extended attrs. */ 1047228753Smm if (gname != NULL) { 1048232153Smm if (gname_length > 31 || has_non_ASCII(gname)) { 1049232153Smm add_pax_attr(&(pax->pax_header), "gname", gname); 1050228753Smm need_extension = 1; 1051228753Smm } 1052228753Smm } 1053228753Smm 1054228753Smm /* If numeric UID is too large, add 'uid' to pax extended attrs. */ 1055228753Smm if ((unsigned int)archive_entry_uid(entry_main) >= (1 << 18)) { 1056228753Smm add_pax_attr_int(&(pax->pax_header), "uid", 1057228753Smm archive_entry_uid(entry_main)); 1058228753Smm need_extension = 1; 1059228753Smm } 1060228753Smm 1061228753Smm /* Add 'uname' to pax extended attrs if necessary. */ 1062228753Smm if (uname != NULL) { 1063232153Smm if (uname_length > 31 || has_non_ASCII(uname)) { 1064232153Smm add_pax_attr(&(pax->pax_header), "uname", uname); 1065228753Smm need_extension = 1; 1066228753Smm } 1067228753Smm } 1068228753Smm 1069228753Smm /* 1070228753Smm * POSIX/SUSv3 doesn't provide a standard key for large device 1071228753Smm * numbers. I use the same keys here that Joerg Schilling 1072228753Smm * used for 'star.' (Which, somewhat confusingly, are called 1073228753Smm * "devXXX" even though they code "rdev" values.) No doubt, 1074228753Smm * other implementations use other keys. Note that there's no 1075228753Smm * reason we can't write the same information into a number of 1076228753Smm * different keys. 1077228753Smm * 1078228753Smm * Of course, this is only needed for block or char device entries. 1079228753Smm */ 1080228753Smm if (archive_entry_filetype(entry_main) == AE_IFBLK 1081228753Smm || archive_entry_filetype(entry_main) == AE_IFCHR) { 1082228753Smm /* 1083228753Smm * If rdevmajor is too large, add 'SCHILY.devmajor' to 1084228753Smm * extended attributes. 1085228753Smm */ 1086232153Smm int rdevmajor, rdevminor; 1087228753Smm rdevmajor = archive_entry_rdevmajor(entry_main); 1088228753Smm rdevminor = archive_entry_rdevminor(entry_main); 1089228753Smm if (rdevmajor >= (1 << 18)) { 1090228753Smm add_pax_attr_int(&(pax->pax_header), "SCHILY.devmajor", 1091228753Smm rdevmajor); 1092228753Smm /* 1093228753Smm * Non-strict formatting below means we don't 1094228753Smm * have to truncate here. Not truncating improves 1095228753Smm * the chance that some more modern tar archivers 1096228753Smm * (such as GNU tar 1.13) can restore the full 1097228753Smm * value even if they don't understand the pax 1098228753Smm * extended attributes. See my rant below about 1099228753Smm * file size fields for additional details. 1100228753Smm */ 1101228753Smm /* archive_entry_set_rdevmajor(entry_main, 1102228753Smm rdevmajor & ((1 << 18) - 1)); */ 1103228753Smm need_extension = 1; 1104228753Smm } 1105228753Smm 1106228753Smm /* 1107228753Smm * If devminor is too large, add 'SCHILY.devminor' to 1108228753Smm * extended attributes. 1109228753Smm */ 1110228753Smm if (rdevminor >= (1 << 18)) { 1111228753Smm add_pax_attr_int(&(pax->pax_header), "SCHILY.devminor", 1112228753Smm rdevminor); 1113228753Smm /* Truncation is not necessary here, either. */ 1114228753Smm /* archive_entry_set_rdevminor(entry_main, 1115228753Smm rdevminor & ((1 << 18) - 1)); */ 1116228753Smm need_extension = 1; 1117228753Smm } 1118228753Smm } 1119228753Smm 1120228753Smm /* 1121228753Smm * Technically, the mtime field in the ustar header can 1122228753Smm * support 33 bits, but many platforms use signed 32-bit time 1123228753Smm * values. The cutoff of 0x7fffffff here is a compromise. 1124228753Smm * Yes, this check is duplicated just below; this helps to 1125228753Smm * avoid writing an mtime attribute just to handle a 1126228753Smm * high-resolution timestamp in "restricted pax" mode. 1127228753Smm */ 1128228753Smm if (!need_extension && 1129228753Smm ((archive_entry_mtime(entry_main) < 0) 1130228753Smm || (archive_entry_mtime(entry_main) >= 0x7fffffff))) 1131228753Smm need_extension = 1; 1132228753Smm 1133228753Smm /* I use a star-compatible file flag attribute. */ 1134228753Smm p = archive_entry_fflags_text(entry_main); 1135228753Smm if (!need_extension && p != NULL && *p != '\0') 1136228753Smm need_extension = 1; 1137228753Smm 1138228753Smm /* If there are extended attributes, we need an extension */ 1139228753Smm if (!need_extension && archive_entry_xattr_count(entry_original) > 0) 1140228753Smm need_extension = 1; 1141228753Smm 1142232153Smm /* If there are sparse info, we need an extension */ 1143232153Smm if (!need_extension && sparse_count > 0) 1144232153Smm need_extension = 1; 1145232153Smm 1146313570Smm acl_types = archive_entry_acl_types(entry_original); 1147313570Smm 1148313570Smm /* If there are any ACL entries, we need an extension */ 1149313570Smm if (!need_extension && acl_types != 0) 1150313570Smm need_extension = 1; 1151313570Smm 1152348607Smm /* If the symlink type is defined, we need an extension */ 1153348607Smm if (!need_extension && archive_entry_symlink_type(entry_main) > 0) 1154348607Smm need_extension = 1; 1155348607Smm 1156228753Smm /* 1157299529Smm * Libarchive used to include these in extended headers for 1158299529Smm * restricted pax format, but that confused people who 1159299529Smm * expected ustar-like time semantics. So now we only include 1160299529Smm * them in full pax format. 1161228753Smm */ 1162299529Smm if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED) { 1163228753Smm if (archive_entry_ctime(entry_main) != 0 || 1164228753Smm archive_entry_ctime_nsec(entry_main) != 0) 1165228753Smm add_pax_attr_time(&(pax->pax_header), "ctime", 1166228753Smm archive_entry_ctime(entry_main), 1167228753Smm archive_entry_ctime_nsec(entry_main)); 1168228753Smm 1169228753Smm if (archive_entry_atime(entry_main) != 0 || 1170228753Smm archive_entry_atime_nsec(entry_main) != 0) 1171228753Smm add_pax_attr_time(&(pax->pax_header), "atime", 1172228753Smm archive_entry_atime(entry_main), 1173228753Smm archive_entry_atime_nsec(entry_main)); 1174228753Smm 1175228753Smm /* Store birth/creationtime only if it's earlier than mtime */ 1176228753Smm if (archive_entry_birthtime_is_set(entry_main) && 1177228753Smm archive_entry_birthtime(entry_main) 1178228753Smm < archive_entry_mtime(entry_main)) 1179228753Smm add_pax_attr_time(&(pax->pax_header), 1180228753Smm "LIBARCHIVE.creationtime", 1181228753Smm archive_entry_birthtime(entry_main), 1182228753Smm archive_entry_birthtime_nsec(entry_main)); 1183299529Smm } 1184228753Smm 1185299529Smm /* 1186299529Smm * The following items are handled differently in "pax 1187299529Smm * restricted" format. In particular, in "pax restricted" 1188299529Smm * format they won't be added unless need_extension is 1189299529Smm * already set (we're already generating an extended header, so 1190299529Smm * may as well include these). 1191299529Smm */ 1192299529Smm if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED || 1193299529Smm need_extension) { 1194299529Smm if (archive_entry_mtime(entry_main) < 0 || 1195299529Smm archive_entry_mtime(entry_main) >= 0x7fffffff || 1196299529Smm archive_entry_mtime_nsec(entry_main) != 0) 1197299529Smm add_pax_attr_time(&(pax->pax_header), "mtime", 1198299529Smm archive_entry_mtime(entry_main), 1199299529Smm archive_entry_mtime_nsec(entry_main)); 1200299529Smm 1201228753Smm /* I use a star-compatible file flag attribute. */ 1202228753Smm p = archive_entry_fflags_text(entry_main); 1203228753Smm if (p != NULL && *p != '\0') 1204228753Smm add_pax_attr(&(pax->pax_header), "SCHILY.fflags", p); 1205228753Smm 1206228753Smm /* I use star-compatible ACL attributes. */ 1207313570Smm if ((acl_types & ARCHIVE_ENTRY_ACL_TYPE_NFS4) != 0) { 1208313570Smm ret = add_pax_acl(a, entry_original, pax, 1209313570Smm ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID | 1210313926Smm ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA | 1211313926Smm ARCHIVE_ENTRY_ACL_STYLE_COMPACT); 1212358088Smm if (ret == ARCHIVE_FATAL) { 1213358088Smm archive_entry_free(entry_main); 1214358088Smm archive_string_free(&entry_name); 1215232153Smm return (ARCHIVE_FATAL); 1216358088Smm } 1217232153Smm } 1218313570Smm if (acl_types & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) { 1219313570Smm ret = add_pax_acl(a, entry_original, pax, 1220313570Smm ARCHIVE_ENTRY_ACL_TYPE_ACCESS | 1221313570Smm ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID | 1222313570Smm ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA); 1223358088Smm if (ret == ARCHIVE_FATAL) { 1224358088Smm archive_entry_free(entry_main); 1225358088Smm archive_string_free(&entry_name); 1226232153Smm return (ARCHIVE_FATAL); 1227358088Smm } 1228232153Smm } 1229313570Smm if (acl_types & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) { 1230313570Smm ret = add_pax_acl(a, entry_original, pax, 1231313570Smm ARCHIVE_ENTRY_ACL_TYPE_DEFAULT | 1232313570Smm ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID | 1233313570Smm ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA); 1234358088Smm if (ret == ARCHIVE_FATAL) { 1235358088Smm archive_entry_free(entry_main); 1236358088Smm archive_string_free(&entry_name); 1237313570Smm return (ARCHIVE_FATAL); 1238358088Smm } 1239313570Smm } 1240228753Smm 1241232153Smm /* We use GNU-tar-compatible sparse attributes. */ 1242232153Smm if (sparse_count > 0) { 1243232153Smm int64_t soffset, slength; 1244228753Smm 1245232153Smm add_pax_attr_int(&(pax->pax_header), 1246232153Smm "GNU.sparse.major", 1); 1247232153Smm add_pax_attr_int(&(pax->pax_header), 1248232153Smm "GNU.sparse.minor", 0); 1249318482Smm /* 1250318482Smm * Make sure to store the original path, since 1251318482Smm * truncation to ustar limit happened already. 1252318482Smm */ 1253232153Smm add_pax_attr(&(pax->pax_header), 1254318482Smm "GNU.sparse.name", path); 1255232153Smm add_pax_attr_int(&(pax->pax_header), 1256232153Smm "GNU.sparse.realsize", 1257232153Smm archive_entry_size(entry_main)); 1258232153Smm 1259232153Smm /* Rename the file name which will be used for 1260232153Smm * ustar header to a special name, which GNU 1261232153Smm * PAX Format 1.0 requires */ 1262232153Smm archive_entry_set_pathname(entry_main, 1263232153Smm build_gnu_sparse_name(gnu_sparse_name, 1264232153Smm entry_name.s)); 1265232153Smm 1266232153Smm /* 1267232153Smm * - Make a sparse map, which will precede a file data. 1268232153Smm * - Get the total size of available data of sparse. 1269232153Smm */ 1270232153Smm archive_string_sprintf(&(pax->sparse_map), "%d\n", 1271232153Smm sparse_count); 1272232153Smm while (archive_entry_sparse_next(entry_main, 1273232153Smm &soffset, &slength) == ARCHIVE_OK) { 1274232153Smm archive_string_sprintf(&(pax->sparse_map), 1275232153Smm "%jd\n%jd\n", 1276232153Smm (intmax_t)soffset, 1277232153Smm (intmax_t)slength); 1278232153Smm sparse_total += slength; 1279232153Smm if (sparse_list_add(pax, soffset, slength) 1280232153Smm != ARCHIVE_OK) { 1281232153Smm archive_set_error(&a->archive, 1282232153Smm ENOMEM, 1283232153Smm "Can't allocate memory"); 1284232153Smm archive_entry_free(entry_main); 1285232153Smm archive_string_free(&entry_name); 1286232153Smm return (ARCHIVE_FATAL); 1287232153Smm } 1288232153Smm } 1289232153Smm } 1290232153Smm 1291228753Smm /* Store extended attributes */ 1292232153Smm if (archive_write_pax_header_xattrs(a, pax, entry_original) 1293232153Smm == ARCHIVE_FATAL) { 1294232153Smm archive_entry_free(entry_main); 1295232153Smm archive_string_free(&entry_name); 1296232153Smm return (ARCHIVE_FATAL); 1297232153Smm } 1298348607Smm 1299348607Smm /* Store extended symlink information */ 1300348607Smm if (archive_entry_symlink_type(entry_main) == 1301348607Smm AE_SYMLINK_TYPE_FILE) { 1302348607Smm add_pax_attr(&(pax->pax_header), 1303348607Smm "LIBARCHIVE.symlinktype", "file"); 1304348607Smm } else if (archive_entry_symlink_type(entry_main) == 1305348607Smm AE_SYMLINK_TYPE_DIRECTORY) { 1306348607Smm add_pax_attr(&(pax->pax_header), 1307348607Smm "LIBARCHIVE.symlinktype", "dir"); 1308348607Smm } 1309228753Smm } 1310228753Smm 1311228753Smm /* Only regular files have data. */ 1312228753Smm if (archive_entry_filetype(entry_main) != AE_IFREG) 1313228753Smm archive_entry_set_size(entry_main, 0); 1314228753Smm 1315228753Smm /* 1316228753Smm * Pax-restricted does not store data for hardlinks, in order 1317228753Smm * to improve compatibility with ustar. 1318228753Smm */ 1319228753Smm if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE && 1320228753Smm hardlink != NULL) 1321228753Smm archive_entry_set_size(entry_main, 0); 1322228753Smm 1323228753Smm /* 1324228753Smm * XXX Full pax interchange format does permit a hardlink 1325228753Smm * entry to have data associated with it. I'm not supporting 1326228753Smm * that here because the client expects me to tell them whether 1327228753Smm * or not this format expects data for hardlinks. If I 1328228753Smm * don't check here, then every pax archive will end up with 1329228753Smm * duplicated data for hardlinks. Someday, there may be 1330228753Smm * need to select this behavior, in which case the following 1331228753Smm * will need to be revisited. XXX 1332228753Smm */ 1333228753Smm if (hardlink != NULL) 1334228753Smm archive_entry_set_size(entry_main, 0); 1335228753Smm 1336232153Smm /* Save a real file size. */ 1337232153Smm real_size = archive_entry_size(entry_main); 1338232153Smm /* 1339232153Smm * Overwrite a file size by the total size of sparse blocks and 1340232153Smm * the size of sparse map info. That file size is the length of 1341232153Smm * the data, which we will exactly store into an archive file. 1342232153Smm */ 1343232153Smm if (archive_strlen(&(pax->sparse_map))) { 1344232153Smm size_t mapsize = archive_strlen(&(pax->sparse_map)); 1345232153Smm pax->sparse_map_padding = 0x1ff & (-(ssize_t)mapsize); 1346232153Smm archive_entry_set_size(entry_main, 1347232153Smm mapsize + pax->sparse_map_padding + sparse_total); 1348232153Smm } 1349232153Smm 1350228753Smm /* Format 'ustar' header for main entry. 1351228753Smm * 1352228753Smm * The trouble with file size: If the reader can't understand 1353228753Smm * the file size, they may not be able to locate the next 1354228753Smm * entry and the rest of the archive is toast. Pax-compliant 1355228753Smm * readers are supposed to ignore the file size in the main 1356228753Smm * header, so the question becomes how to maximize portability 1357228753Smm * for readers that don't support pax attribute extensions. 1358228753Smm * For maximum compatibility, I permit numeric extensions in 1359228753Smm * the main header so that the file size stored will always be 1360228753Smm * correct, even if it's in a format that only some 1361228753Smm * implementations understand. The technique used here is: 1362228753Smm * 1363228753Smm * a) If possible, follow the standard exactly. This handles 1364228753Smm * files up to 8 gigabytes minus 1. 1365228753Smm * 1366228753Smm * b) If that fails, try octal but omit the field terminator. 1367228753Smm * That handles files up to 64 gigabytes minus 1. 1368228753Smm * 1369228753Smm * c) Otherwise, use base-256 extensions. That handles files 1370228753Smm * up to 2^63 in this implementation, with the potential to 1371228753Smm * go up to 2^94. That should hold us for a while. ;-) 1372228753Smm * 1373228753Smm * The non-strict formatter uses similar logic for other 1374228753Smm * numeric fields, though they're less critical. 1375228753Smm */ 1376232153Smm if (__archive_write_format_header_ustar(a, ustarbuff, entry_main, -1, 0, 1377358088Smm NULL) == ARCHIVE_FATAL) { 1378358088Smm archive_entry_free(entry_main); 1379358088Smm archive_string_free(&entry_name); 1380232153Smm return (ARCHIVE_FATAL); 1381358088Smm } 1382228753Smm 1383228753Smm /* If we built any extended attributes, write that entry first. */ 1384228753Smm if (archive_strlen(&(pax->pax_header)) > 0) { 1385228753Smm struct archive_entry *pax_attr_entry; 1386228753Smm time_t s; 1387232153Smm int64_t uid, gid; 1388232153Smm int mode; 1389228753Smm 1390232153Smm pax_attr_entry = archive_entry_new2(&a->archive); 1391232153Smm p = entry_name.s; 1392228753Smm archive_entry_set_pathname(pax_attr_entry, 1393228753Smm build_pax_attribute_name(pax_entry_name, p)); 1394228753Smm archive_entry_set_size(pax_attr_entry, 1395228753Smm archive_strlen(&(pax->pax_header))); 1396228753Smm /* Copy uid/gid (but clip to ustar limits). */ 1397228753Smm uid = archive_entry_uid(entry_main); 1398232153Smm if (uid >= 1 << 18) 1399232153Smm uid = (1 << 18) - 1; 1400228753Smm archive_entry_set_uid(pax_attr_entry, uid); 1401228753Smm gid = archive_entry_gid(entry_main); 1402232153Smm if (gid >= 1 << 18) 1403232153Smm gid = (1 << 18) - 1; 1404228753Smm archive_entry_set_gid(pax_attr_entry, gid); 1405228753Smm /* Copy mode over (but not setuid/setgid bits) */ 1406228753Smm mode = archive_entry_mode(entry_main); 1407228753Smm#ifdef S_ISUID 1408228753Smm mode &= ~S_ISUID; 1409228753Smm#endif 1410228753Smm#ifdef S_ISGID 1411228753Smm mode &= ~S_ISGID; 1412228753Smm#endif 1413228753Smm#ifdef S_ISVTX 1414228753Smm mode &= ~S_ISVTX; 1415228753Smm#endif 1416228753Smm archive_entry_set_mode(pax_attr_entry, mode); 1417228753Smm 1418228753Smm /* Copy uname/gname. */ 1419228753Smm archive_entry_set_uname(pax_attr_entry, 1420228753Smm archive_entry_uname(entry_main)); 1421228753Smm archive_entry_set_gname(pax_attr_entry, 1422228753Smm archive_entry_gname(entry_main)); 1423228753Smm 1424228753Smm /* Copy mtime, but clip to ustar limits. */ 1425228753Smm s = archive_entry_mtime(entry_main); 1426228753Smm if (s < 0) { s = 0; } 1427228753Smm if (s >= 0x7fffffff) { s = 0x7fffffff; } 1428228753Smm archive_entry_set_mtime(pax_attr_entry, s, 0); 1429228753Smm 1430228753Smm /* Standard ustar doesn't support atime. */ 1431228753Smm archive_entry_set_atime(pax_attr_entry, 0, 0); 1432228753Smm 1433228753Smm /* Standard ustar doesn't support ctime. */ 1434228753Smm archive_entry_set_ctime(pax_attr_entry, 0, 0); 1435228753Smm 1436228753Smm r = __archive_write_format_header_ustar(a, paxbuff, 1437232153Smm pax_attr_entry, 'x', 1, NULL); 1438228753Smm 1439228753Smm archive_entry_free(pax_attr_entry); 1440228753Smm 1441228753Smm /* Note that the 'x' header shouldn't ever fail to format */ 1442232153Smm if (r < ARCHIVE_WARN) { 1443232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1444232153Smm "archive_write_pax_header: " 1445232153Smm "'x' header failed?! This can't happen.\n"); 1446358088Smm archive_entry_free(entry_main); 1447358088Smm archive_string_free(&entry_name); 1448232153Smm return (ARCHIVE_FATAL); 1449232153Smm } else if (r < ret) 1450232153Smm ret = r; 1451232153Smm r = __archive_write_output(a, paxbuff, 512); 1452228753Smm if (r != ARCHIVE_OK) { 1453232153Smm sparse_list_clear(pax); 1454228753Smm pax->entry_bytes_remaining = 0; 1455228753Smm pax->entry_padding = 0; 1456358088Smm archive_entry_free(entry_main); 1457358088Smm archive_string_free(&entry_name); 1458228753Smm return (ARCHIVE_FATAL); 1459228753Smm } 1460228753Smm 1461228753Smm pax->entry_bytes_remaining = archive_strlen(&(pax->pax_header)); 1462232153Smm pax->entry_padding = 1463232153Smm 0x1ff & (-(int64_t)pax->entry_bytes_remaining); 1464228753Smm 1465232153Smm r = __archive_write_output(a, pax->pax_header.s, 1466228753Smm archive_strlen(&(pax->pax_header))); 1467228753Smm if (r != ARCHIVE_OK) { 1468228753Smm /* If a write fails, we're pretty much toast. */ 1469358088Smm archive_entry_free(entry_main); 1470358088Smm archive_string_free(&entry_name); 1471228753Smm return (ARCHIVE_FATAL); 1472228753Smm } 1473228753Smm /* Pad out the end of the entry. */ 1474238856Smm r = __archive_write_nulls(a, (size_t)pax->entry_padding); 1475228753Smm if (r != ARCHIVE_OK) { 1476228753Smm /* If a write fails, we're pretty much toast. */ 1477358088Smm archive_entry_free(entry_main); 1478358088Smm archive_string_free(&entry_name); 1479228753Smm return (ARCHIVE_FATAL); 1480228753Smm } 1481228753Smm pax->entry_bytes_remaining = pax->entry_padding = 0; 1482228753Smm } 1483228753Smm 1484228753Smm /* Write the header for main entry. */ 1485232153Smm r = __archive_write_output(a, ustarbuff, 512); 1486358088Smm if (r != ARCHIVE_OK) { 1487358088Smm archive_entry_free(entry_main); 1488358088Smm archive_string_free(&entry_name); 1489228753Smm return (r); 1490358088Smm } 1491228753Smm 1492228753Smm /* 1493228753Smm * Inform the client of the on-disk size we're using, so 1494228753Smm * they can avoid unnecessarily writing a body for something 1495228753Smm * that we're just going to ignore. 1496228753Smm */ 1497232153Smm archive_entry_set_size(entry_original, real_size); 1498232153Smm if (pax->sparse_list == NULL && real_size > 0) { 1499232153Smm /* This is not a sparse file but we handle its data as 1500232153Smm * a sparse block. */ 1501232153Smm sparse_list_add(pax, 0, real_size); 1502232153Smm sparse_total = real_size; 1503232153Smm } 1504232153Smm pax->entry_padding = 0x1ff & (-(int64_t)sparse_total); 1505228753Smm archive_entry_free(entry_main); 1506232153Smm archive_string_free(&entry_name); 1507228753Smm 1508228753Smm return (ret); 1509228753Smm} 1510228753Smm 1511228753Smm/* 1512228753Smm * We need a valid name for the regular 'ustar' entry. This routine 1513228753Smm * tries to hack something more-or-less reasonable. 1514228753Smm * 1515228753Smm * The approach here tries to preserve leading dir names. We do so by 1516228753Smm * working with four sections: 1517228753Smm * 1) "prefix" directory names, 1518228753Smm * 2) "suffix" directory names, 1519228753Smm * 3) inserted dir name (optional), 1520228753Smm * 4) filename. 1521228753Smm * 1522228753Smm * These sections must satisfy the following requirements: 1523228753Smm * * Parts 1 & 2 together form an initial portion of the dir name. 1524228753Smm * * Part 3 is specified by the caller. (It should not contain a leading 1525228753Smm * or trailing '/'.) 1526228753Smm * * Part 4 forms an initial portion of the base filename. 1527228753Smm * * The filename must be <= 99 chars to fit the ustar 'name' field. 1528228753Smm * * Parts 2, 3, 4 together must be <= 99 chars to fit the ustar 'name' fld. 1529228753Smm * * Part 1 must be <= 155 chars to fit the ustar 'prefix' field. 1530228753Smm * * If the original name ends in a '/', the new name must also end in a '/' 1531228753Smm * * Trailing '/.' sequences may be stripped. 1532228753Smm * 1533228753Smm * Note: Recall that the ustar format does not store the '/' separating 1534228753Smm * parts 1 & 2, but does store the '/' separating parts 2 & 3. 1535228753Smm */ 1536228753Smmstatic char * 1537228753Smmbuild_ustar_entry_name(char *dest, const char *src, size_t src_length, 1538228753Smm const char *insert) 1539228753Smm{ 1540228753Smm const char *prefix, *prefix_end; 1541228753Smm const char *suffix, *suffix_end; 1542228753Smm const char *filename, *filename_end; 1543228753Smm char *p; 1544228753Smm int need_slash = 0; /* Was there a trailing slash? */ 1545228753Smm size_t suffix_length = 99; 1546228753Smm size_t insert_length; 1547228753Smm 1548228753Smm /* Length of additional dir element to be added. */ 1549228753Smm if (insert == NULL) 1550228753Smm insert_length = 0; 1551228753Smm else 1552228753Smm /* +2 here allows for '/' before and after the insert. */ 1553228753Smm insert_length = strlen(insert) + 2; 1554228753Smm 1555228753Smm /* Step 0: Quick bailout in a common case. */ 1556228753Smm if (src_length < 100 && insert == NULL) { 1557228753Smm strncpy(dest, src, src_length); 1558228753Smm dest[src_length] = '\0'; 1559228753Smm return (dest); 1560228753Smm } 1561228753Smm 1562228753Smm /* Step 1: Locate filename and enforce the length restriction. */ 1563228753Smm filename_end = src + src_length; 1564228753Smm /* Remove trailing '/' chars and '/.' pairs. */ 1565228753Smm for (;;) { 1566228753Smm if (filename_end > src && filename_end[-1] == '/') { 1567228753Smm filename_end --; 1568228753Smm need_slash = 1; /* Remember to restore trailing '/'. */ 1569228753Smm continue; 1570228753Smm } 1571228753Smm if (filename_end > src + 1 && filename_end[-1] == '.' 1572228753Smm && filename_end[-2] == '/') { 1573228753Smm filename_end -= 2; 1574228753Smm need_slash = 1; /* "foo/." will become "foo/" */ 1575228753Smm continue; 1576228753Smm } 1577228753Smm break; 1578228753Smm } 1579228753Smm if (need_slash) 1580228753Smm suffix_length--; 1581228753Smm /* Find start of filename. */ 1582228753Smm filename = filename_end - 1; 1583228753Smm while ((filename > src) && (*filename != '/')) 1584228753Smm filename --; 1585228753Smm if ((*filename == '/') && (filename < filename_end - 1)) 1586228753Smm filename ++; 1587228753Smm /* Adjust filename_end so that filename + insert fits in 99 chars. */ 1588228753Smm suffix_length -= insert_length; 1589228753Smm if (filename_end > filename + suffix_length) 1590228753Smm filename_end = filename + suffix_length; 1591228753Smm /* Calculate max size for "suffix" section (#3 above). */ 1592228753Smm suffix_length -= filename_end - filename; 1593228753Smm 1594228753Smm /* Step 2: Locate the "prefix" section of the dirname, including 1595228753Smm * trailing '/'. */ 1596228753Smm prefix = src; 1597228753Smm prefix_end = prefix + 155; 1598228753Smm if (prefix_end > filename) 1599228753Smm prefix_end = filename; 1600228753Smm while (prefix_end > prefix && *prefix_end != '/') 1601228753Smm prefix_end--; 1602228753Smm if ((prefix_end < filename) && (*prefix_end == '/')) 1603228753Smm prefix_end++; 1604228753Smm 1605228753Smm /* Step 3: Locate the "suffix" section of the dirname, 1606228753Smm * including trailing '/'. */ 1607228753Smm suffix = prefix_end; 1608228753Smm suffix_end = suffix + suffix_length; /* Enforce limit. */ 1609228753Smm if (suffix_end > filename) 1610228753Smm suffix_end = filename; 1611228753Smm if (suffix_end < suffix) 1612228753Smm suffix_end = suffix; 1613228753Smm while (suffix_end > suffix && *suffix_end != '/') 1614228753Smm suffix_end--; 1615228753Smm if ((suffix_end < filename) && (*suffix_end == '/')) 1616228753Smm suffix_end++; 1617228753Smm 1618228753Smm /* Step 4: Build the new name. */ 1619228753Smm /* The OpenBSD strlcpy function is safer, but less portable. */ 1620228753Smm /* Rather than maintain two versions, just use the strncpy version. */ 1621228753Smm p = dest; 1622228753Smm if (prefix_end > prefix) { 1623228753Smm strncpy(p, prefix, prefix_end - prefix); 1624228753Smm p += prefix_end - prefix; 1625228753Smm } 1626228753Smm if (suffix_end > suffix) { 1627228753Smm strncpy(p, suffix, suffix_end - suffix); 1628228753Smm p += suffix_end - suffix; 1629228753Smm } 1630228753Smm if (insert != NULL) { 1631228753Smm /* Note: assume insert does not have leading or trailing '/' */ 1632228753Smm strcpy(p, insert); 1633228753Smm p += strlen(insert); 1634228753Smm *p++ = '/'; 1635228753Smm } 1636228753Smm strncpy(p, filename, filename_end - filename); 1637228753Smm p += filename_end - filename; 1638228753Smm if (need_slash) 1639228753Smm *p++ = '/'; 1640228753Smm *p = '\0'; 1641228753Smm 1642228753Smm return (dest); 1643228753Smm} 1644228753Smm 1645228753Smm/* 1646228753Smm * The ustar header for the pax extended attributes must have a 1647228753Smm * reasonable name: SUSv3 requires 'dirname'/PaxHeader.'pid'/'filename' 1648228753Smm * where 'pid' is the PID of the archiving process. Unfortunately, 1649228753Smm * that makes testing a pain since the output varies for each run, 1650228753Smm * so I'm sticking with the simpler 'dirname'/PaxHeader/'filename' 1651228753Smm * for now. (Someday, I'll make this settable. Then I can use the 1652228753Smm * SUS recommendation as default and test harnesses can override it 1653228753Smm * to get predictable results.) 1654228753Smm * 1655228753Smm * Joerg Schilling has argued that this is unnecessary because, in 1656228753Smm * practice, if the pax extended attributes get extracted as regular 1657232153Smm * files, no one is going to bother reading those attributes to 1658228753Smm * manually restore them. Based on this, 'star' uses 1659228753Smm * /tmp/PaxHeader/'basename' as the ustar header name. This is a 1660228753Smm * tempting argument, in part because it's simpler than the SUSv3 1661228753Smm * recommendation, but I'm not entirely convinced. I'm also 1662228753Smm * uncomfortable with the fact that "/tmp" is a Unix-ism. 1663228753Smm * 1664228753Smm * The following routine leverages build_ustar_entry_name() above and 1665228753Smm * so is simpler than you might think. It just needs to provide the 1666228753Smm * additional path element and handle a few pathological cases). 1667228753Smm */ 1668228753Smmstatic char * 1669228753Smmbuild_pax_attribute_name(char *dest, const char *src) 1670228753Smm{ 1671228753Smm char buff[64]; 1672228753Smm const char *p; 1673228753Smm 1674228753Smm /* Handle the null filename case. */ 1675228753Smm if (src == NULL || *src == '\0') { 1676228753Smm strcpy(dest, "PaxHeader/blank"); 1677228753Smm return (dest); 1678228753Smm } 1679228753Smm 1680228753Smm /* Prune final '/' and other unwanted final elements. */ 1681228753Smm p = src + strlen(src); 1682228753Smm for (;;) { 1683228753Smm /* Ends in "/", remove the '/' */ 1684228753Smm if (p > src && p[-1] == '/') { 1685228753Smm --p; 1686228753Smm continue; 1687228753Smm } 1688228753Smm /* Ends in "/.", remove the '.' */ 1689228753Smm if (p > src + 1 && p[-1] == '.' 1690228753Smm && p[-2] == '/') { 1691228753Smm --p; 1692228753Smm continue; 1693228753Smm } 1694228753Smm break; 1695228753Smm } 1696228753Smm 1697228753Smm /* Pathological case: After above, there was nothing left. 1698228753Smm * This includes "/." "/./." "/.//./." etc. */ 1699228753Smm if (p == src) { 1700228753Smm strcpy(dest, "/PaxHeader/rootdir"); 1701228753Smm return (dest); 1702228753Smm } 1703228753Smm 1704228753Smm /* Convert unadorned "." into a suitable filename. */ 1705228753Smm if (*src == '.' && p == src + 1) { 1706228753Smm strcpy(dest, "PaxHeader/currentdir"); 1707228753Smm return (dest); 1708228753Smm } 1709228753Smm 1710228753Smm /* 1711228753Smm * TODO: Push this string into the 'pax' structure to avoid 1712228753Smm * recomputing it every time. That will also open the door 1713228753Smm * to having clients override it. 1714228753Smm */ 1715228753Smm#if HAVE_GETPID && 0 /* Disable this for now; see above comment. */ 1716228753Smm sprintf(buff, "PaxHeader.%d", getpid()); 1717228753Smm#else 1718228753Smm /* If the platform can't fetch the pid, don't include it. */ 1719228753Smm strcpy(buff, "PaxHeader"); 1720228753Smm#endif 1721232153Smm /* General case: build a ustar-compatible name adding 1722232153Smm * "/PaxHeader/". */ 1723228753Smm build_ustar_entry_name(dest, src, p - src, buff); 1724228753Smm 1725228753Smm return (dest); 1726228753Smm} 1727228753Smm 1728232153Smm/* 1729232153Smm * GNU PAX Format 1.0 requires the special name, which pattern is: 1730232153Smm * <dir>/GNUSparseFile.<pid>/<original file name> 1731232153Smm * 1732324417Smm * Since reproducible archives are more important, use 0 as pid. 1733318482Smm * 1734232153Smm * This function is used for only Sparse file, a file type of which 1735232153Smm * is regular file. 1736232153Smm */ 1737232153Smmstatic char * 1738232153Smmbuild_gnu_sparse_name(char *dest, const char *src) 1739228753Smm{ 1740232153Smm const char *p; 1741228753Smm 1742232153Smm /* Handle the null filename case. */ 1743232153Smm if (src == NULL || *src == '\0') { 1744232153Smm strcpy(dest, "GNUSparseFile/blank"); 1745232153Smm return (dest); 1746232153Smm } 1747228753Smm 1748232153Smm /* Prune final '/' and other unwanted final elements. */ 1749232153Smm p = src + strlen(src); 1750232153Smm for (;;) { 1751232153Smm /* Ends in "/", remove the '/' */ 1752232153Smm if (p > src && p[-1] == '/') { 1753232153Smm --p; 1754232153Smm continue; 1755232153Smm } 1756232153Smm /* Ends in "/.", remove the '.' */ 1757232153Smm if (p > src + 1 && p[-1] == '.' 1758232153Smm && p[-2] == '/') { 1759232153Smm --p; 1760232153Smm continue; 1761232153Smm } 1762232153Smm break; 1763232153Smm } 1764232153Smm 1765232153Smm /* General case: build a ustar-compatible name adding 1766232153Smm * "/GNUSparseFile/". */ 1767318482Smm build_ustar_entry_name(dest, src, p - src, "GNUSparseFile.0"); 1768232153Smm 1769232153Smm return (dest); 1770228753Smm} 1771228753Smm 1772232153Smm/* Write two null blocks for the end of archive */ 1773228753Smmstatic int 1774232153Smmarchive_write_pax_close(struct archive_write *a) 1775228753Smm{ 1776232153Smm return (__archive_write_nulls(a, 512 * 2)); 1777232153Smm} 1778232153Smm 1779232153Smmstatic int 1780232153Smmarchive_write_pax_free(struct archive_write *a) 1781232153Smm{ 1782228753Smm struct pax *pax; 1783228753Smm 1784228753Smm pax = (struct pax *)a->format_data; 1785228753Smm if (pax == NULL) 1786228753Smm return (ARCHIVE_OK); 1787228753Smm 1788228753Smm archive_string_free(&pax->pax_header); 1789232153Smm archive_string_free(&pax->sparse_map); 1790232153Smm archive_string_free(&pax->l_url_encoded_name); 1791232153Smm sparse_list_clear(pax); 1792228753Smm free(pax); 1793228753Smm a->format_data = NULL; 1794228753Smm return (ARCHIVE_OK); 1795228753Smm} 1796228753Smm 1797228753Smmstatic int 1798228753Smmarchive_write_pax_finish_entry(struct archive_write *a) 1799228753Smm{ 1800228753Smm struct pax *pax; 1801232153Smm uint64_t remaining; 1802228753Smm int ret; 1803228753Smm 1804228753Smm pax = (struct pax *)a->format_data; 1805232153Smm remaining = pax->entry_bytes_remaining; 1806232153Smm if (remaining == 0) { 1807232153Smm while (pax->sparse_list) { 1808232153Smm struct sparse_block *sb; 1809232153Smm if (!pax->sparse_list->is_hole) 1810232153Smm remaining += pax->sparse_list->remaining; 1811232153Smm sb = pax->sparse_list->next; 1812232153Smm free(pax->sparse_list); 1813232153Smm pax->sparse_list = sb; 1814232153Smm } 1815232153Smm } 1816238856Smm ret = __archive_write_nulls(a, (size_t)(remaining + pax->entry_padding)); 1817228753Smm pax->entry_bytes_remaining = pax->entry_padding = 0; 1818228753Smm return (ret); 1819228753Smm} 1820228753Smm 1821232153Smmstatic ssize_t 1822232153Smmarchive_write_pax_data(struct archive_write *a, const void *buff, size_t s) 1823228753Smm{ 1824232153Smm struct pax *pax; 1825232153Smm size_t ws; 1826232153Smm size_t total; 1827228753Smm int ret; 1828228753Smm 1829232153Smm pax = (struct pax *)a->format_data; 1830232153Smm 1831232153Smm /* 1832232153Smm * According to GNU PAX format 1.0, write a sparse map 1833232153Smm * before the body. 1834232153Smm */ 1835232153Smm if (archive_strlen(&(pax->sparse_map))) { 1836232153Smm ret = __archive_write_output(a, pax->sparse_map.s, 1837232153Smm archive_strlen(&(pax->sparse_map))); 1838228753Smm if (ret != ARCHIVE_OK) 1839228753Smm return (ret); 1840232153Smm ret = __archive_write_nulls(a, pax->sparse_map_padding); 1841232153Smm if (ret != ARCHIVE_OK) 1842232153Smm return (ret); 1843232153Smm archive_string_empty(&(pax->sparse_map)); 1844228753Smm } 1845228753Smm 1846232153Smm total = 0; 1847232153Smm while (total < s) { 1848232153Smm const unsigned char *p; 1849228753Smm 1850232153Smm while (pax->sparse_list != NULL && 1851232153Smm pax->sparse_list->remaining == 0) { 1852232153Smm struct sparse_block *sb = pax->sparse_list->next; 1853232153Smm free(pax->sparse_list); 1854232153Smm pax->sparse_list = sb; 1855232153Smm } 1856228753Smm 1857232153Smm if (pax->sparse_list == NULL) 1858232153Smm return (total); 1859232153Smm 1860232153Smm p = ((const unsigned char *)buff) + total; 1861232153Smm ws = s - total; 1862232153Smm if (ws > pax->sparse_list->remaining) 1863238856Smm ws = (size_t)pax->sparse_list->remaining; 1864232153Smm 1865232153Smm if (pax->sparse_list->is_hole) { 1866232153Smm /* Current block is hole thus we do not write 1867232153Smm * the body. */ 1868232153Smm pax->sparse_list->remaining -= ws; 1869232153Smm total += ws; 1870232153Smm continue; 1871232153Smm } 1872232153Smm 1873232153Smm ret = __archive_write_output(a, p, ws); 1874232153Smm pax->sparse_list->remaining -= ws; 1875232153Smm total += ws; 1876232153Smm if (ret != ARCHIVE_OK) 1877232153Smm return (ret); 1878232153Smm } 1879232153Smm return (total); 1880228753Smm} 1881228753Smm 1882228753Smmstatic int 1883232153Smmhas_non_ASCII(const char *_p) 1884228753Smm{ 1885232153Smm const unsigned char *p = (const unsigned char *)_p; 1886232153Smm 1887232153Smm if (p == NULL) 1888228753Smm return (1); 1889232153Smm while (*p != '\0' && *p < 128) 1890232153Smm p++; 1891232153Smm return (*p != '\0'); 1892228753Smm} 1893228753Smm 1894228753Smm/* 1895228753Smm * Used by extended attribute support; encodes the name 1896228753Smm * so that there will be no '=' characters in the result. 1897228753Smm */ 1898228753Smmstatic char * 1899228753Smmurl_encode(const char *in) 1900228753Smm{ 1901228753Smm const char *s; 1902228753Smm char *d; 1903228753Smm int out_len = 0; 1904228753Smm char *out; 1905228753Smm 1906228753Smm for (s = in; *s != '\0'; s++) { 1907228753Smm if (*s < 33 || *s > 126 || *s == '%' || *s == '=') 1908228753Smm out_len += 3; 1909228753Smm else 1910228753Smm out_len++; 1911228753Smm } 1912228753Smm 1913228753Smm out = (char *)malloc(out_len + 1); 1914228753Smm if (out == NULL) 1915228753Smm return (NULL); 1916228753Smm 1917228753Smm for (s = in, d = out; *s != '\0'; s++) { 1918228753Smm /* encode any non-printable ASCII character or '%' or '=' */ 1919228753Smm if (*s < 33 || *s > 126 || *s == '%' || *s == '=') { 1920228753Smm /* URL encoding is '%' followed by two hex digits */ 1921228753Smm *d++ = '%'; 1922228753Smm *d++ = "0123456789ABCDEF"[0x0f & (*s >> 4)]; 1923228753Smm *d++ = "0123456789ABCDEF"[0x0f & *s]; 1924228753Smm } else { 1925228753Smm *d++ = *s; 1926228753Smm } 1927228753Smm } 1928228753Smm *d = '\0'; 1929228753Smm return (out); 1930228753Smm} 1931228753Smm 1932228753Smm/* 1933228753Smm * Encode a sequence of bytes into a C string using base-64 encoding. 1934228753Smm * 1935228753Smm * Returns a null-terminated C string allocated with malloc(); caller 1936228753Smm * is responsible for freeing the result. 1937228753Smm */ 1938228753Smmstatic char * 1939228753Smmbase64_encode(const char *s, size_t len) 1940228753Smm{ 1941228753Smm static const char digits[64] = 1942228753Smm { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', 1943228753Smm 'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d', 1944228753Smm 'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s', 1945228753Smm 't','u','v','w','x','y','z','0','1','2','3','4','5','6','7', 1946228753Smm '8','9','+','/' }; 1947228753Smm int v; 1948228753Smm char *d, *out; 1949228753Smm 1950228753Smm /* 3 bytes becomes 4 chars, but round up and allow for trailing NUL */ 1951228753Smm out = (char *)malloc((len * 4 + 2) / 3 + 1); 1952228753Smm if (out == NULL) 1953228753Smm return (NULL); 1954228753Smm d = out; 1955228753Smm 1956228753Smm /* Convert each group of 3 bytes into 4 characters. */ 1957228753Smm while (len >= 3) { 1958228753Smm v = (((int)s[0] << 16) & 0xff0000) 1959228753Smm | (((int)s[1] << 8) & 0xff00) 1960228753Smm | (((int)s[2]) & 0x00ff); 1961228753Smm s += 3; 1962228753Smm len -= 3; 1963228753Smm *d++ = digits[(v >> 18) & 0x3f]; 1964228753Smm *d++ = digits[(v >> 12) & 0x3f]; 1965228753Smm *d++ = digits[(v >> 6) & 0x3f]; 1966228753Smm *d++ = digits[(v) & 0x3f]; 1967228753Smm } 1968228753Smm /* Handle final group of 1 byte (2 chars) or 2 bytes (3 chars). */ 1969228753Smm switch (len) { 1970228753Smm case 0: break; 1971228753Smm case 1: 1972228753Smm v = (((int)s[0] << 16) & 0xff0000); 1973228753Smm *d++ = digits[(v >> 18) & 0x3f]; 1974228753Smm *d++ = digits[(v >> 12) & 0x3f]; 1975228753Smm break; 1976228753Smm case 2: 1977228753Smm v = (((int)s[0] << 16) & 0xff0000) 1978228753Smm | (((int)s[1] << 8) & 0xff00); 1979228753Smm *d++ = digits[(v >> 18) & 0x3f]; 1980228753Smm *d++ = digits[(v >> 12) & 0x3f]; 1981228753Smm *d++ = digits[(v >> 6) & 0x3f]; 1982228753Smm break; 1983228753Smm } 1984228753Smm /* Add trailing NUL character so output is a valid C string. */ 1985228753Smm *d = '\0'; 1986228753Smm return (out); 1987228753Smm} 1988232153Smm 1989232153Smmstatic void 1990232153Smmsparse_list_clear(struct pax *pax) 1991232153Smm{ 1992232153Smm while (pax->sparse_list != NULL) { 1993232153Smm struct sparse_block *sb = pax->sparse_list; 1994232153Smm pax->sparse_list = sb->next; 1995232153Smm free(sb); 1996232153Smm } 1997232153Smm pax->sparse_tail = NULL; 1998232153Smm} 1999232153Smm 2000232153Smmstatic int 2001232153Smm_sparse_list_add_block(struct pax *pax, int64_t offset, int64_t length, 2002232153Smm int is_hole) 2003232153Smm{ 2004232153Smm struct sparse_block *sb; 2005232153Smm 2006232153Smm sb = (struct sparse_block *)malloc(sizeof(*sb)); 2007232153Smm if (sb == NULL) 2008232153Smm return (ARCHIVE_FATAL); 2009232153Smm sb->next = NULL; 2010232153Smm sb->is_hole = is_hole; 2011232153Smm sb->offset = offset; 2012232153Smm sb->remaining = length; 2013248616Smm if (pax->sparse_list == NULL || pax->sparse_tail == NULL) 2014232153Smm pax->sparse_list = pax->sparse_tail = sb; 2015232153Smm else { 2016232153Smm pax->sparse_tail->next = sb; 2017232153Smm pax->sparse_tail = sb; 2018232153Smm } 2019232153Smm return (ARCHIVE_OK); 2020232153Smm} 2021232153Smm 2022232153Smmstatic int 2023232153Smmsparse_list_add(struct pax *pax, int64_t offset, int64_t length) 2024232153Smm{ 2025232153Smm int64_t last_offset; 2026232153Smm int r; 2027232153Smm 2028232153Smm if (pax->sparse_tail == NULL) 2029232153Smm last_offset = 0; 2030232153Smm else { 2031232153Smm last_offset = pax->sparse_tail->offset + 2032232153Smm pax->sparse_tail->remaining; 2033232153Smm } 2034232153Smm if (last_offset < offset) { 2035232153Smm /* Add a hole block. */ 2036232153Smm r = _sparse_list_add_block(pax, last_offset, 2037232153Smm offset - last_offset, 1); 2038232153Smm if (r != ARCHIVE_OK) 2039232153Smm return (r); 2040232153Smm } 2041232153Smm /* Add data block. */ 2042232153Smm return (_sparse_list_add_block(pax, offset, length, 0)); 2043232153Smm} 2044232153Smm 2045