1/* List a tar archive, with support routines for reading a tar archive. 2 3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 4 2001, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. 5 6 Written by John Gilmore, on 1985-08-26. 7 8 This program is free software; you can redistribute it and/or modify it 9 under the terms of the GNU General Public License as published by the 10 Free Software Foundation; either version 2, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 16 Public License for more details. 17 18 You should have received a copy of the GNU General Public License along 19 with this program; if not, write to the Free Software Foundation, Inc., 20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 21 22#include <system.h> 23#include <inttostr.h> 24#include <quotearg.h> 25 26#ifdef __APPLE__ 27#include <copyfile.h> 28#include <sys/param.h> 29#include <sys/queue.h> 30struct copyfile_list_entry_t { 31 char *src; 32 char *dst; 33 char *tmp; 34 LIST_ENTRY(copyfile_list_entry_t) link; 35} *cle; 36LIST_HEAD(copyfile_list_t, copyfile_list_entry_t) copyfile_list; 37#endif 38 39#include "common.h" 40 41#define max(a, b) ((a) < (b) ? (b) : (a)) 42 43union block *current_header; /* points to current archive header */ 44enum archive_format current_format; /* recognized format */ 45union block *recent_long_name; /* recent long name header and contents */ 46union block *recent_long_link; /* likewise, for long link */ 47size_t recent_long_name_blocks; /* number of blocks in recent_long_name */ 48size_t recent_long_link_blocks; /* likewise, for long link */ 49 50static uintmax_t from_header (const char *, size_t, const char *, 51 uintmax_t, uintmax_t, bool, bool); 52 53/* Base 64 digits; see Internet RFC 2045 Table 1. */ 54static char const base_64_digits[64] = 55{ 56 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 57 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 58 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 59 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 60 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' 61}; 62 63/* Table of base-64 digit values indexed by unsigned chars. 64 The value is 64 for unsigned chars that are not base-64 digits. */ 65static char base64_map[UCHAR_MAX + 1]; 66 67static void 68base64_init (void) 69{ 70 int i; 71 memset (base64_map, 64, sizeof base64_map); 72 for (i = 0; i < 64; i++) 73 base64_map[(int) base_64_digits[i]] = i; 74} 75 76/* Main loop for reading an archive. */ 77void 78read_and (void (*do_something) (void)) 79{ 80 enum read_header status = HEADER_STILL_UNREAD; 81 enum read_header prev_status; 82 struct timespec mtime; 83 84#ifdef __APPLE__ 85 int disable_copyfile = (getenv(COPYFILE_DISABLE_VAR) != NULL); 86 struct copyfile_list_entry_t *cle; 87 LIST_INIT(©file_list); 88#endif 89 90 base64_init (); 91 name_gather (); 92 93 open_archive (ACCESS_READ); 94 do 95 { 96 prev_status = status; 97 tar_stat_destroy (¤t_stat_info); 98 99 status = read_header (false); 100 switch (status) 101 { 102 case HEADER_STILL_UNREAD: 103 case HEADER_SUCCESS_EXTENDED: 104 abort (); 105 106 case HEADER_SUCCESS: 107 108 /* Valid header. We should decode next field (mode) first. 109 Ensure incoming names are null terminated. */ 110 111 if (! name_match (current_stat_info.file_name) 112 || (NEWER_OPTION_INITIALIZED (newer_mtime_option) 113 /* FIXME: We get mtime now, and again later; this causes 114 duplicate diagnostics if header.mtime is bogus. */ 115 && ((mtime.tv_sec 116 = TIME_FROM_HEADER (current_header->header.mtime)), 117 /* FIXME: Grab fractional time stamps from 118 extended header. */ 119 mtime.tv_nsec = 0, 120 current_stat_info.mtime = mtime, 121 OLDER_TAR_STAT_TIME (current_stat_info, m))) 122 || excluded_name (current_stat_info.file_name)) 123 { 124 switch (current_header->header.typeflag) 125 { 126 case GNUTYPE_VOLHDR: 127 case GNUTYPE_MULTIVOL: 128 break; 129 130 case DIRTYPE: 131 if (show_omitted_dirs_option) 132 WARN ((0, 0, _("%s: Omitting"), 133 quotearg_colon (current_stat_info.file_name))); 134 /* Fall through. */ 135 default: 136 decode_header (current_header, 137 ¤t_stat_info, ¤t_format, 0); 138 skip_member (); 139 continue; 140 } 141 } 142 143 (*do_something) (); 144 continue; 145 146 case HEADER_ZERO_BLOCK: 147 if (block_number_option) 148 { 149 char buf[UINTMAX_STRSIZE_BOUND]; 150 fprintf (stdlis, _("block %s: ** Block of NULs **\n"), 151 STRINGIFY_BIGINT (current_block_ordinal (), buf)); 152 } 153 154 set_next_block_after (current_header); 155 156 if (!ignore_zeros_option) 157 { 158 char buf[UINTMAX_STRSIZE_BOUND]; 159 160 status = read_header (false); 161 if (status == HEADER_ZERO_BLOCK) 162 break; 163 WARN ((0, 0, _("A lone zero block at %s"), 164 STRINGIFY_BIGINT (current_block_ordinal (), buf))); 165 break; 166 } 167 status = prev_status; 168 continue; 169 170 case HEADER_END_OF_FILE: 171 if (block_number_option) 172 { 173 char buf[UINTMAX_STRSIZE_BOUND]; 174 fprintf (stdlis, _("block %s: ** End of File **\n"), 175 STRINGIFY_BIGINT (current_block_ordinal (), buf)); 176 } 177 break; 178 179 case HEADER_FAILURE: 180 /* If the previous header was good, tell them that we are 181 skipping bad ones. */ 182 set_next_block_after (current_header); 183 switch (prev_status) 184 { 185 case HEADER_STILL_UNREAD: 186 ERROR ((0, 0, _("This does not look like a tar archive"))); 187 /* Fall through. */ 188 189 case HEADER_ZERO_BLOCK: 190 case HEADER_SUCCESS: 191 if (block_number_option) 192 { 193 char buf[UINTMAX_STRSIZE_BOUND]; 194 off_t block_ordinal = current_block_ordinal (); 195 block_ordinal -= recent_long_name_blocks; 196 block_ordinal -= recent_long_link_blocks; 197 fprintf (stdlis, _("block %s: "), 198 STRINGIFY_BIGINT (block_ordinal, buf)); 199 } 200 ERROR ((0, 0, _("Skipping to next header"))); 201 break; 202 203 case HEADER_END_OF_FILE: 204 case HEADER_FAILURE: 205 /* We are in the middle of a cascade of errors. */ 206 break; 207 208 case HEADER_SUCCESS_EXTENDED: 209 abort (); 210 } 211 continue; 212 } 213 break; 214 } 215 while (!all_names_found (¤t_stat_info)); 216 217#ifdef __APPLE__ 218 LIST_FOREACH(cle, ©file_list, link) 219 { 220 if(!disable_copyfile && copyfile(cle->tmp, cle->dst, 0, COPYFILE_UNPACK | COPYFILE_NOFOLLOW | COPYFILE_ACL | COPYFILE_XATTR) == 0) 221 { 222 unlink(cle->tmp); 223 } 224 else 225 { 226 if (!disable_copyfile) 227 { 228 WARN((0, 0, "copyfile unpack (%s) failed: %s", cle->dst, strerror(errno))); 229 } 230 rename(cle->tmp, cle->src); 231 } 232 233 /* 5781559: Make sure EAs don't destroy overriding quarantine information. */ 234 apply_qtn_to_path(cle->dst); 235 236 free(cle->tmp); 237 free(cle->dst); 238 free(cle->src); 239 } 240#endif 241 242 close_archive (); 243 names_notfound (); /* print names not found */ 244} 245 246/* Print a header block, based on tar options. */ 247void 248list_archive (void) 249{ 250 off_t block_ordinal = current_block_ordinal (); 251 /* Print the header block. */ 252 253 decode_header (current_header, ¤t_stat_info, ¤t_format, 0); 254 if (verbose_option) 255 print_header (¤t_stat_info, block_ordinal); 256 257 if (incremental_option) 258 { 259 if (verbose_option > 2) 260 { 261 if (is_dumpdir (¤t_stat_info)) 262 list_dumpdir (current_stat_info.dumpdir, 263 dumpdir_size (current_stat_info.dumpdir)); 264 } 265 } 266 267 skip_member (); 268} 269 270/* Check header checksum */ 271/* The standard BSD tar sources create the checksum by adding up the 272 bytes in the header as type char. I think the type char was unsigned 273 on the PDP-11, but it's signed on the Next and Sun. It looks like the 274 sources to BSD tar were never changed to compute the checksum 275 correctly, so both the Sun and Next add the bytes of the header as 276 signed chars. This doesn't cause a problem until you get a file with 277 a name containing characters with the high bit set. So tar_checksum 278 computes two checksums -- signed and unsigned. */ 279 280enum read_header 281tar_checksum (union block *header, bool silent) 282{ 283 size_t i; 284 int unsigned_sum = 0; /* the POSIX one :-) */ 285 int signed_sum = 0; /* the Sun one :-( */ 286 int recorded_sum; 287 uintmax_t parsed_sum; 288 char *p; 289 290 p = header->buffer; 291 for (i = sizeof *header; i-- != 0;) 292 { 293 unsigned_sum += (unsigned char) *p; 294 signed_sum += (signed char) (*p++); 295 } 296 297 if (unsigned_sum == 0) 298 return HEADER_ZERO_BLOCK; 299 300 /* Adjust checksum to count the "chksum" field as blanks. */ 301 302 for (i = sizeof header->header.chksum; i-- != 0;) 303 { 304 unsigned_sum -= (unsigned char) header->header.chksum[i]; 305 signed_sum -= (signed char) (header->header.chksum[i]); 306 } 307 unsigned_sum += ' ' * sizeof header->header.chksum; 308 signed_sum += ' ' * sizeof header->header.chksum; 309 310 parsed_sum = from_header (header->header.chksum, 311 sizeof header->header.chksum, 0, 312 (uintmax_t) 0, 313 (uintmax_t) TYPE_MAXIMUM (int), true, silent); 314 if (parsed_sum == (uintmax_t) -1) 315 return HEADER_FAILURE; 316 317 recorded_sum = parsed_sum; 318 319 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum) 320 return HEADER_FAILURE; 321 322 return HEADER_SUCCESS; 323} 324 325/* Read a block that's supposed to be a header block. Return its 326 address in "current_header", and if it is good, the file's size 327 and names (file name, link name) in *info. 328 329 Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a 330 block full of zeros (EOF marker). 331 332 If RAW_EXTENDED_HEADERS is nonzero, do not automagically fold the 333 GNU long name and link headers into later headers. 334 335 You must always set_next_block_after(current_header) to skip past 336 the header which this routine reads. */ 337 338enum read_header 339read_header_primitive (bool raw_extended_headers, struct tar_stat_info *info) 340{ 341 union block *header; 342 union block *header_copy; 343 char *bp; 344 union block *data_block; 345 size_t size, written; 346 union block *next_long_name = 0; 347 union block *next_long_link = 0; 348 size_t next_long_name_blocks; 349 size_t next_long_link_blocks; 350 351 while (1) 352 { 353 enum read_header status; 354 355 header = find_next_block (); 356 current_header = header; 357 if (!header) 358 return HEADER_END_OF_FILE; 359 360 if ((status = tar_checksum (header, false)) != HEADER_SUCCESS) 361 return status; 362 363 /* Good block. Decode file size and return. */ 364 365 if (header->header.typeflag == LNKTYPE) 366 info->stat.st_size = 0; /* links 0 size on tape */ 367 else 368 info->stat.st_size = OFF_FROM_HEADER (header->header.size); 369 370 if (header->header.typeflag == GNUTYPE_LONGNAME 371 || header->header.typeflag == GNUTYPE_LONGLINK 372 || header->header.typeflag == XHDTYPE 373 || header->header.typeflag == XGLTYPE 374 || header->header.typeflag == SOLARIS_XHDTYPE) 375 { 376 if (raw_extended_headers) 377 return HEADER_SUCCESS_EXTENDED; 378 else if (header->header.typeflag == GNUTYPE_LONGNAME 379 || header->header.typeflag == GNUTYPE_LONGLINK) 380 { 381 size_t name_size = info->stat.st_size; 382 size_t n = name_size % BLOCKSIZE; 383 size = name_size + BLOCKSIZE; 384 if (n) 385 size += BLOCKSIZE - n; 386 387 if (name_size != info->stat.st_size || size < name_size) 388 xalloc_die (); 389 390 header_copy = xmalloc (size + 1); 391 392 if (header->header.typeflag == GNUTYPE_LONGNAME) 393 { 394 if (next_long_name) 395 free (next_long_name); 396 next_long_name = header_copy; 397 next_long_name_blocks = size / BLOCKSIZE; 398 } 399 else 400 { 401 if (next_long_link) 402 free (next_long_link); 403 next_long_link = header_copy; 404 next_long_link_blocks = size / BLOCKSIZE; 405 } 406 407 set_next_block_after (header); 408 *header_copy = *header; 409 bp = header_copy->buffer + BLOCKSIZE; 410 411 for (size -= BLOCKSIZE; size > 0; size -= written) 412 { 413 data_block = find_next_block (); 414 if (! data_block) 415 { 416 ERROR ((0, 0, _("Unexpected EOF in archive"))); 417 break; 418 } 419 written = available_space_after (data_block); 420 if (written > size) 421 written = size; 422 423 memcpy (bp, data_block->buffer, written); 424 bp += written; 425 set_next_block_after ((union block *) 426 (data_block->buffer + written - 1)); 427 } 428 429 *bp = '\0'; 430 } 431 else if (header->header.typeflag == XHDTYPE 432 || header->header.typeflag == SOLARIS_XHDTYPE) 433 xheader_read (&info->xhdr, header, 434 OFF_FROM_HEADER (header->header.size)); 435 else if (header->header.typeflag == XGLTYPE) 436 { 437 struct xheader xhdr; 438 memset (&xhdr, 0, sizeof xhdr); 439 xheader_read (&xhdr, header, 440 OFF_FROM_HEADER (header->header.size)); 441 xheader_decode_global (&xhdr); 442 xheader_destroy (&xhdr); 443 } 444 445 /* Loop! */ 446 447 } 448 else 449 { 450 char const *name; 451 struct posix_header const *h = ¤t_header->header; 452 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1]; 453 454 if (recent_long_name) 455 free (recent_long_name); 456 457 if (next_long_name) 458 { 459 name = next_long_name->buffer + BLOCKSIZE; 460 recent_long_name = next_long_name; 461 recent_long_name_blocks = next_long_name_blocks; 462 } 463 else 464 { 465 /* Accept file names as specified by POSIX.1-1996 466 section 10.1.1. */ 467 char *np = namebuf; 468 469 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0) 470 { 471 memcpy (np, h->prefix, sizeof h->prefix); 472 np[sizeof h->prefix] = '\0'; 473 np += strlen (np); 474 *np++ = '/'; 475 } 476 memcpy (np, h->name, sizeof h->name); 477 np[sizeof h->name] = '\0'; 478 name = namebuf; 479 recent_long_name = 0; 480 recent_long_name_blocks = 0; 481 } 482 assign_string (&info->orig_file_name, name); 483 assign_string (&info->file_name, name); 484 info->had_trailing_slash = strip_trailing_slashes (info->file_name); 485 486 if (recent_long_link) 487 free (recent_long_link); 488 489 if (next_long_link) 490 { 491 name = next_long_link->buffer + BLOCKSIZE; 492 recent_long_link = next_long_link; 493 recent_long_link_blocks = next_long_link_blocks; 494 } 495 else 496 { 497 memcpy (namebuf, h->linkname, sizeof h->linkname); 498 namebuf[sizeof h->linkname] = '\0'; 499 name = namebuf; 500 recent_long_link = 0; 501 recent_long_link_blocks = 0; 502 } 503 assign_string (&info->link_name, name); 504 505 return HEADER_SUCCESS; 506 } 507 } 508} 509 510enum read_header 511read_header (bool raw_extended_headers) 512{ 513 return read_header_primitive (raw_extended_headers, ¤t_stat_info); 514} 515 516static char * 517decode_xform (char *file_name, void *data) 518{ 519 xform_type type = *(xform_type*)data; 520 521 switch (type) 522 { 523 case xform_symlink: 524 /* FIXME: It is not quite clear how and to which extent are the symbolic 525 links subject to filename transformation. In the absence of another 526 solution, symbolic links are exempt from component stripping and 527 name suffix normalization, but subject to filename transformation 528 proper. */ 529 return file_name; 530 531 case xform_link: 532 file_name = safer_name_suffix (file_name, true, absolute_names_option); 533 break; 534 535 case xform_regfile: 536 file_name = safer_name_suffix (file_name, false, absolute_names_option); 537 break; 538 } 539 540 if (strip_name_components) 541 { 542 size_t prefix_len = stripped_prefix_len (file_name, 543 strip_name_components); 544 if (prefix_len == (size_t) -1) 545 prefix_len = strlen (file_name); 546 file_name += prefix_len; 547 } 548 return file_name; 549} 550 551bool 552transform_member_name (char **pinput, xform_type type) 553{ 554 return transform_name_fp (pinput, decode_xform, &type); 555} 556 557#define ISOCTAL(c) ((c)>='0'&&(c)<='7') 558 559/* Decode things from a file HEADER block into STAT_INFO, also setting 560 *FORMAT_POINTER depending on the header block format. If 561 DO_USER_GROUP, decode the user/group information (this is useful 562 for extraction, but waste time when merely listing). 563 564 read_header() has already decoded the checksum and length, so we don't. 565 566 This routine should *not* be called twice for the same block, since 567 the two calls might use different DO_USER_GROUP values and thus 568 might end up with different uid/gid for the two calls. If anybody 569 wants the uid/gid they should decode it first, and other callers 570 should decode it without uid/gid before calling a routine, 571 e.g. print_header, that assumes decoded data. */ 572void 573decode_header (union block *header, struct tar_stat_info *stat_info, 574 enum archive_format *format_pointer, int do_user_group) 575{ 576 enum archive_format format; 577 578 if (strcmp (header->header.magic, TMAGIC) == 0) 579 { 580 if (header->star_header.prefix[130] == 0 581 && ISOCTAL (header->star_header.atime[0]) 582 && header->star_header.atime[11] == ' ' 583 && ISOCTAL (header->star_header.ctime[0]) 584 && header->star_header.ctime[11] == ' ') 585 format = STAR_FORMAT; 586 else if (stat_info->xhdr.size) 587 format = POSIX_FORMAT; 588 else 589 format = USTAR_FORMAT; 590 } 591 else if (strcmp (header->header.magic, OLDGNU_MAGIC) == 0) 592 format = OLDGNU_FORMAT; 593 else 594 format = V7_FORMAT; 595 *format_pointer = format; 596 597 stat_info->stat.st_mode = MODE_FROM_HEADER (header->header.mode); 598 stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime); 599 stat_info->mtime.tv_nsec = 0; 600 assign_string (&stat_info->uname, 601 header->header.uname[0] ? header->header.uname : NULL); 602 assign_string (&stat_info->gname, 603 header->header.gname[0] ? header->header.gname : NULL); 604 605 if (format == OLDGNU_FORMAT && incremental_option) 606 { 607 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime); 608 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime); 609 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0; 610 } 611 else if (format == STAR_FORMAT) 612 { 613 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime); 614 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime); 615 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0; 616 } 617 else 618 stat_info->atime = stat_info->ctime = start_time; 619 620 if (format == V7_FORMAT) 621 { 622 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid); 623 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid); 624 stat_info->stat.st_rdev = 0; 625 } 626 else 627 { 628 if (do_user_group) 629 { 630 /* FIXME: Decide if this should somewhat depend on -p. */ 631 632 if (numeric_owner_option 633 || !*header->header.uname 634 || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid)) 635 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid); 636 637 if (numeric_owner_option 638 || !*header->header.gname 639 || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid)) 640 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid); 641 } 642 643 switch (header->header.typeflag) 644 { 645 case BLKTYPE: 646 case CHRTYPE: 647 stat_info->stat.st_rdev = 648 makedev (MAJOR_FROM_HEADER (header->header.devmajor), 649 MINOR_FROM_HEADER (header->header.devminor)); 650 break; 651 652 default: 653 stat_info->stat.st_rdev = 0; 654 } 655 } 656 657 stat_info->archive_file_size = stat_info->stat.st_size; 658 xheader_decode (stat_info); 659 660 if (sparse_member_p (stat_info)) 661 { 662 sparse_fixup_header (stat_info); 663 stat_info->is_sparse = true; 664 } 665 else 666 { 667 stat_info->is_sparse = false; 668 if (((current_format == GNU_FORMAT 669 || current_format == OLDGNU_FORMAT) 670 && current_header->header.typeflag == GNUTYPE_DUMPDIR) 671 || stat_info->dumpdir) 672 stat_info->is_dumpdir = true; 673 } 674 675 transform_member_name (&stat_info->file_name, xform_regfile); 676} 677 678/* Convert buffer at WHERE0 of size DIGS from external format to 679 uintmax_t. DIGS must be positive. If TYPE is nonnull, the data 680 are of type TYPE. The buffer must represent a value in the range 681 -MINUS_MINVAL through MAXVAL. If OCTAL_ONLY, allow only octal 682 numbers instead of the other GNU extensions. Return -1 on error, 683 diagnosing the error if TYPE is nonnull and if !SILENT. */ 684static uintmax_t 685from_header (char const *where0, size_t digs, char const *type, 686 uintmax_t minus_minval, uintmax_t maxval, 687 bool octal_only, bool silent) 688{ 689 uintmax_t value; 690 char const *where = where0; 691 char const *lim = where + digs; 692 int negative = 0; 693 694 /* Accommodate buggy tar of unknown vintage, which outputs leading 695 NUL if the previous field overflows. */ 696 where += !*where; 697 698 /* Accommodate older tars, which output leading spaces. */ 699 for (;;) 700 { 701 if (where == lim) 702 { 703 if (type && !silent) 704 ERROR ((0, 0, 705 /* TRANSLATORS: %s is type of the value (gid_t, uid_t, etc.) */ 706 _("Blanks in header where numeric %s value expected"), 707 type)); 708 return -1; 709 } 710 if (!ISSPACE ((unsigned char) *where)) 711 break; 712 where++; 713 } 714 715 value = 0; 716 if (ISODIGIT (*where)) 717 { 718 char const *where1 = where; 719 uintmax_t overflow = 0; 720 721 for (;;) 722 { 723 value += *where++ - '0'; 724 if (where == lim || ! ISODIGIT (*where)) 725 break; 726 overflow |= value ^ (value << LG_8 >> LG_8); 727 value <<= LG_8; 728 } 729 730 /* Parse the output of older, unportable tars, which generate 731 negative values in two's complement octal. If the leading 732 nonzero digit is 1, we can't recover the original value 733 reliably; so do this only if the digit is 2 or more. This 734 catches the common case of 32-bit negative time stamps. */ 735 if ((overflow || maxval < value) && '2' <= *where1 && type) 736 { 737 /* Compute the negative of the input value, assuming two's 738 complement. */ 739 int digit = (*where1 - '0') | 4; 740 overflow = 0; 741 value = 0; 742 where = where1; 743 for (;;) 744 { 745 value += 7 - digit; 746 where++; 747 if (where == lim || ! ISODIGIT (*where)) 748 break; 749 digit = *where - '0'; 750 overflow |= value ^ (value << LG_8 >> LG_8); 751 value <<= LG_8; 752 } 753 value++; 754 overflow |= !value; 755 756 if (!overflow && value <= minus_minval) 757 { 758 if (!silent) 759 WARN ((0, 0, 760 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */ 761 _("Archive octal value %.*s is out of %s range; assuming two's complement"), 762 (int) (where - where1), where1, type)); 763 negative = 1; 764 } 765 } 766 767 if (overflow) 768 { 769 if (type && !silent) 770 ERROR ((0, 0, 771 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */ 772 _("Archive octal value %.*s is out of %s range"), 773 (int) (where - where1), where1, type)); 774 return -1; 775 } 776 } 777 else if (octal_only) 778 { 779 /* Suppress the following extensions. */ 780 } 781 else if (*where == '-' || *where == '+') 782 { 783 /* Parse base-64 output produced only by tar test versions 784 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23). 785 Support for this will be withdrawn in future releases. */ 786 int dig; 787 if (!silent) 788 { 789 static bool warned_once; 790 if (! warned_once) 791 { 792 warned_once = true; 793 WARN ((0, 0, _("Archive contains obsolescent base-64 headers"))); 794 } 795 } 796 negative = *where++ == '-'; 797 while (where != lim 798 && (dig = base64_map[(unsigned char) *where]) < 64) 799 { 800 if (value << LG_64 >> LG_64 != value) 801 { 802 char *string = alloca (digs + 1); 803 memcpy (string, where0, digs); 804 string[digs] = '\0'; 805 if (type && !silent) 806 ERROR ((0, 0, 807 _("Archive signed base-64 string %s is out of %s range"), 808 quote (string), type)); 809 return -1; 810 } 811 value = (value << LG_64) | dig; 812 where++; 813 } 814 } 815 else if (*where == '\200' /* positive base-256 */ 816 || *where == '\377' /* negative base-256 */) 817 { 818 /* Parse base-256 output. A nonnegative number N is 819 represented as (256**DIGS)/2 + N; a negative number -N is 820 represented as (256**DIGS) - N, i.e. as two's complement. 821 The representation guarantees that the leading bit is 822 always on, so that we don't confuse this format with the 823 others (assuming ASCII bytes of 8 bits or more). */ 824 int signbit = *where & (1 << (LG_256 - 2)); 825 uintmax_t topbits = (((uintmax_t) - signbit) 826 << (CHAR_BIT * sizeof (uintmax_t) 827 - LG_256 - (LG_256 - 2))); 828 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit; 829 for (;;) 830 { 831 value = (value << LG_256) + (unsigned char) *where++; 832 if (where == lim) 833 break; 834 if (((value << LG_256 >> LG_256) | topbits) != value) 835 { 836 if (type && !silent) 837 ERROR ((0, 0, 838 _("Archive base-256 value is out of %s range"), 839 type)); 840 return -1; 841 } 842 } 843 negative = signbit; 844 if (negative) 845 value = -value; 846 } 847 848 if (where != lim && *where && !ISSPACE ((unsigned char) *where)) 849 { 850 if (type) 851 { 852 char buf[1000]; /* Big enough to represent any header. */ 853 static struct quoting_options *o; 854 855 if (!o) 856 { 857 o = clone_quoting_options (0); 858 set_quoting_style (o, locale_quoting_style); 859 } 860 861 while (where0 != lim && ! lim[-1]) 862 lim--; 863 quotearg_buffer (buf, sizeof buf, where0, lim - where, o); 864 if (!silent) 865 ERROR ((0, 0, 866 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */ 867 _("Archive contains %.*s where numeric %s value expected"), 868 (int) sizeof buf, buf, type)); 869 } 870 871 return -1; 872 } 873 874 if (value <= (negative ? minus_minval : maxval)) 875 return negative ? -value : value; 876 877 if (type && !silent) 878 { 879 char minval_buf[UINTMAX_STRSIZE_BOUND + 1]; 880 char maxval_buf[UINTMAX_STRSIZE_BOUND]; 881 char value_buf[UINTMAX_STRSIZE_BOUND + 1]; 882 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1); 883 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1); 884 if (negative) 885 *--value_string = '-'; 886 if (minus_minval) 887 *--minval_string = '-'; 888 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */ 889 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"), 890 value_string, type, 891 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf))); 892 } 893 894 return -1; 895} 896 897gid_t 898gid_from_header (const char *p, size_t s) 899{ 900 return from_header (p, s, "gid_t", 901 - (uintmax_t) TYPE_MINIMUM (gid_t), 902 (uintmax_t) TYPE_MAXIMUM (gid_t), 903 false, false); 904} 905 906major_t 907major_from_header (const char *p, size_t s) 908{ 909 return from_header (p, s, "major_t", 910 - (uintmax_t) TYPE_MINIMUM (major_t), 911 (uintmax_t) TYPE_MAXIMUM (major_t), false, false); 912} 913 914minor_t 915minor_from_header (const char *p, size_t s) 916{ 917 return from_header (p, s, "minor_t", 918 - (uintmax_t) TYPE_MINIMUM (minor_t), 919 (uintmax_t) TYPE_MAXIMUM (minor_t), false, false); 920} 921 922mode_t 923mode_from_header (const char *p, size_t s) 924{ 925 /* Do not complain about unrecognized mode bits. */ 926 unsigned u = from_header (p, s, "mode_t", 927 - (uintmax_t) TYPE_MINIMUM (mode_t), 928 TYPE_MAXIMUM (uintmax_t), false, false); 929 return ((u & TSUID ? S_ISUID : 0) 930 | (u & TSGID ? S_ISGID : 0) 931 | (u & TSVTX ? S_ISVTX : 0) 932 | (u & TUREAD ? S_IRUSR : 0) 933 | (u & TUWRITE ? S_IWUSR : 0) 934 | (u & TUEXEC ? S_IXUSR : 0) 935 | (u & TGREAD ? S_IRGRP : 0) 936 | (u & TGWRITE ? S_IWGRP : 0) 937 | (u & TGEXEC ? S_IXGRP : 0) 938 | (u & TOREAD ? S_IROTH : 0) 939 | (u & TOWRITE ? S_IWOTH : 0) 940 | (u & TOEXEC ? S_IXOTH : 0)); 941} 942 943off_t 944off_from_header (const char *p, size_t s) 945{ 946 /* Negative offsets are not allowed in tar files, so invoke 947 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */ 948 return from_header (p, s, "off_t", (uintmax_t) 0, 949 (uintmax_t) TYPE_MAXIMUM (off_t), false, false); 950} 951 952size_t 953size_from_header (const char *p, size_t s) 954{ 955 return from_header (p, s, "size_t", (uintmax_t) 0, 956 (uintmax_t) TYPE_MAXIMUM (size_t), false, false); 957} 958 959time_t 960time_from_header (const char *p, size_t s) 961{ 962 return from_header (p, s, "time_t", 963 - (uintmax_t) TYPE_MINIMUM (time_t), 964 (uintmax_t) TYPE_MAXIMUM (time_t), false, false); 965} 966 967uid_t 968uid_from_header (const char *p, size_t s) 969{ 970 return from_header (p, s, "uid_t", 971 - (uintmax_t) TYPE_MINIMUM (uid_t), 972 (uintmax_t) TYPE_MAXIMUM (uid_t), false, false); 973} 974 975uintmax_t 976uintmax_from_header (const char *p, size_t s) 977{ 978 return from_header (p, s, "uintmax_t", (uintmax_t) 0, 979 TYPE_MAXIMUM (uintmax_t), false, false); 980} 981 982 983/* Return a printable representation of T. The result points to 984 static storage that can be reused in the next call to this 985 function, to ctime, or to asctime. If FULL_TIME, then output the 986 time stamp to its full resolution; otherwise, just output it to 987 1-minute resolution. */ 988char const * 989tartime (struct timespec t, bool full_time) 990{ 991 enum { fraclen = sizeof ".FFFFFFFFF" - 1 }; 992 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1, 993 INT_STRLEN_BOUND (int) + 16) 994 + fraclen]; 995 struct tm *tm; 996 time_t s = t.tv_sec; 997 int ns = t.tv_nsec; 998 bool negative = s < 0; 999 char *p; 1000 1001 if (negative && ns != 0) 1002 { 1003 s++; 1004 ns = 1000000000 - ns; 1005 } 1006 1007 tm = utc_option ? gmtime (&s) : localtime (&s); 1008 if (tm) 1009 { 1010 if (full_time) 1011 { 1012 sprintf (buffer, "%04ld-%02d-%02d %02d:%02d:%02d", 1013 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday, 1014 tm->tm_hour, tm->tm_min, tm->tm_sec); 1015 code_ns_fraction (ns, buffer + strlen (buffer)); 1016 } 1017 else 1018 sprintf (buffer, "%04ld-%02d-%02d %02d:%02d", 1019 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday, 1020 tm->tm_hour, tm->tm_min); 1021 return buffer; 1022 } 1023 1024 /* The time stamp cannot be broken down, most likely because it 1025 is out of range. Convert it as an integer, 1026 right-adjusted in a field with the same width as the usual 1027 4-year ISO time format. */ 1028 p = umaxtostr (negative ? - (uintmax_t) s : s, 1029 buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen); 1030 if (negative) 1031 *--p = '-'; 1032 while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM" 1033 + (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0)) 1034 < p) 1035 *--p = ' '; 1036 if (full_time) 1037 code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen); 1038 return p; 1039} 1040 1041/* Actually print it. 1042 1043 Plain and fancy file header block logging. Non-verbose just prints 1044 the name, e.g. for "tar t" or "tar x". This should just contain 1045 file names, so it can be fed back into tar with xargs or the "-T" 1046 option. The verbose option can give a bunch of info, one line per 1047 file. I doubt anybody tries to parse its format, or if they do, 1048 they shouldn't. Unix tar is pretty random here anyway. */ 1049 1050 1051/* FIXME: Note that print_header uses the globals HEAD, HSTAT, and 1052 HEAD_STANDARD, which must be set up in advance. Not very clean.. */ 1053 1054/* Width of "user/group size", with initial value chosen 1055 heuristically. This grows as needed, though this may cause some 1056 stairstepping in the output. Make it too small and the output will 1057 almost always look ragged. Make it too large and the output will 1058 be spaced out too far. */ 1059static int ugswidth = 19; 1060 1061/* Width of printed time stamps. It grows if longer time stamps are 1062 found (typically, those with nanosecond resolution). Like 1063 USGWIDTH, some stairstepping may occur. */ 1064static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1; 1065 1066void 1067print_header (struct tar_stat_info *st, off_t block_ordinal) 1068{ 1069 char modes[11]; 1070 char const *time_stamp; 1071 int time_stamp_len; 1072 char *temp_name; 1073 1074 /* These hold formatted ints. */ 1075 char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND]; 1076 char *user, *group; 1077 char size[2 * UINTMAX_STRSIZE_BOUND]; 1078 /* holds formatted size or major,minor */ 1079 char uintbuf[UINTMAX_STRSIZE_BOUND]; 1080 int pad; 1081 int sizelen; 1082 1083 if (test_label_option && current_header->header.typeflag != GNUTYPE_VOLHDR) 1084 return; 1085 1086 if (show_transformed_names_option) 1087 temp_name = st->file_name ? st->file_name : st->orig_file_name; 1088 else 1089 temp_name = st->orig_file_name ? st->orig_file_name : st->file_name; 1090 1091 if (block_number_option) 1092 { 1093 char buf[UINTMAX_STRSIZE_BOUND]; 1094 if (block_ordinal < 0) 1095 block_ordinal = current_block_ordinal (); 1096 block_ordinal -= recent_long_name_blocks; 1097 block_ordinal -= recent_long_link_blocks; 1098 fprintf (stdlis, _("block %s: "), 1099 STRINGIFY_BIGINT (block_ordinal, buf)); 1100 } 1101 1102 if (verbose_option <= 1) 1103 { 1104 /* Just the fax, mam. */ 1105 fprintf (stdlis, "%s\n", quotearg (temp_name)); 1106 } 1107 else 1108 { 1109 /* File type and modes. */ 1110 1111 modes[0] = '?'; 1112 switch (current_header->header.typeflag) 1113 { 1114 case GNUTYPE_VOLHDR: 1115 modes[0] = 'V'; 1116 break; 1117 1118 case GNUTYPE_MULTIVOL: 1119 modes[0] = 'M'; 1120 break; 1121 1122 case GNUTYPE_LONGNAME: 1123 case GNUTYPE_LONGLINK: 1124 modes[0] = 'L'; 1125 ERROR ((0, 0, _("Unexpected long name header"))); 1126 break; 1127 1128 case GNUTYPE_SPARSE: 1129 case REGTYPE: 1130 case AREGTYPE: 1131 modes[0] = '-'; 1132 if (temp_name[strlen (temp_name) - 1] == '/') 1133 modes[0] = 'd'; 1134 break; 1135 case LNKTYPE: 1136 modes[0] = 'h'; 1137 break; 1138 case GNUTYPE_DUMPDIR: 1139 modes[0] = 'd'; 1140 break; 1141 case DIRTYPE: 1142 modes[0] = 'd'; 1143 break; 1144 case SYMTYPE: 1145 modes[0] = 'l'; 1146 break; 1147 case BLKTYPE: 1148 modes[0] = 'b'; 1149 break; 1150 case CHRTYPE: 1151 modes[0] = 'c'; 1152 break; 1153 case FIFOTYPE: 1154 modes[0] = 'p'; 1155 break; 1156 case CONTTYPE: 1157 modes[0] = 'C'; 1158 break; 1159 } 1160 1161 pax_decode_mode (st->stat.st_mode, modes + 1); 1162 1163 /* Time stamp. */ 1164 1165 time_stamp = tartime (st->mtime, false); 1166 time_stamp_len = strlen (time_stamp); 1167 if (datewidth < time_stamp_len) 1168 datewidth = time_stamp_len; 1169 1170 /* User and group names. */ 1171 1172 if (st->uname 1173 && st->uname[0] 1174 && current_format != V7_FORMAT 1175 && !numeric_owner_option) 1176 user = st->uname; 1177 else 1178 { 1179 /* Try parsing it as an unsigned integer first, and as a 1180 uid_t if that fails. This method can list positive user 1181 ids that are too large to fit in a uid_t. */ 1182 uintmax_t u = from_header (current_header->header.uid, 1183 sizeof current_header->header.uid, 0, 1184 (uintmax_t) 0, 1185 (uintmax_t) TYPE_MAXIMUM (uintmax_t), 1186 false, false); 1187 if (u != -1) 1188 user = STRINGIFY_BIGINT (u, uform); 1189 else 1190 { 1191 sprintf (uform, "%ld", 1192 (long) UID_FROM_HEADER (current_header->header.uid)); 1193 user = uform; 1194 } 1195 } 1196 1197 if (st->gname 1198 && st->gname[0] 1199 && current_format != V7_FORMAT 1200 && !numeric_owner_option) 1201 group = st->gname; 1202 else 1203 { 1204 /* Try parsing it as an unsigned integer first, and as a 1205 gid_t if that fails. This method can list positive group 1206 ids that are too large to fit in a gid_t. */ 1207 uintmax_t g = from_header (current_header->header.gid, 1208 sizeof current_header->header.gid, 0, 1209 (uintmax_t) 0, 1210 (uintmax_t) TYPE_MAXIMUM (uintmax_t), 1211 false, false); 1212 if (g != -1) 1213 group = STRINGIFY_BIGINT (g, gform); 1214 else 1215 { 1216 sprintf (gform, "%ld", 1217 (long) GID_FROM_HEADER (current_header->header.gid)); 1218 group = gform; 1219 } 1220 } 1221 1222 /* Format the file size or major/minor device numbers. */ 1223 1224 switch (current_header->header.typeflag) 1225 { 1226 case CHRTYPE: 1227 case BLKTYPE: 1228 strcpy (size, 1229 STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf)); 1230 strcat (size, ","); 1231 strcat (size, 1232 STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf)); 1233 break; 1234 1235 default: 1236 /* st->stat.st_size keeps stored file size */ 1237 strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf)); 1238 break; 1239 } 1240 1241 /* Figure out padding and print the whole line. */ 1242 1243 sizelen = strlen (size); 1244 pad = strlen (user) + 1 + strlen (group) + 1 + sizelen; 1245 if (pad > ugswidth) 1246 ugswidth = pad; 1247 1248 fprintf (stdlis, "%s %s/%s %*s %-*s", 1249 modes, user, group, ugswidth - pad + sizelen, size, 1250 datewidth, time_stamp); 1251 1252 fprintf (stdlis, " %s", quotearg (temp_name)); 1253 1254 switch (current_header->header.typeflag) 1255 { 1256 case SYMTYPE: 1257 fprintf (stdlis, " -> %s\n", quotearg (st->link_name)); 1258 break; 1259 1260 case LNKTYPE: 1261 fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name)); 1262 break; 1263 1264 default: 1265 { 1266 char type_string[2]; 1267 type_string[0] = current_header->header.typeflag; 1268 type_string[1] = '\0'; 1269 fprintf (stdlis, _(" unknown file type %s\n"), 1270 quote (type_string)); 1271 } 1272 break; 1273 1274 case AREGTYPE: 1275 case REGTYPE: 1276 case GNUTYPE_SPARSE: 1277 case CHRTYPE: 1278 case BLKTYPE: 1279 case DIRTYPE: 1280 case FIFOTYPE: 1281 case CONTTYPE: 1282 case GNUTYPE_DUMPDIR: 1283 putc ('\n', stdlis); 1284 break; 1285 1286 case GNUTYPE_LONGLINK: 1287 fprintf (stdlis, _("--Long Link--\n")); 1288 break; 1289 1290 case GNUTYPE_LONGNAME: 1291 fprintf (stdlis, _("--Long Name--\n")); 1292 break; 1293 1294 case GNUTYPE_VOLHDR: 1295 fprintf (stdlis, _("--Volume Header--\n")); 1296 break; 1297 1298 case GNUTYPE_MULTIVOL: 1299 strcpy (size, 1300 STRINGIFY_BIGINT 1301 (UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset), 1302 uintbuf)); 1303 fprintf (stdlis, _("--Continued at byte %s--\n"), size); 1304 break; 1305 } 1306 } 1307 fflush (stdlis); 1308} 1309 1310/* Print a similar line when we make a directory automatically. */ 1311void 1312print_for_mkdir (char *dirname, int length, mode_t mode) 1313{ 1314 char modes[11]; 1315 1316 if (verbose_option > 1) 1317 { 1318 /* File type and modes. */ 1319 1320 modes[0] = 'd'; 1321 pax_decode_mode (mode, modes + 1); 1322 1323 if (block_number_option) 1324 { 1325 char buf[UINTMAX_STRSIZE_BOUND]; 1326 fprintf (stdlis, _("block %s: "), 1327 STRINGIFY_BIGINT (current_block_ordinal (), buf)); 1328 } 1329 1330 fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + 1 + datewidth, 1331 _("Creating directory:"), length, quotearg (dirname)); 1332 } 1333} 1334 1335/* Skip over SIZE bytes of data in blocks in the archive. */ 1336void 1337skip_file (off_t size) 1338{ 1339 union block *x; 1340 1341 /* FIXME: Make sure mv_begin is always called before it */ 1342 1343 if (seekable_archive) 1344 { 1345 off_t nblk = seek_archive (size); 1346 if (nblk >= 0) 1347 size -= nblk * BLOCKSIZE; 1348 else 1349 seekable_archive = false; 1350 } 1351 1352 mv_size_left (size); 1353 1354 while (size > 0) 1355 { 1356 x = find_next_block (); 1357 if (! x) 1358 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive"))); 1359 1360 set_next_block_after (x); 1361 size -= BLOCKSIZE; 1362 mv_size_left (size); 1363 } 1364} 1365 1366/* Skip the current member in the archive. 1367 NOTE: Current header must be decoded before calling this function. */ 1368void 1369skip_member (void) 1370{ 1371 if (!current_stat_info.skipped) 1372 { 1373 char save_typeflag = current_header->header.typeflag; 1374 set_next_block_after (current_header); 1375 1376 mv_begin (¤t_stat_info); 1377 1378 if (current_stat_info.is_sparse) 1379 sparse_skip_file (¤t_stat_info); 1380 else if (save_typeflag != DIRTYPE) 1381 skip_file (current_stat_info.stat.st_size); 1382 1383 mv_end (); 1384 } 1385} 1386