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 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
;
168 static struct zip_magic
const magic
[] = {
170 { ct_compress
, 2, "\037\235", "compress", "-Z" },
171 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
172 { ct_bzip2
, 3, "BZh", "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 ()
184 struct zip_magic
const *p
;
187 /* Prepare global data needed for find_next_block: */
188 record_end
= record_start
; /* set up for 1st record = # 0 */
189 sfr
= read_full_records
;
190 read_full_records
= true; /* Suppress fatal error on reading a partial
192 srp
= reading_from_pipe
;
193 reading_from_pipe
= true; /* Suppress warning message on reading a partial
197 /* Restore global values */
198 read_full_records
= sfr
;
199 reading_from_pipe
= srp
;
201 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
202 /* Probably a valid header */
205 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
206 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
212 /* Open an archive named archive_name_array[0]. Detect if it is
213 a compressed archive of known type and use corresponding decompression
216 open_compressed_archive ()
218 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
219 MODE_RW
, rsh_command_option
);
223 if (!multi_volume_option
)
225 enum compress_type type
= check_compressed_archive ();
230 /* FD is not needed any more */
233 hit_eof
= false; /* It might have been set by find_next_block in
234 check_compressed_archive */
236 /* Open compressed archive */
237 use_compress_program_option
= compress_program (type
);
238 child_pid
= sys_child_open_for_uncompress ();
239 read_full_records
= reading_from_pipe
= true;
243 record_end
= record_start
; /* set up for 1st record = # 0 */
250 print_total_written (void)
252 tarlong written
= prev_written
+ bytes_written
;
253 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
254 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
255 char rate
[LONGEST_HUMAN_READABLE
+ 1];
257 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
259 sprintf (bytes
, TARLONG_FORMAT
, written
);
261 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
262 fprintf (stderr
, _("Total bytes written: %s (%s, %s/s)\n"), bytes
,
263 human_readable (written
, abbr
, human_opts
, 1, 1),
264 (0 < duration
&& written
/ duration
< (uintmax_t) -1
265 ? human_readable (written
/ duration
, rate
, human_opts
, 1, 1)
269 /* Compute and return the block ordinal at current_block. */
271 current_block_ordinal (void)
273 return record_start_block
+ (current_block
- record_start
);
276 /* If the EOF flag is set, reset it, as well as current_block, etc. */
283 current_block
= record_start
;
284 record_end
= record_start
+ blocking_factor
;
285 access_mode
= ACCESS_WRITE
;
289 /* Return the location of the next available input or output block.
290 Return zero for EOF. Once we have returned zero, we just keep returning
291 it, to avoid accidentally going on to the next file on the tape. */
293 find_next_block (void)
295 if (current_block
== record_end
)
300 if (current_block
== record_end
)
306 return current_block
;
309 /* Indicate that we have used all blocks up thru BLOCK. */
311 set_next_block_after (union block
*block
)
313 while (block
>= current_block
)
316 /* Do *not* flush the archive here. If we do, the same argument to
317 set_next_block_after could mean the next block (if the input record
318 is exactly one block long), which is not what is intended. */
320 if (current_block
> record_end
)
324 /* Return the number of bytes comprising the space between POINTER
325 through the end of the current buffer of blocks. This space is
326 available for filling with data, or taking data from. POINTER is
327 usually (but not always) the result of previous find_next_block call. */
329 available_space_after (union block
*pointer
)
331 return record_end
->buffer
- pointer
->buffer
;
334 /* Close file having descriptor FD, and abort if close unsuccessful. */
339 close_error (_("(pipe)"));
342 /* Check the LABEL block against the volume label, seen as a globbing
343 pattern. Return true if the pattern matches. In case of failure,
344 retry matching a volume sequence number before giving up in
345 multi-volume mode. */
347 check_label_pattern (union block
*label
)
352 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
355 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
358 if (!multi_volume_option
)
361 string
= xmalloc (strlen (volume_label_option
)
362 + sizeof VOLUME_LABEL_APPEND
+ 1);
363 strcpy (string
, volume_label_option
);
364 strcat (string
, VOLUME_LABEL_APPEND
);
365 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
370 /* Open an archive file. The argument specifies whether we are
371 reading or writing, or both. */
373 open_archive (enum access_mode wanted_access
)
375 int backed_up_flag
= 0;
379 stdlis
= fopen (index_file_name
, "w");
381 open_error (index_file_name
);
384 stdlis
= to_stdout_option
? stderr
: stdout
;
386 if (record_size
== 0)
387 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
389 if (archive_names
== 0)
390 FATAL_ERROR ((0, 0, _("No archive name given")));
392 tar_stat_destroy (¤t_stat_info
);
397 page_aligned_alloc (&record_buffer
,
399 + (multi_volume_option
? 2 * BLOCKSIZE
: 0)));
400 if (multi_volume_option
)
403 current_block
= record_start
;
404 record_end
= record_start
+ blocking_factor
;
405 /* When updating the archive, we start with reading. */
406 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
408 read_full_records
= read_full_records_option
;
409 reading_from_pipe
= false;
413 if (use_compress_program_option
)
415 switch (wanted_access
)
418 child_pid
= sys_child_open_for_uncompress ();
419 read_full_records
= reading_from_pipe
= true;
420 record_end
= record_start
; /* set up for 1st record = # 0 */
424 child_pid
= sys_child_open_for_compress ();
428 abort (); /* Should not happen */
432 if (wanted_access
== ACCESS_WRITE
433 && strcmp (archive_name_array
[0], "-") == 0)
436 else if (strcmp (archive_name_array
[0], "-") == 0)
438 read_full_records
= true; /* could be a pipe, be safe */
440 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
442 switch (wanted_access
)
446 enum compress_type type
;
448 archive
= STDIN_FILENO
;
450 type
= check_compressed_archive (archive
);
453 _("Archive is compressed. Use %s option"),
454 compress_option (type
)));
459 archive
= STDOUT_FILENO
;
464 archive
= STDIN_FILENO
;
466 write_archive_to_stdout
= true;
470 else if (verify_option
)
471 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
472 MODE_RW
, rsh_command_option
);
474 switch (wanted_access
)
477 archive
= open_compressed_archive ();
483 maybe_backup_file (archive_name_array
[0], 1);
486 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
491 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
492 MODE_RW
, rsh_command_option
);
497 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
499 int saved_errno
= errno
;
504 open_fatal (archive_name_array
[0]);
507 sys_detect_dev_null_output ();
508 sys_save_archive_dev_ino ();
509 SET_BINARY_MODE (archive
);
511 switch (wanted_access
)
515 record_end
= record_start
; /* set up for 1st record = # 0 */
518 find_next_block (); /* read it in, check for EOF */
520 if (volume_label_option
)
522 union block
*label
= find_next_block ();
525 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
526 quote (volume_label_option
)));
527 if (!check_label_pattern (label
))
528 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
529 quote_n (0, label
->header
.name
),
530 quote_n (1, volume_label_option
)));
536 if (volume_label_option
)
538 memset (record_start
, 0, BLOCKSIZE
);
539 if (multi_volume_option
)
540 sprintf (record_start
->header
.name
, "%s Volume 1",
541 volume_label_option
);
543 strcpy (record_start
->header
.name
, volume_label_option
);
545 assign_string (¤t_stat_info
.file_name
,
546 record_start
->header
.name
);
547 current_stat_info
.had_trailing_slash
=
548 strip_trailing_slashes (current_stat_info
.file_name
);
550 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
551 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
552 finish_header (¤t_stat_info
, record_start
, -1);
558 /* Perform a write to flush the buffer. */
565 if (checkpoint_option
&& !(++checkpoint
% 10))
566 /* TRANSLATORS: This is a ``checkpoint of write operation'',
567 *not* ``Writing a checkpoint''.
568 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
569 *not* ``Escribiendo un punto de comprobaci@'on'' */
570 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
572 if (tape_length_option
&& tape_length_option
<= bytes_written
)
577 else if (dev_null_output
)
578 status
= record_size
;
580 status
= sys_write_archive_buffer ();
581 if (status
!= record_size
&& !multi_volume_option
)
582 archive_write_error (status
);
587 bytes_written
+= status
;
590 if (status
== record_size
)
592 if (multi_volume_option
)
596 assign_string (&real_s_name
, safer_name_suffix (save_name
, false));
597 real_s_totsize
= save_totsize
;
598 real_s_sizeleft
= save_sizeleft
;
602 assign_string (&real_s_name
, 0);
610 /* We're multivol. Panic if we didn't get the right kind of response. */
612 /* ENXIO is for the UNIX PC. */
613 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
614 archive_write_error (status
);
616 /* If error indicates a short write, we just move to the next tape. */
618 if (!new_volume (ACCESS_WRITE
))
622 prev_written
+= bytes_written
;
625 if (volume_label_option
&& real_s_name
)
630 else if (volume_label_option
|| real_s_name
)
638 if (volume_label_option
)
640 memset (record_start
, 0, BLOCKSIZE
);
641 sprintf (record_start
->header
.name
, "%s Volume %d",
642 volume_label_option
, volno
);
643 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
644 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
645 finish_header (¤t_stat_info
, record_start
, -1);
652 if (volume_label_option
)
655 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
657 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
658 quotearg_colon (real_s_name
)));
660 memset (record_start
, 0, BLOCKSIZE
);
662 /* FIXME: Michael P Urban writes: [a long name file] is being written
663 when a new volume rolls around [...] Looks like the wrong value is
664 being preserved in real_s_name, though. */
666 strncpy (record_start
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
667 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
669 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
670 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
671 record_start
->oldgnu_header
.offset
);
673 tmp
= verbose_option
;
675 finish_header (¤t_stat_info
, record_start
, -1);
676 verbose_option
= tmp
;
678 if (volume_label_option
)
682 status
= sys_write_archive_buffer ();
683 if (status
!= record_size
)
684 archive_write_error (status
);
686 bytes_written
+= status
;
690 record_start
+= copy_back
;
691 memcpy (current_block
,
692 record_start
+ blocking_factor
- copy_back
,
693 copy_back
* BLOCKSIZE
);
694 current_block
+= copy_back
;
696 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
697 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
698 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
699 assign_string (&real_s_name
, 0);
702 assign_string (&real_s_name
, safer_name_suffix (save_name
, false));
703 real_s_sizeleft
= save_sizeleft
;
704 real_s_totsize
= save_totsize
;
710 /* Handle write errors on the archive. Write errors are always fatal.
711 Hitting the end of a volume does not cause a write error unless the
712 write was the first record of the volume. */
714 archive_write_error (ssize_t status
)
716 /* It might be useful to know how much was written before the error
721 print_total_written ();
725 write_fatal_details (*archive_name_cursor
, status
, record_size
);
728 /* Handle read errors on the archive. If the read should be retried,
729 return to the caller. */
731 archive_read_error (void)
733 read_error (*archive_name_cursor
);
735 if (record_start_block
== 0)
736 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
738 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
739 then give up on reading the archive. */
741 if (read_error_count
++ > READ_ERROR_MAX
)
742 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
747 short_read (size_t status
)
749 size_t left
; /* bytes left */
750 char *more
; /* pointer to next byte to read */
752 more
= record_start
->buffer
+ status
;
753 left
= record_size
- status
;
755 while (left
% BLOCKSIZE
!= 0
756 || (left
&& status
&& read_full_records
))
759 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
760 archive_read_error ();
764 if (!reading_from_pipe
)
766 char buf
[UINTMAX_STRSIZE_BOUND
];
769 ngettext ("Read %s byte from %s",
770 "Read %s bytes from %s",
772 STRINGIFY_BIGINT (record_size
- left
, buf
),
773 *archive_name_cursor
));
778 if (! read_full_records
)
780 unsigned long rest
= record_size
- left
;
783 ngettext ("Unaligned block (%lu byte) in archive",
784 "Unaligned block (%lu bytes) in archive",
789 /* User warned us about this. Fix up. */
795 /* FIXME: for size=0, multi-volume support. On the first record, warn
796 about the problem. */
798 if (!read_full_records
&& verbose_option
> 1
799 && record_start_block
== 0 && status
!= 0)
801 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
803 ngettext ("Record size = %lu block",
804 "Record size = %lu blocks",
809 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
813 /* Perform a read to flush the buffer. */
817 size_t status
; /* result from system call */
819 if (checkpoint_option
&& !(++checkpoint
% 10))
820 /* TRANSLATORS: This is a ``checkpoint of read operation'',
821 *not* ``Reading a checkpoint''.
822 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
823 *not* ``Leyendo un punto de comprobaci@'on'' */
824 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
826 /* Clear the count of errors. This only applies to a single call to
829 read_error_count
= 0; /* clear error count */
831 if (write_archive_to_stdout
&& record_start_block
!= 0)
833 archive
= STDOUT_FILENO
;
834 status
= sys_write_archive_buffer ();
835 archive
= STDIN_FILENO
;
836 if (status
!= record_size
)
837 archive_write_error (status
);
839 if (multi_volume_option
)
843 assign_string (&real_s_name
, safer_name_suffix (save_name
, false));
844 real_s_sizeleft
= save_sizeleft
;
845 real_s_totsize
= save_totsize
;
849 assign_string (&real_s_name
, 0);
856 status
= rmtread (archive
, record_start
->buffer
, record_size
);
857 if (status
== record_size
)
863 /* The condition below used to include
864 || (status > 0 && !read_full_records)
865 This is incorrect since even if new_volume() succeeds, the
866 subsequent call to rmtread will overwrite the chunk of data
867 already read in the buffer, so the processing will fail */
870 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
871 && multi_volume_option
)
876 switch (subcommand_option
)
878 case APPEND_SUBCOMMAND
:
880 case UPDATE_SUBCOMMAND
:
881 if (!new_volume (ACCESS_UPDATE
))
886 if (!new_volume (ACCESS_READ
))
891 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
893 archive_read_error ();
895 if (status
!= record_size
)
898 cursor
= record_start
;
900 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
902 if (volume_label_option
)
904 if (!check_label_pattern (cursor
))
906 WARN ((0, 0, _("Volume %s does not match %s"),
907 quote_n (0, cursor
->header
.name
),
908 quote_n (1, volume_label_option
)));
915 fprintf (stdlis
, _("Reading %s\n"), quote (cursor
->header
.name
));
918 else if (volume_label_option
)
919 WARN ((0, 0, _("WARNING: No volume header")));
924 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
925 || strncmp (cursor
->header
.name
, real_s_name
, NAME_FIELD_SIZE
))
927 WARN ((0, 0, _("%s is not continued on this volume"),
928 quote (real_s_name
)));
933 s1
= UINTMAX_FROM_HEADER (cursor
->header
.size
);
934 s2
= UINTMAX_FROM_HEADER (cursor
->oldgnu_header
.offset
);
935 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
937 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
938 char s1buf
[UINTMAX_STRSIZE_BOUND
];
939 char s2buf
[UINTMAX_STRSIZE_BOUND
];
941 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
942 quote (cursor
->header
.name
),
943 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
944 STRINGIFY_BIGINT (s1
, s1buf
),
945 STRINGIFY_BIGINT (s2
, s2buf
)));
950 if (real_s_totsize
- real_s_sizeleft
951 != OFF_FROM_HEADER (cursor
->oldgnu_header
.offset
))
953 WARN ((0, 0, _("This volume is out of sequence")));
960 current_block
= cursor
;
964 else if (status
== SAFE_READ_ERROR
)
966 archive_read_error ();
967 goto error_loop
; /* try again */
973 /* Flush the current buffer to/from the archive. */
977 record_start_block
+= record_end
- record_start
;
978 current_block
= record_start
;
979 record_end
= record_start
+ blocking_factor
;
981 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
983 access_mode
= ACCESS_WRITE
;
984 time_to_start_writing
= false;
1003 /* Backspace the archive descriptor by one record worth. If it's a
1004 tape, MTIOCTOP will work. If it's something else, try to seek on
1005 it. If we can't seek, we lose! */
1007 backspace_output (void)
1011 struct mtop operation
;
1013 operation
.mt_op
= MTBSR
;
1014 operation
.mt_count
= 1;
1015 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1017 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1023 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1025 /* Seek back to the beginning of this record and start writing there. */
1027 position
-= record_size
;
1030 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1032 /* Lseek failed. Try a different method. */
1035 _("Cannot backspace archive file; it may be unreadable without -i")));
1037 /* Replace the first part of the record with NULs. */
1039 if (record_start
->buffer
!= output_start
)
1040 memset (record_start
->buffer
, 0,
1041 output_start
- record_start
->buffer
);
1047 seek_archive (off_t size
)
1049 off_t start
= current_block_ordinal ();
1052 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
1054 size
-= skipped
* BLOCKSIZE
;
1056 if (size
< record_size
)
1060 /* Compute number of records to skip */
1061 nrec
= size
/ record_size
;
1062 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
1066 if (offset
% record_size
)
1067 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
1069 /* Convert to number of records */
1070 offset
/= BLOCKSIZE
;
1071 /* Compute number of skipped blocks */
1072 nblk
= offset
- start
;
1074 /* Update buffering info */
1075 records_read
+= nblk
/ blocking_factor
;
1076 record_start_block
= offset
- blocking_factor
;
1077 current_block
= record_end
;
1082 /* Close the archive file. */
1084 close_archive (void)
1086 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1089 sys_drain_input_pipe ();
1091 compute_duration ();
1095 if (rmtclose (archive
) != 0)
1096 close_warn (*archive_name_cursor
);
1098 sys_wait_for_child (child_pid
);
1100 tar_stat_destroy (¤t_stat_info
);
1105 free (record_buffer
);
1108 /* Called to initialize the global volume number. */
1110 init_volume_number (void)
1112 FILE *file
= fopen (volno_file_option
, "r");
1116 if (fscanf (file
, "%d", &global_volno
) != 1
1117 || global_volno
< 0)
1118 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1119 quotearg_colon (volno_file_option
)));
1121 read_error (volno_file_option
);
1122 if (fclose (file
) != 0)
1123 close_error (volno_file_option
);
1125 else if (errno
!= ENOENT
)
1126 open_error (volno_file_option
);
1129 /* Called to write out the closing global volume number. */
1131 closeout_volume_number (void)
1133 FILE *file
= fopen (volno_file_option
, "w");
1137 fprintf (file
, "%d\n", global_volno
);
1139 write_error (volno_file_option
);
1140 if (fclose (file
) != 0)
1141 close_error (volno_file_option
);
1144 open_error (volno_file_option
);
1147 /* We've hit the end of the old volume. Close it and open the next one.
1148 Return nonzero on success.
1151 new_volume (enum access_mode mode
)
1153 static FILE *read_file
;
1156 if (!read_file
&& !info_script_option
)
1157 /* FIXME: if fopen is used, it will never be closed. */
1158 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1165 if (rmtclose (archive
) != 0)
1166 close_warn (*archive_name_cursor
);
1169 if (global_volno
< 0)
1170 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1172 archive_name_cursor
++;
1173 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1175 archive_name_cursor
= archive_name_array
;
1182 /* We have to prompt from now on. */
1184 if (info_script_option
)
1186 if (volno_file_option
)
1187 closeout_volume_number ();
1188 if (system (info_script_option
) != 0)
1189 FATAL_ERROR ((0, 0, _("%s command failed"),
1190 quote (info_script_option
)));
1195 char input_buffer
[80];
1197 fputc ('\007', stderr
);
1199 _("Prepare volume #%d for %s and hit return: "),
1200 global_volno
, quote (*archive_name_cursor
));
1203 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
1205 WARN ((0, 0, _("EOF where user reply was expected")));
1207 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1208 && subcommand_option
!= LIST_SUBCOMMAND
1209 && subcommand_option
!= DIFF_SUBCOMMAND
)
1210 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1214 if (input_buffer
[0] == '\n'
1215 || input_buffer
[0] == 'y'
1216 || input_buffer
[0] == 'Y')
1219 switch (input_buffer
[0])
1223 /* FIXME: Might it be useful to disable the '!' command? */
1224 fprintf (stderr
, _("\
1225 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1227 ! Spawn a subshell\n\
1228 ? Print this list\n"));
1235 WARN ((0, 0, _("No new volume; exiting.\n")));
1237 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1238 && subcommand_option
!= LIST_SUBCOMMAND
1239 && subcommand_option
!= DIFF_SUBCOMMAND
)
1240 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1245 /* Get new file name. */
1248 char *name
= &input_buffer
[1];
1251 for (name
= input_buffer
+ 1;
1252 *name
== ' ' || *name
== '\t';
1256 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1260 /* FIXME: the following allocation is never reclaimed. */
1261 *archive_name_cursor
= xstrdup (name
);
1272 if (strcmp (archive_name_cursor
[0], "-") == 0)
1274 read_full_records
= true;
1275 archive
= STDIN_FILENO
;
1277 else if (verify_option
)
1278 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1279 rsh_command_option
);
1284 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1285 rsh_command_option
);
1290 maybe_backup_file (*archive_name_cursor
, 1);
1291 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1292 rsh_command_option
);
1296 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1297 rsh_command_option
);
1303 open_warn (*archive_name_cursor
);
1304 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1305 undo_last_backup ();
1309 SET_BINARY_MODE (archive
);