1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright 1988-2024 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. */
29 union block
*current_header
; /* points to current archive header */
30 enum archive_format current_format
; /* recognized format */
31 union block
*recent_long_name
; /* recent long name header and contents */
32 union block
*recent_long_link
; /* likewise, for long link */
33 size_t recent_long_name_blocks
; /* number of blocks in recent_long_name */
34 size_t recent_long_link_blocks
; /* likewise, for long link */
35 static union block
*recent_global_header
; /* Recent global header block */
37 #define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where))
38 #define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where))
39 #define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where))
40 #define MODE_FROM_HEADER(where, hbits) \
41 mode_from_header (where, sizeof (where), hbits)
42 #define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where))
43 #define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where))
45 static gid_t
gid_from_header (const char *buf
, size_t size
);
46 static major_t
major_from_header (const char *buf
, size_t size
);
47 static minor_t
minor_from_header (const char *buf
, size_t size
);
48 static mode_t
mode_from_header (const char *buf
, size_t size
, bool *hbits
);
49 static time_t time_from_header (const char *buf
, size_t size
);
50 static uid_t
uid_from_header (const char *buf
, size_t size
);
51 static intmax_t from_header (const char *, size_t, const char *,
52 intmax_t, uintmax_t, bool, bool);
54 /* Base 64 digits; see Internet RFC 2045 Table 1. */
55 static char const base_64_digits
[64] =
57 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
58 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
59 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
60 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
61 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
64 /* Table of base-64 digit values indexed by unsigned chars.
65 The value is 64 for unsigned chars that are not base-64 digits. */
66 static char base64_map
[UCHAR_MAX
+ 1];
72 memset (base64_map
, 64, sizeof base64_map
);
73 for (i
= 0; i
< 64; i
++)
74 base64_map
[(int) base_64_digits
[i
]] = i
;
78 decode_xform (char *file_name
, void *data
)
80 int type
= *(int*)data
;
85 /* FIXME: It is not quite clear how and to which extent are the symbolic
86 links subject to filename transformation. In the absence of another
87 solution, symbolic links are exempt from component stripping and
88 name suffix normalization, but subject to filename transformation
93 file_name
= safer_name_suffix (file_name
, true, absolute_names_option
);
97 file_name
= safer_name_suffix (file_name
, false, absolute_names_option
);
101 if (strip_name_components
)
103 size_t prefix_len
= stripped_prefix_len (file_name
,
104 strip_name_components
);
105 if (prefix_len
== (size_t) -1)
106 prefix_len
= strlen (file_name
);
107 file_name
+= prefix_len
;
113 transform_member_name (char **pinput
, int type
)
115 return transform_name_fp (pinput
, type
, decode_xform
, &type
);
119 enforce_one_top_level (char **pfile_name
)
121 char *file_name
= *pfile_name
;
124 for (p
= file_name
; *p
&& (ISSLASH (*p
) || *p
== '.'); p
++)
129 int pos
= strlen (one_top_level_dir
);
130 if (strncmp (p
, one_top_level_dir
, pos
) == 0)
132 if (ISSLASH (p
[pos
]) || p
[pos
] == 0)
136 *pfile_name
= make_file_name (one_top_level_dir
, file_name
);
137 normalize_filename_x (*pfile_name
);
140 *pfile_name
= xstrdup (one_top_level_dir
);
145 transform_stat_info (int typeflag
, struct tar_stat_info
*stat_info
)
147 if (typeflag
== GNUTYPE_VOLHDR
)
148 /* Name transformations don't apply to volume headers. */
151 transform_member_name (&stat_info
->file_name
, XFORM_REGFILE
);
155 transform_member_name (&stat_info
->link_name
, XFORM_SYMLINK
);
159 transform_member_name (&stat_info
->link_name
, XFORM_LINK
);
162 if (one_top_level_option
)
163 enforce_one_top_level (¤t_stat_info
.file_name
);
166 /* Main loop for reading an archive. */
168 read_and (void (*do_something
) (void))
170 enum read_header status
= HEADER_STILL_UNREAD
;
171 enum read_header prev_status
;
172 struct timespec mtime
;
177 open_archive (ACCESS_READ
);
180 prev_status
= status
;
181 tar_stat_destroy (¤t_stat_info
);
183 status
= read_header (¤t_header
, ¤t_stat_info
,
187 case HEADER_STILL_UNREAD
:
188 case HEADER_SUCCESS_EXTENDED
:
193 /* Valid header. We should decode next field (mode) first.
194 Ensure incoming names are null terminated. */
195 decode_header (current_header
, ¤t_stat_info
,
197 if (! name_match (current_stat_info
.file_name
)
198 || (TIME_OPTION_INITIALIZED (newer_mtime_option
)
199 /* FIXME: We get mtime now, and again later; this causes
200 duplicate diagnostics if header.mtime is bogus. */
202 = TIME_FROM_HEADER (current_header
->header
.mtime
)),
203 /* FIXME: Grab fractional time stamps from
206 current_stat_info
.mtime
= mtime
,
207 OLDER_TAR_STAT_TIME (current_stat_info
, m
)))
208 || excluded_name (current_stat_info
.file_name
,
209 current_stat_info
.parent
))
211 switch (current_header
->header
.typeflag
)
214 case GNUTYPE_MULTIVOL
:
218 if (show_omitted_dirs_option
)
219 WARN ((0, 0, _("%s: Omitting"),
220 quotearg_colon (current_stat_info
.file_name
)));
228 transform_stat_info (current_header
->header
.typeflag
,
233 case HEADER_ZERO_BLOCK
:
234 if (block_number_option
)
236 char buf
[UINTMAX_STRSIZE_BOUND
];
237 fprintf (stdlis
, _("block %s: ** Block of NULs **\n"),
238 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
241 set_next_block_after (current_header
);
243 if (!ignore_zeros_option
)
245 char buf
[UINTMAX_STRSIZE_BOUND
];
247 status
= read_header (¤t_header
, ¤t_stat_info
,
249 if (status
== HEADER_ZERO_BLOCK
)
251 WARNOPT (WARN_ALONE_ZERO_BLOCK
,
252 (0, 0, _("A lone zero block at %s"),
253 STRINGIFY_BIGINT (current_block_ordinal (), buf
)));
256 status
= prev_status
;
259 case HEADER_END_OF_FILE
:
260 if (!ignore_zeros_option
)
262 char buf
[UINTMAX_STRSIZE_BOUND
];
263 WARNOPT (WARN_MISSING_ZERO_BLOCKS
,
264 (0, 0, _("Terminating zero blocks missing at %s"),
265 STRINGIFY_BIGINT (current_block_ordinal (), buf
)));
267 if (block_number_option
)
269 char buf
[UINTMAX_STRSIZE_BOUND
];
270 fprintf (stdlis
, _("block %s: ** End of File **\n"),
271 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
276 /* If the previous header was good, tell them that we are
277 skipping bad ones. */
278 set_next_block_after (current_header
);
281 case HEADER_STILL_UNREAD
:
282 ERROR ((0, 0, _("This does not look like a tar archive")));
284 case HEADER_ZERO_BLOCK
:
286 if (block_number_option
)
288 char buf
[UINTMAX_STRSIZE_BOUND
];
289 off_t block_ordinal
= current_block_ordinal ();
290 block_ordinal
-= recent_long_name_blocks
;
291 block_ordinal
-= recent_long_link_blocks
;
292 fprintf (stdlis
, _("block %s: "),
293 STRINGIFY_BIGINT (block_ordinal
, buf
));
295 ERROR ((0, 0, _("Skipping to next header")));
298 case HEADER_END_OF_FILE
:
300 /* We are in the middle of a cascade of errors. */
303 case HEADER_SUCCESS_EXTENDED
:
310 while (!all_names_found (¤t_stat_info
));
313 names_notfound (); /* print names not found */
316 /* Print a header block, based on tar options. */
320 off_t block_ordinal
= current_block_ordinal ();
322 /* Print the header block. */
324 print_header (¤t_stat_info
, current_header
, block_ordinal
);
326 if (incremental_option
)
328 if (verbose_option
> 2)
330 if (is_dumpdir (¤t_stat_info
))
331 list_dumpdir (current_stat_info
.dumpdir
,
332 dumpdir_size (current_stat_info
.dumpdir
));
339 /* Check header checksum */
340 /* The standard BSD tar sources create the checksum by adding up the
341 bytes in the header as type char. I think the type char was unsigned
342 on the PDP-11, but it's signed on the Next and Sun. It looks like the
343 sources to BSD tar were never changed to compute the checksum
344 correctly, so both the Sun and Next add the bytes of the header as
345 signed chars. This doesn't cause a problem until you get a file with
346 a name containing characters with the high bit set. So tar_checksum
347 computes two checksums -- signed and unsigned. */
350 tar_checksum (union block
*header
, bool silent
)
353 int unsigned_sum
= 0; /* the POSIX one :-) */
354 int signed_sum
= 0; /* the Sun one :-( */
360 for (i
= sizeof *header
; i
-- != 0;)
362 unsigned_sum
+= (unsigned char) *p
;
363 signed_sum
+= (signed char) (*p
++);
366 if (unsigned_sum
== 0)
367 return HEADER_ZERO_BLOCK
;
369 /* Adjust checksum to count the "chksum" field as blanks. */
371 for (i
= sizeof header
->header
.chksum
; i
-- != 0;)
373 unsigned_sum
-= (unsigned char) header
->header
.chksum
[i
];
374 signed_sum
-= (signed char) (header
->header
.chksum
[i
]);
376 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
377 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
379 parsed_sum
= from_header (header
->header
.chksum
,
380 sizeof header
->header
.chksum
, 0,
381 0, INT_MAX
, true, silent
);
383 return HEADER_FAILURE
;
385 recorded_sum
= parsed_sum
;
387 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
388 return HEADER_FAILURE
;
390 return HEADER_SUCCESS
;
393 /* Read a block that's supposed to be a header block. Return its
394 address in *RETURN_BLOCK, and if it is good, the file's size
395 and names (file name, link name) in *INFO.
397 Return one of enum read_header describing the status of the
400 The MODE parameter instructs read_header what to do with special
401 header blocks, i.e.: extended POSIX, GNU long name or long link,
404 read_header_auto process them automatically,
405 read_header_x_raw when a special header is read, return
406 HEADER_SUCCESS_EXTENDED without actually
407 processing the header,
408 read_header_x_global when a POSIX global header is read,
409 decode it and return HEADER_SUCCESS_EXTENDED.
411 You must always set_next_block_after(*return_block) to skip past
412 the header which this routine reads. */
415 read_header (union block
**return_block
, struct tar_stat_info
*info
,
416 enum read_header_mode mode
)
420 union block
*data_block
;
421 size_t size
, written
;
422 union block
*next_long_name
= NULL
;
423 union block
*next_long_link
= NULL
;
424 size_t next_long_name_blocks
= 0;
425 size_t next_long_link_blocks
= 0;
426 enum read_header status
= HEADER_SUCCESS
;
430 header
= find_next_block ();
431 *return_block
= header
;
434 status
= HEADER_END_OF_FILE
;
438 if ((status
= tar_checksum (header
, false)) != HEADER_SUCCESS
)
441 /* Good block. Decode file size and return. */
443 if (header
->header
.typeflag
== LNKTYPE
)
444 info
->stat
.st_size
= 0; /* links 0 size on tape */
447 info
->stat
.st_size
= OFF_FROM_HEADER (header
->header
.size
);
448 if (info
->stat
.st_size
< 0)
450 status
= HEADER_FAILURE
;
455 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
456 || header
->header
.typeflag
== GNUTYPE_LONGLINK
457 || header
->header
.typeflag
== XHDTYPE
458 || header
->header
.typeflag
== XGLTYPE
459 || header
->header
.typeflag
== SOLARIS_XHDTYPE
)
461 if (mode
== read_header_x_raw
)
463 status
= HEADER_SUCCESS_EXTENDED
;
466 else if (header
->header
.typeflag
== GNUTYPE_LONGNAME
467 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
469 union block
*header_copy
;
470 size_t name_size
= info
->stat
.st_size
;
471 size_t n
= name_size
% BLOCKSIZE
;
472 size
= name_size
+ BLOCKSIZE
;
474 size
+= BLOCKSIZE
- n
;
476 if (name_size
!= info
->stat
.st_size
|| size
< name_size
)
479 header_copy
= xmalloc (size
+ 1);
481 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
)
483 free (next_long_name
);
484 next_long_name
= header_copy
;
485 next_long_name_blocks
= size
/ BLOCKSIZE
;
489 free (next_long_link
);
490 next_long_link
= header_copy
;
491 next_long_link_blocks
= size
/ BLOCKSIZE
;
494 set_next_block_after (header
);
495 *header_copy
= *header
;
496 bp
= header_copy
->buffer
+ BLOCKSIZE
;
498 for (size
-= BLOCKSIZE
; size
> 0; size
-= written
)
500 data_block
= find_next_block ();
503 ERROR ((0, 0, _("Unexpected EOF in archive")));
506 written
= available_space_after (data_block
);
510 memcpy (bp
, data_block
->buffer
, written
);
512 set_next_block_after ((union block
*)
513 (data_block
->buffer
+ written
- 1));
518 else if (header
->header
.typeflag
== XHDTYPE
519 || header
->header
.typeflag
== SOLARIS_XHDTYPE
)
520 xheader_read (&info
->xhdr
, header
,
521 OFF_FROM_HEADER (header
->header
.size
));
522 else if (header
->header
.typeflag
== XGLTYPE
)
526 if (!recent_global_header
)
527 recent_global_header
= xmalloc (sizeof *recent_global_header
);
528 memcpy (recent_global_header
, header
,
529 sizeof *recent_global_header
);
530 memset (&xhdr
, 0, sizeof xhdr
);
531 xheader_read (&xhdr
, header
,
532 OFF_FROM_HEADER (header
->header
.size
));
533 xheader_decode_global (&xhdr
);
534 xheader_destroy (&xhdr
);
535 if (mode
== read_header_x_global
)
537 status
= HEADER_SUCCESS_EXTENDED
;
548 struct posix_header
const *h
= &header
->header
;
549 char namebuf
[sizeof h
->prefix
+ 1 + NAME_FIELD_SIZE
+ 1];
551 free (recent_long_name
);
555 name
= next_long_name
->buffer
+ BLOCKSIZE
;
556 recent_long_name
= next_long_name
;
557 recent_long_name_blocks
= next_long_name_blocks
;
558 next_long_name
= NULL
;
562 /* Accept file names as specified by POSIX.1-1996
566 if (h
->prefix
[0] && strcmp (h
->magic
, TMAGIC
) == 0)
568 memcpy (np
, h
->prefix
, sizeof h
->prefix
);
569 np
[sizeof h
->prefix
] = '\0';
573 memcpy (np
, h
->name
, sizeof h
->name
);
574 np
[sizeof h
->name
] = '\0';
576 recent_long_name
= 0;
577 recent_long_name_blocks
= 0;
579 assign_string (&info
->orig_file_name
, name
);
580 assign_string (&info
->file_name
, name
);
581 info
->had_trailing_slash
= strip_trailing_slashes (info
->file_name
);
583 free (recent_long_link
);
587 name
= next_long_link
->buffer
+ BLOCKSIZE
;
588 recent_long_link
= next_long_link
;
589 recent_long_link_blocks
= next_long_link_blocks
;
590 next_long_link
= NULL
;
594 memcpy (namebuf
, h
->linkname
, sizeof h
->linkname
);
595 namebuf
[sizeof h
->linkname
] = '\0';
597 recent_long_link
= 0;
598 recent_long_link_blocks
= 0;
600 assign_string (&info
->link_name
, name
);
605 free (next_long_name
);
606 free (next_long_link
);
611 is_octal_digit (char c
)
613 return '0' <= c
&& c
<= '7';
616 /* Decode things from a file HEADER block into STAT_INFO, also setting
617 *FORMAT_POINTER depending on the header block format. If
618 DO_USER_GROUP, decode the user/group information (this is useful
619 for extraction, but waste time when merely listing).
621 read_header() has already decoded the checksum and length, so we don't.
623 This routine should *not* be called twice for the same block, since
624 the two calls might use different DO_USER_GROUP values and thus
625 might end up with different uid/gid for the two calls. If anybody
626 wants the uid/gid they should decode it first, and other callers
627 should decode it without uid/gid before calling a routine,
628 e.g. print_header, that assumes decoded data. */
630 decode_header (union block
*header
, struct tar_stat_info
*stat_info
,
631 enum archive_format
*format_pointer
, int do_user_group
)
633 enum archive_format format
;
635 mode_t mode
= MODE_FROM_HEADER (header
->header
.mode
, &hbits
);
637 if (strcmp (header
->header
.magic
, TMAGIC
) == 0)
639 if (header
->star_header
.prefix
[130] == 0
640 && is_octal_digit (header
->star_header
.atime
[0])
641 && header
->star_header
.atime
[11] == ' '
642 && is_octal_digit (header
->star_header
.ctime
[0])
643 && header
->star_header
.ctime
[11] == ' ')
644 format
= STAR_FORMAT
;
645 else if (stat_info
->xhdr
.size
)
646 format
= POSIX_FORMAT
;
648 format
= USTAR_FORMAT
;
650 else if (strcmp (header
->buffer
+ offsetof (struct posix_header
, magic
),
653 format
= hbits
? OLDGNU_FORMAT
: GNU_FORMAT
;
656 *format_pointer
= format
;
658 stat_info
->stat
.st_mode
= mode
;
659 stat_info
->mtime
.tv_sec
= TIME_FROM_HEADER (header
->header
.mtime
);
660 stat_info
->mtime
.tv_nsec
= 0;
661 assign_string_n (&stat_info
->uname
,
662 header
->header
.uname
[0] ? header
->header
.uname
: NULL
,
663 sizeof (header
->header
.uname
));
664 assign_string_n (&stat_info
->gname
,
665 header
->header
.gname
[0] ? header
->header
.gname
: NULL
,
666 sizeof (header
->header
.gname
));
668 xheader_xattr_init (stat_info
);
670 if (format
== OLDGNU_FORMAT
&& incremental_option
)
672 stat_info
->atime
.tv_sec
= TIME_FROM_HEADER (header
->oldgnu_header
.atime
);
673 stat_info
->ctime
.tv_sec
= TIME_FROM_HEADER (header
->oldgnu_header
.ctime
);
674 stat_info
->atime
.tv_nsec
= stat_info
->ctime
.tv_nsec
= 0;
676 else if (format
== STAR_FORMAT
)
678 stat_info
->atime
.tv_sec
= TIME_FROM_HEADER (header
->star_header
.atime
);
679 stat_info
->ctime
.tv_sec
= TIME_FROM_HEADER (header
->star_header
.ctime
);
680 stat_info
->atime
.tv_nsec
= stat_info
->ctime
.tv_nsec
= 0;
683 stat_info
->atime
= stat_info
->ctime
= start_time
;
685 if (format
== V7_FORMAT
)
687 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
688 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
689 stat_info
->stat
.st_rdev
= 0;
695 /* FIXME: Decide if this should somewhat depend on -p. */
697 if (numeric_owner_option
698 || !*header
->header
.uname
699 || !uname_to_uid (header
->header
.uname
, &stat_info
->stat
.st_uid
))
700 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
702 if (numeric_owner_option
703 || !*header
->header
.gname
704 || !gname_to_gid (header
->header
.gname
, &stat_info
->stat
.st_gid
))
705 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
708 switch (header
->header
.typeflag
)
712 stat_info
->stat
.st_rdev
=
713 makedev (MAJOR_FROM_HEADER (header
->header
.devmajor
),
714 MINOR_FROM_HEADER (header
->header
.devminor
));
718 stat_info
->stat
.st_rdev
= 0;
722 xheader_decode (stat_info
);
724 if (sparse_member_p (stat_info
))
726 sparse_fixup_header (stat_info
);
727 stat_info
->is_sparse
= true;
731 stat_info
->is_sparse
= false;
732 if (((current_format
== GNU_FORMAT
733 || current_format
== OLDGNU_FORMAT
)
734 && current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
735 || stat_info
->dumpdir
)
736 stat_info
->is_dumpdir
= true;
741 /* Convert buffer at WHERE0 of size DIGS from external format to
742 intmax_t. DIGS must be positive. If TYPE is nonnull, the data are
743 of type TYPE. The buffer must represent a value in the range
744 MINVAL through MAXVAL; if the mathematically correct result V would
745 be greater than INTMAX_MAX, return a negative integer V such that
746 (uintmax_t) V yields the correct result. If OCTAL_ONLY, allow only octal
747 numbers instead of the other GNU extensions. Return -1 on error,
748 diagnosing the error if TYPE is nonnull and if !SILENT. */
749 #if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX)
750 # error "from_header internally represents intmax_t as uintmax_t + sign"
752 #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
753 # error "from_header returns intmax_t to represent uintmax_t"
756 from_header (char const *where0
, size_t digs
, char const *type
,
757 intmax_t minval
, uintmax_t maxval
,
758 bool octal_only
, bool silent
)
761 uintmax_t uminval
= minval
;
762 uintmax_t minus_minval
= - uminval
;
763 char const *where
= where0
;
764 char const *lim
= where
+ digs
;
765 bool negative
= false;
767 /* Accommodate buggy tar of unknown vintage, which outputs leading
768 NUL if the previous field overflows. */
771 /* Accommodate older tars, which output leading spaces. */
778 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
780 _("Blanks in header where numeric %s value expected"),
784 if (!c_isspace (*where
))
790 if (is_octal_digit (*where
))
792 char const *where1
= where
;
793 bool overflow
= false;
797 value
+= *where
++ - '0';
798 if (where
== lim
|| ! is_octal_digit (*where
))
800 overflow
|= value
!= (value
<< LG_8
>> LG_8
);
804 /* Parse the output of older, unportable tars, which generate
805 negative values in two's complement octal. If the leading
806 nonzero digit is 1, we can't recover the original value
807 reliably; so do this only if the digit is 2 or more. This
808 catches the common case of 32-bit negative time stamps. */
809 if ((overflow
|| maxval
< value
) && '2' <= *where1
&& type
)
811 /* Compute the negative of the input value, assuming two's
813 int digit
= (*where1
- '0') | 4;
821 if (where
== lim
|| ! is_octal_digit (*where
))
823 digit
= *where
- '0';
824 overflow
|= value
!= (value
<< LG_8
>> LG_8
);
830 if (!overflow
&& value
<= minus_minval
)
834 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
835 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
836 (int) (where
- where1
), where1
, type
));
845 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
846 _("Archive octal value %.*s is out of %s range"),
847 (int) (where
- where1
), where1
, type
));
853 /* Suppress the following extensions. */
855 else if (*where
== '-' || *where
== '+')
857 /* Parse base-64 output produced only by tar test versions
858 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
859 Support for this will be withdrawn in future releases. */
863 static bool warned_once
;
867 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
870 negative
= *where
++ == '-';
872 && (dig
= base64_map
[(unsigned char) *where
]) < 64)
874 if (value
<< LG_64
>> LG_64
!= value
)
878 _("Archive signed base-64 string %s is out of %s range"),
879 quote_mem (where0
, digs
), type
));
882 value
= (value
<< LG_64
) | dig
;
886 else if (where
<= lim
- 2
887 && (*where
== '\200' /* positive base-256 */
888 || *where
== '\377' /* negative base-256 */))
890 /* Parse base-256 output. A nonnegative number N is
891 represented as (256**DIGS)/2 + N; a negative number -N is
892 represented as (256**DIGS) - N, i.e. as two's complement.
893 The representation guarantees that the leading bit is
894 always on, so that we don't confuse this format with the
895 others (assuming ASCII bytes of 8 bits or more). */
896 int signbit
= *where
& (1 << (LG_256
- 2));
897 uintmax_t topbits
= (((uintmax_t) - signbit
)
898 << (CHAR_BIT
* sizeof (uintmax_t)
899 - LG_256
- (LG_256
- 2)));
900 value
= (*where
++ & ((1 << (LG_256
- 2)) - 1)) - signbit
;
903 value
= (value
<< LG_256
) + (unsigned char) *where
++;
906 if (((value
<< LG_256
>> LG_256
) | topbits
) != value
)
910 _("Archive base-256 value is out of %s range"),
915 negative
= signbit
!= 0;
920 if (where
!= lim
&& *where
&& !c_isspace (*where
))
924 char buf
[1000]; /* Big enough to represent any header. */
925 static struct quoting_options
*o
;
929 o
= clone_quoting_options (0);
930 set_quoting_style (o
, locale_quoting_style
);
933 while (where0
!= lim
&& ! lim
[-1])
935 quotearg_buffer (buf
, sizeof buf
, where0
, lim
- where0
, o
);
938 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
939 _("Archive contains %.*s where numeric %s value expected"),
940 (int) sizeof buf
, buf
, type
));
946 if (value
<= (negative
? minus_minval
: maxval
))
947 return represent_uintmax (negative
? -value
: value
);
951 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
952 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
953 char value_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
954 char *minval_string
= STRINGIFY_BIGINT (minus_minval
, minval_buf
+ 1);
955 char *value_string
= STRINGIFY_BIGINT (value
, value_buf
+ 1);
957 *--value_string
= '-';
959 *--minval_string
= '-';
960 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
961 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
963 minval_string
, STRINGIFY_BIGINT (maxval
, maxval_buf
)));
970 gid_from_header (const char *p
, size_t s
)
972 return from_header (p
, s
, "gid_t",
973 TYPE_MINIMUM (gid_t
), TYPE_MAXIMUM (gid_t
),
978 major_from_header (const char *p
, size_t s
)
980 return from_header (p
, s
, "major_t",
981 TYPE_MINIMUM (major_t
), TYPE_MAXIMUM (major_t
),
986 minor_from_header (const char *p
, size_t s
)
988 return from_header (p
, s
, "minor_t",
989 TYPE_MINIMUM (minor_t
), TYPE_MAXIMUM (minor_t
),
993 /* Convert P to the file mode, as understood by tar.
994 Set *HBITS if there are any unrecognized bits. */
996 mode_from_header (const char *p
, size_t s
, bool *hbits
)
998 intmax_t u
= from_header (p
, s
, "mode_t",
999 INTMAX_MIN
, UINTMAX_MAX
,
1001 mode_t mode
= ((u
& TSUID
? S_ISUID
: 0)
1002 | (u
& TSGID
? S_ISGID
: 0)
1003 | (u
& TSVTX
? S_ISVTX
: 0)
1004 | (u
& TUREAD
? S_IRUSR
: 0)
1005 | (u
& TUWRITE
? S_IWUSR
: 0)
1006 | (u
& TUEXEC
? S_IXUSR
: 0)
1007 | (u
& TGREAD
? S_IRGRP
: 0)
1008 | (u
& TGWRITE
? S_IWGRP
: 0)
1009 | (u
& TGEXEC
? S_IXGRP
: 0)
1010 | (u
& TOREAD
? S_IROTH
: 0)
1011 | (u
& TOWRITE
? S_IWOTH
: 0)
1012 | (u
& TOEXEC
? S_IXOTH
: 0));
1013 *hbits
= (u
& ~07777) != 0;
1018 off_from_header (const char *p
, size_t s
)
1020 /* Negative offsets are not allowed in tar files, so invoke
1021 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
1022 return from_header (p
, s
, "off_t",
1023 0, TYPE_MAXIMUM (off_t
),
1028 time_from_header (const char *p
, size_t s
)
1030 return from_header (p
, s
, "time_t",
1031 TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t),
1036 uid_from_header (const char *p
, size_t s
)
1038 return from_header (p
, s
, "uid_t",
1039 TYPE_MINIMUM (uid_t
), TYPE_MAXIMUM (uid_t
),
1044 uintmax_from_header (const char *p
, size_t s
)
1046 return from_header (p
, s
, "uintmax_t", 0, UINTMAX_MAX
, false, false);
1050 /* Return a printable representation of T. The result points to
1051 static storage that can be reused in the next call to this
1052 function, to ctime, or to asctime. If FULL_TIME, then output the
1053 time stamp to its full resolution; otherwise, just output it to
1054 1-minute resolution. */
1056 tartime (struct timespec t
, bool full_time
)
1058 enum { fraclen
= sizeof ".FFFFFFFFF" - 1 };
1059 static char buffer
[max (UINTMAX_STRSIZE_BOUND
+ 1,
1060 INT_STRLEN_BOUND (int) + 16)
1063 time_t s
= t
.tv_sec
;
1065 bool negative
= s
< 0;
1068 if (negative
&& ns
!= 0)
1071 ns
= 1000000000 - ns
;
1074 tm
= utc_option
? gmtime (&s
) : localtime (&s
);
1079 strftime (buffer
, sizeof buffer
, "%Y-%m-%d %H:%M:%S", tm
);
1080 code_ns_fraction (ns
, buffer
+ strlen (buffer
));
1083 strftime (buffer
, sizeof buffer
, "%Y-%m-%d %H:%M", tm
);
1087 /* The time stamp cannot be broken down, most likely because it
1088 is out of range. Convert it as an integer,
1089 right-adjusted in a field with the same width as the usual
1090 4-year ISO time format. */
1091 p
= umaxtostr (negative
? - (uintmax_t) s
: s
,
1092 buffer
+ sizeof buffer
- UINTMAX_STRSIZE_BOUND
- fraclen
);
1095 while ((buffer
+ sizeof buffer
- sizeof "YYYY-MM-DD HH:MM"
1096 + (full_time
? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1100 code_ns_fraction (ns
, buffer
+ sizeof buffer
- 1 - fraclen
);
1104 /* Actually print it.
1106 Plain and fancy file header block logging. Non-verbose just prints
1107 the name, e.g. for "tar t" or "tar x". This should just contain
1108 file names, so it can be fed back into tar with xargs or the "-T"
1109 option. The verbose option can give a bunch of info, one line per
1110 file. I doubt anybody tries to parse its format, or if they do,
1111 they shouldn't. Unix tar is pretty random here anyway. */
1114 /* Width of "user/group size", with initial value chosen
1115 heuristically. This grows as needed, though this may cause some
1116 stairstepping in the output. Make it too small and the output will
1117 almost always look ragged. Make it too large and the output will
1118 be spaced out too far. */
1119 static int ugswidth
= 19;
1121 /* Width of printed time stamps. It grows if longer time stamps are
1122 found (typically, those with nanosecond resolution). Like
1123 USGWIDTH, some stairstepping may occur. */
1124 static int datewidth
= sizeof "YYYY-MM-DD HH:MM" - 1;
1126 static bool volume_label_printed
= false;
1129 simple_print_header (struct tar_stat_info
*st
, union block
*blk
,
1130 off_t block_ordinal
)
1133 char const *time_stamp
;
1137 /* These hold formatted ints. */
1138 char uform
[max (INT_BUFSIZE_BOUND (intmax_t), UINTMAX_STRSIZE_BOUND
)];
1139 char gform
[sizeof uform
];
1141 char size
[2 * UINTMAX_STRSIZE_BOUND
];
1142 /* holds formatted size or major,minor */
1143 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
1147 if (show_transformed_names_option
)
1148 temp_name
= st
->file_name
? st
->file_name
: st
->orig_file_name
;
1150 temp_name
= st
->orig_file_name
? st
->orig_file_name
: st
->file_name
;
1152 if (block_number_option
)
1154 char buf
[UINTMAX_STRSIZE_BOUND
];
1155 if (block_ordinal
< 0)
1156 block_ordinal
= current_block_ordinal ();
1157 block_ordinal
-= recent_long_name_blocks
;
1158 block_ordinal
-= recent_long_link_blocks
;
1159 fprintf (stdlis
, _("block %s: "),
1160 STRINGIFY_BIGINT (block_ordinal
, buf
));
1163 if (verbose_option
<= 1)
1165 /* Just the fax, mam. */
1166 fputs (quotearg (temp_name
), stdlis
);
1167 if (show_transformed_names_option
&& st
->had_trailing_slash
)
1168 fputc ('/', stdlis
);
1169 fputc ('\n', stdlis
);
1173 /* File type and modes. */
1176 switch (blk
->header
.typeflag
)
1178 case GNUTYPE_VOLHDR
:
1179 volume_label_printed
= true;
1183 case GNUTYPE_MULTIVOL
:
1187 case GNUTYPE_LONGNAME
:
1188 case GNUTYPE_LONGLINK
:
1190 ERROR ((0, 0, _("Unexpected long name header")));
1193 case GNUTYPE_SPARSE
:
1196 modes
[0] = st
->had_trailing_slash
? 'd' : '-';
1201 case GNUTYPE_DUMPDIR
:
1224 pax_decode_mode (st
->stat
.st_mode
, modes
+ 1);
1226 /* extended attributes: GNU `ls -l'-like preview */
1227 xattrs_print_char (st
, modes
+ 10);
1231 time_stamp
= tartime (st
->mtime
, full_time_option
);
1232 time_stamp_len
= strlen (time_stamp
);
1233 if (datewidth
< time_stamp_len
)
1234 datewidth
= time_stamp_len
;
1236 /* User and group names. */
1240 && current_format
!= V7_FORMAT
1241 && !numeric_owner_option
)
1244 user
= STRINGIFY_BIGINT (st
->stat
.st_uid
, uform
);
1248 && current_format
!= V7_FORMAT
1249 && !numeric_owner_option
)
1252 group
= STRINGIFY_BIGINT (st
->stat
.st_gid
, gform
);
1254 /* Format the file size or major/minor device numbers. */
1256 switch (blk
->header
.typeflag
)
1261 STRINGIFY_BIGINT (major (st
->stat
.st_rdev
), uintbuf
));
1264 STRINGIFY_BIGINT (minor (st
->stat
.st_rdev
), uintbuf
));
1268 /* st->stat.st_size keeps stored file size */
1269 strcpy (size
, STRINGIFY_BIGINT (st
->stat
.st_size
, uintbuf
));
1273 /* Figure out padding and print the whole line. */
1275 sizelen
= strlen (size
);
1276 pad
= strlen (user
) + 1 + strlen (group
) + 1 + sizelen
;
1280 fprintf (stdlis
, "%s %s/%s %*s %-*s",
1281 modes
, user
, group
, ugswidth
- pad
+ sizelen
, size
,
1282 datewidth
, time_stamp
);
1284 fprintf (stdlis
, " %s", quotearg (temp_name
));
1285 if (show_transformed_names_option
&& st
->had_trailing_slash
)
1286 fputc ('/', stdlis
);
1288 switch (blk
->header
.typeflag
)
1291 fprintf (stdlis
, " -> %s\n", quotearg (st
->link_name
));
1295 fprintf (stdlis
, _(" link to %s\n"), quotearg (st
->link_name
));
1300 char type_string
[2];
1301 type_string
[0] = blk
->header
.typeflag
;
1302 type_string
[1] = '\0';
1303 fprintf (stdlis
, _(" unknown file type %s\n"),
1304 quote (type_string
));
1310 case GNUTYPE_SPARSE
:
1316 case GNUTYPE_DUMPDIR
:
1317 putc ('\n', stdlis
);
1320 case GNUTYPE_LONGLINK
:
1321 fprintf (stdlis
, _("--Long Link--\n"));
1324 case GNUTYPE_LONGNAME
:
1325 fprintf (stdlis
, _("--Long Name--\n"));
1328 case GNUTYPE_VOLHDR
:
1329 fprintf (stdlis
, _("--Volume Header--\n"));
1332 case GNUTYPE_MULTIVOL
:
1335 (UINTMAX_FROM_HEADER (blk
->oldgnu_header
.offset
),
1337 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
1347 print_volume_label (void)
1349 struct tar_stat_info vstat
;
1351 enum archive_format dummy
;
1353 memset (&vblk
, 0, sizeof (vblk
));
1354 vblk
.header
.typeflag
= GNUTYPE_VOLHDR
;
1355 if (recent_global_header
)
1356 memcpy (vblk
.header
.mtime
, recent_global_header
->header
.mtime
,
1357 sizeof vblk
.header
.mtime
);
1358 tar_stat_init (&vstat
);
1359 assign_string (&vstat
.file_name
, ".");
1360 decode_header (&vblk
, &vstat
, &dummy
, 0);
1361 assign_string (&vstat
.file_name
, volume_label
);
1362 simple_print_header (&vstat
, &vblk
, 0);
1363 tar_stat_destroy (&vstat
);
1367 print_header (struct tar_stat_info
*st
, union block
*blk
,
1368 off_t block_ordinal
)
1370 if (current_format
== POSIX_FORMAT
&& !volume_label_printed
&& volume_label
)
1372 print_volume_label ();
1373 volume_label_printed
= true;
1376 simple_print_header (st
, blk
, block_ordinal
);
1379 /* Print a similar line when we make a directory automatically. */
1381 print_for_mkdir (char *dirname
, mode_t mode
)
1385 if (verbose_option
> 1)
1387 /* File type and modes. */
1390 pax_decode_mode (mode
, modes
+ 1);
1392 if (block_number_option
)
1394 char buf
[UINTMAX_STRSIZE_BOUND
];
1395 fprintf (stdlis
, _("block %s: "),
1396 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
1399 fprintf (stdlis
, "%s %*s %s\n", modes
, ugswidth
+ 1 + datewidth
,
1400 _("Creating directory:"), quotearg (dirname
));
1404 /* Skip over SIZE bytes of data in blocks in the archive.
1405 This may involve copying the data.
1406 If MUST_COPY, always copy instead of skipping. */
1408 skim_file (off_t size
, bool must_copy
)
1412 /* FIXME: Make sure mv_begin_read is always called before it */
1414 if (seekable_archive
&& !must_copy
)
1416 off_t nblk
= seek_archive (size
);
1418 size
-= nblk
* BLOCKSIZE
;
1420 seekable_archive
= false;
1423 mv_size_left (size
);
1427 x
= find_next_block ();
1429 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1431 set_next_block_after (x
);
1433 mv_size_left (size
);
1437 /* Skip the current member in the archive.
1438 NOTE: Current header must be decoded before calling this function. */
1442 skim_member (false);
1445 /* Skip the current member in the archive.
1446 If MUST_COPY, always copy instead of skipping. */
1448 skim_member (bool must_copy
)
1450 if (!current_stat_info
.skipped
)
1452 char save_typeflag
= current_header
->header
.typeflag
;
1453 set_next_block_after (current_header
);
1455 mv_begin_read (¤t_stat_info
);
1457 if (current_stat_info
.is_sparse
)
1458 sparse_skim_file (¤t_stat_info
, must_copy
);
1459 else if (save_typeflag
!= DIRTYPE
)
1460 skim_file (current_stat_info
.stat
.st_size
, must_copy
);
1467 test_archive_label (void)
1472 open_archive (ACCESS_READ
);
1473 if (read_header (¤t_header
, ¤t_stat_info
, read_header_auto
)
1476 decode_header (current_header
,
1477 ¤t_stat_info
, ¤t_format
, 0);
1478 if (current_header
->header
.typeflag
== GNUTYPE_VOLHDR
)
1479 ASSIGN_STRING_N (&volume_label
, current_header
->header
.name
);
1484 print_volume_label ();
1485 if (!name_match (volume_label
) && multi_volume_option
)
1487 char *s
= drop_volume_label_suffix (volume_label
);