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, 2012
5 Free Software Foundation, Inc.
7 Written by John Gilmore, on 1985-08-26.
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any later
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 #define max(a, b) ((a) < (b) ? (b) : (a))
31 union block
*current_header
; /* points to current archive header */
32 enum archive_format current_format
; /* recognized format */
33 union block
*recent_long_name
; /* recent long name header and contents */
34 union block
*recent_long_link
; /* likewise, for long link */
35 size_t recent_long_name_blocks
; /* number of blocks in recent_long_name */
36 size_t recent_long_link_blocks
; /* likewise, for long link */
37 union block
*recent_global_header
; /* Recent global header block */
39 #define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where))
40 #define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where))
41 #define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where))
42 #define MODE_FROM_HEADER(where, hbits) \
43 mode_from_header (where, sizeof (where), hbits)
44 #define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where))
45 #define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where))
47 static gid_t
gid_from_header (const char *buf
, size_t size
);
48 static major_t
major_from_header (const char *buf
, size_t size
);
49 static minor_t
minor_from_header (const char *buf
, size_t size
);
50 static mode_t
mode_from_header (const char *buf
, size_t size
, unsigned *hbits
);
51 static time_t time_from_header (const char *buf
, size_t size
);
52 static uid_t
uid_from_header (const char *buf
, size_t size
);
53 static uintmax_t from_header (const char *, size_t, const char *,
54 uintmax_t, uintmax_t, bool, bool);
56 /* Base 64 digits; see Internet RFC 2045 Table 1. */
57 static char const base_64_digits
[64] =
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 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
62 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
63 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
66 /* Table of base-64 digit values indexed by unsigned chars.
67 The value is 64 for unsigned chars that are not base-64 digits. */
68 static char base64_map
[UCHAR_MAX
+ 1];
74 memset (base64_map
, 64, sizeof base64_map
);
75 for (i
= 0; i
< 64; i
++)
76 base64_map
[(int) base_64_digits
[i
]] = i
;
80 decode_xform (char *file_name
, void *data
)
82 int type
= *(int*)data
;
87 /* FIXME: It is not quite clear how and to which extent are the symbolic
88 links subject to filename transformation. In the absence of another
89 solution, symbolic links are exempt from component stripping and
90 name suffix normalization, but subject to filename transformation
95 file_name
= safer_name_suffix (file_name
, true, absolute_names_option
);
99 file_name
= safer_name_suffix (file_name
, false, absolute_names_option
);
103 if (strip_name_components
)
105 size_t prefix_len
= stripped_prefix_len (file_name
,
106 strip_name_components
);
107 if (prefix_len
== (size_t) -1)
108 prefix_len
= strlen (file_name
);
109 file_name
+= prefix_len
;
115 transform_member_name (char **pinput
, int type
)
117 return transform_name_fp (pinput
, type
, decode_xform
, &type
);
121 transform_stat_info (int typeflag
, struct tar_stat_info
*stat_info
)
123 if (typeflag
== GNUTYPE_VOLHDR
)
124 /* Name transformations don't apply to volume headers. */
127 transform_member_name (&stat_info
->file_name
, XFORM_REGFILE
);
131 transform_member_name (&stat_info
->link_name
, XFORM_SYMLINK
);
135 transform_member_name (&stat_info
->link_name
, XFORM_LINK
);
139 /* Main loop for reading an archive. */
141 read_and (void (*do_something
) (void))
143 enum read_header status
= HEADER_STILL_UNREAD
;
144 enum read_header prev_status
;
145 struct timespec mtime
;
150 open_archive (ACCESS_READ
);
153 prev_status
= status
;
154 tar_stat_destroy (¤t_stat_info
);
156 status
= read_header (¤t_header
, ¤t_stat_info
,
160 case HEADER_STILL_UNREAD
:
161 case HEADER_SUCCESS_EXTENDED
:
166 /* Valid header. We should decode next field (mode) first.
167 Ensure incoming names are null terminated. */
168 decode_header (current_header
, ¤t_stat_info
,
170 if (! name_match (current_stat_info
.file_name
)
171 || (NEWER_OPTION_INITIALIZED (newer_mtime_option
)
172 /* FIXME: We get mtime now, and again later; this causes
173 duplicate diagnostics if header.mtime is bogus. */
175 = TIME_FROM_HEADER (current_header
->header
.mtime
)),
176 /* FIXME: Grab fractional time stamps from
179 current_stat_info
.mtime
= mtime
,
180 OLDER_TAR_STAT_TIME (current_stat_info
, m
)))
181 || excluded_name (current_stat_info
.file_name
))
183 switch (current_header
->header
.typeflag
)
186 case GNUTYPE_MULTIVOL
:
190 if (show_omitted_dirs_option
)
191 WARN ((0, 0, _("%s: Omitting"),
192 quotearg_colon (current_stat_info
.file_name
)));
199 transform_stat_info (current_header
->header
.typeflag
,
204 case HEADER_ZERO_BLOCK
:
205 if (block_number_option
)
207 char buf
[UINTMAX_STRSIZE_BOUND
];
208 fprintf (stdlis
, _("block %s: ** Block of NULs **\n"),
209 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
212 set_next_block_after (current_header
);
214 if (!ignore_zeros_option
)
216 char buf
[UINTMAX_STRSIZE_BOUND
];
218 status
= read_header (¤t_header
, ¤t_stat_info
,
220 if (status
== HEADER_ZERO_BLOCK
)
222 WARNOPT (WARN_ALONE_ZERO_BLOCK
,
223 (0, 0, _("A lone zero block at %s"),
224 STRINGIFY_BIGINT (current_block_ordinal (), buf
)));
227 status
= prev_status
;
230 case HEADER_END_OF_FILE
:
231 if (block_number_option
)
233 char buf
[UINTMAX_STRSIZE_BOUND
];
234 fprintf (stdlis
, _("block %s: ** End of File **\n"),
235 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
240 /* If the previous header was good, tell them that we are
241 skipping bad ones. */
242 set_next_block_after (current_header
);
245 case HEADER_STILL_UNREAD
:
246 ERROR ((0, 0, _("This does not look like a tar archive")));
249 case HEADER_ZERO_BLOCK
:
251 if (block_number_option
)
253 char buf
[UINTMAX_STRSIZE_BOUND
];
254 off_t block_ordinal
= current_block_ordinal ();
255 block_ordinal
-= recent_long_name_blocks
;
256 block_ordinal
-= recent_long_link_blocks
;
257 fprintf (stdlis
, _("block %s: "),
258 STRINGIFY_BIGINT (block_ordinal
, buf
));
260 ERROR ((0, 0, _("Skipping to next header")));
263 case HEADER_END_OF_FILE
:
265 /* We are in the middle of a cascade of errors. */
268 case HEADER_SUCCESS_EXTENDED
:
275 while (!all_names_found (¤t_stat_info
));
278 names_notfound (); /* print names not found */
281 /* Print a header block, based on tar options. */
285 off_t block_ordinal
= current_block_ordinal ();
287 /* Print the header block. */
289 print_header (¤t_stat_info
, current_header
, block_ordinal
);
291 if (incremental_option
)
293 if (verbose_option
> 2)
295 if (is_dumpdir (¤t_stat_info
))
296 list_dumpdir (current_stat_info
.dumpdir
,
297 dumpdir_size (current_stat_info
.dumpdir
));
304 /* Check header checksum */
305 /* The standard BSD tar sources create the checksum by adding up the
306 bytes in the header as type char. I think the type char was unsigned
307 on the PDP-11, but it's signed on the Next and Sun. It looks like the
308 sources to BSD tar were never changed to compute the checksum
309 correctly, so both the Sun and Next add the bytes of the header as
310 signed chars. This doesn't cause a problem until you get a file with
311 a name containing characters with the high bit set. So tar_checksum
312 computes two checksums -- signed and unsigned. */
315 tar_checksum (union block
*header
, bool silent
)
318 int unsigned_sum
= 0; /* the POSIX one :-) */
319 int signed_sum
= 0; /* the Sun one :-( */
321 uintmax_t parsed_sum
;
325 for (i
= sizeof *header
; i
-- != 0;)
327 unsigned_sum
+= (unsigned char) *p
;
328 signed_sum
+= (signed char) (*p
++);
331 if (unsigned_sum
== 0)
332 return HEADER_ZERO_BLOCK
;
334 /* Adjust checksum to count the "chksum" field as blanks. */
336 for (i
= sizeof header
->header
.chksum
; i
-- != 0;)
338 unsigned_sum
-= (unsigned char) header
->header
.chksum
[i
];
339 signed_sum
-= (signed char) (header
->header
.chksum
[i
]);
341 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
342 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
344 parsed_sum
= from_header (header
->header
.chksum
,
345 sizeof header
->header
.chksum
, 0,
347 (uintmax_t) TYPE_MAXIMUM (int), true, silent
);
348 if (parsed_sum
== (uintmax_t) -1)
349 return HEADER_FAILURE
;
351 recorded_sum
= parsed_sum
;
353 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
354 return HEADER_FAILURE
;
356 return HEADER_SUCCESS
;
359 /* Read a block that's supposed to be a header block. Return its
360 address in *RETURN_BLOCK, and if it is good, the file's size
361 and names (file name, link name) in *INFO.
363 Return one of enum read_header describing the status of the
366 The MODE parameter instructs read_header what to do with special
367 header blocks, i.e.: extended POSIX, GNU long name or long link,
370 read_header_auto process them automatically,
371 read_header_x_raw when a special header is read, return
372 HEADER_SUCCESS_EXTENDED without actually
373 processing the header,
374 read_header_x_global when a POSIX global header is read,
375 decode it and return HEADER_SUCCESS_EXTENDED.
377 You must always set_next_block_after(*return_block) to skip past
378 the header which this routine reads. */
381 read_header (union block
**return_block
, struct tar_stat_info
*info
,
382 enum read_header_mode mode
)
385 union block
*header_copy
;
387 union block
*data_block
;
388 size_t size
, written
;
389 union block
*next_long_name
= 0;
390 union block
*next_long_link
= 0;
391 size_t next_long_name_blocks
= 0;
392 size_t next_long_link_blocks
= 0;
396 enum read_header status
;
398 header
= find_next_block ();
399 *return_block
= header
;
401 return HEADER_END_OF_FILE
;
403 if ((status
= tar_checksum (header
, false)) != HEADER_SUCCESS
)
406 /* Good block. Decode file size and return. */
408 if (header
->header
.typeflag
== LNKTYPE
)
409 info
->stat
.st_size
= 0; /* links 0 size on tape */
411 info
->stat
.st_size
= OFF_FROM_HEADER (header
->header
.size
);
413 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
414 || header
->header
.typeflag
== GNUTYPE_LONGLINK
415 || header
->header
.typeflag
== XHDTYPE
416 || header
->header
.typeflag
== XGLTYPE
417 || header
->header
.typeflag
== SOLARIS_XHDTYPE
)
419 if (mode
== read_header_x_raw
)
420 return HEADER_SUCCESS_EXTENDED
;
421 else if (header
->header
.typeflag
== GNUTYPE_LONGNAME
422 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
424 size_t name_size
= info
->stat
.st_size
;
425 size_t n
= name_size
% BLOCKSIZE
;
426 size
= name_size
+ BLOCKSIZE
;
428 size
+= BLOCKSIZE
- n
;
430 if (name_size
!= info
->stat
.st_size
|| size
< name_size
)
433 header_copy
= xmalloc (size
+ 1);
435 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
)
437 free (next_long_name
);
438 next_long_name
= header_copy
;
439 next_long_name_blocks
= size
/ BLOCKSIZE
;
443 free (next_long_link
);
444 next_long_link
= header_copy
;
445 next_long_link_blocks
= size
/ BLOCKSIZE
;
448 set_next_block_after (header
);
449 *header_copy
= *header
;
450 bp
= header_copy
->buffer
+ BLOCKSIZE
;
452 for (size
-= BLOCKSIZE
; size
> 0; size
-= written
)
454 data_block
= find_next_block ();
457 ERROR ((0, 0, _("Unexpected EOF in archive")));
460 written
= available_space_after (data_block
);
464 memcpy (bp
, data_block
->buffer
, written
);
466 set_next_block_after ((union block
*)
467 (data_block
->buffer
+ written
- 1));
472 else if (header
->header
.typeflag
== XHDTYPE
473 || header
->header
.typeflag
== SOLARIS_XHDTYPE
)
474 xheader_read (&info
->xhdr
, header
,
475 OFF_FROM_HEADER (header
->header
.size
));
476 else if (header
->header
.typeflag
== XGLTYPE
)
480 if (!recent_global_header
)
481 recent_global_header
= xmalloc (sizeof *recent_global_header
);
482 memcpy (recent_global_header
, header
,
483 sizeof *recent_global_header
);
484 memset (&xhdr
, 0, sizeof xhdr
);
485 xheader_read (&xhdr
, header
,
486 OFF_FROM_HEADER (header
->header
.size
));
487 xheader_decode_global (&xhdr
);
488 xheader_destroy (&xhdr
);
489 if (mode
== read_header_x_global
)
490 return HEADER_SUCCESS_EXTENDED
;
499 struct posix_header
const *h
= &header
->header
;
500 char namebuf
[sizeof h
->prefix
+ 1 + NAME_FIELD_SIZE
+ 1];
502 free (recent_long_name
);
506 name
= next_long_name
->buffer
+ BLOCKSIZE
;
507 recent_long_name
= next_long_name
;
508 recent_long_name_blocks
= next_long_name_blocks
;
512 /* Accept file names as specified by POSIX.1-1996
516 if (h
->prefix
[0] && strcmp (h
->magic
, TMAGIC
) == 0)
518 memcpy (np
, h
->prefix
, sizeof h
->prefix
);
519 np
[sizeof h
->prefix
] = '\0';
523 memcpy (np
, h
->name
, sizeof h
->name
);
524 np
[sizeof h
->name
] = '\0';
526 recent_long_name
= 0;
527 recent_long_name_blocks
= 0;
529 assign_string (&info
->orig_file_name
, name
);
530 assign_string (&info
->file_name
, name
);
531 info
->had_trailing_slash
= strip_trailing_slashes (info
->file_name
);
533 free (recent_long_link
);
537 name
= next_long_link
->buffer
+ BLOCKSIZE
;
538 recent_long_link
= next_long_link
;
539 recent_long_link_blocks
= next_long_link_blocks
;
543 memcpy (namebuf
, h
->linkname
, sizeof h
->linkname
);
544 namebuf
[sizeof h
->linkname
] = '\0';
546 recent_long_link
= 0;
547 recent_long_link_blocks
= 0;
549 assign_string (&info
->link_name
, name
);
551 return HEADER_SUCCESS
;
556 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
558 /* Decode things from a file HEADER block into STAT_INFO, also setting
559 *FORMAT_POINTER depending on the header block format. If
560 DO_USER_GROUP, decode the user/group information (this is useful
561 for extraction, but waste time when merely listing).
563 read_header() has already decoded the checksum and length, so we don't.
565 This routine should *not* be called twice for the same block, since
566 the two calls might use different DO_USER_GROUP values and thus
567 might end up with different uid/gid for the two calls. If anybody
568 wants the uid/gid they should decode it first, and other callers
569 should decode it without uid/gid before calling a routine,
570 e.g. print_header, that assumes decoded data. */
572 decode_header (union block
*header
, struct tar_stat_info
*stat_info
,
573 enum archive_format
*format_pointer
, int do_user_group
)
575 enum archive_format format
;
576 unsigned hbits
; /* high bits of the file mode. */
577 mode_t mode
= MODE_FROM_HEADER (header
->header
.mode
, &hbits
);
579 if (strcmp (header
->header
.magic
, TMAGIC
) == 0)
581 if (header
->star_header
.prefix
[130] == 0
582 && ISOCTAL (header
->star_header
.atime
[0])
583 && header
->star_header
.atime
[11] == ' '
584 && ISOCTAL (header
->star_header
.ctime
[0])
585 && header
->star_header
.ctime
[11] == ' ')
586 format
= STAR_FORMAT
;
587 else if (stat_info
->xhdr
.size
)
588 format
= POSIX_FORMAT
;
590 format
= USTAR_FORMAT
;
592 else if (strcmp (header
->buffer
+ offsetof (struct posix_header
, magic
),
595 format
= hbits
? OLDGNU_FORMAT
: GNU_FORMAT
;
598 *format_pointer
= format
;
600 stat_info
->stat
.st_mode
= mode
;
601 stat_info
->mtime
.tv_sec
= TIME_FROM_HEADER (header
->header
.mtime
);
602 stat_info
->mtime
.tv_nsec
= 0;
603 assign_string (&stat_info
->uname
,
604 header
->header
.uname
[0] ? header
->header
.uname
: NULL
);
605 assign_string (&stat_info
->gname
,
606 header
->header
.gname
[0] ? header
->header
.gname
: NULL
);
608 xheader_xattr_init (stat_info
);
610 if (format
== OLDGNU_FORMAT
&& incremental_option
)
612 stat_info
->atime
.tv_sec
= TIME_FROM_HEADER (header
->oldgnu_header
.atime
);
613 stat_info
->ctime
.tv_sec
= TIME_FROM_HEADER (header
->oldgnu_header
.ctime
);
614 stat_info
->atime
.tv_nsec
= stat_info
->ctime
.tv_nsec
= 0;
616 else if (format
== STAR_FORMAT
)
618 stat_info
->atime
.tv_sec
= TIME_FROM_HEADER (header
->star_header
.atime
);
619 stat_info
->ctime
.tv_sec
= TIME_FROM_HEADER (header
->star_header
.ctime
);
620 stat_info
->atime
.tv_nsec
= stat_info
->ctime
.tv_nsec
= 0;
623 stat_info
->atime
= stat_info
->ctime
= start_time
;
625 if (format
== V7_FORMAT
)
627 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
628 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
629 stat_info
->stat
.st_rdev
= 0;
635 /* FIXME: Decide if this should somewhat depend on -p. */
637 if (numeric_owner_option
638 || !*header
->header
.uname
639 || !uname_to_uid (header
->header
.uname
, &stat_info
->stat
.st_uid
))
640 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
642 if (numeric_owner_option
643 || !*header
->header
.gname
644 || !gname_to_gid (header
->header
.gname
, &stat_info
->stat
.st_gid
))
645 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
648 switch (header
->header
.typeflag
)
652 stat_info
->stat
.st_rdev
=
653 makedev (MAJOR_FROM_HEADER (header
->header
.devmajor
),
654 MINOR_FROM_HEADER (header
->header
.devminor
));
658 stat_info
->stat
.st_rdev
= 0;
662 stat_info
->archive_file_size
= stat_info
->stat
.st_size
;
663 xheader_decode (stat_info
);
665 if (sparse_member_p (stat_info
))
667 sparse_fixup_header (stat_info
);
668 stat_info
->is_sparse
= true;
672 stat_info
->is_sparse
= false;
673 if (((current_format
== GNU_FORMAT
674 || current_format
== OLDGNU_FORMAT
)
675 && current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
676 || stat_info
->dumpdir
)
677 stat_info
->is_dumpdir
= true;
682 /* Convert buffer at WHERE0 of size DIGS from external format to
683 uintmax_t. DIGS must be positive. If TYPE is nonnull, the data
684 are of type TYPE. The buffer must represent a value in the range
685 -MINUS_MINVAL through MAXVAL. If OCTAL_ONLY, allow only octal
686 numbers instead of the other GNU extensions. Return -1 on error,
687 diagnosing the error if TYPE is nonnull and if !SILENT. */
689 from_header (char const *where0
, size_t digs
, char const *type
,
690 uintmax_t minus_minval
, uintmax_t maxval
,
691 bool octal_only
, bool silent
)
694 char const *where
= where0
;
695 char const *lim
= where
+ digs
;
698 /* Accommodate buggy tar of unknown vintage, which outputs leading
699 NUL if the previous field overflows. */
702 /* Accommodate older tars, which output leading spaces. */
709 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
711 _("Blanks in header where numeric %s value expected"),
715 if (!ISSPACE ((unsigned char) *where
))
721 if (ISODIGIT (*where
))
723 char const *where1
= where
;
724 uintmax_t overflow
= 0;
728 value
+= *where
++ - '0';
729 if (where
== lim
|| ! ISODIGIT (*where
))
731 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
735 /* Parse the output of older, unportable tars, which generate
736 negative values in two's complement octal. If the leading
737 nonzero digit is 1, we can't recover the original value
738 reliably; so do this only if the digit is 2 or more. This
739 catches the common case of 32-bit negative time stamps. */
740 if ((overflow
|| maxval
< value
) && '2' <= *where1
&& type
)
742 /* Compute the negative of the input value, assuming two's
744 int digit
= (*where1
- '0') | 4;
752 if (where
== lim
|| ! ISODIGIT (*where
))
754 digit
= *where
- '0';
755 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
761 if (!overflow
&& value
<= minus_minval
)
765 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
766 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
767 (int) (where
- where1
), where1
, type
));
776 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
777 _("Archive octal value %.*s is out of %s range"),
778 (int) (where
- where1
), where1
, type
));
784 /* Suppress the following extensions. */
786 else if (*where
== '-' || *where
== '+')
788 /* Parse base-64 output produced only by tar test versions
789 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
790 Support for this will be withdrawn in future releases. */
794 static bool warned_once
;
798 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
801 negative
= *where
++ == '-';
803 && (dig
= base64_map
[(unsigned char) *where
]) < 64)
805 if (value
<< LG_64
>> LG_64
!= value
)
807 char *string
= alloca (digs
+ 1);
808 memcpy (string
, where0
, digs
);
812 _("Archive signed base-64 string %s is out of %s range"),
813 quote (string
), type
));
816 value
= (value
<< LG_64
) | dig
;
820 else if (*where
== '\200' /* positive base-256 */
821 || *where
== '\377' /* negative base-256 */)
823 /* Parse base-256 output. A nonnegative number N is
824 represented as (256**DIGS)/2 + N; a negative number -N is
825 represented as (256**DIGS) - N, i.e. as two's complement.
826 The representation guarantees that the leading bit is
827 always on, so that we don't confuse this format with the
828 others (assuming ASCII bytes of 8 bits or more). */
829 int signbit
= *where
& (1 << (LG_256
- 2));
830 uintmax_t topbits
= (((uintmax_t) - signbit
)
831 << (CHAR_BIT
* sizeof (uintmax_t)
832 - LG_256
- (LG_256
- 2)));
833 value
= (*where
++ & ((1 << (LG_256
- 2)) - 1)) - signbit
;
836 value
= (value
<< LG_256
) + (unsigned char) *where
++;
839 if (((value
<< LG_256
>> LG_256
) | topbits
) != value
)
843 _("Archive base-256 value is out of %s range"),
853 if (where
!= lim
&& *where
&& !ISSPACE ((unsigned char) *where
))
857 char buf
[1000]; /* Big enough to represent any header. */
858 static struct quoting_options
*o
;
862 o
= clone_quoting_options (0);
863 set_quoting_style (o
, locale_quoting_style
);
866 while (where0
!= lim
&& ! lim
[-1])
868 quotearg_buffer (buf
, sizeof buf
, where0
, lim
- where
, o
);
871 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
872 _("Archive contains %.*s where numeric %s value expected"),
873 (int) sizeof buf
, buf
, type
));
879 if (value
<= (negative
? minus_minval
: maxval
))
880 return negative
? -value
: value
;
884 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
885 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
886 char value_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
887 char *minval_string
= STRINGIFY_BIGINT (minus_minval
, minval_buf
+ 1);
888 char *value_string
= STRINGIFY_BIGINT (value
, value_buf
+ 1);
890 *--value_string
= '-';
892 *--minval_string
= '-';
893 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
894 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
896 minval_string
, STRINGIFY_BIGINT (maxval
, maxval_buf
)));
903 gid_from_header (const char *p
, size_t s
)
905 return from_header (p
, s
, "gid_t",
906 - (uintmax_t) TYPE_MINIMUM (gid_t
),
907 (uintmax_t) TYPE_MAXIMUM (gid_t
),
912 major_from_header (const char *p
, size_t s
)
914 return from_header (p
, s
, "major_t",
915 - (uintmax_t) TYPE_MINIMUM (major_t
),
916 (uintmax_t) TYPE_MAXIMUM (major_t
), false, false);
920 minor_from_header (const char *p
, size_t s
)
922 return from_header (p
, s
, "minor_t",
923 - (uintmax_t) TYPE_MINIMUM (minor_t
),
924 (uintmax_t) TYPE_MAXIMUM (minor_t
), false, false);
927 /* Convert P to the file mode, as understood by tar.
928 Store unrecognized mode bits (from 10th up) in HBITS. */
930 mode_from_header (const char *p
, size_t s
, unsigned *hbits
)
932 unsigned u
= from_header (p
, s
, "mode_t",
933 - (uintmax_t) TYPE_MINIMUM (mode_t
),
934 TYPE_MAXIMUM (uintmax_t), false, false);
935 mode_t mode
= ((u
& TSUID
? S_ISUID
: 0)
936 | (u
& TSGID
? S_ISGID
: 0)
937 | (u
& TSVTX
? S_ISVTX
: 0)
938 | (u
& TUREAD
? S_IRUSR
: 0)
939 | (u
& TUWRITE
? S_IWUSR
: 0)
940 | (u
& TUEXEC
? S_IXUSR
: 0)
941 | (u
& TGREAD
? S_IRGRP
: 0)
942 | (u
& TGWRITE
? S_IWGRP
: 0)
943 | (u
& TGEXEC
? S_IXGRP
: 0)
944 | (u
& TOREAD
? S_IROTH
: 0)
945 | (u
& TOWRITE
? S_IWOTH
: 0)
946 | (u
& TOEXEC
? S_IXOTH
: 0));
952 off_from_header (const char *p
, size_t s
)
954 /* Negative offsets are not allowed in tar files, so invoke
955 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
956 return from_header (p
, s
, "off_t", (uintmax_t) 0,
957 (uintmax_t) TYPE_MAXIMUM (off_t
), false, false);
961 time_from_header (const char *p
, size_t s
)
963 return from_header (p
, s
, "time_t",
964 - (uintmax_t) TYPE_MINIMUM (time_t),
965 (uintmax_t) TYPE_MAXIMUM (time_t), false, false);
969 uid_from_header (const char *p
, size_t s
)
971 return from_header (p
, s
, "uid_t",
972 - (uintmax_t) TYPE_MINIMUM (uid_t
),
973 (uintmax_t) TYPE_MAXIMUM (uid_t
), false, false);
977 uintmax_from_header (const char *p
, size_t s
)
979 return from_header (p
, s
, "uintmax_t", (uintmax_t) 0,
980 TYPE_MAXIMUM (uintmax_t), false, false);
984 /* Return a printable representation of T. The result points to
985 static storage that can be reused in the next call to this
986 function, to ctime, or to asctime. If FULL_TIME, then output the
987 time stamp to its full resolution; otherwise, just output it to
988 1-minute resolution. */
990 tartime (struct timespec t
, bool full_time
)
992 enum { fraclen
= sizeof ".FFFFFFFFF" - 1 };
993 static char buffer
[max (UINTMAX_STRSIZE_BOUND
+ 1,
994 INT_STRLEN_BOUND (int) + 16)
999 bool negative
= s
< 0;
1002 if (negative
&& ns
!= 0)
1005 ns
= 1000000000 - ns
;
1008 tm
= utc_option
? gmtime (&s
) : localtime (&s
);
1013 sprintf (buffer
, "%04ld-%02d-%02d %02d:%02d:%02d",
1014 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
1015 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1016 code_ns_fraction (ns
, buffer
+ strlen (buffer
));
1019 sprintf (buffer
, "%04ld-%02d-%02d %02d:%02d",
1020 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
1021 tm
->tm_hour
, tm
->tm_min
);
1025 /* The time stamp cannot be broken down, most likely because it
1026 is out of range. Convert it as an integer,
1027 right-adjusted in a field with the same width as the usual
1028 4-year ISO time format. */
1029 p
= umaxtostr (negative
? - (uintmax_t) s
: s
,
1030 buffer
+ sizeof buffer
- UINTMAX_STRSIZE_BOUND
- fraclen
);
1033 while ((buffer
+ sizeof buffer
- sizeof "YYYY-MM-DD HH:MM"
1034 + (full_time
? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1038 code_ns_fraction (ns
, buffer
+ sizeof buffer
- 1 - fraclen
);
1042 /* Actually print it.
1044 Plain and fancy file header block logging. Non-verbose just prints
1045 the name, e.g. for "tar t" or "tar x". This should just contain
1046 file names, so it can be fed back into tar with xargs or the "-T"
1047 option. The verbose option can give a bunch of info, one line per
1048 file. I doubt anybody tries to parse its format, or if they do,
1049 they shouldn't. Unix tar is pretty random here anyway. */
1052 /* Width of "user/group size", with initial value chosen
1053 heuristically. This grows as needed, though this may cause some
1054 stairstepping in the output. Make it too small and the output will
1055 almost always look ragged. Make it too large and the output will
1056 be spaced out too far. */
1057 static int ugswidth
= 19;
1059 /* Width of printed time stamps. It grows if longer time stamps are
1060 found (typically, those with nanosecond resolution). Like
1061 USGWIDTH, some stairstepping may occur. */
1062 static int datewidth
= sizeof "YYYY-MM-DD HH:MM" - 1;
1064 static bool volume_label_printed
= false;
1067 simple_print_header (struct tar_stat_info
*st
, union block
*blk
,
1068 off_t block_ordinal
)
1071 char const *time_stamp
;
1075 /* These hold formatted ints. */
1076 char uform
[UINTMAX_STRSIZE_BOUND
], gform
[UINTMAX_STRSIZE_BOUND
];
1078 char size
[2 * UINTMAX_STRSIZE_BOUND
];
1079 /* holds formatted size or major,minor */
1080 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
1084 if (show_transformed_names_option
)
1085 temp_name
= st
->file_name
? st
->file_name
: st
->orig_file_name
;
1087 temp_name
= st
->orig_file_name
? st
->orig_file_name
: st
->file_name
;
1089 if (block_number_option
)
1091 char buf
[UINTMAX_STRSIZE_BOUND
];
1092 if (block_ordinal
< 0)
1093 block_ordinal
= current_block_ordinal ();
1094 block_ordinal
-= recent_long_name_blocks
;
1095 block_ordinal
-= recent_long_link_blocks
;
1096 fprintf (stdlis
, _("block %s: "),
1097 STRINGIFY_BIGINT (block_ordinal
, buf
));
1100 if (verbose_option
<= 1)
1102 /* Just the fax, mam. */
1103 fprintf (stdlis
, "%s\n", quotearg (temp_name
));
1107 /* File type and modes. */
1110 switch (blk
->header
.typeflag
)
1112 case GNUTYPE_VOLHDR
:
1113 volume_label_printed
= true;
1117 case GNUTYPE_MULTIVOL
:
1121 case GNUTYPE_LONGNAME
:
1122 case GNUTYPE_LONGLINK
:
1124 ERROR ((0, 0, _("Unexpected long name header")));
1127 case GNUTYPE_SPARSE
:
1131 if (temp_name
[strlen (temp_name
) - 1] == '/')
1137 case GNUTYPE_DUMPDIR
:
1160 pax_decode_mode (st
->stat
.st_mode
, modes
+ 1);
1162 /* extended attributes: GNU `ls -l'-like preview */
1163 xattrs_print_char (st
, modes
+ 10);
1167 time_stamp
= tartime (st
->mtime
, full_time_option
);
1168 time_stamp_len
= strlen (time_stamp
);
1169 if (datewidth
< time_stamp_len
)
1170 datewidth
= time_stamp_len
;
1172 /* User and group names. */
1176 && current_format
!= V7_FORMAT
1177 && !numeric_owner_option
)
1181 /* Try parsing it as an unsigned integer first, and as a
1182 uid_t if that fails. This method can list positive user
1183 ids that are too large to fit in a uid_t. */
1184 uintmax_t u
= from_header (blk
->header
.uid
,
1185 sizeof blk
->header
.uid
, 0,
1187 (uintmax_t) TYPE_MAXIMUM (uintmax_t),
1190 user
= STRINGIFY_BIGINT (u
, uform
);
1193 sprintf (uform
, "%ld",
1194 (long) UID_FROM_HEADER (blk
->header
.uid
));
1201 && current_format
!= V7_FORMAT
1202 && !numeric_owner_option
)
1206 /* Try parsing it as an unsigned integer first, and as a
1207 gid_t if that fails. This method can list positive group
1208 ids that are too large to fit in a gid_t. */
1209 uintmax_t g
= from_header (blk
->header
.gid
,
1210 sizeof blk
->header
.gid
, 0,
1212 (uintmax_t) TYPE_MAXIMUM (uintmax_t),
1215 group
= STRINGIFY_BIGINT (g
, gform
);
1218 sprintf (gform
, "%ld",
1219 (long) GID_FROM_HEADER (blk
->header
.gid
));
1224 /* Format the file size or major/minor device numbers. */
1226 switch (blk
->header
.typeflag
)
1231 STRINGIFY_BIGINT (major (st
->stat
.st_rdev
), uintbuf
));
1234 STRINGIFY_BIGINT (minor (st
->stat
.st_rdev
), uintbuf
));
1238 /* st->stat.st_size keeps stored file size */
1239 strcpy (size
, STRINGIFY_BIGINT (st
->stat
.st_size
, uintbuf
));
1243 /* Figure out padding and print the whole line. */
1245 sizelen
= strlen (size
);
1246 pad
= strlen (user
) + 1 + strlen (group
) + 1 + sizelen
;
1250 fprintf (stdlis
, "%s %s/%s %*s %-*s",
1251 modes
, user
, group
, ugswidth
- pad
+ sizelen
, size
,
1252 datewidth
, time_stamp
);
1254 fprintf (stdlis
, " %s", quotearg (temp_name
));
1256 switch (blk
->header
.typeflag
)
1259 fprintf (stdlis
, " -> %s\n", quotearg (st
->link_name
));
1263 fprintf (stdlis
, _(" link to %s\n"), quotearg (st
->link_name
));
1268 char type_string
[2];
1269 type_string
[0] = blk
->header
.typeflag
;
1270 type_string
[1] = '\0';
1271 fprintf (stdlis
, _(" unknown file type %s\n"),
1272 quote (type_string
));
1278 case GNUTYPE_SPARSE
:
1284 case GNUTYPE_DUMPDIR
:
1285 putc ('\n', stdlis
);
1288 case GNUTYPE_LONGLINK
:
1289 fprintf (stdlis
, _("--Long Link--\n"));
1292 case GNUTYPE_LONGNAME
:
1293 fprintf (stdlis
, _("--Long Name--\n"));
1296 case GNUTYPE_VOLHDR
:
1297 fprintf (stdlis
, _("--Volume Header--\n"));
1300 case GNUTYPE_MULTIVOL
:
1303 (UINTMAX_FROM_HEADER (blk
->oldgnu_header
.offset
),
1305 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
1315 print_volume_label (void)
1317 struct tar_stat_info vstat
;
1319 enum archive_format dummy
;
1321 memset (&vblk
, 0, sizeof (vblk
));
1322 vblk
.header
.typeflag
= GNUTYPE_VOLHDR
;
1323 if (recent_global_header
)
1324 memcpy (vblk
.header
.mtime
, recent_global_header
->header
.mtime
,
1325 sizeof vblk
.header
.mtime
);
1326 tar_stat_init (&vstat
);
1327 assign_string (&vstat
.file_name
, ".");
1328 decode_header (&vblk
, &vstat
, &dummy
, 0);
1329 assign_string (&vstat
.file_name
, volume_label
);
1330 simple_print_header (&vstat
, &vblk
, 0);
1331 tar_stat_destroy (&vstat
);
1335 print_header (struct tar_stat_info
*st
, union block
*blk
,
1336 off_t block_ordinal
)
1338 if (current_format
== POSIX_FORMAT
&& !volume_label_printed
&& volume_label
)
1340 print_volume_label ();
1341 volume_label_printed
= true;
1344 simple_print_header (st
, blk
, block_ordinal
);
1347 /* Print a similar line when we make a directory automatically. */
1349 print_for_mkdir (char *dirname
, int length
, mode_t mode
)
1353 if (verbose_option
> 1)
1355 /* File type and modes. */
1358 pax_decode_mode (mode
, modes
+ 1);
1360 if (block_number_option
)
1362 char buf
[UINTMAX_STRSIZE_BOUND
];
1363 fprintf (stdlis
, _("block %s: "),
1364 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
1367 fprintf (stdlis
, "%s %*s %s\n", modes
, ugswidth
+ 1 + datewidth
,
1368 _("Creating directory:"), quotearg (dirname
));
1372 /* Skip over SIZE bytes of data in blocks in the archive. */
1374 skip_file (off_t size
)
1378 /* FIXME: Make sure mv_begin_read is always called before it */
1380 if (seekable_archive
)
1382 off_t nblk
= seek_archive (size
);
1384 size
-= nblk
* BLOCKSIZE
;
1386 seekable_archive
= false;
1389 mv_size_left (size
);
1393 x
= find_next_block ();
1395 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1397 set_next_block_after (x
);
1399 mv_size_left (size
);
1403 /* Skip the current member in the archive.
1404 NOTE: Current header must be decoded before calling this function. */
1408 if (!current_stat_info
.skipped
)
1410 char save_typeflag
= current_header
->header
.typeflag
;
1411 set_next_block_after (current_header
);
1413 mv_begin_read (¤t_stat_info
);
1415 if (current_stat_info
.is_sparse
)
1416 sparse_skip_file (¤t_stat_info
);
1417 else if (save_typeflag
!= DIRTYPE
)
1418 skip_file (current_stat_info
.stat
.st_size
);
1425 test_archive_label (void)
1430 open_archive (ACCESS_READ
);
1431 if (read_header (¤t_header
, ¤t_stat_info
, read_header_auto
)
1434 decode_header (current_header
,
1435 ¤t_stat_info
, ¤t_format
, 0);
1436 if (current_header
->header
.typeflag
== GNUTYPE_VOLHDR
)
1437 assign_string (&volume_label
, current_header
->header
.name
);
1442 print_volume_label ();
1443 if (!name_match (volume_label
) && multi_volume_option
)
1445 char *s
= drop_volume_label_suffix (volume_label
);