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))
31 #ifndef FNM_LEADING_DIR
37 union block
*current_header
; /* points to current archive header */
38 struct stat current_stat
; /* stat struct corresponding */
39 enum archive_format current_format
; /* recognized format */
41 /*-----------------------------------.
42 | Main loop for reading an archive. |
43 `-----------------------------------*/
46 read_and (void (*do_something
) ())
48 enum read_header status
= HEADER_STILL_UNREAD
;
49 enum read_header prev_status
;
53 open_archive (ACCESS_READ
);
58 status
= read_header ();
61 case HEADER_STILL_UNREAD
:
66 /* Valid header. We should decode next field (mode) first.
67 Ensure incoming names are null terminated. */
69 if (ending_file_option
&&
70 fnmatch (ending_file_option
, current_file_name
,
71 FNM_LEADING_DIR
) == 0) {
75 /* FIXME: This is a quick kludge before 1.12 goes out. */
76 current_stat
.st_mtime
= TIME_FROM_OCT (current_header
->header
.mtime
);
78 if (!name_match (current_file_name
)
79 || current_stat
.st_mtime
< newer_mtime_option
80 || (exclude_option
&& check_exclude (current_file_name
)))
84 if (current_header
->header
.typeflag
== GNUTYPE_VOLHDR
85 || current_header
->header
.typeflag
== GNUTYPE_MULTIVOL
86 || current_header
->header
.typeflag
== GNUTYPE_NAMES
)
91 if (show_omitted_dirs_option
92 && current_header
->header
.typeflag
== DIRTYPE
)
93 WARN ((0, 0, _("Omitting %s"), current_file_name
));
95 /* Skip past it in the archive. */
97 if (current_header
->oldgnu_header
.isextended
)
99 save_typeflag
= current_header
->header
.typeflag
;
100 set_next_block_after (current_header
);
108 exhdr
= find_next_block ();
109 if (!exhdr
->sparse_header
.isextended
)
111 set_next_block_after (exhdr
);
115 set_next_block_after (exhdr
);
117 skip_extended_headers ();
120 /* Skip to the next header on the archive. */
122 if (save_typeflag
!= DIRTYPE
)
123 skip_file (current_stat
.st_size
);
130 case HEADER_ZERO_BLOCK
:
131 if (block_number_option
)
133 char buf
[UINTMAX_STRSIZE_BOUND
];
134 fprintf (stdlis
, _("block %s: ** Block of NULs **\n"),
135 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
138 set_next_block_after (current_header
);
139 status
= prev_status
;
140 if (ignore_zeros_option
)
144 case HEADER_END_OF_FILE
:
145 if (block_number_option
)
147 char buf
[UINTMAX_STRSIZE_BOUND
];
148 fprintf (stdlis
, _("block %s: ** End of File **\n"),
149 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
154 /* If the previous header was good, tell them that we are
155 skipping bad ones. */
156 set_next_block_after (current_header
);
159 case HEADER_STILL_UNREAD
:
160 WARN ((0, 0, _("Hmm, this doesn't look like a tar archive")));
163 case HEADER_ZERO_BLOCK
:
165 WARN ((0, 0, _("Skipping to next file header")));
168 case HEADER_END_OF_FILE
:
170 /* We are in the middle of a cascade of errors. */
179 apply_delayed_set_stat ();
181 names_notfound (); /* print names not found */
184 /*---------------------------------------------.
185 | Print a header block, based on tar options. |
186 `---------------------------------------------*/
191 int isextended
= 0; /* to remember if current_header is extended */
193 /* Print the header block. */
197 if (verbose_option
> 1)
198 decode_header (current_header
, ¤t_stat
, ¤t_format
, 0);
202 if (incremental_option
&& current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
205 size_t written
, check
;
206 union block
*data_block
;
208 set_next_block_after (current_header
);
209 if (multi_volume_option
)
211 assign_string (&save_name
, current_file_name
);
212 save_totsize
= current_stat
.st_size
;
214 for (size
= current_stat
.st_size
; size
> 0; size
-= written
)
216 if (multi_volume_option
)
217 save_sizeleft
= size
;
218 data_block
= find_next_block ();
221 ERROR ((0, 0, _("EOF in archive file")));
222 break; /* FIXME: What happens, then? */
224 written
= available_space_after (data_block
);
227 errno
= 0; /* FIXME: errno should be read-only */
228 check
= fwrite (data_block
->buffer
, sizeof (char), written
, stdlis
);
229 set_next_block_after ((union block
*)
230 (data_block
->buffer
+ written
- 1));
231 if (check
!= written
)
233 ERROR ((0, errno
, _("Only wrote %lu of %lu bytes to file %s"),
234 (unsigned long) check
,
235 (unsigned long) written
, current_file_name
));
236 skip_file (size
- written
);
240 if (multi_volume_option
)
241 assign_string (&save_name
, NULL
);
242 fputc ('\n', stdlis
);
248 /* Check to see if we have an extended header to skip over also. */
250 if (current_header
->oldgnu_header
.isextended
)
253 /* Skip past the header in the archive. */
255 set_next_block_after (current_header
);
257 /* If we needed to skip any extended headers, do so now, by reading
258 extended headers and skipping past them in the archive. */
267 exhdr
= find_next_block ();
269 if (!exhdr
->sparse_header
.isextended
)
271 set_next_block_after (exhdr
);
274 set_next_block_after (exhdr
);
277 skip_extended_headers ();
280 if (multi_volume_option
)
281 assign_string (&save_name
, current_file_name
);
283 /* Skip to the next header on the archive. */
285 skip_file (current_stat
.st_size
);
287 if (multi_volume_option
)
288 assign_string (&save_name
, NULL
);
291 /*-----------------------------------------------------------------------.
292 | Read a block that's supposed to be a header block. Return its address |
293 | in "current_header", and if it is good, the file's size in |
294 | current_stat.st_size. |
296 | Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a |
297 | block full of zeros (EOF marker). |
299 | You must always set_next_block_after(current_header) to skip past the |
300 | header which this routine reads. |
301 `-----------------------------------------------------------------------*/
303 /* The standard BSD tar sources create the checksum by adding up the
304 bytes in the header as type char. I think the type char was unsigned
305 on the PDP-11, but it's signed on the Next and Sun. It looks like the
306 sources to BSD tar were never changed to compute the checksum
307 currectly, so both the Sun and Next add the bytes of the header as
308 signed chars. This doesn't cause a problem until you get a file with
309 a name containing characters with the high bit set. So read_header
310 computes two checksums -- signed and unsigned. */
312 /* FIXME: The signed checksum computation is broken on machines where char's
313 are unsigned. It's uneasy to handle all cases correctly... */
319 long unsigned_sum
; /* the POSIX one :-) */
320 long signed_sum
; /* the Sun one :-( */
326 union block
*data_block
;
327 size_t size
, written
;
328 static char *next_long_name
, *next_long_link
;
332 header
= find_next_block ();
333 current_header
= header
;
335 return HEADER_END_OF_FILE
;
337 recorded_sum
= UINTMAX_FROM_OCT (header
->header
.chksum
);
342 for (i
= sizeof (*header
); i
-- != 0;)
344 /* We can't use unsigned char here because of old compilers,
347 unsigned_sum
+= 0xFF & *p
;
351 /* Adjust checksum to count the "chksum" field as blanks. */
353 for (i
= sizeof (header
->header
.chksum
); i
-- != 0;)
355 unsigned_sum
-= 0xFF & header
->header
.chksum
[i
];
356 signed_sum
-= header
->header
.chksum
[i
];
358 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
359 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
361 if (unsigned_sum
== sizeof header
->header
.chksum
* ' ')
363 /* This is a zeroed block...whole block is 0's except for the
364 blanks we faked for the checksum field. */
366 return HEADER_ZERO_BLOCK
;
369 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
370 return HEADER_FAILURE
;
372 /* Good block. Decode file size and return. */
374 if (header
->header
.typeflag
== LNKTYPE
)
375 current_stat
.st_size
= 0; /* links 0 size on tape */
377 current_stat
.st_size
= OFF_FROM_OCT (header
->header
.size
);
379 header
->header
.name
[NAME_FIELD_SIZE
- 1] = '\0';
380 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
381 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
383 longp
= ((header
->header
.typeflag
== GNUTYPE_LONGNAME
)
387 set_next_block_after (header
);
390 size
= current_stat
.st_size
;
391 if (size
!= current_stat
.st_size
)
392 FATAL_ERROR ((0, 0, _("Memory exhausted")));
393 bp
= *longp
= (char *) xmalloc (size
);
395 for (; size
> 0; size
-= written
)
397 data_block
= find_next_block ();
398 if (data_block
== NULL
)
400 ERROR ((0, 0, _("Unexpected EOF on archive file")));
403 written
= available_space_after (data_block
);
407 memcpy (bp
, data_block
->buffer
, written
);
409 set_next_block_after ((union block
*)
410 (data_block
->buffer
+ written
- 1));
418 assign_string (¤t_file_name
,
419 (next_long_name
? next_long_name
420 : current_header
->header
.name
));
421 assign_string (¤t_link_name
,
422 (next_long_link
? next_long_link
423 : current_header
->header
.linkname
));
424 next_long_link
= next_long_name
= 0;
425 return HEADER_SUCCESS
;
430 /*-------------------------------------------------------------------------.
431 | Decode things from a file HEADER block into STAT_INFO, also setting |
432 | *FORMAT_POINTER depending on the header block format. If DO_USER_GROUP, |
433 | decode the user/group information (this is useful for extraction, but |
434 | waste time when merely listing). |
436 | read_header() has already decoded the checksum and length, so we don't. |
438 | This routine should *not* be called twice for the same block, since the |
439 | two calls might use different DO_USER_GROUP values and thus might end up |
440 | with different uid/gid for the two calls. If anybody wants the uid/gid |
441 | they should decode it first, and other callers should decode it without |
442 | uid/gid before calling a routine, e.g. print_header, that assumes |
444 `-------------------------------------------------------------------------*/
447 decode_header (union block
*header
, struct stat
*stat_info
,
448 enum archive_format
*format_pointer
, int do_user_group
)
450 enum archive_format format
;
452 if (strcmp (header
->header
.magic
, TMAGIC
) == 0)
453 format
= POSIX_FORMAT
;
454 else if (strcmp (header
->header
.magic
, OLDGNU_MAGIC
) == 0)
455 format
= OLDGNU_FORMAT
;
458 *format_pointer
= format
;
460 stat_info
->st_mode
= MODE_FROM_OCT (header
->header
.mode
);
461 stat_info
->st_mode
&= 07777;
462 stat_info
->st_mtime
= TIME_FROM_OCT (header
->header
.mtime
);
464 if (format
== OLDGNU_FORMAT
&& incremental_option
)
466 stat_info
->st_atime
= TIME_FROM_OCT (header
->oldgnu_header
.atime
);
467 stat_info
->st_ctime
= TIME_FROM_OCT (header
->oldgnu_header
.ctime
);
470 if (format
== V7_FORMAT
)
472 stat_info
->st_uid
= UID_FROM_OCT (header
->header
.uid
);
473 stat_info
->st_gid
= GID_FROM_OCT (header
->header
.gid
);
474 stat_info
->st_rdev
= 0;
480 /* FIXME: Decide if this should somewhat depend on -p. */
482 if (numeric_owner_option
483 || !*header
->header
.uname
484 || !uname_to_uid (header
->header
.uname
, &stat_info
->st_uid
))
485 stat_info
->st_uid
= UID_FROM_OCT (header
->header
.uid
);
487 if (numeric_owner_option
488 || !*header
->header
.gname
489 || !gname_to_gid (header
->header
.gname
, &stat_info
->st_gid
))
490 stat_info
->st_gid
= GID_FROM_OCT (header
->header
.gid
);
492 switch (header
->header
.typeflag
)
497 = makedev (MAJOR_FROM_OCT (header
->header
.devmajor
),
498 MINOR_FROM_OCT (header
->header
.devminor
));
505 = makedev (MAJOR_FROM_OCT (header
->header
.devmajor
),
506 MINOR_FROM_OCT (header
->header
.devminor
));
511 stat_info
->st_rdev
= 0;
516 /*------------------------------------------------------------------------.
517 | Quick and dirty octal conversion. Result is -1 if the field is invalid |
518 | (all blank, or nonoctal). |
519 `------------------------------------------------------------------------*/
522 from_oct (const char *where0
, size_t digs0
, const char *type
, uintmax_t maxval
)
525 const char *where
= where0
;
532 ERROR ((0, 0, _("Blanks in header where octal %s value expected"),
536 if (!ISSPACE (*where
))
543 while (digs
!= 0 && ISODIGIT (*where
))
545 /* Scan til nonoctal. */
547 if (value
<< 3 >> 3 != value
)
549 value
= (value
<< 3) | (*where
++ - '0');
553 if (digs
!= 0 && *where
&& !ISSPACE (*where
))
555 ERROR ((0, 0, _("Header contains `%.*s' where octal %s value expected"),
556 (int) digs0
, where0
, type
));
564 ERROR ((0, 0, _("Octal value `%.*s' is out of range for %s"),
565 (int) digs0
, where0
, type
));
569 gid_from_oct (const char *p
, size_t s
)
571 return from_oct (p
, s
, "gid_t", GID_MAX
);
574 major_from_oct (const char *p
, size_t s
)
576 return from_oct (p
, s
, "major_t", MAJOR_MAX
);
579 minor_from_oct (const char *p
, size_t s
)
581 return from_oct (p
, s
, "minor_t", MINOR_MAX
);
584 mode_from_oct (const char *p
, size_t s
)
586 return from_oct (p
, s
, "mode_t", MODE_MAX
);
589 off_from_oct (const char *p
, size_t s
)
591 return from_oct (p
, s
, "off_t", OFF_MAX
);
594 size_from_oct (const char *p
, size_t s
)
596 return from_oct (p
, s
, "size_t", SIZE_MAX
);
599 time_from_oct (const char *p
, size_t s
)
601 return from_oct (p
, s
, "time_t", TIME_MAX
);
604 uid_from_oct (const char *p
, size_t s
)
606 return from_oct (p
, s
, "uid_t", UID_MAX
);
609 uintmax_from_oct (const char *p
, size_t s
)
611 return from_oct (p
, s
, "uintmax_t", UINTMAX_MAX
);
616 /*----------------------------------------------------------------------.
617 | Format O as a null-terminated decimal string into BUF _backwards_; |
618 | return pointer to start of result. |
619 `----------------------------------------------------------------------*/
621 stringify_uintmax_t_backwards (uintmax_t o
, char *buf
)
625 *--buf
= '0' + (int) (o
% 10);
626 while ((o
/= 10) != 0);
632 /*-------------------------------------------.
633 | Return the time formatted along ISO 8601. |
634 `-------------------------------------------*/
636 /* Also, see http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html. */
639 isotime (const time_t *time
)
641 static char buffer
[21];
644 tm
= localtime (time
);
645 sprintf (buffer
, "%4d-%02d-%02d %02d:%02d:%02d\n",
646 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
647 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
651 #endif /* not USE_OLD_CTIME */
653 /*-------------------------------------------------------------------------.
654 | Decode MODE from its binary form in a stat structure, and encode it into |
655 | a 9 characters string STRING, terminated with a NUL. |
656 `-------------------------------------------------------------------------*/
659 decode_mode (mode_t mode
, char *string
)
662 const char *rwx
= "rwxrwxrwx";
664 for (mask
= 0400; mask
!= 0; mask
>>= 1)
674 string
[-7] = string
[-7] == 'x' ? 's' : 'S';
676 string
[-4] = string
[-4] == 'x' ? 's' : 'S';
678 string
[-1] = string
[-1] == 'x' ? 't' : 'T';
683 /*-------------------------------------------------------------------------.
684 | Actually print it. |
686 | Plain and fancy file header block logging. Non-verbose just prints the |
687 | name, e.g. for "tar t" or "tar x". This should just contain file names, |
688 | so it can be fed back into tar with xargs or the "-T" option. The |
689 | verbose option can give a bunch of info, one line per file. I doubt |
690 | anybody tries to parse its format, or if they do, they shouldn't. Unix |
691 | tar is pretty random here anyway. |
692 `-------------------------------------------------------------------------*/
694 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
695 HEAD_STANDARD, which must be set up in advance. Not very clean... */
697 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
698 columns never shift during the listing. */
700 static int ugswidth
= UGSWIDTH
; /* maximum width encountered so far */
702 /* DATEWIDTH is the number of columns taken by the date and time fields. */
704 # define DATEWIDTH 19
706 # define DATEWIDTH 18
714 char uform
[11], gform
[11]; /* these hold formatted ints */
716 char size
[2 * UINTMAX_STRSIZE_BOUND
];
717 /* holds formatted size or major,minor */
718 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
719 time_t longie
; /* to make ctime() call portable */
723 if (block_number_option
)
725 char buf
[UINTMAX_STRSIZE_BOUND
];
726 fprintf (stdlis
, _("block %s: "),
727 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
730 if (verbose_option
<= 1)
732 /* Just the fax, mam. */
734 char *quoted_name
= quote_copy_string (current_file_name
);
738 fprintf (stdlis
, "%s\n", quoted_name
);
742 fprintf (stdlis
, "%s\n", current_file_name
);
746 /* File type and modes. */
749 switch (current_header
->header
.typeflag
)
755 case GNUTYPE_MULTIVOL
:
763 case GNUTYPE_LONGNAME
:
764 case GNUTYPE_LONGLINK
:
765 ERROR ((0, 0, _("Visible longname error")));
773 if (current_file_name
[strlen (current_file_name
) - 1] == '/')
776 case GNUTYPE_DUMPDIR
:
799 decode_mode (current_stat
.st_mode
, modes
+ 1);
803 longie
= current_stat
.st_mtime
;
805 timestamp
= ctime (&longie
);
806 timestamp
[16] = '\0';
807 timestamp
[24] = '\0';
809 timestamp
= isotime (&longie
);
810 timestamp
[16] = '\0';
813 /* User and group names. */
815 if (*current_header
->header
.uname
&& current_format
!= V7_FORMAT
)
816 user
= current_header
->header
.uname
;
818 user
= STRINGIFY_BIGINT (UINTMAX_FROM_OCT (current_header
->header
.uid
),
821 if (*current_header
->header
.gname
&& current_format
!= V7_FORMAT
)
822 group
= current_header
->header
.gname
;
824 group
= STRINGIFY_BIGINT (UINTMAX_FROM_OCT
825 (current_header
->header
.gid
),
828 /* Format the file size or major/minor device numbers. */
830 switch (current_header
->header
.typeflag
)
832 #if defined(S_IFBLK) || defined(S_IFCHR)
835 sprintf (size
, "%lu,%lu",
836 (unsigned long) major (current_stat
.st_rdev
),
837 (unsigned long) minor (current_stat
.st_rdev
));
843 (UINTMAX_FROM_OCT (current_header
->oldgnu_header
.realsize
),
847 strcpy (size
, STRINGIFY_BIGINT (current_stat
.st_size
, uintbuf
));
851 /* Figure out padding and print the whole line. */
853 pad
= strlen (user
) + strlen (group
) + strlen (size
) + 1;
858 fprintf (stdlis
, "%s %s/%s %*s%s %s %s",
859 modes
, user
, group
, ugswidth
- pad
, "",
860 size
, timestamp
+ 4, timestamp
+ 20);
862 fprintf (stdlis
, "%s %s/%s %*s%s %s",
863 modes
, user
, group
, ugswidth
- pad
, "", size
, timestamp
);
866 name
= quote_copy_string (current_file_name
);
869 fprintf (stdlis
, " %s", name
);
873 fprintf (stdlis
, " %s", current_file_name
);
875 switch (current_header
->header
.typeflag
)
878 name
= quote_copy_string (current_link_name
);
881 fprintf (stdlis
, " -> %s\n", name
);
885 fprintf (stdlis
, " -> %s\n", current_link_name
);
889 name
= quote_copy_string (current_link_name
);
892 fprintf (stdlis
, _(" link to %s\n"), name
);
896 fprintf (stdlis
, _(" link to %s\n"), current_link_name
);
900 fprintf (stdlis
, _(" unknown file type `%c'\n"),
901 current_header
->header
.typeflag
);
912 case GNUTYPE_DUMPDIR
:
917 fprintf (stdlis
, _("--Volume Header--\n"));
920 case GNUTYPE_MULTIVOL
:
923 (UINTMAX_FROM_OCT (current_header
->oldgnu_header
.offset
),
925 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
929 fprintf (stdlis
, _("--Mangled file names--\n"));
936 /*--------------------------------------------------------------.
937 | Print a similar line when we make a directory automatically. |
938 `--------------------------------------------------------------*/
941 print_for_mkdir (char *pathname
, int length
, mode_t mode
)
946 if (verbose_option
> 1)
948 /* File type and modes. */
951 decode_mode (mode
, modes
+ 1);
953 if (block_number_option
)
955 char buf
[UINTMAX_STRSIZE_BOUND
];
956 fprintf (stdlis
, _("block %s: "),
957 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
959 name
= quote_copy_string (pathname
);
962 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
963 _("Creating directory:"), length
, name
);
967 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
968 _("Creating directory:"), length
, pathname
);
972 /*--------------------------------------------------------.
973 | Skip over SIZE bytes of data in blocks in the archive. |
974 `--------------------------------------------------------*/
977 skip_file (off_t size
)
981 if (multi_volume_option
)
984 save_sizeleft
= size
;
989 x
= find_next_block ();
991 FATAL_ERROR ((0, 0, _("Unexpected EOF on archive file")));
993 set_next_block_after (x
);
995 if (multi_volume_option
)
996 save_sizeleft
-= BLOCKSIZE
;
1005 skip_extended_headers (void)
1011 exhdr
= find_next_block ();
1012 if (!exhdr
->sparse_header
.isextended
)
1014 set_next_block_after (exhdr
);
1017 set_next_block_after (exhdr
);