]> Dogcows Code - chaz/tar/blob - src/list.c
(decode_header): Initialize st_atime and st_ctime.
[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 stat_info->stat.st_atime = start_time;
506 stat_info->stat.st_ctime = start_time;
507
508 if (format == OLDGNU_FORMAT && incremental_option)
509 {
510 stat_info->stat.st_atime = TIME_FROM_HEADER (header->oldgnu_header.atime);
511 stat_info->stat.st_ctime = TIME_FROM_HEADER (header->oldgnu_header.ctime);
512 }
513
514 if (format == V7_FORMAT)
515 {
516 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
517 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
518 stat_info->stat.st_rdev = 0;
519 }
520 else
521 {
522
523 if (format == STAR_FORMAT)
524 {
525 stat_info->stat.st_atime = TIME_FROM_HEADER (header->star_header.atime);
526 stat_info->stat.st_ctime = TIME_FROM_HEADER (header->star_header.ctime);
527 }
528
529 if (do_user_group)
530 {
531 /* FIXME: Decide if this should somewhat depend on -p. */
532
533 if (numeric_owner_option
534 || !*header->header.uname
535 || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid))
536 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
537
538 if (numeric_owner_option
539 || !*header->header.gname
540 || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid))
541 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
542 }
543
544 switch (header->header.typeflag)
545 {
546 case BLKTYPE:
547 case CHRTYPE:
548 stat_info->stat.st_rdev = makedev (stat_info->devmajor, stat_info->devminor);
549 break;
550
551 default:
552 stat_info->stat.st_rdev = 0;
553 }
554 }
555
556 if (extended_header.size)
557 xheader_decode (stat_info);
558 }
559
560 /* Convert buffer at WHERE0 of size DIGS from external format to
561 uintmax_t. The data is of type TYPE. The buffer must represent a
562 value in the range -MINUS_MINVAL through MAXVAL. DIGS must be
563 positive. Return -1 on error, diagnosing the error if TYPE is
564 nonzero. */
565 static uintmax_t
566 from_header (char const *where0, size_t digs, char const *type,
567 uintmax_t minus_minval, uintmax_t maxval)
568 {
569 uintmax_t value;
570 char const *where = where0;
571 char const *lim = where + digs;
572 int negative = 0;
573
574 /* Accommodate buggy tar of unknown vintage, which outputs leading
575 NUL if the previous field overflows. */
576 where += !*where;
577
578 /* Accommodate older tars, which output leading spaces. */
579 for (;;)
580 {
581 if (where == lim)
582 {
583 if (type)
584 ERROR ((0, 0,
585 _("Blanks in header where numeric %s value expected"),
586 type));
587 return -1;
588 }
589 if (!ISSPACE ((unsigned char) *where))
590 break;
591 where++;
592 }
593
594 value = 0;
595 if (ISODIGIT (*where))
596 {
597 char const *where1 = where;
598 uintmax_t overflow = 0;
599
600 for (;;)
601 {
602 value += *where++ - '0';
603 if (where == lim || ! ISODIGIT (*where))
604 break;
605 overflow |= value ^ (value << LG_8 >> LG_8);
606 value <<= LG_8;
607 }
608
609 /* Parse the output of older, unportable tars, which generate
610 negative values in two's complement octal. If the leading
611 nonzero digit is 1, we can't recover the original value
612 reliably; so do this only if the digit is 2 or more. This
613 catches the common case of 32-bit negative time stamps. */
614 if ((overflow || maxval < value) && '2' <= *where1 && type)
615 {
616 /* Compute the negative of the input value, assuming two's
617 complement. */
618 int digit = (*where1 - '0') | 4;
619 overflow = 0;
620 value = 0;
621 where = where1;
622 for (;;)
623 {
624 value += 7 - digit;
625 where++;
626 if (where == lim || ! ISODIGIT (*where))
627 break;
628 digit = *where - '0';
629 overflow |= value ^ (value << LG_8 >> LG_8);
630 value <<= LG_8;
631 }
632 value++;
633 overflow |= !value;
634
635 if (!overflow && value <= minus_minval)
636 {
637 WARN ((0, 0,
638 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
639 (int) (where - where1), where1, type));
640 negative = 1;
641 }
642 }
643
644 if (overflow)
645 {
646 if (type)
647 ERROR ((0, 0,
648 _("Archive octal value %.*s is out of %s range"),
649 (int) (where - where1), where1, type));
650 return -1;
651 }
652 }
653 else if (*where == '-' || *where == '+')
654 {
655 /* Parse base-64 output produced only by tar test versions
656 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
657 Support for this will be withdrawn in future releases. */
658 int dig;
659 static int warned_once;
660 if (! warned_once)
661 {
662 warned_once = 1;
663 WARN ((0, 0,
664 _("Archive contains obsolescent base-64 headers")));
665 }
666 negative = *where++ == '-';
667 while (where != lim
668 && (dig = base64_map[(unsigned char) *where]) < 64)
669 {
670 if (value << LG_64 >> LG_64 != value)
671 {
672 char *string = alloca (digs + 1);
673 memcpy (string, where0, digs);
674 string[digs] = '\0';
675 if (type)
676 ERROR ((0, 0,
677 _("Archive signed base-64 string %s is out of %s range"),
678 quote (string), type));
679 return -1;
680 }
681 value = (value << LG_64) | dig;
682 where++;
683 }
684 }
685 else if (*where == '\200' /* positive base-256 */
686 || *where == '\377' /* negative base-256 */)
687 {
688 /* Parse base-256 output. A nonnegative number N is
689 represented as (256**DIGS)/2 + N; a negative number -N is
690 represented as (256**DIGS) - N, i.e. as two's complement.
691 The representation guarantees that the leading bit is
692 always on, so that we don't confuse this format with the
693 others (assuming ASCII bytes of 8 bits or more). */
694 int signbit = *where & (1 << (LG_256 - 2));
695 uintmax_t topbits = (((uintmax_t) - signbit)
696 << (CHAR_BIT * sizeof (uintmax_t)
697 - LG_256 - (LG_256 - 2)));
698 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
699 for (;;)
700 {
701 value = (value << LG_256) + (unsigned char) *where++;
702 if (where == lim)
703 break;
704 if (((value << LG_256 >> LG_256) | topbits) != value)
705 {
706 if (type)
707 ERROR ((0, 0,
708 _("Archive base-256 value is out of %s range"),
709 type));
710 return -1;
711 }
712 }
713 negative = signbit;
714 if (negative)
715 value = -value;
716 }
717
718 if (where != lim && *where && !ISSPACE ((unsigned char) *where))
719 {
720 if (type)
721 {
722 char buf[1000]; /* Big enough to represent any header. */
723 static struct quoting_options *o;
724
725 if (!o)
726 {
727 o = clone_quoting_options (0);
728 set_quoting_style (o, locale_quoting_style);
729 }
730
731 while (where0 != lim && ! lim[-1])
732 lim--;
733 quotearg_buffer (buf, sizeof buf, where0, lim - where, o);
734 ERROR ((0, 0,
735 _("Archive contains %.*s where numeric %s value expected"),
736 (int) sizeof buf, buf, type));
737 }
738
739 return -1;
740 }
741
742 if (value <= (negative ? minus_minval : maxval))
743 return negative ? -value : value;
744
745 if (type)
746 {
747 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
748 char maxval_buf[UINTMAX_STRSIZE_BOUND];
749 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
750 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
751 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
752 if (negative)
753 *--value_string = '-';
754 if (minus_minval)
755 *--minval_string = '-';
756 ERROR ((0, 0, _("Archive value %s is out of %s range %s.%s"),
757 value_string, type,
758 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
759 }
760
761 return -1;
762 }
763
764 gid_t
765 gid_from_header (const char *p, size_t s)
766 {
767 return from_header (p, s, "gid_t",
768 - (uintmax_t) TYPE_MINIMUM (gid_t),
769 (uintmax_t) TYPE_MAXIMUM (gid_t));
770 }
771
772 major_t
773 major_from_header (const char *p, size_t s)
774 {
775 return from_header (p, s, "major_t",
776 - (uintmax_t) TYPE_MINIMUM (major_t),
777 (uintmax_t) TYPE_MAXIMUM (major_t));
778 }
779
780 minor_t
781 minor_from_header (const char *p, size_t s)
782 {
783 return from_header (p, s, "minor_t",
784 - (uintmax_t) TYPE_MINIMUM (minor_t),
785 (uintmax_t) TYPE_MAXIMUM (minor_t));
786 }
787
788 mode_t
789 mode_from_header (const char *p, size_t s)
790 {
791 /* Do not complain about unrecognized mode bits. */
792 unsigned u = from_header (p, s, "mode_t",
793 - (uintmax_t) TYPE_MINIMUM (mode_t),
794 TYPE_MAXIMUM (uintmax_t));
795 return ((u & TSUID ? S_ISUID : 0)
796 | (u & TSGID ? S_ISGID : 0)
797 | (u & TSVTX ? S_ISVTX : 0)
798 | (u & TUREAD ? S_IRUSR : 0)
799 | (u & TUWRITE ? S_IWUSR : 0)
800 | (u & TUEXEC ? S_IXUSR : 0)
801 | (u & TGREAD ? S_IRGRP : 0)
802 | (u & TGWRITE ? S_IWGRP : 0)
803 | (u & TGEXEC ? S_IXGRP : 0)
804 | (u & TOREAD ? S_IROTH : 0)
805 | (u & TOWRITE ? S_IWOTH : 0)
806 | (u & TOEXEC ? S_IXOTH : 0));
807 }
808
809 off_t
810 off_from_header (const char *p, size_t s)
811 {
812 /* Negative offsets are not allowed in tar files, so invoke
813 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
814 return from_header (p, s, "off_t", (uintmax_t) 0,
815 (uintmax_t) TYPE_MAXIMUM (off_t));
816 }
817
818 size_t
819 size_from_header (const char *p, size_t s)
820 {
821 return from_header (p, s, "size_t", (uintmax_t) 0,
822 (uintmax_t) TYPE_MAXIMUM (size_t));
823 }
824
825 time_t
826 time_from_header (const char *p, size_t s)
827 {
828 return from_header (p, s, "time_t",
829 - (uintmax_t) TYPE_MINIMUM (time_t),
830 (uintmax_t) TYPE_MAXIMUM (time_t));
831 }
832
833 uid_t
834 uid_from_header (const char *p, size_t s)
835 {
836 return from_header (p, s, "uid_t",
837 - (uintmax_t) TYPE_MINIMUM (uid_t),
838 (uintmax_t) TYPE_MAXIMUM (uid_t));
839 }
840
841 uintmax_t
842 uintmax_from_header (const char *p, size_t s)
843 {
844 return from_header (p, s, "uintmax_t", (uintmax_t) 0,
845 TYPE_MAXIMUM (uintmax_t));
846 }
847
848
849 /* Format O as a null-terminated decimal string into BUF _backwards_;
850 return pointer to start of result. */
851 char *
852 stringify_uintmax_t_backwards (uintmax_t o, char *buf)
853 {
854 *--buf = '\0';
855 do
856 *--buf = '0' + (int) (o % 10);
857 while ((o /= 10) != 0);
858 return buf;
859 }
860
861 /* Return a printable representation of T. The result points to
862 static storage that can be reused in the next call to this
863 function, to ctime, or to asctime. */
864 char const *
865 tartime (time_t t)
866 {
867 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
868 INT_STRLEN_BOUND (int) + 16)];
869 char *p;
870
871 #if USE_OLD_CTIME
872 p = ctime (&t);
873 if (p)
874 {
875 char const *time_stamp = p + 4;
876 for (p += 16; p[3] != '\n'; p++)
877 p[0] = p[3];
878 p[0] = '\0';
879 return time_stamp;
880 }
881 #else
882 /* Use ISO 8610 format. See:
883 http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
884 struct tm *tm = localtime (&t);
885 if (tm)
886 {
887 sprintf (buffer, "%04ld-%02d-%02d %02d:%02d:%02d",
888 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
889 tm->tm_hour, tm->tm_min, tm->tm_sec);
890 return buffer;
891 }
892 #endif
893
894 /* The time stamp cannot be broken down, most likely because it
895 is out of range. Convert it as an integer,
896 right-adjusted in a field with the same width as the usual
897 19-byte 4-year ISO time format. */
898 p = stringify_uintmax_t_backwards (t < 0 ? - (uintmax_t) t : (uintmax_t) t,
899 buffer + sizeof buffer);
900 if (t < 0)
901 *--p = '-';
902 while (buffer + sizeof buffer - 19 - 1 < p)
903 *--p = ' ';
904 return p;
905 }
906
907 /* Actually print it.
908
909 Plain and fancy file header block logging. Non-verbose just prints
910 the name, e.g. for "tar t" or "tar x". This should just contain
911 file names, so it can be fed back into tar with xargs or the "-T"
912 option. The verbose option can give a bunch of info, one line per
913 file. I doubt anybody tries to parse its format, or if they do,
914 they shouldn't. Unix tar is pretty random here anyway. */
915
916
917 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
918 HEAD_STANDARD, which must be set up in advance. Not very clean.. */
919
920 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
921 columns never shift during the listing. */
922 #define UGSWIDTH 18
923 static int ugswidth = UGSWIDTH; /* maximum width encountered so far */
924
925 /* DATEWIDTH is the number of columns taken by the date and time fields. */
926 #if USE_OLD_CDATE
927 # define DATEWIDTH 19
928 #else
929 # define DATEWIDTH 18
930 #endif
931
932 void
933 print_header (off_t block_ordinal)
934 {
935 char modes[11];
936 char const *time_stamp;
937 char *temp_name = current_stat_info.orig_file_name ? current_stat_info.orig_file_name : current_stat_info.file_name;
938
939 /* These hold formatted ints. */
940 char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND];
941 char *user, *group;
942 char size[2 * UINTMAX_STRSIZE_BOUND];
943 /* holds formatted size or major,minor */
944 char uintbuf[UINTMAX_STRSIZE_BOUND];
945 int pad;
946
947 if (block_number_option)
948 {
949 char buf[UINTMAX_STRSIZE_BOUND];
950 if (block_ordinal < 0)
951 block_ordinal = current_block_ordinal ();
952 block_ordinal -= recent_long_name_blocks;
953 block_ordinal -= recent_long_link_blocks;
954 fprintf (stdlis, _("block %s: "),
955 STRINGIFY_BIGINT (block_ordinal, buf));
956 }
957
958 if (verbose_option <= 1)
959 {
960 /* Just the fax, mam. */
961 fprintf (stdlis, "%s\n", quotearg (temp_name));
962 }
963 else
964 {
965 /* File type and modes. */
966
967 modes[0] = '?';
968 switch (current_header->header.typeflag)
969 {
970 case GNUTYPE_VOLHDR:
971 modes[0] = 'V';
972 break;
973
974 case GNUTYPE_MULTIVOL:
975 modes[0] = 'M';
976 break;
977
978 case GNUTYPE_NAMES:
979 modes[0] = 'N';
980 break;
981
982 case GNUTYPE_LONGNAME:
983 case GNUTYPE_LONGLINK:
984 modes[0] = 'L';
985 ERROR ((0, 0, _("Visible longname error")));
986 break;
987
988 case GNUTYPE_SPARSE:
989 case REGTYPE:
990 case AREGTYPE:
991 modes[0] = '-';
992 if (temp_name[strlen (temp_name) - 1] == '/')
993 modes[0] = 'd';
994 break;
995 case LNKTYPE:
996 modes[0] = 'h';
997 break;
998 case GNUTYPE_DUMPDIR:
999 modes[0] = 'd';
1000 break;
1001 case DIRTYPE:
1002 modes[0] = 'd';
1003 break;
1004 case SYMTYPE:
1005 modes[0] = 'l';
1006 break;
1007 case BLKTYPE:
1008 modes[0] = 'b';
1009 break;
1010 case CHRTYPE:
1011 modes[0] = 'c';
1012 break;
1013 case FIFOTYPE:
1014 modes[0] = 'p';
1015 break;
1016 case CONTTYPE:
1017 modes[0] = 'C';
1018 break;
1019 }
1020
1021 decode_mode (current_stat_info.stat.st_mode, modes + 1);
1022
1023 /* Time stamp. */
1024
1025 time_stamp = tartime (current_stat_info.stat.st_mtime);
1026
1027 /* User and group names. */
1028
1029 if (current_stat_info.uname && current_format != V7_FORMAT
1030 && !numeric_owner_option)
1031 user = current_stat_info.uname;
1032 else
1033 {
1034 /* Try parsing it as an unsigned integer first, and as a
1035 uid_t if that fails. This method can list positive user
1036 ids that are too large to fit in a uid_t. */
1037 uintmax_t u = from_header (current_header->header.uid,
1038 sizeof current_header->header.uid, 0,
1039 (uintmax_t) 0,
1040 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1041 if (u != -1)
1042 user = STRINGIFY_BIGINT (u, uform);
1043 else
1044 {
1045 sprintf (uform, "%ld",
1046 (long) UID_FROM_HEADER (current_header->header.uid));
1047 user = uform;
1048 }
1049 }
1050
1051 if (current_stat_info.gname && current_format != V7_FORMAT
1052 && !numeric_owner_option)
1053 group = current_stat_info.gname;
1054 else
1055 {
1056 /* Try parsing it as an unsigned integer first, and as a
1057 gid_t if that fails. This method can list positive group
1058 ids that are too large to fit in a gid_t. */
1059 uintmax_t g = from_header (current_header->header.gid,
1060 sizeof current_header->header.gid, 0,
1061 (uintmax_t) 0,
1062 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1063 if (g != -1)
1064 group = STRINGIFY_BIGINT (g, gform);
1065 else
1066 {
1067 sprintf (gform, "%ld",
1068 (long) GID_FROM_HEADER (current_header->header.gid));
1069 group = gform;
1070 }
1071 }
1072
1073 /* Format the file size or major/minor device numbers. */
1074
1075 switch (current_header->header.typeflag)
1076 {
1077 case CHRTYPE:
1078 case BLKTYPE:
1079 strcpy (size,
1080 STRINGIFY_BIGINT (major (current_stat_info.stat.st_rdev), uintbuf));
1081 strcat (size, ",");
1082 strcat (size,
1083 STRINGIFY_BIGINT (minor (current_stat_info.stat.st_rdev), uintbuf));
1084 break;
1085 case GNUTYPE_SPARSE:
1086 strcpy (size,
1087 STRINGIFY_BIGINT
1088 (UINTMAX_FROM_HEADER (current_header
1089 ->oldgnu_header.realsize),
1090 uintbuf));
1091 break;
1092 default:
1093 strcpy (size, STRINGIFY_BIGINT (current_stat_info.stat.st_size, uintbuf));
1094 break;
1095 }
1096
1097 /* Figure out padding and print the whole line. */
1098
1099 pad = strlen (user) + strlen (group) + strlen (size) + 1;
1100 if (pad > ugswidth)
1101 ugswidth = pad;
1102
1103 fprintf (stdlis, "%s %s/%s %*s%s %s",
1104 modes, user, group, ugswidth - pad, "", size, time_stamp);
1105
1106 fprintf (stdlis, " %s", quotearg (temp_name));
1107
1108 switch (current_header->header.typeflag)
1109 {
1110 case SYMTYPE:
1111 fprintf (stdlis, " -> %s\n", quotearg (current_stat_info.link_name));
1112 break;
1113
1114 case LNKTYPE:
1115 fprintf (stdlis, _(" link to %s\n"), quotearg (current_stat_info.link_name));
1116 break;
1117
1118 default:
1119 {
1120 char type_string[2];
1121 type_string[0] = current_header->header.typeflag;
1122 type_string[1] = '\0';
1123 fprintf (stdlis, _(" unknown file type %s\n"),
1124 quote (type_string));
1125 }
1126 break;
1127
1128 case AREGTYPE:
1129 case REGTYPE:
1130 case GNUTYPE_SPARSE:
1131 case CHRTYPE:
1132 case BLKTYPE:
1133 case DIRTYPE:
1134 case FIFOTYPE:
1135 case CONTTYPE:
1136 case GNUTYPE_DUMPDIR:
1137 putc ('\n', stdlis);
1138 break;
1139
1140 case GNUTYPE_LONGLINK:
1141 fprintf (stdlis, _("--Long Link--\n"));
1142 break;
1143
1144 case GNUTYPE_LONGNAME:
1145 fprintf (stdlis, _("--Long Name--\n"));
1146 break;
1147
1148 case GNUTYPE_VOLHDR:
1149 fprintf (stdlis, _("--Volume Header--\n"));
1150 break;
1151
1152 case GNUTYPE_MULTIVOL:
1153 strcpy (size,
1154 STRINGIFY_BIGINT
1155 (UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset),
1156 uintbuf));
1157 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1158 break;
1159
1160 case GNUTYPE_NAMES:
1161 fprintf (stdlis, _("--Mangled file names--\n"));
1162 break;
1163 }
1164 }
1165 fflush (stdlis);
1166 }
1167
1168 /* Print a similar line when we make a directory automatically. */
1169 void
1170 print_for_mkdir (char *pathname, int length, mode_t mode)
1171 {
1172 char modes[11];
1173
1174 if (verbose_option > 1)
1175 {
1176 /* File type and modes. */
1177
1178 modes[0] = 'd';
1179 decode_mode (mode, modes + 1);
1180
1181 if (block_number_option)
1182 {
1183 char buf[UINTMAX_STRSIZE_BOUND];
1184 fprintf (stdlis, _("block %s: "),
1185 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1186 }
1187
1188 fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH,
1189 _("Creating directory:"), length, quotearg (pathname));
1190 }
1191 }
1192
1193 /* Skip over SIZE bytes of data in blocks in the archive. */
1194 void
1195 skip_file (off_t size)
1196 {
1197 union block *x;
1198
1199 if (multi_volume_option)
1200 {
1201 save_totsize = size;
1202 save_sizeleft = size;
1203 }
1204
1205 while (size > 0)
1206 {
1207 x = find_next_block ();
1208 if (! x)
1209 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1210
1211 set_next_block_after (x);
1212 size -= BLOCKSIZE;
1213 if (multi_volume_option)
1214 save_sizeleft -= BLOCKSIZE;
1215 }
1216 }
1217
1218 /* Skip the current member in the archive. */
1219 void
1220 skip_member (void)
1221 {
1222 char save_typeflag = current_header->header.typeflag;
1223 set_next_block_after (current_header);
1224
1225 if (current_format == OLDGNU_FORMAT
1226 && current_header->oldgnu_header.isextended)
1227 {
1228 union block *exhdr;
1229 do
1230 {
1231 exhdr = find_next_block ();
1232 if (!exhdr)
1233 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1234 set_next_block_after (exhdr);
1235 }
1236 while (exhdr->sparse_header.isextended);
1237 }
1238
1239 if (save_typeflag != DIRTYPE)
1240 skip_file (current_stat_info.stat.st_size);
1241 }
This page took 0.09061 seconds and 5 git commands to generate.