X-Git-Url: https://git.brokenzipper.com/gitweb?a=blobdiff_plain;f=src%2Fbuffer.c;h=f419dd7bcb357232ca76f254a78417c0a6794142;hb=28f2669b15f2ddf5a25e4d7a7f36023ae0268c8b;hp=c544ee00d85b5b9a862035dadbce8636f57f52c5;hpb=1d79c6734cfbd302e53358760b4c3fe3e7b9be61;p=chaz%2Ftar diff --git a/src/buffer.c b/src/buffer.c index c544ee0..f419dd7 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1,7 +1,8 @@ /* Buffer management for tar. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001, - 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. + 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software + Foundation, Inc. Written by John Gilmore, on 1985-08-25. @@ -35,9 +36,6 @@ /* Number of retries before giving up on read. */ #define READ_ERROR_MAX 10 -/* Globbing pattern to append to volume label if initial match failed. */ -#define VOLUME_LABEL_APPEND " Volume [1-9]*" - /* Variables. */ static tarlong prev_written; /* bytes written on previous volumes */ @@ -204,7 +202,8 @@ enum compress_type { ct_gzip, ct_bzip2, ct_lzma, - ct_lzop + ct_lzop, + ct_xz }; struct zip_magic @@ -219,11 +218,12 @@ struct zip_magic static struct zip_magic const magic[] = { { ct_tar }, { ct_none, }, - { ct_compress, 2, "\037\235", "compress", "-Z" }, - { ct_gzip, 2, "\037\213", "gzip", "-z" }, - { ct_bzip2, 3, "BZh", "bzip2", "-j" }, - { ct_lzma, 6, "\xFFLZMA", "lzma", "-J" }, /* FIXME: ???? */ - { ct_lzop, 4, "\211LZO", "lzop", "--lzop" }, + { ct_compress, 2, "\037\235", COMPRESS_PROGRAM, "-Z" }, + { ct_gzip, 2, "\037\213", GZIP_PROGRAM, "-z" }, + { ct_bzip2, 3, "BZh", BZIP2_PROGRAM, "-j" }, + { ct_lzma, 6, "\xFFLZMA", LZMA_PROGRAM, "--lzma" }, + { ct_lzop, 4, "\211LZO", LZOP_PROGRAM, "--lzop" }, + { ct_xz, 6, "\0xFD7zXZ", XZ_PROGRAM, "-J" }, }; #define NMAGIC (sizeof(magic)/sizeof(magic[0])) @@ -263,6 +263,37 @@ check_compressed_archive (bool *pshort) return ct_none; } +/* Guess if the archive is seekable. */ +static void +guess_seekable_archive () +{ + struct stat st; + + if (subcommand_option == DELETE_SUBCOMMAND) + { + /* The current code in delete.c is based on the assumption that + skip_member() reads all data from the archive. So, we should + make sure it won't use seeks. On the other hand, the same code + depends on the ability to backspace a record in the archive, + so setting seekable_archive to false is technically incorrect. + However, it is tested only in skip_member(), so it's not a + problem. */ + seekable_archive = false; + } + + if (seek_option != -1) + { + seekable_archive = !!seek_option; + return; + } + + if (!multi_volume_option && !use_compress_program_option + && fstat (archive, &st) == 0) + seekable_archive = S_ISREG (st.st_mode); + else + seekable_archive = false; +} + /* Open an archive named archive_name_array[0]. Detect if it is a compressed archive of known type and use corresponding decompression program if so */ @@ -293,7 +324,7 @@ open_compressed_archive () ERROR ((0, 0, _("This does not look like a tar archive"))); set_comression_program_by_suffix (archive_name_array[0], NULL); if (!use_compress_program_option) - return archive; + return archive; break; default: @@ -304,7 +335,7 @@ open_compressed_archive () /* FD is not needed any more */ rmtclose (archive); - + hit_eof = false; /* It might have been set by find_next_block in check_compressed_archive */ @@ -563,6 +594,8 @@ _open_archive (enum access_mode wanted_access) { case ACCESS_READ: archive = open_compressed_archive (); + if (archive >= 0) + guess_seekable_archive (); break; case ACCESS_WRITE: @@ -677,6 +710,19 @@ archive_read_error (void) return; } +static bool +archive_is_dev () +{ + struct stat st; + + if (fstat (archive, &st)) + { + stat_diag (*archive_name_cursor); + return false; + } + return S_ISBLK (st.st_mode) || S_ISCHR (st.st_mode); +} + static void short_read (size_t status) { @@ -686,6 +732,19 @@ short_read (size_t status) more = record_start->buffer + status; left = record_size - status; + if (left && left % BLOCKSIZE == 0 + && verbose_option + && record_start_block == 0 && status != 0 + && archive_is_dev ()) + { + unsigned long rsize = status / BLOCKSIZE; + WARN ((0, 0, + ngettext ("Record size = %lu block", + "Record size = %lu blocks", + rsize), + rsize)); + } + while (left % BLOCKSIZE != 0 || (left && status && read_full_records)) { @@ -707,26 +766,10 @@ short_read (size_t status) rest)); } - /* User warned us about this. Fix up. */ - left -= status; more += status; } - /* FIXME: for size=0, multi-volume support. On the first record, warn - about the problem. */ - - if (!read_full_records && verbose_option > 1 - && record_start_block == 0 && status != 0) - { - unsigned long rsize = (record_size - left) / BLOCKSIZE; - WARN ((0, 0, - ngettext ("Record size = %lu block", - "Record size = %lu blocks", - rsize), - rsize)); - } - record_end = record_start + (record_size - left) / BLOCKSIZE; records_read++; } @@ -852,8 +895,6 @@ close_archive (void) flush_archive (); } - sys_drain_input_pipe (); - compute_duration (); if (verify_option) verify_volume (); @@ -861,7 +902,7 @@ close_archive (void) if (rmtclose (archive) != 0) close_error (*archive_name_cursor); - sys_wait_for_child (child_pid); + sys_wait_for_child (child_pid, hit_eof); tar_stat_destroy (¤t_stat_info); if (save_name) @@ -1088,6 +1129,7 @@ new_volume (enum access_mode mode) case ACCESS_READ: archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW, rsh_command_option); + guess_seekable_archive (); break; case ACCESS_WRITE: @@ -1123,7 +1165,7 @@ read_header0 (struct tar_stat_info *info) enum read_header rc; tar_stat_init (info); - rc = read_header_primitive (false, info); + rc = read_header (¤t_header, info, false); if (rc == HEADER_SUCCESS) { set_next_block_after (current_header); @@ -1268,31 +1310,57 @@ try_new_volume () } -/* Check the LABEL block against the volume label, seen as a globbing +#define VOLUME_TEXT " Volume " +#define VOLUME_TEXT_LEN (sizeof VOLUME_TEXT - 1) + +char * +drop_volume_label_suffix (const char *label) +{ + const char *p; + size_t len = strlen (label); + + if (len < 1) + return NULL; + + for (p = label + len - 1; p > label && isdigit ((unsigned char) *p); p--) + ; + if (p > label && p - (VOLUME_TEXT_LEN - 1) > label) + { + p -= VOLUME_TEXT_LEN - 1; + if (memcmp (p, VOLUME_TEXT, VOLUME_TEXT_LEN) == 0) + { + char *s = xmalloc ((len = p - label) + 1); + memcpy (s, label, len); + s[len] = 0; + return s; + } + } + + return NULL; +} + +/* Check LABEL against the volume label, seen as a globbing pattern. Return true if the pattern matches. In case of failure, retry matching a volume sequence number before giving up in multi-volume mode. */ static bool -check_label_pattern (union block *label) +check_label_pattern (const char *label) { char *string; bool result; - if (! memchr (label->header.name, '\0', sizeof label->header.name)) - return false; - - if (fnmatch (volume_label_option, label->header.name, 0) == 0) + if (fnmatch (volume_label_option, label, 0) == 0) return true; if (!multi_volume_option) return false; - string = xmalloc (strlen (volume_label_option) - + sizeof VOLUME_LABEL_APPEND + 1); - strcpy (string, volume_label_option); - strcat (string, VOLUME_LABEL_APPEND); - result = fnmatch (string, label->header.name, 0) == 0; - free (string); + string = drop_volume_label_suffix (label); + if (string) + { + result = fnmatch (string, volume_label_option, 0) == 0; + free (string); + } return result; } @@ -1301,14 +1369,43 @@ check_label_pattern (union block *label) static void match_volume_label (void) { - union block *label = find_next_block (); - - if (!label) + if (!volume_label) + { + union block *label = find_next_block (); + + if (!label) + FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"), + quote (volume_label_option))); + if (label->header.typeflag == GNUTYPE_VOLHDR) + { + if (memchr (label->header.name, '\0', sizeof label->header.name)) + assign_string (&volume_label, label->header.name); + else + { + volume_label = xmalloc (sizeof (label->header.name) + 1); + memcpy (volume_label, label->header.name, + sizeof (label->header.name)); + volume_label[sizeof (label->header.name)] = 0; + } + } + else if (label->header.typeflag == XGLTYPE) + { + struct tar_stat_info st; + tar_stat_init (&st); + xheader_read (&st.xhdr, label, + OFF_FROM_HEADER (label->header.size)); + xheader_decode (&st); + tar_stat_destroy (&st); + } + } + + if (!volume_label) FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"), quote (volume_label_option))); - if (!check_label_pattern (label)) + + if (!check_label_pattern (volume_label)) FATAL_ERROR ((0, 0, _("Volume %s does not match %s"), - quote_n (0, label->header.name), + quote_n (0, volume_label), quote_n (1, volume_label_option))); } @@ -1324,7 +1421,7 @@ _write_volume_label (const char *str) memset (label, 0, BLOCKSIZE); - strcpy (label->header.name, volume_label_option); + strcpy (label->header.name, str); assign_string (¤t_stat_info.file_name, label->header.name); current_stat_info.had_trailing_slash = @@ -1571,6 +1668,9 @@ _gnu_flush_read (void) { while (!try_new_volume ()) ; + if (current_block == record_end) + /* Necessary for blocking_factor == 1 */ + flush_archive(); return; } else if (status == SAFE_READ_ERROR)