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