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