1 /* Buffer management for tar.
2 Copyright 1988, 92, 93, 94, 96, 97, 1999 Free Software Foundation, Inc.
3 Written by John Gilmore, on 1985-08-25.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Enable GNU extensions in fnmatch.h. */
21 # define _GNU_SOURCE 1
37 # include <sys/inode.h>
46 #define DEBUG_FORK 0 /* if nonzero, childs are born stopped */
48 #define PREAD 0 /* read file descriptor from pipe() */
49 #define PWRITE 1 /* write file descriptor from pipe() */
51 /* Number of retries before giving up on read. */
52 #define READ_ERROR_MAX 10
54 /* Globbing pattern to append to volume label if initial match failed. */
55 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
59 static tarlong prev_written
; /* bytes written on previous volumes */
60 static tarlong bytes_written
; /* bytes written on this volume */
62 /* FIXME: The following four variables should ideally be static to this
63 module. However, this cannot be done yet, as update.c uses the first
64 three a lot, and compare.c uses the fourth. The cleanup continues! */
66 union block
*record_start
; /* start of record of archive */
67 union block
*record_end
; /* last+1 block of archive record */
68 union block
*current_block
; /* current block of archive */
69 enum access_mode access_mode
; /* how do we handle the archive */
70 static struct stat archive_stat
; /* stat block for archive file */
72 static off_t record_start_block
; /* block ordinal at record_start */
74 /* Where we write list messages (not errors, not interactions) to. Stdout
75 unless we're writing a pipe, in which case stderr. */
78 static void backspace_output
PARAMS ((void));
79 static int new_volume
PARAMS ((enum access_mode
));
80 static void write_error
PARAMS ((ssize_t
));
81 static void read_error
PARAMS ((void));
84 /* Obnoxious test to see if dimwit is trying to dump the archive. */
89 /* PID of child program, if compress_option or remote archive access. */
90 static pid_t child_pid
;
92 /* Error recovery stuff */
93 static int read_error_count
;
95 /* Have we hit EOF yet? */
98 /* Checkpointing counter */
99 static int checkpoint
;
101 /* We're reading, but we just read the last block and its time to update. */
102 /* As least EXTERN like this one as possible. FIXME! */
103 extern int time_to_start_writing
;
105 int file_to_switch_to
= -1; /* if remote update, close archive, and use
106 this descriptor to write to */
108 static int volno
= 1; /* which volume of a multi-volume tape we're
110 static int global_volno
= 1; /* volume number to print in external
113 /* The pointer save_name, which is set in function dump_file() of module
114 create.c, points to the original long filename instead of the new,
115 shorter mangled name that is set in start_header() of module create.c.
116 The pointer save_name is only used in multi-volume mode when the file
117 being processed is non-sparse; if a file is split between volumes, the
118 save_name is used in generating the LF_MULTIVOL record on the second
119 volume. (From Pierce Cantrell, 1991-08-13.) */
121 char *save_name
; /* name of the file we are currently writing */
122 off_t save_totsize
; /* total size of file we are writing, only
123 valid if save_name is nonzero */
124 off_t save_sizeleft
; /* where we are in the file we are writing,
125 only valid if save_name is nonzero */
127 int write_archive_to_stdout
;
129 /* Used by flush_read and flush_write to store the real info about saved
131 static char *real_s_name
;
132 static off_t real_s_totsize
;
133 static off_t real_s_sizeleft
;
142 pid_t result
= fork ();
145 kill (getpid (), SIGSTOP
);
151 #endif /* DEBUG FORK */
154 print_total_written (void)
156 tarlong written
= prev_written
+ bytes_written
;
157 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
158 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
159 char rate
[LONGEST_HUMAN_READABLE
+ 1];
162 #if HAVE_CLOCK_GETTIME
164 if (clock_gettime (CLOCK_REALTIME
, &now
) == 0)
165 seconds
= ((now
.tv_sec
- start_timespec
.tv_sec
)
166 + (now
.tv_nsec
- start_timespec
.tv_nsec
) / 1e9
);
169 seconds
= time (0) - start_time
;
171 sprintf (bytes
, TARLONG_FORMAT
, written
);
172 fprintf (stderr
, _("Total written: %s bytes (%sB, %sB/s)\n"), bytes
,
173 human_readable ((uintmax_t) written
, abbr
, 1, -1024),
174 (0 < seconds
&& written
/ seconds
< (uintmax_t) -1
175 ? human_readable ((uintmax_t) (written
/ seconds
), rate
, 1, -1024)
179 /*--------------------------------------------------------.
180 | Compute and return the block ordinal at current_block. |
181 `--------------------------------------------------------*/
184 current_block_ordinal (void)
186 return record_start_block
+ (current_block
- record_start
);
189 /*------------------------------------------------------------------.
190 | If the EOF flag is set, reset it, as well as current_block, etc. |
191 `------------------------------------------------------------------*/
199 current_block
= record_start
;
200 record_end
= record_start
+ blocking_factor
;
201 access_mode
= ACCESS_WRITE
;
205 /*-------------------------------------------------------------------------.
206 | Return the location of the next available input or output block. |
207 | Return zero for EOF. Once we have returned zero, we just keep returning |
208 | it, to avoid accidentally going on to the next file on the tape. |
209 `-------------------------------------------------------------------------*/
212 find_next_block (void)
214 if (current_block
== record_end
)
219 if (current_block
== record_end
)
225 return current_block
;
228 /*------------------------------------------------------.
229 | Indicate that we have used all blocks up thru BLOCK. |
231 | FIXME: should the arg have an off-by-1? |
232 `------------------------------------------------------*/
235 set_next_block_after (union block
*block
)
237 while (block
>= current_block
)
240 /* Do *not* flush the archive here. If we do, the same argument to
241 set_next_block_after could mean the next block (if the input record
242 is exactly one block long), which is not what is intended. */
244 if (current_block
> record_end
)
248 /*------------------------------------------------------------------------.
249 | Return the number of bytes comprising the space between POINTER through |
250 | the end of the current buffer of blocks. This space is available for |
251 | filling with data, or taking data from. POINTER is usually (but not |
252 | always) the result previous find_next_block call. |
253 `------------------------------------------------------------------------*/
256 available_space_after (union block
*pointer
)
258 return record_end
->buffer
- pointer
->buffer
;
261 /*------------------------------------------------------------------.
262 | Close file having descriptor FD, and abort if close unsuccessful. |
263 `------------------------------------------------------------------*/
269 FATAL_ERROR ((0, errno
, _("Cannot close file #%d"), fd
));
272 /*-----------------------------------------------------------------------.
273 | Duplicate file descriptor FROM into becoming INTO, or else, issue |
274 | MESSAGE. INTO is closed first and has to be the next available slot. |
275 `-----------------------------------------------------------------------*/
278 xdup2 (int from
, int into
, const char *message
)
282 int status
= close (into
);
284 if (status
< 0 && errno
!= EBADF
)
285 FATAL_ERROR ((0, errno
, _("Cannot close descriptor %d"), into
));
288 FATAL_ERROR ((0, errno
, _("Cannot properly duplicate %s"), message
));
295 /*-------------------------------------------------------.
296 | Set ARCHIVE for writing, then compressing an archive. |
297 `-------------------------------------------------------*/
300 child_open_for_compress (void)
302 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
305 /*---------------------------------------------------------.
306 | Set ARCHIVE for uncompressing, then reading an archive. |
307 `---------------------------------------------------------*/
310 child_open_for_uncompress (void)
312 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
315 #else /* not MSDOS */
317 /*---------------------------------------------------------------------.
318 | Return nonzero if NAME is the name of a regular file, or if the file |
319 | does not exist (so it would be created as a regular file). |
320 `---------------------------------------------------------------------*/
323 is_regular_file (const char *name
)
327 if (stat (name
, &stbuf
) == 0)
328 return S_ISREG (stbuf
.st_mode
);
330 return errno
== ENOENT
;
334 write_archive_buffer (void)
339 while (0 <= (status
= rmtwrite (archive
, record_start
->buffer
+ written
,
340 record_size
- written
)))
343 if (written
== record_size
344 || _isrmt (archive
) || ! S_ISFIFO (archive_stat
.st_mode
))
348 return written
? written
: status
;
351 /*-------------------------------------------------------.
352 | Set ARCHIVE for writing, then compressing an archive. |
353 `-------------------------------------------------------*/
356 child_open_for_compress (void)
360 pid_t grandchild_pid
;
362 if (pipe (parent_pipe
) < 0)
363 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
367 FATAL_ERROR ((0, errno
, _("Cannot fork")));
371 /* The parent tar is still here! Just clean up. */
373 archive
= parent_pipe
[PWRITE
];
374 xclose (parent_pipe
[PREAD
]);
378 /* The new born child tar is here! */
380 program_name
= _("tar (child)");
382 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
, _("(child) Pipe to stdin"));
383 xclose (parent_pipe
[PWRITE
]);
385 /* Check if we need a grandchild tar. This happens only if either:
386 a) we are writing stdout: to force reblocking;
387 b) the file is to be accessed by rmt: compressor doesn't know how;
388 c) the file is not a plain file. */
390 if (strcmp (archive_name_array
[0], "-") != 0
391 && !_remdev (archive_name_array
[0])
392 && is_regular_file (archive_name_array
[0]))
395 maybe_backup_file (archive_name_array
[0], 1);
397 /* We don't need a grandchild tar. Open the archive and launch the
400 archive
= creat (archive_name_array
[0], MODE_RW
);
403 int saved_errno
= errno
;
407 FATAL_ERROR ((0, saved_errno
, _("Cannot open archive %s"),
408 archive_name_array
[0]));
410 xdup2 (archive
, STDOUT_FILENO
, _("Archive to stdout"));
411 execlp (use_compress_program_option
, use_compress_program_option
,
413 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
414 use_compress_program_option
));
417 /* We do need a grandchild tar. */
419 if (pipe (child_pipe
) < 0)
420 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
422 grandchild_pid
= fork ();
423 if (grandchild_pid
< 0)
424 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
426 if (grandchild_pid
> 0)
428 /* The child tar is still here! Launch the compressor. */
430 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
,
431 _("((child)) Pipe to stdout"));
432 xclose (child_pipe
[PREAD
]);
433 execlp (use_compress_program_option
, use_compress_program_option
,
435 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
436 use_compress_program_option
));
439 /* The new born grandchild tar is here! */
441 program_name
= _("tar (grandchild)");
443 /* Prepare for reblocking the data from the compressor into the archive. */
445 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("(grandchild) Pipe to stdin"));
446 xclose (child_pipe
[PWRITE
]);
448 if (strcmp (archive_name_array
[0], "-") == 0)
449 archive
= STDOUT_FILENO
;
451 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
453 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
454 archive_name_array
[0]));
456 /* Let's read out of the stdin pipe and write an archive. */
464 /* Assemble a record. */
466 for (length
= 0, cursor
= record_start
->buffer
;
467 length
< record_size
;
468 length
+= status
, cursor
+= status
)
470 size_t size
= record_size
- length
;
472 if (size
< BLOCKSIZE
)
474 status
= safe_read (STDIN_FILENO
, cursor
, size
);
480 FATAL_ERROR ((0, errno
, _("Cannot read from compression program")));
482 /* Copy the record. */
486 /* We hit the end of the file. Write last record at
487 full length, as the only role of the grandchild is
488 doing proper reblocking. */
492 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
493 status
= write_archive_buffer ();
494 if (status
!= record_size
)
495 write_error (status
);
498 /* There is nothing else to read, break out. */
502 status
= write_archive_buffer ();
503 if (status
!= record_size
)
504 write_error (status
);
513 /*---------------------------------------------------------.
514 | Set ARCHIVE for uncompressing, then reading an archive. |
515 `---------------------------------------------------------*/
518 child_open_for_uncompress (void)
522 pid_t grandchild_pid
;
524 if (pipe (parent_pipe
) < 0)
525 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
529 FATAL_ERROR ((0, errno
, _("Cannot fork")));
533 /* The parent tar is still here! Just clean up. */
535 read_full_records_option
= 1;
536 archive
= parent_pipe
[PREAD
];
537 xclose (parent_pipe
[PWRITE
]);
541 /* The new born child tar is here! */
543 program_name
= _("tar (child)");
545 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
, _("(child) Pipe to stdout"));
546 xclose (parent_pipe
[PREAD
]);
548 /* Check if we need a grandchild tar. This happens only if either:
549 a) we're reading stdin: to force unblocking;
550 b) the file is to be accessed by rmt: compressor doesn't know how;
551 c) the file is not a plain file. */
553 if (strcmp (archive_name_array
[0], "-") != 0
554 && !_remdev (archive_name_array
[0])
555 && is_regular_file (archive_name_array
[0]))
557 /* We don't need a grandchild tar. Open the archive and lauch the
560 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
562 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
563 archive_name_array
[0]));
564 xdup2 (archive
, STDIN_FILENO
, _("Archive to stdin"));
565 execlp (use_compress_program_option
, use_compress_program_option
,
567 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
568 use_compress_program_option
));
571 /* We do need a grandchild tar. */
573 if (pipe (child_pipe
) < 0)
574 FATAL_ERROR ((0, errno
, _("Cannot open pipe")));
576 grandchild_pid
= fork ();
577 if (grandchild_pid
< 0)
578 FATAL_ERROR ((0, errno
, _("Child cannot fork")));
580 if (grandchild_pid
> 0)
582 /* The child tar is still here! Launch the uncompressor. */
584 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
, _("((child)) Pipe to stdin"));
585 xclose (child_pipe
[PWRITE
]);
586 execlp (use_compress_program_option
, use_compress_program_option
,
588 FATAL_ERROR ((0, errno
, _("Cannot exec %s"),
589 use_compress_program_option
));
592 /* The new born grandchild tar is here! */
594 program_name
= _("tar (grandchild)");
596 /* Prepare for unblocking the data from the archive into the uncompressor. */
598 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
, _("(grandchild) Pipe to stdout"));
599 xclose (child_pipe
[PREAD
]);
601 if (strcmp (archive_name_array
[0], "-") == 0)
602 archive
= STDIN_FILENO
;
604 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
605 MODE_RW
, rsh_command_option
);
607 FATAL_ERROR ((0, errno
, _("Cannot open archive %s"),
608 archive_name_array
[0]));
610 /* Let's read the archive and pipe it into stdout. */
619 read_error_count
= 0;
622 status
= rmtread (archive
, record_start
->buffer
, record_size
);
630 cursor
= record_start
->buffer
;
634 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
635 status
= full_write (STDOUT_FILENO
, cursor
, count
);
637 FATAL_ERROR ((0, errno
, _("Cannot write to compression program")));
641 ERROR ((0, 0, _("Write to compression program short %lu bytes"),
642 (unsigned long) (count
- status
)));
657 #endif /* not MSDOS */
659 /*--------------------------------------------------------------------------.
660 | Check the LABEL block against the volume label, seen as a globbing |
661 | pattern. Return true if the pattern matches. In case of failure, retry |
662 | matching a volume sequence number before giving up in multi-volume mode. |
663 `--------------------------------------------------------------------------*/
666 check_label_pattern (union block
*label
)
671 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
674 if (!multi_volume_option
)
677 string
= xmalloc (strlen (volume_label_option
)
678 + sizeof VOLUME_LABEL_APPEND
+ 1);
679 strcpy (string
, volume_label_option
);
680 strcat (string
, VOLUME_LABEL_APPEND
);
681 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
686 /*------------------------------------------------------------------------.
687 | Open an archive file. The argument specifies whether we are reading or |
688 | writing, or both. |
689 `------------------------------------------------------------------------*/
692 open_archive (enum access_mode access
)
694 int backed_up_flag
= 0;
696 stdlis
= to_stdout_option
? stderr
: stdout
;
698 if (record_size
== 0)
699 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
701 if (archive_names
== 0)
702 FATAL_ERROR ((0, 0, _("No archive name given")));
704 current_file_name
= 0;
705 current_link_name
= 0;
707 /* FIXME: According to POSIX.1, PATH_MAX may well not be a compile-time
708 constant, and the value from sysconf (_SC_PATH_MAX) may well not be any
709 size that is reasonable to allocate a buffer. In the GNU system, there
710 is no fixed limit. The only correct thing to do is to use dynamic
711 allocation. (Roland McGrath) */
714 real_s_name
= xmalloc (PATH_MAX
);
715 /* FIXME: real_s_name is never freed. */
719 if (multi_volume_option
)
721 record_start
= valloc (record_size
+ (2 * BLOCKSIZE
));
726 record_start
= valloc (record_size
);
728 FATAL_ERROR ((0, 0, _("Could not allocate memory for blocking factor %d"),
731 current_block
= record_start
;
732 record_end
= record_start
+ blocking_factor
;
733 /* When updating the archive, we start with reading. */
734 access_mode
= access
== ACCESS_UPDATE
? ACCESS_READ
: access
;
736 if (multi_volume_option
&& verify_option
)
737 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
739 if (use_compress_program_option
)
741 if (multi_volume_option
)
742 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
744 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
749 child_open_for_uncompress ();
753 child_open_for_compress ();
757 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
761 if (access
== ACCESS_WRITE
&& strcmp (archive_name_array
[0], "-") == 0)
764 else if (strcmp (archive_name_array
[0], "-") == 0)
766 read_full_records_option
= 1; /* could be a pipe, be safe */
768 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
773 archive
= STDIN_FILENO
;
777 archive
= STDOUT_FILENO
;
782 archive
= STDIN_FILENO
;
784 write_archive_to_stdout
= 1;
788 else if (verify_option
)
789 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
790 MODE_RW
, rsh_command_option
);
795 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
796 MODE_RW
, rsh_command_option
);
802 maybe_backup_file (archive_name_array
[0], 1);
805 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
810 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
811 MODE_RW
, rsh_command_option
);
816 || (! _isrmt (archive
) && fstat (archive
, &archive_stat
) < 0))
818 int saved_errno
= errno
;
822 FATAL_ERROR ((0, saved_errno
, "%s", archive_name_array
[0]));
827 /* Detect if outputting to "/dev/null". */
829 static char const dev_null
[] = "/dev/null";
830 struct stat dev_null_stat
;
833 (strcmp (archive_name_array
[0], dev_null
) == 0
834 || (! _isrmt (archive
)
835 && S_ISCHR (archive_stat
.st_mode
)
836 && stat (dev_null
, &dev_null_stat
) == 0
837 && S_ISCHR (dev_null_stat
.st_mode
)
838 && archive_stat
.st_rdev
== dev_null_stat
.st_rdev
));
841 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
843 ar_dev
= archive_stat
.st_dev
;
844 ar_ino
= archive_stat
.st_ino
;
849 #endif /* not MSDOS */
852 setmode (archive
, O_BINARY
);
859 record_end
= record_start
; /* set up for 1st record = # 0 */
860 find_next_block (); /* read it in, check for EOF */
862 if (volume_label_option
)
864 union block
*label
= find_next_block ();
867 FATAL_ERROR ((0, 0, _("Archive not labeled to match `%s'"),
868 volume_label_option
));
869 if (!check_label_pattern (label
))
870 FATAL_ERROR ((0, 0, _("Volume `%s' does not match `%s'"),
871 label
->header
.name
, volume_label_option
));
876 if (volume_label_option
)
878 memset (record_start
, 0, BLOCKSIZE
);
879 if (multi_volume_option
)
880 sprintf (record_start
->header
.name
, "%s Volume 1",
881 volume_label_option
);
883 strcpy (record_start
->header
.name
, volume_label_option
);
885 assign_string (¤t_file_name
, record_start
->header
.name
);
887 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
888 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
889 finish_header (record_start
);
898 /*--------------------------------------.
899 | Perform a write to flush the buffer. |
900 `--------------------------------------*/
908 if (checkpoint_option
&& !(++checkpoint
% 10))
909 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
911 if (tape_length_option
&& tape_length_option
<= bytes_written
)
913 errno
= ENOSPC
; /* FIXME: errno should be read-only */
916 else if (dev_null_output
)
917 status
= record_size
;
919 status
= write_archive_buffer ();
920 if (status
!= record_size
&& !multi_volume_option
)
921 write_error (status
);
924 bytes_written
+= status
;
926 if (status
== record_size
)
928 if (multi_volume_option
)
934 real_s_name
[0] = '\0';
940 cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
941 while (*cursor
== '/')
944 strcpy (real_s_name
, cursor
);
945 real_s_totsize
= save_totsize
;
946 real_s_sizeleft
= save_sizeleft
;
951 /* We're multivol. Panic if we didn't get the right kind of response. */
953 /* ENXIO is for the UNIX PC. */
954 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
955 write_error (status
);
957 /* If error indicates a short write, we just move to the next tape. */
959 if (!new_volume (ACCESS_WRITE
))
963 prev_written
+= bytes_written
;
966 if (volume_label_option
&& real_s_name
[0])
971 else if (volume_label_option
|| real_s_name
[0])
979 if (volume_label_option
)
981 memset (record_start
, 0, BLOCKSIZE
);
982 sprintf (record_start
->header
.name
, "%s Volume %d", volume_label_option
, volno
);
983 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
984 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
985 finish_header (record_start
);
992 if (volume_label_option
)
995 memset (record_start
, 0, BLOCKSIZE
);
997 /* FIXME: Michael P Urban writes: [a long name file] is being written
998 when a new volume rolls around [...] Looks like the wrong value is
999 being preserved in real_s_name, though. */
1001 strcpy (record_start
->header
.name
, real_s_name
);
1002 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1003 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
1004 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1005 record_start
->oldgnu_header
.offset
);
1006 tmp
= verbose_option
;
1008 finish_header (record_start
);
1009 verbose_option
= tmp
;
1011 if (volume_label_option
)
1015 status
= write_archive_buffer ();
1016 if (status
!= record_size
)
1017 write_error (status
);
1019 bytes_written
+= status
;
1023 record_start
+= copy_back
;
1024 memcpy (current_block
,
1025 record_start
+ blocking_factor
- copy_back
,
1026 copy_back
* BLOCKSIZE
);
1027 current_block
+= copy_back
;
1029 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
1030 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
1031 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
1032 real_s_name
[0] = '\0';
1035 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
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
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
)
1123 char *cursor
= save_name
+ FILESYSTEM_PREFIX_LEN (save_name
);
1125 while (*cursor
== '/')
1128 strcpy (real_s_name
, cursor
);
1129 real_s_sizeleft
= save_sizeleft
;
1130 real_s_totsize
= save_totsize
;
1134 real_s_name
[0] = '\0';
1136 real_s_sizeleft
= 0;
1141 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1142 if (status
== record_size
)
1146 || (status
< 0 && errno
== ENOSPC
)
1147 || (status
> 0 && !read_full_records_option
))
1148 && multi_volume_option
)
1150 union block
*cursor
;
1153 switch (subcommand_option
)
1155 case APPEND_SUBCOMMAND
:
1156 case CAT_SUBCOMMAND
:
1157 case UPDATE_SUBCOMMAND
:
1158 if (!new_volume (ACCESS_UPDATE
))
1163 if (!new_volume (ACCESS_READ
))
1169 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1175 if (status
!= record_size
)
1178 cursor
= record_start
;
1180 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
1182 if (volume_label_option
)
1184 if (!check_label_pattern (cursor
))
1186 WARN ((0, 0, _("Volume `%s' does not match `%s'"),
1187 cursor
->header
.name
, volume_label_option
));
1194 fprintf (stdlis
, _("Reading %s\n"), cursor
->header
.name
);
1197 else if (volume_label_option
)
1198 WARN ((0, 0, _("WARNING: No volume header")));
1203 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
1204 || strcmp (cursor
->header
.name
, real_s_name
))
1206 WARN ((0, 0, _("%s is not continued on this volume"),
1212 s1
= UINTMAX_FROM_HEADER (cursor
->header
.size
);
1213 s2
= UINTMAX_FROM_HEADER (cursor
->oldgnu_header
.offset
);
1214 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
1216 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1217 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1218 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1220 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1221 cursor
->header
.name
,
1222 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1223 STRINGIFY_BIGINT (s1
, s1buf
),
1224 STRINGIFY_BIGINT (s2
, s2buf
)));
1229 if (real_s_totsize
- real_s_sizeleft
1230 != OFF_FROM_HEADER (cursor
->oldgnu_header
.offset
))
1232 WARN ((0, 0, _("This volume is out of sequence")));
1239 current_block
= cursor
;
1242 else if (status
< 0)
1245 goto error_loop
; /* try again */
1249 more
= record_start
->buffer
+ status
;
1250 left
= record_size
- status
;
1252 while (left
% BLOCKSIZE
!= 0)
1254 while ((status
= rmtread (archive
, more
, left
)) < 0)
1259 ERROR ((0, 0, _("%d garbage bytes ignored at end of archive"),
1260 (int) ((record_size
- left
) % BLOCKSIZE
)));
1264 if (! read_full_records_option
)
1265 FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
1266 (unsigned long) (record_size
- left
)));
1268 /* User warned us about this. Fix up. */
1274 /* FIXME: for size=0, multi-volume support. On the first record, warn
1275 about the problem. */
1277 if (!read_full_records_option
&& verbose_option
1278 && record_start_block
== 0 && status
> 0)
1279 WARN ((0, 0, _("Record size = %lu blocks"),
1280 (unsigned long) ((record_size
- left
) / BLOCKSIZE
)));
1282 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
1285 /*-----------------------------------------------.
1286 | Flush the current buffer to/from the archive. |
1287 `-----------------------------------------------*/
1290 flush_archive (void)
1292 record_start_block
+= record_end
- record_start
;
1293 current_block
= record_start
;
1294 record_end
= record_start
+ blocking_factor
;
1296 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
1298 access_mode
= ACCESS_WRITE
;
1299 time_to_start_writing
= 0;
1301 if (file_to_switch_to
>= 0)
1303 int status
= rmtclose (archive
);
1306 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1307 *archive_name_cursor
, archive
, status
));
1309 archive
= file_to_switch_to
;
1312 backspace_output ();
1315 switch (access_mode
)
1330 /*-------------------------------------------------------------------------.
1331 | Backspace the archive descriptor by one record worth. If its a tape, |
1332 | MTIOCTOP will work. If its something else, we try to seek on it. If we |
1333 | can't seek, we lose! |
1334 `-------------------------------------------------------------------------*/
1337 backspace_output (void)
1341 struct mtop operation
;
1343 operation
.mt_op
= MTBSR
;
1344 operation
.mt_count
= 1;
1345 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1347 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1353 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1355 /* Seek back to the beginning of this record and start writing there. */
1357 position
-= record_size
;
1358 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1360 /* Lseek failed. Try a different method. */
1363 _("Could not backspace archive file; it may be unreadable without -i")));
1365 /* Replace the first part of the record with NULs. */
1367 if (record_start
->buffer
!= output_start
)
1368 memset (record_start
->buffer
, 0,
1369 output_start
- record_start
->buffer
);
1374 /*-------------------------.
1375 | Close the archive file. |
1376 `-------------------------*/
1379 close_archive (void)
1381 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1386 /* Manage to fully drain a pipe we might be reading, so to not break it on
1387 the producer after the EOF block. FIXME: one of these days, GNU tar
1388 might become clever enough to just stop working, once there is no more
1389 work to do, we might have to revise this area in such time. */
1391 if (access_mode
== ACCESS_READ
1392 && ! _isrmt (archive
)
1393 && S_ISFIFO (archive_stat
.st_mode
))
1394 while (rmtread (archive
, record_start
->buffer
, record_size
) > 0)
1398 if (! _isrmt (archive
) && subcommand_option
== DELETE_SUBCOMMAND
)
1401 int status
= write (archive
, "", 0);
1403 off_t pos
= lseek (archive
, (off_t
) 0, SEEK_CUR
);
1404 int status
= pos
< 0 ? -1 : ftruncate (archive
, pos
);
1407 WARN ((0, errno
, _("WARNING: Cannot truncate %s"),
1408 *archive_name_cursor
));
1414 int status
= rmtclose (archive
);
1417 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1418 *archive_name_cursor
, archive
, status
));
1427 while (waitpid (child_pid
, &wait_status
, 0) == -1)
1430 ERROR ((0, errno
, _("While waiting for child")));
1434 if (WIFSIGNALED (wait_status
))
1436 /* SIGPIPE is OK, everything else is a problem. */
1438 if (WTERMSIG (wait_status
) != SIGPIPE
)
1439 ERROR ((0, 0, _("Child died with signal %d"),
1440 WTERMSIG (wait_status
)));
1444 /* Child voluntarily terminated -- but why? /bin/sh returns
1445 SIGPIPE + 128 if its child, then do nothing. */
1447 if (WEXITSTATUS (wait_status
)
1448 && WEXITSTATUS (wait_status
) != (SIGPIPE
+ 128))
1449 ERROR ((0, 0, _("Child returned status %d"),
1450 WEXITSTATUS (wait_status
)));
1455 if (current_file_name
)
1456 free (current_file_name
);
1457 if (current_link_name
)
1458 free (current_link_name
);
1461 free (multi_volume_option
? record_start
- 2 : record_start
);
1464 /*------------------------------------------------.
1465 | Called to initialize the global volume number. |
1466 `------------------------------------------------*/
1469 init_volume_number (void)
1471 FILE *file
= fopen (volno_file_option
, "r");
1475 fscanf (file
, "%d", &global_volno
);
1476 if (fclose (file
) == EOF
)
1477 ERROR ((0, errno
, "%s", volno_file_option
));
1479 else if (errno
!= ENOENT
)
1480 ERROR ((0, errno
, "%s", volno_file_option
));
1483 /*-------------------------------------------------------.
1484 | Called to write out the closing global volume number. |
1485 `-------------------------------------------------------*/
1488 closeout_volume_number (void)
1490 FILE *file
= fopen (volno_file_option
, "w");
1494 fprintf (file
, "%d\n", global_volno
);
1495 if (fclose (file
) == EOF
)
1496 ERROR ((0, errno
, "%s", volno_file_option
));
1499 ERROR ((0, errno
, "%s", volno_file_option
));
1502 /*-----------------------------------------------------------------------.
1503 | We've hit the end of the old volume. Close it and open the next one. |
1504 | Return nonzero on success. |
1505 `-----------------------------------------------------------------------*/
1508 new_volume (enum access_mode access
)
1510 static FILE *read_file
;
1515 if (!read_file
&& !info_script_option
)
1516 /* FIXME: if fopen is used, it will never be closed. */
1517 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1524 if (status
= rmtclose (archive
), status
< 0)
1525 WARN ((0, errno
, _("WARNING: Cannot close %s (%d, %d)"),
1526 *archive_name_cursor
, archive
, status
));
1530 archive_name_cursor
++;
1531 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1533 archive_name_cursor
= archive_name_array
;
1540 /* We have to prompt from now on. */
1542 if (info_script_option
)
1544 if (volno_file_option
)
1545 closeout_volume_number ();
1546 system (info_script_option
);
1551 char input_buffer
[80];
1553 fputc ('\007', stderr
);
1555 _("Prepare volume #%d for %s and hit return: "),
1556 global_volno
, *archive_name_cursor
);
1559 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
1561 WARN ((0, 0, _("EOF where user reply was expected")));
1563 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1564 && subcommand_option
!= LIST_SUBCOMMAND
1565 && subcommand_option
!= DIFF_SUBCOMMAND
)
1566 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1568 apply_delayed_set_stat ();
1569 exit (TAREXIT_FAILURE
);
1571 if (input_buffer
[0] == '\n'
1572 || input_buffer
[0] == 'y'
1573 || input_buffer
[0] == 'Y')
1576 switch (input_buffer
[0])
1580 fprintf (stderr
, _("\
1581 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1583 ! Spawn a subshell\n\
1584 ? Print this list\n"));
1591 WARN ((0, 0, _("No new volume; exiting.\n")));
1593 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1594 && subcommand_option
!= LIST_SUBCOMMAND
1595 && subcommand_option
!= DIFF_SUBCOMMAND
)
1596 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1598 apply_delayed_set_stat ();
1599 exit (TAREXIT_FAILURE
);
1602 /* Get new file name. */
1605 char *name
= &input_buffer
[1];
1608 while (*name
== ' ' || *name
== '\t')
1611 while (*cursor
&& *cursor
!= '\n')
1615 /* FIXME: the following allocation is never reclaimed. */
1616 *archive_name_cursor
= xstrdup (name
);
1622 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
1623 #else /* not MSDOS */
1625 pid_t child
= fork ();
1629 WARN ((0, errno
, _("Cannot fork!")));
1634 const char *shell
= getenv ("SHELL");
1638 execlp (shell
, "-sh", "-i", 0);
1639 FATAL_ERROR ((0, errno
, _("Cannot exec a shell %s"),
1646 while (waitpid (child
, &wait_status
, 0) == -1)
1650 _("While waiting for child")));
1657 #endif /* not MSDOS */
1664 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1665 rsh_command_option
);
1670 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1671 rsh_command_option
);
1676 maybe_backup_file (*archive_name_cursor
, 1);
1677 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1678 rsh_command_option
);
1682 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1683 rsh_command_option
);
1689 WARN ((0, errno
, _("Cannot open %s"), *archive_name_cursor
));
1690 if (!verify_option
&& access
== ACCESS_WRITE
&& backup_option
)
1691 undo_last_backup ();
1696 setmode (archive
, O_BINARY
);