1 /* List a tar archive, with support routines for reading a tar archive.
2 Copyright (C) 1988, 92, 93, 94, 96, 97 Free Software Foundation, Inc.
3 Written by John Gilmore, on 1985-08-26.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Define to non-zero for forcing old ctime() instead of isotime(). */
27 #define ISODIGIT(Char) \
28 ((unsigned char) (Char) >= '0' && (unsigned char) (Char) <= '7')
29 #define ISSPACE(Char) (ISASCII (Char) && isspace (Char))
33 union block
*current_header
; /* points to current archive header */
34 struct stat current_stat
; /* stat struct corresponding */
35 enum archive_format current_format
; /* recognized format */
37 /*-----------------------------------.
38 | Main loop for reading an archive. |
39 `-----------------------------------*/
42 read_and (void (*do_something
) ())
44 enum read_header status
= HEADER_STILL_UNREAD
;
45 enum read_header prev_status
;
49 open_archive (ACCESS_READ
);
54 status
= read_header ();
57 case HEADER_STILL_UNREAD
:
62 /* Valid header. We should decode next field (mode) first.
63 Ensure incoming names are null terminated. */
65 /* FIXME: This is a quick kludge before 1.12 goes out. */
67 = from_oct (1 + 12, current_header
->header
.mtime
);
69 if (!name_match (current_file_name
)
70 || current_stat
.st_mtime
< newer_mtime_option
71 || (exclude_option
&& check_exclude (current_file_name
)))
75 if (current_header
->header
.typeflag
== GNUTYPE_VOLHDR
76 || current_header
->header
.typeflag
== GNUTYPE_MULTIVOL
77 || current_header
->header
.typeflag
== GNUTYPE_NAMES
)
82 if (show_omitted_dirs_option
83 && current_header
->header
.typeflag
== DIRTYPE
)
84 WARN ((0, 0, _("Omitting %s"), current_file_name
));
86 /* Skip past it in the archive. */
88 if (current_header
->oldgnu_header
.isextended
)
90 save_typeflag
= current_header
->header
.typeflag
;
91 set_next_block_after (current_header
);
99 exhdr
= find_next_block ();
100 if (!exhdr
->sparse_header
.isextended
)
102 set_next_block_after (exhdr
);
106 set_next_block_after (exhdr
);
108 skip_extended_headers ();
111 /* Skip to the next header on the archive. */
113 if (save_typeflag
!= DIRTYPE
)
114 skip_file ((long) current_stat
.st_size
);
121 case HEADER_ZERO_BLOCK
:
122 if (block_number_option
)
123 fprintf (stdlis
, _("block %10ld: ** Block of NULs **\n"),
124 current_block_ordinal ());
126 set_next_block_after (current_header
);
127 status
= prev_status
;
128 if (ignore_zeros_option
)
132 case HEADER_END_OF_FILE
:
133 if (block_number_option
)
134 fprintf (stdlis
, _("block %10ld: ** End of File **\n"),
135 current_block_ordinal ());
139 /* If the previous header was good, tell them that we are
140 skipping bad ones. */
141 set_next_block_after (current_header
);
144 case HEADER_STILL_UNREAD
:
145 WARN ((0, 0, _("Hmm, this doesn't look like a tar archive")));
148 case HEADER_ZERO_BLOCK
:
150 WARN ((0, 0, _("Skipping to next file header")));
153 case HEADER_END_OF_FILE
:
155 /* We are in the middle of a cascade of errors. */
163 apply_delayed_set_stat ();
165 names_notfound (); /* print names not found */
168 /*---------------------------------------------.
169 | Print a header block, based on tar options. |
170 `---------------------------------------------*/
175 int isextended
= 0; /* to remember if current_header is extended */
177 /* Print the header block. */
181 if (verbose_option
> 1)
182 decode_header (current_header
, ¤t_stat
, ¤t_format
, 0);
186 if (incremental_option
&& current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
188 size_t size
, written
, check
;
189 union block
*data_block
;
191 set_next_block_after (current_header
);
192 if (multi_volume_option
)
194 assign_string (&save_name
, current_file_name
);
195 save_totsize
= current_stat
.st_size
;
197 for (size
= current_stat
.st_size
; size
> 0; size
-= written
)
199 if (multi_volume_option
)
200 save_sizeleft
= size
;
201 data_block
= find_next_block ();
204 ERROR ((0, 0, _("EOF in archive file")));
205 break; /* FIXME: What happens, then? */
207 written
= available_space_after (data_block
);
210 errno
= 0; /* FIXME: errno should be read-only */
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
)
216 ERROR ((0, errno
, _("Only wrote %ld of %ld bytes to file %s"),
217 check
, written
, current_file_name
));
218 skip_file ((long) (size
- written
));
222 if (multi_volume_option
)
223 assign_string (&save_name
, NULL
);
224 fputc ('\n', stdlis
);
230 /* Check to see if we have an extended header to skip over also. */
232 if (current_header
->oldgnu_header
.isextended
)
235 /* Skip past the header in the archive. */
237 set_next_block_after (current_header
);
239 /* If we needed to skip any extended headers, do so now, by reading
240 extended headers and skipping past them in the archive. */
249 exhdr
= find_next_block ();
251 if (!exhdr
->sparse_header
.isextended
)
253 set_next_block_after (exhdr
);
256 set_next_block_after (exhdr
);
259 skip_extended_headers ();
262 if (multi_volume_option
)
263 assign_string (&save_name
, current_file_name
);
265 /* Skip to the next header on the archive. */
267 skip_file ((long) current_stat
.st_size
);
269 if (multi_volume_option
)
270 assign_string (&save_name
, NULL
);
273 /*-----------------------------------------------------------------------.
274 | Read a block that's supposed to be a header block. Return its address |
275 | in "current_header", and if it is good, the file's size in |
276 | current_stat.st_size. |
278 | Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a |
279 | block full of zeros (EOF marker). |
281 | You must always set_next_block_after(current_header) to skip past the |
282 | header which this routine reads. |
283 `-----------------------------------------------------------------------*/
285 /* The standard BSD tar sources create the checksum by adding up the
286 bytes in the header as type char. I think the type char was unsigned
287 on the PDP-11, but it's signed on the Next and Sun. It looks like the
288 sources to BSD tar were never changed to compute the checksum
289 currectly, so both the Sun and Next add the bytes of the header as
290 signed chars. This doesn't cause a problem until you get a file with
291 a name containing characters with the high bit set. So read_header
292 computes two checksums -- signed and unsigned. */
294 /* FIXME: The signed checksum computation is broken on machines where char's
295 are unsigned. It's uneasy to handle all cases correctly... */
301 long unsigned_sum
; /* the POSIX one :-) */
302 long signed_sum
; /* the Sun one :-( */
308 union block
*data_block
;
310 static char *next_long_name
, *next_long_link
;
314 header
= find_next_block ();
315 current_header
= header
;
317 return HEADER_END_OF_FILE
;
320 = from_oct (sizeof header
->header
.chksum
, header
->header
.chksum
);
325 for (i
= sizeof (*header
); --i
>= 0;)
327 /* We can't use unsigned char here because of old compilers,
330 unsigned_sum
+= 0xFF & *p
;
334 /* Adjust checksum to count the "chksum" field as blanks. */
336 for (i
= sizeof (header
->header
.chksum
); --i
>= 0;)
338 unsigned_sum
-= 0xFF & header
->header
.chksum
[i
];
339 signed_sum
-= header
->header
.chksum
[i
];
341 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
342 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
344 if (unsigned_sum
== sizeof header
->header
.chksum
* ' ')
346 /* This is a zeroed block...whole block is 0's except for the
347 blanks we faked for the checksum field. */
349 return HEADER_ZERO_BLOCK
;
352 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
353 return HEADER_FAILURE
;
355 /* Good block. Decode file size and return. */
357 if (header
->header
.typeflag
== LNKTYPE
)
358 current_stat
.st_size
= 0; /* links 0 size on tape */
360 current_stat
.st_size
= from_oct (1 + 12, header
->header
.size
);
362 header
->header
.name
[NAME_FIELD_SIZE
- 1] = '\0';
363 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
364 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
366 longp
= ((header
->header
.typeflag
== GNUTYPE_LONGNAME
)
370 set_next_block_after (header
);
373 bp
= *longp
= (char *) xmalloc ((size_t) current_stat
.st_size
);
375 for (size
= current_stat
.st_size
; size
> 0; size
-= written
)
377 data_block
= find_next_block ();
378 if (data_block
== NULL
)
380 ERROR ((0, 0, _("Unexpected EOF on archive file")));
383 written
= available_space_after (data_block
);
387 memcpy (bp
, data_block
->buffer
, (size_t) written
);
389 set_next_block_after ((union block
*)
390 (data_block
->buffer
+ written
- 1));
398 assign_string (¤t_file_name
,
399 (next_long_name
? next_long_name
400 : current_header
->header
.name
));
401 assign_string (¤t_link_name
,
402 (next_long_link
? next_long_link
403 : current_header
->header
.linkname
));
404 next_long_link
= next_long_name
= 0;
405 return HEADER_SUCCESS
;
410 /*-------------------------------------------------------------------------.
411 | Decode things from a file HEADER block into STAT_INFO, also setting |
412 | *FORMAT_POINTER depending on the header block format. If DO_USER_GROUP, |
413 | decode the user/group information (this is useful for extraction, but |
414 | waste time when merely listing). |
416 | read_header() has already decoded the checksum and length, so we don't. |
418 | This routine should *not* be called twice for the same block, since the |
419 | two calls might use different DO_USER_GROUP values and thus might end up |
420 | with different uid/gid for the two calls. If anybody wants the uid/gid |
421 | they should decode it first, and other callers should decode it without |
422 | uid/gid before calling a routine, e.g. print_header, that assumes |
424 `-------------------------------------------------------------------------*/
427 decode_header (union block
*header
, struct stat
*stat_info
,
428 enum archive_format
*format_pointer
, int do_user_group
)
430 enum archive_format format
;
432 if (strcmp (header
->header
.magic
, TMAGIC
) == 0)
433 format
= POSIX_FORMAT
;
434 else if (strcmp (header
->header
.magic
, OLDGNU_MAGIC
) == 0)
435 format
= OLDGNU_FORMAT
;
438 *format_pointer
= format
;
440 stat_info
->st_mode
= from_oct (8, header
->header
.mode
);
441 stat_info
->st_mode
&= 07777;
442 stat_info
->st_mtime
= from_oct (1 + 12, header
->header
.mtime
);
444 if (format
== OLDGNU_FORMAT
&& incremental_option
)
446 stat_info
->st_atime
= from_oct (1 + 12, header
->oldgnu_header
.atime
);
447 stat_info
->st_ctime
= from_oct (1 + 12, header
->oldgnu_header
.ctime
);
450 if (format
== V7_FORMAT
)
452 stat_info
->st_uid
= from_oct (8, header
->header
.uid
);
453 stat_info
->st_gid
= from_oct (8, header
->header
.gid
);
454 stat_info
->st_rdev
= 0;
460 /* FIXME: Decide if this should somewhat depend on -p. */
462 if (numeric_owner_option
463 || !*header
->header
.uname
464 || !uname_to_uid (header
->header
.uname
, &stat_info
->st_uid
))
465 stat_info
->st_uid
= from_oct (8, header
->header
.uid
);
467 if (numeric_owner_option
468 || !*header
->header
.gname
469 || !gname_to_gid (header
->header
.gname
, &stat_info
->st_gid
))
470 stat_info
->st_gid
= from_oct (8, header
->header
.gid
);
472 switch (header
->header
.typeflag
)
476 stat_info
->st_rdev
= makedev (from_oct (8, header
->header
.devmajor
),
477 from_oct (8, header
->header
.devminor
));
483 stat_info
->st_rdev
= makedev (from_oct (8, header
->header
.devmajor
),
484 from_oct (8, header
->header
.devminor
));
489 stat_info
->st_rdev
= 0;
494 /*------------------------------------------------------------------------.
495 | Quick and dirty octal conversion. Result is -1 if the field is invalid |
496 | (all blank, or nonoctal). |
497 `------------------------------------------------------------------------*/
500 from_oct (int digs
, char *where
)
504 while (ISSPACE (*where
))
508 return -1; /* all blank field */
511 while (digs
> 0 && ISODIGIT (*where
))
513 /* Scan til nonoctal. */
515 value
= (value
<< 3) | (*where
++ - '0');
519 if (digs
> 0 && *where
&& !ISSPACE (*where
))
520 return -1; /* ended on non-space/nul */
527 /*-------------------------------------------.
528 | Return the time formatted along ISO 8601. |
529 `-------------------------------------------*/
531 /* Also, see http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html. */
534 isotime (const time_t *time
)
536 static char buffer
[21];
539 tm
= localtime (time
);
540 sprintf (buffer
, "%4d-%02d-%02d %02d:%02d:%02d\n",
541 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
542 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
546 #endif /* not USE_OLD_CTIME */
548 /*-------------------------------------------------------------------------.
549 | Decode MODE from its binary form in a stat structure, and encode it into |
550 | a 9 characters string STRING, terminated with a NUL. |
551 `-------------------------------------------------------------------------*/
554 decode_mode (unsigned mode
, char *string
)
557 const char *rwx
= "rwxrwxrwx";
559 for (mask
= 0400; mask
!= 0; mask
>>= 1)
569 string
[-7] = string
[-7] == 'x' ? 's' : 'S';
571 string
[-4] = string
[-4] == 'x' ? 's' : 'S';
573 string
[-1] = string
[-1] == 'x' ? 't' : 'T';
578 /*-------------------------------------------------------------------------.
579 | Actually print it. |
581 | Plain and fancy file header block logging. Non-verbose just prints the |
582 | name, e.g. for "tar t" or "tar x". This should just contain file names, |
583 | so it can be fed back into tar with xargs or the "-T" option. The |
584 | verbose option can give a bunch of info, one line per file. I doubt |
585 | anybody tries to parse its format, or if they do, they shouldn't. Unix |
586 | tar is pretty random here anyway. |
587 `-------------------------------------------------------------------------*/
589 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
590 HEAD_STANDARD, which must be set up in advance. Not very clean... */
592 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
593 columns never shift during the listing. */
595 static int ugswidth
= UGSWIDTH
; /* maximum width encountered so far */
597 /* DATEWIDTH is the number of columns taken by the date and time fields. */
599 # define DATEWIDTH 19
601 # define DATEWIDTH 18
609 char uform
[11], gform
[11]; /* these hold formatted ints */
611 char size
[24]; /* holds a formatted long or maj, min */
612 time_t longie
; /* to make ctime() call portable */
616 if (block_number_option
)
617 fprintf (stdlis
, _("block %10ld: "), current_block_ordinal ());
619 if (verbose_option
<= 1)
621 /* Just the fax, mam. */
623 char *quoted_name
= quote_copy_string (current_file_name
);
627 fprintf (stdlis
, "%s\n", quoted_name
);
631 fprintf (stdlis
, "%s\n", current_file_name
);
635 /* File type and modes. */
638 switch (current_header
->header
.typeflag
)
644 case GNUTYPE_MULTIVOL
:
652 case GNUTYPE_LONGNAME
:
653 case GNUTYPE_LONGLINK
:
654 ERROR ((0, 0, _("Visible longname error")));
662 if (current_file_name
[strlen (current_file_name
) - 1] == '/')
665 case GNUTYPE_DUMPDIR
:
688 decode_mode ((unsigned) current_stat
.st_mode
, modes
+ 1);
692 longie
= current_stat
.st_mtime
;
694 timestamp
= ctime (&longie
);
695 timestamp
[16] = '\0';
696 timestamp
[24] = '\0';
698 timestamp
= isotime (&longie
);
699 timestamp
[16] = '\0';
702 /* User and group names. */
704 if (*current_header
->header
.uname
&& current_format
!= V7_FORMAT
)
705 user
= current_header
->header
.uname
;
709 sprintf (uform
, "%ld", from_oct (8, current_header
->header
.uid
));
712 if (*current_header
->header
.gname
&& current_format
!= V7_FORMAT
)
713 group
= current_header
->header
.gname
;
717 sprintf (gform
, "%ld", from_oct (8, current_header
->header
.gid
));
720 /* Format the file size or major/minor device numbers. */
722 switch (current_header
->header
.typeflag
)
724 #if defined(S_IFBLK) || defined(S_IFCHR)
727 sprintf (size
, "%d,%d",
728 major (current_stat
.st_rdev
), minor (current_stat
.st_rdev
));
732 sprintf (size
, "%ld",
733 from_oct (1 + 12, current_header
->oldgnu_header
.realsize
));
736 sprintf (size
, "%ld", (long) current_stat
.st_size
);
739 /* Figure out padding and print the whole line. */
741 pad
= strlen (user
) + strlen (group
) + strlen (size
) + 1;
746 fprintf (stdlis
, "%s %s/%s %*s%s %s %s",
747 modes
, user
, group
, ugswidth
- pad
, "",
748 size
, timestamp
+ 4, timestamp
+ 20);
750 fprintf (stdlis
, "%s %s/%s %*s%s %s",
751 modes
, user
, group
, ugswidth
- pad
, "", size
, timestamp
);
754 name
= quote_copy_string (current_file_name
);
757 fprintf (stdlis
, " %s", name
);
761 fprintf (stdlis
, " %s", current_file_name
);
763 switch (current_header
->header
.typeflag
)
766 name
= quote_copy_string (current_link_name
);
769 fprintf (stdlis
, " -> %s\n", name
);
773 fprintf (stdlis
, " -> %s\n", current_link_name
);
777 name
= quote_copy_string (current_link_name
);
780 fprintf (stdlis
, _(" link to %s\n"), name
);
784 fprintf (stdlis
, _(" link to %s\n"), current_link_name
);
788 fprintf (stdlis
, _(" unknown file type `%c'\n"),
789 current_header
->header
.typeflag
);
800 case GNUTYPE_DUMPDIR
:
805 fprintf (stdlis
, _("--Volume Header--\n"));
808 case GNUTYPE_MULTIVOL
:
809 fprintf (stdlis
, _("--Continued at byte %ld--\n"),
810 from_oct (1 + 12, current_header
->oldgnu_header
.offset
));
814 fprintf (stdlis
, _("--Mangled file names--\n"));
821 /*--------------------------------------------------------------.
822 | Print a similar line when we make a directory automatically. |
823 `--------------------------------------------------------------*/
826 print_for_mkdir (char *pathname
, int length
, int mode
)
831 if (verbose_option
> 1)
833 /* File type and modes. */
836 decode_mode ((unsigned) mode
, modes
+ 1);
838 if (block_number_option
)
839 fprintf (stdlis
, _("block %10ld: "), current_block_ordinal ());
840 name
= quote_copy_string (pathname
);
843 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
844 _("Creating directory:"), length
, name
);
848 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
849 _("Creating directory:"), length
, pathname
);
853 /*--------------------------------------------------------.
854 | Skip over SIZE bytes of data in blocks in the archive. |
855 `--------------------------------------------------------*/
858 skip_file (long size
)
862 if (multi_volume_option
)
865 save_sizeleft
= size
;
870 x
= find_next_block ();
872 FATAL_ERROR ((0, 0, _("Unexpected EOF on archive file")));
874 set_next_block_after (x
);
876 if (multi_volume_option
)
877 save_sizeleft
-= BLOCKSIZE
;
886 skip_extended_headers (void)
892 exhdr
= find_next_block ();
893 if (!exhdr
->sparse_header
.isextended
)
895 set_next_block_after (exhdr
);
898 set_next_block_after (exhdr
);