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>
46 #define DEBUG_FORK 0 /* if nonzero, childs are born stopped */
48 #define PREAD 0 /* read file descriptor from pipe() */
49 #define PWRITE 1 /* write file descriptor from pipe() */
51 /* Number of retries before giving up on read. */
52 #define READ_ERROR_MAX 10
54 /* Globbing pattern to append to volume label if initial match failed. */
55 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
59 static tarlong prev_written
; /* bytes written on previous volumes */
60 static tarlong bytes_written
; /* bytes written on this volume */
62 /* FIXME: The following four variables should ideally be static to this
63 module. However, this cannot be done yet, as update.c uses the first
64 three a lot, and compare.c uses the fourth. The cleanup continues! */
66 union block
*record_start
; /* start of record of archive */
67 union block
*record_end
; /* last+1 block of archive record */
68 union block
*current_block
; /* current block of archive */
69 enum access_mode access_mode
; /* how do we handle the archive */
70 static struct stat archive_stat
; /* stat block for archive file */
72 static off_t record_start_block
; /* block ordinal at record_start */
74 /* Where we write list messages (not errors, not interactions) to. Stdout
75 unless we're writing a pipe, in which case stderr. */
78 static void backspace_output
PARAMS ((void));
79 static int new_volume
PARAMS ((enum access_mode
));
80 static void write_error
PARAMS ((ssize_t
));
81 static void read_error
PARAMS ((void));
84 /* Obnoxious test to see if dimwit is trying to dump the archive. */
89 /* PID of child program, if compress_option or remote archive access. */
90 static pid_t child_pid
;
92 /* Error recovery stuff */
93 static int read_error_count
;
95 /* Have we hit EOF yet? */
98 /* Checkpointing counter */
99 static int checkpoint
;
101 /* We're reading, but we just read the last block and its time to update. */
102 /* As least EXTERN like this one as possible. FIXME! */
103 extern int time_to_start_writing
;
105 int file_to_switch_to
= -1; /* if remote update, close archive, and use
106 this descriptor to write to */
108 static int volno
= 1; /* which volume of a multi-volume tape we're
110 static int global_volno
= 1; /* volume number to print in external
113 /* The pointer save_name, which is set in function dump_file() of module
114 create.c, points to the original long filename instead of the new,
115 shorter mangled name that is set in start_header() of module create.c.
116 The pointer save_name is only used in multi-volume mode when the file
117 being processed is non-sparse; if a file is split between volumes, the
118 save_name is used in generating the LF_MULTIVOL record on the second
119 volume. (From Pierce Cantrell, 1991-08-13.) */
121 char *save_name
; /* name of the file we are currently writing */
122 off_t save_totsize
; /* total size of file we are writing, only
123 valid if save_name is nonzero */
124 off_t save_sizeleft
; /* where we are in the file we are writing,
125 only valid if save_name is nonzero */
127 int write_archive_to_stdout
;
129 /* Used by flush_read and flush_write to store the real info about saved
131 static char *real_s_name
;
132 static off_t real_s_totsize
;
133 static off_t real_s_sizeleft
;
142 pid_t result
= fork ();
145 kill (getpid (), SIGSTOP
);
151 #endif /* DEBUG FORK */
154 print_total_written (void)
156 tarlong written
= prev_written
+ bytes_written
;
157 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
158 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
159 char rate
[LONGEST_HUMAN_READABLE
+ 1];
162 #if HAVE_CLOCK_GETTIME
164 if (clock_gettime (CLOCK_REALTIME
, &now
) == 0)
165 seconds
= ((now
.tv_sec
- start_timespec
.tv_sec
)
166 + (now
.tv_nsec
- start_timespec
.tv_nsec
) / 1e9
);
169 seconds
= time (0) - start_time
;
171 sprintf (bytes
, TARLONG_FORMAT
, written
);
173 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
174 fprintf (stderr
, _("Total bytes written: %s (%sB, %sB/s)\n"), bytes
,
175 human_readable ((uintmax_t) written
, abbr
, 1, -1024),
176 (0 < seconds
&& written
/ seconds
< (uintmax_t) -1
177 ? human_readable ((uintmax_t) (written
/ seconds
), rate
, 1, -1024)
181 /*--------------------------------------------------------.
182 | Compute and return the block ordinal at current_block. |
183 `--------------------------------------------------------*/
186 current_block_ordinal (void)
188 return record_start_block
+ (current_block
- record_start
);
191 /*------------------------------------------------------------------.
192 | If the EOF flag is set, reset it, as well as current_block, etc. |
193 `------------------------------------------------------------------*/
201 current_block
= record_start
;
202 record_end
= record_start
+ blocking_factor
;
203 access_mode
= ACCESS_WRITE
;
207 /*-------------------------------------------------------------------------.
208 | Return the location of the next available input or output block. |
209 | Return zero for EOF. Once we have returned zero, we just keep returning |
210 | it, to avoid accidentally going on to the next file on the tape. |
211 `-------------------------------------------------------------------------*/
214 find_next_block (void)
216 if (current_block
== record_end
)
221 if (current_block
== record_end
)
227 return current_block
;
230 /*------------------------------------------------------.
231 | Indicate that we have used all blocks up thru BLOCK. |
233 | FIXME: should the arg have an off-by-1? |
234 `------------------------------------------------------*/
237 set_next_block_after (union block
*block
)
239 while (block
>= current_block
)
242 /* Do *not* flush the archive here. If we do, the same argument to
243 set_next_block_after could mean the next block (if the input record
244 is exactly one block long), which is not what is intended. */
246 if (current_block
> record_end
)
250 /*------------------------------------------------------------------------.
251 | Return the number of bytes comprising the space between POINTER through |
252 | the end of the current buffer of blocks. This space is available for |
253 | filling with data, or taking data from. POINTER is usually (but not |
254 | always) the result previous find_next_block call. |
255 `------------------------------------------------------------------------*/
258 available_space_after (union block
*pointer
)
260 return record_end
->buffer
- pointer
->buffer
;
263 /*------------------------------------------------------------------.
264 | Close file having descriptor FD, and abort if close unsuccessful. |
265 `------------------------------------------------------------------*/
271 FATAL_ERROR ((0, errno
, _("Cannot close file #%d"), fd
));
274 /*-----------------------------------------------------------------------.
275 | Duplicate file descriptor FROM into becoming INTO, or else, issue |
276 | MESSAGE. INTO is closed first and has to be the next available slot. |
277 `-----------------------------------------------------------------------*/
280 xdup2 (int from
, int into
, const char *message
)
284 int status
= close (into
);
286 if (status
< 0 && errno
!= EBADF
)
287 FATAL_ERROR ((0, errno
, _("Cannot close descriptor %d"), into
));
290 FATAL_ERROR ((0, errno
, _("Cannot properly duplicate %s"), message
));
297 /*-------------------------------------------------------.
298 | Set ARCHIVE for writing, then compressing an archive. |
299 `-------------------------------------------------------*/
302 child_open_for_compress (void)
304 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
307 /*---------------------------------------------------------.
308 | Set ARCHIVE for uncompressing, then reading an archive. |
309 `---------------------------------------------------------*/
312 child_open_for_uncompress (void)
314 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
317 #else /* not MSDOS */
319 /*---------------------------------------------------------------------.
320 | Return nonzero if NAME is the name of a regular file, or if the file |
321 | does not exist (so it would be created as a regular file). |
322 `---------------------------------------------------------------------*/
325 is_regular_file (const char *name
)
329 if (stat (name
, &stbuf
) == 0)
330 return S_ISREG (stbuf
.st_mode
);
332 return errno
== ENOENT
;
336 write_archive_buffer (void)
341 while (0 <= (status
= rmtwrite (archive
, record_start
->buffer
+ written
,
342 record_size
- written
)))
345 if (written
== record_size
346 || _isrmt (archive
) || ! S_ISFIFO (archive_stat
.st_mode
))
350 return written
? written
: status
;
353 /*-------------------------------------------------------.
354 | Set ARCHIVE for writing, then compressing an archive. |
355 `-------------------------------------------------------*/
358 child_open_for_compress (void)
362 pid_t grandchild_pid
;
364 if (pipe (parent_pipe
) < 0)
365 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
369 FATAL_ERROR ((0, errno
, _("Cannot fork")));
373 /* The parent tar is still here! Just clean up. */
375 archive
= parent_pipe
[PWRITE
];
376 xclose (parent_pipe
[PREAD
]);
380 /* The new born child tar is here! */
382 program_name
= _("tar (child)");
384 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
, _("(child) Pipe to stdin"));
385 xclose (parent_pipe
[PWRITE
]);
387 /* Check if we need a grandchild tar. This happens only if either:
388 a) we are writing stdout: to force reblocking;
389 b) the file is to be accessed by rmt: compressor doesn't know how;
390 c) the file is not a plain file. */
392 if (strcmp (archive_name_array
[0], "-") != 0
393 && !_remdev (archive_name_array
[0])
394 && is_regular_file (archive_name_array
[0]))
397 maybe_backup_file (archive_name_array
[0], 1);
399 /* We don't need a grandchild tar. Open the archive and launch the
402 archive
= creat (archive_name_array
[0], MODE_RW
);
405 int saved_errno
= errno
;
409 FATAL_ERROR ((0, saved_errno
, _("Cannot open archive %s"),
410 archive_name_array
[0]));
412 xdup2 (archive
, STDOUT_FILENO
, _("Archive to stdout"));
413 execlp (use_compress_program_option
, use_compress_program_option
,
415 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
416 use_compress_program_option
));
419 /* We do need a grandchild tar. */
421 if (pipe (child_pipe
) < 0)
422 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
424 grandchild_pid
= fork ();
425 if (grandchild_pid
< 0)
426 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
428 if (grandchild_pid
> 0)
430 /* The child tar is still here! Launch the compressor. */
432 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
,
433 _("((child)) Pipe to stdout"));
434 xclose (child_pipe
[PREAD
]);
435 execlp (use_compress_program_option
, use_compress_program_option
,
437 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
438 use_compress_program_option
));
441 /* The new born grandchild tar is here! */
443 program_name
= _("tar (grandchild)");
445 /* Prepare for reblocking the data from the compressor into the archive. */
447 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("(grandchild) Pipe to stdin"));
448 xclose (child_pipe
[PWRITE
]);
450 if (strcmp (archive_name_array
[0], "-") == 0)
451 archive
= STDOUT_FILENO
;
453 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
455 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
456 archive_name_array
[0]));
458 /* Let's read out of the stdin pipe and write an archive. */
466 /* Assemble a record. */
468 for (length
= 0, cursor
= record_start
->buffer
;
469 length
< record_size
;
470 length
+= status
, cursor
+= status
)
472 size_t size
= record_size
- length
;
474 if (size
< BLOCKSIZE
)
476 status
= safe_read (STDIN_FILENO
, cursor
, size
);
482 FATAL_ERROR ((0, errno
, _("Cannot read from compression program")));
484 /* Copy the record. */
488 /* We hit the end of the file. Write last record at
489 full length, as the only role of the grandchild is
490 doing proper reblocking. */
494 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
495 status
= write_archive_buffer ();
496 if (status
!= record_size
)
497 write_error (status
);
500 /* There is nothing else to read, break out. */
504 status
= write_archive_buffer ();
505 if (status
!= record_size
)
506 write_error (status
);
515 /*---------------------------------------------------------.
516 | Set ARCHIVE for uncompressing, then reading an archive. |
517 `---------------------------------------------------------*/
520 child_open_for_uncompress (void)
524 pid_t grandchild_pid
;
526 if (pipe (parent_pipe
) < 0)
527 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
531 FATAL_ERROR ((0, errno
, _("Cannot fork")));
535 /* The parent tar is still here! Just clean up. */
537 read_full_records_option
= 1;
538 archive
= parent_pipe
[PREAD
];
539 xclose (parent_pipe
[PWRITE
]);
543 /* The new born child tar is here! */
545 program_name
= _("tar (child)");
547 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
, _("(child) Pipe to stdout"));
548 xclose (parent_pipe
[PREAD
]);
550 /* Check if we need a grandchild tar. This happens only if either:
551 a) we're reading stdin: to force unblocking;
552 b) the file is to be accessed by rmt: compressor doesn't know how;
553 c) the file is not a plain file. */
555 if (strcmp (archive_name_array
[0], "-") != 0
556 && !_remdev (archive_name_array
[0])
557 && is_regular_file (archive_name_array
[0]))
559 /* We don't need a grandchild tar. Open the archive and lauch the
562 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
564 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
565 archive_name_array
[0]));
566 xdup2 (archive
, STDIN_FILENO
, _("Archive to stdin"));
567 execlp (use_compress_program_option
, use_compress_program_option
,
569 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
570 use_compress_program_option
));
573 /* We do need a grandchild tar. */
575 if (pipe (child_pipe
) < 0)
576 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
578 grandchild_pid
= fork ();
579 if (grandchild_pid
< 0)
580 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
582 if (grandchild_pid
> 0)
584 /* The child tar is still here! Launch the uncompressor. */
586 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("((child)) Pipe to stdin"));
587 xclose (child_pipe
[PWRITE
]);
588 execlp (use_compress_program_option
, use_compress_program_option
,
590 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
591 use_compress_program_option
));
594 /* The new born grandchild tar is here! */
596 program_name
= _("tar (grandchild)");
598 /* Prepare for unblocking the data from the archive into the uncompressor. */
600 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
, _("(grandchild) Pipe to stdout"));
601 xclose (child_pipe
[PREAD
]);
603 if (strcmp (archive_name_array
[0], "-") == 0)
604 archive
= STDIN_FILENO
;
606 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
607 MODE_RW
, rsh_command_option
);
609 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
610 archive_name_array
[0]));
612 /* Let's read the archive and pipe it into stdout. */
621 read_error_count
= 0;
624 status
= rmtread (archive
, record_start
->buffer
, record_size
);
632 cursor
= record_start
->buffer
;
636 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
637 status
= full_write (STDOUT_FILENO
, cursor
, count
);
639 FATAL_ERROR ((0, errno
, _("Cannot write to compression program")));
643 ERROR ((0, 0, _("Write to compression program short %lu bytes"),
644 (unsigned long) (count
- status
)));
659 #endif /* not MSDOS */
661 /*--------------------------------------------------------------------------.
662 | Check the LABEL block against the volume label, seen as a globbing |
663 | pattern. Return true if the pattern matches. In case of failure, retry |
664 | matching a volume sequence number before giving up in multi-volume mode. |
665 `--------------------------------------------------------------------------*/
668 check_label_pattern (union block
*label
)
673 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
676 if (!multi_volume_option
)
679 string
= xmalloc (strlen (volume_label_option
)
680 + sizeof VOLUME_LABEL_APPEND
+ 1);
681 strcpy (string
, volume_label_option
);
682 strcat (string
, VOLUME_LABEL_APPEND
);
683 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
688 /*------------------------------------------------------------------------.
689 | Open an archive file. The argument specifies whether we are reading or |
690 | writing, or both. |
691 `------------------------------------------------------------------------*/
694 open_archive (enum access_mode access
)
696 int backed_up_flag
= 0;
698 stdlis
= to_stdout_option
? stderr
: stdout
;
700 if (record_size
== 0)
701 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
703 if (archive_names
== 0)
704 FATAL_ERROR ((0, 0, _("No archive name given")));
706 current_file_name
= 0;
707 current_link_name
= 0;
709 /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
710 constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
711 size that is reasonable to allocate a buffer. In the GNU system, there
712 is no fixed limit. The only correct thing to do is to use dynamic
713 allocation. (Roland McGrath) */
716 real_s_name
= xmalloc (PATH_MAX
);
717 /* FIXME: real_s_name is never freed. */
721 if (multi_volume_option
)
723 record_start
= valloc (record_size
+ (2 * BLOCKSIZE
));
728 record_start
= valloc (record_size
);
730 FATAL_ERROR ((0, 0, _("Could not allocate memory for blocking factor %d"),
733 current_block
= record_start
;
734 record_end
= record_start
+ blocking_factor
;
735 /* When updating the archive, we start with reading. */
736 access_mode
= access
== ACCESS_UPDATE
? ACCESS_READ
: access
;
738 if (multi_volume_option
&& verify_option
)
739 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
741 if (use_compress_program_option
)
743 if (multi_volume_option
)
744 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
746 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
751 child_open_for_uncompress ();
755 child_open_for_compress ();
759 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
763 if (access
== ACCESS_WRITE
&& strcmp (archive_name_array
[0], "-") == 0)
766 else if (strcmp (archive_name_array
[0], "-") == 0)
768 read_full_records_option
= 1; /* could be a pipe, be safe */
770 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
775 archive
= STDIN_FILENO
;
779 archive
= STDOUT_FILENO
;
784 archive
= STDIN_FILENO
;
786 write_archive_to_stdout
= 1;
790 else if (verify_option
)
791 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
792 MODE_RW
, rsh_command_option
);
797 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
798 MODE_RW
, rsh_command_option
);
804 maybe_backup_file (archive_name_array
[0], 1);
807 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
812 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
813 MODE_RW
, rsh_command_option
);
818 || (! _isrmt (archive
) && fstat (archive
, &archive_stat
) < 0))
820 int saved_errno
= errno
;
824 FATAL_ERROR ((0, saved_errno
, "%s", archive_name_array
[0]));
829 /* Detect if outputting to "/dev/null". */
831 static char const dev_null
[] = "/dev/null";
832 struct stat dev_null_stat
;
835 (strcmp (archive_name_array
[0], dev_null
) == 0
836 || (! _isrmt (archive
)
837 && S_ISCHR (archive_stat
.st_mode
)
838 && stat (dev_null
, &dev_null_stat
) == 0
839 && S_ISCHR (dev_null_stat
.st_mode
)
840 && archive_stat
.st_rdev
== dev_null_stat
.st_rdev
));
843 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
845 ar_dev
= archive_stat
.st_dev
;
846 ar_ino
= archive_stat
.st_ino
;
851 #endif /* not MSDOS */
854 setmode (archive
, O_BINARY
);
861 record_end
= record_start
; /* set up for 1st record = # 0 */
862 find_next_block (); /* read it in, check for EOF */
864 if (volume_label_option
)
866 union block
*label
= find_next_block ();
869 FATAL_ERROR ((0, 0, _("Archive not labeled to match `%s'"),
870 volume_label_option
));
871 if (!check_label_pattern (label
))
872 FATAL_ERROR ((0, 0, _("Volume `%s' does not match `%s'"),
873 label
->header
.name
, volume_label_option
));
878 if (volume_label_option
)
880 memset (record_start
, 0, BLOCKSIZE
);
881 if (multi_volume_option
)
882 sprintf (record_start
->header
.name
, "%s Volume 1",
883 volume_label_option
);
885 strcpy (record_start
->header
.name
, volume_label_option
);
887 assign_string (¤t_file_name
, record_start
->header
.name
);
889 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
890 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
891 finish_header (record_start
);
900 /*--------------------------------------.
901 | Perform a write to flush the buffer. |
902 `--------------------------------------*/
910 if (checkpoint_option
&& !(++checkpoint
% 10))
911 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
913 if (tape_length_option
&& tape_length_option
<= bytes_written
)
915 errno
= ENOSPC
; /* FIXME: errno should be read-only */
918 else if (dev_null_output
)
919 status
= record_size
;
921 status
= write_archive_buffer ();
922 if (status
!= record_size
&& !multi_volume_option
)
923 write_error (status
);
926 bytes_written
+= status
;
928 if (status
== record_size
)
930 if (multi_volume_option
)
936 real_s_name
[0] = '\0';
942 cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
943 while (*cursor
== '/')
946 strcpy (real_s_name
, cursor
);
947 real_s_totsize
= save_totsize
;
948 real_s_sizeleft
= save_sizeleft
;
953 /* We're multivol. Panic if we didn't get the right kind of response. */
955 /* ENXIO is for the UNIX PC. */
956 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
957 write_error (status
);
959 /* If error indicates a short write, we just move to the next tape. */
961 if (!new_volume (ACCESS_WRITE
))
965 prev_written
+= bytes_written
;
968 if (volume_label_option
&& real_s_name
[0])
973 else if (volume_label_option
|| real_s_name
[0])
981 if (volume_label_option
)
983 memset (record_start
, 0, BLOCKSIZE
);
984 sprintf (record_start
->header
.name
, "%s Volume %d", volume_label_option
, volno
);
985 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
986 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
987 finish_header (record_start
);
994 if (volume_label_option
)
997 memset (record_start
, 0, BLOCKSIZE
);
999 /* FIXME: Michael P Urban writes: [a long name file] is being written
1000 when a new volume rolls around [...] Looks like the wrong value is
1001 being preserved in real_s_name, though. */
1003 strcpy (record_start
->header
.name
, real_s_name
);
1004 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1005 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
1006 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1007 record_start
->oldgnu_header
.offset
);
1008 tmp
= verbose_option
;
1010 finish_header (record_start
);
1011 verbose_option
= tmp
;
1013 if (volume_label_option
)
1017 status
= write_archive_buffer ();
1018 if (status
!= record_size
)
1019 write_error (status
);
1021 bytes_written
+= status
;
1025 record_start
+= copy_back
;
1026 memcpy (current_block
,
1027 record_start
+ blocking_factor
- copy_back
,
1028 copy_back
* BLOCKSIZE
);
1029 current_block
+= copy_back
;
1031 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
1032 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
1033 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
1034 real_s_name
[0] = '\0';
1037 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
1039 while (*cursor
== '/')
1042 strcpy (real_s_name
, cursor
);
1043 real_s_sizeleft
= save_sizeleft
;
1044 real_s_totsize
= save_totsize
;
1050 /*---------------------------------------------------------------------.
1051 | Handle write errors on the archive. Write errors are always fatal. |
1052 | Hitting the end of a volume does not cause a write error unless the |
1053 | write was the first record of the volume. |
1054 `---------------------------------------------------------------------*/
1057 write_error (ssize_t status
)
1059 int saved_errno
= errno
;
1061 /* It might be useful to know how much was written before the error
1064 print_total_written ();
1067 FATAL_ERROR ((0, saved_errno
, _("Cannot write to %s"),
1068 *archive_name_cursor
));
1070 FATAL_ERROR ((0, 0, _("Only wrote %lu of %lu bytes to %s"),
1071 (unsigned long) status
, (unsigned long) record_size
,
1072 *archive_name_cursor
));
1075 /*-------------------------------------------------------------------.
1076 | Handle read errors on the archive. If the read should be retried, |
1077 | returns to the caller. |
1078 `-------------------------------------------------------------------*/
1083 WARN ((0, errno
, _("Read error on %s"), *archive_name_cursor
));
1085 if (record_start_block
== 0)
1086 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1088 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1089 then give up on reading the archive. */
1091 if (read_error_count
++ > READ_ERROR_MAX
)
1092 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1096 /*-------------------------------------.
1097 | Perform a read to flush the buffer. |
1098 `-------------------------------------*/
1103 ssize_t status
; /* result from system call */
1104 size_t left
; /* bytes left */
1105 char *more
; /* pointer to next byte to read */
1107 if (checkpoint_option
&& !(++checkpoint
% 10))
1108 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1110 /* Clear the count of errors. This only applies to a single call to
1113 read_error_count
= 0; /* clear error count */
1115 if (write_archive_to_stdout
&& record_start_block
!= 0)
1117 status
= write_archive_buffer ();
1118 if (status
!= record_size
)
1119 write_error (status
);
1121 if (multi_volume_option
)
1125 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
1127 while (*cursor
== '/')
1130 strcpy (real_s_name
, cursor
);
1131 real_s_sizeleft
= save_sizeleft
;
1132 real_s_totsize
= save_totsize
;
1136 real_s_name
[0] = '\0';
1138 real_s_sizeleft
= 0;
1143 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1144 if (status
== record_size
)
1148 || (status
< 0 && errno
== ENOSPC
)
1149 || (status
> 0 && !read_full_records_option
))
1150 && multi_volume_option
)
1152 union block
*cursor
;
1155 switch (subcommand_option
)
1157 case APPEND_SUBCOMMAND
:
1158 case CAT_SUBCOMMAND
:
1159 case UPDATE_SUBCOMMAND
:
1160 if (!new_volume (ACCESS_UPDATE
))
1165 if (!new_volume (ACCESS_READ
))
1171 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1177 if (status
!= record_size
)
1180 cursor
= record_start
;
1182 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1184 if (volume_label_option
)
1186 if (!check_label_pattern (cursor
))
1188 WARN ((0, 0, _("Volume `%s' does not match `%s'"),
1189 cursor
->header
.name
, volume_label_option
));
1196 fprintf (stdlis
, _("Reading %s\n"), cursor
->header
.name
);
1199 else if (volume_label_option
)
1200 WARN ((0, 0, _("WARNING: No volume header")));
1205 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1206 || strcmp (cursor
->header
.name
, real_s_name
))
1208 WARN ((0, 0, _("%s is not continued on this volume"),
1214 s1
= UINTMAX_FROM_HEADER (cursor
->header
.size
);
1215 s2
= UINTMAX_FROM_HEADER (cursor
->oldgnu_header
.offset
);
1216 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
1218 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1219 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1220 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1222 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1223 cursor
->header
.name
,
1224 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1225 STRINGIFY_BIGINT (s1
, s1buf
),
1226 STRINGIFY_BIGINT (s2
, s2buf
)));
1231 if (real_s_totsize
- real_s_sizeleft
1232 != OFF_FROM_HEADER (cursor
->oldgnu_header
.offset
))
1234 WARN ((0, 0, _("This volume is out of sequence")));
1241 current_block
= cursor
;
1244 else if (status
< 0)
1247 goto error_loop
; /* try again */
1251 more
= record_start
->buffer
+ status
;
1252 left
= record_size
- status
;
1254 while (left
% BLOCKSIZE
!= 0)
1256 while ((status
= rmtread (archive
, more
, left
)) < 0)
1261 ERROR ((0, 0, _("%d garbage bytes ignored at end of archive"),
1262 (int) ((record_size
- left
) % BLOCKSIZE
)));
1266 if (! read_full_records_option
)
1267 FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
1268 (unsigned long) (record_size
- left
)));
1270 /* User warned us about this. Fix up. */
1276 /* FIXME: for size=0, multi-volume support. On the first record, warn
1277 about the problem. */
1279 if (!read_full_records_option
&& verbose_option
1280 && record_start_block
== 0 && status
> 0)
1281 WARN ((0, 0, _("Record size = %lu blocks"),
1282 (unsigned long) ((record_size
- left
) / BLOCKSIZE
)));
1284 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
1287 /*-----------------------------------------------.
1288 | Flush the current buffer to/from the archive. |
1289 `-----------------------------------------------*/
1292 flush_archive (void)
1294 record_start_block
+= record_end
- record_start
;
1295 current_block
= record_start
;
1296 record_end
= record_start
+ blocking_factor
;
1298 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1300 access_mode
= ACCESS_WRITE
;
1301 time_to_start_writing
= 0;
1303 if (file_to_switch_to
>= 0)
1305 int status
= rmtclose (archive
);
1308 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1309 *archive_name_cursor
, archive
, status
));
1311 archive
= file_to_switch_to
;
1314 backspace_output ();
1317 switch (access_mode
)
1332 /*-------------------------------------------------------------------------.
1333 | Backspace the archive descriptor by one record worth. If its a tape, |
1334 | MTIOCTOP will work. If its something else, we try to seek on it. If we |
1335 | can't seek, we lose! |
1336 `-------------------------------------------------------------------------*/
1339 backspace_output (void)
1343 struct mtop operation
;
1345 operation
.mt_op
= MTBSR
;
1346 operation
.mt_count
= 1;
1347 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1349 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1355 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1357 /* Seek back to the beginning of this record and start writing there. */
1359 position
-= record_size
;
1360 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1362 /* Lseek failed. Try a different method. */
1365 _("Could not backspace archive file; it may be unreadable without -i")));
1367 /* Replace the first part of the record with NULs. */
1369 if (record_start
->buffer
!= output_start
)
1370 memset (record_start
->buffer
, 0,
1371 output_start
- record_start
->buffer
);
1376 /*-------------------------.
1377 | Close the archive file. |
1378 `-------------------------*/
1381 close_archive (void)
1383 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1388 /* Manage to fully drain a pipe we might be reading, so to not break it on
1389 the producer after the EOF block. FIXME: one of these days, GNU tar
1390 might become clever enough to just stop working, once there is no more
1391 work to do, we might have to revise this area in such time. */
1393 if (access_mode
== ACCESS_READ
1394 && ! _isrmt (archive
)
1395 && S_ISFIFO (archive_stat
.st_mode
))
1396 while (rmtread (archive
, record_start
->buffer
, record_size
) > 0)
1400 if (! _isrmt (archive
) && subcommand_option
== DELETE_SUBCOMMAND
)
1403 int status
= write (archive
, "", 0);
1405 off_t pos
= lseek (archive
, (off_t
) 0, SEEK_CUR
);
1406 int status
= pos
< 0 ? -1 : ftruncate (archive
, pos
);
1409 WARN ((0, errno
, _("WARNING: Cannot truncate %s"),
1410 *archive_name_cursor
));
1416 int status
= rmtclose (archive
);
1419 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1420 *archive_name_cursor
, archive
, status
));
1429 while (waitpid (child_pid
, &wait_status
, 0) == -1)
1432 ERROR ((0, errno
, _("While waiting for child")));
1436 if (WIFSIGNALED (wait_status
))
1438 /* SIGPIPE is OK, everything else is a problem. */
1440 if (WTERMSIG (wait_status
) != SIGPIPE
)
1441 ERROR ((0, 0, _("Child died with signal %d"),
1442 WTERMSIG (wait_status
)));
1446 /* Child voluntarily terminated -- but why? /bin/sh returns
1447 SIGPIPE + 128 if its child, then do nothing. */
1449 if (WEXITSTATUS (wait_status
)
1450 && WEXITSTATUS (wait_status
) != (SIGPIPE
+ 128))
1451 ERROR ((0, 0, _("Child returned status %d"),
1452 WEXITSTATUS (wait_status
)));
1457 if (current_file_name
)
1458 free (current_file_name
);
1459 if (current_link_name
)
1460 free (current_link_name
);
1463 free (multi_volume_option
? record_start
- 2 : record_start
);
1466 /*------------------------------------------------.
1467 | Called to initialize the global volume number. |
1468 `------------------------------------------------*/
1471 init_volume_number (void)
1473 FILE *file
= fopen (volno_file_option
, "r");
1477 fscanf (file
, "%d", &global_volno
);
1478 if (fclose (file
) == EOF
)
1479 ERROR ((0, errno
, "%s", volno_file_option
));
1481 else if (errno
!= ENOENT
)
1482 ERROR ((0, errno
, "%s", volno_file_option
));
1485 /*-------------------------------------------------------.
1486 | Called to write out the closing global volume number. |
1487 `-------------------------------------------------------*/
1490 closeout_volume_number (void)
1492 FILE *file
= fopen (volno_file_option
, "w");
1496 fprintf (file
, "%d\n", global_volno
);
1497 if (fclose (file
) == EOF
)
1498 ERROR ((0, errno
, "%s", volno_file_option
));
1501 ERROR ((0, errno
, "%s", volno_file_option
));
1504 /*-----------------------------------------------------------------------.
1505 | We've hit the end of the old volume. Close it and open the next one. |
1506 | Return nonzero on success. |
1507 `-----------------------------------------------------------------------*/
1510 new_volume (enum access_mode access
)
1512 static FILE *read_file
;
1517 if (!read_file
&& !info_script_option
)
1518 /* FIXME: if fopen is used, it will never be closed. */
1519 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1526 if (status
= rmtclose (archive
), status
< 0)
1527 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1528 *archive_name_cursor
, archive
, status
));
1532 archive_name_cursor
++;
1533 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1535 archive_name_cursor
= archive_name_array
;
1542 /* We have to prompt from now on. */
1544 if (info_script_option
)
1546 if (volno_file_option
)
1547 closeout_volume_number ();
1548 system (info_script_option
);
1553 char input_buffer
[80];
1555 fputc ('\007', stderr
);
1557 _("Prepare volume #%d for %s and hit return: "),
1558 global_volno
, *archive_name_cursor
);
1561 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
1563 WARN ((0, 0, _("EOF where user reply was expected")));
1565 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1566 && subcommand_option
!= LIST_SUBCOMMAND
1567 && subcommand_option
!= DIFF_SUBCOMMAND
)
1568 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1570 apply_delayed_set_stat ();
1571 exit (TAREXIT_FAILURE
);
1573 if (input_buffer
[0] == '\n'
1574 || input_buffer
[0] == 'y'
1575 || input_buffer
[0] == 'Y')
1578 switch (input_buffer
[0])
1582 fprintf (stderr
, _("\
1583 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1585 ! Spawn a subshell\n\
1586 ? Print this list\n"));
1593 WARN ((0, 0, _("No new volume; exiting.\n")));
1595 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1596 && subcommand_option
!= LIST_SUBCOMMAND
1597 && subcommand_option
!= DIFF_SUBCOMMAND
)
1598 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1600 apply_delayed_set_stat ();
1601 exit (TAREXIT_FAILURE
);
1604 /* Get new file name. */
1607 char *name
= &input_buffer
[1];
1610 while (*name
== ' ' || *name
== '\t')
1613 while (*cursor
&& *cursor
!= '\n')
1617 /* FIXME: the following allocation is never reclaimed. */
1618 *archive_name_cursor
= xstrdup (name
);
1624 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1625 #else /* not MSDOS */
1627 pid_t child
= fork ();
1631 WARN ((0, errno
, _("Cannot fork!")));
1636 const char *shell
= getenv ("SHELL");
1640 execlp (shell
, "-sh", "-i", 0);
1641 FATAL_ERROR ((0, errno
, _("Cannot exec a shell %s"),
1648 while (waitpid (child
, &wait_status
, 0) == -1)
1652 _("While waiting for child")));
1659 #endif /* not MSDOS */
1666 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1667 rsh_command_option
);
1672 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1673 rsh_command_option
);
1678 maybe_backup_file (*archive_name_cursor
, 1);
1679 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1680 rsh_command_option
);
1684 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1685 rsh_command_option
);
1691 WARN ((0, errno
, _("Cannot open %s"), *archive_name_cursor
));
1692 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1693 undo_last_backup ();
1698 setmode (archive
, O_BINARY
);