1 /* Buffer management for tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005 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 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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;
77 /* We're reading, but we just read the last block and it's time to update.
80 As least EXTERN like this one as possible. (?? --gray)
81 FIXME: Either eliminate it or move it to common.h.
83 extern bool time_to_start_writing
;
85 static int volno
= 1; /* which volume of a multi-volume tape we're
87 static int global_volno
= 1; /* volume number to print in external
90 /* The pointer save_name, which is set in function dump_file() of module
91 create.c, points to the original long filename instead of the new,
92 shorter mangled name that is set in start_header() of module create.c.
93 The pointer save_name is only used in multi-volume mode when the file
94 being processed is non-sparse; if a file is split between volumes, the
95 save_name is used in generating the LF_MULTIVOL record on the second
96 volume. (From Pierce Cantrell, 1991-08-13.) */
98 char *save_name
; /* name of the file we are currently writing */
99 off_t save_totsize
; /* total size of file we are writing, only
100 valid if save_name is nonzero */
101 off_t save_sizeleft
; /* where we are in the file we are writing,
102 only valid if save_name is nonzero */
104 bool write_archive_to_stdout
;
106 /* Used by flush_read and flush_write to store the real info about saved
108 static char *real_s_name
;
109 static off_t real_s_totsize
;
110 static off_t real_s_sizeleft
;
115 clear_read_error_count (void)
117 read_error_count
= 0;
121 /* Time-related functions */
128 gettime (&start_time
);
136 duration
+= ((now
.tv_sec
- start_time
.tv_sec
)
137 + (now
.tv_nsec
- start_time
.tv_nsec
) / 1e9
);
142 /* Compression detection */
153 enum compress_type type
;
160 static struct zip_magic
const magic
[] = {
162 { ct_compress
, 2, "\037\235", "compress", "-Z" },
163 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
164 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
167 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
169 #define compress_option(t) magic[t].option
170 #define compress_program(t) magic[t].program
172 /* Check if the file ARCHIVE is a compressed archive. */
174 check_compressed_archive ()
176 struct zip_magic
const *p
;
179 /* Prepare global data needed for find_next_block: */
180 record_end
= record_start
; /* set up for 1st record = # 0 */
181 sfr
= read_full_records
;
182 read_full_records
= true; /* Suppress fatal error on reading a partial
186 /* Restore global values */
187 read_full_records
= sfr
;
189 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
190 /* Probably a valid header */
193 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
194 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
200 /* Open an archive named archive_name_array[0]. Detect if it is
201 a compressed archive of known type and use corresponding decompression
204 open_compressed_archive ()
206 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
207 MODE_RW
, rsh_command_option
);
211 if (!multi_volume_option
)
213 enum compress_type type
= check_compressed_archive ();
218 /* FD is not needed any more */
221 hit_eof
= false; /* It might have been set by find_next_block in
222 check_compressed_archive */
224 /* Open compressed archive */
225 use_compress_program_option
= compress_program (type
);
226 child_pid
= sys_child_open_for_uncompress ();
227 read_full_records
= true;
231 record_end
= record_start
; /* set up for 1st record = # 0 */
238 print_total_written (void)
240 tarlong written
= prev_written
+ bytes_written
;
241 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
242 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
243 char rate
[LONGEST_HUMAN_READABLE
+ 1];
245 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
247 sprintf (bytes
, TARLONG_FORMAT
, written
);
249 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
250 fprintf (stderr
, _("Total bytes written: %s (%s, %s/s)\n"), bytes
,
251 human_readable (written
, abbr
, human_opts
, 1, 1),
252 (0 < duration
&& written
/ duration
< (uintmax_t) -1
253 ? human_readable (written
/ duration
, rate
, human_opts
, 1, 1)
257 /* Compute and return the block ordinal at current_block. */
259 current_block_ordinal (void)
261 return record_start_block
+ (current_block
- record_start
);
264 /* If the EOF flag is set, reset it, as well as current_block, etc. */
271 current_block
= record_start
;
272 record_end
= record_start
+ blocking_factor
;
273 access_mode
= ACCESS_WRITE
;
277 /* Return the location of the next available input or output block.
278 Return zero for EOF. Once we have returned zero, we just keep returning
279 it, to avoid accidentally going on to the next file on the tape. */
281 find_next_block (void)
283 if (current_block
== record_end
)
288 if (current_block
== record_end
)
294 return current_block
;
297 /* Indicate that we have used all blocks up thru BLOCK. */
299 set_next_block_after (union block
*block
)
301 while (block
>= current_block
)
304 /* Do *not* flush the archive here. If we do, the same argument to
305 set_next_block_after could mean the next block (if the input record
306 is exactly one block long), which is not what is intended. */
308 if (current_block
> record_end
)
312 /* Return the number of bytes comprising the space between POINTER
313 through the end of the current buffer of blocks. This space is
314 available for filling with data, or taking data from. POINTER is
315 usually (but not always) the result of previous find_next_block call. */
317 available_space_after (union block
*pointer
)
319 return record_end
->buffer
- pointer
->buffer
;
322 /* Close file having descriptor FD, and abort if close unsuccessful. */
327 close_error (_("(pipe)"));
330 /* Check the LABEL block against the volume label, seen as a globbing
331 pattern. Return true if the pattern matches. In case of failure,
332 retry matching a volume sequence number before giving up in
333 multi-volume mode. */
335 check_label_pattern (union block
*label
)
340 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
343 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
346 if (!multi_volume_option
)
349 string
= xmalloc (strlen (volume_label_option
)
350 + sizeof VOLUME_LABEL_APPEND
+ 1);
351 strcpy (string
, volume_label_option
);
352 strcat (string
, VOLUME_LABEL_APPEND
);
353 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
358 /* Open an archive file. The argument specifies whether we are
359 reading or writing, or both. */
361 open_archive (enum access_mode wanted_access
)
363 int backed_up_flag
= 0;
367 stdlis
= fopen (index_file_name
, "w");
369 open_error (index_file_name
);
372 stdlis
= to_stdout_option
? stderr
: stdout
;
374 if (record_size
== 0)
375 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
377 if (archive_names
== 0)
378 FATAL_ERROR ((0, 0, _("No archive name given")));
380 tar_stat_destroy (¤t_stat_info
);
385 page_aligned_alloc (&record_buffer
,
387 + (multi_volume_option
? 2 * BLOCKSIZE
: 0)));
388 if (multi_volume_option
)
391 current_block
= record_start
;
392 record_end
= record_start
+ blocking_factor
;
393 /* When updating the archive, we start with reading. */
394 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
396 read_full_records
= read_full_records_option
;
400 if (use_compress_program_option
)
402 switch (wanted_access
)
405 child_pid
= sys_child_open_for_uncompress ();
406 read_full_records
= true;
407 record_end
= record_start
; /* set up for 1st record = # 0 */
411 child_pid
= sys_child_open_for_compress ();
415 abort (); /* Should not happen */
419 if (wanted_access
== ACCESS_WRITE
420 && strcmp (archive_name_array
[0], "-") == 0)
423 else if (strcmp (archive_name_array
[0], "-") == 0)
425 read_full_records
= true; /* could be a pipe, be safe */
427 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
429 switch (wanted_access
)
433 enum compress_type type
;
435 archive
= STDIN_FILENO
;
437 type
= check_compressed_archive (archive
);
440 _("Archive is compressed. Use %s option"),
441 compress_option (type
)));
446 archive
= STDOUT_FILENO
;
451 archive
= STDIN_FILENO
;
453 write_archive_to_stdout
= true;
457 else if (verify_option
)
458 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
459 MODE_RW
, rsh_command_option
);
461 switch (wanted_access
)
464 archive
= open_compressed_archive ();
470 maybe_backup_file (archive_name_array
[0], 1);
473 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
478 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
479 MODE_RW
, rsh_command_option
);
484 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
486 int saved_errno
= errno
;
491 open_fatal (archive_name_array
[0]);
494 sys_detect_dev_null_output ();
495 sys_save_archive_dev_ino ();
496 SET_BINARY_MODE (archive
);
498 switch (wanted_access
)
502 record_end
= record_start
; /* set up for 1st record = # 0 */
505 find_next_block (); /* read it in, check for EOF */
507 if (volume_label_option
)
509 union block
*label
= find_next_block ();
512 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
513 quote (volume_label_option
)));
514 if (!check_label_pattern (label
))
515 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
516 quote_n (0, label
->header
.name
),
517 quote_n (1, volume_label_option
)));
523 if (volume_label_option
)
525 memset (record_start
, 0, BLOCKSIZE
);
526 if (multi_volume_option
)
527 sprintf (record_start
->header
.name
, "%s Volume 1",
528 volume_label_option
);
530 strcpy (record_start
->header
.name
, volume_label_option
);
532 assign_string (¤t_stat_info
.file_name
,
533 record_start
->header
.name
);
534 current_stat_info
.had_trailing_slash
=
535 strip_trailing_slashes (current_stat_info
.file_name
);
537 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
538 TIME_TO_CHARS (start_time
.tv_sec
, record_start
->header
.mtime
);
539 finish_header (¤t_stat_info
, record_start
, -1);
545 /* Perform a write to flush the buffer. */
552 if (checkpoint_option
&& !(++checkpoint
% 10))
553 /* TRANSLATORS: This is a ``checkpoint of write operation'',
554 *not* ``Writing a checkpoint''.
555 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
556 *not* ``Escribiendo un punto de comprobaci@'on'' */
557 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
559 if (tape_length_option
&& tape_length_option
<= bytes_written
)
564 else if (dev_null_output
)
565 status
= record_size
;
567 status
= sys_write_archive_buffer ();
568 if (status
!= record_size
&& !multi_volume_option
)
569 archive_write_error (status
);
574 bytes_written
+= status
;
577 if (status
== record_size
)
579 if (multi_volume_option
)
583 assign_string (&real_s_name
,
584 safer_name_suffix (save_name
, false,
585 absolute_names_option
));
586 real_s_totsize
= save_totsize
;
587 real_s_sizeleft
= save_sizeleft
;
591 assign_string (&real_s_name
, 0);
599 /* We're multivol. Panic if we didn't get the right kind of response. */
601 /* ENXIO is for the UNIX PC. */
602 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
603 archive_write_error (status
);
605 /* If error indicates a short write, we just move to the next tape. */
607 if (!new_volume (ACCESS_WRITE
))
611 prev_written
+= bytes_written
;
614 if (volume_label_option
&& real_s_name
)
619 else if (volume_label_option
|| real_s_name
)
627 if (volume_label_option
)
629 memset (record_start
, 0, BLOCKSIZE
);
630 sprintf (record_start
->header
.name
, "%s Volume %d",
631 volume_label_option
, volno
);
632 TIME_TO_CHARS (start_time
.tv_sec
, record_start
->header
.mtime
);
633 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
634 finish_header (¤t_stat_info
, record_start
, -1);
641 if (volume_label_option
)
644 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
646 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
647 quotearg_colon (real_s_name
)));
649 memset (record_start
, 0, BLOCKSIZE
);
651 /* FIXME: Michael P Urban writes: [a long name file] is being written
652 when a new volume rolls around [...] Looks like the wrong value is
653 being preserved in real_s_name, though. */
655 strncpy (record_start
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
656 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
658 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
659 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
660 record_start
->oldgnu_header
.offset
);
662 tmp
= verbose_option
;
664 finish_header (¤t_stat_info
, record_start
, -1);
665 verbose_option
= tmp
;
667 if (volume_label_option
)
671 status
= sys_write_archive_buffer ();
672 if (status
!= record_size
)
673 archive_write_error (status
);
675 bytes_written
+= status
;
679 record_start
+= copy_back
;
680 memcpy (current_block
,
681 record_start
+ blocking_factor
- copy_back
,
682 copy_back
* BLOCKSIZE
);
683 current_block
+= copy_back
;
685 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
686 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
687 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
688 assign_string (&real_s_name
, 0);
691 assign_string (&real_s_name
,
692 safer_name_suffix (save_name
, false,
693 absolute_names_option
));
694 real_s_sizeleft
= save_sizeleft
;
695 real_s_totsize
= save_totsize
;
701 /* Handle write errors on the archive. Write errors are always fatal.
702 Hitting the end of a volume does not cause a write error unless the
703 write was the first record of the volume. */
705 archive_write_error (ssize_t status
)
707 /* It might be useful to know how much was written before the error
712 print_total_written ();
716 write_fatal_details (*archive_name_cursor
, status
, record_size
);
719 /* Handle read errors on the archive. If the read should be retried,
720 return to the caller. */
722 archive_read_error (void)
724 read_error (*archive_name_cursor
);
726 if (record_start_block
== 0)
727 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
729 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
730 then give up on reading the archive. */
732 if (read_error_count
++ > READ_ERROR_MAX
)
733 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
738 short_read (size_t status
)
740 size_t left
; /* bytes left */
741 char *more
; /* pointer to next byte to read */
743 more
= record_start
->buffer
+ status
;
744 left
= record_size
- status
;
746 while (left
% BLOCKSIZE
!= 0
747 || (left
&& status
&& read_full_records
))
750 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
751 archive_read_error ();
756 if (! read_full_records
)
758 unsigned long rest
= record_size
- left
;
761 ngettext ("Unaligned block (%lu byte) in archive",
762 "Unaligned block (%lu bytes) in archive",
767 /* User warned us about this. Fix up. */
773 /* FIXME: for size=0, multi-volume support. On the first record, warn
774 about the problem. */
776 if (!read_full_records
&& verbose_option
> 1
777 && record_start_block
== 0 && status
!= 0)
779 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
781 ngettext ("Record size = %lu block",
782 "Record size = %lu blocks",
787 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
791 /* Perform a read to flush the buffer. */
795 size_t status
; /* result from system call */
797 if (checkpoint_option
&& !(++checkpoint
% 10))
798 /* TRANSLATORS: This is a ``checkpoint of read operation'',
799 *not* ``Reading a checkpoint''.
800 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
801 *not* ``Leyendo un punto de comprobaci@'on'' */
802 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
804 /* Clear the count of errors. This only applies to a single call to
807 read_error_count
= 0; /* clear error count */
809 if (write_archive_to_stdout
&& record_start_block
!= 0)
811 archive
= STDOUT_FILENO
;
812 status
= sys_write_archive_buffer ();
813 archive
= STDIN_FILENO
;
814 if (status
!= record_size
)
815 archive_write_error (status
);
817 if (multi_volume_option
)
821 assign_string (&real_s_name
,
822 safer_name_suffix (save_name
, false,
823 absolute_names_option
));
824 real_s_sizeleft
= save_sizeleft
;
825 real_s_totsize
= save_totsize
;
829 assign_string (&real_s_name
, 0);
836 status
= rmtread (archive
, record_start
->buffer
, record_size
);
837 if (status
== record_size
)
843 /* The condition below used to include
844 || (status > 0 && !read_full_records)
845 This is incorrect since even if new_volume() succeeds, the
846 subsequent call to rmtread will overwrite the chunk of data
847 already read in the buffer, so the processing will fail */
850 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
851 && multi_volume_option
)
856 switch (subcommand_option
)
858 case APPEND_SUBCOMMAND
:
860 case UPDATE_SUBCOMMAND
:
861 if (!new_volume (ACCESS_UPDATE
))
866 if (!new_volume (ACCESS_READ
))
871 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
873 archive_read_error ();
875 if (status
!= record_size
)
878 cursor
= record_start
;
880 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
882 if (volume_label_option
)
884 if (!check_label_pattern (cursor
))
886 WARN ((0, 0, _("Volume %s does not match %s"),
887 quote_n (0, cursor
->header
.name
),
888 quote_n (1, volume_label_option
)));
895 fprintf (stdlis
, _("Reading %s\n"), quote (cursor
->header
.name
));
898 else if (volume_label_option
)
899 WARN ((0, 0, _("WARNING: No volume header")));
904 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
905 || strncmp (cursor
->header
.name
, real_s_name
, NAME_FIELD_SIZE
))
907 WARN ((0, 0, _("%s is not continued on this volume"),
908 quote (real_s_name
)));
913 s1
= UINTMAX_FROM_HEADER (cursor
->header
.size
);
914 s2
= UINTMAX_FROM_HEADER (cursor
->oldgnu_header
.offset
);
915 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
917 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
918 char s1buf
[UINTMAX_STRSIZE_BOUND
];
919 char s2buf
[UINTMAX_STRSIZE_BOUND
];
921 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
922 quote (cursor
->header
.name
),
923 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
924 STRINGIFY_BIGINT (s1
, s1buf
),
925 STRINGIFY_BIGINT (s2
, s2buf
)));
930 if (real_s_totsize
- real_s_sizeleft
931 != OFF_FROM_HEADER (cursor
->oldgnu_header
.offset
))
933 WARN ((0, 0, _("This volume is out of sequence")));
940 current_block
= cursor
;
944 else if (status
== SAFE_READ_ERROR
)
946 archive_read_error ();
947 goto error_loop
; /* try again */
953 /* Flush the current buffer to/from the archive. */
957 record_start_block
+= record_end
- record_start
;
958 current_block
= record_start
;
959 record_end
= record_start
+ blocking_factor
;
961 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
963 access_mode
= ACCESS_WRITE
;
964 time_to_start_writing
= false;
983 /* Backspace the archive descriptor by one record worth. If it's a
984 tape, MTIOCTOP will work. If it's something else, try to seek on
985 it. If we can't seek, we lose! */
987 backspace_output (void)
991 struct mtop operation
;
993 operation
.mt_op
= MTBSR
;
994 operation
.mt_count
= 1;
995 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
997 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1003 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1005 /* Seek back to the beginning of this record and start writing there. */
1007 position
-= record_size
;
1010 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1012 /* Lseek failed. Try a different method. */
1015 _("Cannot backspace archive file; it may be unreadable without -i")));
1017 /* Replace the first part of the record with NULs. */
1019 if (record_start
->buffer
!= output_start
)
1020 memset (record_start
->buffer
, 0,
1021 output_start
- record_start
->buffer
);
1027 seek_archive (off_t size
)
1029 off_t start
= current_block_ordinal ();
1032 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
1034 size
-= skipped
* BLOCKSIZE
;
1036 if (size
< record_size
)
1040 /* Compute number of records to skip */
1041 nrec
= size
/ record_size
;
1042 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
1046 if (offset
% record_size
)
1047 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
1049 /* Convert to number of records */
1050 offset
/= BLOCKSIZE
;
1051 /* Compute number of skipped blocks */
1052 nblk
= offset
- start
;
1054 /* Update buffering info */
1055 records_read
+= nblk
/ blocking_factor
;
1056 record_start_block
= offset
- blocking_factor
;
1057 current_block
= record_end
;
1062 /* Close the archive file. */
1064 close_archive (void)
1066 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1069 sys_drain_input_pipe ();
1071 compute_duration ();
1075 if (rmtclose (archive
) != 0)
1076 close_warn (*archive_name_cursor
);
1078 sys_wait_for_child (child_pid
);
1080 tar_stat_destroy (¤t_stat_info
);
1085 free (record_buffer
);
1088 /* Called to initialize the global volume number. */
1090 init_volume_number (void)
1092 FILE *file
= fopen (volno_file_option
, "r");
1096 if (fscanf (file
, "%d", &global_volno
) != 1
1097 || global_volno
< 0)
1098 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1099 quotearg_colon (volno_file_option
)));
1101 read_error (volno_file_option
);
1102 if (fclose (file
) != 0)
1103 close_error (volno_file_option
);
1105 else if (errno
!= ENOENT
)
1106 open_error (volno_file_option
);
1109 /* Called to write out the closing global volume number. */
1111 closeout_volume_number (void)
1113 FILE *file
= fopen (volno_file_option
, "w");
1117 fprintf (file
, "%d\n", global_volno
);
1119 write_error (volno_file_option
);
1120 if (fclose (file
) != 0)
1121 close_error (volno_file_option
);
1124 open_error (volno_file_option
);
1127 /* We've hit the end of the old volume. Close it and open the next one.
1128 Return nonzero on success.
1131 new_volume (enum access_mode mode
)
1133 static FILE *read_file
;
1136 if (!read_file
&& !info_script_option
)
1137 /* FIXME: if fopen is used, it will never be closed. */
1138 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1145 if (rmtclose (archive
) != 0)
1146 close_warn (*archive_name_cursor
);
1149 if (global_volno
< 0)
1150 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1152 archive_name_cursor
++;
1153 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1155 archive_name_cursor
= archive_name_array
;
1162 /* We have to prompt from now on. */
1164 if (info_script_option
)
1166 if (volno_file_option
)
1167 closeout_volume_number ();
1168 if (system (info_script_option
) != 0)
1169 FATAL_ERROR ((0, 0, _("%s command failed"),
1170 quote (info_script_option
)));
1175 char input_buffer
[80];
1177 fputc ('\007', stderr
);
1179 _("Prepare volume #%d for %s and hit return: "),
1180 global_volno
, quote (*archive_name_cursor
));
1183 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
1185 WARN ((0, 0, _("EOF where user reply was expected")));
1187 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1188 && subcommand_option
!= LIST_SUBCOMMAND
1189 && subcommand_option
!= DIFF_SUBCOMMAND
)
1190 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1194 if (input_buffer
[0] == '\n'
1195 || input_buffer
[0] == 'y'
1196 || input_buffer
[0] == 'Y')
1199 switch (input_buffer
[0])
1203 /* FIXME: Might it be useful to disable the '!' command? */
1204 fprintf (stderr
, _("\
1205 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1207 ! Spawn a subshell\n\
1208 ? Print this list\n"));
1215 WARN ((0, 0, _("No new volume; exiting.\n")));
1217 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1218 && subcommand_option
!= LIST_SUBCOMMAND
1219 && subcommand_option
!= DIFF_SUBCOMMAND
)
1220 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1225 /* Get new file name. */
1228 char *name
= &input_buffer
[1];
1231 for (name
= input_buffer
+ 1;
1232 *name
== ' ' || *name
== '\t';
1236 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1240 /* FIXME: the following allocation is never reclaimed. */
1241 *archive_name_cursor
= xstrdup (name
);
1252 if (strcmp (archive_name_cursor
[0], "-") == 0)
1254 read_full_records
= true;
1255 archive
= STDIN_FILENO
;
1257 else if (verify_option
)
1258 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1259 rsh_command_option
);
1264 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1265 rsh_command_option
);
1270 maybe_backup_file (*archive_name_cursor
, 1);
1271 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1272 rsh_command_option
);
1276 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1277 rsh_command_option
);
1283 open_warn (*archive_name_cursor
);
1284 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1285 undo_last_backup ();
1289 SET_BINARY_MODE (archive
);