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
[2]; /* allocated memory */
44 static int record_index
;
46 /* FIXME: The following variables should ideally be static to this
47 module. However, this cannot be done yet. The cleanup continues! */
49 union block
*record_start
; /* start of record of archive */
50 union block
*record_end
; /* last+1 block of archive record */
51 union block
*current_block
; /* current block of archive */
52 enum access_mode access_mode
; /* how do we handle the archive */
53 off_t records_read
; /* number of records read from this archive */
54 off_t records_written
; /* likewise, for records written */
56 static off_t record_start_block
; /* block ordinal at record_start */
58 /* Where we write list messages (not errors, not interactions) to. */
61 static void backspace_output (void);
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 bool write_archive_to_stdout
;
89 char *continued_file_name
;
90 uintmax_t continued_file_size
;
91 uintmax_t continued_file_offset
;
94 static int volno
= 1; /* which volume of a multi-volume tape we're
96 static int global_volno
= 1; /* volume number to print in external
99 bool write_archive_to_stdout
;
101 /* Used by flush_read and flush_write to store the real info about saved
103 static char *real_s_name
;
104 static off_t real_s_totsize
;
105 static off_t real_s_sizeleft
;
108 /* Multi-volume tracking support */
109 static char *save_name
; /* name of the file we are currently writing */
110 static off_t save_totsize
; /* total size of file we are writing, only
111 valid if save_name is nonzero */
112 static off_t save_sizeleft
; /* where we are in the file we are writing,
113 only valid if save_name is nonzero */
116 mv_begin (struct tar_stat_info
*st
)
118 if (multi_volume_option
)
120 assign_string (&save_name
, st
->orig_file_name
);
121 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
128 if (multi_volume_option
)
129 assign_string (&save_name
, 0);
133 mv_total_size (off_t size
)
139 mv_size_left (off_t size
)
141 save_sizeleft
= size
;
148 clear_read_error_count (void)
150 read_error_count
= 0;
154 /* Time-related functions */
161 gettime (&start_time
);
169 duration
+= ((now
.tv_sec
- start_time
.tv_sec
)
170 + (now
.tv_nsec
- start_time
.tv_nsec
) / 1e9
);
175 /* Compression detection */
186 enum compress_type type
;
193 static struct zip_magic
const magic
[] = {
195 { ct_compress
, 2, "\037\235", "compress", "-Z" },
196 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
197 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
200 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
202 #define compress_option(t) magic[t].option
203 #define compress_program(t) magic[t].program
205 /* Check if the file ARCHIVE is a compressed archive. */
207 check_compressed_archive ()
209 struct zip_magic
const *p
;
212 /* Prepare global data needed for find_next_block: */
213 record_end
= record_start
; /* set up for 1st record = # 0 */
214 sfr
= read_full_records
;
215 read_full_records
= true; /* Suppress fatal error on reading a partial
219 /* Restore global values */
220 read_full_records
= sfr
;
222 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
223 /* Probably a valid header */
226 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
227 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
233 /* Open an archive named archive_name_array[0]. Detect if it is
234 a compressed archive of known type and use corresponding decompression
237 open_compressed_archive ()
239 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
240 MODE_RW
, rsh_command_option
);
244 if (!multi_volume_option
)
246 enum compress_type type
= check_compressed_archive ();
251 /* FD is not needed any more */
254 hit_eof
= false; /* It might have been set by find_next_block in
255 check_compressed_archive */
257 /* Open compressed archive */
258 use_compress_program_option
= compress_program (type
);
259 child_pid
= sys_child_open_for_uncompress ();
260 read_full_records
= true;
264 record_end
= record_start
; /* set up for 1st record = # 0 */
271 print_total_written (void)
273 tarlong written
= prev_written
+ bytes_written
;
274 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
275 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
276 char rate
[LONGEST_HUMAN_READABLE
+ 1];
278 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
280 sprintf (bytes
, TARLONG_FORMAT
, written
);
282 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
283 fprintf (stderr
, _("Total bytes written: %s (%s, %s/s)\n"), bytes
,
284 human_readable (written
, abbr
, human_opts
, 1, 1),
285 (0 < duration
&& written
/ duration
< (uintmax_t) -1
286 ? human_readable (written
/ duration
, rate
, human_opts
, 1, 1)
290 /* Compute and return the block ordinal at current_block. */
292 current_block_ordinal (void)
294 return record_start_block
+ (current_block
- record_start
);
297 /* If the EOF flag is set, reset it, as well as current_block, etc. */
304 current_block
= record_start
;
305 record_end
= record_start
+ blocking_factor
;
306 access_mode
= ACCESS_WRITE
;
310 /* Return the location of the next available input or output block.
311 Return zero for EOF. Once we have returned zero, we just keep returning
312 it, to avoid accidentally going on to the next file on the tape. */
314 find_next_block (void)
316 if (current_block
== record_end
)
321 if (current_block
== record_end
)
327 return current_block
;
330 /* Indicate that we have used all blocks up thru BLOCK. */
332 set_next_block_after (union block
*block
)
334 while (block
>= current_block
)
337 /* Do *not* flush the archive here. If we do, the same argument to
338 set_next_block_after could mean the next block (if the input record
339 is exactly one block long), which is not what is intended. */
341 if (current_block
> record_end
)
345 /* Return the number of bytes comprising the space between POINTER
346 through the end of the current buffer of blocks. This space is
347 available for filling with data, or taking data from. POINTER is
348 usually (but not always) the result of previous find_next_block call. */
350 available_space_after (union block
*pointer
)
352 return record_end
->buffer
- pointer
->buffer
;
355 /* Close file having descriptor FD, and abort if close unsuccessful. */
360 close_error (_("(pipe)"));
366 if (!record_buffer
[record_index
])
367 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
369 record_start
= record_buffer
[record_index
];
370 current_block
= record_start
;
371 record_end
= record_start
+ blocking_factor
;
374 /* Open an archive file. The argument specifies whether we are
375 reading or writing, or both. */
377 _open_archive (enum access_mode wanted_access
)
379 int backed_up_flag
= 0;
383 stdlis
= fopen (index_file_name
, "w");
385 open_error (index_file_name
);
388 stdlis
= to_stdout_option
? stderr
: stdout
;
390 if (record_size
== 0)
391 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
393 if (archive_names
== 0)
394 FATAL_ERROR ((0, 0, _("No archive name given")));
396 tar_stat_destroy (¤t_stat_info
);
403 /* When updating the archive, we start with reading. */
404 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
406 read_full_records
= read_full_records_option
;
410 if (use_compress_program_option
)
412 switch (wanted_access
)
415 child_pid
= sys_child_open_for_uncompress ();
416 read_full_records
= true;
417 record_end
= record_start
; /* set up for 1st record = # 0 */
421 child_pid
= sys_child_open_for_compress ();
425 abort (); /* Should not happen */
429 if (wanted_access
== ACCESS_WRITE
430 && strcmp (archive_name_array
[0], "-") == 0)
433 else if (strcmp (archive_name_array
[0], "-") == 0)
435 read_full_records
= true; /* could be a pipe, be safe */
437 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
439 switch (wanted_access
)
443 enum compress_type type
;
445 archive
= STDIN_FILENO
;
447 type
= check_compressed_archive (archive
);
450 _("Archive is compressed. Use %s option"),
451 compress_option (type
)));
456 archive
= STDOUT_FILENO
;
461 archive
= STDIN_FILENO
;
463 write_archive_to_stdout
= true;
467 else if (verify_option
)
468 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
469 MODE_RW
, rsh_command_option
);
471 switch (wanted_access
)
474 archive
= open_compressed_archive ();
480 maybe_backup_file (archive_name_array
[0], 1);
483 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
488 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
489 MODE_RW
, rsh_command_option
);
494 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
496 int saved_errno
= errno
;
501 open_fatal (archive_name_array
[0]);
504 sys_detect_dev_null_output ();
505 sys_save_archive_dev_ino ();
506 SET_BINARY_MODE (archive
);
508 switch (wanted_access
)
512 record_end
= record_start
; /* set up for 1st record = # 0 */
515 find_next_block (); /* read it in, check for EOF */
524 /* Perform a write to flush the buffer. */
530 if (checkpoint_option
&& !(++checkpoint
% 10))
531 /* TRANSLATORS: This is a ``checkpoint of write operation'',
532 *not* ``Writing a checkpoint''.
533 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
534 *not* ``Escribiendo un punto de comprobaci@'on'' */
535 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
537 if (tape_length_option
&& tape_length_option
<= bytes_written
)
542 else if (dev_null_output
)
543 status
= record_size
;
545 status
= sys_write_archive_buffer ();
550 /* Handle write errors on the archive. Write errors are always fatal.
551 Hitting the end of a volume does not cause a write error unless the
552 write was the first record of the volume. */
554 archive_write_error (ssize_t status
)
556 /* It might be useful to know how much was written before the error
561 print_total_written ();
565 write_fatal_details (*archive_name_cursor
, status
, record_size
);
568 /* Handle read errors on the archive. If the read should be retried,
569 return to the caller. */
571 archive_read_error (void)
573 read_error (*archive_name_cursor
);
575 if (record_start_block
== 0)
576 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
578 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
579 then give up on reading the archive. */
581 if (read_error_count
++ > READ_ERROR_MAX
)
582 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
587 short_read (size_t status
)
589 size_t left
; /* bytes left */
590 char *more
; /* pointer to next byte to read */
592 more
= record_start
->buffer
+ status
;
593 left
= record_size
- status
;
595 while (left
% BLOCKSIZE
!= 0
596 || (left
&& status
&& read_full_records
))
599 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
600 archive_read_error ();
605 if (! read_full_records
)
607 unsigned long rest
= record_size
- left
;
610 ngettext ("Unaligned block (%lu byte) in archive",
611 "Unaligned block (%lu bytes) in archive",
616 /* User warned us about this. Fix up. */
622 /* FIXME: for size=0, multi-volume support. On the first record, warn
623 about the problem. */
625 if (!read_full_records
&& verbose_option
> 1
626 && record_start_block
== 0 && status
!= 0)
628 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
630 ngettext ("Record size = %lu block",
631 "Record size = %lu blocks",
636 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
640 /* Perform a read to flush the buffer. */
644 size_t status
; /* result from system call */
646 if (checkpoint_option
&& !(++checkpoint
% 10))
647 /* TRANSLATORS: This is a ``checkpoint of read operation'',
648 *not* ``Reading a checkpoint''.
649 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
650 *not* ``Leyendo un punto de comprobaci@'on'' */
651 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
653 /* Clear the count of errors. This only applies to a single call to
656 read_error_count
= 0; /* clear error count */
658 if (write_archive_to_stdout
&& record_start_block
!= 0)
660 archive
= STDOUT_FILENO
;
661 status
= sys_write_archive_buffer ();
662 archive
= STDIN_FILENO
;
663 if (status
!= record_size
)
664 archive_write_error (status
);
667 status
= rmtread (archive
, record_start
->buffer
, record_size
);
668 if (status
== record_size
)
673 /* Flush the current buffer to/from the archive. */
677 record_start_block
+= record_end
- record_start
;
678 current_block
= record_start
;
679 record_end
= record_start
+ blocking_factor
;
681 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
683 access_mode
= ACCESS_WRITE
;
684 time_to_start_writing
= false;
703 /* Backspace the archive descriptor by one record worth. If it's a
704 tape, MTIOCTOP will work. If it's something else, try to seek on
705 it. If we can't seek, we lose! */
707 backspace_output (void)
711 struct mtop operation
;
713 operation
.mt_op
= MTBSR
;
714 operation
.mt_count
= 1;
715 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
717 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
723 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
725 /* Seek back to the beginning of this record and start writing there. */
727 position
-= record_size
;
730 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
732 /* Lseek failed. Try a different method. */
735 _("Cannot backspace archive file; it may be unreadable without -i")));
737 /* Replace the first part of the record with NULs. */
739 if (record_start
->buffer
!= output_start
)
740 memset (record_start
->buffer
, 0,
741 output_start
- record_start
->buffer
);
747 seek_archive (off_t size
)
749 off_t start
= current_block_ordinal ();
752 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
754 size
-= skipped
* BLOCKSIZE
;
756 if (size
< record_size
)
760 /* Compute number of records to skip */
761 nrec
= size
/ record_size
;
762 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
766 if (offset
% record_size
)
767 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
769 /* Convert to number of records */
771 /* Compute number of skipped blocks */
772 nblk
= offset
- start
;
774 /* Update buffering info */
775 records_read
+= nblk
/ blocking_factor
;
776 record_start_block
= offset
- blocking_factor
;
777 current_block
= record_end
;
782 /* Close the archive file. */
786 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
789 sys_drain_input_pipe ();
795 if (rmtclose (archive
) != 0)
796 close_warn (*archive_name_cursor
);
798 sys_wait_for_child (child_pid
);
800 tar_stat_destroy (¤t_stat_info
);
805 free (record_buffer
[0]);
806 free (record_buffer
[1]);
809 /* Called to initialize the global volume number. */
811 init_volume_number (void)
813 FILE *file
= fopen (volno_file_option
, "r");
817 if (fscanf (file
, "%d", &global_volno
) != 1
819 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
820 quotearg_colon (volno_file_option
)));
822 read_error (volno_file_option
);
823 if (fclose (file
) != 0)
824 close_error (volno_file_option
);
826 else if (errno
!= ENOENT
)
827 open_error (volno_file_option
);
830 /* Called to write out the closing global volume number. */
832 closeout_volume_number (void)
834 FILE *file
= fopen (volno_file_option
, "w");
838 fprintf (file
, "%d\n", global_volno
);
840 write_error (volno_file_option
);
841 if (fclose (file
) != 0)
842 close_error (volno_file_option
);
845 open_error (volno_file_option
);
850 increase_volume_number ()
853 if (global_volno
< 0)
854 FATAL_ERROR ((0, 0, _("Volume number overflow")));
858 /* We've hit the end of the old volume. Close it and open the next one.
859 Return nonzero on success.
862 new_volume (enum access_mode mode
)
864 static FILE *read_file
;
867 if (!read_file
&& !info_script_option
)
868 /* FIXME: if fopen is used, it will never be closed. */
869 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
876 assign_string (&volume_label
, NULL
);
877 assign_string (&continued_file_name
, NULL
);
878 continued_file_size
= continued_file_offset
= 0;
880 if (rmtclose (archive
) != 0)
881 close_warn (*archive_name_cursor
);
883 archive_name_cursor
++;
884 if (archive_name_cursor
== archive_name_array
+ archive_names
)
886 archive_name_cursor
= archive_name_array
;
893 /* We have to prompt from now on. */
895 if (info_script_option
)
897 if (volno_file_option
)
898 closeout_volume_number ();
899 if (system (info_script_option
) != 0)
900 FATAL_ERROR ((0, 0, _("%s command failed"),
901 quote (info_script_option
)));
906 char input_buffer
[80];
908 fputc ('\007', stderr
);
910 _("Prepare volume #%d for %s and hit return: "),
911 global_volno
+ 1, quote (*archive_name_cursor
));
914 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
916 WARN ((0, 0, _("EOF where user reply was expected")));
918 if (subcommand_option
!= EXTRACT_SUBCOMMAND
919 && subcommand_option
!= LIST_SUBCOMMAND
920 && subcommand_option
!= DIFF_SUBCOMMAND
)
921 WARN ((0, 0, _("WARNING: Archive is incomplete")));
925 if (input_buffer
[0] == '\n'
926 || input_buffer
[0] == 'y'
927 || input_buffer
[0] == 'Y')
930 switch (input_buffer
[0])
934 /* FIXME: Might it be useful to disable the '!' command? */
935 fprintf (stderr
, _("\
936 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
938 ! Spawn a subshell\n\
939 ? Print this list\n"));
946 WARN ((0, 0, _("No new volume; exiting.\n")));
948 if (subcommand_option
!= EXTRACT_SUBCOMMAND
949 && subcommand_option
!= LIST_SUBCOMMAND
950 && subcommand_option
!= DIFF_SUBCOMMAND
)
951 WARN ((0, 0, _("WARNING: Archive is incomplete")));
956 /* Get new file name. */
959 char *name
= &input_buffer
[1];
962 for (name
= input_buffer
+ 1;
963 *name
== ' ' || *name
== '\t';
967 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
971 /* FIXME: the following allocation is never reclaimed. */
972 *archive_name_cursor
= xstrdup (name
);
983 if (strcmp (archive_name_cursor
[0], "-") == 0)
985 read_full_records
= true;
986 archive
= STDIN_FILENO
;
988 else if (verify_option
)
989 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
995 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1001 maybe_backup_file (*archive_name_cursor
, 1);
1002 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1003 rsh_command_option
);
1007 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1008 rsh_command_option
);
1014 open_warn (*archive_name_cursor
);
1015 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1016 undo_last_backup ();
1020 SET_BINARY_MODE (archive
);
1029 enum read_header rc
;
1032 switch (subcommand_option
)
1034 case APPEND_SUBCOMMAND
:
1035 case CAT_SUBCOMMAND
:
1036 case UPDATE_SUBCOMMAND
:
1037 if (!new_volume (ACCESS_UPDATE
))
1042 if (!new_volume (ACCESS_READ
))
1047 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1049 archive_read_error ();
1051 if (status
!= record_size
)
1052 short_read (status
);
1055 block
= current_block
;
1056 rc
= read_header (true);
1059 case HEADER_SUCCESS_EXTENDED
:
1060 if (current_header
->header
.typeflag
== XGLTYPE
)
1062 struct tar_stat_info dummy
;
1063 xheader_read (current_header
,
1064 OFF_FROM_HEADER (current_header
->header
.size
));
1065 xheader_decode_global ();
1066 xheader_destroy (&extended_header
);
1067 tar_stat_init (&dummy
);
1068 xheader_decode (&dummy
); /* decodes values from the global header */
1069 tar_stat_destroy (&dummy
);
1073 case HEADER_SUCCESS
:
1074 if (current_header
->header
.typeflag
== GNUTYPE_VOLHDR
)
1075 assign_string (&volume_label
, current_header
->header
.name
);
1076 else if (current_header
->header
.typeflag
== GNUTYPE_MULTIVOL
)
1078 assign_string (&continued_file_name
, current_header
->header
.name
);
1079 continued_file_size
=
1080 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1081 continued_file_offset
=
1082 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1086 set_next_block_after (current_header
);
1089 case HEADER_ZERO_BLOCK
:
1090 case HEADER_END_OF_FILE
:
1091 case HEADER_FAILURE
:
1092 current_block
= block
;
1102 if (!continued_file_name
1103 || strcmp (continued_file_name
, real_s_name
))
1105 WARN ((0, 0, _("%s is not continued on this volume"),
1106 quote (real_s_name
)));
1110 s
= continued_file_size
+ continued_file_offset
;
1112 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1114 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1115 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1116 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1118 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1119 quote (continued_file_name
),
1120 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1121 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1122 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1126 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1128 WARN ((0, 0, _("This volume is out of sequence")));
1133 increase_volume_number ();
1138 /* Check the LABEL block against the volume label, seen as a globbing
1139 pattern. Return true if the pattern matches. In case of failure,
1140 retry matching a volume sequence number before giving up in
1141 multi-volume mode. */
1143 check_label_pattern (union block
*label
)
1148 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1151 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1154 if (!multi_volume_option
)
1157 string
= xmalloc (strlen (volume_label_option
)
1158 + sizeof VOLUME_LABEL_APPEND
+ 1);
1159 strcpy (string
, volume_label_option
);
1160 strcat (string
, VOLUME_LABEL_APPEND
);
1161 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1166 /* Check if the next block contains a volume label and if this matches
1167 the one given in the command line */
1169 match_volume_label (void)
1171 union block
*label
= find_next_block ();
1174 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1175 quote (volume_label_option
)));
1176 if (!check_label_pattern (label
))
1177 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1178 quote_n (0, label
->header
.name
),
1179 quote_n (1, volume_label_option
)));
1182 /* Mark the archive with volume label STR. */
1184 _write_volume_label (const char *str
)
1186 if (archive_format
== POSIX_FORMAT
)
1187 xheader_store ("GNU.volume.label", NULL
, str
);
1190 union block
*label
= find_next_block ();
1192 memset (label
, 0, BLOCKSIZE
);
1194 strcpy (label
->header
.name
, volume_label_option
);
1195 assign_string (¤t_stat_info
.file_name
,
1196 label
->header
.name
);
1197 current_stat_info
.had_trailing_slash
=
1198 strip_trailing_slashes (current_stat_info
.file_name
);
1200 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1201 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1202 finish_header (¤t_stat_info
, label
, -1);
1203 set_next_block_after (label
);
1207 #define VOL_SUFFIX "Volume"
1209 /* Add a volume label to a part of multi-volume archive */
1211 add_volume_label (void)
1213 char buf
[UINTMAX_STRSIZE_BOUND
];
1214 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1215 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1217 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1218 _write_volume_label (s
);
1222 /* Add a volume label to the current archive */
1224 write_volume_label (void)
1226 if (multi_volume_option
)
1227 add_volume_label ();
1229 _write_volume_label (volume_label_option
);
1232 /* Write GNU multi-volume header */
1234 gnu_add_multi_volume_header (void)
1237 union block
*block
= find_next_block ();
1239 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1241 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1242 quotearg_colon (real_s_name
)));
1244 memset (block
, 0, BLOCKSIZE
);
1246 /* FIXME: Michael P Urban writes: [a long name file] is being written
1247 when a new volume rolls around [...] Looks like the wrong value is
1248 being preserved in real_s_name, though. */
1250 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1251 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1253 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1254 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1255 block
->oldgnu_header
.offset
);
1257 tmp
= verbose_option
;
1259 finish_header (¤t_stat_info
, block
, -1);
1260 verbose_option
= tmp
;
1261 set_next_block_after (block
);
1264 /* Add a multi volume header to the current archive. The exact header format
1265 depends on the archive format. */
1267 add_multi_volume_header (void)
1269 if (archive_format
== POSIX_FORMAT
)
1271 off_t d
= real_s_totsize
- real_s_sizeleft
;
1272 xheader_store ("GNU.volume.filename", NULL
, real_s_name
);
1273 xheader_store ("GNU.volume.size", NULL
, &real_s_sizeleft
);
1274 xheader_store ("GNU.volume.offset", NULL
, &d
);
1277 gnu_add_multi_volume_header ();
1280 /* Synchronize multi-volume globals */
1282 multi_volume_sync ()
1284 if (multi_volume_option
)
1288 assign_string (&real_s_name
,
1289 safer_name_suffix (save_name
, false,
1290 absolute_names_option
));
1291 real_s_totsize
= save_totsize
;
1292 real_s_sizeleft
= save_sizeleft
;
1296 assign_string (&real_s_name
, 0);
1298 real_s_sizeleft
= 0;
1304 /* Low-level flush functions */
1306 /* Simple flush read (no multi-volume or label extensions) */
1308 simple_flush_read (void)
1310 size_t status
; /* result from system call */
1312 if (checkpoint_option
&& !(++checkpoint
% 10))
1313 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1314 *not* ``Reading a checkpoint''.
1315 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1316 *not* ``Leyendo un punto de comprobaci@'on'' */
1317 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1319 /* Clear the count of errors. This only applies to a single call to
1322 read_error_count
= 0; /* clear error count */
1324 if (write_archive_to_stdout
&& record_start_block
!= 0)
1326 archive
= STDOUT_FILENO
;
1327 status
= sys_write_archive_buffer ();
1328 archive
= STDIN_FILENO
;
1329 if (status
!= record_size
)
1330 archive_write_error (status
);
1335 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1336 if (status
== record_size
)
1341 if (status
== SAFE_READ_ERROR
)
1343 archive_read_error ();
1344 continue; /* try again */
1348 short_read (status
);
1351 /* Simple flush write (no multi-volume or label extensions) */
1353 simple_flush_write (void)
1357 status
= _flush_write ();
1358 if (status
!= record_size
)
1359 archive_write_error (status
);
1363 bytes_written
+= status
;
1368 /* GNU flush functions. These support multi-volume and archive labels in
1369 GNU and PAX archive formats. */
1372 _gnu_flush_read (void)
1374 size_t status
; /* result from system call */
1376 if (checkpoint_option
&& !(++checkpoint
% 10))
1377 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1378 *not* ``Reading a checkpoint''.
1379 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1380 *not* ``Leyendo un punto de comprobaci@'on'' */
1381 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1383 /* Clear the count of errors. This only applies to a single call to
1386 read_error_count
= 0; /* clear error count */
1388 if (write_archive_to_stdout
&& record_start_block
!= 0)
1390 archive
= STDOUT_FILENO
;
1391 status
= sys_write_archive_buffer ();
1392 archive
= STDIN_FILENO
;
1393 if (status
!= record_size
)
1394 archive_write_error (status
);
1397 multi_volume_sync ();
1401 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1402 if (status
== record_size
)
1408 /* The condition below used to include
1409 || (status > 0 && !read_full_records)
1410 This is incorrect since even if new_volume() succeeds, the
1411 subsequent call to rmtread will overwrite the chunk of data
1412 already read in the buffer, so the processing will fail */
1414 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1415 && multi_volume_option
)
1417 while (!try_new_volume ())
1421 else if (status
== SAFE_READ_ERROR
)
1423 archive_read_error ();
1428 short_read (status
);
1432 gnu_flush_read (void)
1434 flush_read
= simple_flush_read
; /* Avoid recursion */
1436 flush_read
= gnu_flush_read
;
1440 _gnu_flush_write (void)
1443 union block
*header
;
1448 status
= _flush_write ();
1449 if (status
!= record_size
&& !multi_volume_option
)
1450 archive_write_error (status
);
1454 bytes_written
+= status
;
1457 if (status
== record_size
)
1459 multi_volume_sync ();
1463 /* In multi-volume mode. */
1464 /* ENXIO is for the UNIX PC. */
1465 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1466 archive_write_error (status
);
1468 if (!new_volume (ACCESS_WRITE
))
1471 xheader_destroy (&extended_header
);
1473 increase_volume_number ();
1475 prev_written
+= bytes_written
;
1478 copy_ptr
= record_start
->buffer
+ status
;
1479 copy_size
= record_size
- status
;
1480 /* Switch to the next buffer */
1481 record_index
= !record_index
;
1484 if (volume_label_option
)
1485 add_volume_label ();
1488 add_multi_volume_header ();
1490 header
= write_extended (XGLTYPE
, NULL
, find_next_block ());
1491 bufsize
= available_space_after (header
);
1492 while (bufsize
< copy_size
)
1494 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1495 copy_ptr
+= bufsize
;
1496 copy_size
-= bufsize
;
1497 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1498 header
= find_next_block ();
1499 bufsize
= available_space_after (header
);
1501 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1502 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1503 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1508 gnu_flush_write (void)
1510 flush_write
= simple_flush_write
; /* Avoid recursion */
1511 _gnu_flush_write ();
1512 flush_write
= gnu_flush_write
;
1516 open_archive (enum access_mode wanted_access
)
1518 flush_read
= gnu_flush_read
;
1519 flush_write
= gnu_flush_write
;
1521 _open_archive (wanted_access
);
1522 switch (wanted_access
)
1525 if (volume_label_option
)
1526 match_volume_label ();
1530 records_written
= 0;
1531 if (volume_label_option
)
1532 write_volume_label ();