1 /* Buffer management for tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007 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>
36 /* Number of retries before giving up on read. */
37 #define READ_ERROR_MAX 10
39 /* Globbing pattern to append to volume label if initial match failed. */
40 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
44 static tarlong prev_written
; /* bytes written on previous volumes */
45 static tarlong bytes_written
; /* bytes written on this volume */
46 static void *record_buffer
[2]; /* allocated memory */
47 union block
*record_buffer_aligned
[2];
48 static int record_index
;
50 /* FIXME: The following variables should ideally be static to this
51 module. However, this cannot be done yet. The cleanup continues! */
53 union block
*record_start
; /* start of record of archive */
54 union block
*record_end
; /* last+1 block of archive record */
55 union block
*current_block
; /* current block of archive */
56 enum access_mode access_mode
; /* how do we handle the archive */
57 off_t records_read
; /* number of records read from this archive */
58 off_t records_written
; /* likewise, for records written */
59 extern off_t records_skipped
; /* number of records skipped at the start
60 of the archive, defined in delete.c */
62 static off_t record_start_block
; /* block ordinal at record_start */
64 /* Where we write list messages (not errors, not interactions) to. */
67 static void backspace_output (void);
69 /* PID of child program, if compress_option or remote archive access. */
70 static pid_t child_pid
;
72 /* Error recovery stuff */
73 static int read_error_count
;
75 /* Have we hit EOF yet? */
78 /* Checkpointing counter */
79 static unsigned checkpoint
;
81 static bool read_full_records
= false;
83 /* We're reading, but we just read the last block and it's time to update.
86 As least EXTERN like this one as possible. (?? --gray)
87 FIXME: Either eliminate it or move it to common.h.
89 extern bool time_to_start_writing
;
91 bool write_archive_to_stdout
;
93 void (*flush_write_ptr
) (size_t);
94 void (*flush_read_ptr
) (void);
98 char *continued_file_name
;
99 uintmax_t continued_file_size
;
100 uintmax_t continued_file_offset
;
103 static int volno
= 1; /* which volume of a multi-volume tape we're
105 static int global_volno
= 1; /* volume number to print in external
108 bool write_archive_to_stdout
;
110 /* Used by flush_read and flush_write to store the real info about saved
112 static char *real_s_name
;
113 static off_t real_s_totsize
;
114 static off_t real_s_sizeleft
;
117 /* Multi-volume tracking support */
118 static char *save_name
; /* name of the file we are currently writing */
119 static off_t save_totsize
; /* total size of file we are writing, only
120 valid if save_name is nonzero */
121 static off_t save_sizeleft
; /* where we are in the file we are writing,
122 only valid if save_name is nonzero */
125 static struct tar_stat_info dummy
;
128 buffer_write_global_xheader ()
130 xheader_write_global (&dummy
.xhdr
);
134 mv_begin (struct tar_stat_info
*st
)
136 if (multi_volume_option
)
138 assign_string (&save_name
, st
->orig_file_name
);
139 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
146 if (multi_volume_option
)
147 assign_string (&save_name
, 0);
151 mv_total_size (off_t size
)
157 mv_size_left (off_t size
)
159 save_sizeleft
= size
;
166 clear_read_error_count (void)
168 read_error_count
= 0;
172 /* Time-related functions */
179 gettime (&start_time
);
180 volume_start_time
= start_time
;
181 last_stat_time
= start_time
;
185 set_volume_start_time ()
187 gettime (&volume_start_time
);
188 last_stat_time
= volume_start_time
;
196 duration
+= ((now
.tv_sec
- last_stat_time
.tv_sec
)
197 + (now
.tv_nsec
- last_stat_time
.tv_nsec
) / 1e9
);
198 gettime (&last_stat_time
);
202 /* Compression detection */
213 enum compress_type type
;
220 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" },
227 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
229 #define compress_option(t) magic[t].option
230 #define compress_program(t) magic[t].program
232 /* Check if the file ARCHIVE is a compressed archive. */
234 check_compressed_archive ()
236 struct zip_magic
const *p
;
238 bool short_file
= false;
240 /* Prepare global data needed for find_next_block: */
241 record_end
= record_start
; /* set up for 1st record = # 0 */
242 sfr
= read_full_records
;
243 read_full_records
= true; /* Suppress fatal error on reading a partial
245 if (find_next_block () == 0)
248 /* Restore global values */
249 read_full_records
= sfr
;
251 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
252 /* Probably a valid header */
255 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
256 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
260 ERROR ((0, 0, _("This does not look like a tar archive")));
265 /* Open an archive named archive_name_array[0]. Detect if it is
266 a compressed archive of known type and use corresponding decompression
269 open_compressed_archive ()
271 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
272 MODE_RW
, rsh_command_option
);
276 if (!multi_volume_option
)
278 enum compress_type type
= check_compressed_archive ();
283 /* FD is not needed any more */
286 hit_eof
= false; /* It might have been set by find_next_block in
287 check_compressed_archive */
289 /* Open compressed archive */
290 use_compress_program_option
= compress_program (type
);
291 child_pid
= sys_child_open_for_uncompress ();
292 read_full_records
= true;
296 record_end
= record_start
; /* set up for 1st record = # 0 */
303 print_stats (FILE *fp
, const char *text
, tarlong numbytes
)
305 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
306 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
307 char rate
[LONGEST_HUMAN_READABLE
+ 1];
309 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
311 sprintf (bytes
, TARLONG_FORMAT
, numbytes
);
313 fprintf (fp
, "%s: %s (%s, %s/s)\n",
315 human_readable (numbytes
, abbr
, human_opts
, 1, 1),
316 (0 < duration
&& numbytes
/ duration
< (uintmax_t) -1
317 ? human_readable (numbytes
/ duration
, rate
, human_opts
, 1, 1)
324 switch (subcommand_option
)
326 case CREATE_SUBCOMMAND
:
328 case UPDATE_SUBCOMMAND
:
329 case APPEND_SUBCOMMAND
:
330 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
331 print_stats (stderr
, _("Total bytes written"),
332 prev_written
+ bytes_written
);
335 case DELETE_SUBCOMMAND
:
337 char buf
[UINTMAX_STRSIZE_BOUND
];
338 print_stats (stderr
, _("Total bytes read"),
339 records_read
* record_size
);
340 print_stats (stderr
, _("Total bytes written"),
341 prev_written
+ bytes_written
);
342 fprintf (stderr
, _("Total bytes deleted: %s\n"),
343 STRINGIFY_BIGINT ((records_read
- records_skipped
)
345 - (prev_written
+ bytes_written
), buf
));
349 case EXTRACT_SUBCOMMAND
:
350 case LIST_SUBCOMMAND
:
351 case DIFF_SUBCOMMAND
:
352 print_stats (stderr
, _("Total bytes read"),
353 records_read
* record_size
);
361 /* Compute and return the block ordinal at current_block. */
363 current_block_ordinal (void)
365 return record_start_block
+ (current_block
- record_start
);
368 /* If the EOF flag is set, reset it, as well as current_block, etc. */
375 current_block
= record_start
;
376 record_end
= record_start
+ blocking_factor
;
377 access_mode
= ACCESS_WRITE
;
381 /* Return the location of the next available input or output block.
382 Return zero for EOF. Once we have returned zero, we just keep returning
383 it, to avoid accidentally going on to the next file on the tape. */
385 find_next_block (void)
387 if (current_block
== record_end
)
392 if (current_block
== record_end
)
398 return current_block
;
401 /* Indicate that we have used all blocks up thru BLOCK. */
403 set_next_block_after (union block
*block
)
405 while (block
>= current_block
)
408 /* Do *not* flush the archive here. If we do, the same argument to
409 set_next_block_after could mean the next block (if the input record
410 is exactly one block long), which is not what is intended. */
412 if (current_block
> record_end
)
416 /* Return the number of bytes comprising the space between POINTER
417 through the end of the current buffer of blocks. This space is
418 available for filling with data, or taking data from. POINTER is
419 usually (but not always) the result of previous find_next_block call. */
421 available_space_after (union block
*pointer
)
423 return record_end
->buffer
- pointer
->buffer
;
426 /* Close file having descriptor FD, and abort if close unsuccessful. */
431 close_error (_("(pipe)"));
437 if (! record_buffer_aligned
[record_index
])
438 record_buffer_aligned
[record_index
] =
439 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
441 record_start
= record_buffer_aligned
[record_index
];
442 current_block
= record_start
;
443 record_end
= record_start
+ blocking_factor
;
446 /* Open an archive file. The argument specifies whether we are
447 reading or writing, or both. */
449 _open_archive (enum access_mode wanted_access
)
451 int backed_up_flag
= 0;
453 if (record_size
== 0)
454 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
456 if (archive_names
== 0)
457 FATAL_ERROR ((0, 0, _("No archive name given")));
459 tar_stat_destroy (¤t_stat_info
);
466 /* When updating the archive, we start with reading. */
467 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
469 read_full_records
= read_full_records_option
;
473 if (use_compress_program_option
)
475 switch (wanted_access
)
478 child_pid
= sys_child_open_for_uncompress ();
479 read_full_records
= true;
480 record_end
= record_start
; /* set up for 1st record = # 0 */
484 child_pid
= sys_child_open_for_compress ();
488 abort (); /* Should not happen */
493 && wanted_access
== ACCESS_WRITE
494 && strcmp (archive_name_array
[0], "-") == 0)
497 else if (strcmp (archive_name_array
[0], "-") == 0)
499 read_full_records
= true; /* could be a pipe, be safe */
501 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
503 switch (wanted_access
)
507 enum compress_type type
;
509 archive
= STDIN_FILENO
;
511 type
= check_compressed_archive ();
514 _("Archive is compressed. Use %s option"),
515 compress_option (type
)));
520 archive
= STDOUT_FILENO
;
521 if (!index_file_name
)
526 archive
= STDIN_FILENO
;
527 write_archive_to_stdout
= true;
528 record_end
= record_start
; /* set up for 1st record = # 0 */
529 if (!index_file_name
)
534 else if (verify_option
)
535 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
536 MODE_RW
, rsh_command_option
);
538 switch (wanted_access
)
541 archive
= open_compressed_archive ();
547 maybe_backup_file (archive_name_array
[0], 1);
550 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
555 archive
= rmtopen (archive_name_array
[0],
556 O_RDWR
| O_CREAT
| O_BINARY
,
557 MODE_RW
, rsh_command_option
);
559 if (check_compressed_archive () != ct_none
)
561 _("Cannot update compressed archives")));
566 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
568 int saved_errno
= errno
;
573 open_fatal (archive_name_array
[0]);
576 sys_detect_dev_null_output ();
577 sys_save_archive_dev_ino ();
578 SET_BINARY_MODE (archive
);
580 switch (wanted_access
)
583 find_next_block (); /* read it in, check for EOF */
594 do_checkpoint (bool write
)
596 if (checkpoint_option
&& !(++checkpoint
% checkpoint_option
))
598 switch (checkpoint_style
)
605 case checkpoint_text
:
607 /* TRANSLATORS: This is a ``checkpoint of write operation'',
608 *not* ``Writing a checkpoint''.
609 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
610 *not* ``Escribiendo un punto de comprobaci@'on'' */
611 WARN ((0, 0, _("Write checkpoint %u"), checkpoint
));
613 /* TRANSLATORS: This is a ``checkpoint of read operation'',
614 *not* ``Reading a checkpoint''.
615 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
616 *not* ``Leyendo un punto de comprobaci@'on'' */
617 WARN ((0, 0, _("Read checkpoint %u"), checkpoint
));
623 /* Perform a write to flush the buffer. */
629 do_checkpoint (true);
630 if (tape_length_option
&& tape_length_option
<= bytes_written
)
635 else if (dev_null_output
)
636 status
= record_size
;
638 status
= sys_write_archive_buffer ();
643 /* Handle write errors on the archive. Write errors are always fatal.
644 Hitting the end of a volume does not cause a write error unless the
645 write was the first record of the volume. */
647 archive_write_error (ssize_t status
)
649 /* It might be useful to know how much was written before the error
654 print_total_stats ();
658 write_fatal_details (*archive_name_cursor
, status
, record_size
);
661 /* Handle read errors on the archive. If the read should be retried,
662 return to the caller. */
664 archive_read_error (void)
666 read_error (*archive_name_cursor
);
668 if (record_start_block
== 0)
669 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
671 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
672 then give up on reading the archive. */
674 if (read_error_count
++ > READ_ERROR_MAX
)
675 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
680 short_read (size_t status
)
682 size_t left
; /* bytes left */
683 char *more
; /* pointer to next byte to read */
685 more
= record_start
->buffer
+ status
;
686 left
= record_size
- status
;
688 while (left
% BLOCKSIZE
!= 0
689 || (left
&& status
&& read_full_records
))
692 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
693 archive_read_error ();
698 if (! read_full_records
)
700 unsigned long rest
= record_size
- left
;
703 ngettext ("Unaligned block (%lu byte) in archive",
704 "Unaligned block (%lu bytes) in archive",
709 /* User warned us about this. Fix up. */
715 /* FIXME: for size=0, multi-volume support. On the first record, warn
716 about the problem. */
718 if (!read_full_records
&& verbose_option
> 1
719 && record_start_block
== 0 && status
!= 0)
721 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
723 ngettext ("Record size = %lu block",
724 "Record size = %lu blocks",
729 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
733 /* Flush the current buffer to/from the archive. */
737 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
738 record_start_block
+= record_end
- record_start
;
739 current_block
= record_start
;
740 record_end
= record_start
+ blocking_factor
;
742 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
744 access_mode
= ACCESS_WRITE
;
745 time_to_start_writing
= false;
756 flush_write_ptr (buffer_level
);
764 /* Backspace the archive descriptor by one record worth. If it's a
765 tape, MTIOCTOP will work. If it's something else, try to seek on
766 it. If we can't seek, we lose! */
768 backspace_output (void)
772 struct mtop operation
;
774 operation
.mt_op
= MTBSR
;
775 operation
.mt_count
= 1;
776 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
778 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
784 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
786 /* Seek back to the beginning of this record and start writing there. */
788 position
-= record_size
;
791 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
793 /* Lseek failed. Try a different method. */
796 _("Cannot backspace archive file; it may be unreadable without -i")));
798 /* Replace the first part of the record with NULs. */
800 if (record_start
->buffer
!= output_start
)
801 memset (record_start
->buffer
, 0,
802 output_start
- record_start
->buffer
);
808 seek_archive (off_t size
)
810 off_t start
= current_block_ordinal ();
813 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
815 size
-= skipped
* BLOCKSIZE
;
817 if (size
< record_size
)
821 /* Compute number of records to skip */
822 nrec
= size
/ record_size
;
823 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
827 if (offset
% record_size
)
828 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
830 /* Convert to number of records */
832 /* Compute number of skipped blocks */
833 nblk
= offset
- start
;
835 /* Update buffering info */
836 records_read
+= nblk
/ blocking_factor
;
837 record_start_block
= offset
- blocking_factor
;
838 current_block
= record_end
;
843 /* Close the archive file. */
847 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
850 if (current_block
> record_start
)
854 sys_drain_input_pipe ();
860 if (rmtclose (archive
) != 0)
861 close_warn (*archive_name_cursor
);
863 sys_wait_for_child (child_pid
);
865 tar_stat_destroy (¤t_stat_info
);
870 free (record_buffer
[0]);
871 free (record_buffer
[1]);
874 /* Called to initialize the global volume number. */
876 init_volume_number (void)
878 FILE *file
= fopen (volno_file_option
, "r");
882 if (fscanf (file
, "%d", &global_volno
) != 1
884 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
885 quotearg_colon (volno_file_option
)));
887 read_error (volno_file_option
);
888 if (fclose (file
) != 0)
889 close_error (volno_file_option
);
891 else if (errno
!= ENOENT
)
892 open_error (volno_file_option
);
895 /* Called to write out the closing global volume number. */
897 closeout_volume_number (void)
899 FILE *file
= fopen (volno_file_option
, "w");
903 fprintf (file
, "%d\n", global_volno
);
905 write_error (volno_file_option
);
906 if (fclose (file
) != 0)
907 close_error (volno_file_option
);
910 open_error (volno_file_option
);
915 increase_volume_number ()
918 if (global_volno
< 0)
919 FATAL_ERROR ((0, 0, _("Volume number overflow")));
924 change_tape_menu (FILE *read_file
)
926 char *input_buffer
= NULL
;
932 fputc ('\007', stderr
);
934 _("Prepare volume #%d for %s and hit return: "),
935 global_volno
+ 1, quote (*archive_name_cursor
));
938 if (getline (&input_buffer
, &size
, read_file
) <= 0)
940 WARN ((0, 0, _("EOF where user reply was expected")));
942 if (subcommand_option
!= EXTRACT_SUBCOMMAND
943 && subcommand_option
!= LIST_SUBCOMMAND
944 && subcommand_option
!= DIFF_SUBCOMMAND
)
945 WARN ((0, 0, _("WARNING: Archive is incomplete")));
950 if (input_buffer
[0] == '\n'
951 || input_buffer
[0] == 'y'
952 || input_buffer
[0] == 'Y')
955 switch (input_buffer
[0])
959 fprintf (stderr
, _("\
960 n name Give a new file name for the next (and subsequent) volume(s)\n\
962 y or newline Continue operation\n"));
963 if (!restrict_option
)
964 fprintf (stderr
, _(" ! Spawn a subshell\n"));
965 fprintf (stderr
, _(" ? Print this list\n"));
972 WARN ((0, 0, _("No new volume; exiting.\n")));
974 if (subcommand_option
!= EXTRACT_SUBCOMMAND
975 && subcommand_option
!= LIST_SUBCOMMAND
976 && subcommand_option
!= DIFF_SUBCOMMAND
)
977 WARN ((0, 0, _("WARNING: Archive is incomplete")));
982 /* Get new file name. */
988 for (name
= input_buffer
+ 1;
989 *name
== ' ' || *name
== '\t';
993 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
999 /* FIXME: the following allocation is never reclaimed. */
1000 *archive_name_cursor
= xstrdup (name
);
1004 fprintf (stderr
, "%s",
1005 _("File name not specified. Try again.\n"));
1010 if (!restrict_option
)
1018 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
1021 free (input_buffer
);
1024 /* We've hit the end of the old volume. Close it and open the next one.
1025 Return nonzero on success.
1028 new_volume (enum access_mode mode
)
1030 static FILE *read_file
;
1034 if (!read_file
&& !info_script_option
)
1035 /* FIXME: if fopen is used, it will never be closed. */
1036 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1043 assign_string (&volume_label
, NULL
);
1044 assign_string (&continued_file_name
, NULL
);
1045 continued_file_size
= continued_file_offset
= 0;
1046 current_block
= record_start
;
1048 if (rmtclose (archive
) != 0)
1049 close_warn (*archive_name_cursor
);
1051 archive_name_cursor
++;
1052 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1054 archive_name_cursor
= archive_name_array
;
1062 /* We have to prompt from now on. */
1064 if (info_script_option
)
1066 if (volno_file_option
)
1067 closeout_volume_number ();
1068 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1069 FATAL_ERROR ((0, 0, _("%s command failed"),
1070 quote (info_script_option
)));
1073 change_tape_menu (read_file
);
1076 if (strcmp (archive_name_cursor
[0], "-") == 0)
1078 read_full_records
= true;
1079 archive
= STDIN_FILENO
;
1081 else if (verify_option
)
1082 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1083 rsh_command_option
);
1088 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1089 rsh_command_option
);
1094 maybe_backup_file (*archive_name_cursor
, 1);
1095 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1096 rsh_command_option
);
1100 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1101 rsh_command_option
);
1107 open_warn (*archive_name_cursor
);
1108 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1109 undo_last_backup ();
1114 SET_BINARY_MODE (archive
);
1120 read_header0 (struct tar_stat_info
*info
)
1122 enum read_header rc
;
1124 tar_stat_init (info
);
1125 rc
= read_header_primitive (false, info
);
1126 if (rc
== HEADER_SUCCESS
)
1128 set_next_block_after (current_header
);
1131 ERROR ((0, 0, _("This does not look like a tar archive")));
1139 union block
*header
;
1142 switch (subcommand_option
)
1144 case APPEND_SUBCOMMAND
:
1145 case CAT_SUBCOMMAND
:
1146 case UPDATE_SUBCOMMAND
:
1147 access
= ACCESS_UPDATE
;
1151 access
= ACCESS_READ
;
1155 if (!new_volume (access
))
1158 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1160 archive_read_error ();
1162 if (status
!= record_size
)
1163 short_read (status
);
1165 header
= find_next_block ();
1169 switch (header
->header
.typeflag
)
1173 if (!read_header0 (&dummy
))
1175 xheader_decode (&dummy
); /* decodes values from the global header */
1176 tar_stat_destroy (&dummy
);
1179 /* We have read the extended header of the first member in
1180 this volume. Put it back, so next read_header works as
1182 current_block
= record_start
;
1187 case GNUTYPE_VOLHDR
:
1188 if (!read_header0 (&dummy
))
1190 tar_stat_destroy (&dummy
);
1191 assign_string (&volume_label
, current_header
->header
.name
);
1192 set_next_block_after (header
);
1193 header
= find_next_block ();
1194 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1198 case GNUTYPE_MULTIVOL
:
1199 if (!read_header0 (&dummy
))
1201 tar_stat_destroy (&dummy
);
1202 assign_string (&continued_file_name
, current_header
->header
.name
);
1203 continued_file_size
=
1204 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1205 continued_file_offset
=
1206 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1216 if (!continued_file_name
1217 || strcmp (continued_file_name
, real_s_name
))
1219 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1220 && strlen (real_s_name
) >= NAME_FIELD_SIZE
1221 && strncmp (continued_file_name
, real_s_name
,
1222 NAME_FIELD_SIZE
) == 0)
1224 _("%s is possibly continued on this volume: header contains truncated name"),
1225 quote (real_s_name
)));
1228 WARN ((0, 0, _("%s is not continued on this volume"),
1229 quote (real_s_name
)));
1234 s
= continued_file_size
+ continued_file_offset
;
1236 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1238 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1239 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1240 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1242 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1243 quote (continued_file_name
),
1244 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1245 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1246 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1250 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1252 WARN ((0, 0, _("This volume is out of sequence")));
1257 increase_volume_number ();
1262 /* Check the LABEL block against the volume label, seen as a globbing
1263 pattern. Return true if the pattern matches. In case of failure,
1264 retry matching a volume sequence number before giving up in
1265 multi-volume mode. */
1267 check_label_pattern (union block
*label
)
1272 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1275 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1278 if (!multi_volume_option
)
1281 string
= xmalloc (strlen (volume_label_option
)
1282 + sizeof VOLUME_LABEL_APPEND
+ 1);
1283 strcpy (string
, volume_label_option
);
1284 strcat (string
, VOLUME_LABEL_APPEND
);
1285 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1290 /* Check if the next block contains a volume label and if this matches
1291 the one given in the command line */
1293 match_volume_label (void)
1295 union block
*label
= find_next_block ();
1298 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1299 quote (volume_label_option
)));
1300 if (!check_label_pattern (label
))
1301 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1302 quote_n (0, label
->header
.name
),
1303 quote_n (1, volume_label_option
)));
1306 /* Mark the archive with volume label STR. */
1308 _write_volume_label (const char *str
)
1310 if (archive_format
== POSIX_FORMAT
)
1311 xheader_store ("GNU.volume.label", &dummy
, str
);
1314 union block
*label
= find_next_block ();
1316 memset (label
, 0, BLOCKSIZE
);
1318 strcpy (label
->header
.name
, volume_label_option
);
1319 assign_string (¤t_stat_info
.file_name
,
1320 label
->header
.name
);
1321 current_stat_info
.had_trailing_slash
=
1322 strip_trailing_slashes (current_stat_info
.file_name
);
1324 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1325 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1326 finish_header (¤t_stat_info
, label
, -1);
1327 set_next_block_after (label
);
1331 #define VOL_SUFFIX "Volume"
1333 /* Add a volume label to a part of multi-volume archive */
1335 add_volume_label (void)
1337 char buf
[UINTMAX_STRSIZE_BOUND
];
1338 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1339 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1341 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1342 _write_volume_label (s
);
1349 if (archive_format
== POSIX_FORMAT
)
1351 off_t block_ordinal
;
1353 struct tar_stat_info st
;
1354 static size_t real_s_part_no
; /* FIXME */
1357 memset (&st
, 0, sizeof st
);
1358 st
.orig_file_name
= st
.file_name
= real_s_name
;
1359 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1360 st
.stat
.st_uid
= getuid ();
1361 st
.stat
.st_gid
= getgid ();
1362 st
.orig_file_name
= xheader_format_name (&st
,
1363 "%d/GNUFileParts.%p/%f.%n",
1365 st
.file_name
= st
.orig_file_name
;
1366 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1368 block_ordinal
= current_block_ordinal ();
1369 blk
= start_header (&st
);
1371 abort (); /* FIXME */
1372 finish_header (&st
, blk
, block_ordinal
);
1373 free (st
.orig_file_name
);
1378 /* Add a volume label to the current archive */
1380 write_volume_label (void)
1382 if (multi_volume_option
)
1383 add_volume_label ();
1385 _write_volume_label (volume_label_option
);
1388 /* Write GNU multi-volume header */
1390 gnu_add_multi_volume_header (void)
1393 union block
*block
= find_next_block ();
1395 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1397 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1398 quotearg_colon (real_s_name
)));
1400 memset (block
, 0, BLOCKSIZE
);
1402 /* FIXME: Michael P Urban writes: [a long name file] is being written
1403 when a new volume rolls around [...] Looks like the wrong value is
1404 being preserved in real_s_name, though. */
1406 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1407 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1409 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1410 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1411 block
->oldgnu_header
.offset
);
1413 tmp
= verbose_option
;
1415 finish_header (¤t_stat_info
, block
, -1);
1416 verbose_option
= tmp
;
1417 set_next_block_after (block
);
1420 /* Add a multi volume header to the current archive. The exact header format
1421 depends on the archive format. */
1423 add_multi_volume_header (void)
1425 if (archive_format
== POSIX_FORMAT
)
1427 off_t d
= real_s_totsize
- real_s_sizeleft
;
1428 xheader_store ("GNU.volume.filename", &dummy
, real_s_name
);
1429 xheader_store ("GNU.volume.size", &dummy
, &real_s_sizeleft
);
1430 xheader_store ("GNU.volume.offset", &dummy
, &d
);
1433 gnu_add_multi_volume_header ();
1436 /* Synchronize multi-volume globals */
1438 multi_volume_sync ()
1440 if (multi_volume_option
)
1444 assign_string (&real_s_name
,
1445 safer_name_suffix (save_name
, false,
1446 absolute_names_option
));
1447 real_s_totsize
= save_totsize
;
1448 real_s_sizeleft
= save_sizeleft
;
1452 assign_string (&real_s_name
, 0);
1454 real_s_sizeleft
= 0;
1460 /* Low-level flush functions */
1462 /* Simple flush read (no multi-volume or label extensions) */
1464 simple_flush_read (void)
1466 size_t status
; /* result from system call */
1468 do_checkpoint (false);
1470 /* Clear the count of errors. This only applies to a single call to
1473 read_error_count
= 0; /* clear error count */
1475 if (write_archive_to_stdout
&& record_start_block
!= 0)
1477 archive
= STDOUT_FILENO
;
1478 status
= sys_write_archive_buffer ();
1479 archive
= STDIN_FILENO
;
1480 if (status
!= record_size
)
1481 archive_write_error (status
);
1486 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1487 if (status
== record_size
)
1492 if (status
== SAFE_READ_ERROR
)
1494 archive_read_error ();
1495 continue; /* try again */
1499 short_read (status
);
1502 /* Simple flush write (no multi-volume or label extensions) */
1504 simple_flush_write (size_t level
__attribute__((unused
)))
1508 status
= _flush_write ();
1509 if (status
!= record_size
)
1510 archive_write_error (status
);
1514 bytes_written
+= status
;
1519 /* GNU flush functions. These support multi-volume and archive labels in
1520 GNU and PAX archive formats. */
1523 _gnu_flush_read (void)
1525 size_t status
; /* result from system call */
1527 do_checkpoint (false);
1529 /* Clear the count of errors. This only applies to a single call to
1532 read_error_count
= 0; /* clear error count */
1534 if (write_archive_to_stdout
&& record_start_block
!= 0)
1536 archive
= STDOUT_FILENO
;
1537 status
= sys_write_archive_buffer ();
1538 archive
= STDIN_FILENO
;
1539 if (status
!= record_size
)
1540 archive_write_error (status
);
1543 multi_volume_sync ();
1547 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1548 if (status
== record_size
)
1554 /* The condition below used to include
1555 || (status > 0 && !read_full_records)
1556 This is incorrect since even if new_volume() succeeds, the
1557 subsequent call to rmtread will overwrite the chunk of data
1558 already read in the buffer, so the processing will fail */
1560 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1561 && multi_volume_option
)
1563 while (!try_new_volume ())
1567 else if (status
== SAFE_READ_ERROR
)
1569 archive_read_error ();
1574 short_read (status
);
1578 gnu_flush_read (void)
1580 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1582 flush_read_ptr
= gnu_flush_read
;
1586 _gnu_flush_write (size_t buffer_level
)
1589 union block
*header
;
1594 status
= _flush_write ();
1595 if (status
!= record_size
&& !multi_volume_option
)
1596 archive_write_error (status
);
1600 bytes_written
+= status
;
1603 if (status
== record_size
)
1605 multi_volume_sync ();
1609 /* In multi-volume mode. */
1610 /* ENXIO is for the UNIX PC. */
1611 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1612 archive_write_error (status
);
1614 if (!new_volume (ACCESS_WRITE
))
1617 tar_stat_destroy (&dummy
);
1619 increase_volume_number ();
1620 prev_written
+= bytes_written
;
1623 copy_ptr
= record_start
->buffer
+ status
;
1624 copy_size
= buffer_level
- status
;
1625 /* Switch to the next buffer */
1626 record_index
= !record_index
;
1629 if (volume_label_option
)
1630 add_volume_label ();
1633 add_multi_volume_header ();
1635 write_extended (true, &dummy
, find_next_block ());
1636 tar_stat_destroy (&dummy
);
1639 add_chunk_header ();
1640 header
= find_next_block ();
1641 bufsize
= available_space_after (header
);
1642 while (bufsize
< copy_size
)
1644 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1645 copy_ptr
+= bufsize
;
1646 copy_size
-= bufsize
;
1647 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1648 header
= find_next_block ();
1649 bufsize
= available_space_after (header
);
1651 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1652 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1653 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1658 gnu_flush_write (size_t buffer_level
)
1660 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1661 _gnu_flush_write (buffer_level
);
1662 flush_write_ptr
= gnu_flush_write
;
1674 flush_write_ptr (record_size
);
1678 open_archive (enum access_mode wanted_access
)
1680 flush_read_ptr
= gnu_flush_read
;
1681 flush_write_ptr
= gnu_flush_write
;
1683 _open_archive (wanted_access
);
1684 switch (wanted_access
)
1687 if (volume_label_option
)
1688 match_volume_label ();
1692 records_written
= 0;
1693 if (volume_label_option
)
1694 write_volume_label ();
1700 set_volume_start_time ();