1 /* Buffer management for tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-08-25.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 /* Number of retries before giving up on read. */
34 #define READ_ERROR_MAX 10
36 /* Globbing pattern to append to volume label if initial match failed. */
37 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
41 static tarlong prev_written
; /* bytes written on previous volumes */
42 static tarlong bytes_written
; /* bytes written on this volume */
43 static void *record_buffer
; /* allocated memory */
45 /* FIXME: The following variables should ideally be static to this
46 module. However, this cannot be done yet. The cleanup continues! */
48 union block
*record_start
; /* start of record of archive */
49 union block
*record_end
; /* last+1 block of archive record */
50 union block
*current_block
; /* current block of archive */
51 enum access_mode access_mode
; /* how do we handle the archive */
52 off_t records_read
; /* number of records read from this archive */
53 off_t records_written
; /* likewise, for records written */
55 static off_t record_start_block
; /* block ordinal at record_start */
57 /* Where we write list messages (not errors, not interactions) to. */
60 static void backspace_output (void);
61 static bool new_volume (enum access_mode
);
63 /* PID of child program, if compress_option or remote archive access. */
64 static pid_t child_pid
;
66 /* Error recovery stuff */
67 static int read_error_count
;
69 /* Have we hit EOF yet? */
72 /* Checkpointing counter */
73 static int checkpoint
;
75 static bool read_full_records
= false;
76 static bool reading_from_pipe
= false;
78 /* We're reading, but we just read the last block and it's time to update.
81 As least EXTERN like this one as possible. (?? --gray)
82 FIXME: Either eliminate it or move it to common.h.
84 extern bool time_to_start_writing
;
86 static int volno
= 1; /* which volume of a multi-volume tape we're
88 static int global_volno
= 1; /* volume number to print in external
91 /* The pointer save_name, which is set in function dump_file() of module
92 create.c, points to the original long filename instead of the new,
93 shorter mangled name that is set in start_header() of module create.c.
94 The pointer save_name is only used in multi-volume mode when the file
95 being processed is non-sparse; if a file is split between volumes, the
96 save_name is used in generating the LF_MULTIVOL record on the second
97 volume. (From Pierce Cantrell, 1991-08-13.) */
99 char *save_name
; /* name of the file we are currently writing */
100 off_t save_totsize
; /* total size of file we are writing, only
101 valid if save_name is nonzero */
102 off_t save_sizeleft
; /* where we are in the file we are writing,
103 only valid if save_name is nonzero */
105 bool write_archive_to_stdout
;
107 /* Used by flush_read and flush_write to store the real info about saved
109 static char *real_s_name
;
110 static off_t real_s_totsize
;
111 static off_t real_s_sizeleft
;
116 clear_read_error_count (void)
118 read_error_count
= 0;
122 /* Time-related functions */
129 #if HAVE_CLOCK_GETTIME
130 if (clock_gettime (CLOCK_REALTIME
, &start_timespec
) != 0)
132 start_time
= time (0);
138 #if HAVE_CLOCK_GETTIME
140 if (clock_gettime (CLOCK_REALTIME
, &now
) == 0)
141 duration
+= ((now
.tv_sec
- start_timespec
.tv_sec
)
142 + (now
.tv_nsec
- start_timespec
.tv_nsec
) / 1e9
);
145 duration
+= time (NULL
) - start_time
;
150 /* Compression detection */
161 enum compress_type type
;
162 unsigned char *magic
;
168 static struct zip_magic magic
[] = {
170 { ct_compress
, "\037\235", 2, "compress", "-Z" },
171 { ct_gzip
, "\037\213", 2, "gzip", "-z" },
172 { ct_bzip2
, "BZh", 3, "bzip2", "-j" },
175 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
177 #define compress_option(t) magic[t].option
178 #define compress_program(t) magic[t].program
180 /* Check if the file ARCHIVE is a compressed archive. */
182 check_compressed_archive ()
188 /* Prepare global data needed for find_next_block: */
189 record_end
= record_start
; /* set up for 1st record = # 0 */
190 sfr
= read_full_records
;
191 read_full_records
= true; /* Suppress fatal error on reading a partial
193 srp
= reading_from_pipe
;
194 reading_from_pipe
= true; /* Suppress warning message on reading a partial
198 /* Restore global values */
199 read_full_records
= sfr
;
200 reading_from_pipe
= srp
;
202 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
203 /* Probably a valid header */
206 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
207 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
213 /* Open an archive named archive_name_array[0]. Detect if it is
214 a compressed archive of known type and use corresponding decompression
217 open_compressed_archive ()
219 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
220 MODE_RW
, rsh_command_option
);
224 if (!multi_volume_option
)
226 enum compress_type type
= check_compressed_archive ();
231 /* FD is not needed any more */
234 hit_eof
= false; /* It might have been set by find_next_block in
235 check_compressed_archive */
237 /* Open compressed archive */
238 use_compress_program_option
= compress_program (type
);
239 child_pid
= sys_child_open_for_uncompress ();
240 read_full_records
= reading_from_pipe
= true;
244 record_end
= record_start
; /* set up for 1st record = # 0 */
251 print_total_written (void)
253 tarlong written
= prev_written
+ bytes_written
;
254 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
255 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
256 char rate
[LONGEST_HUMAN_READABLE
+ 1];
258 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
260 sprintf (bytes
, TARLONG_FORMAT
, written
);
262 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
263 fprintf (stderr
, _("Total bytes written: %s (%s, %s/s)\n"), bytes
,
264 human_readable (written
, abbr
, human_opts
, 1, 1),
265 (0 < duration
&& written
/ duration
< (uintmax_t) -1
266 ? human_readable (written
/ duration
, rate
, human_opts
, 1, 1)
270 /* Compute and return the block ordinal at current_block. */
272 current_block_ordinal (void)
274 return record_start_block
+ (current_block
- record_start
);
277 /* If the EOF flag is set, reset it, as well as current_block, etc. */
284 current_block
= record_start
;
285 record_end
= record_start
+ blocking_factor
;
286 access_mode
= ACCESS_WRITE
;
290 /* Return the location of the next available input or output block.
291 Return zero for EOF. Once we have returned zero, we just keep returning
292 it, to avoid accidentally going on to the next file on the tape. */
294 find_next_block (void)
296 if (current_block
== record_end
)
301 if (current_block
== record_end
)
307 return current_block
;
310 /* Indicate that we have used all blocks up thru BLOCK. */
312 set_next_block_after (union block
*block
)
314 while (block
>= current_block
)
317 /* Do *not* flush the archive here. If we do, the same argument to
318 set_next_block_after could mean the next block (if the input record
319 is exactly one block long), which is not what is intended. */
321 if (current_block
> record_end
)
325 /* Return the number of bytes comprising the space between POINTER
326 through the end of the current buffer of blocks. This space is
327 available for filling with data, or taking data from. POINTER is
328 usually (but not always) the result of previous find_next_block call. */
330 available_space_after (union block
*pointer
)
332 return record_end
->buffer
- pointer
->buffer
;
335 /* Close file having descriptor FD, and abort if close unsuccessful. */
340 close_error (_("(pipe)"));
343 /* Check the LABEL block against the volume label, seen as a globbing
344 pattern. Return true if the pattern matches. In case of failure,
345 retry matching a volume sequence number before giving up in
346 multi-volume mode. */
348 check_label_pattern (union block
*label
)
353 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
356 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
359 if (!multi_volume_option
)
362 string
= xmalloc (strlen (volume_label_option
)
363 + sizeof VOLUME_LABEL_APPEND
+ 1);
364 strcpy (string
, volume_label_option
);
365 strcat (string
, VOLUME_LABEL_APPEND
);
366 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
371 /* Open an archive file. The argument specifies whether we are
372 reading or writing, or both. */
374 open_archive (enum access_mode wanted_access
)
376 int backed_up_flag
= 0;
380 stdlis
= fopen (index_file_name
, "w");
382 open_error (index_file_name
);
385 stdlis
= to_stdout_option
? stderr
: stdout
;
387 if (record_size
== 0)
388 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
390 if (archive_names
== 0)
391 FATAL_ERROR ((0, 0, _("No archive name given")));
393 tar_stat_destroy (¤t_stat_info
);
398 page_aligned_alloc (&record_buffer
,
400 + (multi_volume_option
? 2 * BLOCKSIZE
: 0)));
401 if (multi_volume_option
)
404 current_block
= record_start
;
405 record_end
= record_start
+ blocking_factor
;
406 /* When updating the archive, we start with reading. */
407 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
409 read_full_records
= read_full_records_option
;
410 reading_from_pipe
= false;
414 if (use_compress_program_option
)
416 switch (wanted_access
)
419 child_pid
= sys_child_open_for_uncompress ();
420 read_full_records
= reading_from_pipe
= true;
421 record_end
= record_start
; /* set up for 1st record = # 0 */
425 child_pid
= sys_child_open_for_compress ();
429 abort (); /* Should not happen */
433 if (wanted_access
== ACCESS_WRITE
434 && strcmp (archive_name_array
[0], "-") == 0)
437 else if (strcmp (archive_name_array
[0], "-") == 0)
439 read_full_records
= true; /* could be a pipe, be safe */
441 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
443 switch (wanted_access
)
447 enum compress_type type
;
449 archive
= STDIN_FILENO
;
451 type
= check_compressed_archive (archive
);
454 _("Archive is compressed. Use %s option"),
455 compress_option (type
)));
460 archive
= STDOUT_FILENO
;
465 archive
= STDIN_FILENO
;
467 write_archive_to_stdout
= true;
471 else if (verify_option
)
472 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
473 MODE_RW
, rsh_command_option
);
475 switch (wanted_access
)
478 archive
= open_compressed_archive ();
484 maybe_backup_file (archive_name_array
[0], 1);
487 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
492 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
493 MODE_RW
, rsh_command_option
);
498 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
500 int saved_errno
= errno
;
505 open_fatal (archive_name_array
[0]);
508 sys_detect_dev_null_output ();
509 sys_save_archive_dev_ino ();
510 SET_BINARY_MODE (archive
);
512 switch (wanted_access
)
516 record_end
= record_start
; /* set up for 1st record = # 0 */
519 find_next_block (); /* read it in, check for EOF */
521 if (volume_label_option
)
523 union block
*label
= find_next_block ();
526 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
527 quote (volume_label_option
)));
528 if (!check_label_pattern (label
))
529 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
530 quote_n (0, label
->header
.name
),
531 quote_n (1, volume_label_option
)));
537 if (volume_label_option
)
539 memset (record_start
, 0, BLOCKSIZE
);
540 if (multi_volume_option
)
541 sprintf (record_start
->header
.name
, "%s Volume 1",
542 volume_label_option
);
544 strcpy (record_start
->header
.name
, volume_label_option
);
546 assign_string (¤t_stat_info
.file_name
,
547 record_start
->header
.name
);
548 current_stat_info
.had_trailing_slash
=
549 strip_trailing_slashes (current_stat_info
.file_name
);
551 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
552 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
553 finish_header (¤t_stat_info
, record_start
, -1);
559 /* Perform a write to flush the buffer. */
566 if (checkpoint_option
&& !(++checkpoint
% 10))
567 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
569 if (tape_length_option
&& tape_length_option
<= bytes_written
)
574 else if (dev_null_output
)
575 status
= record_size
;
577 status
= sys_write_archive_buffer ();
578 if (status
!= record_size
&& !multi_volume_option
)
579 archive_write_error (status
);
584 bytes_written
+= status
;
587 if (status
== record_size
)
589 if (multi_volume_option
)
593 assign_string (&real_s_name
, safer_name_suffix (save_name
, false));
594 real_s_totsize
= save_totsize
;
595 real_s_sizeleft
= save_sizeleft
;
599 assign_string (&real_s_name
, 0);
607 /* We're multivol. Panic if we didn't get the right kind of response. */
609 /* ENXIO is for the UNIX PC. */
610 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
611 archive_write_error (status
);
613 /* If error indicates a short write, we just move to the next tape. */
615 if (!new_volume (ACCESS_WRITE
))
619 prev_written
+= bytes_written
;
622 if (volume_label_option
&& real_s_name
)
627 else if (volume_label_option
|| real_s_name
)
635 if (volume_label_option
)
637 memset (record_start
, 0, BLOCKSIZE
);
638 sprintf (record_start
->header
.name
, "%s Volume %d",
639 volume_label_option
, volno
);
640 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
641 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
642 finish_header (¤t_stat_info
, record_start
, -1);
649 if (volume_label_option
)
652 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
654 _("%s: file name too long to be stored in a GNU multivolume header"),
655 quotearg_colon (real_s_name
)));
657 memset (record_start
, 0, BLOCKSIZE
);
659 /* FIXME: Michael P Urban writes: [a long name file] is being written
660 when a new volume rolls around [...] Looks like the wrong value is
661 being preserved in real_s_name, though. */
663 strncpy (record_start
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
664 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
666 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
667 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
668 record_start
->oldgnu_header
.offset
);
670 tmp
= verbose_option
;
672 finish_header (¤t_stat_info
, record_start
, -1);
673 verbose_option
= tmp
;
675 if (volume_label_option
)
679 status
= sys_write_archive_buffer ();
680 if (status
!= record_size
)
681 archive_write_error (status
);
683 bytes_written
+= status
;
687 record_start
+= copy_back
;
688 memcpy (current_block
,
689 record_start
+ blocking_factor
- copy_back
,
690 copy_back
* BLOCKSIZE
);
691 current_block
+= copy_back
;
693 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
694 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
695 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
696 assign_string (&real_s_name
, 0);
699 assign_string (&real_s_name
, safer_name_suffix (save_name
, false));
700 real_s_sizeleft
= save_sizeleft
;
701 real_s_totsize
= save_totsize
;
707 /* Handle write errors on the archive. Write errors are always fatal.
708 Hitting the end of a volume does not cause a write error unless the
709 write was the first record of the volume. */
711 archive_write_error (ssize_t status
)
713 /* It might be useful to know how much was written before the error
718 print_total_written ();
722 write_fatal_details (*archive_name_cursor
, status
, record_size
);
725 /* Handle read errors on the archive. If the read should be retried,
726 return to the caller. */
728 archive_read_error (void)
730 read_error (*archive_name_cursor
);
732 if (record_start_block
== 0)
733 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
735 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
736 then give up on reading the archive. */
738 if (read_error_count
++ > READ_ERROR_MAX
)
739 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
744 short_read (size_t status
)
746 size_t left
; /* bytes left */
747 char *more
; /* pointer to next byte to read */
749 more
= record_start
->buffer
+ status
;
750 left
= record_size
- status
;
752 while (left
% BLOCKSIZE
!= 0
753 || (left
&& status
&& read_full_records
))
756 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
757 archive_read_error ();
761 if (!reading_from_pipe
)
763 char buf
[UINTMAX_STRSIZE_BOUND
];
765 WARN((0, 0, _("Read %s bytes from %s"),
766 STRINGIFY_BIGINT (record_size
- left
, buf
),
767 *archive_name_cursor
));
772 if (! read_full_records
)
774 unsigned long rest
= record_size
- left
;
777 ngettext ("Unaligned block (%lu byte) in archive",
778 "Unaligned block (%lu bytes) in archive",
783 /* User warned us about this. Fix up. */
789 /* FIXME: for size=0, multi-volume support. On the first record, warn
790 about the problem. */
792 if (!read_full_records
&& verbose_option
> 1
793 && record_start_block
== 0 && status
!= 0)
795 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
797 ngettext ("Record size = %lu block",
798 "Record size = %lu blocks",
803 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
807 /* Perform a read to flush the buffer. */
811 size_t status
; /* result from system call */
813 if (checkpoint_option
&& !(++checkpoint
% 10))
814 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
816 /* Clear the count of errors. This only applies to a single call to
819 read_error_count
= 0; /* clear error count */
821 if (write_archive_to_stdout
&& record_start_block
!= 0)
823 archive
= STDOUT_FILENO
;
824 status
= sys_write_archive_buffer ();
825 archive
= STDIN_FILENO
;
826 if (status
!= record_size
)
827 archive_write_error (status
);
829 if (multi_volume_option
)
833 assign_string (&real_s_name
, safer_name_suffix (save_name
, false));
834 real_s_sizeleft
= save_sizeleft
;
835 real_s_totsize
= save_totsize
;
839 assign_string (&real_s_name
, 0);
846 status
= rmtread (archive
, record_start
->buffer
, record_size
);
847 if (status
== record_size
)
853 /* The condition below used to include
854 || (status > 0 && !read_full_records)
855 This is incorrect since even if new_volume() succeeds, the
856 subsequent call to rmtread will overwrite the chunk of data
857 already read in the buffer, so the processing will fail */
860 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
861 && multi_volume_option
)
866 switch (subcommand_option
)
868 case APPEND_SUBCOMMAND
:
870 case UPDATE_SUBCOMMAND
:
871 if (!new_volume (ACCESS_UPDATE
))
876 if (!new_volume (ACCESS_READ
))
881 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
883 archive_read_error ();
885 if (status
!= record_size
)
888 cursor
= record_start
;
890 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
892 if (volume_label_option
)
894 if (!check_label_pattern (cursor
))
896 WARN ((0, 0, _("Volume %s does not match %s"),
897 quote_n (0, cursor
->header
.name
),
898 quote_n (1, volume_label_option
)));
905 fprintf (stdlis
, _("Reading %s\n"), quote (cursor
->header
.name
));
908 else if (volume_label_option
)
909 WARN ((0, 0, _("WARNING: No volume header")));
914 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
915 || strncmp (cursor
->header
.name
, real_s_name
, NAME_FIELD_SIZE
))
917 WARN ((0, 0, _("%s is not continued on this volume"),
918 quote (real_s_name
)));
923 s1
= UINTMAX_FROM_HEADER (cursor
->header
.size
);
924 s2
= UINTMAX_FROM_HEADER (cursor
->oldgnu_header
.offset
);
925 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
927 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
928 char s1buf
[UINTMAX_STRSIZE_BOUND
];
929 char s2buf
[UINTMAX_STRSIZE_BOUND
];
931 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
932 quote (cursor
->header
.name
),
933 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
934 STRINGIFY_BIGINT (s1
, s1buf
),
935 STRINGIFY_BIGINT (s2
, s2buf
)));
940 if (real_s_totsize
- real_s_sizeleft
941 != OFF_FROM_HEADER (cursor
->oldgnu_header
.offset
))
943 WARN ((0, 0, _("This volume is out of sequence")));
950 current_block
= cursor
;
954 else if (status
== SAFE_READ_ERROR
)
956 archive_read_error ();
957 goto error_loop
; /* try again */
963 /* Flush the current buffer to/from the archive. */
967 record_start_block
+= record_end
- record_start
;
968 current_block
= record_start
;
969 record_end
= record_start
+ blocking_factor
;
971 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
973 access_mode
= ACCESS_WRITE
;
974 time_to_start_writing
= false;
993 /* Backspace the archive descriptor by one record worth. If it's a
994 tape, MTIOCTOP will work. If it's something else, try to seek on
995 it. If we can't seek, we lose! */
997 backspace_output (void)
1001 struct mtop operation
;
1003 operation
.mt_op
= MTBSR
;
1004 operation
.mt_count
= 1;
1005 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1007 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1013 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1015 /* Seek back to the beginning of this record and start writing there. */
1017 position
-= record_size
;
1020 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1022 /* Lseek failed. Try a different method. */
1025 _("Cannot backspace archive file; it may be unreadable without -i")));
1027 /* Replace the first part of the record with NULs. */
1029 if (record_start
->buffer
!= output_start
)
1030 memset (record_start
->buffer
, 0,
1031 output_start
- record_start
->buffer
);
1037 seek_archive (off_t size
)
1039 off_t start
= current_block_ordinal ();
1042 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
1044 size
-= skipped
* BLOCKSIZE
;
1046 if (size
< record_size
)
1050 /* Compute number of records to skip */
1051 nrec
= size
/ record_size
;
1052 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
1056 if (offset
% record_size
)
1057 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
1059 /* Convert to number of records */
1060 offset
/= BLOCKSIZE
;
1061 /* Compute number of skipped blocks */
1062 nblk
= offset
- start
;
1064 /* Update buffering info */
1065 records_read
+= nblk
/ blocking_factor
;
1066 record_start_block
= offset
- blocking_factor
;
1067 current_block
= record_end
;
1072 /* Close the archive file. */
1074 close_archive (void)
1076 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1079 sys_drain_input_pipe ();
1081 compute_duration ();
1085 if (rmtclose (archive
) != 0)
1086 close_warn (*archive_name_cursor
);
1088 sys_wait_for_child (child_pid
);
1090 tar_stat_destroy (¤t_stat_info
);
1095 free (record_buffer
);
1098 /* Called to initialize the global volume number. */
1100 init_volume_number (void)
1102 FILE *file
= fopen (volno_file_option
, "r");
1106 if (fscanf (file
, "%d", &global_volno
) != 1
1107 || global_volno
< 0)
1108 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1109 quotearg_colon (volno_file_option
)));
1111 read_error (volno_file_option
);
1112 if (fclose (file
) != 0)
1113 close_error (volno_file_option
);
1115 else if (errno
!= ENOENT
)
1116 open_error (volno_file_option
);
1119 /* Called to write out the closing global volume number. */
1121 closeout_volume_number (void)
1123 FILE *file
= fopen (volno_file_option
, "w");
1127 fprintf (file
, "%d\n", global_volno
);
1129 write_error (volno_file_option
);
1130 if (fclose (file
) != 0)
1131 close_error (volno_file_option
);
1134 open_error (volno_file_option
);
1137 /* We've hit the end of the old volume. Close it and open the next one.
1138 Return nonzero on success.
1141 new_volume (enum access_mode mode
)
1143 static FILE *read_file
;
1146 if (!read_file
&& !info_script_option
)
1147 /* FIXME: if fopen is used, it will never be closed. */
1148 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1155 if (rmtclose (archive
) != 0)
1156 close_warn (*archive_name_cursor
);
1159 if (global_volno
< 0)
1160 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1162 archive_name_cursor
++;
1163 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1165 archive_name_cursor
= archive_name_array
;
1172 /* We have to prompt from now on. */
1174 if (info_script_option
)
1176 if (volno_file_option
)
1177 closeout_volume_number ();
1178 if (system (info_script_option
) != 0)
1179 FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option
));
1184 char input_buffer
[80];
1186 fputc ('\007', stderr
);
1188 _("Prepare volume #%d for %s and hit return: "),
1189 global_volno
, quote (*archive_name_cursor
));
1192 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
1194 WARN ((0, 0, _("EOF where user reply was expected")));
1196 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1197 && subcommand_option
!= LIST_SUBCOMMAND
1198 && subcommand_option
!= DIFF_SUBCOMMAND
)
1199 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1203 if (input_buffer
[0] == '\n'
1204 || input_buffer
[0] == 'y'
1205 || input_buffer
[0] == 'Y')
1208 switch (input_buffer
[0])
1212 /* FIXME: Might it be useful to disable the '!' command? */
1213 fprintf (stderr
, _("\
1214 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1216 ! Spawn a subshell\n\
1217 ? Print this list\n"));
1224 WARN ((0, 0, _("No new volume; exiting.\n")));
1226 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1227 && subcommand_option
!= LIST_SUBCOMMAND
1228 && subcommand_option
!= DIFF_SUBCOMMAND
)
1229 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1234 /* Get new file name. */
1237 char *name
= &input_buffer
[1];
1240 for (name
= input_buffer
+ 1;
1241 *name
== ' ' || *name
== '\t';
1245 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1249 /* FIXME: the following allocation is never reclaimed. */
1250 *archive_name_cursor
= xstrdup (name
);
1261 if (strcmp (archive_name_cursor
[0], "-") == 0)
1263 read_full_records
= true;
1264 archive
= STDIN_FILENO
;
1266 else if (verify_option
)
1267 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1268 rsh_command_option
);
1273 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1274 rsh_command_option
);
1279 maybe_backup_file (*archive_name_cursor
, 1);
1280 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1281 rsh_command_option
);
1285 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1286 rsh_command_option
);
1292 open_warn (*archive_name_cursor
);
1293 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1294 undo_last_backup ();
1298 SET_BINARY_MODE (archive
);