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 #if HAVE_CLOCK_GETTIME
129 if (clock_gettime (CLOCK_REALTIME
, &start_timespec
) != 0)
131 start_time
= time (0);
137 #if HAVE_CLOCK_GETTIME
139 if (clock_gettime (CLOCK_REALTIME
, &now
) == 0)
140 duration
+= ((now
.tv_sec
- start_timespec
.tv_sec
)
141 + (now
.tv_nsec
- start_timespec
.tv_nsec
) / 1e9
);
144 duration
+= time (NULL
) - start_time
;
149 /* Compression detection */
160 enum compress_type type
;
167 static struct zip_magic
const magic
[] = {
169 { ct_compress
, 2, "\037\235", "compress", "-Z" },
170 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
171 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
174 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
176 #define compress_option(t) magic[t].option
177 #define compress_program(t) magic[t].program
179 /* Check if the file ARCHIVE is a compressed archive. */
181 check_compressed_archive ()
183 struct zip_magic
const *p
;
186 /* Prepare global data needed for find_next_block: */
187 record_end
= record_start
; /* set up for 1st record = # 0 */
188 sfr
= read_full_records
;
189 read_full_records
= true; /* Suppress fatal error on reading a partial
193 /* Restore global values */
194 read_full_records
= sfr
;
196 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
197 /* Probably a valid header */
200 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
201 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
207 /* Open an archive named archive_name_array[0]. Detect if it is
208 a compressed archive of known type and use corresponding decompression
211 open_compressed_archive ()
213 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
214 MODE_RW
, rsh_command_option
);
218 if (!multi_volume_option
)
220 enum compress_type type
= check_compressed_archive ();
225 /* FD is not needed any more */
228 hit_eof
= false; /* It might have been set by find_next_block in
229 check_compressed_archive */
231 /* Open compressed archive */
232 use_compress_program_option
= compress_program (type
);
233 child_pid
= sys_child_open_for_uncompress ();
234 read_full_records
= true;
238 record_end
= record_start
; /* set up for 1st record = # 0 */
245 print_total_written (void)
247 tarlong written
= prev_written
+ bytes_written
;
248 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
249 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
250 char rate
[LONGEST_HUMAN_READABLE
+ 1];
252 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
254 sprintf (bytes
, TARLONG_FORMAT
, written
);
256 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
257 fprintf (stderr
, _("Total bytes written: %s (%s, %s/s)\n"), bytes
,
258 human_readable (written
, abbr
, human_opts
, 1, 1),
259 (0 < duration
&& written
/ duration
< (uintmax_t) -1
260 ? human_readable (written
/ duration
, rate
, human_opts
, 1, 1)
264 /* Compute and return the block ordinal at current_block. */
266 current_block_ordinal (void)
268 return record_start_block
+ (current_block
- record_start
);
271 /* If the EOF flag is set, reset it, as well as current_block, etc. */
278 current_block
= record_start
;
279 record_end
= record_start
+ blocking_factor
;
280 access_mode
= ACCESS_WRITE
;
284 /* Return the location of the next available input or output block.
285 Return zero for EOF. Once we have returned zero, we just keep returning
286 it, to avoid accidentally going on to the next file on the tape. */
288 find_next_block (void)
290 if (current_block
== record_end
)
295 if (current_block
== record_end
)
301 return current_block
;
304 /* Indicate that we have used all blocks up thru BLOCK. */
306 set_next_block_after (union block
*block
)
308 while (block
>= current_block
)
311 /* Do *not* flush the archive here. If we do, the same argument to
312 set_next_block_after could mean the next block (if the input record
313 is exactly one block long), which is not what is intended. */
315 if (current_block
> record_end
)
319 /* Return the number of bytes comprising the space between POINTER
320 through the end of the current buffer of blocks. This space is
321 available for filling with data, or taking data from. POINTER is
322 usually (but not always) the result of previous find_next_block call. */
324 available_space_after (union block
*pointer
)
326 return record_end
->buffer
- pointer
->buffer
;
329 /* Close file having descriptor FD, and abort if close unsuccessful. */
334 close_error (_("(pipe)"));
337 /* Check the LABEL block against the volume label, seen as a globbing
338 pattern. Return true if the pattern matches. In case of failure,
339 retry matching a volume sequence number before giving up in
340 multi-volume mode. */
342 check_label_pattern (union block
*label
)
347 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
350 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
353 if (!multi_volume_option
)
356 string
= xmalloc (strlen (volume_label_option
)
357 + sizeof VOLUME_LABEL_APPEND
+ 1);
358 strcpy (string
, volume_label_option
);
359 strcat (string
, VOLUME_LABEL_APPEND
);
360 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
365 /* Open an archive file. The argument specifies whether we are
366 reading or writing, or both. */
368 open_archive (enum access_mode wanted_access
)
370 int backed_up_flag
= 0;
374 stdlis
= fopen (index_file_name
, "w");
376 open_error (index_file_name
);
379 stdlis
= to_stdout_option
? stderr
: stdout
;
381 if (record_size
== 0)
382 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
384 if (archive_names
== 0)
385 FATAL_ERROR ((0, 0, _("No archive name given")));
387 tar_stat_destroy (¤t_stat_info
);
392 page_aligned_alloc (&record_buffer
,
394 + (multi_volume_option
? 2 * BLOCKSIZE
: 0)));
395 if (multi_volume_option
)
398 current_block
= record_start
;
399 record_end
= record_start
+ blocking_factor
;
400 /* When updating the archive, we start with reading. */
401 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
403 read_full_records
= read_full_records_option
;
407 if (use_compress_program_option
)
409 switch (wanted_access
)
412 child_pid
= sys_child_open_for_uncompress ();
413 read_full_records
= true;
414 record_end
= record_start
; /* set up for 1st record = # 0 */
418 child_pid
= sys_child_open_for_compress ();
422 abort (); /* Should not happen */
426 if (wanted_access
== ACCESS_WRITE
427 && strcmp (archive_name_array
[0], "-") == 0)
430 else if (strcmp (archive_name_array
[0], "-") == 0)
432 read_full_records
= true; /* could be a pipe, be safe */
434 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
436 switch (wanted_access
)
440 enum compress_type type
;
442 archive
= STDIN_FILENO
;
444 type
= check_compressed_archive (archive
);
447 _("Archive is compressed. Use %s option"),
448 compress_option (type
)));
453 archive
= STDOUT_FILENO
;
458 archive
= STDIN_FILENO
;
460 write_archive_to_stdout
= true;
464 else if (verify_option
)
465 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
466 MODE_RW
, rsh_command_option
);
468 switch (wanted_access
)
471 archive
= open_compressed_archive ();
477 maybe_backup_file (archive_name_array
[0], 1);
480 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
485 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
486 MODE_RW
, rsh_command_option
);
491 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
493 int saved_errno
= errno
;
498 open_fatal (archive_name_array
[0]);
501 sys_detect_dev_null_output ();
502 sys_save_archive_dev_ino ();
503 SET_BINARY_MODE (archive
);
505 switch (wanted_access
)
509 record_end
= record_start
; /* set up for 1st record = # 0 */
512 find_next_block (); /* read it in, check for EOF */
514 if (volume_label_option
)
516 union block
*label
= find_next_block ();
519 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
520 quote (volume_label_option
)));
521 if (!check_label_pattern (label
))
522 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
523 quote_n (0, label
->header
.name
),
524 quote_n (1, volume_label_option
)));
530 if (volume_label_option
)
532 memset (record_start
, 0, BLOCKSIZE
);
533 if (multi_volume_option
)
534 sprintf (record_start
->header
.name
, "%s Volume 1",
535 volume_label_option
);
537 strcpy (record_start
->header
.name
, volume_label_option
);
539 assign_string (¤t_stat_info
.file_name
,
540 record_start
->header
.name
);
541 current_stat_info
.had_trailing_slash
=
542 strip_trailing_slashes (current_stat_info
.file_name
);
544 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
545 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
546 finish_header (¤t_stat_info
, record_start
, -1);
552 /* Perform a write to flush the buffer. */
559 if (checkpoint_option
&& !(++checkpoint
% 10))
560 /* TRANSLATORS: This is a ``checkpoint of write operation'',
561 *not* ``Writing a checkpoint''.
562 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
563 *not* ``Escribiendo un punto de comprobaci@'on'' */
564 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
566 if (tape_length_option
&& tape_length_option
<= bytes_written
)
571 else if (dev_null_output
)
572 status
= record_size
;
574 status
= sys_write_archive_buffer ();
575 if (status
!= record_size
&& !multi_volume_option
)
576 archive_write_error (status
);
581 bytes_written
+= status
;
584 if (status
== record_size
)
586 if (multi_volume_option
)
590 assign_string (&real_s_name
, safer_name_suffix (save_name
, false));
591 real_s_totsize
= save_totsize
;
592 real_s_sizeleft
= save_sizeleft
;
596 assign_string (&real_s_name
, 0);
604 /* We're multivol. Panic if we didn't get the right kind of response. */
606 /* ENXIO is for the UNIX PC. */
607 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
608 archive_write_error (status
);
610 /* If error indicates a short write, we just move to the next tape. */
612 if (!new_volume (ACCESS_WRITE
))
616 prev_written
+= bytes_written
;
619 if (volume_label_option
&& real_s_name
)
624 else if (volume_label_option
|| real_s_name
)
632 if (volume_label_option
)
634 memset (record_start
, 0, BLOCKSIZE
);
635 sprintf (record_start
->header
.name
, "%s Volume %d",
636 volume_label_option
, volno
);
637 TIME_TO_CHARS (start_time
, record_start
->header
.mtime
);
638 record_start
->header
.typeflag
= GNUTYPE_VOLHDR
;
639 finish_header (¤t_stat_info
, record_start
, -1);
646 if (volume_label_option
)
649 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
651 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
652 quotearg_colon (real_s_name
)));
654 memset (record_start
, 0, BLOCKSIZE
);
656 /* FIXME: Michael P Urban writes: [a long name file] is being written
657 when a new volume rolls around [...] Looks like the wrong value is
658 being preserved in real_s_name, though. */
660 strncpy (record_start
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
661 record_start
->header
.typeflag
= GNUTYPE_MULTIVOL
;
663 OFF_TO_CHARS (real_s_sizeleft
, record_start
->header
.size
);
664 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
665 record_start
->oldgnu_header
.offset
);
667 tmp
= verbose_option
;
669 finish_header (¤t_stat_info
, record_start
, -1);
670 verbose_option
= tmp
;
672 if (volume_label_option
)
676 status
= sys_write_archive_buffer ();
677 if (status
!= record_size
)
678 archive_write_error (status
);
680 bytes_written
+= status
;
684 record_start
+= copy_back
;
685 memcpy (current_block
,
686 record_start
+ blocking_factor
- copy_back
,
687 copy_back
* BLOCKSIZE
);
688 current_block
+= copy_back
;
690 if (real_s_sizeleft
>= copy_back
* BLOCKSIZE
)
691 real_s_sizeleft
-= copy_back
* BLOCKSIZE
;
692 else if ((real_s_sizeleft
+ BLOCKSIZE
- 1) / BLOCKSIZE
<= copy_back
)
693 assign_string (&real_s_name
, 0);
696 assign_string (&real_s_name
, safer_name_suffix (save_name
, false));
697 real_s_sizeleft
= save_sizeleft
;
698 real_s_totsize
= save_totsize
;
704 /* Handle write errors on the archive. Write errors are always fatal.
705 Hitting the end of a volume does not cause a write error unless the
706 write was the first record of the volume. */
708 archive_write_error (ssize_t status
)
710 /* It might be useful to know how much was written before the error
715 print_total_written ();
719 write_fatal_details (*archive_name_cursor
, status
, record_size
);
722 /* Handle read errors on the archive. If the read should be retried,
723 return to the caller. */
725 archive_read_error (void)
727 read_error (*archive_name_cursor
);
729 if (record_start_block
== 0)
730 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
732 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
733 then give up on reading the archive. */
735 if (read_error_count
++ > READ_ERROR_MAX
)
736 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
741 short_read (size_t status
)
743 size_t left
; /* bytes left */
744 char *more
; /* pointer to next byte to read */
746 more
= record_start
->buffer
+ status
;
747 left
= record_size
- status
;
749 while (left
% BLOCKSIZE
!= 0
750 || (left
&& status
&& read_full_records
))
753 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
754 archive_read_error ();
759 if (! read_full_records
)
761 unsigned long rest
= record_size
- left
;
764 ngettext ("Unaligned block (%lu byte) in archive",
765 "Unaligned block (%lu bytes) in archive",
770 /* User warned us about this. Fix up. */
776 /* FIXME: for size=0, multi-volume support. On the first record, warn
777 about the problem. */
779 if (!read_full_records
&& verbose_option
> 1
780 && record_start_block
== 0 && status
!= 0)
782 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
784 ngettext ("Record size = %lu block",
785 "Record size = %lu blocks",
790 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
794 /* Perform a read to flush the buffer. */
798 size_t status
; /* result from system call */
800 if (checkpoint_option
&& !(++checkpoint
% 10))
801 /* TRANSLATORS: This is a ``checkpoint of read operation'',
802 *not* ``Reading a checkpoint''.
803 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
804 *not* ``Leyendo un punto de comprobaci@'on'' */
805 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
807 /* Clear the count of errors. This only applies to a single call to
810 read_error_count
= 0; /* clear error count */
812 if (write_archive_to_stdout
&& record_start_block
!= 0)
814 archive
= STDOUT_FILENO
;
815 status
= sys_write_archive_buffer ();
816 archive
= STDIN_FILENO
;
817 if (status
!= record_size
)
818 archive_write_error (status
);
820 if (multi_volume_option
)
824 assign_string (&real_s_name
, safer_name_suffix (save_name
, false));
825 real_s_sizeleft
= save_sizeleft
;
826 real_s_totsize
= save_totsize
;
830 assign_string (&real_s_name
, 0);
837 status
= rmtread (archive
, record_start
->buffer
, record_size
);
838 if (status
== record_size
)
844 /* The condition below used to include
845 || (status > 0 && !read_full_records)
846 This is incorrect since even if new_volume() succeeds, the
847 subsequent call to rmtread will overwrite the chunk of data
848 already read in the buffer, so the processing will fail */
851 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
852 && multi_volume_option
)
857 switch (subcommand_option
)
859 case APPEND_SUBCOMMAND
:
861 case UPDATE_SUBCOMMAND
:
862 if (!new_volume (ACCESS_UPDATE
))
867 if (!new_volume (ACCESS_READ
))
872 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
874 archive_read_error ();
876 if (status
!= record_size
)
879 cursor
= record_start
;
881 if (cursor
->header
.typeflag
== GNUTYPE_VOLHDR
)
883 if (volume_label_option
)
885 if (!check_label_pattern (cursor
))
887 WARN ((0, 0, _("Volume %s does not match %s"),
888 quote_n (0, cursor
->header
.name
),
889 quote_n (1, volume_label_option
)));
896 fprintf (stdlis
, _("Reading %s\n"), quote (cursor
->header
.name
));
899 else if (volume_label_option
)
900 WARN ((0, 0, _("WARNING: No volume header")));
905 if (cursor
->header
.typeflag
!= GNUTYPE_MULTIVOL
906 || strncmp (cursor
->header
.name
, real_s_name
, NAME_FIELD_SIZE
))
908 WARN ((0, 0, _("%s is not continued on this volume"),
909 quote (real_s_name
)));
914 s1
= UINTMAX_FROM_HEADER (cursor
->header
.size
);
915 s2
= UINTMAX_FROM_HEADER (cursor
->oldgnu_header
.offset
);
916 if (real_s_totsize
!= s1
+ s2
|| s1
+ s2
< s2
)
918 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
919 char s1buf
[UINTMAX_STRSIZE_BOUND
];
920 char s2buf
[UINTMAX_STRSIZE_BOUND
];
922 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
923 quote (cursor
->header
.name
),
924 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
925 STRINGIFY_BIGINT (s1
, s1buf
),
926 STRINGIFY_BIGINT (s2
, s2buf
)));
931 if (real_s_totsize
- real_s_sizeleft
932 != OFF_FROM_HEADER (cursor
->oldgnu_header
.offset
))
934 WARN ((0, 0, _("This volume is out of sequence")));
941 current_block
= cursor
;
945 else if (status
== SAFE_READ_ERROR
)
947 archive_read_error ();
948 goto error_loop
; /* try again */
954 /* Flush the current buffer to/from the archive. */
958 record_start_block
+= record_end
- record_start
;
959 current_block
= record_start
;
960 record_end
= record_start
+ blocking_factor
;
962 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
964 access_mode
= ACCESS_WRITE
;
965 time_to_start_writing
= false;
984 /* Backspace the archive descriptor by one record worth. If it's a
985 tape, MTIOCTOP will work. If it's something else, try to seek on
986 it. If we can't seek, we lose! */
988 backspace_output (void)
992 struct mtop operation
;
994 operation
.mt_op
= MTBSR
;
995 operation
.mt_count
= 1;
996 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
998 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1004 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1006 /* Seek back to the beginning of this record and start writing there. */
1008 position
-= record_size
;
1011 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1013 /* Lseek failed. Try a different method. */
1016 _("Cannot backspace archive file; it may be unreadable without -i")));
1018 /* Replace the first part of the record with NULs. */
1020 if (record_start
->buffer
!= output_start
)
1021 memset (record_start
->buffer
, 0,
1022 output_start
- record_start
->buffer
);
1028 seek_archive (off_t size
)
1030 off_t start
= current_block_ordinal ();
1033 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
1035 size
-= skipped
* BLOCKSIZE
;
1037 if (size
< record_size
)
1041 /* Compute number of records to skip */
1042 nrec
= size
/ record_size
;
1043 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
1047 if (offset
% record_size
)
1048 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
1050 /* Convert to number of records */
1051 offset
/= BLOCKSIZE
;
1052 /* Compute number of skipped blocks */
1053 nblk
= offset
- start
;
1055 /* Update buffering info */
1056 records_read
+= nblk
/ blocking_factor
;
1057 record_start_block
= offset
- blocking_factor
;
1058 current_block
= record_end
;
1063 /* Close the archive file. */
1065 close_archive (void)
1067 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1070 sys_drain_input_pipe ();
1072 compute_duration ();
1076 if (rmtclose (archive
) != 0)
1077 close_warn (*archive_name_cursor
);
1079 sys_wait_for_child (child_pid
);
1081 tar_stat_destroy (¤t_stat_info
);
1086 free (record_buffer
);
1089 /* Called to initialize the global volume number. */
1091 init_volume_number (void)
1093 FILE *file
= fopen (volno_file_option
, "r");
1097 if (fscanf (file
, "%d", &global_volno
) != 1
1098 || global_volno
< 0)
1099 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1100 quotearg_colon (volno_file_option
)));
1102 read_error (volno_file_option
);
1103 if (fclose (file
) != 0)
1104 close_error (volno_file_option
);
1106 else if (errno
!= ENOENT
)
1107 open_error (volno_file_option
);
1110 /* Called to write out the closing global volume number. */
1112 closeout_volume_number (void)
1114 FILE *file
= fopen (volno_file_option
, "w");
1118 fprintf (file
, "%d\n", global_volno
);
1120 write_error (volno_file_option
);
1121 if (fclose (file
) != 0)
1122 close_error (volno_file_option
);
1125 open_error (volno_file_option
);
1128 /* We've hit the end of the old volume. Close it and open the next one.
1129 Return nonzero on success.
1132 new_volume (enum access_mode mode
)
1134 static FILE *read_file
;
1137 if (!read_file
&& !info_script_option
)
1138 /* FIXME: if fopen is used, it will never be closed. */
1139 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1146 if (rmtclose (archive
) != 0)
1147 close_warn (*archive_name_cursor
);
1150 if (global_volno
< 0)
1151 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1153 archive_name_cursor
++;
1154 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1156 archive_name_cursor
= archive_name_array
;
1163 /* We have to prompt from now on. */
1165 if (info_script_option
)
1167 if (volno_file_option
)
1168 closeout_volume_number ();
1169 if (system (info_script_option
) != 0)
1170 FATAL_ERROR ((0, 0, _("%s command failed"),
1171 quote (info_script_option
)));
1176 char input_buffer
[80];
1178 fputc ('\007', stderr
);
1180 _("Prepare volume #%d for %s and hit return: "),
1181 global_volno
, quote (*archive_name_cursor
));
1184 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
1186 WARN ((0, 0, _("EOF where user reply was expected")));
1188 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1189 && subcommand_option
!= LIST_SUBCOMMAND
1190 && subcommand_option
!= DIFF_SUBCOMMAND
)
1191 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1195 if (input_buffer
[0] == '\n'
1196 || input_buffer
[0] == 'y'
1197 || input_buffer
[0] == 'Y')
1200 switch (input_buffer
[0])
1204 /* FIXME: Might it be useful to disable the '!' command? */
1205 fprintf (stderr
, _("\
1206 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1208 ! Spawn a subshell\n\
1209 ? Print this list\n"));
1216 WARN ((0, 0, _("No new volume; exiting.\n")));
1218 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1219 && subcommand_option
!= LIST_SUBCOMMAND
1220 && subcommand_option
!= DIFF_SUBCOMMAND
)
1221 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1226 /* Get new file name. */
1229 char *name
= &input_buffer
[1];
1232 for (name
= input_buffer
+ 1;
1233 *name
== ' ' || *name
== '\t';
1237 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1241 /* FIXME: the following allocation is never reclaimed. */
1242 *archive_name_cursor
= xstrdup (name
);
1253 if (strcmp (archive_name_cursor
[0], "-") == 0)
1255 read_full_records
= true;
1256 archive
= STDIN_FILENO
;
1258 else if (verify_option
)
1259 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1260 rsh_command_option
);
1265 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1266 rsh_command_option
);
1271 maybe_backup_file (*archive_name_cursor
, 1);
1272 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1273 rsh_command_option
);
1277 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1278 rsh_command_option
);
1284 open_warn (*archive_name_cursor
);
1285 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1286 undo_last_backup ();
1290 SET_BINARY_MODE (archive
);