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