1 /* Buffer management for tar.
3 Copyright 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001 Free
4 Software Foundation, Inc.
6 Written by John Gilmore, on 1985-08-25.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 # define _PATH_BSHELL "/bin/sh"
39 # include <sys/inode.h>
49 #define PREAD 0 /* read file descriptor from pipe() */
50 #define PWRITE 1 /* write file descriptor from pipe() */
52 /* Number of retries before giving up on read. */
53 #define READ_ERROR_MAX 10
55 /* Globbing pattern to append to volume label if initial match failed. */
56 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
60 static tarlong prev_written; /* bytes written on previous volumes */
61 static tarlong bytes_written; /* bytes written on this volume */
63 /* FIXME: The following variables should ideally be static to this
64 module. However, this cannot be done yet. The cleanup continues! */
66 union block *record_start; /* start of record of archive */
67 union block *record_end; /* last+1 block of archive record */
68 union block *current_block; /* current block of archive */
69 enum access_mode access_mode; /* how do we handle the archive */
70 off_t records_read; /* number of records read from this archive */
71 off_t records_written; /* likewise, for records written */
73 static struct stat archive_stat; /* stat block for archive file */
75 static off_t record_start_block; /* block ordinal at record_start */
77 /* Where we write list messages (not errors, not interactions) to. Stdout
78 unless we're writing a pipe, in which case stderr. */
81 static void backspace_output PARAMS ((void));
82 static int new_volume PARAMS ((enum access_mode));
83 static void archive_write_error PARAMS ((ssize_t)) __attribute__ ((noreturn));
84 static void archive_read_error PARAMS ((void));
87 /* Obnoxious test to see if dimwit is trying to dump the archive. */
92 /* PID of child program, if compress_option or remote archive access. */
93 static pid_t child_pid;
95 /* Error recovery stuff */
96 static int read_error_count;
98 /* Have we hit EOF yet? */
101 /* Checkpointing counter */
102 static int checkpoint;
104 /* We're reading, but we just read the last block and its time to update. */
105 /* As least EXTERN like this one as possible. FIXME! */
106 extern int time_to_start_writing;
108 int file_to_switch_to = -1; /* if remote update, close archive, and use
109 this descriptor to write to */
111 static int volno = 1; /* which volume of a multi-volume tape we're
113 static int global_volno = 1; /* volume number to print in external
115 static pid_t grandchild_pid;
117 /* The pointer save_name, which is set in function dump_file() of module
118 create.c, points to the original long filename instead of the new,
119 shorter mangled name that is set in start_header() of module create.c.
120 The pointer save_name is only used in multi-volume mode when the file
121 being processed is non-sparse; if a file is split between volumes, the
122 save_name is used in generating the LF_MULTIVOL record on the second
123 volume. (From Pierce Cantrell, 1991-08-13.) */
125 char *save_name; /* name of the file we are currently writing */
126 off_t save_totsize; /* total size of file we are writing, only
127 valid if save_name is nonzero */
128 off_t save_sizeleft; /* where we are in the file we are writing,
129 only valid if save_name is nonzero */
131 bool write_archive_to_stdout;
133 /* Used by flush_read and flush_write to store the real info about saved
135 static char *real_s_name;
136 static off_t real_s_totsize;
137 static off_t real_s_sizeleft;
142 print_total_written (void)
144 tarlong written = prev_written + bytes_written;
145 char bytes[sizeof (tarlong) * CHAR_BIT];
146 char abbr[LONGEST_HUMAN_READABLE + 1];
147 char rate[LONGEST_HUMAN_READABLE + 1];
150 #if HAVE_CLOCK_GETTIME
152 if (clock_gettime (CLOCK_REALTIME, &now) == 0)
153 seconds = ((now.tv_sec - start_timespec.tv_sec)
154 + (now.tv_nsec - start_timespec.tv_nsec) / 1e9);
157 seconds = time (0) - start_time;
159 sprintf (bytes, TARLONG_FORMAT, written);
161 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
162 fprintf (stderr, _("Total bytes written: %s (%sB, %sB/s)\n"), bytes,
163 human_readable ((uintmax_t) written, abbr, 1, -1024),
164 (0 < seconds && written / seconds < (uintmax_t) -1
165 ? human_readable ((uintmax_t) (written / seconds), rate, 1, -1024)
169 /* Compute and return the block ordinal at current_block. */
171 current_block_ordinal (void)
173 return record_start_block + (current_block - record_start);
176 /* If the EOF flag is set, reset it, as well as current_block, etc. */
183 current_block = record_start;
184 record_end = record_start + blocking_factor;
185 access_mode = ACCESS_WRITE;
189 /* Return the location of the next available input or output block.
190 Return zero for EOF. Once we have returned zero, we just keep returning
191 it, to avoid accidentally going on to the next file on the tape. */
193 find_next_block (void)
195 if (current_block == record_end)
200 if (current_block == record_end)
206 return current_block;
209 /* Indicate that we have used all blocks up thru BLOCK.
210 FIXME: should the arg have an off-by-1? */
212 set_next_block_after (union block *block)
214 while (block >= current_block)
217 /* Do *not* flush the archive here. If we do, the same argument to
218 set_next_block_after could mean the next block (if the input record
219 is exactly one block long), which is not what is intended. */
221 if (current_block > record_end)
225 /* Return the number of bytes comprising the space between POINTER
226 through the end of the current buffer of blocks. This space is
227 available for filling with data, or taking data from. POINTER is
228 usually (but not always) the result previous find_next_block call. */
230 available_space_after (union block *pointer)
232 return record_end->buffer - pointer->buffer;
235 /* Close file having descriptor FD, and abort if close unsuccessful. */
240 close_error (_("(pipe)"));
243 /* Duplicate file descriptor FROM into becoming INTO.
244 INTO is closed first and has to be the next available slot. */
246 xdup2 (int from, int into)
250 int status = close (into);
252 if (status != 0 && errno != EBADF)
255 FATAL_ERROR ((0, e, _("Cannot close")));
263 FATAL_ERROR ((0, e, _("Cannot dup")));
273 /* Set ARCHIVE for writing, then compressing an archive. */
275 child_open_for_compress (void)
277 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
280 /* Set ARCHIVE for uncompressing, then reading an archive. */
282 child_open_for_uncompress (void)
284 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
287 #else /* not MSDOS */
289 /* Return nonzero if NAME is the name of a regular file, or if the file
290 does not exist (so it would be created as a regular file). */
292 is_regular_file (const char *name)
296 if (stat (name, &stbuf) == 0)
297 return S_ISREG (stbuf.st_mode);
299 return errno == ENOENT;
303 write_archive_buffer (void)
308 while (0 <= (status = rmtwrite (archive, record_start->buffer + written,
309 record_size - written)))
312 if (written == record_size
314 || ! (S_ISFIFO (archive_stat.st_mode)
315 || S_ISSOCK (archive_stat.st_mode)))
319 return written ? written : status;
322 /* Set ARCHIVE for writing, then compressing an archive. */
324 child_open_for_compress (void)
331 child_pid = xfork ();
335 /* The parent tar is still here! Just clean up. */
337 archive = parent_pipe[PWRITE];
338 xclose (parent_pipe[PREAD]);
342 /* The new born child tar is here! */
344 program_name = _("tar (child)");
346 xdup2 (parent_pipe[PREAD], STDIN_FILENO);
347 xclose (parent_pipe[PWRITE]);
349 /* Check if we need a grandchild tar. This happens only if either:
350 a) we are writing stdout: to force reblocking;
351 b) the file is to be accessed by rmt: compressor doesn't know how;
352 c) the file is not a plain file. */
354 if (strcmp (archive_name_array[0], "-") != 0
355 && !_remdev (archive_name_array[0])
356 && is_regular_file (archive_name_array[0]))
359 maybe_backup_file (archive_name_array[0], 1);
361 /* We don't need a grandchild tar. Open the archive and launch the
364 archive = creat (archive_name_array[0], MODE_RW);
367 int saved_errno = errno;
372 open_fatal (archive_name_array[0]);
374 xdup2 (archive, STDOUT_FILENO);
375 execlp (use_compress_program_option, use_compress_program_option,
377 exec_fatal (use_compress_program_option);
380 /* We do need a grandchild tar. */
383 grandchild_pid = xfork ();
385 if (grandchild_pid == 0)
387 /* The newborn grandchild tar is here! Launch the compressor. */
389 program_name = _("tar (grandchild)");
391 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
392 xclose (child_pipe[PREAD]);
393 execlp (use_compress_program_option, use_compress_program_option,
395 exec_fatal (use_compress_program_option);
398 /* The child tar is still here! */
400 /* Prepare for reblocking the data from the compressor into the archive. */
402 xdup2 (child_pipe[PREAD], STDIN_FILENO);
403 xclose (child_pipe[PWRITE]);
405 if (strcmp (archive_name_array[0], "-") == 0)
406 archive = STDOUT_FILENO;
409 archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
411 open_fatal (archive_name_array[0]);
414 /* Let's read out of the stdin pipe and write an archive. */
422 /* Assemble a record. */
424 for (length = 0, cursor = record_start->buffer;
425 length < record_size;
426 length += status, cursor += status)
428 size_t size = record_size - length;
430 if (size < BLOCKSIZE)
432 status = safe_read (STDIN_FILENO, cursor, size);
438 read_fatal (use_compress_program_option);
440 /* Copy the record. */
444 /* We hit the end of the file. Write last record at
445 full length, as the only role of the grandchild is
446 doing proper reblocking. */
450 memset (record_start->buffer + length, 0, record_size - length);
451 status = write_archive_buffer ();
452 if (status != record_size)
453 archive_write_error (status);
456 /* There is nothing else to read, break out. */
460 status = write_archive_buffer ();
461 if (status != record_size)
462 archive_write_error (status);
469 /* Propagate any failure of the grandchild back to the parent. */
471 while (waitpid (grandchild_pid, &wait_status, 0) == -1)
474 waitpid_error (use_compress_program_option);
478 if (WIFSIGNALED (wait_status))
480 kill (child_pid, WTERMSIG (wait_status));
481 exit_status = TAREXIT_FAILURE;
483 else if (WEXITSTATUS (wait_status) != 0)
484 exit_status = WEXITSTATUS (wait_status);
490 sig_propagate(int sig)
492 kill (grandchild_pid, sig);
493 exit (TAREXIT_FAILURE);
496 /* Set ARCHIVE for uncompressing, then reading an archive. */
498 child_open_for_uncompress (void)
505 child_pid = xfork ();
509 /* The parent tar is still here! Just clean up. */
511 read_full_records_option = 1;
512 archive = parent_pipe[PREAD];
513 xclose (parent_pipe[PWRITE]);
517 /* The newborn child tar is here! */
519 program_name = _("tar (child)");
521 xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
522 xclose (parent_pipe[PREAD]);
524 /* Check if we need a grandchild tar. This happens only if either:
525 a) we're reading stdin: to force unblocking;
526 b) the file is to be accessed by rmt: compressor doesn't know how;
527 c) the file is not a plain file. */
529 if (strcmp (archive_name_array[0], "-") != 0
530 && !_remdev (archive_name_array[0])
531 && is_regular_file (archive_name_array[0]))
533 /* We don't need a grandchild tar. Open the archive and lauch the
536 archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
538 open_fatal (archive_name_array[0]);
539 xdup2 (archive, STDIN_FILENO);
540 execlp (use_compress_program_option, use_compress_program_option,
542 exec_fatal (use_compress_program_option);
545 /* We do need a grandchild tar. */
548 grandchild_pid = xfork ();
550 if (grandchild_pid == 0)
552 /* The newborn grandchild tar is here! Launch the uncompressor. */
554 program_name = _("tar (grandchild)");
556 xdup2 (child_pipe[PREAD], STDIN_FILENO);
557 xclose (child_pipe[PWRITE]);
558 execlp (use_compress_program_option, use_compress_program_option,
560 exec_fatal (use_compress_program_option);
563 /* The child tar is still here! */
564 signal (SIGTERM, sig_propagate);
566 /* Prepare for unblocking the data from the archive into the
569 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
570 xclose (child_pipe[PREAD]);
572 if (strcmp (archive_name_array[0], "-") == 0)
573 archive = STDIN_FILENO;
575 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
576 MODE_RW, rsh_command_option);
578 open_fatal (archive_name_array[0]);
580 /* Let's read the archive and pipe it into stdout. */
589 read_error_count = 0;
592 status = rmtread (archive, record_start->buffer, record_size);
595 archive_read_error ();
600 cursor = record_start->buffer;
604 count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
605 if (full_write (STDOUT_FILENO, cursor, count) != count)
606 write_error (use_compress_program_option);
612 xclose (STDOUT_FILENO);
617 /* Propagate any failure of the grandchild back to the parent. */
619 while (waitpid (grandchild_pid, &wait_status, 0) == -1)
622 waitpid_error (use_compress_program_option);
626 if (WIFSIGNALED (wait_status))
628 kill (child_pid, WTERMSIG (wait_status));
629 exit_status = TAREXIT_FAILURE;
631 else if (WEXITSTATUS (wait_status) != 0)
632 exit_status = WEXITSTATUS (wait_status);
637 #endif /* not MSDOS */
639 /* Check the LABEL block against the volume label, seen as a globbing
640 pattern. Return true if the pattern matches. In case of failure,
641 retry matching a volume sequence number before giving up in
642 multi-volume mode. */
644 check_label_pattern (union block *label)
649 if (! memchr (label->header.name, '\0', sizeof label->header.name))
652 if (fnmatch (volume_label_option, label->header.name, 0) == 0)
655 if (!multi_volume_option)
658 string = xmalloc (strlen (volume_label_option)
659 + sizeof VOLUME_LABEL_APPEND + 1);
660 strcpy (string, volume_label_option);
661 strcat (string, VOLUME_LABEL_APPEND);
662 result = fnmatch (string, label->header.name, 0) == 0;
667 /* Open an archive file. The argument specifies whether we are
668 reading or writing, or both. */
670 open_archive (enum access_mode wanted_access)
672 int backed_up_flag = 0;
674 stdlis = to_stdout_option ? stderr : stdout;
676 if (record_size == 0)
677 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
679 if (archive_names == 0)
680 FATAL_ERROR ((0, 0, _("No archive name given")));
682 current_file_name = 0;
683 current_link_name = 0;
687 if (multi_volume_option)
690 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
691 record_start = valloc (record_size + (2 * BLOCKSIZE));
696 record_start = valloc (record_size);
698 FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
701 current_block = record_start;
702 record_end = record_start + blocking_factor;
703 /* When updating the archive, we start with reading. */
704 access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
706 if (use_compress_program_option)
708 if (multi_volume_option)
709 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
711 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
713 switch (wanted_access)
716 child_open_for_uncompress ();
720 child_open_for_compress ();
724 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
728 if (wanted_access == ACCESS_WRITE
729 && strcmp (archive_name_array[0], "-") == 0)
732 else if (strcmp (archive_name_array[0], "-") == 0)
734 read_full_records_option = 1; /* could be a pipe, be safe */
736 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
738 switch (wanted_access)
741 archive = STDIN_FILENO;
745 archive = STDOUT_FILENO;
750 archive = STDIN_FILENO;
752 write_archive_to_stdout = 1;
756 else if (verify_option)
757 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
758 MODE_RW, rsh_command_option);
760 switch (wanted_access)
763 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
764 MODE_RW, rsh_command_option);
770 maybe_backup_file (archive_name_array[0], 1);
773 archive = rmtcreat (archive_name_array[0], MODE_RW,
778 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
779 MODE_RW, rsh_command_option);
784 || (! _isrmt (archive) && fstat (archive, &archive_stat) < 0))
786 int saved_errno = errno;
791 open_fatal (archive_name_array[0]);
796 /* Detect if outputting to "/dev/null". */
798 static char const dev_null[] = "/dev/null";
799 struct stat dev_null_stat;
802 (strcmp (archive_name_array[0], dev_null) == 0
803 || (! _isrmt (archive)
804 && S_ISCHR (archive_stat.st_mode)
805 && stat (dev_null, &dev_null_stat) == 0
806 && archive_stat.st_dev == dev_null_stat.st_dev
807 && archive_stat.st_ino == dev_null_stat.st_ino));
810 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
812 ar_dev = archive_stat.st_dev;
813 ar_ino = archive_stat.st_ino;
818 #endif /* not MSDOS */
821 setmode (archive, O_BINARY);
824 switch (wanted_access)
830 record_end = record_start; /* set up for 1st record = # 0 */
831 find_next_block (); /* read it in, check for EOF */
833 if (volume_label_option)
835 union block *label = find_next_block ();
838 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
839 quote (volume_label_option)));
840 if (!check_label_pattern (label))
841 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
842 quote_n (0, label->header.name),
843 quote_n (1, volume_label_option)));
849 if (volume_label_option)
851 memset (record_start, 0, BLOCKSIZE);
852 if (multi_volume_option)
853 sprintf (record_start->header.name, "%s Volume 1",
854 volume_label_option);
856 strcpy (record_start->header.name, volume_label_option);
858 assign_string (¤t_file_name, record_start->header.name);
860 record_start->header.typeflag = GNUTYPE_VOLHDR;
861 TIME_TO_CHARS (start_time, record_start->header.mtime);
862 finish_header (record_start);
871 /* Perform a write to flush the buffer. */
878 if (checkpoint_option && !(++checkpoint % 10))
879 WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
881 if (tape_length_option && tape_length_option <= bytes_written)
886 else if (dev_null_output)
887 status = record_size;
889 status = write_archive_buffer ();
890 if (status != record_size && !multi_volume_option)
891 archive_write_error (status);
896 bytes_written += status;
899 if (status == record_size)
901 if (multi_volume_option)
907 assign_string (&real_s_name, 0);
913 cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
914 while (ISSLASH (*cursor))
917 assign_string (&real_s_name, cursor);
918 real_s_totsize = save_totsize;
919 real_s_sizeleft = save_sizeleft;
924 /* We're multivol. Panic if we didn't get the right kind of response. */
926 /* ENXIO is for the UNIX PC. */
927 if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
928 archive_write_error (status);
930 /* If error indicates a short write, we just move to the next tape. */
932 if (!new_volume (ACCESS_WRITE))
936 prev_written += bytes_written;
939 if (volume_label_option && real_s_name)
944 else if (volume_label_option || real_s_name)
952 if (volume_label_option)
954 memset (record_start, 0, BLOCKSIZE);
955 sprintf (record_start->header.name, "%s Volume %d",
956 volume_label_option, volno);
957 TIME_TO_CHARS (start_time, record_start->header.mtime);
958 record_start->header.typeflag = GNUTYPE_VOLHDR;
959 finish_header (record_start);
966 if (volume_label_option)
969 memset (record_start, 0, BLOCKSIZE);
971 /* FIXME: Michael P Urban writes: [a long name file] is being written
972 when a new volume rolls around [...] Looks like the wrong value is
973 being preserved in real_s_name, though. */
975 strcpy (record_start->header.name, real_s_name);
976 record_start->header.typeflag = GNUTYPE_MULTIVOL;
977 OFF_TO_CHARS (real_s_sizeleft, record_start->header.size);
978 OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
979 record_start->oldgnu_header.offset);
980 tmp = verbose_option;
982 finish_header (record_start);
983 verbose_option = tmp;
985 if (volume_label_option)
989 status = write_archive_buffer ();
990 if (status != record_size)
991 archive_write_error (status);
993 bytes_written += status;
997 record_start += copy_back;
998 memcpy (current_block,
999 record_start + blocking_factor - copy_back,
1000 copy_back * BLOCKSIZE);
1001 current_block += copy_back;
1003 if (real_s_sizeleft >= copy_back * BLOCKSIZE)
1004 real_s_sizeleft -= copy_back * BLOCKSIZE;
1005 else if ((real_s_sizeleft + BLOCKSIZE - 1) / BLOCKSIZE <= copy_back)
1006 assign_string (&real_s_name, 0);
1009 char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
1011 while (ISSLASH (*cursor))
1014 assign_string (&real_s_name, cursor);
1015 real_s_sizeleft = save_sizeleft;
1016 real_s_totsize = save_totsize;
1022 /* Handle write errors on the archive. Write errors are always fatal.
1023 Hitting the end of a volume does not cause a write error unless the
1024 write was the first record of the volume. */
1026 archive_write_error (ssize_t status)
1028 /* It might be useful to know how much was written before the error
1033 print_total_written ();
1037 write_fatal_details (*archive_name_cursor, status, record_size);
1040 /* Handle read errors on the archive. If the read should be retried,
1041 return to the caller. */
1043 archive_read_error (void)
1045 read_error (*archive_name_cursor);
1047 if (record_start_block == 0)
1048 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1050 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1051 then give up on reading the archive. */
1053 if (read_error_count++ > READ_ERROR_MAX)
1054 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1058 /* Perform a read to flush the buffer. */
1062 ssize_t status; /* result from system call */
1063 size_t left; /* bytes left */
1064 char *more; /* pointer to next byte to read */
1066 if (checkpoint_option && !(++checkpoint % 10))
1067 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
1069 /* Clear the count of errors. This only applies to a single call to
1072 read_error_count = 0; /* clear error count */
1074 if (write_archive_to_stdout && record_start_block != 0)
1076 archive = STDOUT_FILENO;
1077 status = write_archive_buffer ();
1078 archive = STDIN_FILENO;
1079 if (status != record_size)
1080 archive_write_error (status);
1082 if (multi_volume_option)
1086 char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
1088 while (ISSLASH (*cursor))
1091 assign_string (&real_s_name, cursor);
1092 real_s_sizeleft = save_sizeleft;
1093 real_s_totsize = save_totsize;
1097 assign_string (&real_s_name, 0);
1099 real_s_sizeleft = 0;
1104 status = rmtread (archive, record_start->buffer, record_size);
1105 if (status == record_size)
1112 || (status < 0 && errno == ENOSPC)
1113 || (status > 0 && !read_full_records_option))
1114 && multi_volume_option)
1116 union block *cursor;
1119 switch (subcommand_option)
1121 case APPEND_SUBCOMMAND:
1122 case CAT_SUBCOMMAND:
1123 case UPDATE_SUBCOMMAND:
1124 if (!new_volume (ACCESS_UPDATE))
1129 if (!new_volume (ACCESS_READ))
1135 status = rmtread (archive, record_start->buffer, record_size);
1138 archive_read_error ();
1141 if (status != record_size)
1144 cursor = record_start;
1146 if (cursor->header.typeflag == GNUTYPE_VOLHDR)
1148 if (volume_label_option)
1150 if (!check_label_pattern (cursor))
1152 WARN ((0, 0, _("Volume %s does not match %s"),
1153 quote_n (0, cursor->header.name),
1154 quote_n (1, volume_label_option)));
1161 fprintf (stdlis, _("Reading %s\n"), quote (cursor->header.name));
1164 else if (volume_label_option)
1165 WARN ((0, 0, _("WARNING: No volume header")));
1170 if (cursor->header.typeflag != GNUTYPE_MULTIVOL
1171 || strcmp (cursor->header.name, real_s_name))
1173 WARN ((0, 0, _("%s is not continued on this volume"),
1174 quote (real_s_name)));
1179 s1 = UINTMAX_FROM_HEADER (cursor->header.size);
1180 s2 = UINTMAX_FROM_HEADER (cursor->oldgnu_header.offset);
1181 if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
1183 char totsizebuf[UINTMAX_STRSIZE_BOUND];
1184 char s1buf[UINTMAX_STRSIZE_BOUND];
1185 char s2buf[UINTMAX_STRSIZE_BOUND];
1187 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1188 quote (cursor->header.name),
1189 STRINGIFY_BIGINT (save_totsize, totsizebuf),
1190 STRINGIFY_BIGINT (s1, s1buf),
1191 STRINGIFY_BIGINT (s2, s2buf)));
1196 if (real_s_totsize - real_s_sizeleft
1197 != OFF_FROM_HEADER (cursor->oldgnu_header.offset))
1199 WARN ((0, 0, _("This volume is out of sequence")));
1206 current_block = cursor;
1210 else if (status < 0)
1212 archive_read_error ();
1213 goto error_loop; /* try again */
1217 more = record_start->buffer + status;
1218 left = record_size - status;
1220 while (left % BLOCKSIZE != 0
1221 || (left && status && read_full_records_option))
1224 while ((status = rmtread (archive, more, left)) < 0)
1225 archive_read_error ();
1230 if (! read_full_records_option)
1231 FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
1232 (unsigned long) (record_size - left)));
1234 /* User warned us about this. Fix up. */
1240 /* FIXME: for size=0, multi-volume support. On the first record, warn
1241 about the problem. */
1243 if (!read_full_records_option && verbose_option
1244 && record_start_block == 0 && status > 0)
1245 WARN ((0, 0, _("Record size = %lu blocks"),
1246 (unsigned long) ((record_size - left) / BLOCKSIZE)));
1248 record_end = record_start + (record_size - left) / BLOCKSIZE;
1252 /* Flush the current buffer to/from the archive. */
1254 flush_archive (void)
1256 record_start_block += record_end - record_start;
1257 current_block = record_start;
1258 record_end = record_start + blocking_factor;
1260 if (access_mode == ACCESS_READ && time_to_start_writing)
1262 access_mode = ACCESS_WRITE;
1263 time_to_start_writing = 0;
1265 if (file_to_switch_to >= 0)
1267 if (rmtclose (archive) != 0)
1268 close_warn (*archive_name_cursor);
1270 archive = file_to_switch_to;
1273 backspace_output ();
1276 switch (access_mode)
1291 /* Backspace the archive descriptor by one record worth. If it's a
1292 tape, MTIOCTOP will work. If it's something else, try to seek on
1293 it. If we can't seek, we lose! */
1295 backspace_output (void)
1299 struct mtop operation;
1301 operation.mt_op = MTBSR;
1302 operation.mt_count = 1;
1303 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1305 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1311 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
1313 /* Seek back to the beginning of this record and start writing there. */
1315 position -= record_size;
1318 if (rmtlseek (archive, position, SEEK_SET) != position)
1320 /* Lseek failed. Try a different method. */
1323 _("Cannot backspace archive file; it may be unreadable without -i")));
1325 /* Replace the first part of the record with NULs. */
1327 if (record_start->buffer != output_start)
1328 memset (record_start->buffer, 0,
1329 output_start - record_start->buffer);
1334 /* Close the archive file. */
1336 close_archive (void)
1338 if (time_to_start_writing || access_mode == ACCESS_WRITE)
1343 /* Manage to fully drain a pipe we might be reading, so to not break it on
1344 the producer after the EOF block. FIXME: one of these days, GNU tar
1345 might become clever enough to just stop working, once there is no more
1346 work to do, we might have to revise this area in such time. */
1348 if (fast_read_option && namelist_freed && child_pid > 0)
1349 kill(child_pid, SIGTERM);
1351 if (access_mode == ACCESS_READ
1352 && ! _isrmt (archive)
1353 && (S_ISFIFO (archive_stat.st_mode) || S_ISSOCK (archive_stat.st_mode)))
1354 while (rmtread (archive, record_start->buffer, record_size) > 0)
1361 if (rmtclose (archive) != 0)
1362 close_warn (*archive_name_cursor);
1370 while (waitpid (child_pid, &wait_status, 0) == -1)
1373 waitpid_error (use_compress_program_option);
1377 if (!fast_read_option || !namelist_freed)
1378 if (WIFSIGNALED (wait_status))
1379 ERROR ((0, 0, _("Child died with signal %d"),
1380 WTERMSIG (wait_status)));
1381 else if (WEXITSTATUS (wait_status) != 0)
1382 ERROR ((0, 0, _("Child returned status %d"),
1383 WEXITSTATUS (wait_status)));
1387 if (current_file_name)
1388 free (current_file_name);
1389 if (current_link_name)
1390 free (current_link_name);
1395 free (multi_volume_option ? record_start - 2 : record_start);
1398 /* Called to initialize the global volume number. */
1400 init_volume_number (void)
1402 FILE *file = fopen (volno_file_option, "r");
1406 if (fscanf (file, "%d", &global_volno) != 1
1407 || global_volno < 0)
1408 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1409 quotearg_colon (volno_file_option)));
1411 read_error (volno_file_option);
1412 if (fclose (file) != 0)
1413 close_error (volno_file_option);
1415 else if (errno != ENOENT)
1416 open_error (volno_file_option);
1419 /* Called to write out the closing global volume number. */
1421 closeout_volume_number (void)
1423 FILE *file = fopen (volno_file_option, "w");
1427 fprintf (file, "%d\n", global_volno);
1429 write_error (volno_file_option);
1430 if (fclose (file) != 0)
1431 close_error (volno_file_option);
1434 open_error (volno_file_option);
1437 /* We've hit the end of the old volume. Close it and open the next one.
1438 Return nonzero on success. */
1440 new_volume (enum access_mode access)
1442 static FILE *read_file;
1445 if (!read_file && !info_script_option)
1446 /* FIXME: if fopen is used, it will never be closed. */
1447 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
1454 if (rmtclose (archive) != 0)
1455 close_warn (*archive_name_cursor);
1458 if (global_volno < 0)
1459 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1461 archive_name_cursor++;
1462 if (archive_name_cursor == archive_name_array + archive_names)
1464 archive_name_cursor = archive_name_array;
1471 /* We have to prompt from now on. */
1473 if (info_script_option)
1475 if (volno_file_option)
1476 closeout_volume_number ();
1477 if (system (info_script_option) != 0)
1478 FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option));
1483 char input_buffer[80];
1485 fputc ('\007', stderr);
1487 _("Prepare volume #%d for %s and hit return: "),
1488 global_volno, quote (*archive_name_cursor));
1491 if (fgets (input_buffer, sizeof input_buffer, read_file) == 0)
1493 WARN ((0, 0, _("EOF where user reply was expected")));
1495 if (subcommand_option != EXTRACT_SUBCOMMAND
1496 && subcommand_option != LIST_SUBCOMMAND
1497 && subcommand_option != DIFF_SUBCOMMAND)
1498 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1502 if (input_buffer[0] == '\n'
1503 || input_buffer[0] == 'y'
1504 || input_buffer[0] == 'Y')
1507 switch (input_buffer[0])
1511 fprintf (stderr, _("\
1512 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1514 ! Spawn a subshell\n\
1515 ? Print this list\n"));
1522 WARN ((0, 0, _("No new volume; exiting.\n")));
1524 if (subcommand_option != EXTRACT_SUBCOMMAND
1525 && subcommand_option != LIST_SUBCOMMAND
1526 && subcommand_option != DIFF_SUBCOMMAND)
1527 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1532 /* Get new file name. */
1535 char *name = &input_buffer[1];
1538 while (*name == ' ' || *name == '\t')
1541 while (*cursor && *cursor != '\n')
1545 /* FIXME: the following allocation is never reclaimed. */
1546 *archive_name_cursor = xstrdup (name);
1552 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
1553 #else /* not MSDOS */
1556 const char *shell = getenv ("SHELL");
1558 shell = _PATH_BSHELL;
1562 execlp (shell, "-sh", "-i", (char *) 0);
1568 while (waitpid (child, &wait_status, 0) == -1)
1571 waitpid_error (shell);
1576 #endif /* not MSDOS */
1583 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1584 rsh_command_option);
1589 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1590 rsh_command_option);
1595 maybe_backup_file (*archive_name_cursor, 1);
1596 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1597 rsh_command_option);
1601 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1602 rsh_command_option);
1608 open_warn (*archive_name_cursor);
1609 if (!verify_option && access == ACCESS_WRITE && backup_option)
1610 undo_last_backup ();
1615 setmode (archive, O_BINARY);