1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright 1988, 1992-1994, 1996-2001, 2003-2007, 2010, 2012-2016 Free
4 Software Foundation, Inc.
6 This file is part of GNU tar.
8 GNU tar is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GNU tar is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 Written by John Gilmore, on 1985-08-26. */
29 union block *current_header; /* points to current archive header */
30 enum archive_format current_format; /* recognized format */
31 union block *recent_long_name; /* recent long name header and contents */
32 union block *recent_long_link; /* likewise, for long link */
33 size_t recent_long_name_blocks; /* number of blocks in recent_long_name */
34 size_t recent_long_link_blocks; /* likewise, for long link */
35 static union block *recent_global_header; /* Recent global header block */
37 #define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where))
38 #define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where))
39 #define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where))
40 #define MODE_FROM_HEADER(where, hbits) \
41 mode_from_header (where, sizeof (where), hbits)
42 #define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where))
43 #define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where))
45 static gid_t gid_from_header (const char *buf, size_t size);
46 static major_t major_from_header (const char *buf, size_t size);
47 static minor_t minor_from_header (const char *buf, size_t size);
48 static mode_t mode_from_header (const char *buf, size_t size, bool *hbits);
49 static time_t time_from_header (const char *buf, size_t size);
50 static uid_t uid_from_header (const char *buf, size_t size);
51 static intmax_t from_header (const char *, size_t, const char *,
52 intmax_t, uintmax_t, bool, bool);
54 /* Base 64 digits; see Internet RFC 2045 Table 1. */
55 static char const base_64_digits[64] =
57 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
58 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
59 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
60 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
61 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
64 /* Table of base-64 digit values indexed by unsigned chars.
65 The value is 64 for unsigned chars that are not base-64 digits. */
66 static char base64_map[UCHAR_MAX + 1];
72 memset (base64_map, 64, sizeof base64_map);
73 for (i = 0; i < 64; i++)
74 base64_map[(int) base_64_digits[i]] = i;
78 decode_xform (char *file_name, void *data)
80 int type = *(int*)data;
85 /* FIXME: It is not quite clear how and to which extent are the symbolic
86 links subject to filename transformation. In the absence of another
87 solution, symbolic links are exempt from component stripping and
88 name suffix normalization, but subject to filename transformation
93 file_name = safer_name_suffix (file_name, true, absolute_names_option);
97 file_name = safer_name_suffix (file_name, false, absolute_names_option);
101 if (strip_name_components)
103 size_t prefix_len = stripped_prefix_len (file_name,
104 strip_name_components);
105 if (prefix_len == (size_t) -1)
106 prefix_len = strlen (file_name);
107 file_name += prefix_len;
113 transform_member_name (char **pinput, int type)
115 return transform_name_fp (pinput, type, decode_xform, &type);
119 enforce_one_top_level (char **pfile_name)
121 char *file_name = *pfile_name;
124 for (p = file_name; *p && (ISSLASH (*p) || *p == '.'); p++)
129 int pos = strlen (one_top_level_dir);
130 if (strncmp (p, one_top_level_dir, pos) == 0)
132 if (ISSLASH (p[pos]) || p[pos] == 0)
136 *pfile_name = make_file_name (one_top_level_dir, file_name);
137 normalize_filename_x (*pfile_name);
140 *pfile_name = xstrdup (one_top_level_dir);
145 transform_stat_info (int typeflag, struct tar_stat_info *stat_info)
147 if (typeflag == GNUTYPE_VOLHDR)
148 /* Name transformations don't apply to volume headers. */
151 transform_member_name (&stat_info->file_name, XFORM_REGFILE);
155 transform_member_name (&stat_info->link_name, XFORM_SYMLINK);
159 transform_member_name (&stat_info->link_name, XFORM_LINK);
162 if (one_top_level_option)
163 enforce_one_top_level (¤t_stat_info.file_name);
166 /* Main loop for reading an archive. */
168 read_and (void (*do_something) (void))
170 enum read_header status = HEADER_STILL_UNREAD;
171 enum read_header prev_status;
172 struct timespec mtime;
177 open_archive (ACCESS_READ);
180 prev_status = status;
181 tar_stat_destroy (¤t_stat_info);
183 status = read_header (¤t_header, ¤t_stat_info,
187 case HEADER_STILL_UNREAD:
188 case HEADER_SUCCESS_EXTENDED:
193 /* Valid header. We should decode next field (mode) first.
194 Ensure incoming names are null terminated. */
195 decode_header (current_header, ¤t_stat_info,
197 if (! name_match (current_stat_info.file_name)
198 || (TIME_OPTION_INITIALIZED (newer_mtime_option)
199 /* FIXME: We get mtime now, and again later; this causes
200 duplicate diagnostics if header.mtime is bogus. */
202 = TIME_FROM_HEADER (current_header->header.mtime)),
203 /* FIXME: Grab fractional time stamps from
206 current_stat_info.mtime = mtime,
207 OLDER_TAR_STAT_TIME (current_stat_info, m)))
208 || excluded_name (current_stat_info.file_name,
209 current_stat_info.parent))
211 switch (current_header->header.typeflag)
214 case GNUTYPE_MULTIVOL:
218 if (show_omitted_dirs_option)
219 WARN ((0, 0, _("%s: Omitting"),
220 quotearg_colon (current_stat_info.file_name)));
228 transform_stat_info (current_header->header.typeflag,
233 case HEADER_ZERO_BLOCK:
234 if (block_number_option)
236 char buf[UINTMAX_STRSIZE_BOUND];
237 fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
238 STRINGIFY_BIGINT (current_block_ordinal (), buf));
241 set_next_block_after (current_header);
243 if (!ignore_zeros_option)
245 char buf[UINTMAX_STRSIZE_BOUND];
247 status = read_header (¤t_header, ¤t_stat_info,
249 if (status == HEADER_ZERO_BLOCK)
251 WARNOPT (WARN_ALONE_ZERO_BLOCK,
252 (0, 0, _("A lone zero block at %s"),
253 STRINGIFY_BIGINT (current_block_ordinal (), buf)));
256 status = prev_status;
259 case HEADER_END_OF_FILE:
260 if (block_number_option)
262 char buf[UINTMAX_STRSIZE_BOUND];
263 fprintf (stdlis, _("block %s: ** End of File **\n"),
264 STRINGIFY_BIGINT (current_block_ordinal (), buf));
269 /* If the previous header was good, tell them that we are
270 skipping bad ones. */
271 set_next_block_after (current_header);
274 case HEADER_STILL_UNREAD:
275 ERROR ((0, 0, _("This does not look like a tar archive")));
278 case HEADER_ZERO_BLOCK:
280 if (block_number_option)
282 char buf[UINTMAX_STRSIZE_BOUND];
283 off_t block_ordinal = current_block_ordinal ();
284 block_ordinal -= recent_long_name_blocks;
285 block_ordinal -= recent_long_link_blocks;
286 fprintf (stdlis, _("block %s: "),
287 STRINGIFY_BIGINT (block_ordinal, buf));
289 ERROR ((0, 0, _("Skipping to next header")));
292 case HEADER_END_OF_FILE:
294 /* We are in the middle of a cascade of errors. */
297 case HEADER_SUCCESS_EXTENDED:
304 while (!all_names_found (¤t_stat_info));
307 names_notfound (); /* print names not found */
310 /* Print a header block, based on tar options. */
314 off_t block_ordinal = current_block_ordinal ();
316 /* Print the header block. */
318 print_header (¤t_stat_info, current_header, block_ordinal);
320 if (incremental_option)
322 if (verbose_option > 2)
324 if (is_dumpdir (¤t_stat_info))
325 list_dumpdir (current_stat_info.dumpdir,
326 dumpdir_size (current_stat_info.dumpdir));
333 /* Check header checksum */
334 /* The standard BSD tar sources create the checksum by adding up the
335 bytes in the header as type char. I think the type char was unsigned
336 on the PDP-11, but it's signed on the Next and Sun. It looks like the
337 sources to BSD tar were never changed to compute the checksum
338 correctly, so both the Sun and Next add the bytes of the header as
339 signed chars. This doesn't cause a problem until you get a file with
340 a name containing characters with the high bit set. So tar_checksum
341 computes two checksums -- signed and unsigned. */
344 tar_checksum (union block *header, bool silent)
347 int unsigned_sum = 0; /* the POSIX one :-) */
348 int signed_sum = 0; /* the Sun one :-( */
354 for (i = sizeof *header; i-- != 0;)
356 unsigned_sum += (unsigned char) *p;
357 signed_sum += (signed char) (*p++);
360 if (unsigned_sum == 0)
361 return HEADER_ZERO_BLOCK;
363 /* Adjust checksum to count the "chksum" field as blanks. */
365 for (i = sizeof header->header.chksum; i-- != 0;)
367 unsigned_sum -= (unsigned char) header->header.chksum[i];
368 signed_sum -= (signed char) (header->header.chksum[i]);
370 unsigned_sum += ' ' * sizeof header->header.chksum;
371 signed_sum += ' ' * sizeof header->header.chksum;
373 parsed_sum = from_header (header->header.chksum,
374 sizeof header->header.chksum, 0,
375 0, INT_MAX, true, silent);
377 return HEADER_FAILURE;
379 recorded_sum = parsed_sum;
381 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
382 return HEADER_FAILURE;
384 return HEADER_SUCCESS;
387 /* Read a block that's supposed to be a header block. Return its
388 address in *RETURN_BLOCK, and if it is good, the file's size
389 and names (file name, link name) in *INFO.
391 Return one of enum read_header describing the status of the
394 The MODE parameter instructs read_header what to do with special
395 header blocks, i.e.: extended POSIX, GNU long name or long link,
398 read_header_auto process them automatically,
399 read_header_x_raw when a special header is read, return
400 HEADER_SUCCESS_EXTENDED without actually
401 processing the header,
402 read_header_x_global when a POSIX global header is read,
403 decode it and return HEADER_SUCCESS_EXTENDED.
405 You must always set_next_block_after(*return_block) to skip past
406 the header which this routine reads. */
409 read_header (union block **return_block, struct tar_stat_info *info,
410 enum read_header_mode mode)
413 union block *header_copy;
415 union block *data_block;
416 size_t size, written;
417 union block *next_long_name = 0;
418 union block *next_long_link = 0;
419 size_t next_long_name_blocks = 0;
420 size_t next_long_link_blocks = 0;
424 enum read_header status;
426 header = find_next_block ();
427 *return_block = header;
429 return HEADER_END_OF_FILE;
431 if ((status = tar_checksum (header, false)) != HEADER_SUCCESS)
434 /* Good block. Decode file size and return. */
436 if (header->header.typeflag == LNKTYPE)
437 info->stat.st_size = 0; /* links 0 size on tape */
440 info->stat.st_size = OFF_FROM_HEADER (header->header.size);
441 if (info->stat.st_size < 0)
442 return HEADER_FAILURE;
445 if (header->header.typeflag == GNUTYPE_LONGNAME
446 || header->header.typeflag == GNUTYPE_LONGLINK
447 || header->header.typeflag == XHDTYPE
448 || header->header.typeflag == XGLTYPE
449 || header->header.typeflag == SOLARIS_XHDTYPE)
451 if (mode == read_header_x_raw)
452 return HEADER_SUCCESS_EXTENDED;
453 else if (header->header.typeflag == GNUTYPE_LONGNAME
454 || header->header.typeflag == GNUTYPE_LONGLINK)
456 size_t name_size = info->stat.st_size;
457 size_t n = name_size % BLOCKSIZE;
458 size = name_size + BLOCKSIZE;
460 size += BLOCKSIZE - n;
462 if (name_size != info->stat.st_size || size < name_size)
465 header_copy = xmalloc (size + 1);
467 if (header->header.typeflag == GNUTYPE_LONGNAME)
469 free (next_long_name);
470 next_long_name = header_copy;
471 next_long_name_blocks = size / BLOCKSIZE;
475 free (next_long_link);
476 next_long_link = header_copy;
477 next_long_link_blocks = size / BLOCKSIZE;
480 set_next_block_after (header);
481 *header_copy = *header;
482 bp = header_copy->buffer + BLOCKSIZE;
484 for (size -= BLOCKSIZE; size > 0; size -= written)
486 data_block = find_next_block ();
489 ERROR ((0, 0, _("Unexpected EOF in archive")));
492 written = available_space_after (data_block);
496 memcpy (bp, data_block->buffer, written);
498 set_next_block_after ((union block *)
499 (data_block->buffer + written - 1));
504 else if (header->header.typeflag == XHDTYPE
505 || header->header.typeflag == SOLARIS_XHDTYPE)
506 xheader_read (&info->xhdr, header,
507 OFF_FROM_HEADER (header->header.size));
508 else if (header->header.typeflag == XGLTYPE)
512 if (!recent_global_header)
513 recent_global_header = xmalloc (sizeof *recent_global_header);
514 memcpy (recent_global_header, header,
515 sizeof *recent_global_header);
516 memset (&xhdr, 0, sizeof xhdr);
517 xheader_read (&xhdr, header,
518 OFF_FROM_HEADER (header->header.size));
519 xheader_decode_global (&xhdr);
520 xheader_destroy (&xhdr);
521 if (mode == read_header_x_global)
522 return HEADER_SUCCESS_EXTENDED;
531 struct posix_header const *h = &header->header;
532 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
534 free (recent_long_name);
538 name = next_long_name->buffer + BLOCKSIZE;
539 recent_long_name = next_long_name;
540 recent_long_name_blocks = next_long_name_blocks;
544 /* Accept file names as specified by POSIX.1-1996
548 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
550 memcpy (np, h->prefix, sizeof h->prefix);
551 np[sizeof h->prefix] = '\0';
555 memcpy (np, h->name, sizeof h->name);
556 np[sizeof h->name] = '\0';
558 recent_long_name = 0;
559 recent_long_name_blocks = 0;
561 assign_string (&info->orig_file_name, name);
562 assign_string (&info->file_name, name);
563 info->had_trailing_slash = strip_trailing_slashes (info->file_name);
565 free (recent_long_link);
569 name = next_long_link->buffer + BLOCKSIZE;
570 recent_long_link = next_long_link;
571 recent_long_link_blocks = next_long_link_blocks;
575 memcpy (namebuf, h->linkname, sizeof h->linkname);
576 namebuf[sizeof h->linkname] = '\0';
578 recent_long_link = 0;
579 recent_long_link_blocks = 0;
581 assign_string (&info->link_name, name);
583 return HEADER_SUCCESS;
588 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
590 /* Decode things from a file HEADER block into STAT_INFO, also setting
591 *FORMAT_POINTER depending on the header block format. If
592 DO_USER_GROUP, decode the user/group information (this is useful
593 for extraction, but waste time when merely listing).
595 read_header() has already decoded the checksum and length, so we don't.
597 This routine should *not* be called twice for the same block, since
598 the two calls might use different DO_USER_GROUP values and thus
599 might end up with different uid/gid for the two calls. If anybody
600 wants the uid/gid they should decode it first, and other callers
601 should decode it without uid/gid before calling a routine,
602 e.g. print_header, that assumes decoded data. */
604 decode_header (union block *header, struct tar_stat_info *stat_info,
605 enum archive_format *format_pointer, int do_user_group)
607 enum archive_format format;
609 mode_t mode = MODE_FROM_HEADER (header->header.mode, &hbits);
611 if (strcmp (header->header.magic, TMAGIC) == 0)
613 if (header->star_header.prefix[130] == 0
614 && ISOCTAL (header->star_header.atime[0])
615 && header->star_header.atime[11] == ' '
616 && ISOCTAL (header->star_header.ctime[0])
617 && header->star_header.ctime[11] == ' ')
618 format = STAR_FORMAT;
619 else if (stat_info->xhdr.size)
620 format = POSIX_FORMAT;
622 format = USTAR_FORMAT;
624 else if (strcmp (header->buffer + offsetof (struct posix_header, magic),
627 format = hbits ? OLDGNU_FORMAT : GNU_FORMAT;
630 *format_pointer = format;
632 stat_info->stat.st_mode = mode;
633 stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime);
634 stat_info->mtime.tv_nsec = 0;
635 assign_string (&stat_info->uname,
636 header->header.uname[0] ? header->header.uname : NULL);
637 assign_string (&stat_info->gname,
638 header->header.gname[0] ? header->header.gname : NULL);
640 xheader_xattr_init (stat_info);
642 if (format == OLDGNU_FORMAT && incremental_option)
644 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime);
645 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime);
646 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
648 else if (format == STAR_FORMAT)
650 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime);
651 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime);
652 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
655 stat_info->atime = stat_info->ctime = start_time;
657 if (format == V7_FORMAT)
659 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
660 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
661 stat_info->stat.st_rdev = 0;
667 /* FIXME: Decide if this should somewhat depend on -p. */
669 if (numeric_owner_option
670 || !*header->header.uname
671 || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid))
672 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
674 if (numeric_owner_option
675 || !*header->header.gname
676 || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid))
677 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
680 switch (header->header.typeflag)
684 stat_info->stat.st_rdev =
685 makedev (MAJOR_FROM_HEADER (header->header.devmajor),
686 MINOR_FROM_HEADER (header->header.devminor));
690 stat_info->stat.st_rdev = 0;
694 xheader_decode (stat_info);
696 if (sparse_member_p (stat_info))
698 sparse_fixup_header (stat_info);
699 stat_info->is_sparse = true;
703 stat_info->is_sparse = false;
704 if (((current_format == GNU_FORMAT
705 || current_format == OLDGNU_FORMAT)
706 && current_header->header.typeflag == GNUTYPE_DUMPDIR)
707 || stat_info->dumpdir)
708 stat_info->is_dumpdir = true;
713 /* Convert buffer at WHERE0 of size DIGS from external format to
714 intmax_t. DIGS must be positive. If TYPE is nonnull, the data are
715 of type TYPE. The buffer must represent a value in the range
716 MINVAL through MAXVAL; if the mathematically correct result V would
717 be greater than INTMAX_MAX, return a negative integer V such that
718 (uintmax_t) V yields the correct result. If OCTAL_ONLY, allow only octal
719 numbers instead of the other GNU extensions. Return -1 on error,
720 diagnosing the error if TYPE is nonnull and if !SILENT. */
721 #if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX)
722 # error "from_header internally represents intmax_t as uintmax_t + sign"
724 #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
725 # error "from_header returns intmax_t to represent uintmax_t"
728 from_header (char const *where0, size_t digs, char const *type,
729 intmax_t minval, uintmax_t maxval,
730 bool octal_only, bool silent)
733 uintmax_t uminval = minval;
734 uintmax_t minus_minval = - uminval;
735 char const *where = where0;
736 char const *lim = where + digs;
737 bool negative = false;
739 /* Accommodate buggy tar of unknown vintage, which outputs leading
740 NUL if the previous field overflows. */
743 /* Accommodate older tars, which output leading spaces. */
750 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
752 _("Blanks in header where numeric %s value expected"),
756 if (!isspace ((unsigned char) *where))
762 if (ISODIGIT (*where))
764 char const *where1 = where;
765 bool overflow = false;
769 value += *where++ - '0';
770 if (where == lim || ! ISODIGIT (*where))
772 overflow |= value != (value << LG_8 >> LG_8);
776 /* Parse the output of older, unportable tars, which generate
777 negative values in two's complement octal. If the leading
778 nonzero digit is 1, we can't recover the original value
779 reliably; so do this only if the digit is 2 or more. This
780 catches the common case of 32-bit negative time stamps. */
781 if ((overflow || maxval < value) && '2' <= *where1 && type)
783 /* Compute the negative of the input value, assuming two's
785 int digit = (*where1 - '0') | 4;
793 if (where == lim || ! ISODIGIT (*where))
795 digit = *where - '0';
796 overflow |= value != (value << LG_8 >> LG_8);
802 if (!overflow && value <= minus_minval)
806 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
807 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
808 (int) (where - where1), where1, type));
817 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
818 _("Archive octal value %.*s is out of %s range"),
819 (int) (where - where1), where1, type));
825 /* Suppress the following extensions. */
827 else if (*where == '-' || *where == '+')
829 /* Parse base-64 output produced only by tar test versions
830 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
831 Support for this will be withdrawn in future releases. */
835 static bool warned_once;
839 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
842 negative = *where++ == '-';
844 && (dig = base64_map[(unsigned char) *where]) < 64)
846 if (value << LG_64 >> LG_64 != value)
848 char *string = alloca (digs + 1);
849 memcpy (string, where0, digs);
853 _("Archive signed base-64 string %s is out of %s range"),
854 quote (string), type));
857 value = (value << LG_64) | dig;
861 else if (*where == '\200' /* positive base-256 */
862 || *where == '\377' /* negative base-256 */)
864 /* Parse base-256 output. A nonnegative number N is
865 represented as (256**DIGS)/2 + N; a negative number -N is
866 represented as (256**DIGS) - N, i.e. as two's complement.
867 The representation guarantees that the leading bit is
868 always on, so that we don't confuse this format with the
869 others (assuming ASCII bytes of 8 bits or more). */
870 int signbit = *where & (1 << (LG_256 - 2));
871 uintmax_t topbits = (((uintmax_t) - signbit)
872 << (CHAR_BIT * sizeof (uintmax_t)
873 - LG_256 - (LG_256 - 2)));
874 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
877 value = (value << LG_256) + (unsigned char) *where++;
880 if (((value << LG_256 >> LG_256) | topbits) != value)
884 _("Archive base-256 value is out of %s range"),
889 negative = signbit != 0;
894 if (where != lim && *where && !isspace ((unsigned char) *where))
898 char buf[1000]; /* Big enough to represent any header. */
899 static struct quoting_options *o;
903 o = clone_quoting_options (0);
904 set_quoting_style (o, locale_quoting_style);
907 while (where0 != lim && ! lim[-1])
909 quotearg_buffer (buf, sizeof buf, where0, lim - where0, o);
912 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
913 _("Archive contains %.*s where numeric %s value expected"),
914 (int) sizeof buf, buf, type));
920 if (value <= (negative ? minus_minval : maxval))
921 return represent_uintmax (negative ? -value : value);
925 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
926 char maxval_buf[UINTMAX_STRSIZE_BOUND];
927 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
928 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
929 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
931 *--value_string = '-';
933 *--minval_string = '-';
934 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
935 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
937 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
944 gid_from_header (const char *p, size_t s)
946 return from_header (p, s, "gid_t",
947 TYPE_MINIMUM (gid_t), TYPE_MAXIMUM (gid_t),
952 major_from_header (const char *p, size_t s)
954 return from_header (p, s, "major_t",
955 TYPE_MINIMUM (major_t), TYPE_MAXIMUM (major_t),
960 minor_from_header (const char *p, size_t s)
962 return from_header (p, s, "minor_t",
963 TYPE_MINIMUM (minor_t), TYPE_MAXIMUM (minor_t),
967 /* Convert P to the file mode, as understood by tar.
968 Set *HBITS if there are any unrecognized bits. */
970 mode_from_header (const char *p, size_t s, bool *hbits)
972 intmax_t u = from_header (p, s, "mode_t",
973 INTMAX_MIN, UINTMAX_MAX,
975 mode_t mode = ((u & TSUID ? S_ISUID : 0)
976 | (u & TSGID ? S_ISGID : 0)
977 | (u & TSVTX ? S_ISVTX : 0)
978 | (u & TUREAD ? S_IRUSR : 0)
979 | (u & TUWRITE ? S_IWUSR : 0)
980 | (u & TUEXEC ? S_IXUSR : 0)
981 | (u & TGREAD ? S_IRGRP : 0)
982 | (u & TGWRITE ? S_IWGRP : 0)
983 | (u & TGEXEC ? S_IXGRP : 0)
984 | (u & TOREAD ? S_IROTH : 0)
985 | (u & TOWRITE ? S_IWOTH : 0)
986 | (u & TOEXEC ? S_IXOTH : 0));
987 *hbits = (u & ~07777) != 0;
992 off_from_header (const char *p, size_t s)
994 /* Negative offsets are not allowed in tar files, so invoke
995 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
996 return from_header (p, s, "off_t",
997 0, TYPE_MAXIMUM (off_t),
1002 time_from_header (const char *p, size_t s)
1004 return from_header (p, s, "time_t",
1005 TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t),
1010 uid_from_header (const char *p, size_t s)
1012 return from_header (p, s, "uid_t",
1013 TYPE_MINIMUM (uid_t), TYPE_MAXIMUM (uid_t),
1018 uintmax_from_header (const char *p, size_t s)
1020 return from_header (p, s, "uintmax_t", 0, UINTMAX_MAX, false, false);
1024 /* Return a printable representation of T. The result points to
1025 static storage that can be reused in the next call to this
1026 function, to ctime, or to asctime. If FULL_TIME, then output the
1027 time stamp to its full resolution; otherwise, just output it to
1028 1-minute resolution. */
1030 tartime (struct timespec t, bool full_time)
1032 enum { fraclen = sizeof ".FFFFFFFFF" - 1 };
1033 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
1034 INT_STRLEN_BOUND (int) + 16)
1037 time_t s = t.tv_sec;
1039 bool negative = s < 0;
1042 if (negative && ns != 0)
1045 ns = 1000000000 - ns;
1048 tm = utc_option ? gmtime (&s) : localtime (&s);
1053 sprintf (buffer, "%04ld-%02d-%02d %02d:%02d:%02d",
1054 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
1055 tm->tm_hour, tm->tm_min, tm->tm_sec);
1056 code_ns_fraction (ns, buffer + strlen (buffer));
1059 sprintf (buffer, "%04ld-%02d-%02d %02d:%02d",
1060 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
1061 tm->tm_hour, tm->tm_min);
1065 /* The time stamp cannot be broken down, most likely because it
1066 is out of range. Convert it as an integer,
1067 right-adjusted in a field with the same width as the usual
1068 4-year ISO time format. */
1069 p = umaxtostr (negative ? - (uintmax_t) s : s,
1070 buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen);
1073 while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM"
1074 + (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1078 code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen);
1082 /* Actually print it.
1084 Plain and fancy file header block logging. Non-verbose just prints
1085 the name, e.g. for "tar t" or "tar x". This should just contain
1086 file names, so it can be fed back into tar with xargs or the "-T"
1087 option. The verbose option can give a bunch of info, one line per
1088 file. I doubt anybody tries to parse its format, or if they do,
1089 they shouldn't. Unix tar is pretty random here anyway. */
1092 /* Width of "user/group size", with initial value chosen
1093 heuristically. This grows as needed, though this may cause some
1094 stairstepping in the output. Make it too small and the output will
1095 almost always look ragged. Make it too large and the output will
1096 be spaced out too far. */
1097 static int ugswidth = 19;
1099 /* Width of printed time stamps. It grows if longer time stamps are
1100 found (typically, those with nanosecond resolution). Like
1101 USGWIDTH, some stairstepping may occur. */
1102 static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1;
1104 static bool volume_label_printed = false;
1107 simple_print_header (struct tar_stat_info *st, union block *blk,
1108 off_t block_ordinal)
1111 char const *time_stamp;
1115 /* These hold formatted ints. */
1116 char uform[max (INT_BUFSIZE_BOUND (intmax_t), UINTMAX_STRSIZE_BOUND)];
1117 char gform[sizeof uform];
1119 char size[2 * UINTMAX_STRSIZE_BOUND];
1120 /* holds formatted size or major,minor */
1121 char uintbuf[UINTMAX_STRSIZE_BOUND];
1125 if (show_transformed_names_option)
1126 temp_name = st->file_name ? st->file_name : st->orig_file_name;
1128 temp_name = st->orig_file_name ? st->orig_file_name : st->file_name;
1130 if (block_number_option)
1132 char buf[UINTMAX_STRSIZE_BOUND];
1133 if (block_ordinal < 0)
1134 block_ordinal = current_block_ordinal ();
1135 block_ordinal -= recent_long_name_blocks;
1136 block_ordinal -= recent_long_link_blocks;
1137 fprintf (stdlis, _("block %s: "),
1138 STRINGIFY_BIGINT (block_ordinal, buf));
1141 if (verbose_option <= 1)
1143 /* Just the fax, mam. */
1144 fputs (quotearg (temp_name), stdlis);
1145 if (show_transformed_names_option && st->had_trailing_slash)
1146 fputc ('/', stdlis);
1147 fputc ('\n', stdlis);
1151 /* File type and modes. */
1154 switch (blk->header.typeflag)
1156 case GNUTYPE_VOLHDR:
1157 volume_label_printed = true;
1161 case GNUTYPE_MULTIVOL:
1165 case GNUTYPE_LONGNAME:
1166 case GNUTYPE_LONGLINK:
1168 ERROR ((0, 0, _("Unexpected long name header")));
1171 case GNUTYPE_SPARSE:
1174 modes[0] = st->had_trailing_slash ? 'd' : '-';
1179 case GNUTYPE_DUMPDIR:
1202 pax_decode_mode (st->stat.st_mode, modes + 1);
1204 /* extended attributes: GNU `ls -l'-like preview */
1205 xattrs_print_char (st, modes + 10);
1209 time_stamp = tartime (st->mtime, full_time_option);
1210 time_stamp_len = strlen (time_stamp);
1211 if (datewidth < time_stamp_len)
1212 datewidth = time_stamp_len;
1214 /* User and group names. */
1218 && current_format != V7_FORMAT
1219 && !numeric_owner_option)
1222 user = STRINGIFY_BIGINT (st->stat.st_uid, uform);
1226 && current_format != V7_FORMAT
1227 && !numeric_owner_option)
1230 group = STRINGIFY_BIGINT (st->stat.st_gid, gform);
1232 /* Format the file size or major/minor device numbers. */
1234 switch (blk->header.typeflag)
1239 STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf));
1242 STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf));
1246 /* st->stat.st_size keeps stored file size */
1247 strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf));
1251 /* Figure out padding and print the whole line. */
1253 sizelen = strlen (size);
1254 pad = strlen (user) + 1 + strlen (group) + 1 + sizelen;
1258 fprintf (stdlis, "%s %s/%s %*s %-*s",
1259 modes, user, group, ugswidth - pad + sizelen, size,
1260 datewidth, time_stamp);
1262 fprintf (stdlis, " %s", quotearg (temp_name));
1263 if (show_transformed_names_option && st->had_trailing_slash)
1264 fputc ('/', stdlis);
1266 switch (blk->header.typeflag)
1269 fprintf (stdlis, " -> %s\n", quotearg (st->link_name));
1273 fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name));
1278 char type_string[2];
1279 type_string[0] = blk->header.typeflag;
1280 type_string[1] = '\0';
1281 fprintf (stdlis, _(" unknown file type %s\n"),
1282 quote (type_string));
1288 case GNUTYPE_SPARSE:
1294 case GNUTYPE_DUMPDIR:
1295 putc ('\n', stdlis);
1298 case GNUTYPE_LONGLINK:
1299 fprintf (stdlis, _("--Long Link--\n"));
1302 case GNUTYPE_LONGNAME:
1303 fprintf (stdlis, _("--Long Name--\n"));
1306 case GNUTYPE_VOLHDR:
1307 fprintf (stdlis, _("--Volume Header--\n"));
1310 case GNUTYPE_MULTIVOL:
1313 (UINTMAX_FROM_HEADER (blk->oldgnu_header.offset),
1315 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1325 print_volume_label (void)
1327 struct tar_stat_info vstat;
1329 enum archive_format dummy;
1331 memset (&vblk, 0, sizeof (vblk));
1332 vblk.header.typeflag = GNUTYPE_VOLHDR;
1333 if (recent_global_header)
1334 memcpy (vblk.header.mtime, recent_global_header->header.mtime,
1335 sizeof vblk.header.mtime);
1336 tar_stat_init (&vstat);
1337 assign_string (&vstat.file_name, ".");
1338 decode_header (&vblk, &vstat, &dummy, 0);
1339 assign_string (&vstat.file_name, volume_label);
1340 simple_print_header (&vstat, &vblk, 0);
1341 tar_stat_destroy (&vstat);
1345 print_header (struct tar_stat_info *st, union block *blk,
1346 off_t block_ordinal)
1348 if (current_format == POSIX_FORMAT && !volume_label_printed && volume_label)
1350 print_volume_label ();
1351 volume_label_printed = true;
1354 simple_print_header (st, blk, block_ordinal);
1357 /* Print a similar line when we make a directory automatically. */
1359 print_for_mkdir (char *dirname, int length, mode_t mode)
1363 if (verbose_option > 1)
1365 /* File type and modes. */
1368 pax_decode_mode (mode, modes + 1);
1370 if (block_number_option)
1372 char buf[UINTMAX_STRSIZE_BOUND];
1373 fprintf (stdlis, _("block %s: "),
1374 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1377 fprintf (stdlis, "%s %*s %s\n", modes, ugswidth + 1 + datewidth,
1378 _("Creating directory:"), quotearg (dirname));
1382 /* Skip over SIZE bytes of data in blocks in the archive. */
1384 skip_file (off_t size)
1388 /* FIXME: Make sure mv_begin_read is always called before it */
1390 if (seekable_archive)
1392 off_t nblk = seek_archive (size);
1394 size -= nblk * BLOCKSIZE;
1396 seekable_archive = false;
1399 mv_size_left (size);
1403 x = find_next_block ();
1405 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1407 set_next_block_after (x);
1409 mv_size_left (size);
1413 /* Skip the current member in the archive.
1414 NOTE: Current header must be decoded before calling this function. */
1418 if (!current_stat_info.skipped)
1420 char save_typeflag = current_header->header.typeflag;
1421 set_next_block_after (current_header);
1423 mv_begin_read (¤t_stat_info);
1425 if (current_stat_info.is_sparse)
1426 sparse_skip_file (¤t_stat_info);
1427 else if (save_typeflag != DIRTYPE)
1428 skip_file (current_stat_info.stat.st_size);
1435 test_archive_label (void)
1440 open_archive (ACCESS_READ);
1441 if (read_header (¤t_header, ¤t_stat_info, read_header_auto)
1444 decode_header (current_header,
1445 ¤t_stat_info, ¤t_format, 0);
1446 if (current_header->header.typeflag == GNUTYPE_VOLHDR)
1447 assign_string (&volume_label, current_header->header.name);
1452 print_volume_label ();
1453 if (!name_match (volume_label) && multi_volume_option)
1455 char *s = drop_volume_label_suffix (volume_label);