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 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
))
323 write_archive_buffer (void)
328 while (0 <= (status
= rmtwrite (archive
, record_start
->buffer
+ written
,
329 record_size
- written
)))
332 if (written
== record_size
333 || _isrmt (archive
) || ! S_ISFIFO (archive_stat
.st_mode
))
337 return written
? written
: status
;
340 /*-------------------------------------------------------.
341 | Set ARCHIVE for writing, then compressing an archive. |
342 `-------------------------------------------------------*/
345 child_open_for_compress (void)
349 pid_t grandchild_pid
;
351 if (pipe (parent_pipe
) < 0)
352 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
356 FATAL_ERROR ((0, errno
, _("Cannot fork")));
360 /* The parent tar is still here! Just clean up. */
362 archive
= parent_pipe
[PWRITE
];
363 xclose (parent_pipe
[PREAD
]);
367 /* The new born child tar is here! */
369 program_name
= _("tar (child)");
371 xdup2 (parent_pipe
[PREAD
], STDIN
, _("(child) Pipe to stdin"));
372 xclose (parent_pipe
[PWRITE
]);
374 /* Check if we need a grandchild tar. This happens only if either:
375 a) we are writing stdout: to force reblocking;
376 b) the file is to be accessed by rmt: compressor doesn't know how;
377 c) the file is not a plain file. */
379 if (strcmp (archive_name_array
[0], "-") != 0
380 && !_remdev (archive_name_array
[0])
381 && is_regular_file (archive_name_array
[0]))
384 maybe_backup_file (archive_name_array
[0], 1);
386 /* We don't need a grandchild tar. Open the archive and launch the
389 archive
= creat (archive_name_array
[0], 0666);
392 int saved_errno
= errno
;
396 FATAL_ERROR ((0, saved_errno
, _("Cannot open archive %s"),
397 archive_name_array
[0]));
399 xdup2 (archive
, STDOUT
, _("Archive to stdout"));
400 execlp (use_compress_program_option
, use_compress_program_option
,
402 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
403 use_compress_program_option
));
406 /* We do need a grandchild tar. */
408 if (pipe (child_pipe
) < 0)
409 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
411 grandchild_pid
= fork ();
412 if (grandchild_pid
< 0)
413 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
415 if (grandchild_pid
> 0)
417 /* The child tar is still here! Launch the compressor. */
419 xdup2 (child_pipe
[PWRITE
], STDOUT
, _("((child)) Pipe to stdout"));
420 xclose (child_pipe
[PREAD
]);
421 execlp (use_compress_program_option
, use_compress_program_option
,
423 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
424 use_compress_program_option
));
427 /* The new born grandchild tar is here! */
429 program_name
= _("tar (grandchild)");
431 /* Prepare for reblocking the data from the compressor into the archive. */
433 xdup2 (child_pipe
[PREAD
], STDIN
, _("(grandchild) Pipe to stdin"));
434 xclose (child_pipe
[PWRITE
]);
436 if (strcmp (archive_name_array
[0], "-") == 0)
439 archive
= rmtcreat (archive_name_array
[0], 0666, rsh_command_option
);
441 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
442 archive_name_array
[0]));
444 /* Let's read out of the stdin pipe and write an archive. */
452 /* Assemble a record. */
454 for (length
= 0, cursor
= record_start
->buffer
;
455 length
< record_size
;
456 length
+= status
, cursor
+= status
)
458 size_t size
= record_size
- length
;
460 if (size
< BLOCKSIZE
)
462 status
= read (STDIN
, cursor
, size
);
468 FATAL_ERROR ((0, errno
, _("Cannot read from compression program")));
470 /* Copy the record. */
474 /* We hit the end of the file. Write last record at
475 full length, as the only role of the grandchild is
476 doing proper reblocking. */
480 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
481 status
= write_archive_buffer ();
482 if (status
!= record_size
)
483 write_error (status
);
486 /* There is nothing else to read, break out. */
490 status
= write_archive_buffer ();
491 if (status
!= record_size
)
492 write_error (status
);
501 /*---------------------------------------------------------.
502 | Set ARCHIVE for uncompressing, then reading an archive. |
503 `---------------------------------------------------------*/
506 child_open_for_uncompress (void)
510 pid_t grandchild_pid
;
512 if (pipe (parent_pipe
) < 0)
513 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
517 FATAL_ERROR ((0, errno
, _("Cannot fork")));
521 /* The parent tar is still here! Just clean up. */
523 read_full_records_option
= 1;
524 archive
= parent_pipe
[PREAD
];
525 xclose (parent_pipe
[PWRITE
]);
529 /* The new born child tar is here! */
531 program_name
= _("tar (child)");
533 xdup2 (parent_pipe
[PWRITE
], STDOUT
, _("(child) Pipe to stdout"));
534 xclose (parent_pipe
[PREAD
]);
536 /* Check if we need a grandchild tar. This happens only if either:
537 a) we're reading stdin: to force unblocking;
538 b) the file is to be accessed by rmt: compressor doesn't know how;
539 c) the file is not a plain file. */
541 if (strcmp (archive_name_array
[0], "-") != 0
542 && !_remdev (archive_name_array
[0])
543 && is_regular_file (archive_name_array
[0]))
545 /* We don't need a grandchild tar. Open the archive and lauch the
548 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, 0666);
550 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
551 archive_name_array
[0]));
552 xdup2 (archive
, STDIN
, _("Archive to stdin"));
553 execlp (use_compress_program_option
, use_compress_program_option
,
555 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
556 use_compress_program_option
));
559 /* We do need a grandchild tar. */
561 if (pipe (child_pipe
) < 0)
562 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
564 grandchild_pid
= fork ();
565 if (grandchild_pid
< 0)
566 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
568 if (grandchild_pid
> 0)
570 /* The child tar is still here! Launch the uncompressor. */
572 xdup2 (child_pipe
[PREAD
], STDIN
, _("((child)) Pipe to stdin"));
573 xclose (child_pipe
[PWRITE
]);
574 execlp (use_compress_program_option
, use_compress_program_option
,
576 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
577 use_compress_program_option
));
580 /* The new born grandchild tar is here! */
582 program_name
= _("tar (grandchild)");
584 /* Prepare for unblocking the data from the archive into the uncompressor. */
586 xdup2 (child_pipe
[PWRITE
], STDOUT
, _("(grandchild) Pipe to stdout"));
587 xclose (child_pipe
[PREAD
]);
589 if (strcmp (archive_name_array
[0], "-") == 0)
592 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
593 0666, rsh_command_option
);
595 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
596 archive_name_array
[0]));
598 /* Let's read the archive and pipe it into stdout. */
607 read_error_count
= 0;
610 status
= rmtread (archive
, record_start
->buffer
, record_size
);
618 cursor
= record_start
->buffer
;
622 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
623 status
= write (STDOUT
, cursor
, count
);
625 FATAL_ERROR ((0, errno
, _("\
626 Cannot write to compression program")));
631 Write to compression program short %lu bytes"),
632 (unsigned long) (count
- status
)));
647 #endif /* not MSDOS */
649 /*--------------------------------------------------------------------------.
650 | Check the LABEL block against the volume label, seen as a globbing |
651 | pattern. Return true if the pattern matches. In case of failure, retry |
652 | matching a volume sequence number before giving up in multi-volume mode. |
653 `--------------------------------------------------------------------------*/
656 check_label_pattern (union block
*label
)
661 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
664 if (!multi_volume_option
)
667 string
= xmalloc (strlen (volume_label_option
)
668 + sizeof VOLUME_LABEL_APPEND
+ 1);
669 strcpy (string
, volume_label_option
);
670 strcat (string
, VOLUME_LABEL_APPEND
);
671 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
676 /*------------------------------------------------------------------------.
677 | Open an archive file. The argument specifies whether we are reading or |
678 | writing, or both. |
679 `------------------------------------------------------------------------*/
682 open_archive (enum access_mode access
)
684 int backed_up_flag
= 0;
686 stdlis
= to_stdout_option
? stderr
: stdout
;
688 if (record_size
== 0)
689 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
691 if (archive_names
== 0)
692 FATAL_ERROR ((0, 0, _("No archive name given")));
694 current_file_name
= NULL
;
695 current_link_name
= NULL
;
697 /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
698 constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
699 size that is reasonable to allocate a buffer. In the GNU system, there
700 is no fixed limit. The only correct thing to do is to use dynamic
701 allocation. (Roland McGrath) */
704 real_s_name
= (char *) xmalloc (PATH_MAX
);
705 /* FIXME: real_s_name is never freed. */
709 if (multi_volume_option
)
712 = (union block
*) valloc (record_size
+ (2 * BLOCKSIZE
));
717 record_start
= (union block
*) valloc (record_size
);
719 FATAL_ERROR ((0, 0, _("Could not allocate memory for blocking factor %d"),
722 current_block
= record_start
;
723 record_end
= record_start
+ blocking_factor
;
724 /* When updating the archive, we start with reading. */
725 access_mode
= access
== ACCESS_UPDATE
? ACCESS_READ
: access
;
727 if (multi_volume_option
&& verify_option
)
728 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
730 if (use_compress_program_option
)
732 if (multi_volume_option
)
733 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
735 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
740 child_open_for_uncompress ();
744 child_open_for_compress ();
748 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
752 if (access
== ACCESS_WRITE
&& strcmp (archive_name_array
[0], "-") == 0)
755 else if (strcmp (archive_name_array
[0], "-") == 0)
757 read_full_records_option
= 1; /* could be a pipe, be safe */
759 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
775 write_archive_to_stdout
= 1;
779 else if (verify_option
)
780 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
781 0666, rsh_command_option
);
786 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
, 0666,
793 maybe_backup_file (archive_name_array
[0], 1);
796 archive
= rmtcreat (archive_name_array
[0], 0666, rsh_command_option
);
800 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
801 0666, rsh_command_option
);
806 || (! _isrmt (archive
) && fstat (archive
, &archive_stat
) < 0))
808 int saved_errno
= errno
;
812 FATAL_ERROR ((0, saved_errno
, _("Cannot open %s"),
813 archive_name_array
[0]));
818 /* Detect if outputting to "/dev/null". */
820 static char const dev_null
[] = "/dev/null";
821 struct stat dev_null_stat
;
824 (strcmp (archive_name_array
[0], dev_null
) == 0
825 || (! _isrmt (archive
)
826 && stat (dev_null
, &dev_null_stat
) == 0
827 && S_ISCHR (archive_stat
.st_mode
)
828 && archive_stat
.st_rdev
== dev_null_stat
.st_rdev
));
831 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
833 ar_dev
= archive_stat
.st_dev
;
834 ar_ino
= archive_stat
.st_ino
;
839 #endif /* not MSDOS */
842 setmode (archive
, O_BINARY
);
849 record_end
= record_start
; /* set up for 1st record = # 0 */
850 find_next_block (); /* read it in, check for EOF */
852 if (volume_label_option
)
854 union block
*label
= find_next_block ();
857 FATAL_ERROR ((0, 0, _("Archive not labelled to match `%s'"),
858 volume_label_option
));
859 if (!check_label_pattern (label
))
860 FATAL_ERROR ((0, 0, _("Volume `%s' does not match `%s'"),
861 label
->header
.name
, volume_label_option
));
866 if (volume_label_option
)
868 memset ((void *) record_start
, 0, BLOCKSIZE
);
869 if (multi_volume_option
)
870 sprintf (record_start
->header
.name
, "%s Volume 1",
871 volume_label_option
);
873 strcpy (record_start
->header
.name
, volume_label_option
);
875 assign_string (¤t_file_name
, record_start
->header
.name
);
877 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
878 TIME_TO_OCT (time (0), record_start
->header
.mtime
);
879 finish_header (record_start
);
888 /*--------------------------------------.
889 | Perform a write to flush the buffer. |
890 `--------------------------------------*/
898 if (checkpoint_option
&& !(++checkpoint
% 10))
899 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
901 if (!zerop_tarlong (tape_length_option
)
902 && !lessp_tarlong (bytes_written
, tape_length_option
))
904 errno
= ENOSPC
; /* FIXME: errno should be read-only */
907 else if (dev_null_output
)
908 status
= record_size
;
910 status
= write_archive_buffer ();
911 if (status
!= record_size
&& !multi_volume_option
)
912 write_error (status
);
913 else if (totals_option
)
914 add_to_tarlong (total_written
, record_size
);
917 add_to_tarlong (bytes_written
, status
);
919 if (status
== record_size
)
921 if (multi_volume_option
)
927 real_s_name
[0] = '\0';
935 if (cursor
[1] == ':')
938 while (*cursor
== '/')
941 strcpy (real_s_name
, cursor
);
942 real_s_totsize
= save_totsize
;
943 real_s_sizeleft
= save_sizeleft
;
948 /* We're multivol. Panic if we didn't get the right kind of response. */
950 /* ENXIO is for the UNIX PC. */
951 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
952 write_error (status
);
954 /* If error indicates a short write, we just move to the next tape. */
956 if (!new_volume (ACCESS_WRITE
))
959 clear_tarlong (bytes_written
);
961 if (volume_label_option
&& real_s_name
[0])
966 else if (volume_label_option
|| real_s_name
[0])
974 if (volume_label_option
)
976 memset ((void *) record_start
, 0, BLOCKSIZE
);
977 sprintf (record_start
->header
.name
, "%s Volume %d", volume_label_option
, volno
);
978 TIME_TO_OCT (time (0), record_start
->header
.mtime
);
979 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
980 finish_header (record_start
);
987 if (volume_label_option
)
990 memset ((void *) record_start
, 0, BLOCKSIZE
);
992 /* FIXME: Michael P Urban writes: [a long name file] is being written
993 when a new volume rolls around [...] Looks like the wrong value is
994 being preserved in real_s_name, though. */
996 strcpy (record_start
->header
.name
, real_s_name
);
997 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
998 OFF_TO_OCT (real_s_sizeleft
, record_start
->header
.size
);
999 OFF_TO_OCT (real_s_totsize
- real_s_sizeleft
,
1000 record_start
->oldgnu_header
.offset
);
1001 tmp
= verbose_option
;
1003 finish_header (record_start
);
1004 verbose_option
= tmp
;
1006 if (volume_label_option
)
1010 status
= write_archive_buffer ();
1011 if (status
!= record_size
)
1012 write_error (status
);
1013 else if (totals_option
)
1014 add_to_tarlong (total_written
, record_size
);
1016 add_to_tarlong (bytes_written
, record_size
);
1019 record_start
+= copy_back
;
1020 memcpy ((void *) current_block
,
1021 (void *) (record_start
+ blocking_factor
- copy_back
),
1022 (size_t) (copy_back
* BLOCKSIZE
));
1023 current_block
+= copy_back
;
1025 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
1026 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
1027 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
1028 real_s_name
[0] = '\0';
1031 char *cursor
= save_name
;
1034 if (cursor
[1] == ':')
1037 while (*cursor
== '/')
1040 strcpy (real_s_name
, cursor
);
1041 real_s_sizeleft
= save_sizeleft
;
1042 real_s_totsize
= save_totsize
;
1048 /*---------------------------------------------------------------------.
1049 | Handle write errors on the archive. Write errors are always fatal. |
1050 | Hitting the end of a volume does not cause a write error unless the |
1051 | write was the first record of the volume. |
1052 `---------------------------------------------------------------------*/
1055 write_error (ssize_t status
)
1057 int saved_errno
= errno
;
1059 /* It might be useful to know how much was written before the error
1060 occured. Beware that mere printing maybe change errno value. */
1062 print_total_written ();
1065 FATAL_ERROR ((0, saved_errno
, _("Cannot write to %s"),
1066 *archive_name_cursor
));
1068 FATAL_ERROR ((0, 0, _("Only wrote %lu of %lu bytes to %s"),
1069 (unsigned long) status
, (unsigned long) record_size
,
1070 *archive_name_cursor
));
1073 /*-------------------------------------------------------------------.
1074 | Handle read errors on the archive. If the read should be retried, |
1075 | returns to the caller. |
1076 `-------------------------------------------------------------------*/
1081 WARN ((0, errno
, _("Read error on %s"), *archive_name_cursor
));
1083 if (record_start_block
== 0)
1084 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1086 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1087 then give up on reading the archive. */
1089 if (read_error_count
++ > READ_ERROR_MAX
)
1090 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1094 /*-------------------------------------.
1095 | Perform a read to flush the buffer. |
1096 `-------------------------------------*/
1101 ssize_t status
; /* result from system call */
1102 size_t left
; /* bytes left */
1103 char *more
; /* pointer to next byte to read */
1105 if (checkpoint_option
&& !(++checkpoint
% 10))
1106 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1108 /* Clear the count of errors. This only applies to a single call to
1111 read_error_count
= 0; /* clear error count */
1113 if (write_archive_to_stdout
&& record_start_block
!= 0)
1115 status
= write_archive_buffer ();
1116 if (status
!= record_size
)
1117 write_error (status
);
1119 if (multi_volume_option
)
1122 char *cursor
= save_name
;
1125 if (cursor
[1] == ':')
1128 while (*cursor
== '/')
1131 strcpy (real_s_name
, cursor
);
1132 real_s_sizeleft
= save_sizeleft
;
1133 real_s_totsize
= save_totsize
;
1137 real_s_name
[0] = '\0';
1139 real_s_sizeleft
= 0;
1143 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1144 if (status
== record_size
)
1148 || (status
< 0 && errno
== ENOSPC
)
1149 || (status
> 0 && !read_full_records_option
))
1150 && multi_volume_option
)
1152 union block
*cursor
;
1155 switch (subcommand_option
)
1157 case APPEND_SUBCOMMAND
:
1158 case CAT_SUBCOMMAND
:
1159 case UPDATE_SUBCOMMAND
:
1160 if (!new_volume (ACCESS_UPDATE
))
1165 if (!new_volume (ACCESS_READ
))
1171 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1177 if (status
!= record_size
)
1180 cursor
= record_start
;
1182 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1184 if (volume_label_option
)
1186 if (!check_label_pattern (cursor
))
1188 WARN ((0, 0, _("Volume `%s' does not match `%s'"),
1189 cursor
->header
.name
, volume_label_option
));
1196 fprintf (stdlis
, _("Reading %s\n"), cursor
->header
.name
);
1199 else if (volume_label_option
)
1200 WARN ((0, 0, _("WARNING: No volume header")));
1205 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1206 || strcmp (cursor
->header
.name
, real_s_name
))
1208 WARN ((0, 0, _("%s is not continued on this volume"),
1214 s1
= UINTMAX_FROM_OCT (cursor
->header
.size
);
1215 s2
= UINTMAX_FROM_OCT (cursor
->oldgnu_header
.offset
);
1216 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
1218 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1219 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1220 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1222 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1223 cursor
->header
.name
,
1224 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1225 STRINGIFY_BIGINT (s1
, s1buf
),
1226 STRINGIFY_BIGINT (s2
, s2buf
)));
1231 if (real_s_totsize
- real_s_sizeleft
1232 != OFF_FROM_OCT (cursor
->oldgnu_header
.offset
))
1234 WARN ((0, 0, _("This volume is out of sequence")));
1241 current_block
= cursor
;
1244 else if (status
< 0)
1247 goto error_loop
; /* try again */
1251 more
= record_start
->buffer
+ status
;
1252 left
= record_size
- status
;
1255 if (left
% BLOCKSIZE
== 0)
1257 /* FIXME: for size=0, multi-volume support. On the first record, warn
1258 about the problem. */
1260 if (!read_full_records_option
&& verbose_option
1261 && record_start_block
== 0 && status
> 0)
1262 WARN ((0, 0, _("Record size = %lu blocks"),
1263 (unsigned long) (status
/ BLOCKSIZE
)));
1265 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
1269 if (read_full_records_option
)
1271 /* User warned us about this. Fix up. */
1276 status
= rmtread (archive
, more
, left
);
1280 goto error2loop
; /* try again */
1283 FATAL_ERROR ((0, 0, _("Archive %s EOF not on block boundary"),
1284 *archive_name_cursor
));
1291 FATAL_ERROR ((0, 0, _("Only read %lu bytes from archive %s"),
1292 (unsigned long) status
, *archive_name_cursor
));
1295 /*-----------------------------------------------.
1296 | Flush the current buffer to/from the archive. |
1297 `-----------------------------------------------*/
1300 flush_archive (void)
1302 record_start_block
+= record_end
- record_start
;
1303 current_block
= record_start
;
1304 record_end
= record_start
+ blocking_factor
;
1306 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1308 access_mode
= ACCESS_WRITE
;
1309 time_to_start_writing
= 0;
1311 if (file_to_switch_to
>= 0)
1313 int status
= rmtclose (archive
);
1316 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1317 *archive_name_cursor
, archive
, status
));
1319 archive
= file_to_switch_to
;
1322 backspace_output ();
1325 switch (access_mode
)
1340 /*-------------------------------------------------------------------------.
1341 | Backspace the archive descriptor by one record worth. If its a tape, |
1342 | MTIOCTOP will work. If its something else, we try to seek on it. If we |
1343 | can't seek, we lose! |
1344 `-------------------------------------------------------------------------*/
1347 backspace_output (void)
1351 struct mtop operation
;
1353 operation
.mt_op
= MTBSR
;
1354 operation
.mt_count
= 1;
1355 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1357 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1363 off_t position
= rmtlseek (archive
, (off_t
) 0, 1);
1365 /* Seek back to the beginning of this record and start writing there. */
1367 position
-= record_size
;
1368 if (rmtlseek (archive
, position
, 0) != position
)
1370 /* Lseek failed. Try a different method. */
1373 Could not backspace archive file; it may be unreadable without -i")));
1375 /* Replace the first part of the record with NULs. */
1377 if (record_start
->buffer
!= output_start
)
1378 memset (record_start
->buffer
, 0,
1379 (size_t) (output_start
- record_start
->buffer
));
1384 /*-------------------------.
1385 | Close the archive file. |
1386 `-------------------------*/
1389 close_archive (void)
1391 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1396 /* Manage to fully drain a pipe we might be reading, so to not break it on
1397 the producer after the EOF block. FIXME: one of these days, GNU tar
1398 might become clever enough to just stop working, once there is no more
1399 work to do, we might have to revise this area in such time. */
1401 if (access_mode
== ACCESS_READ
1402 && ! _isrmt (archive
)
1403 && S_ISFIFO (archive_stat
.st_mode
)
1404 && ! ending_file_option
)
1405 while (rmtread (archive
, record_start
->buffer
, record_size
) > 0)
1409 if (! _isrmt (archive
) && subcommand_option
== DELETE_SUBCOMMAND
)
1412 int status
= write (archive
, "", 0);
1414 off_t pos
= lseek (archive
, (off_t
) 0, 1);
1415 int status
= pos
== -1 ? -1 : ftruncate (archive
, pos
);
1418 WARN ((0, errno
, _("WARNING: Cannot truncate %s"),
1419 *archive_name_cursor
));
1425 int status
= rmtclose (archive
);
1428 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1429 *archive_name_cursor
, archive
, status
));
1439 /* Loop waiting for the right child to die, or for no more kids. */
1441 while ((child
= wait (&wait_status
), child
!= child_pid
)
1446 if (WIFSIGNALED (wait_status
)
1448 && !WIFSTOPPED (wait_status
)
1452 /* SIGPIPE is OK, everything else is a problem. */
1454 if (WTERMSIG (wait_status
) != SIGPIPE
)
1455 ERROR ((0, 0, _("Child died with signal %d%s"),
1456 WTERMSIG (wait_status
),
1457 WCOREDUMP (wait_status
) ? _(" (core dumped)") : ""));
1461 /* Child voluntarily terminated -- but why? /bin/sh returns
1462 SIGPIPE + 128 if its child, then do nothing. */
1464 if (WEXITSTATUS (wait_status
) != (SIGPIPE
+ 128)
1465 && WEXITSTATUS (wait_status
))
1466 ERROR ((0, 0, _("Child returned status %d"),
1467 WEXITSTATUS (wait_status
)));
1472 if (current_file_name
)
1473 free (current_file_name
);
1474 if (current_link_name
)
1475 free (current_link_name
);
1478 free (multi_volume_option
? record_start
- 2 : record_start
);
1481 /*------------------------------------------------.
1482 | Called to initialize the global volume number. |
1483 `------------------------------------------------*/
1486 init_volume_number (void)
1488 FILE *file
= fopen (volno_file_option
, "r");
1492 fscanf (file
, "%d", &global_volno
);
1493 if (fclose (file
) == EOF
)
1494 ERROR ((0, errno
, "%s", volno_file_option
));
1496 else if (errno
!= ENOENT
)
1497 ERROR ((0, errno
, "%s", volno_file_option
));
1500 /*-------------------------------------------------------.
1501 | Called to write out the closing global volume number. |
1502 `-------------------------------------------------------*/
1505 closeout_volume_number (void)
1507 FILE *file
= fopen (volno_file_option
, "w");
1511 fprintf (file
, "%d\n", global_volno
);
1512 if (fclose (file
) == EOF
)
1513 ERROR ((0, errno
, "%s", volno_file_option
));
1516 ERROR ((0, errno
, "%s", volno_file_option
));
1519 /*-----------------------------------------------------------------------.
1520 | We've hit the end of the old volume. Close it and open the next one. |
1521 | Return nonzero on success. |
1522 `-----------------------------------------------------------------------*/
1525 new_volume (enum access_mode access
)
1527 static FILE *read_file
= NULL
;
1528 static int looped
= 0;
1532 if (!read_file
&& !info_script_option
)
1533 /* FIXME: if fopen is used, it will never be closed. */
1534 read_file
= archive
== STDIN
? fopen (TTY_NAME
, "r") : stdin
;
1541 if (status
= rmtclose (archive
), status
< 0)
1542 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1543 *archive_name_cursor
, archive
, status
));
1547 archive_name_cursor
++;
1548 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1550 archive_name_cursor
= archive_name_array
;
1557 /* We have to prompt from now on. */
1559 if (info_script_option
)
1561 if (volno_file_option
)
1562 closeout_volume_number ();
1563 system (info_script_option
);
1568 char input_buffer
[80];
1571 _("\007Prepare volume #%d for %s and hit return: "),
1572 global_volno
, *archive_name_cursor
);
1575 if (fgets (input_buffer
, sizeof (input_buffer
), read_file
) == 0)
1577 fprintf (stderr
, _("EOF where user reply was expected"));
1579 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1580 && subcommand_option
!= LIST_SUBCOMMAND
1581 && subcommand_option
!= DIFF_SUBCOMMAND
)
1582 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1584 exit (TAREXIT_FAILURE
);
1586 if (input_buffer
[0] == '\n'
1587 || input_buffer
[0] == 'y'
1588 || input_buffer
[0] == 'Y')
1591 switch (input_buffer
[0])
1595 fprintf (stderr
, _("\
1596 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1598 ! Spawn a subshell\n\
1599 ? Print this list\n"));
1606 fprintf (stdlis
, _("No new volume; exiting.\n"));
1608 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1609 && subcommand_option
!= LIST_SUBCOMMAND
1610 && subcommand_option
!= DIFF_SUBCOMMAND
)
1611 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1613 exit (TAREXIT_FAILURE
);
1616 /* Get new file name. */
1619 char *name
= &input_buffer
[1];
1622 while (*name
== ' ' || *name
== '\t')
1625 while (*cursor
&& *cursor
!= '\n')
1629 /* FIXME: the following allocation is never reclaimed. */
1630 *archive_name_cursor
= xstrdup (name
);
1636 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1637 #else /* not MSDOS */
1641 WARN ((0, errno
, _("Cannot fork!")));
1646 const char *shell
= getenv ("SHELL");
1650 execlp (shell
, "-sh", "-i", 0);
1651 FATAL_ERROR ((0, errno
, _("Cannot exec a shell %s"),
1659 wait (&wait_status
);
1664 /* FIXME: I'm not sure if that's all that has to be done
1667 #endif /* not MSDOS */
1674 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, 0666,
1675 rsh_command_option
);
1680 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, 0666,
1681 rsh_command_option
);
1686 maybe_backup_file (*archive_name_cursor
, 1);
1687 archive
= rmtcreat (*archive_name_cursor
, 0666, rsh_command_option
);
1691 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, 0666,
1692 rsh_command_option
);
1698 WARN ((0, errno
, _("Cannot open %s"), *archive_name_cursor
));
1699 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1700 undo_last_backup ();
1705 setmode (archive
, O_BINARY
);