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 destroy_stat (¤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);
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
;
493 format
= POSIX_FORMAT
;
495 else if (strcmp (header
->header
.magic
, OLDGNU_MAGIC
) == 0)
496 format
= OLDGNU_FORMAT
;
499 *format_pointer
= format
;
501 stat_info
->stat
.st_mode
= MODE_FROM_HEADER (header
->header
.mode
);
502 stat_info
->stat
.st_mtime
= TIME_FROM_HEADER (header
->header
.mtime
);
503 assign_string (&stat_info
->uname
, header
->header
.uname
);
504 assign_string (&stat_info
->gname
, header
->header
.gname
);
505 stat_info
->devmajor
= MAJOR_FROM_HEADER (header
->header
.devmajor
);
506 stat_info
->devminor
= MINOR_FROM_HEADER (header
->header
.devminor
);
508 stat_info
->stat
.st_atime
= start_time
;
509 stat_info
->stat
.st_ctime
= start_time
;
511 if (format
== OLDGNU_FORMAT
&& incremental_option
)
513 stat_info
->stat
.st_atime
= TIME_FROM_HEADER (header
->oldgnu_header
.atime
);
514 stat_info
->stat
.st_ctime
= TIME_FROM_HEADER (header
->oldgnu_header
.ctime
);
517 if (format
== V7_FORMAT
)
519 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
520 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
521 stat_info
->stat
.st_rdev
= 0;
526 if (format
== STAR_FORMAT
)
528 stat_info
->stat
.st_atime
= TIME_FROM_HEADER (header
->star_header
.atime
);
529 stat_info
->stat
.st_ctime
= TIME_FROM_HEADER (header
->star_header
.ctime
);
534 /* FIXME: Decide if this should somewhat depend on -p. */
536 if (numeric_owner_option
537 || !*header
->header
.uname
538 || !uname_to_uid (header
->header
.uname
, &stat_info
->stat
.st_uid
))
539 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
541 if (numeric_owner_option
542 || !*header
->header
.gname
543 || !gname_to_gid (header
->header
.gname
, &stat_info
->stat
.st_gid
))
544 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
547 switch (header
->header
.typeflag
)
551 stat_info
->stat
.st_rdev
= makedev (stat_info
->devmajor
, stat_info
->devminor
);
555 stat_info
->stat
.st_rdev
= 0;
559 if (extended_header
.size
)
560 xheader_decode (stat_info
);
563 /* Convert buffer at WHERE0 of size DIGS from external format to
564 uintmax_t. The data is of type TYPE. The buffer must represent a
565 value in the range -MINUS_MINVAL through MAXVAL. DIGS must be
566 positive. Return -1 on error, diagnosing the error if TYPE is
569 from_header (char const *where0
, size_t digs
, char const *type
,
570 uintmax_t minus_minval
, uintmax_t maxval
)
573 char const *where
= where0
;
574 char const *lim
= where
+ digs
;
577 /* Accommodate buggy tar of unknown vintage, which outputs leading
578 NUL if the previous field overflows. */
581 /* Accommodate older tars, which output leading spaces. */
588 _("Blanks in header where numeric %s value expected"),
592 if (!ISSPACE ((unsigned char) *where
))
598 if (ISODIGIT (*where
))
600 char const *where1
= where
;
601 uintmax_t overflow
= 0;
605 value
+= *where
++ - '0';
606 if (where
== lim
|| ! ISODIGIT (*where
))
608 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
612 /* Parse the output of older, unportable tars, which generate
613 negative values in two's complement octal. If the leading
614 nonzero digit is 1, we can't recover the original value
615 reliably; so do this only if the digit is 2 or more. This
616 catches the common case of 32-bit negative time stamps. */
617 if ((overflow
|| maxval
< value
) && '2' <= *where1
&& type
)
619 /* Compute the negative of the input value, assuming two's
621 int digit
= (*where1
- '0') | 4;
629 if (where
== lim
|| ! ISODIGIT (*where
))
631 digit
= *where
- '0';
632 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
638 if (!overflow
&& value
<= minus_minval
)
641 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
642 (int) (where
- where1
), where1
, type
));
651 _("Archive octal value %.*s is out of %s range"),
652 (int) (where
- where1
), where1
, type
));
656 else if (*where
== '-' || *where
== '+')
658 /* Parse base-64 output produced only by tar test versions
659 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
660 Support for this will be withdrawn in future releases. */
662 static int warned_once
;
667 _("Archive contains obsolescent base-64 headers")));
669 negative
= *where
++ == '-';
671 && (dig
= base64_map
[(unsigned char) *where
]) < 64)
673 if (value
<< LG_64
>> LG_64
!= value
)
675 char *string
= alloca (digs
+ 1);
676 memcpy (string
, where0
, digs
);
680 _("Archive signed base-64 string %s is out of %s range"),
681 quote (string
), type
));
684 value
= (value
<< LG_64
) | dig
;
688 else if (*where
== '\200' /* positive base-256 */
689 || *where
== '\377' /* negative base-256 */)
691 /* Parse base-256 output. A nonnegative number N is
692 represented as (256**DIGS)/2 + N; a negative number -N is
693 represented as (256**DIGS) - N, i.e. as two's complement.
694 The representation guarantees that the leading bit is
695 always on, so that we don't confuse this format with the
696 others (assuming ASCII bytes of 8 bits or more). */
697 int signbit
= *where
& (1 << (LG_256
- 2));
698 uintmax_t topbits
= (((uintmax_t) - signbit
)
699 << (CHAR_BIT
* sizeof (uintmax_t)
700 - LG_256
- (LG_256
- 2)));
701 value
= (*where
++ & ((1 << (LG_256
- 2)) - 1)) - signbit
;
704 value
= (value
<< LG_256
) + (unsigned char) *where
++;
707 if (((value
<< LG_256
>> LG_256
) | topbits
) != value
)
711 _("Archive base-256 value is out of %s range"),
721 if (where
!= lim
&& *where
&& !ISSPACE ((unsigned char) *where
))
725 char buf
[1000]; /* Big enough to represent any header. */
726 static struct quoting_options
*o
;
730 o
= clone_quoting_options (0);
731 set_quoting_style (o
, locale_quoting_style
);
734 while (where0
!= lim
&& ! lim
[-1])
736 quotearg_buffer (buf
, sizeof buf
, where0
, lim
- where
, o
);
738 _("Archive contains %.*s where numeric %s value expected"),
739 (int) sizeof buf
, buf
, type
));
745 if (value
<= (negative
? minus_minval
: maxval
))
746 return negative
? -value
: value
;
750 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
751 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
752 char value_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
753 char *minval_string
= STRINGIFY_BIGINT (minus_minval
, minval_buf
+ 1);
754 char *value_string
= STRINGIFY_BIGINT (value
, value_buf
+ 1);
756 *--value_string
= '-';
758 *--minval_string
= '-';
759 ERROR ((0, 0, _("Archive value %s is out of %s range %s.%s"),
761 minval_string
, STRINGIFY_BIGINT (maxval
, maxval_buf
)));
768 gid_from_header (const char *p
, size_t s
)
770 return from_header (p
, s
, "gid_t",
771 - (uintmax_t) TYPE_MINIMUM (gid_t
),
772 (uintmax_t) TYPE_MAXIMUM (gid_t
));
776 major_from_header (const char *p
, size_t s
)
778 return from_header (p
, s
, "major_t",
779 - (uintmax_t) TYPE_MINIMUM (major_t
),
780 (uintmax_t) TYPE_MAXIMUM (major_t
));
784 minor_from_header (const char *p
, size_t s
)
786 return from_header (p
, s
, "minor_t",
787 - (uintmax_t) TYPE_MINIMUM (minor_t
),
788 (uintmax_t) TYPE_MAXIMUM (minor_t
));
792 mode_from_header (const char *p
, size_t s
)
794 /* Do not complain about unrecognized mode bits. */
795 unsigned u
= from_header (p
, s
, "mode_t",
796 - (uintmax_t) TYPE_MINIMUM (mode_t
),
797 TYPE_MAXIMUM (uintmax_t));
798 return ((u
& TSUID
? S_ISUID
: 0)
799 | (u
& TSGID
? S_ISGID
: 0)
800 | (u
& TSVTX
? S_ISVTX
: 0)
801 | (u
& TUREAD
? S_IRUSR
: 0)
802 | (u
& TUWRITE
? S_IWUSR
: 0)
803 | (u
& TUEXEC
? S_IXUSR
: 0)
804 | (u
& TGREAD
? S_IRGRP
: 0)
805 | (u
& TGWRITE
? S_IWGRP
: 0)
806 | (u
& TGEXEC
? S_IXGRP
: 0)
807 | (u
& TOREAD
? S_IROTH
: 0)
808 | (u
& TOWRITE
? S_IWOTH
: 0)
809 | (u
& TOEXEC
? S_IXOTH
: 0));
813 off_from_header (const char *p
, size_t s
)
815 /* Negative offsets are not allowed in tar files, so invoke
816 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
817 return from_header (p
, s
, "off_t", (uintmax_t) 0,
818 (uintmax_t) TYPE_MAXIMUM (off_t
));
822 size_from_header (const char *p
, size_t s
)
824 return from_header (p
, s
, "size_t", (uintmax_t) 0,
825 (uintmax_t) TYPE_MAXIMUM (size_t));
829 time_from_header (const char *p
, size_t s
)
831 return from_header (p
, s
, "time_t",
832 - (uintmax_t) TYPE_MINIMUM (time_t),
833 (uintmax_t) TYPE_MAXIMUM (time_t));
837 uid_from_header (const char *p
, size_t s
)
839 return from_header (p
, s
, "uid_t",
840 - (uintmax_t) TYPE_MINIMUM (uid_t
),
841 (uintmax_t) TYPE_MAXIMUM (uid_t
));
845 uintmax_from_header (const char *p
, size_t s
)
847 return from_header (p
, s
, "uintmax_t", (uintmax_t) 0,
848 TYPE_MAXIMUM (uintmax_t));
852 /* Format O as a null-terminated decimal string into BUF _backwards_;
853 return pointer to start of result. */
855 stringify_uintmax_t_backwards (uintmax_t o
, char *buf
)
859 *--buf
= '0' + (int) (o
% 10);
860 while ((o
/= 10) != 0);
864 /* Return a printable representation of T. The result points to
865 static storage that can be reused in the next call to this
866 function, to ctime, or to asctime. */
870 static char buffer
[max (UINTMAX_STRSIZE_BOUND
+ 1,
871 INT_STRLEN_BOUND (int) + 16)];
878 char const *time_stamp
= p
+ 4;
879 for (p
+= 16; p
[3] != '\n'; p
++)
885 /* Use ISO 8610 format. See:
886 http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
887 struct tm
*tm
= localtime (&t
);
890 sprintf (buffer
, "%04ld-%02d-%02d %02d:%02d:%02d",
891 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
892 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
897 /* The time stamp cannot be broken down, most likely because it
898 is out of range. Convert it as an integer,
899 right-adjusted in a field with the same width as the usual
900 19-byte 4-year ISO time format. */
901 p
= stringify_uintmax_t_backwards (t
< 0 ? - (uintmax_t) t
: (uintmax_t) t
,
902 buffer
+ sizeof buffer
);
905 while (buffer
+ sizeof buffer
- 19 - 1 < p
)
910 /* Actually print it.
912 Plain and fancy file header block logging. Non-verbose just prints
913 the name, e.g. for "tar t" or "tar x". This should just contain
914 file names, so it can be fed back into tar with xargs or the "-T"
915 option. The verbose option can give a bunch of info, one line per
916 file. I doubt anybody tries to parse its format, or if they do,
917 they shouldn't. Unix tar is pretty random here anyway. */
920 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
921 HEAD_STANDARD, which must be set up in advance. Not very clean.. */
923 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
924 columns never shift during the listing. */
926 static int ugswidth
= UGSWIDTH
; /* maximum width encountered so far */
928 /* DATEWIDTH is the number of columns taken by the date and time fields. */
930 # define DATEWIDTH 19
932 # define DATEWIDTH 18
936 print_header (off_t block_ordinal
)
939 char const *time_stamp
;
940 char *temp_name
= current_stat_info
.orig_file_name
? current_stat_info
.orig_file_name
: current_stat_info
.file_name
;
942 /* These hold formatted ints. */
943 char uform
[UINTMAX_STRSIZE_BOUND
], gform
[UINTMAX_STRSIZE_BOUND
];
945 char size
[2 * UINTMAX_STRSIZE_BOUND
];
946 /* holds formatted size or major,minor */
947 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
950 if (block_number_option
)
952 char buf
[UINTMAX_STRSIZE_BOUND
];
953 if (block_ordinal
< 0)
954 block_ordinal
= current_block_ordinal ();
955 block_ordinal
-= recent_long_name_blocks
;
956 block_ordinal
-= recent_long_link_blocks
;
957 fprintf (stdlis
, _("block %s: "),
958 STRINGIFY_BIGINT (block_ordinal
, buf
));
961 if (verbose_option
<= 1)
963 /* Just the fax, mam. */
964 fprintf (stdlis
, "%s\n", quotearg (temp_name
));
968 /* File type and modes. */
971 switch (current_header
->header
.typeflag
)
977 case GNUTYPE_MULTIVOL
:
985 case GNUTYPE_LONGNAME
:
986 case GNUTYPE_LONGLINK
:
988 ERROR ((0, 0, _("Visible longname error")));
995 if (temp_name
[strlen (temp_name
) - 1] == '/')
1001 case GNUTYPE_DUMPDIR
:
1024 decode_mode (current_stat_info
.stat
.st_mode
, modes
+ 1);
1028 time_stamp
= tartime (current_stat_info
.stat
.st_mtime
);
1030 /* User and group names. */
1032 if (current_stat_info
.uname
&& current_format
!= V7_FORMAT
1033 && !numeric_owner_option
)
1034 user
= current_stat_info
.uname
;
1037 /* Try parsing it as an unsigned integer first, and as a
1038 uid_t if that fails. This method can list positive user
1039 ids that are too large to fit in a uid_t. */
1040 uintmax_t u
= from_header (current_header
->header
.uid
,
1041 sizeof current_header
->header
.uid
, 0,
1043 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1045 user
= STRINGIFY_BIGINT (u
, uform
);
1048 sprintf (uform
, "%ld",
1049 (long) UID_FROM_HEADER (current_header
->header
.uid
));
1054 if (current_stat_info
.gname
&& current_format
!= V7_FORMAT
1055 && !numeric_owner_option
)
1056 group
= current_stat_info
.gname
;
1059 /* Try parsing it as an unsigned integer first, and as a
1060 gid_t if that fails. This method can list positive group
1061 ids that are too large to fit in a gid_t. */
1062 uintmax_t g
= from_header (current_header
->header
.gid
,
1063 sizeof current_header
->header
.gid
, 0,
1065 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1067 group
= STRINGIFY_BIGINT (g
, gform
);
1070 sprintf (gform
, "%ld",
1071 (long) GID_FROM_HEADER (current_header
->header
.gid
));
1076 /* Format the file size or major/minor device numbers. */
1078 switch (current_header
->header
.typeflag
)
1083 STRINGIFY_BIGINT (major (current_stat_info
.stat
.st_rdev
), uintbuf
));
1086 STRINGIFY_BIGINT (minor (current_stat_info
.stat
.st_rdev
), uintbuf
));
1088 case GNUTYPE_SPARSE
:
1091 (UINTMAX_FROM_HEADER (current_header
1092 ->oldgnu_header
.realsize
),
1096 strcpy (size
, STRINGIFY_BIGINT (current_stat_info
.stat
.st_size
, uintbuf
));
1100 /* Figure out padding and print the whole line. */
1102 pad
= strlen (user
) + strlen (group
) + strlen (size
) + 1;
1106 fprintf (stdlis
, "%s %s/%s %*s%s %s",
1107 modes
, user
, group
, ugswidth
- pad
, "", size
, time_stamp
);
1109 fprintf (stdlis
, " %s", quotearg (temp_name
));
1111 switch (current_header
->header
.typeflag
)
1114 fprintf (stdlis
, " -> %s\n", quotearg (current_stat_info
.link_name
));
1118 fprintf (stdlis
, _(" link to %s\n"), quotearg (current_stat_info
.link_name
));
1123 char type_string
[2];
1124 type_string
[0] = current_header
->header
.typeflag
;
1125 type_string
[1] = '\0';
1126 fprintf (stdlis
, _(" unknown file type %s\n"),
1127 quote (type_string
));
1133 case GNUTYPE_SPARSE
:
1139 case GNUTYPE_DUMPDIR
:
1140 putc ('\n', stdlis
);
1143 case GNUTYPE_LONGLINK
:
1144 fprintf (stdlis
, _("--Long Link--\n"));
1147 case GNUTYPE_LONGNAME
:
1148 fprintf (stdlis
, _("--Long Name--\n"));
1151 case GNUTYPE_VOLHDR
:
1152 fprintf (stdlis
, _("--Volume Header--\n"));
1155 case GNUTYPE_MULTIVOL
:
1158 (UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
),
1160 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
1164 fprintf (stdlis
, _("--Mangled file names--\n"));
1171 /* Print a similar line when we make a directory automatically. */
1173 print_for_mkdir (char *pathname
, int length
, mode_t mode
)
1177 if (verbose_option
> 1)
1179 /* File type and modes. */
1182 decode_mode (mode
, modes
+ 1);
1184 if (block_number_option
)
1186 char buf
[UINTMAX_STRSIZE_BOUND
];
1187 fprintf (stdlis
, _("block %s: "),
1188 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
1191 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
1192 _("Creating directory:"), length
, quotearg (pathname
));
1196 /* Skip over SIZE bytes of data in blocks in the archive. */
1198 skip_file (off_t size
)
1202 if (multi_volume_option
)
1204 save_totsize
= size
;
1205 save_sizeleft
= size
;
1210 x
= find_next_block ();
1212 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1214 set_next_block_after (x
);
1216 if (multi_volume_option
)
1217 save_sizeleft
-= BLOCKSIZE
;
1221 /* Skip the current member in the archive. */
1225 char save_typeflag
= current_header
->header
.typeflag
;
1226 set_next_block_after (current_header
);
1228 if (current_format
== OLDGNU_FORMAT
1229 && current_header
->oldgnu_header
.isextended
)
1234 exhdr
= find_next_block ();
1236 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1237 set_next_block_after (exhdr
);
1239 while (exhdr
->sparse_header
.isextended
);
1242 if (save_typeflag
!= DIRTYPE
)
1243 skip_file (current_stat_info
.stat
.st_size
);