Bugfix
[tar.git] / src / list.c
blobba9c306e9aa0a33ef963397ce147c0ffd5892ef0
1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright 1988, 1992-1994, 1996-2001, 2003-2007, 2010, 2012-2017 Free
4 Software Foundation, Inc.
6 This file is part of GNU tar.
8 GNU tar is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GNU tar is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 Written by John Gilmore, on 1985-08-26. */
23 #include <system.h>
24 #include <inttostr.h>
25 #include <quotearg.h>
26 #include <time.h>
27 #include "common.h"
29 union block *current_header; /* points to current archive header */
30 enum archive_format current_format; /* recognized format */
31 union block *recent_long_name; /* recent long name header and contents */
32 union block *recent_long_link; /* likewise, for long link */
33 size_t recent_long_name_blocks; /* number of blocks in recent_long_name */
34 size_t recent_long_link_blocks; /* likewise, for long link */
35 static union block *recent_global_header; /* Recent global header block */
37 #define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where))
38 #define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where))
39 #define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where))
40 #define MODE_FROM_HEADER(where, hbits) \
41 mode_from_header (where, sizeof (where), hbits)
42 #define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where))
43 #define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where))
45 static gid_t gid_from_header (const char *buf, size_t size);
46 static major_t major_from_header (const char *buf, size_t size);
47 static minor_t minor_from_header (const char *buf, size_t size);
48 static mode_t mode_from_header (const char *buf, size_t size, bool *hbits);
49 static time_t time_from_header (const char *buf, size_t size);
50 static uid_t uid_from_header (const char *buf, size_t size);
51 static intmax_t from_header (const char *, size_t, const char *,
52 intmax_t, uintmax_t, bool, bool);
54 /* Base 64 digits; see Internet RFC 2045 Table 1. */
55 static char const base_64_digits[64] =
57 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
58 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
59 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
60 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
61 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
64 /* Table of base-64 digit values indexed by unsigned chars.
65 The value is 64 for unsigned chars that are not base-64 digits. */
66 static char base64_map[UCHAR_MAX + 1];
68 static void
69 base64_init (void)
71 int i;
72 memset (base64_map, 64, sizeof base64_map);
73 for (i = 0; i < 64; i++)
74 base64_map[(int) base_64_digits[i]] = i;
77 static char *
78 decode_xform (char *file_name, void *data)
80 int type = *(int*)data;
82 switch (type)
84 case XFORM_SYMLINK:
85 /* FIXME: It is not quite clear how and to which extent are the symbolic
86 links subject to filename transformation. In the absence of another
87 solution, symbolic links are exempt from component stripping and
88 name suffix normalization, but subject to filename transformation
89 proper. */
90 return file_name;
92 case XFORM_LINK:
93 file_name = safer_name_suffix (file_name, true, absolute_names_option);
94 break;
96 case XFORM_REGFILE:
97 file_name = safer_name_suffix (file_name, false, absolute_names_option);
98 break;
101 if (strip_name_components)
103 size_t prefix_len = stripped_prefix_len (file_name,
104 strip_name_components);
105 if (prefix_len == (size_t) -1)
106 prefix_len = strlen (file_name);
107 file_name += prefix_len;
109 return file_name;
112 static bool
113 transform_member_name (char **pinput, int type)
115 return transform_name_fp (pinput, type, decode_xform, &type);
118 static void
119 enforce_one_top_level (char **pfile_name)
121 char *file_name = *pfile_name;
122 char *p;
124 for (p = file_name; *p && (ISSLASH (*p) || *p == '.'); p++)
127 if (*p)
129 int pos = strlen (one_top_level_dir);
130 if (strncmp (p, one_top_level_dir, pos) == 0)
132 if (ISSLASH (p[pos]) || p[pos] == 0)
133 return;
136 *pfile_name = make_file_name (one_top_level_dir, file_name);
137 normalize_filename_x (*pfile_name);
139 else
140 *pfile_name = xstrdup (one_top_level_dir);
141 free (file_name);
144 void
145 transform_stat_info (int typeflag, struct tar_stat_info *stat_info)
147 if (typeflag == GNUTYPE_VOLHDR)
148 /* Name transformations don't apply to volume headers. */
149 return;
151 transform_member_name (&stat_info->file_name, XFORM_REGFILE);
152 switch (typeflag)
154 case SYMTYPE:
155 transform_member_name (&stat_info->link_name, XFORM_SYMLINK);
156 break;
158 case LNKTYPE:
159 transform_member_name (&stat_info->link_name, XFORM_LINK);
162 if (one_top_level_option)
163 enforce_one_top_level (&current_stat_info.file_name);
166 /* Main loop for reading an archive. */
167 void
168 read_and (void (*do_something) (void))
170 enum read_header status = HEADER_STILL_UNREAD;
171 enum read_header prev_status;
172 struct timespec mtime;
174 base64_init ();
175 name_gather ();
177 open_archive (ACCESS_READ);
180 prev_status = status;
181 tar_stat_destroy (&current_stat_info);
183 status = read_header (&current_header, &current_stat_info,
184 read_header_auto);
185 switch (status)
187 case HEADER_STILL_UNREAD:
188 case HEADER_SUCCESS_EXTENDED:
189 abort ();
191 case HEADER_SUCCESS:
193 /* Valid header. We should decode next field (mode) first.
194 Ensure incoming names are null terminated. */
195 decode_header (current_header, &current_stat_info,
196 &current_format, 1);
197 if (! name_match (current_stat_info.file_name)
198 || (TIME_OPTION_INITIALIZED (newer_mtime_option)
199 /* FIXME: We get mtime now, and again later; this causes
200 duplicate diagnostics if header.mtime is bogus. */
201 && ((mtime.tv_sec
202 = TIME_FROM_HEADER (current_header->header.mtime)),
203 /* FIXME: Grab fractional time stamps from
204 extended header. */
205 mtime.tv_nsec = 0,
206 current_stat_info.mtime = mtime,
207 OLDER_TAR_STAT_TIME (current_stat_info, m)))
208 || excluded_name (current_stat_info.file_name,
209 current_stat_info.parent))
211 switch (current_header->header.typeflag)
213 case GNUTYPE_VOLHDR:
214 case GNUTYPE_MULTIVOL:
215 break;
217 case DIRTYPE:
218 if (show_omitted_dirs_option)
219 WARN ((0, 0, _("%s: Omitting"),
220 quotearg_colon (current_stat_info.file_name)));
221 FALLTHROUGH;
222 default:
223 skip_member ();
224 continue;
228 transform_stat_info (current_header->header.typeflag,
229 &current_stat_info);
230 (*do_something) ();
231 continue;
233 case HEADER_ZERO_BLOCK:
234 if (block_number_option)
236 char buf[UINTMAX_STRSIZE_BOUND];
237 fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
238 STRINGIFY_BIGINT (current_block_ordinal (), buf));
241 set_next_block_after (current_header);
243 if (!ignore_zeros_option)
245 char buf[UINTMAX_STRSIZE_BOUND];
247 status = read_header (&current_header, &current_stat_info,
248 read_header_auto);
249 if (status == HEADER_ZERO_BLOCK)
250 break;
251 WARNOPT (WARN_ALONE_ZERO_BLOCK,
252 (0, 0, _("A lone zero block at %s"),
253 STRINGIFY_BIGINT (current_block_ordinal (), buf)));
254 break;
256 status = prev_status;
257 continue;
259 case HEADER_END_OF_FILE:
260 if (block_number_option)
262 char buf[UINTMAX_STRSIZE_BOUND];
263 fprintf (stdlis, _("block %s: ** End of File **\n"),
264 STRINGIFY_BIGINT (current_block_ordinal (), buf));
266 break;
268 case HEADER_FAILURE:
269 /* If the previous header was good, tell them that we are
270 skipping bad ones. */
271 set_next_block_after (current_header);
272 switch (prev_status)
274 case HEADER_STILL_UNREAD:
275 ERROR ((0, 0, _("This does not look like a tar archive")));
276 FALLTHROUGH;
277 case HEADER_ZERO_BLOCK:
278 case HEADER_SUCCESS:
279 if (block_number_option)
281 char buf[UINTMAX_STRSIZE_BOUND];
282 off_t block_ordinal = current_block_ordinal ();
283 block_ordinal -= recent_long_name_blocks;
284 block_ordinal -= recent_long_link_blocks;
285 fprintf (stdlis, _("block %s: "),
286 STRINGIFY_BIGINT (block_ordinal, buf));
288 ERROR ((0, 0, _("Skipping to next header")));
289 break;
291 case HEADER_END_OF_FILE:
292 case HEADER_FAILURE:
293 /* We are in the middle of a cascade of errors. */
294 break;
296 case HEADER_SUCCESS_EXTENDED:
297 abort ();
299 continue;
301 break;
303 while (!all_names_found (&current_stat_info));
305 close_archive ();
306 names_notfound (); /* print names not found */
309 /* Print a header block, based on tar options. */
310 void
311 list_archive (void)
313 off_t block_ordinal = current_block_ordinal ();
315 /* Print the header block. */
316 if (verbose_option)
317 print_header (&current_stat_info, current_header, block_ordinal);
319 if (incremental_option)
321 if (verbose_option > 2)
323 if (is_dumpdir (&current_stat_info))
324 list_dumpdir (current_stat_info.dumpdir,
325 dumpdir_size (current_stat_info.dumpdir));
329 skip_member ();
332 /* Check header checksum */
333 /* The standard BSD tar sources create the checksum by adding up the
334 bytes in the header as type char. I think the type char was unsigned
335 on the PDP-11, but it's signed on the Next and Sun. It looks like the
336 sources to BSD tar were never changed to compute the checksum
337 correctly, so both the Sun and Next add the bytes of the header as
338 signed chars. This doesn't cause a problem until you get a file with
339 a name containing characters with the high bit set. So tar_checksum
340 computes two checksums -- signed and unsigned. */
342 enum read_header
343 tar_checksum (union block *header, bool silent)
345 size_t i;
346 int unsigned_sum = 0; /* the POSIX one :-) */
347 int signed_sum = 0; /* the Sun one :-( */
348 int recorded_sum;
349 int parsed_sum;
350 char *p;
352 p = header->buffer;
353 for (i = sizeof *header; i-- != 0;)
355 unsigned_sum += (unsigned char) *p;
356 signed_sum += (signed char) (*p++);
359 if (unsigned_sum == 0)
360 return HEADER_ZERO_BLOCK;
362 /* Adjust checksum to count the "chksum" field as blanks. */
364 for (i = sizeof header->header.chksum; i-- != 0;)
366 unsigned_sum -= (unsigned char) header->header.chksum[i];
367 signed_sum -= (signed char) (header->header.chksum[i]);
369 unsigned_sum += ' ' * sizeof header->header.chksum;
370 signed_sum += ' ' * sizeof header->header.chksum;
372 parsed_sum = from_header (header->header.chksum,
373 sizeof header->header.chksum, 0,
374 0, INT_MAX, true, silent);
375 if (parsed_sum < 0)
376 return HEADER_FAILURE;
378 recorded_sum = parsed_sum;
380 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
381 return HEADER_FAILURE;
383 return HEADER_SUCCESS;
386 /* Read a block that's supposed to be a header block. Return its
387 address in *RETURN_BLOCK, and if it is good, the file's size
388 and names (file name, link name) in *INFO.
390 Return one of enum read_header describing the status of the
391 operation.
393 The MODE parameter instructs read_header what to do with special
394 header blocks, i.e.: extended POSIX, GNU long name or long link,
395 etc.:
397 read_header_auto process them automatically,
398 read_header_x_raw when a special header is read, return
399 HEADER_SUCCESS_EXTENDED without actually
400 processing the header,
401 read_header_x_global when a POSIX global header is read,
402 decode it and return HEADER_SUCCESS_EXTENDED.
404 You must always set_next_block_after(*return_block) to skip past
405 the header which this routine reads. */
407 enum read_header
408 read_header (union block **return_block, struct tar_stat_info *info,
409 enum read_header_mode mode)
411 union block *header;
412 union block *header_copy;
413 char *bp;
414 union block *data_block;
415 size_t size, written;
416 union block *next_long_name = 0;
417 union block *next_long_link = 0;
418 size_t next_long_name_blocks = 0;
419 size_t next_long_link_blocks = 0;
421 while (1)
423 enum read_header status;
425 header = find_next_block ();
426 *return_block = header;
427 if (!header)
428 return HEADER_END_OF_FILE;
430 if ((status = tar_checksum (header, false)) != HEADER_SUCCESS)
431 return status;
433 /* Good block. Decode file size and return. */
435 if (header->header.typeflag == LNKTYPE)
436 info->stat.st_size = 0; /* links 0 size on tape */
437 else
439 info->stat.st_size = OFF_FROM_HEADER (header->header.size);
440 if (info->stat.st_size < 0)
441 return HEADER_FAILURE;
444 if (header->header.typeflag == GNUTYPE_LONGNAME
445 || header->header.typeflag == GNUTYPE_LONGLINK
446 || header->header.typeflag == XHDTYPE
447 || header->header.typeflag == XGLTYPE
448 || header->header.typeflag == SOLARIS_XHDTYPE)
450 if (mode == read_header_x_raw)
451 return HEADER_SUCCESS_EXTENDED;
452 else if (header->header.typeflag == GNUTYPE_LONGNAME
453 || header->header.typeflag == GNUTYPE_LONGLINK)
455 size_t name_size = info->stat.st_size;
456 size_t n = name_size % BLOCKSIZE;
457 size = name_size + BLOCKSIZE;
458 if (n)
459 size += BLOCKSIZE - n;
461 if (name_size != info->stat.st_size || size < name_size)
462 xalloc_die ();
464 header_copy = xmalloc (size + 1);
466 if (header->header.typeflag == GNUTYPE_LONGNAME)
468 free (next_long_name);
469 next_long_name = header_copy;
470 next_long_name_blocks = size / BLOCKSIZE;
472 else
474 free (next_long_link);
475 next_long_link = header_copy;
476 next_long_link_blocks = size / BLOCKSIZE;
479 set_next_block_after (header);
480 *header_copy = *header;
481 bp = header_copy->buffer + BLOCKSIZE;
483 for (size -= BLOCKSIZE; size > 0; size -= written)
485 data_block = find_next_block ();
486 if (! data_block)
488 ERROR ((0, 0, _("Unexpected EOF in archive")));
489 break;
491 written = available_space_after (data_block);
492 if (written > size)
493 written = size;
495 memcpy (bp, data_block->buffer, written);
496 bp += written;
497 set_next_block_after ((union block *)
498 (data_block->buffer + written - 1));
501 *bp = '\0';
503 else if (header->header.typeflag == XHDTYPE
504 || header->header.typeflag == SOLARIS_XHDTYPE)
505 xheader_read (&info->xhdr, header,
506 OFF_FROM_HEADER (header->header.size));
507 else if (header->header.typeflag == XGLTYPE)
509 struct xheader xhdr;
511 if (!recent_global_header)
512 recent_global_header = xmalloc (sizeof *recent_global_header);
513 memcpy (recent_global_header, header,
514 sizeof *recent_global_header);
515 memset (&xhdr, 0, sizeof xhdr);
516 xheader_read (&xhdr, header,
517 OFF_FROM_HEADER (header->header.size));
518 xheader_decode_global (&xhdr);
519 xheader_destroy (&xhdr);
520 if (mode == read_header_x_global)
521 return HEADER_SUCCESS_EXTENDED;
524 /* Loop! */
527 else
529 char const *name;
530 struct posix_header const *h = &header->header;
531 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
533 free (recent_long_name);
535 if (next_long_name)
537 name = next_long_name->buffer + BLOCKSIZE;
538 recent_long_name = next_long_name;
539 recent_long_name_blocks = next_long_name_blocks;
541 else
543 /* Accept file names as specified by POSIX.1-1996
544 section 10.1.1. */
545 char *np = namebuf;
547 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
549 memcpy (np, h->prefix, sizeof h->prefix);
550 np[sizeof h->prefix] = '\0';
551 np += strlen (np);
552 *np++ = '/';
554 memcpy (np, h->name, sizeof h->name);
555 np[sizeof h->name] = '\0';
556 name = namebuf;
557 recent_long_name = 0;
558 recent_long_name_blocks = 0;
560 assign_string (&info->orig_file_name, name);
561 assign_string (&info->file_name, name);
562 info->had_trailing_slash = strip_trailing_slashes (info->file_name);
564 free (recent_long_link);
566 if (next_long_link)
568 name = next_long_link->buffer + BLOCKSIZE;
569 recent_long_link = next_long_link;
570 recent_long_link_blocks = next_long_link_blocks;
572 else
574 memcpy (namebuf, h->linkname, sizeof h->linkname);
575 namebuf[sizeof h->linkname] = '\0';
576 name = namebuf;
577 recent_long_link = 0;
578 recent_long_link_blocks = 0;
580 assign_string (&info->link_name, name);
582 return HEADER_SUCCESS;
587 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
589 /* Decode things from a file HEADER block into STAT_INFO, also setting
590 *FORMAT_POINTER depending on the header block format. If
591 DO_USER_GROUP, decode the user/group information (this is useful
592 for extraction, but waste time when merely listing).
594 read_header() has already decoded the checksum and length, so we don't.
596 This routine should *not* be called twice for the same block, since
597 the two calls might use different DO_USER_GROUP values and thus
598 might end up with different uid/gid for the two calls. If anybody
599 wants the uid/gid they should decode it first, and other callers
600 should decode it without uid/gid before calling a routine,
601 e.g. print_header, that assumes decoded data. */
602 void
603 decode_header (union block *header, struct tar_stat_info *stat_info,
604 enum archive_format *format_pointer, int do_user_group)
606 enum archive_format format;
607 bool hbits;
608 mode_t mode = MODE_FROM_HEADER (header->header.mode, &hbits);
610 if (strcmp (header->header.magic, TMAGIC) == 0)
612 if (header->star_header.prefix[130] == 0
613 && ISOCTAL (header->star_header.atime[0])
614 && header->star_header.atime[11] == ' '
615 && ISOCTAL (header->star_header.ctime[0])
616 && header->star_header.ctime[11] == ' ')
617 format = STAR_FORMAT;
618 else if (stat_info->xhdr.size)
619 format = POSIX_FORMAT;
620 else
621 format = USTAR_FORMAT;
623 else if (strcmp (header->buffer + offsetof (struct posix_header, magic),
624 OLDGNU_MAGIC)
625 == 0)
626 format = hbits ? OLDGNU_FORMAT : GNU_FORMAT;
627 else
628 format = V7_FORMAT;
629 *format_pointer = format;
631 stat_info->stat.st_mode = mode;
632 stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime);
633 stat_info->mtime.tv_nsec = 0;
634 assign_string_n (&stat_info->uname,
635 header->header.uname[0] ? header->header.uname : NULL,
636 sizeof (header->header.uname));
637 assign_string_n (&stat_info->gname,
638 header->header.gname[0] ? header->header.gname : NULL,
639 sizeof (header->header.gname));
641 xheader_xattr_init (stat_info);
643 if (format == OLDGNU_FORMAT && incremental_option)
645 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime);
646 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime);
647 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
649 else if (format == STAR_FORMAT)
651 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime);
652 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime);
653 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
655 else
656 stat_info->atime = stat_info->ctime = start_time;
658 if (format == V7_FORMAT)
660 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
661 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
662 stat_info->stat.st_rdev = 0;
664 else
666 if (do_user_group)
668 /* FIXME: Decide if this should somewhat depend on -p. */
670 if (numeric_owner_option
671 || !*header->header.uname
672 || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid))
673 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
675 if (numeric_owner_option
676 || !*header->header.gname
677 || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid))
678 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
681 switch (header->header.typeflag)
683 case BLKTYPE:
684 case CHRTYPE:
685 stat_info->stat.st_rdev =
686 makedev (MAJOR_FROM_HEADER (header->header.devmajor),
687 MINOR_FROM_HEADER (header->header.devminor));
688 break;
690 default:
691 stat_info->stat.st_rdev = 0;
695 xheader_decode (stat_info);
697 if (sparse_member_p (stat_info))
699 sparse_fixup_header (stat_info);
700 stat_info->is_sparse = true;
702 else
704 stat_info->is_sparse = false;
705 if (((current_format == GNU_FORMAT
706 || current_format == OLDGNU_FORMAT)
707 && current_header->header.typeflag == GNUTYPE_DUMPDIR)
708 || stat_info->dumpdir)
709 stat_info->is_dumpdir = true;
714 /* Convert buffer at WHERE0 of size DIGS from external format to
715 intmax_t. DIGS must be positive. If TYPE is nonnull, the data are
716 of type TYPE. The buffer must represent a value in the range
717 MINVAL through MAXVAL; if the mathematically correct result V would
718 be greater than INTMAX_MAX, return a negative integer V such that
719 (uintmax_t) V yields the correct result. If OCTAL_ONLY, allow only octal
720 numbers instead of the other GNU extensions. Return -1 on error,
721 diagnosing the error if TYPE is nonnull and if !SILENT. */
722 #if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX)
723 # error "from_header internally represents intmax_t as uintmax_t + sign"
724 #endif
725 #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
726 # error "from_header returns intmax_t to represent uintmax_t"
727 #endif
728 static intmax_t
729 from_header (char const *where0, size_t digs, char const *type,
730 intmax_t minval, uintmax_t maxval,
731 bool octal_only, bool silent)
733 uintmax_t value;
734 uintmax_t uminval = minval;
735 uintmax_t minus_minval = - uminval;
736 char const *where = where0;
737 char const *lim = where + digs;
738 bool negative = false;
740 /* Accommodate buggy tar of unknown vintage, which outputs leading
741 NUL if the previous field overflows. */
742 where += !*where;
744 /* Accommodate older tars, which output leading spaces. */
745 for (;;)
747 if (where == lim)
749 if (type && !silent)
750 ERROR ((0, 0,
751 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
752 etc.) */
753 _("Blanks in header where numeric %s value expected"),
754 type));
755 return -1;
757 if (!isspace ((unsigned char) *where))
758 break;
759 where++;
762 value = 0;
763 if (ISODIGIT (*where))
765 char const *where1 = where;
766 bool overflow = false;
768 for (;;)
770 value += *where++ - '0';
771 if (where == lim || ! ISODIGIT (*where))
772 break;
773 overflow |= value != (value << LG_8 >> LG_8);
774 value <<= LG_8;
777 /* Parse the output of older, unportable tars, which generate
778 negative values in two's complement octal. If the leading
779 nonzero digit is 1, we can't recover the original value
780 reliably; so do this only if the digit is 2 or more. This
781 catches the common case of 32-bit negative time stamps. */
782 if ((overflow || maxval < value) && '2' <= *where1 && type)
784 /* Compute the negative of the input value, assuming two's
785 complement. */
786 int digit = (*where1 - '0') | 4;
787 overflow = 0;
788 value = 0;
789 where = where1;
790 for (;;)
792 value += 7 - digit;
793 where++;
794 if (where == lim || ! ISODIGIT (*where))
795 break;
796 digit = *where - '0';
797 overflow |= value != (value << LG_8 >> LG_8);
798 value <<= LG_8;
800 value++;
801 overflow |= !value;
803 if (!overflow && value <= minus_minval)
805 if (!silent)
806 WARN ((0, 0,
807 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
808 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
809 (int) (where - where1), where1, type));
810 negative = true;
814 if (overflow)
816 if (type && !silent)
817 ERROR ((0, 0,
818 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
819 _("Archive octal value %.*s is out of %s range"),
820 (int) (where - where1), where1, type));
821 return -1;
824 else if (octal_only)
826 /* Suppress the following extensions. */
828 else if (*where == '-' || *where == '+')
830 /* Parse base-64 output produced only by tar test versions
831 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
832 Support for this will be withdrawn in future releases. */
833 int dig;
834 if (!silent)
836 static bool warned_once;
837 if (! warned_once)
839 warned_once = true;
840 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
843 negative = *where++ == '-';
844 while (where != lim
845 && (dig = base64_map[(unsigned char) *where]) < 64)
847 if (value << LG_64 >> LG_64 != value)
849 char *string = alloca (digs + 1);
850 memcpy (string, where0, digs);
851 string[digs] = '\0';
852 if (type && !silent)
853 ERROR ((0, 0,
854 _("Archive signed base-64 string %s is out of %s range"),
855 quote (string), type));
856 return -1;
858 value = (value << LG_64) | dig;
859 where++;
862 else if (*where == '\200' /* positive base-256 */
863 || *where == '\377' /* negative base-256 */)
865 /* Parse base-256 output. A nonnegative number N is
866 represented as (256**DIGS)/2 + N; a negative number -N is
867 represented as (256**DIGS) - N, i.e. as two's complement.
868 The representation guarantees that the leading bit is
869 always on, so that we don't confuse this format with the
870 others (assuming ASCII bytes of 8 bits or more). */
871 int signbit = *where & (1 << (LG_256 - 2));
872 uintmax_t topbits = (((uintmax_t) - signbit)
873 << (CHAR_BIT * sizeof (uintmax_t)
874 - LG_256 - (LG_256 - 2)));
875 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
876 for (;;)
878 value = (value << LG_256) + (unsigned char) *where++;
879 if (where == lim)
880 break;
881 if (((value << LG_256 >> LG_256) | topbits) != value)
883 if (type && !silent)
884 ERROR ((0, 0,
885 _("Archive base-256 value is out of %s range"),
886 type));
887 return -1;
890 negative = signbit != 0;
891 if (negative)
892 value = -value;
895 if (where != lim && *where && !isspace ((unsigned char) *where))
897 if (type)
899 char buf[1000]; /* Big enough to represent any header. */
900 static struct quoting_options *o;
902 if (!o)
904 o = clone_quoting_options (0);
905 set_quoting_style (o, locale_quoting_style);
908 while (where0 != lim && ! lim[-1])
909 lim--;
910 quotearg_buffer (buf, sizeof buf, where0, lim - where0, o);
911 if (!silent)
912 ERROR ((0, 0,
913 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
914 _("Archive contains %.*s where numeric %s value expected"),
915 (int) sizeof buf, buf, type));
918 return -1;
921 if (value <= (negative ? minus_minval : maxval))
922 return represent_uintmax (negative ? -value : value);
924 if (type && !silent)
926 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
927 char maxval_buf[UINTMAX_STRSIZE_BOUND];
928 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
929 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
930 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
931 if (negative)
932 *--value_string = '-';
933 if (minus_minval)
934 *--minval_string = '-';
935 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
936 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
937 value_string, type,
938 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
941 return -1;
944 static gid_t
945 gid_from_header (const char *p, size_t s)
947 return from_header (p, s, "gid_t",
948 TYPE_MINIMUM (gid_t), TYPE_MAXIMUM (gid_t),
949 false, false);
952 static major_t
953 major_from_header (const char *p, size_t s)
955 return from_header (p, s, "major_t",
956 TYPE_MINIMUM (major_t), TYPE_MAXIMUM (major_t),
957 false, false);
960 static minor_t
961 minor_from_header (const char *p, size_t s)
963 return from_header (p, s, "minor_t",
964 TYPE_MINIMUM (minor_t), TYPE_MAXIMUM (minor_t),
965 false, false);
968 /* Convert P to the file mode, as understood by tar.
969 Set *HBITS if there are any unrecognized bits. */
970 static mode_t
971 mode_from_header (const char *p, size_t s, bool *hbits)
973 intmax_t u = from_header (p, s, "mode_t",
974 INTMAX_MIN, UINTMAX_MAX,
975 false, false);
976 mode_t mode = ((u & TSUID ? S_ISUID : 0)
977 | (u & TSGID ? S_ISGID : 0)
978 | (u & TSVTX ? S_ISVTX : 0)
979 | (u & TUREAD ? S_IRUSR : 0)
980 | (u & TUWRITE ? S_IWUSR : 0)
981 | (u & TUEXEC ? S_IXUSR : 0)
982 | (u & TGREAD ? S_IRGRP : 0)
983 | (u & TGWRITE ? S_IWGRP : 0)
984 | (u & TGEXEC ? S_IXGRP : 0)
985 | (u & TOREAD ? S_IROTH : 0)
986 | (u & TOWRITE ? S_IWOTH : 0)
987 | (u & TOEXEC ? S_IXOTH : 0));
988 *hbits = (u & ~07777) != 0;
989 return mode;
992 off_t
993 off_from_header (const char *p, size_t s)
995 /* Negative offsets are not allowed in tar files, so invoke
996 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
997 return from_header (p, s, "off_t",
998 0, TYPE_MAXIMUM (off_t),
999 false, false);
1002 static time_t
1003 time_from_header (const char *p, size_t s)
1005 return from_header (p, s, "time_t",
1006 TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t),
1007 false, false);
1010 static uid_t
1011 uid_from_header (const char *p, size_t s)
1013 return from_header (p, s, "uid_t",
1014 TYPE_MINIMUM (uid_t), TYPE_MAXIMUM (uid_t),
1015 false, false);
1018 uintmax_t
1019 uintmax_from_header (const char *p, size_t s)
1021 return from_header (p, s, "uintmax_t", 0, UINTMAX_MAX, false, false);
1025 /* Return a printable representation of T. The result points to
1026 static storage that can be reused in the next call to this
1027 function, to ctime, or to asctime. If FULL_TIME, then output the
1028 time stamp to its full resolution; otherwise, just output it to
1029 1-minute resolution. */
1030 char const *
1031 tartime (struct timespec t, bool full_time)
1033 enum { fraclen = sizeof ".FFFFFFFFF" - 1 };
1034 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
1035 INT_STRLEN_BOUND (int) + 16)
1036 + fraclen];
1037 struct tm *tm;
1038 time_t s = t.tv_sec;
1039 int ns = t.tv_nsec;
1040 bool negative = s < 0;
1041 char *p;
1043 if (negative && ns != 0)
1045 s++;
1046 ns = 1000000000 - ns;
1049 tm = utc_option ? gmtime (&s) : localtime (&s);
1050 if (tm)
1052 if (full_time)
1054 strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M:%S", tm);
1055 code_ns_fraction (ns, buffer + strlen (buffer));
1057 else
1058 strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M", tm);
1059 return buffer;
1062 /* The time stamp cannot be broken down, most likely because it
1063 is out of range. Convert it as an integer,
1064 right-adjusted in a field with the same width as the usual
1065 4-year ISO time format. */
1066 p = umaxtostr (negative ? - (uintmax_t) s : s,
1067 buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen);
1068 if (negative)
1069 *--p = '-';
1070 while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM"
1071 + (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1072 < p)
1073 *--p = ' ';
1074 if (full_time)
1075 code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen);
1076 return p;
1079 /* Actually print it.
1081 Plain and fancy file header block logging. Non-verbose just prints
1082 the name, e.g. for "tar t" or "tar x". This should just contain
1083 file names, so it can be fed back into tar with xargs or the "-T"
1084 option. The verbose option can give a bunch of info, one line per
1085 file. I doubt anybody tries to parse its format, or if they do,
1086 they shouldn't. Unix tar is pretty random here anyway. */
1089 /* Width of "user/group size", with initial value chosen
1090 heuristically. This grows as needed, though this may cause some
1091 stairstepping in the output. Make it too small and the output will
1092 almost always look ragged. Make it too large and the output will
1093 be spaced out too far. */
1094 static int ugswidth = 19;
1096 /* Width of printed time stamps. It grows if longer time stamps are
1097 found (typically, those with nanosecond resolution). Like
1098 USGWIDTH, some stairstepping may occur. */
1099 static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1;
1101 static bool volume_label_printed = false;
1103 static void
1104 simple_print_header (struct tar_stat_info *st, union block *blk,
1105 off_t block_ordinal)
1107 char modes[12];
1108 char const *time_stamp;
1109 int time_stamp_len;
1110 char *temp_name;
1112 /* These hold formatted ints. */
1113 char uform[max (INT_BUFSIZE_BOUND (intmax_t), UINTMAX_STRSIZE_BOUND)];
1114 char gform[sizeof uform];
1115 char *user, *group;
1116 char size[2 * UINTMAX_STRSIZE_BOUND];
1117 /* holds formatted size or major,minor */
1118 char uintbuf[UINTMAX_STRSIZE_BOUND];
1119 int pad;
1120 int sizelen;
1122 if (show_transformed_names_option)
1123 temp_name = st->file_name ? st->file_name : st->orig_file_name;
1124 else
1125 temp_name = st->orig_file_name ? st->orig_file_name : st->file_name;
1127 if (block_number_option)
1129 char buf[UINTMAX_STRSIZE_BOUND];
1130 if (block_ordinal < 0)
1131 block_ordinal = current_block_ordinal ();
1132 block_ordinal -= recent_long_name_blocks;
1133 block_ordinal -= recent_long_link_blocks;
1134 fprintf (stdlis, _("block %s: "),
1135 STRINGIFY_BIGINT (block_ordinal, buf));
1138 if (verbose_option <= 1)
1140 /* Just the fax, mam. */
1141 fputs (quotearg (temp_name), stdlis);
1142 if (show_transformed_names_option && st->had_trailing_slash)
1143 fputc ('/', stdlis);
1144 fputc ('\n', stdlis);
1146 else
1148 /* File type and modes. */
1150 modes[0] = '?';
1151 switch (blk->header.typeflag)
1153 case GNUTYPE_VOLHDR:
1154 volume_label_printed = true;
1155 modes[0] = 'V';
1156 break;
1158 case GNUTYPE_MULTIVOL:
1159 modes[0] = 'M';
1160 break;
1162 case GNUTYPE_LONGNAME:
1163 case GNUTYPE_LONGLINK:
1164 modes[0] = 'L';
1165 ERROR ((0, 0, _("Unexpected long name header")));
1166 break;
1168 case GNUTYPE_SPARSE:
1169 case REGTYPE:
1170 case AREGTYPE:
1171 modes[0] = st->had_trailing_slash ? 'd' : '-';
1172 break;
1173 case LNKTYPE:
1174 modes[0] = 'h';
1175 break;
1176 case GNUTYPE_DUMPDIR:
1177 modes[0] = 'd';
1178 break;
1179 case DIRTYPE:
1180 modes[0] = 'd';
1181 break;
1182 case SYMTYPE:
1183 modes[0] = 'l';
1184 break;
1185 case BLKTYPE:
1186 modes[0] = 'b';
1187 break;
1188 case CHRTYPE:
1189 modes[0] = 'c';
1190 break;
1191 case FIFOTYPE:
1192 modes[0] = 'p';
1193 break;
1194 case CONTTYPE:
1195 modes[0] = 'C';
1196 break;
1199 pax_decode_mode (st->stat.st_mode, modes + 1);
1201 /* extended attributes: GNU `ls -l'-like preview */
1202 xattrs_print_char (st, modes + 10);
1204 /* Time stamp. */
1206 time_stamp = tartime (st->mtime, full_time_option);
1207 time_stamp_len = strlen (time_stamp);
1208 if (datewidth < time_stamp_len)
1209 datewidth = time_stamp_len;
1211 /* User and group names. */
1213 if (st->uname
1214 && st->uname[0]
1215 && current_format != V7_FORMAT
1216 && !numeric_owner_option)
1217 user = st->uname;
1218 else
1219 user = STRINGIFY_BIGINT (st->stat.st_uid, uform);
1221 if (st->gname
1222 && st->gname[0]
1223 && current_format != V7_FORMAT
1224 && !numeric_owner_option)
1225 group = st->gname;
1226 else
1227 group = STRINGIFY_BIGINT (st->stat.st_gid, gform);
1229 /* Format the file size or major/minor device numbers. */
1231 switch (blk->header.typeflag)
1233 case CHRTYPE:
1234 case BLKTYPE:
1235 strcpy (size,
1236 STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf));
1237 strcat (size, ",");
1238 strcat (size,
1239 STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf));
1240 break;
1242 default:
1243 /* st->stat.st_size keeps stored file size */
1244 strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf));
1245 break;
1248 /* Figure out padding and print the whole line. */
1250 sizelen = strlen (size);
1251 pad = strlen (user) + 1 + strlen (group) + 1 + sizelen;
1252 if (pad > ugswidth)
1253 ugswidth = pad;
1255 fprintf (stdlis, "%s %s/%s %*s %-*s",
1256 modes, user, group, ugswidth - pad + sizelen, size,
1257 datewidth, time_stamp);
1259 fprintf (stdlis, " %s", quotearg (temp_name));
1260 if (show_transformed_names_option && st->had_trailing_slash)
1261 fputc ('/', stdlis);
1263 switch (blk->header.typeflag)
1265 case SYMTYPE:
1266 fprintf (stdlis, " -> %s\n", quotearg (st->link_name));
1267 break;
1269 case LNKTYPE:
1270 fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name));
1271 break;
1273 default:
1275 char type_string[2];
1276 type_string[0] = blk->header.typeflag;
1277 type_string[1] = '\0';
1278 fprintf (stdlis, _(" unknown file type %s\n"),
1279 quote (type_string));
1281 break;
1283 case AREGTYPE:
1284 case REGTYPE:
1285 case GNUTYPE_SPARSE:
1286 case CHRTYPE:
1287 case BLKTYPE:
1288 case DIRTYPE:
1289 case FIFOTYPE:
1290 case CONTTYPE:
1291 case GNUTYPE_DUMPDIR:
1292 putc ('\n', stdlis);
1293 break;
1295 case GNUTYPE_LONGLINK:
1296 fprintf (stdlis, _("--Long Link--\n"));
1297 break;
1299 case GNUTYPE_LONGNAME:
1300 fprintf (stdlis, _("--Long Name--\n"));
1301 break;
1303 case GNUTYPE_VOLHDR:
1304 fprintf (stdlis, _("--Volume Header--\n"));
1305 break;
1307 case GNUTYPE_MULTIVOL:
1308 strcpy (size,
1309 STRINGIFY_BIGINT
1310 (UINTMAX_FROM_HEADER (blk->oldgnu_header.offset),
1311 uintbuf));
1312 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1313 break;
1316 fflush (stdlis);
1317 xattrs_print (st);
1321 static void
1322 print_volume_label (void)
1324 struct tar_stat_info vstat;
1325 union block vblk;
1326 enum archive_format dummy;
1328 memset (&vblk, 0, sizeof (vblk));
1329 vblk.header.typeflag = GNUTYPE_VOLHDR;
1330 if (recent_global_header)
1331 memcpy (vblk.header.mtime, recent_global_header->header.mtime,
1332 sizeof vblk.header.mtime);
1333 tar_stat_init (&vstat);
1334 assign_string (&vstat.file_name, ".");
1335 decode_header (&vblk, &vstat, &dummy, 0);
1336 assign_string (&vstat.file_name, volume_label);
1337 simple_print_header (&vstat, &vblk, 0);
1338 tar_stat_destroy (&vstat);
1341 void
1342 print_header (struct tar_stat_info *st, union block *blk,
1343 off_t block_ordinal)
1345 if (current_format == POSIX_FORMAT && !volume_label_printed && volume_label)
1347 print_volume_label ();
1348 volume_label_printed = true;
1351 simple_print_header (st, blk, block_ordinal);
1354 /* Print a similar line when we make a directory automatically. */
1355 void
1356 print_for_mkdir (char *dirname, int length, mode_t mode)
1358 char modes[11];
1360 if (verbose_option > 1)
1362 /* File type and modes. */
1364 modes[0] = 'd';
1365 pax_decode_mode (mode, modes + 1);
1367 if (block_number_option)
1369 char buf[UINTMAX_STRSIZE_BOUND];
1370 fprintf (stdlis, _("block %s: "),
1371 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1374 fprintf (stdlis, "%s %*s %s\n", modes, ugswidth + 1 + datewidth,
1375 _("Creating directory:"), quotearg (dirname));
1379 /* Skip over SIZE bytes of data in blocks in the archive. */
1380 void
1381 skip_file (off_t size)
1383 union block *x;
1385 /* FIXME: Make sure mv_begin_read is always called before it */
1387 if (seekable_archive)
1389 off_t nblk = seek_archive (size);
1390 if (nblk >= 0)
1391 size -= nblk * BLOCKSIZE;
1392 else
1393 seekable_archive = false;
1396 mv_size_left (size);
1398 while (size > 0)
1400 x = find_next_block ();
1401 if (! x)
1402 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1404 set_next_block_after (x);
1405 size -= BLOCKSIZE;
1406 mv_size_left (size);
1410 /* Skip the current member in the archive.
1411 NOTE: Current header must be decoded before calling this function. */
1412 void
1413 skip_member (void)
1415 if (!current_stat_info.skipped)
1417 char save_typeflag = current_header->header.typeflag;
1418 set_next_block_after (current_header);
1420 mv_begin_read (&current_stat_info);
1422 if (current_stat_info.is_sparse)
1423 sparse_skip_file (&current_stat_info);
1424 else if (save_typeflag != DIRTYPE)
1425 skip_file (current_stat_info.stat.st_size);
1427 mv_end ();
1431 void
1432 test_archive_label (void)
1434 base64_init ();
1435 name_gather ();
1437 open_archive (ACCESS_READ);
1438 if (read_header (&current_header, &current_stat_info, read_header_auto)
1439 == HEADER_SUCCESS)
1441 decode_header (current_header,
1442 &current_stat_info, &current_format, 0);
1443 if (current_header->header.typeflag == GNUTYPE_VOLHDR)
1444 ASSIGN_STRING_N (&volume_label, current_header->header.name);
1446 if (volume_label)
1448 if (verbose_option)
1449 print_volume_label ();
1450 if (!name_match (volume_label) && multi_volume_option)
1452 char *s = drop_volume_label_suffix (volume_label);
1453 name_match (s);
1454 free (s);
1458 close_archive ();
1459 label_notfound ();