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 off_t 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 ((ssize_t
));
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 pid_t child_pid
;
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 off_t save_totsize
; /* total size of file we are writing, only
120 valid if save_name is non NULL */
121 off_t 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 off_t real_s_totsize
;
130 static off_t 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 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)
331 pid_t grandchild_pid
;
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 size_t size
= record_size
- length
;
442 if (size
< BLOCKSIZE
)
444 status
= read (STDIN
, cursor
, 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, record_size
- length
);
463 status
= rmtwrite (archive
, record_start
->buffer
, record_size
);
464 if (status
!= record_size
)
465 write_error (status
);
468 /* There is nothing else to read, break out. */
472 status
= rmtwrite (archive
, record_start
->buffer
, record_size
);
473 if (status
!= record_size
)
474 write_error (status
);
483 /*---------------------------------------------------------.
484 | Set ARCHIVE for uncompressing, then reading an archive. |
485 `---------------------------------------------------------*/
488 child_open_for_uncompress (void)
492 pid_t grandchild_pid
;
494 if (pipe (parent_pipe
) < 0)
495 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
499 FATAL_ERROR ((0, errno
, _("Cannot fork")));
503 /* The parent tar is still here! Just clean up. */
505 read_full_records_option
= 1;
506 archive
= parent_pipe
[PREAD
];
507 xclose (parent_pipe
[PWRITE
]);
511 /* The new born child tar is here! */
513 program_name
= _("tar (child)");
515 xdup2 (parent_pipe
[PWRITE
], STDOUT
, _("(child) Pipe to stdout"));
516 xclose (parent_pipe
[PREAD
]);
518 /* Check if we need a grandchild tar. This happens only if either:
519 a) we're reading stdin: to force unblocking;
520 b) the file is to be accessed by rmt: compressor doesn't know how;
521 c) the file is not a plain file. */
523 if (strcmp (archive_name_array
[0], "-") != 0
524 && !_remdev (archive_name_array
[0])
525 && is_regular_file (archive_name_array
[0]))
527 /* We don't need a grandchild tar. Open the archive and lauch the
530 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, 0666);
532 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
533 archive_name_array
[0]));
534 xdup2 (archive
, STDIN
, _("Archive to stdin"));
535 execlp (use_compress_program_option
, use_compress_program_option
,
537 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
538 use_compress_program_option
));
541 /* We do need a grandchild tar. */
543 if (pipe (child_pipe
) < 0)
544 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
546 grandchild_pid
= fork ();
547 if (grandchild_pid
< 0)
548 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
550 if (grandchild_pid
> 0)
552 /* The child tar is still here! Launch the uncompressor. */
554 xdup2 (child_pipe
[PREAD
], STDIN
, _("((child)) Pipe to stdin"));
555 xclose (child_pipe
[PWRITE
]);
556 execlp (use_compress_program_option
, use_compress_program_option
,
558 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
559 use_compress_program_option
));
562 /* The new born grandchild tar is here! */
564 program_name
= _("tar (grandchild)");
566 /* Prepare for unblocking the data from the archive into the uncompressor. */
568 xdup2 (child_pipe
[PWRITE
], STDOUT
, _("(grandchild) Pipe to stdout"));
569 xclose (child_pipe
[PREAD
]);
571 if (strcmp (archive_name_array
[0], "-") == 0)
574 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
575 0666, rsh_command_option
);
577 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
578 archive_name_array
[0]));
580 /* Let's read the archive and pipe it into stdout. */
589 read_error_count
= 0;
592 status
= rmtread (archive
, record_start
->buffer
, record_size
);
600 cursor
= record_start
->buffer
;
604 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
605 status
= write (STDOUT
, cursor
, count
);
607 FATAL_ERROR ((0, errno
, _("\
608 Cannot write to compression program")));
613 Write to compression program short %lu bytes"),
614 (unsigned long) (count
- status
)));
629 #endif /* not MSDOS */
631 /*--------------------------------------------------------------------------.
632 | Check the LABEL block against the volume label, seen as a globbing |
633 | pattern. Return true if the pattern matches. In case of failure, retry |
634 | matching a volume sequence number before giving up in multi-volume mode. |
635 `--------------------------------------------------------------------------*/
638 check_label_pattern (union block
*label
)
643 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
646 if (!multi_volume_option
)
649 string
= xmalloc (strlen (volume_label_option
)
650 + sizeof VOLUME_LABEL_APPEND
+ 1);
651 strcpy (string
, volume_label_option
);
652 strcat (string
, VOLUME_LABEL_APPEND
);
653 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
658 /*------------------------------------------------------------------------.
659 | Open an archive file. The argument specifies whether we are reading or |
660 | writing, or both. |
661 `------------------------------------------------------------------------*/
664 open_archive (enum access_mode access
)
666 int backed_up_flag
= 0;
668 stdlis
= to_stdout_option
? stderr
: stdout
;
670 if (record_size
== 0)
671 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
673 if (archive_names
== 0)
674 FATAL_ERROR ((0, 0, _("No archive name given")));
676 current_file_name
= NULL
;
677 current_link_name
= NULL
;
679 /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
680 constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
681 size that is reasonable to allocate a buffer. In the GNU system, there
682 is no fixed limit. The only correct thing to do is to use dynamic
683 allocation. (Roland McGrath) */
686 real_s_name
= (char *) xmalloc (PATH_MAX
);
687 /* FIXME: real_s_name is never freed. */
691 if (multi_volume_option
)
694 = (union block
*) valloc (record_size
+ (2 * BLOCKSIZE
));
699 record_start
= (union block
*) valloc (record_size
);
701 FATAL_ERROR ((0, 0, _("Could not allocate memory for blocking factor %d"),
704 current_block
= record_start
;
705 record_end
= record_start
+ blocking_factor
;
706 /* When updating the archive, we start with reading. */
707 access_mode
= access
== ACCESS_UPDATE
? ACCESS_READ
: access
;
709 if (multi_volume_option
&& verify_option
)
710 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
712 if (use_compress_program_option
)
714 if (multi_volume_option
)
715 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
717 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
722 child_open_for_uncompress ();
726 child_open_for_compress ();
730 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
734 if (access
== ACCESS_WRITE
&& strcmp (archive_name_array
[0], "-") == 0)
737 else if (strcmp (archive_name_array
[0], "-") == 0)
739 read_full_records_option
= 1; /* could be a pipe, be safe */
741 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
757 write_archive_to_stdout
= 1;
761 else if (verify_option
)
762 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
763 0666, rsh_command_option
);
768 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
, 0666,
775 maybe_backup_file (archive_name_array
[0], 1);
778 archive
= rmtcreat (archive_name_array
[0], 0666, rsh_command_option
);
782 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
783 0666, rsh_command_option
);
789 int saved_errno
= errno
;
793 FATAL_ERROR ((0, saved_errno
, _("Cannot open %s"),
794 archive_name_array
[0]));
799 fstat (archive
, &archive_stat
);
801 /* Detect if outputting to "/dev/null". */
803 struct stat dev_null_stat
;
805 stat ("/dev/null", &dev_null_stat
);
806 dev_null_output
= (S_ISCHR (archive_stat
.st_mode
)
807 && archive_stat
.st_rdev
== dev_null_stat
.st_rdev
);
810 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
812 ar_dev
= archive_stat
.st_dev
;
813 ar_ino
= archive_stat
.st_ino
;
816 #endif /* not MSDOS */
819 setmode (archive
, O_BINARY
);
826 record_end
= record_start
; /* set up for 1st record = # 0 */
827 find_next_block (); /* read it in, check for EOF */
829 if (volume_label_option
)
831 union block
*label
= find_next_block ();
834 FATAL_ERROR ((0, 0, _("Archive not labelled to match `%s'"),
835 volume_label_option
));
836 if (!check_label_pattern (label
))
837 FATAL_ERROR ((0, 0, _("Volume `%s' does not match `%s'"),
838 label
->header
.name
, volume_label_option
));
843 if (volume_label_option
)
845 memset ((void *) record_start
, 0, BLOCKSIZE
);
846 if (multi_volume_option
)
847 sprintf (record_start
->header
.name
, "%s Volume 1",
848 volume_label_option
);
850 strcpy (record_start
->header
.name
, volume_label_option
);
852 assign_string (¤t_file_name
, record_start
->header
.name
);
854 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
855 TIME_TO_OCT (time (0), record_start
->header
.mtime
);
856 finish_header (record_start
);
865 /*--------------------------------------.
866 | Perform a write to flush the buffer. |
867 `--------------------------------------*/
875 if (checkpoint_option
&& !(++checkpoint
% 10))
876 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
878 if (!zerop_tarlong (tape_length_option
)
879 && !lessp_tarlong (bytes_written
, tape_length_option
))
881 errno
= ENOSPC
; /* FIXME: errno should be read-only */
884 else if (dev_null_output
)
885 status
= record_size
;
887 status
= rmtwrite (archive
, record_start
->buffer
, record_size
);
888 if (status
!= record_size
&& !multi_volume_option
)
889 write_error (status
);
890 else if (totals_option
)
891 add_to_tarlong (total_written
, record_size
);
894 add_to_tarlong (bytes_written
, status
);
896 if (status
== record_size
)
898 if (multi_volume_option
)
904 real_s_name
[0] = '\0';
912 if (cursor
[1] == ':')
915 while (*cursor
== '/')
918 strcpy (real_s_name
, cursor
);
919 real_s_totsize
= save_totsize
;
920 real_s_sizeleft
= save_sizeleft
;
925 /* We're multivol. Panic if we didn't get the right kind of response. */
927 /* ENXIO is for the UNIX PC. */
928 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
929 write_error (status
);
931 /* If error indicates a short write, we just move to the next tape. */
933 if (!new_volume (ACCESS_WRITE
))
936 clear_tarlong (bytes_written
);
938 if (volume_label_option
&& real_s_name
[0])
943 else if (volume_label_option
|| real_s_name
[0])
951 if (volume_label_option
)
953 memset ((void *) record_start
, 0, BLOCKSIZE
);
954 sprintf (record_start
->header
.name
, "%s Volume %d", volume_label_option
, volno
);
955 TIME_TO_OCT (time (0), record_start
->header
.mtime
);
956 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
957 finish_header (record_start
);
964 if (volume_label_option
)
967 memset ((void *) record_start
, 0, BLOCKSIZE
);
969 /* FIXME: Michael P Urban writes: [a long name file] is being written
970 when a new volume rolls around [...] Looks like the wrong value is
971 being preserved in real_s_name, though. */
973 strcpy (record_start
->header
.name
, real_s_name
);
974 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
975 OFF_TO_OCT (real_s_sizeleft
, record_start
->header
.size
);
976 OFF_TO_OCT (real_s_totsize
- real_s_sizeleft
,
977 record_start
->oldgnu_header
.offset
);
978 tmp
= verbose_option
;
980 finish_header (record_start
);
981 verbose_option
= tmp
;
983 if (volume_label_option
)
987 status
= rmtwrite (archive
, record_start
->buffer
, record_size
);
988 if (status
!= record_size
)
989 write_error (status
);
990 else if (totals_option
)
991 add_to_tarlong (total_written
, record_size
);
993 add_to_tarlong (bytes_written
, record_size
);
996 record_start
+= copy_back
;
997 memcpy ((void *) current_block
,
998 (void *) (record_start
+ blocking_factor
- copy_back
),
999 (size_t) (copy_back
* BLOCKSIZE
));
1000 current_block
+= copy_back
;
1002 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
1003 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
1004 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
1005 real_s_name
[0] = '\0';
1008 char *cursor
= save_name
;
1011 if (cursor
[1] == ':')
1014 while (*cursor
== '/')
1017 strcpy (real_s_name
, cursor
);
1018 real_s_sizeleft
= save_sizeleft
;
1019 real_s_totsize
= save_totsize
;
1025 /*---------------------------------------------------------------------.
1026 | Handle write errors on the archive. Write errors are always fatal. |
1027 | Hitting the end of a volume does not cause a write error unless the |
1028 | write was the first record of the volume. |
1029 `---------------------------------------------------------------------*/
1032 write_error (ssize_t status
)
1034 int saved_errno
= errno
;
1036 /* It might be useful to know how much was written before the error
1037 occured. Beware that mere printing maybe change errno value. */
1039 print_total_written ();
1042 FATAL_ERROR ((0, saved_errno
, _("Cannot write to %s"),
1043 *archive_name_cursor
));
1045 FATAL_ERROR ((0, 0, _("Only wrote %lu of %lu bytes to %s"),
1046 (unsigned long) status
, (unsigned long) record_size
,
1047 *archive_name_cursor
));
1050 /*-------------------------------------------------------------------.
1051 | Handle read errors on the archive. If the read should be retried, |
1052 | returns to the caller. |
1053 `-------------------------------------------------------------------*/
1058 WARN ((0, errno
, _("Read error on %s"), *archive_name_cursor
));
1060 if (record_start_block
== 0)
1061 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1063 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1064 then give up on reading the archive. */
1066 if (read_error_count
++ > READ_ERROR_MAX
)
1067 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1071 /*-------------------------------------.
1072 | Perform a read to flush the buffer. |
1073 `-------------------------------------*/
1078 ssize_t status
; /* result from system call */
1079 size_t left
; /* bytes left */
1080 char *more
; /* pointer to next byte to read */
1082 if (checkpoint_option
&& !(++checkpoint
% 10))
1083 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1085 /* Clear the count of errors. This only applies to a single call to
1088 read_error_count
= 0; /* clear error count */
1090 if (write_archive_to_stdout
&& record_start_block
!= 0)
1092 status
= rmtwrite (1, record_start
->buffer
, record_size
);
1093 if (status
!= record_size
)
1094 write_error (status
);
1096 if (multi_volume_option
)
1099 char *cursor
= save_name
;
1102 if (cursor
[1] == ':')
1105 while (*cursor
== '/')
1108 strcpy (real_s_name
, cursor
);
1109 real_s_sizeleft
= save_sizeleft
;
1110 real_s_totsize
= save_totsize
;
1114 real_s_name
[0] = '\0';
1116 real_s_sizeleft
= 0;
1120 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1121 if (status
== record_size
)
1125 || (status
< 0 && errno
== ENOSPC
)
1126 || (status
> 0 && !read_full_records_option
))
1127 && multi_volume_option
)
1129 union block
*cursor
;
1132 switch (subcommand_option
)
1134 case APPEND_SUBCOMMAND
:
1135 case CAT_SUBCOMMAND
:
1136 case UPDATE_SUBCOMMAND
:
1137 if (!new_volume (ACCESS_UPDATE
))
1142 if (!new_volume (ACCESS_READ
))
1148 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1154 if (status
!= record_size
)
1157 cursor
= record_start
;
1159 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1161 if (volume_label_option
)
1163 if (!check_label_pattern (cursor
))
1165 WARN ((0, 0, _("Volume `%s' does not match `%s'"),
1166 cursor
->header
.name
, volume_label_option
));
1173 fprintf (stdlis
, _("Reading %s\n"), cursor
->header
.name
);
1176 else if (volume_label_option
)
1177 WARN ((0, 0, _("WARNING: No volume header")));
1182 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1183 || strcmp (cursor
->header
.name
, real_s_name
))
1185 WARN ((0, 0, _("%s is not continued on this volume"),
1191 s1
= UINTMAX_FROM_OCT (cursor
->header
.size
);
1192 s2
= UINTMAX_FROM_OCT (cursor
->oldgnu_header
.offset
);
1193 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
1195 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1196 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1197 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1199 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1200 cursor
->header
.name
,
1201 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1202 STRINGIFY_BIGINT (s1
, s1buf
),
1203 STRINGIFY_BIGINT (s2
, s2buf
)));
1208 if (real_s_totsize
- real_s_sizeleft
1209 != OFF_FROM_OCT (cursor
->oldgnu_header
.offset
))
1211 WARN ((0, 0, _("This volume is out of sequence")));
1218 current_block
= cursor
;
1221 else if (status
< 0)
1224 goto error_loop
; /* try again */
1228 more
= record_start
->buffer
+ status
;
1229 left
= record_size
- status
;
1232 if (left
% BLOCKSIZE
== 0)
1234 /* FIXME: for size=0, multi-volume support. On the first record, warn
1235 about the problem. */
1237 if (!read_full_records_option
&& verbose_option
1238 && record_start_block
== 0 && status
> 0)
1239 WARN ((0, 0, _("Record size = %lu blocks"),
1240 (unsigned long) (status
/ BLOCKSIZE
)));
1242 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
1246 if (read_full_records_option
)
1248 /* User warned us about this. Fix up. */
1253 status
= rmtread (archive
, more
, left
);
1257 goto error2loop
; /* try again */
1260 FATAL_ERROR ((0, 0, _("Archive %s EOF not on block boundary"),
1261 *archive_name_cursor
));
1268 FATAL_ERROR ((0, 0, _("Only read %lu bytes from archive %s"),
1269 (unsigned long) status
, *archive_name_cursor
));
1272 /*-----------------------------------------------.
1273 | Flush the current buffer to/from the archive. |
1274 `-----------------------------------------------*/
1277 flush_archive (void)
1279 record_start_block
+= record_end
- record_start
;
1280 current_block
= record_start
;
1281 record_end
= record_start
+ blocking_factor
;
1283 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1285 access_mode
= ACCESS_WRITE
;
1286 time_to_start_writing
= 0;
1288 if (file_to_switch_to
>= 0)
1290 int status
= rmtclose (archive
);
1293 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1294 *archive_name_cursor
, archive
, status
));
1296 archive
= file_to_switch_to
;
1299 backspace_output ();
1302 switch (access_mode
)
1317 /*-------------------------------------------------------------------------.
1318 | Backspace the archive descriptor by one record worth. If its a tape, |
1319 | MTIOCTOP will work. If its something else, we try to seek on it. If we |
1320 | can't seek, we lose! |
1321 `-------------------------------------------------------------------------*/
1324 backspace_output (void)
1328 struct mtop operation
;
1330 operation
.mt_op
= MTBSR
;
1331 operation
.mt_count
= 1;
1332 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1334 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1340 off_t position
= rmtlseek (archive
, 0L, 1);
1342 /* Seek back to the beginning of this record and start writing there. */
1344 position
-= record_size
;
1345 if (rmtlseek (archive
, position
, 0) != position
)
1347 /* Lseek failed. Try a different method. */
1350 Could not backspace archive file; it may be unreadable without -i")));
1352 /* Replace the first part of the record with NULs. */
1354 if (record_start
->buffer
!= output_start
)
1355 memset (record_start
->buffer
, 0,
1356 (size_t) (output_start
- record_start
->buffer
));
1361 /*-------------------------.
1362 | Close the archive file. |
1363 `-------------------------*/
1366 close_archive (void)
1368 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1373 /* Manage to fully drain a pipe we might be reading, so to not break it on
1374 the producer after the EOF block. FIXME: one of these days, GNU tar
1375 might become clever enough to just stop working, once there is no more
1376 work to do, we might have to revise this area in such time. */
1378 if (access_mode
== ACCESS_READ
&& S_ISFIFO (archive_stat
.st_mode
))
1379 while (rmtread (archive
, record_start
->buffer
, record_size
) > 0)
1383 if (subcommand_option
== DELETE_SUBCOMMAND
)
1387 pos
= rmtlseek (archive
, 0L, 1);
1389 rmtwrite (archive
, "", 0);
1391 ftruncate (archive
, pos
);
1398 int status
= rmtclose (archive
);
1401 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1402 *archive_name_cursor
, archive
, status
));
1412 /* Loop waiting for the right child to die, or for no more kids. */
1414 while ((child
= wait (&wait_status
), child
!= child_pid
)
1419 if (WIFSIGNALED (wait_status
)
1421 && !WIFSTOPPED (wait_status
)
1425 /* SIGPIPE is OK, everything else is a problem. */
1427 if (WTERMSIG (wait_status
) != SIGPIPE
)
1428 ERROR ((0, 0, _("Child died with signal %d%s"),
1429 WTERMSIG (wait_status
),
1430 WCOREDUMP (wait_status
) ? _(" (core dumped)") : ""));
1434 /* Child voluntarily terminated -- but why? /bin/sh returns
1435 SIGPIPE + 128 if its child, then do nothing. */
1437 if (WEXITSTATUS (wait_status
) != (SIGPIPE
+ 128)
1438 && WEXITSTATUS (wait_status
))
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
? 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];
1544 _("\007Prepare volume #%d for %s and hit return: "),
1545 global_volno
, *archive_name_cursor
);
1548 if (fgets (input_buffer
, sizeof (input_buffer
), read_file
) == 0)
1550 fprintf (stderr
, _("EOF where user reply was expected"));
1552 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1553 && subcommand_option
!= LIST_SUBCOMMAND
1554 && subcommand_option
!= DIFF_SUBCOMMAND
)
1555 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1557 exit (TAREXIT_FAILURE
);
1559 if (input_buffer
[0] == '\n'
1560 || input_buffer
[0] == 'y'
1561 || input_buffer
[0] == 'Y')
1564 switch (input_buffer
[0])
1568 fprintf (stderr
, _("\
1569 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1571 ! Spawn a subshell\n\
1572 ? Print this list\n"));
1579 fprintf (stdlis
, _("No new volume; exiting.\n"));
1581 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1582 && subcommand_option
!= LIST_SUBCOMMAND
1583 && subcommand_option
!= DIFF_SUBCOMMAND
)
1584 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1586 exit (TAREXIT_FAILURE
);
1589 /* Get new file name. */
1592 char *name
= &input_buffer
[1];
1595 while (*name
== ' ' || *name
== '\t')
1598 while (*cursor
&& *cursor
!= '\n')
1602 /* FIXME: the following allocation is never reclaimed. */
1603 *archive_name_cursor
= xstrdup (name
);
1609 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1610 #else /* not MSDOS */
1614 WARN ((0, errno
, _("Cannot fork!")));
1619 const char *shell
= getenv ("SHELL");
1623 execlp (shell
, "-sh", "-i", 0);
1624 FATAL_ERROR ((0, errno
, _("Cannot exec a shell %s"),
1632 wait (&wait_status
);
1637 /* FIXME: I'm not sure if that's all that has to be done
1640 #endif /* not MSDOS */
1647 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, 0666,
1648 rsh_command_option
);
1653 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, 0666,
1654 rsh_command_option
);
1659 maybe_backup_file (*archive_name_cursor
, 1);
1660 archive
= rmtcreat (*archive_name_cursor
, 0666, rsh_command_option
);
1664 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, 0666,
1665 rsh_command_option
);
1671 WARN ((0, errno
, _("Cannot open %s"), *archive_name_cursor
));
1672 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1673 undo_last_backup ();
1678 setmode (archive
, O_BINARY
);