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. */
66 current_stat
.st_mtime
= TIME_FROM_OCT (current_header
->header
.mtime
);
68 if (!name_match (current_file_name
)
69 || current_stat
.st_mtime
< newer_mtime_option
70 || (exclude_option
&& check_exclude (current_file_name
)))
74 if (current_header
->header
.typeflag
== GNUTYPE_VOLHDR
75 || current_header
->header
.typeflag
== GNUTYPE_MULTIVOL
76 || current_header
->header
.typeflag
== GNUTYPE_NAMES
)
81 if (show_omitted_dirs_option
82 && current_header
->header
.typeflag
== DIRTYPE
)
83 WARN ((0, 0, _("Omitting %s"), current_file_name
));
85 /* Skip past it in the archive. */
87 if (current_header
->oldgnu_header
.isextended
)
89 save_typeflag
= current_header
->header
.typeflag
;
90 set_next_block_after (current_header
);
98 exhdr
= find_next_block ();
99 if (!exhdr
->sparse_header
.isextended
)
101 set_next_block_after (exhdr
);
105 set_next_block_after (exhdr
);
107 skip_extended_headers ();
110 /* Skip to the next header on the archive. */
112 if (save_typeflag
!= DIRTYPE
)
113 skip_file (current_stat
.st_size
);
120 case HEADER_ZERO_BLOCK
:
121 if (block_number_option
)
123 char buf
[UINTMAX_STRSIZE_BOUND
];
124 fprintf (stdlis
, _("block %s: ** Block of NULs **\n"),
125 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
128 set_next_block_after (current_header
);
129 status
= prev_status
;
130 if (ignore_zeros_option
)
134 case HEADER_END_OF_FILE
:
135 if (block_number_option
)
137 char buf
[UINTMAX_STRSIZE_BOUND
];
138 fprintf (stdlis
, _("block %s: ** End of File **\n"),
139 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
144 /* If the previous header was good, tell them that we are
145 skipping bad ones. */
146 set_next_block_after (current_header
);
149 case HEADER_STILL_UNREAD
:
150 WARN ((0, 0, _("Hmm, this doesn't look like a tar archive")));
153 case HEADER_ZERO_BLOCK
:
155 WARN ((0, 0, _("Skipping to next file header")));
158 case HEADER_END_OF_FILE
:
160 /* We are in the middle of a cascade of errors. */
168 apply_delayed_set_stat ();
170 names_notfound (); /* print names not found */
173 /*---------------------------------------------.
174 | Print a header block, based on tar options. |
175 `---------------------------------------------*/
180 int isextended
= 0; /* to remember if current_header is extended */
182 /* Print the header block. */
186 if (verbose_option
> 1)
187 decode_header (current_header
, ¤t_stat
, ¤t_format
, 0);
191 if (incremental_option
&& current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
194 size_t written
, check
;
195 union block
*data_block
;
197 set_next_block_after (current_header
);
198 if (multi_volume_option
)
200 assign_string (&save_name
, current_file_name
);
201 save_totsize
= current_stat
.st_size
;
203 for (size
= current_stat
.st_size
; size
> 0; size
-= written
)
205 if (multi_volume_option
)
206 save_sizeleft
= size
;
207 data_block
= find_next_block ();
210 ERROR ((0, 0, _("EOF in archive file")));
211 break; /* FIXME: What happens, then? */
213 written
= available_space_after (data_block
);
216 errno
= 0; /* FIXME: errno should be read-only */
217 check
= fwrite (data_block
->buffer
, sizeof (char), written
, stdlis
);
218 set_next_block_after ((union block
*)
219 (data_block
->buffer
+ written
- 1));
220 if (check
!= written
)
222 ERROR ((0, errno
, _("Only wrote %lu of %lu bytes to file %s"),
223 (unsigned long) check
,
224 (unsigned long) written
, current_file_name
));
225 skip_file (size
- written
);
229 if (multi_volume_option
)
230 assign_string (&save_name
, NULL
);
231 fputc ('\n', stdlis
);
237 /* Check to see if we have an extended header to skip over also. */
239 if (current_header
->oldgnu_header
.isextended
)
242 /* Skip past the header in the archive. */
244 set_next_block_after (current_header
);
246 /* If we needed to skip any extended headers, do so now, by reading
247 extended headers and skipping past them in the archive. */
256 exhdr
= find_next_block ();
258 if (!exhdr
->sparse_header
.isextended
)
260 set_next_block_after (exhdr
);
263 set_next_block_after (exhdr
);
266 skip_extended_headers ();
269 if (multi_volume_option
)
270 assign_string (&save_name
, current_file_name
);
272 /* Skip to the next header on the archive. */
274 skip_file (current_stat
.st_size
);
276 if (multi_volume_option
)
277 assign_string (&save_name
, NULL
);
280 /*-----------------------------------------------------------------------.
281 | Read a block that's supposed to be a header block. Return its address |
282 | in "current_header", and if it is good, the file's size in |
283 | current_stat.st_size. |
285 | Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a |
286 | block full of zeros (EOF marker). |
288 | You must always set_next_block_after(current_header) to skip past the |
289 | header which this routine reads. |
290 `-----------------------------------------------------------------------*/
292 /* The standard BSD tar sources create the checksum by adding up the
293 bytes in the header as type char. I think the type char was unsigned
294 on the PDP-11, but it's signed on the Next and Sun. It looks like the
295 sources to BSD tar were never changed to compute the checksum
296 currectly, so both the Sun and Next add the bytes of the header as
297 signed chars. This doesn't cause a problem until you get a file with
298 a name containing characters with the high bit set. So read_header
299 computes two checksums -- signed and unsigned. */
301 /* FIXME: The signed checksum computation is broken on machines where char's
302 are unsigned. It's uneasy to handle all cases correctly... */
308 long unsigned_sum
; /* the POSIX one :-) */
309 long signed_sum
; /* the Sun one :-( */
315 union block
*data_block
;
316 size_t size
, written
;
317 static char *next_long_name
, *next_long_link
;
321 header
= find_next_block ();
322 current_header
= header
;
324 return HEADER_END_OF_FILE
;
326 recorded_sum
= UINTMAX_FROM_OCT (header
->header
.chksum
);
331 for (i
= sizeof (*header
); i
-- != 0;)
333 /* We can't use unsigned char here because of old compilers,
336 unsigned_sum
+= 0xFF & *p
;
340 /* Adjust checksum to count the "chksum" field as blanks. */
342 for (i
= sizeof (header
->header
.chksum
); i
-- != 0;)
344 unsigned_sum
-= 0xFF & header
->header
.chksum
[i
];
345 signed_sum
-= header
->header
.chksum
[i
];
347 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
348 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
350 if (unsigned_sum
== sizeof header
->header
.chksum
* ' ')
352 /* This is a zeroed block...whole block is 0's except for the
353 blanks we faked for the checksum field. */
355 return HEADER_ZERO_BLOCK
;
358 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
359 return HEADER_FAILURE
;
361 /* Good block. Decode file size and return. */
363 if (header
->header
.typeflag
== LNKTYPE
)
364 current_stat
.st_size
= 0; /* links 0 size on tape */
366 current_stat
.st_size
= OFF_FROM_OCT (header
->header
.size
);
368 header
->header
.name
[NAME_FIELD_SIZE
- 1] = '\0';
369 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
370 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
372 longp
= ((header
->header
.typeflag
== GNUTYPE_LONGNAME
)
376 set_next_block_after (header
);
379 size
= current_stat
.st_size
;
380 if (size
!= current_stat
.st_size
)
381 FATAL_ERROR ((0, 0, _("Memory exhausted")));
382 bp
= *longp
= (char *) xmalloc (size
);
384 for (; size
> 0; size
-= written
)
386 data_block
= find_next_block ();
387 if (data_block
== NULL
)
389 ERROR ((0, 0, _("Unexpected EOF on archive file")));
392 written
= available_space_after (data_block
);
396 memcpy (bp
, data_block
->buffer
, written
);
398 set_next_block_after ((union block
*)
399 (data_block
->buffer
+ written
- 1));
407 assign_string (¤t_file_name
,
408 (next_long_name
? next_long_name
409 : current_header
->header
.name
));
410 assign_string (¤t_link_name
,
411 (next_long_link
? next_long_link
412 : current_header
->header
.linkname
));
413 next_long_link
= next_long_name
= 0;
414 return HEADER_SUCCESS
;
419 /*-------------------------------------------------------------------------.
420 | Decode things from a file HEADER block into STAT_INFO, also setting |
421 | *FORMAT_POINTER depending on the header block format. If DO_USER_GROUP, |
422 | decode the user/group information (this is useful for extraction, but |
423 | waste time when merely listing). |
425 | read_header() has already decoded the checksum and length, so we don't. |
427 | This routine should *not* be called twice for the same block, since the |
428 | two calls might use different DO_USER_GROUP values and thus might end up |
429 | with different uid/gid for the two calls. If anybody wants the uid/gid |
430 | they should decode it first, and other callers should decode it without |
431 | uid/gid before calling a routine, e.g. print_header, that assumes |
433 `-------------------------------------------------------------------------*/
436 decode_header (union block
*header
, struct stat
*stat_info
,
437 enum archive_format
*format_pointer
, int do_user_group
)
439 enum archive_format format
;
441 if (strcmp (header
->header
.magic
, TMAGIC
) == 0)
442 format
= POSIX_FORMAT
;
443 else if (strcmp (header
->header
.magic
, OLDGNU_MAGIC
) == 0)
444 format
= OLDGNU_FORMAT
;
447 *format_pointer
= format
;
449 stat_info
->st_mode
= MODE_FROM_OCT (header
->header
.mode
);
450 stat_info
->st_mode
&= 07777;
451 stat_info
->st_mtime
= TIME_FROM_OCT (header
->header
.mtime
);
453 if (format
== OLDGNU_FORMAT
&& incremental_option
)
455 stat_info
->st_atime
= TIME_FROM_OCT (header
->oldgnu_header
.atime
);
456 stat_info
->st_ctime
= TIME_FROM_OCT (header
->oldgnu_header
.ctime
);
459 if (format
== V7_FORMAT
)
461 stat_info
->st_uid
= UID_FROM_OCT (header
->header
.uid
);
462 stat_info
->st_gid
= GID_FROM_OCT (header
->header
.gid
);
463 stat_info
->st_rdev
= 0;
469 /* FIXME: Decide if this should somewhat depend on -p. */
471 if (numeric_owner_option
472 || !*header
->header
.uname
473 || !uname_to_uid (header
->header
.uname
, &stat_info
->st_uid
))
474 stat_info
->st_uid
= UID_FROM_OCT (header
->header
.uid
);
476 if (numeric_owner_option
477 || !*header
->header
.gname
478 || !gname_to_gid (header
->header
.gname
, &stat_info
->st_gid
))
479 stat_info
->st_gid
= GID_FROM_OCT (header
->header
.gid
);
481 switch (header
->header
.typeflag
)
486 = makedev (MAJOR_FROM_OCT (header
->header
.devmajor
),
487 MINOR_FROM_OCT (header
->header
.devminor
));
494 = makedev (MAJOR_FROM_OCT (header
->header
.devmajor
),
495 MINOR_FROM_OCT (header
->header
.devminor
));
500 stat_info
->st_rdev
= 0;
505 /*------------------------------------------------------------------------.
506 | Quick and dirty octal conversion. Result is -1 if the field is invalid |
507 | (all blank, or nonoctal). |
508 `------------------------------------------------------------------------*/
511 from_oct (const char *where0
, size_t digs0
, const char *type
, uintmax_t maxval
)
514 const char *where
= where0
;
521 ERROR ((0, 0, _("Blanks in header where octal %s value expected"),
525 if (!ISSPACE (*where
))
532 while (digs
!= 0 && ISODIGIT (*where
))
534 /* Scan til nonoctal. */
536 if (value
<< 3 >> 3 != value
)
538 value
= (value
<< 3) | (*where
++ - '0');
542 if (digs
!= 0 && *where
&& !ISSPACE (*where
))
544 ERROR ((0, 0, _("Header contains `%.*s' where octal %s value expected"),
545 (int) digs0
, where0
, type
));
553 ERROR ((0, 0, _("Octal value `%.*s' is out of range for %s"),
554 (int) digs0
, where0
, type
));
558 gid_from_oct (const char *p
, size_t s
)
560 return from_oct (p
, s
, "gid_t", GID_MAX
);
563 major_from_oct (const char *p
, size_t s
)
565 return from_oct (p
, s
, "major_t", MAJOR_MAX
);
568 minor_from_oct (const char *p
, size_t s
)
570 return from_oct (p
, s
, "minor_t", MINOR_MAX
);
573 mode_from_oct (const char *p
, size_t s
)
575 return from_oct (p
, s
, "mode_t", MODE_MAX
);
578 off_from_oct (const char *p
, size_t s
)
580 return from_oct (p
, s
, "off_t", OFF_MAX
);
583 size_from_oct (const char *p
, size_t s
)
585 return from_oct (p
, s
, "size_t", SIZE_MAX
);
588 time_from_oct (const char *p
, size_t s
)
590 return from_oct (p
, s
, "time_t", TIME_MAX
);
593 uid_from_oct (const char *p
, size_t s
)
595 return from_oct (p
, s
, "uid_t", UID_MAX
);
598 uintmax_from_oct (const char *p
, size_t s
)
600 return from_oct (p
, s
, "uintmax_t", UINTMAX_MAX
);
605 /*----------------------------------------------------------------------.
606 | Format O as a null-terminated decimal string into BUF _backwards_; |
607 | return pointer to start of result. |
608 `----------------------------------------------------------------------*/
610 stringify_uintmax_t_backwards (uintmax_t o
, char *buf
)
614 *--buf
= '0' + (int) (o
% 10);
615 while ((o
/= 10) != 0);
621 /*-------------------------------------------.
622 | Return the time formatted along ISO 8601. |
623 `-------------------------------------------*/
625 /* Also, see http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html. */
628 isotime (const time_t *time
)
630 static char buffer
[21];
633 tm
= localtime (time
);
634 sprintf (buffer
, "%4d-%02d-%02d %02d:%02d:%02d\n",
635 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
636 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
640 #endif /* not USE_OLD_CTIME */
642 /*-------------------------------------------------------------------------.
643 | Decode MODE from its binary form in a stat structure, and encode it into |
644 | a 9 characters string STRING, terminated with a NUL. |
645 `-------------------------------------------------------------------------*/
648 decode_mode (mode_t mode
, char *string
)
651 const char *rwx
= "rwxrwxrwx";
653 for (mask
= 0400; mask
!= 0; mask
>>= 1)
663 string
[-7] = string
[-7] == 'x' ? 's' : 'S';
665 string
[-4] = string
[-4] == 'x' ? 's' : 'S';
667 string
[-1] = string
[-1] == 'x' ? 't' : 'T';
672 /*-------------------------------------------------------------------------.
673 | Actually print it. |
675 | Plain and fancy file header block logging. Non-verbose just prints the |
676 | name, e.g. for "tar t" or "tar x". This should just contain file names, |
677 | so it can be fed back into tar with xargs or the "-T" option. The |
678 | verbose option can give a bunch of info, one line per file. I doubt |
679 | anybody tries to parse its format, or if they do, they shouldn't. Unix |
680 | tar is pretty random here anyway. |
681 `-------------------------------------------------------------------------*/
683 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
684 HEAD_STANDARD, which must be set up in advance. Not very clean... */
686 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
687 columns never shift during the listing. */
689 static int ugswidth
= UGSWIDTH
; /* maximum width encountered so far */
691 /* DATEWIDTH is the number of columns taken by the date and time fields. */
693 # define DATEWIDTH 19
695 # define DATEWIDTH 18
703 char uform
[11], gform
[11]; /* these hold formatted ints */
705 char size
[2 * UINTMAX_STRSIZE_BOUND
];
706 /* holds formatted size or major,minor */
707 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
708 time_t longie
; /* to make ctime() call portable */
712 if (block_number_option
)
714 char buf
[UINTMAX_STRSIZE_BOUND
];
715 fprintf (stdlis
, _("block %s: "),
716 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
719 if (verbose_option
<= 1)
721 /* Just the fax, mam. */
723 char *quoted_name
= quote_copy_string (current_file_name
);
727 fprintf (stdlis
, "%s\n", quoted_name
);
731 fprintf (stdlis
, "%s\n", current_file_name
);
735 /* File type and modes. */
738 switch (current_header
->header
.typeflag
)
744 case GNUTYPE_MULTIVOL
:
752 case GNUTYPE_LONGNAME
:
753 case GNUTYPE_LONGLINK
:
754 ERROR ((0, 0, _("Visible longname error")));
762 if (current_file_name
[strlen (current_file_name
) - 1] == '/')
765 case GNUTYPE_DUMPDIR
:
788 decode_mode (current_stat
.st_mode
, modes
+ 1);
792 longie
= current_stat
.st_mtime
;
794 timestamp
= ctime (&longie
);
795 timestamp
[16] = '\0';
796 timestamp
[24] = '\0';
798 timestamp
= isotime (&longie
);
799 timestamp
[16] = '\0';
802 /* User and group names. */
804 if (*current_header
->header
.uname
&& current_format
!= V7_FORMAT
)
805 user
= current_header
->header
.uname
;
807 user
= STRINGIFY_BIGINT (UINTMAX_FROM_OCT (current_header
->header
.uid
),
810 if (*current_header
->header
.gname
&& current_format
!= V7_FORMAT
)
811 group
= current_header
->header
.gname
;
813 group
= STRINGIFY_BIGINT (UINTMAX_FROM_OCT
814 (current_header
->header
.gid
),
817 /* Format the file size or major/minor device numbers. */
819 switch (current_header
->header
.typeflag
)
821 #if defined(S_IFBLK) || defined(S_IFCHR)
824 sprintf (size
, "%lu,%lu",
825 (unsigned long) major (current_stat
.st_rdev
),
826 (unsigned long) minor (current_stat
.st_rdev
));
832 (UINTMAX_FROM_OCT (current_header
->oldgnu_header
.realsize
),
836 strcpy (size
, STRINGIFY_BIGINT (current_stat
.st_size
, uintbuf
));
840 /* Figure out padding and print the whole line. */
842 pad
= strlen (user
) + strlen (group
) + strlen (size
) + 1;
847 fprintf (stdlis
, "%s %s/%s %*s%s %s %s",
848 modes
, user
, group
, ugswidth
- pad
, "",
849 size
, timestamp
+ 4, timestamp
+ 20);
851 fprintf (stdlis
, "%s %s/%s %*s%s %s",
852 modes
, user
, group
, ugswidth
- pad
, "", size
, timestamp
);
855 name
= quote_copy_string (current_file_name
);
858 fprintf (stdlis
, " %s", name
);
862 fprintf (stdlis
, " %s", current_file_name
);
864 switch (current_header
->header
.typeflag
)
867 name
= quote_copy_string (current_link_name
);
870 fprintf (stdlis
, " -> %s\n", name
);
874 fprintf (stdlis
, " -> %s\n", current_link_name
);
878 name
= quote_copy_string (current_link_name
);
881 fprintf (stdlis
, _(" link to %s\n"), name
);
885 fprintf (stdlis
, _(" link to %s\n"), current_link_name
);
889 fprintf (stdlis
, _(" unknown file type `%c'\n"),
890 current_header
->header
.typeflag
);
901 case GNUTYPE_DUMPDIR
:
906 fprintf (stdlis
, _("--Volume Header--\n"));
909 case GNUTYPE_MULTIVOL
:
912 (UINTMAX_FROM_OCT (current_header
->oldgnu_header
.offset
),
914 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
918 fprintf (stdlis
, _("--Mangled file names--\n"));
925 /*--------------------------------------------------------------.
926 | Print a similar line when we make a directory automatically. |
927 `--------------------------------------------------------------*/
930 print_for_mkdir (char *pathname
, int length
, mode_t mode
)
935 if (verbose_option
> 1)
937 /* File type and modes. */
940 decode_mode (mode
, modes
+ 1);
942 if (block_number_option
)
944 char buf
[UINTMAX_STRSIZE_BOUND
];
945 fprintf (stdlis
, _("block %s: "),
946 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
948 name
= quote_copy_string (pathname
);
951 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
952 _("Creating directory:"), length
, name
);
956 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
957 _("Creating directory:"), length
, pathname
);
961 /*--------------------------------------------------------.
962 | Skip over SIZE bytes of data in blocks in the archive. |
963 `--------------------------------------------------------*/
966 skip_file (off_t size
)
970 if (multi_volume_option
)
973 save_sizeleft
= size
;
978 x
= find_next_block ();
980 FATAL_ERROR ((0, 0, _("Unexpected EOF on archive file")));
982 set_next_block_after (x
);
984 if (multi_volume_option
)
985 save_sizeleft
-= BLOCKSIZE
;
994 skip_extended_headers (void)
1000 exhdr
= find_next_block ();
1001 if (!exhdr
->sparse_header
.isextended
)
1003 set_next_block_after (exhdr
);
1006 set_next_block_after (exhdr
);