1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2003, 2004, 2005, 2006, 2007, 2010 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-08-26.
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 3, or (at your option) any later
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.
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. */
28 #define max(a, b) ((a) < (b) ? (b) : (a))
30 union block
*current_header
; /* points to current archive header */
31 enum archive_format current_format
; /* recognized format */
32 union block
*recent_long_name
; /* recent long name header and contents */
33 union block
*recent_long_link
; /* likewise, for long link */
34 size_t recent_long_name_blocks
; /* number of blocks in recent_long_name */
35 size_t recent_long_link_blocks
; /* likewise, for long link */
36 union block
*recent_global_header
; /* Recent global header block */
38 static uintmax_t from_header (const char *, size_t, const char *,
39 uintmax_t, uintmax_t, bool, bool);
41 /* Base 64 digits; see Internet RFC 2045 Table 1. */
42 static char const base_64_digits
[64] =
44 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
45 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
46 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
47 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
48 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
51 /* Table of base-64 digit values indexed by unsigned chars.
52 The value is 64 for unsigned chars that are not base-64 digits. */
53 static char base64_map
[UCHAR_MAX
+ 1];
59 memset (base64_map
, 64, sizeof base64_map
);
60 for (i
= 0; i
< 64; i
++)
61 base64_map
[(int) base_64_digits
[i
]] = i
;
64 /* Main loop for reading an archive. */
66 read_and (void (*do_something
) (void))
68 enum read_header status
= HEADER_STILL_UNREAD
;
69 enum read_header prev_status
;
70 struct timespec mtime
;
75 open_archive (ACCESS_READ
);
79 tar_stat_destroy (¤t_stat_info
);
81 status
= read_header (¤t_header
, ¤t_stat_info
,
85 case HEADER_STILL_UNREAD
:
86 case HEADER_SUCCESS_EXTENDED
:
91 /* Valid header. We should decode next field (mode) first.
92 Ensure incoming names are null terminated. */
94 if (! name_match (current_stat_info
.file_name
)
95 || (NEWER_OPTION_INITIALIZED (newer_mtime_option
)
96 /* FIXME: We get mtime now, and again later; this causes
97 duplicate diagnostics if header.mtime is bogus. */
99 = TIME_FROM_HEADER (current_header
->header
.mtime
)),
100 /* FIXME: Grab fractional time stamps from
103 current_stat_info
.mtime
= mtime
,
104 OLDER_TAR_STAT_TIME (current_stat_info
, m
)))
105 || excluded_name (current_stat_info
.file_name
))
107 switch (current_header
->header
.typeflag
)
110 case GNUTYPE_MULTIVOL
:
114 if (show_omitted_dirs_option
)
115 WARN ((0, 0, _("%s: Omitting"),
116 quotearg_colon (current_stat_info
.file_name
)));
119 decode_header (current_header
,
120 ¤t_stat_info
, ¤t_format
, 0);
129 case HEADER_ZERO_BLOCK
:
130 if (block_number_option
)
132 char buf
[UINTMAX_STRSIZE_BOUND
];
133 fprintf (stdlis
, _("block %s: ** Block of NULs **\n"),
134 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
137 set_next_block_after (current_header
);
139 if (!ignore_zeros_option
)
141 char buf
[UINTMAX_STRSIZE_BOUND
];
143 status
= read_header (¤t_header
, ¤t_stat_info
,
145 if (status
== HEADER_ZERO_BLOCK
)
147 WARNOPT (WARN_ALONE_ZERO_BLOCK
,
148 (0, 0, _("A lone zero block at %s"),
149 STRINGIFY_BIGINT (current_block_ordinal (), buf
)));
152 status
= prev_status
;
155 case HEADER_END_OF_FILE
:
156 if (block_number_option
)
158 char buf
[UINTMAX_STRSIZE_BOUND
];
159 fprintf (stdlis
, _("block %s: ** End of File **\n"),
160 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
165 /* If the previous header was good, tell them that we are
166 skipping bad ones. */
167 set_next_block_after (current_header
);
170 case HEADER_STILL_UNREAD
:
171 ERROR ((0, 0, _("This does not look like a tar archive")));
174 case HEADER_ZERO_BLOCK
:
176 if (block_number_option
)
178 char buf
[UINTMAX_STRSIZE_BOUND
];
179 off_t block_ordinal
= current_block_ordinal ();
180 block_ordinal
-= recent_long_name_blocks
;
181 block_ordinal
-= recent_long_link_blocks
;
182 fprintf (stdlis
, _("block %s: "),
183 STRINGIFY_BIGINT (block_ordinal
, buf
));
185 ERROR ((0, 0, _("Skipping to next header")));
188 case HEADER_END_OF_FILE
:
190 /* We are in the middle of a cascade of errors. */
193 case HEADER_SUCCESS_EXTENDED
:
200 while (!all_names_found (¤t_stat_info
));
203 names_notfound (); /* print names not found */
206 /* Print a header block, based on tar options. */
210 off_t block_ordinal
= current_block_ordinal ();
212 /* Print the header block. */
214 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 0);
216 print_header (¤t_stat_info
, current_header
, block_ordinal
);
218 if (incremental_option
)
220 if (verbose_option
> 2)
222 if (is_dumpdir (¤t_stat_info
))
223 list_dumpdir (current_stat_info
.dumpdir
,
224 dumpdir_size (current_stat_info
.dumpdir
));
231 /* Check header checksum */
232 /* The standard BSD tar sources create the checksum by adding up the
233 bytes in the header as type char. I think the type char was unsigned
234 on the PDP-11, but it's signed on the Next and Sun. It looks like the
235 sources to BSD tar were never changed to compute the checksum
236 correctly, so both the Sun and Next add the bytes of the header as
237 signed chars. This doesn't cause a problem until you get a file with
238 a name containing characters with the high bit set. So tar_checksum
239 computes two checksums -- signed and unsigned. */
242 tar_checksum (union block
*header
, bool silent
)
245 int unsigned_sum
= 0; /* the POSIX one :-) */
246 int signed_sum
= 0; /* the Sun one :-( */
248 uintmax_t parsed_sum
;
252 for (i
= sizeof *header
; i
-- != 0;)
254 unsigned_sum
+= (unsigned char) *p
;
255 signed_sum
+= (signed char) (*p
++);
258 if (unsigned_sum
== 0)
259 return HEADER_ZERO_BLOCK
;
261 /* Adjust checksum to count the "chksum" field as blanks. */
263 for (i
= sizeof header
->header
.chksum
; i
-- != 0;)
265 unsigned_sum
-= (unsigned char) header
->header
.chksum
[i
];
266 signed_sum
-= (signed char) (header
->header
.chksum
[i
]);
268 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
269 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
271 parsed_sum
= from_header (header
->header
.chksum
,
272 sizeof header
->header
.chksum
, 0,
274 (uintmax_t) TYPE_MAXIMUM (int), true, silent
);
275 if (parsed_sum
== (uintmax_t) -1)
276 return HEADER_FAILURE
;
278 recorded_sum
= parsed_sum
;
280 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
281 return HEADER_FAILURE
;
283 return HEADER_SUCCESS
;
286 /* Read a block that's supposed to be a header block. Return its
287 address in *RETURN_BLOCK, and if it is good, the file's size
288 and names (file name, link name) in *INFO.
290 Return one of enum read_header describing the status of the
293 The MODE parameter instructs read_header what to do with special
294 header blocks, i.e.: extended POSIX, GNU long name or long link,
297 read_header_auto process them automatically,
298 read_header_x_raw when a special header is read, return
299 HEADER_SUCCESS_EXTENDED without actually
300 processing the header,
301 read_header_x_global when a POSIX global header is read,
302 decode it and return HEADER_SUCCESS_EXTENDED.
304 You must always set_next_block_after(*return_block) to skip past
305 the header which this routine reads. */
308 read_header (union block
**return_block
, struct tar_stat_info
*info
,
309 enum read_header_mode mode
)
312 union block
*header_copy
;
314 union block
*data_block
;
315 size_t size
, written
;
316 union block
*next_long_name
= 0;
317 union block
*next_long_link
= 0;
318 size_t next_long_name_blocks
= 0;
319 size_t next_long_link_blocks
= 0;
323 enum read_header status
;
325 header
= find_next_block ();
326 *return_block
= header
;
328 return HEADER_END_OF_FILE
;
330 if ((status
= tar_checksum (header
, false)) != HEADER_SUCCESS
)
333 /* Good block. Decode file size and return. */
335 if (header
->header
.typeflag
== LNKTYPE
)
336 info
->stat
.st_size
= 0; /* links 0 size on tape */
338 info
->stat
.st_size
= OFF_FROM_HEADER (header
->header
.size
);
340 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
341 || header
->header
.typeflag
== GNUTYPE_LONGLINK
342 || header
->header
.typeflag
== XHDTYPE
343 || header
->header
.typeflag
== XGLTYPE
344 || header
->header
.typeflag
== SOLARIS_XHDTYPE
)
346 if (mode
== read_header_x_raw
)
347 return HEADER_SUCCESS_EXTENDED
;
348 else if (header
->header
.typeflag
== GNUTYPE_LONGNAME
349 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
351 size_t name_size
= info
->stat
.st_size
;
352 size_t n
= name_size
% BLOCKSIZE
;
353 size
= name_size
+ BLOCKSIZE
;
355 size
+= BLOCKSIZE
- n
;
357 if (name_size
!= info
->stat
.st_size
|| size
< name_size
)
360 header_copy
= xmalloc (size
+ 1);
362 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
)
365 free (next_long_name
);
366 next_long_name
= header_copy
;
367 next_long_name_blocks
= size
/ BLOCKSIZE
;
372 free (next_long_link
);
373 next_long_link
= header_copy
;
374 next_long_link_blocks
= size
/ BLOCKSIZE
;
377 set_next_block_after (header
);
378 *header_copy
= *header
;
379 bp
= header_copy
->buffer
+ BLOCKSIZE
;
381 for (size
-= BLOCKSIZE
; size
> 0; size
-= written
)
383 data_block
= find_next_block ();
386 ERROR ((0, 0, _("Unexpected EOF in archive")));
389 written
= available_space_after (data_block
);
393 memcpy (bp
, data_block
->buffer
, written
);
395 set_next_block_after ((union block
*)
396 (data_block
->buffer
+ written
- 1));
401 else if (header
->header
.typeflag
== XHDTYPE
402 || header
->header
.typeflag
== SOLARIS_XHDTYPE
)
403 xheader_read (&info
->xhdr
, header
,
404 OFF_FROM_HEADER (header
->header
.size
));
405 else if (header
->header
.typeflag
== XGLTYPE
)
409 if (!recent_global_header
)
410 recent_global_header
= xmalloc (sizeof *recent_global_header
);
411 memcpy (recent_global_header
, header
,
412 sizeof *recent_global_header
);
413 memset (&xhdr
, 0, sizeof xhdr
);
414 xheader_read (&xhdr
, header
,
415 OFF_FROM_HEADER (header
->header
.size
));
416 xheader_decode_global (&xhdr
);
417 xheader_destroy (&xhdr
);
418 if (mode
== read_header_x_global
)
419 return HEADER_SUCCESS_EXTENDED
;
428 struct posix_header
const *h
= &header
->header
;
429 char namebuf
[sizeof h
->prefix
+ 1 + NAME_FIELD_SIZE
+ 1];
431 if (recent_long_name
)
432 free (recent_long_name
);
436 name
= next_long_name
->buffer
+ BLOCKSIZE
;
437 recent_long_name
= next_long_name
;
438 recent_long_name_blocks
= next_long_name_blocks
;
442 /* Accept file names as specified by POSIX.1-1996
446 if (h
->prefix
[0] && strcmp (h
->magic
, TMAGIC
) == 0)
448 memcpy (np
, h
->prefix
, sizeof h
->prefix
);
449 np
[sizeof h
->prefix
] = '\0';
453 memcpy (np
, h
->name
, sizeof h
->name
);
454 np
[sizeof h
->name
] = '\0';
456 recent_long_name
= 0;
457 recent_long_name_blocks
= 0;
459 assign_string (&info
->orig_file_name
, name
);
460 assign_string (&info
->file_name
, name
);
461 info
->had_trailing_slash
= strip_trailing_slashes (info
->file_name
);
463 if (recent_long_link
)
464 free (recent_long_link
);
468 name
= next_long_link
->buffer
+ BLOCKSIZE
;
469 recent_long_link
= next_long_link
;
470 recent_long_link_blocks
= next_long_link_blocks
;
474 memcpy (namebuf
, h
->linkname
, sizeof h
->linkname
);
475 namebuf
[sizeof h
->linkname
] = '\0';
477 recent_long_link
= 0;
478 recent_long_link_blocks
= 0;
480 assign_string (&info
->link_name
, name
);
482 return HEADER_SUCCESS
;
488 decode_xform (char *file_name
, void *data
)
490 int type
= *(int*)data
;
495 /* FIXME: It is not quite clear how and to which extent are the symbolic
496 links subject to filename transformation. In the absence of another
497 solution, symbolic links are exempt from component stripping and
498 name suffix normalization, but subject to filename transformation
503 file_name
= safer_name_suffix (file_name
, true, absolute_names_option
);
507 file_name
= safer_name_suffix (file_name
, false, absolute_names_option
);
511 if (strip_name_components
)
513 size_t prefix_len
= stripped_prefix_len (file_name
,
514 strip_name_components
);
515 if (prefix_len
== (size_t) -1)
516 prefix_len
= strlen (file_name
);
517 file_name
+= prefix_len
;
523 transform_member_name (char **pinput
, int type
)
525 return transform_name_fp (pinput
, type
, decode_xform
, &type
);
528 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
530 /* Decode things from a file HEADER block into STAT_INFO, also setting
531 *FORMAT_POINTER depending on the header block format. If
532 DO_USER_GROUP, decode the user/group information (this is useful
533 for extraction, but waste time when merely listing).
535 read_header() has already decoded the checksum and length, so we don't.
537 This routine should *not* be called twice for the same block, since
538 the two calls might use different DO_USER_GROUP values and thus
539 might end up with different uid/gid for the two calls. If anybody
540 wants the uid/gid they should decode it first, and other callers
541 should decode it without uid/gid before calling a routine,
542 e.g. print_header, that assumes decoded data. */
544 decode_header (union block
*header
, struct tar_stat_info
*stat_info
,
545 enum archive_format
*format_pointer
, int do_user_group
)
547 enum archive_format format
;
548 unsigned hbits
; /* high bits of the file mode. */
549 mode_t mode
= MODE_FROM_HEADER (header
->header
.mode
, &hbits
);
551 if (strcmp (header
->header
.magic
, TMAGIC
) == 0)
553 if (header
->star_header
.prefix
[130] == 0
554 && ISOCTAL (header
->star_header
.atime
[0])
555 && header
->star_header
.atime
[11] == ' '
556 && ISOCTAL (header
->star_header
.ctime
[0])
557 && header
->star_header
.ctime
[11] == ' ')
558 format
= STAR_FORMAT
;
559 else if (stat_info
->xhdr
.size
)
560 format
= POSIX_FORMAT
;
562 format
= USTAR_FORMAT
;
564 else if (strcmp (header
->header
.magic
, OLDGNU_MAGIC
) == 0)
565 format
= hbits
? OLDGNU_FORMAT
: GNU_FORMAT
;
568 *format_pointer
= format
;
570 stat_info
->stat
.st_mode
= mode
;
571 stat_info
->mtime
.tv_sec
= TIME_FROM_HEADER (header
->header
.mtime
);
572 stat_info
->mtime
.tv_nsec
= 0;
573 assign_string (&stat_info
->uname
,
574 header
->header
.uname
[0] ? header
->header
.uname
: NULL
);
575 assign_string (&stat_info
->gname
,
576 header
->header
.gname
[0] ? header
->header
.gname
: NULL
);
578 if (format
== OLDGNU_FORMAT
&& incremental_option
)
580 stat_info
->atime
.tv_sec
= TIME_FROM_HEADER (header
->oldgnu_header
.atime
);
581 stat_info
->ctime
.tv_sec
= TIME_FROM_HEADER (header
->oldgnu_header
.ctime
);
582 stat_info
->atime
.tv_nsec
= stat_info
->ctime
.tv_nsec
= 0;
584 else if (format
== STAR_FORMAT
)
586 stat_info
->atime
.tv_sec
= TIME_FROM_HEADER (header
->star_header
.atime
);
587 stat_info
->ctime
.tv_sec
= TIME_FROM_HEADER (header
->star_header
.ctime
);
588 stat_info
->atime
.tv_nsec
= stat_info
->ctime
.tv_nsec
= 0;
591 stat_info
->atime
= stat_info
->ctime
= start_time
;
593 if (format
== V7_FORMAT
)
595 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
596 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
597 stat_info
->stat
.st_rdev
= 0;
603 /* FIXME: Decide if this should somewhat depend on -p. */
605 if (numeric_owner_option
606 || !*header
->header
.uname
607 || !uname_to_uid (header
->header
.uname
, &stat_info
->stat
.st_uid
))
608 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
610 if (numeric_owner_option
611 || !*header
->header
.gname
612 || !gname_to_gid (header
->header
.gname
, &stat_info
->stat
.st_gid
))
613 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
616 switch (header
->header
.typeflag
)
620 stat_info
->stat
.st_rdev
=
621 makedev (MAJOR_FROM_HEADER (header
->header
.devmajor
),
622 MINOR_FROM_HEADER (header
->header
.devminor
));
626 stat_info
->stat
.st_rdev
= 0;
630 stat_info
->archive_file_size
= stat_info
->stat
.st_size
;
631 xheader_decode (stat_info
);
633 if (sparse_member_p (stat_info
))
635 sparse_fixup_header (stat_info
);
636 stat_info
->is_sparse
= true;
640 stat_info
->is_sparse
= false;
641 if (((current_format
== GNU_FORMAT
642 || current_format
== OLDGNU_FORMAT
)
643 && current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
644 || stat_info
->dumpdir
)
645 stat_info
->is_dumpdir
= true;
648 transform_member_name (&stat_info
->file_name
, XFORM_REGFILE
);
649 switch (header
->header
.typeflag
)
652 transform_member_name (&stat_info
->link_name
, XFORM_SYMLINK
);
656 transform_member_name (&stat_info
->link_name
, XFORM_LINK
);
660 /* Convert buffer at WHERE0 of size DIGS from external format to
661 uintmax_t. DIGS must be positive. If TYPE is nonnull, the data
662 are of type TYPE. The buffer must represent a value in the range
663 -MINUS_MINVAL through MAXVAL. If OCTAL_ONLY, allow only octal
664 numbers instead of the other GNU extensions. Return -1 on error,
665 diagnosing the error if TYPE is nonnull and if !SILENT. */
667 from_header (char const *where0
, size_t digs
, char const *type
,
668 uintmax_t minus_minval
, uintmax_t maxval
,
669 bool octal_only
, bool silent
)
672 char const *where
= where0
;
673 char const *lim
= where
+ digs
;
676 /* Accommodate buggy tar of unknown vintage, which outputs leading
677 NUL if the previous field overflows. */
680 /* Accommodate older tars, which output leading spaces. */
687 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
689 _("Blanks in header where numeric %s value expected"),
693 if (!ISSPACE ((unsigned char) *where
))
699 if (ISODIGIT (*where
))
701 char const *where1
= where
;
702 uintmax_t overflow
= 0;
706 value
+= *where
++ - '0';
707 if (where
== lim
|| ! ISODIGIT (*where
))
709 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
713 /* Parse the output of older, unportable tars, which generate
714 negative values in two's complement octal. If the leading
715 nonzero digit is 1, we can't recover the original value
716 reliably; so do this only if the digit is 2 or more. This
717 catches the common case of 32-bit negative time stamps. */
718 if ((overflow
|| maxval
< value
) && '2' <= *where1
&& type
)
720 /* Compute the negative of the input value, assuming two's
722 int digit
= (*where1
- '0') | 4;
730 if (where
== lim
|| ! ISODIGIT (*where
))
732 digit
= *where
- '0';
733 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
739 if (!overflow
&& value
<= minus_minval
)
743 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
744 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
745 (int) (where
- where1
), where1
, type
));
754 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
755 _("Archive octal value %.*s is out of %s range"),
756 (int) (where
- where1
), where1
, type
));
762 /* Suppress the following extensions. */
764 else if (*where
== '-' || *where
== '+')
766 /* Parse base-64 output produced only by tar test versions
767 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
768 Support for this will be withdrawn in future releases. */
772 static bool warned_once
;
776 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
779 negative
= *where
++ == '-';
781 && (dig
= base64_map
[(unsigned char) *where
]) < 64)
783 if (value
<< LG_64
>> LG_64
!= value
)
785 char *string
= alloca (digs
+ 1);
786 memcpy (string
, where0
, digs
);
790 _("Archive signed base-64 string %s is out of %s range"),
791 quote (string
), type
));
794 value
= (value
<< LG_64
) | dig
;
798 else if (*where
== '\200' /* positive base-256 */
799 || *where
== '\377' /* negative base-256 */)
801 /* Parse base-256 output. A nonnegative number N is
802 represented as (256**DIGS)/2 + N; a negative number -N is
803 represented as (256**DIGS) - N, i.e. as two's complement.
804 The representation guarantees that the leading bit is
805 always on, so that we don't confuse this format with the
806 others (assuming ASCII bytes of 8 bits or more). */
807 int signbit
= *where
& (1 << (LG_256
- 2));
808 uintmax_t topbits
= (((uintmax_t) - signbit
)
809 << (CHAR_BIT
* sizeof (uintmax_t)
810 - LG_256
- (LG_256
- 2)));
811 value
= (*where
++ & ((1 << (LG_256
- 2)) - 1)) - signbit
;
814 value
= (value
<< LG_256
) + (unsigned char) *where
++;
817 if (((value
<< LG_256
>> LG_256
) | topbits
) != value
)
821 _("Archive base-256 value is out of %s range"),
831 if (where
!= lim
&& *where
&& !ISSPACE ((unsigned char) *where
))
835 char buf
[1000]; /* Big enough to represent any header. */
836 static struct quoting_options
*o
;
840 o
= clone_quoting_options (0);
841 set_quoting_style (o
, locale_quoting_style
);
844 while (where0
!= lim
&& ! lim
[-1])
846 quotearg_buffer (buf
, sizeof buf
, where0
, lim
- where
, o
);
849 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
850 _("Archive contains %.*s where numeric %s value expected"),
851 (int) sizeof buf
, buf
, type
));
857 if (value
<= (negative
? minus_minval
: maxval
))
858 return negative
? -value
: value
;
862 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
863 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
864 char value_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
865 char *minval_string
= STRINGIFY_BIGINT (minus_minval
, minval_buf
+ 1);
866 char *value_string
= STRINGIFY_BIGINT (value
, value_buf
+ 1);
868 *--value_string
= '-';
870 *--minval_string
= '-';
871 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
872 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
874 minval_string
, STRINGIFY_BIGINT (maxval
, maxval_buf
)));
881 gid_from_header (const char *p
, size_t s
)
883 return from_header (p
, s
, "gid_t",
884 - (uintmax_t) TYPE_MINIMUM (gid_t
),
885 (uintmax_t) TYPE_MAXIMUM (gid_t
),
890 major_from_header (const char *p
, size_t s
)
892 return from_header (p
, s
, "major_t",
893 - (uintmax_t) TYPE_MINIMUM (major_t
),
894 (uintmax_t) TYPE_MAXIMUM (major_t
), false, false);
898 minor_from_header (const char *p
, size_t s
)
900 return from_header (p
, s
, "minor_t",
901 - (uintmax_t) TYPE_MINIMUM (minor_t
),
902 (uintmax_t) TYPE_MAXIMUM (minor_t
), false, false);
905 /* Convert P to the file mode, as understood by tar.
906 Store unrecognized mode bits (from 10th up) in HBITS. */
908 mode_from_header (const char *p
, size_t s
, unsigned *hbits
)
910 unsigned u
= from_header (p
, s
, "mode_t",
911 - (uintmax_t) TYPE_MINIMUM (mode_t
),
912 TYPE_MAXIMUM (uintmax_t), false, false);
913 mode_t mode
= ((u
& TSUID
? S_ISUID
: 0)
914 | (u
& TSGID
? S_ISGID
: 0)
915 | (u
& TSVTX
? S_ISVTX
: 0)
916 | (u
& TUREAD
? S_IRUSR
: 0)
917 | (u
& TUWRITE
? S_IWUSR
: 0)
918 | (u
& TUEXEC
? S_IXUSR
: 0)
919 | (u
& TGREAD
? S_IRGRP
: 0)
920 | (u
& TGWRITE
? S_IWGRP
: 0)
921 | (u
& TGEXEC
? S_IXGRP
: 0)
922 | (u
& TOREAD
? S_IROTH
: 0)
923 | (u
& TOWRITE
? S_IWOTH
: 0)
924 | (u
& TOEXEC
? S_IXOTH
: 0));
930 off_from_header (const char *p
, size_t s
)
932 /* Negative offsets are not allowed in tar files, so invoke
933 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
934 return from_header (p
, s
, "off_t", (uintmax_t) 0,
935 (uintmax_t) TYPE_MAXIMUM (off_t
), false, false);
939 size_from_header (const char *p
, size_t s
)
941 return from_header (p
, s
, "size_t", (uintmax_t) 0,
942 (uintmax_t) TYPE_MAXIMUM (size_t), false, false);
946 time_from_header (const char *p
, size_t s
)
948 return from_header (p
, s
, "time_t",
949 - (uintmax_t) TYPE_MINIMUM (time_t),
950 (uintmax_t) TYPE_MAXIMUM (time_t), false, false);
954 uid_from_header (const char *p
, size_t s
)
956 return from_header (p
, s
, "uid_t",
957 - (uintmax_t) TYPE_MINIMUM (uid_t
),
958 (uintmax_t) TYPE_MAXIMUM (uid_t
), false, false);
962 uintmax_from_header (const char *p
, size_t s
)
964 return from_header (p
, s
, "uintmax_t", (uintmax_t) 0,
965 TYPE_MAXIMUM (uintmax_t), false, false);
969 /* Return a printable representation of T. The result points to
970 static storage that can be reused in the next call to this
971 function, to ctime, or to asctime. If FULL_TIME, then output the
972 time stamp to its full resolution; otherwise, just output it to
973 1-minute resolution. */
975 tartime (struct timespec t
, bool full_time
)
977 enum { fraclen
= sizeof ".FFFFFFFFF" - 1 };
978 static char buffer
[max (UINTMAX_STRSIZE_BOUND
+ 1,
979 INT_STRLEN_BOUND (int) + 16)
984 bool negative
= s
< 0;
987 if (negative
&& ns
!= 0)
990 ns
= 1000000000 - ns
;
993 tm
= utc_option
? gmtime (&s
) : localtime (&s
);
998 sprintf (buffer
, "%04ld-%02d-%02d %02d:%02d:%02d",
999 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
1000 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1001 code_ns_fraction (ns
, buffer
+ strlen (buffer
));
1004 sprintf (buffer
, "%04ld-%02d-%02d %02d:%02d",
1005 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
1006 tm
->tm_hour
, tm
->tm_min
);
1010 /* The time stamp cannot be broken down, most likely because it
1011 is out of range. Convert it as an integer,
1012 right-adjusted in a field with the same width as the usual
1013 4-year ISO time format. */
1014 p
= umaxtostr (negative
? - (uintmax_t) s
: s
,
1015 buffer
+ sizeof buffer
- UINTMAX_STRSIZE_BOUND
- fraclen
);
1018 while ((buffer
+ sizeof buffer
- sizeof "YYYY-MM-DD HH:MM"
1019 + (full_time
? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1023 code_ns_fraction (ns
, buffer
+ sizeof buffer
- 1 - fraclen
);
1027 /* Actually print it.
1029 Plain and fancy file header block logging. Non-verbose just prints
1030 the name, e.g. for "tar t" or "tar x". This should just contain
1031 file names, so it can be fed back into tar with xargs or the "-T"
1032 option. The verbose option can give a bunch of info, one line per
1033 file. I doubt anybody tries to parse its format, or if they do,
1034 they shouldn't. Unix tar is pretty random here anyway. */
1037 /* Width of "user/group size", with initial value chosen
1038 heuristically. This grows as needed, though this may cause some
1039 stairstepping in the output. Make it too small and the output will
1040 almost always look ragged. Make it too large and the output will
1041 be spaced out too far. */
1042 static int ugswidth
= 19;
1044 /* Width of printed time stamps. It grows if longer time stamps are
1045 found (typically, those with nanosecond resolution). Like
1046 USGWIDTH, some stairstepping may occur. */
1047 static int datewidth
= sizeof "YYYY-MM-DD HH:MM" - 1;
1049 static bool volume_label_printed
= false;
1052 simple_print_header (struct tar_stat_info
*st
, union block
*blk
,
1053 off_t block_ordinal
)
1056 char const *time_stamp
;
1060 /* These hold formatted ints. */
1061 char uform
[UINTMAX_STRSIZE_BOUND
], gform
[UINTMAX_STRSIZE_BOUND
];
1063 char size
[2 * UINTMAX_STRSIZE_BOUND
];
1064 /* holds formatted size or major,minor */
1065 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
1069 if (show_transformed_names_option
)
1070 temp_name
= st
->file_name
? st
->file_name
: st
->orig_file_name
;
1072 temp_name
= st
->orig_file_name
? st
->orig_file_name
: st
->file_name
;
1074 if (block_number_option
)
1076 char buf
[UINTMAX_STRSIZE_BOUND
];
1077 if (block_ordinal
< 0)
1078 block_ordinal
= current_block_ordinal ();
1079 block_ordinal
-= recent_long_name_blocks
;
1080 block_ordinal
-= recent_long_link_blocks
;
1081 fprintf (stdlis
, _("block %s: "),
1082 STRINGIFY_BIGINT (block_ordinal
, buf
));
1085 if (verbose_option
<= 1)
1087 /* Just the fax, mam. */
1088 fprintf (stdlis
, "%s\n", quotearg (temp_name
));
1092 /* File type and modes. */
1095 switch (blk
->header
.typeflag
)
1097 case GNUTYPE_VOLHDR
:
1098 volume_label_printed
= true;
1102 case GNUTYPE_MULTIVOL
:
1106 case GNUTYPE_LONGNAME
:
1107 case GNUTYPE_LONGLINK
:
1109 ERROR ((0, 0, _("Unexpected long name header")));
1112 case GNUTYPE_SPARSE
:
1116 if (temp_name
[strlen (temp_name
) - 1] == '/')
1122 case GNUTYPE_DUMPDIR
:
1145 pax_decode_mode (st
->stat
.st_mode
, modes
+ 1);
1149 time_stamp
= tartime (st
->mtime
, full_time_option
);
1150 time_stamp_len
= strlen (time_stamp
);
1151 if (datewidth
< time_stamp_len
)
1152 datewidth
= time_stamp_len
;
1154 /* User and group names. */
1158 && current_format
!= V7_FORMAT
1159 && !numeric_owner_option
)
1163 /* Try parsing it as an unsigned integer first, and as a
1164 uid_t if that fails. This method can list positive user
1165 ids that are too large to fit in a uid_t. */
1166 uintmax_t u
= from_header (blk
->header
.uid
,
1167 sizeof blk
->header
.uid
, 0,
1169 (uintmax_t) TYPE_MAXIMUM (uintmax_t),
1172 user
= STRINGIFY_BIGINT (u
, uform
);
1175 sprintf (uform
, "%ld",
1176 (long) UID_FROM_HEADER (blk
->header
.uid
));
1183 && current_format
!= V7_FORMAT
1184 && !numeric_owner_option
)
1188 /* Try parsing it as an unsigned integer first, and as a
1189 gid_t if that fails. This method can list positive group
1190 ids that are too large to fit in a gid_t. */
1191 uintmax_t g
= from_header (blk
->header
.gid
,
1192 sizeof blk
->header
.gid
, 0,
1194 (uintmax_t) TYPE_MAXIMUM (uintmax_t),
1197 group
= STRINGIFY_BIGINT (g
, gform
);
1200 sprintf (gform
, "%ld",
1201 (long) GID_FROM_HEADER (blk
->header
.gid
));
1206 /* Format the file size or major/minor device numbers. */
1208 switch (blk
->header
.typeflag
)
1213 STRINGIFY_BIGINT (major (st
->stat
.st_rdev
), uintbuf
));
1216 STRINGIFY_BIGINT (minor (st
->stat
.st_rdev
), uintbuf
));
1220 /* st->stat.st_size keeps stored file size */
1221 strcpy (size
, STRINGIFY_BIGINT (st
->stat
.st_size
, uintbuf
));
1225 /* Figure out padding and print the whole line. */
1227 sizelen
= strlen (size
);
1228 pad
= strlen (user
) + 1 + strlen (group
) + 1 + sizelen
;
1232 fprintf (stdlis
, "%s %s/%s %*s %-*s",
1233 modes
, user
, group
, ugswidth
- pad
+ sizelen
, size
,
1234 datewidth
, time_stamp
);
1236 fprintf (stdlis
, " %s", quotearg (temp_name
));
1238 switch (blk
->header
.typeflag
)
1241 fprintf (stdlis
, " -> %s\n", quotearg (st
->link_name
));
1245 fprintf (stdlis
, _(" link to %s\n"), quotearg (st
->link_name
));
1250 char type_string
[2];
1251 type_string
[0] = blk
->header
.typeflag
;
1252 type_string
[1] = '\0';
1253 fprintf (stdlis
, _(" unknown file type %s\n"),
1254 quote (type_string
));
1260 case GNUTYPE_SPARSE
:
1266 case GNUTYPE_DUMPDIR
:
1267 putc ('\n', stdlis
);
1270 case GNUTYPE_LONGLINK
:
1271 fprintf (stdlis
, _("--Long Link--\n"));
1274 case GNUTYPE_LONGNAME
:
1275 fprintf (stdlis
, _("--Long Name--\n"));
1278 case GNUTYPE_VOLHDR
:
1279 fprintf (stdlis
, _("--Volume Header--\n"));
1282 case GNUTYPE_MULTIVOL
:
1285 (UINTMAX_FROM_HEADER (blk
->oldgnu_header
.offset
),
1287 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
1296 print_volume_label (void)
1298 struct tar_stat_info vstat
;
1300 enum archive_format dummy
;
1302 memset (&vblk
, 0, sizeof (vblk
));
1303 vblk
.header
.typeflag
= GNUTYPE_VOLHDR
;
1304 if (recent_global_header
)
1305 memcpy (vblk
.header
.mtime
, recent_global_header
->header
.mtime
,
1306 sizeof vblk
.header
.mtime
);
1307 tar_stat_init (&vstat
);
1308 assign_string (&vstat
.file_name
, ".");
1309 decode_header (&vblk
, &vstat
, &dummy
, 0);
1310 assign_string (&vstat
.file_name
, volume_label
);
1311 simple_print_header (&vstat
, &vblk
, 0);
1312 tar_stat_destroy (&vstat
);
1316 print_header (struct tar_stat_info
*st
, union block
*blk
,
1317 off_t block_ordinal
)
1319 if (current_format
== POSIX_FORMAT
&& !volume_label_printed
&& volume_label
)
1321 print_volume_label ();
1322 volume_label_printed
= true;
1325 simple_print_header (st
, blk
, block_ordinal
);
1328 /* Print a similar line when we make a directory automatically. */
1330 print_for_mkdir (char *dirname
, int length
, mode_t mode
)
1334 if (verbose_option
> 1)
1336 /* File type and modes. */
1339 pax_decode_mode (mode
, modes
+ 1);
1341 if (block_number_option
)
1343 char buf
[UINTMAX_STRSIZE_BOUND
];
1344 fprintf (stdlis
, _("block %s: "),
1345 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
1348 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ 1 + datewidth
,
1349 _("Creating directory:"), length
, quotearg (dirname
));
1353 /* Skip over SIZE bytes of data in blocks in the archive. */
1355 skip_file (off_t size
)
1359 /* FIXME: Make sure mv_begin is always called before it */
1361 if (seekable_archive
)
1363 off_t nblk
= seek_archive (size
);
1365 size
-= nblk
* BLOCKSIZE
;
1367 seekable_archive
= false;
1370 mv_size_left (size
);
1374 x
= find_next_block ();
1376 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1378 set_next_block_after (x
);
1380 mv_size_left (size
);
1384 /* Skip the current member in the archive.
1385 NOTE: Current header must be decoded before calling this function. */
1389 if (!current_stat_info
.skipped
)
1391 char save_typeflag
= current_header
->header
.typeflag
;
1392 set_next_block_after (current_header
);
1394 mv_begin (¤t_stat_info
);
1396 if (current_stat_info
.is_sparse
)
1397 sparse_skip_file (¤t_stat_info
);
1398 else if (save_typeflag
!= DIRTYPE
)
1399 skip_file (current_stat_info
.stat
.st_size
);
1406 test_archive_label ()
1411 open_archive (ACCESS_READ
);
1412 if (read_header (¤t_header
, ¤t_stat_info
, read_header_auto
)
1415 decode_header (current_header
,
1416 ¤t_stat_info
, ¤t_format
, 0);
1417 if (current_header
->header
.typeflag
== GNUTYPE_VOLHDR
)
1418 assign_string (&volume_label
, current_header
->header
.name
);
1423 print_volume_label ();
1424 if (!name_match (volume_label
) && multi_volume_option
)
1426 char *s
= drop_volume_label_suffix (volume_label
);