Version 1.31
[tar.git] / src / list.c
bloba985bcc144a0808b2dbc049a2cabec252cd02e58
1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright 1988-2019 Free Software Foundation, Inc.
5 This file is part of GNU tar.
7 GNU tar is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU tar is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Written by John Gilmore, on 1985-08-26. */
22 #include <system.h>
23 #include <inttostr.h>
24 #include <quotearg.h>
25 #include <time.h>
26 #include "common.h"
28 union block *current_header; /* points to current archive header */
29 enum archive_format current_format; /* recognized format */
30 union block *recent_long_name; /* recent long name header and contents */
31 union block *recent_long_link; /* likewise, for long link */
32 size_t recent_long_name_blocks; /* number of blocks in recent_long_name */
33 size_t recent_long_link_blocks; /* likewise, for long link */
34 static union block *recent_global_header; /* Recent global header block */
36 #define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where))
37 #define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where))
38 #define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where))
39 #define MODE_FROM_HEADER(where, hbits) \
40 mode_from_header (where, sizeof (where), hbits)
41 #define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where))
42 #define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where))
44 static gid_t gid_from_header (const char *buf, size_t size);
45 static major_t major_from_header (const char *buf, size_t size);
46 static minor_t minor_from_header (const char *buf, size_t size);
47 static mode_t mode_from_header (const char *buf, size_t size, bool *hbits);
48 static time_t time_from_header (const char *buf, size_t size);
49 static uid_t uid_from_header (const char *buf, size_t size);
50 static intmax_t from_header (const char *, size_t, const char *,
51 intmax_t, uintmax_t, bool, bool);
53 /* Base 64 digits; see Internet RFC 2045 Table 1. */
54 static char const base_64_digits[64] =
56 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
57 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
58 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
59 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
60 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
63 /* Table of base-64 digit values indexed by unsigned chars.
64 The value is 64 for unsigned chars that are not base-64 digits. */
65 static char base64_map[UCHAR_MAX + 1];
67 static void
68 base64_init (void)
70 int i;
71 memset (base64_map, 64, sizeof base64_map);
72 for (i = 0; i < 64; i++)
73 base64_map[(int) base_64_digits[i]] = i;
76 static char *
77 decode_xform (char *file_name, void *data)
79 int type = *(int*)data;
81 switch (type)
83 case XFORM_SYMLINK:
84 /* FIXME: It is not quite clear how and to which extent are the symbolic
85 links subject to filename transformation. In the absence of another
86 solution, symbolic links are exempt from component stripping and
87 name suffix normalization, but subject to filename transformation
88 proper. */
89 return file_name;
91 case XFORM_LINK:
92 file_name = safer_name_suffix (file_name, true, absolute_names_option);
93 break;
95 case XFORM_REGFILE:
96 file_name = safer_name_suffix (file_name, false, absolute_names_option);
97 break;
100 if (strip_name_components)
102 size_t prefix_len = stripped_prefix_len (file_name,
103 strip_name_components);
104 if (prefix_len == (size_t) -1)
105 prefix_len = strlen (file_name);
106 file_name += prefix_len;
108 return file_name;
111 static bool
112 transform_member_name (char **pinput, int type)
114 return transform_name_fp (pinput, type, decode_xform, &type);
117 static void
118 enforce_one_top_level (char **pfile_name)
120 char *file_name = *pfile_name;
121 char *p;
123 for (p = file_name; *p && (ISSLASH (*p) || *p == '.'); p++)
126 if (*p)
128 int pos = strlen (one_top_level_dir);
129 if (strncmp (p, one_top_level_dir, pos) == 0)
131 if (ISSLASH (p[pos]) || p[pos] == 0)
132 return;
135 *pfile_name = make_file_name (one_top_level_dir, file_name);
136 normalize_filename_x (*pfile_name);
138 else
139 *pfile_name = xstrdup (one_top_level_dir);
140 free (file_name);
143 void
144 transform_stat_info (int typeflag, struct tar_stat_info *stat_info)
146 if (typeflag == GNUTYPE_VOLHDR)
147 /* Name transformations don't apply to volume headers. */
148 return;
150 transform_member_name (&stat_info->file_name, XFORM_REGFILE);
151 switch (typeflag)
153 case SYMTYPE:
154 transform_member_name (&stat_info->link_name, XFORM_SYMLINK);
155 break;
157 case LNKTYPE:
158 transform_member_name (&stat_info->link_name, XFORM_LINK);
161 if (one_top_level_option)
162 enforce_one_top_level (&current_stat_info.file_name);
165 /* Main loop for reading an archive. */
166 void
167 read_and (void (*do_something) (void))
169 enum read_header status = HEADER_STILL_UNREAD;
170 enum read_header prev_status;
171 struct timespec mtime;
173 base64_init ();
174 name_gather ();
176 open_archive (ACCESS_READ);
179 prev_status = status;
180 tar_stat_destroy (&current_stat_info);
182 status = read_header (&current_header, &current_stat_info,
183 read_header_auto);
184 switch (status)
186 case HEADER_STILL_UNREAD:
187 case HEADER_SUCCESS_EXTENDED:
188 abort ();
190 case HEADER_SUCCESS:
192 /* Valid header. We should decode next field (mode) first.
193 Ensure incoming names are null terminated. */
194 decode_header (current_header, &current_stat_info,
195 &current_format, 1);
196 if (! name_match (current_stat_info.file_name)
197 || (TIME_OPTION_INITIALIZED (newer_mtime_option)
198 /* FIXME: We get mtime now, and again later; this causes
199 duplicate diagnostics if header.mtime is bogus. */
200 && ((mtime.tv_sec
201 = TIME_FROM_HEADER (current_header->header.mtime)),
202 /* FIXME: Grab fractional time stamps from
203 extended header. */
204 mtime.tv_nsec = 0,
205 current_stat_info.mtime = mtime,
206 OLDER_TAR_STAT_TIME (current_stat_info, m)))
207 || excluded_name (current_stat_info.file_name,
208 current_stat_info.parent))
210 switch (current_header->header.typeflag)
212 case GNUTYPE_VOLHDR:
213 case GNUTYPE_MULTIVOL:
214 break;
216 case DIRTYPE:
217 if (show_omitted_dirs_option)
218 WARN ((0, 0, _("%s: Omitting"),
219 quotearg_colon (current_stat_info.file_name)));
220 FALLTHROUGH;
221 default:
222 skip_member ();
223 continue;
227 transform_stat_info (current_header->header.typeflag,
228 &current_stat_info);
229 (*do_something) ();
230 continue;
232 case HEADER_ZERO_BLOCK:
233 if (block_number_option)
235 char buf[UINTMAX_STRSIZE_BOUND];
236 fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
237 STRINGIFY_BIGINT (current_block_ordinal (), buf));
240 set_next_block_after (current_header);
242 if (!ignore_zeros_option)
244 char buf[UINTMAX_STRSIZE_BOUND];
246 status = read_header (&current_header, &current_stat_info,
247 read_header_auto);
248 if (status == HEADER_ZERO_BLOCK)
249 break;
250 WARNOPT (WARN_ALONE_ZERO_BLOCK,
251 (0, 0, _("A lone zero block at %s"),
252 STRINGIFY_BIGINT (current_block_ordinal (), buf)));
253 break;
255 status = prev_status;
256 continue;
258 case HEADER_END_OF_FILE:
259 if (block_number_option)
261 char buf[UINTMAX_STRSIZE_BOUND];
262 fprintf (stdlis, _("block %s: ** End of File **\n"),
263 STRINGIFY_BIGINT (current_block_ordinal (), buf));
265 break;
267 case HEADER_FAILURE:
268 /* If the previous header was good, tell them that we are
269 skipping bad ones. */
270 set_next_block_after (current_header);
271 switch (prev_status)
273 case HEADER_STILL_UNREAD:
274 ERROR ((0, 0, _("This does not look like a tar archive")));
275 FALLTHROUGH;
276 case HEADER_ZERO_BLOCK:
277 case HEADER_SUCCESS:
278 if (block_number_option)
280 char buf[UINTMAX_STRSIZE_BOUND];
281 off_t block_ordinal = current_block_ordinal ();
282 block_ordinal -= recent_long_name_blocks;
283 block_ordinal -= recent_long_link_blocks;
284 fprintf (stdlis, _("block %s: "),
285 STRINGIFY_BIGINT (block_ordinal, buf));
287 ERROR ((0, 0, _("Skipping to next header")));
288 break;
290 case HEADER_END_OF_FILE:
291 case HEADER_FAILURE:
292 /* We are in the middle of a cascade of errors. */
293 break;
295 case HEADER_SUCCESS_EXTENDED:
296 abort ();
298 continue;
300 break;
302 while (!all_names_found (&current_stat_info));
304 close_archive ();
305 names_notfound (); /* print names not found */
308 /* Print a header block, based on tar options. */
309 void
310 list_archive (void)
312 off_t block_ordinal = current_block_ordinal ();
314 /* Print the header block. */
315 if (verbose_option)
316 print_header (&current_stat_info, current_header, block_ordinal);
318 if (incremental_option)
320 if (verbose_option > 2)
322 if (is_dumpdir (&current_stat_info))
323 list_dumpdir (current_stat_info.dumpdir,
324 dumpdir_size (current_stat_info.dumpdir));
328 skip_member ();
331 /* Check header checksum */
332 /* The standard BSD tar sources create the checksum by adding up the
333 bytes in the header as type char. I think the type char was unsigned
334 on the PDP-11, but it's signed on the Next and Sun. It looks like the
335 sources to BSD tar were never changed to compute the checksum
336 correctly, so both the Sun and Next add the bytes of the header as
337 signed chars. This doesn't cause a problem until you get a file with
338 a name containing characters with the high bit set. So tar_checksum
339 computes two checksums -- signed and unsigned. */
341 enum read_header
342 tar_checksum (union block *header, bool silent)
344 size_t i;
345 int unsigned_sum = 0; /* the POSIX one :-) */
346 int signed_sum = 0; /* the Sun one :-( */
347 int recorded_sum;
348 int parsed_sum;
349 char *p;
351 p = header->buffer;
352 for (i = sizeof *header; i-- != 0;)
354 unsigned_sum += (unsigned char) *p;
355 signed_sum += (signed char) (*p++);
358 if (unsigned_sum == 0)
359 return HEADER_ZERO_BLOCK;
361 /* Adjust checksum to count the "chksum" field as blanks. */
363 for (i = sizeof header->header.chksum; i-- != 0;)
365 unsigned_sum -= (unsigned char) header->header.chksum[i];
366 signed_sum -= (signed char) (header->header.chksum[i]);
368 unsigned_sum += ' ' * sizeof header->header.chksum;
369 signed_sum += ' ' * sizeof header->header.chksum;
371 parsed_sum = from_header (header->header.chksum,
372 sizeof header->header.chksum, 0,
373 0, INT_MAX, true, silent);
374 if (parsed_sum < 0)
375 return HEADER_FAILURE;
377 recorded_sum = parsed_sum;
379 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
380 return HEADER_FAILURE;
382 return HEADER_SUCCESS;
385 /* Read a block that's supposed to be a header block. Return its
386 address in *RETURN_BLOCK, and if it is good, the file's size
387 and names (file name, link name) in *INFO.
389 Return one of enum read_header describing the status of the
390 operation.
392 The MODE parameter instructs read_header what to do with special
393 header blocks, i.e.: extended POSIX, GNU long name or long link,
394 etc.:
396 read_header_auto process them automatically,
397 read_header_x_raw when a special header is read, return
398 HEADER_SUCCESS_EXTENDED without actually
399 processing the header,
400 read_header_x_global when a POSIX global header is read,
401 decode it and return HEADER_SUCCESS_EXTENDED.
403 You must always set_next_block_after(*return_block) to skip past
404 the header which this routine reads. */
406 enum read_header
407 read_header (union block **return_block, struct tar_stat_info *info,
408 enum read_header_mode mode)
410 union block *header;
411 union block *header_copy;
412 char *bp;
413 union block *data_block;
414 size_t size, written;
415 union block *next_long_name = 0;
416 union block *next_long_link = 0;
417 size_t next_long_name_blocks = 0;
418 size_t next_long_link_blocks = 0;
420 while (1)
422 enum read_header status;
424 header = find_next_block ();
425 *return_block = header;
426 if (!header)
427 return HEADER_END_OF_FILE;
429 if ((status = tar_checksum (header, false)) != HEADER_SUCCESS)
430 return status;
432 /* Good block. Decode file size and return. */
434 if (header->header.typeflag == LNKTYPE)
435 info->stat.st_size = 0; /* links 0 size on tape */
436 else
438 info->stat.st_size = OFF_FROM_HEADER (header->header.size);
439 if (info->stat.st_size < 0)
440 return HEADER_FAILURE;
443 if (header->header.typeflag == GNUTYPE_LONGNAME
444 || header->header.typeflag == GNUTYPE_LONGLINK
445 || header->header.typeflag == XHDTYPE
446 || header->header.typeflag == XGLTYPE
447 || header->header.typeflag == SOLARIS_XHDTYPE)
449 if (mode == read_header_x_raw)
450 return HEADER_SUCCESS_EXTENDED;
451 else if (header->header.typeflag == GNUTYPE_LONGNAME
452 || header->header.typeflag == GNUTYPE_LONGLINK)
454 size_t name_size = info->stat.st_size;
455 size_t n = name_size % BLOCKSIZE;
456 size = name_size + BLOCKSIZE;
457 if (n)
458 size += BLOCKSIZE - n;
460 if (name_size != info->stat.st_size || size < name_size)
461 xalloc_die ();
463 header_copy = xmalloc (size + 1);
465 if (header->header.typeflag == GNUTYPE_LONGNAME)
467 free (next_long_name);
468 next_long_name = header_copy;
469 next_long_name_blocks = size / BLOCKSIZE;
471 else
473 free (next_long_link);
474 next_long_link = header_copy;
475 next_long_link_blocks = size / BLOCKSIZE;
478 set_next_block_after (header);
479 *header_copy = *header;
480 bp = header_copy->buffer + BLOCKSIZE;
482 for (size -= BLOCKSIZE; size > 0; size -= written)
484 data_block = find_next_block ();
485 if (! data_block)
487 ERROR ((0, 0, _("Unexpected EOF in archive")));
488 break;
490 written = available_space_after (data_block);
491 if (written > size)
492 written = size;
494 memcpy (bp, data_block->buffer, written);
495 bp += written;
496 set_next_block_after ((union block *)
497 (data_block->buffer + written - 1));
500 *bp = '\0';
502 else if (header->header.typeflag == XHDTYPE
503 || header->header.typeflag == SOLARIS_XHDTYPE)
504 xheader_read (&info->xhdr, header,
505 OFF_FROM_HEADER (header->header.size));
506 else if (header->header.typeflag == XGLTYPE)
508 struct xheader xhdr;
510 if (!recent_global_header)
511 recent_global_header = xmalloc (sizeof *recent_global_header);
512 memcpy (recent_global_header, header,
513 sizeof *recent_global_header);
514 memset (&xhdr, 0, sizeof xhdr);
515 xheader_read (&xhdr, header,
516 OFF_FROM_HEADER (header->header.size));
517 xheader_decode_global (&xhdr);
518 xheader_destroy (&xhdr);
519 if (mode == read_header_x_global)
520 return HEADER_SUCCESS_EXTENDED;
523 /* Loop! */
526 else
528 char const *name;
529 struct posix_header const *h = &header->header;
530 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
532 free (recent_long_name);
534 if (next_long_name)
536 name = next_long_name->buffer + BLOCKSIZE;
537 recent_long_name = next_long_name;
538 recent_long_name_blocks = next_long_name_blocks;
540 else
542 /* Accept file names as specified by POSIX.1-1996
543 section 10.1.1. */
544 char *np = namebuf;
546 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
548 memcpy (np, h->prefix, sizeof h->prefix);
549 np[sizeof h->prefix] = '\0';
550 np += strlen (np);
551 *np++ = '/';
553 memcpy (np, h->name, sizeof h->name);
554 np[sizeof h->name] = '\0';
555 name = namebuf;
556 recent_long_name = 0;
557 recent_long_name_blocks = 0;
559 assign_string (&info->orig_file_name, name);
560 assign_string (&info->file_name, name);
561 info->had_trailing_slash = strip_trailing_slashes (info->file_name);
563 free (recent_long_link);
565 if (next_long_link)
567 name = next_long_link->buffer + BLOCKSIZE;
568 recent_long_link = next_long_link;
569 recent_long_link_blocks = next_long_link_blocks;
571 else
573 memcpy (namebuf, h->linkname, sizeof h->linkname);
574 namebuf[sizeof h->linkname] = '\0';
575 name = namebuf;
576 recent_long_link = 0;
577 recent_long_link_blocks = 0;
579 assign_string (&info->link_name, name);
581 return HEADER_SUCCESS;
586 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
588 /* Decode things from a file HEADER block into STAT_INFO, also setting
589 *FORMAT_POINTER depending on the header block format. If
590 DO_USER_GROUP, decode the user/group information (this is useful
591 for extraction, but waste time when merely listing).
593 read_header() has already decoded the checksum and length, so we don't.
595 This routine should *not* be called twice for the same block, since
596 the two calls might use different DO_USER_GROUP values and thus
597 might end up with different uid/gid for the two calls. If anybody
598 wants the uid/gid they should decode it first, and other callers
599 should decode it without uid/gid before calling a routine,
600 e.g. print_header, that assumes decoded data. */
601 void
602 decode_header (union block *header, struct tar_stat_info *stat_info,
603 enum archive_format *format_pointer, int do_user_group)
605 enum archive_format format;
606 bool hbits;
607 mode_t mode = MODE_FROM_HEADER (header->header.mode, &hbits);
609 if (strcmp (header->header.magic, TMAGIC) == 0)
611 if (header->star_header.prefix[130] == 0
612 && ISOCTAL (header->star_header.atime[0])
613 && header->star_header.atime[11] == ' '
614 && ISOCTAL (header->star_header.ctime[0])
615 && header->star_header.ctime[11] == ' ')
616 format = STAR_FORMAT;
617 else if (stat_info->xhdr.size)
618 format = POSIX_FORMAT;
619 else
620 format = USTAR_FORMAT;
622 else if (strcmp (header->buffer + offsetof (struct posix_header, magic),
623 OLDGNU_MAGIC)
624 == 0)
625 format = hbits ? OLDGNU_FORMAT : GNU_FORMAT;
626 else
627 format = V7_FORMAT;
628 *format_pointer = format;
630 stat_info->stat.st_mode = mode;
631 stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime);
632 stat_info->mtime.tv_nsec = 0;
633 assign_string_n (&stat_info->uname,
634 header->header.uname[0] ? header->header.uname : NULL,
635 sizeof (header->header.uname));
636 assign_string_n (&stat_info->gname,
637 header->header.gname[0] ? header->header.gname : NULL,
638 sizeof (header->header.gname));
640 xheader_xattr_init (stat_info);
642 if (format == OLDGNU_FORMAT && incremental_option)
644 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime);
645 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime);
646 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
648 else if (format == STAR_FORMAT)
650 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime);
651 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime);
652 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
654 else
655 stat_info->atime = stat_info->ctime = start_time;
657 if (format == V7_FORMAT)
659 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
660 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
661 stat_info->stat.st_rdev = 0;
663 else
665 if (do_user_group)
667 /* FIXME: Decide if this should somewhat depend on -p. */
669 if (numeric_owner_option
670 || !*header->header.uname
671 || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid))
672 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
674 if (numeric_owner_option
675 || !*header->header.gname
676 || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid))
677 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
680 switch (header->header.typeflag)
682 case BLKTYPE:
683 case CHRTYPE:
684 stat_info->stat.st_rdev =
685 makedev (MAJOR_FROM_HEADER (header->header.devmajor),
686 MINOR_FROM_HEADER (header->header.devminor));
687 break;
689 default:
690 stat_info->stat.st_rdev = 0;
694 xheader_decode (stat_info);
696 if (sparse_member_p (stat_info))
698 sparse_fixup_header (stat_info);
699 stat_info->is_sparse = true;
701 else
703 stat_info->is_sparse = false;
704 if (((current_format == GNU_FORMAT
705 || current_format == OLDGNU_FORMAT)
706 && current_header->header.typeflag == GNUTYPE_DUMPDIR)
707 || stat_info->dumpdir)
708 stat_info->is_dumpdir = true;
713 /* Convert buffer at WHERE0 of size DIGS from external format to
714 intmax_t. DIGS must be positive. If TYPE is nonnull, the data are
715 of type TYPE. The buffer must represent a value in the range
716 MINVAL through MAXVAL; if the mathematically correct result V would
717 be greater than INTMAX_MAX, return a negative integer V such that
718 (uintmax_t) V yields the correct result. If OCTAL_ONLY, allow only octal
719 numbers instead of the other GNU extensions. Return -1 on error,
720 diagnosing the error if TYPE is nonnull and if !SILENT. */
721 #if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX)
722 # error "from_header internally represents intmax_t as uintmax_t + sign"
723 #endif
724 #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
725 # error "from_header returns intmax_t to represent uintmax_t"
726 #endif
727 static intmax_t
728 from_header (char const *where0, size_t digs, char const *type,
729 intmax_t minval, uintmax_t maxval,
730 bool octal_only, bool silent)
732 uintmax_t value;
733 uintmax_t uminval = minval;
734 uintmax_t minus_minval = - uminval;
735 char const *where = where0;
736 char const *lim = where + digs;
737 bool negative = false;
739 /* Accommodate buggy tar of unknown vintage, which outputs leading
740 NUL if the previous field overflows. */
741 where += !*where;
743 /* Accommodate older tars, which output leading spaces. */
744 for (;;)
746 if (where == lim)
748 if (type && !silent)
749 ERROR ((0, 0,
750 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
751 etc.) */
752 _("Blanks in header where numeric %s value expected"),
753 type));
754 return -1;
756 if (!isspace ((unsigned char) *where))
757 break;
758 where++;
761 value = 0;
762 if (ISODIGIT (*where))
764 char const *where1 = where;
765 bool overflow = false;
767 for (;;)
769 value += *where++ - '0';
770 if (where == lim || ! ISODIGIT (*where))
771 break;
772 overflow |= value != (value << LG_8 >> LG_8);
773 value <<= LG_8;
776 /* Parse the output of older, unportable tars, which generate
777 negative values in two's complement octal. If the leading
778 nonzero digit is 1, we can't recover the original value
779 reliably; so do this only if the digit is 2 or more. This
780 catches the common case of 32-bit negative time stamps. */
781 if ((overflow || maxval < value) && '2' <= *where1 && type)
783 /* Compute the negative of the input value, assuming two's
784 complement. */
785 int digit = (*where1 - '0') | 4;
786 overflow = 0;
787 value = 0;
788 where = where1;
789 for (;;)
791 value += 7 - digit;
792 where++;
793 if (where == lim || ! ISODIGIT (*where))
794 break;
795 digit = *where - '0';
796 overflow |= value != (value << LG_8 >> LG_8);
797 value <<= LG_8;
799 value++;
800 overflow |= !value;
802 if (!overflow && value <= minus_minval)
804 if (!silent)
805 WARN ((0, 0,
806 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
807 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
808 (int) (where - where1), where1, type));
809 negative = true;
813 if (overflow)
815 if (type && !silent)
816 ERROR ((0, 0,
817 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
818 _("Archive octal value %.*s is out of %s range"),
819 (int) (where - where1), where1, type));
820 return -1;
823 else if (octal_only)
825 /* Suppress the following extensions. */
827 else if (*where == '-' || *where == '+')
829 /* Parse base-64 output produced only by tar test versions
830 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
831 Support for this will be withdrawn in future releases. */
832 int dig;
833 if (!silent)
835 static bool warned_once;
836 if (! warned_once)
838 warned_once = true;
839 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
842 negative = *where++ == '-';
843 while (where != lim
844 && (dig = base64_map[(unsigned char) *where]) < 64)
846 if (value << LG_64 >> LG_64 != value)
848 char *string = alloca (digs + 1);
849 memcpy (string, where0, digs);
850 string[digs] = '\0';
851 if (type && !silent)
852 ERROR ((0, 0,
853 _("Archive signed base-64 string %s is out of %s range"),
854 quote (string), type));
855 return -1;
857 value = (value << LG_64) | dig;
858 where++;
861 else if (*where == '\200' /* positive base-256 */
862 || *where == '\377' /* negative base-256 */)
864 /* Parse base-256 output. A nonnegative number N is
865 represented as (256**DIGS)/2 + N; a negative number -N is
866 represented as (256**DIGS) - N, i.e. as two's complement.
867 The representation guarantees that the leading bit is
868 always on, so that we don't confuse this format with the
869 others (assuming ASCII bytes of 8 bits or more). */
870 int signbit = *where & (1 << (LG_256 - 2));
871 uintmax_t topbits = (((uintmax_t) - signbit)
872 << (CHAR_BIT * sizeof (uintmax_t)
873 - LG_256 - (LG_256 - 2)));
874 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
875 for (;;)
877 value = (value << LG_256) + (unsigned char) *where++;
878 if (where == lim)
879 break;
880 if (((value << LG_256 >> LG_256) | topbits) != value)
882 if (type && !silent)
883 ERROR ((0, 0,
884 _("Archive base-256 value is out of %s range"),
885 type));
886 return -1;
889 negative = signbit != 0;
890 if (negative)
891 value = -value;
894 if (where != lim && *where && !isspace ((unsigned char) *where))
896 if (type)
898 char buf[1000]; /* Big enough to represent any header. */
899 static struct quoting_options *o;
901 if (!o)
903 o = clone_quoting_options (0);
904 set_quoting_style (o, locale_quoting_style);
907 while (where0 != lim && ! lim[-1])
908 lim--;
909 quotearg_buffer (buf, sizeof buf, where0, lim - where0, o);
910 if (!silent)
911 ERROR ((0, 0,
912 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
913 _("Archive contains %.*s where numeric %s value expected"),
914 (int) sizeof buf, buf, type));
917 return -1;
920 if (value <= (negative ? minus_minval : maxval))
921 return represent_uintmax (negative ? -value : value);
923 if (type && !silent)
925 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
926 char maxval_buf[UINTMAX_STRSIZE_BOUND];
927 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
928 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
929 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
930 if (negative)
931 *--value_string = '-';
932 if (minus_minval)
933 *--minval_string = '-';
934 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
935 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
936 value_string, type,
937 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
940 return -1;
943 static gid_t
944 gid_from_header (const char *p, size_t s)
946 return from_header (p, s, "gid_t",
947 TYPE_MINIMUM (gid_t), TYPE_MAXIMUM (gid_t),
948 false, false);
951 static major_t
952 major_from_header (const char *p, size_t s)
954 return from_header (p, s, "major_t",
955 TYPE_MINIMUM (major_t), TYPE_MAXIMUM (major_t),
956 false, false);
959 static minor_t
960 minor_from_header (const char *p, size_t s)
962 return from_header (p, s, "minor_t",
963 TYPE_MINIMUM (minor_t), TYPE_MAXIMUM (minor_t),
964 false, false);
967 /* Convert P to the file mode, as understood by tar.
968 Set *HBITS if there are any unrecognized bits. */
969 static mode_t
970 mode_from_header (const char *p, size_t s, bool *hbits)
972 intmax_t u = from_header (p, s, "mode_t",
973 INTMAX_MIN, UINTMAX_MAX,
974 false, false);
975 mode_t mode = ((u & TSUID ? S_ISUID : 0)
976 | (u & TSGID ? S_ISGID : 0)
977 | (u & TSVTX ? S_ISVTX : 0)
978 | (u & TUREAD ? S_IRUSR : 0)
979 | (u & TUWRITE ? S_IWUSR : 0)
980 | (u & TUEXEC ? S_IXUSR : 0)
981 | (u & TGREAD ? S_IRGRP : 0)
982 | (u & TGWRITE ? S_IWGRP : 0)
983 | (u & TGEXEC ? S_IXGRP : 0)
984 | (u & TOREAD ? S_IROTH : 0)
985 | (u & TOWRITE ? S_IWOTH : 0)
986 | (u & TOEXEC ? S_IXOTH : 0));
987 *hbits = (u & ~07777) != 0;
988 return mode;
991 off_t
992 off_from_header (const char *p, size_t s)
994 /* Negative offsets are not allowed in tar files, so invoke
995 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
996 return from_header (p, s, "off_t",
997 0, TYPE_MAXIMUM (off_t),
998 false, false);
1001 static time_t
1002 time_from_header (const char *p, size_t s)
1004 return from_header (p, s, "time_t",
1005 TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t),
1006 false, false);
1009 static uid_t
1010 uid_from_header (const char *p, size_t s)
1012 return from_header (p, s, "uid_t",
1013 TYPE_MINIMUM (uid_t), TYPE_MAXIMUM (uid_t),
1014 false, false);
1017 uintmax_t
1018 uintmax_from_header (const char *p, size_t s)
1020 return from_header (p, s, "uintmax_t", 0, UINTMAX_MAX, false, false);
1024 /* Return a printable representation of T. The result points to
1025 static storage that can be reused in the next call to this
1026 function, to ctime, or to asctime. If FULL_TIME, then output the
1027 time stamp to its full resolution; otherwise, just output it to
1028 1-minute resolution. */
1029 char const *
1030 tartime (struct timespec t, bool full_time)
1032 enum { fraclen = sizeof ".FFFFFFFFF" - 1 };
1033 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
1034 INT_STRLEN_BOUND (int) + 16)
1035 + fraclen];
1036 struct tm *tm;
1037 time_t s = t.tv_sec;
1038 int ns = t.tv_nsec;
1039 bool negative = s < 0;
1040 char *p;
1042 if (negative && ns != 0)
1044 s++;
1045 ns = 1000000000 - ns;
1048 tm = utc_option ? gmtime (&s) : localtime (&s);
1049 if (tm)
1051 if (full_time)
1053 strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M:%S", tm);
1054 code_ns_fraction (ns, buffer + strlen (buffer));
1056 else
1057 strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M", tm);
1058 return buffer;
1061 /* The time stamp cannot be broken down, most likely because it
1062 is out of range. Convert it as an integer,
1063 right-adjusted in a field with the same width as the usual
1064 4-year ISO time format. */
1065 p = umaxtostr (negative ? - (uintmax_t) s : s,
1066 buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen);
1067 if (negative)
1068 *--p = '-';
1069 while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM"
1070 + (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1071 < p)
1072 *--p = ' ';
1073 if (full_time)
1074 code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen);
1075 return p;
1078 /* Actually print it.
1080 Plain and fancy file header block logging. Non-verbose just prints
1081 the name, e.g. for "tar t" or "tar x". This should just contain
1082 file names, so it can be fed back into tar with xargs or the "-T"
1083 option. The verbose option can give a bunch of info, one line per
1084 file. I doubt anybody tries to parse its format, or if they do,
1085 they shouldn't. Unix tar is pretty random here anyway. */
1088 /* Width of "user/group size", with initial value chosen
1089 heuristically. This grows as needed, though this may cause some
1090 stairstepping in the output. Make it too small and the output will
1091 almost always look ragged. Make it too large and the output will
1092 be spaced out too far. */
1093 static int ugswidth = 19;
1095 /* Width of printed time stamps. It grows if longer time stamps are
1096 found (typically, those with nanosecond resolution). Like
1097 USGWIDTH, some stairstepping may occur. */
1098 static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1;
1100 static bool volume_label_printed = false;
1102 static void
1103 simple_print_header (struct tar_stat_info *st, union block *blk,
1104 off_t block_ordinal)
1106 char modes[12];
1107 char const *time_stamp;
1108 int time_stamp_len;
1109 char *temp_name;
1111 /* These hold formatted ints. */
1112 char uform[max (INT_BUFSIZE_BOUND (intmax_t), UINTMAX_STRSIZE_BOUND)];
1113 char gform[sizeof uform];
1114 char *user, *group;
1115 char size[2 * UINTMAX_STRSIZE_BOUND];
1116 /* holds formatted size or major,minor */
1117 char uintbuf[UINTMAX_STRSIZE_BOUND];
1118 int pad;
1119 int sizelen;
1121 if (show_transformed_names_option)
1122 temp_name = st->file_name ? st->file_name : st->orig_file_name;
1123 else
1124 temp_name = st->orig_file_name ? st->orig_file_name : st->file_name;
1126 if (block_number_option)
1128 char buf[UINTMAX_STRSIZE_BOUND];
1129 if (block_ordinal < 0)
1130 block_ordinal = current_block_ordinal ();
1131 block_ordinal -= recent_long_name_blocks;
1132 block_ordinal -= recent_long_link_blocks;
1133 fprintf (stdlis, _("block %s: "),
1134 STRINGIFY_BIGINT (block_ordinal, buf));
1137 if (verbose_option <= 1)
1139 /* Just the fax, mam. */
1140 fputs (quotearg (temp_name), stdlis);
1141 if (show_transformed_names_option && st->had_trailing_slash)
1142 fputc ('/', stdlis);
1143 fputc ('\n', stdlis);
1145 else
1147 /* File type and modes. */
1149 modes[0] = '?';
1150 switch (blk->header.typeflag)
1152 case GNUTYPE_VOLHDR:
1153 volume_label_printed = true;
1154 modes[0] = 'V';
1155 break;
1157 case GNUTYPE_MULTIVOL:
1158 modes[0] = 'M';
1159 break;
1161 case GNUTYPE_LONGNAME:
1162 case GNUTYPE_LONGLINK:
1163 modes[0] = 'L';
1164 ERROR ((0, 0, _("Unexpected long name header")));
1165 break;
1167 case GNUTYPE_SPARSE:
1168 case REGTYPE:
1169 case AREGTYPE:
1170 modes[0] = st->had_trailing_slash ? 'd' : '-';
1171 break;
1172 case LNKTYPE:
1173 modes[0] = 'h';
1174 break;
1175 case GNUTYPE_DUMPDIR:
1176 modes[0] = 'd';
1177 break;
1178 case DIRTYPE:
1179 modes[0] = 'd';
1180 break;
1181 case SYMTYPE:
1182 modes[0] = 'l';
1183 break;
1184 case BLKTYPE:
1185 modes[0] = 'b';
1186 break;
1187 case CHRTYPE:
1188 modes[0] = 'c';
1189 break;
1190 case FIFOTYPE:
1191 modes[0] = 'p';
1192 break;
1193 case CONTTYPE:
1194 modes[0] = 'C';
1195 break;
1198 pax_decode_mode (st->stat.st_mode, modes + 1);
1200 /* extended attributes: GNU `ls -l'-like preview */
1201 xattrs_print_char (st, modes + 10);
1203 /* Time stamp. */
1205 time_stamp = tartime (st->mtime, full_time_option);
1206 time_stamp_len = strlen (time_stamp);
1207 if (datewidth < time_stamp_len)
1208 datewidth = time_stamp_len;
1210 /* User and group names. */
1212 if (st->uname
1213 && st->uname[0]
1214 && current_format != V7_FORMAT
1215 && !numeric_owner_option)
1216 user = st->uname;
1217 else
1218 user = STRINGIFY_BIGINT (st->stat.st_uid, uform);
1220 if (st->gname
1221 && st->gname[0]
1222 && current_format != V7_FORMAT
1223 && !numeric_owner_option)
1224 group = st->gname;
1225 else
1226 group = STRINGIFY_BIGINT (st->stat.st_gid, gform);
1228 /* Format the file size or major/minor device numbers. */
1230 switch (blk->header.typeflag)
1232 case CHRTYPE:
1233 case BLKTYPE:
1234 strcpy (size,
1235 STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf));
1236 strcat (size, ",");
1237 strcat (size,
1238 STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf));
1239 break;
1241 default:
1242 /* st->stat.st_size keeps stored file size */
1243 strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf));
1244 break;
1247 /* Figure out padding and print the whole line. */
1249 sizelen = strlen (size);
1250 pad = strlen (user) + 1 + strlen (group) + 1 + sizelen;
1251 if (pad > ugswidth)
1252 ugswidth = pad;
1254 fprintf (stdlis, "%s %s/%s %*s %-*s",
1255 modes, user, group, ugswidth - pad + sizelen, size,
1256 datewidth, time_stamp);
1258 fprintf (stdlis, " %s", quotearg (temp_name));
1259 if (show_transformed_names_option && st->had_trailing_slash)
1260 fputc ('/', stdlis);
1262 switch (blk->header.typeflag)
1264 case SYMTYPE:
1265 fprintf (stdlis, " -> %s\n", quotearg (st->link_name));
1266 break;
1268 case LNKTYPE:
1269 fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name));
1270 break;
1272 default:
1274 char type_string[2];
1275 type_string[0] = blk->header.typeflag;
1276 type_string[1] = '\0';
1277 fprintf (stdlis, _(" unknown file type %s\n"),
1278 quote (type_string));
1280 break;
1282 case AREGTYPE:
1283 case REGTYPE:
1284 case GNUTYPE_SPARSE:
1285 case CHRTYPE:
1286 case BLKTYPE:
1287 case DIRTYPE:
1288 case FIFOTYPE:
1289 case CONTTYPE:
1290 case GNUTYPE_DUMPDIR:
1291 putc ('\n', stdlis);
1292 break;
1294 case GNUTYPE_LONGLINK:
1295 fprintf (stdlis, _("--Long Link--\n"));
1296 break;
1298 case GNUTYPE_LONGNAME:
1299 fprintf (stdlis, _("--Long Name--\n"));
1300 break;
1302 case GNUTYPE_VOLHDR:
1303 fprintf (stdlis, _("--Volume Header--\n"));
1304 break;
1306 case GNUTYPE_MULTIVOL:
1307 strcpy (size,
1308 STRINGIFY_BIGINT
1309 (UINTMAX_FROM_HEADER (blk->oldgnu_header.offset),
1310 uintbuf));
1311 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1312 break;
1315 fflush (stdlis);
1316 xattrs_print (st);
1320 static void
1321 print_volume_label (void)
1323 struct tar_stat_info vstat;
1324 union block vblk;
1325 enum archive_format dummy;
1327 memset (&vblk, 0, sizeof (vblk));
1328 vblk.header.typeflag = GNUTYPE_VOLHDR;
1329 if (recent_global_header)
1330 memcpy (vblk.header.mtime, recent_global_header->header.mtime,
1331 sizeof vblk.header.mtime);
1332 tar_stat_init (&vstat);
1333 assign_string (&vstat.file_name, ".");
1334 decode_header (&vblk, &vstat, &dummy, 0);
1335 assign_string (&vstat.file_name, volume_label);
1336 simple_print_header (&vstat, &vblk, 0);
1337 tar_stat_destroy (&vstat);
1340 void
1341 print_header (struct tar_stat_info *st, union block *blk,
1342 off_t block_ordinal)
1344 if (current_format == POSIX_FORMAT && !volume_label_printed && volume_label)
1346 print_volume_label ();
1347 volume_label_printed = true;
1350 simple_print_header (st, blk, block_ordinal);
1353 /* Print a similar line when we make a directory automatically. */
1354 void
1355 print_for_mkdir (char *dirname, int length, mode_t mode)
1357 char modes[11];
1359 if (verbose_option > 1)
1361 /* File type and modes. */
1363 modes[0] = 'd';
1364 pax_decode_mode (mode, modes + 1);
1366 if (block_number_option)
1368 char buf[UINTMAX_STRSIZE_BOUND];
1369 fprintf (stdlis, _("block %s: "),
1370 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1373 fprintf (stdlis, "%s %*s %s\n", modes, ugswidth + 1 + datewidth,
1374 _("Creating directory:"), quotearg (dirname));
1378 /* Skip over SIZE bytes of data in blocks in the archive. */
1379 void
1380 skip_file (off_t size)
1382 union block *x;
1384 /* FIXME: Make sure mv_begin_read is always called before it */
1386 if (seekable_archive)
1388 off_t nblk = seek_archive (size);
1389 if (nblk >= 0)
1390 size -= nblk * BLOCKSIZE;
1391 else
1392 seekable_archive = false;
1395 mv_size_left (size);
1397 while (size > 0)
1399 x = find_next_block ();
1400 if (! x)
1401 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1403 set_next_block_after (x);
1404 size -= BLOCKSIZE;
1405 mv_size_left (size);
1409 /* Skip the current member in the archive.
1410 NOTE: Current header must be decoded before calling this function. */
1411 void
1412 skip_member (void)
1414 if (!current_stat_info.skipped)
1416 char save_typeflag = current_header->header.typeflag;
1417 set_next_block_after (current_header);
1419 mv_begin_read (&current_stat_info);
1421 if (current_stat_info.is_sparse)
1422 sparse_skip_file (&current_stat_info);
1423 else if (save_typeflag != DIRTYPE)
1424 skip_file (current_stat_info.stat.st_size);
1426 mv_end ();
1430 void
1431 test_archive_label (void)
1433 base64_init ();
1434 name_gather ();
1436 open_archive (ACCESS_READ);
1437 if (read_header (&current_header, &current_stat_info, read_header_auto)
1438 == HEADER_SUCCESS)
1440 decode_header (current_header,
1441 &current_stat_info, &current_format, 0);
1442 if (current_header->header.typeflag == GNUTYPE_VOLHDR)
1443 ASSIGN_STRING_N (&volume_label, current_header->header.name);
1445 if (volume_label)
1447 if (verbose_option)
1448 print_volume_label ();
1449 if (!name_match (volume_label) && multi_volume_option)
1451 char *s = drop_volume_label_suffix (volume_label);
1452 name_match (s);
1453 free (s);
1457 close_archive ();
1458 label_notfound ();