1 /* Buffer management for tar.
2 Copyright (C) 1988, 92, 93, 94, 96, 97 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 Place - Suite 330, Boston, MA 02111-1307, USA. */
30 # include <sys/inode.h>
33 #ifndef FNM_LEADING_DIR
40 #define DEBUG_FORK 0 /* if nonzero, childs are born stopped */
42 #define STDIN 0 /* standard input file descriptor */
43 #define STDOUT 1 /* standard output file descriptor */
45 #define PREAD 0 /* read file descriptor from pipe() */
46 #define PWRITE 1 /* write file descriptor from pipe() */
48 /* Number of retries before giving up on read. */
49 #define READ_ERROR_MAX 10
51 /* Globbing pattern to append to volume label if initial match failed. */
52 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
56 static tarlong total_written
; /* bytes written on all volumes */
57 static tarlong bytes_written
; /* bytes written on this volume */
59 /* FIXME: The following four variables should ideally be static to this
60 module. However, this cannot be done yet, as update.c uses the first
61 three a lot, and compare.c uses the fourth. The cleanup continues! */
63 union block
*record_start
; /* start of record of archive */
64 union block
*record_end
; /* last+1 block of archive record */
65 union block
*current_block
; /* current block of archive */
66 enum access_mode access_mode
; /* how do we handle the archive */
67 static struct stat archive_stat
; /* stat block for archive file */
69 static long record_start_block
; /* block ordinal at record_start */
71 /* Where we write list messages (not errors, not interactions) to. Stdout
72 unless we're writing a pipe, in which case stderr. */
75 static void backspace_output
PARAMS ((void));
76 static int new_volume
PARAMS ((enum access_mode
));
77 static void write_error
PARAMS ((int));
78 static void read_error
PARAMS ((void));
81 /* Obnoxious test to see if dimwit is trying to dump the archive. */
86 /* PID of child program, if compress_option or remote archive access. */
87 static int child_pid
= 0;
89 /* Error recovery stuff */
90 static int read_error_count
;
92 /* Have we hit EOF yet? */
95 /* Checkpointing counter */
96 static int checkpoint
;
98 /* We're reading, but we just read the last block and its time to update. */
99 /* As least EXTERN like this one as possible. FIXME! */
100 extern int time_to_start_writing
;
102 int file_to_switch_to
= -1; /* if remote update, close archive, and use
103 this descriptor to write to */
105 static int volno
= 1; /* which volume of a multi-volume tape we're
107 static int global_volno
= 1; /* volume number to print in external
110 /* The pointer save_name, which is set in function dump_file() of module
111 create.c, points to the original long filename instead of the new,
112 shorter mangled name that is set in start_header() of module create.c.
113 The pointer save_name is only used in multi-volume mode when the file
114 being processed is non-sparse; if a file is split between volumes, the
115 save_name is used in generating the LF_MULTIVOL record on the second
116 volume. (From Pierce Cantrell, 1991-08-13.) */
118 char *save_name
; /* name of the file we are currently writing */
119 long save_totsize
; /* total size of file we are writing, only
120 valid if save_name is non NULL */
121 long save_sizeleft
; /* where we are in the file we are writing,
122 only valid if save_name is nonzero */
124 int write_archive_to_stdout
= 0;
126 /* Used by flush_read and flush_write to store the real info about saved
128 static char *real_s_name
= NULL
;
129 static long real_s_totsize
;
130 static long real_s_sizeleft
;
139 pid_t result
= fork();
142 kill (getpid (), SIGSTOP
);
148 #endif /* DEBUG FORK */
151 init_total_written (void)
153 clear_tarlong (total_written
);
154 clear_tarlong (bytes_written
);
158 print_total_written (void)
160 fprintf (stderr
, _("Total bytes written: "));
161 print_tarlong (total_written
, stderr
);
162 fprintf (stderr
, "\n");
165 /*--------------------------------------------------------.
166 | Compute and return the block ordinal at current_block. |
167 `--------------------------------------------------------*/
170 current_block_ordinal (void)
172 return record_start_block
+ (current_block
- record_start
);
175 /*------------------------------------------------------------------.
176 | If the EOF flag is set, reset it, as well as current_block, etc. |
177 `------------------------------------------------------------------*/
185 current_block
= record_start
;
186 record_end
= record_start
+ blocking_factor
;
187 access_mode
= ACCESS_WRITE
;
191 /*-------------------------------------------------------------------------.
192 | Return the location of the next available input or output block. |
193 | Return NULL for EOF. Once we have returned NULL, we just keep returning |
194 | it, to avoid accidentally going on to the next file on the tape. |
195 `-------------------------------------------------------------------------*/
198 find_next_block (void)
200 if (current_block
== record_end
)
205 if (current_block
== record_end
)
211 return current_block
;
214 /*------------------------------------------------------.
215 | Indicate that we have used all blocks up thru BLOCK. |
217 | FIXME: should the arg have an off-by-1? |
218 `------------------------------------------------------*/
221 set_next_block_after (union block
*block
)
223 while (block
>= current_block
)
226 /* Do *not* flush the archive here. If we do, the same argument to
227 set_next_block_after could mean the next block (if the input record
228 is exactly one block long), which is not what is intended. */
230 if (current_block
> record_end
)
234 /*------------------------------------------------------------------------.
235 | Return the number of bytes comprising the space between POINTER through |
236 | the end of the current buffer of blocks. This space is available for |
237 | filling with data, or taking data from. POINTER is usually (but not |
238 | always) the result previous find_next_block call. |
239 `------------------------------------------------------------------------*/
242 available_space_after (union block
*pointer
)
244 return (int) (record_end
->buffer
- pointer
->buffer
);
247 /*------------------------------------------------------------------.
248 | Close file having descriptor FD, and abort if close unsucessful. |
249 `------------------------------------------------------------------*/
255 FATAL_ERROR ((0, errno
, _("Cannot close file #%d"), fd
));
258 /*-----------------------------------------------------------------------.
259 | Duplicate file descriptor FROM into becoming INTO, or else, issue |
260 | MESSAGE. INTO is closed first and has to be the next available slot. |
261 `-----------------------------------------------------------------------*/
264 xdup2 (int from
, int into
, const char *message
)
268 int status
= close (into
);
270 if (status
< 0 && errno
!= EBADF
)
271 FATAL_ERROR ((0, errno
, _("Cannot close descriptor %d"), into
));
274 FATAL_ERROR ((0, errno
, _("Cannot properly duplicate %s"), message
));
281 /*-------------------------------------------------------.
282 | Set ARCHIVE for writing, then compressing an archive. |
283 `-------------------------------------------------------*/
286 child_open_for_compress (void)
288 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
291 /*---------------------------------------------------------.
292 | Set ARCHIVE for uncompressing, then reading an archive. |
293 `---------------------------------------------------------*/
296 child_open_for_uncompress (void)
298 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
301 #else /* not MSDOS */
303 /*---------------------------------------------------------------------.
304 | Return nonzero if NAME is the name of a regular file, or if the file |
305 | does not exist (so it would be created as a regular file). |
306 `---------------------------------------------------------------------*/
309 is_regular_file (const char *name
)
313 if (stat (name
, &stbuf
) < 0)
316 if (S_ISREG (stbuf
.st_mode
))
322 /*-------------------------------------------------------.
323 | Set ARCHIVE for writing, then compressing an archive. |
324 `-------------------------------------------------------*/
327 child_open_for_compress (void)
333 if (pipe (parent_pipe
) < 0)
334 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
338 FATAL_ERROR ((0, errno
, _("Cannot fork")));
342 /* The parent tar is still here! Just clean up. */
344 archive
= parent_pipe
[PWRITE
];
345 xclose (parent_pipe
[PREAD
]);
349 /* The new born child tar is here! */
351 program_name
= _("tar (child)");
353 xdup2 (parent_pipe
[PREAD
], STDIN
, _("(child) Pipe to stdin"));
354 xclose (parent_pipe
[PWRITE
]);
356 /* Check if we need a grandchild tar. This happens only if either:
357 a) we are writing stdout: to force reblocking;
358 b) the file is to be accessed by rmt: compressor doesn't know how;
359 c) the file is not a plain file. */
361 if (strcmp (archive_name_array
[0], "-") != 0
362 && !_remdev (archive_name_array
[0])
363 && is_regular_file (archive_name_array
[0]))
366 maybe_backup_file (archive_name_array
[0], 1);
368 /* We don't need a grandchild tar. Open the archive and launch the
371 archive
= creat (archive_name_array
[0], 0666);
374 int saved_errno
= errno
;
378 FATAL_ERROR ((0, saved_errno
, _("Cannot open archive %s"),
379 archive_name_array
[0]));
381 xdup2 (archive
, STDOUT
, _("Archive to stdout"));
382 execlp (use_compress_program_option
, use_compress_program_option
,
384 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
385 use_compress_program_option
));
388 /* We do need a grandchild tar. */
390 if (pipe (child_pipe
) < 0)
391 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
393 grandchild_pid
= fork ();
394 if (grandchild_pid
< 0)
395 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
397 if (grandchild_pid
> 0)
399 /* The child tar is still here! Launch the compressor. */
401 xdup2 (child_pipe
[PWRITE
], STDOUT
, _("((child)) Pipe to stdout"));
402 xclose (child_pipe
[PREAD
]);
403 execlp (use_compress_program_option
, use_compress_program_option
,
405 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
406 use_compress_program_option
));
409 /* The new born grandchild tar is here! */
411 program_name
= _("tar (grandchild)");
413 /* Prepare for reblocking the data from the compressor into the archive. */
415 xdup2 (child_pipe
[PREAD
], STDIN
, _("(grandchild) Pipe to stdin"));
416 xclose (child_pipe
[PWRITE
]);
418 if (strcmp (archive_name_array
[0], "-") == 0)
421 archive
= rmtcreat (archive_name_array
[0], 0666, rsh_command_option
);
423 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
424 archive_name_array
[0]));
426 /* Let's read out of the stdin pipe and write an archive. */
434 /* Assemble a record. */
436 for (length
= 0, cursor
= record_start
->buffer
;
437 length
< record_size
;
438 length
+= status
, cursor
+= status
)
440 int size
= record_size
- length
;
442 if (size
< BLOCKSIZE
)
444 status
= read (STDIN
, cursor
, (size_t) size
);
450 FATAL_ERROR ((0, errno
, _("Cannot read from compression program")));
452 /* Copy the record. */
456 /* We hit the end of the file. Write last record at
457 full length, as the only role of the grandchild is
458 doing proper reblocking. */
462 memset (record_start
->buffer
+ length
, 0,
463 (size_t) record_size
- length
);
464 status
= rmtwrite (archive
, record_start
->buffer
,
465 (unsigned int) record_size
);
466 if (status
!= record_size
)
467 write_error (status
);
470 /* There is nothing else to read, break out. */
474 status
= rmtwrite (archive
, record_start
->buffer
,
475 (unsigned int) record_size
);
476 if (status
!= record_size
)
477 write_error (status
);
486 /*---------------------------------------------------------.
487 | Set ARCHIVE for uncompressing, then reading an archive. |
488 `---------------------------------------------------------*/
491 child_open_for_uncompress (void)
497 if (pipe (parent_pipe
) < 0)
498 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
502 FATAL_ERROR ((0, errno
, _("Cannot fork")));
506 /* The parent tar is still here! Just clean up. */
508 read_full_records_option
= 1;
509 archive
= parent_pipe
[PREAD
];
510 xclose (parent_pipe
[PWRITE
]);
514 /* The new born child tar is here! */
516 program_name
= _("tar (child)");
518 xdup2 (parent_pipe
[PWRITE
], STDOUT
, _("(child) Pipe to stdout"));
519 xclose (parent_pipe
[PREAD
]);
521 /* Check if we need a grandchild tar. This happens only if either:
522 a) we're reading stdin: to force unblocking;
523 b) the file is to be accessed by rmt: compressor doesn't know how;
524 c) the file is not a plain file. */
526 if (strcmp (archive_name_array
[0], "-") != 0
527 && !_remdev (archive_name_array
[0])
528 && is_regular_file (archive_name_array
[0]))
530 /* We don't need a grandchild tar. Open the archive and lauch the
533 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, 0666);
535 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
536 archive_name_array
[0]));
537 xdup2 (archive
, STDIN
, _("Archive to stdin"));
538 execlp (use_compress_program_option
, use_compress_program_option
,
540 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
541 use_compress_program_option
));
544 /* We do need a grandchild tar. */
546 if (pipe (child_pipe
) < 0)
547 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
549 grandchild_pid
= fork ();
550 if (grandchild_pid
< 0)
551 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
553 if (grandchild_pid
> 0)
555 /* The child tar is still here! Launch the uncompressor. */
557 xdup2 (child_pipe
[PREAD
], STDIN
, _("((child)) Pipe to stdin"));
558 xclose (child_pipe
[PWRITE
]);
559 execlp (use_compress_program_option
, use_compress_program_option
,
561 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
562 use_compress_program_option
));
565 /* The new born grandchild tar is here! */
567 program_name
= _("tar (grandchild)");
569 /* Prepare for unblocking the data from the archive into the uncompressor. */
571 xdup2 (child_pipe
[PWRITE
], STDOUT
, _("(grandchild) Pipe to stdout"));
572 xclose (child_pipe
[PREAD
]);
574 if (strcmp (archive_name_array
[0], "-") == 0)
577 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
578 0666, rsh_command_option
);
580 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
581 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
,
596 (unsigned int) (record_size
));
604 cursor
= record_start
->buffer
;
608 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
609 status
= write (STDOUT
, cursor
, (size_t) count
);
611 FATAL_ERROR ((0, errno
, _("\
612 Cannot write to compression program")));
617 Write to compression program short %d bytes"),
633 #endif /* not MSDOS */
635 /*--------------------------------------------------------------------------.
636 | Check the LABEL block against the volume label, seen as a globbing |
637 | pattern. Return true if the pattern matches. In case of failure, retry |
638 | matching a volume sequence number before giving up in multi-volume mode. |
639 `--------------------------------------------------------------------------*/
642 check_label_pattern (union block
*label
)
647 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
650 if (!multi_volume_option
)
653 string
= xmalloc (strlen (volume_label_option
)
654 + sizeof VOLUME_LABEL_APPEND
+ 1);
655 strcpy (string
, volume_label_option
);
656 strcat (string
, VOLUME_LABEL_APPEND
);
657 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
662 /*------------------------------------------------------------------------.
663 | Open an archive file. The argument specifies whether we are reading or |
664 | writing, or both. |
665 `------------------------------------------------------------------------*/
668 open_archive (enum access_mode access
)
670 int backed_up_flag
= 0;
672 stdlis
= to_stdout_option
? stderr
: stdout
;
674 if (record_size
== 0)
675 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
677 if (archive_names
== 0)
678 FATAL_ERROR ((0, 0, _("No archive name given")));
680 current_file_name
= NULL
;
681 current_link_name
= NULL
;
683 /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
684 constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
685 size that is reasonable to allocate a buffer. In the GNU system, there
686 is no fixed limit. The only correct thing to do is to use dynamic
687 allocation. (Roland McGrath) */
690 real_s_name
= (char *) xmalloc (PATH_MAX
);
691 /* FIXME: real_s_name is never freed. */
695 if (multi_volume_option
)
698 = (union block
*) valloc ((unsigned) (record_size
+ (2 * BLOCKSIZE
)));
703 record_start
= (union block
*) valloc ((unsigned) record_size
);
705 FATAL_ERROR ((0, 0, _("Could not allocate memory for blocking factor %d"),
708 current_block
= record_start
;
709 record_end
= record_start
+ blocking_factor
;
710 /* When updating the archive, we start with reading. */
711 access_mode
= access
== ACCESS_UPDATE
? ACCESS_READ
: access
;
713 if (multi_volume_option
&& verify_option
)
714 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
716 if (use_compress_program_option
)
718 if (multi_volume_option
)
719 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
721 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
726 child_open_for_uncompress ();
730 child_open_for_compress ();
734 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
738 if (access
== ACCESS_WRITE
&& strcmp (archive_name_array
[0], "-") == 0)
741 else if (strcmp (archive_name_array
[0], "-") == 0)
743 read_full_records_option
= 1; /* could be a pipe, be safe */
745 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
761 write_archive_to_stdout
= 1;
765 else if (verify_option
)
766 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
767 0666, rsh_command_option
);
772 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
, 0666,
779 maybe_backup_file (archive_name_array
[0], 1);
782 archive
= rmtcreat (archive_name_array
[0], 0666, rsh_command_option
);
786 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
787 0666, rsh_command_option
);
793 int saved_errno
= errno
;
797 FATAL_ERROR ((0, saved_errno
, _("Cannot open %s"),
798 archive_name_array
[0]));
803 fstat (archive
, &archive_stat
);
805 /* Detect if outputting to "/dev/null". */
807 struct stat dev_null_stat
;
809 stat ("/dev/null", &dev_null_stat
);
810 dev_null_output
= (S_ISCHR (archive_stat
.st_mode
)
811 && archive_stat
.st_rdev
== dev_null_stat
.st_rdev
);
814 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
816 ar_dev
= archive_stat
.st_dev
;
817 ar_ino
= archive_stat
.st_ino
;
820 #endif /* not MSDOS */
823 setmode (archive
, O_BINARY
);
830 record_end
= record_start
; /* set up for 1st record = # 0 */
831 find_next_block (); /* read it in, check for EOF */
833 if (volume_label_option
)
835 union block
*label
= find_next_block ();
838 FATAL_ERROR ((0, 0, _("Archive not labelled to match `%s'"),
839 volume_label_option
));
840 if (!check_label_pattern (label
))
841 FATAL_ERROR ((0, 0, _("Volume `%s' does not match `%s'"),
842 label
->header
.name
, volume_label_option
));
847 if (volume_label_option
)
849 memset ((void *) record_start
, 0, BLOCKSIZE
);
850 if (multi_volume_option
)
851 sprintf (record_start
->header
.name
, "%s Volume 1",
852 volume_label_option
);
854 strcpy (record_start
->header
.name
, volume_label_option
);
856 assign_string (¤t_file_name
, record_start
->header
.name
);
858 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
859 to_oct (time (0), 1 + 12, record_start
->header
.mtime
);
860 finish_header (record_start
);
869 /*--------------------------------------.
870 | Perform a write to flush the buffer. |
871 `--------------------------------------*/
879 if (checkpoint_option
&& !(++checkpoint
% 10))
880 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
882 if (!zerop_tarlong (tape_length_option
)
883 && !lessp_tarlong (bytes_written
, tape_length_option
))
885 errno
= ENOSPC
; /* FIXME: errno should be read-only */
888 else if (dev_null_output
)
889 status
= record_size
;
891 status
= rmtwrite (archive
, record_start
->buffer
,
892 (unsigned int) record_size
);
893 if (status
!= record_size
&& !multi_volume_option
)
894 write_error (status
);
895 else if (totals_option
)
896 add_to_tarlong (total_written
, record_size
);
899 add_to_tarlong (bytes_written
, status
);
901 if (status
== record_size
)
903 if (multi_volume_option
)
909 real_s_name
[0] = '\0';
917 if (cursor
[1] == ':')
920 while (*cursor
== '/')
923 strcpy (real_s_name
, cursor
);
924 real_s_totsize
= save_totsize
;
925 real_s_sizeleft
= save_sizeleft
;
930 /* We're multivol. Panic if we didn't get the right kind of response. */
932 /* ENXIO is for the UNIX PC. */
933 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
934 write_error (status
);
936 /* If error indicates a short write, we just move to the next tape. */
938 if (!new_volume (ACCESS_WRITE
))
941 clear_tarlong (bytes_written
);
943 if (volume_label_option
&& real_s_name
[0])
948 else if (volume_label_option
|| real_s_name
[0])
956 if (volume_label_option
)
958 memset ((void *) record_start
, 0, BLOCKSIZE
);
959 sprintf (record_start
->header
.name
, "%s Volume %d", volume_label_option
, volno
);
960 to_oct (time (0), 1 + 12, record_start
->header
.mtime
);
961 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
962 finish_header (record_start
);
969 if (volume_label_option
)
972 memset ((void *) record_start
, 0, BLOCKSIZE
);
974 /* FIXME: Michael P Urban writes: [a long name file] is being written
975 when a new volume rolls around [...] Looks like the wrong value is
976 being preserved in real_s_name, though. */
978 strcpy (record_start
->header
.name
, real_s_name
);
979 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
980 to_oct ((long) real_s_sizeleft
, 1 + 12,
981 record_start
->header
.size
);
982 to_oct ((long) real_s_totsize
- real_s_sizeleft
,
983 1 + 12, record_start
->oldgnu_header
.offset
);
984 tmp
= verbose_option
;
986 finish_header (record_start
);
987 verbose_option
= tmp
;
989 if (volume_label_option
)
993 status
= rmtwrite (archive
, record_start
->buffer
,
994 (unsigned int) record_size
);
995 if (status
!= record_size
)
996 write_error (status
);
997 else if (totals_option
)
998 add_to_tarlong (total_written
, record_size
);
1000 add_to_tarlong (bytes_written
, record_size
);
1003 record_start
+= copy_back
;
1004 memcpy ((void *) current_block
,
1005 (void *) (record_start
+ blocking_factor
- copy_back
),
1006 (size_t) (copy_back
* BLOCKSIZE
));
1007 current_block
+= copy_back
;
1009 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
1010 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
1011 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
1012 real_s_name
[0] = '\0';
1015 char *cursor
= save_name
;
1018 if (cursor
[1] == ':')
1021 while (*cursor
== '/')
1024 strcpy (real_s_name
, cursor
);
1025 real_s_sizeleft
= save_sizeleft
;
1026 real_s_totsize
= save_totsize
;
1032 /*---------------------------------------------------------------------.
1033 | Handle write errors on the archive. Write errors are always fatal. |
1034 | Hitting the end of a volume does not cause a write error unless the |
1035 | write was the first record of the volume. |
1036 `---------------------------------------------------------------------*/
1039 write_error (int status
)
1041 int saved_errno
= errno
;
1043 /* It might be useful to know how much was written before the error
1044 occured. Beware that mere printing maybe change errno value. */
1046 print_total_written ();
1049 FATAL_ERROR ((0, saved_errno
, _("Cannot write to %s"),
1050 *archive_name_cursor
));
1052 FATAL_ERROR ((0, 0, _("Only wrote %u of %u bytes to %s"),
1053 status
, record_size
, *archive_name_cursor
));
1056 /*-------------------------------------------------------------------.
1057 | Handle read errors on the archive. If the read should be retried, |
1058 | returns to the caller. |
1059 `-------------------------------------------------------------------*/
1064 WARN ((0, errno
, _("Read error on %s"), *archive_name_cursor
));
1066 if (record_start_block
== 0)
1067 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1069 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1070 then give up on reading the archive. */
1072 if (read_error_count
++ > READ_ERROR_MAX
)
1073 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1077 /*-------------------------------------.
1078 | Perform a read to flush the buffer. |
1079 `-------------------------------------*/
1084 int status
; /* result from system call */
1085 int left
; /* bytes left */
1086 char *more
; /* pointer to next byte to read */
1088 if (checkpoint_option
&& !(++checkpoint
% 10))
1089 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1091 /* Clear the count of errors. This only applies to a single call to
1094 read_error_count
= 0; /* clear error count */
1096 if (write_archive_to_stdout
&& record_start_block
!= 0)
1098 status
= rmtwrite (1, record_start
->buffer
, (unsigned int) record_size
);
1099 if (status
!= record_size
)
1100 write_error (status
);
1102 if (multi_volume_option
)
1105 char *cursor
= save_name
;
1108 if (cursor
[1] == ':')
1111 while (*cursor
== '/')
1114 strcpy (real_s_name
, cursor
);
1115 real_s_sizeleft
= save_sizeleft
;
1116 real_s_totsize
= save_totsize
;
1120 real_s_name
[0] = '\0';
1122 real_s_sizeleft
= 0;
1126 status
= rmtread (archive
, record_start
->buffer
, (unsigned int) record_size
);
1127 if (status
== record_size
)
1131 || (status
< 0 && errno
== ENOSPC
)
1132 || (status
> 0 && !read_full_records_option
))
1133 && multi_volume_option
)
1135 union block
*cursor
;
1138 switch (subcommand_option
)
1140 case APPEND_SUBCOMMAND
:
1141 case CAT_SUBCOMMAND
:
1142 case UPDATE_SUBCOMMAND
:
1143 if (!new_volume (ACCESS_UPDATE
))
1148 if (!new_volume (ACCESS_READ
))
1154 status
= rmtread (archive
, record_start
->buffer
,
1155 (unsigned int) record_size
);
1161 if (status
!= record_size
)
1164 cursor
= record_start
;
1166 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1168 if (volume_label_option
)
1170 if (!check_label_pattern (cursor
))
1172 WARN ((0, 0, _("Volume `%s' does not match `%s'"),
1173 cursor
->header
.name
, volume_label_option
));
1180 fprintf (stdlis
, _("Reading %s\n"), cursor
->header
.name
);
1183 else if (volume_label_option
)
1184 WARN ((0, 0, _("WARNING: No volume header")));
1188 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1189 || strcmp (cursor
->header
.name
, real_s_name
))
1191 WARN ((0, 0, _("%s is not continued on this volume"),
1198 != (from_oct (1 + 12, cursor
->header
.size
)
1199 + from_oct (1 + 12, cursor
->oldgnu_header
.offset
)))
1201 WARN ((0, 0, _("%s is the wrong size (%ld != %ld + %ld)"),
1202 cursor
->header
.name
, save_totsize
,
1203 from_oct (1 + 12, cursor
->header
.size
),
1204 from_oct (1 + 12, cursor
->oldgnu_header
.offset
)));
1209 if (real_s_totsize
- real_s_sizeleft
1210 != from_oct (1 + 12, cursor
->oldgnu_header
.offset
))
1212 WARN ((0, 0, _("This volume is out of sequence")));
1219 current_block
= cursor
;
1222 else if (status
< 0)
1225 goto error_loop
; /* try again */
1229 more
= record_start
->buffer
+ status
;
1230 left
= record_size
- status
;
1233 if ((unsigned) left
% BLOCKSIZE
== 0)
1235 /* FIXME: for size=0, multi-volume support. On the first record, warn
1236 about the problem. */
1238 if (!read_full_records_option
&& verbose_option
1239 && record_start_block
== 0 && status
> 0)
1240 WARN ((0, 0, _("Record size = %d blocks"), status
/ BLOCKSIZE
));
1243 = record_start
+ ((unsigned) (record_size
- left
)) / BLOCKSIZE
;
1247 if (read_full_records_option
)
1249 /* User warned us about this. Fix up. */
1254 status
= rmtread (archive
, more
, (unsigned int) left
);
1258 goto error2loop
; /* try again */
1261 FATAL_ERROR ((0, 0, _("Archive %s EOF not on block boundary"),
1262 *archive_name_cursor
));
1269 FATAL_ERROR ((0, 0, _("Only read %d bytes from archive %s"),
1270 status
, *archive_name_cursor
));
1273 /*-----------------------------------------------.
1274 | Flush the current buffer to/from the archive. |
1275 `-----------------------------------------------*/
1278 flush_archive (void)
1280 record_start_block
+= record_end
- record_start
;
1281 current_block
= record_start
;
1282 record_end
= record_start
+ blocking_factor
;
1284 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1286 access_mode
= ACCESS_WRITE
;
1287 time_to_start_writing
= 0;
1289 if (file_to_switch_to
>= 0)
1291 int status
= rmtclose (archive
);
1294 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1295 *archive_name_cursor
, archive
, status
));
1297 archive
= file_to_switch_to
;
1300 backspace_output ();
1303 switch (access_mode
)
1318 /*-------------------------------------------------------------------------.
1319 | Backspace the archive descriptor by one record worth. If its a tape, |
1320 | MTIOCTOP will work. If its something else, we try to seek on it. If we |
1321 | can't seek, we lose! |
1322 `-------------------------------------------------------------------------*/
1325 backspace_output (void)
1329 struct mtop operation
;
1331 operation
.mt_op
= MTBSR
;
1332 operation
.mt_count
= 1;
1333 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1335 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1341 off_t position
= rmtlseek (archive
, 0L, 1);
1343 /* Seek back to the beginning of this record and start writing there. */
1345 position
-= record_size
;
1346 if (rmtlseek (archive
, position
, 0) != position
)
1348 /* Lseek failed. Try a different method. */
1351 Could not backspace archive file; it may be unreadable without -i")));
1353 /* Replace the first part of the record with NULs. */
1355 if (record_start
->buffer
!= output_start
)
1356 memset (record_start
->buffer
, 0,
1357 (size_t) (output_start
- record_start
->buffer
));
1362 /*-------------------------.
1363 | Close the archive file. |
1364 `-------------------------*/
1367 close_archive (void)
1369 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1374 /* Manage to fully drain a pipe we might be reading, so to not break it on
1375 the producer after the EOF block. FIXME: one of these days, GNU tar
1376 might become clever enough to just stop working, once there is no more
1377 work to do, we might have to revise this area in such time. */
1379 if (access_mode
== ACCESS_READ
&& S_ISFIFO (archive_stat
.st_mode
))
1380 while (rmtread (archive
, record_start
->buffer
, (unsigned int) record_size
)
1385 if (subcommand_option
== DELETE_SUBCOMMAND
)
1389 pos
= rmtlseek (archive
, 0L, 1);
1391 rmtwrite (archive
, "", 0);
1393 ftruncate (archive
, (size_t) pos
);
1400 int status
= rmtclose (archive
);
1403 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1404 *archive_name_cursor
, archive
, status
));
1414 /* Loop waiting for the right child to die, or for no more kids. */
1416 while ((child
= wait (&wait_status
), child
!= child_pid
)
1421 if (WIFSIGNALED (wait_status
)
1423 && !WIFSTOPPED (wait_status
)
1427 /* SIGPIPE is OK, everything else is a problem. */
1429 if (WTERMSIG (wait_status
) != SIGPIPE
)
1430 ERROR ((0, 0, _("Child died with signal %d%s"),
1431 WTERMSIG (wait_status
),
1432 WCOREDUMP (wait_status
) ? _(" (core dumped)") : ""));
1436 /* Child voluntarily terminated -- but why? /bin/sh returns
1437 SIGPIPE + 128 if its child, then do nothing. */
1439 if (WEXITSTATUS (wait_status
) != (SIGPIPE
+ 128)
1440 && WEXITSTATUS (wait_status
))
1441 ERROR ((0, 0, _("Child returned status %d"),
1442 WEXITSTATUS (wait_status
)));
1447 if (current_file_name
)
1448 free (current_file_name
);
1449 if (current_link_name
)
1450 free (current_link_name
);
1453 free (multi_volume_option
? record_start
- 2 : record_start
);
1456 /*------------------------------------------------.
1457 | Called to initialize the global volume number. |
1458 `------------------------------------------------*/
1461 init_volume_number (void)
1463 FILE *file
= fopen (volno_file_option
, "r");
1467 fscanf (file
, "%d", &global_volno
);
1468 if (fclose (file
) == EOF
)
1469 ERROR ((0, errno
, "%s", volno_file_option
));
1471 else if (errno
!= ENOENT
)
1472 ERROR ((0, errno
, "%s", volno_file_option
));
1475 /*-------------------------------------------------------.
1476 | Called to write out the closing global volume number. |
1477 `-------------------------------------------------------*/
1480 closeout_volume_number (void)
1482 FILE *file
= fopen (volno_file_option
, "w");
1486 fprintf (file
, "%d\n", global_volno
);
1487 if (fclose (file
) == EOF
)
1488 ERROR ((0, errno
, "%s", volno_file_option
));
1491 ERROR ((0, errno
, "%s", volno_file_option
));
1494 /*-----------------------------------------------------------------------.
1495 | We've hit the end of the old volume. Close it and open the next one. |
1496 | Return nonzero on success. |
1497 `-----------------------------------------------------------------------*/
1500 new_volume (enum access_mode access
)
1502 static FILE *read_file
= NULL
;
1503 static int looped
= 0;
1507 if (!read_file
&& !info_script_option
)
1508 /* FIXME: if fopen is used, it will never be closed. */
1509 read_file
= archive
== STDIN
? fopen (TTY_NAME
, "r") : stdin
;
1516 if (status
= rmtclose (archive
), status
< 0)
1517 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1518 *archive_name_cursor
, archive
, status
));
1522 archive_name_cursor
++;
1523 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1525 archive_name_cursor
= archive_name_array
;
1532 /* We have to prompt from now on. */
1534 if (info_script_option
)
1536 if (volno_file_option
)
1537 closeout_volume_number ();
1538 system (info_script_option
);
1543 char input_buffer
[80];
1546 _("\007Prepare volume #%d for %s and hit return: "),
1547 global_volno
, *archive_name_cursor
);
1550 if (fgets (input_buffer
, sizeof (input_buffer
), read_file
) == 0)
1552 fprintf (stderr
, _("EOF where user reply was expected"));
1554 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1555 && subcommand_option
!= LIST_SUBCOMMAND
1556 && subcommand_option
!= DIFF_SUBCOMMAND
)
1557 WARN ((0, 0, _("WARNING: Archive is incomplete")));
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 exit (TAREXIT_FAILURE
);
1591 /* Get new file name. */
1594 char *name
= &input_buffer
[1];
1597 while (*name
== ' ' || *name
== '\t')
1600 while (*cursor
&& *cursor
!= '\n')
1604 /* FIXME: the following allocation is never reclaimed. */
1605 *archive_name_cursor
= xstrdup (name
);
1611 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1612 #else /* not MSDOS */
1616 WARN ((0, errno
, _("Cannot fork!")));
1621 const char *shell
= getenv ("SHELL");
1625 execlp (shell
, "-sh", "-i", 0);
1626 FATAL_ERROR ((0, errno
, _("Cannot exec a shell %s"),
1634 wait (&wait_status
);
1639 /* FIXME: I'm not sure if that's all that has to be done
1642 #endif /* not MSDOS */
1649 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, 0666,
1650 rsh_command_option
);
1655 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, 0666,
1656 rsh_command_option
);
1661 maybe_backup_file (*archive_name_cursor
, 1);
1662 archive
= rmtcreat (*archive_name_cursor
, 0666, rsh_command_option
);
1666 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, 0666,
1667 rsh_command_option
);
1673 WARN ((0, errno
, _("Cannot open %s"), *archive_name_cursor
));
1674 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1675 undo_last_backup ();
1680 setmode (archive
, O_BINARY
);