X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Flist.c;h=0d96e8bb67eaef9d9e4ab05552e57c771838b1da;hb=322114744f56ed23688077e33dc459efcf529f41;hp=77dce5507215fc6875c0de0e047045bc362f85d3;hpb=50b2227279c0094f4ed42c747c501c9dc8115526;p=chaz%2Ftar diff --git a/src/list.c b/src/list.c index 77dce55..0d96e8b 100644 --- a/src/list.c +++ b/src/list.c @@ -1,7 +1,7 @@ /* List a tar archive, with support routines for reading a tar archive. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, - 2001, 2003 Free Software Foundation, Inc. + 2001, 2003, 2004 Free Software Foundation, Inc. Written by John Gilmore, on 1985-08-26. @@ -22,7 +22,7 @@ /* Define to non-zero for forcing old ctime format instead of ISO format. */ #undef USE_OLD_CTIME -#include "system.h" +#include #include #include "common.h" @@ -78,7 +78,7 @@ read_and (void (*do_something) (void)) prev_status = status; tar_stat_destroy (¤t_stat_info); xheader_destroy (&extended_header); - + status = read_header (false); switch (status) { @@ -92,12 +92,17 @@ read_and (void (*do_something) (void)) Ensure incoming names are null terminated. */ if (! name_match (current_stat_info.file_name) - || (newer_mtime_option != TYPE_MINIMUM (time_t) + || (NEWER_OPTION_INITIALIZED (newer_mtime_option) /* FIXME: We get mtime now, and again later; this causes duplicate diagnostics if header.mtime is bogus. */ && ((current_stat_info.stat.st_mtime - = TIME_FROM_HEADER (current_header->header.mtime)) - < newer_mtime_option)) + = TIME_FROM_HEADER (current_header->header.mtime)), +#ifdef ST_MTIM_NSEC + /* FIXME: Grab fractional time stamps from + extended header. */ + current_stat_info.stat.st_mtim.ST_MTIM_NSEC = 0, +#endif + OLDER_STAT_TIME (current_stat_info.stat, m))) || excluded_name (current_stat_info.file_name)) { switch (current_header->header.typeflag) @@ -113,6 +118,8 @@ read_and (void (*do_something) (void)) quotearg_colon (current_stat_info.file_name))); /* Fall through. */ default: + decode_header (current_header, + ¤t_stat_info, ¤t_format, 0); skip_member (); continue; } @@ -130,10 +137,20 @@ read_and (void (*do_something) (void)) } set_next_block_after (current_header); + + if (!ignore_zeros_option) + { + char buf[UINTMAX_STRSIZE_BOUND]; + + status = read_header (false); + if (status == HEADER_ZERO_BLOCK) + break; + WARN ((0, 0, _("A lone zero block at %s"), + STRINGIFY_BIGINT (current_block_ordinal (), buf))); + break; + } status = prev_status; - if (ignore_zeros_option) - continue; - break; + continue; case HEADER_END_OF_FILE: if (block_number_option) @@ -156,6 +173,15 @@ read_and (void (*do_something) (void)) case HEADER_ZERO_BLOCK: case HEADER_SUCCESS: + if (block_number_option) + { + char buf[UINTMAX_STRSIZE_BOUND]; + off_t block_ordinal = current_block_ordinal (); + block_ordinal -= recent_long_name_blocks; + block_ordinal -= recent_long_link_blocks; + fprintf (stdlis, _("block %s: "), + STRINGIFY_BIGINT (block_ordinal, buf)); + } ERROR ((0, 0, _("Skipping to next header"))); break; @@ -240,6 +266,61 @@ list_archive (void) assign_string (&save_name, 0); } +/* Check header checksum */ +/* The standard BSD tar sources create the checksum by adding up the + bytes in the header as type char. I think the type char was unsigned + on the PDP-11, but it's signed on the Next and Sun. It looks like the + sources to BSD tar were never changed to compute the checksum + correctly, so both the Sun and Next add the bytes of the header as + signed chars. This doesn't cause a problem until you get a file with + a name containing characters with the high bit set. So tar_checksum + computes two checksums -- signed and unsigned. */ + +enum read_header +tar_checksum (union block *header) +{ + size_t i; + int unsigned_sum = 0; /* the POSIX one :-) */ + int signed_sum = 0; /* the Sun one :-( */ + int recorded_sum; + uintmax_t parsed_sum; + char *p; + + p = header->buffer; + for (i = sizeof *header; i-- != 0;) + { + unsigned_sum += (unsigned char) *p; + signed_sum += (signed char) (*p++); + } + + if (unsigned_sum == 0) + return HEADER_ZERO_BLOCK; + + /* Adjust checksum to count the "chksum" field as blanks. */ + + for (i = sizeof header->header.chksum; i-- != 0;) + { + unsigned_sum -= (unsigned char) header->header.chksum[i]; + signed_sum -= (signed char) (header->header.chksum[i]); + } + unsigned_sum += ' ' * sizeof header->header.chksum; + signed_sum += ' ' * sizeof header->header.chksum; + + parsed_sum = from_header (header->header.chksum, + sizeof header->header.chksum, 0, + (uintmax_t) 0, + (uintmax_t) TYPE_MAXIMUM (int)); + if (parsed_sum == (uintmax_t) -1) + return HEADER_FAILURE; + + recorded_sum = parsed_sum; + + if (unsigned_sum != recorded_sum && signed_sum != recorded_sum) + return HEADER_FAILURE; + + return HEADER_SUCCESS; +} + /* Read a block that's supposed to be a header block. Return its address in "current_header", and if it is good, the file's size in current_stat_info.stat.st_size. @@ -253,23 +334,9 @@ list_archive (void) You must always set_next_block_after(current_header) to skip past the header which this routine reads. */ -/* The standard BSD tar sources create the checksum by adding up the - bytes in the header as type char. I think the type char was unsigned - on the PDP-11, but it's signed on the Next and Sun. It looks like the - sources to BSD tar were never changed to compute the checksum - correctly, so both the Sun and Next add the bytes of the header as - signed chars. This doesn't cause a problem until you get a file with - a name containing characters with the high bit set. So read_header - computes two checksums -- signed and unsigned. */ - enum read_header read_header (bool raw_extended_headers) { - size_t i; - int unsigned_sum; /* the POSIX one :-) */ - int signed_sum; /* the Sun one :-( */ - int recorded_sum; - uintmax_t parsed_sum; char *p; union block *header; union block *header_copy; @@ -283,44 +350,15 @@ read_header (bool raw_extended_headers) while (1) { + enum read_header status; + header = find_next_block (); current_header = header; if (!header) return HEADER_END_OF_FILE; - unsigned_sum = 0; - signed_sum = 0; - p = header->buffer; - for (i = sizeof *header; i-- != 0;) - { - unsigned_sum += (unsigned char) *p; - signed_sum += (signed char) (*p++); - } - - if (unsigned_sum == 0) - return HEADER_ZERO_BLOCK; - - /* Adjust checksum to count the "chksum" field as blanks. */ - - for (i = sizeof header->header.chksum; i-- != 0;) - { - unsigned_sum -= (unsigned char) header->header.chksum[i]; - signed_sum -= (signed char) (header->header.chksum[i]); - } - unsigned_sum += ' ' * sizeof header->header.chksum; - signed_sum += ' ' * sizeof header->header.chksum; - - parsed_sum = from_header (header->header.chksum, - sizeof header->header.chksum, 0, - (uintmax_t) 0, - (uintmax_t) TYPE_MAXIMUM (int)); - if (parsed_sum == (uintmax_t) -1) - return HEADER_FAILURE; - - recorded_sum = parsed_sum; - - if (unsigned_sum != recorded_sum && signed_sum != recorded_sum) - return HEADER_FAILURE; + if ((status = tar_checksum (header)) != HEADER_SUCCESS) + return status; /* Good block. Decode file size and return. */ @@ -346,7 +384,7 @@ read_header (bool raw_extended_headers) xalloc_die (); header_copy = xmalloc (size + 1); - + if (header->header.typeflag == GNUTYPE_LONGNAME) { if (next_long_name) @@ -361,7 +399,7 @@ read_header (bool raw_extended_headers) next_long_link = header_copy; next_long_link_blocks = size / BLOCKSIZE; } - + set_next_block_after (header); *header_copy = *header; bp = header_copy->buffer + BLOCKSIZE; @@ -377,7 +415,7 @@ read_header (bool raw_extended_headers) written = available_space_after (data_block); if (written > size) written = size; - + memcpy (bp, data_block->buffer, written); bp += written; set_next_block_after ((union block *) @@ -386,10 +424,14 @@ read_header (bool raw_extended_headers) *bp = '\0'; } - else if (header->header.typeflag == XHDTYPE - || header->header.typeflag == XGLTYPE) + else if (header->header.typeflag == XHDTYPE) xheader_read (header, OFF_FROM_HEADER (header->header.size)); - + else if (header->header.typeflag == XGLTYPE) + { + xheader_read (header, OFF_FROM_HEADER (header->header.size)); + xheader_decode_global (); + } + /* Loop! */ } @@ -502,11 +544,13 @@ decode_header (union block *header, struct tar_stat_info *stat_info, stat_info->stat.st_mode = MODE_FROM_HEADER (header->header.mode); stat_info->stat.st_mtime = TIME_FROM_HEADER (header->header.mtime); - assign_string (&stat_info->uname, header->header.uname); - assign_string (&stat_info->gname, header->header.gname); + assign_string (&stat_info->uname, + header->header.uname[0] ? header->header.uname : NULL); + assign_string (&stat_info->gname, + header->header.gname[0] ? header->header.gname : NULL); stat_info->devmajor = MAJOR_FROM_HEADER (header->header.devmajor); stat_info->devminor = MINOR_FROM_HEADER (header->header.devminor); - + stat_info->stat.st_atime = start_time; stat_info->stat.st_ctime = start_time; @@ -545,12 +589,13 @@ decode_header (union block *header, struct tar_stat_info *stat_info, || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid)) stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid); } - + switch (header->header.typeflag) { case BLKTYPE: case CHRTYPE: - stat_info->stat.st_rdev = makedev (stat_info->devmajor, stat_info->devminor); + stat_info->stat.st_rdev = makedev (stat_info->devmajor, + stat_info->devminor); break; default: @@ -558,9 +603,16 @@ decode_header (union block *header, struct tar_stat_info *stat_info, } } - current_stat_info.archive_file_size = current_stat_info.stat.st_size; - if (extended_header.size) - xheader_decode (stat_info); + stat_info->archive_file_size = stat_info->stat.st_size; + xheader_decode (stat_info); + + if (sparse_member_p (stat_info)) + { + sparse_fixup_header (stat_info); + stat_info->is_sparse = true; + } + else + stat_info->is_sparse = false; } /* Convert buffer at WHERE0 of size DIGS from external format to @@ -887,7 +939,7 @@ tartime (time_t t) #else /* Use ISO 8610 format. See: http://www.cl.cam.ac.uk/~mgk25/iso-time.html */ - struct tm *tm = localtime (&t); + struct tm *tm = utc_option ? gmtime (&t) : localtime (&t); if (tm) { sprintf (buffer, "%04ld-%02d-%02d %02d:%02d:%02d", @@ -941,7 +993,7 @@ print_header (struct tar_stat_info *st, off_t block_ordinal) char modes[11]; char const *time_stamp; char *temp_name = st->orig_file_name ? st->orig_file_name : st->file_name; - + /* These hold formatted ints. */ char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND]; char *user, *group; @@ -1088,16 +1140,10 @@ print_header (struct tar_stat_info *st, off_t block_ordinal) strcat (size, STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf)); break; - case GNUTYPE_SPARSE: - strcpy (size, - STRINGIFY_BIGINT - (UINTMAX_FROM_HEADER (current_header - ->oldgnu_header.realsize), - uintbuf)); - break; + default: /* st->stat.st_size keeps stored file size */ - strcpy (size, STRINGIFY_BIGINT (st->archive_file_size, uintbuf)); + strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf)); break; } @@ -1174,7 +1220,7 @@ print_header (struct tar_stat_info *st, off_t block_ordinal) /* Print a similar line when we make a directory automatically. */ void -print_for_mkdir (char *pathname, int length, mode_t mode) +print_for_mkdir (char *dirname, int length, mode_t mode) { char modes[11]; @@ -1193,7 +1239,7 @@ print_for_mkdir (char *pathname, int length, mode_t mode) } fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH, - _("Creating directory:"), length, quotearg (pathname)); + _("Creating directory:"), length, quotearg (dirname)); } } @@ -1209,6 +1255,19 @@ skip_file (off_t size) save_sizeleft = size; } + if (seekable_archive) + { + off_t nblk = seek_archive (size); + if (nblk >= 0) + { + size -= nblk * BLOCKSIZE; + if (multi_volume_option) /* Argh.. */ + save_sizeleft -= nblk * BLOCKSIZE; + } + else + seekable_archive = false; + } + while (size > 0) { x = find_next_block (); @@ -1222,27 +1281,18 @@ skip_file (off_t size) } } -/* Skip the current member in the archive. */ +/* Skip the current member in the archive. + NOTE: Current header must be decoded before calling this function. */ void skip_member (void) { char save_typeflag = current_header->header.typeflag; set_next_block_after (current_header); + + assign_string (&save_name, current_stat_info.file_name); - if (current_format == OLDGNU_FORMAT - && current_header->oldgnu_header.isextended) - { - union block *exhdr; - do - { - exhdr = find_next_block (); - if (!exhdr) - FATAL_ERROR ((0, 0, _("Unexpected EOF in archive"))); - set_next_block_after (exhdr); - } - while (exhdr->sparse_header.isextended); - } - - if (save_typeflag != DIRTYPE) + if (current_stat_info.is_sparse) + sparse_skip_file (¤t_stat_info); + else if (save_typeflag != DIRTYPE) skip_file (current_stat_info.stat.st_size); }