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