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 status
= read_header (false);
82 case HEADER_STILL_UNREAD
:
83 case HEADER_SUCCESS_EXTENDED
:
88 /* Valid header. We should decode next field (mode) first.
89 Ensure incoming names are null terminated. */
91 if (! name_match (current_stat_info
.file_name
)
92 || (newer_mtime_option
!= TYPE_MINIMUM (time_t)
93 /* FIXME: We get mtime now, and again later; this causes
94 duplicate diagnostics if header.mtime is bogus. */
95 && ((current_stat_info
.stat
.st_mtime
96 = TIME_FROM_HEADER (current_header
->header
.mtime
))
97 < newer_mtime_option
))
98 || excluded_name (current_stat_info
.file_name
))
100 switch (current_header
->header
.typeflag
)
103 case GNUTYPE_MULTIVOL
:
108 if (show_omitted_dirs_option
)
109 WARN ((0, 0, _("%s: Omitting"),
110 quotearg_colon (current_stat_info
.file_name
)));
121 case HEADER_ZERO_BLOCK
:
122 if (block_number_option
)
124 char buf
[UINTMAX_STRSIZE_BOUND
];
125 fprintf (stdlis
, _("block %s: ** Block of NULs **\n"),
126 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
129 set_next_block_after (current_header
);
130 status
= prev_status
;
131 if (ignore_zeros_option
)
135 case HEADER_END_OF_FILE
:
136 if (block_number_option
)
138 char buf
[UINTMAX_STRSIZE_BOUND
];
139 fprintf (stdlis
, _("block %s: ** End of File **\n"),
140 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
145 /* If the previous header was good, tell them that we are
146 skipping bad ones. */
147 set_next_block_after (current_header
);
150 case HEADER_STILL_UNREAD
:
151 ERROR ((0, 0, _("This does not look like a tar archive")));
154 case HEADER_ZERO_BLOCK
:
156 ERROR ((0, 0, _("Skipping to next header")));
159 case HEADER_END_OF_FILE
:
161 /* We are in the middle of a cascade of errors. */
164 case HEADER_SUCCESS_EXTENDED
:
171 while (!all_names_found (¤t_stat_info
));
174 names_notfound (); /* print names not found */
177 /* Print a header block, based on tar options. */
181 /* Print the header block. */
183 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 0);
187 if (incremental_option
&& current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
190 size_t written
, check
;
191 union block
*data_block
;
193 set_next_block_after (current_header
);
194 if (multi_volume_option
)
196 assign_string (&save_name
, current_stat_info
.file_name
);
197 save_totsize
= current_stat_info
.stat
.st_size
;
199 for (size
= current_stat_info
.stat
.st_size
; size
> 0; size
-= written
)
201 if (multi_volume_option
)
202 save_sizeleft
= size
;
203 data_block
= find_next_block ();
206 ERROR ((0, 0, _("Unexpected EOF in archive")));
207 break; /* FIXME: What happens, then? */
209 written
= available_space_after (data_block
);
213 check
= fwrite (data_block
->buffer
, sizeof (char), written
, stdlis
);
214 set_next_block_after ((union block
*)
215 (data_block
->buffer
+ written
- 1));
216 if (check
!= written
)
218 write_error_details (current_stat_info
.file_name
, check
, written
);
219 skip_file (size
- written
);
223 if (multi_volume_option
)
224 assign_string (&save_name
, 0);
225 fputc ('\n', stdlis
);
231 if (multi_volume_option
)
232 assign_string (&save_name
, current_stat_info
.file_name
);
236 if (multi_volume_option
)
237 assign_string (&save_name
, 0);
240 /* Read a block that's supposed to be a header block. Return its
241 address in "current_header", and if it is good, the file's size in
242 current_stat_info.stat.st_size.
244 Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a
245 block full of zeros (EOF marker).
247 If RAW_EXTENDED_HEADERS is nonzero, do not automagically fold the
248 GNU long name and link headers into later headers.
250 You must always set_next_block_after(current_header) to skip past
251 the header which this routine reads. */
253 /* The standard BSD tar sources create the checksum by adding up the
254 bytes in the header as type char. I think the type char was unsigned
255 on the PDP-11, but it's signed on the Next and Sun. It looks like the
256 sources to BSD tar were never changed to compute the checksum
257 correctly, so both the Sun and Next add the bytes of the header as
258 signed chars. This doesn't cause a problem until you get a file with
259 a name containing characters with the high bit set. So read_header
260 computes two checksums -- signed and unsigned. */
263 read_header (bool raw_extended_headers
)
266 int unsigned_sum
; /* the POSIX one :-) */
267 int signed_sum
; /* the Sun one :-( */
269 uintmax_t parsed_sum
;
272 union block
*header_copy
;
274 union block
*data_block
;
275 size_t size
, written
;
276 union block
*next_long_name
= 0;
277 union block
*next_long_link
= 0;
278 size_t next_long_name_blocks
;
279 size_t next_long_link_blocks
;
283 header
= find_next_block ();
284 current_header
= header
;
286 return HEADER_END_OF_FILE
;
291 for (i
= sizeof *header
; i
-- != 0;)
293 unsigned_sum
+= (unsigned char) *p
;
294 signed_sum
+= (signed char) (*p
++);
297 if (unsigned_sum
== 0)
298 return HEADER_ZERO_BLOCK
;
300 /* Adjust checksum to count the "chksum" field as blanks. */
302 for (i
= sizeof header
->header
.chksum
; i
-- != 0;)
304 unsigned_sum
-= (unsigned char) header
->header
.chksum
[i
];
305 signed_sum
-= (signed char) (header
->header
.chksum
[i
]);
307 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
308 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
310 parsed_sum
= from_header (header
->header
.chksum
,
311 sizeof header
->header
.chksum
, 0,
313 (uintmax_t) TYPE_MAXIMUM (int));
314 if (parsed_sum
== (uintmax_t) -1)
315 return HEADER_FAILURE
;
317 recorded_sum
= parsed_sum
;
319 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
320 return HEADER_FAILURE
;
322 /* Good block. Decode file size and return. */
324 if (header
->header
.typeflag
== LNKTYPE
)
325 current_stat_info
.stat
.st_size
= 0; /* links 0 size on tape */
327 current_stat_info
.stat
.st_size
= OFF_FROM_HEADER (header
->header
.size
);
329 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
330 || header
->header
.typeflag
== GNUTYPE_LONGLINK
331 || header
->header
.typeflag
== XHDTYPE
332 || header
->header
.typeflag
== XGLTYPE
)
334 if (raw_extended_headers
)
335 return HEADER_SUCCESS_EXTENDED
;
336 else if (header
->header
.typeflag
== GNUTYPE_LONGNAME
337 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
339 size_t name_size
= current_stat_info
.stat
.st_size
;
340 size
= name_size
- name_size
% BLOCKSIZE
+ 2 * BLOCKSIZE
;
341 if (name_size
!= current_stat_info
.stat
.st_size
345 header_copy
= xmalloc (size
+ 1);
347 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
)
350 free (next_long_name
);
351 next_long_name
= header_copy
;
352 next_long_name_blocks
= size
/ BLOCKSIZE
;
357 free (next_long_link
);
358 next_long_link
= header_copy
;
359 next_long_link_blocks
= size
/ BLOCKSIZE
;
362 set_next_block_after (header
);
363 *header_copy
= *header
;
364 bp
= header_copy
->buffer
+ BLOCKSIZE
;
366 for (size
-= BLOCKSIZE
; size
> 0; size
-= written
)
368 data_block
= find_next_block ();
371 ERROR ((0, 0, _("Unexpected EOF in archive")));
374 written
= available_space_after (data_block
);
378 memcpy (bp
, data_block
->buffer
, written
);
380 set_next_block_after ((union block
*)
381 (data_block
->buffer
+ written
- 1));
386 else if (header
->header
.typeflag
== XHDTYPE
387 || header
->header
.typeflag
== XGLTYPE
)
388 xheader_read (header
, OFF_FROM_HEADER (header
->header
.size
));
396 struct posix_header
const *h
= ¤t_header
->header
;
397 char namebuf
[sizeof h
->prefix
+ 1 + NAME_FIELD_SIZE
+ 1];
399 if (recent_long_name
)
400 free (recent_long_name
);
404 name
= next_long_name
->buffer
+ BLOCKSIZE
;
405 recent_long_name
= next_long_name
;
406 recent_long_name_blocks
= next_long_name_blocks
;
410 /* Accept file names as specified by POSIX.1-1996
414 if (h
->prefix
[0] && strcmp (h
->magic
, TMAGIC
) == 0)
416 memcpy (np
, h
->prefix
, sizeof h
->prefix
);
417 np
[sizeof h
->prefix
] = '\0';
421 /* Prevent later references to current_header from
422 mistakenly treating this as an old GNU header.
423 This assignment invalidates h->prefix. */
424 current_header
->oldgnu_header
.isextended
= 0;
426 memcpy (np
, h
->name
, sizeof h
->name
);
427 np
[sizeof h
->name
] = '\0';
429 recent_long_name
= 0;
430 recent_long_name_blocks
= 0;
432 assign_string (¤t_stat_info
.orig_file_name
, name
);
433 assign_string (¤t_stat_info
.file_name
, name
);
434 current_stat_info
.had_trailing_slash
= strip_trailing_slashes (current_stat_info
.file_name
);
436 if (recent_long_link
)
437 free (recent_long_link
);
441 name
= next_long_link
->buffer
+ BLOCKSIZE
;
442 recent_long_link
= next_long_link
;
443 recent_long_link_blocks
= next_long_link_blocks
;
447 memcpy (namebuf
, h
->linkname
, sizeof h
->linkname
);
448 namebuf
[sizeof h
->linkname
] = '\0';
450 recent_long_link
= 0;
451 recent_long_link_blocks
= 0;
453 assign_string (¤t_stat_info
.link_name
, name
);
455 return HEADER_SUCCESS
;
460 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
462 /* Decode things from a file HEADER block into STAT_INFO, also setting
463 *FORMAT_POINTER depending on the header block format. If
464 DO_USER_GROUP, decode the user/group information (this is useful
465 for extraction, but waste time when merely listing).
467 read_header() has already decoded the checksum and length, so we don't.
469 This routine should *not* be called twice for the same block, since
470 the two calls might use different DO_USER_GROUP values and thus
471 might end up with different uid/gid for the two calls. If anybody
472 wants the uid/gid they should decode it first, and other callers
473 should decode it without uid/gid before calling a routine,
474 e.g. print_header, that assumes decoded data. */
476 decode_header (union block
*header
, struct tar_stat_info
*stat_info
,
477 enum archive_format
*format_pointer
, int do_user_group
)
479 enum archive_format format
;
481 if (strcmp (header
->header
.magic
, TMAGIC
) == 0)
483 if (header
->star_header
.prefix
[130] == 0
484 && ISOCTAL (header
->star_header
.atime
[0])
485 && header
->star_header
.atime
[11] == ' '
486 && ISOCTAL (header
->star_header
.ctime
[0])
487 && header
->star_header
.ctime
[11] == ' ')
488 format
= STAR_FORMAT
;
490 format
= POSIX_FORMAT
;
492 else if (strcmp (header
->header
.magic
, OLDGNU_MAGIC
) == 0)
493 format
= OLDGNU_FORMAT
;
496 *format_pointer
= format
;
498 stat_info
->stat
.st_mode
= MODE_FROM_HEADER (header
->header
.mode
);
499 stat_info
->stat
.st_mtime
= TIME_FROM_HEADER (header
->header
.mtime
);
500 assign_string (&stat_info
->uname
, header
->header
.uname
);
501 assign_string (&stat_info
->gname
, header
->header
.gname
);
502 stat_info
->devmajor
= MAJOR_FROM_HEADER (header
->header
.devmajor
);
503 stat_info
->devminor
= MINOR_FROM_HEADER (header
->header
.devminor
);
505 stat_info
->stat
.st_atime
= start_time
;
506 stat_info
->stat
.st_ctime
= start_time
;
508 if (format
== OLDGNU_FORMAT
&& incremental_option
)
510 stat_info
->stat
.st_atime
= TIME_FROM_HEADER (header
->oldgnu_header
.atime
);
511 stat_info
->stat
.st_ctime
= TIME_FROM_HEADER (header
->oldgnu_header
.ctime
);
514 if (format
== V7_FORMAT
)
516 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
517 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
518 stat_info
->stat
.st_rdev
= 0;
523 if (format
== STAR_FORMAT
)
525 stat_info
->stat
.st_atime
= TIME_FROM_HEADER (header
->star_header
.atime
);
526 stat_info
->stat
.st_ctime
= TIME_FROM_HEADER (header
->star_header
.ctime
);
531 /* FIXME: Decide if this should somewhat depend on -p. */
533 if (numeric_owner_option
534 || !*header
->header
.uname
535 || !uname_to_uid (header
->header
.uname
, &stat_info
->stat
.st_uid
))
536 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
538 if (numeric_owner_option
539 || !*header
->header
.gname
540 || !gname_to_gid (header
->header
.gname
, &stat_info
->stat
.st_gid
))
541 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
544 switch (header
->header
.typeflag
)
548 stat_info
->stat
.st_rdev
= makedev (stat_info
->devmajor
, stat_info
->devminor
);
552 stat_info
->stat
.st_rdev
= 0;
556 if (extended_header
.size
)
557 xheader_decode (stat_info
);
560 /* Convert buffer at WHERE0 of size DIGS from external format to
561 uintmax_t. The data is of type TYPE. The buffer must represent a
562 value in the range -MINUS_MINVAL through MAXVAL. DIGS must be
563 positive. Return -1 on error, diagnosing the error if TYPE is
566 from_header (char const *where0
, size_t digs
, char const *type
,
567 uintmax_t minus_minval
, uintmax_t maxval
)
570 char const *where
= where0
;
571 char const *lim
= where
+ digs
;
574 /* Accommodate buggy tar of unknown vintage, which outputs leading
575 NUL if the previous field overflows. */
578 /* Accommodate older tars, which output leading spaces. */
585 _("Blanks in header where numeric %s value expected"),
589 if (!ISSPACE ((unsigned char) *where
))
595 if (ISODIGIT (*where
))
597 char const *where1
= where
;
598 uintmax_t overflow
= 0;
602 value
+= *where
++ - '0';
603 if (where
== lim
|| ! ISODIGIT (*where
))
605 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
609 /* Parse the output of older, unportable tars, which generate
610 negative values in two's complement octal. If the leading
611 nonzero digit is 1, we can't recover the original value
612 reliably; so do this only if the digit is 2 or more. This
613 catches the common case of 32-bit negative time stamps. */
614 if ((overflow
|| maxval
< value
) && '2' <= *where1
&& type
)
616 /* Compute the negative of the input value, assuming two's
618 int digit
= (*where1
- '0') | 4;
626 if (where
== lim
|| ! ISODIGIT (*where
))
628 digit
= *where
- '0';
629 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
635 if (!overflow
&& value
<= minus_minval
)
638 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
639 (int) (where
- where1
), where1
, type
));
648 _("Archive octal value %.*s is out of %s range"),
649 (int) (where
- where1
), where1
, type
));
653 else if (*where
== '-' || *where
== '+')
655 /* Parse base-64 output produced only by tar test versions
656 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
657 Support for this will be withdrawn in future releases. */
659 static int warned_once
;
664 _("Archive contains obsolescent base-64 headers")));
666 negative
= *where
++ == '-';
668 && (dig
= base64_map
[(unsigned char) *where
]) < 64)
670 if (value
<< LG_64
>> LG_64
!= value
)
672 char *string
= alloca (digs
+ 1);
673 memcpy (string
, where0
, digs
);
677 _("Archive signed base-64 string %s is out of %s range"),
678 quote (string
), type
));
681 value
= (value
<< LG_64
) | dig
;
685 else if (*where
== '\200' /* positive base-256 */
686 || *where
== '\377' /* negative base-256 */)
688 /* Parse base-256 output. A nonnegative number N is
689 represented as (256**DIGS)/2 + N; a negative number -N is
690 represented as (256**DIGS) - N, i.e. as two's complement.
691 The representation guarantees that the leading bit is
692 always on, so that we don't confuse this format with the
693 others (assuming ASCII bytes of 8 bits or more). */
694 int signbit
= *where
& (1 << (LG_256
- 2));
695 uintmax_t topbits
= (((uintmax_t) - signbit
)
696 << (CHAR_BIT
* sizeof (uintmax_t)
697 - LG_256
- (LG_256
- 2)));
698 value
= (*where
++ & ((1 << (LG_256
- 2)) - 1)) - signbit
;
701 value
= (value
<< LG_256
) + (unsigned char) *where
++;
704 if (((value
<< LG_256
>> LG_256
) | topbits
) != value
)
708 _("Archive base-256 value is out of %s range"),
718 if (where
!= lim
&& *where
&& !ISSPACE ((unsigned char) *where
))
722 char buf
[1000]; /* Big enough to represent any header. */
723 static struct quoting_options
*o
;
727 o
= clone_quoting_options (0);
728 set_quoting_style (o
, locale_quoting_style
);
731 while (where0
!= lim
&& ! lim
[-1])
733 quotearg_buffer (buf
, sizeof buf
, where0
, lim
- where
, o
);
735 _("Archive contains %.*s where numeric %s value expected"),
736 (int) sizeof buf
, buf
, type
));
742 if (value
<= (negative
? minus_minval
: maxval
))
743 return negative
? -value
: value
;
747 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
748 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
749 char value_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
750 char *minval_string
= STRINGIFY_BIGINT (minus_minval
, minval_buf
+ 1);
751 char *value_string
= STRINGIFY_BIGINT (value
, value_buf
+ 1);
753 *--value_string
= '-';
755 *--minval_string
= '-';
756 ERROR ((0, 0, _("Archive value %s is out of %s range %s.%s"),
758 minval_string
, STRINGIFY_BIGINT (maxval
, maxval_buf
)));
765 gid_from_header (const char *p
, size_t s
)
767 return from_header (p
, s
, "gid_t",
768 - (uintmax_t) TYPE_MINIMUM (gid_t
),
769 (uintmax_t) TYPE_MAXIMUM (gid_t
));
773 major_from_header (const char *p
, size_t s
)
775 return from_header (p
, s
, "major_t",
776 - (uintmax_t) TYPE_MINIMUM (major_t
),
777 (uintmax_t) TYPE_MAXIMUM (major_t
));
781 minor_from_header (const char *p
, size_t s
)
783 return from_header (p
, s
, "minor_t",
784 - (uintmax_t) TYPE_MINIMUM (minor_t
),
785 (uintmax_t) TYPE_MAXIMUM (minor_t
));
789 mode_from_header (const char *p
, size_t s
)
791 /* Do not complain about unrecognized mode bits. */
792 unsigned u
= from_header (p
, s
, "mode_t",
793 - (uintmax_t) TYPE_MINIMUM (mode_t
),
794 TYPE_MAXIMUM (uintmax_t));
795 return ((u
& TSUID
? S_ISUID
: 0)
796 | (u
& TSGID
? S_ISGID
: 0)
797 | (u
& TSVTX
? S_ISVTX
: 0)
798 | (u
& TUREAD
? S_IRUSR
: 0)
799 | (u
& TUWRITE
? S_IWUSR
: 0)
800 | (u
& TUEXEC
? S_IXUSR
: 0)
801 | (u
& TGREAD
? S_IRGRP
: 0)
802 | (u
& TGWRITE
? S_IWGRP
: 0)
803 | (u
& TGEXEC
? S_IXGRP
: 0)
804 | (u
& TOREAD
? S_IROTH
: 0)
805 | (u
& TOWRITE
? S_IWOTH
: 0)
806 | (u
& TOEXEC
? S_IXOTH
: 0));
810 off_from_header (const char *p
, size_t s
)
812 /* Negative offsets are not allowed in tar files, so invoke
813 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
814 return from_header (p
, s
, "off_t", (uintmax_t) 0,
815 (uintmax_t) TYPE_MAXIMUM (off_t
));
819 size_from_header (const char *p
, size_t s
)
821 return from_header (p
, s
, "size_t", (uintmax_t) 0,
822 (uintmax_t) TYPE_MAXIMUM (size_t));
826 time_from_header (const char *p
, size_t s
)
828 return from_header (p
, s
, "time_t",
829 - (uintmax_t) TYPE_MINIMUM (time_t),
830 (uintmax_t) TYPE_MAXIMUM (time_t));
834 uid_from_header (const char *p
, size_t s
)
836 return from_header (p
, s
, "uid_t",
837 - (uintmax_t) TYPE_MINIMUM (uid_t
),
838 (uintmax_t) TYPE_MAXIMUM (uid_t
));
842 uintmax_from_header (const char *p
, size_t s
)
844 return from_header (p
, s
, "uintmax_t", (uintmax_t) 0,
845 TYPE_MAXIMUM (uintmax_t));
849 /* Format O as a null-terminated decimal string into BUF _backwards_;
850 return pointer to start of result. */
852 stringify_uintmax_t_backwards (uintmax_t o
, char *buf
)
856 *--buf
= '0' + (int) (o
% 10);
857 while ((o
/= 10) != 0);
861 /* Return a printable representation of T. The result points to
862 static storage that can be reused in the next call to this
863 function, to ctime, or to asctime. */
867 static char buffer
[max (UINTMAX_STRSIZE_BOUND
+ 1,
868 INT_STRLEN_BOUND (int) + 16)];
875 char const *time_stamp
= p
+ 4;
876 for (p
+= 16; p
[3] != '\n'; p
++)
882 /* Use ISO 8610 format. See:
883 http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
884 struct tm
*tm
= localtime (&t
);
887 sprintf (buffer
, "%04ld-%02d-%02d %02d:%02d:%02d",
888 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
889 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
894 /* The time stamp cannot be broken down, most likely because it
895 is out of range. Convert it as an integer,
896 right-adjusted in a field with the same width as the usual
897 19-byte 4-year ISO time format. */
898 p
= stringify_uintmax_t_backwards (t
< 0 ? - (uintmax_t) t
: (uintmax_t) t
,
899 buffer
+ sizeof buffer
);
902 while (buffer
+ sizeof buffer
- 19 - 1 < p
)
907 /* Actually print it.
909 Plain and fancy file header block logging. Non-verbose just prints
910 the name, e.g. for "tar t" or "tar x". This should just contain
911 file names, so it can be fed back into tar with xargs or the "-T"
912 option. The verbose option can give a bunch of info, one line per
913 file. I doubt anybody tries to parse its format, or if they do,
914 they shouldn't. Unix tar is pretty random here anyway. */
917 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
918 HEAD_STANDARD, which must be set up in advance. Not very clean.. */
920 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
921 columns never shift during the listing. */
923 static int ugswidth
= UGSWIDTH
; /* maximum width encountered so far */
925 /* DATEWIDTH is the number of columns taken by the date and time fields. */
927 # define DATEWIDTH 19
929 # define DATEWIDTH 18
933 print_header (off_t block_ordinal
)
936 char const *time_stamp
;
937 char *temp_name
= current_stat_info
.orig_file_name
? current_stat_info
.orig_file_name
: current_stat_info
.file_name
;
939 /* These hold formatted ints. */
940 char uform
[UINTMAX_STRSIZE_BOUND
], gform
[UINTMAX_STRSIZE_BOUND
];
942 char size
[2 * UINTMAX_STRSIZE_BOUND
];
943 /* holds formatted size or major,minor */
944 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
947 if (block_number_option
)
949 char buf
[UINTMAX_STRSIZE_BOUND
];
950 if (block_ordinal
< 0)
951 block_ordinal
= current_block_ordinal ();
952 block_ordinal
-= recent_long_name_blocks
;
953 block_ordinal
-= recent_long_link_blocks
;
954 fprintf (stdlis
, _("block %s: "),
955 STRINGIFY_BIGINT (block_ordinal
, buf
));
958 if (verbose_option
<= 1)
960 /* Just the fax, mam. */
961 fprintf (stdlis
, "%s\n", quotearg (temp_name
));
965 /* File type and modes. */
968 switch (current_header
->header
.typeflag
)
974 case GNUTYPE_MULTIVOL
:
982 case GNUTYPE_LONGNAME
:
983 case GNUTYPE_LONGLINK
:
985 ERROR ((0, 0, _("Visible longname error")));
992 if (temp_name
[strlen (temp_name
) - 1] == '/')
998 case GNUTYPE_DUMPDIR
:
1021 decode_mode (current_stat_info
.stat
.st_mode
, modes
+ 1);
1025 time_stamp
= tartime (current_stat_info
.stat
.st_mtime
);
1027 /* User and group names. */
1029 if (current_stat_info
.uname
&& current_format
!= V7_FORMAT
1030 && !numeric_owner_option
)
1031 user
= current_stat_info
.uname
;
1034 /* Try parsing it as an unsigned integer first, and as a
1035 uid_t if that fails. This method can list positive user
1036 ids that are too large to fit in a uid_t. */
1037 uintmax_t u
= from_header (current_header
->header
.uid
,
1038 sizeof current_header
->header
.uid
, 0,
1040 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1042 user
= STRINGIFY_BIGINT (u
, uform
);
1045 sprintf (uform
, "%ld",
1046 (long) UID_FROM_HEADER (current_header
->header
.uid
));
1051 if (current_stat_info
.gname
&& current_format
!= V7_FORMAT
1052 && !numeric_owner_option
)
1053 group
= current_stat_info
.gname
;
1056 /* Try parsing it as an unsigned integer first, and as a
1057 gid_t if that fails. This method can list positive group
1058 ids that are too large to fit in a gid_t. */
1059 uintmax_t g
= from_header (current_header
->header
.gid
,
1060 sizeof current_header
->header
.gid
, 0,
1062 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1064 group
= STRINGIFY_BIGINT (g
, gform
);
1067 sprintf (gform
, "%ld",
1068 (long) GID_FROM_HEADER (current_header
->header
.gid
));
1073 /* Format the file size or major/minor device numbers. */
1075 switch (current_header
->header
.typeflag
)
1080 STRINGIFY_BIGINT (major (current_stat_info
.stat
.st_rdev
), uintbuf
));
1083 STRINGIFY_BIGINT (minor (current_stat_info
.stat
.st_rdev
), uintbuf
));
1085 case GNUTYPE_SPARSE
:
1088 (UINTMAX_FROM_HEADER (current_header
1089 ->oldgnu_header
.realsize
),
1093 strcpy (size
, STRINGIFY_BIGINT (current_stat_info
.stat
.st_size
, uintbuf
));
1097 /* Figure out padding and print the whole line. */
1099 pad
= strlen (user
) + strlen (group
) + strlen (size
) + 1;
1103 fprintf (stdlis
, "%s %s/%s %*s%s %s",
1104 modes
, user
, group
, ugswidth
- pad
, "", size
, time_stamp
);
1106 fprintf (stdlis
, " %s", quotearg (temp_name
));
1108 switch (current_header
->header
.typeflag
)
1111 fprintf (stdlis
, " -> %s\n", quotearg (current_stat_info
.link_name
));
1115 fprintf (stdlis
, _(" link to %s\n"), quotearg (current_stat_info
.link_name
));
1120 char type_string
[2];
1121 type_string
[0] = current_header
->header
.typeflag
;
1122 type_string
[1] = '\0';
1123 fprintf (stdlis
, _(" unknown file type %s\n"),
1124 quote (type_string
));
1130 case GNUTYPE_SPARSE
:
1136 case GNUTYPE_DUMPDIR
:
1137 putc ('\n', stdlis
);
1140 case GNUTYPE_LONGLINK
:
1141 fprintf (stdlis
, _("--Long Link--\n"));
1144 case GNUTYPE_LONGNAME
:
1145 fprintf (stdlis
, _("--Long Name--\n"));
1148 case GNUTYPE_VOLHDR
:
1149 fprintf (stdlis
, _("--Volume Header--\n"));
1152 case GNUTYPE_MULTIVOL
:
1155 (UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
),
1157 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
1161 fprintf (stdlis
, _("--Mangled file names--\n"));
1168 /* Print a similar line when we make a directory automatically. */
1170 print_for_mkdir (char *pathname
, int length
, mode_t mode
)
1174 if (verbose_option
> 1)
1176 /* File type and modes. */
1179 decode_mode (mode
, modes
+ 1);
1181 if (block_number_option
)
1183 char buf
[UINTMAX_STRSIZE_BOUND
];
1184 fprintf (stdlis
, _("block %s: "),
1185 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
1188 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
1189 _("Creating directory:"), length
, quotearg (pathname
));
1193 /* Skip over SIZE bytes of data in blocks in the archive. */
1195 skip_file (off_t size
)
1199 if (multi_volume_option
)
1201 save_totsize
= size
;
1202 save_sizeleft
= size
;
1207 x
= find_next_block ();
1209 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1211 set_next_block_after (x
);
1213 if (multi_volume_option
)
1214 save_sizeleft
-= BLOCKSIZE
;
1218 /* Skip the current member in the archive. */
1222 char save_typeflag
= current_header
->header
.typeflag
;
1223 set_next_block_after (current_header
);
1225 if (current_format
== OLDGNU_FORMAT
1226 && current_header
->oldgnu_header
.isextended
)
1231 exhdr
= find_next_block ();
1233 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1234 set_next_block_after (exhdr
);
1236 while (exhdr
->sparse_header
.isextended
);
1239 if (save_typeflag
!= DIRTYPE
)
1240 skip_file (current_stat_info
.stat
.st_size
);