1 /* List a tar archive, with support routines for reading a tar archive.
2 
3    Copyright 1988-2021 Free Software Foundation, Inc.
4 
5    This file is part of GNU tar.
6 
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.
11 
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.
16 
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/>.
19 
20    Written by John Gilmore, on 1985-08-26.  */
21 
22 #include <system.h>
23 #include <inttostr.h>
24 #include <quotearg.h>
25 #include <time.h>
26 #include "common.h"
27 
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 */
35 
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))
43 
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);
52 
53 /* Base 64 digits; see Internet RFC 2045 Table 1.  */
54 static char const base_64_digits[64] =
55 {
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', '+', '/'
61 };
62 
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];
66 
67 static void
base64_init(void)68 base64_init (void)
69 {
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;
74 }
75 
76 static char *
decode_xform(char * file_name,void * data)77 decode_xform (char *file_name, void *data)
78 {
79   int type = *(int*)data;
80 
81   switch (type)
82     {
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;
90 
91     case XFORM_LINK:
92       file_name = safer_name_suffix (file_name, true, absolute_names_option);
93       break;
94 
95     case XFORM_REGFILE:
96       file_name = safer_name_suffix (file_name, false, absolute_names_option);
97       break;
98     }
99 
100   if (strip_name_components)
101     {
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;
107     }
108   return file_name;
109 }
110 
111 static bool
transform_member_name(char ** pinput,int type)112 transform_member_name (char **pinput, int type)
113 {
114   return transform_name_fp (pinput, type, decode_xform, &type);
115 }
116 
117 static void
enforce_one_top_level(char ** pfile_name)118 enforce_one_top_level (char **pfile_name)
119 {
120   char *file_name = *pfile_name;
121   char *p;
122 
123   for (p = file_name; *p && (ISSLASH (*p) || *p == '.'); p++)
124     ;
125 
126   if (*p)
127     {
128       int pos = strlen (one_top_level_dir);
129       if (strncmp (p, one_top_level_dir, pos) == 0)
130 	{
131 	  if (ISSLASH (p[pos]) || p[pos] == 0)
132 	    return;
133 	}
134 
135       *pfile_name = make_file_name (one_top_level_dir, file_name);
136       normalize_filename_x (*pfile_name);
137     }
138   else
139     *pfile_name = xstrdup (one_top_level_dir);
140   free (file_name);
141 }
142 
143 void
transform_stat_info(int typeflag,struct tar_stat_info * stat_info)144 transform_stat_info (int typeflag, struct tar_stat_info *stat_info)
145 {
146   if (typeflag == GNUTYPE_VOLHDR)
147     /* Name transformations don't apply to volume headers. */
148     return;
149 
150   transform_member_name (&stat_info->file_name, XFORM_REGFILE);
151   switch (typeflag)
152     {
153     case SYMTYPE:
154       transform_member_name (&stat_info->link_name, XFORM_SYMLINK);
155       break;
156 
157     case LNKTYPE:
158       transform_member_name (&stat_info->link_name, XFORM_LINK);
159     }
160 
161   if (one_top_level_option)
162     enforce_one_top_level (&current_stat_info.file_name);
163 }
164 
165 /* Main loop for reading an archive.  */
166 void
read_and(void (* do_something)(void))167 read_and (void (*do_something) (void))
168 {
169   enum read_header status = HEADER_STILL_UNREAD;
170   enum read_header prev_status;
171   struct timespec mtime;
172 
173   base64_init ();
174   name_gather ();
175 
176   open_archive (ACCESS_READ);
177   do
178     {
179       prev_status = status;
180       tar_stat_destroy (&current_stat_info);
181 
182       status = read_header (&current_header, &current_stat_info,
183                             read_header_auto);
184       switch (status)
185 	{
186 	case HEADER_STILL_UNREAD:
187 	case HEADER_SUCCESS_EXTENDED:
188 	  abort ();
189 
190 	case HEADER_SUCCESS:
191 
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))
209 	    {
210 	      switch (current_header->header.typeflag)
211 		{
212 		case GNUTYPE_VOLHDR:
213 		case GNUTYPE_MULTIVOL:
214 		  break;
215 
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;
224 		}
225 	    }
226 
227 	  transform_stat_info (current_header->header.typeflag,
228 			       &current_stat_info);
229 	  (*do_something) ();
230 	  continue;
231 
232 	case HEADER_ZERO_BLOCK:
233 	  if (block_number_option)
234 	    {
235 	      char buf[UINTMAX_STRSIZE_BOUND];
236 	      fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
237 		       STRINGIFY_BIGINT (current_block_ordinal (), buf));
238 	    }
239 
240 	  set_next_block_after (current_header);
241 
242 	  if (!ignore_zeros_option)
243 	    {
244 	      char buf[UINTMAX_STRSIZE_BOUND];
245 
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;
254 	    }
255 	  status = prev_status;
256 	  continue;
257 
258 	case HEADER_END_OF_FILE:
259 	  if (block_number_option)
260 	    {
261 	      char buf[UINTMAX_STRSIZE_BOUND];
262 	      fprintf (stdlis, _("block %s: ** End of File **\n"),
263 		       STRINGIFY_BIGINT (current_block_ordinal (), buf));
264 	    }
265 	  break;
266 
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)
272 	    {
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)
279 		{
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));
286 		}
287 	      ERROR ((0, 0, _("Skipping to next header")));
288 	      break;
289 
290 	    case HEADER_END_OF_FILE:
291 	    case HEADER_FAILURE:
292 	      /* We are in the middle of a cascade of errors.  */
293 	      break;
294 
295 	    case HEADER_SUCCESS_EXTENDED:
296 	      abort ();
297 	    }
298 	  continue;
299 	}
300       break;
301     }
302   while (!all_names_found (&current_stat_info));
303 
304   close_archive ();
305   names_notfound ();		/* print names not found */
306 }
307 
308 /* Print a header block, based on tar options.  */
309 void
list_archive(void)310 list_archive (void)
311 {
312   off_t block_ordinal = current_block_ordinal ();
313 
314   /* Print the header block.  */
315   if (verbose_option)
316     print_header (&current_stat_info, current_header, block_ordinal);
317 
318   if (incremental_option)
319     {
320       if (verbose_option > 2)
321 	{
322 	  if (is_dumpdir (&current_stat_info))
323 	    list_dumpdir (current_stat_info.dumpdir,
324 			  dumpdir_size (current_stat_info.dumpdir));
325 	}
326     }
327 
328   skip_member ();
329 }
330 
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.  */
340 
341 enum read_header
tar_checksum(union block * header,bool silent)342 tar_checksum (union block *header, bool silent)
343 {
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;
350 
351   p = header->buffer;
352   for (i = sizeof *header; i-- != 0;)
353     {
354       unsigned_sum += (unsigned char) *p;
355       signed_sum += (signed char) (*p++);
356     }
357 
358   if (unsigned_sum == 0)
359     return HEADER_ZERO_BLOCK;
360 
361   /* Adjust checksum to count the "chksum" field as blanks.  */
362 
363   for (i = sizeof header->header.chksum; i-- != 0;)
364     {
365       unsigned_sum -= (unsigned char) header->header.chksum[i];
366       signed_sum -= (signed char) (header->header.chksum[i]);
367     }
368   unsigned_sum += ' ' * sizeof header->header.chksum;
369   signed_sum += ' ' * sizeof header->header.chksum;
370 
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;
376 
377   recorded_sum = parsed_sum;
378 
379   if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
380     return HEADER_FAILURE;
381 
382   return HEADER_SUCCESS;
383 }
384 
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.
388 
389    Return one of enum read_header describing the status of the
390    operation.
391 
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.:
395 
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.
402 
403    You must always set_next_block_after(*return_block) to skip past
404    the header which this routine reads.  */
405 
406 enum read_header
read_header(union block ** return_block,struct tar_stat_info * info,enum read_header_mode mode)407 read_header (union block **return_block, struct tar_stat_info *info,
408 	     enum read_header_mode mode)
409 {
410   union block *header;
411   char *bp;
412   union block *data_block;
413   size_t size, written;
414   union block *next_long_name = NULL;
415   union block *next_long_link = NULL;
416   size_t next_long_name_blocks = 0;
417   size_t next_long_link_blocks = 0;
418   enum read_header status = HEADER_SUCCESS;
419 
420   while (1)
421     {
422       header = find_next_block ();
423       *return_block = header;
424       if (!header)
425 	{
426 	  status = HEADER_END_OF_FILE;
427 	  break;
428 	}
429 
430       if ((status = tar_checksum (header, false)) != HEADER_SUCCESS)
431 	break;
432 
433       /* Good block.  Decode file size and return.  */
434 
435       if (header->header.typeflag == LNKTYPE)
436 	info->stat.st_size = 0;	/* links 0 size on tape */
437       else
438 	{
439 	  info->stat.st_size = OFF_FROM_HEADER (header->header.size);
440 	  if (info->stat.st_size < 0)
441 	    {
442 	      status = HEADER_FAILURE;
443 	      break;
444 	    }
445 	}
446 
447       if (header->header.typeflag == GNUTYPE_LONGNAME
448 	  || header->header.typeflag == GNUTYPE_LONGLINK
449 	  || header->header.typeflag == XHDTYPE
450 	  || header->header.typeflag == XGLTYPE
451 	  || header->header.typeflag == SOLARIS_XHDTYPE)
452 	{
453 	  if (mode == read_header_x_raw)
454 	    {
455 	      status = HEADER_SUCCESS_EXTENDED;
456 	      break;
457 	    }
458 	  else if (header->header.typeflag == GNUTYPE_LONGNAME
459 		   || header->header.typeflag == GNUTYPE_LONGLINK)
460 	    {
461 	      union block *header_copy;
462 	      size_t name_size = info->stat.st_size;
463 	      size_t n = name_size % BLOCKSIZE;
464 	      size = name_size + BLOCKSIZE;
465 	      if (n)
466 		size += BLOCKSIZE - n;
467 
468 	      if (name_size != info->stat.st_size || size < name_size)
469 		xalloc_die ();
470 
471 	      header_copy = xmalloc (size + 1);
472 
473 	      if (header->header.typeflag == GNUTYPE_LONGNAME)
474 		{
475 		  free (next_long_name);
476 		  next_long_name = header_copy;
477 		  next_long_name_blocks = size / BLOCKSIZE;
478 		}
479 	      else
480 		{
481 		  free (next_long_link);
482 		  next_long_link = header_copy;
483 		  next_long_link_blocks = size / BLOCKSIZE;
484 		}
485 
486 	      set_next_block_after (header);
487 	      *header_copy = *header;
488 	      bp = header_copy->buffer + BLOCKSIZE;
489 
490 	      for (size -= BLOCKSIZE; size > 0; size -= written)
491 		{
492 		  data_block = find_next_block ();
493 		  if (! data_block)
494 		    {
495 		      ERROR ((0, 0, _("Unexpected EOF in archive")));
496 		      break;
497 		    }
498 		  written = available_space_after (data_block);
499 		  if (written > size)
500 		    written = size;
501 
502 		  memcpy (bp, data_block->buffer, written);
503 		  bp += written;
504 		  set_next_block_after ((union block *)
505 					(data_block->buffer + written - 1));
506 		}
507 
508 	      *bp = '\0';
509 	    }
510 	  else if (header->header.typeflag == XHDTYPE
511 		   || header->header.typeflag == SOLARIS_XHDTYPE)
512 	    xheader_read (&info->xhdr, header,
513 			  OFF_FROM_HEADER (header->header.size));
514 	  else if (header->header.typeflag == XGLTYPE)
515 	    {
516 	      struct xheader xhdr;
517 
518 	      if (!recent_global_header)
519 		recent_global_header = xmalloc (sizeof *recent_global_header);
520 	      memcpy (recent_global_header, header,
521 		      sizeof *recent_global_header);
522 	      memset (&xhdr, 0, sizeof xhdr);
523 	      xheader_read (&xhdr, header,
524 			    OFF_FROM_HEADER (header->header.size));
525 	      xheader_decode_global (&xhdr);
526 	      xheader_destroy (&xhdr);
527 	      if (mode == read_header_x_global)
528 		{
529 		  status = HEADER_SUCCESS_EXTENDED;
530 		  break;
531 		}
532 	    }
533 
534 	  /* Loop!  */
535 
536 	}
537       else
538 	{
539 	  char const *name;
540 	  struct posix_header const *h = &header->header;
541 	  char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
542 
543 	  free (recent_long_name);
544 
545 	  if (next_long_name)
546 	    {
547 	      name = next_long_name->buffer + BLOCKSIZE;
548 	      recent_long_name = next_long_name;
549 	      recent_long_name_blocks = next_long_name_blocks;
550 	      next_long_name = NULL;
551 	    }
552 	  else
553 	    {
554 	      /* Accept file names as specified by POSIX.1-1996
555                  section 10.1.1.  */
556 	      char *np = namebuf;
557 
558 	      if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
559 		{
560 		  memcpy (np, h->prefix, sizeof h->prefix);
561 		  np[sizeof h->prefix] = '\0';
562 		  np += strlen (np);
563 		  *np++ = '/';
564 		}
565 	      memcpy (np, h->name, sizeof h->name);
566 	      np[sizeof h->name] = '\0';
567 	      name = namebuf;
568 	      recent_long_name = 0;
569 	      recent_long_name_blocks = 0;
570 	    }
571 	  assign_string (&info->orig_file_name, name);
572 	  assign_string (&info->file_name, name);
573 	  info->had_trailing_slash = strip_trailing_slashes (info->file_name);
574 
575 	  free (recent_long_link);
576 
577 	  if (next_long_link)
578 	    {
579 	      name = next_long_link->buffer + BLOCKSIZE;
580 	      recent_long_link = next_long_link;
581 	      recent_long_link_blocks = next_long_link_blocks;
582 	      next_long_link = NULL;
583 	    }
584 	  else
585 	    {
586 	      memcpy (namebuf, h->linkname, sizeof h->linkname);
587 	      namebuf[sizeof h->linkname] = '\0';
588 	      name = namebuf;
589 	      recent_long_link = 0;
590 	      recent_long_link_blocks = 0;
591 	    }
592 	  assign_string (&info->link_name, name);
593 
594 	  break;
595 	}
596     }
597   free (next_long_name);
598   free (next_long_link);
599   return status;
600 }
601 
602 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
603 
604 /* Decode things from a file HEADER block into STAT_INFO, also setting
605    *FORMAT_POINTER depending on the header block format.  If
606    DO_USER_GROUP, decode the user/group information (this is useful
607    for extraction, but waste time when merely listing).
608 
609    read_header() has already decoded the checksum and length, so we don't.
610 
611    This routine should *not* be called twice for the same block, since
612    the two calls might use different DO_USER_GROUP values and thus
613    might end up with different uid/gid for the two calls.  If anybody
614    wants the uid/gid they should decode it first, and other callers
615    should decode it without uid/gid before calling a routine,
616    e.g. print_header, that assumes decoded data.  */
617 void
decode_header(union block * header,struct tar_stat_info * stat_info,enum archive_format * format_pointer,int do_user_group)618 decode_header (union block *header, struct tar_stat_info *stat_info,
619 	       enum archive_format *format_pointer, int do_user_group)
620 {
621   enum archive_format format;
622   bool hbits;
623   mode_t mode = MODE_FROM_HEADER (header->header.mode, &hbits);
624 
625   if (strcmp (header->header.magic, TMAGIC) == 0)
626     {
627       if (header->star_header.prefix[130] == 0
628 	  && ISOCTAL (header->star_header.atime[0])
629 	  && header->star_header.atime[11] == ' '
630 	  && ISOCTAL (header->star_header.ctime[0])
631 	  && header->star_header.ctime[11] == ' ')
632 	format = STAR_FORMAT;
633       else if (stat_info->xhdr.size)
634 	format = POSIX_FORMAT;
635       else
636 	format = USTAR_FORMAT;
637     }
638   else if (strcmp (header->buffer + offsetof (struct posix_header, magic),
639 		   OLDGNU_MAGIC)
640 	   == 0)
641     format = hbits ? OLDGNU_FORMAT : GNU_FORMAT;
642   else
643     format = V7_FORMAT;
644   *format_pointer = format;
645 
646   stat_info->stat.st_mode = mode;
647   stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime);
648   stat_info->mtime.tv_nsec = 0;
649   assign_string_n (&stat_info->uname,
650 		   header->header.uname[0] ? header->header.uname : NULL,
651 		   sizeof (header->header.uname));
652   assign_string_n (&stat_info->gname,
653 		   header->header.gname[0] ? header->header.gname : NULL,
654 		   sizeof (header->header.gname));
655 
656   xheader_xattr_init (stat_info);
657 
658   if (format == OLDGNU_FORMAT && incremental_option)
659     {
660       stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime);
661       stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime);
662       stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
663     }
664   else if (format == STAR_FORMAT)
665     {
666       stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime);
667       stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime);
668       stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0;
669     }
670   else
671     stat_info->atime = stat_info->ctime = start_time;
672 
673   if (format == V7_FORMAT)
674     {
675       stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
676       stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
677       stat_info->stat.st_rdev = 0;
678     }
679   else
680     {
681       if (do_user_group)
682 	{
683 	  /* FIXME: Decide if this should somewhat depend on -p.  */
684 
685 	  if (numeric_owner_option
686 	      || !*header->header.uname
687 	      || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid))
688 	    stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
689 
690 	  if (numeric_owner_option
691 	      || !*header->header.gname
692 	      || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid))
693 	    stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
694 	}
695 
696       switch (header->header.typeflag)
697 	{
698 	case BLKTYPE:
699 	case CHRTYPE:
700 	  stat_info->stat.st_rdev =
701 	    makedev (MAJOR_FROM_HEADER (header->header.devmajor),
702 		     MINOR_FROM_HEADER (header->header.devminor));
703 	  break;
704 
705 	default:
706 	  stat_info->stat.st_rdev = 0;
707 	}
708     }
709 
710   xheader_decode (stat_info);
711 
712   if (sparse_member_p (stat_info))
713     {
714       sparse_fixup_header (stat_info);
715       stat_info->is_sparse = true;
716     }
717   else
718     {
719       stat_info->is_sparse = false;
720       if (((current_format == GNU_FORMAT
721 	    || current_format == OLDGNU_FORMAT)
722 	   && current_header->header.typeflag == GNUTYPE_DUMPDIR)
723           || stat_info->dumpdir)
724 	stat_info->is_dumpdir = true;
725     }
726 }
727 
728 
729 /* Convert buffer at WHERE0 of size DIGS from external format to
730    intmax_t.  DIGS must be positive.  If TYPE is nonnull, the data are
731    of type TYPE.  The buffer must represent a value in the range
732    MINVAL through MAXVAL; if the mathematically correct result V would
733    be greater than INTMAX_MAX, return a negative integer V such that
734    (uintmax_t) V yields the correct result.  If OCTAL_ONLY, allow only octal
735    numbers instead of the other GNU extensions.  Return -1 on error,
736    diagnosing the error if TYPE is nonnull and if !SILENT.  */
737 #if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX)
738 # error "from_header internally represents intmax_t as uintmax_t + sign"
739 #endif
740 #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
741 # error "from_header returns intmax_t to represent uintmax_t"
742 #endif
743 static intmax_t
from_header(char const * where0,size_t digs,char const * type,intmax_t minval,uintmax_t maxval,bool octal_only,bool silent)744 from_header (char const *where0, size_t digs, char const *type,
745 	     intmax_t minval, uintmax_t maxval,
746 	     bool octal_only, bool silent)
747 {
748   uintmax_t value;
749   uintmax_t uminval = minval;
750   uintmax_t minus_minval = - uminval;
751   char const *where = where0;
752   char const *lim = where + digs;
753   bool negative = false;
754 
755   /* Accommodate buggy tar of unknown vintage, which outputs leading
756      NUL if the previous field overflows.  */
757   where += !*where;
758 
759   /* Accommodate older tars, which output leading spaces.  */
760   for (;;)
761     {
762       if (where == lim)
763 	{
764 	  if (type && !silent)
765 	    ERROR ((0, 0,
766 		    /* TRANSLATORS: %s is type of the value (gid_t, uid_t,
767 		       etc.) */
768 		    _("Blanks in header where numeric %s value expected"),
769 		    type));
770 	  return -1;
771 	}
772       if (!isspace ((unsigned char) *where))
773 	break;
774       where++;
775     }
776 
777   value = 0;
778   if (ISODIGIT (*where))
779     {
780       char const *where1 = where;
781       bool overflow = false;
782 
783       for (;;)
784 	{
785 	  value += *where++ - '0';
786 	  if (where == lim || ! ISODIGIT (*where))
787 	    break;
788 	  overflow |= value != (value << LG_8 >> LG_8);
789 	  value <<= LG_8;
790 	}
791 
792       /* Parse the output of older, unportable tars, which generate
793          negative values in two's complement octal.  If the leading
794          nonzero digit is 1, we can't recover the original value
795          reliably; so do this only if the digit is 2 or more.  This
796          catches the common case of 32-bit negative time stamps.  */
797       if ((overflow || maxval < value) && '2' <= *where1 && type)
798 	{
799 	  /* Compute the negative of the input value, assuming two's
800 	     complement.  */
801 	  int digit = (*where1 - '0') | 4;
802 	  overflow = 0;
803 	  value = 0;
804 	  where = where1;
805 	  for (;;)
806 	    {
807 	      value += 7 - digit;
808 	      where++;
809 	      if (where == lim || ! ISODIGIT (*where))
810 		break;
811 	      digit = *where - '0';
812 	      overflow |= value != (value << LG_8 >> LG_8);
813 	      value <<= LG_8;
814 	    }
815 	  value++;
816 	  overflow |= !value;
817 
818 	  if (!overflow && value <= minus_minval)
819 	    {
820 	      if (!silent)
821 		WARN ((0, 0,
822 		       /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
823 		       _("Archive octal value %.*s is out of %s range; assuming two's complement"),
824 		       (int) (where - where1), where1, type));
825 	      negative = true;
826 	    }
827 	}
828 
829       if (overflow)
830 	{
831 	  if (type && !silent)
832 	    ERROR ((0, 0,
833 		    /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
834 		    _("Archive octal value %.*s is out of %s range"),
835 		    (int) (where - where1), where1, type));
836 	  return -1;
837 	}
838     }
839   else if (octal_only)
840     {
841       /* Suppress the following extensions.  */
842     }
843   else if (*where == '-' || *where == '+')
844     {
845       /* Parse base-64 output produced only by tar test versions
846 	 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
847 	 Support for this will be withdrawn in future releases.  */
848       int dig;
849       if (!silent)
850 	{
851 	  static bool warned_once;
852 	  if (! warned_once)
853 	    {
854 	      warned_once = true;
855 	      WARN ((0, 0, _("Archive contains obsolescent base-64 headers")));
856 	    }
857 	}
858       negative = *where++ == '-';
859       while (where != lim
860 	     && (dig = base64_map[(unsigned char) *where]) < 64)
861 	{
862 	  if (value << LG_64 >> LG_64 != value)
863 	    {
864 	      char *string = alloca (digs + 1);
865 	      memcpy (string, where0, digs);
866 	      string[digs] = '\0';
867 	      if (type && !silent)
868 		ERROR ((0, 0,
869 			_("Archive signed base-64 string %s is out of %s range"),
870 			quote (string), type));
871 	      return -1;
872 	    }
873 	  value = (value << LG_64) | dig;
874 	  where++;
875 	}
876     }
877   else if (*where == '\200' /* positive base-256 */
878 	   || *where == '\377' /* negative base-256 */)
879     {
880       /* Parse base-256 output.  A nonnegative number N is
881 	 represented as (256**DIGS)/2 + N; a negative number -N is
882 	 represented as (256**DIGS) - N, i.e. as two's complement.
883 	 The representation guarantees that the leading bit is
884 	 always on, so that we don't confuse this format with the
885 	 others (assuming ASCII bytes of 8 bits or more).  */
886       int signbit = *where & (1 << (LG_256 - 2));
887       uintmax_t topbits = (((uintmax_t) - signbit)
888 			   << (CHAR_BIT * sizeof (uintmax_t)
889 			       - LG_256 - (LG_256 - 2)));
890       value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
891       for (;;)
892 	{
893 	  value = (value << LG_256) + (unsigned char) *where++;
894 	  if (where == lim)
895 	    break;
896 	  if (((value << LG_256 >> LG_256) | topbits) != value)
897 	    {
898 	      if (type && !silent)
899 		ERROR ((0, 0,
900 			_("Archive base-256 value is out of %s range"),
901 			type));
902 	      return -1;
903 	    }
904 	}
905       negative = signbit != 0;
906       if (negative)
907 	value = -value;
908     }
909 
910   if (where != lim && *where && !isspace ((unsigned char) *where))
911     {
912       if (type)
913 	{
914 	  char buf[1000]; /* Big enough to represent any header.  */
915 	  static struct quoting_options *o;
916 
917 	  if (!o)
918 	    {
919 	      o = clone_quoting_options (0);
920 	      set_quoting_style (o, locale_quoting_style);
921 	    }
922 
923 	  while (where0 != lim && ! lim[-1])
924 	    lim--;
925 	  quotearg_buffer (buf, sizeof buf, where0, lim - where0, o);
926 	  if (!silent)
927 	    ERROR ((0, 0,
928 		    /* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */
929 		    _("Archive contains %.*s where numeric %s value expected"),
930 		    (int) sizeof buf, buf, type));
931 	}
932 
933       return -1;
934     }
935 
936   if (value <= (negative ? minus_minval : maxval))
937     return represent_uintmax (negative ? -value : value);
938 
939   if (type && !silent)
940     {
941       char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
942       char maxval_buf[UINTMAX_STRSIZE_BOUND];
943       char value_buf[UINTMAX_STRSIZE_BOUND + 1];
944       char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
945       char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
946       if (negative)
947 	*--value_string = '-';
948       if (minus_minval)
949 	*--minval_string = '-';
950       /* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */
951       ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
952 	      value_string, type,
953 	      minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
954     }
955 
956   return -1;
957 }
958 
959 static gid_t
gid_from_header(const char * p,size_t s)960 gid_from_header (const char *p, size_t s)
961 {
962   return from_header (p, s, "gid_t",
963 		      TYPE_MINIMUM (gid_t), TYPE_MAXIMUM (gid_t),
964 		      false, false);
965 }
966 
967 static major_t
major_from_header(const char * p,size_t s)968 major_from_header (const char *p, size_t s)
969 {
970   return from_header (p, s, "major_t",
971 		      TYPE_MINIMUM (major_t), TYPE_MAXIMUM (major_t),
972 		      false, false);
973 }
974 
975 static minor_t
minor_from_header(const char * p,size_t s)976 minor_from_header (const char *p, size_t s)
977 {
978   return from_header (p, s, "minor_t",
979 		      TYPE_MINIMUM (minor_t), TYPE_MAXIMUM (minor_t),
980 		      false, false);
981 }
982 
983 /* Convert P to the file mode, as understood by tar.
984    Set *HBITS if there are any unrecognized bits.  */
985 static mode_t
mode_from_header(const char * p,size_t s,bool * hbits)986 mode_from_header (const char *p, size_t s, bool *hbits)
987 {
988   intmax_t u = from_header (p, s, "mode_t",
989 			    INTMAX_MIN, UINTMAX_MAX,
990 			    false, false);
991   mode_t mode = ((u & TSUID ? S_ISUID : 0)
992 		 | (u & TSGID ? S_ISGID : 0)
993 		 | (u & TSVTX ? S_ISVTX : 0)
994 		 | (u & TUREAD ? S_IRUSR : 0)
995 		 | (u & TUWRITE ? S_IWUSR : 0)
996 		 | (u & TUEXEC ? S_IXUSR : 0)
997 		 | (u & TGREAD ? S_IRGRP : 0)
998 		 | (u & TGWRITE ? S_IWGRP : 0)
999 		 | (u & TGEXEC ? S_IXGRP : 0)
1000 		 | (u & TOREAD ? S_IROTH : 0)
1001 		 | (u & TOWRITE ? S_IWOTH : 0)
1002 		 | (u & TOEXEC ? S_IXOTH : 0));
1003   *hbits = (u & ~07777) != 0;
1004   return mode;
1005 }
1006 
1007 off_t
off_from_header(const char * p,size_t s)1008 off_from_header (const char *p, size_t s)
1009 {
1010   /* Negative offsets are not allowed in tar files, so invoke
1011      from_header with minimum value 0, not TYPE_MINIMUM (off_t).  */
1012   return from_header (p, s, "off_t",
1013 		      0, TYPE_MAXIMUM (off_t),
1014 		      false, false);
1015 }
1016 
1017 static time_t
time_from_header(const char * p,size_t s)1018 time_from_header (const char *p, size_t s)
1019 {
1020   return from_header (p, s, "time_t",
1021 		      TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t),
1022 		      false, false);
1023 }
1024 
1025 static uid_t
uid_from_header(const char * p,size_t s)1026 uid_from_header (const char *p, size_t s)
1027 {
1028   return from_header (p, s, "uid_t",
1029 		      TYPE_MINIMUM (uid_t), TYPE_MAXIMUM (uid_t),
1030 		      false, false);
1031 }
1032 
1033 uintmax_t
uintmax_from_header(const char * p,size_t s)1034 uintmax_from_header (const char *p, size_t s)
1035 {
1036   return from_header (p, s, "uintmax_t", 0, UINTMAX_MAX, false, false);
1037 }
1038 
1039 
1040 /* Return a printable representation of T.  The result points to
1041    static storage that can be reused in the next call to this
1042    function, to ctime, or to asctime.  If FULL_TIME, then output the
1043    time stamp to its full resolution; otherwise, just output it to
1044    1-minute resolution.  */
1045 char const *
tartime(struct timespec t,bool full_time)1046 tartime (struct timespec t, bool full_time)
1047 {
1048   enum { fraclen = sizeof ".FFFFFFFFF" - 1 };
1049   static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
1050 			  INT_STRLEN_BOUND (int) + 16)
1051 		     + fraclen];
1052   struct tm *tm;
1053   time_t s = t.tv_sec;
1054   int ns = t.tv_nsec;
1055   bool negative = s < 0;
1056   char *p;
1057 
1058   if (negative && ns != 0)
1059     {
1060       s++;
1061       ns = 1000000000 - ns;
1062     }
1063 
1064   tm = utc_option ? gmtime (&s) : localtime (&s);
1065   if (tm)
1066     {
1067       if (full_time)
1068 	{
1069 	  strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M:%S", tm);
1070 	  code_ns_fraction (ns, buffer + strlen (buffer));
1071 	}
1072       else
1073 	strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M", tm);
1074       return buffer;
1075     }
1076 
1077   /* The time stamp cannot be broken down, most likely because it
1078      is out of range.  Convert it as an integer,
1079      right-adjusted in a field with the same width as the usual
1080      4-year ISO time format.  */
1081   p = umaxtostr (negative ? - (uintmax_t) s : s,
1082 		 buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen);
1083   if (negative)
1084     *--p = '-';
1085   while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM"
1086 	  + (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0))
1087 	 < p)
1088     *--p = ' ';
1089   if (full_time)
1090     code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen);
1091   return p;
1092 }
1093 
1094 /* Actually print it.
1095 
1096    Plain and fancy file header block logging.  Non-verbose just prints
1097    the name, e.g. for "tar t" or "tar x".  This should just contain
1098    file names, so it can be fed back into tar with xargs or the "-T"
1099    option.  The verbose option can give a bunch of info, one line per
1100    file.  I doubt anybody tries to parse its format, or if they do,
1101    they shouldn't.  Unix tar is pretty random here anyway.  */
1102 
1103 
1104 /* Width of "user/group size", with initial value chosen
1105    heuristically.  This grows as needed, though this may cause some
1106    stairstepping in the output.  Make it too small and the output will
1107    almost always look ragged.  Make it too large and the output will
1108    be spaced out too far.  */
1109 static int ugswidth = 19;
1110 
1111 /* Width of printed time stamps.  It grows if longer time stamps are
1112    found (typically, those with nanosecond resolution).  Like
1113    USGWIDTH, some stairstepping may occur.  */
1114 static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1;
1115 
1116 static bool volume_label_printed = false;
1117 
1118 static void
simple_print_header(struct tar_stat_info * st,union block * blk,off_t block_ordinal)1119 simple_print_header (struct tar_stat_info *st, union block *blk,
1120 		     off_t block_ordinal)
1121 {
1122   char modes[12];
1123   char const *time_stamp;
1124   int time_stamp_len;
1125   char *temp_name;
1126 
1127   /* These hold formatted ints.  */
1128   char uform[max (INT_BUFSIZE_BOUND (intmax_t), UINTMAX_STRSIZE_BOUND)];
1129   char gform[sizeof uform];
1130   char *user, *group;
1131   char size[2 * UINTMAX_STRSIZE_BOUND];
1132   				/* holds formatted size or major,minor */
1133   char uintbuf[UINTMAX_STRSIZE_BOUND];
1134   int pad;
1135   int sizelen;
1136 
1137   if (show_transformed_names_option)
1138     temp_name = st->file_name ? st->file_name : st->orig_file_name;
1139   else
1140     temp_name = st->orig_file_name ? st->orig_file_name : st->file_name;
1141 
1142   if (block_number_option)
1143     {
1144       char buf[UINTMAX_STRSIZE_BOUND];
1145       if (block_ordinal < 0)
1146 	block_ordinal = current_block_ordinal ();
1147       block_ordinal -= recent_long_name_blocks;
1148       block_ordinal -= recent_long_link_blocks;
1149       fprintf (stdlis, _("block %s: "),
1150 	       STRINGIFY_BIGINT (block_ordinal, buf));
1151     }
1152 
1153   if (verbose_option <= 1)
1154     {
1155       /* Just the fax, mam.  */
1156       fputs (quotearg (temp_name), stdlis);
1157       if (show_transformed_names_option && st->had_trailing_slash)
1158 	fputc ('/', stdlis);
1159       fputc ('\n', stdlis);
1160     }
1161   else
1162     {
1163       /* File type and modes.  */
1164 
1165       modes[0] = '?';
1166       switch (blk->header.typeflag)
1167 	{
1168 	case GNUTYPE_VOLHDR:
1169 	  volume_label_printed = true;
1170 	  modes[0] = 'V';
1171 	  break;
1172 
1173 	case GNUTYPE_MULTIVOL:
1174 	  modes[0] = 'M';
1175 	  break;
1176 
1177 	case GNUTYPE_LONGNAME:
1178 	case GNUTYPE_LONGLINK:
1179 	  modes[0] = 'L';
1180 	  ERROR ((0, 0, _("Unexpected long name header")));
1181 	  break;
1182 
1183 	case GNUTYPE_SPARSE:
1184 	case REGTYPE:
1185 	case AREGTYPE:
1186 	  modes[0] = st->had_trailing_slash ? 'd' : '-';
1187 	  break;
1188 	case LNKTYPE:
1189 	  modes[0] = 'h';
1190 	  break;
1191 	case GNUTYPE_DUMPDIR:
1192 	  modes[0] = 'd';
1193 	  break;
1194 	case DIRTYPE:
1195 	  modes[0] = 'd';
1196 	  break;
1197 	case SYMTYPE:
1198 	  modes[0] = 'l';
1199 	  break;
1200 	case BLKTYPE:
1201 	  modes[0] = 'b';
1202 	  break;
1203 	case CHRTYPE:
1204 	  modes[0] = 'c';
1205 	  break;
1206 	case FIFOTYPE:
1207 	  modes[0] = 'p';
1208 	  break;
1209 	case CONTTYPE:
1210 	  modes[0] = 'C';
1211 	  break;
1212 	}
1213 
1214       pax_decode_mode (st->stat.st_mode, modes + 1);
1215 
1216       /* extended attributes:  GNU `ls -l'-like preview */
1217       xattrs_print_char (st, modes + 10);
1218 
1219       /* Time stamp.  */
1220 
1221       time_stamp = tartime (st->mtime, full_time_option);
1222       time_stamp_len = strlen (time_stamp);
1223       if (datewidth < time_stamp_len)
1224 	datewidth = time_stamp_len;
1225 
1226       /* User and group names.  */
1227 
1228       if (st->uname
1229 	  && st->uname[0]
1230 	  && current_format != V7_FORMAT
1231 	  && !numeric_owner_option)
1232 	user = st->uname;
1233       else
1234 	user = STRINGIFY_BIGINT (st->stat.st_uid, uform);
1235 
1236       if (st->gname
1237 	  && st->gname[0]
1238 	  && current_format != V7_FORMAT
1239 	  && !numeric_owner_option)
1240 	group = st->gname;
1241       else
1242 	group = STRINGIFY_BIGINT (st->stat.st_gid, gform);
1243 
1244       /* Format the file size or major/minor device numbers.  */
1245 
1246       switch (blk->header.typeflag)
1247 	{
1248 	case CHRTYPE:
1249 	case BLKTYPE:
1250 	  strcpy (size,
1251 		  STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf));
1252 	  strcat (size, ",");
1253 	  strcat (size,
1254 		  STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf));
1255 	  break;
1256 
1257 	default:
1258 	  /* st->stat.st_size keeps stored file size */
1259 	  strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf));
1260 	  break;
1261 	}
1262 
1263       /* Figure out padding and print the whole line.  */
1264 
1265       sizelen = strlen (size);
1266       pad = strlen (user) + 1 + strlen (group) + 1 + sizelen;
1267       if (pad > ugswidth)
1268 	ugswidth = pad;
1269 
1270       fprintf (stdlis, "%s %s/%s %*s %-*s",
1271 	       modes, user, group, ugswidth - pad + sizelen, size,
1272 	       datewidth, time_stamp);
1273 
1274       fprintf (stdlis, " %s", quotearg (temp_name));
1275       if (show_transformed_names_option && st->had_trailing_slash)
1276 	fputc ('/', stdlis);
1277 
1278       switch (blk->header.typeflag)
1279 	{
1280 	case SYMTYPE:
1281 	  fprintf (stdlis, " -> %s\n", quotearg (st->link_name));
1282 	  break;
1283 
1284 	case LNKTYPE:
1285 	  fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name));
1286 	  break;
1287 
1288 	default:
1289 	  {
1290 	    char type_string[2];
1291 	    type_string[0] = blk->header.typeflag;
1292 	    type_string[1] = '\0';
1293 	    fprintf (stdlis, _(" unknown file type %s\n"),
1294 		     quote (type_string));
1295 	  }
1296 	  break;
1297 
1298 	case AREGTYPE:
1299 	case REGTYPE:
1300 	case GNUTYPE_SPARSE:
1301 	case CHRTYPE:
1302 	case BLKTYPE:
1303 	case DIRTYPE:
1304 	case FIFOTYPE:
1305 	case CONTTYPE:
1306 	case GNUTYPE_DUMPDIR:
1307 	  putc ('\n', stdlis);
1308 	  break;
1309 
1310 	case GNUTYPE_LONGLINK:
1311 	  fprintf (stdlis, _("--Long Link--\n"));
1312 	  break;
1313 
1314 	case GNUTYPE_LONGNAME:
1315 	  fprintf (stdlis, _("--Long Name--\n"));
1316 	  break;
1317 
1318 	case GNUTYPE_VOLHDR:
1319 	  fprintf (stdlis, _("--Volume Header--\n"));
1320 	  break;
1321 
1322 	case GNUTYPE_MULTIVOL:
1323 	  strcpy (size,
1324 		  STRINGIFY_BIGINT
1325 		  (UINTMAX_FROM_HEADER (blk->oldgnu_header.offset),
1326 		   uintbuf));
1327 	  fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1328 	  break;
1329 	}
1330     }
1331   fflush (stdlis);
1332   xattrs_print (st);
1333 }
1334 
1335 
1336 static void
print_volume_label(void)1337 print_volume_label (void)
1338 {
1339   struct tar_stat_info vstat;
1340   union block vblk;
1341   enum archive_format dummy;
1342 
1343   memset (&vblk, 0, sizeof (vblk));
1344   vblk.header.typeflag = GNUTYPE_VOLHDR;
1345   if (recent_global_header)
1346     memcpy (vblk.header.mtime, recent_global_header->header.mtime,
1347 	    sizeof vblk.header.mtime);
1348   tar_stat_init (&vstat);
1349   assign_string (&vstat.file_name, ".");
1350   decode_header (&vblk, &vstat, &dummy, 0);
1351   assign_string (&vstat.file_name, volume_label);
1352   simple_print_header (&vstat, &vblk, 0);
1353   tar_stat_destroy (&vstat);
1354 }
1355 
1356 void
print_header(struct tar_stat_info * st,union block * blk,off_t block_ordinal)1357 print_header (struct tar_stat_info *st, union block *blk,
1358 	      off_t block_ordinal)
1359 {
1360   if (current_format == POSIX_FORMAT && !volume_label_printed && volume_label)
1361     {
1362       print_volume_label ();
1363       volume_label_printed = true;
1364     }
1365 
1366   simple_print_header (st, blk, block_ordinal);
1367 }
1368 
1369 /* Print a similar line when we make a directory automatically.  */
1370 void
print_for_mkdir(char * dirname,int length,mode_t mode)1371 print_for_mkdir (char *dirname, int length, mode_t mode)
1372 {
1373   char modes[11];
1374 
1375   if (verbose_option > 1)
1376     {
1377       /* File type and modes.  */
1378 
1379       modes[0] = 'd';
1380       pax_decode_mode (mode, modes + 1);
1381 
1382       if (block_number_option)
1383 	{
1384 	  char buf[UINTMAX_STRSIZE_BOUND];
1385 	  fprintf (stdlis, _("block %s: "),
1386 		   STRINGIFY_BIGINT (current_block_ordinal (), buf));
1387 	}
1388 
1389       fprintf (stdlis, "%s %*s %s\n", modes, ugswidth + 1 + datewidth,
1390 	       _("Creating directory:"), quotearg (dirname));
1391     }
1392 }
1393 
1394 /* Skip over SIZE bytes of data in blocks in the archive.  */
1395 void
skip_file(off_t size)1396 skip_file (off_t size)
1397 {
1398   union block *x;
1399 
1400   /* FIXME: Make sure mv_begin_read is always called before it */
1401 
1402   if (seekable_archive)
1403     {
1404       off_t nblk = seek_archive (size);
1405       if (nblk >= 0)
1406 	size -= nblk * BLOCKSIZE;
1407       else
1408 	seekable_archive = false;
1409     }
1410 
1411   mv_size_left (size);
1412 
1413   while (size > 0)
1414     {
1415       x = find_next_block ();
1416       if (! x)
1417 	FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1418 
1419       set_next_block_after (x);
1420       size -= BLOCKSIZE;
1421       mv_size_left (size);
1422     }
1423 }
1424 
1425 /* Skip the current member in the archive.
1426    NOTE: Current header must be decoded before calling this function. */
1427 void
skip_member(void)1428 skip_member (void)
1429 {
1430   if (!current_stat_info.skipped)
1431     {
1432       char save_typeflag = current_header->header.typeflag;
1433       set_next_block_after (current_header);
1434 
1435       mv_begin_read (&current_stat_info);
1436 
1437       if (current_stat_info.is_sparse)
1438 	sparse_skip_file (&current_stat_info);
1439       else if (save_typeflag != DIRTYPE)
1440 	skip_file (current_stat_info.stat.st_size);
1441 
1442       mv_end ();
1443     }
1444 }
1445 
1446 void
test_archive_label(void)1447 test_archive_label (void)
1448 {
1449   base64_init ();
1450   name_gather ();
1451 
1452   open_archive (ACCESS_READ);
1453   if (read_header (&current_header, &current_stat_info, read_header_auto)
1454       == HEADER_SUCCESS)
1455     {
1456       decode_header (current_header,
1457 		     &current_stat_info, &current_format, 0);
1458       if (current_header->header.typeflag == GNUTYPE_VOLHDR)
1459 	ASSIGN_STRING_N (&volume_label, current_header->header.name);
1460 
1461       if (volume_label)
1462 	{
1463 	  if (verbose_option)
1464 	    print_volume_label ();
1465 	  if (!name_match (volume_label) && multi_volume_option)
1466 	    {
1467 	      char *s = drop_volume_label_suffix (volume_label);
1468 	      name_match (s);
1469 	      free (s);
1470 	    }
1471 	}
1472     }
1473   close_archive ();
1474   label_notfound ();
1475 }
1476