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