Add basic suuport for extended attributes.
[tar.git] / src / list.c
blobf2605ad0c258d68ab6214ed5af1eeb8e68707951
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, 2004, 2005, 2006, 2007, 2010, 2012
5 Free Software Foundation, Inc.
7 Written by John Gilmore, on 1985-08-26.
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any later
12 version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include <system.h>
24 #include <inttostr.h>
25 #include <quotearg.h>
27 #include "common.h"
29 #define max(a, b) ((a) < (b) ? (b) : (a))
31 union block *current_header; /* points to current archive header */
32 enum archive_format current_format; /* recognized format */
33 union block *recent_long_name; /* recent long name header and contents */
34 union block *recent_long_link; /* likewise, for long link */
35 size_t recent_long_name_blocks; /* number of blocks in recent_long_name */
36 size_t recent_long_link_blocks; /* likewise, for long link */
37 union block *recent_global_header; /* Recent global header block */
39 #define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where))
40 #define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where))
41 #define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where))
42 #define MODE_FROM_HEADER(where, hbits) \
43 mode_from_header (where, sizeof (where), hbits)
44 #define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where))
45 #define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where))
47 static gid_t gid_from_header (const char *buf, size_t size);
48 static major_t major_from_header (const char *buf, size_t size);
49 static minor_t minor_from_header (const char *buf, size_t size);
50 static mode_t mode_from_header (const char *buf, size_t size, unsigned *hbits);
51 static time_t time_from_header (const char *buf, size_t size);
52 static uid_t uid_from_header (const char *buf, size_t size);
53 static uintmax_t from_header (const char *, size_t, const char *,
54 uintmax_t, uintmax_t, bool, bool);
56 /* Base 64 digits; see Internet RFC 2045 Table 1. */
57 static char const base_64_digits[64] =
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 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
62 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
63 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
66 /* Table of base-64 digit values indexed by unsigned chars.
67 The value is 64 for unsigned chars that are not base-64 digits. */
68 static char base64_map[UCHAR_MAX + 1];
70 static void
71 base64_init (void)
73 int i;
74 memset (base64_map, 64, sizeof base64_map);
75 for (i = 0; i < 64; i++)
76 base64_map[(int) base_64_digits[i]] = i;
79 static char *
80 decode_xform (char *file_name, void *data)
82 int type = *(int*)data;
84 switch (type)
86 case XFORM_SYMLINK:
87 /* FIXME: It is not quite clear how and to which extent are the symbolic
88 links subject to filename transformation. In the absence of another
89 solution, symbolic links are exempt from component stripping and
90 name suffix normalization, but subject to filename transformation
91 proper. */
92 return file_name;
94 case XFORM_LINK:
95 file_name = safer_name_suffix (file_name, true, absolute_names_option);
96 break;
98 case XFORM_REGFILE:
99 file_name = safer_name_suffix (file_name, false, absolute_names_option);
100 break;
103 if (strip_name_components)
105 size_t prefix_len = stripped_prefix_len (file_name,
106 strip_name_components);
107 if (prefix_len == (size_t) -1)
108 prefix_len = strlen (file_name);
109 file_name += prefix_len;
111 return file_name;
114 static bool
115 transform_member_name (char **pinput, int type)
117 return transform_name_fp (pinput, type, decode_xform, &type);
120 void
121 transform_stat_info (int typeflag, struct tar_stat_info *stat_info)
123 if (typeflag == GNUTYPE_VOLHDR)
124 /* Name transformations don't apply to volume headers. */
125 return;
127 transform_member_name (&stat_info->file_name, XFORM_REGFILE);
128 switch (typeflag)
130 case SYMTYPE:
131 transform_member_name (&stat_info->link_name, XFORM_SYMLINK);
132 break;
134 case LNKTYPE:
135 transform_member_name (&stat_info->link_name, XFORM_LINK);
139 /* Main loop for reading an archive. */
140 void
141 read_and (void (*do_something) (void))
143 enum read_header status = HEADER_STILL_UNREAD;
144 enum read_header prev_status;
145 struct timespec mtime;
147 base64_init ();
148 name_gather ();
150 open_archive (ACCESS_READ);
153 prev_status = status;
154 tar_stat_destroy (&current_stat_info);
156 status = read_header (&current_header, &current_stat_info,
157 read_header_auto);
158 switch (status)
160 case HEADER_STILL_UNREAD:
161 case HEADER_SUCCESS_EXTENDED:
162 abort ();
164 case HEADER_SUCCESS:
166 /* Valid header. We should decode next field (mode) first.
167 Ensure incoming names are null terminated. */
168 decode_header (current_header, &current_stat_info,
169 &current_format, 1);
170 if (! name_match (current_stat_info.file_name)
171 || (NEWER_OPTION_INITIALIZED (newer_mtime_option)
172 /* FIXME: We get mtime now, and again later; this causes
173 duplicate diagnostics if header.mtime is bogus. */
174 && ((mtime.tv_sec
175 = TIME_FROM_HEADER (current_header->header.mtime)),
176 /* FIXME: Grab fractional time stamps from
177 extended header. */
178 mtime.tv_nsec = 0,
179 current_stat_info.mtime = mtime,
180 OLDER_TAR_STAT_TIME (current_stat_info, m)))
181 || excluded_name (current_stat_info.file_name))
183 switch (current_header->header.typeflag)
185 case GNUTYPE_VOLHDR:
186 case GNUTYPE_MULTIVOL:
187 break;
189 case DIRTYPE:
190 if (show_omitted_dirs_option)
191 WARN ((0, 0, _("%s: Omitting"),
192 quotearg_colon (current_stat_info.file_name)));
193 /* Fall through. */
194 default:
195 skip_member ();
196 continue;
199 transform_stat_info (current_header->header.typeflag,
200 &current_stat_info);
201 (*do_something) ();
202 continue;
204 case HEADER_ZERO_BLOCK:
205 if (block_number_option)
207 char buf[UINTMAX_STRSIZE_BOUND];
208 fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
209 STRINGIFY_BIGINT (current_block_ordinal (), buf));
212 set_next_block_after (current_header);
214 if (!ignore_zeros_option)
216 char buf[UINTMAX_STRSIZE_BOUND];
218 status = read_header (&current_header, &current_stat_info,
219 read_header_auto);
220 if (status == HEADER_ZERO_BLOCK)
221 break;
222 WARNOPT (WARN_ALONE_ZERO_BLOCK,
223 (0, 0, _("A lone zero block at %s"),
224 STRINGIFY_BIGINT (current_block_ordinal (), buf)));
225 break;
227 status = prev_status;
228 continue;
230 case HEADER_END_OF_FILE:
231 if (block_number_option)
233 char buf[UINTMAX_STRSIZE_BOUND];
234 fprintf (stdlis, _("block %s: ** End of File **\n"),
235 STRINGIFY_BIGINT (current_block_ordinal (), buf));
237 break;
239 case HEADER_FAILURE:
240 /* If the previous header was good, tell them that we are
241 skipping bad ones. */
242 set_next_block_after (current_header);
243 switch (prev_status)
245 case HEADER_STILL_UNREAD:
246 ERROR ((0, 0, _("This does not look like a tar archive")));
247 /* Fall through. */
249 case HEADER_ZERO_BLOCK:
250 case HEADER_SUCCESS:
251 if (block_number_option)
253 char buf[UINTMAX_STRSIZE_BOUND];
254 off_t block_ordinal = current_block_ordinal ();
255 block_ordinal -= recent_long_name_blocks;
256 block_ordinal -= recent_long_link_blocks;
257 fprintf (stdlis, _("block %s: "),
258 STRINGIFY_BIGINT (block_ordinal, buf));
260 ERROR ((0, 0, _("Skipping to next header")));
261 break;
263 case HEADER_END_OF_FILE:
264 case HEADER_FAILURE:
265 /* We are in the middle of a cascade of errors. */
266 break;
268 case HEADER_SUCCESS_EXTENDED:
269 abort ();
271 continue;
273 break;
275 while (!all_names_found (&current_stat_info));
277 close_archive ();
278 names_notfound (); /* print names not found */
281 /* Print a header block, based on tar options. */
282 void
283 list_archive (void)
285 off_t block_ordinal = current_block_ordinal ();
287 /* Print the header block. */
288 if (verbose_option)
289 print_header (&current_stat_info, current_header, block_ordinal);
291 if (incremental_option)
293 if (verbose_option > 2)
295 if (is_dumpdir (&current_stat_info))
296 list_dumpdir (current_stat_info.dumpdir,
297 dumpdir_size (current_stat_info.dumpdir));
301 skip_member ();
304 /* Check header checksum */
305 /* The standard BSD tar sources create the checksum by adding up the
306 bytes in the header as type char. I think the type char was unsigned
307 on the PDP-11, but it's signed on the Next and Sun. It looks like the
308 sources to BSD tar were never changed to compute the checksum
309 correctly, so both the Sun and Next add the bytes of the header as
310 signed chars. This doesn't cause a problem until you get a file with
311 a name containing characters with the high bit set. So tar_checksum
312 computes two checksums -- signed and unsigned. */
314 enum read_header
315 tar_checksum (union block *header, bool silent)
317 size_t i;
318 int unsigned_sum = 0; /* the POSIX one :-) */
319 int signed_sum = 0; /* the Sun one :-( */
320 int recorded_sum;
321 uintmax_t parsed_sum;
322 char *p;
324 p = header->buffer;
325 for (i = sizeof *header; i-- != 0;)
327 unsigned_sum += (unsigned char) *p;
328 signed_sum += (signed char) (*p++);
331 if (unsigned_sum == 0)
332 return HEADER_ZERO_BLOCK;
334 /* Adjust checksum to count the "chksum" field as blanks. */
336 for (i = sizeof header->header.chksum; i-- != 0;)
338 unsigned_sum -= (unsigned char) header->header.chksum[i];
339 signed_sum -= (signed char) (header->header.chksum[i]);
341 unsigned_sum += ' ' * sizeof header->header.chksum;
342 signed_sum += ' ' * sizeof header->header.chksum;
344 parsed_sum = from_header (header->header.chksum,
345 sizeof header->header.chksum, 0,
346 (uintmax_t) 0,
347 (uintmax_t) TYPE_MAXIMUM (int), true, silent);
348 if (parsed_sum == (uintmax_t) -1)
349 return HEADER_FAILURE;
351 recorded_sum = parsed_sum;
353 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
354 return HEADER_FAILURE;
356 return HEADER_SUCCESS;
359 /* Read a block that's supposed to be a header block. Return its
360 address in *RETURN_BLOCK, and if it is good, the file's size
361 and names (file name, link name) in *INFO.
363 Return one of enum read_header describing the status of the
364 operation.
366 The MODE parameter instructs read_header what to do with special
367 header blocks, i.e.: extended POSIX, GNU long name or long link,
368 etc.:
370 read_header_auto process them automatically,
371 read_header_x_raw when a special header is read, return
372 HEADER_SUCCESS_EXTENDED without actually
373 processing the header,
374 read_header_x_global when a POSIX global header is read,
375 decode it and return HEADER_SUCCESS_EXTENDED.
377 You must always set_next_block_after(*return_block) to skip past
378 the header which this routine reads. */
380 enum read_header
381 read_header (union block **return_block, struct tar_stat_info *info,
382 enum read_header_mode mode)
384 union block *header;
385 union block *header_copy;
386 char *bp;
387 union block *data_block;
388 size_t size, written;
389 union block *next_long_name = 0;
390 union block *next_long_link = 0;
391 size_t next_long_name_blocks = 0;
392 size_t next_long_link_blocks = 0;
394 while (1)
396 enum read_header status;
398 header = find_next_block ();
399 *return_block = header;
400 if (!header)
401 return HEADER_END_OF_FILE;
403 if ((status = tar_checksum (header, false)) != HEADER_SUCCESS)
404 return status;
406 /* Good block. Decode file size and return. */
408 if (header->header.typeflag == LNKTYPE)
409 info->stat.st_size = 0; /* links 0 size on tape */
410 else
411 info->stat.st_size = OFF_FROM_HEADER (header->header.size);
413 if (header->header.typeflag == GNUTYPE_LONGNAME
414 || header->header.typeflag == GNUTYPE_LONGLINK
415 || header->header.typeflag == XHDTYPE
416 || header->header.typeflag == XGLTYPE
417 || header->header.typeflag == SOLARIS_XHDTYPE)
419 if (mode == read_header_x_raw)
420 return HEADER_SUCCESS_EXTENDED;
421 else if (header->header.typeflag == GNUTYPE_LONGNAME
422 || header->header.typeflag == GNUTYPE_LONGLINK)
424 size_t name_size = info->stat.st_size;
425 size_t n = name_size % BLOCKSIZE;
426 size = name_size + BLOCKSIZE;
427 if (n)
428 size += BLOCKSIZE - n;
430 if (name_size != info->stat.st_size || size < name_size)
431 xalloc_die ();
433 header_copy = xmalloc (size + 1);
435 if (header->header.typeflag == GNUTYPE_LONGNAME)
437 free (next_long_name);
438 next_long_name = header_copy;
439 next_long_name_blocks = size / BLOCKSIZE;
441 else
443 free (next_long_link);
444 next_long_link = header_copy;
445 next_long_link_blocks = size / BLOCKSIZE;
448 set_next_block_after (header);
449 *header_copy = *header;
450 bp = header_copy->buffer + BLOCKSIZE;
452 for (size -= BLOCKSIZE; size > 0; size -= written)
454 data_block = find_next_block ();
455 if (! data_block)
457 ERROR ((0, 0, _("Unexpected EOF in archive")));
458 break;
460 written = available_space_after (data_block);
461 if (written > size)
462 written = size;
464 memcpy (bp, data_block->buffer, written);
465 bp += written;
466 set_next_block_after ((union block *)
467 (data_block->buffer + written - 1));
470 *bp = '\0';
472 else if (header->header.typeflag == XHDTYPE
473 || header->header.typeflag == SOLARIS_XHDTYPE)
474 xheader_read (&info->xhdr, header,
475 OFF_FROM_HEADER (header->header.size));
476 else if (header->header.typeflag == XGLTYPE)
478 struct xheader xhdr;
480 if (!recent_global_header)
481 recent_global_header = xmalloc (sizeof *recent_global_header);
482 memcpy (recent_global_header, header,
483 sizeof *recent_global_header);
484 memset (&xhdr, 0, sizeof xhdr);
485 xheader_read (&xhdr, header,
486 OFF_FROM_HEADER (header->header.size));
487 xheader_decode_global (&xhdr);
488 xheader_destroy (&xhdr);
489 if (mode == read_header_x_global)
490 return HEADER_SUCCESS_EXTENDED;
493 /* Loop! */
496 else
498 char const *name;
499 struct posix_header const *h = &header->header;
500 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
502 free (recent_long_name);
504 if (next_long_name)
506 name = next_long_name->buffer + BLOCKSIZE;
507 recent_long_name = next_long_name;
508 recent_long_name_blocks = next_long_name_blocks;
510 else
512 /* Accept file names as specified by POSIX.1-1996
513 section 10.1.1. */
514 char *np = namebuf;
516 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
518 memcpy (np, h->prefix, sizeof h->prefix);
519 np[sizeof h->prefix] = '\0';
520 np += strlen (np);
521 *np++ = '/';
523 memcpy (np, h->name, sizeof h->name);
524 np[sizeof h->name] = '\0';
525 name = namebuf;
526 recent_long_name = 0;
527 recent_long_name_blocks = 0;
529 assign_string (&info->orig_file_name, name);
530 assign_string (&info->file_name, name);
531 info->had_trailing_slash = strip_trailing_slashes (info->file_name);
533 free (recent_long_link);
535 if (next_long_link)
537 name = next_long_link->buffer + BLOCKSIZE;
538 recent_long_link = next_long_link;
539 recent_long_link_blocks = next_long_link_blocks;
541 else
543 memcpy (namebuf, h->linkname, sizeof h->linkname);
544 namebuf[sizeof h->linkname] = '\0';
545 name = namebuf;
546 recent_long_link = 0;
547 recent_long_link_blocks = 0;
549 assign_string (&info->link_name, name);
551 return HEADER_SUCCESS;
556 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
558 /* Decode things from a file HEADER block into STAT_INFO, also setting
559 *FORMAT_POINTER depending on the header block format. If
560 DO_USER_GROUP, decode the user/group information (this is useful
561 for extraction, but waste time when merely listing).
563 read_header() has already decoded the checksum and length, so we don't.
565 This routine should *not* be called twice for the same block, since
566 the two calls might use different DO_USER_GROUP values and thus
567 might end up with different uid/gid for the two calls. If anybody
568 wants the uid/gid they should decode it first, and other callers
569 should decode it without uid/gid before calling a routine,
570 e.g. print_header, that assumes decoded data. */
571 void
572 decode_header (union block *header, struct tar_stat_info *stat_info,
573 enum archive_format *format_pointer, int do_user_group)
575 enum archive_format format;
576 unsigned hbits; /* high bits of the file mode. */
577 mode_t mode = MODE_FROM_HEADER (header->header.mode, &hbits);
579 if (strcmp (header->header.magic, TMAGIC) == 0)
581 if (header->star_header.prefix[130] == 0
582 && ISOCTAL (header->star_header.atime[0])
583 && header->star_header.atime[11] == ' '
584 && ISOCTAL (header->star_header.ctime[0])
585 && header->star_header.ctime[11] == ' ')
586 format = STAR_FORMAT;
587 else if (stat_info->xhdr.size)
588 format = POSIX_FORMAT;
589 else
590 format = USTAR_FORMAT;
592 else if (strcmp (header->buffer + offsetof (struct posix_header, magic),
593 OLDGNU_MAGIC)
594 == 0)
595 format = hbits ? OLDGNU_FORMAT : GNU_FORMAT;
596 else
597 format = V7_FORMAT;
598 *format_pointer = format;
600 stat_info->stat.st_mode = mode;
601 stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime);
602 stat_info->mtime.tv_nsec = 0;
603 assign_string (&stat_info->uname,
604 header->header.uname[0] ? header->header.uname : NULL);
605 assign_string (&stat_info->gname,
606 header->header.gname[0] ? header->header.gname : NULL);
608 xheader_xattr_init (stat_info);
610 if (format == OLDGNU_FORMAT && incremental_option)
612 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime);
613 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime);
614 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
616 else if (format == STAR_FORMAT)
618 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime);
619 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime);
620 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
622 else
623 stat_info->atime = stat_info->ctime = start_time;
625 if (format == V7_FORMAT)
627 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
628 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
629 stat_info->stat.st_rdev = 0;
631 else
633 if (do_user_group)
635 /* FIXME: Decide if this should somewhat depend on -p. */
637 if (numeric_owner_option
638 || !*header->header.uname
639 || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid))
640 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
642 if (numeric_owner_option
643 || !*header->header.gname
644 || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid))
645 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
648 switch (header->header.typeflag)
650 case BLKTYPE:
651 case CHRTYPE:
652 stat_info->stat.st_rdev =
653 makedev (MAJOR_FROM_HEADER (header->header.devmajor),
654 MINOR_FROM_HEADER (header->header.devminor));
655 break;
657 default:
658 stat_info->stat.st_rdev = 0;
662 stat_info->archive_file_size = stat_info->stat.st_size;
663 xheader_decode (stat_info);
665 if (sparse_member_p (stat_info))
667 sparse_fixup_header (stat_info);
668 stat_info->is_sparse = true;
670 else
672 stat_info->is_sparse = false;
673 if (((current_format == GNU_FORMAT
674 || current_format == OLDGNU_FORMAT)
675 && current_header->header.typeflag == GNUTYPE_DUMPDIR)
676 || stat_info->dumpdir)
677 stat_info->is_dumpdir = true;
682 /* Convert buffer at WHERE0 of size DIGS from external format to
683 uintmax_t. DIGS must be positive. If TYPE is nonnull, the data
684 are of type TYPE. The buffer must represent a value in the range
685 -MINUS_MINVAL through MAXVAL. If OCTAL_ONLY, allow only octal
686 numbers instead of the other GNU extensions. Return -1 on error,
687 diagnosing the error if TYPE is nonnull and if !SILENT. */
688 static uintmax_t
689 from_header (char const *where0, size_t digs, char const *type,
690 uintmax_t minus_minval, uintmax_t maxval,
691 bool octal_only, bool silent)
693 uintmax_t value;
694 char const *where = where0;
695 char const *lim = where + digs;
696 int negative = 0;
698 /* Accommodate buggy tar of unknown vintage, which outputs leading
699 NUL if the previous field overflows. */
700 where += !*where;
702 /* Accommodate older tars, which output leading spaces. */
703 for (;;)
705 if (where == lim)
707 if (type && !silent)
708 ERROR ((0, 0,
709 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
710 etc.) */
711 _("Blanks in header where numeric %s value expected"),
712 type));
713 return -1;
715 if (!ISSPACE ((unsigned char) *where))
716 break;
717 where++;
720 value = 0;
721 if (ISODIGIT (*where))
723 char const *where1 = where;
724 uintmax_t overflow = 0;
726 for (;;)
728 value += *where++ - '0';
729 if (where == lim || ! ISODIGIT (*where))
730 break;
731 overflow |= value ^ (value << LG_8 >> LG_8);
732 value <<= LG_8;
735 /* Parse the output of older, unportable tars, which generate
736 negative values in two's complement octal. If the leading
737 nonzero digit is 1, we can't recover the original value
738 reliably; so do this only if the digit is 2 or more. This
739 catches the common case of 32-bit negative time stamps. */
740 if ((overflow || maxval < value) && '2' <= *where1 && type)
742 /* Compute the negative of the input value, assuming two's
743 complement. */
744 int digit = (*where1 - '0') | 4;
745 overflow = 0;
746 value = 0;
747 where = where1;
748 for (;;)
750 value += 7 - digit;
751 where++;
752 if (where == lim || ! ISODIGIT (*where))
753 break;
754 digit = *where - '0';
755 overflow |= value ^ (value << LG_8 >> LG_8);
756 value <<= LG_8;
758 value++;
759 overflow |= !value;
761 if (!overflow && value <= minus_minval)
763 if (!silent)
764 WARN ((0, 0,
765 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
766 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
767 (int) (where - where1), where1, type));
768 negative = 1;
772 if (overflow)
774 if (type && !silent)
775 ERROR ((0, 0,
776 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
777 _("Archive octal value %.*s is out of %s range"),
778 (int) (where - where1), where1, type));
779 return -1;
782 else if (octal_only)
784 /* Suppress the following extensions. */
786 else if (*where == '-' || *where == '+')
788 /* Parse base-64 output produced only by tar test versions
789 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
790 Support for this will be withdrawn in future releases. */
791 int dig;
792 if (!silent)
794 static bool warned_once;
795 if (! warned_once)
797 warned_once = true;
798 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
801 negative = *where++ == '-';
802 while (where != lim
803 && (dig = base64_map[(unsigned char) *where]) < 64)
805 if (value << LG_64 >> LG_64 != value)
807 char *string = alloca (digs + 1);
808 memcpy (string, where0, digs);
809 string[digs] = '\0';
810 if (type && !silent)
811 ERROR ((0, 0,
812 _("Archive signed base-64 string %s is out of %s range"),
813 quote (string), type));
814 return -1;
816 value = (value << LG_64) | dig;
817 where++;
820 else if (*where == '\200' /* positive base-256 */
821 || *where == '\377' /* negative base-256 */)
823 /* Parse base-256 output. A nonnegative number N is
824 represented as (256**DIGS)/2 + N; a negative number -N is
825 represented as (256**DIGS) - N, i.e. as two's complement.
826 The representation guarantees that the leading bit is
827 always on, so that we don't confuse this format with the
828 others (assuming ASCII bytes of 8 bits or more). */
829 int signbit = *where & (1 << (LG_256 - 2));
830 uintmax_t topbits = (((uintmax_t) - signbit)
831 << (CHAR_BIT * sizeof (uintmax_t)
832 - LG_256 - (LG_256 - 2)));
833 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
834 for (;;)
836 value = (value << LG_256) + (unsigned char) *where++;
837 if (where == lim)
838 break;
839 if (((value << LG_256 >> LG_256) | topbits) != value)
841 if (type && !silent)
842 ERROR ((0, 0,
843 _("Archive base-256 value is out of %s range"),
844 type));
845 return -1;
848 negative = signbit;
849 if (negative)
850 value = -value;
853 if (where != lim && *where && !ISSPACE ((unsigned char) *where))
855 if (type)
857 char buf[1000]; /* Big enough to represent any header. */
858 static struct quoting_options *o;
860 if (!o)
862 o = clone_quoting_options (0);
863 set_quoting_style (o, locale_quoting_style);
866 while (where0 != lim && ! lim[-1])
867 lim--;
868 quotearg_buffer (buf, sizeof buf, where0, lim - where, o);
869 if (!silent)
870 ERROR ((0, 0,
871 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
872 _("Archive contains %.*s where numeric %s value expected"),
873 (int) sizeof buf, buf, type));
876 return -1;
879 if (value <= (negative ? minus_minval : maxval))
880 return negative ? -value : value;
882 if (type && !silent)
884 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
885 char maxval_buf[UINTMAX_STRSIZE_BOUND];
886 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
887 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
888 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
889 if (negative)
890 *--value_string = '-';
891 if (minus_minval)
892 *--minval_string = '-';
893 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
894 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
895 value_string, type,
896 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
899 return -1;
902 static gid_t
903 gid_from_header (const char *p, size_t s)
905 return from_header (p, s, "gid_t",
906 - (uintmax_t) TYPE_MINIMUM (gid_t),
907 (uintmax_t) TYPE_MAXIMUM (gid_t),
908 false, false);
911 static major_t
912 major_from_header (const char *p, size_t s)
914 return from_header (p, s, "major_t",
915 - (uintmax_t) TYPE_MINIMUM (major_t),
916 (uintmax_t) TYPE_MAXIMUM (major_t), false, false);
919 static minor_t
920 minor_from_header (const char *p, size_t s)
922 return from_header (p, s, "minor_t",
923 - (uintmax_t) TYPE_MINIMUM (minor_t),
924 (uintmax_t) TYPE_MAXIMUM (minor_t), false, false);
927 /* Convert P to the file mode, as understood by tar.
928 Store unrecognized mode bits (from 10th up) in HBITS. */
929 static mode_t
930 mode_from_header (const char *p, size_t s, unsigned *hbits)
932 unsigned u = from_header (p, s, "mode_t",
933 - (uintmax_t) TYPE_MINIMUM (mode_t),
934 TYPE_MAXIMUM (uintmax_t), false, false);
935 mode_t mode = ((u & TSUID ? S_ISUID : 0)
936 | (u & TSGID ? S_ISGID : 0)
937 | (u & TSVTX ? S_ISVTX : 0)
938 | (u & TUREAD ? S_IRUSR : 0)
939 | (u & TUWRITE ? S_IWUSR : 0)
940 | (u & TUEXEC ? S_IXUSR : 0)
941 | (u & TGREAD ? S_IRGRP : 0)
942 | (u & TGWRITE ? S_IWGRP : 0)
943 | (u & TGEXEC ? S_IXGRP : 0)
944 | (u & TOREAD ? S_IROTH : 0)
945 | (u & TOWRITE ? S_IWOTH : 0)
946 | (u & TOEXEC ? S_IXOTH : 0));
947 *hbits = mode ^ u;
948 return mode;
951 off_t
952 off_from_header (const char *p, size_t s)
954 /* Negative offsets are not allowed in tar files, so invoke
955 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
956 return from_header (p, s, "off_t", (uintmax_t) 0,
957 (uintmax_t) TYPE_MAXIMUM (off_t), false, false);
960 static time_t
961 time_from_header (const char *p, size_t s)
963 return from_header (p, s, "time_t",
964 - (uintmax_t) TYPE_MINIMUM (time_t),
965 (uintmax_t) TYPE_MAXIMUM (time_t), false, false);
968 static uid_t
969 uid_from_header (const char *p, size_t s)
971 return from_header (p, s, "uid_t",
972 - (uintmax_t) TYPE_MINIMUM (uid_t),
973 (uintmax_t) TYPE_MAXIMUM (uid_t), false, false);
976 uintmax_t
977 uintmax_from_header (const char *p, size_t s)
979 return from_header (p, s, "uintmax_t", (uintmax_t) 0,
980 TYPE_MAXIMUM (uintmax_t), false, false);
984 /* Return a printable representation of T. The result points to
985 static storage that can be reused in the next call to this
986 function, to ctime, or to asctime. If FULL_TIME, then output the
987 time stamp to its full resolution; otherwise, just output it to
988 1-minute resolution. */
989 char const *
990 tartime (struct timespec t, bool full_time)
992 enum { fraclen = sizeof ".FFFFFFFFF" - 1 };
993 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
994 INT_STRLEN_BOUND (int) + 16)
995 + fraclen];
996 struct tm *tm;
997 time_t s = t.tv_sec;
998 int ns = t.tv_nsec;
999 bool negative = s < 0;
1000 char *p;
1002 if (negative && ns != 0)
1004 s++;
1005 ns = 1000000000 - ns;
1008 tm = utc_option ? gmtime (&s) : localtime (&s);
1009 if (tm)
1011 if (full_time)
1013 sprintf (buffer, "%04ld-%02d-%02d %02d:%02d:%02d",
1014 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
1015 tm->tm_hour, tm->tm_min, tm->tm_sec);
1016 code_ns_fraction (ns, buffer + strlen (buffer));
1018 else
1019 sprintf (buffer, "%04ld-%02d-%02d %02d:%02d",
1020 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
1021 tm->tm_hour, tm->tm_min);
1022 return buffer;
1025 /* The time stamp cannot be broken down, most likely because it
1026 is out of range. Convert it as an integer,
1027 right-adjusted in a field with the same width as the usual
1028 4-year ISO time format. */
1029 p = umaxtostr (negative ? - (uintmax_t) s : s,
1030 buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen);
1031 if (negative)
1032 *--p = '-';
1033 while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM"
1034 + (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1035 < p)
1036 *--p = ' ';
1037 if (full_time)
1038 code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen);
1039 return p;
1042 /* Actually print it.
1044 Plain and fancy file header block logging. Non-verbose just prints
1045 the name, e.g. for "tar t" or "tar x". This should just contain
1046 file names, so it can be fed back into tar with xargs or the "-T"
1047 option. The verbose option can give a bunch of info, one line per
1048 file. I doubt anybody tries to parse its format, or if they do,
1049 they shouldn't. Unix tar is pretty random here anyway. */
1052 /* Width of "user/group size", with initial value chosen
1053 heuristically. This grows as needed, though this may cause some
1054 stairstepping in the output. Make it too small and the output will
1055 almost always look ragged. Make it too large and the output will
1056 be spaced out too far. */
1057 static int ugswidth = 19;
1059 /* Width of printed time stamps. It grows if longer time stamps are
1060 found (typically, those with nanosecond resolution). Like
1061 USGWIDTH, some stairstepping may occur. */
1062 static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1;
1064 static bool volume_label_printed = false;
1066 static void
1067 simple_print_header (struct tar_stat_info *st, union block *blk,
1068 off_t block_ordinal)
1070 char modes[12];
1071 char const *time_stamp;
1072 int time_stamp_len;
1073 char *temp_name;
1075 /* These hold formatted ints. */
1076 char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND];
1077 char *user, *group;
1078 char size[2 * UINTMAX_STRSIZE_BOUND];
1079 /* holds formatted size or major,minor */
1080 char uintbuf[UINTMAX_STRSIZE_BOUND];
1081 int pad;
1082 int sizelen;
1084 if (show_transformed_names_option)
1085 temp_name = st->file_name ? st->file_name : st->orig_file_name;
1086 else
1087 temp_name = st->orig_file_name ? st->orig_file_name : st->file_name;
1089 if (block_number_option)
1091 char buf[UINTMAX_STRSIZE_BOUND];
1092 if (block_ordinal < 0)
1093 block_ordinal = current_block_ordinal ();
1094 block_ordinal -= recent_long_name_blocks;
1095 block_ordinal -= recent_long_link_blocks;
1096 fprintf (stdlis, _("block %s: "),
1097 STRINGIFY_BIGINT (block_ordinal, buf));
1100 if (verbose_option <= 1)
1102 /* Just the fax, mam. */
1103 fprintf (stdlis, "%s\n", quotearg (temp_name));
1105 else
1107 /* File type and modes. */
1109 modes[0] = '?';
1110 switch (blk->header.typeflag)
1112 case GNUTYPE_VOLHDR:
1113 volume_label_printed = true;
1114 modes[0] = 'V';
1115 break;
1117 case GNUTYPE_MULTIVOL:
1118 modes[0] = 'M';
1119 break;
1121 case GNUTYPE_LONGNAME:
1122 case GNUTYPE_LONGLINK:
1123 modes[0] = 'L';
1124 ERROR ((0, 0, _("Unexpected long name header")));
1125 break;
1127 case GNUTYPE_SPARSE:
1128 case REGTYPE:
1129 case AREGTYPE:
1130 modes[0] = '-';
1131 if (temp_name[strlen (temp_name) - 1] == '/')
1132 modes[0] = 'd';
1133 break;
1134 case LNKTYPE:
1135 modes[0] = 'h';
1136 break;
1137 case GNUTYPE_DUMPDIR:
1138 modes[0] = 'd';
1139 break;
1140 case DIRTYPE:
1141 modes[0] = 'd';
1142 break;
1143 case SYMTYPE:
1144 modes[0] = 'l';
1145 break;
1146 case BLKTYPE:
1147 modes[0] = 'b';
1148 break;
1149 case CHRTYPE:
1150 modes[0] = 'c';
1151 break;
1152 case FIFOTYPE:
1153 modes[0] = 'p';
1154 break;
1155 case CONTTYPE:
1156 modes[0] = 'C';
1157 break;
1160 pax_decode_mode (st->stat.st_mode, modes + 1);
1162 /* extended attributes: GNU `ls -l'-like preview */
1163 xattrs_print_char (st, modes + 10);
1165 /* Time stamp. */
1167 time_stamp = tartime (st->mtime, full_time_option);
1168 time_stamp_len = strlen (time_stamp);
1169 if (datewidth < time_stamp_len)
1170 datewidth = time_stamp_len;
1172 /* User and group names. */
1174 if (st->uname
1175 && st->uname[0]
1176 && current_format != V7_FORMAT
1177 && !numeric_owner_option)
1178 user = st->uname;
1179 else
1181 /* Try parsing it as an unsigned integer first, and as a
1182 uid_t if that fails. This method can list positive user
1183 ids that are too large to fit in a uid_t. */
1184 uintmax_t u = from_header (blk->header.uid,
1185 sizeof blk->header.uid, 0,
1186 (uintmax_t) 0,
1187 (uintmax_t) TYPE_MAXIMUM (uintmax_t),
1188 false, false);
1189 if (u != -1)
1190 user = STRINGIFY_BIGINT (u, uform);
1191 else
1193 sprintf (uform, "%ld",
1194 (long) UID_FROM_HEADER (blk->header.uid));
1195 user = uform;
1199 if (st->gname
1200 && st->gname[0]
1201 && current_format != V7_FORMAT
1202 && !numeric_owner_option)
1203 group = st->gname;
1204 else
1206 /* Try parsing it as an unsigned integer first, and as a
1207 gid_t if that fails. This method can list positive group
1208 ids that are too large to fit in a gid_t. */
1209 uintmax_t g = from_header (blk->header.gid,
1210 sizeof blk->header.gid, 0,
1211 (uintmax_t) 0,
1212 (uintmax_t) TYPE_MAXIMUM (uintmax_t),
1213 false, false);
1214 if (g != -1)
1215 group = STRINGIFY_BIGINT (g, gform);
1216 else
1218 sprintf (gform, "%ld",
1219 (long) GID_FROM_HEADER (blk->header.gid));
1220 group = gform;
1224 /* Format the file size or major/minor device numbers. */
1226 switch (blk->header.typeflag)
1228 case CHRTYPE:
1229 case BLKTYPE:
1230 strcpy (size,
1231 STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf));
1232 strcat (size, ",");
1233 strcat (size,
1234 STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf));
1235 break;
1237 default:
1238 /* st->stat.st_size keeps stored file size */
1239 strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf));
1240 break;
1243 /* Figure out padding and print the whole line. */
1245 sizelen = strlen (size);
1246 pad = strlen (user) + 1 + strlen (group) + 1 + sizelen;
1247 if (pad > ugswidth)
1248 ugswidth = pad;
1250 fprintf (stdlis, "%s %s/%s %*s %-*s",
1251 modes, user, group, ugswidth - pad + sizelen, size,
1252 datewidth, time_stamp);
1254 fprintf (stdlis, " %s", quotearg (temp_name));
1256 switch (blk->header.typeflag)
1258 case SYMTYPE:
1259 fprintf (stdlis, " -> %s\n", quotearg (st->link_name));
1260 break;
1262 case LNKTYPE:
1263 fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name));
1264 break;
1266 default:
1268 char type_string[2];
1269 type_string[0] = blk->header.typeflag;
1270 type_string[1] = '\0';
1271 fprintf (stdlis, _(" unknown file type %s\n"),
1272 quote (type_string));
1274 break;
1276 case AREGTYPE:
1277 case REGTYPE:
1278 case GNUTYPE_SPARSE:
1279 case CHRTYPE:
1280 case BLKTYPE:
1281 case DIRTYPE:
1282 case FIFOTYPE:
1283 case CONTTYPE:
1284 case GNUTYPE_DUMPDIR:
1285 putc ('\n', stdlis);
1286 break;
1288 case GNUTYPE_LONGLINK:
1289 fprintf (stdlis, _("--Long Link--\n"));
1290 break;
1292 case GNUTYPE_LONGNAME:
1293 fprintf (stdlis, _("--Long Name--\n"));
1294 break;
1296 case GNUTYPE_VOLHDR:
1297 fprintf (stdlis, _("--Volume Header--\n"));
1298 break;
1300 case GNUTYPE_MULTIVOL:
1301 strcpy (size,
1302 STRINGIFY_BIGINT
1303 (UINTMAX_FROM_HEADER (blk->oldgnu_header.offset),
1304 uintbuf));
1305 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1306 break;
1309 fflush (stdlis);
1310 xattrs_print (st);
1314 static void
1315 print_volume_label (void)
1317 struct tar_stat_info vstat;
1318 union block vblk;
1319 enum archive_format dummy;
1321 memset (&vblk, 0, sizeof (vblk));
1322 vblk.header.typeflag = GNUTYPE_VOLHDR;
1323 if (recent_global_header)
1324 memcpy (vblk.header.mtime, recent_global_header->header.mtime,
1325 sizeof vblk.header.mtime);
1326 tar_stat_init (&vstat);
1327 assign_string (&vstat.file_name, ".");
1328 decode_header (&vblk, &vstat, &dummy, 0);
1329 assign_string (&vstat.file_name, volume_label);
1330 simple_print_header (&vstat, &vblk, 0);
1331 tar_stat_destroy (&vstat);
1334 void
1335 print_header (struct tar_stat_info *st, union block *blk,
1336 off_t block_ordinal)
1338 if (current_format == POSIX_FORMAT && !volume_label_printed && volume_label)
1340 print_volume_label ();
1341 volume_label_printed = true;
1344 simple_print_header (st, blk, block_ordinal);
1347 /* Print a similar line when we make a directory automatically. */
1348 void
1349 print_for_mkdir (char *dirname, int length, mode_t mode)
1351 char modes[11];
1353 if (verbose_option > 1)
1355 /* File type and modes. */
1357 modes[0] = 'd';
1358 pax_decode_mode (mode, modes + 1);
1360 if (block_number_option)
1362 char buf[UINTMAX_STRSIZE_BOUND];
1363 fprintf (stdlis, _("block %s: "),
1364 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1367 fprintf (stdlis, "%s %*s %s\n", modes, ugswidth + 1 + datewidth,
1368 _("Creating directory:"), quotearg (dirname));
1372 /* Skip over SIZE bytes of data in blocks in the archive. */
1373 void
1374 skip_file (off_t size)
1376 union block *x;
1378 /* FIXME: Make sure mv_begin_read is always called before it */
1380 if (seekable_archive)
1382 off_t nblk = seek_archive (size);
1383 if (nblk >= 0)
1384 size -= nblk * BLOCKSIZE;
1385 else
1386 seekable_archive = false;
1389 mv_size_left (size);
1391 while (size > 0)
1393 x = find_next_block ();
1394 if (! x)
1395 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1397 set_next_block_after (x);
1398 size -= BLOCKSIZE;
1399 mv_size_left (size);
1403 /* Skip the current member in the archive.
1404 NOTE: Current header must be decoded before calling this function. */
1405 void
1406 skip_member (void)
1408 if (!current_stat_info.skipped)
1410 char save_typeflag = current_header->header.typeflag;
1411 set_next_block_after (current_header);
1413 mv_begin_read (&current_stat_info);
1415 if (current_stat_info.is_sparse)
1416 sparse_skip_file (&current_stat_info);
1417 else if (save_typeflag != DIRTYPE)
1418 skip_file (current_stat_info.stat.st_size);
1420 mv_end ();
1424 void
1425 test_archive_label (void)
1427 base64_init ();
1428 name_gather ();
1430 open_archive (ACCESS_READ);
1431 if (read_header (&current_header, &current_stat_info, read_header_auto)
1432 == HEADER_SUCCESS)
1434 decode_header (current_header,
1435 &current_stat_info, &current_format, 0);
1436 if (current_header->header.typeflag == GNUTYPE_VOLHDR)
1437 assign_string (&volume_label, current_header->header.name);
1439 if (volume_label)
1441 if (verbose_option)
1442 print_volume_label ();
1443 if (!name_match (volume_label) && multi_volume_option)
1445 char *s = drop_volume_label_suffix (volume_label);
1446 name_match (s);
1447 free (s);
1451 close_archive ();
1452 label_notfound ();