1 /* Buffer management for tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007, 2008 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 3, 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. */
23 #include <system-ioctl.h>
35 /* Number of retries before giving up on read. */
36 #define READ_ERROR_MAX 10
38 /* Globbing pattern to append to volume label if initial match failed. */
39 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
43 static tarlong prev_written
; /* bytes written on previous volumes */
44 static tarlong bytes_written
; /* bytes written on this volume */
45 static void *record_buffer
[2]; /* allocated memory */
46 union block
*record_buffer_aligned
[2];
47 static int record_index
;
49 /* FIXME: The following variables should ideally be static to this
50 module. However, this cannot be done yet. The cleanup continues! */
52 union block
*record_start
; /* start of record of archive */
53 union block
*record_end
; /* last+1 block of archive record */
54 union block
*current_block
; /* current block of archive */
55 enum access_mode access_mode
; /* how do we handle the archive */
56 off_t records_read
; /* number of records read from this archive */
57 off_t records_written
; /* likewise, for records written */
58 extern off_t records_skipped
; /* number of records skipped at the start
59 of the archive, defined in delete.c */
61 static off_t record_start_block
; /* block ordinal at record_start */
63 /* Where we write list messages (not errors, not interactions) to. */
66 static void backspace_output (void);
68 /* PID of child program, if compress_option or remote archive access. */
69 static pid_t child_pid
;
71 /* Error recovery stuff */
72 static int read_error_count
;
74 /* Have we hit EOF yet? */
77 static bool read_full_records
= false;
79 /* We're reading, but we just read the last block and it's time to update.
82 As least EXTERN like this one as possible. (?? --gray)
83 FIXME: Either eliminate it or move it to common.h.
85 extern bool time_to_start_writing
;
87 bool write_archive_to_stdout
;
89 void (*flush_write_ptr
) (size_t);
90 void (*flush_read_ptr
) (void);
94 char *continued_file_name
;
95 uintmax_t continued_file_size
;
96 uintmax_t continued_file_offset
;
99 static int volno
= 1; /* which volume of a multi-volume tape we're
101 static int global_volno
= 1; /* volume number to print in external
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
;
113 /* Multi-volume tracking support */
114 static char *save_name
; /* name of the file we are currently writing */
115 static off_t save_totsize
; /* total size of file we are writing, only
116 valid if save_name is nonzero */
117 static off_t save_sizeleft
; /* where we are in the file we are writing,
118 only valid if save_name is nonzero */
121 static struct tar_stat_info dummy
;
124 buffer_write_global_xheader ()
126 xheader_write_global (&dummy
.xhdr
);
130 mv_begin (struct tar_stat_info
*st
)
132 if (multi_volume_option
)
134 assign_string (&save_name
, st
->orig_file_name
);
135 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
142 if (multi_volume_option
)
143 assign_string (&save_name
, 0);
147 mv_total_size (off_t size
)
153 mv_size_left (off_t size
)
155 save_sizeleft
= size
;
162 clear_read_error_count (void)
164 read_error_count
= 0;
168 /* Time-related functions */
175 gettime (&start_time
);
176 volume_start_time
= start_time
;
177 last_stat_time
= start_time
;
181 set_volume_start_time ()
183 gettime (&volume_start_time
);
184 last_stat_time
= volume_start_time
;
192 duration
+= ((now
.tv_sec
- last_stat_time
.tv_sec
)
193 + (now
.tv_nsec
- last_stat_time
.tv_nsec
) / 1e9
);
194 gettime (&last_stat_time
);
198 /* Compression detection */
201 ct_tar
, /* Plain tar file */
202 ct_none
, /* Unknown compression type */
212 enum compress_type type
;
219 static struct zip_magic
const magic
[] = {
222 { ct_compress
, 2, "\037\235", "compress", "-Z" },
223 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
224 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
225 { ct_lzma
, 6, "\xFFLZMA", "lzma", "-J" }, /* FIXME: ???? */
226 { ct_lzop
, 4, "\211LZO", "lzop", "--lzop" },
229 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
231 #define compress_option(t) magic[t].option
232 #define compress_program(t) magic[t].program
234 /* Check if the file ARCHIVE is a compressed archive. */
236 check_compressed_archive (bool *pshort
)
238 struct zip_magic
const *p
;
245 /* Prepare global data needed for find_next_block: */
246 record_end
= record_start
; /* set up for 1st record = # 0 */
247 sfr
= read_full_records
;
248 read_full_records
= true; /* Suppress fatal error on reading a partial
250 *pshort
= find_next_block () == 0;
252 /* Restore global values */
253 read_full_records
= sfr
;
255 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
256 /* Probably a valid header */
259 for (p
= magic
+ 2; p
< magic
+ NMAGIC
; p
++)
260 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
266 /* Open an archive named archive_name_array[0]. Detect if it is
267 a compressed archive of known type and use corresponding decompression
270 open_compressed_archive ()
272 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
273 MODE_RW
, rsh_command_option
);
277 if (!multi_volume_option
)
279 if (!use_compress_program_option
)
282 enum compress_type type
= check_compressed_archive (&shortfile
);
288 ERROR ((0, 0, _("This does not look like a tar archive")));
293 ERROR ((0, 0, _("This does not look like a tar archive")));
294 set_comression_program_by_suffix (archive_name_array
[0], NULL
);
295 if (!use_compress_program_option
)
300 use_compress_program_option
= compress_program (type
);
305 /* FD is not needed any more */
308 hit_eof
= false; /* It might have been set by find_next_block in
309 check_compressed_archive */
311 /* Open compressed archive */
312 child_pid
= sys_child_open_for_uncompress ();
313 read_full_records
= true;
317 record_end
= record_start
; /* set up for 1st record = # 0 */
324 print_stats (FILE *fp
, const char *text
, tarlong numbytes
)
326 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
327 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
328 char rate
[LONGEST_HUMAN_READABLE
+ 1];
330 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
332 sprintf (bytes
, TARLONG_FORMAT
, numbytes
);
334 fprintf (fp
, "%s: %s (%s, %s/s)\n",
336 human_readable (numbytes
, abbr
, human_opts
, 1, 1),
337 (0 < duration
&& numbytes
/ duration
< (uintmax_t) -1
338 ? human_readable (numbytes
/ duration
, rate
, human_opts
, 1, 1)
345 switch (subcommand_option
)
347 case CREATE_SUBCOMMAND
:
349 case UPDATE_SUBCOMMAND
:
350 case APPEND_SUBCOMMAND
:
351 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
352 print_stats (stderr
, _("Total bytes written"),
353 prev_written
+ bytes_written
);
356 case DELETE_SUBCOMMAND
:
358 char buf
[UINTMAX_STRSIZE_BOUND
];
359 print_stats (stderr
, _("Total bytes read"),
360 records_read
* record_size
);
361 print_stats (stderr
, _("Total bytes written"),
362 prev_written
+ bytes_written
);
363 fprintf (stderr
, _("Total bytes deleted: %s\n"),
364 STRINGIFY_BIGINT ((records_read
- records_skipped
)
366 - (prev_written
+ bytes_written
), buf
));
370 case EXTRACT_SUBCOMMAND
:
371 case LIST_SUBCOMMAND
:
372 case DIFF_SUBCOMMAND
:
373 print_stats (stderr
, _("Total bytes read"),
374 records_read
* record_size
);
382 /* Compute and return the block ordinal at current_block. */
384 current_block_ordinal (void)
386 return record_start_block
+ (current_block
- record_start
);
389 /* If the EOF flag is set, reset it, as well as current_block, etc. */
396 current_block
= record_start
;
397 record_end
= record_start
+ blocking_factor
;
398 access_mode
= ACCESS_WRITE
;
402 /* Return the location of the next available input or output block.
403 Return zero for EOF. Once we have returned zero, we just keep returning
404 it, to avoid accidentally going on to the next file on the tape. */
406 find_next_block (void)
408 if (current_block
== record_end
)
413 if (current_block
== record_end
)
419 return current_block
;
422 /* Indicate that we have used all blocks up thru BLOCK. */
424 set_next_block_after (union block
*block
)
426 while (block
>= current_block
)
429 /* Do *not* flush the archive here. If we do, the same argument to
430 set_next_block_after could mean the next block (if the input record
431 is exactly one block long), which is not what is intended. */
433 if (current_block
> record_end
)
437 /* Return the number of bytes comprising the space between POINTER
438 through the end of the current buffer of blocks. This space is
439 available for filling with data, or taking data from. POINTER is
440 usually (but not always) the result of previous find_next_block call. */
442 available_space_after (union block
*pointer
)
444 return record_end
->buffer
- pointer
->buffer
;
447 /* Close file having descriptor FD, and abort if close unsuccessful. */
452 close_error (_("(pipe)"));
458 if (! record_buffer_aligned
[record_index
])
459 record_buffer_aligned
[record_index
] =
460 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
462 record_start
= record_buffer_aligned
[record_index
];
463 current_block
= record_start
;
464 record_end
= record_start
+ blocking_factor
;
467 /* Open an archive file. The argument specifies whether we are
468 reading or writing, or both. */
470 _open_archive (enum access_mode wanted_access
)
472 int backed_up_flag
= 0;
474 if (record_size
== 0)
475 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
477 if (archive_names
== 0)
478 FATAL_ERROR ((0, 0, _("No archive name given")));
480 tar_stat_destroy (¤t_stat_info
);
487 /* When updating the archive, we start with reading. */
488 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
490 read_full_records
= read_full_records_option
;
494 if (use_compress_program_option
)
496 switch (wanted_access
)
499 child_pid
= sys_child_open_for_uncompress ();
500 read_full_records
= true;
501 record_end
= record_start
; /* set up for 1st record = # 0 */
505 child_pid
= sys_child_open_for_compress ();
509 abort (); /* Should not happen */
514 && wanted_access
== ACCESS_WRITE
515 && strcmp (archive_name_array
[0], "-") == 0)
518 else if (strcmp (archive_name_array
[0], "-") == 0)
520 read_full_records
= true; /* could be a pipe, be safe */
522 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
524 switch (wanted_access
)
529 enum compress_type type
;
531 archive
= STDIN_FILENO
;
533 type
= check_compressed_archive (&shortfile
);
534 if (type
!= ct_tar
&& type
!= ct_none
)
536 _("Archive is compressed. Use %s option"),
537 compress_option (type
)));
539 ERROR ((0, 0, _("This does not look like a tar archive")));
544 archive
= STDOUT_FILENO
;
545 if (!index_file_name
)
550 archive
= STDIN_FILENO
;
551 write_archive_to_stdout
= true;
552 record_end
= record_start
; /* set up for 1st record = # 0 */
553 if (!index_file_name
)
558 else if (verify_option
)
559 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
560 MODE_RW
, rsh_command_option
);
562 switch (wanted_access
)
565 archive
= open_compressed_archive ();
571 maybe_backup_file (archive_name_array
[0], 1);
574 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
579 archive
= rmtopen (archive_name_array
[0],
580 O_RDWR
| O_CREAT
| O_BINARY
,
581 MODE_RW
, rsh_command_option
);
583 switch (check_compressed_archive (NULL
))
591 _("Cannot update compressed archives")));
597 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
599 int saved_errno
= errno
;
604 open_fatal (archive_name_array
[0]);
607 sys_detect_dev_null_output ();
608 sys_save_archive_dev_ino ();
609 SET_BINARY_MODE (archive
);
611 switch (wanted_access
)
614 find_next_block (); /* read it in, check for EOF */
624 /* Perform a write to flush the buffer. */
630 checkpoint_run (true);
631 if (tape_length_option
&& tape_length_option
<= bytes_written
)
636 else if (dev_null_output
)
637 status
= record_size
;
639 status
= sys_write_archive_buffer ();
644 /* Handle write errors on the archive. Write errors are always fatal.
645 Hitting the end of a volume does not cause a write error unless the
646 write was the first record of the volume. */
648 archive_write_error (ssize_t status
)
650 /* It might be useful to know how much was written before the error
655 print_total_stats ();
659 write_fatal_details (*archive_name_cursor
, status
, record_size
);
662 /* Handle read errors on the archive. If the read should be retried,
663 return to the caller. */
665 archive_read_error (void)
667 read_error (*archive_name_cursor
);
669 if (record_start_block
== 0)
670 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
672 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
673 then give up on reading the archive. */
675 if (read_error_count
++ > READ_ERROR_MAX
)
676 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
681 short_read (size_t status
)
683 size_t left
; /* bytes left */
684 char *more
; /* pointer to next byte to read */
686 more
= record_start
->buffer
+ status
;
687 left
= record_size
- status
;
689 while (left
% BLOCKSIZE
!= 0
690 || (left
&& status
&& read_full_records
))
693 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
694 archive_read_error ();
699 if (! read_full_records
)
701 unsigned long rest
= record_size
- left
;
704 ngettext ("Unaligned block (%lu byte) in archive",
705 "Unaligned block (%lu bytes) in archive",
710 /* User warned us about this. Fix up. */
716 /* FIXME: for size=0, multi-volume support. On the first record, warn
717 about the problem. */
719 if (!read_full_records
&& verbose_option
> 1
720 && record_start_block
== 0 && status
!= 0)
722 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
724 ngettext ("Record size = %lu block",
725 "Record size = %lu blocks",
730 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
734 /* Flush the current buffer to/from the archive. */
738 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
739 record_start_block
+= record_end
- record_start
;
740 current_block
= record_start
;
741 record_end
= record_start
+ blocking_factor
;
743 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
745 access_mode
= ACCESS_WRITE
;
746 time_to_start_writing
= false;
757 flush_write_ptr (buffer_level
);
765 /* Backspace the archive descriptor by one record worth. If it's a
766 tape, MTIOCTOP will work. If it's something else, try to seek on
767 it. If we can't seek, we lose! */
769 backspace_output (void)
773 struct mtop operation
;
775 operation
.mt_op
= MTBSR
;
776 operation
.mt_count
= 1;
777 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
779 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
785 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
787 /* Seek back to the beginning of this record and start writing there. */
789 position
-= record_size
;
792 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
794 /* Lseek failed. Try a different method. */
797 _("Cannot backspace archive file; it may be unreadable without -i")));
799 /* Replace the first part of the record with NULs. */
801 if (record_start
->buffer
!= output_start
)
802 memset (record_start
->buffer
, 0,
803 output_start
- record_start
->buffer
);
809 seek_archive (off_t size
)
811 off_t start
= current_block_ordinal ();
814 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
816 size
-= skipped
* BLOCKSIZE
;
818 if (size
< record_size
)
822 /* Compute number of records to skip */
823 nrec
= size
/ record_size
;
824 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
828 if (offset
% record_size
)
829 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
831 /* Convert to number of records */
833 /* Compute number of skipped blocks */
834 nblk
= offset
- start
;
836 /* Update buffering info */
837 records_read
+= nblk
/ blocking_factor
;
838 record_start_block
= offset
- blocking_factor
;
839 current_block
= record_end
;
844 /* Close the archive file. */
848 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
851 if (current_block
> record_start
)
855 sys_drain_input_pipe ();
861 if (rmtclose (archive
) != 0)
862 close_error (*archive_name_cursor
);
864 sys_wait_for_child (child_pid
);
866 tar_stat_destroy (¤t_stat_info
);
871 free (record_buffer
[0]);
872 free (record_buffer
[1]);
875 /* Called to initialize the global volume number. */
877 init_volume_number (void)
879 FILE *file
= fopen (volno_file_option
, "r");
883 if (fscanf (file
, "%d", &global_volno
) != 1
885 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
886 quotearg_colon (volno_file_option
)));
888 read_error (volno_file_option
);
889 if (fclose (file
) != 0)
890 close_error (volno_file_option
);
892 else if (errno
!= ENOENT
)
893 open_error (volno_file_option
);
896 /* Called to write out the closing global volume number. */
898 closeout_volume_number (void)
900 FILE *file
= fopen (volno_file_option
, "w");
904 fprintf (file
, "%d\n", global_volno
);
906 write_error (volno_file_option
);
907 if (fclose (file
) != 0)
908 close_error (volno_file_option
);
911 open_error (volno_file_option
);
916 increase_volume_number ()
919 if (global_volno
< 0)
920 FATAL_ERROR ((0, 0, _("Volume number overflow")));
925 change_tape_menu (FILE *read_file
)
927 char *input_buffer
= NULL
;
933 fputc ('\007', stderr
);
935 _("Prepare volume #%d for %s and hit return: "),
936 global_volno
+ 1, quote (*archive_name_cursor
));
939 if (getline (&input_buffer
, &size
, read_file
) <= 0)
941 WARN ((0, 0, _("EOF where user reply was expected")));
943 if (subcommand_option
!= EXTRACT_SUBCOMMAND
944 && subcommand_option
!= LIST_SUBCOMMAND
945 && subcommand_option
!= DIFF_SUBCOMMAND
)
946 WARN ((0, 0, _("WARNING: Archive is incomplete")));
951 if (input_buffer
[0] == '\n'
952 || input_buffer
[0] == 'y'
953 || input_buffer
[0] == 'Y')
956 switch (input_buffer
[0])
960 fprintf (stderr
, _("\
961 n name Give a new file name for the next (and subsequent) volume(s)\n\
963 y or newline Continue operation\n"));
964 if (!restrict_option
)
965 fprintf (stderr
, _(" ! Spawn a subshell\n"));
966 fprintf (stderr
, _(" ? Print this list\n"));
973 WARN ((0, 0, _("No new volume; exiting.\n")));
975 if (subcommand_option
!= EXTRACT_SUBCOMMAND
976 && subcommand_option
!= LIST_SUBCOMMAND
977 && subcommand_option
!= DIFF_SUBCOMMAND
)
978 WARN ((0, 0, _("WARNING: Archive is incomplete")));
983 /* Get new file name. */
989 for (name
= input_buffer
+ 1;
990 *name
== ' ' || *name
== '\t';
994 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1000 /* FIXME: the following allocation is never reclaimed. */
1001 *archive_name_cursor
= xstrdup (name
);
1005 fprintf (stderr
, "%s",
1006 _("File name not specified. Try again.\n"));
1011 if (!restrict_option
)
1019 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
1022 free (input_buffer
);
1025 /* We've hit the end of the old volume. Close it and open the next one.
1026 Return nonzero on success.
1029 new_volume (enum access_mode mode
)
1031 static FILE *read_file
;
1035 if (!read_file
&& !info_script_option
)
1036 /* FIXME: if fopen is used, it will never be closed. */
1037 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1044 assign_string (&volume_label
, NULL
);
1045 assign_string (&continued_file_name
, NULL
);
1046 continued_file_size
= continued_file_offset
= 0;
1047 current_block
= record_start
;
1049 if (rmtclose (archive
) != 0)
1050 close_error (*archive_name_cursor
);
1052 archive_name_cursor
++;
1053 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1055 archive_name_cursor
= archive_name_array
;
1063 /* We have to prompt from now on. */
1065 if (info_script_option
)
1067 if (volno_file_option
)
1068 closeout_volume_number ();
1069 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1070 FATAL_ERROR ((0, 0, _("%s command failed"),
1071 quote (info_script_option
)));
1074 change_tape_menu (read_file
);
1077 if (strcmp (archive_name_cursor
[0], "-") == 0)
1079 read_full_records
= true;
1080 archive
= STDIN_FILENO
;
1082 else if (verify_option
)
1083 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1084 rsh_command_option
);
1089 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1090 rsh_command_option
);
1095 maybe_backup_file (*archive_name_cursor
, 1);
1096 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1097 rsh_command_option
);
1101 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1102 rsh_command_option
);
1108 open_warn (*archive_name_cursor
);
1109 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1110 undo_last_backup ();
1115 SET_BINARY_MODE (archive
);
1121 read_header0 (struct tar_stat_info
*info
)
1123 enum read_header rc
;
1125 tar_stat_init (info
);
1126 rc
= read_header_primitive (false, info
);
1127 if (rc
== HEADER_SUCCESS
)
1129 set_next_block_after (current_header
);
1132 ERROR ((0, 0, _("This does not look like a tar archive")));
1140 union block
*header
;
1141 enum access_mode acc
;
1143 switch (subcommand_option
)
1145 case APPEND_SUBCOMMAND
:
1146 case CAT_SUBCOMMAND
:
1147 case UPDATE_SUBCOMMAND
:
1148 acc
= ACCESS_UPDATE
;
1156 if (!new_volume (acc
))
1159 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1161 archive_read_error ();
1163 if (status
!= record_size
)
1164 short_read (status
);
1166 header
= find_next_block ();
1170 switch (header
->header
.typeflag
)
1174 if (!read_header0 (&dummy
))
1176 xheader_decode (&dummy
); /* decodes values from the global header */
1177 tar_stat_destroy (&dummy
);
1180 /* We have read the extended header of the first member in
1181 this volume. Put it back, so next read_header works as
1183 current_block
= record_start
;
1188 case GNUTYPE_VOLHDR
:
1189 if (!read_header0 (&dummy
))
1191 tar_stat_destroy (&dummy
);
1192 assign_string (&volume_label
, current_header
->header
.name
);
1193 set_next_block_after (header
);
1194 header
= find_next_block ();
1195 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1199 case GNUTYPE_MULTIVOL
:
1200 if (!read_header0 (&dummy
))
1202 tar_stat_destroy (&dummy
);
1203 assign_string (&continued_file_name
, current_header
->header
.name
);
1204 continued_file_size
=
1205 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1206 continued_file_offset
=
1207 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1217 if (!continued_file_name
1218 || strcmp (continued_file_name
, real_s_name
))
1220 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1221 && strlen (real_s_name
) >= NAME_FIELD_SIZE
1222 && strncmp (continued_file_name
, real_s_name
,
1223 NAME_FIELD_SIZE
) == 0)
1225 _("%s is possibly continued on this volume: header contains truncated name"),
1226 quote (real_s_name
)));
1229 WARN ((0, 0, _("%s is not continued on this volume"),
1230 quote (real_s_name
)));
1235 s
= continued_file_size
+ continued_file_offset
;
1237 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1239 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1240 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1241 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1243 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1244 quote (continued_file_name
),
1245 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1246 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1247 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1251 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1253 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1254 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1255 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1257 WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
1258 STRINGIFY_BIGINT (real_s_totsize
, totsizebuf
),
1259 STRINGIFY_BIGINT (real_s_sizeleft
, s1buf
),
1260 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1266 increase_volume_number ();
1271 /* Check the LABEL block against the volume label, seen as a globbing
1272 pattern. Return true if the pattern matches. In case of failure,
1273 retry matching a volume sequence number before giving up in
1274 multi-volume mode. */
1276 check_label_pattern (union block
*label
)
1281 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1284 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1287 if (!multi_volume_option
)
1290 string
= xmalloc (strlen (volume_label_option
)
1291 + sizeof VOLUME_LABEL_APPEND
+ 1);
1292 strcpy (string
, volume_label_option
);
1293 strcat (string
, VOLUME_LABEL_APPEND
);
1294 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1299 /* Check if the next block contains a volume label and if this matches
1300 the one given in the command line */
1302 match_volume_label (void)
1304 union block
*label
= find_next_block ();
1307 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1308 quote (volume_label_option
)));
1309 if (!check_label_pattern (label
))
1310 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1311 quote_n (0, label
->header
.name
),
1312 quote_n (1, volume_label_option
)));
1315 /* Mark the archive with volume label STR. */
1317 _write_volume_label (const char *str
)
1319 if (archive_format
== POSIX_FORMAT
)
1320 xheader_store ("GNU.volume.label", &dummy
, str
);
1323 union block
*label
= find_next_block ();
1325 memset (label
, 0, BLOCKSIZE
);
1327 strcpy (label
->header
.name
, volume_label_option
);
1328 assign_string (¤t_stat_info
.file_name
,
1329 label
->header
.name
);
1330 current_stat_info
.had_trailing_slash
=
1331 strip_trailing_slashes (current_stat_info
.file_name
);
1333 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1334 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1335 finish_header (¤t_stat_info
, label
, -1);
1336 set_next_block_after (label
);
1340 #define VOL_SUFFIX "Volume"
1342 /* Add a volume label to a part of multi-volume archive */
1344 add_volume_label (void)
1346 char buf
[UINTMAX_STRSIZE_BOUND
];
1347 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1348 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1350 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1351 _write_volume_label (s
);
1358 if (archive_format
== POSIX_FORMAT
)
1360 off_t block_ordinal
;
1362 struct tar_stat_info st
;
1363 static size_t real_s_part_no
; /* FIXME */
1366 memset (&st
, 0, sizeof st
);
1367 st
.orig_file_name
= st
.file_name
= real_s_name
;
1368 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1369 st
.stat
.st_uid
= getuid ();
1370 st
.stat
.st_gid
= getgid ();
1371 st
.orig_file_name
= xheader_format_name (&st
,
1372 "%d/GNUFileParts.%p/%f.%n",
1374 st
.file_name
= st
.orig_file_name
;
1375 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1377 block_ordinal
= current_block_ordinal ();
1378 blk
= start_header (&st
);
1380 abort (); /* FIXME */
1381 finish_header (&st
, blk
, block_ordinal
);
1382 free (st
.orig_file_name
);
1387 /* Add a volume label to the current archive */
1389 write_volume_label (void)
1391 if (multi_volume_option
)
1392 add_volume_label ();
1394 _write_volume_label (volume_label_option
);
1397 /* Write GNU multi-volume header */
1399 gnu_add_multi_volume_header (void)
1402 union block
*block
= find_next_block ();
1404 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1406 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1407 quotearg_colon (real_s_name
)));
1409 memset (block
, 0, BLOCKSIZE
);
1411 /* FIXME: Michael P Urban writes: [a long name file] is being written
1412 when a new volume rolls around [...] Looks like the wrong value is
1413 being preserved in real_s_name, though. */
1415 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1416 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1418 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1419 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1420 block
->oldgnu_header
.offset
);
1422 tmp
= verbose_option
;
1424 finish_header (¤t_stat_info
, block
, -1);
1425 verbose_option
= tmp
;
1426 set_next_block_after (block
);
1429 /* Add a multi volume header to the current archive. The exact header format
1430 depends on the archive format. */
1432 add_multi_volume_header (void)
1434 if (archive_format
== POSIX_FORMAT
)
1436 off_t d
= real_s_totsize
- real_s_sizeleft
;
1437 xheader_store ("GNU.volume.filename", &dummy
, real_s_name
);
1438 xheader_store ("GNU.volume.size", &dummy
, &real_s_sizeleft
);
1439 xheader_store ("GNU.volume.offset", &dummy
, &d
);
1442 gnu_add_multi_volume_header ();
1445 /* Synchronize multi-volume globals */
1447 multi_volume_sync ()
1449 if (multi_volume_option
)
1453 assign_string (&real_s_name
,
1454 safer_name_suffix (save_name
, false,
1455 absolute_names_option
));
1456 real_s_totsize
= save_totsize
;
1457 real_s_sizeleft
= save_sizeleft
;
1461 assign_string (&real_s_name
, 0);
1463 real_s_sizeleft
= 0;
1469 /* Low-level flush functions */
1471 /* Simple flush read (no multi-volume or label extensions) */
1473 simple_flush_read (void)
1475 size_t status
; /* result from system call */
1477 checkpoint_run (false);
1479 /* Clear the count of errors. This only applies to a single call to
1482 read_error_count
= 0; /* clear error count */
1484 if (write_archive_to_stdout
&& record_start_block
!= 0)
1486 archive
= STDOUT_FILENO
;
1487 status
= sys_write_archive_buffer ();
1488 archive
= STDIN_FILENO
;
1489 if (status
!= record_size
)
1490 archive_write_error (status
);
1495 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1496 if (status
== record_size
)
1501 if (status
== SAFE_READ_ERROR
)
1503 archive_read_error ();
1504 continue; /* try again */
1508 short_read (status
);
1511 /* Simple flush write (no multi-volume or label extensions) */
1513 simple_flush_write (size_t level
__attribute__((unused
)))
1517 status
= _flush_write ();
1518 if (status
!= record_size
)
1519 archive_write_error (status
);
1523 bytes_written
+= status
;
1528 /* GNU flush functions. These support multi-volume and archive labels in
1529 GNU and PAX archive formats. */
1532 _gnu_flush_read (void)
1534 size_t status
; /* result from system call */
1536 checkpoint_run (false);
1538 /* Clear the count of errors. This only applies to a single call to
1541 read_error_count
= 0; /* clear error count */
1543 if (write_archive_to_stdout
&& record_start_block
!= 0)
1545 archive
= STDOUT_FILENO
;
1546 status
= sys_write_archive_buffer ();
1547 archive
= STDIN_FILENO
;
1548 if (status
!= record_size
)
1549 archive_write_error (status
);
1552 multi_volume_sync ();
1556 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1557 if (status
== record_size
)
1563 /* The condition below used to include
1564 || (status > 0 && !read_full_records)
1565 This is incorrect since even if new_volume() succeeds, the
1566 subsequent call to rmtread will overwrite the chunk of data
1567 already read in the buffer, so the processing will fail */
1569 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1570 && multi_volume_option
)
1572 while (!try_new_volume ())
1576 else if (status
== SAFE_READ_ERROR
)
1578 archive_read_error ();
1583 short_read (status
);
1587 gnu_flush_read (void)
1589 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1591 flush_read_ptr
= gnu_flush_read
;
1595 _gnu_flush_write (size_t buffer_level
)
1598 union block
*header
;
1604 status
= _flush_write ();
1605 if (status
!= record_size
&& !multi_volume_option
)
1606 archive_write_error (status
);
1611 bytes_written
+= status
;
1614 if (status
== record_size
)
1616 multi_volume_sync ();
1620 if (status
% BLOCKSIZE
)
1622 ERROR ((0, 0, _("write did not end on a block boundary")));
1623 archive_write_error (status
);
1626 /* In multi-volume mode. */
1627 /* ENXIO is for the UNIX PC. */
1628 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1629 archive_write_error (status
);
1631 real_s_sizeleft
-= status
;
1632 if (!new_volume (ACCESS_WRITE
))
1635 tar_stat_destroy (&dummy
);
1637 increase_volume_number ();
1638 prev_written
+= bytes_written
;
1641 copy_ptr
= record_start
->buffer
+ status
;
1642 copy_size
= buffer_level
- status
;
1644 /* Switch to the next buffer */
1645 record_index
= !record_index
;
1648 if (volume_label_option
)
1649 add_volume_label ();
1652 add_multi_volume_header ();
1654 write_extended (true, &dummy
, find_next_block ());
1655 tar_stat_destroy (&dummy
);
1658 add_chunk_header ();
1659 wrt
= bytes_written
;
1660 header
= find_next_block ();
1661 bufsize
= available_space_after (header
);
1662 while (bufsize
< copy_size
)
1664 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1665 copy_ptr
+= bufsize
;
1666 copy_size
-= bufsize
;
1667 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1668 header
= find_next_block ();
1669 bufsize
= available_space_after (header
);
1671 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1672 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1673 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1674 if (multi_volume_option
&& wrt
< bytes_written
)
1676 /* The value of bytes_written has changed while moving data;
1677 that means that flush_archive was executed at least once in
1678 between, and, as a consequence, copy_size bytes were not written
1679 to disk. We need to update sizeleft variables to compensate for
1681 save_sizeleft
+= copy_size
;
1682 multi_volume_sync ();
1688 gnu_flush_write (size_t buffer_level
)
1690 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1691 _gnu_flush_write (buffer_level
);
1692 flush_write_ptr
= gnu_flush_write
;
1704 flush_write_ptr (record_size
);
1708 open_archive (enum access_mode wanted_access
)
1710 flush_read_ptr
= gnu_flush_read
;
1711 flush_write_ptr
= gnu_flush_write
;
1713 _open_archive (wanted_access
);
1714 switch (wanted_access
)
1717 if (volume_label_option
)
1718 match_volume_label ();
1722 records_written
= 0;
1723 if (volume_label_option
)
1724 write_volume_label ();
1730 set_volume_start_time ();