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. */
22 /* $FreeBSD: src/contrib/tar/src/buffer.c,v 1.2.2.2 2002/10/11 14:32:25 sobomax Exp $ */
33 # include <sys/inode.h>
43 #define PREAD 0 /* read file descriptor from pipe() */
44 #define PWRITE 1 /* write file descriptor from pipe() */
46 /* Number of retries before giving up on read. */
47 #define READ_ERROR_MAX 10
49 /* Globbing pattern to append to volume label if initial match failed. */
50 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
54 static tarlong prev_written; /* bytes written on previous volumes */
55 static tarlong bytes_written; /* bytes written on this volume */
57 /* FIXME: The following variables should ideally be static to this
58 module. However, this cannot be done yet. The cleanup continues! */
60 union block *record_start; /* start of record of archive */
61 union block *record_end; /* last+1 block of archive record */
62 union block *current_block; /* current block of archive */
63 enum access_mode access_mode; /* how do we handle the archive */
64 off_t records_read; /* number of records read from this archive */
65 off_t records_written; /* likewise, for records written */
67 static struct stat archive_stat; /* stat block for archive file */
69 static off_t record_start_block; /* block ordinal at record_start */
71 /* Where we write list messages (not errors, not interactions) to. Stdout
72 unless we're writing a pipe, in which case stderr. */
75 static void backspace_output PARAMS ((void));
76 static int new_volume PARAMS ((enum access_mode));
77 static void archive_write_error PARAMS ((ssize_t)) __attribute__ ((noreturn));
78 static void archive_read_error PARAMS ((void));
81 /* Obnoxious test to see if dimwit is trying to dump the archive. */
86 /* PID of child program, if compress_option or remote archive access. */
87 static pid_t child_pid;
89 /* Error recovery stuff */
90 static int read_error_count;
92 /* Have we hit EOF yet? */
95 /* Checkpointing counter */
96 static int checkpoint;
98 /* We're reading, but we just read the last block and its time to update. */
99 /* As least EXTERN like this one as possible. FIXME! */
100 extern int time_to_start_writing;
102 int file_to_switch_to = -1; /* if remote update, close archive, and use
103 this descriptor to write to */
105 static int volno = 1; /* which volume of a multi-volume tape we're
107 static int global_volno = 1; /* volume number to print in external
109 static pid_t grandchild_pid;
111 /* The pointer save_name, which is set in function dump_file() of module
112 create.c, points to the original long filename instead of the new,
113 shorter mangled name that is set in start_header() of module create.c.
114 The pointer save_name is only used in multi-volume mode when the file
115 being processed is non-sparse; if a file is split between volumes, the
116 save_name is used in generating the LF_MULTIVOL record on the second
117 volume. (From Pierce Cantrell, 1991-08-13.) */
119 char *save_name; /* name of the file we are currently writing */
120 off_t save_totsize; /* total size of file we are writing, only
121 valid if save_name is nonzero */
122 off_t save_sizeleft; /* where we are in the file we are writing,
123 only valid if save_name is nonzero */
125 bool write_archive_to_stdout;
127 /* Used by flush_read and flush_write to store the real info about saved
129 static char *real_s_name;
130 static off_t real_s_totsize;
131 static off_t real_s_sizeleft;
136 print_total_written (void)
138 tarlong written = prev_written + bytes_written;
139 char bytes[sizeof (tarlong) * CHAR_BIT];
140 char abbr[LONGEST_HUMAN_READABLE + 1];
141 char rate[LONGEST_HUMAN_READABLE + 1];
144 #if HAVE_CLOCK_GETTIME
146 if (clock_gettime (CLOCK_REALTIME, &now) == 0)
147 seconds = ((now.tv_sec - start_timespec.tv_sec)
148 + (now.tv_nsec - start_timespec.tv_nsec) / 1e9);
151 seconds = time (0) - start_time;
153 sprintf (bytes, TARLONG_FORMAT, written);
155 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
156 fprintf (stderr, _("Total bytes written: %s (%sB, %sB/s)\n"), bytes,
157 human_readable ((uintmax_t) written, abbr, 1, -1024),
158 (0 < seconds && written / seconds < (uintmax_t) -1
159 ? human_readable ((uintmax_t) (written / seconds), rate, 1, -1024)
163 /* Compute and return the block ordinal at current_block. */
165 current_block_ordinal (void)
167 return record_start_block + (current_block - record_start);
170 /* If the EOF flag is set, reset it, as well as current_block, etc. */
177 current_block = record_start;
178 record_end = record_start + blocking_factor;
179 access_mode = ACCESS_WRITE;
183 /* Return the location of the next available input or output block.
184 Return zero for EOF. Once we have returned zero, we just keep returning
185 it, to avoid accidentally going on to the next file on the tape. */
187 find_next_block (void)
189 if (current_block == record_end)
194 if (current_block == record_end)
200 return current_block;
203 /* Indicate that we have used all blocks up thru BLOCK.
204 FIXME: should the arg have an off-by-1? */
206 set_next_block_after (union block *block)
208 while (block >= current_block)
211 /* Do *not* flush the archive here. If we do, the same argument to
212 set_next_block_after could mean the next block (if the input record
213 is exactly one block long), which is not what is intended. */
215 if (current_block > record_end)
219 /* Return the number of bytes comprising the space between POINTER
220 through the end of the current buffer of blocks. This space is
221 available for filling with data, or taking data from. POINTER is
222 usually (but not always) the result previous find_next_block call. */
224 available_space_after (union block *pointer)
226 return record_end->buffer - pointer->buffer;
229 /* Close file having descriptor FD, and abort if close unsuccessful. */
234 close_error (_("(pipe)"));
237 /* Duplicate file descriptor FROM into becoming INTO.
238 INTO is closed first and has to be the next available slot. */
240 xdup2 (int from, int into)
244 int status = close (into);
246 if (status != 0 && errno != EBADF)
249 FATAL_ERROR ((0, e, _("Cannot close")));
257 FATAL_ERROR ((0, e, _("Cannot dup")));
267 /* Set ARCHIVE for writing, then compressing an archive. */
269 child_open_for_compress (void)
271 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
274 /* Set ARCHIVE for uncompressing, then reading an archive. */
276 child_open_for_uncompress (void)
278 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
281 #else /* not MSDOS */
283 /* Return nonzero if NAME is the name of a regular file, or if the file
284 does not exist (so it would be created as a regular file). */
286 is_regular_file (const char *name)
290 if (stat (name, &stbuf) == 0)
291 return S_ISREG (stbuf.st_mode);
293 return errno == ENOENT;
297 write_archive_buffer (void)
302 while (0 <= (status = rmtwrite (archive, record_start->buffer + written,
303 record_size - written)))
306 if (written == record_size
308 || ! (S_ISFIFO (archive_stat.st_mode)
309 || S_ISSOCK (archive_stat.st_mode)))
313 return written ? written : status;
316 /* Set ARCHIVE for writing, then compressing an archive. */
318 child_open_for_compress (void)
325 child_pid = xfork ();
329 /* The parent tar is still here! Just clean up. */
331 archive = parent_pipe[PWRITE];
332 xclose (parent_pipe[PREAD]);
336 /* The new born child tar is here! */
338 program_name = _("tar (child)");
340 xdup2 (parent_pipe[PREAD], STDIN_FILENO);
341 xclose (parent_pipe[PWRITE]);
343 /* Check if we need a grandchild tar. This happens only if either:
344 a) we are writing stdout: to force reblocking;
345 b) the file is to be accessed by rmt: compressor doesn't know how;
346 c) the file is not a plain file. */
348 if (strcmp (archive_name_array[0], "-") != 0
349 && !_remdev (archive_name_array[0])
350 && is_regular_file (archive_name_array[0]))
353 maybe_backup_file (archive_name_array[0], 1);
355 /* We don't need a grandchild tar. Open the archive and launch the
358 archive = creat (archive_name_array[0], MODE_RW);
361 int saved_errno = errno;
366 open_fatal (archive_name_array[0]);
368 xdup2 (archive, STDOUT_FILENO);
369 execlp (use_compress_program_option, use_compress_program_option,
371 exec_fatal (use_compress_program_option);
374 /* We do need a grandchild tar. */
377 grandchild_pid = xfork ();
379 if (grandchild_pid == 0)
381 /* The newborn grandchild tar is here! Launch the compressor. */
383 program_name = _("tar (grandchild)");
385 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
386 xclose (child_pipe[PREAD]);
387 execlp (use_compress_program_option, use_compress_program_option,
389 exec_fatal (use_compress_program_option);
392 /* The child tar is still here! */
394 /* Prepare for reblocking the data from the compressor into the archive. */
396 xdup2 (child_pipe[PREAD], STDIN_FILENO);
397 xclose (child_pipe[PWRITE]);
399 if (strcmp (archive_name_array[0], "-") == 0)
400 archive = STDOUT_FILENO;
403 archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
405 open_fatal (archive_name_array[0]);
408 /* Let's read out of the stdin pipe and write an archive. */
416 /* Assemble a record. */
418 for (length = 0, cursor = record_start->buffer;
419 length < record_size;
420 length += status, cursor += status)
422 size_t size = record_size - length;
424 if (size < BLOCKSIZE)
426 status = safe_read (STDIN_FILENO, cursor, size);
432 read_fatal (use_compress_program_option);
434 /* Copy the record. */
438 /* We hit the end of the file. Write last record at
439 full length, as the only role of the grandchild is
440 doing proper reblocking. */
444 memset (record_start->buffer + length, 0, record_size - length);
445 status = write_archive_buffer ();
446 if (status != record_size)
447 archive_write_error (status);
450 /* There is nothing else to read, break out. */
454 status = write_archive_buffer ();
455 if (status != record_size)
456 archive_write_error (status);
463 /* Propagate any failure of the grandchild back to the parent. */
465 while (waitpid (grandchild_pid, &wait_status, 0) == -1)
468 waitpid_error (use_compress_program_option);
472 if (WIFSIGNALED (wait_status))
474 kill (child_pid, WTERMSIG (wait_status));
475 exit_status = TAREXIT_FAILURE;
477 else if (WEXITSTATUS (wait_status) != 0)
478 exit_status = WEXITSTATUS (wait_status);
484 sig_propagate(int sig)
486 kill (grandchild_pid, sig);
487 exit (TAREXIT_FAILURE);
490 /* Set ARCHIVE for uncompressing, then reading an archive. */
492 child_open_for_uncompress (void)
499 child_pid = xfork ();
503 /* The parent tar is still here! Just clean up. */
505 read_full_records_option = 1;
506 archive = parent_pipe[PREAD];
507 xclose (parent_pipe[PWRITE]);
511 /* The newborn child tar is here! */
513 program_name = _("tar (child)");
515 xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
516 xclose (parent_pipe[PREAD]);
518 /* Check if we need a grandchild tar. This happens only if either:
519 a) we're reading stdin: to force unblocking;
520 b) the file is to be accessed by rmt: compressor doesn't know how;
521 c) the file is not a plain file. */
523 if (strcmp (archive_name_array[0], "-") != 0
524 && !_remdev (archive_name_array[0])
525 && is_regular_file (archive_name_array[0]))
527 /* We don't need a grandchild tar. Open the archive and lauch the
530 archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
532 open_fatal (archive_name_array[0]);
533 xdup2 (archive, STDIN_FILENO);
534 execlp (use_compress_program_option, use_compress_program_option,
536 exec_fatal (use_compress_program_option);
539 /* We do need a grandchild tar. */
542 grandchild_pid = xfork ();
544 if (grandchild_pid == 0)
546 /* The newborn grandchild tar is here! Launch the uncompressor. */
548 program_name = _("tar (grandchild)");
550 xdup2 (child_pipe[PREAD], STDIN_FILENO);
551 xclose (child_pipe[PWRITE]);
552 execlp (use_compress_program_option, use_compress_program_option,
554 exec_fatal (use_compress_program_option);
557 /* The child tar is still here! */
558 signal (SIGTERM, sig_propagate);
560 /* Prepare for unblocking the data from the archive into the
563 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
564 xclose (child_pipe[PREAD]);
566 if (strcmp (archive_name_array[0], "-") == 0)
567 archive = STDIN_FILENO;
569 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
570 MODE_RW, rsh_command_option);
572 open_fatal (archive_name_array[0]);
574 /* Let's read the archive and pipe it into stdout. */
583 read_error_count = 0;
586 status = rmtread (archive, record_start->buffer, record_size);
589 archive_read_error ();
594 cursor = record_start->buffer;
598 count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
599 if (full_write (STDOUT_FILENO, cursor, count) != count)
600 write_error (use_compress_program_option);
606 xclose (STDOUT_FILENO);
611 /* Propagate any failure of the grandchild back to the parent. */
613 while (waitpid (grandchild_pid, &wait_status, 0) == -1)
616 waitpid_error (use_compress_program_option);
620 if (WIFSIGNALED (wait_status))
622 kill (child_pid, WTERMSIG (wait_status));
623 exit_status = TAREXIT_FAILURE;
625 else if (WEXITSTATUS (wait_status) != 0)
626 exit_status = WEXITSTATUS (wait_status);
631 #endif /* not MSDOS */
633 /* Check the LABEL block against the volume label, seen as a globbing
634 pattern. Return true if the pattern matches. In case of failure,
635 retry matching a volume sequence number before giving up in
636 multi-volume mode. */
638 check_label_pattern (union block *label)
643 if (! memchr (label->header.name, '\0', sizeof label->header.name))
646 if (fnmatch (volume_label_option, label->header.name, 0) == 0)
649 if (!multi_volume_option)
652 string = xmalloc (strlen (volume_label_option)
653 + sizeof VOLUME_LABEL_APPEND + 1);
654 strcpy (string, volume_label_option);
655 strcat (string, VOLUME_LABEL_APPEND);
656 result = fnmatch (string, label->header.name, 0) == 0;
661 /* Open an archive file. The argument specifies whether we are
662 reading or writing, or both. */
664 open_archive (enum access_mode wanted_access)
666 int backed_up_flag = 0;
668 stdlis = to_stdout_option ? stderr : stdout;
670 if (record_size == 0)
671 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
673 if (archive_names == 0)
674 FATAL_ERROR ((0, 0, _("No archive name given")));
676 current_file_name = 0;
677 current_link_name = 0;
681 if (multi_volume_option)
684 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
685 record_start = valloc (record_size + (2 * BLOCKSIZE));
690 record_start = valloc (record_size);
692 FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
695 current_block = record_start;
696 record_end = record_start + blocking_factor;
697 /* When updating the archive, we start with reading. */
698 access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
700 if (use_compress_program_option)
702 if (multi_volume_option)
703 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
705 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
707 switch (wanted_access)
710 child_open_for_uncompress ();
714 child_open_for_compress ();
718 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
722 if (wanted_access == ACCESS_WRITE
723 && strcmp (archive_name_array[0], "-") == 0)
726 else if (strcmp (archive_name_array[0], "-") == 0)
728 read_full_records_option = 1; /* could be a pipe, be safe */
730 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
732 switch (wanted_access)
735 archive = STDIN_FILENO;
739 archive = STDOUT_FILENO;
744 archive = STDIN_FILENO;
746 write_archive_to_stdout = 1;
750 else if (verify_option)
751 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
752 MODE_RW, rsh_command_option);
754 switch (wanted_access)
757 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
758 MODE_RW, rsh_command_option);
764 maybe_backup_file (archive_name_array[0], 1);
767 archive = rmtcreat (archive_name_array[0], MODE_RW,
772 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
773 MODE_RW, rsh_command_option);
778 || (! _isrmt (archive) && fstat (archive, &archive_stat) < 0))
780 int saved_errno = errno;
785 open_fatal (archive_name_array[0]);
790 /* Detect if outputting to "/dev/null". */
792 static char const dev_null[] = "/dev/null";
793 struct stat dev_null_stat;
796 (strcmp (archive_name_array[0], dev_null) == 0
797 || (! _isrmt (archive)
798 && S_ISCHR (archive_stat.st_mode)
799 && stat (dev_null, &dev_null_stat) == 0
800 && archive_stat.st_dev == dev_null_stat.st_dev
801 && archive_stat.st_ino == dev_null_stat.st_ino));
804 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
806 ar_dev = archive_stat.st_dev;
807 ar_ino = archive_stat.st_ino;
812 #endif /* not MSDOS */
815 setmode (archive, O_BINARY);
818 switch (wanted_access)
824 record_end = record_start; /* set up for 1st record = # 0 */
825 find_next_block (); /* read it in, check for EOF */
827 if (volume_label_option)
829 union block *label = find_next_block ();
832 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
833 quote (volume_label_option)));
834 if (!check_label_pattern (label))
835 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
836 quote_n (0, label->header.name),
837 quote_n (1, volume_label_option)));
843 if (volume_label_option)
845 memset (record_start, 0, BLOCKSIZE);
846 if (multi_volume_option)
847 sprintf (record_start->header.name, "%s Volume 1",
848 volume_label_option);
850 strcpy (record_start->header.name, volume_label_option);
852 assign_string (¤t_file_name, record_start->header.name);
854 record_start->header.typeflag = GNUTYPE_VOLHDR;
855 TIME_TO_CHARS (start_time, record_start->header.mtime);
856 finish_header (record_start);
865 /* Perform a write to flush the buffer. */
872 if (checkpoint_option && !(++checkpoint % 10))
873 WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
875 if (tape_length_option && tape_length_option <= bytes_written)
880 else if (dev_null_output)
881 status = record_size;
883 status = write_archive_buffer ();
884 if (status != record_size && !multi_volume_option)
885 archive_write_error (status);
890 bytes_written += status;
893 if (status == record_size)
895 if (multi_volume_option)
901 assign_string (&real_s_name, 0);
907 cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
908 while (ISSLASH (*cursor))
911 assign_string (&real_s_name, cursor);
912 real_s_totsize = save_totsize;
913 real_s_sizeleft = save_sizeleft;
918 /* We're multivol. Panic if we didn't get the right kind of response. */
920 /* ENXIO is for the UNIX PC. */
921 if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
922 archive_write_error (status);
924 /* If error indicates a short write, we just move to the next tape. */
926 if (!new_volume (ACCESS_WRITE))
930 prev_written += bytes_written;
933 if (volume_label_option && real_s_name)
938 else if (volume_label_option || real_s_name)
946 if (volume_label_option)
948 memset (record_start, 0, BLOCKSIZE);
949 sprintf (record_start->header.name, "%s Volume %d",
950 volume_label_option, volno);
951 TIME_TO_CHARS (start_time, record_start->header.mtime);
952 record_start->header.typeflag = GNUTYPE_VOLHDR;
953 finish_header (record_start);
960 if (volume_label_option)
963 memset (record_start, 0, BLOCKSIZE);
965 /* FIXME: Michael P Urban writes: [a long name file] is being written
966 when a new volume rolls around [...] Looks like the wrong value is
967 being preserved in real_s_name, though. */
969 strcpy (record_start->header.name, real_s_name);
970 record_start->header.typeflag = GNUTYPE_MULTIVOL;
971 OFF_TO_CHARS (real_s_sizeleft, record_start->header.size);
972 OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
973 record_start->oldgnu_header.offset);
974 tmp = verbose_option;
976 finish_header (record_start);
977 verbose_option = tmp;
979 if (volume_label_option)
983 status = write_archive_buffer ();
984 if (status != record_size)
985 archive_write_error (status);
987 bytes_written += status;
991 record_start += copy_back;
992 memcpy (current_block,
993 record_start + blocking_factor - copy_back,
994 copy_back * BLOCKSIZE);
995 current_block += copy_back;
997 if (real_s_sizeleft >= copy_back * BLOCKSIZE)
998 real_s_sizeleft -= copy_back * BLOCKSIZE;
999 else if ((real_s_sizeleft + BLOCKSIZE - 1) / BLOCKSIZE <= copy_back)
1000 assign_string (&real_s_name, 0);
1003 char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
1005 while (ISSLASH (*cursor))
1008 assign_string (&real_s_name, cursor);
1009 real_s_sizeleft = save_sizeleft;
1010 real_s_totsize = save_totsize;
1016 /* Handle write errors on the archive. Write errors are always fatal.
1017 Hitting the end of a volume does not cause a write error unless the
1018 write was the first record of the volume. */
1020 archive_write_error (ssize_t status)
1022 /* It might be useful to know how much was written before the error
1027 print_total_written ();
1031 write_fatal_details (*archive_name_cursor, status, record_size);
1034 /* Handle read errors on the archive. If the read should be retried,
1035 return to the caller. */
1037 archive_read_error (void)
1039 read_error (*archive_name_cursor);
1041 if (record_start_block == 0)
1042 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1044 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1045 then give up on reading the archive. */
1047 if (read_error_count++ > READ_ERROR_MAX)
1048 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1052 /* Perform a read to flush the buffer. */
1056 ssize_t status; /* result from system call */
1057 size_t left; /* bytes left */
1058 char *more; /* pointer to next byte to read */
1060 if (checkpoint_option && !(++checkpoint % 10))
1061 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
1063 /* Clear the count of errors. This only applies to a single call to
1066 read_error_count = 0; /* clear error count */
1068 if (write_archive_to_stdout && record_start_block != 0)
1070 archive = STDOUT_FILENO;
1071 status = write_archive_buffer ();
1072 archive = STDIN_FILENO;
1073 if (status != record_size)
1074 archive_write_error (status);
1076 if (multi_volume_option)
1080 char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
1082 while (ISSLASH (*cursor))
1085 assign_string (&real_s_name, cursor);
1086 real_s_sizeleft = save_sizeleft;
1087 real_s_totsize = save_totsize;
1091 assign_string (&real_s_name, 0);
1093 real_s_sizeleft = 0;
1098 status = rmtread (archive, record_start->buffer, record_size);
1099 if (status == record_size)
1106 || (status < 0 && errno == ENOSPC)
1107 || (status > 0 && !read_full_records_option))
1108 && multi_volume_option)
1110 union block *cursor;
1113 switch (subcommand_option)
1115 case APPEND_SUBCOMMAND:
1116 case CAT_SUBCOMMAND:
1117 case UPDATE_SUBCOMMAND:
1118 if (!new_volume (ACCESS_UPDATE))
1123 if (!new_volume (ACCESS_READ))
1129 status = rmtread (archive, record_start->buffer, record_size);
1132 archive_read_error ();
1135 if (status != record_size)
1138 cursor = record_start;
1140 if (cursor->header.typeflag == GNUTYPE_VOLHDR)
1142 if (volume_label_option)
1144 if (!check_label_pattern (cursor))
1146 WARN ((0, 0, _("Volume %s does not match %s"),
1147 quote_n (0, cursor->header.name),
1148 quote_n (1, volume_label_option)));
1155 fprintf (stdlis, _("Reading %s\n"), quote (cursor->header.name));
1158 else if (volume_label_option)
1159 WARN ((0, 0, _("WARNING: No volume header")));
1164 if (cursor->header.typeflag != GNUTYPE_MULTIVOL
1165 || strcmp (cursor->header.name, real_s_name))
1167 WARN ((0, 0, _("%s is not continued on this volume"),
1168 quote (real_s_name)));
1173 s1 = UINTMAX_FROM_HEADER (cursor->header.size);
1174 s2 = UINTMAX_FROM_HEADER (cursor->oldgnu_header.offset);
1175 if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
1177 char totsizebuf[UINTMAX_STRSIZE_BOUND];
1178 char s1buf[UINTMAX_STRSIZE_BOUND];
1179 char s2buf[UINTMAX_STRSIZE_BOUND];
1181 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1182 quote (cursor->header.name),
1183 STRINGIFY_BIGINT (save_totsize, totsizebuf),
1184 STRINGIFY_BIGINT (s1, s1buf),
1185 STRINGIFY_BIGINT (s2, s2buf)));
1190 if (real_s_totsize - real_s_sizeleft
1191 != OFF_FROM_HEADER (cursor->oldgnu_header.offset))
1193 WARN ((0, 0, _("This volume is out of sequence")));
1200 current_block = cursor;
1204 else if (status < 0)
1206 archive_read_error ();
1207 goto error_loop; /* try again */
1211 more = record_start->buffer + status;
1212 left = record_size - status;
1214 while (left % BLOCKSIZE != 0
1215 || (left && status && read_full_records_option))
1218 while ((status = rmtread (archive, more, left)) < 0)
1219 archive_read_error ();
1224 if (! read_full_records_option)
1225 FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
1226 (unsigned long) (record_size - left)));
1228 /* User warned us about this. Fix up. */
1234 /* FIXME: for size=0, multi-volume support. On the first record, warn
1235 about the problem. */
1237 if (!read_full_records_option && verbose_option
1238 && record_start_block == 0 && status > 0)
1239 WARN ((0, 0, _("Record size = %lu blocks"),
1240 (unsigned long) ((record_size - left) / BLOCKSIZE)));
1242 record_end = record_start + (record_size - left) / BLOCKSIZE;
1246 /* Flush the current buffer to/from the archive. */
1248 flush_archive (void)
1250 record_start_block += record_end - record_start;
1251 current_block = record_start;
1252 record_end = record_start + blocking_factor;
1254 if (access_mode == ACCESS_READ && time_to_start_writing)
1256 access_mode = ACCESS_WRITE;
1257 time_to_start_writing = 0;
1259 if (file_to_switch_to >= 0)
1261 if (rmtclose (archive) != 0)
1262 close_warn (*archive_name_cursor);
1264 archive = file_to_switch_to;
1267 backspace_output ();
1270 switch (access_mode)
1285 /* Backspace the archive descriptor by one record worth. If it's a
1286 tape, MTIOCTOP will work. If it's something else, try to seek on
1287 it. If we can't seek, we lose! */
1289 backspace_output (void)
1293 struct mtop operation;
1295 operation.mt_op = MTBSR;
1296 operation.mt_count = 1;
1297 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1299 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1305 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
1307 /* Seek back to the beginning of this record and start writing there. */
1309 position -= record_size;
1312 if (rmtlseek (archive, position, SEEK_SET) != position)
1314 /* Lseek failed. Try a different method. */
1317 _("Cannot backspace archive file; it may be unreadable without -i")));
1319 /* Replace the first part of the record with NULs. */
1321 if (record_start->buffer != output_start)
1322 memset (record_start->buffer, 0,
1323 output_start - record_start->buffer);
1328 /* Close the archive file. */
1330 close_archive (void)
1332 if (time_to_start_writing || access_mode == ACCESS_WRITE)
1337 /* Manage to fully drain a pipe we might be reading, so to not break it on
1338 the producer after the EOF block. FIXME: one of these days, GNU tar
1339 might become clever enough to just stop working, once there is no more
1340 work to do, we might have to revise this area in such time. */
1342 if (fast_read_option && namelist_freed && child_pid > 0)
1343 kill(child_pid, SIGTERM);
1345 if (access_mode == ACCESS_READ
1346 && ! _isrmt (archive)
1347 && (S_ISFIFO (archive_stat.st_mode) || S_ISSOCK (archive_stat.st_mode)))
1348 while (rmtread (archive, record_start->buffer, record_size) > 0)
1355 if (rmtclose (archive) != 0)
1356 close_warn (*archive_name_cursor);
1364 while (waitpid (child_pid, &wait_status, 0) == -1)
1367 waitpid_error (use_compress_program_option);
1371 if (!fast_read_option || !namelist_freed)
1372 if (WIFSIGNALED (wait_status))
1373 ERROR ((0, 0, _("Child died with signal %d"),
1374 WTERMSIG (wait_status)));
1375 else if (WEXITSTATUS (wait_status) != 0)
1376 ERROR ((0, 0, _("Child returned status %d"),
1377 WEXITSTATUS (wait_status)));
1381 if (current_file_name)
1382 free (current_file_name);
1383 if (current_link_name)
1384 free (current_link_name);
1389 free (multi_volume_option ? record_start - 2 : record_start);
1392 /* Called to initialize the global volume number. */
1394 init_volume_number (void)
1396 FILE *file = fopen (volno_file_option, "r");
1400 if (fscanf (file, "%d", &global_volno) != 1
1401 || global_volno < 0)
1402 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1403 quotearg_colon (volno_file_option)));
1405 read_error (volno_file_option);
1406 if (fclose (file) != 0)
1407 close_error (volno_file_option);
1409 else if (errno != ENOENT)
1410 open_error (volno_file_option);
1413 /* Called to write out the closing global volume number. */
1415 closeout_volume_number (void)
1417 FILE *file = fopen (volno_file_option, "w");
1421 fprintf (file, "%d\n", global_volno);
1423 write_error (volno_file_option);
1424 if (fclose (file) != 0)
1425 close_error (volno_file_option);
1428 open_error (volno_file_option);
1431 /* We've hit the end of the old volume. Close it and open the next one.
1432 Return nonzero on success. */
1434 new_volume (enum access_mode access)
1436 static FILE *read_file;
1439 if (!read_file && !info_script_option)
1440 /* FIXME: if fopen is used, it will never be closed. */
1441 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
1448 if (rmtclose (archive) != 0)
1449 close_warn (*archive_name_cursor);
1452 if (global_volno < 0)
1453 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1455 archive_name_cursor++;
1456 if (archive_name_cursor == archive_name_array + archive_names)
1458 archive_name_cursor = archive_name_array;
1465 /* We have to prompt from now on. */
1467 if (info_script_option)
1469 if (volno_file_option)
1470 closeout_volume_number ();
1471 if (system (info_script_option) != 0)
1472 FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option));
1477 char input_buffer[80];
1479 fputc ('\007', stderr);
1481 _("Prepare volume #%d for %s and hit return: "),
1482 global_volno, quote (*archive_name_cursor));
1485 if (fgets (input_buffer, sizeof input_buffer, read_file) == 0)
1487 WARN ((0, 0, _("EOF where user reply was expected")));
1489 if (subcommand_option != EXTRACT_SUBCOMMAND
1490 && subcommand_option != LIST_SUBCOMMAND
1491 && subcommand_option != DIFF_SUBCOMMAND)
1492 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1496 if (input_buffer[0] == '\n'
1497 || input_buffer[0] == 'y'
1498 || input_buffer[0] == 'Y')
1501 switch (input_buffer[0])
1505 fprintf (stderr, _("\
1506 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1508 ! Spawn a subshell\n\
1509 ? Print this list\n"));
1516 WARN ((0, 0, _("No new volume; exiting.\n")));
1518 if (subcommand_option != EXTRACT_SUBCOMMAND
1519 && subcommand_option != LIST_SUBCOMMAND
1520 && subcommand_option != DIFF_SUBCOMMAND)
1521 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1526 /* Get new file name. */
1529 char *name = &input_buffer[1];
1532 while (*name == ' ' || *name == '\t')
1535 while (*cursor && *cursor != '\n')
1539 /* FIXME: the following allocation is never reclaimed. */
1540 *archive_name_cursor = xstrdup (name);
1546 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
1547 #else /* not MSDOS */
1550 const char *shell = getenv ("SHELL");
1556 execlp (shell, "-sh", "-i", (char *) 0);
1562 while (waitpid (child, &wait_status, 0) == -1)
1565 waitpid_error (shell);
1570 #endif /* not MSDOS */
1577 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1578 rsh_command_option);
1583 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1584 rsh_command_option);
1589 maybe_backup_file (*archive_name_cursor, 1);
1590 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1591 rsh_command_option);
1595 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1596 rsh_command_option);
1602 open_warn (*archive_name_cursor);
1603 if (!verify_option && access == ACCESS_WRITE && backup_option)
1604 undo_last_backup ();
1609 setmode (archive, O_BINARY);