1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright 1988-2023 Free Software Foundation, Inc.
5 This file is part of GNU tar.
7 GNU tar is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU tar is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Written by John Gilmore, on 1985-08-26. */
28 union block
*current_header
; /* points to current archive header */
29 enum archive_format current_format
; /* recognized format */
30 union block
*recent_long_name
; /* recent long name header and contents */
31 union block
*recent_long_link
; /* likewise, for long link */
32 size_t recent_long_name_blocks
; /* number of blocks in recent_long_name */
33 size_t recent_long_link_blocks
; /* likewise, for long link */
34 static union block
*recent_global_header
; /* Recent global header block */
36 #define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where))
37 #define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where))
38 #define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where))
39 #define MODE_FROM_HEADER(where, hbits) \
40 mode_from_header (where, sizeof (where), hbits)
41 #define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where))
42 #define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where))
44 static gid_t
gid_from_header (const char *buf
, size_t size
);
45 static major_t
major_from_header (const char *buf
, size_t size
);
46 static minor_t
minor_from_header (const char *buf
, size_t size
);
47 static mode_t
mode_from_header (const char *buf
, size_t size
, bool *hbits
);
48 static time_t time_from_header (const char *buf
, size_t size
);
49 static uid_t
uid_from_header (const char *buf
, size_t size
);
50 static intmax_t from_header (const char *, size_t, const char *,
51 intmax_t, uintmax_t, bool, bool);
53 /* Base 64 digits; see Internet RFC 2045 Table 1. */
54 static char const base_64_digits
[64] =
56 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
57 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
58 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
59 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
60 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
63 /* Table of base-64 digit values indexed by unsigned chars.
64 The value is 64 for unsigned chars that are not base-64 digits. */
65 static char base64_map
[UCHAR_MAX
+ 1];
71 memset (base64_map
, 64, sizeof base64_map
);
72 for (i
= 0; i
< 64; i
++)
73 base64_map
[(int) base_64_digits
[i
]] = i
;
77 decode_xform (char *file_name
, void *data
)
79 int type
= *(int*)data
;
84 /* FIXME: It is not quite clear how and to which extent are the symbolic
85 links subject to filename transformation. In the absence of another
86 solution, symbolic links are exempt from component stripping and
87 name suffix normalization, but subject to filename transformation
92 file_name
= safer_name_suffix (file_name
, true, absolute_names_option
);
96 file_name
= safer_name_suffix (file_name
, false, absolute_names_option
);
100 if (strip_name_components
)
102 size_t prefix_len
= stripped_prefix_len (file_name
,
103 strip_name_components
);
104 if (prefix_len
== (size_t) -1)
105 prefix_len
= strlen (file_name
);
106 file_name
+= prefix_len
;
112 transform_member_name (char **pinput
, int type
)
114 return transform_name_fp (pinput
, type
, decode_xform
, &type
);
118 enforce_one_top_level (char **pfile_name
)
120 char *file_name
= *pfile_name
;
123 for (p
= file_name
; *p
&& (ISSLASH (*p
) || *p
== '.'); p
++)
128 int pos
= strlen (one_top_level_dir
);
129 if (strncmp (p
, one_top_level_dir
, pos
) == 0)
131 if (ISSLASH (p
[pos
]) || p
[pos
] == 0)
135 *pfile_name
= make_file_name (one_top_level_dir
, file_name
);
136 normalize_filename_x (*pfile_name
);
139 *pfile_name
= xstrdup (one_top_level_dir
);
144 transform_stat_info (int typeflag
, struct tar_stat_info
*stat_info
)
146 if (typeflag
== GNUTYPE_VOLHDR
)
147 /* Name transformations don't apply to volume headers. */
150 transform_member_name (&stat_info
->file_name
, XFORM_REGFILE
);
154 transform_member_name (&stat_info
->link_name
, XFORM_SYMLINK
);
158 transform_member_name (&stat_info
->link_name
, XFORM_LINK
);
161 if (one_top_level_option
)
162 enforce_one_top_level (¤t_stat_info
.file_name
);
165 /* Main loop for reading an archive. */
167 read_and (void (*do_something
) (void))
169 enum read_header status
= HEADER_STILL_UNREAD
;
170 enum read_header prev_status
;
171 struct timespec mtime
;
176 open_archive (ACCESS_READ
);
179 prev_status
= status
;
180 tar_stat_destroy (¤t_stat_info
);
182 status
= read_header (¤t_header
, ¤t_stat_info
,
186 case HEADER_STILL_UNREAD
:
187 case HEADER_SUCCESS_EXTENDED
:
192 /* Valid header. We should decode next field (mode) first.
193 Ensure incoming names are null terminated. */
194 decode_header (current_header
, ¤t_stat_info
,
196 if (! name_match (current_stat_info
.file_name
)
197 || (TIME_OPTION_INITIALIZED (newer_mtime_option
)
198 /* FIXME: We get mtime now, and again later; this causes
199 duplicate diagnostics if header.mtime is bogus. */
201 = TIME_FROM_HEADER (current_header
->header
.mtime
)),
202 /* FIXME: Grab fractional time stamps from
205 current_stat_info
.mtime
= mtime
,
206 OLDER_TAR_STAT_TIME (current_stat_info
, m
)))
207 || excluded_name (current_stat_info
.file_name
,
208 current_stat_info
.parent
))
210 switch (current_header
->header
.typeflag
)
213 case GNUTYPE_MULTIVOL
:
217 if (show_omitted_dirs_option
)
218 WARN ((0, 0, _("%s: Omitting"),
219 quotearg_colon (current_stat_info
.file_name
)));
227 transform_stat_info (current_header
->header
.typeflag
,
232 case HEADER_ZERO_BLOCK
:
233 if (block_number_option
)
235 char buf
[UINTMAX_STRSIZE_BOUND
];
236 fprintf (stdlis
, _("block %s: ** Block of NULs **\n"),
237 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
240 set_next_block_after (current_header
);
242 if (!ignore_zeros_option
)
244 char buf
[UINTMAX_STRSIZE_BOUND
];
246 status
= read_header (¤t_header
, ¤t_stat_info
,
248 if (status
== HEADER_ZERO_BLOCK
)
250 WARNOPT (WARN_ALONE_ZERO_BLOCK
,
251 (0, 0, _("A lone zero block at %s"),
252 STRINGIFY_BIGINT (current_block_ordinal (), buf
)));
255 status
= prev_status
;
258 case HEADER_END_OF_FILE
:
259 if (!ignore_zeros_option
)
261 char buf
[UINTMAX_STRSIZE_BOUND
];
262 WARNOPT (WARN_MISSING_ZERO_BLOCKS
,
263 (0, 0, _("Terminating zero blocks missing at %s"),
264 STRINGIFY_BIGINT (current_block_ordinal (), buf
)));
266 if (block_number_option
)
268 char buf
[UINTMAX_STRSIZE_BOUND
];
269 fprintf (stdlis
, _("block %s: ** End of File **\n"),
270 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
275 /* If the previous header was good, tell them that we are
276 skipping bad ones. */
277 set_next_block_after (current_header
);
280 case HEADER_STILL_UNREAD
:
281 ERROR ((0, 0, _("This does not look like a tar archive")));
283 case HEADER_ZERO_BLOCK
:
285 if (block_number_option
)
287 char buf
[UINTMAX_STRSIZE_BOUND
];
288 off_t block_ordinal
= current_block_ordinal ();
289 block_ordinal
-= recent_long_name_blocks
;
290 block_ordinal
-= recent_long_link_blocks
;
291 fprintf (stdlis
, _("block %s: "),
292 STRINGIFY_BIGINT (block_ordinal
, buf
));
294 ERROR ((0, 0, _("Skipping to next header")));
297 case HEADER_END_OF_FILE
:
299 /* We are in the middle of a cascade of errors. */
302 case HEADER_SUCCESS_EXTENDED
:
309 while (!all_names_found (¤t_stat_info
));
312 names_notfound (); /* print names not found */
315 /* Print a header block, based on tar options. */
319 off_t block_ordinal
= current_block_ordinal ();
321 /* Print the header block. */
323 print_header (¤t_stat_info
, current_header
, block_ordinal
);
325 if (incremental_option
)
327 if (verbose_option
> 2)
329 if (is_dumpdir (¤t_stat_info
))
330 list_dumpdir (current_stat_info
.dumpdir
,
331 dumpdir_size (current_stat_info
.dumpdir
));
338 /* Check header checksum */
339 /* The standard BSD tar sources create the checksum by adding up the
340 bytes in the header as type char. I think the type char was unsigned
341 on the PDP-11, but it's signed on the Next and Sun. It looks like the
342 sources to BSD tar were never changed to compute the checksum
343 correctly, so both the Sun and Next add the bytes of the header as
344 signed chars. This doesn't cause a problem until you get a file with
345 a name containing characters with the high bit set. So tar_checksum
346 computes two checksums -- signed and unsigned. */
349 tar_checksum (union block
*header
, bool silent
)
352 int unsigned_sum
= 0; /* the POSIX one :-) */
353 int signed_sum
= 0; /* the Sun one :-( */
359 for (i
= sizeof *header
; i
-- != 0;)
361 unsigned_sum
+= (unsigned char) *p
;
362 signed_sum
+= (signed char) (*p
++);
365 if (unsigned_sum
== 0)
366 return HEADER_ZERO_BLOCK
;
368 /* Adjust checksum to count the "chksum" field as blanks. */
370 for (i
= sizeof header
->header
.chksum
; i
-- != 0;)
372 unsigned_sum
-= (unsigned char) header
->header
.chksum
[i
];
373 signed_sum
-= (signed char) (header
->header
.chksum
[i
]);
375 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
376 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
378 parsed_sum
= from_header (header
->header
.chksum
,
379 sizeof header
->header
.chksum
, 0,
380 0, INT_MAX
, true, silent
);
382 return HEADER_FAILURE
;
384 recorded_sum
= parsed_sum
;
386 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
387 return HEADER_FAILURE
;
389 return HEADER_SUCCESS
;
392 /* Read a block that's supposed to be a header block. Return its
393 address in *RETURN_BLOCK, and if it is good, the file's size
394 and names (file name, link name) in *INFO.
396 Return one of enum read_header describing the status of the
399 The MODE parameter instructs read_header what to do with special
400 header blocks, i.e.: extended POSIX, GNU long name or long link,
403 read_header_auto process them automatically,
404 read_header_x_raw when a special header is read, return
405 HEADER_SUCCESS_EXTENDED without actually
406 processing the header,
407 read_header_x_global when a POSIX global header is read,
408 decode it and return HEADER_SUCCESS_EXTENDED.
410 You must always set_next_block_after(*return_block) to skip past
411 the header which this routine reads. */
414 read_header (union block
**return_block
, struct tar_stat_info
*info
,
415 enum read_header_mode mode
)
419 union block
*data_block
;
420 size_t size
, written
;
421 union block
*next_long_name
= NULL
;
422 union block
*next_long_link
= NULL
;
423 size_t next_long_name_blocks
= 0;
424 size_t next_long_link_blocks
= 0;
425 enum read_header status
= HEADER_SUCCESS
;
429 header
= find_next_block ();
430 *return_block
= header
;
433 status
= HEADER_END_OF_FILE
;
437 if ((status
= tar_checksum (header
, false)) != HEADER_SUCCESS
)
440 /* Good block. Decode file size and return. */
442 if (header
->header
.typeflag
== LNKTYPE
)
443 info
->stat
.st_size
= 0; /* links 0 size on tape */
446 info
->stat
.st_size
= OFF_FROM_HEADER (header
->header
.size
);
447 if (info
->stat
.st_size
< 0)
449 status
= HEADER_FAILURE
;
454 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
455 || header
->header
.typeflag
== GNUTYPE_LONGLINK
456 || header
->header
.typeflag
== XHDTYPE
457 || header
->header
.typeflag
== XGLTYPE
458 || header
->header
.typeflag
== SOLARIS_XHDTYPE
)
460 if (mode
== read_header_x_raw
)
462 status
= HEADER_SUCCESS_EXTENDED
;
465 else if (header
->header
.typeflag
== GNUTYPE_LONGNAME
466 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
468 union block
*header_copy
;
469 size_t name_size
= info
->stat
.st_size
;
470 size_t n
= name_size
% BLOCKSIZE
;
471 size
= name_size
+ BLOCKSIZE
;
473 size
+= BLOCKSIZE
- n
;
475 if (name_size
!= info
->stat
.st_size
|| size
< name_size
)
478 header_copy
= xmalloc (size
+ 1);
480 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
)
482 free (next_long_name
);
483 next_long_name
= header_copy
;
484 next_long_name_blocks
= size
/ BLOCKSIZE
;
488 free (next_long_link
);
489 next_long_link
= header_copy
;
490 next_long_link_blocks
= size
/ BLOCKSIZE
;
493 set_next_block_after (header
);
494 *header_copy
= *header
;
495 bp
= header_copy
->buffer
+ BLOCKSIZE
;
497 for (size
-= BLOCKSIZE
; size
> 0; size
-= written
)
499 data_block
= find_next_block ();
502 ERROR ((0, 0, _("Unexpected EOF in archive")));
505 written
= available_space_after (data_block
);
509 memcpy (bp
, data_block
->buffer
, written
);
511 set_next_block_after ((union block
*)
512 (data_block
->buffer
+ written
- 1));
517 else if (header
->header
.typeflag
== XHDTYPE
518 || header
->header
.typeflag
== SOLARIS_XHDTYPE
)
519 xheader_read (&info
->xhdr
, header
,
520 OFF_FROM_HEADER (header
->header
.size
));
521 else if (header
->header
.typeflag
== XGLTYPE
)
525 if (!recent_global_header
)
526 recent_global_header
= xmalloc (sizeof *recent_global_header
);
527 memcpy (recent_global_header
, header
,
528 sizeof *recent_global_header
);
529 memset (&xhdr
, 0, sizeof xhdr
);
530 xheader_read (&xhdr
, header
,
531 OFF_FROM_HEADER (header
->header
.size
));
532 xheader_decode_global (&xhdr
);
533 xheader_destroy (&xhdr
);
534 if (mode
== read_header_x_global
)
536 status
= HEADER_SUCCESS_EXTENDED
;
547 struct posix_header
const *h
= &header
->header
;
548 char namebuf
[sizeof h
->prefix
+ 1 + NAME_FIELD_SIZE
+ 1];
550 free (recent_long_name
);
554 name
= next_long_name
->buffer
+ BLOCKSIZE
;
555 recent_long_name
= next_long_name
;
556 recent_long_name_blocks
= next_long_name_blocks
;
557 next_long_name
= NULL
;
561 /* Accept file names as specified by POSIX.1-1996
565 if (h
->prefix
[0] && strcmp (h
->magic
, TMAGIC
) == 0)
567 memcpy (np
, h
->prefix
, sizeof h
->prefix
);
568 np
[sizeof h
->prefix
] = '\0';
572 memcpy (np
, h
->name
, sizeof h
->name
);
573 np
[sizeof h
->name
] = '\0';
575 recent_long_name
= 0;
576 recent_long_name_blocks
= 0;
578 assign_string (&info
->orig_file_name
, name
);
579 assign_string (&info
->file_name
, name
);
580 info
->had_trailing_slash
= strip_trailing_slashes (info
->file_name
);
582 free (recent_long_link
);
586 name
= next_long_link
->buffer
+ BLOCKSIZE
;
587 recent_long_link
= next_long_link
;
588 recent_long_link_blocks
= next_long_link_blocks
;
589 next_long_link
= NULL
;
593 memcpy (namebuf
, h
->linkname
, sizeof h
->linkname
);
594 namebuf
[sizeof h
->linkname
] = '\0';
596 recent_long_link
= 0;
597 recent_long_link_blocks
= 0;
599 assign_string (&info
->link_name
, name
);
604 free (next_long_name
);
605 free (next_long_link
);
609 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
611 /* Decode things from a file HEADER block into STAT_INFO, also setting
612 *FORMAT_POINTER depending on the header block format. If
613 DO_USER_GROUP, decode the user/group information (this is useful
614 for extraction, but waste time when merely listing).
616 read_header() has already decoded the checksum and length, so we don't.
618 This routine should *not* be called twice for the same block, since
619 the two calls might use different DO_USER_GROUP values and thus
620 might end up with different uid/gid for the two calls. If anybody
621 wants the uid/gid they should decode it first, and other callers
622 should decode it without uid/gid before calling a routine,
623 e.g. print_header, that assumes decoded data. */
625 decode_header (union block
*header
, struct tar_stat_info
*stat_info
,
626 enum archive_format
*format_pointer
, int do_user_group
)
628 enum archive_format format
;
630 mode_t mode
= MODE_FROM_HEADER (header
->header
.mode
, &hbits
);
632 if (strcmp (header
->header
.magic
, TMAGIC
) == 0)
634 if (header
->star_header
.prefix
[130] == 0
635 && ISOCTAL (header
->star_header
.atime
[0])
636 && header
->star_header
.atime
[11] == ' '
637 && ISOCTAL (header
->star_header
.ctime
[0])
638 && header
->star_header
.ctime
[11] == ' ')
639 format
= STAR_FORMAT
;
640 else if (stat_info
->xhdr
.size
)
641 format
= POSIX_FORMAT
;
643 format
= USTAR_FORMAT
;
645 else if (strcmp (header
->buffer
+ offsetof (struct posix_header
, magic
),
648 format
= hbits
? OLDGNU_FORMAT
: GNU_FORMAT
;
651 *format_pointer
= format
;
653 stat_info
->stat
.st_mode
= mode
;
654 stat_info
->mtime
.tv_sec
= TIME_FROM_HEADER (header
->header
.mtime
);
655 stat_info
->mtime
.tv_nsec
= 0;
656 assign_string_n (&stat_info
->uname
,
657 header
->header
.uname
[0] ? header
->header
.uname
: NULL
,
658 sizeof (header
->header
.uname
));
659 assign_string_n (&stat_info
->gname
,
660 header
->header
.gname
[0] ? header
->header
.gname
: NULL
,
661 sizeof (header
->header
.gname
));
663 xheader_xattr_init (stat_info
);
665 if (format
== OLDGNU_FORMAT
&& incremental_option
)
667 stat_info
->atime
.tv_sec
= TIME_FROM_HEADER (header
->oldgnu_header
.atime
);
668 stat_info
->ctime
.tv_sec
= TIME_FROM_HEADER (header
->oldgnu_header
.ctime
);
669 stat_info
->atime
.tv_nsec
= stat_info
->ctime
.tv_nsec
= 0;
671 else if (format
== STAR_FORMAT
)
673 stat_info
->atime
.tv_sec
= TIME_FROM_HEADER (header
->star_header
.atime
);
674 stat_info
->ctime
.tv_sec
= TIME_FROM_HEADER (header
->star_header
.ctime
);
675 stat_info
->atime
.tv_nsec
= stat_info
->ctime
.tv_nsec
= 0;
678 stat_info
->atime
= stat_info
->ctime
= start_time
;
680 if (format
== V7_FORMAT
)
682 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
683 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
684 stat_info
->stat
.st_rdev
= 0;
690 /* FIXME: Decide if this should somewhat depend on -p. */
692 if (numeric_owner_option
693 || !*header
->header
.uname
694 || !uname_to_uid (header
->header
.uname
, &stat_info
->stat
.st_uid
))
695 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
697 if (numeric_owner_option
698 || !*header
->header
.gname
699 || !gname_to_gid (header
->header
.gname
, &stat_info
->stat
.st_gid
))
700 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
703 switch (header
->header
.typeflag
)
707 stat_info
->stat
.st_rdev
=
708 makedev (MAJOR_FROM_HEADER (header
->header
.devmajor
),
709 MINOR_FROM_HEADER (header
->header
.devminor
));
713 stat_info
->stat
.st_rdev
= 0;
717 xheader_decode (stat_info
);
719 if (sparse_member_p (stat_info
))
721 sparse_fixup_header (stat_info
);
722 stat_info
->is_sparse
= true;
726 stat_info
->is_sparse
= false;
727 if (((current_format
== GNU_FORMAT
728 || current_format
== OLDGNU_FORMAT
)
729 && current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
730 || stat_info
->dumpdir
)
731 stat_info
->is_dumpdir
= true;
736 /* Convert buffer at WHERE0 of size DIGS from external format to
737 intmax_t. DIGS must be positive. If TYPE is nonnull, the data are
738 of type TYPE. The buffer must represent a value in the range
739 MINVAL through MAXVAL; if the mathematically correct result V would
740 be greater than INTMAX_MAX, return a negative integer V such that
741 (uintmax_t) V yields the correct result. If OCTAL_ONLY, allow only octal
742 numbers instead of the other GNU extensions. Return -1 on error,
743 diagnosing the error if TYPE is nonnull and if !SILENT. */
744 #if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX)
745 # error "from_header internally represents intmax_t as uintmax_t + sign"
747 #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
748 # error "from_header returns intmax_t to represent uintmax_t"
751 from_header (char const *where0
, size_t digs
, char const *type
,
752 intmax_t minval
, uintmax_t maxval
,
753 bool octal_only
, bool silent
)
756 uintmax_t uminval
= minval
;
757 uintmax_t minus_minval
= - uminval
;
758 char const *where
= where0
;
759 char const *lim
= where
+ digs
;
760 bool negative
= false;
762 /* Accommodate buggy tar of unknown vintage, which outputs leading
763 NUL if the previous field overflows. */
766 /* Accommodate older tars, which output leading spaces. */
773 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
775 _("Blanks in header where numeric %s value expected"),
779 if (!isspace ((unsigned char) *where
))
785 if (ISODIGIT (*where
))
787 char const *where1
= where
;
788 bool overflow
= false;
792 value
+= *where
++ - '0';
793 if (where
== lim
|| ! ISODIGIT (*where
))
795 overflow
|= value
!= (value
<< LG_8
>> LG_8
);
799 /* Parse the output of older, unportable tars, which generate
800 negative values in two's complement octal. If the leading
801 nonzero digit is 1, we can't recover the original value
802 reliably; so do this only if the digit is 2 or more. This
803 catches the common case of 32-bit negative time stamps. */
804 if ((overflow
|| maxval
< value
) && '2' <= *where1
&& type
)
806 /* Compute the negative of the input value, assuming two's
808 int digit
= (*where1
- '0') | 4;
816 if (where
== lim
|| ! ISODIGIT (*where
))
818 digit
= *where
- '0';
819 overflow
|= value
!= (value
<< LG_8
>> LG_8
);
825 if (!overflow
&& value
<= minus_minval
)
829 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
830 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
831 (int) (where
- where1
), where1
, type
));
840 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
841 _("Archive octal value %.*s is out of %s range"),
842 (int) (where
- where1
), where1
, type
));
848 /* Suppress the following extensions. */
850 else if (*where
== '-' || *where
== '+')
852 /* Parse base-64 output produced only by tar test versions
853 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
854 Support for this will be withdrawn in future releases. */
858 static bool warned_once
;
862 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
865 negative
= *where
++ == '-';
867 && (dig
= base64_map
[(unsigned char) *where
]) < 64)
869 if (value
<< LG_64
>> LG_64
!= value
)
871 char *string
= alloca (digs
+ 1);
872 memcpy (string
, where0
, digs
);
876 _("Archive signed base-64 string %s is out of %s range"),
877 quote (string
), type
));
880 value
= (value
<< LG_64
) | dig
;
884 else if (where
<= lim
- 2
885 && (*where
== '\200' /* positive base-256 */
886 || *where
== '\377' /* negative base-256 */))
888 /* Parse base-256 output. A nonnegative number N is
889 represented as (256**DIGS)/2 + N; a negative number -N is
890 represented as (256**DIGS) - N, i.e. as two's complement.
891 The representation guarantees that the leading bit is
892 always on, so that we don't confuse this format with the
893 others (assuming ASCII bytes of 8 bits or more). */
894 int signbit
= *where
& (1 << (LG_256
- 2));
895 uintmax_t topbits
= (((uintmax_t) - signbit
)
896 << (CHAR_BIT
* sizeof (uintmax_t)
897 - LG_256
- (LG_256
- 2)));
898 value
= (*where
++ & ((1 << (LG_256
- 2)) - 1)) - signbit
;
901 value
= (value
<< LG_256
) + (unsigned char) *where
++;
904 if (((value
<< LG_256
>> LG_256
) | topbits
) != value
)
908 _("Archive base-256 value is out of %s range"),
913 negative
= signbit
!= 0;
918 if (where
!= lim
&& *where
&& !isspace ((unsigned char) *where
))
922 char buf
[1000]; /* Big enough to represent any header. */
923 static struct quoting_options
*o
;
927 o
= clone_quoting_options (0);
928 set_quoting_style (o
, locale_quoting_style
);
931 while (where0
!= lim
&& ! lim
[-1])
933 quotearg_buffer (buf
, sizeof buf
, where0
, lim
- where0
, o
);
936 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
937 _("Archive contains %.*s where numeric %s value expected"),
938 (int) sizeof buf
, buf
, type
));
944 if (value
<= (negative
? minus_minval
: maxval
))
945 return represent_uintmax (negative
? -value
: value
);
949 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
950 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
951 char value_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
952 char *minval_string
= STRINGIFY_BIGINT (minus_minval
, minval_buf
+ 1);
953 char *value_string
= STRINGIFY_BIGINT (value
, value_buf
+ 1);
955 *--value_string
= '-';
957 *--minval_string
= '-';
958 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
959 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
961 minval_string
, STRINGIFY_BIGINT (maxval
, maxval_buf
)));
968 gid_from_header (const char *p
, size_t s
)
970 return from_header (p
, s
, "gid_t",
971 TYPE_MINIMUM (gid_t
), TYPE_MAXIMUM (gid_t
),
976 major_from_header (const char *p
, size_t s
)
978 return from_header (p
, s
, "major_t",
979 TYPE_MINIMUM (major_t
), TYPE_MAXIMUM (major_t
),
984 minor_from_header (const char *p
, size_t s
)
986 return from_header (p
, s
, "minor_t",
987 TYPE_MINIMUM (minor_t
), TYPE_MAXIMUM (minor_t
),
991 /* Convert P to the file mode, as understood by tar.
992 Set *HBITS if there are any unrecognized bits. */
994 mode_from_header (const char *p
, size_t s
, bool *hbits
)
996 intmax_t u
= from_header (p
, s
, "mode_t",
997 INTMAX_MIN
, UINTMAX_MAX
,
999 mode_t mode
= ((u
& TSUID
? S_ISUID
: 0)
1000 | (u
& TSGID
? S_ISGID
: 0)
1001 | (u
& TSVTX
? S_ISVTX
: 0)
1002 | (u
& TUREAD
? S_IRUSR
: 0)
1003 | (u
& TUWRITE
? S_IWUSR
: 0)
1004 | (u
& TUEXEC
? S_IXUSR
: 0)
1005 | (u
& TGREAD
? S_IRGRP
: 0)
1006 | (u
& TGWRITE
? S_IWGRP
: 0)
1007 | (u
& TGEXEC
? S_IXGRP
: 0)
1008 | (u
& TOREAD
? S_IROTH
: 0)
1009 | (u
& TOWRITE
? S_IWOTH
: 0)
1010 | (u
& TOEXEC
? S_IXOTH
: 0));
1011 *hbits
= (u
& ~07777) != 0;
1016 off_from_header (const char *p
, size_t s
)
1018 /* Negative offsets are not allowed in tar files, so invoke
1019 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
1020 return from_header (p
, s
, "off_t",
1021 0, TYPE_MAXIMUM (off_t
),
1026 time_from_header (const char *p
, size_t s
)
1028 return from_header (p
, s
, "time_t",
1029 TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t),
1034 uid_from_header (const char *p
, size_t s
)
1036 return from_header (p
, s
, "uid_t",
1037 TYPE_MINIMUM (uid_t
), TYPE_MAXIMUM (uid_t
),
1042 uintmax_from_header (const char *p
, size_t s
)
1044 return from_header (p
, s
, "uintmax_t", 0, UINTMAX_MAX
, false, false);
1048 /* Return a printable representation of T. The result points to
1049 static storage that can be reused in the next call to this
1050 function, to ctime, or to asctime. If FULL_TIME, then output the
1051 time stamp to its full resolution; otherwise, just output it to
1052 1-minute resolution. */
1054 tartime (struct timespec t
, bool full_time
)
1056 enum { fraclen
= sizeof ".FFFFFFFFF" - 1 };
1057 static char buffer
[max (UINTMAX_STRSIZE_BOUND
+ 1,
1058 INT_STRLEN_BOUND (int) + 16)
1061 time_t s
= t
.tv_sec
;
1063 bool negative
= s
< 0;
1066 if (negative
&& ns
!= 0)
1069 ns
= 1000000000 - ns
;
1072 tm
= utc_option
? gmtime (&s
) : localtime (&s
);
1077 strftime (buffer
, sizeof buffer
, "%Y-%m-%d %H:%M:%S", tm
);
1078 code_ns_fraction (ns
, buffer
+ strlen (buffer
));
1081 strftime (buffer
, sizeof buffer
, "%Y-%m-%d %H:%M", tm
);
1085 /* The time stamp cannot be broken down, most likely because it
1086 is out of range. Convert it as an integer,
1087 right-adjusted in a field with the same width as the usual
1088 4-year ISO time format. */
1089 p
= umaxtostr (negative
? - (uintmax_t) s
: s
,
1090 buffer
+ sizeof buffer
- UINTMAX_STRSIZE_BOUND
- fraclen
);
1093 while ((buffer
+ sizeof buffer
- sizeof "YYYY-MM-DD HH:MM"
1094 + (full_time
? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1098 code_ns_fraction (ns
, buffer
+ sizeof buffer
- 1 - fraclen
);
1102 /* Actually print it.
1104 Plain and fancy file header block logging. Non-verbose just prints
1105 the name, e.g. for "tar t" or "tar x". This should just contain
1106 file names, so it can be fed back into tar with xargs or the "-T"
1107 option. The verbose option can give a bunch of info, one line per
1108 file. I doubt anybody tries to parse its format, or if they do,
1109 they shouldn't. Unix tar is pretty random here anyway. */
1112 /* Width of "user/group size", with initial value chosen
1113 heuristically. This grows as needed, though this may cause some
1114 stairstepping in the output. Make it too small and the output will
1115 almost always look ragged. Make it too large and the output will
1116 be spaced out too far. */
1117 static int ugswidth
= 19;
1119 /* Width of printed time stamps. It grows if longer time stamps are
1120 found (typically, those with nanosecond resolution). Like
1121 USGWIDTH, some stairstepping may occur. */
1122 static int datewidth
= sizeof "YYYY-MM-DD HH:MM" - 1;
1124 static bool volume_label_printed
= false;
1127 simple_print_header (struct tar_stat_info
*st
, union block
*blk
,
1128 off_t block_ordinal
)
1131 char const *time_stamp
;
1135 /* These hold formatted ints. */
1136 char uform
[max (INT_BUFSIZE_BOUND (intmax_t), UINTMAX_STRSIZE_BOUND
)];
1137 char gform
[sizeof uform
];
1139 char size
[2 * UINTMAX_STRSIZE_BOUND
];
1140 /* holds formatted size or major,minor */
1141 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
1145 if (show_transformed_names_option
)
1146 temp_name
= st
->file_name
? st
->file_name
: st
->orig_file_name
;
1148 temp_name
= st
->orig_file_name
? st
->orig_file_name
: st
->file_name
;
1150 if (block_number_option
)
1152 char buf
[UINTMAX_STRSIZE_BOUND
];
1153 if (block_ordinal
< 0)
1154 block_ordinal
= current_block_ordinal ();
1155 block_ordinal
-= recent_long_name_blocks
;
1156 block_ordinal
-= recent_long_link_blocks
;
1157 fprintf (stdlis
, _("block %s: "),
1158 STRINGIFY_BIGINT (block_ordinal
, buf
));
1161 if (verbose_option
<= 1)
1163 /* Just the fax, mam. */
1164 fputs (quotearg (temp_name
), stdlis
);
1165 if (show_transformed_names_option
&& st
->had_trailing_slash
)
1166 fputc ('/', stdlis
);
1167 fputc ('\n', stdlis
);
1171 /* File type and modes. */
1174 switch (blk
->header
.typeflag
)
1176 case GNUTYPE_VOLHDR
:
1177 volume_label_printed
= true;
1181 case GNUTYPE_MULTIVOL
:
1185 case GNUTYPE_LONGNAME
:
1186 case GNUTYPE_LONGLINK
:
1188 ERROR ((0, 0, _("Unexpected long name header")));
1191 case GNUTYPE_SPARSE
:
1194 modes
[0] = st
->had_trailing_slash
? 'd' : '-';
1199 case GNUTYPE_DUMPDIR
:
1222 pax_decode_mode (st
->stat
.st_mode
, modes
+ 1);
1224 /* extended attributes: GNU `ls -l'-like preview */
1225 xattrs_print_char (st
, modes
+ 10);
1229 time_stamp
= tartime (st
->mtime
, full_time_option
);
1230 time_stamp_len
= strlen (time_stamp
);
1231 if (datewidth
< time_stamp_len
)
1232 datewidth
= time_stamp_len
;
1234 /* User and group names. */
1238 && current_format
!= V7_FORMAT
1239 && !numeric_owner_option
)
1242 user
= STRINGIFY_BIGINT (st
->stat
.st_uid
, uform
);
1246 && current_format
!= V7_FORMAT
1247 && !numeric_owner_option
)
1250 group
= STRINGIFY_BIGINT (st
->stat
.st_gid
, gform
);
1252 /* Format the file size or major/minor device numbers. */
1254 switch (blk
->header
.typeflag
)
1259 STRINGIFY_BIGINT (major (st
->stat
.st_rdev
), uintbuf
));
1262 STRINGIFY_BIGINT (minor (st
->stat
.st_rdev
), uintbuf
));
1266 /* st->stat.st_size keeps stored file size */
1267 strcpy (size
, STRINGIFY_BIGINT (st
->stat
.st_size
, uintbuf
));
1271 /* Figure out padding and print the whole line. */
1273 sizelen
= strlen (size
);
1274 pad
= strlen (user
) + 1 + strlen (group
) + 1 + sizelen
;
1278 fprintf (stdlis
, "%s %s/%s %*s %-*s",
1279 modes
, user
, group
, ugswidth
- pad
+ sizelen
, size
,
1280 datewidth
, time_stamp
);
1282 fprintf (stdlis
, " %s", quotearg (temp_name
));
1283 if (show_transformed_names_option
&& st
->had_trailing_slash
)
1284 fputc ('/', stdlis
);
1286 switch (blk
->header
.typeflag
)
1289 fprintf (stdlis
, " -> %s\n", quotearg (st
->link_name
));
1293 fprintf (stdlis
, _(" link to %s\n"), quotearg (st
->link_name
));
1298 char type_string
[2];
1299 type_string
[0] = blk
->header
.typeflag
;
1300 type_string
[1] = '\0';
1301 fprintf (stdlis
, _(" unknown file type %s\n"),
1302 quote (type_string
));
1308 case GNUTYPE_SPARSE
:
1314 case GNUTYPE_DUMPDIR
:
1315 putc ('\n', stdlis
);
1318 case GNUTYPE_LONGLINK
:
1319 fprintf (stdlis
, _("--Long Link--\n"));
1322 case GNUTYPE_LONGNAME
:
1323 fprintf (stdlis
, _("--Long Name--\n"));
1326 case GNUTYPE_VOLHDR
:
1327 fprintf (stdlis
, _("--Volume Header--\n"));
1330 case GNUTYPE_MULTIVOL
:
1333 (UINTMAX_FROM_HEADER (blk
->oldgnu_header
.offset
),
1335 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
1345 print_volume_label (void)
1347 struct tar_stat_info vstat
;
1349 enum archive_format dummy
;
1351 memset (&vblk
, 0, sizeof (vblk
));
1352 vblk
.header
.typeflag
= GNUTYPE_VOLHDR
;
1353 if (recent_global_header
)
1354 memcpy (vblk
.header
.mtime
, recent_global_header
->header
.mtime
,
1355 sizeof vblk
.header
.mtime
);
1356 tar_stat_init (&vstat
);
1357 assign_string (&vstat
.file_name
, ".");
1358 decode_header (&vblk
, &vstat
, &dummy
, 0);
1359 assign_string (&vstat
.file_name
, volume_label
);
1360 simple_print_header (&vstat
, &vblk
, 0);
1361 tar_stat_destroy (&vstat
);
1365 print_header (struct tar_stat_info
*st
, union block
*blk
,
1366 off_t block_ordinal
)
1368 if (current_format
== POSIX_FORMAT
&& !volume_label_printed
&& volume_label
)
1370 print_volume_label ();
1371 volume_label_printed
= true;
1374 simple_print_header (st
, blk
, block_ordinal
);
1377 /* Print a similar line when we make a directory automatically. */
1379 print_for_mkdir (char *dirname
, int length
, mode_t mode
)
1383 if (verbose_option
> 1)
1385 /* File type and modes. */
1388 pax_decode_mode (mode
, modes
+ 1);
1390 if (block_number_option
)
1392 char buf
[UINTMAX_STRSIZE_BOUND
];
1393 fprintf (stdlis
, _("block %s: "),
1394 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
1397 fprintf (stdlis
, "%s %*s %s\n", modes
, ugswidth
+ 1 + datewidth
,
1398 _("Creating directory:"), quotearg (dirname
));
1402 /* Skip over SIZE bytes of data in blocks in the archive.
1403 This may involve copying the data.
1404 If MUST_COPY, always copy instead of skipping. */
1406 skim_file (off_t size
, bool must_copy
)
1410 /* FIXME: Make sure mv_begin_read is always called before it */
1412 if (seekable_archive
&& !must_copy
)
1414 off_t nblk
= seek_archive (size
);
1416 size
-= nblk
* BLOCKSIZE
;
1418 seekable_archive
= false;
1421 mv_size_left (size
);
1425 x
= find_next_block ();
1427 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1429 set_next_block_after (x
);
1431 mv_size_left (size
);
1435 /* Skip the current member in the archive.
1436 NOTE: Current header must be decoded before calling this function. */
1440 skim_member (false);
1443 /* Skip the current member in the archive.
1444 If MUST_COPY, always copy instead of skipping. */
1446 skim_member (bool must_copy
)
1448 if (!current_stat_info
.skipped
)
1450 char save_typeflag
= current_header
->header
.typeflag
;
1451 set_next_block_after (current_header
);
1453 mv_begin_read (¤t_stat_info
);
1455 if (current_stat_info
.is_sparse
)
1456 sparse_skim_file (¤t_stat_info
, must_copy
);
1457 else if (save_typeflag
!= DIRTYPE
)
1458 skim_file (current_stat_info
.stat
.st_size
, must_copy
);
1465 test_archive_label (void)
1470 open_archive (ACCESS_READ
);
1471 if (read_header (¤t_header
, ¤t_stat_info
, read_header_auto
)
1474 decode_header (current_header
,
1475 ¤t_stat_info
, ¤t_format
, 0);
1476 if (current_header
->header
.typeflag
== GNUTYPE_VOLHDR
)
1477 ASSIGN_STRING_N (&volume_label
, current_header
->header
.name
);
1482 print_volume_label ();
1483 if (!name_match (volume_label
) && multi_volume_option
)
1485 char *s
= drop_volume_label_suffix (volume_label
);