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 four variables should ideally be static to this
56 module. However, this cannot be done yet, as update.c uses the first
57 three a lot, and compare.c uses the fourth. The cleanup continues! */
59 union block
*record_start
; /* start of record of archive */
60 union block
*record_end
; /* last+1 block of archive record */
61 union block
*current_block
; /* current block of archive */
62 enum access_mode access_mode
; /* how do we handle the archive */
63 static struct stat archive_stat
; /* stat block for archive file */
65 static off_t record_start_block
; /* block ordinal at record_start */
67 /* Where we write list messages (not errors, not interactions) to. Stdout
68 unless we're writing a pipe, in which case stderr. */
71 static void backspace_output
PARAMS ((void));
72 static int new_volume
PARAMS ((enum access_mode
));
73 static void archive_write_error
PARAMS ((ssize_t
)) __attribute__ ((noreturn
));
74 static void archive_read_error
PARAMS ((void));
77 /* Obnoxious test to see if dimwit is trying to dump the archive. */
82 /* PID of child program, if compress_option or remote archive access. */
83 static pid_t child_pid
;
85 /* Error recovery stuff */
86 static int read_error_count
;
88 /* Have we hit EOF yet? */
91 /* Checkpointing counter */
92 static int checkpoint
;
94 /* We're reading, but we just read the last block and its time to update. */
95 /* As least EXTERN like this one as possible. FIXME! */
96 extern int time_to_start_writing
;
98 int file_to_switch_to
= -1; /* if remote update, close archive, and use
99 this descriptor to write to */
101 static int volno
= 1; /* which volume of a multi-volume tape we're
103 static int global_volno
= 1; /* volume number to print in external
106 /* The pointer save_name, which is set in function dump_file() of module
107 create.c, points to the original long filename instead of the new,
108 shorter mangled name that is set in start_header() of module create.c.
109 The pointer save_name is only used in multi-volume mode when the file
110 being processed is non-sparse; if a file is split between volumes, the
111 save_name is used in generating the LF_MULTIVOL record on the second
112 volume. (From Pierce Cantrell, 1991-08-13.) */
114 char *save_name
; /* name of the file we are currently writing */
115 off_t save_totsize
; /* total size of file we are writing, only
116 valid if save_name is nonzero */
117 off_t save_sizeleft
; /* where we are in the file we are writing,
118 only valid if save_name is nonzero */
120 int write_archive_to_stdout
;
122 /* Used by flush_read and flush_write to store the real info about saved
124 static char *real_s_name
;
125 static off_t real_s_totsize
;
126 static off_t real_s_sizeleft
;
131 print_total_written (void)
133 tarlong written
= prev_written
+ bytes_written
;
134 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
135 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
136 char rate
[LONGEST_HUMAN_READABLE
+ 1];
139 #if HAVE_CLOCK_GETTIME
141 if (clock_gettime (CLOCK_REALTIME
, &now
) == 0)
142 seconds
= ((now
.tv_sec
- start_timespec
.tv_sec
)
143 + (now
.tv_nsec
- start_timespec
.tv_nsec
) / 1e9
);
146 seconds
= time (0) - start_time
;
148 sprintf (bytes
, TARLONG_FORMAT
, written
);
150 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
151 fprintf (stderr
, _("Total bytes written: %s (%sB, %sB/s)\n"), bytes
,
152 human_readable ((uintmax_t) written
, abbr
, 1, -1024),
153 (0 < seconds
&& written
/ seconds
< (uintmax_t) -1
154 ? human_readable ((uintmax_t) (written
/ seconds
), rate
, 1, -1024)
158 /* Compute and return the block ordinal at current_block. */
160 current_block_ordinal (void)
162 return record_start_block
+ (current_block
- record_start
);
165 /* If the EOF flag is set, reset it, as well as current_block, etc. */
172 current_block
= record_start
;
173 record_end
= record_start
+ blocking_factor
;
174 access_mode
= ACCESS_WRITE
;
178 /* Return the location of the next available input or output block.
179 Return zero for EOF. Once we have returned zero, we just keep returning
180 it, to avoid accidentally going on to the next file on the tape. */
182 find_next_block (void)
184 if (current_block
== record_end
)
189 if (current_block
== record_end
)
195 return current_block
;
198 /* Indicate that we have used all blocks up thru BLOCK.
199 FIXME: should the arg have an off-by-1? */
201 set_next_block_after (union block
*block
)
203 while (block
>= current_block
)
206 /* Do *not* flush the archive here. If we do, the same argument to
207 set_next_block_after could mean the next block (if the input record
208 is exactly one block long), which is not what is intended. */
210 if (current_block
> record_end
)
214 /* Return the number of bytes comprising the space between POINTER
215 through the end of the current buffer of blocks. This space is
216 available for filling with data, or taking data from. POINTER is
217 usually (but not always) the result previous find_next_block call. */
219 available_space_after (union block
*pointer
)
221 return record_end
->buffer
- pointer
->buffer
;
224 /* Close file having descriptor FD, and abort if close unsuccessful. */
229 close_error (_("(pipe)"));
232 /* Duplicate file descriptor FROM into becoming INTO.
233 INTO is closed first and has to be the next available slot. */
235 xdup2 (int from
, int into
)
239 int status
= close (into
);
241 if (status
!= 0 && errno
!= EBADF
)
244 FATAL_ERROR ((0, e
, _("Cannot close")));
252 FATAL_ERROR ((0, e
, _("Cannot dup")));
262 /* Set ARCHIVE for writing, then compressing an archive. */
264 child_open_for_compress (void)
266 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
269 /* Set ARCHIVE for uncompressing, then reading an archive. */
271 child_open_for_uncompress (void)
273 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
276 #else /* not MSDOS */
278 /* Return nonzero if NAME is the name of a regular file, or if the file
279 does not exist (so it would be created as a regular file). */
281 is_regular_file (const char *name
)
285 if (stat (name
, &stbuf
) == 0)
286 return S_ISREG (stbuf
.st_mode
);
288 return errno
== ENOENT
;
292 write_archive_buffer (void)
297 while (0 <= (status
= rmtwrite (archive
, record_start
->buffer
+ written
,
298 record_size
- written
)))
301 if (written
== record_size
302 || _isrmt (archive
) || ! S_ISFIFO (archive_stat
.st_mode
))
306 return written
? written
: status
;
309 /* Set ARCHIVE for writing, then compressing an archive. */
311 child_open_for_compress (void)
315 pid_t grandchild_pid
;
319 child_pid
= xfork ();
323 /* The parent tar is still here! Just clean up. */
325 archive
= parent_pipe
[PWRITE
];
326 xclose (parent_pipe
[PREAD
]);
330 /* The new born child tar is here! */
332 program_name
= _("tar (child)");
334 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
);
335 xclose (parent_pipe
[PWRITE
]);
337 /* Check if we need a grandchild tar. This happens only if either:
338 a) we are writing stdout: to force reblocking;
339 b) the file is to be accessed by rmt: compressor doesn't know how;
340 c) the file is not a plain file. */
342 if (strcmp (archive_name_array
[0], "-") != 0
343 && !_remdev (archive_name_array
[0])
344 && is_regular_file (archive_name_array
[0]))
347 maybe_backup_file (archive_name_array
[0], 1);
349 /* We don't need a grandchild tar. Open the archive and launch the
352 archive
= creat (archive_name_array
[0], MODE_RW
);
355 int saved_errno
= errno
;
360 open_fatal (archive_name_array
[0]);
362 xdup2 (archive
, STDOUT_FILENO
);
363 execlp (use_compress_program_option
, use_compress_program_option
,
365 exec_fatal (use_compress_program_option
);
368 /* We do need a grandchild tar. */
371 grandchild_pid
= xfork ();
373 if (grandchild_pid
== 0)
375 /* The newborn grandchild tar is here! Launch the compressor. */
377 program_name
= _("tar (grandchild)");
379 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
380 xclose (child_pipe
[PREAD
]);
381 execlp (use_compress_program_option
, use_compress_program_option
,
383 exec_fatal (use_compress_program_option
);
386 /* The child tar is still here! */
388 /* Prepare for reblocking the data from the compressor into the archive. */
390 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
391 xclose (child_pipe
[PWRITE
]);
393 if (strcmp (archive_name_array
[0], "-") == 0)
394 archive
= STDOUT_FILENO
;
397 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
399 open_fatal (archive_name_array
[0]);
402 /* Let's read out of the stdin pipe and write an archive. */
410 /* Assemble a record. */
412 for (length
= 0, cursor
= record_start
->buffer
;
413 length
< record_size
;
414 length
+= status
, cursor
+= status
)
416 size_t size
= record_size
- length
;
418 if (size
< BLOCKSIZE
)
420 status
= safe_read (STDIN_FILENO
, cursor
, size
);
426 read_fatal (use_compress_program_option
);
428 /* Copy the record. */
432 /* We hit the end of the file. Write last record at
433 full length, as the only role of the grandchild is
434 doing proper reblocking. */
438 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
439 status
= write_archive_buffer ();
440 if (status
!= record_size
)
441 archive_write_error (status
);
444 /* There is nothing else to read, break out. */
448 status
= write_archive_buffer ();
449 if (status
!= record_size
)
450 archive_write_error (status
);
457 /* Propagate any failure of the grandchild back to the parent. */
459 while (waitpid (grandchild_pid
, &wait_status
, 0) == -1)
462 waitpid_error (use_compress_program_option
);
466 if (WIFSIGNALED (wait_status
))
468 kill (child_pid
, WTERMSIG (wait_status
));
469 exit_status
= TAREXIT_FAILURE
;
471 else if (WEXITSTATUS (wait_status
) != 0)
472 exit_status
= WEXITSTATUS (wait_status
);
477 /* Set ARCHIVE for uncompressing, then reading an archive. */
479 child_open_for_uncompress (void)
483 pid_t grandchild_pid
;
487 child_pid
= xfork ();
491 /* The parent tar is still here! Just clean up. */
493 read_full_records_option
= 1;
494 archive
= parent_pipe
[PREAD
];
495 xclose (parent_pipe
[PWRITE
]);
499 /* The newborn child tar is here! */
501 program_name
= _("tar (child)");
503 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
);
504 xclose (parent_pipe
[PREAD
]);
506 /* Check if we need a grandchild tar. This happens only if either:
507 a) we're reading stdin: to force unblocking;
508 b) the file is to be accessed by rmt: compressor doesn't know how;
509 c) the file is not a plain file. */
511 if (strcmp (archive_name_array
[0], "-") != 0
512 && !_remdev (archive_name_array
[0])
513 && is_regular_file (archive_name_array
[0]))
515 /* We don't need a grandchild tar. Open the archive and lauch the
518 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
520 open_fatal (archive_name_array
[0]);
521 xdup2 (archive
, STDIN_FILENO
);
522 execlp (use_compress_program_option
, use_compress_program_option
,
524 exec_fatal (use_compress_program_option
);
527 /* We do need a grandchild tar. */
530 grandchild_pid
= xfork ();
532 if (grandchild_pid
== 0)
534 /* The newborn grandchild tar is here! Launch the uncompressor. */
536 program_name
= _("tar (grandchild)");
538 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
539 xclose (child_pipe
[PWRITE
]);
540 execlp (use_compress_program_option
, use_compress_program_option
,
542 exec_fatal (use_compress_program_option
);
545 /* The child tar is still here! */
547 /* Prepare for unblocking the data from the archive into the
550 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
551 xclose (child_pipe
[PREAD
]);
553 if (strcmp (archive_name_array
[0], "-") == 0)
554 archive
= STDIN_FILENO
;
556 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
557 MODE_RW
, rsh_command_option
);
559 open_fatal (archive_name_array
[0]);
561 /* Let's read the archive and pipe it into stdout. */
570 read_error_count
= 0;
573 status
= rmtread (archive
, record_start
->buffer
, record_size
);
576 archive_read_error ();
581 cursor
= record_start
->buffer
;
585 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
586 if (full_write (STDOUT_FILENO
, cursor
, count
) != count
)
587 write_error (use_compress_program_option
);
593 xclose (STDOUT_FILENO
);
598 /* Propagate any failure of the grandchild back to the parent. */
600 while (waitpid (grandchild_pid
, &wait_status
, 0) == -1)
603 waitpid_error (use_compress_program_option
);
607 if (WIFSIGNALED (wait_status
))
609 kill (child_pid
, WTERMSIG (wait_status
));
610 exit_status
= TAREXIT_FAILURE
;
612 else if (WEXITSTATUS (wait_status
) != 0)
613 exit_status
= WEXITSTATUS (wait_status
);
618 #endif /* not MSDOS */
620 /* Check the LABEL block against the volume label, seen as a globbing
621 pattern. Return true if the pattern matches. In case of failure,
622 retry matching a volume sequence number before giving up in
623 multi-volume mode. */
625 check_label_pattern (union block
*label
)
630 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
633 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
636 if (!multi_volume_option
)
639 string
= xmalloc (strlen (volume_label_option
)
640 + sizeof VOLUME_LABEL_APPEND
+ 1);
641 strcpy (string
, volume_label_option
);
642 strcat (string
, VOLUME_LABEL_APPEND
);
643 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
648 /* Open an archive file. The argument specifies whether we are
649 reading or writing, or both. */
651 open_archive (enum access_mode wanted_access
)
653 int backed_up_flag
= 0;
655 stdlis
= to_stdout_option
? stderr
: stdout
;
657 if (record_size
== 0)
658 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
660 if (archive_names
== 0)
661 FATAL_ERROR ((0, 0, _("No archive name given")));
663 current_file_name
= 0;
664 current_link_name
= 0;
668 if (multi_volume_option
)
671 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
672 record_start
= valloc (record_size
+ (2 * BLOCKSIZE
));
677 record_start
= valloc (record_size
);
679 FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
682 current_block
= record_start
;
683 record_end
= record_start
+ blocking_factor
;
684 /* When updating the archive, we start with reading. */
685 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
687 if (use_compress_program_option
)
689 if (multi_volume_option
)
690 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
692 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
694 switch (wanted_access
)
697 child_open_for_uncompress ();
701 child_open_for_compress ();
705 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
709 if (wanted_access
== ACCESS_WRITE
710 && strcmp (archive_name_array
[0], "-") == 0)
713 else if (strcmp (archive_name_array
[0], "-") == 0)
715 read_full_records_option
= 1; /* could be a pipe, be safe */
717 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
719 switch (wanted_access
)
722 archive
= STDIN_FILENO
;
726 archive
= STDOUT_FILENO
;
731 archive
= STDIN_FILENO
;
733 write_archive_to_stdout
= 1;
737 else if (verify_option
)
738 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
739 MODE_RW
, rsh_command_option
);
741 switch (wanted_access
)
744 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
745 MODE_RW
, rsh_command_option
);
751 maybe_backup_file (archive_name_array
[0], 1);
754 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
759 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
760 MODE_RW
, rsh_command_option
);
765 || (! _isrmt (archive
) && fstat (archive
, &archive_stat
) < 0))
767 int saved_errno
= errno
;
772 open_fatal (archive_name_array
[0]);
777 /* Detect if outputting to "/dev/null". */
779 static char const dev_null
[] = "/dev/null";
780 struct stat dev_null_stat
;
783 (strcmp (archive_name_array
[0], dev_null
) == 0
784 || (! _isrmt (archive
)
785 && S_ISCHR (archive_stat
.st_mode
)
786 && stat (dev_null
, &dev_null_stat
) == 0
787 && archive_stat
.st_dev
== dev_null_stat
.st_dev
788 && archive_stat
.st_ino
== dev_null_stat
.st_ino
));
791 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
793 ar_dev
= archive_stat
.st_dev
;
794 ar_ino
= archive_stat
.st_ino
;
799 #endif /* not MSDOS */
802 setmode (archive
, O_BINARY
);
805 switch (wanted_access
)
809 record_end
= record_start
; /* set up for 1st record = # 0 */
810 find_next_block (); /* read it in, check for EOF */
812 if (volume_label_option
)
814 union block
*label
= find_next_block ();
817 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
818 quote (volume_label_option
)));
819 if (!check_label_pattern (label
))
820 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
821 quote_n (0, label
->header
.name
),
822 quote_n (1, volume_label_option
)));
827 if (volume_label_option
)
829 memset (record_start
, 0, BLOCKSIZE
);
830 if (multi_volume_option
)
831 sprintf (record_start
->header
.name
, "%s Volume 1",
832 volume_label_option
);
834 strcpy (record_start
->header
.name
, volume_label_option
);
836 assign_string (¤t_file_name
, record_start
->header
.name
);
838 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
839 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
840 finish_header (record_start
);
849 /* Perform a write to flush the buffer. */
856 if (checkpoint_option
&& !(++checkpoint
% 10))
857 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
859 if (tape_length_option
&& tape_length_option
<= bytes_written
)
864 else if (dev_null_output
)
865 status
= record_size
;
867 status
= write_archive_buffer ();
868 if (status
!= record_size
&& !multi_volume_option
)
869 archive_write_error (status
);
872 bytes_written
+= status
;
874 if (status
== record_size
)
876 if (multi_volume_option
)
882 assign_string (&real_s_name
, 0);
888 cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
889 while (ISSLASH (*cursor
))
892 assign_string (&real_s_name
, cursor
);
893 real_s_totsize
= save_totsize
;
894 real_s_sizeleft
= save_sizeleft
;
899 /* We're multivol. Panic if we didn't get the right kind of response. */
901 /* ENXIO is for the UNIX PC. */
902 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
903 archive_write_error (status
);
905 /* If error indicates a short write, we just move to the next tape. */
907 if (!new_volume (ACCESS_WRITE
))
911 prev_written
+= bytes_written
;
914 if (volume_label_option
&& real_s_name
)
919 else if (volume_label_option
|| real_s_name
)
927 if (volume_label_option
)
929 memset (record_start
, 0, BLOCKSIZE
);
930 sprintf (record_start
->header
.name
, "%s Volume %d",
931 volume_label_option
, volno
);
932 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
933 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
934 finish_header (record_start
);
941 if (volume_label_option
)
944 memset (record_start
, 0, BLOCKSIZE
);
946 /* FIXME: Michael P Urban writes: [a long name file] is being written
947 when a new volume rolls around [...] Looks like the wrong value is
948 being preserved in real_s_name, though. */
950 strcpy (record_start
->header
.name
, real_s_name
);
951 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
952 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
953 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
954 record_start
->oldgnu_header
.offset
);
955 tmp
= verbose_option
;
957 finish_header (record_start
);
958 verbose_option
= tmp
;
960 if (volume_label_option
)
964 status
= write_archive_buffer ();
965 if (status
!= record_size
)
966 archive_write_error (status
);
968 bytes_written
+= status
;
972 record_start
+= copy_back
;
973 memcpy (current_block
,
974 record_start
+ blocking_factor
- copy_back
,
975 copy_back
* BLOCKSIZE
);
976 current_block
+= copy_back
;
978 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
979 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
980 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
981 assign_string (&real_s_name
, 0);
984 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
986 while (ISSLASH (*cursor
))
989 assign_string (&real_s_name
, cursor
);
990 real_s_sizeleft
= save_sizeleft
;
991 real_s_totsize
= save_totsize
;
997 /* Handle write errors on the archive. Write errors are always fatal.
998 Hitting the end of a volume does not cause a write error unless the
999 write was the first record of the volume. */
1001 archive_write_error (ssize_t status
)
1003 /* It might be useful to know how much was written before the error
1008 print_total_written ();
1012 write_fatal_details (*archive_name_cursor
, status
, record_size
);
1015 /* Handle read errors on the archive. If the read should be retried,
1016 return to the caller. */
1018 archive_read_error (void)
1020 read_error (*archive_name_cursor
);
1022 if (record_start_block
== 0)
1023 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1025 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1026 then give up on reading the archive. */
1028 if (read_error_count
++ > READ_ERROR_MAX
)
1029 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1033 /* Perform a read to flush the buffer. */
1037 ssize_t status
; /* result from system call */
1038 size_t left
; /* bytes left */
1039 char *more
; /* pointer to next byte to read */
1041 if (checkpoint_option
&& !(++checkpoint
% 10))
1042 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1044 /* Clear the count of errors. This only applies to a single call to
1047 read_error_count
= 0; /* clear error count */
1049 if (write_archive_to_stdout
&& record_start_block
!= 0)
1051 archive
= STDOUT_FILENO
;
1052 status
= write_archive_buffer ();
1053 archive
= STDIN_FILENO
;
1054 if (status
!= record_size
)
1055 archive_write_error (status
);
1057 if (multi_volume_option
)
1061 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
1063 while (ISSLASH (*cursor
))
1066 assign_string (&real_s_name
, cursor
);
1067 real_s_sizeleft
= save_sizeleft
;
1068 real_s_totsize
= save_totsize
;
1072 assign_string (&real_s_name
, 0);
1074 real_s_sizeleft
= 0;
1079 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1080 if (status
== record_size
)
1084 || (status
< 0 && errno
== ENOSPC
)
1085 || (status
> 0 && !read_full_records_option
))
1086 && multi_volume_option
)
1088 union block
*cursor
;
1091 switch (subcommand_option
)
1093 case APPEND_SUBCOMMAND
:
1094 case CAT_SUBCOMMAND
:
1095 case UPDATE_SUBCOMMAND
:
1096 if (!new_volume (ACCESS_UPDATE
))
1101 if (!new_volume (ACCESS_READ
))
1107 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1110 archive_read_error ();
1113 if (status
!= record_size
)
1116 cursor
= record_start
;
1118 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1120 if (volume_label_option
)
1122 if (!check_label_pattern (cursor
))
1124 WARN ((0, 0, _("Volume %s does not match %s"),
1125 quote_n (0, cursor
->header
.name
),
1126 quote_n (1, volume_label_option
)));
1133 fprintf (stdlis
, _("Reading %s\n"), quote (cursor
->header
.name
));
1136 else if (volume_label_option
)
1137 WARN ((0, 0, _("WARNING: No volume header")));
1142 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1143 || strcmp (cursor
->header
.name
, real_s_name
))
1145 WARN ((0, 0, _("%s is not continued on this volume"),
1146 quote (real_s_name
)));
1151 s1
= UINTMAX_FROM_HEADER (cursor
->header
.size
);
1152 s2
= UINTMAX_FROM_HEADER (cursor
->oldgnu_header
.offset
);
1153 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
1155 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1156 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1157 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1159 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1160 quote (cursor
->header
.name
),
1161 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1162 STRINGIFY_BIGINT (s1
, s1buf
),
1163 STRINGIFY_BIGINT (s2
, s2buf
)));
1168 if (real_s_totsize
- real_s_sizeleft
1169 != OFF_FROM_HEADER (cursor
->oldgnu_header
.offset
))
1171 WARN ((0, 0, _("This volume is out of sequence")));
1178 current_block
= cursor
;
1181 else if (status
< 0)
1183 archive_read_error ();
1184 goto error_loop
; /* try again */
1188 more
= record_start
->buffer
+ status
;
1189 left
= record_size
- status
;
1191 while (left
% BLOCKSIZE
!= 0
1192 || (left
&& status
&& read_full_records_option
))
1195 while ((status
= rmtread (archive
, more
, left
)) < 0)
1196 archive_read_error ();
1200 if (left
% BLOCKSIZE
!= 0)
1201 ERROR ((0, 0, _("%d garbage bytes ignored at end of archive"),
1202 (int) ((record_size
- left
) % BLOCKSIZE
)));
1206 if (! read_full_records_option
)
1207 FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
1208 (unsigned long) (record_size
- left
)));
1210 /* User warned us about this. Fix up. */
1216 /* FIXME: for size=0, multi-volume support. On the first record, warn
1217 about the problem. */
1219 if (!read_full_records_option
&& verbose_option
1220 && record_start_block
== 0 && status
> 0)
1221 WARN ((0, 0, _("Record size = %lu blocks"),
1222 (unsigned long) ((record_size
- left
) / BLOCKSIZE
)));
1224 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
1227 /* Flush the current buffer to/from the archive. */
1229 flush_archive (void)
1231 record_start_block
+= record_end
- record_start
;
1232 current_block
= record_start
;
1233 record_end
= record_start
+ blocking_factor
;
1235 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1237 access_mode
= ACCESS_WRITE
;
1238 time_to_start_writing
= 0;
1240 if (file_to_switch_to
>= 0)
1242 if (rmtclose (archive
) != 0)
1243 close_warn (*archive_name_cursor
);
1245 archive
= file_to_switch_to
;
1248 backspace_output ();
1251 switch (access_mode
)
1266 /* Backspace the archive descriptor by one record worth. If it's a
1267 tape, MTIOCTOP will work. If it's something else, try to seek on
1268 it. If we can't seek, we lose! */
1270 backspace_output (void)
1274 struct mtop operation
;
1276 operation
.mt_op
= MTBSR
;
1277 operation
.mt_count
= 1;
1278 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1280 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1286 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1288 /* Seek back to the beginning of this record and start writing there. */
1290 position
-= record_size
;
1293 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1295 /* Lseek failed. Try a different method. */
1298 _("Cannot backspace archive file; it may be unreadable without -i")));
1300 /* Replace the first part of the record with NULs. */
1302 if (record_start
->buffer
!= output_start
)
1303 memset (record_start
->buffer
, 0,
1304 output_start
- record_start
->buffer
);
1309 /* Close the archive file. */
1311 close_archive (void)
1313 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1318 /* Manage to fully drain a pipe we might be reading, so to not break it on
1319 the producer after the EOF block. FIXME: one of these days, GNU tar
1320 might become clever enough to just stop working, once there is no more
1321 work to do, we might have to revise this area in such time. */
1323 if (access_mode
== ACCESS_READ
1324 && ! _isrmt (archive
)
1325 && S_ISFIFO (archive_stat
.st_mode
))
1326 while (rmtread (archive
, record_start
->buffer
, record_size
) > 0)
1333 if (rmtclose (archive
) != 0)
1334 close_warn (*archive_name_cursor
);
1342 while (waitpid (child_pid
, &wait_status
, 0) == -1)
1345 waitpid_error (use_compress_program_option
);
1349 if (WIFSIGNALED (wait_status
))
1350 ERROR ((0, 0, _("Child died with signal %d"),
1351 WTERMSIG (wait_status
)));
1352 else if (WEXITSTATUS (wait_status
) != 0)
1353 ERROR ((0, 0, _("Child returned status %d"),
1354 WEXITSTATUS (wait_status
)));
1358 if (current_file_name
)
1359 free (current_file_name
);
1360 if (current_link_name
)
1361 free (current_link_name
);
1366 free (multi_volume_option
? record_start
- 2 : record_start
);
1369 /* Called to initialize the global volume number. */
1371 init_volume_number (void)
1373 FILE *file
= fopen (volno_file_option
, "r");
1377 if (fscanf (file
, "%d", &global_volno
) != 1
1378 || global_volno
< 0)
1379 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1380 quotearg_colon (volno_file_option
)));
1382 read_error (volno_file_option
);
1383 if (fclose (file
) != 0)
1384 close_error (volno_file_option
);
1386 else if (errno
!= ENOENT
)
1387 open_error (volno_file_option
);
1390 /* Called to write out the closing global volume number. */
1392 closeout_volume_number (void)
1394 FILE *file
= fopen (volno_file_option
, "w");
1398 fprintf (file
, "%d\n", global_volno
);
1400 write_error (volno_file_option
);
1401 if (fclose (file
) != 0)
1402 close_error (volno_file_option
);
1405 open_error (volno_file_option
);
1408 /* We've hit the end of the old volume. Close it and open the next one.
1409 Return nonzero on success. */
1411 new_volume (enum access_mode access
)
1413 static FILE *read_file
;
1416 if (!read_file
&& !info_script_option
)
1417 /* FIXME: if fopen is used, it will never be closed. */
1418 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1425 if (rmtclose (archive
) != 0)
1426 close_warn (*archive_name_cursor
);
1429 if (global_volno
< 0)
1430 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1432 archive_name_cursor
++;
1433 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1435 archive_name_cursor
= archive_name_array
;
1442 /* We have to prompt from now on. */
1444 if (info_script_option
)
1446 if (volno_file_option
)
1447 closeout_volume_number ();
1448 if (system (info_script_option
) != 0)
1449 FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option
));
1454 char input_buffer
[80];
1456 fputc ('\007', stderr
);
1458 _("Prepare volume #%d for %s and hit return: "),
1459 global_volno
, quote (*archive_name_cursor
));
1462 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
1464 WARN ((0, 0, _("EOF where user reply was expected")));
1466 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1467 && subcommand_option
!= LIST_SUBCOMMAND
1468 && subcommand_option
!= DIFF_SUBCOMMAND
)
1469 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1473 if (input_buffer
[0] == '\n'
1474 || input_buffer
[0] == 'y'
1475 || input_buffer
[0] == 'Y')
1478 switch (input_buffer
[0])
1482 fprintf (stderr
, _("\
1483 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1485 ! Spawn a subshell\n\
1486 ? Print this list\n"));
1493 WARN ((0, 0, _("No new volume; exiting.\n")));
1495 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1496 && subcommand_option
!= LIST_SUBCOMMAND
1497 && subcommand_option
!= DIFF_SUBCOMMAND
)
1498 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1503 /* Get new file name. */
1506 char *name
= &input_buffer
[1];
1509 while (*name
== ' ' || *name
== '\t')
1512 while (*cursor
&& *cursor
!= '\n')
1516 /* FIXME: the following allocation is never reclaimed. */
1517 *archive_name_cursor
= xstrdup (name
);
1523 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1524 #else /* not MSDOS */
1527 const char *shell
= getenv ("SHELL");
1533 execlp (shell
, "-sh", "-i", 0);
1539 while (waitpid (child
, &wait_status
, 0) == -1)
1542 waitpid_error (shell
);
1547 #endif /* not MSDOS */
1554 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1555 rsh_command_option
);
1560 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1561 rsh_command_option
);
1566 maybe_backup_file (*archive_name_cursor
, 1);
1567 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1568 rsh_command_option
);
1572 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1573 rsh_command_option
);
1579 open_warn (*archive_name_cursor
);
1580 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1581 undo_last_backup ();
1586 setmode (archive
, O_BINARY
);