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