]> Dogcows Code - chaz/tar/blob - src/list.c
(from_header): Do not issue a diagnostic if TYPE is zero.
[chaz/tar] / src / list.c
1 /* List a tar archive, with support routines for reading a tar archive.
2
3 Copyright 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001 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 2, 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 /* Define to non-zero for forcing old ctime format instead of ISO format. */
23 #undef USE_OLD_CTIME
24
25 #include "system.h"
26 #include <quotearg.h>
27
28 #include "common.h"
29
30 #define max(a, b) ((a) < (b) ? (b) : (a))
31
32 union block *current_header; /* points to current archive header */
33 struct stat current_stat; /* stat struct corresponding */
34 enum archive_format current_format; /* recognized format */
35
36 static uintmax_t from_header PARAMS ((const char *, size_t, const char *,
37 uintmax_t, uintmax_t));
38
39 /* Base 64 digits; see Internet RFC 2045 Table 1. */
40 static char const base_64_digits[64] =
41 {
42 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
43 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
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 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
47 };
48
49 /* Table of base-64 digit values indexed by unsigned chars.
50 The value is 64 for unsigned chars that are not base-64 digits. */
51 static char base64_map[UCHAR_MAX + 1];
52
53 static void
54 base64_init (void)
55 {
56 int i;
57 memset (base64_map, 64, sizeof base64_map);
58 for (i = 0; i < 64; i++)
59 base64_map[(int) base_64_digits[i]] = i;
60 }
61
62 /* Main loop for reading an archive. */
63 void
64 read_and (void (*do_something) ())
65 {
66 enum read_header status = HEADER_STILL_UNREAD;
67 enum read_header prev_status;
68
69 base64_init ();
70 name_gather ();
71 open_archive (ACCESS_READ);
72
73 while (1)
74 {
75 prev_status = status;
76 status = read_header ();
77 switch (status)
78 {
79 case HEADER_STILL_UNREAD:
80 abort ();
81
82 case HEADER_SUCCESS:
83
84 /* Valid header. We should decode next field (mode) first.
85 Ensure incoming names are null terminated. */
86
87 if (! name_match (current_file_name)
88 || (newer_mtime_option != TYPE_MINIMUM (time_t)
89 /* FIXME: We get mtime now, and again later; this causes
90 duplicate diagnostics if header.mtime is bogus. */
91 && ((current_stat.st_mtime
92 = TIME_FROM_HEADER (current_header->header.mtime))
93 < newer_mtime_option))
94 || excluded_name (current_file_name))
95 {
96 switch (current_header->header.typeflag)
97 {
98 case GNUTYPE_VOLHDR:
99 case GNUTYPE_MULTIVOL:
100 case GNUTYPE_NAMES:
101 break;
102
103 case DIRTYPE:
104 if (show_omitted_dirs_option)
105 WARN ((0, 0, _("%s: Omitting"),
106 quotearg_colon (current_file_name)));
107 /* Fall through. */
108 default:
109 skip_member ();
110 continue;
111 }
112 }
113
114 (*do_something) ();
115 continue;
116
117 case HEADER_ZERO_BLOCK:
118 if (block_number_option)
119 {
120 char buf[UINTMAX_STRSIZE_BOUND];
121 fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
122 STRINGIFY_BIGINT (current_block_ordinal (), buf));
123 }
124
125 set_next_block_after (current_header);
126 status = prev_status;
127 if (ignore_zeros_option)
128 continue;
129 break;
130
131 case HEADER_END_OF_FILE:
132 if (block_number_option)
133 {
134 char buf[UINTMAX_STRSIZE_BOUND];
135 fprintf (stdlis, _("block %s: ** End of File **\n"),
136 STRINGIFY_BIGINT (current_block_ordinal (), buf));
137 }
138 break;
139
140 case HEADER_FAILURE:
141 /* If the previous header was good, tell them that we are
142 skipping bad ones. */
143 set_next_block_after (current_header);
144 switch (prev_status)
145 {
146 case HEADER_STILL_UNREAD:
147 ERROR ((0, 0, _("This does not look like a tar archive")));
148 /* Fall through. */
149
150 case HEADER_ZERO_BLOCK:
151 case HEADER_SUCCESS:
152 ERROR ((0, 0, _("Skipping to next header")));
153 break;
154
155 case HEADER_END_OF_FILE:
156 case HEADER_FAILURE:
157 /* We are in the middle of a cascade of errors. */
158 break;
159 }
160 continue;
161 }
162 break;
163 }
164
165 close_archive ();
166 names_notfound (); /* print names not found */
167 }
168
169 /* Print a header block, based on tar options. */
170 void
171 list_archive (void)
172 {
173 /* Print the header block. */
174
175 if (verbose_option)
176 {
177 if (verbose_option > 1)
178 decode_header (current_header, &current_stat, &current_format, 0);
179 print_header ();
180 }
181
182 if (incremental_option && current_header->header.typeflag == GNUTYPE_DUMPDIR)
183 {
184 off_t size;
185 size_t written, check;
186 union block *data_block;
187
188 set_next_block_after (current_header);
189 if (multi_volume_option)
190 {
191 assign_string (&save_name, current_file_name);
192 save_totsize = current_stat.st_size;
193 }
194 for (size = current_stat.st_size; size > 0; size -= written)
195 {
196 if (multi_volume_option)
197 save_sizeleft = size;
198 data_block = find_next_block ();
199 if (!data_block)
200 {
201 ERROR ((0, 0, _("Unexpected EOF in archive")));
202 break; /* FIXME: What happens, then? */
203 }
204 written = available_space_after (data_block);
205 if (written > size)
206 written = size;
207 errno = 0;
208 check = fwrite (data_block->buffer, sizeof (char), written, stdlis);
209 set_next_block_after ((union block *)
210 (data_block->buffer + written - 1));
211 if (check != written)
212 {
213 write_error_details (current_file_name, check, written);
214 skip_file (size - written);
215 break;
216 }
217 }
218 if (multi_volume_option)
219 assign_string (&save_name, 0);
220 fputc ('\n', stdlis);
221 fflush (stdlis);
222 return;
223
224 }
225
226 if (multi_volume_option)
227 assign_string (&save_name, current_file_name);
228
229 skip_member ();
230
231 if (multi_volume_option)
232 assign_string (&save_name, 0);
233 }
234
235 /* Read a block that's supposed to be a header block. Return its
236 address in "current_header", and if it is good, the file's size in
237 current_stat.st_size.
238
239 Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a
240 block full of zeros (EOF marker).
241
242 You must always set_next_block_after(current_header) to skip past
243 the header which this routine reads. */
244
245 /* The standard BSD tar sources create the checksum by adding up the
246 bytes in the header as type char. I think the type char was unsigned
247 on the PDP-11, but it's signed on the Next and Sun. It looks like the
248 sources to BSD tar were never changed to compute the checksum
249 correctly, so both the Sun and Next add the bytes of the header as
250 signed chars. This doesn't cause a problem until you get a file with
251 a name containing characters with the high bit set. So read_header
252 computes two checksums -- signed and unsigned. */
253
254 enum read_header
255 read_header (void)
256 {
257 size_t i;
258 int unsigned_sum; /* the POSIX one :-) */
259 int signed_sum; /* the Sun one :-( */
260 int recorded_sum;
261 uintmax_t parsed_sum;
262 char *p;
263 union block *header;
264 char **longp;
265 char *bp;
266 union block *data_block;
267 size_t size, written;
268 static char *next_long_name, *next_long_link;
269
270 while (1)
271 {
272 header = find_next_block ();
273 current_header = header;
274 if (!header)
275 return HEADER_END_OF_FILE;
276
277 unsigned_sum = 0;
278 signed_sum = 0;
279 p = header->buffer;
280 for (i = sizeof *header; i-- != 0;)
281 {
282 unsigned_sum += (unsigned char) *p;
283 signed_sum += (signed char) (*p++);
284 }
285
286 if (unsigned_sum == 0)
287 return HEADER_ZERO_BLOCK;
288
289 /* Adjust checksum to count the "chksum" field as blanks. */
290
291 for (i = sizeof header->header.chksum; i-- != 0;)
292 {
293 unsigned_sum -= (unsigned char) header->header.chksum[i];
294 signed_sum -= (signed char) (header->header.chksum[i]);
295 }
296 unsigned_sum += ' ' * sizeof header->header.chksum;
297 signed_sum += ' ' * sizeof header->header.chksum;
298
299 parsed_sum = from_header (header->header.chksum,
300 sizeof header->header.chksum, 0,
301 (uintmax_t) 0,
302 (uintmax_t) TYPE_MAXIMUM (int));
303 if (parsed_sum == (uintmax_t) -1)
304 return HEADER_FAILURE;
305
306 recorded_sum = parsed_sum;
307
308 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
309 return HEADER_FAILURE;
310
311 /* Good block. Decode file size and return. */
312
313 if (header->header.typeflag == LNKTYPE)
314 current_stat.st_size = 0; /* links 0 size on tape */
315 else
316 current_stat.st_size = OFF_FROM_HEADER (header->header.size);
317
318 if (header->header.typeflag == GNUTYPE_LONGNAME
319 || header->header.typeflag == GNUTYPE_LONGLINK)
320 {
321 longp = ((header->header.typeflag == GNUTYPE_LONGNAME)
322 ? &next_long_name
323 : &next_long_link);
324
325 set_next_block_after (header);
326 if (*longp)
327 free (*longp);
328 size = current_stat.st_size;
329 if (size != current_stat.st_size)
330 xalloc_die ();
331 bp = *longp = xmalloc (size);
332
333 for (; size > 0; size -= written)
334 {
335 data_block = find_next_block ();
336 if (! data_block)
337 {
338 ERROR ((0, 0, _("Unexpected EOF in archive")));
339 break;
340 }
341 written = available_space_after (data_block);
342 if (written > size)
343 written = size;
344
345 memcpy (bp, data_block->buffer, written);
346 bp += written;
347 set_next_block_after ((union block *)
348 (data_block->buffer + written - 1));
349 }
350
351 /* Loop! */
352
353 }
354 else
355 {
356 char const *name;
357 struct posix_header const *h = &current_header->header;
358 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
359
360 name = next_long_name;
361 if (! name)
362 {
363 /* Accept file names as specified by POSIX.1-1996
364 section 10.1.1. */
365 char *np = namebuf;
366
367 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
368 {
369 memcpy (np, h->prefix, sizeof h->prefix);
370 np[sizeof h->prefix] = '\0';
371 np += strlen (np);
372 *np++ = '/';
373
374 /* Prevent later references to current_header from
375 mistakenly treating this as an old GNU header.
376 This assignment invalidates h->prefix. */
377 current_header->oldgnu_header.isextended = 0;
378 }
379 memcpy (np, h->name, sizeof h->name);
380 np[sizeof h->name] = '\0';
381 name = namebuf;
382 }
383 assign_string (&current_file_name, name);
384 if (next_long_name)
385 {
386 free (next_long_name);
387 next_long_name = 0;
388 }
389
390 name = next_long_link;
391 if (! name)
392 {
393 memcpy (namebuf, h->linkname, sizeof h->linkname);
394 namebuf[sizeof h->linkname] = '\0';
395 name = namebuf;
396 }
397 assign_string (&current_link_name, name);
398 if (next_long_link)
399 {
400 free (next_long_link);
401 next_long_link = 0;
402 }
403
404 return HEADER_SUCCESS;
405 }
406 }
407 }
408
409 /* Decode things from a file HEADER block into STAT_INFO, also setting
410 *FORMAT_POINTER depending on the header block format. If
411 DO_USER_GROUP, decode the user/group information (this is useful
412 for extraction, but waste time when merely listing).
413
414 read_header() has already decoded the checksum and length, so we don't.
415
416 This routine should *not* be called twice for the same block, since
417 the two calls might use different DO_USER_GROUP values and thus
418 might end up with different uid/gid for the two calls. If anybody
419 wants the uid/gid they should decode it first, and other callers
420 should decode it without uid/gid before calling a routine,
421 e.g. print_header, that assumes decoded data. */
422 void
423 decode_header (union block *header, struct stat *stat_info,
424 enum archive_format *format_pointer, int do_user_group)
425 {
426 enum archive_format format;
427
428 if (strcmp (header->header.magic, TMAGIC) == 0)
429 format = POSIX_FORMAT;
430 else if (strcmp (header->header.magic, OLDGNU_MAGIC) == 0)
431 format = OLDGNU_FORMAT;
432 else
433 format = V7_FORMAT;
434 *format_pointer = format;
435
436 stat_info->st_mode = MODE_FROM_HEADER (header->header.mode);
437 stat_info->st_mtime = TIME_FROM_HEADER (header->header.mtime);
438
439 if (format == OLDGNU_FORMAT && incremental_option)
440 {
441 stat_info->st_atime = TIME_FROM_HEADER (header->oldgnu_header.atime);
442 stat_info->st_ctime = TIME_FROM_HEADER (header->oldgnu_header.ctime);
443 }
444
445 if (format == V7_FORMAT)
446 {
447 stat_info->st_uid = UID_FROM_HEADER (header->header.uid);
448 stat_info->st_gid = GID_FROM_HEADER (header->header.gid);
449 stat_info->st_rdev = 0;
450 }
451 else
452 {
453 if (do_user_group)
454 {
455 /* FIXME: Decide if this should somewhat depend on -p. */
456
457 if (numeric_owner_option
458 || !*header->header.uname
459 || !uname_to_uid (header->header.uname, &stat_info->st_uid))
460 stat_info->st_uid = UID_FROM_HEADER (header->header.uid);
461
462 if (numeric_owner_option
463 || !*header->header.gname
464 || !gname_to_gid (header->header.gname, &stat_info->st_gid))
465 stat_info->st_gid = GID_FROM_HEADER (header->header.gid);
466 }
467 switch (header->header.typeflag)
468 {
469 case BLKTYPE:
470 stat_info->st_rdev
471 = makedev (MAJOR_FROM_HEADER (header->header.devmajor),
472 MINOR_FROM_HEADER (header->header.devminor));
473 break;
474
475 case CHRTYPE:
476 stat_info->st_rdev
477 = makedev (MAJOR_FROM_HEADER (header->header.devmajor),
478 MINOR_FROM_HEADER (header->header.devminor));
479 break;
480
481 default:
482 stat_info->st_rdev = 0;
483 }
484 }
485 }
486
487 /* Convert buffer at WHERE0 of size DIGS from external format to
488 uintmax_t. The data is of type TYPE. The buffer must represent a
489 value in the range -MINUS_MINVAL through MAXVAL. DIGS must be
490 positive. Return -1 on error, diagnosing the error if TYPE is
491 nonzero. */
492 static uintmax_t
493 from_header (char const *where0, size_t digs, char const *type,
494 uintmax_t minus_minval, uintmax_t maxval)
495 {
496 uintmax_t value;
497 char const *where = where0;
498 char const *lim = where + digs;
499 int negative = 0;
500
501 /* Accommodate buggy tar of unknown vintage, which outputs leading
502 NUL if the previous field overflows. */
503 where += !*where;
504
505 /* Accommodate older tars, which output leading spaces. */
506 for (;;)
507 {
508 if (where == lim)
509 {
510 if (type)
511 ERROR ((0, 0,
512 _("Blanks in header where numeric %s value expected"),
513 type));
514 return -1;
515 }
516 if (!ISSPACE ((unsigned char) *where))
517 break;
518 where++;
519 }
520
521 value = 0;
522 if (ISODIGIT (*where))
523 {
524 char const *where1 = where;
525 uintmax_t overflow = 0;
526
527 for (;;)
528 {
529 value += *where++ - '0';
530 if (where == lim || ! ISODIGIT (*where))
531 break;
532 overflow |= value ^ (value << LG_8 >> LG_8);
533 value <<= LG_8;
534 }
535
536 /* Parse the output of older, unportable tars, which generate
537 negative values in two's complement octal. If the leading
538 nonzero digit is 1, we can't recover the original value
539 reliably; so do this only if the digit is 2 or more. This
540 catches the common case of 32-bit negative time stamps. */
541 if ((overflow || maxval < value) && '2' <= *where1 && type)
542 {
543 /* Compute the negative of the input value, assuming two's
544 complement. */
545 int digit = (*where1 - '0') | 4;
546 overflow = 0;
547 value = 0;
548 where = where1;
549 for (;;)
550 {
551 value += 7 - digit;
552 where++;
553 if (where == lim || ! ISODIGIT (*where))
554 break;
555 digit = *where - '0';
556 overflow |= value ^ (value << LG_8 >> LG_8);
557 value <<= LG_8;
558 }
559 value++;
560 overflow |= !value;
561
562 if (!overflow && value <= minus_minval)
563 {
564 WARN ((0, 0,
565 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
566 (int) (where - where1), where1, type));
567 negative = 1;
568 }
569 }
570
571 if (overflow)
572 {
573 if (type)
574 ERROR ((0, 0,
575 _("Archive octal value %.*s is out of %s range"),
576 (int) (where - where1), where1, type));
577 return -1;
578 }
579 }
580 else if (*where == '-' || *where == '+')
581 {
582 /* Parse base-64 output produced only by tar test versions
583 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
584 Support for this will be withdrawn in future releases. */
585 int dig;
586 static int warned_once;
587 if (! warned_once)
588 {
589 warned_once = 1;
590 WARN ((0, 0,
591 _("Archive contains obsolescent base-64 headers")));
592 }
593 negative = *where++ == '-';
594 while (where != lim
595 && (dig = base64_map[(unsigned char) *where]) < 64)
596 {
597 if (value << LG_64 >> LG_64 != value)
598 {
599 char *string = alloca (digs + 1);
600 memcpy (string, where0, digs);
601 string[digs] = '\0';
602 if (type)
603 ERROR ((0, 0,
604 _("Archive signed base-64 string %s is out of %s range"),
605 quote (string), type));
606 return -1;
607 }
608 value = (value << LG_64) | dig;
609 where++;
610 }
611 }
612 else if (*where == '\200' /* positive base-256 */
613 || *where == '\377' /* negative base-256 */)
614 {
615 /* Parse base-256 output. A nonnegative number N is
616 represented as (256**DIGS)/2 + N; a negative number -N is
617 represented as (256**DIGS) - N, i.e. as two's complement.
618 The representation guarantees that the leading bit is
619 always on, so that we don't confuse this format with the
620 others (assuming ASCII bytes of 8 bits or more). */
621 int signbit = *where & (1 << (LG_256 - 2));
622 uintmax_t topbits = (((uintmax_t) - signbit)
623 << (CHAR_BIT * sizeof (uintmax_t)
624 - LG_256 - (LG_256 - 2)));
625 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
626 for (;;)
627 {
628 value = (value << LG_256) + (unsigned char) *where++;
629 if (where == lim)
630 break;
631 if (((value << LG_256 >> LG_256) | topbits) != value)
632 {
633 if (type)
634 ERROR ((0, 0,
635 _("Archive base-256 value is out of %s range"),
636 type));
637 return -1;
638 }
639 }
640 negative = signbit;
641 if (negative)
642 value = -value;
643 }
644
645 if (where != lim && *where && !ISSPACE ((unsigned char) *where))
646 {
647 if (type)
648 {
649 char buf[1000]; /* Big enough to represent any header. */
650 static struct quoting_options *o;
651
652 if (!o)
653 {
654 o = clone_quoting_options (0);
655 set_quoting_style (o, locale_quoting_style);
656 }
657
658 while (where0 != lim && ! lim[-1])
659 lim--;
660 quotearg_buffer (buf, sizeof buf, where0, lim - where, o);
661 ERROR ((0, 0,
662 _("Archive contains %.*s where numeric %s value expected"),
663 (int) sizeof buf, buf, type));
664 }
665
666 return -1;
667 }
668
669 if (value <= (negative ? minus_minval : maxval))
670 return negative ? -value : value;
671
672 if (type)
673 {
674 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
675 char maxval_buf[UINTMAX_STRSIZE_BOUND];
676 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
677 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
678 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
679 if (negative)
680 *--value_string = '-';
681 if (minus_minval)
682 *--minval_string = '-';
683 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
684 value_string, type,
685 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
686 }
687
688 return -1;
689 }
690
691 gid_t
692 gid_from_header (const char *p, size_t s)
693 {
694 return from_header (p, s, "gid_t",
695 - (uintmax_t) TYPE_MINIMUM (gid_t),
696 (uintmax_t) TYPE_MAXIMUM (gid_t));
697 }
698
699 major_t
700 major_from_header (const char *p, size_t s)
701 {
702 return from_header (p, s, "major_t",
703 - (uintmax_t) TYPE_MINIMUM (major_t),
704 (uintmax_t) TYPE_MAXIMUM (major_t));
705 }
706
707 minor_t
708 minor_from_header (const char *p, size_t s)
709 {
710 return from_header (p, s, "minor_t",
711 - (uintmax_t) TYPE_MINIMUM (minor_t),
712 (uintmax_t) TYPE_MAXIMUM (minor_t));
713 }
714
715 mode_t
716 mode_from_header (const char *p, size_t s)
717 {
718 /* Do not complain about unrecognized mode bits. */
719 unsigned u = from_header (p, s, "mode_t",
720 - (uintmax_t) TYPE_MINIMUM (mode_t),
721 TYPE_MAXIMUM (uintmax_t));
722 return ((u & TSUID ? S_ISUID : 0)
723 | (u & TSGID ? S_ISGID : 0)
724 | (u & TSVTX ? S_ISVTX : 0)
725 | (u & TUREAD ? S_IRUSR : 0)
726 | (u & TUWRITE ? S_IWUSR : 0)
727 | (u & TUEXEC ? S_IXUSR : 0)
728 | (u & TGREAD ? S_IRGRP : 0)
729 | (u & TGWRITE ? S_IWGRP : 0)
730 | (u & TGEXEC ? S_IXGRP : 0)
731 | (u & TOREAD ? S_IROTH : 0)
732 | (u & TOWRITE ? S_IWOTH : 0)
733 | (u & TOEXEC ? S_IXOTH : 0));
734 }
735
736 off_t
737 off_from_header (const char *p, size_t s)
738 {
739 /* Negative offsets are not allowed in tar files, so invoke
740 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
741 return from_header (p, s, "off_t", (uintmax_t) 0,
742 (uintmax_t) TYPE_MAXIMUM (off_t));
743 }
744
745 size_t
746 size_from_header (const char *p, size_t s)
747 {
748 return from_header (p, s, "size_t", (uintmax_t) 0,
749 (uintmax_t) TYPE_MAXIMUM (size_t));
750 }
751
752 time_t
753 time_from_header (const char *p, size_t s)
754 {
755 return from_header (p, s, "time_t",
756 - (uintmax_t) TYPE_MINIMUM (time_t),
757 (uintmax_t) TYPE_MAXIMUM (time_t));
758 }
759
760 uid_t
761 uid_from_header (const char *p, size_t s)
762 {
763 return from_header (p, s, "uid_t",
764 - (uintmax_t) TYPE_MINIMUM (uid_t),
765 (uintmax_t) TYPE_MAXIMUM (uid_t));
766 }
767
768 uintmax_t
769 uintmax_from_header (const char *p, size_t s)
770 {
771 return from_header (p, s, "uintmax_t", (uintmax_t) 0,
772 TYPE_MAXIMUM (uintmax_t));
773 }
774
775
776 /* Format O as a null-terminated decimal string into BUF _backwards_;
777 return pointer to start of result. */
778 char *
779 stringify_uintmax_t_backwards (uintmax_t o, char *buf)
780 {
781 *--buf = '\0';
782 do
783 *--buf = '0' + (int) (o % 10);
784 while ((o /= 10) != 0);
785 return buf;
786 }
787
788 /* Return a printable representation of T. The result points to
789 static storage that can be reused in the next call to this
790 function, to ctime, or to asctime. */
791 char const *
792 tartime (time_t t)
793 {
794 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
795 INT_STRLEN_BOUND (int) + 16)];
796 char *p;
797
798 #if USE_OLD_CTIME
799 p = ctime (&t);
800 if (p)
801 {
802 char const *time_stamp = p + 4;
803 for (p += 16; p[3] != '\n'; p++)
804 p[0] = p[3];
805 p[0] = '\0';
806 return time_stamp;
807 }
808 #else
809 /* Use ISO 8610 format. See:
810 http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
811 struct tm *tm = localtime (&t);
812 if (tm)
813 {
814 sprintf (buffer, "%04d-%02d-%02d %02d:%02d:%02d",
815 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
816 tm->tm_hour, tm->tm_min, tm->tm_sec);
817 return buffer;
818 }
819 #endif
820
821 /* The time stamp cannot be broken down, most likely because it
822 is out of range. Convert it as an integer,
823 right-adjusted in a field with the same width as the usual
824 19-byte 4-year ISO time format. */
825 p = stringify_uintmax_t_backwards (t < 0 ? - (uintmax_t) t : (uintmax_t) t,
826 buffer + sizeof buffer);
827 if (t < 0)
828 *--p = '-';
829 while (buffer + sizeof buffer - 19 - 1 < p)
830 *--p = ' ';
831 return p;
832 }
833
834 /* Actually print it.
835
836 Plain and fancy file header block logging. Non-verbose just prints
837 the name, e.g. for "tar t" or "tar x". This should just contain
838 file names, so it can be fed back into tar with xargs or the "-T"
839 option. The verbose option can give a bunch of info, one line per
840 file. I doubt anybody tries to parse its format, or if they do,
841 they shouldn't. Unix tar is pretty random here anyway. */
842
843
844 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
845 HEAD_STANDARD, which must be set up in advance. Not very clean... */
846
847 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
848 columns never shift during the listing. */
849 #define UGSWIDTH 18
850 static int ugswidth = UGSWIDTH; /* maximum width encountered so far */
851
852 /* DATEWIDTH is the number of columns taken by the date and time fields. */
853 #if USE_OLD_CDATE
854 # define DATEWIDTH 19
855 #else
856 # define DATEWIDTH 18
857 #endif
858
859 void
860 print_header (void)
861 {
862 char modes[11];
863 char const *time_stamp;
864 /* These hold formatted ints. */
865 char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND];
866 char *user, *group;
867 char size[2 * UINTMAX_STRSIZE_BOUND];
868 /* holds formatted size or major,minor */
869 char uintbuf[UINTMAX_STRSIZE_BOUND];
870 int pad;
871
872 if (block_number_option)
873 {
874 char buf[UINTMAX_STRSIZE_BOUND];
875 fprintf (stdlis, _("block %s: "),
876 STRINGIFY_BIGINT (current_block_ordinal (), buf));
877 }
878
879 if (verbose_option <= 1)
880 {
881 /* Just the fax, mam. */
882 fprintf (stdlis, "%s\n", quotearg (current_file_name));
883 }
884 else
885 {
886 /* File type and modes. */
887
888 modes[0] = '?';
889 switch (current_header->header.typeflag)
890 {
891 case GNUTYPE_VOLHDR:
892 modes[0] = 'V';
893 break;
894
895 case GNUTYPE_MULTIVOL:
896 modes[0] = 'M';
897 break;
898
899 case GNUTYPE_NAMES:
900 modes[0] = 'N';
901 break;
902
903 case GNUTYPE_LONGNAME:
904 case GNUTYPE_LONGLINK:
905 ERROR ((0, 0, _("Visible longname error")));
906 break;
907
908 case GNUTYPE_SPARSE:
909 case REGTYPE:
910 case AREGTYPE:
911 case LNKTYPE:
912 modes[0] = '-';
913 if (current_file_name[strlen (current_file_name) - 1] == '/')
914 modes[0] = 'd';
915 break;
916 case GNUTYPE_DUMPDIR:
917 modes[0] = 'd';
918 break;
919 case DIRTYPE:
920 modes[0] = 'd';
921 break;
922 case SYMTYPE:
923 modes[0] = 'l';
924 break;
925 case BLKTYPE:
926 modes[0] = 'b';
927 break;
928 case CHRTYPE:
929 modes[0] = 'c';
930 break;
931 case FIFOTYPE:
932 modes[0] = 'p';
933 break;
934 case CONTTYPE:
935 modes[0] = 'C';
936 break;
937 }
938
939 decode_mode (current_stat.st_mode, modes + 1);
940
941 /* Time stamp. */
942
943 time_stamp = tartime (current_stat.st_mtime);
944
945 /* User and group names. */
946
947 if (*current_header->header.uname && current_format != V7_FORMAT
948 && !numeric_owner_option)
949 user = current_header->header.uname;
950 else
951 {
952 /* Try parsing it as an unsigned integer first, and as a
953 uid_t if that fails. This method can list positive user
954 ids that are too large to fit in a uid_t. */
955 uintmax_t u = from_header (current_header->header.uid,
956 sizeof current_header->header.uid, 0,
957 (uintmax_t) 0,
958 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
959 if (u != -1)
960 user = STRINGIFY_BIGINT (u, uform);
961 else
962 {
963 sprintf (uform, "%ld",
964 (long) UID_FROM_HEADER (current_header->header.uid));
965 user = uform;
966 }
967 }
968
969 if (*current_header->header.gname && current_format != V7_FORMAT
970 && !numeric_owner_option)
971 group = current_header->header.gname;
972 else
973 {
974 /* Try parsing it as an unsigned integer first, and as a
975 gid_t if that fails. This method can list positive group
976 ids that are too large to fit in a gid_t. */
977 uintmax_t g = from_header (current_header->header.gid,
978 sizeof current_header->header.gid, 0,
979 (uintmax_t) 0,
980 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
981 if (g != -1)
982 group = STRINGIFY_BIGINT (g, gform);
983 else
984 {
985 sprintf (gform, "%ld",
986 (long) GID_FROM_HEADER (current_header->header.gid));
987 group = gform;
988 }
989 }
990
991 /* Format the file size or major/minor device numbers. */
992
993 switch (current_header->header.typeflag)
994 {
995 case CHRTYPE:
996 case BLKTYPE:
997 strcpy (size,
998 STRINGIFY_BIGINT (major (current_stat.st_rdev), uintbuf));
999 strcat (size, ",");
1000 strcat (size,
1001 STRINGIFY_BIGINT (minor (current_stat.st_rdev), uintbuf));
1002 break;
1003 case GNUTYPE_SPARSE:
1004 strcpy (size,
1005 STRINGIFY_BIGINT
1006 (UINTMAX_FROM_HEADER (current_header
1007 ->oldgnu_header.realsize),
1008 uintbuf));
1009 break;
1010 default:
1011 strcpy (size, STRINGIFY_BIGINT (current_stat.st_size, uintbuf));
1012 break;
1013 }
1014
1015 /* Figure out padding and print the whole line. */
1016
1017 pad = strlen (user) + strlen (group) + strlen (size) + 1;
1018 if (pad > ugswidth)
1019 ugswidth = pad;
1020
1021 fprintf (stdlis, "%s %s/%s %*s%s %s",
1022 modes, user, group, ugswidth - pad, "", size, time_stamp);
1023
1024 fprintf (stdlis, " %s", quotearg (current_file_name));
1025
1026 switch (current_header->header.typeflag)
1027 {
1028 case SYMTYPE:
1029 fprintf (stdlis, " -> %s\n", quotearg (current_link_name));
1030 break;
1031
1032 case LNKTYPE:
1033 fprintf (stdlis, _(" link to %s\n"), quotearg (current_link_name));
1034 break;
1035
1036 default:
1037 {
1038 char type_string[2];
1039 type_string[0] = current_header->header.typeflag;
1040 type_string[1] = '\0';
1041 fprintf (stdlis, _(" unknown file type %s\n"),
1042 quote (type_string));
1043 }
1044 break;
1045
1046 case AREGTYPE:
1047 case REGTYPE:
1048 case GNUTYPE_SPARSE:
1049 case CHRTYPE:
1050 case BLKTYPE:
1051 case DIRTYPE:
1052 case FIFOTYPE:
1053 case CONTTYPE:
1054 case GNUTYPE_DUMPDIR:
1055 putc ('\n', stdlis);
1056 break;
1057
1058 case GNUTYPE_VOLHDR:
1059 fprintf (stdlis, _("--Volume Header--\n"));
1060 break;
1061
1062 case GNUTYPE_MULTIVOL:
1063 strcpy (size,
1064 STRINGIFY_BIGINT
1065 (UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset),
1066 uintbuf));
1067 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1068 break;
1069
1070 case GNUTYPE_NAMES:
1071 fprintf (stdlis, _("--Mangled file names--\n"));
1072 break;
1073 }
1074 }
1075 fflush (stdlis);
1076 }
1077
1078 /* Print a similar line when we make a directory automatically. */
1079 void
1080 print_for_mkdir (char *pathname, int length, mode_t mode)
1081 {
1082 char modes[11];
1083
1084 if (verbose_option > 1)
1085 {
1086 /* File type and modes. */
1087
1088 modes[0] = 'd';
1089 decode_mode (mode, modes + 1);
1090
1091 if (block_number_option)
1092 {
1093 char buf[UINTMAX_STRSIZE_BOUND];
1094 fprintf (stdlis, _("block %s: "),
1095 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1096 }
1097
1098 fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH,
1099 _("Creating directory:"), length, quotearg (pathname));
1100 }
1101 }
1102
1103 /* Skip over SIZE bytes of data in blocks in the archive. */
1104 void
1105 skip_file (off_t size)
1106 {
1107 union block *x;
1108
1109 if (multi_volume_option)
1110 {
1111 save_totsize = size;
1112 save_sizeleft = size;
1113 }
1114
1115 while (size > 0)
1116 {
1117 x = find_next_block ();
1118 if (! x)
1119 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1120
1121 set_next_block_after (x);
1122 size -= BLOCKSIZE;
1123 if (multi_volume_option)
1124 save_sizeleft -= BLOCKSIZE;
1125 }
1126 }
1127
1128 /* Skip the current member in the archive. */
1129 void
1130 skip_member (void)
1131 {
1132 char save_typeflag = current_header->header.typeflag;
1133 set_next_block_after (current_header);
1134
1135 if (current_header->oldgnu_header.isextended)
1136 {
1137 union block *exhdr;
1138 do
1139 {
1140 exhdr = find_next_block ();
1141 if (!exhdr)
1142 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1143 set_next_block_after (exhdr);
1144 }
1145 while (exhdr->sparse_header.isextended);
1146 }
1147
1148 if (save_typeflag != DIRTYPE)
1149 skip_file (current_stat.st_size);
1150 }
This page took 0.083068 seconds and 5 git commands to generate.