-/*---------------------------------------------------------.
-| Set ARCHIVE for uncompressing, then reading an archive. |
-`---------------------------------------------------------*/
-
-static void
-child_open_for_uncompress (void)
-{
- int parent_pipe[2];
- int child_pipe[2];
- pid_t grandchild_pid;
-
- if (pipe (parent_pipe) < 0)
- FATAL_ERROR ((0, errno, _("Cannot open pipe")));
-
- child_pid = fork ();
- if (child_pid < 0)
- FATAL_ERROR ((0, errno, _("Cannot fork")));
-
- if (child_pid > 0)
- {
- /* The parent tar is still here! Just clean up. */
-
- read_full_records_option = 1;
- archive = parent_pipe[PREAD];
- xclose (parent_pipe[PWRITE]);
- return;
- }
-
- /* The new born child tar is here! */
-
- program_name = _("tar (child)");
-
- xdup2 (parent_pipe[PWRITE], STDOUT_FILENO, _("(child) Pipe to stdout"));
- xclose (parent_pipe[PREAD]);
-
- /* Check if we need a grandchild tar. This happens only if either:
- a) we're reading stdin: to force unblocking;
- b) the file is to be accessed by rmt: compressor doesn't know how;
- c) the file is not a plain file. */
-
- if (strcmp (archive_name_array[0], "-") != 0
- && !_remdev (archive_name_array[0])
- && is_regular_file (archive_name_array[0]))
- {
- /* We don't need a grandchild tar. Open the archive and lauch the
- uncompressor. */
-
- archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
- if (archive < 0)
- FATAL_ERROR ((0, errno, _("Cannot open archive %s"),
- archive_name_array[0]));
- xdup2 (archive, STDIN_FILENO, _("Archive to stdin"));
- execlp (use_compress_program_option, use_compress_program_option,
- "-d", (char *) 0);
- FATAL_ERROR ((0, errno, _("Cannot exec %s"),
- use_compress_program_option));
- }
-
- /* We do need a grandchild tar. */
-
- if (pipe (child_pipe) < 0)
- FATAL_ERROR ((0, errno, _("Cannot open pipe")));
-
- grandchild_pid = fork ();
- if (grandchild_pid < 0)
- FATAL_ERROR ((0, errno, _("Child cannot fork")));
-
- if (grandchild_pid > 0)
- {
- /* The child tar is still here! Launch the uncompressor. */
-
- xdup2 (child_pipe[PREAD], STDIN_FILENO, _("((child)) Pipe to stdin"));
- xclose (child_pipe[PWRITE]);
- execlp (use_compress_program_option, use_compress_program_option,
- "-d", (char *) 0);
- FATAL_ERROR ((0, errno, _("Cannot exec %s"),
- use_compress_program_option));
- }
-
- /* The new born grandchild tar is here! */
-
- program_name = _("tar (grandchild)");
-
- /* Prepare for unblocking the data from the archive into the uncompressor. */
-
- xdup2 (child_pipe[PWRITE], STDOUT_FILENO, _("(grandchild) Pipe to stdout"));
- xclose (child_pipe[PREAD]);
-
- if (strcmp (archive_name_array[0], "-") == 0)
- archive = STDIN_FILENO;
- else
- archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
- MODE_RW, rsh_command_option);
- if (archive < 0)
- FATAL_ERROR ((0, errno, _("Cannot open archive %s"),
- archive_name_array[0]));
-
- /* Let's read the archive and pipe it into stdout. */
-
- while (1)
- {
- char *cursor;
- size_t maximum;
- size_t count;
- ssize_t status;
-
- read_error_count = 0;
-
- error_loop:
- status = rmtread (archive, record_start->buffer, record_size);
- if (status < 0)
- {
- read_error ();
- goto error_loop;
- }
- if (status == 0)
- break;
- cursor = record_start->buffer;
- maximum = status;
- while (maximum)
- {
- count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
- status = full_write (STDOUT_FILENO, cursor, count);
- if (status < 0)
- FATAL_ERROR ((0, errno, _("Cannot write to compression program")));
-
- if (status != count)
- {
- ERROR ((0, 0, _("Write to compression program short %lu bytes"),
- (unsigned long) (count - status)));
- count = status;
- }
-
- cursor += count;
- maximum -= count;
- }
- }
-
-#if 0
- close_archive ();
-#endif
- exit (exit_status);
-}
-
-#endif /* not MSDOS */
-
-/*--------------------------------------------------------------------------.
-| Check the LABEL block 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 int
-check_label_pattern (union block *label)
-{
- char *string;
- int result;
-
- if (fnmatch (volume_label_option, label->header.name, 0) == 0)
- return 1;
-
- if (!multi_volume_option)
- return 0;
-
- 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);
- return result;
-}
-
-/*------------------------------------------------------------------------.
-| Open an archive file. The argument specifies whether we are reading or |
-| writing, or both. |
-`------------------------------------------------------------------------*/
-
-void
-open_archive (enum access_mode access)
-{
- int backed_up_flag = 0;
-
- stdlis = to_stdout_option ? stderr : stdout;
-
- if (record_size == 0)
- FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
-
- if (archive_names == 0)
- FATAL_ERROR ((0, 0, _("No archive name given")));
-
- current_file_name = 0;
- current_link_name = 0;
-
- /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
- constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
- size that is reasonable to allocate a buffer. In the GNU system, there
- is no fixed limit. The only correct thing to do is to use dynamic
- allocation. (Roland McGrath) */
-
- if (!real_s_name)
- real_s_name = xmalloc (PATH_MAX);
- /* FIXME: real_s_name is never freed. */
-
- save_name = 0;
-
- if (multi_volume_option)
- {
- record_start = valloc (record_size + (2 * BLOCKSIZE));
- if (record_start)
- record_start += 2;
- }
- else
- record_start = valloc (record_size);
- if (!record_start)
- FATAL_ERROR ((0, 0, _("Could not allocate memory for blocking factor %d"),
- blocking_factor));
-
- current_block = record_start;
- record_end = record_start + blocking_factor;
- /* When updating the archive, we start with reading. */
- access_mode = access == ACCESS_UPDATE ? ACCESS_READ : access;
-
- if (multi_volume_option && verify_option)
- FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
-
- if (use_compress_program_option)
- {
- if (multi_volume_option)
- FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
- if (verify_option)
- FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
-
- switch (access)
- {
- case ACCESS_READ:
- child_open_for_uncompress ();
- break;
-
- case ACCESS_WRITE:
- child_open_for_compress ();
- break;
-
- case ACCESS_UPDATE:
- FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
- break;
- }
-
- if (access == ACCESS_WRITE && strcmp (archive_name_array[0], "-") == 0)
- stdlis = stderr;
- }
- else if (strcmp (archive_name_array[0], "-") == 0)
- {
- read_full_records_option = 1; /* could be a pipe, be safe */
- if (verify_option)
- FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
-
- switch (access)
- {
- case ACCESS_READ:
- archive = STDIN_FILENO;
- break;
-
- case ACCESS_WRITE:
- archive = STDOUT_FILENO;
- stdlis = stderr;
- break;
-
- case ACCESS_UPDATE:
- archive = STDIN_FILENO;
- stdlis = stderr;
- write_archive_to_stdout = 1;
- break;
- }
- }
- else if (verify_option)
- archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
- MODE_RW, rsh_command_option);
- else
- switch (access)
- {
- case ACCESS_READ:
- archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
- MODE_RW, rsh_command_option);
- break;
-
- case ACCESS_WRITE:
- if (backup_option)
- {
- maybe_backup_file (archive_name_array[0], 1);
- backed_up_flag = 1;
- }
- archive = rmtcreat (archive_name_array[0], MODE_RW,
- rsh_command_option);
- break;
-
- case ACCESS_UPDATE:
- archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
- MODE_RW, rsh_command_option);
- break;
- }
-
- if (archive < 0
- || (! _isrmt (archive) && fstat (archive, &archive_stat) < 0))
- {
- int saved_errno = errno;
-
- if (backed_up_flag)
- undo_last_backup ();
- FATAL_ERROR ((0, saved_errno, "%s", archive_name_array[0]));
- }
-
-#if !MSDOS
-
- /* Detect if outputting to "/dev/null". */
- {
- static char const dev_null[] = "/dev/null";
- struct stat dev_null_stat;
-
- dev_null_output =
- (strcmp (archive_name_array[0], dev_null) == 0
- || (! _isrmt (archive)
- && S_ISCHR (archive_stat.st_mode)
- && stat (dev_null, &dev_null_stat) == 0
- && S_ISCHR (dev_null_stat.st_mode)
- && archive_stat.st_rdev == dev_null_stat.st_rdev));
- }
-
- if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
- {
- ar_dev = archive_stat.st_dev;
- ar_ino = archive_stat.st_ino;
- }
- else
- ar_dev = 0;
-
-#endif /* not MSDOS */
-
-#if MSDOS
- setmode (archive, O_BINARY);
-#endif
-
- switch (access)
- {
- case ACCESS_READ:
- case ACCESS_UPDATE:
- record_end = record_start; /* set up for 1st record = # 0 */
- find_next_block (); /* read it in, check for EOF */
-
- if (volume_label_option)
- {
- union block *label = find_next_block ();
-
- if (!label)
- FATAL_ERROR ((0, 0, _("Archive not labeled to match `%s'"),
- volume_label_option));
- if (!check_label_pattern (label))
- FATAL_ERROR ((0, 0, _("Volume `%s' does not match `%s'"),
- label->header.name, volume_label_option));
- }
- break;
-
- case ACCESS_WRITE:
- if (volume_label_option)
- {
- memset (record_start, 0, BLOCKSIZE);
- if (multi_volume_option)
- sprintf (record_start->header.name, "%s Volume 1",
- volume_label_option);
- else
- strcpy (record_start->header.name, volume_label_option);
-
- assign_string (¤t_file_name, record_start->header.name);
-
- record_start->header.typeflag = GNUTYPE_VOLHDR;
- TIME_TO_CHARS (start_time, record_start->header.mtime);
- finish_header (record_start);
-#if 0
- current_block++;
-#endif
- }
- break;
- }
-}
-
-/*--------------------------------------.
-| Perform a write to flush the buffer. |
-`--------------------------------------*/
-
-void
-flush_write (void)
-{
- int copy_back;
- ssize_t status;
-
- if (checkpoint_option && !(++checkpoint % 10))
- WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
-
- if (tape_length_option && tape_length_option <= bytes_written)
- {
- errno = ENOSPC;
- status = 0;
- }
- else if (dev_null_output)
- status = record_size;
- else
- status = write_archive_buffer ();
- if (status != record_size && !multi_volume_option)
- write_error (status);
-
- if (status > 0)
- bytes_written += status;
-
- if (status == record_size)
- {
- if (multi_volume_option)
- {
- char *cursor;
-
- if (!save_name)
- {
- real_s_name[0] = '\0';
- real_s_totsize = 0;
- real_s_sizeleft = 0;
- return;
- }
-
- cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
- while (*cursor == '/')
- cursor++;
-
- strcpy (real_s_name, cursor);
- real_s_totsize = save_totsize;
- real_s_sizeleft = save_sizeleft;
- }
- return;
- }
-
- /* We're multivol. Panic if we didn't get the right kind of response. */
-
- /* ENXIO is for the UNIX PC. */
- if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
- write_error (status);
-
- /* If error indicates a short write, we just move to the next tape. */
-
- if (!new_volume (ACCESS_WRITE))
- return;
-
- if (totals_option)
- prev_written += bytes_written;
- bytes_written = 0;
-
- if (volume_label_option && real_s_name[0])
- {
- copy_back = 2;
- record_start -= 2;
- }
- else if (volume_label_option || real_s_name[0])
- {
- copy_back = 1;
- record_start--;
- }
- else
- copy_back = 0;
-
- if (volume_label_option)
- {
- memset (record_start, 0, BLOCKSIZE);
- sprintf (record_start->header.name, "%s Volume %d", volume_label_option, volno);
- TIME_TO_CHARS (start_time, record_start->header.mtime);
- record_start->header.typeflag = GNUTYPE_VOLHDR;
- finish_header (record_start);
- }
-
- if (real_s_name[0])
- {
- int tmp;
-
- if (volume_label_option)
- record_start++;
-
- memset (record_start, 0, BLOCKSIZE);
-
- /* FIXME: Michael P Urban writes: [a long name file] is being written
- when a new volume rolls around [...] Looks like the wrong value is
- being preserved in real_s_name, though. */
-
- strcpy (record_start->header.name, real_s_name);
- record_start->header.typeflag = GNUTYPE_MULTIVOL;
- OFF_TO_CHARS (real_s_sizeleft, record_start->header.size);
- OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
- record_start->oldgnu_header.offset);
- tmp = verbose_option;
- verbose_option = 0;
- finish_header (record_start);
- verbose_option = tmp;
-
- if (volume_label_option)
- record_start--;
- }
-
- status = write_archive_buffer ();
- if (status != record_size)
- write_error (status);
-
- bytes_written += status;
-
- if (copy_back)
- {
- record_start += copy_back;
- memcpy (current_block,
- record_start + blocking_factor - copy_back,
- copy_back * BLOCKSIZE);
- current_block += copy_back;
-
- if (real_s_sizeleft >= copy_back * BLOCKSIZE)
- real_s_sizeleft -= copy_back * BLOCKSIZE;
- else if ((real_s_sizeleft + BLOCKSIZE - 1) / BLOCKSIZE <= copy_back)
- real_s_name[0] = '\0';
- else
- {
- char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
-
- while (*cursor == '/')
- cursor++;
-
- strcpy (real_s_name, cursor);
- real_s_sizeleft = save_sizeleft;
- real_s_totsize = save_totsize;
- }
- copy_back = 0;
- }
-}
-
-/*---------------------------------------------------------------------.
-| Handle write errors on the archive. Write errors are always fatal. |
-| Hitting the end of a volume does not cause a write error unless the |
-| write was the first record of the volume. |
-`---------------------------------------------------------------------*/
-
-static void
-write_error (ssize_t status)
-{
- int saved_errno = errno;
-
- /* It might be useful to know how much was written before the error
- occurred. */
- if (totals_option)
- print_total_written ();
-
- if (status < 0)
- FATAL_ERROR ((0, saved_errno, _("Cannot write to %s"),
- *archive_name_cursor));
- else
- FATAL_ERROR ((0, 0, _("Only wrote %lu of %lu bytes to %s"),
- (unsigned long) status, (unsigned long) record_size,
- *archive_name_cursor));
-}
-
-/*-------------------------------------------------------------------.
-| Handle read errors on the archive. If the read should be retried, |
-| returns to the caller. |
-`-------------------------------------------------------------------*/
-
-static void
-read_error (void)
-{
- ERROR ((0, errno, _("Read error on %s"), *archive_name_cursor));
-
- if (record_start_block == 0)
- FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
-
- /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
- then give up on reading the archive. */
-
- if (read_error_count++ > READ_ERROR_MAX)
- FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
- return;
-}
-
-/*-------------------------------------.
-| Perform a read to flush the buffer. |
-`-------------------------------------*/
-
-void
-flush_read (void)
-{
- ssize_t status; /* result from system call */
- size_t left; /* bytes left */
- char *more; /* pointer to next byte to read */