Exclude VCS directory with writing from an archive
[tar.git] / src / list.c
blob86bcfdd1cc30cad5b4882a6745a435d28ab1ec82
1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright 1988-2023 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 (!ignore_zeros_option)
261 char buf[UINTMAX_STRSIZE_BOUND];
262 WARNOPT (WARN_MISSING_ZERO_BLOCKS,
263 (0, 0, _("Terminating zero blocks missing at %s"),
264 STRINGIFY_BIGINT (current_block_ordinal (), buf)));
266 if (block_number_option)
268 char buf[UINTMAX_STRSIZE_BOUND];
269 fprintf (stdlis, _("block %s: ** End of File **\n"),
270 STRINGIFY_BIGINT (current_block_ordinal (), buf));
272 break;
274 case HEADER_FAILURE:
275 /* If the previous header was good, tell them that we are
276 skipping bad ones. */
277 set_next_block_after (current_header);
278 switch (prev_status)
280 case HEADER_STILL_UNREAD:
281 ERROR ((0, 0, _("This does not look like a tar archive")));
282 FALLTHROUGH;
283 case HEADER_ZERO_BLOCK:
284 case HEADER_SUCCESS:
285 if (block_number_option)
287 char buf[UINTMAX_STRSIZE_BOUND];
288 off_t block_ordinal = current_block_ordinal ();
289 block_ordinal -= recent_long_name_blocks;
290 block_ordinal -= recent_long_link_blocks;
291 fprintf (stdlis, _("block %s: "),
292 STRINGIFY_BIGINT (block_ordinal, buf));
294 ERROR ((0, 0, _("Skipping to next header")));
295 break;
297 case HEADER_END_OF_FILE:
298 case HEADER_FAILURE:
299 /* We are in the middle of a cascade of errors. */
300 break;
302 case HEADER_SUCCESS_EXTENDED:
303 abort ();
305 continue;
307 break;
309 while (!all_names_found (&current_stat_info));
311 close_archive ();
312 names_notfound (); /* print names not found */
315 /* Print a header block, based on tar options. */
316 void
317 list_archive (void)
319 off_t block_ordinal = current_block_ordinal ();
321 /* Print the header block. */
322 if (verbose_option)
323 print_header (&current_stat_info, current_header, block_ordinal);
325 if (incremental_option)
327 if (verbose_option > 2)
329 if (is_dumpdir (&current_stat_info))
330 list_dumpdir (current_stat_info.dumpdir,
331 dumpdir_size (current_stat_info.dumpdir));
335 skip_member ();
338 /* Check header checksum */
339 /* The standard BSD tar sources create the checksum by adding up the
340 bytes in the header as type char. I think the type char was unsigned
341 on the PDP-11, but it's signed on the Next and Sun. It looks like the
342 sources to BSD tar were never changed to compute the checksum
343 correctly, so both the Sun and Next add the bytes of the header as
344 signed chars. This doesn't cause a problem until you get a file with
345 a name containing characters with the high bit set. So tar_checksum
346 computes two checksums -- signed and unsigned. */
348 enum read_header
349 tar_checksum (union block *header, bool silent)
351 size_t i;
352 int unsigned_sum = 0; /* the POSIX one :-) */
353 int signed_sum = 0; /* the Sun one :-( */
354 int recorded_sum;
355 int parsed_sum;
356 char *p;
358 p = header->buffer;
359 for (i = sizeof *header; i-- != 0;)
361 unsigned_sum += (unsigned char) *p;
362 signed_sum += (signed char) (*p++);
365 if (unsigned_sum == 0)
366 return HEADER_ZERO_BLOCK;
368 /* Adjust checksum to count the "chksum" field as blanks. */
370 for (i = sizeof header->header.chksum; i-- != 0;)
372 unsigned_sum -= (unsigned char) header->header.chksum[i];
373 signed_sum -= (signed char) (header->header.chksum[i]);
375 unsigned_sum += ' ' * sizeof header->header.chksum;
376 signed_sum += ' ' * sizeof header->header.chksum;
378 parsed_sum = from_header (header->header.chksum,
379 sizeof header->header.chksum, 0,
380 0, INT_MAX, true, silent);
381 if (parsed_sum < 0)
382 return HEADER_FAILURE;
384 recorded_sum = parsed_sum;
386 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
387 return HEADER_FAILURE;
389 return HEADER_SUCCESS;
392 /* Read a block that's supposed to be a header block. Return its
393 address in *RETURN_BLOCK, and if it is good, the file's size
394 and names (file name, link name) in *INFO.
396 Return one of enum read_header describing the status of the
397 operation.
399 The MODE parameter instructs read_header what to do with special
400 header blocks, i.e.: extended POSIX, GNU long name or long link,
401 etc.:
403 read_header_auto process them automatically,
404 read_header_x_raw when a special header is read, return
405 HEADER_SUCCESS_EXTENDED without actually
406 processing the header,
407 read_header_x_global when a POSIX global header is read,
408 decode it and return HEADER_SUCCESS_EXTENDED.
410 You must always set_next_block_after(*return_block) to skip past
411 the header which this routine reads. */
413 enum read_header
414 read_header (union block **return_block, struct tar_stat_info *info,
415 enum read_header_mode mode)
417 union block *header;
418 char *bp;
419 union block *data_block;
420 size_t size, written;
421 union block *next_long_name = NULL;
422 union block *next_long_link = NULL;
423 size_t next_long_name_blocks = 0;
424 size_t next_long_link_blocks = 0;
425 enum read_header status = HEADER_SUCCESS;
427 while (1)
429 header = find_next_block ();
430 *return_block = header;
431 if (!header)
433 status = HEADER_END_OF_FILE;
434 break;
437 if ((status = tar_checksum (header, false)) != HEADER_SUCCESS)
438 break;
440 /* Good block. Decode file size and return. */
442 if (header->header.typeflag == LNKTYPE)
443 info->stat.st_size = 0; /* links 0 size on tape */
444 else
446 info->stat.st_size = OFF_FROM_HEADER (header->header.size);
447 if (info->stat.st_size < 0)
449 status = HEADER_FAILURE;
450 break;
454 if (header->header.typeflag == GNUTYPE_LONGNAME
455 || header->header.typeflag == GNUTYPE_LONGLINK
456 || header->header.typeflag == XHDTYPE
457 || header->header.typeflag == XGLTYPE
458 || header->header.typeflag == SOLARIS_XHDTYPE)
460 if (mode == read_header_x_raw)
462 status = HEADER_SUCCESS_EXTENDED;
463 break;
465 else if (header->header.typeflag == GNUTYPE_LONGNAME
466 || header->header.typeflag == GNUTYPE_LONGLINK)
468 union block *header_copy;
469 size_t name_size = info->stat.st_size;
470 size_t n = name_size % BLOCKSIZE;
471 size = name_size + BLOCKSIZE;
472 if (n)
473 size += BLOCKSIZE - n;
475 if (name_size != info->stat.st_size || size < name_size)
476 xalloc_die ();
478 header_copy = xmalloc (size + 1);
480 if (header->header.typeflag == GNUTYPE_LONGNAME)
482 free (next_long_name);
483 next_long_name = header_copy;
484 next_long_name_blocks = size / BLOCKSIZE;
486 else
488 free (next_long_link);
489 next_long_link = header_copy;
490 next_long_link_blocks = size / BLOCKSIZE;
493 set_next_block_after (header);
494 *header_copy = *header;
495 bp = header_copy->buffer + BLOCKSIZE;
497 for (size -= BLOCKSIZE; size > 0; size -= written)
499 data_block = find_next_block ();
500 if (! data_block)
502 ERROR ((0, 0, _("Unexpected EOF in archive")));
503 break;
505 written = available_space_after (data_block);
506 if (written > size)
507 written = size;
509 memcpy (bp, data_block->buffer, written);
510 bp += written;
511 set_next_block_after ((union block *)
512 (data_block->buffer + written - 1));
515 *bp = '\0';
517 else if (header->header.typeflag == XHDTYPE
518 || header->header.typeflag == SOLARIS_XHDTYPE)
519 xheader_read (&info->xhdr, header,
520 OFF_FROM_HEADER (header->header.size));
521 else if (header->header.typeflag == XGLTYPE)
523 struct xheader xhdr;
525 if (!recent_global_header)
526 recent_global_header = xmalloc (sizeof *recent_global_header);
527 memcpy (recent_global_header, header,
528 sizeof *recent_global_header);
529 memset (&xhdr, 0, sizeof xhdr);
530 xheader_read (&xhdr, header,
531 OFF_FROM_HEADER (header->header.size));
532 xheader_decode_global (&xhdr);
533 xheader_destroy (&xhdr);
534 if (mode == read_header_x_global)
536 status = HEADER_SUCCESS_EXTENDED;
537 break;
541 /* Loop! */
544 else
546 char const *name;
547 struct posix_header const *h = &header->header;
548 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
550 free (recent_long_name);
552 if (next_long_name)
554 name = next_long_name->buffer + BLOCKSIZE;
555 recent_long_name = next_long_name;
556 recent_long_name_blocks = next_long_name_blocks;
557 next_long_name = NULL;
559 else
561 /* Accept file names as specified by POSIX.1-1996
562 section 10.1.1. */
563 char *np = namebuf;
565 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
567 memcpy (np, h->prefix, sizeof h->prefix);
568 np[sizeof h->prefix] = '\0';
569 np += strlen (np);
570 *np++ = '/';
572 memcpy (np, h->name, sizeof h->name);
573 np[sizeof h->name] = '\0';
574 name = namebuf;
575 recent_long_name = 0;
576 recent_long_name_blocks = 0;
578 assign_string (&info->orig_file_name, name);
579 assign_string (&info->file_name, name);
580 info->had_trailing_slash = strip_trailing_slashes (info->file_name);
582 free (recent_long_link);
584 if (next_long_link)
586 name = next_long_link->buffer + BLOCKSIZE;
587 recent_long_link = next_long_link;
588 recent_long_link_blocks = next_long_link_blocks;
589 next_long_link = NULL;
591 else
593 memcpy (namebuf, h->linkname, sizeof h->linkname);
594 namebuf[sizeof h->linkname] = '\0';
595 name = namebuf;
596 recent_long_link = 0;
597 recent_long_link_blocks = 0;
599 assign_string (&info->link_name, name);
601 break;
604 free (next_long_name);
605 free (next_long_link);
606 return status;
609 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
611 /* Decode things from a file HEADER block into STAT_INFO, also setting
612 *FORMAT_POINTER depending on the header block format. If
613 DO_USER_GROUP, decode the user/group information (this is useful
614 for extraction, but waste time when merely listing).
616 read_header() has already decoded the checksum and length, so we don't.
618 This routine should *not* be called twice for the same block, since
619 the two calls might use different DO_USER_GROUP values and thus
620 might end up with different uid/gid for the two calls. If anybody
621 wants the uid/gid they should decode it first, and other callers
622 should decode it without uid/gid before calling a routine,
623 e.g. print_header, that assumes decoded data. */
624 void
625 decode_header (union block *header, struct tar_stat_info *stat_info,
626 enum archive_format *format_pointer, int do_user_group)
628 enum archive_format format;
629 bool hbits;
630 mode_t mode = MODE_FROM_HEADER (header->header.mode, &hbits);
632 if (strcmp (header->header.magic, TMAGIC) == 0)
634 if (header->star_header.prefix[130] == 0
635 && ISOCTAL (header->star_header.atime[0])
636 && header->star_header.atime[11] == ' '
637 && ISOCTAL (header->star_header.ctime[0])
638 && header->star_header.ctime[11] == ' ')
639 format = STAR_FORMAT;
640 else if (stat_info->xhdr.size)
641 format = POSIX_FORMAT;
642 else
643 format = USTAR_FORMAT;
645 else if (strcmp (header->buffer + offsetof (struct posix_header, magic),
646 OLDGNU_MAGIC)
647 == 0)
648 format = hbits ? OLDGNU_FORMAT : GNU_FORMAT;
649 else
650 format = V7_FORMAT;
651 *format_pointer = format;
653 stat_info->stat.st_mode = mode;
654 stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime);
655 stat_info->mtime.tv_nsec = 0;
656 assign_string_n (&stat_info->uname,
657 header->header.uname[0] ? header->header.uname : NULL,
658 sizeof (header->header.uname));
659 assign_string_n (&stat_info->gname,
660 header->header.gname[0] ? header->header.gname : NULL,
661 sizeof (header->header.gname));
663 xheader_xattr_init (stat_info);
665 if (format == OLDGNU_FORMAT && incremental_option)
667 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime);
668 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime);
669 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
671 else if (format == STAR_FORMAT)
673 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime);
674 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime);
675 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
677 else
678 stat_info->atime = stat_info->ctime = start_time;
680 if (format == V7_FORMAT)
682 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
683 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
684 stat_info->stat.st_rdev = 0;
686 else
688 if (do_user_group)
690 /* FIXME: Decide if this should somewhat depend on -p. */
692 if (numeric_owner_option
693 || !*header->header.uname
694 || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid))
695 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
697 if (numeric_owner_option
698 || !*header->header.gname
699 || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid))
700 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
703 switch (header->header.typeflag)
705 case BLKTYPE:
706 case CHRTYPE:
707 stat_info->stat.st_rdev =
708 makedev (MAJOR_FROM_HEADER (header->header.devmajor),
709 MINOR_FROM_HEADER (header->header.devminor));
710 break;
712 default:
713 stat_info->stat.st_rdev = 0;
717 xheader_decode (stat_info);
719 if (sparse_member_p (stat_info))
721 sparse_fixup_header (stat_info);
722 stat_info->is_sparse = true;
724 else
726 stat_info->is_sparse = false;
727 if (((current_format == GNU_FORMAT
728 || current_format == OLDGNU_FORMAT)
729 && current_header->header.typeflag == GNUTYPE_DUMPDIR)
730 || stat_info->dumpdir)
731 stat_info->is_dumpdir = true;
736 /* Convert buffer at WHERE0 of size DIGS from external format to
737 intmax_t. DIGS must be positive. If TYPE is nonnull, the data are
738 of type TYPE. The buffer must represent a value in the range
739 MINVAL through MAXVAL; if the mathematically correct result V would
740 be greater than INTMAX_MAX, return a negative integer V such that
741 (uintmax_t) V yields the correct result. If OCTAL_ONLY, allow only octal
742 numbers instead of the other GNU extensions. Return -1 on error,
743 diagnosing the error if TYPE is nonnull and if !SILENT. */
744 #if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX)
745 # error "from_header internally represents intmax_t as uintmax_t + sign"
746 #endif
747 #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
748 # error "from_header returns intmax_t to represent uintmax_t"
749 #endif
750 static intmax_t
751 from_header (char const *where0, size_t digs, char const *type,
752 intmax_t minval, uintmax_t maxval,
753 bool octal_only, bool silent)
755 uintmax_t value;
756 uintmax_t uminval = minval;
757 uintmax_t minus_minval = - uminval;
758 char const *where = where0;
759 char const *lim = where + digs;
760 bool negative = false;
762 /* Accommodate buggy tar of unknown vintage, which outputs leading
763 NUL if the previous field overflows. */
764 where += !*where;
766 /* Accommodate older tars, which output leading spaces. */
767 for (;;)
769 if (where == lim)
771 if (type && !silent)
772 ERROR ((0, 0,
773 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
774 etc.) */
775 _("Blanks in header where numeric %s value expected"),
776 type));
777 return -1;
779 if (!isspace ((unsigned char) *where))
780 break;
781 where++;
784 value = 0;
785 if (ISODIGIT (*where))
787 char const *where1 = where;
788 bool overflow = false;
790 for (;;)
792 value += *where++ - '0';
793 if (where == lim || ! ISODIGIT (*where))
794 break;
795 overflow |= value != (value << LG_8 >> LG_8);
796 value <<= LG_8;
799 /* Parse the output of older, unportable tars, which generate
800 negative values in two's complement octal. If the leading
801 nonzero digit is 1, we can't recover the original value
802 reliably; so do this only if the digit is 2 or more. This
803 catches the common case of 32-bit negative time stamps. */
804 if ((overflow || maxval < value) && '2' <= *where1 && type)
806 /* Compute the negative of the input value, assuming two's
807 complement. */
808 int digit = (*where1 - '0') | 4;
809 overflow = 0;
810 value = 0;
811 where = where1;
812 for (;;)
814 value += 7 - digit;
815 where++;
816 if (where == lim || ! ISODIGIT (*where))
817 break;
818 digit = *where - '0';
819 overflow |= value != (value << LG_8 >> LG_8);
820 value <<= LG_8;
822 value++;
823 overflow |= !value;
825 if (!overflow && value <= minus_minval)
827 if (!silent)
828 WARN ((0, 0,
829 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
830 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
831 (int) (where - where1), where1, type));
832 negative = true;
836 if (overflow)
838 if (type && !silent)
839 ERROR ((0, 0,
840 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
841 _("Archive octal value %.*s is out of %s range"),
842 (int) (where - where1), where1, type));
843 return -1;
846 else if (octal_only)
848 /* Suppress the following extensions. */
850 else if (*where == '-' || *where == '+')
852 /* Parse base-64 output produced only by tar test versions
853 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
854 Support for this will be withdrawn in future releases. */
855 int dig;
856 if (!silent)
858 static bool warned_once;
859 if (! warned_once)
861 warned_once = true;
862 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
865 negative = *where++ == '-';
866 while (where != lim
867 && (dig = base64_map[(unsigned char) *where]) < 64)
869 if (value << LG_64 >> LG_64 != value)
871 char *string = alloca (digs + 1);
872 memcpy (string, where0, digs);
873 string[digs] = '\0';
874 if (type && !silent)
875 ERROR ((0, 0,
876 _("Archive signed base-64 string %s is out of %s range"),
877 quote (string), type));
878 return -1;
880 value = (value << LG_64) | dig;
881 where++;
884 else if (where <= lim - 2
885 && (*where == '\200' /* positive base-256 */
886 || *where == '\377' /* negative base-256 */))
888 /* Parse base-256 output. A nonnegative number N is
889 represented as (256**DIGS)/2 + N; a negative number -N is
890 represented as (256**DIGS) - N, i.e. as two's complement.
891 The representation guarantees that the leading bit is
892 always on, so that we don't confuse this format with the
893 others (assuming ASCII bytes of 8 bits or more). */
894 int signbit = *where & (1 << (LG_256 - 2));
895 uintmax_t topbits = (((uintmax_t) - signbit)
896 << (CHAR_BIT * sizeof (uintmax_t)
897 - LG_256 - (LG_256 - 2)));
898 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
899 for (;;)
901 value = (value << LG_256) + (unsigned char) *where++;
902 if (where == lim)
903 break;
904 if (((value << LG_256 >> LG_256) | topbits) != value)
906 if (type && !silent)
907 ERROR ((0, 0,
908 _("Archive base-256 value is out of %s range"),
909 type));
910 return -1;
913 negative = signbit != 0;
914 if (negative)
915 value = -value;
918 if (where != lim && *where && !isspace ((unsigned char) *where))
920 if (type)
922 char buf[1000]; /* Big enough to represent any header. */
923 static struct quoting_options *o;
925 if (!o)
927 o = clone_quoting_options (0);
928 set_quoting_style (o, locale_quoting_style);
931 while (where0 != lim && ! lim[-1])
932 lim--;
933 quotearg_buffer (buf, sizeof buf, where0, lim - where0, o);
934 if (!silent)
935 ERROR ((0, 0,
936 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
937 _("Archive contains %.*s where numeric %s value expected"),
938 (int) sizeof buf, buf, type));
941 return -1;
944 if (value <= (negative ? minus_minval : maxval))
945 return represent_uintmax (negative ? -value : value);
947 if (type && !silent)
949 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
950 char maxval_buf[UINTMAX_STRSIZE_BOUND];
951 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
952 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
953 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
954 if (negative)
955 *--value_string = '-';
956 if (minus_minval)
957 *--minval_string = '-';
958 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
959 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
960 value_string, type,
961 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
964 return -1;
967 static gid_t
968 gid_from_header (const char *p, size_t s)
970 return from_header (p, s, "gid_t",
971 TYPE_MINIMUM (gid_t), TYPE_MAXIMUM (gid_t),
972 false, false);
975 static major_t
976 major_from_header (const char *p, size_t s)
978 return from_header (p, s, "major_t",
979 TYPE_MINIMUM (major_t), TYPE_MAXIMUM (major_t),
980 false, false);
983 static minor_t
984 minor_from_header (const char *p, size_t s)
986 return from_header (p, s, "minor_t",
987 TYPE_MINIMUM (minor_t), TYPE_MAXIMUM (minor_t),
988 false, false);
991 /* Convert P to the file mode, as understood by tar.
992 Set *HBITS if there are any unrecognized bits. */
993 static mode_t
994 mode_from_header (const char *p, size_t s, bool *hbits)
996 intmax_t u = from_header (p, s, "mode_t",
997 INTMAX_MIN, UINTMAX_MAX,
998 false, false);
999 mode_t mode = ((u & TSUID ? S_ISUID : 0)
1000 | (u & TSGID ? S_ISGID : 0)
1001 | (u & TSVTX ? S_ISVTX : 0)
1002 | (u & TUREAD ? S_IRUSR : 0)
1003 | (u & TUWRITE ? S_IWUSR : 0)
1004 | (u & TUEXEC ? S_IXUSR : 0)
1005 | (u & TGREAD ? S_IRGRP : 0)
1006 | (u & TGWRITE ? S_IWGRP : 0)
1007 | (u & TGEXEC ? S_IXGRP : 0)
1008 | (u & TOREAD ? S_IROTH : 0)
1009 | (u & TOWRITE ? S_IWOTH : 0)
1010 | (u & TOEXEC ? S_IXOTH : 0));
1011 *hbits = (u & ~07777) != 0;
1012 return mode;
1015 off_t
1016 off_from_header (const char *p, size_t s)
1018 /* Negative offsets are not allowed in tar files, so invoke
1019 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
1020 return from_header (p, s, "off_t",
1021 0, TYPE_MAXIMUM (off_t),
1022 false, false);
1025 static time_t
1026 time_from_header (const char *p, size_t s)
1028 return from_header (p, s, "time_t",
1029 TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t),
1030 false, false);
1033 static uid_t
1034 uid_from_header (const char *p, size_t s)
1036 return from_header (p, s, "uid_t",
1037 TYPE_MINIMUM (uid_t), TYPE_MAXIMUM (uid_t),
1038 false, false);
1041 uintmax_t
1042 uintmax_from_header (const char *p, size_t s)
1044 return from_header (p, s, "uintmax_t", 0, UINTMAX_MAX, false, false);
1048 /* Return a printable representation of T. The result points to
1049 static storage that can be reused in the next call to this
1050 function, to ctime, or to asctime. If FULL_TIME, then output the
1051 time stamp to its full resolution; otherwise, just output it to
1052 1-minute resolution. */
1053 char const *
1054 tartime (struct timespec t, bool full_time)
1056 enum { fraclen = sizeof ".FFFFFFFFF" - 1 };
1057 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
1058 INT_STRLEN_BOUND (int) + 16)
1059 + fraclen];
1060 struct tm *tm;
1061 time_t s = t.tv_sec;
1062 int ns = t.tv_nsec;
1063 bool negative = s < 0;
1064 char *p;
1066 if (negative && ns != 0)
1068 s++;
1069 ns = 1000000000 - ns;
1072 tm = utc_option ? gmtime (&s) : localtime (&s);
1073 if (tm)
1075 if (full_time)
1077 strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M:%S", tm);
1078 code_ns_fraction (ns, buffer + strlen (buffer));
1080 else
1081 strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M", tm);
1082 return buffer;
1085 /* The time stamp cannot be broken down, most likely because it
1086 is out of range. Convert it as an integer,
1087 right-adjusted in a field with the same width as the usual
1088 4-year ISO time format. */
1089 p = umaxtostr (negative ? - (uintmax_t) s : s,
1090 buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen);
1091 if (negative)
1092 *--p = '-';
1093 while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM"
1094 + (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1095 < p)
1096 *--p = ' ';
1097 if (full_time)
1098 code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen);
1099 return p;
1102 /* Actually print it.
1104 Plain and fancy file header block logging. Non-verbose just prints
1105 the name, e.g. for "tar t" or "tar x". This should just contain
1106 file names, so it can be fed back into tar with xargs or the "-T"
1107 option. The verbose option can give a bunch of info, one line per
1108 file. I doubt anybody tries to parse its format, or if they do,
1109 they shouldn't. Unix tar is pretty random here anyway. */
1112 /* Width of "user/group size", with initial value chosen
1113 heuristically. This grows as needed, though this may cause some
1114 stairstepping in the output. Make it too small and the output will
1115 almost always look ragged. Make it too large and the output will
1116 be spaced out too far. */
1117 static int ugswidth = 19;
1119 /* Width of printed time stamps. It grows if longer time stamps are
1120 found (typically, those with nanosecond resolution). Like
1121 USGWIDTH, some stairstepping may occur. */
1122 static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1;
1124 static bool volume_label_printed = false;
1126 static void
1127 simple_print_header (struct tar_stat_info *st, union block *blk,
1128 off_t block_ordinal)
1130 char modes[12];
1131 char const *time_stamp;
1132 int time_stamp_len;
1133 char *temp_name;
1135 /* These hold formatted ints. */
1136 char uform[max (INT_BUFSIZE_BOUND (intmax_t), UINTMAX_STRSIZE_BOUND)];
1137 char gform[sizeof uform];
1138 char *user, *group;
1139 char size[2 * UINTMAX_STRSIZE_BOUND];
1140 /* holds formatted size or major,minor */
1141 char uintbuf[UINTMAX_STRSIZE_BOUND];
1142 int pad;
1143 int sizelen;
1145 if (show_transformed_names_option)
1146 temp_name = st->file_name ? st->file_name : st->orig_file_name;
1147 else
1148 temp_name = st->orig_file_name ? st->orig_file_name : st->file_name;
1150 if (block_number_option)
1152 char buf[UINTMAX_STRSIZE_BOUND];
1153 if (block_ordinal < 0)
1154 block_ordinal = current_block_ordinal ();
1155 block_ordinal -= recent_long_name_blocks;
1156 block_ordinal -= recent_long_link_blocks;
1157 fprintf (stdlis, _("block %s: "),
1158 STRINGIFY_BIGINT (block_ordinal, buf));
1161 if (verbose_option <= 1)
1163 /* Just the fax, mam. */
1164 fputs (quotearg (temp_name), stdlis);
1165 if (show_transformed_names_option && st->had_trailing_slash)
1166 fputc ('/', stdlis);
1167 fputc ('\n', stdlis);
1169 else
1171 /* File type and modes. */
1173 modes[0] = '?';
1174 switch (blk->header.typeflag)
1176 case GNUTYPE_VOLHDR:
1177 volume_label_printed = true;
1178 modes[0] = 'V';
1179 break;
1181 case GNUTYPE_MULTIVOL:
1182 modes[0] = 'M';
1183 break;
1185 case GNUTYPE_LONGNAME:
1186 case GNUTYPE_LONGLINK:
1187 modes[0] = 'L';
1188 ERROR ((0, 0, _("Unexpected long name header")));
1189 break;
1191 case GNUTYPE_SPARSE:
1192 case REGTYPE:
1193 case AREGTYPE:
1194 modes[0] = st->had_trailing_slash ? 'd' : '-';
1195 break;
1196 case LNKTYPE:
1197 modes[0] = 'h';
1198 break;
1199 case GNUTYPE_DUMPDIR:
1200 modes[0] = 'd';
1201 break;
1202 case DIRTYPE:
1203 modes[0] = 'd';
1204 break;
1205 case SYMTYPE:
1206 modes[0] = 'l';
1207 break;
1208 case BLKTYPE:
1209 modes[0] = 'b';
1210 break;
1211 case CHRTYPE:
1212 modes[0] = 'c';
1213 break;
1214 case FIFOTYPE:
1215 modes[0] = 'p';
1216 break;
1217 case CONTTYPE:
1218 modes[0] = 'C';
1219 break;
1222 pax_decode_mode (st->stat.st_mode, modes + 1);
1224 /* extended attributes: GNU `ls -l'-like preview */
1225 xattrs_print_char (st, modes + 10);
1227 /* Time stamp. */
1229 time_stamp = tartime (st->mtime, full_time_option);
1230 time_stamp_len = strlen (time_stamp);
1231 if (datewidth < time_stamp_len)
1232 datewidth = time_stamp_len;
1234 /* User and group names. */
1236 if (st->uname
1237 && st->uname[0]
1238 && current_format != V7_FORMAT
1239 && !numeric_owner_option)
1240 user = st->uname;
1241 else
1242 user = STRINGIFY_BIGINT (st->stat.st_uid, uform);
1244 if (st->gname
1245 && st->gname[0]
1246 && current_format != V7_FORMAT
1247 && !numeric_owner_option)
1248 group = st->gname;
1249 else
1250 group = STRINGIFY_BIGINT (st->stat.st_gid, gform);
1252 /* Format the file size or major/minor device numbers. */
1254 switch (blk->header.typeflag)
1256 case CHRTYPE:
1257 case BLKTYPE:
1258 strcpy (size,
1259 STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf));
1260 strcat (size, ",");
1261 strcat (size,
1262 STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf));
1263 break;
1265 default:
1266 /* st->stat.st_size keeps stored file size */
1267 strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf));
1268 break;
1271 /* Figure out padding and print the whole line. */
1273 sizelen = strlen (size);
1274 pad = strlen (user) + 1 + strlen (group) + 1 + sizelen;
1275 if (pad > ugswidth)
1276 ugswidth = pad;
1278 fprintf (stdlis, "%s %s/%s %*s %-*s",
1279 modes, user, group, ugswidth - pad + sizelen, size,
1280 datewidth, time_stamp);
1282 fprintf (stdlis, " %s", quotearg (temp_name));
1283 if (show_transformed_names_option && st->had_trailing_slash)
1284 fputc ('/', stdlis);
1286 switch (blk->header.typeflag)
1288 case SYMTYPE:
1289 fprintf (stdlis, " -> %s\n", quotearg (st->link_name));
1290 break;
1292 case LNKTYPE:
1293 fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name));
1294 break;
1296 default:
1298 char type_string[2];
1299 type_string[0] = blk->header.typeflag;
1300 type_string[1] = '\0';
1301 fprintf (stdlis, _(" unknown file type %s\n"),
1302 quote (type_string));
1304 break;
1306 case AREGTYPE:
1307 case REGTYPE:
1308 case GNUTYPE_SPARSE:
1309 case CHRTYPE:
1310 case BLKTYPE:
1311 case DIRTYPE:
1312 case FIFOTYPE:
1313 case CONTTYPE:
1314 case GNUTYPE_DUMPDIR:
1315 putc ('\n', stdlis);
1316 break;
1318 case GNUTYPE_LONGLINK:
1319 fprintf (stdlis, _("--Long Link--\n"));
1320 break;
1322 case GNUTYPE_LONGNAME:
1323 fprintf (stdlis, _("--Long Name--\n"));
1324 break;
1326 case GNUTYPE_VOLHDR:
1327 fprintf (stdlis, _("--Volume Header--\n"));
1328 break;
1330 case GNUTYPE_MULTIVOL:
1331 strcpy (size,
1332 STRINGIFY_BIGINT
1333 (UINTMAX_FROM_HEADER (blk->oldgnu_header.offset),
1334 uintbuf));
1335 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1336 break;
1339 fflush (stdlis);
1340 xattrs_print (st);
1344 static void
1345 print_volume_label (void)
1347 struct tar_stat_info vstat;
1348 union block vblk;
1349 enum archive_format dummy;
1351 memset (&vblk, 0, sizeof (vblk));
1352 vblk.header.typeflag = GNUTYPE_VOLHDR;
1353 if (recent_global_header)
1354 memcpy (vblk.header.mtime, recent_global_header->header.mtime,
1355 sizeof vblk.header.mtime);
1356 tar_stat_init (&vstat);
1357 assign_string (&vstat.file_name, ".");
1358 decode_header (&vblk, &vstat, &dummy, 0);
1359 assign_string (&vstat.file_name, volume_label);
1360 simple_print_header (&vstat, &vblk, 0);
1361 tar_stat_destroy (&vstat);
1364 void
1365 print_header (struct tar_stat_info *st, union block *blk,
1366 off_t block_ordinal)
1368 if (current_format == POSIX_FORMAT && !volume_label_printed && volume_label)
1370 print_volume_label ();
1371 volume_label_printed = true;
1374 simple_print_header (st, blk, block_ordinal);
1377 /* Print a similar line when we make a directory automatically. */
1378 void
1379 print_for_mkdir (char *dirname, int length, mode_t mode)
1381 char modes[11];
1383 if (verbose_option > 1)
1385 /* File type and modes. */
1387 modes[0] = 'd';
1388 pax_decode_mode (mode, modes + 1);
1390 if (block_number_option)
1392 char buf[UINTMAX_STRSIZE_BOUND];
1393 fprintf (stdlis, _("block %s: "),
1394 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1397 fprintf (stdlis, "%s %*s %s\n", modes, ugswidth + 1 + datewidth,
1398 _("Creating directory:"), quotearg (dirname));
1402 /* Skip over SIZE bytes of data in blocks in the archive.
1403 This may involve copying the data.
1404 If MUST_COPY, always copy instead of skipping. */
1405 void
1406 skim_file (off_t size, bool must_copy)
1408 union block *x;
1410 /* FIXME: Make sure mv_begin_read is always called before it */
1412 if (seekable_archive && !must_copy)
1414 off_t nblk = seek_archive (size);
1415 if (nblk >= 0)
1416 size -= nblk * BLOCKSIZE;
1417 else
1418 seekable_archive = false;
1421 mv_size_left (size);
1423 while (size > 0)
1425 x = find_next_block ();
1426 if (! x)
1427 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1429 set_next_block_after (x);
1430 size -= BLOCKSIZE;
1431 mv_size_left (size);
1435 /* Skip the current member in the archive.
1436 NOTE: Current header must be decoded before calling this function. */
1437 void
1438 skip_member (void)
1440 skim_member (false);
1443 /* Skip the current member in the archive.
1444 If MUST_COPY, always copy instead of skipping. */
1445 void
1446 skim_member (bool must_copy)
1448 if (!current_stat_info.skipped)
1450 char save_typeflag = current_header->header.typeflag;
1451 set_next_block_after (current_header);
1453 mv_begin_read (&current_stat_info);
1455 if (current_stat_info.is_sparse)
1456 sparse_skim_file (&current_stat_info, must_copy);
1457 else if (save_typeflag != DIRTYPE)
1458 skim_file (current_stat_info.stat.st_size, must_copy);
1460 mv_end ();
1464 void
1465 test_archive_label (void)
1467 base64_init ();
1468 name_gather ();
1470 open_archive (ACCESS_READ);
1471 if (read_header (&current_header, &current_stat_info, read_header_auto)
1472 == HEADER_SUCCESS)
1474 decode_header (current_header,
1475 &current_stat_info, &current_format, 0);
1476 if (current_header->header.typeflag == GNUTYPE_VOLHDR)
1477 ASSIGN_STRING_N (&volume_label, current_header->header.name);
1479 if (volume_label)
1481 if (verbose_option)
1482 print_volume_label ();
1483 if (!name_match (volume_label) && multi_volume_option)
1485 char *s = drop_volume_label_suffix (volume_label);
1486 name_match (s);
1487 free (s);
1491 close_archive ();
1492 label_notfound ();