1 /* Buffer management for tar.
2 Copyright 1988, 92, 93, 94, 96, 97, 1999 Free Software Foundation, Inc.
3 Written by John Gilmore, on 1985-08-25.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Enable GNU extensions in fnmatch.h. */
21 # define _GNU_SOURCE 1
37 # include <sys/inode.h>
47 #define PREAD 0 /* read file descriptor from pipe() */
48 #define PWRITE 1 /* write file descriptor from pipe() */
50 /* Number of retries before giving up on read. */
51 #define READ_ERROR_MAX 10
53 /* Globbing pattern to append to volume label if initial match failed. */
54 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
58 static tarlong prev_written
; /* bytes written on previous volumes */
59 static tarlong bytes_written
; /* bytes written on this volume */
61 /* FIXME: The following four variables should ideally be static to this
62 module. However, this cannot be done yet, as update.c uses the first
63 three a lot, and compare.c uses the fourth. The cleanup continues! */
65 union block
*record_start
; /* start of record of archive */
66 union block
*record_end
; /* last+1 block of archive record */
67 union block
*current_block
; /* current block of archive */
68 enum access_mode access_mode
; /* how do we handle the archive */
69 static struct stat archive_stat
; /* stat block for archive file */
71 static off_t record_start_block
; /* block ordinal at record_start */
73 /* Where we write list messages (not errors, not interactions) to. Stdout
74 unless we're writing a pipe, in which case stderr. */
77 static void backspace_output
PARAMS ((void));
78 static int new_volume
PARAMS ((enum access_mode
));
79 static void archive_write_error
PARAMS ((ssize_t
));
80 static void archive_read_error
PARAMS ((void));
83 /* Obnoxious test to see if dimwit is trying to dump the archive. */
88 /* PID of child program, if compress_option or remote archive access. */
89 static pid_t child_pid
;
91 /* Error recovery stuff */
92 static int read_error_count
;
94 /* Have we hit EOF yet? */
97 /* Checkpointing counter */
98 static int checkpoint
;
100 /* We're reading, but we just read the last block and its time to update. */
101 /* As least EXTERN like this one as possible. FIXME! */
102 extern int time_to_start_writing
;
104 int file_to_switch_to
= -1; /* if remote update, close archive, and use
105 this descriptor to write to */
107 static int volno
= 1; /* which volume of a multi-volume tape we're
109 static int global_volno
= 1; /* volume number to print in external
112 /* The pointer save_name, which is set in function dump_file() of module
113 create.c, points to the original long filename instead of the new,
114 shorter mangled name that is set in start_header() of module create.c.
115 The pointer save_name is only used in multi-volume mode when the file
116 being processed is non-sparse; if a file is split between volumes, the
117 save_name is used in generating the LF_MULTIVOL record on the second
118 volume. (From Pierce Cantrell, 1991-08-13.) */
120 char *save_name
; /* name of the file we are currently writing */
121 off_t save_totsize
; /* total size of file we are writing, only
122 valid if save_name is nonzero */
123 off_t save_sizeleft
; /* where we are in the file we are writing,
124 only valid if save_name is nonzero */
126 int write_archive_to_stdout
;
128 /* Used by flush_read and flush_write to store the real info about saved
130 static char *real_s_name
;
131 static off_t real_s_totsize
;
132 static off_t real_s_sizeleft
;
137 print_total_written (void)
139 tarlong written
= prev_written
+ bytes_written
;
140 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
141 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
142 char rate
[LONGEST_HUMAN_READABLE
+ 1];
145 #if HAVE_CLOCK_GETTIME
147 if (clock_gettime (CLOCK_REALTIME
, &now
) == 0)
148 seconds
= ((now
.tv_sec
- start_timespec
.tv_sec
)
149 + (now
.tv_nsec
- start_timespec
.tv_nsec
) / 1e9
);
152 seconds
= time (0) - start_time
;
154 sprintf (bytes
, TARLONG_FORMAT
, written
);
156 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
157 fprintf (stderr
, _("Total bytes written: %s (%sB, %sB/s)\n"), bytes
,
158 human_readable ((uintmax_t) written
, abbr
, 1, -1024),
159 (0 < seconds
&& written
/ seconds
< (uintmax_t) -1
160 ? human_readable ((uintmax_t) (written
/ seconds
), rate
, 1, -1024)
164 /*--------------------------------------------------------.
165 | Compute and return the block ordinal at current_block. |
166 `--------------------------------------------------------*/
169 current_block_ordinal (void)
171 return record_start_block
+ (current_block
- record_start
);
174 /*------------------------------------------------------------------.
175 | If the EOF flag is set, reset it, as well as current_block, etc. |
176 `------------------------------------------------------------------*/
184 current_block
= record_start
;
185 record_end
= record_start
+ blocking_factor
;
186 access_mode
= ACCESS_WRITE
;
190 /*-------------------------------------------------------------------------.
191 | Return the location of the next available input or output block. |
192 | Return zero for EOF. Once we have returned zero, we just keep returning |
193 | it, to avoid accidentally going on to the next file on the tape. |
194 `-------------------------------------------------------------------------*/
197 find_next_block (void)
199 if (current_block
== record_end
)
204 if (current_block
== record_end
)
210 return current_block
;
213 /*------------------------------------------------------.
214 | Indicate that we have used all blocks up thru BLOCK. |
216 | FIXME: should the arg have an off-by-1? |
217 `------------------------------------------------------*/
220 set_next_block_after (union block
*block
)
222 while (block
>= current_block
)
225 /* Do *not* flush the archive here. If we do, the same argument to
226 set_next_block_after could mean the next block (if the input record
227 is exactly one block long), which is not what is intended. */
229 if (current_block
> record_end
)
233 /*------------------------------------------------------------------------.
234 | Return the number of bytes comprising the space between POINTER through |
235 | the end of the current buffer of blocks. This space is available for |
236 | filling with data, or taking data from. POINTER is usually (but not |
237 | always) the result previous find_next_block call. |
238 `------------------------------------------------------------------------*/
241 available_space_after (union block
*pointer
)
243 return record_end
->buffer
- pointer
->buffer
;
246 /*------------------------------------------------------------------.
247 | Close file having descriptor FD, and abort if close unsuccessful. |
248 `------------------------------------------------------------------*/
256 FATAL_ERROR ((0, e
, _("Cannot close file #%d"), fd
));
260 /*-----------------------------------------------------------------------.
261 | Duplicate file descriptor FROM into becoming INTO, or else, issue |
262 | MESSAGE. INTO is closed first and has to be the next available slot. |
263 `-----------------------------------------------------------------------*/
266 xdup2 (int from
, int into
, const char *message
)
270 int status
= close (into
);
272 if (status
!= 0 && errno
!= EBADF
)
275 FATAL_ERROR ((0, e
, _("Cannot close file descriptor")));
280 int e
= status
< 0 ? errno
: 0;
281 FATAL_ERROR ((0, e
, _("Cannot properly duplicate %s"), message
));
289 /*-------------------------------------------------------.
290 | Set ARCHIVE for writing, then compressing an archive. |
291 `-------------------------------------------------------*/
294 child_open_for_compress (void)
296 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
299 /*---------------------------------------------------------.
300 | Set ARCHIVE for uncompressing, then reading an archive. |
301 `---------------------------------------------------------*/
304 child_open_for_uncompress (void)
306 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
309 #else /* not MSDOS */
311 /*---------------------------------------------------------------------.
312 | Return nonzero if NAME is the name of a regular file, or if the file |
313 | does not exist (so it would be created as a regular file). |
314 `---------------------------------------------------------------------*/
317 is_regular_file (const char *name
)
321 if (stat (name
, &stbuf
) == 0)
322 return S_ISREG (stbuf
.st_mode
);
324 return errno
== ENOENT
;
328 write_archive_buffer (void)
333 while (0 <= (status
= rmtwrite (archive
, record_start
->buffer
+ written
,
334 record_size
- written
)))
337 if (written
== record_size
338 || _isrmt (archive
) || ! S_ISFIFO (archive_stat
.st_mode
))
342 return written
? written
: status
;
345 /*-------------------------------------------------------.
346 | Set ARCHIVE for writing, then compressing an archive. |
347 `-------------------------------------------------------*/
350 child_open_for_compress (void)
354 pid_t grandchild_pid
;
357 child_pid
= xfork ();
361 /* The parent tar is still here! Just clean up. */
363 archive
= parent_pipe
[PWRITE
];
364 xclose (parent_pipe
[PREAD
]);
368 /* The new born child tar is here! */
370 program_name
= _("tar (child)");
372 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
, _("(child) Pipe to stdin"));
373 xclose (parent_pipe
[PWRITE
]);
375 /* Check if we need a grandchild tar. This happens only if either:
376 a) we are writing stdout: to force reblocking;
377 b) the file is to be accessed by rmt: compressor doesn't know how;
378 c) the file is not a plain file. */
380 if (strcmp (archive_name_array
[0], "-") != 0
381 && !_remdev (archive_name_array
[0])
382 && is_regular_file (archive_name_array
[0]))
385 maybe_backup_file (archive_name_array
[0], 1);
387 /* We don't need a grandchild tar. Open the archive and launch the
390 archive
= creat (archive_name_array
[0], MODE_RW
);
393 int saved_errno
= errno
;
398 open_fatal (archive_name_array
[0]);
400 xdup2 (archive
, STDOUT_FILENO
, _("Archive to stdout"));
401 execlp (use_compress_program_option
, use_compress_program_option
,
403 exec_fatal (use_compress_program_option
);
406 /* We do need a grandchild tar. */
409 grandchild_pid
= xfork ();
411 if (grandchild_pid
> 0)
413 /* The child tar is still here! Launch the compressor. */
415 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
,
416 _("((child)) Pipe to stdout"));
417 xclose (child_pipe
[PREAD
]);
418 execlp (use_compress_program_option
, use_compress_program_option
,
420 exec_fatal (use_compress_program_option
);
423 /* The new born grandchild tar is here! */
425 program_name
= _("tar (grandchild)");
427 /* Prepare for reblocking the data from the compressor into the archive. */
429 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("(grandchild) Pipe to stdin"));
430 xclose (child_pipe
[PWRITE
]);
432 if (strcmp (archive_name_array
[0], "-") == 0)
433 archive
= STDOUT_FILENO
;
436 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
438 open_fatal (archive_name_array
[0]);
441 /* Let's read out of the stdin pipe and write an archive. */
449 /* Assemble a record. */
451 for (length
= 0, cursor
= record_start
->buffer
;
452 length
< record_size
;
453 length
+= status
, cursor
+= status
)
455 size_t size
= record_size
- length
;
457 if (size
< BLOCKSIZE
)
459 status
= safe_read (STDIN_FILENO
, cursor
, size
);
465 read_fatal (use_compress_program_option
);
467 /* Copy the record. */
471 /* We hit the end of the file. Write last record at
472 full length, as the only role of the grandchild is
473 doing proper reblocking. */
477 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
478 status
= write_archive_buffer ();
479 if (status
!= record_size
)
480 archive_write_error (status
);
483 /* There is nothing else to read, break out. */
487 status
= write_archive_buffer ();
488 if (status
!= record_size
)
489 archive_write_error (status
);
498 /*---------------------------------------------------------.
499 | Set ARCHIVE for uncompressing, then reading an archive. |
500 `---------------------------------------------------------*/
503 child_open_for_uncompress (void)
507 pid_t grandchild_pid
;
510 child_pid
= xfork ();
514 /* The parent tar is still here! Just clean up. */
516 read_full_records_option
= 1;
517 archive
= parent_pipe
[PREAD
];
518 xclose (parent_pipe
[PWRITE
]);
522 /* The new born child tar is here! */
524 program_name
= _("tar (child)");
526 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
, _("(child) Pipe to stdout"));
527 xclose (parent_pipe
[PREAD
]);
529 /* Check if we need a grandchild tar. This happens only if either:
530 a) we're reading stdin: to force unblocking;
531 b) the file is to be accessed by rmt: compressor doesn't know how;
532 c) the file is not a plain file. */
534 if (strcmp (archive_name_array
[0], "-") != 0
535 && !_remdev (archive_name_array
[0])
536 && is_regular_file (archive_name_array
[0]))
538 /* We don't need a grandchild tar. Open the archive and lauch the
541 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
543 open_fatal (archive_name_array
[0]);
544 xdup2 (archive
, STDIN_FILENO
, _("Archive to stdin"));
545 execlp (use_compress_program_option
, use_compress_program_option
,
547 exec_fatal (use_compress_program_option
);
550 /* We do need a grandchild tar. */
553 grandchild_pid
= xfork ();
555 if (grandchild_pid
> 0)
557 /* The child tar is still here! Launch the uncompressor. */
559 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("((child)) Pipe to stdin"));
560 xclose (child_pipe
[PWRITE
]);
561 execlp (use_compress_program_option
, use_compress_program_option
,
563 exec_fatal (use_compress_program_option
);
566 /* The new born grandchild tar is here! */
568 program_name
= _("tar (grandchild)");
570 /* Prepare for unblocking the data from the archive into the uncompressor. */
572 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
, _("(grandchild) Pipe to stdout"));
573 xclose (child_pipe
[PREAD
]);
575 if (strcmp (archive_name_array
[0], "-") == 0)
576 archive
= STDIN_FILENO
;
578 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
579 MODE_RW
, rsh_command_option
);
581 open_fatal (archive_name_array
[0]);
583 /* Let's read the archive and pipe it into stdout. */
592 read_error_count
= 0;
595 status
= rmtread (archive
, record_start
->buffer
, record_size
);
598 archive_read_error ();
603 cursor
= record_start
->buffer
;
607 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
608 status
= full_write (STDOUT_FILENO
, cursor
, count
);
610 write_error (use_compress_program_option
);
614 ERROR ((0, 0, _("Write to compression program short %lu bytes"),
615 (unsigned long) (count
- status
)));
630 #endif /* not MSDOS */
632 /*--------------------------------------------------------------------------.
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, retry |
635 | matching a volume sequence number before giving up in multi-volume mode. |
636 `--------------------------------------------------------------------------*/
639 check_label_pattern (union block
*label
)
644 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
647 if (!multi_volume_option
)
650 string
= xmalloc (strlen (volume_label_option
)
651 + sizeof VOLUME_LABEL_APPEND
+ 1);
652 strcpy (string
, volume_label_option
);
653 strcat (string
, VOLUME_LABEL_APPEND
);
654 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
659 /*------------------------------------------------------------------------.
660 | Open an archive file. The argument specifies whether we are reading or |
661 | writing, or both. |
662 `------------------------------------------------------------------------*/
665 open_archive (enum access_mode wanted_access
)
667 int backed_up_flag
= 0;
669 stdlis
= to_stdout_option
? stderr
: stdout
;
671 if (record_size
== 0)
672 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
674 if (archive_names
== 0)
675 FATAL_ERROR ((0, 0, _("No archive name given")));
677 current_file_name
= 0;
678 current_link_name
= 0;
680 /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
681 constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
682 size that is reasonable to allocate a buffer. In the GNU system, there
683 is no fixed limit. The only correct thing to do is to use dynamic
684 allocation. (Roland McGrath) */
687 real_s_name
= xmalloc (PATH_MAX
);
688 /* FIXME: real_s_name is never freed. */
692 if (multi_volume_option
)
695 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
696 record_start
= valloc (record_size
+ (2 * BLOCKSIZE
));
701 record_start
= valloc (record_size
);
703 FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
706 current_block
= record_start
;
707 record_end
= record_start
+ blocking_factor
;
708 /* When updating the archive, we start with reading. */
709 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
711 if (use_compress_program_option
)
713 if (multi_volume_option
)
714 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
716 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
718 switch (wanted_access
)
721 child_open_for_uncompress ();
725 child_open_for_compress ();
729 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
733 if (wanted_access
== ACCESS_WRITE
734 && strcmp (archive_name_array
[0], "-") == 0)
737 else if (strcmp (archive_name_array
[0], "-") == 0)
739 read_full_records_option
= 1; /* could be a pipe, be safe */
741 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
743 switch (wanted_access
)
746 archive
= STDIN_FILENO
;
750 archive
= STDOUT_FILENO
;
755 archive
= STDIN_FILENO
;
757 write_archive_to_stdout
= 1;
761 else if (verify_option
)
762 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
763 MODE_RW
, rsh_command_option
);
765 switch (wanted_access
)
768 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
769 MODE_RW
, rsh_command_option
);
775 maybe_backup_file (archive_name_array
[0], 1);
778 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
783 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
784 MODE_RW
, rsh_command_option
);
789 || (! _isrmt (archive
) && fstat (archive
, &archive_stat
) < 0))
791 int saved_errno
= errno
;
796 open_fatal (archive_name_array
[0]);
801 /* Detect if outputting to "/dev/null". */
803 static char const dev_null
[] = "/dev/null";
804 struct stat dev_null_stat
;
807 (strcmp (archive_name_array
[0], dev_null
) == 0
808 || (! _isrmt (archive
)
809 && S_ISCHR (archive_stat
.st_mode
)
810 && stat (dev_null
, &dev_null_stat
) == 0
811 && S_ISCHR (dev_null_stat
.st_mode
)
812 && archive_stat
.st_rdev
== dev_null_stat
.st_rdev
));
815 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
817 ar_dev
= archive_stat
.st_dev
;
818 ar_ino
= archive_stat
.st_ino
;
823 #endif /* not MSDOS */
826 setmode (archive
, O_BINARY
);
829 switch (wanted_access
)
833 record_end
= record_start
; /* set up for 1st record = # 0 */
834 find_next_block (); /* read it in, check for EOF */
836 if (volume_label_option
)
838 union block
*label
= find_next_block ();
841 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
842 quote (volume_label_option
)));
843 if (!check_label_pattern (label
))
844 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
845 quote_n (0, label
->header
.name
),
846 quote_n (1, volume_label_option
)));
851 if (volume_label_option
)
853 memset (record_start
, 0, BLOCKSIZE
);
854 if (multi_volume_option
)
855 sprintf (record_start
->header
.name
, "%s Volume 1",
856 volume_label_option
);
858 strcpy (record_start
->header
.name
, volume_label_option
);
860 assign_string (¤t_file_name
, record_start
->header
.name
);
862 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
863 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
864 finish_header (record_start
);
873 /*--------------------------------------.
874 | Perform a write to flush the buffer. |
875 `--------------------------------------*/
883 if (checkpoint_option
&& !(++checkpoint
% 10))
884 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
886 if (tape_length_option
&& tape_length_option
<= bytes_written
)
891 else if (dev_null_output
)
892 status
= record_size
;
894 status
= write_archive_buffer ();
895 if (status
!= record_size
&& !multi_volume_option
)
896 archive_write_error (status
);
899 bytes_written
+= status
;
901 if (status
== record_size
)
903 if (multi_volume_option
)
909 real_s_name
[0] = '\0';
915 cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
916 while (*cursor
== '/')
919 strcpy (real_s_name
, cursor
);
920 real_s_totsize
= save_totsize
;
921 real_s_sizeleft
= save_sizeleft
;
926 /* We're multivol. Panic if we didn't get the right kind of response. */
928 /* ENXIO is for the UNIX PC. */
929 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
930 archive_write_error (status
);
932 /* If error indicates a short write, we just move to the next tape. */
934 if (!new_volume (ACCESS_WRITE
))
938 prev_written
+= bytes_written
;
941 if (volume_label_option
&& real_s_name
[0])
946 else if (volume_label_option
|| real_s_name
[0])
954 if (volume_label_option
)
956 memset (record_start
, 0, BLOCKSIZE
);
957 sprintf (record_start
->header
.name
, "%s Volume %d",
958 volume_label_option
, volno
);
959 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
960 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
961 finish_header (record_start
);
968 if (volume_label_option
)
971 memset (record_start
, 0, BLOCKSIZE
);
973 /* FIXME: Michael P Urban writes: [a long name file] is being written
974 when a new volume rolls around [...] Looks like the wrong value is
975 being preserved in real_s_name, though. */
977 strcpy (record_start
->header
.name
, real_s_name
);
978 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
979 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
980 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
981 record_start
->oldgnu_header
.offset
);
982 tmp
= verbose_option
;
984 finish_header (record_start
);
985 verbose_option
= tmp
;
987 if (volume_label_option
)
991 status
= write_archive_buffer ();
992 if (status
!= record_size
)
993 archive_write_error (status
);
995 bytes_written
+= status
;
999 record_start
+= copy_back
;
1000 memcpy (current_block
,
1001 record_start
+ blocking_factor
- copy_back
,
1002 copy_back
* BLOCKSIZE
);
1003 current_block
+= copy_back
;
1005 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
1006 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
1007 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
1008 real_s_name
[0] = '\0';
1011 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
1013 while (*cursor
== '/')
1016 strcpy (real_s_name
, cursor
);
1017 real_s_sizeleft
= save_sizeleft
;
1018 real_s_totsize
= save_totsize
;
1024 /*---------------------------------------------------------------------.
1025 | Handle write errors on the archive. Write errors are always fatal. |
1026 | Hitting the end of a volume does not cause a write error unless the |
1027 | write was the first record of the volume. |
1028 `---------------------------------------------------------------------*/
1031 archive_write_error (ssize_t status
)
1033 int saved_errno
= errno
;
1035 /* It might be useful to know how much was written before the error
1038 print_total_written ();
1042 errno
= saved_errno
;
1043 write_fatal (*archive_name_cursor
);
1046 FATAL_ERROR ((0, 0, _("Only wrote %lu of %lu bytes to %s"),
1047 (unsigned long) status
, (unsigned long) record_size
,
1048 quote (*archive_name_cursor
)));
1051 /*-------------------------------------------------------------------.
1052 | Handle read errors on the archive. If the read should be retried, |
1053 | returns to the caller. |
1054 `-------------------------------------------------------------------*/
1057 archive_read_error (void)
1059 read_error (*archive_name_cursor
);
1061 if (record_start_block
== 0)
1062 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1064 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1065 then give up on reading the archive. */
1067 if (read_error_count
++ > READ_ERROR_MAX
)
1068 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1072 /*-------------------------------------.
1073 | Perform a read to flush the buffer. |
1074 `-------------------------------------*/
1079 ssize_t status
; /* result from system call */
1080 size_t left
; /* bytes left */
1081 char *more
; /* pointer to next byte to read */
1083 if (checkpoint_option
&& !(++checkpoint
% 10))
1084 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1086 /* Clear the count of errors. This only applies to a single call to
1089 read_error_count
= 0; /* clear error count */
1091 if (write_archive_to_stdout
&& record_start_block
!= 0)
1093 archive
= STDOUT_FILENO
;
1094 status
= write_archive_buffer ();
1095 archive
= STDIN_FILENO
;
1096 if (status
!= record_size
)
1097 archive_write_error (status
);
1099 if (multi_volume_option
)
1103 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
1105 while (*cursor
== '/')
1108 strcpy (real_s_name
, cursor
);
1109 real_s_sizeleft
= save_sizeleft
;
1110 real_s_totsize
= save_totsize
;
1114 real_s_name
[0] = '\0';
1116 real_s_sizeleft
= 0;
1121 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1122 if (status
== record_size
)
1126 || (status
< 0 && errno
== ENOSPC
)
1127 || (status
> 0 && !read_full_records_option
))
1128 && multi_volume_option
)
1130 union block
*cursor
;
1133 switch (subcommand_option
)
1135 case APPEND_SUBCOMMAND
:
1136 case CAT_SUBCOMMAND
:
1137 case UPDATE_SUBCOMMAND
:
1138 if (!new_volume (ACCESS_UPDATE
))
1143 if (!new_volume (ACCESS_READ
))
1149 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1152 archive_read_error ();
1155 if (status
!= record_size
)
1158 cursor
= record_start
;
1160 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1162 if (volume_label_option
)
1164 if (!check_label_pattern (cursor
))
1166 WARN ((0, 0, _("Volume %s does not match %s"),
1167 quote_n (0, cursor
->header
.name
),
1168 quote_n (1, volume_label_option
)));
1175 fprintf (stdlis
, _("Reading %s\n"), quote (cursor
->header
.name
));
1178 else if (volume_label_option
)
1179 WARN ((0, 0, _("WARNING: No volume header")));
1184 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1185 || strcmp (cursor
->header
.name
, real_s_name
))
1187 WARN ((0, 0, _("%s is not continued on this volume"),
1188 quote (real_s_name
)));
1193 s1
= UINTMAX_FROM_HEADER (cursor
->header
.size
);
1194 s2
= UINTMAX_FROM_HEADER (cursor
->oldgnu_header
.offset
);
1195 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
1197 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1198 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1199 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1201 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1202 quote (cursor
->header
.name
),
1203 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1204 STRINGIFY_BIGINT (s1
, s1buf
),
1205 STRINGIFY_BIGINT (s2
, s2buf
)));
1210 if (real_s_totsize
- real_s_sizeleft
1211 != OFF_FROM_HEADER (cursor
->oldgnu_header
.offset
))
1213 WARN ((0, 0, _("This volume is out of sequence")));
1220 current_block
= cursor
;
1223 else if (status
< 0)
1225 archive_read_error ();
1226 goto error_loop
; /* try again */
1230 more
= record_start
->buffer
+ status
;
1231 left
= record_size
- status
;
1233 while (left
% BLOCKSIZE
!= 0)
1235 while ((status
= rmtread (archive
, more
, left
)) < 0)
1236 archive_read_error ();
1240 ERROR ((0, 0, _("%d garbage bytes ignored at end of archive"),
1241 (int) ((record_size
- left
) % BLOCKSIZE
)));
1245 if (! read_full_records_option
)
1246 FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
1247 (unsigned long) (record_size
- left
)));
1249 /* User warned us about this. Fix up. */
1255 /* FIXME: for size=0, multi-volume support. On the first record, warn
1256 about the problem. */
1258 if (!read_full_records_option
&& verbose_option
1259 && record_start_block
== 0 && status
> 0)
1260 WARN ((0, 0, _("Record size = %lu blocks"),
1261 (unsigned long) ((record_size
- left
) / BLOCKSIZE
)));
1263 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
1266 /*-----------------------------------------------.
1267 | Flush the current buffer to/from the archive. |
1268 `-----------------------------------------------*/
1271 flush_archive (void)
1273 record_start_block
+= record_end
- record_start
;
1274 current_block
= record_start
;
1275 record_end
= record_start
+ blocking_factor
;
1277 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1279 access_mode
= ACCESS_WRITE
;
1280 time_to_start_writing
= 0;
1282 if (file_to_switch_to
>= 0)
1284 int status
= rmtclose (archive
);
1289 WARN ((0, e
, _("WARNING: %s: close (%d, %d)"),
1290 quotearg_colon (*archive_name_cursor
), archive
, status
));
1293 archive
= file_to_switch_to
;
1296 backspace_output ();
1299 switch (access_mode
)
1314 /*-------------------------------------------------------------------------.
1315 | Backspace the archive descriptor by one record worth. If its a tape, |
1316 | MTIOCTOP will work. If its something else, we try to seek on it. If we |
1317 | can't seek, we lose! |
1318 `-------------------------------------------------------------------------*/
1321 backspace_output (void)
1325 struct mtop operation
;
1327 operation
.mt_op
= MTBSR
;
1328 operation
.mt_count
= 1;
1329 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1331 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1337 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1339 /* Seek back to the beginning of this record and start writing there. */
1341 position
-= record_size
;
1342 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1344 /* Lseek failed. Try a different method. */
1347 _("Cannot backspace archive file; it may be unreadable without -i")));
1349 /* Replace the first part of the record with NULs. */
1351 if (record_start
->buffer
!= output_start
)
1352 memset (record_start
->buffer
, 0,
1353 output_start
- record_start
->buffer
);
1358 /*-------------------------.
1359 | Close the archive file. |
1360 `-------------------------*/
1363 close_archive (void)
1365 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1370 /* Manage to fully drain a pipe we might be reading, so to not break it on
1371 the producer after the EOF block. FIXME: one of these days, GNU tar
1372 might become clever enough to just stop working, once there is no more
1373 work to do, we might have to revise this area in such time. */
1375 if (access_mode
== ACCESS_READ
1376 && ! _isrmt (archive
)
1377 && S_ISFIFO (archive_stat
.st_mode
))
1378 while (rmtread (archive
, record_start
->buffer
, record_size
) > 0)
1382 if (! _isrmt (archive
) && subcommand_option
== DELETE_SUBCOMMAND
)
1385 int status
= write (archive
, "", 0);
1387 off_t pos
= lseek (archive
, (off_t
) 0, SEEK_CUR
);
1388 int status
= pos
< 0 ? -1 : ftruncate (archive
, pos
);
1391 truncate_warn (*archive_name_cursor
);
1397 int status
= rmtclose (archive
);
1402 WARN ((0, e
, _("WARNING: %s: close (%d, %d)"),
1403 quotearg_colon (*archive_name_cursor
), archive
, status
));
1413 while (waitpid (child_pid
, &wait_status
, 0) == -1)
1416 waitpid_error (use_compress_program_option
);
1420 if (WIFSIGNALED (wait_status
))
1422 /* SIGPIPE is OK, everything else is a problem. */
1424 if (WTERMSIG (wait_status
) != SIGPIPE
)
1425 ERROR ((0, 0, _("Child died with signal %d"),
1426 WTERMSIG (wait_status
)));
1430 /* Child voluntarily terminated -- but why? /bin/sh returns
1431 SIGPIPE + 128 if its child, then do nothing. */
1433 if (WEXITSTATUS (wait_status
)
1434 && WEXITSTATUS (wait_status
) != (SIGPIPE
+ 128))
1435 ERROR ((0, 0, _("Child returned status %d"),
1436 WEXITSTATUS (wait_status
)));
1441 if (current_file_name
)
1442 free (current_file_name
);
1443 if (current_link_name
)
1444 free (current_link_name
);
1447 free (multi_volume_option
? record_start
- 2 : record_start
);
1450 /*------------------------------------------------.
1451 | Called to initialize the global volume number. |
1452 `------------------------------------------------*/
1455 init_volume_number (void)
1457 FILE *file
= fopen (volno_file_option
, "r");
1461 fscanf (file
, "%d", &global_volno
);
1463 read_error (volno_file_option
);
1464 if (fclose (file
) != 0)
1465 close_error (volno_file_option
);
1467 else if (errno
!= ENOENT
)
1468 open_error (volno_file_option
);
1471 /*-------------------------------------------------------.
1472 | Called to write out the closing global volume number. |
1473 `-------------------------------------------------------*/
1476 closeout_volume_number (void)
1478 FILE *file
= fopen (volno_file_option
, "w");
1482 fprintf (file
, "%d\n", global_volno
);
1484 write_error (volno_file_option
);
1485 if (fclose (file
) != 0)
1486 close_error (volno_file_option
);
1489 open_error (volno_file_option
);
1492 /*-----------------------------------------------------------------------.
1493 | We've hit the end of the old volume. Close it and open the next one. |
1494 | Return nonzero on success. |
1495 `-----------------------------------------------------------------------*/
1498 new_volume (enum access_mode access
)
1500 static FILE *read_file
;
1505 if (!read_file
&& !info_script_option
)
1506 /* FIXME: if fopen is used, it will never be closed. */
1507 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1514 if (status
= rmtclose (archive
), status
< 0)
1517 WARN ((0, e
, _("WARNING: %s: close (%d, %d)"),
1518 quotearg_colon (*archive_name_cursor
), archive
, status
));
1523 archive_name_cursor
++;
1524 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1526 archive_name_cursor
= archive_name_array
;
1533 /* We have to prompt from now on. */
1535 if (info_script_option
)
1537 if (volno_file_option
)
1538 closeout_volume_number ();
1539 system (info_script_option
);
1544 char input_buffer
[80];
1546 fputc ('\007', stderr
);
1548 _("Prepare volume #%d for %s and hit return: "),
1549 global_volno
, quote (*archive_name_cursor
));
1552 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
1554 WARN ((0, 0, _("EOF where user reply was expected")));
1556 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1557 && subcommand_option
!= LIST_SUBCOMMAND
1558 && subcommand_option
!= DIFF_SUBCOMMAND
)
1559 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1563 if (input_buffer
[0] == '\n'
1564 || input_buffer
[0] == 'y'
1565 || input_buffer
[0] == 'Y')
1568 switch (input_buffer
[0])
1572 fprintf (stderr
, _("\
1573 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1575 ! Spawn a subshell\n\
1576 ? Print this list\n"));
1583 WARN ((0, 0, _("No new volume; exiting.\n")));
1585 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1586 && subcommand_option
!= LIST_SUBCOMMAND
1587 && subcommand_option
!= DIFF_SUBCOMMAND
)
1588 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1593 /* Get new file name. */
1596 char *name
= &input_buffer
[1];
1599 while (*name
== ' ' || *name
== '\t')
1602 while (*cursor
&& *cursor
!= '\n')
1606 /* FIXME: the following allocation is never reclaimed. */
1607 *archive_name_cursor
= xstrdup (name
);
1613 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1614 #else /* not MSDOS */
1617 const char *shell
= getenv ("SHELL");
1623 execlp (shell
, "-sh", "-i", 0);
1629 while (waitpid (child
, &wait_status
, 0) == -1)
1632 waitpid_error (shell
);
1637 #endif /* not MSDOS */
1644 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1645 rsh_command_option
);
1650 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1651 rsh_command_option
);
1656 maybe_backup_file (*archive_name_cursor
, 1);
1657 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1658 rsh_command_option
);
1662 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1663 rsh_command_option
);
1669 open_warn (*archive_name_cursor
);
1670 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1671 undo_last_backup ();
1676 setmode (archive
, O_BINARY
);