1 /* Buffer management for tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006 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. */
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 mv_begin (struct tar_stat_info
*st
)
127 if (multi_volume_option
)
129 assign_string (&save_name
, st
->orig_file_name
);
130 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
137 if (multi_volume_option
)
138 assign_string (&save_name
, 0);
142 mv_total_size (off_t size
)
148 mv_size_left (off_t size
)
150 save_sizeleft
= size
;
157 clear_read_error_count (void)
159 read_error_count
= 0;
163 /* Time-related functions */
170 gettime (&start_time
);
171 volume_start_time
= start_time
;
172 last_stat_time
= start_time
;
176 set_volume_start_time ()
178 gettime (&volume_start_time
);
179 last_stat_time
= volume_start_time
;
187 duration
+= ((now
.tv_sec
- last_stat_time
.tv_sec
)
188 + (now
.tv_nsec
- last_stat_time
.tv_nsec
) / 1e9
);
189 gettime (&last_stat_time
);
193 /* Compression detection */
204 enum compress_type type
;
211 static struct zip_magic
const magic
[] = {
213 { ct_compress
, 2, "\037\235", "compress", "-Z" },
214 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
215 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
218 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
220 #define compress_option(t) magic[t].option
221 #define compress_program(t) magic[t].program
223 /* Check if the file ARCHIVE is a compressed archive. */
225 check_compressed_archive ()
227 struct zip_magic
const *p
;
230 /* Prepare global data needed for find_next_block: */
231 record_end
= record_start
; /* set up for 1st record = # 0 */
232 sfr
= read_full_records
;
233 read_full_records
= true; /* Suppress fatal error on reading a partial
237 /* Restore global values */
238 read_full_records
= sfr
;
240 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
241 /* Probably a valid header */
244 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
245 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
251 /* Open an archive named archive_name_array[0]. Detect if it is
252 a compressed archive of known type and use corresponding decompression
255 open_compressed_archive ()
257 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
258 MODE_RW
, rsh_command_option
);
262 if (!multi_volume_option
)
264 enum compress_type type
= check_compressed_archive ();
269 /* FD is not needed any more */
272 hit_eof
= false; /* It might have been set by find_next_block in
273 check_compressed_archive */
275 /* Open compressed archive */
276 use_compress_program_option
= compress_program (type
);
277 child_pid
= sys_child_open_for_uncompress ();
278 read_full_records
= true;
282 record_end
= record_start
; /* set up for 1st record = # 0 */
289 print_stats (FILE *fp
, const char *text
, tarlong numbytes
)
291 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
292 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
293 char rate
[LONGEST_HUMAN_READABLE
+ 1];
295 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
297 sprintf (bytes
, TARLONG_FORMAT
, numbytes
);
299 fprintf (fp
, "%s: %s (%s, %s/s)\n",
301 human_readable (numbytes
, abbr
, human_opts
, 1, 1),
302 (0 < duration
&& numbytes
/ duration
< (uintmax_t) -1
303 ? human_readable (numbytes
/ duration
, rate
, human_opts
, 1, 1)
310 switch (subcommand_option
)
312 case CREATE_SUBCOMMAND
:
314 case UPDATE_SUBCOMMAND
:
315 case APPEND_SUBCOMMAND
:
316 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
317 print_stats (stderr
, _("Total bytes written"),
318 prev_written
+ bytes_written
);
321 case DELETE_SUBCOMMAND
:
323 char buf
[UINTMAX_STRSIZE_BOUND
];
324 print_stats (stderr
, _("Total bytes read"),
325 records_read
* record_size
);
326 print_stats (stderr
, _("Total bytes written"),
327 prev_written
+ bytes_written
);
328 fprintf (stderr
, _("Total bytes deleted: %s\n"),
329 STRINGIFY_BIGINT ((records_read
- records_skipped
)
331 - (prev_written
+ bytes_written
), buf
));
335 case EXTRACT_SUBCOMMAND
:
336 case LIST_SUBCOMMAND
:
337 case DIFF_SUBCOMMAND
:
338 print_stats (stderr
, _("Total bytes read"),
339 records_read
* record_size
);
347 /* Compute and return the block ordinal at current_block. */
349 current_block_ordinal (void)
351 return record_start_block
+ (current_block
- record_start
);
354 /* If the EOF flag is set, reset it, as well as current_block, etc. */
361 current_block
= record_start
;
362 record_end
= record_start
+ blocking_factor
;
363 access_mode
= ACCESS_WRITE
;
367 /* Return the location of the next available input or output block.
368 Return zero for EOF. Once we have returned zero, we just keep returning
369 it, to avoid accidentally going on to the next file on the tape. */
371 find_next_block (void)
373 if (current_block
== record_end
)
378 if (current_block
== record_end
)
384 return current_block
;
387 /* Indicate that we have used all blocks up thru BLOCK. */
389 set_next_block_after (union block
*block
)
391 while (block
>= current_block
)
394 /* Do *not* flush the archive here. If we do, the same argument to
395 set_next_block_after could mean the next block (if the input record
396 is exactly one block long), which is not what is intended. */
398 if (current_block
> record_end
)
402 /* Return the number of bytes comprising the space between POINTER
403 through the end of the current buffer of blocks. This space is
404 available for filling with data, or taking data from. POINTER is
405 usually (but not always) the result of previous find_next_block call. */
407 available_space_after (union block
*pointer
)
409 return record_end
->buffer
- pointer
->buffer
;
412 /* Close file having descriptor FD, and abort if close unsuccessful. */
417 close_error (_("(pipe)"));
423 if (! record_buffer_aligned
[record_index
])
424 record_buffer_aligned
[record_index
] =
425 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
427 record_start
= record_buffer_aligned
[record_index
];
428 current_block
= record_start
;
429 record_end
= record_start
+ blocking_factor
;
432 /* Open an archive file. The argument specifies whether we are
433 reading or writing, or both. */
435 _open_archive (enum access_mode wanted_access
)
437 int backed_up_flag
= 0;
439 if (record_size
== 0)
440 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
442 if (archive_names
== 0)
443 FATAL_ERROR ((0, 0, _("No archive name given")));
445 tar_stat_destroy (¤t_stat_info
);
452 /* When updating the archive, we start with reading. */
453 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
455 read_full_records
= read_full_records_option
;
459 if (use_compress_program_option
)
461 switch (wanted_access
)
464 child_pid
= sys_child_open_for_uncompress ();
465 read_full_records
= true;
466 record_end
= record_start
; /* set up for 1st record = # 0 */
470 child_pid
= sys_child_open_for_compress ();
474 abort (); /* Should not happen */
478 else if (strcmp (archive_name_array
[0], "-") == 0)
480 read_full_records
= true; /* could be a pipe, be safe */
482 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
484 switch (wanted_access
)
488 enum compress_type type
;
490 archive
= STDIN_FILENO
;
492 type
= check_compressed_archive ();
495 _("Archive is compressed. Use %s option"),
496 compress_option (type
)));
501 archive
= STDOUT_FILENO
;
505 archive
= STDIN_FILENO
;
506 write_archive_to_stdout
= true;
507 record_end
= record_start
; /* set up for 1st record = # 0 */
511 else if (verify_option
)
512 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
513 MODE_RW
, rsh_command_option
);
515 switch (wanted_access
)
518 archive
= open_compressed_archive ();
524 maybe_backup_file (archive_name_array
[0], 1);
527 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
532 archive
= rmtopen (archive_name_array
[0],
533 O_RDWR
| O_CREAT
| O_BINARY
,
534 MODE_RW
, rsh_command_option
);
536 if (check_compressed_archive () != ct_none
)
538 _("Cannot update compressed archives")));
543 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
545 int saved_errno
= errno
;
550 open_fatal (archive_name_array
[0]);
553 sys_detect_dev_null_output ();
554 sys_save_archive_dev_ino ();
555 SET_BINARY_MODE (archive
);
557 switch (wanted_access
)
560 find_next_block (); /* read it in, check for EOF */
571 do_checkpoint (bool write
)
573 if (checkpoint_option
&& !(++checkpoint
% checkpoint_option
))
575 switch (checkpoint_style
)
582 case checkpoint_text
:
584 /* TRANSLATORS: This is a ``checkpoint of write operation'',
585 *not* ``Writing a checkpoint''.
586 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
587 *not* ``Escribiendo un punto de comprobaci@'on'' */
588 WARN ((0, 0, _("Write checkpoint %u"), checkpoint
));
590 /* TRANSLATORS: This is a ``checkpoint of read operation'',
591 *not* ``Reading a checkpoint''.
592 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
593 *not* ``Leyendo un punto de comprobaci@'on'' */
594 WARN ((0, 0, _("Read checkpoint %u"), checkpoint
));
600 /* Perform a write to flush the buffer. */
606 do_checkpoint (true);
607 if (tape_length_option
&& tape_length_option
<= bytes_written
)
612 else if (dev_null_output
)
613 status
= record_size
;
615 status
= sys_write_archive_buffer ();
620 /* Handle write errors on the archive. Write errors are always fatal.
621 Hitting the end of a volume does not cause a write error unless the
622 write was the first record of the volume. */
624 archive_write_error (ssize_t status
)
626 /* It might be useful to know how much was written before the error
631 print_total_stats ();
635 write_fatal_details (*archive_name_cursor
, status
, record_size
);
638 /* Handle read errors on the archive. If the read should be retried,
639 return to the caller. */
641 archive_read_error (void)
643 read_error (*archive_name_cursor
);
645 if (record_start_block
== 0)
646 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
648 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
649 then give up on reading the archive. */
651 if (read_error_count
++ > READ_ERROR_MAX
)
652 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
657 short_read (size_t status
)
659 size_t left
; /* bytes left */
660 char *more
; /* pointer to next byte to read */
662 more
= record_start
->buffer
+ status
;
663 left
= record_size
- status
;
665 while (left
% BLOCKSIZE
!= 0
666 || (left
&& status
&& read_full_records
))
669 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
670 archive_read_error ();
675 if (! read_full_records
)
677 unsigned long rest
= record_size
- left
;
680 ngettext ("Unaligned block (%lu byte) in archive",
681 "Unaligned block (%lu bytes) in archive",
686 /* User warned us about this. Fix up. */
692 /* FIXME: for size=0, multi-volume support. On the first record, warn
693 about the problem. */
695 if (!read_full_records
&& verbose_option
> 1
696 && record_start_block
== 0 && status
!= 0)
698 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
700 ngettext ("Record size = %lu block",
701 "Record size = %lu blocks",
706 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
710 /* Flush the current buffer to/from the archive. */
714 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
715 record_start_block
+= record_end
- record_start
;
716 current_block
= record_start
;
717 record_end
= record_start
+ blocking_factor
;
719 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
721 access_mode
= ACCESS_WRITE
;
722 time_to_start_writing
= false;
733 flush_write_ptr (buffer_level
);
741 /* Backspace the archive descriptor by one record worth. If it's a
742 tape, MTIOCTOP will work. If it's something else, try to seek on
743 it. If we can't seek, we lose! */
745 backspace_output (void)
749 struct mtop operation
;
751 operation
.mt_op
= MTBSR
;
752 operation
.mt_count
= 1;
753 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
755 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
761 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
763 /* Seek back to the beginning of this record and start writing there. */
765 position
-= record_size
;
768 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
770 /* Lseek failed. Try a different method. */
773 _("Cannot backspace archive file; it may be unreadable without -i")));
775 /* Replace the first part of the record with NULs. */
777 if (record_start
->buffer
!= output_start
)
778 memset (record_start
->buffer
, 0,
779 output_start
- record_start
->buffer
);
785 seek_archive (off_t size
)
787 off_t start
= current_block_ordinal ();
790 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
792 size
-= skipped
* BLOCKSIZE
;
794 if (size
< record_size
)
798 /* Compute number of records to skip */
799 nrec
= size
/ record_size
;
800 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
804 if (offset
% record_size
)
805 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
807 /* Convert to number of records */
809 /* Compute number of skipped blocks */
810 nblk
= offset
- start
;
812 /* Update buffering info */
813 records_read
+= nblk
/ blocking_factor
;
814 record_start_block
= offset
- blocking_factor
;
815 current_block
= record_end
;
820 /* Close the archive file. */
824 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
827 if (current_block
> record_start
)
831 sys_drain_input_pipe ();
837 if (rmtclose (archive
) != 0)
838 close_warn (*archive_name_cursor
);
840 sys_wait_for_child (child_pid
);
842 tar_stat_destroy (¤t_stat_info
);
847 free (record_buffer
[0]);
848 free (record_buffer
[1]);
851 /* Called to initialize the global volume number. */
853 init_volume_number (void)
855 FILE *file
= fopen (volno_file_option
, "r");
859 if (fscanf (file
, "%d", &global_volno
) != 1
861 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
862 quotearg_colon (volno_file_option
)));
864 read_error (volno_file_option
);
865 if (fclose (file
) != 0)
866 close_error (volno_file_option
);
868 else if (errno
!= ENOENT
)
869 open_error (volno_file_option
);
872 /* Called to write out the closing global volume number. */
874 closeout_volume_number (void)
876 FILE *file
= fopen (volno_file_option
, "w");
880 fprintf (file
, "%d\n", global_volno
);
882 write_error (volno_file_option
);
883 if (fclose (file
) != 0)
884 close_error (volno_file_option
);
887 open_error (volno_file_option
);
892 increase_volume_number ()
895 if (global_volno
< 0)
896 FATAL_ERROR ((0, 0, _("Volume number overflow")));
901 change_tape_menu (FILE *read_file
)
903 char *input_buffer
= NULL
;
909 fputc ('\007', stderr
);
911 _("Prepare volume #%d for %s and hit return: "),
912 global_volno
+ 1, quote (*archive_name_cursor
));
915 if (getline (&input_buffer
, &size
, read_file
) <= 0)
917 WARN ((0, 0, _("EOF where user reply was expected")));
919 if (subcommand_option
!= EXTRACT_SUBCOMMAND
920 && subcommand_option
!= LIST_SUBCOMMAND
921 && subcommand_option
!= DIFF_SUBCOMMAND
)
922 WARN ((0, 0, _("WARNING: Archive is incomplete")));
927 if (input_buffer
[0] == '\n'
928 || input_buffer
[0] == 'y'
929 || input_buffer
[0] == 'Y')
932 switch (input_buffer
[0])
936 fprintf (stderr
, _("\
937 n name Give a new file name for the next (and subsequent) volume(s)\n\
939 y or newline Continue operation\n"));
940 if (!restrict_option
)
941 fprintf (stderr
, _(" ! Spawn a subshell\n"));
942 fprintf (stderr
, _(" ? Print this list\n"));
949 WARN ((0, 0, _("No new volume; exiting.\n")));
951 if (subcommand_option
!= EXTRACT_SUBCOMMAND
952 && subcommand_option
!= LIST_SUBCOMMAND
953 && subcommand_option
!= DIFF_SUBCOMMAND
)
954 WARN ((0, 0, _("WARNING: Archive is incomplete")));
959 /* Get new file name. */
965 for (name
= input_buffer
+ 1;
966 *name
== ' ' || *name
== '\t';
970 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
976 /* FIXME: the following allocation is never reclaimed. */
977 *archive_name_cursor
= xstrdup (name
);
981 fprintf (stderr
, "%s",
982 _("File name not specified. Try again.\n"));
987 if (!restrict_option
)
995 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
1001 /* We've hit the end of the old volume. Close it and open the next one.
1002 Return nonzero on success.
1005 new_volume (enum access_mode mode
)
1007 static FILE *read_file
;
1011 if (!read_file
&& !info_script_option
)
1012 /* FIXME: if fopen is used, it will never be closed. */
1013 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1020 assign_string (&volume_label
, NULL
);
1021 assign_string (&continued_file_name
, NULL
);
1022 continued_file_size
= continued_file_offset
= 0;
1024 if (rmtclose (archive
) != 0)
1025 close_warn (*archive_name_cursor
);
1027 archive_name_cursor
++;
1028 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1030 archive_name_cursor
= archive_name_array
;
1038 /* We have to prompt from now on. */
1040 if (info_script_option
)
1042 if (volno_file_option
)
1043 closeout_volume_number ();
1044 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1045 FATAL_ERROR ((0, 0, _("%s command failed"),
1046 quote (info_script_option
)));
1049 change_tape_menu (read_file
);
1052 if (strcmp (archive_name_cursor
[0], "-") == 0)
1054 read_full_records
= true;
1055 archive
= STDIN_FILENO
;
1057 else if (verify_option
)
1058 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1059 rsh_command_option
);
1064 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1065 rsh_command_option
);
1070 maybe_backup_file (*archive_name_cursor
, 1);
1071 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1072 rsh_command_option
);
1076 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1077 rsh_command_option
);
1083 open_warn (*archive_name_cursor
);
1084 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1085 undo_last_backup ();
1090 SET_BINARY_MODE (archive
);
1096 read_header0 (struct tar_stat_info
*info
)
1098 enum read_header rc
;
1100 tar_stat_init (info
);
1101 rc
= read_header_primitive (false, info
);
1102 if (rc
== HEADER_SUCCESS
)
1104 set_next_block_after (current_header
);
1107 ERROR ((0, 0, _("This does not look like a tar archive")));
1115 union block
*header
;
1116 struct tar_stat_info dummy
;
1118 switch (subcommand_option
)
1120 case APPEND_SUBCOMMAND
:
1121 case CAT_SUBCOMMAND
:
1122 case UPDATE_SUBCOMMAND
:
1123 if (!new_volume (ACCESS_UPDATE
))
1128 if (!new_volume (ACCESS_READ
))
1133 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1135 archive_read_error ();
1137 if (status
!= record_size
)
1138 short_read (status
);
1140 header
= find_next_block ();
1144 switch (header
->header
.typeflag
)
1148 if (!read_header0 (&dummy
))
1150 xheader_decode (&dummy
); /* decodes values from the global header */
1151 tar_stat_destroy (&dummy
);
1154 /* We have read the extended header of the first member in
1155 this volume. Put it back, so next read_header works as
1157 current_block
= record_start
;
1162 case GNUTYPE_VOLHDR
:
1163 if (!read_header0 (&dummy
))
1165 tar_stat_destroy (&dummy
);
1166 assign_string (&volume_label
, current_header
->header
.name
);
1167 set_next_block_after (header
);
1168 header
= find_next_block ();
1169 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1173 case GNUTYPE_MULTIVOL
:
1174 if (!read_header0 (&dummy
))
1176 tar_stat_destroy (&dummy
);
1177 assign_string (&continued_file_name
, current_header
->header
.name
);
1178 continued_file_size
=
1179 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1180 continued_file_offset
=
1181 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1191 if (!continued_file_name
1192 || strcmp (continued_file_name
, real_s_name
))
1194 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1195 && strlen (real_s_name
) >= NAME_FIELD_SIZE
1196 && strncmp (continued_file_name
, real_s_name
,
1197 NAME_FIELD_SIZE
) == 0)
1199 _("%s is possibly continued on this volume: header contains truncated name"),
1200 quote (real_s_name
)));
1203 WARN ((0, 0, _("%s is not continued on this volume"),
1204 quote (real_s_name
)));
1209 s
= continued_file_size
+ continued_file_offset
;
1211 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1213 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1214 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1215 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1217 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1218 quote (continued_file_name
),
1219 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1220 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1221 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1225 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1227 WARN ((0, 0, _("This volume is out of sequence")));
1232 increase_volume_number ();
1237 /* Check the LABEL block against the volume label, seen as a globbing
1238 pattern. Return true if the pattern matches. In case of failure,
1239 retry matching a volume sequence number before giving up in
1240 multi-volume mode. */
1242 check_label_pattern (union block
*label
)
1247 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1250 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1253 if (!multi_volume_option
)
1256 string
= xmalloc (strlen (volume_label_option
)
1257 + sizeof VOLUME_LABEL_APPEND
+ 1);
1258 strcpy (string
, volume_label_option
);
1259 strcat (string
, VOLUME_LABEL_APPEND
);
1260 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1265 /* Check if the next block contains a volume label and if this matches
1266 the one given in the command line */
1268 match_volume_label (void)
1270 union block
*label
= find_next_block ();
1273 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1274 quote (volume_label_option
)));
1275 if (!check_label_pattern (label
))
1276 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1277 quote_n (0, label
->header
.name
),
1278 quote_n (1, volume_label_option
)));
1281 /* Mark the archive with volume label STR. */
1283 _write_volume_label (const char *str
)
1285 if (archive_format
== POSIX_FORMAT
)
1286 xheader_store ("GNU.volume.label", NULL
, str
);
1289 union block
*label
= find_next_block ();
1291 memset (label
, 0, BLOCKSIZE
);
1293 strcpy (label
->header
.name
, volume_label_option
);
1294 assign_string (¤t_stat_info
.file_name
,
1295 label
->header
.name
);
1296 current_stat_info
.had_trailing_slash
=
1297 strip_trailing_slashes (current_stat_info
.file_name
);
1299 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1300 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1301 finish_header (¤t_stat_info
, label
, -1);
1302 set_next_block_after (label
);
1306 #define VOL_SUFFIX "Volume"
1308 /* Add a volume label to a part of multi-volume archive */
1310 add_volume_label (void)
1312 char buf
[UINTMAX_STRSIZE_BOUND
];
1313 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1314 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1316 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1317 _write_volume_label (s
);
1324 if (archive_format
== POSIX_FORMAT
)
1326 off_t block_ordinal
;
1328 struct tar_stat_info st
;
1329 static size_t real_s_part_no
; /* FIXME */
1332 memset (&st
, 0, sizeof st
);
1333 st
.orig_file_name
= st
.file_name
= real_s_name
;
1334 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1335 st
.stat
.st_uid
= getuid ();
1336 st
.stat
.st_gid
= getgid ();
1337 st
.orig_file_name
= xheader_format_name (&st
,
1338 "%d/GNUFileParts.%p/%f.%n",
1340 st
.file_name
= st
.orig_file_name
;
1341 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1343 block_ordinal
= current_block_ordinal ();
1344 blk
= start_header (&st
);
1346 abort (); /* FIXME */
1347 finish_header (&st
, blk
, block_ordinal
);
1348 free (st
.orig_file_name
);
1353 /* Add a volume label to the current archive */
1355 write_volume_label (void)
1357 if (multi_volume_option
)
1358 add_volume_label ();
1360 _write_volume_label (volume_label_option
);
1363 /* Write GNU multi-volume header */
1365 gnu_add_multi_volume_header (void)
1368 union block
*block
= find_next_block ();
1370 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1372 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1373 quotearg_colon (real_s_name
)));
1375 memset (block
, 0, BLOCKSIZE
);
1377 /* FIXME: Michael P Urban writes: [a long name file] is being written
1378 when a new volume rolls around [...] Looks like the wrong value is
1379 being preserved in real_s_name, though. */
1381 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1382 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1384 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1385 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1386 block
->oldgnu_header
.offset
);
1388 tmp
= verbose_option
;
1390 finish_header (¤t_stat_info
, block
, -1);
1391 verbose_option
= tmp
;
1392 set_next_block_after (block
);
1395 /* Add a multi volume header to the current archive. The exact header format
1396 depends on the archive format. */
1398 add_multi_volume_header (void)
1400 if (archive_format
== POSIX_FORMAT
)
1402 off_t d
= real_s_totsize
- real_s_sizeleft
;
1403 xheader_store ("GNU.volume.filename", NULL
, real_s_name
);
1404 xheader_store ("GNU.volume.size", NULL
, &real_s_sizeleft
);
1405 xheader_store ("GNU.volume.offset", NULL
, &d
);
1408 gnu_add_multi_volume_header ();
1411 /* Synchronize multi-volume globals */
1413 multi_volume_sync ()
1415 if (multi_volume_option
)
1419 assign_string (&real_s_name
,
1420 safer_name_suffix (save_name
, false,
1421 absolute_names_option
));
1422 real_s_totsize
= save_totsize
;
1423 real_s_sizeleft
= save_sizeleft
;
1427 assign_string (&real_s_name
, 0);
1429 real_s_sizeleft
= 0;
1435 /* Low-level flush functions */
1437 /* Simple flush read (no multi-volume or label extensions) */
1439 simple_flush_read (void)
1441 size_t status
; /* result from system call */
1443 do_checkpoint (false);
1445 /* Clear the count of errors. This only applies to a single call to
1448 read_error_count
= 0; /* clear error count */
1450 if (write_archive_to_stdout
&& record_start_block
!= 0)
1452 archive
= STDOUT_FILENO
;
1453 status
= sys_write_archive_buffer ();
1454 archive
= STDIN_FILENO
;
1455 if (status
!= record_size
)
1456 archive_write_error (status
);
1461 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1462 if (status
== record_size
)
1467 if (status
== SAFE_READ_ERROR
)
1469 archive_read_error ();
1470 continue; /* try again */
1474 short_read (status
);
1477 /* Simple flush write (no multi-volume or label extensions) */
1479 simple_flush_write (size_t level
__attribute__((unused
)))
1483 status
= _flush_write ();
1484 if (status
!= record_size
)
1485 archive_write_error (status
);
1489 bytes_written
+= status
;
1494 /* GNU flush functions. These support multi-volume and archive labels in
1495 GNU and PAX archive formats. */
1498 _gnu_flush_read (void)
1500 size_t status
; /* result from system call */
1502 do_checkpoint (false);
1504 /* Clear the count of errors. This only applies to a single call to
1507 read_error_count
= 0; /* clear error count */
1509 if (write_archive_to_stdout
&& record_start_block
!= 0)
1511 archive
= STDOUT_FILENO
;
1512 status
= sys_write_archive_buffer ();
1513 archive
= STDIN_FILENO
;
1514 if (status
!= record_size
)
1515 archive_write_error (status
);
1518 multi_volume_sync ();
1522 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1523 if (status
== record_size
)
1529 /* The condition below used to include
1530 || (status > 0 && !read_full_records)
1531 This is incorrect since even if new_volume() succeeds, the
1532 subsequent call to rmtread will overwrite the chunk of data
1533 already read in the buffer, so the processing will fail */
1535 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1536 && multi_volume_option
)
1538 while (!try_new_volume ())
1542 else if (status
== SAFE_READ_ERROR
)
1544 archive_read_error ();
1549 short_read (status
);
1553 gnu_flush_read (void)
1555 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1557 flush_read_ptr
= gnu_flush_read
;
1561 _gnu_flush_write (size_t buffer_level
)
1564 union block
*header
;
1569 status
= _flush_write ();
1570 if (status
!= record_size
&& !multi_volume_option
)
1571 archive_write_error (status
);
1575 bytes_written
+= status
;
1578 if (status
== record_size
)
1580 multi_volume_sync ();
1584 /* In multi-volume mode. */
1585 /* ENXIO is for the UNIX PC. */
1586 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1587 archive_write_error (status
);
1589 if (!new_volume (ACCESS_WRITE
))
1592 xheader_destroy (&extended_header
);
1594 increase_volume_number ();
1595 prev_written
+= bytes_written
;
1598 copy_ptr
= record_start
->buffer
+ status
;
1599 copy_size
= buffer_level
- status
;
1600 /* Switch to the next buffer */
1601 record_index
= !record_index
;
1604 if (volume_label_option
)
1605 add_volume_label ();
1608 add_multi_volume_header ();
1610 write_extended (true, NULL
, find_next_block ());
1612 add_chunk_header ();
1613 header
= find_next_block ();
1614 bufsize
= available_space_after (header
);
1615 while (bufsize
< copy_size
)
1617 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1618 copy_ptr
+= bufsize
;
1619 copy_size
-= bufsize
;
1620 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1621 header
= find_next_block ();
1622 bufsize
= available_space_after (header
);
1624 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1625 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1626 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1631 gnu_flush_write (size_t buffer_level
)
1633 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1634 _gnu_flush_write (buffer_level
);
1635 flush_write_ptr
= gnu_flush_write
;
1647 flush_write_ptr (record_size
);
1651 open_archive (enum access_mode wanted_access
)
1653 flush_read_ptr
= gnu_flush_read
;
1654 flush_write_ptr
= gnu_flush_write
;
1656 _open_archive (wanted_access
);
1657 switch (wanted_access
)
1660 if (volume_label_option
)
1661 match_volume_label ();
1665 records_written
= 0;
1666 if (volume_label_option
)
1667 write_volume_label ();
1673 set_volume_start_time ();