Fix description of the CACHEDIR.TAG file.
[tar.git] / src / list.c
blobe6a5aa60c840c7beda31dc950978395d649204e1
1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright 1988-2024 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 <c-ctype.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 (!ignore_zeros_option)
262 char buf[UINTMAX_STRSIZE_BOUND];
263 WARNOPT (WARN_MISSING_ZERO_BLOCKS,
264 (0, 0, _("Terminating zero blocks missing at %s"),
265 STRINGIFY_BIGINT (current_block_ordinal (), buf)));
267 if (block_number_option)
269 char buf[UINTMAX_STRSIZE_BOUND];
270 fprintf (stdlis, _("block %s: ** End of File **\n"),
271 STRINGIFY_BIGINT (current_block_ordinal (), buf));
273 break;
275 case HEADER_FAILURE:
276 /* If the previous header was good, tell them that we are
277 skipping bad ones. */
278 set_next_block_after (current_header);
279 switch (prev_status)
281 case HEADER_STILL_UNREAD:
282 ERROR ((0, 0, _("This does not look like a tar archive")));
283 FALLTHROUGH;
284 case HEADER_ZERO_BLOCK:
285 case HEADER_SUCCESS:
286 if (block_number_option)
288 char buf[UINTMAX_STRSIZE_BOUND];
289 off_t block_ordinal = current_block_ordinal ();
290 block_ordinal -= recent_long_name_blocks;
291 block_ordinal -= recent_long_link_blocks;
292 fprintf (stdlis, _("block %s: "),
293 STRINGIFY_BIGINT (block_ordinal, buf));
295 ERROR ((0, 0, _("Skipping to next header")));
296 break;
298 case HEADER_END_OF_FILE:
299 case HEADER_FAILURE:
300 /* We are in the middle of a cascade of errors. */
301 break;
303 case HEADER_SUCCESS_EXTENDED:
304 abort ();
306 continue;
308 break;
310 while (!all_names_found (&current_stat_info));
312 close_archive ();
313 names_notfound (); /* print names not found */
316 /* Print a header block, based on tar options. */
317 void
318 list_archive (void)
320 off_t block_ordinal = current_block_ordinal ();
322 /* Print the header block. */
323 if (verbose_option)
324 print_header (&current_stat_info, current_header, block_ordinal);
326 if (incremental_option)
328 if (verbose_option > 2)
330 if (is_dumpdir (&current_stat_info))
331 list_dumpdir (current_stat_info.dumpdir,
332 dumpdir_size (current_stat_info.dumpdir));
336 skip_member ();
339 /* Check header checksum */
340 /* The standard BSD tar sources create the checksum by adding up the
341 bytes in the header as type char. I think the type char was unsigned
342 on the PDP-11, but it's signed on the Next and Sun. It looks like the
343 sources to BSD tar were never changed to compute the checksum
344 correctly, so both the Sun and Next add the bytes of the header as
345 signed chars. This doesn't cause a problem until you get a file with
346 a name containing characters with the high bit set. So tar_checksum
347 computes two checksums -- signed and unsigned. */
349 enum read_header
350 tar_checksum (union block *header, bool silent)
352 size_t i;
353 int unsigned_sum = 0; /* the POSIX one :-) */
354 int signed_sum = 0; /* the Sun one :-( */
355 int recorded_sum;
356 int parsed_sum;
357 char *p;
359 p = header->buffer;
360 for (i = sizeof *header; i-- != 0;)
362 unsigned_sum += (unsigned char) *p;
363 signed_sum += (signed char) (*p++);
366 if (unsigned_sum == 0)
367 return HEADER_ZERO_BLOCK;
369 /* Adjust checksum to count the "chksum" field as blanks. */
371 for (i = sizeof header->header.chksum; i-- != 0;)
373 unsigned_sum -= (unsigned char) header->header.chksum[i];
374 signed_sum -= (signed char) (header->header.chksum[i]);
376 unsigned_sum += ' ' * sizeof header->header.chksum;
377 signed_sum += ' ' * sizeof header->header.chksum;
379 parsed_sum = from_header (header->header.chksum,
380 sizeof header->header.chksum, 0,
381 0, INT_MAX, true, silent);
382 if (parsed_sum < 0)
383 return HEADER_FAILURE;
385 recorded_sum = parsed_sum;
387 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
388 return HEADER_FAILURE;
390 return HEADER_SUCCESS;
393 /* Read a block that's supposed to be a header block. Return its
394 address in *RETURN_BLOCK, and if it is good, the file's size
395 and names (file name, link name) in *INFO.
397 Return one of enum read_header describing the status of the
398 operation.
400 The MODE parameter instructs read_header what to do with special
401 header blocks, i.e.: extended POSIX, GNU long name or long link,
402 etc.:
404 read_header_auto process them automatically,
405 read_header_x_raw when a special header is read, return
406 HEADER_SUCCESS_EXTENDED without actually
407 processing the header,
408 read_header_x_global when a POSIX global header is read,
409 decode it and return HEADER_SUCCESS_EXTENDED.
411 You must always set_next_block_after(*return_block) to skip past
412 the header which this routine reads. */
414 enum read_header
415 read_header (union block **return_block, struct tar_stat_info *info,
416 enum read_header_mode mode)
418 union block *header;
419 char *bp;
420 union block *data_block;
421 size_t size, written;
422 union block *next_long_name = NULL;
423 union block *next_long_link = NULL;
424 size_t next_long_name_blocks = 0;
425 size_t next_long_link_blocks = 0;
426 enum read_header status = HEADER_SUCCESS;
428 while (1)
430 header = find_next_block ();
431 *return_block = header;
432 if (!header)
434 status = HEADER_END_OF_FILE;
435 break;
438 if ((status = tar_checksum (header, false)) != HEADER_SUCCESS)
439 break;
441 /* Good block. Decode file size and return. */
443 if (header->header.typeflag == LNKTYPE)
444 info->stat.st_size = 0; /* links 0 size on tape */
445 else
447 info->stat.st_size = OFF_FROM_HEADER (header->header.size);
448 if (info->stat.st_size < 0)
450 status = HEADER_FAILURE;
451 break;
455 if (header->header.typeflag == GNUTYPE_LONGNAME
456 || header->header.typeflag == GNUTYPE_LONGLINK
457 || header->header.typeflag == XHDTYPE
458 || header->header.typeflag == XGLTYPE
459 || header->header.typeflag == SOLARIS_XHDTYPE)
461 if (mode == read_header_x_raw)
463 status = HEADER_SUCCESS_EXTENDED;
464 break;
466 else if (header->header.typeflag == GNUTYPE_LONGNAME
467 || header->header.typeflag == GNUTYPE_LONGLINK)
469 union block *header_copy;
470 size_t name_size = info->stat.st_size;
471 size_t n = name_size % BLOCKSIZE;
472 size = name_size + BLOCKSIZE;
473 if (n)
474 size += BLOCKSIZE - n;
476 if (name_size != info->stat.st_size || size < name_size)
477 xalloc_die ();
479 header_copy = xmalloc (size + 1);
481 if (header->header.typeflag == GNUTYPE_LONGNAME)
483 free (next_long_name);
484 next_long_name = header_copy;
485 next_long_name_blocks = size / BLOCKSIZE;
487 else
489 free (next_long_link);
490 next_long_link = header_copy;
491 next_long_link_blocks = size / BLOCKSIZE;
494 set_next_block_after (header);
495 *header_copy = *header;
496 bp = header_copy->buffer + BLOCKSIZE;
498 for (size -= BLOCKSIZE; size > 0; size -= written)
500 data_block = find_next_block ();
501 if (! data_block)
503 ERROR ((0, 0, _("Unexpected EOF in archive")));
504 break;
506 written = available_space_after (data_block);
507 if (written > size)
508 written = size;
510 memcpy (bp, data_block->buffer, written);
511 bp += written;
512 set_next_block_after ((union block *)
513 (data_block->buffer + written - 1));
516 *bp = '\0';
518 else if (header->header.typeflag == XHDTYPE
519 || header->header.typeflag == SOLARIS_XHDTYPE)
520 xheader_read (&info->xhdr, header,
521 OFF_FROM_HEADER (header->header.size));
522 else if (header->header.typeflag == XGLTYPE)
524 struct xheader xhdr;
526 if (!recent_global_header)
527 recent_global_header = xmalloc (sizeof *recent_global_header);
528 memcpy (recent_global_header, header,
529 sizeof *recent_global_header);
530 memset (&xhdr, 0, sizeof xhdr);
531 xheader_read (&xhdr, header,
532 OFF_FROM_HEADER (header->header.size));
533 xheader_decode_global (&xhdr);
534 xheader_destroy (&xhdr);
535 if (mode == read_header_x_global)
537 status = HEADER_SUCCESS_EXTENDED;
538 break;
542 /* Loop! */
545 else
547 char const *name;
548 struct posix_header const *h = &header->header;
549 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
551 free (recent_long_name);
553 if (next_long_name)
555 name = next_long_name->buffer + BLOCKSIZE;
556 recent_long_name = next_long_name;
557 recent_long_name_blocks = next_long_name_blocks;
558 next_long_name = NULL;
560 else
562 /* Accept file names as specified by POSIX.1-1996
563 section 10.1.1. */
564 char *np = namebuf;
566 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
568 memcpy (np, h->prefix, sizeof h->prefix);
569 np[sizeof h->prefix] = '\0';
570 np += strlen (np);
571 *np++ = '/';
573 memcpy (np, h->name, sizeof h->name);
574 np[sizeof h->name] = '\0';
575 name = namebuf;
576 recent_long_name = 0;
577 recent_long_name_blocks = 0;
579 assign_string (&info->orig_file_name, name);
580 assign_string (&info->file_name, name);
581 info->had_trailing_slash = strip_trailing_slashes (info->file_name);
583 free (recent_long_link);
585 if (next_long_link)
587 name = next_long_link->buffer + BLOCKSIZE;
588 recent_long_link = next_long_link;
589 recent_long_link_blocks = next_long_link_blocks;
590 next_long_link = NULL;
592 else
594 memcpy (namebuf, h->linkname, sizeof h->linkname);
595 namebuf[sizeof h->linkname] = '\0';
596 name = namebuf;
597 recent_long_link = 0;
598 recent_long_link_blocks = 0;
600 assign_string (&info->link_name, name);
602 break;
605 free (next_long_name);
606 free (next_long_link);
607 return status;
610 static bool
611 is_octal_digit (char c)
613 return '0' <= c && c <= '7';
616 /* Decode things from a file HEADER block into STAT_INFO, also setting
617 *FORMAT_POINTER depending on the header block format. If
618 DO_USER_GROUP, decode the user/group information (this is useful
619 for extraction, but waste time when merely listing).
621 read_header() has already decoded the checksum and length, so we don't.
623 This routine should *not* be called twice for the same block, since
624 the two calls might use different DO_USER_GROUP values and thus
625 might end up with different uid/gid for the two calls. If anybody
626 wants the uid/gid they should decode it first, and other callers
627 should decode it without uid/gid before calling a routine,
628 e.g. print_header, that assumes decoded data. */
629 void
630 decode_header (union block *header, struct tar_stat_info *stat_info,
631 enum archive_format *format_pointer, int do_user_group)
633 enum archive_format format;
634 bool hbits;
635 mode_t mode = MODE_FROM_HEADER (header->header.mode, &hbits);
637 if (strcmp (header->header.magic, TMAGIC) == 0)
639 if (header->star_header.prefix[130] == 0
640 && is_octal_digit (header->star_header.atime[0])
641 && header->star_header.atime[11] == ' '
642 && is_octal_digit (header->star_header.ctime[0])
643 && header->star_header.ctime[11] == ' ')
644 format = STAR_FORMAT;
645 else if (stat_info->xhdr.size)
646 format = POSIX_FORMAT;
647 else
648 format = USTAR_FORMAT;
650 else if (strcmp (header->buffer + offsetof (struct posix_header, magic),
651 OLDGNU_MAGIC)
652 == 0)
653 format = hbits ? OLDGNU_FORMAT : GNU_FORMAT;
654 else
655 format = V7_FORMAT;
656 *format_pointer = format;
658 stat_info->stat.st_mode = mode;
659 stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime);
660 stat_info->mtime.tv_nsec = 0;
661 assign_string_n (&stat_info->uname,
662 header->header.uname[0] ? header->header.uname : NULL,
663 sizeof (header->header.uname));
664 assign_string_n (&stat_info->gname,
665 header->header.gname[0] ? header->header.gname : NULL,
666 sizeof (header->header.gname));
668 xheader_xattr_init (stat_info);
670 if (format == OLDGNU_FORMAT && incremental_option)
672 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime);
673 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime);
674 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
676 else if (format == STAR_FORMAT)
678 stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime);
679 stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime);
680 stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
682 else
683 stat_info->atime = stat_info->ctime = start_time;
685 if (format == V7_FORMAT)
687 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
688 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
689 stat_info->stat.st_rdev = 0;
691 else
693 if (do_user_group)
695 /* FIXME: Decide if this should somewhat depend on -p. */
697 if (numeric_owner_option
698 || !*header->header.uname
699 || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid))
700 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
702 if (numeric_owner_option
703 || !*header->header.gname
704 || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid))
705 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
708 switch (header->header.typeflag)
710 case BLKTYPE:
711 case CHRTYPE:
712 stat_info->stat.st_rdev =
713 makedev (MAJOR_FROM_HEADER (header->header.devmajor),
714 MINOR_FROM_HEADER (header->header.devminor));
715 break;
717 default:
718 stat_info->stat.st_rdev = 0;
722 xheader_decode (stat_info);
724 if (sparse_member_p (stat_info))
726 sparse_fixup_header (stat_info);
727 stat_info->is_sparse = true;
729 else
731 stat_info->is_sparse = false;
732 if (((current_format == GNU_FORMAT
733 || current_format == OLDGNU_FORMAT)
734 && current_header->header.typeflag == GNUTYPE_DUMPDIR)
735 || stat_info->dumpdir)
736 stat_info->is_dumpdir = true;
741 /* Convert buffer at WHERE0 of size DIGS from external format to
742 intmax_t. DIGS must be positive. If TYPE is nonnull, the data are
743 of type TYPE. The buffer must represent a value in the range
744 MINVAL through MAXVAL; if the mathematically correct result V would
745 be greater than INTMAX_MAX, return a negative integer V such that
746 (uintmax_t) V yields the correct result. If OCTAL_ONLY, allow only octal
747 numbers instead of the other GNU extensions. Return -1 on error,
748 diagnosing the error if TYPE is nonnull and if !SILENT. */
749 #if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX)
750 # error "from_header internally represents intmax_t as uintmax_t + sign"
751 #endif
752 #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
753 # error "from_header returns intmax_t to represent uintmax_t"
754 #endif
755 static intmax_t
756 from_header (char const *where0, size_t digs, char const *type,
757 intmax_t minval, uintmax_t maxval,
758 bool octal_only, bool silent)
760 uintmax_t value;
761 uintmax_t uminval = minval;
762 uintmax_t minus_minval = - uminval;
763 char const *where = where0;
764 char const *lim = where + digs;
765 bool negative = false;
767 /* Accommodate buggy tar of unknown vintage, which outputs leading
768 NUL if the previous field overflows. */
769 where += !*where;
771 /* Accommodate older tars, which output leading spaces. */
772 for (;;)
774 if (where == lim)
776 if (type && !silent)
777 ERROR ((0, 0,
778 /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
779 etc.) */
780 _("Blanks in header where numeric %s value expected"),
781 type));
782 return -1;
784 if (!c_isspace (*where))
785 break;
786 where++;
789 value = 0;
790 if (is_octal_digit (*where))
792 char const *where1 = where;
793 bool overflow = false;
795 for (;;)
797 value += *where++ - '0';
798 if (where == lim || ! is_octal_digit (*where))
799 break;
800 overflow |= value != (value << LG_8 >> LG_8);
801 value <<= LG_8;
804 /* Parse the output of older, unportable tars, which generate
805 negative values in two's complement octal. If the leading
806 nonzero digit is 1, we can't recover the original value
807 reliably; so do this only if the digit is 2 or more. This
808 catches the common case of 32-bit negative time stamps. */
809 if ((overflow || maxval < value) && '2' <= *where1 && type)
811 /* Compute the negative of the input value, assuming two's
812 complement. */
813 int digit = (*where1 - '0') | 4;
814 overflow = 0;
815 value = 0;
816 where = where1;
817 for (;;)
819 value += 7 - digit;
820 where++;
821 if (where == lim || ! is_octal_digit (*where))
822 break;
823 digit = *where - '0';
824 overflow |= value != (value << LG_8 >> LG_8);
825 value <<= LG_8;
827 value++;
828 overflow |= !value;
830 if (!overflow && value <= minus_minval)
832 if (!silent)
833 WARN ((0, 0,
834 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
835 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
836 (int) (where - where1), where1, type));
837 negative = true;
841 if (overflow)
843 if (type && !silent)
844 ERROR ((0, 0,
845 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
846 _("Archive octal value %.*s is out of %s range"),
847 (int) (where - where1), where1, type));
848 return -1;
851 else if (octal_only)
853 /* Suppress the following extensions. */
855 else if (*where == '-' || *where == '+')
857 /* Parse base-64 output produced only by tar test versions
858 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
859 Support for this will be withdrawn in future releases. */
860 int dig;
861 if (!silent)
863 static bool warned_once;
864 if (! warned_once)
866 warned_once = true;
867 WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
870 negative = *where++ == '-';
871 while (where != lim
872 && (dig = base64_map[(unsigned char) *where]) < 64)
874 if (value << LG_64 >> LG_64 != value)
876 if (type && !silent)
877 ERROR ((0, 0,
878 _("Archive signed base-64 string %s is out of %s range"),
879 quote_mem (where0, digs), type));
880 return -1;
882 value = (value << LG_64) | dig;
883 where++;
886 else if (where <= lim - 2
887 && (*where == '\200' /* positive base-256 */
888 || *where == '\377' /* negative base-256 */))
890 /* Parse base-256 output. A nonnegative number N is
891 represented as (256**DIGS)/2 + N; a negative number -N is
892 represented as (256**DIGS) - N, i.e. as two's complement.
893 The representation guarantees that the leading bit is
894 always on, so that we don't confuse this format with the
895 others (assuming ASCII bytes of 8 bits or more). */
896 int signbit = *where & (1 << (LG_256 - 2));
897 uintmax_t topbits = (((uintmax_t) - signbit)
898 << (CHAR_BIT * sizeof (uintmax_t)
899 - LG_256 - (LG_256 - 2)));
900 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
901 for (;;)
903 value = (value << LG_256) + (unsigned char) *where++;
904 if (where == lim)
905 break;
906 if (((value << LG_256 >> LG_256) | topbits) != value)
908 if (type && !silent)
909 ERROR ((0, 0,
910 _("Archive base-256 value is out of %s range"),
911 type));
912 return -1;
915 negative = signbit != 0;
916 if (negative)
917 value = -value;
920 if (where != lim && *where && !c_isspace (*where))
922 if (type)
924 char buf[1000]; /* Big enough to represent any header. */
925 static struct quoting_options *o;
927 if (!o)
929 o = clone_quoting_options (0);
930 set_quoting_style (o, locale_quoting_style);
933 while (where0 != lim && ! lim[-1])
934 lim--;
935 quotearg_buffer (buf, sizeof buf, where0, lim - where0, o);
936 if (!silent)
937 ERROR ((0, 0,
938 /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
939 _("Archive contains %.*s where numeric %s value expected"),
940 (int) sizeof buf, buf, type));
943 return -1;
946 if (value <= (negative ? minus_minval : maxval))
947 return represent_uintmax (negative ? -value : value);
949 if (type && !silent)
951 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
952 char maxval_buf[UINTMAX_STRSIZE_BOUND];
953 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
954 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
955 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
956 if (negative)
957 *--value_string = '-';
958 if (minus_minval)
959 *--minval_string = '-';
960 /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
961 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
962 value_string, type,
963 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
966 return -1;
969 static gid_t
970 gid_from_header (const char *p, size_t s)
972 return from_header (p, s, "gid_t",
973 TYPE_MINIMUM (gid_t), TYPE_MAXIMUM (gid_t),
974 false, false);
977 static major_t
978 major_from_header (const char *p, size_t s)
980 return from_header (p, s, "major_t",
981 TYPE_MINIMUM (major_t), TYPE_MAXIMUM (major_t),
982 false, false);
985 static minor_t
986 minor_from_header (const char *p, size_t s)
988 return from_header (p, s, "minor_t",
989 TYPE_MINIMUM (minor_t), TYPE_MAXIMUM (minor_t),
990 false, false);
993 /* Convert P to the file mode, as understood by tar.
994 Set *HBITS if there are any unrecognized bits. */
995 static mode_t
996 mode_from_header (const char *p, size_t s, bool *hbits)
998 intmax_t u = from_header (p, s, "mode_t",
999 INTMAX_MIN, UINTMAX_MAX,
1000 false, false);
1001 mode_t mode = ((u & TSUID ? S_ISUID : 0)
1002 | (u & TSGID ? S_ISGID : 0)
1003 | (u & TSVTX ? S_ISVTX : 0)
1004 | (u & TUREAD ? S_IRUSR : 0)
1005 | (u & TUWRITE ? S_IWUSR : 0)
1006 | (u & TUEXEC ? S_IXUSR : 0)
1007 | (u & TGREAD ? S_IRGRP : 0)
1008 | (u & TGWRITE ? S_IWGRP : 0)
1009 | (u & TGEXEC ? S_IXGRP : 0)
1010 | (u & TOREAD ? S_IROTH : 0)
1011 | (u & TOWRITE ? S_IWOTH : 0)
1012 | (u & TOEXEC ? S_IXOTH : 0));
1013 *hbits = (u & ~07777) != 0;
1014 return mode;
1017 off_t
1018 off_from_header (const char *p, size_t s)
1020 /* Negative offsets are not allowed in tar files, so invoke
1021 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
1022 return from_header (p, s, "off_t",
1023 0, TYPE_MAXIMUM (off_t),
1024 false, false);
1027 static time_t
1028 time_from_header (const char *p, size_t s)
1030 return from_header (p, s, "time_t",
1031 TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t),
1032 false, false);
1035 static uid_t
1036 uid_from_header (const char *p, size_t s)
1038 return from_header (p, s, "uid_t",
1039 TYPE_MINIMUM (uid_t), TYPE_MAXIMUM (uid_t),
1040 false, false);
1043 uintmax_t
1044 uintmax_from_header (const char *p, size_t s)
1046 return from_header (p, s, "uintmax_t", 0, UINTMAX_MAX, false, false);
1050 /* Return a printable representation of T. The result points to
1051 static storage that can be reused in the next call to this
1052 function, to ctime, or to asctime. If FULL_TIME, then output the
1053 time stamp to its full resolution; otherwise, just output it to
1054 1-minute resolution. */
1055 char const *
1056 tartime (struct timespec t, bool full_time)
1058 enum { fraclen = sizeof ".FFFFFFFFF" - 1 };
1059 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
1060 INT_STRLEN_BOUND (int) + 16)
1061 + fraclen];
1062 struct tm *tm;
1063 time_t s = t.tv_sec;
1064 int ns = t.tv_nsec;
1065 bool negative = s < 0;
1066 char *p;
1068 if (negative && ns != 0)
1070 s++;
1071 ns = 1000000000 - ns;
1074 tm = utc_option ? gmtime (&s) : localtime (&s);
1075 if (tm)
1077 if (full_time)
1079 strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M:%S", tm);
1080 code_ns_fraction (ns, buffer + strlen (buffer));
1082 else
1083 strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M", tm);
1084 return buffer;
1087 /* The time stamp cannot be broken down, most likely because it
1088 is out of range. Convert it as an integer,
1089 right-adjusted in a field with the same width as the usual
1090 4-year ISO time format. */
1091 p = umaxtostr (negative ? - (uintmax_t) s : s,
1092 buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen);
1093 if (negative)
1094 *--p = '-';
1095 while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM"
1096 + (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1097 < p)
1098 *--p = ' ';
1099 if (full_time)
1100 code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen);
1101 return p;
1104 /* Actually print it.
1106 Plain and fancy file header block logging. Non-verbose just prints
1107 the name, e.g. for "tar t" or "tar x". This should just contain
1108 file names, so it can be fed back into tar with xargs or the "-T"
1109 option. The verbose option can give a bunch of info, one line per
1110 file. I doubt anybody tries to parse its format, or if they do,
1111 they shouldn't. Unix tar is pretty random here anyway. */
1114 /* Width of "user/group size", with initial value chosen
1115 heuristically. This grows as needed, though this may cause some
1116 stairstepping in the output. Make it too small and the output will
1117 almost always look ragged. Make it too large and the output will
1118 be spaced out too far. */
1119 static int ugswidth = 19;
1121 /* Width of printed time stamps. It grows if longer time stamps are
1122 found (typically, those with nanosecond resolution). Like
1123 USGWIDTH, some stairstepping may occur. */
1124 static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1;
1126 static bool volume_label_printed = false;
1128 static void
1129 simple_print_header (struct tar_stat_info *st, union block *blk,
1130 off_t block_ordinal)
1132 char modes[12];
1133 char const *time_stamp;
1134 int time_stamp_len;
1135 char *temp_name;
1137 /* These hold formatted ints. */
1138 char uform[max (INT_BUFSIZE_BOUND (intmax_t), UINTMAX_STRSIZE_BOUND)];
1139 char gform[sizeof uform];
1140 char *user, *group;
1141 char size[2 * UINTMAX_STRSIZE_BOUND];
1142 /* holds formatted size or major,minor */
1143 char uintbuf[UINTMAX_STRSIZE_BOUND];
1144 int pad;
1145 int sizelen;
1147 if (show_transformed_names_option)
1148 temp_name = st->file_name ? st->file_name : st->orig_file_name;
1149 else
1150 temp_name = st->orig_file_name ? st->orig_file_name : st->file_name;
1152 if (block_number_option)
1154 char buf[UINTMAX_STRSIZE_BOUND];
1155 if (block_ordinal < 0)
1156 block_ordinal = current_block_ordinal ();
1157 block_ordinal -= recent_long_name_blocks;
1158 block_ordinal -= recent_long_link_blocks;
1159 fprintf (stdlis, _("block %s: "),
1160 STRINGIFY_BIGINT (block_ordinal, buf));
1163 if (verbose_option <= 1)
1165 /* Just the fax, mam. */
1166 fputs (quotearg (temp_name), stdlis);
1167 if (show_transformed_names_option && st->had_trailing_slash)
1168 fputc ('/', stdlis);
1169 fputc ('\n', stdlis);
1171 else
1173 /* File type and modes. */
1175 modes[0] = '?';
1176 switch (blk->header.typeflag)
1178 case GNUTYPE_VOLHDR:
1179 volume_label_printed = true;
1180 modes[0] = 'V';
1181 break;
1183 case GNUTYPE_MULTIVOL:
1184 modes[0] = 'M';
1185 break;
1187 case GNUTYPE_LONGNAME:
1188 case GNUTYPE_LONGLINK:
1189 modes[0] = 'L';
1190 ERROR ((0, 0, _("Unexpected long name header")));
1191 break;
1193 case GNUTYPE_SPARSE:
1194 case REGTYPE:
1195 case AREGTYPE:
1196 modes[0] = st->had_trailing_slash ? 'd' : '-';
1197 break;
1198 case LNKTYPE:
1199 modes[0] = 'h';
1200 break;
1201 case GNUTYPE_DUMPDIR:
1202 modes[0] = 'd';
1203 break;
1204 case DIRTYPE:
1205 modes[0] = 'd';
1206 break;
1207 case SYMTYPE:
1208 modes[0] = 'l';
1209 break;
1210 case BLKTYPE:
1211 modes[0] = 'b';
1212 break;
1213 case CHRTYPE:
1214 modes[0] = 'c';
1215 break;
1216 case FIFOTYPE:
1217 modes[0] = 'p';
1218 break;
1219 case CONTTYPE:
1220 modes[0] = 'C';
1221 break;
1224 pax_decode_mode (st->stat.st_mode, modes + 1);
1226 /* extended attributes: GNU `ls -l'-like preview */
1227 xattrs_print_char (st, modes + 10);
1229 /* Time stamp. */
1231 time_stamp = tartime (st->mtime, full_time_option);
1232 time_stamp_len = strlen (time_stamp);
1233 if (datewidth < time_stamp_len)
1234 datewidth = time_stamp_len;
1236 /* User and group names. */
1238 if (st->uname
1239 && st->uname[0]
1240 && current_format != V7_FORMAT
1241 && !numeric_owner_option)
1242 user = st->uname;
1243 else
1244 user = STRINGIFY_BIGINT (st->stat.st_uid, uform);
1246 if (st->gname
1247 && st->gname[0]
1248 && current_format != V7_FORMAT
1249 && !numeric_owner_option)
1250 group = st->gname;
1251 else
1252 group = STRINGIFY_BIGINT (st->stat.st_gid, gform);
1254 /* Format the file size or major/minor device numbers. */
1256 switch (blk->header.typeflag)
1258 case CHRTYPE:
1259 case BLKTYPE:
1260 strcpy (size,
1261 STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf));
1262 strcat (size, ",");
1263 strcat (size,
1264 STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf));
1265 break;
1267 default:
1268 /* st->stat.st_size keeps stored file size */
1269 strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf));
1270 break;
1273 /* Figure out padding and print the whole line. */
1275 sizelen = strlen (size);
1276 pad = strlen (user) + 1 + strlen (group) + 1 + sizelen;
1277 if (pad > ugswidth)
1278 ugswidth = pad;
1280 fprintf (stdlis, "%s %s/%s %*s %-*s",
1281 modes, user, group, ugswidth - pad + sizelen, size,
1282 datewidth, time_stamp);
1284 fprintf (stdlis, " %s", quotearg (temp_name));
1285 if (show_transformed_names_option && st->had_trailing_slash)
1286 fputc ('/', stdlis);
1288 switch (blk->header.typeflag)
1290 case SYMTYPE:
1291 fprintf (stdlis, " -> %s\n", quotearg (st->link_name));
1292 break;
1294 case LNKTYPE:
1295 fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name));
1296 break;
1298 default:
1300 char type_string[2];
1301 type_string[0] = blk->header.typeflag;
1302 type_string[1] = '\0';
1303 fprintf (stdlis, _(" unknown file type %s\n"),
1304 quote (type_string));
1306 break;
1308 case AREGTYPE:
1309 case REGTYPE:
1310 case GNUTYPE_SPARSE:
1311 case CHRTYPE:
1312 case BLKTYPE:
1313 case DIRTYPE:
1314 case FIFOTYPE:
1315 case CONTTYPE:
1316 case GNUTYPE_DUMPDIR:
1317 putc ('\n', stdlis);
1318 break;
1320 case GNUTYPE_LONGLINK:
1321 fprintf (stdlis, _("--Long Link--\n"));
1322 break;
1324 case GNUTYPE_LONGNAME:
1325 fprintf (stdlis, _("--Long Name--\n"));
1326 break;
1328 case GNUTYPE_VOLHDR:
1329 fprintf (stdlis, _("--Volume Header--\n"));
1330 break;
1332 case GNUTYPE_MULTIVOL:
1333 strcpy (size,
1334 STRINGIFY_BIGINT
1335 (UINTMAX_FROM_HEADER (blk->oldgnu_header.offset),
1336 uintbuf));
1337 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1338 break;
1341 fflush (stdlis);
1342 xattrs_print (st);
1346 static void
1347 print_volume_label (void)
1349 struct tar_stat_info vstat;
1350 union block vblk;
1351 enum archive_format dummy;
1353 memset (&vblk, 0, sizeof (vblk));
1354 vblk.header.typeflag = GNUTYPE_VOLHDR;
1355 if (recent_global_header)
1356 memcpy (vblk.header.mtime, recent_global_header->header.mtime,
1357 sizeof vblk.header.mtime);
1358 tar_stat_init (&vstat);
1359 assign_string (&vstat.file_name, ".");
1360 decode_header (&vblk, &vstat, &dummy, 0);
1361 assign_string (&vstat.file_name, volume_label);
1362 simple_print_header (&vstat, &vblk, 0);
1363 tar_stat_destroy (&vstat);
1366 void
1367 print_header (struct tar_stat_info *st, union block *blk,
1368 off_t block_ordinal)
1370 if (current_format == POSIX_FORMAT && !volume_label_printed && volume_label)
1372 print_volume_label ();
1373 volume_label_printed = true;
1376 simple_print_header (st, blk, block_ordinal);
1379 /* Print a similar line when we make a directory automatically. */
1380 void
1381 print_for_mkdir (char *dirname, mode_t mode)
1383 char modes[11];
1385 if (verbose_option > 1)
1387 /* File type and modes. */
1389 modes[0] = 'd';
1390 pax_decode_mode (mode, modes + 1);
1392 if (block_number_option)
1394 char buf[UINTMAX_STRSIZE_BOUND];
1395 fprintf (stdlis, _("block %s: "),
1396 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1399 fprintf (stdlis, "%s %*s %s\n", modes, ugswidth + 1 + datewidth,
1400 _("Creating directory:"), quotearg (dirname));
1404 /* Skip over SIZE bytes of data in blocks in the archive.
1405 This may involve copying the data.
1406 If MUST_COPY, always copy instead of skipping. */
1407 void
1408 skim_file (off_t size, bool must_copy)
1410 union block *x;
1412 /* FIXME: Make sure mv_begin_read is always called before it */
1414 if (seekable_archive && !must_copy)
1416 off_t nblk = seek_archive (size);
1417 if (nblk >= 0)
1418 size -= nblk * BLOCKSIZE;
1419 else
1420 seekable_archive = false;
1423 mv_size_left (size);
1425 while (size > 0)
1427 x = find_next_block ();
1428 if (! x)
1429 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1431 set_next_block_after (x);
1432 size -= BLOCKSIZE;
1433 mv_size_left (size);
1437 /* Skip the current member in the archive.
1438 NOTE: Current header must be decoded before calling this function. */
1439 void
1440 skip_member (void)
1442 skim_member (false);
1445 /* Skip the current member in the archive.
1446 If MUST_COPY, always copy instead of skipping. */
1447 void
1448 skim_member (bool must_copy)
1450 if (!current_stat_info.skipped)
1452 char save_typeflag = current_header->header.typeflag;
1453 set_next_block_after (current_header);
1455 mv_begin_read (&current_stat_info);
1457 if (current_stat_info.is_sparse)
1458 sparse_skim_file (&current_stat_info, must_copy);
1459 else if (save_typeflag != DIRTYPE)
1460 skim_file (current_stat_info.stat.st_size, must_copy);
1462 mv_end ();
1466 void
1467 test_archive_label (void)
1469 base64_init ();
1470 name_gather ();
1472 open_archive (ACCESS_READ);
1473 if (read_header (&current_header, &current_stat_info, read_header_auto)
1474 == HEADER_SUCCESS)
1476 decode_header (current_header,
1477 &current_stat_info, &current_format, 0);
1478 if (current_header->header.typeflag == GNUTYPE_VOLHDR)
1479 ASSIGN_STRING_N (&volume_label, current_header->header.name);
1481 if (volume_label)
1483 if (verbose_option)
1484 print_volume_label ();
1485 if (!name_match (volume_label) && multi_volume_option)
1487 char *s = drop_volume_label_suffix (volume_label);
1488 name_match (s);
1489 free (s);
1493 close_archive ();
1494 label_notfound ();