+clear_read_error_count (void)
+{
+ read_error_count = 0;
+}
+
+\f
+/* Time-related functions */
+
+double duration;
+
+void
+set_start_time (void)
+{
+ 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 (void)
+{
+ 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_none, /* Unknown compression type */
+ ct_tar, /* Plain tar file */
+ ct_compress,
+ ct_gzip,
+ ct_bzip2,
+ ct_lzip,
+ ct_lzma,
+ ct_lzop,
+ ct_xz
+};
+
+static enum compress_type archive_compression_type = ct_none;
+
+struct zip_magic
+{
+ enum compress_type type;
+ size_t length;
+ char const *magic;
+};
+
+struct zip_program
+{
+ enum compress_type type;
+ char const *program;
+ char const *option;
+};
+
+static struct zip_magic const magic[] = {
+ { ct_none, },
+ { ct_tar },
+ { ct_compress, 2, "\037\235" },
+ { ct_gzip, 2, "\037\213" },
+ { ct_bzip2, 3, "BZh" },
+ { ct_lzip, 4, "LZIP" },
+ { ct_lzma, 6, "\xFFLZMA" },
+ { ct_lzop, 4, "\211LZO" },
+ { ct_xz, 6, "\xFD" "7zXZ" },
+};
+
+#define NMAGIC (sizeof(magic)/sizeof(magic[0]))
+
+static struct zip_program zip_program[] = {
+ { ct_compress, COMPRESS_PROGRAM, "-Z" },
+ { ct_compress, GZIP_PROGRAM, "-z" },
+ { ct_gzip, GZIP_PROGRAM, "-z" },
+ { ct_bzip2, BZIP2_PROGRAM, "-j" },
+ { ct_bzip2, "lbzip2", "-j" },
+ { ct_lzip, LZIP_PROGRAM, "--lzip" },
+ { ct_lzma, LZMA_PROGRAM, "--lzma" },
+ { ct_lzma, XZ_PROGRAM, "-J" },
+ { ct_lzop, LZOP_PROGRAM, "--lzop" },
+ { ct_xz, XZ_PROGRAM, "-J" },
+ { ct_none }
+};
+
+static struct zip_program const *
+find_zip_program (enum compress_type type, int *pstate)
+{
+ int i;
+
+ for (i = *pstate; zip_program[i].type != ct_none; i++)
+ {
+ if (zip_program[i].type == type)
+ {
+ *pstate = i + 1;
+ return zip_program + i;
+ }
+ }
+ *pstate = i;
+ return NULL;
+}
+
+const char *
+first_decompress_program (int *pstate)
+{
+ struct zip_program const *zp;
+
+ if (use_compress_program_option)
+ return use_compress_program_option;
+
+ if (archive_compression_type == ct_none)
+ return NULL;
+
+ *pstate = 0;
+ zp = find_zip_program (archive_compression_type, pstate);
+ return zp ? zp->program : NULL;
+}
+
+const char *
+next_decompress_program (int *pstate)
+{
+ struct zip_program const *zp;
+
+ if (use_compress_program_option)
+ return NULL;
+ zp = find_zip_program (archive_compression_type, pstate);
+ return zp ? zp->program : NULL;
+}
+
+static const char *
+compress_option (enum compress_type type)
+{
+ struct zip_program const *zp;
+ int i = 0;
+ zp = find_zip_program (type, &i);
+ return zp ? zp->option : NULL;
+}
+
+/* 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_compression_program_by_suffix (archive_name_array[0], NULL);
+ if (!use_compress_program_option)
+ return archive;
+ break;
+
+ default:
+ archive_compression_type = 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)