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>
45 #define DEBUG_FORK 0 /* if nonzero, childs are born stopped */
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 write_error
PARAMS ((ssize_t
));
80 static void 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 non NULL */
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
= 0;
128 /* Used by flush_read and flush_write to store the real info about saved
130 static char *real_s_name
= NULL
;
131 static off_t real_s_totsize
;
132 static off_t real_s_sizeleft
;
141 pid_t result
= fork ();
144 kill (getpid (), SIGSTOP
);
150 #endif /* DEBUG FORK */
153 print_total_written (void)
155 fprintf (stderr
, _("Total bytes written: "));
156 fprintf (stderr
, TARLONG_FORMAT
, prev_written
+ bytes_written
);
157 fprintf (stderr
, "\n");
160 /*--------------------------------------------------------.
161 | Compute and return the block ordinal at current_block. |
162 `--------------------------------------------------------*/
165 current_block_ordinal (void)
167 return record_start_block
+ (current_block
- record_start
);
170 /*------------------------------------------------------------------.
171 | If the EOF flag is set, reset it, as well as current_block, etc. |
172 `------------------------------------------------------------------*/
180 current_block
= record_start
;
181 record_end
= record_start
+ blocking_factor
;
182 access_mode
= ACCESS_WRITE
;
186 /*-------------------------------------------------------------------------.
187 | Return the location of the next available input or output block. |
188 | Return NULL for EOF. Once we have returned NULL, we just keep returning |
189 | it, to avoid accidentally going on to the next file on the tape. |
190 `-------------------------------------------------------------------------*/
193 find_next_block (void)
195 if (current_block
== record_end
)
200 if (current_block
== record_end
)
206 return current_block
;
209 /*------------------------------------------------------.
210 | Indicate that we have used all blocks up thru BLOCK. |
212 | FIXME: should the arg have an off-by-1? |
213 `------------------------------------------------------*/
216 set_next_block_after (union block
*block
)
218 while (block
>= current_block
)
221 /* Do *not* flush the archive here. If we do, the same argument to
222 set_next_block_after could mean the next block (if the input record
223 is exactly one block long), which is not what is intended. */
225 if (current_block
> record_end
)
229 /*------------------------------------------------------------------------.
230 | Return the number of bytes comprising the space between POINTER through |
231 | the end of the current buffer of blocks. This space is available for |
232 | filling with data, or taking data from. POINTER is usually (but not |
233 | always) the result previous find_next_block call. |
234 `------------------------------------------------------------------------*/
237 available_space_after (union block
*pointer
)
239 return record_end
->buffer
- pointer
->buffer
;
242 /*------------------------------------------------------------------.
243 | Close file having descriptor FD, and abort if close unsucessful. |
244 `------------------------------------------------------------------*/
250 FATAL_ERROR ((0, errno
, _("Cannot close file #%d"), fd
));
253 /*-----------------------------------------------------------------------.
254 | Duplicate file descriptor FROM into becoming INTO, or else, issue |
255 | MESSAGE. INTO is closed first and has to be the next available slot. |
256 `-----------------------------------------------------------------------*/
259 xdup2 (int from
, int into
, const char *message
)
263 int status
= close (into
);
265 if (status
< 0 && errno
!= EBADF
)
266 FATAL_ERROR ((0, errno
, _("Cannot close descriptor %d"), into
));
269 FATAL_ERROR ((0, errno
, _("Cannot properly duplicate %s"), message
));
276 /*-------------------------------------------------------.
277 | Set ARCHIVE for writing, then compressing an archive. |
278 `-------------------------------------------------------*/
281 child_open_for_compress (void)
283 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
286 /*---------------------------------------------------------.
287 | Set ARCHIVE for uncompressing, then reading an archive. |
288 `---------------------------------------------------------*/
291 child_open_for_uncompress (void)
293 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
296 #else /* not MSDOS */
298 /*---------------------------------------------------------------------.
299 | Return nonzero if NAME is the name of a regular file, or if the file |
300 | does not exist (so it would be created as a regular file). |
301 `---------------------------------------------------------------------*/
304 is_regular_file (const char *name
)
308 if (stat (name
, &stbuf
) == 0)
309 return S_ISREG (stbuf
.st_mode
);
311 return errno
== ENOENT
;
315 write_archive_buffer (void)
320 while (0 <= (status
= rmtwrite (archive
, record_start
->buffer
+ written
,
321 record_size
- written
)))
324 if (written
== record_size
325 || _isrmt (archive
) || ! S_ISFIFO (archive_stat
.st_mode
))
329 return written
? written
: status
;
332 /*-------------------------------------------------------.
333 | Set ARCHIVE for writing, then compressing an archive. |
334 `-------------------------------------------------------*/
337 child_open_for_compress (void)
341 pid_t grandchild_pid
;
343 if (pipe (parent_pipe
) < 0)
344 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
348 FATAL_ERROR ((0, errno
, _("Cannot fork")));
352 /* The parent tar is still here! Just clean up. */
354 archive
= parent_pipe
[PWRITE
];
355 xclose (parent_pipe
[PREAD
]);
359 /* The new born child tar is here! */
361 program_name
= _("tar (child)");
363 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
, _("(child) Pipe to stdin"));
364 xclose (parent_pipe
[PWRITE
]);
366 /* Check if we need a grandchild tar. This happens only if either:
367 a) we are writing stdout: to force reblocking;
368 b) the file is to be accessed by rmt: compressor doesn't know how;
369 c) the file is not a plain file. */
371 if (strcmp (archive_name_array
[0], "-") != 0
372 && !_remdev (archive_name_array
[0])
373 && is_regular_file (archive_name_array
[0]))
376 maybe_backup_file (archive_name_array
[0], 1);
378 /* We don't need a grandchild tar. Open the archive and launch the
381 archive
= creat (archive_name_array
[0], MODE_RW
);
384 int saved_errno
= errno
;
388 FATAL_ERROR ((0, saved_errno
, _("Cannot open archive %s"),
389 archive_name_array
[0]));
391 xdup2 (archive
, STDOUT_FILENO
, _("Archive to stdout"));
392 execlp (use_compress_program_option
, use_compress_program_option
,
394 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
395 use_compress_program_option
));
398 /* We do need a grandchild tar. */
400 if (pipe (child_pipe
) < 0)
401 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
403 grandchild_pid
= fork ();
404 if (grandchild_pid
< 0)
405 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
407 if (grandchild_pid
> 0)
409 /* The child tar is still here! Launch the compressor. */
411 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
,
412 _("((child)) Pipe to stdout"));
413 xclose (child_pipe
[PREAD
]);
414 execlp (use_compress_program_option
, use_compress_program_option
,
416 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
417 use_compress_program_option
));
420 /* The new born grandchild tar is here! */
422 program_name
= _("tar (grandchild)");
424 /* Prepare for reblocking the data from the compressor into the archive. */
426 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("(grandchild) Pipe to stdin"));
427 xclose (child_pipe
[PWRITE
]);
429 if (strcmp (archive_name_array
[0], "-") == 0)
430 archive
= STDOUT_FILENO
;
432 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
434 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
435 archive_name_array
[0]));
437 /* Let's read out of the stdin pipe and write an archive. */
445 /* Assemble a record. */
447 for (length
= 0, cursor
= record_start
->buffer
;
448 length
< record_size
;
449 length
+= status
, cursor
+= status
)
451 size_t size
= record_size
- length
;
453 if (size
< BLOCKSIZE
)
455 status
= safe_read (STDIN_FILENO
, cursor
, size
);
461 FATAL_ERROR ((0, errno
, _("Cannot read from compression program")));
463 /* Copy the record. */
467 /* We hit the end of the file. Write last record at
468 full length, as the only role of the grandchild is
469 doing proper reblocking. */
473 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
474 status
= write_archive_buffer ();
475 if (status
!= record_size
)
476 write_error (status
);
479 /* There is nothing else to read, break out. */
483 status
= write_archive_buffer ();
484 if (status
!= record_size
)
485 write_error (status
);
494 /*---------------------------------------------------------.
495 | Set ARCHIVE for uncompressing, then reading an archive. |
496 `---------------------------------------------------------*/
499 child_open_for_uncompress (void)
503 pid_t grandchild_pid
;
505 if (pipe (parent_pipe
) < 0)
506 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
510 FATAL_ERROR ((0, errno
, _("Cannot fork")));
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 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
544 archive_name_array
[0]));
545 xdup2 (archive
, STDIN_FILENO
, _("Archive to stdin"));
546 execlp (use_compress_program_option
, use_compress_program_option
,
548 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
549 use_compress_program_option
));
552 /* We do need a grandchild tar. */
554 if (pipe (child_pipe
) < 0)
555 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
557 grandchild_pid
= fork ();
558 if (grandchild_pid
< 0)
559 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
561 if (grandchild_pid
> 0)
563 /* The child tar is still here! Launch the uncompressor. */
565 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("((child)) Pipe to stdin"));
566 xclose (child_pipe
[PWRITE
]);
567 execlp (use_compress_program_option
, use_compress_program_option
,
569 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
570 use_compress_program_option
));
573 /* The new born grandchild tar is here! */
575 program_name
= _("tar (grandchild)");
577 /* Prepare for unblocking the data from the archive into the uncompressor. */
579 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
, _("(grandchild) Pipe to stdout"));
580 xclose (child_pipe
[PREAD
]);
582 if (strcmp (archive_name_array
[0], "-") == 0)
583 archive
= STDIN_FILENO
;
585 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
586 MODE_RW
, rsh_command_option
);
588 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
589 archive_name_array
[0]));
591 /* Let's read the archive and pipe it into stdout. */
600 read_error_count
= 0;
603 status
= rmtread (archive
, record_start
->buffer
, record_size
);
611 cursor
= record_start
->buffer
;
615 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
616 status
= full_write (STDOUT_FILENO
, cursor
, count
);
618 FATAL_ERROR ((0, errno
, _("Cannot write to compression program")));
622 ERROR ((0, 0, _("Write to compression program short %lu bytes"),
623 (unsigned long) (count
- status
)));
638 #endif /* not MSDOS */
640 /*--------------------------------------------------------------------------.
641 | Check the LABEL block against the volume label, seen as a globbing |
642 | pattern. Return true if the pattern matches. In case of failure, retry |
643 | matching a volume sequence number before giving up in multi-volume mode. |
644 `--------------------------------------------------------------------------*/
647 check_label_pattern (union block
*label
)
652 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
655 if (!multi_volume_option
)
658 string
= xmalloc (strlen (volume_label_option
)
659 + sizeof VOLUME_LABEL_APPEND
+ 1);
660 strcpy (string
, volume_label_option
);
661 strcat (string
, VOLUME_LABEL_APPEND
);
662 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
667 /*------------------------------------------------------------------------.
668 | Open an archive file. The argument specifies whether we are reading or |
669 | writing, or both. |
670 `------------------------------------------------------------------------*/
673 open_archive (enum access_mode access
)
675 int backed_up_flag
= 0;
677 stdlis
= to_stdout_option
? stderr
: stdout
;
679 if (record_size
== 0)
680 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
682 if (archive_names
== 0)
683 FATAL_ERROR ((0, 0, _("No archive name given")));
685 current_file_name
= NULL
;
686 current_link_name
= NULL
;
688 /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
689 constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
690 size that is reasonable to allocate a buffer. In the GNU system, there
691 is no fixed limit. The only correct thing to do is to use dynamic
692 allocation. (Roland McGrath) */
695 real_s_name
= (char *) xmalloc (PATH_MAX
);
696 /* FIXME: real_s_name is never freed. */
700 if (multi_volume_option
)
703 = (union block
*) valloc (record_size
+ (2 * BLOCKSIZE
));
708 record_start
= (union block
*) valloc (record_size
);
710 FATAL_ERROR ((0, 0, _("Could not allocate memory for blocking factor %d"),
713 current_block
= record_start
;
714 record_end
= record_start
+ blocking_factor
;
715 /* When updating the archive, we start with reading. */
716 access_mode
= access
== ACCESS_UPDATE
? ACCESS_READ
: access
;
718 if (multi_volume_option
&& verify_option
)
719 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
721 if (use_compress_program_option
)
723 if (multi_volume_option
)
724 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
726 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
731 child_open_for_uncompress ();
735 child_open_for_compress ();
739 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
743 if (access
== ACCESS_WRITE
&& strcmp (archive_name_array
[0], "-") == 0)
746 else if (strcmp (archive_name_array
[0], "-") == 0)
748 read_full_records_option
= 1; /* could be a pipe, be safe */
750 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
755 archive
= STDIN_FILENO
;
759 archive
= STDOUT_FILENO
;
764 archive
= STDIN_FILENO
;
766 write_archive_to_stdout
= 1;
770 else if (verify_option
)
771 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
772 MODE_RW
, rsh_command_option
);
777 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
778 MODE_RW
, rsh_command_option
);
784 maybe_backup_file (archive_name_array
[0], 1);
787 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
792 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
793 MODE_RW
, rsh_command_option
);
798 || (! _isrmt (archive
) && fstat (archive
, &archive_stat
) < 0))
800 int saved_errno
= errno
;
804 FATAL_ERROR ((0, saved_errno
, "%s", archive_name_array
[0]));
809 /* Detect if outputting to "/dev/null". */
811 static char const dev_null
[] = "/dev/null";
812 struct stat dev_null_stat
;
815 (strcmp (archive_name_array
[0], dev_null
) == 0
816 || (! _isrmt (archive
)
817 && S_ISCHR (archive_stat
.st_mode
)
818 && stat (dev_null
, &dev_null_stat
) == 0
819 && S_ISCHR (dev_null_stat
.st_mode
)
820 && archive_stat
.st_rdev
== dev_null_stat
.st_rdev
));
823 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
825 ar_dev
= archive_stat
.st_dev
;
826 ar_ino
= archive_stat
.st_ino
;
831 #endif /* not MSDOS */
834 setmode (archive
, O_BINARY
);
841 record_end
= record_start
; /* set up for 1st record = # 0 */
842 find_next_block (); /* read it in, check for EOF */
844 if (volume_label_option
)
846 union block
*label
= find_next_block ();
849 FATAL_ERROR ((0, 0, _("Archive not labelled to match `%s'"),
850 volume_label_option
));
851 if (!check_label_pattern (label
))
852 FATAL_ERROR ((0, 0, _("Volume `%s' does not match `%s'"),
853 label
->header
.name
, volume_label_option
));
858 if (volume_label_option
)
860 memset ((void *) record_start
, 0, BLOCKSIZE
);
861 if (multi_volume_option
)
862 sprintf (record_start
->header
.name
, "%s Volume 1",
863 volume_label_option
);
865 strcpy (record_start
->header
.name
, volume_label_option
);
867 assign_string (¤t_file_name
, record_start
->header
.name
);
869 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
870 TIME_TO_CHARS (time (0), record_start
->header
.mtime
);
871 finish_header (record_start
);
880 /*--------------------------------------.
881 | Perform a write to flush the buffer. |
882 `--------------------------------------*/
890 if (checkpoint_option
&& !(++checkpoint
% 10))
891 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
893 if (tape_length_option
&& tape_length_option
<= bytes_written
)
895 errno
= ENOSPC
; /* FIXME: errno should be read-only */
898 else if (dev_null_output
)
899 status
= record_size
;
901 status
= write_archive_buffer ();
902 if (status
!= record_size
&& !multi_volume_option
)
903 write_error (status
);
906 bytes_written
+= status
;
908 if (status
== record_size
)
910 if (multi_volume_option
)
916 real_s_name
[0] = '\0';
922 cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
923 while (*cursor
== '/')
926 strcpy (real_s_name
, cursor
);
927 real_s_totsize
= save_totsize
;
928 real_s_sizeleft
= save_sizeleft
;
933 /* We're multivol. Panic if we didn't get the right kind of response. */
935 /* ENXIO is for the UNIX PC. */
936 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
937 write_error (status
);
939 /* If error indicates a short write, we just move to the next tape. */
941 if (!new_volume (ACCESS_WRITE
))
945 prev_written
+= bytes_written
;
948 if (volume_label_option
&& real_s_name
[0])
953 else if (volume_label_option
|| real_s_name
[0])
961 if (volume_label_option
)
963 memset ((void *) record_start
, 0, BLOCKSIZE
);
964 sprintf (record_start
->header
.name
, "%s Volume %d", volume_label_option
, volno
);
965 TIME_TO_CHARS (time (0), record_start
->header
.mtime
);
966 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
967 finish_header (record_start
);
974 if (volume_label_option
)
977 memset ((void *) record_start
, 0, BLOCKSIZE
);
979 /* FIXME: Michael P Urban writes: [a long name file] is being written
980 when a new volume rolls around [...] Looks like the wrong value is
981 being preserved in real_s_name, though. */
983 strcpy (record_start
->header
.name
, real_s_name
);
984 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
985 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
986 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
987 record_start
->oldgnu_header
.offset
);
988 tmp
= verbose_option
;
990 finish_header (record_start
);
991 verbose_option
= tmp
;
993 if (volume_label_option
)
997 status
= write_archive_buffer ();
998 if (status
!= record_size
)
999 write_error (status
);
1001 bytes_written
+= status
;
1005 record_start
+= copy_back
;
1006 memcpy ((void *) current_block
,
1007 (void *) (record_start
+ blocking_factor
- copy_back
),
1008 (size_t) (copy_back
* BLOCKSIZE
));
1009 current_block
+= copy_back
;
1011 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
1012 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
1013 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
1014 real_s_name
[0] = '\0';
1017 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
1019 while (*cursor
== '/')
1022 strcpy (real_s_name
, cursor
);
1023 real_s_sizeleft
= save_sizeleft
;
1024 real_s_totsize
= save_totsize
;
1030 /*---------------------------------------------------------------------.
1031 | Handle write errors on the archive. Write errors are always fatal. |
1032 | Hitting the end of a volume does not cause a write error unless the |
1033 | write was the first record of the volume. |
1034 `---------------------------------------------------------------------*/
1037 write_error (ssize_t status
)
1039 int saved_errno
= errno
;
1041 /* It might be useful to know how much was written before the error
1042 occured. Beware that mere printing maybe change errno value. */
1044 print_total_written ();
1047 FATAL_ERROR ((0, saved_errno
, _("Cannot write to %s"),
1048 *archive_name_cursor
));
1050 FATAL_ERROR ((0, 0, _("Only wrote %lu of %lu bytes to %s"),
1051 (unsigned long) status
, (unsigned long) record_size
,
1052 *archive_name_cursor
));
1055 /*-------------------------------------------------------------------.
1056 | Handle read errors on the archive. If the read should be retried, |
1057 | returns to the caller. |
1058 `-------------------------------------------------------------------*/
1063 WARN ((0, errno
, _("Read error on %s"), *archive_name_cursor
));
1065 if (record_start_block
== 0)
1066 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1068 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1069 then give up on reading the archive. */
1071 if (read_error_count
++ > READ_ERROR_MAX
)
1072 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1076 /*-------------------------------------.
1077 | Perform a read to flush the buffer. |
1078 `-------------------------------------*/
1083 ssize_t status
; /* result from system call */
1084 size_t left
; /* bytes left */
1085 char *more
; /* pointer to next byte to read */
1087 if (checkpoint_option
&& !(++checkpoint
% 10))
1088 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1090 /* Clear the count of errors. This only applies to a single call to
1093 read_error_count
= 0; /* clear error count */
1095 if (write_archive_to_stdout
&& record_start_block
!= 0)
1097 status
= write_archive_buffer ();
1098 if (status
!= record_size
)
1099 write_error (status
);
1101 if (multi_volume_option
)
1105 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
1107 while (*cursor
== '/')
1110 strcpy (real_s_name
, cursor
);
1111 real_s_sizeleft
= save_sizeleft
;
1112 real_s_totsize
= save_totsize
;
1116 real_s_name
[0] = '\0';
1118 real_s_sizeleft
= 0;
1123 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1124 if (status
== record_size
)
1128 || (status
< 0 && errno
== ENOSPC
)
1129 || (status
> 0 && !read_full_records_option
))
1130 && multi_volume_option
)
1132 union block
*cursor
;
1135 switch (subcommand_option
)
1137 case APPEND_SUBCOMMAND
:
1138 case CAT_SUBCOMMAND
:
1139 case UPDATE_SUBCOMMAND
:
1140 if (!new_volume (ACCESS_UPDATE
))
1145 if (!new_volume (ACCESS_READ
))
1151 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1157 if (status
!= record_size
)
1160 cursor
= record_start
;
1162 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1164 if (volume_label_option
)
1166 if (!check_label_pattern (cursor
))
1168 WARN ((0, 0, _("Volume `%s' does not match `%s'"),
1169 cursor
->header
.name
, volume_label_option
));
1176 fprintf (stdlis
, _("Reading %s\n"), cursor
->header
.name
);
1179 else if (volume_label_option
)
1180 WARN ((0, 0, _("WARNING: No volume header")));
1185 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1186 || strcmp (cursor
->header
.name
, real_s_name
))
1188 WARN ((0, 0, _("%s is not continued on this volume"),
1194 s1
= UINTMAX_FROM_CHARS (cursor
->header
.size
);
1195 s2
= UINTMAX_FROM_CHARS (cursor
->oldgnu_header
.offset
);
1196 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
1198 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1199 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1200 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1202 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1203 cursor
->header
.name
,
1204 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1205 STRINGIFY_BIGINT (s1
, s1buf
),
1206 STRINGIFY_BIGINT (s2
, s2buf
)));
1211 if (real_s_totsize
- real_s_sizeleft
1212 != OFF_FROM_CHARS (cursor
->oldgnu_header
.offset
))
1214 WARN ((0, 0, _("This volume is out of sequence")));
1221 current_block
= cursor
;
1224 else if (status
< 0)
1227 goto error_loop
; /* try again */
1231 more
= record_start
->buffer
+ status
;
1232 left
= record_size
- status
;
1235 if (left
% BLOCKSIZE
== 0)
1237 /* FIXME: for size=0, multi-volume support. On the first record, warn
1238 about the problem. */
1240 if (!read_full_records_option
&& verbose_option
1241 && record_start_block
== 0 && status
> 0)
1242 WARN ((0, 0, _("Record size = %lu blocks"),
1243 (unsigned long) (status
/ BLOCKSIZE
)));
1245 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
1249 if (read_full_records_option
)
1251 /* User warned us about this. Fix up. */
1256 status
= rmtread (archive
, more
, left
);
1260 goto error2loop
; /* try again */
1263 FATAL_ERROR ((0, 0, _("Archive %s EOF not on block boundary"),
1264 *archive_name_cursor
));
1271 FATAL_ERROR ((0, 0, _("Only read %lu bytes from archive %s"),
1272 (unsigned long) status
, *archive_name_cursor
));
1275 /*-----------------------------------------------.
1276 | Flush the current buffer to/from the archive. |
1277 `-----------------------------------------------*/
1280 flush_archive (void)
1282 record_start_block
+= record_end
- record_start
;
1283 current_block
= record_start
;
1284 record_end
= record_start
+ blocking_factor
;
1286 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1288 access_mode
= ACCESS_WRITE
;
1289 time_to_start_writing
= 0;
1291 if (file_to_switch_to
>= 0)
1293 int status
= rmtclose (archive
);
1296 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1297 *archive_name_cursor
, archive
, status
));
1299 archive
= file_to_switch_to
;
1302 backspace_output ();
1305 switch (access_mode
)
1320 /*-------------------------------------------------------------------------.
1321 | Backspace the archive descriptor by one record worth. If its a tape, |
1322 | MTIOCTOP will work. If its something else, we try to seek on it. If we |
1323 | can't seek, we lose! |
1324 `-------------------------------------------------------------------------*/
1327 backspace_output (void)
1331 struct mtop operation
;
1333 operation
.mt_op
= MTBSR
;
1334 operation
.mt_count
= 1;
1335 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1337 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1343 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1345 /* Seek back to the beginning of this record and start writing there. */
1347 position
-= record_size
;
1348 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1350 /* Lseek failed. Try a different method. */
1353 Could not backspace archive file; it may be unreadable without -i")));
1355 /* Replace the first part of the record with NULs. */
1357 if (record_start
->buffer
!= output_start
)
1358 memset (record_start
->buffer
, 0,
1359 (size_t) (output_start
- record_start
->buffer
));
1364 /*-------------------------.
1365 | Close the archive file. |
1366 `-------------------------*/
1369 close_archive (void)
1371 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1376 /* Manage to fully drain a pipe we might be reading, so to not break it on
1377 the producer after the EOF block. FIXME: one of these days, GNU tar
1378 might become clever enough to just stop working, once there is no more
1379 work to do, we might have to revise this area in such time. */
1381 if (access_mode
== ACCESS_READ
1382 && ! _isrmt (archive
)
1383 && S_ISFIFO (archive_stat
.st_mode
))
1384 while (rmtread (archive
, record_start
->buffer
, record_size
) > 0)
1388 if (! _isrmt (archive
) && subcommand_option
== DELETE_SUBCOMMAND
)
1391 int status
= write (archive
, "", 0);
1393 off_t pos
= lseek (archive
, (off_t
) 0, SEEK_CUR
);
1394 int status
= pos
< 0 ? -1 : ftruncate (archive
, pos
);
1397 WARN ((0, errno
, _("WARNING: Cannot truncate %s"),
1398 *archive_name_cursor
));
1404 int status
= rmtclose (archive
);
1407 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1408 *archive_name_cursor
, archive
, status
));
1417 while (waitpid (child_pid
, &wait_status
, 0) == -1)
1420 ERROR ((0, errno
, _("While waiting for child")));
1424 if (WIFSIGNALED (wait_status
))
1426 /* SIGPIPE is OK, everything else is a problem. */
1428 if (WTERMSIG (wait_status
) != SIGPIPE
)
1429 ERROR ((0, 0, _("Child died with signal %d"),
1430 WTERMSIG (wait_status
)));
1434 /* Child voluntarily terminated -- but why? /bin/sh returns
1435 SIGPIPE + 128 if its child, then do nothing. */
1437 if (WEXITSTATUS (wait_status
)
1438 && WEXITSTATUS (wait_status
) != (SIGPIPE
+ 128))
1439 ERROR ((0, 0, _("Child returned status %d"),
1440 WEXITSTATUS (wait_status
)));
1445 if (current_file_name
)
1446 free (current_file_name
);
1447 if (current_link_name
)
1448 free (current_link_name
);
1451 free (multi_volume_option
? record_start
- 2 : record_start
);
1454 /*------------------------------------------------.
1455 | Called to initialize the global volume number. |
1456 `------------------------------------------------*/
1459 init_volume_number (void)
1461 FILE *file
= fopen (volno_file_option
, "r");
1465 fscanf (file
, "%d", &global_volno
);
1466 if (fclose (file
) == EOF
)
1467 ERROR ((0, errno
, "%s", volno_file_option
));
1469 else if (errno
!= ENOENT
)
1470 ERROR ((0, errno
, "%s", volno_file_option
));
1473 /*-------------------------------------------------------.
1474 | Called to write out the closing global volume number. |
1475 `-------------------------------------------------------*/
1478 closeout_volume_number (void)
1480 FILE *file
= fopen (volno_file_option
, "w");
1484 fprintf (file
, "%d\n", global_volno
);
1485 if (fclose (file
) == EOF
)
1486 ERROR ((0, errno
, "%s", volno_file_option
));
1489 ERROR ((0, errno
, "%s", 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
= NULL
;
1501 static int looped
= 0;
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)
1515 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1516 *archive_name_cursor
, archive
, status
));
1520 archive_name_cursor
++;
1521 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1523 archive_name_cursor
= archive_name_array
;
1530 /* We have to prompt from now on. */
1532 if (info_script_option
)
1534 if (volno_file_option
)
1535 closeout_volume_number ();
1536 system (info_script_option
);
1541 char input_buffer
[80];
1543 fputc ('\007', stderr
);
1545 _("Prepare volume #%d for %s and hit return: "),
1546 global_volno
, *archive_name_cursor
);
1549 if (fgets (input_buffer
, sizeof (input_buffer
), read_file
) == 0)
1551 fprintf (stderr
, _("EOF where user reply was expected"));
1553 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1554 && subcommand_option
!= LIST_SUBCOMMAND
1555 && subcommand_option
!= DIFF_SUBCOMMAND
)
1556 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1558 apply_delayed_set_stat ();
1559 exit (TAREXIT_FAILURE
);
1561 if (input_buffer
[0] == '\n'
1562 || input_buffer
[0] == 'y'
1563 || input_buffer
[0] == 'Y')
1566 switch (input_buffer
[0])
1570 fprintf (stderr
, _("\
1571 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1573 ! Spawn a subshell\n\
1574 ? Print this list\n"));
1581 fprintf (stdlis
, _("No new volume; exiting.\n"));
1583 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1584 && subcommand_option
!= LIST_SUBCOMMAND
1585 && subcommand_option
!= DIFF_SUBCOMMAND
)
1586 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1588 apply_delayed_set_stat ();
1589 exit (TAREXIT_FAILURE
);
1592 /* Get new file name. */
1595 char *name
= &input_buffer
[1];
1598 while (*name
== ' ' || *name
== '\t')
1601 while (*cursor
&& *cursor
!= '\n')
1605 /* FIXME: the following allocation is never reclaimed. */
1606 *archive_name_cursor
= xstrdup (name
);
1612 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1613 #else /* not MSDOS */
1615 pid_t child
= fork ();
1619 WARN ((0, errno
, _("Cannot fork!")));
1624 const char *shell
= getenv ("SHELL");
1628 execlp (shell
, "-sh", "-i", 0);
1629 FATAL_ERROR ((0, errno
, _("Cannot exec a shell %s"),
1636 while (waitpid (child
, &wait_status
, 0) == -1)
1640 _("While waiting for child")));
1647 #endif /* not MSDOS */
1654 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1655 rsh_command_option
);
1660 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1661 rsh_command_option
);
1666 maybe_backup_file (*archive_name_cursor
, 1);
1667 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1668 rsh_command_option
);
1672 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1673 rsh_command_option
);
1679 WARN ((0, errno
, _("Cannot open %s"), *archive_name_cursor
));
1680 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1681 undo_last_backup ();
1686 setmode (archive
, O_BINARY
);