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