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 # include <sys/inode.h>
41 #define PREAD 0 /* read file descriptor from pipe() */
42 #define PWRITE 1 /* write file descriptor from pipe() */
44 /* Number of retries before giving up on read. */
45 #define READ_ERROR_MAX 10
47 /* Globbing pattern to append to volume label if initial match failed. */
48 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
52 static tarlong prev_written
; /* bytes written on previous volumes */
53 static tarlong bytes_written
; /* bytes written on this volume */
55 /* FIXME: The following variables should ideally be static to this
56 module. However, this cannot be done yet. The cleanup continues! */
58 union block
*record_start
; /* start of record of archive */
59 union block
*record_end
; /* last+1 block of archive record */
60 union block
*current_block
; /* current block of archive */
61 enum access_mode access_mode
; /* how do we handle the archive */
62 off_t records_read
; /* number of records read from this archive */
63 off_t records_written
; /* likewise, for records written */
65 static struct stat archive_stat
; /* stat block for archive file */
67 static off_t record_start_block
; /* block ordinal at record_start */
69 /* Where we write list messages (not errors, not interactions) to. Stdout
70 unless we're writing a pipe, in which case stderr. */
73 static void backspace_output
PARAMS ((void));
74 static int new_volume
PARAMS ((enum access_mode
));
75 static void archive_write_error
PARAMS ((ssize_t
)) __attribute__ ((noreturn
));
76 static void archive_read_error
PARAMS ((void));
79 /* Obnoxious test to see if dimwit is trying to dump the archive. */
84 /* PID of child program, if compress_option or remote archive access. */
85 static pid_t child_pid
;
87 /* Error recovery stuff */
88 static int read_error_count
;
90 /* Have we hit EOF yet? */
93 /* Checkpointing counter */
94 static int checkpoint
;
96 /* We're reading, but we just read the last block and its time to update. */
97 /* As least EXTERN like this one as possible. FIXME! */
98 extern int time_to_start_writing
;
100 int file_to_switch_to
= -1; /* if remote update, close archive, and use
101 this descriptor to write to */
103 static int volno
= 1; /* which volume of a multi-volume tape we're
105 static int global_volno
= 1; /* volume number to print in external
108 /* The pointer save_name, which is set in function dump_file() of module
109 create.c, points to the original long filename instead of the new,
110 shorter mangled name that is set in start_header() of module create.c.
111 The pointer save_name is only used in multi-volume mode when the file
112 being processed is non-sparse; if a file is split between volumes, the
113 save_name is used in generating the LF_MULTIVOL record on the second
114 volume. (From Pierce Cantrell, 1991-08-13.) */
116 char *save_name
; /* name of the file we are currently writing */
117 off_t save_totsize
; /* total size of file we are writing, only
118 valid if save_name is nonzero */
119 off_t save_sizeleft
; /* where we are in the file we are writing,
120 only valid if save_name is nonzero */
122 bool write_archive_to_stdout
;
124 /* Used by flush_read and flush_write to store the real info about saved
126 static char *real_s_name
;
127 static off_t real_s_totsize
;
128 static off_t real_s_sizeleft
;
133 print_total_written (void)
135 tarlong written
= prev_written
+ bytes_written
;
136 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
137 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
138 char rate
[LONGEST_HUMAN_READABLE
+ 1];
141 #if HAVE_CLOCK_GETTIME
143 if (clock_gettime (CLOCK_REALTIME
, &now
) == 0)
144 seconds
= ((now
.tv_sec
- start_timespec
.tv_sec
)
145 + (now
.tv_nsec
- start_timespec
.tv_nsec
) / 1e9
);
148 seconds
= time (0) - start_time
;
150 sprintf (bytes
, TARLONG_FORMAT
, written
);
152 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
153 fprintf (stderr
, _("Total bytes written: %s (%sB, %sB/s)\n"), bytes
,
154 human_readable ((uintmax_t) written
, abbr
, 1, -1024),
155 (0 < seconds
&& written
/ seconds
< (uintmax_t) -1
156 ? human_readable ((uintmax_t) (written
/ seconds
), rate
, 1, -1024)
160 /* Compute and return the block ordinal at current_block. */
162 current_block_ordinal (void)
164 return record_start_block
+ (current_block
- record_start
);
167 /* If the EOF flag is set, reset it, as well as current_block, etc. */
174 current_block
= record_start
;
175 record_end
= record_start
+ blocking_factor
;
176 access_mode
= ACCESS_WRITE
;
180 /* Return the location of the next available input or output block.
181 Return zero for EOF. Once we have returned zero, we just keep returning
182 it, to avoid accidentally going on to the next file on the tape. */
184 find_next_block (void)
186 if (current_block
== record_end
)
191 if (current_block
== record_end
)
197 return current_block
;
200 /* Indicate that we have used all blocks up thru BLOCK.
201 FIXME: should the arg have an off-by-1? */
203 set_next_block_after (union block
*block
)
205 while (block
>= current_block
)
208 /* Do *not* flush the archive here. If we do, the same argument to
209 set_next_block_after could mean the next block (if the input record
210 is exactly one block long), which is not what is intended. */
212 if (current_block
> record_end
)
216 /* Return the number of bytes comprising the space between POINTER
217 through the end of the current buffer of blocks. This space is
218 available for filling with data, or taking data from. POINTER is
219 usually (but not always) the result previous find_next_block call. */
221 available_space_after (union block
*pointer
)
223 return record_end
->buffer
- pointer
->buffer
;
226 /* Close file having descriptor FD, and abort if close unsuccessful. */
231 close_error (_("(pipe)"));
234 /* Duplicate file descriptor FROM into becoming INTO.
235 INTO is closed first and has to be the next available slot. */
237 xdup2 (int from
, int into
)
241 int status
= close (into
);
243 if (status
!= 0 && errno
!= EBADF
)
246 FATAL_ERROR ((0, e
, _("Cannot close")));
254 FATAL_ERROR ((0, e
, _("Cannot dup")));
264 /* Set ARCHIVE for writing, then compressing an archive. */
266 child_open_for_compress (void)
268 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
271 /* Set ARCHIVE for uncompressing, then reading an archive. */
273 child_open_for_uncompress (void)
275 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
278 #else /* not MSDOS */
280 /* Return nonzero if NAME is the name of a regular file, or if the file
281 does not exist (so it would be created as a regular file). */
283 is_regular_file (const char *name
)
287 if (stat (name
, &stbuf
) == 0)
288 return S_ISREG (stbuf
.st_mode
);
290 return errno
== ENOENT
;
294 write_archive_buffer (void)
299 while (0 <= (status
= rmtwrite (archive
, record_start
->buffer
+ written
,
300 record_size
- written
)))
303 if (written
== record_size
304 || _isrmt (archive
) || ! S_ISFIFO (archive_stat
.st_mode
))
308 return written
? written
: status
;
311 /* Set ARCHIVE for writing, then compressing an archive. */
313 child_open_for_compress (void)
317 pid_t grandchild_pid
;
321 child_pid
= xfork ();
325 /* The parent tar is still here! Just clean up. */
327 archive
= parent_pipe
[PWRITE
];
328 xclose (parent_pipe
[PREAD
]);
332 /* The new born child tar is here! */
334 program_name
= _("tar (child)");
336 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
);
337 xclose (parent_pipe
[PWRITE
]);
339 /* Check if we need a grandchild tar. This happens only if either:
340 a) we are writing stdout: to force reblocking;
341 b) the file is to be accessed by rmt: compressor doesn't know how;
342 c) the file is not a plain file. */
344 if (strcmp (archive_name_array
[0], "-") != 0
345 && !_remdev (archive_name_array
[0])
346 && is_regular_file (archive_name_array
[0]))
349 maybe_backup_file (archive_name_array
[0], 1);
351 /* We don't need a grandchild tar. Open the archive and launch the
354 archive
= creat (archive_name_array
[0], MODE_RW
);
357 int saved_errno
= errno
;
362 open_fatal (archive_name_array
[0]);
364 xdup2 (archive
, STDOUT_FILENO
);
365 execlp (use_compress_program_option
, use_compress_program_option
,
367 exec_fatal (use_compress_program_option
);
370 /* We do need a grandchild tar. */
373 grandchild_pid
= xfork ();
375 if (grandchild_pid
== 0)
377 /* The newborn grandchild tar is here! Launch the compressor. */
379 program_name
= _("tar (grandchild)");
381 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
382 xclose (child_pipe
[PREAD
]);
383 execlp (use_compress_program_option
, use_compress_program_option
,
385 exec_fatal (use_compress_program_option
);
388 /* The child tar is still here! */
390 /* Prepare for reblocking the data from the compressor into the archive. */
392 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
393 xclose (child_pipe
[PWRITE
]);
395 if (strcmp (archive_name_array
[0], "-") == 0)
396 archive
= STDOUT_FILENO
;
399 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
401 open_fatal (archive_name_array
[0]);
404 /* Let's read out of the stdin pipe and write an archive. */
412 /* Assemble a record. */
414 for (length
= 0, cursor
= record_start
->buffer
;
415 length
< record_size
;
416 length
+= status
, cursor
+= status
)
418 size_t size
= record_size
- length
;
420 if (size
< BLOCKSIZE
)
422 status
= safe_read (STDIN_FILENO
, cursor
, size
);
428 read_fatal (use_compress_program_option
);
430 /* Copy the record. */
434 /* We hit the end of the file. Write last record at
435 full length, as the only role of the grandchild is
436 doing proper reblocking. */
440 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
441 status
= write_archive_buffer ();
442 if (status
!= record_size
)
443 archive_write_error (status
);
446 /* There is nothing else to read, break out. */
450 status
= write_archive_buffer ();
451 if (status
!= record_size
)
452 archive_write_error (status
);
459 /* Propagate any failure of the grandchild back to the parent. */
461 while (waitpid (grandchild_pid
, &wait_status
, 0) == -1)
464 waitpid_error (use_compress_program_option
);
468 if (WIFSIGNALED (wait_status
))
470 kill (child_pid
, WTERMSIG (wait_status
));
471 exit_status
= TAREXIT_FAILURE
;
473 else if (WEXITSTATUS (wait_status
) != 0)
474 exit_status
= WEXITSTATUS (wait_status
);
479 /* Set ARCHIVE for uncompressing, then reading an archive. */
481 child_open_for_uncompress (void)
485 pid_t grandchild_pid
;
489 child_pid
= xfork ();
493 /* The parent tar is still here! Just clean up. */
495 read_full_records_option
= 1;
496 archive
= parent_pipe
[PREAD
];
497 xclose (parent_pipe
[PWRITE
]);
501 /* The newborn child tar is here! */
503 program_name
= _("tar (child)");
505 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
);
506 xclose (parent_pipe
[PREAD
]);
508 /* Check if we need a grandchild tar. This happens only if either:
509 a) we're reading stdin: to force unblocking;
510 b) the file is to be accessed by rmt: compressor doesn't know how;
511 c) the file is not a plain file. */
513 if (strcmp (archive_name_array
[0], "-") != 0
514 && !_remdev (archive_name_array
[0])
515 && is_regular_file (archive_name_array
[0]))
517 /* We don't need a grandchild tar. Open the archive and lauch the
520 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
522 open_fatal (archive_name_array
[0]);
523 xdup2 (archive
, STDIN_FILENO
);
524 execlp (use_compress_program_option
, use_compress_program_option
,
526 exec_fatal (use_compress_program_option
);
529 /* We do need a grandchild tar. */
532 grandchild_pid
= xfork ();
534 if (grandchild_pid
== 0)
536 /* The newborn grandchild tar is here! Launch the uncompressor. */
538 program_name
= _("tar (grandchild)");
540 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
541 xclose (child_pipe
[PWRITE
]);
542 execlp (use_compress_program_option
, use_compress_program_option
,
544 exec_fatal (use_compress_program_option
);
547 /* The child tar is still here! */
549 /* Prepare for unblocking the data from the archive into the
552 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
553 xclose (child_pipe
[PREAD
]);
555 if (strcmp (archive_name_array
[0], "-") == 0)
556 archive
= STDIN_FILENO
;
558 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
559 MODE_RW
, rsh_command_option
);
561 open_fatal (archive_name_array
[0]);
563 /* Let's read the archive and pipe it into stdout. */
572 read_error_count
= 0;
575 status
= rmtread (archive
, record_start
->buffer
, record_size
);
578 archive_read_error ();
583 cursor
= record_start
->buffer
;
587 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
588 if (full_write (STDOUT_FILENO
, cursor
, count
) != count
)
589 write_error (use_compress_program_option
);
595 xclose (STDOUT_FILENO
);
600 /* Propagate any failure of the grandchild back to the parent. */
602 while (waitpid (grandchild_pid
, &wait_status
, 0) == -1)
605 waitpid_error (use_compress_program_option
);
609 if (WIFSIGNALED (wait_status
))
611 kill (child_pid
, WTERMSIG (wait_status
));
612 exit_status
= TAREXIT_FAILURE
;
614 else if (WEXITSTATUS (wait_status
) != 0)
615 exit_status
= WEXITSTATUS (wait_status
);
620 #endif /* not MSDOS */
622 /* Check the LABEL block against the volume label, seen as a globbing
623 pattern. Return true if the pattern matches. In case of failure,
624 retry matching a volume sequence number before giving up in
625 multi-volume mode. */
627 check_label_pattern (union block
*label
)
632 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
635 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
638 if (!multi_volume_option
)
641 string
= xmalloc (strlen (volume_label_option
)
642 + sizeof VOLUME_LABEL_APPEND
+ 1);
643 strcpy (string
, volume_label_option
);
644 strcat (string
, VOLUME_LABEL_APPEND
);
645 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
650 /* Open an archive file. The argument specifies whether we are
651 reading or writing, or both. */
653 open_archive (enum access_mode wanted_access
)
655 int backed_up_flag
= 0;
657 stdlis
= to_stdout_option
? stderr
: stdout
;
659 if (record_size
== 0)
660 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
662 if (archive_names
== 0)
663 FATAL_ERROR ((0, 0, _("No archive name given")));
665 current_file_name
= 0;
666 current_link_name
= 0;
670 if (multi_volume_option
)
673 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
674 record_start
= valloc (record_size
+ (2 * BLOCKSIZE
));
679 record_start
= valloc (record_size
);
681 FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
684 current_block
= record_start
;
685 record_end
= record_start
+ blocking_factor
;
686 /* When updating the archive, we start with reading. */
687 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
689 if (use_compress_program_option
)
691 if (multi_volume_option
)
692 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
694 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
696 switch (wanted_access
)
699 child_open_for_uncompress ();
703 child_open_for_compress ();
707 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
711 if (wanted_access
== ACCESS_WRITE
712 && strcmp (archive_name_array
[0], "-") == 0)
715 else if (strcmp (archive_name_array
[0], "-") == 0)
717 read_full_records_option
= 1; /* could be a pipe, be safe */
719 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
721 switch (wanted_access
)
724 archive
= STDIN_FILENO
;
728 archive
= STDOUT_FILENO
;
733 archive
= STDIN_FILENO
;
735 write_archive_to_stdout
= 1;
739 else if (verify_option
)
740 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
741 MODE_RW
, rsh_command_option
);
743 switch (wanted_access
)
746 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
747 MODE_RW
, rsh_command_option
);
753 maybe_backup_file (archive_name_array
[0], 1);
756 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
761 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
762 MODE_RW
, rsh_command_option
);
767 || (! _isrmt (archive
) && fstat (archive
, &archive_stat
) < 0))
769 int saved_errno
= errno
;
774 open_fatal (archive_name_array
[0]);
779 /* Detect if outputting to "/dev/null". */
781 static char const dev_null
[] = "/dev/null";
782 struct stat dev_null_stat
;
785 (strcmp (archive_name_array
[0], dev_null
) == 0
786 || (! _isrmt (archive
)
787 && S_ISCHR (archive_stat
.st_mode
)
788 && stat (dev_null
, &dev_null_stat
) == 0
789 && archive_stat
.st_dev
== dev_null_stat
.st_dev
790 && archive_stat
.st_ino
== dev_null_stat
.st_ino
));
793 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
795 ar_dev
= archive_stat
.st_dev
;
796 ar_ino
= archive_stat
.st_ino
;
801 #endif /* not MSDOS */
804 setmode (archive
, O_BINARY
);
807 switch (wanted_access
)
813 record_end
= record_start
; /* set up for 1st record = # 0 */
814 find_next_block (); /* read it in, check for EOF */
816 if (volume_label_option
)
818 union block
*label
= find_next_block ();
821 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
822 quote (volume_label_option
)));
823 if (!check_label_pattern (label
))
824 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
825 quote_n (0, label
->header
.name
),
826 quote_n (1, volume_label_option
)));
832 if (volume_label_option
)
834 memset (record_start
, 0, BLOCKSIZE
);
835 if (multi_volume_option
)
836 sprintf (record_start
->header
.name
, "%s Volume 1",
837 volume_label_option
);
839 strcpy (record_start
->header
.name
, volume_label_option
);
841 assign_string (¤t_file_name
, record_start
->header
.name
);
843 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
844 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
845 finish_header (record_start
);
854 /* Perform a write to flush the buffer. */
861 if (checkpoint_option
&& !(++checkpoint
% 10))
862 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
864 if (tape_length_option
&& tape_length_option
<= bytes_written
)
869 else if (dev_null_output
)
870 status
= record_size
;
872 status
= write_archive_buffer ();
873 if (status
!= record_size
&& !multi_volume_option
)
874 archive_write_error (status
);
879 bytes_written
+= status
;
882 if (status
== record_size
)
884 if (multi_volume_option
)
890 assign_string (&real_s_name
, 0);
896 cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
897 while (ISSLASH (*cursor
))
900 assign_string (&real_s_name
, cursor
);
901 real_s_totsize
= save_totsize
;
902 real_s_sizeleft
= save_sizeleft
;
907 /* We're multivol. Panic if we didn't get the right kind of response. */
909 /* ENXIO is for the UNIX PC. */
910 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
911 archive_write_error (status
);
913 /* If error indicates a short write, we just move to the next tape. */
915 if (!new_volume (ACCESS_WRITE
))
919 prev_written
+= bytes_written
;
922 if (volume_label_option
&& real_s_name
)
927 else if (volume_label_option
|| real_s_name
)
935 if (volume_label_option
)
937 memset (record_start
, 0, BLOCKSIZE
);
938 sprintf (record_start
->header
.name
, "%s Volume %d",
939 volume_label_option
, volno
);
940 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
941 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
942 finish_header (record_start
);
949 if (volume_label_option
)
952 memset (record_start
, 0, BLOCKSIZE
);
954 /* FIXME: Michael P Urban writes: [a long name file] is being written
955 when a new volume rolls around [...] Looks like the wrong value is
956 being preserved in real_s_name, though. */
958 strcpy (record_start
->header
.name
, real_s_name
);
959 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
960 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
961 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
962 record_start
->oldgnu_header
.offset
);
963 tmp
= verbose_option
;
965 finish_header (record_start
);
966 verbose_option
= tmp
;
968 if (volume_label_option
)
972 status
= write_archive_buffer ();
973 if (status
!= record_size
)
974 archive_write_error (status
);
976 bytes_written
+= status
;
980 record_start
+= copy_back
;
981 memcpy (current_block
,
982 record_start
+ blocking_factor
- copy_back
,
983 copy_back
* BLOCKSIZE
);
984 current_block
+= copy_back
;
986 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
987 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
988 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
989 assign_string (&real_s_name
, 0);
992 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
994 while (ISSLASH (*cursor
))
997 assign_string (&real_s_name
, cursor
);
998 real_s_sizeleft
= save_sizeleft
;
999 real_s_totsize
= save_totsize
;
1005 /* Handle write errors on the archive. Write errors are always fatal.
1006 Hitting the end of a volume does not cause a write error unless the
1007 write was the first record of the volume. */
1009 archive_write_error (ssize_t status
)
1011 /* It might be useful to know how much was written before the error
1016 print_total_written ();
1020 write_fatal_details (*archive_name_cursor
, status
, record_size
);
1023 /* Handle read errors on the archive. If the read should be retried,
1024 return to the caller. */
1026 archive_read_error (void)
1028 read_error (*archive_name_cursor
);
1030 if (record_start_block
== 0)
1031 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1033 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1034 then give up on reading the archive. */
1036 if (read_error_count
++ > READ_ERROR_MAX
)
1037 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1041 /* Perform a read to flush the buffer. */
1045 ssize_t status
; /* result from system call */
1046 size_t left
; /* bytes left */
1047 char *more
; /* pointer to next byte to read */
1049 if (checkpoint_option
&& !(++checkpoint
% 10))
1050 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1052 /* Clear the count of errors. This only applies to a single call to
1055 read_error_count
= 0; /* clear error count */
1057 if (write_archive_to_stdout
&& record_start_block
!= 0)
1059 archive
= STDOUT_FILENO
;
1060 status
= write_archive_buffer ();
1061 archive
= STDIN_FILENO
;
1062 if (status
!= record_size
)
1063 archive_write_error (status
);
1065 if (multi_volume_option
)
1069 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
1071 while (ISSLASH (*cursor
))
1074 assign_string (&real_s_name
, cursor
);
1075 real_s_sizeleft
= save_sizeleft
;
1076 real_s_totsize
= save_totsize
;
1080 assign_string (&real_s_name
, 0);
1082 real_s_sizeleft
= 0;
1087 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1088 if (status
== record_size
)
1095 || (status
< 0 && errno
== ENOSPC
)
1096 || (status
> 0 && !read_full_records_option
))
1097 && multi_volume_option
)
1099 union block
*cursor
;
1102 switch (subcommand_option
)
1104 case APPEND_SUBCOMMAND
:
1105 case CAT_SUBCOMMAND
:
1106 case UPDATE_SUBCOMMAND
:
1107 if (!new_volume (ACCESS_UPDATE
))
1112 if (!new_volume (ACCESS_READ
))
1118 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1121 archive_read_error ();
1124 if (status
!= record_size
)
1127 cursor
= record_start
;
1129 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1131 if (volume_label_option
)
1133 if (!check_label_pattern (cursor
))
1135 WARN ((0, 0, _("Volume %s does not match %s"),
1136 quote_n (0, cursor
->header
.name
),
1137 quote_n (1, volume_label_option
)));
1144 fprintf (stdlis
, _("Reading %s\n"), quote (cursor
->header
.name
));
1147 else if (volume_label_option
)
1148 WARN ((0, 0, _("WARNING: No volume header")));
1153 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1154 || strcmp (cursor
->header
.name
, real_s_name
))
1156 WARN ((0, 0, _("%s is not continued on this volume"),
1157 quote (real_s_name
)));
1162 s1
= UINTMAX_FROM_HEADER (cursor
->header
.size
);
1163 s2
= UINTMAX_FROM_HEADER (cursor
->oldgnu_header
.offset
);
1164 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
1166 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1167 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1168 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1170 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1171 quote (cursor
->header
.name
),
1172 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1173 STRINGIFY_BIGINT (s1
, s1buf
),
1174 STRINGIFY_BIGINT (s2
, s2buf
)));
1179 if (real_s_totsize
- real_s_sizeleft
1180 != OFF_FROM_HEADER (cursor
->oldgnu_header
.offset
))
1182 WARN ((0, 0, _("This volume is out of sequence")));
1189 current_block
= cursor
;
1193 else if (status
< 0)
1195 archive_read_error ();
1196 goto error_loop
; /* try again */
1200 more
= record_start
->buffer
+ status
;
1201 left
= record_size
- status
;
1203 while (left
% BLOCKSIZE
!= 0
1204 || (left
&& status
&& read_full_records_option
))
1207 while ((status
= rmtread (archive
, more
, left
)) < 0)
1208 archive_read_error ();
1212 if (left
% BLOCKSIZE
!= 0)
1213 ERROR ((0, 0, _("%d garbage bytes ignored at end of archive"),
1214 (int) ((record_size
- left
) % BLOCKSIZE
)));
1218 if (! read_full_records_option
)
1219 FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
1220 (unsigned long) (record_size
- left
)));
1222 /* User warned us about this. Fix up. */
1228 /* FIXME: for size=0, multi-volume support. On the first record, warn
1229 about the problem. */
1231 if (!read_full_records_option
&& verbose_option
1232 && record_start_block
== 0 && status
> 0)
1233 WARN ((0, 0, _("Record size = %lu blocks"),
1234 (unsigned long) ((record_size
- left
) / BLOCKSIZE
)));
1236 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
1240 /* Flush the current buffer to/from the archive. */
1242 flush_archive (void)
1244 record_start_block
+= record_end
- record_start
;
1245 current_block
= record_start
;
1246 record_end
= record_start
+ blocking_factor
;
1248 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1250 access_mode
= ACCESS_WRITE
;
1251 time_to_start_writing
= 0;
1253 if (file_to_switch_to
>= 0)
1255 if (rmtclose (archive
) != 0)
1256 close_warn (*archive_name_cursor
);
1258 archive
= file_to_switch_to
;
1261 backspace_output ();
1264 switch (access_mode
)
1279 /* Backspace the archive descriptor by one record worth. If it's a
1280 tape, MTIOCTOP will work. If it's something else, try to seek on
1281 it. If we can't seek, we lose! */
1283 backspace_output (void)
1287 struct mtop operation
;
1289 operation
.mt_op
= MTBSR
;
1290 operation
.mt_count
= 1;
1291 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1293 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1299 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1301 /* Seek back to the beginning of this record and start writing there. */
1303 position
-= record_size
;
1306 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1308 /* Lseek failed. Try a different method. */
1311 _("Cannot backspace archive file; it may be unreadable without -i")));
1313 /* Replace the first part of the record with NULs. */
1315 if (record_start
->buffer
!= output_start
)
1316 memset (record_start
->buffer
, 0,
1317 output_start
- record_start
->buffer
);
1322 /* Close the archive file. */
1324 close_archive (void)
1326 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1331 /* Manage to fully drain a pipe we might be reading, so to not break it on
1332 the producer after the EOF block. FIXME: one of these days, GNU tar
1333 might become clever enough to just stop working, once there is no more
1334 work to do, we might have to revise this area in such time. */
1336 if (access_mode
== ACCESS_READ
1337 && ! _isrmt (archive
)
1338 && S_ISFIFO (archive_stat
.st_mode
))
1339 while (rmtread (archive
, record_start
->buffer
, record_size
) > 0)
1346 if (rmtclose (archive
) != 0)
1347 close_warn (*archive_name_cursor
);
1355 while (waitpid (child_pid
, &wait_status
, 0) == -1)
1358 waitpid_error (use_compress_program_option
);
1362 if (WIFSIGNALED (wait_status
))
1363 ERROR ((0, 0, _("Child died with signal %d"),
1364 WTERMSIG (wait_status
)));
1365 else if (WEXITSTATUS (wait_status
) != 0)
1366 ERROR ((0, 0, _("Child returned status %d"),
1367 WEXITSTATUS (wait_status
)));
1371 if (current_file_name
)
1372 free (current_file_name
);
1373 if (current_link_name
)
1374 free (current_link_name
);
1379 free (multi_volume_option
? record_start
- 2 : record_start
);
1382 /* Called to initialize the global volume number. */
1384 init_volume_number (void)
1386 FILE *file
= fopen (volno_file_option
, "r");
1390 if (fscanf (file
, "%d", &global_volno
) != 1
1391 || global_volno
< 0)
1392 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1393 quotearg_colon (volno_file_option
)));
1395 read_error (volno_file_option
);
1396 if (fclose (file
) != 0)
1397 close_error (volno_file_option
);
1399 else if (errno
!= ENOENT
)
1400 open_error (volno_file_option
);
1403 /* Called to write out the closing global volume number. */
1405 closeout_volume_number (void)
1407 FILE *file
= fopen (volno_file_option
, "w");
1411 fprintf (file
, "%d\n", global_volno
);
1413 write_error (volno_file_option
);
1414 if (fclose (file
) != 0)
1415 close_error (volno_file_option
);
1418 open_error (volno_file_option
);
1421 /* We've hit the end of the old volume. Close it and open the next one.
1422 Return nonzero on success. */
1424 new_volume (enum access_mode access
)
1426 static FILE *read_file
;
1429 if (!read_file
&& !info_script_option
)
1430 /* FIXME: if fopen is used, it will never be closed. */
1431 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1438 if (rmtclose (archive
) != 0)
1439 close_warn (*archive_name_cursor
);
1442 if (global_volno
< 0)
1443 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1445 archive_name_cursor
++;
1446 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1448 archive_name_cursor
= archive_name_array
;
1455 /* We have to prompt from now on. */
1457 if (info_script_option
)
1459 if (volno_file_option
)
1460 closeout_volume_number ();
1461 if (system (info_script_option
) != 0)
1462 FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option
));
1467 char input_buffer
[80];
1469 fputc ('\007', stderr
);
1471 _("Prepare volume #%d for %s and hit return: "),
1472 global_volno
, quote (*archive_name_cursor
));
1475 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
1477 WARN ((0, 0, _("EOF where user reply was expected")));
1479 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1480 && subcommand_option
!= LIST_SUBCOMMAND
1481 && subcommand_option
!= DIFF_SUBCOMMAND
)
1482 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1486 if (input_buffer
[0] == '\n'
1487 || input_buffer
[0] == 'y'
1488 || input_buffer
[0] == 'Y')
1491 switch (input_buffer
[0])
1495 fprintf (stderr
, _("\
1496 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1498 ! Spawn a subshell\n\
1499 ? Print this list\n"));
1506 WARN ((0, 0, _("No new volume; exiting.\n")));
1508 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1509 && subcommand_option
!= LIST_SUBCOMMAND
1510 && subcommand_option
!= DIFF_SUBCOMMAND
)
1511 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1516 /* Get new file name. */
1519 char *name
= &input_buffer
[1];
1522 while (*name
== ' ' || *name
== '\t')
1525 while (*cursor
&& *cursor
!= '\n')
1529 /* FIXME: the following allocation is never reclaimed. */
1530 *archive_name_cursor
= xstrdup (name
);
1536 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1537 #else /* not MSDOS */
1540 const char *shell
= getenv ("SHELL");
1546 execlp (shell
, "-sh", "-i", 0);
1552 while (waitpid (child
, &wait_status
, 0) == -1)
1555 waitpid_error (shell
);
1560 #endif /* not MSDOS */
1567 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1568 rsh_command_option
);
1573 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1574 rsh_command_option
);
1579 maybe_backup_file (*archive_name_cursor
, 1);
1580 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1581 rsh_command_option
);
1585 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1586 rsh_command_option
);
1592 open_warn (*archive_name_cursor
);
1593 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1594 undo_last_backup ();
1599 setmode (archive
, O_BINARY
);