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