1 /* Buffer management for tar.
2 Copyright (C) 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. */
30 # include <sys/inode.h>
33 #ifndef FNM_LEADING_DIR
40 #define DEBUG_FORK 0 /* if nonzero, childs are born stopped */
42 #define PREAD 0 /* read file descriptor from pipe() */
43 #define PWRITE 1 /* write file descriptor from pipe() */
45 /* Number of retries before giving up on read. */
46 #define READ_ERROR_MAX 10
48 /* Globbing pattern to append to volume label if initial match failed. */
49 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
53 static tarlong prev_written
; /* bytes written on previous volumes */
54 static tarlong bytes_written
; /* bytes written on this volume */
56 /* FIXME: The following four variables should ideally be static to this
57 module. However, this cannot be done yet, as update.c uses the first
58 three a lot, and compare.c uses the fourth. The cleanup continues! */
60 union block
*record_start
; /* start of record of archive */
61 union block
*record_end
; /* last+1 block of archive record */
62 union block
*current_block
; /* current block of archive */
63 enum access_mode access_mode
; /* how do we handle the archive */
64 static struct stat archive_stat
; /* stat block for archive file */
66 static off_t record_start_block
; /* block ordinal at record_start */
68 /* Where we write list messages (not errors, not interactions) to. Stdout
69 unless we're writing a pipe, in which case stderr. */
72 static void backspace_output
PARAMS ((void));
73 static int new_volume
PARAMS ((enum access_mode
));
74 static void write_error
PARAMS ((ssize_t
));
75 static void read_error
PARAMS ((void));
78 /* Obnoxious test to see if dimwit is trying to dump the archive. */
83 /* PID of child program, if compress_option or remote archive access. */
84 static pid_t child_pid
;
86 /* Error recovery stuff */
87 static int read_error_count
;
89 /* Have we hit EOF yet? */
92 /* Checkpointing counter */
93 static int checkpoint
;
95 /* We're reading, but we just read the last block and its time to update. */
96 /* As least EXTERN like this one as possible. FIXME! */
97 extern int time_to_start_writing
;
99 int file_to_switch_to
= -1; /* if remote update, close archive, and use
100 this descriptor to write to */
102 static int volno
= 1; /* which volume of a multi-volume tape we're
104 static int global_volno
= 1; /* volume number to print in external
107 /* The pointer save_name, which is set in function dump_file() of module
108 create.c, points to the original long filename instead of the new,
109 shorter mangled name that is set in start_header() of module create.c.
110 The pointer save_name is only used in multi-volume mode when the file
111 being processed is non-sparse; if a file is split between volumes, the
112 save_name is used in generating the LF_MULTIVOL record on the second
113 volume. (From Pierce Cantrell, 1991-08-13.) */
115 char *save_name
; /* name of the file we are currently writing */
116 off_t save_totsize
; /* total size of file we are writing, only
117 valid if save_name is non NULL */
118 off_t save_sizeleft
; /* where we are in the file we are writing,
119 only valid if save_name is nonzero */
121 int write_archive_to_stdout
= 0;
123 /* Used by flush_read and flush_write to store the real info about saved
125 static char *real_s_name
= NULL
;
126 static off_t real_s_totsize
;
127 static off_t real_s_sizeleft
;
136 pid_t result
= fork();
139 kill (getpid (), SIGSTOP
);
145 #endif /* DEBUG FORK */
148 print_total_written (void)
150 fprintf (stderr
, _("Total bytes written: "));
151 fprintf (stderr
, TARLONG_FORMAT
, prev_written
+ bytes_written
);
152 fprintf (stderr
, "\n");
155 /*--------------------------------------------------------.
156 | Compute and return the block ordinal at current_block. |
157 `--------------------------------------------------------*/
160 current_block_ordinal (void)
162 return record_start_block
+ (current_block
- record_start
);
165 /*------------------------------------------------------------------.
166 | If the EOF flag is set, reset it, as well as current_block, etc. |
167 `------------------------------------------------------------------*/
175 current_block
= record_start
;
176 record_end
= record_start
+ blocking_factor
;
177 access_mode
= ACCESS_WRITE
;
181 /*-------------------------------------------------------------------------.
182 | Return the location of the next available input or output block. |
183 | Return NULL for EOF. Once we have returned NULL, we just keep returning |
184 | it, to avoid accidentally going on to the next file on the tape. |
185 `-------------------------------------------------------------------------*/
188 find_next_block (void)
190 if (current_block
== record_end
)
195 if (current_block
== record_end
)
201 return current_block
;
204 /*------------------------------------------------------.
205 | Indicate that we have used all blocks up thru BLOCK. |
207 | FIXME: should the arg have an off-by-1? |
208 `------------------------------------------------------*/
211 set_next_block_after (union block
*block
)
213 while (block
>= current_block
)
216 /* Do *not* flush the archive here. If we do, the same argument to
217 set_next_block_after could mean the next block (if the input record
218 is exactly one block long), which is not what is intended. */
220 if (current_block
> record_end
)
224 /*------------------------------------------------------------------------.
225 | Return the number of bytes comprising the space between POINTER through |
226 | the end of the current buffer of blocks. This space is available for |
227 | filling with data, or taking data from. POINTER is usually (but not |
228 | always) the result previous find_next_block call. |
229 `------------------------------------------------------------------------*/
232 available_space_after (union block
*pointer
)
234 return record_end
->buffer
- pointer
->buffer
;
237 /*------------------------------------------------------------------.
238 | Close file having descriptor FD, and abort if close unsucessful. |
239 `------------------------------------------------------------------*/
245 FATAL_ERROR ((0, errno
, _("Cannot close file #%d"), fd
));
248 /*-----------------------------------------------------------------------.
249 | Duplicate file descriptor FROM into becoming INTO, or else, issue |
250 | MESSAGE. INTO is closed first and has to be the next available slot. |
251 `-----------------------------------------------------------------------*/
254 xdup2 (int from
, int into
, const char *message
)
258 int status
= close (into
);
260 if (status
< 0 && errno
!= EBADF
)
261 FATAL_ERROR ((0, errno
, _("Cannot close descriptor %d"), into
));
264 FATAL_ERROR ((0, errno
, _("Cannot properly duplicate %s"), message
));
271 /*-------------------------------------------------------.
272 | Set ARCHIVE for writing, then compressing an archive. |
273 `-------------------------------------------------------*/
276 child_open_for_compress (void)
278 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
281 /*---------------------------------------------------------.
282 | Set ARCHIVE for uncompressing, then reading an archive. |
283 `---------------------------------------------------------*/
286 child_open_for_uncompress (void)
288 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
291 #else /* not MSDOS */
293 /*---------------------------------------------------------------------.
294 | Return nonzero if NAME is the name of a regular file, or if the file |
295 | does not exist (so it would be created as a regular file). |
296 `---------------------------------------------------------------------*/
299 is_regular_file (const char *name
)
303 if (stat (name
, &stbuf
) == 0)
304 return S_ISREG (stbuf
.st_mode
);
306 return errno
== ENOENT
;
310 write_archive_buffer (void)
315 while (0 <= (status
= rmtwrite (archive
, record_start
->buffer
+ written
,
316 record_size
- written
)))
319 if (written
== record_size
320 || _isrmt (archive
) || ! S_ISFIFO (archive_stat
.st_mode
))
324 return written
? written
: status
;
327 /*-------------------------------------------------------.
328 | Set ARCHIVE for writing, then compressing an archive. |
329 `-------------------------------------------------------*/
332 child_open_for_compress (void)
336 pid_t grandchild_pid
;
338 if (pipe (parent_pipe
) < 0)
339 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
343 FATAL_ERROR ((0, errno
, _("Cannot fork")));
347 /* The parent tar is still here! Just clean up. */
349 archive
= parent_pipe
[PWRITE
];
350 xclose (parent_pipe
[PREAD
]);
354 /* The new born child tar is here! */
356 program_name
= _("tar (child)");
358 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
, _("(child) Pipe to stdin"));
359 xclose (parent_pipe
[PWRITE
]);
361 /* Check if we need a grandchild tar. This happens only if either:
362 a) we are writing stdout: to force reblocking;
363 b) the file is to be accessed by rmt: compressor doesn't know how;
364 c) the file is not a plain file. */
366 if (strcmp (archive_name_array
[0], "-") != 0
367 && !_remdev (archive_name_array
[0])
368 && is_regular_file (archive_name_array
[0]))
371 maybe_backup_file (archive_name_array
[0], 1);
373 /* We don't need a grandchild tar. Open the archive and launch the
376 archive
= creat (archive_name_array
[0], MODE_RW
);
379 int saved_errno
= errno
;
383 FATAL_ERROR ((0, saved_errno
, _("Cannot open archive %s"),
384 archive_name_array
[0]));
386 xdup2 (archive
, STDOUT_FILENO
, _("Archive to stdout"));
387 execlp (use_compress_program_option
, use_compress_program_option
,
389 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
390 use_compress_program_option
));
393 /* We do need a grandchild tar. */
395 if (pipe (child_pipe
) < 0)
396 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
398 grandchild_pid
= fork ();
399 if (grandchild_pid
< 0)
400 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
402 if (grandchild_pid
> 0)
404 /* The child tar is still here! Launch the compressor. */
406 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
,
407 _("((child)) Pipe to stdout"));
408 xclose (child_pipe
[PREAD
]);
409 execlp (use_compress_program_option
, use_compress_program_option
,
411 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
412 use_compress_program_option
));
415 /* The new born grandchild tar is here! */
417 program_name
= _("tar (grandchild)");
419 /* Prepare for reblocking the data from the compressor into the archive. */
421 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("(grandchild) Pipe to stdin"));
422 xclose (child_pipe
[PWRITE
]);
424 if (strcmp (archive_name_array
[0], "-") == 0)
425 archive
= STDOUT_FILENO
;
427 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
429 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
430 archive_name_array
[0]));
432 /* Let's read out of the stdin pipe and write an archive. */
440 /* Assemble a record. */
442 for (length
= 0, cursor
= record_start
->buffer
;
443 length
< record_size
;
444 length
+= status
, cursor
+= status
)
446 size_t size
= record_size
- length
;
448 if (size
< BLOCKSIZE
)
450 status
= safe_read (STDIN_FILENO
, cursor
, size
);
456 FATAL_ERROR ((0, errno
, _("Cannot read from compression program")));
458 /* Copy the record. */
462 /* We hit the end of the file. Write last record at
463 full length, as the only role of the grandchild is
464 doing proper reblocking. */
468 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
469 status
= write_archive_buffer ();
470 if (status
!= record_size
)
471 write_error (status
);
474 /* There is nothing else to read, break out. */
478 status
= write_archive_buffer ();
479 if (status
!= record_size
)
480 write_error (status
);
489 /*---------------------------------------------------------.
490 | Set ARCHIVE for uncompressing, then reading an archive. |
491 `---------------------------------------------------------*/
494 child_open_for_uncompress (void)
498 pid_t grandchild_pid
;
500 if (pipe (parent_pipe
) < 0)
501 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
505 FATAL_ERROR ((0, errno
, _("Cannot fork")));
509 /* The parent tar is still here! Just clean up. */
511 read_full_records_option
= 1;
512 archive
= parent_pipe
[PREAD
];
513 xclose (parent_pipe
[PWRITE
]);
517 /* The new born child tar is here! */
519 program_name
= _("tar (child)");
521 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
, _("(child) Pipe to stdout"));
522 xclose (parent_pipe
[PREAD
]);
524 /* Check if we need a grandchild tar. This happens only if either:
525 a) we're reading stdin: to force unblocking;
526 b) the file is to be accessed by rmt: compressor doesn't know how;
527 c) the file is not a plain file. */
529 if (strcmp (archive_name_array
[0], "-") != 0
530 && !_remdev (archive_name_array
[0])
531 && is_regular_file (archive_name_array
[0]))
533 /* We don't need a grandchild tar. Open the archive and lauch the
536 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
538 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
539 archive_name_array
[0]));
540 xdup2 (archive
, STDIN_FILENO
, _("Archive to stdin"));
541 execlp (use_compress_program_option
, use_compress_program_option
,
543 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
544 use_compress_program_option
));
547 /* We do need a grandchild tar. */
549 if (pipe (child_pipe
) < 0)
550 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
552 grandchild_pid
= fork ();
553 if (grandchild_pid
< 0)
554 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
556 if (grandchild_pid
> 0)
558 /* The child tar is still here! Launch the uncompressor. */
560 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("((child)) Pipe to stdin"));
561 xclose (child_pipe
[PWRITE
]);
562 execlp (use_compress_program_option
, use_compress_program_option
,
564 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
565 use_compress_program_option
));
568 /* The new born grandchild tar is here! */
570 program_name
= _("tar (grandchild)");
572 /* Prepare for unblocking the data from the archive into the uncompressor. */
574 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
, _("(grandchild) Pipe to stdout"));
575 xclose (child_pipe
[PREAD
]);
577 if (strcmp (archive_name_array
[0], "-") == 0)
578 archive
= STDIN_FILENO
;
580 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
581 MODE_RW
, rsh_command_option
);
583 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
584 archive_name_array
[0]));
586 /* Let's read the archive and pipe it into stdout. */
595 read_error_count
= 0;
598 status
= rmtread (archive
, record_start
->buffer
, record_size
);
606 cursor
= record_start
->buffer
;
610 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
611 status
= full_write (STDOUT_FILENO
, cursor
, count
);
613 FATAL_ERROR ((0, errno
, _("\
614 Cannot write to compression program")));
619 Write to compression program short %lu bytes"),
620 (unsigned long) (count
- status
)));
635 #endif /* not MSDOS */
637 /*--------------------------------------------------------------------------.
638 | Check the LABEL block against the volume label, seen as a globbing |
639 | pattern. Return true if the pattern matches. In case of failure, retry |
640 | matching a volume sequence number before giving up in multi-volume mode. |
641 `--------------------------------------------------------------------------*/
644 check_label_pattern (union block
*label
)
649 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
652 if (!multi_volume_option
)
655 string
= xmalloc (strlen (volume_label_option
)
656 + sizeof VOLUME_LABEL_APPEND
+ 1);
657 strcpy (string
, volume_label_option
);
658 strcat (string
, VOLUME_LABEL_APPEND
);
659 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
664 /*------------------------------------------------------------------------.
665 | Open an archive file. The argument specifies whether we are reading or |
666 | writing, or both. |
667 `------------------------------------------------------------------------*/
670 open_archive (enum access_mode access
)
672 int backed_up_flag
= 0;
674 stdlis
= to_stdout_option
? stderr
: stdout
;
676 if (record_size
== 0)
677 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
679 if (archive_names
== 0)
680 FATAL_ERROR ((0, 0, _("No archive name given")));
682 current_file_name
= NULL
;
683 current_link_name
= NULL
;
685 /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
686 constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
687 size that is reasonable to allocate a buffer. In the GNU system, there
688 is no fixed limit. The only correct thing to do is to use dynamic
689 allocation. (Roland McGrath) */
692 real_s_name
= (char *) xmalloc (PATH_MAX
);
693 /* FIXME: real_s_name is never freed. */
697 if (multi_volume_option
)
700 = (union block
*) valloc (record_size
+ (2 * BLOCKSIZE
));
705 record_start
= (union block
*) valloc (record_size
);
707 FATAL_ERROR ((0, 0, _("Could not allocate memory for blocking factor %d"),
710 current_block
= record_start
;
711 record_end
= record_start
+ blocking_factor
;
712 /* When updating the archive, we start with reading. */
713 access_mode
= access
== ACCESS_UPDATE
? ACCESS_READ
: access
;
715 if (multi_volume_option
&& verify_option
)
716 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
718 if (use_compress_program_option
)
720 if (multi_volume_option
)
721 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
723 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
728 child_open_for_uncompress ();
732 child_open_for_compress ();
736 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
740 if (access
== ACCESS_WRITE
&& strcmp (archive_name_array
[0], "-") == 0)
743 else if (strcmp (archive_name_array
[0], "-") == 0)
745 read_full_records_option
= 1; /* could be a pipe, be safe */
747 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
752 archive
= STDIN_FILENO
;
756 archive
= STDOUT_FILENO
;
761 archive
= STDIN_FILENO
;
763 write_archive_to_stdout
= 1;
767 else if (verify_option
)
768 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
769 MODE_RW
, rsh_command_option
);
774 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
775 MODE_RW
, rsh_command_option
);
781 maybe_backup_file (archive_name_array
[0], 1);
784 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
789 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
790 MODE_RW
, rsh_command_option
);
795 || (! _isrmt (archive
) && fstat (archive
, &archive_stat
) < 0))
797 int saved_errno
= errno
;
801 FATAL_ERROR ((0, saved_errno
, "%s", archive_name_array
[0]));
806 /* Detect if outputting to "/dev/null". */
808 static char const dev_null
[] = "/dev/null";
809 struct stat dev_null_stat
;
812 (strcmp (archive_name_array
[0], dev_null
) == 0
813 || (! _isrmt (archive
)
814 && S_ISCHR (archive_stat
.st_mode
)
815 && stat (dev_null
, &dev_null_stat
) == 0
816 && S_ISCHR (dev_null_stat
.st_mode
)
817 && archive_stat
.st_rdev
== dev_null_stat
.st_rdev
));
820 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
822 ar_dev
= archive_stat
.st_dev
;
823 ar_ino
= archive_stat
.st_ino
;
828 #endif /* not MSDOS */
831 setmode (archive
, O_BINARY
);
838 record_end
= record_start
; /* set up for 1st record = # 0 */
839 find_next_block (); /* read it in, check for EOF */
841 if (volume_label_option
)
843 union block
*label
= find_next_block ();
846 FATAL_ERROR ((0, 0, _("Archive not labelled to match `%s'"),
847 volume_label_option
));
848 if (!check_label_pattern (label
))
849 FATAL_ERROR ((0, 0, _("Volume `%s' does not match `%s'"),
850 label
->header
.name
, volume_label_option
));
855 if (volume_label_option
)
857 memset ((void *) record_start
, 0, BLOCKSIZE
);
858 if (multi_volume_option
)
859 sprintf (record_start
->header
.name
, "%s Volume 1",
860 volume_label_option
);
862 strcpy (record_start
->header
.name
, volume_label_option
);
864 assign_string (¤t_file_name
, record_start
->header
.name
);
866 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
867 TIME_TO_OCT (time (0), record_start
->header
.mtime
);
868 finish_header (record_start
);
877 /*--------------------------------------.
878 | Perform a write to flush the buffer. |
879 `--------------------------------------*/
887 if (checkpoint_option
&& !(++checkpoint
% 10))
888 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
890 if (tape_length_option
&& tape_length_option
<= bytes_written
)
892 errno
= ENOSPC
; /* FIXME: errno should be read-only */
895 else if (dev_null_output
)
896 status
= record_size
;
898 status
= write_archive_buffer ();
899 if (status
!= record_size
&& !multi_volume_option
)
900 write_error (status
);
903 bytes_written
+= status
;
905 if (status
== record_size
)
907 if (multi_volume_option
)
913 real_s_name
[0] = '\0';
921 if (cursor
[1] == ':')
924 while (*cursor
== '/')
927 strcpy (real_s_name
, cursor
);
928 real_s_totsize
= save_totsize
;
929 real_s_sizeleft
= save_sizeleft
;
934 /* We're multivol. Panic if we didn't get the right kind of response. */
936 /* ENXIO is for the UNIX PC. */
937 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
938 write_error (status
);
940 /* If error indicates a short write, we just move to the next tape. */
942 if (!new_volume (ACCESS_WRITE
))
946 prev_written
+= bytes_written
;
949 if (volume_label_option
&& real_s_name
[0])
954 else if (volume_label_option
|| real_s_name
[0])
962 if (volume_label_option
)
964 memset ((void *) record_start
, 0, BLOCKSIZE
);
965 sprintf (record_start
->header
.name
, "%s Volume %d", volume_label_option
, volno
);
966 TIME_TO_OCT (time (0), record_start
->header
.mtime
);
967 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
968 finish_header (record_start
);
975 if (volume_label_option
)
978 memset ((void *) record_start
, 0, BLOCKSIZE
);
980 /* FIXME: Michael P Urban writes: [a long name file] is being written
981 when a new volume rolls around [...] Looks like the wrong value is
982 being preserved in real_s_name, though. */
984 strcpy (record_start
->header
.name
, real_s_name
);
985 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
986 OFF_TO_OCT (real_s_sizeleft
, record_start
->header
.size
);
987 OFF_TO_OCT (real_s_totsize
- real_s_sizeleft
,
988 record_start
->oldgnu_header
.offset
);
989 tmp
= verbose_option
;
991 finish_header (record_start
);
992 verbose_option
= tmp
;
994 if (volume_label_option
)
998 status
= write_archive_buffer ();
999 if (status
!= record_size
)
1000 write_error (status
);
1002 bytes_written
+= status
;
1006 record_start
+= copy_back
;
1007 memcpy ((void *) current_block
,
1008 (void *) (record_start
+ blocking_factor
- copy_back
),
1009 (size_t) (copy_back
* BLOCKSIZE
));
1010 current_block
+= copy_back
;
1012 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
1013 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
1014 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
1015 real_s_name
[0] = '\0';
1018 char *cursor
= save_name
;
1021 if (cursor
[1] == ':')
1024 while (*cursor
== '/')
1027 strcpy (real_s_name
, cursor
);
1028 real_s_sizeleft
= save_sizeleft
;
1029 real_s_totsize
= save_totsize
;
1035 /*---------------------------------------------------------------------.
1036 | Handle write errors on the archive. Write errors are always fatal. |
1037 | Hitting the end of a volume does not cause a write error unless the |
1038 | write was the first record of the volume. |
1039 `---------------------------------------------------------------------*/
1042 write_error (ssize_t status
)
1044 int saved_errno
= errno
;
1046 /* It might be useful to know how much was written before the error
1047 occured. Beware that mere printing maybe change errno value. */
1049 print_total_written ();
1052 FATAL_ERROR ((0, saved_errno
, _("Cannot write to %s"),
1053 *archive_name_cursor
));
1055 FATAL_ERROR ((0, 0, _("Only wrote %lu of %lu bytes to %s"),
1056 (unsigned long) status
, (unsigned long) record_size
,
1057 *archive_name_cursor
));
1060 /*-------------------------------------------------------------------.
1061 | Handle read errors on the archive. If the read should be retried, |
1062 | returns to the caller. |
1063 `-------------------------------------------------------------------*/
1068 WARN ((0, errno
, _("Read error on %s"), *archive_name_cursor
));
1070 if (record_start_block
== 0)
1071 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1073 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1074 then give up on reading the archive. */
1076 if (read_error_count
++ > READ_ERROR_MAX
)
1077 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1081 /*-------------------------------------.
1082 | Perform a read to flush the buffer. |
1083 `-------------------------------------*/
1088 ssize_t status
; /* result from system call */
1089 size_t left
; /* bytes left */
1090 char *more
; /* pointer to next byte to read */
1092 if (checkpoint_option
&& !(++checkpoint
% 10))
1093 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1095 /* Clear the count of errors. This only applies to a single call to
1098 read_error_count
= 0; /* clear error count */
1100 if (write_archive_to_stdout
&& record_start_block
!= 0)
1102 status
= write_archive_buffer ();
1103 if (status
!= record_size
)
1104 write_error (status
);
1106 if (multi_volume_option
)
1110 char *cursor
= save_name
;
1113 if (cursor
[1] == ':')
1116 while (*cursor
== '/')
1119 strcpy (real_s_name
, cursor
);
1120 real_s_sizeleft
= save_sizeleft
;
1121 real_s_totsize
= save_totsize
;
1125 real_s_name
[0] = '\0';
1127 real_s_sizeleft
= 0;
1132 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1133 if (status
== record_size
)
1137 || (status
< 0 && errno
== ENOSPC
)
1138 || (status
> 0 && !read_full_records_option
))
1139 && multi_volume_option
)
1141 union block
*cursor
;
1144 switch (subcommand_option
)
1146 case APPEND_SUBCOMMAND
:
1147 case CAT_SUBCOMMAND
:
1148 case UPDATE_SUBCOMMAND
:
1149 if (!new_volume (ACCESS_UPDATE
))
1154 if (!new_volume (ACCESS_READ
))
1160 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1166 if (status
!= record_size
)
1169 cursor
= record_start
;
1171 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1173 if (volume_label_option
)
1175 if (!check_label_pattern (cursor
))
1177 WARN ((0, 0, _("Volume `%s' does not match `%s'"),
1178 cursor
->header
.name
, volume_label_option
));
1185 fprintf (stdlis
, _("Reading %s\n"), cursor
->header
.name
);
1188 else if (volume_label_option
)
1189 WARN ((0, 0, _("WARNING: No volume header")));
1194 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1195 || strcmp (cursor
->header
.name
, real_s_name
))
1197 WARN ((0, 0, _("%s is not continued on this volume"),
1203 s1
= UINTMAX_FROM_OCT (cursor
->header
.size
);
1204 s2
= UINTMAX_FROM_OCT (cursor
->oldgnu_header
.offset
);
1205 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
1207 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1208 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1209 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1211 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1212 cursor
->header
.name
,
1213 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1214 STRINGIFY_BIGINT (s1
, s1buf
),
1215 STRINGIFY_BIGINT (s2
, s2buf
)));
1220 if (real_s_totsize
- real_s_sizeleft
1221 != OFF_FROM_OCT (cursor
->oldgnu_header
.offset
))
1223 WARN ((0, 0, _("This volume is out of sequence")));
1230 current_block
= cursor
;
1233 else if (status
< 0)
1236 goto error_loop
; /* try again */
1240 more
= record_start
->buffer
+ status
;
1241 left
= record_size
- status
;
1244 if (left
% BLOCKSIZE
== 0)
1246 /* FIXME: for size=0, multi-volume support. On the first record, warn
1247 about the problem. */
1249 if (!read_full_records_option
&& verbose_option
1250 && record_start_block
== 0 && status
> 0)
1251 WARN ((0, 0, _("Record size = %lu blocks"),
1252 (unsigned long) (status
/ BLOCKSIZE
)));
1254 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
1258 if (read_full_records_option
)
1260 /* User warned us about this. Fix up. */
1265 status
= rmtread (archive
, more
, left
);
1269 goto error2loop
; /* try again */
1272 FATAL_ERROR ((0, 0, _("Archive %s EOF not on block boundary"),
1273 *archive_name_cursor
));
1280 FATAL_ERROR ((0, 0, _("Only read %lu bytes from archive %s"),
1281 (unsigned long) status
, *archive_name_cursor
));
1284 /*-----------------------------------------------.
1285 | Flush the current buffer to/from the archive. |
1286 `-----------------------------------------------*/
1289 flush_archive (void)
1291 record_start_block
+= record_end
- record_start
;
1292 current_block
= record_start
;
1293 record_end
= record_start
+ blocking_factor
;
1295 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1297 access_mode
= ACCESS_WRITE
;
1298 time_to_start_writing
= 0;
1300 if (file_to_switch_to
>= 0)
1302 int status
= rmtclose (archive
);
1305 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1306 *archive_name_cursor
, archive
, status
));
1308 archive
= file_to_switch_to
;
1311 backspace_output ();
1314 switch (access_mode
)
1329 /*-------------------------------------------------------------------------.
1330 | Backspace the archive descriptor by one record worth. If its a tape, |
1331 | MTIOCTOP will work. If its something else, we try to seek on it. If we |
1332 | can't seek, we lose! |
1333 `-------------------------------------------------------------------------*/
1336 backspace_output (void)
1340 struct mtop operation
;
1342 operation
.mt_op
= MTBSR
;
1343 operation
.mt_count
= 1;
1344 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1346 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1352 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1354 /* Seek back to the beginning of this record and start writing there. */
1356 position
-= record_size
;
1357 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1359 /* Lseek failed. Try a different method. */
1362 Could not backspace archive file; it may be unreadable without -i")));
1364 /* Replace the first part of the record with NULs. */
1366 if (record_start
->buffer
!= output_start
)
1367 memset (record_start
->buffer
, 0,
1368 (size_t) (output_start
- record_start
->buffer
));
1373 /*-------------------------.
1374 | Close the archive file. |
1375 `-------------------------*/
1378 close_archive (void)
1380 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1385 /* Manage to fully drain a pipe we might be reading, so to not break it on
1386 the producer after the EOF block. FIXME: one of these days, GNU tar
1387 might become clever enough to just stop working, once there is no more
1388 work to do, we might have to revise this area in such time. */
1390 if (access_mode
== ACCESS_READ
1391 && ! _isrmt (archive
)
1392 && S_ISFIFO (archive_stat
.st_mode
))
1393 while (rmtread (archive
, record_start
->buffer
, record_size
) > 0)
1397 if (! _isrmt (archive
) && subcommand_option
== DELETE_SUBCOMMAND
)
1400 int status
= write (archive
, "", 0);
1402 off_t pos
= lseek (archive
, (off_t
) 0, SEEK_CUR
);
1403 int status
= pos
< 0 ? -1 : ftruncate (archive
, pos
);
1406 WARN ((0, errno
, _("WARNING: Cannot truncate %s"),
1407 *archive_name_cursor
));
1413 int status
= rmtclose (archive
);
1416 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1417 *archive_name_cursor
, archive
, status
));
1427 /* Loop waiting for the right child to die, or for no more kids. */
1429 while ((child
= wait (&wait_status
), child
!= child_pid
)
1435 if (WIFSIGNALED (wait_status
)
1437 && !WIFSTOPPED (wait_status
)
1441 /* SIGPIPE is OK, everything else is a problem. */
1443 if (WTERMSIG (wait_status
) != SIGPIPE
)
1444 ERROR ((0, 0, _("Child died with signal %d%s"),
1445 WTERMSIG (wait_status
),
1446 WCOREDUMP (wait_status
) ? _(" (core dumped)") : ""));
1450 /* Child voluntarily terminated -- but why? /bin/sh returns
1451 SIGPIPE + 128 if its child, then do nothing. */
1453 if (WEXITSTATUS (wait_status
) != (SIGPIPE
+ 128)
1454 && WEXITSTATUS (wait_status
))
1455 ERROR ((0, 0, _("Child returned status %d"),
1456 WEXITSTATUS (wait_status
)));
1462 if (current_file_name
)
1463 free (current_file_name
);
1464 if (current_link_name
)
1465 free (current_link_name
);
1468 free (multi_volume_option
? record_start
- 2 : record_start
);
1471 /*------------------------------------------------.
1472 | Called to initialize the global volume number. |
1473 `------------------------------------------------*/
1476 init_volume_number (void)
1478 FILE *file
= fopen (volno_file_option
, "r");
1482 fscanf (file
, "%d", &global_volno
);
1483 if (fclose (file
) == EOF
)
1484 ERROR ((0, errno
, "%s", volno_file_option
));
1486 else if (errno
!= ENOENT
)
1487 ERROR ((0, errno
, "%s", volno_file_option
));
1490 /*-------------------------------------------------------.
1491 | Called to write out the closing global volume number. |
1492 `-------------------------------------------------------*/
1495 closeout_volume_number (void)
1497 FILE *file
= fopen (volno_file_option
, "w");
1501 fprintf (file
, "%d\n", global_volno
);
1502 if (fclose (file
) == EOF
)
1503 ERROR ((0, errno
, "%s", volno_file_option
));
1506 ERROR ((0, errno
, "%s", volno_file_option
));
1509 /*-----------------------------------------------------------------------.
1510 | We've hit the end of the old volume. Close it and open the next one. |
1511 | Return nonzero on success. |
1512 `-----------------------------------------------------------------------*/
1515 new_volume (enum access_mode access
)
1517 static FILE *read_file
= NULL
;
1518 static int looped
= 0;
1522 if (!read_file
&& !info_script_option
)
1523 /* FIXME: if fopen is used, it will never be closed. */
1524 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1531 if (status
= rmtclose (archive
), status
< 0)
1532 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1533 *archive_name_cursor
, archive
, status
));
1537 archive_name_cursor
++;
1538 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1540 archive_name_cursor
= archive_name_array
;
1547 /* We have to prompt from now on. */
1549 if (info_script_option
)
1551 if (volno_file_option
)
1552 closeout_volume_number ();
1553 system (info_script_option
);
1558 char input_buffer
[80];
1560 fputc ('\007', stderr
);
1562 _("Prepare volume #%d for %s and hit return: "),
1563 global_volno
, *archive_name_cursor
);
1566 if (fgets (input_buffer
, sizeof (input_buffer
), read_file
) == 0)
1568 fprintf (stderr
, _("EOF where user reply was expected"));
1570 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1571 && subcommand_option
!= LIST_SUBCOMMAND
1572 && subcommand_option
!= DIFF_SUBCOMMAND
)
1573 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1575 exit (TAREXIT_FAILURE
);
1577 if (input_buffer
[0] == '\n'
1578 || input_buffer
[0] == 'y'
1579 || input_buffer
[0] == 'Y')
1582 switch (input_buffer
[0])
1586 fprintf (stderr
, _("\
1587 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1589 ! Spawn a subshell\n\
1590 ? Print this list\n"));
1597 fprintf (stdlis
, _("No new volume; exiting.\n"));
1599 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1600 && subcommand_option
!= LIST_SUBCOMMAND
1601 && subcommand_option
!= DIFF_SUBCOMMAND
)
1602 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1604 exit (TAREXIT_FAILURE
);
1607 /* Get new file name. */
1610 char *name
= &input_buffer
[1];
1613 while (*name
== ' ' || *name
== '\t')
1616 while (*cursor
&& *cursor
!= '\n')
1620 /* FIXME: the following allocation is never reclaimed. */
1621 *archive_name_cursor
= xstrdup (name
);
1627 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1628 #else /* not MSDOS */
1632 WARN ((0, errno
, _("Cannot fork!")));
1637 const char *shell
= getenv ("SHELL");
1641 execlp (shell
, "-sh", "-i", 0);
1642 FATAL_ERROR ((0, errno
, _("Cannot exec a shell %s"),
1650 wait (&wait_status
);
1655 /* FIXME: I'm not sure if that's all that has to be done
1658 #endif /* not MSDOS */
1665 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1666 rsh_command_option
);
1671 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1672 rsh_command_option
);
1677 maybe_backup_file (*archive_name_cursor
, 1);
1678 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1679 rsh_command_option
);
1683 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1684 rsh_command_option
);
1690 WARN ((0, errno
, _("Cannot open %s"), *archive_name_cursor
));
1691 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1692 undo_last_backup ();
1697 setmode (archive
, O_BINARY
);