+ return true;
+}
+
+static bool
+read_header0 (struct tar_stat_info *info)
+{
+ enum read_header rc;
+
+ tar_stat_init (info);
+ rc = read_header (¤t_header, info, read_header_auto);
+ if (rc == HEADER_SUCCESS)
+ {
+ set_next_block_after (current_header);
+ return true;
+ }
+ ERROR ((0, 0, _("This does not look like a tar archive")));
+ return false;
+}
+
+static bool
+try_new_volume (void)
+{
+ size_t status;
+ union block *header;
+ enum access_mode acc;
+
+ switch (subcommand_option)
+ {
+ case APPEND_SUBCOMMAND:
+ case CAT_SUBCOMMAND:
+ case UPDATE_SUBCOMMAND:
+ acc = ACCESS_UPDATE;
+ break;
+
+ default:
+ acc = ACCESS_READ;
+ break;
+ }
+
+ if (!new_volume (acc))
+ return true;
+
+ while ((status = rmtread (archive, record_start->buffer, record_size))
+ == SAFE_READ_ERROR)
+ archive_read_error ();
+
+ if (status != record_size)
+ short_read (status);
+
+ header = find_next_block ();
+ if (!header)
+ return false;
+
+ switch (header->header.typeflag)
+ {
+ case XGLTYPE:
+ {
+ tar_stat_init (&dummy);
+ if (read_header (&header, &dummy, read_header_x_global)
+ != HEADER_SUCCESS_EXTENDED)
+ {
+ ERROR ((0, 0, _("This does not look like a tar archive")));
+ return false;
+ }
+
+ xheader_decode (&dummy); /* decodes values from the global header */
+ tar_stat_destroy (&dummy);
+
+ /* The initial global header must be immediately followed by
+ an extended PAX header for the first member in this volume.
+ However, in some cases tar may split volumes in the middle
+ of a PAX header. This is incorrect, and should be fixed
+ in the future versions. In the meantime we must be
+ prepared to correctly list and extract such archives.
+
+ If this happens, the following call to read_header returns
+ HEADER_FAILURE, which is ignored.
+
+ See also tests/multiv07.at */
+
+ switch (read_header (&header, &dummy, read_header_auto))
+ {
+ case HEADER_SUCCESS:
+ set_next_block_after (header);
+ break;
+
+ case HEADER_FAILURE:
+ break;
+
+ default:
+ ERROR ((0, 0, _("This does not look like a tar archive")));
+ return false;
+ }
+ break;
+ }
+
+ case GNUTYPE_VOLHDR:
+ if (!read_header0 (&dummy))
+ return false;
+ tar_stat_destroy (&dummy);
+ assign_string (&volume_label, current_header->header.name);
+ set_next_block_after (header);
+ header = find_next_block ();
+ if (header->header.typeflag != GNUTYPE_MULTIVOL)
+ break;
+ /* FALL THROUGH */
+
+ case GNUTYPE_MULTIVOL:
+ if (!read_header0 (&dummy))
+ return false;
+ tar_stat_destroy (&dummy);
+ assign_string (&continued_file_name, current_header->header.name);
+ continued_file_size =
+ UINTMAX_FROM_HEADER (current_header->header.size);
+ continued_file_offset =
+ UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset);
+ break;
+
+ default:
+ break;
+ }
+
+ if (bufmap_head)
+ {
+ uintmax_t s;
+ if (!continued_file_name
+ || strcmp (continued_file_name, bufmap_head->file_name))
+ {
+ if ((archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
+ && strlen (bufmap_head->file_name) >= NAME_FIELD_SIZE
+ && strncmp (continued_file_name, bufmap_head->file_name,
+ NAME_FIELD_SIZE) == 0)
+ WARN ((0, 0,
+ _("%s is possibly continued on this volume: header contains truncated name"),
+ quote (bufmap_head->file_name)));
+ else
+ {
+ WARN ((0, 0, _("%s is not continued on this volume"),
+ quote (bufmap_head->file_name)));
+ return false;
+ }
+ }
+
+ s = continued_file_size + continued_file_offset;
+
+ if (bufmap_head->sizetotal != s || s < continued_file_offset)
+ {
+ char totsizebuf[UINTMAX_STRSIZE_BOUND];
+ char s1buf[UINTMAX_STRSIZE_BOUND];
+ char s2buf[UINTMAX_STRSIZE_BOUND];
+
+ WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
+ quote (continued_file_name),
+ STRINGIFY_BIGINT (bufmap_head->sizetotal, totsizebuf),
+ STRINGIFY_BIGINT (continued_file_size, s1buf),
+ STRINGIFY_BIGINT (continued_file_offset, s2buf)));
+ return false;
+ }
+
+ if (bufmap_head->sizetotal - bufmap_head->sizeleft !=
+ continued_file_offset)
+ {
+ char totsizebuf[UINTMAX_STRSIZE_BOUND];
+ char s1buf[UINTMAX_STRSIZE_BOUND];
+ char s2buf[UINTMAX_STRSIZE_BOUND];
+
+ WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
+ STRINGIFY_BIGINT (bufmap_head->sizetotal, totsizebuf),
+ STRINGIFY_BIGINT (bufmap_head->sizeleft, s1buf),
+ STRINGIFY_BIGINT (continued_file_offset, s2buf)));
+
+ return false;
+ }
+ }
+
+ increase_volume_number ();
+ return true;
+}
+
+\f
+#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 (const char *label)
+{
+ char *string;
+ bool result = false;
+
+ if (fnmatch (volume_label_option, label, 0) == 0)
+ return true;
+
+ if (!multi_volume_option)
+ return false;
+
+ string = drop_volume_label_suffix (label);
+ if (string)
+ {
+ result = fnmatch (string, volume_label_option, 0) == 0;
+ free (string);
+ }
+ return result;
+}
+
+/* Check if the next block contains a volume label and if this matches
+ the one given in the command line */
+static void
+match_volume_label (void)
+{
+ 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 (volume_label))
+ FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
+ quote_n (0, volume_label),
+ quote_n (1, volume_label_option)));
+}
+
+/* Mark the archive with volume label STR. */
+static void
+_write_volume_label (const char *str)
+{
+ if (archive_format == POSIX_FORMAT)
+ xheader_store ("GNU.volume.label", &dummy, str);
+ else
+ {
+ union block *label = find_next_block ();
+
+ memset (label, 0, BLOCKSIZE);
+
+ strcpy (label->header.name, str);
+ assign_string (¤t_stat_info.file_name,
+ label->header.name);
+ current_stat_info.had_trailing_slash =
+ strip_trailing_slashes (current_stat_info.file_name);
+
+ label->header.typeflag = GNUTYPE_VOLHDR;
+ TIME_TO_CHARS (start_time.tv_sec, label->header.mtime);
+ finish_header (¤t_stat_info, label, -1);
+ set_next_block_after (label);
+ }
+}
+
+#define VOL_SUFFIX "Volume"
+
+/* Add a volume label to a part of multi-volume archive */
+static void
+add_volume_label (void)
+{
+ char buf[UINTMAX_STRSIZE_BOUND];
+ char *p = STRINGIFY_BIGINT (volno, buf);
+ char *s = xmalloc (strlen (volume_label_option) + sizeof VOL_SUFFIX
+ + strlen (p) + 2);
+ sprintf (s, "%s %s %s", volume_label_option, VOL_SUFFIX, p);
+ _write_volume_label (s);
+ free (s);