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 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-08-26.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Define to non-zero for forcing old ctime format instead of ISO format. */
30 #define max(a, b) ((a) < (b) ? (b) : (a))
32 union block
*current_header
; /* points to current archive header */
33 enum archive_format current_format
; /* recognized format */
34 union block
*recent_long_name
; /* recent long name header and contents */
35 union block
*recent_long_link
; /* likewise, for long link */
36 size_t recent_long_name_blocks
; /* number of blocks in recent_long_name */
37 size_t recent_long_link_blocks
; /* likewise, for long link */
39 static uintmax_t from_header (const char *, size_t, const char *,
40 uintmax_t, uintmax_t);
42 /* Base 64 digits; see Internet RFC 2045 Table 1. */
43 static char const base_64_digits
[64] =
45 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
46 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
47 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
48 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
49 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
52 /* Table of base-64 digit values indexed by unsigned chars.
53 The value is 64 for unsigned chars that are not base-64 digits. */
54 static char base64_map
[UCHAR_MAX
+ 1];
60 memset (base64_map
, 64, sizeof base64_map
);
61 for (i
= 0; i
< 64; i
++)
62 base64_map
[(int) base_64_digits
[i
]] = i
;
65 /* Main loop for reading an archive. */
67 read_and (void (*do_something
) (void))
69 enum read_header status
= HEADER_STILL_UNREAD
;
70 enum read_header prev_status
;
74 open_archive (ACCESS_READ
);
79 tar_stat_destroy (¤t_stat_info
);
80 xheader_destroy (&extended_header
);
82 status
= read_header (false);
85 case HEADER_STILL_UNREAD
:
86 case HEADER_SUCCESS_EXTENDED
:
91 /* Valid header. We should decode next field (mode) first.
92 Ensure incoming names are null terminated. */
94 if (! name_match (current_stat_info
.file_name
)
95 || (newer_mtime_option
!= TYPE_MINIMUM (time_t)
96 /* FIXME: We get mtime now, and again later; this causes
97 duplicate diagnostics if header.mtime is bogus. */
98 && ((current_stat_info
.stat
.st_mtime
99 = TIME_FROM_HEADER (current_header
->header
.mtime
))
100 < newer_mtime_option
))
101 || excluded_name (current_stat_info
.file_name
))
103 switch (current_header
->header
.typeflag
)
106 case GNUTYPE_MULTIVOL
:
111 if (show_omitted_dirs_option
)
112 WARN ((0, 0, _("%s: Omitting"),
113 quotearg_colon (current_stat_info
.file_name
)));
124 case HEADER_ZERO_BLOCK
:
125 if (block_number_option
)
127 char buf
[UINTMAX_STRSIZE_BOUND
];
128 fprintf (stdlis
, _("block %s: ** Block of NULs **\n"),
129 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
132 set_next_block_after (current_header
);
133 status
= prev_status
;
134 if (ignore_zeros_option
)
138 case HEADER_END_OF_FILE
:
139 if (block_number_option
)
141 char buf
[UINTMAX_STRSIZE_BOUND
];
142 fprintf (stdlis
, _("block %s: ** End of File **\n"),
143 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
148 /* If the previous header was good, tell them that we are
149 skipping bad ones. */
150 set_next_block_after (current_header
);
153 case HEADER_STILL_UNREAD
:
154 ERROR ((0, 0, _("This does not look like a tar archive")));
157 case HEADER_ZERO_BLOCK
:
159 ERROR ((0, 0, _("Skipping to next header")));
162 case HEADER_END_OF_FILE
:
164 /* We are in the middle of a cascade of errors. */
167 case HEADER_SUCCESS_EXTENDED
:
174 while (!all_names_found (¤t_stat_info
));
177 names_notfound (); /* print names not found */
180 /* Print a header block, based on tar options. */
184 /* Print the header block. */
186 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 0);
188 print_header (¤t_stat_info
, -1);
190 if (incremental_option
&& current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
193 size_t written
, check
;
194 union block
*data_block
;
196 set_next_block_after (current_header
);
197 if (multi_volume_option
)
199 assign_string (&save_name
, current_stat_info
.file_name
);
200 save_totsize
= current_stat_info
.stat
.st_size
;
202 for (size
= current_stat_info
.stat
.st_size
; size
> 0; size
-= written
)
204 if (multi_volume_option
)
205 save_sizeleft
= size
;
206 data_block
= find_next_block ();
209 ERROR ((0, 0, _("Unexpected EOF in archive")));
210 break; /* FIXME: What happens, then? */
212 written
= available_space_after (data_block
);
216 check
= fwrite (data_block
->buffer
, sizeof (char), written
, stdlis
);
217 set_next_block_after ((union block
*)
218 (data_block
->buffer
+ written
- 1));
219 if (check
!= written
)
221 write_error_details (current_stat_info
.file_name
, check
, written
);
222 skip_file (size
- written
);
226 if (multi_volume_option
)
227 assign_string (&save_name
, 0);
228 fputc ('\n', stdlis
);
234 if (multi_volume_option
)
235 assign_string (&save_name
, current_stat_info
.file_name
);
239 if (multi_volume_option
)
240 assign_string (&save_name
, 0);
243 /* Read a block that's supposed to be a header block. Return its
244 address in "current_header", and if it is good, the file's size in
245 current_stat_info.stat.st_size.
247 Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a
248 block full of zeros (EOF marker).
250 If RAW_EXTENDED_HEADERS is nonzero, do not automagically fold the
251 GNU long name and link headers into later headers.
253 You must always set_next_block_after(current_header) to skip past
254 the header which this routine reads. */
256 /* The standard BSD tar sources create the checksum by adding up the
257 bytes in the header as type char. I think the type char was unsigned
258 on the PDP-11, but it's signed on the Next and Sun. It looks like the
259 sources to BSD tar were never changed to compute the checksum
260 correctly, so both the Sun and Next add the bytes of the header as
261 signed chars. This doesn't cause a problem until you get a file with
262 a name containing characters with the high bit set. So read_header
263 computes two checksums -- signed and unsigned. */
266 read_header (bool raw_extended_headers
)
269 int unsigned_sum
; /* the POSIX one :-) */
270 int signed_sum
; /* the Sun one :-( */
272 uintmax_t parsed_sum
;
275 union block
*header_copy
;
277 union block
*data_block
;
278 size_t size
, written
;
279 union block
*next_long_name
= 0;
280 union block
*next_long_link
= 0;
281 size_t next_long_name_blocks
;
282 size_t next_long_link_blocks
;
286 header
= find_next_block ();
287 current_header
= header
;
289 return HEADER_END_OF_FILE
;
294 for (i
= sizeof *header
; i
-- != 0;)
296 unsigned_sum
+= (unsigned char) *p
;
297 signed_sum
+= (signed char) (*p
++);
300 if (unsigned_sum
== 0)
301 return HEADER_ZERO_BLOCK
;
303 /* Adjust checksum to count the "chksum" field as blanks. */
305 for (i
= sizeof header
->header
.chksum
; i
-- != 0;)
307 unsigned_sum
-= (unsigned char) header
->header
.chksum
[i
];
308 signed_sum
-= (signed char) (header
->header
.chksum
[i
]);
310 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
311 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
313 parsed_sum
= from_header (header
->header
.chksum
,
314 sizeof header
->header
.chksum
, 0,
316 (uintmax_t) TYPE_MAXIMUM (int));
317 if (parsed_sum
== (uintmax_t) -1)
318 return HEADER_FAILURE
;
320 recorded_sum
= parsed_sum
;
322 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
323 return HEADER_FAILURE
;
325 /* Good block. Decode file size and return. */
327 if (header
->header
.typeflag
== LNKTYPE
)
328 current_stat_info
.stat
.st_size
= 0; /* links 0 size on tape */
330 current_stat_info
.stat
.st_size
= OFF_FROM_HEADER (header
->header
.size
);
332 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
333 || header
->header
.typeflag
== GNUTYPE_LONGLINK
334 || header
->header
.typeflag
== XHDTYPE
335 || header
->header
.typeflag
== XGLTYPE
)
337 if (raw_extended_headers
)
338 return HEADER_SUCCESS_EXTENDED
;
339 else if (header
->header
.typeflag
== GNUTYPE_LONGNAME
340 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
342 size_t name_size
= current_stat_info
.stat
.st_size
;
343 size
= name_size
- name_size
% BLOCKSIZE
+ 2 * BLOCKSIZE
;
344 if (name_size
!= current_stat_info
.stat
.st_size
348 header_copy
= xmalloc (size
+ 1);
350 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
)
353 free (next_long_name
);
354 next_long_name
= header_copy
;
355 next_long_name_blocks
= size
/ BLOCKSIZE
;
360 free (next_long_link
);
361 next_long_link
= header_copy
;
362 next_long_link_blocks
= size
/ BLOCKSIZE
;
365 set_next_block_after (header
);
366 *header_copy
= *header
;
367 bp
= header_copy
->buffer
+ BLOCKSIZE
;
369 for (size
-= BLOCKSIZE
; size
> 0; size
-= written
)
371 data_block
= find_next_block ();
374 ERROR ((0, 0, _("Unexpected EOF in archive")));
377 written
= available_space_after (data_block
);
381 memcpy (bp
, data_block
->buffer
, written
);
383 set_next_block_after ((union block
*)
384 (data_block
->buffer
+ written
- 1));
389 else if (header
->header
.typeflag
== XHDTYPE
390 || header
->header
.typeflag
== XGLTYPE
)
391 xheader_read (header
, OFF_FROM_HEADER (header
->header
.size
));
399 struct posix_header
const *h
= ¤t_header
->header
;
400 char namebuf
[sizeof h
->prefix
+ 1 + NAME_FIELD_SIZE
+ 1];
402 if (recent_long_name
)
403 free (recent_long_name
);
407 name
= next_long_name
->buffer
+ BLOCKSIZE
;
408 recent_long_name
= next_long_name
;
409 recent_long_name_blocks
= next_long_name_blocks
;
413 /* Accept file names as specified by POSIX.1-1996
417 if (h
->prefix
[0] && strcmp (h
->magic
, TMAGIC
) == 0)
419 memcpy (np
, h
->prefix
, sizeof h
->prefix
);
420 np
[sizeof h
->prefix
] = '\0';
424 /* Prevent later references to current_header from
425 mistakenly treating this as an old GNU header.
426 This assignment invalidates h->prefix. */
427 current_header
->oldgnu_header
.isextended
= 0;
429 memcpy (np
, h
->name
, sizeof h
->name
);
430 np
[sizeof h
->name
] = '\0';
432 recent_long_name
= 0;
433 recent_long_name_blocks
= 0;
435 assign_string (¤t_stat_info
.orig_file_name
, name
);
436 assign_string (¤t_stat_info
.file_name
, name
);
437 current_stat_info
.had_trailing_slash
= strip_trailing_slashes (current_stat_info
.file_name
);
439 if (recent_long_link
)
440 free (recent_long_link
);
444 name
= next_long_link
->buffer
+ BLOCKSIZE
;
445 recent_long_link
= next_long_link
;
446 recent_long_link_blocks
= next_long_link_blocks
;
450 memcpy (namebuf
, h
->linkname
, sizeof h
->linkname
);
451 namebuf
[sizeof h
->linkname
] = '\0';
453 recent_long_link
= 0;
454 recent_long_link_blocks
= 0;
456 assign_string (¤t_stat_info
.link_name
, name
);
458 return HEADER_SUCCESS
;
463 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
465 /* Decode things from a file HEADER block into STAT_INFO, also setting
466 *FORMAT_POINTER depending on the header block format. If
467 DO_USER_GROUP, decode the user/group information (this is useful
468 for extraction, but waste time when merely listing).
470 read_header() has already decoded the checksum and length, so we don't.
472 This routine should *not* be called twice for the same block, since
473 the two calls might use different DO_USER_GROUP values and thus
474 might end up with different uid/gid for the two calls. If anybody
475 wants the uid/gid they should decode it first, and other callers
476 should decode it without uid/gid before calling a routine,
477 e.g. print_header, that assumes decoded data. */
479 decode_header (union block
*header
, struct tar_stat_info
*stat_info
,
480 enum archive_format
*format_pointer
, int do_user_group
)
482 enum archive_format format
;
484 if (strcmp (header
->header
.magic
, TMAGIC
) == 0)
486 if (header
->star_header
.prefix
[130] == 0
487 && ISOCTAL (header
->star_header
.atime
[0])
488 && header
->star_header
.atime
[11] == ' '
489 && ISOCTAL (header
->star_header
.ctime
[0])
490 && header
->star_header
.ctime
[11] == ' ')
491 format
= STAR_FORMAT
;
492 else if (extended_header
.size
)
493 format
= POSIX_FORMAT
;
495 format
= USTAR_FORMAT
;
497 else if (strcmp (header
->header
.magic
, OLDGNU_MAGIC
) == 0)
498 format
= OLDGNU_FORMAT
;
501 *format_pointer
= format
;
503 stat_info
->stat
.st_mode
= MODE_FROM_HEADER (header
->header
.mode
);
504 stat_info
->stat
.st_mtime
= TIME_FROM_HEADER (header
->header
.mtime
);
505 assign_string (&stat_info
->uname
, header
->header
.uname
);
506 assign_string (&stat_info
->gname
, header
->header
.gname
);
507 stat_info
->devmajor
= MAJOR_FROM_HEADER (header
->header
.devmajor
);
508 stat_info
->devminor
= MINOR_FROM_HEADER (header
->header
.devminor
);
510 stat_info
->stat
.st_atime
= start_time
;
511 stat_info
->stat
.st_ctime
= start_time
;
513 if (format
== OLDGNU_FORMAT
&& incremental_option
)
515 stat_info
->stat
.st_atime
= TIME_FROM_HEADER (header
->oldgnu_header
.atime
);
516 stat_info
->stat
.st_ctime
= TIME_FROM_HEADER (header
->oldgnu_header
.ctime
);
519 if (format
== V7_FORMAT
)
521 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
522 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
523 stat_info
->stat
.st_rdev
= 0;
528 if (format
== STAR_FORMAT
)
530 stat_info
->stat
.st_atime
= TIME_FROM_HEADER (header
->star_header
.atime
);
531 stat_info
->stat
.st_ctime
= TIME_FROM_HEADER (header
->star_header
.ctime
);
536 /* FIXME: Decide if this should somewhat depend on -p. */
538 if (numeric_owner_option
539 || !*header
->header
.uname
540 || !uname_to_uid (header
->header
.uname
, &stat_info
->stat
.st_uid
))
541 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
543 if (numeric_owner_option
544 || !*header
->header
.gname
545 || !gname_to_gid (header
->header
.gname
, &stat_info
->stat
.st_gid
))
546 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
549 switch (header
->header
.typeflag
)
553 stat_info
->stat
.st_rdev
= makedev (stat_info
->devmajor
, stat_info
->devminor
);
557 stat_info
->stat
.st_rdev
= 0;
561 current_stat_info
.archive_file_size
= current_stat_info
.stat
.st_size
;
562 if (extended_header
.size
)
563 xheader_decode (stat_info
);
566 /* Convert buffer at WHERE0 of size DIGS from external format to
567 uintmax_t. The data is of type TYPE. The buffer must represent a
568 value in the range -MINUS_MINVAL through MAXVAL. DIGS must be
569 positive. Return -1 on error, diagnosing the error if TYPE is
572 from_header (char const *where0
, size_t digs
, char const *type
,
573 uintmax_t minus_minval
, uintmax_t maxval
)
576 char const *where
= where0
;
577 char const *lim
= where
+ digs
;
580 /* Accommodate buggy tar of unknown vintage, which outputs leading
581 NUL if the previous field overflows. */
584 /* Accommodate older tars, which output leading spaces. */
591 _("Blanks in header where numeric %s value expected"),
595 if (!ISSPACE ((unsigned char) *where
))
601 if (ISODIGIT (*where
))
603 char const *where1
= where
;
604 uintmax_t overflow
= 0;
608 value
+= *where
++ - '0';
609 if (where
== lim
|| ! ISODIGIT (*where
))
611 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
615 /* Parse the output of older, unportable tars, which generate
616 negative values in two's complement octal. If the leading
617 nonzero digit is 1, we can't recover the original value
618 reliably; so do this only if the digit is 2 or more. This
619 catches the common case of 32-bit negative time stamps. */
620 if ((overflow
|| maxval
< value
) && '2' <= *where1
&& type
)
622 /* Compute the negative of the input value, assuming two's
624 int digit
= (*where1
- '0') | 4;
632 if (where
== lim
|| ! ISODIGIT (*where
))
634 digit
= *where
- '0';
635 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
641 if (!overflow
&& value
<= minus_minval
)
644 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
645 (int) (where
- where1
), where1
, type
));
654 _("Archive octal value %.*s is out of %s range"),
655 (int) (where
- where1
), where1
, type
));
659 else if (*where
== '-' || *where
== '+')
661 /* Parse base-64 output produced only by tar test versions
662 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
663 Support for this will be withdrawn in future releases. */
665 static int warned_once
;
670 _("Archive contains obsolescent base-64 headers")));
672 negative
= *where
++ == '-';
674 && (dig
= base64_map
[(unsigned char) *where
]) < 64)
676 if (value
<< LG_64
>> LG_64
!= value
)
678 char *string
= alloca (digs
+ 1);
679 memcpy (string
, where0
, digs
);
683 _("Archive signed base-64 string %s is out of %s range"),
684 quote (string
), type
));
687 value
= (value
<< LG_64
) | dig
;
691 else if (*where
== '\200' /* positive base-256 */
692 || *where
== '\377' /* negative base-256 */)
694 /* Parse base-256 output. A nonnegative number N is
695 represented as (256**DIGS)/2 + N; a negative number -N is
696 represented as (256**DIGS) - N, i.e. as two's complement.
697 The representation guarantees that the leading bit is
698 always on, so that we don't confuse this format with the
699 others (assuming ASCII bytes of 8 bits or more). */
700 int signbit
= *where
& (1 << (LG_256
- 2));
701 uintmax_t topbits
= (((uintmax_t) - signbit
)
702 << (CHAR_BIT
* sizeof (uintmax_t)
703 - LG_256
- (LG_256
- 2)));
704 value
= (*where
++ & ((1 << (LG_256
- 2)) - 1)) - signbit
;
707 value
= (value
<< LG_256
) + (unsigned char) *where
++;
710 if (((value
<< LG_256
>> LG_256
) | topbits
) != value
)
714 _("Archive base-256 value is out of %s range"),
724 if (where
!= lim
&& *where
&& !ISSPACE ((unsigned char) *where
))
728 char buf
[1000]; /* Big enough to represent any header. */
729 static struct quoting_options
*o
;
733 o
= clone_quoting_options (0);
734 set_quoting_style (o
, locale_quoting_style
);
737 while (where0
!= lim
&& ! lim
[-1])
739 quotearg_buffer (buf
, sizeof buf
, where0
, lim
- where
, o
);
741 _("Archive contains %.*s where numeric %s value expected"),
742 (int) sizeof buf
, buf
, type
));
748 if (value
<= (negative
? minus_minval
: maxval
))
749 return negative
? -value
: value
;
753 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
754 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
755 char value_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
756 char *minval_string
= STRINGIFY_BIGINT (minus_minval
, minval_buf
+ 1);
757 char *value_string
= STRINGIFY_BIGINT (value
, value_buf
+ 1);
759 *--value_string
= '-';
761 *--minval_string
= '-';
762 ERROR ((0, 0, _("Archive value %s is out of %s range %s.%s"),
764 minval_string
, STRINGIFY_BIGINT (maxval
, maxval_buf
)));
771 gid_from_header (const char *p
, size_t s
)
773 return from_header (p
, s
, "gid_t",
774 - (uintmax_t) TYPE_MINIMUM (gid_t
),
775 (uintmax_t) TYPE_MAXIMUM (gid_t
));
779 major_from_header (const char *p
, size_t s
)
781 return from_header (p
, s
, "major_t",
782 - (uintmax_t) TYPE_MINIMUM (major_t
),
783 (uintmax_t) TYPE_MAXIMUM (major_t
));
787 minor_from_header (const char *p
, size_t s
)
789 return from_header (p
, s
, "minor_t",
790 - (uintmax_t) TYPE_MINIMUM (minor_t
),
791 (uintmax_t) TYPE_MAXIMUM (minor_t
));
795 mode_from_header (const char *p
, size_t s
)
797 /* Do not complain about unrecognized mode bits. */
798 unsigned u
= from_header (p
, s
, "mode_t",
799 - (uintmax_t) TYPE_MINIMUM (mode_t
),
800 TYPE_MAXIMUM (uintmax_t));
801 return ((u
& TSUID
? S_ISUID
: 0)
802 | (u
& TSGID
? S_ISGID
: 0)
803 | (u
& TSVTX
? S_ISVTX
: 0)
804 | (u
& TUREAD
? S_IRUSR
: 0)
805 | (u
& TUWRITE
? S_IWUSR
: 0)
806 | (u
& TUEXEC
? S_IXUSR
: 0)
807 | (u
& TGREAD
? S_IRGRP
: 0)
808 | (u
& TGWRITE
? S_IWGRP
: 0)
809 | (u
& TGEXEC
? S_IXGRP
: 0)
810 | (u
& TOREAD
? S_IROTH
: 0)
811 | (u
& TOWRITE
? S_IWOTH
: 0)
812 | (u
& TOEXEC
? S_IXOTH
: 0));
816 off_from_header (const char *p
, size_t s
)
818 /* Negative offsets are not allowed in tar files, so invoke
819 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
820 return from_header (p
, s
, "off_t", (uintmax_t) 0,
821 (uintmax_t) TYPE_MAXIMUM (off_t
));
825 size_from_header (const char *p
, size_t s
)
827 return from_header (p
, s
, "size_t", (uintmax_t) 0,
828 (uintmax_t) TYPE_MAXIMUM (size_t));
832 time_from_header (const char *p
, size_t s
)
834 return from_header (p
, s
, "time_t",
835 - (uintmax_t) TYPE_MINIMUM (time_t),
836 (uintmax_t) TYPE_MAXIMUM (time_t));
840 uid_from_header (const char *p
, size_t s
)
842 return from_header (p
, s
, "uid_t",
843 - (uintmax_t) TYPE_MINIMUM (uid_t
),
844 (uintmax_t) TYPE_MAXIMUM (uid_t
));
848 uintmax_from_header (const char *p
, size_t s
)
850 return from_header (p
, s
, "uintmax_t", (uintmax_t) 0,
851 TYPE_MAXIMUM (uintmax_t));
855 /* Format O as a null-terminated decimal string into BUF _backwards_;
856 return pointer to start of result. */
858 stringify_uintmax_t_backwards (uintmax_t o
, char *buf
)
862 *--buf
= '0' + (int) (o
% 10);
863 while ((o
/= 10) != 0);
867 /* Return a printable representation of T. The result points to
868 static storage that can be reused in the next call to this
869 function, to ctime, or to asctime. */
873 static char buffer
[max (UINTMAX_STRSIZE_BOUND
+ 1,
874 INT_STRLEN_BOUND (int) + 16)];
881 char const *time_stamp
= p
+ 4;
882 for (p
+= 16; p
[3] != '\n'; p
++)
888 /* Use ISO 8610 format. See:
889 http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
890 struct tm
*tm
= localtime (&t
);
893 sprintf (buffer
, "%04ld-%02d-%02d %02d:%02d:%02d",
894 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
895 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
900 /* The time stamp cannot be broken down, most likely because it
901 is out of range. Convert it as an integer,
902 right-adjusted in a field with the same width as the usual
903 19-byte 4-year ISO time format. */
904 p
= stringify_uintmax_t_backwards (t
< 0 ? - (uintmax_t) t
: (uintmax_t) t
,
905 buffer
+ sizeof buffer
);
908 while (buffer
+ sizeof buffer
- 19 - 1 < p
)
913 /* Actually print it.
915 Plain and fancy file header block logging. Non-verbose just prints
916 the name, e.g. for "tar t" or "tar x". This should just contain
917 file names, so it can be fed back into tar with xargs or the "-T"
918 option. The verbose option can give a bunch of info, one line per
919 file. I doubt anybody tries to parse its format, or if they do,
920 they shouldn't. Unix tar is pretty random here anyway. */
923 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
924 HEAD_STANDARD, which must be set up in advance. Not very clean.. */
926 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
927 columns never shift during the listing. */
929 static int ugswidth
= UGSWIDTH
; /* maximum width encountered so far */
931 /* DATEWIDTH is the number of columns taken by the date and time fields. */
933 # define DATEWIDTH 19
935 # define DATEWIDTH 18
939 print_header (struct tar_stat_info
*st
, off_t block_ordinal
)
942 char const *time_stamp
;
943 char *temp_name
= st
->orig_file_name
? st
->orig_file_name
: st
->file_name
;
945 /* These hold formatted ints. */
946 char uform
[UINTMAX_STRSIZE_BOUND
], gform
[UINTMAX_STRSIZE_BOUND
];
948 char size
[2 * UINTMAX_STRSIZE_BOUND
];
949 /* holds formatted size or major,minor */
950 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
953 if (block_number_option
)
955 char buf
[UINTMAX_STRSIZE_BOUND
];
956 if (block_ordinal
< 0)
957 block_ordinal
= current_block_ordinal ();
958 block_ordinal
-= recent_long_name_blocks
;
959 block_ordinal
-= recent_long_link_blocks
;
960 fprintf (stdlis
, _("block %s: "),
961 STRINGIFY_BIGINT (block_ordinal
, buf
));
964 if (verbose_option
<= 1)
966 /* Just the fax, mam. */
967 fprintf (stdlis
, "%s\n", quotearg (temp_name
));
971 /* File type and modes. */
974 switch (current_header
->header
.typeflag
)
980 case GNUTYPE_MULTIVOL
:
988 case GNUTYPE_LONGNAME
:
989 case GNUTYPE_LONGLINK
:
991 ERROR ((0, 0, _("Visible longname error")));
998 if (temp_name
[strlen (temp_name
) - 1] == '/')
1004 case GNUTYPE_DUMPDIR
:
1027 decode_mode (st
->stat
.st_mode
, modes
+ 1);
1031 time_stamp
= tartime (st
->stat
.st_mtime
);
1033 /* User and group names. */
1035 if (st
->uname
&& current_format
!= V7_FORMAT
1036 && !numeric_owner_option
)
1040 /* Try parsing it as an unsigned integer first, and as a
1041 uid_t if that fails. This method can list positive user
1042 ids that are too large to fit in a uid_t. */
1043 uintmax_t u
= from_header (current_header
->header
.uid
,
1044 sizeof current_header
->header
.uid
, 0,
1046 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1048 user
= STRINGIFY_BIGINT (u
, uform
);
1051 sprintf (uform
, "%ld",
1052 (long) UID_FROM_HEADER (current_header
->header
.uid
));
1057 if (st
->gname
&& current_format
!= V7_FORMAT
1058 && !numeric_owner_option
)
1062 /* Try parsing it as an unsigned integer first, and as a
1063 gid_t if that fails. This method can list positive group
1064 ids that are too large to fit in a gid_t. */
1065 uintmax_t g
= from_header (current_header
->header
.gid
,
1066 sizeof current_header
->header
.gid
, 0,
1068 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1070 group
= STRINGIFY_BIGINT (g
, gform
);
1073 sprintf (gform
, "%ld",
1074 (long) GID_FROM_HEADER (current_header
->header
.gid
));
1079 /* Format the file size or major/minor device numbers. */
1081 switch (current_header
->header
.typeflag
)
1086 STRINGIFY_BIGINT (major (st
->stat
.st_rdev
), uintbuf
));
1089 STRINGIFY_BIGINT (minor (st
->stat
.st_rdev
), uintbuf
));
1091 case GNUTYPE_SPARSE
:
1094 (UINTMAX_FROM_HEADER (current_header
1095 ->oldgnu_header
.realsize
),
1099 /* st->stat.st_size keeps stored file size */
1100 strcpy (size
, STRINGIFY_BIGINT (st
->archive_file_size
, uintbuf
));
1104 /* Figure out padding and print the whole line. */
1106 pad
= strlen (user
) + strlen (group
) + strlen (size
) + 1;
1110 fprintf (stdlis
, "%s %s/%s %*s%s %s",
1111 modes
, user
, group
, ugswidth
- pad
, "", size
, time_stamp
);
1113 fprintf (stdlis
, " %s", quotearg (temp_name
));
1115 switch (current_header
->header
.typeflag
)
1118 fprintf (stdlis
, " -> %s\n", quotearg (st
->link_name
));
1122 fprintf (stdlis
, _(" link to %s\n"), quotearg (st
->link_name
));
1127 char type_string
[2];
1128 type_string
[0] = current_header
->header
.typeflag
;
1129 type_string
[1] = '\0';
1130 fprintf (stdlis
, _(" unknown file type %s\n"),
1131 quote (type_string
));
1137 case GNUTYPE_SPARSE
:
1143 case GNUTYPE_DUMPDIR
:
1144 putc ('\n', stdlis
);
1147 case GNUTYPE_LONGLINK
:
1148 fprintf (stdlis
, _("--Long Link--\n"));
1151 case GNUTYPE_LONGNAME
:
1152 fprintf (stdlis
, _("--Long Name--\n"));
1155 case GNUTYPE_VOLHDR
:
1156 fprintf (stdlis
, _("--Volume Header--\n"));
1159 case GNUTYPE_MULTIVOL
:
1162 (UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
),
1164 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
1168 fprintf (stdlis
, _("--Mangled file names--\n"));
1175 /* Print a similar line when we make a directory automatically. */
1177 print_for_mkdir (char *pathname
, int length
, mode_t mode
)
1181 if (verbose_option
> 1)
1183 /* File type and modes. */
1186 decode_mode (mode
, modes
+ 1);
1188 if (block_number_option
)
1190 char buf
[UINTMAX_STRSIZE_BOUND
];
1191 fprintf (stdlis
, _("block %s: "),
1192 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
1195 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
1196 _("Creating directory:"), length
, quotearg (pathname
));
1200 /* Skip over SIZE bytes of data in blocks in the archive. */
1202 skip_file (off_t size
)
1206 if (multi_volume_option
)
1208 save_totsize
= size
;
1209 save_sizeleft
= size
;
1214 x
= find_next_block ();
1216 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1218 set_next_block_after (x
);
1220 if (multi_volume_option
)
1221 save_sizeleft
-= BLOCKSIZE
;
1225 /* Skip the current member in the archive. */
1229 char save_typeflag
= current_header
->header
.typeflag
;
1230 set_next_block_after (current_header
);
1232 if (current_format
== OLDGNU_FORMAT
1233 && current_header
->oldgnu_header
.isextended
)
1238 exhdr
= find_next_block ();
1240 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1241 set_next_block_after (exhdr
);
1243 while (exhdr
->sparse_header
.isextended
);
1246 if (save_typeflag
!= DIRTYPE
)
1247 skip_file (current_stat_info
.stat
.st_size
);