+clear_read_error_count (void)
+{
+ read_error_count = 0;
+}
+
+\f
+/* Time-related functions */
+
+double duration;
+
+void
+set_start_time ()
+{
+ gettime (&start_time);
+ volume_start_time = start_time;
+ last_stat_time = start_time;
+}
+
+static void
+set_volume_start_time (void)
+{
+ gettime (&volume_start_time);
+ last_stat_time = volume_start_time;
+}
+
+void
+compute_duration ()
+{
+ struct timespec now;
+ gettime (&now);
+ duration += ((now.tv_sec - last_stat_time.tv_sec)
+ + (now.tv_nsec - last_stat_time.tv_nsec) / 1e9);
+ gettime (&last_stat_time);
+}
+
+\f
+/* Compression detection */
+
+enum compress_type {
+ ct_tar, /* Plain tar file */
+ ct_none, /* Unknown compression type */
+ ct_compress,
+ ct_gzip,
+ ct_bzip2,
+ ct_lzip,
+ ct_lzma,
+ ct_lzop,
+ ct_xz
+};
+
+struct zip_magic
+{
+ enum compress_type type;
+ size_t length;
+ char const *magic;
+ char const *program;
+ char const *option;
+};
+
+static struct zip_magic const magic[] = {
+ { ct_tar },
+ { ct_none, },
+ { ct_compress, 2, "\037\235", COMPRESS_PROGRAM, "-Z" },
+ { ct_gzip, 2, "\037\213", GZIP_PROGRAM, "-z" },
+ { ct_bzip2, 3, "BZh", BZIP2_PROGRAM, "-j" },
+ { ct_lzip, 4, "LZIP", LZIP_PROGRAM, "--lzip" },
+ { ct_lzma, 6, "\xFFLZMA", LZMA_PROGRAM, "--lzma" },
+ { ct_lzop, 4, "\211LZO", LZOP_PROGRAM, "--lzop" },
+ { ct_xz, 6, "\xFD" "7zXZ", XZ_PROGRAM, "-J" },
+};
+
+#define NMAGIC (sizeof(magic)/sizeof(magic[0]))
+
+#define compress_option(t) magic[t].option
+#define compress_program(t) magic[t].program
+
+/* Check if the file ARCHIVE is a compressed archive. */
+static enum compress_type
+check_compressed_archive (bool *pshort)
+{
+ struct zip_magic const *p;
+ bool sfr;
+ bool temp;
+
+ if (!pshort)
+ pshort = &temp;
+
+ /* Prepare global data needed for find_next_block: */
+ record_end = record_start; /* set up for 1st record = # 0 */
+ sfr = read_full_records;
+ read_full_records = true; /* Suppress fatal error on reading a partial
+ record */
+ *pshort = find_next_block () == 0;
+
+ /* Restore global values */
+ read_full_records = sfr;
+
+ if (tar_checksum (record_start, true) == HEADER_SUCCESS)
+ /* Probably a valid header */
+ return ct_tar;
+
+ for (p = magic + 2; p < magic + NMAGIC; p++)
+ if (memcmp (record_start->buffer, p->magic, p->length) == 0)
+ return p->type;
+
+ return ct_none;
+}
+
+/* Guess if the archive is seekable. */
+static void
+guess_seekable_archive (void)
+{
+ 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 */
+static int
+open_compressed_archive (void)
+{
+ archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
+ MODE_RW, rsh_command_option);
+ if (archive == -1)
+ return archive;
+
+ if (!multi_volume_option)
+ {
+ if (!use_compress_program_option)
+ {
+ bool shortfile;
+ enum compress_type type = check_compressed_archive (&shortfile);
+
+ switch (type)
+ {
+ case ct_tar:
+ if (shortfile)
+ ERROR ((0, 0, _("This does not look like a tar archive")));
+ return archive;
+
+ case ct_none:
+ if (shortfile)
+ 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;
+ break;
+
+ default:
+ use_compress_program_option = compress_program (type);
+ break;
+ }
+ }
+
+ /* FD is not needed any more */
+ rmtclose (archive);
+
+ hit_eof = false; /* It might have been set by find_next_block in
+ check_compressed_archive */
+
+ /* Open compressed archive */
+ child_pid = sys_child_open_for_uncompress ();
+ read_full_records = true;
+ }
+
+ records_read = 0;
+ record_end = record_start; /* set up for 1st record = # 0 */
+
+ return archive;
+}
+\f
+
+static void
+print_stats (FILE *fp, const char *text, tarlong numbytes)