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. */
34 /* Number of retries before giving up on read. */
35 #define READ_ERROR_MAX 10
37 /* Globbing pattern to append to volume label if initial match failed. */
38 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
42 static tarlong prev_written
; /* bytes written on previous volumes */
43 static tarlong bytes_written
; /* bytes written on this volume */
44 static void *record_buffer
[2]; /* allocated memory */
45 static int record_index
;
47 /* FIXME: The following variables should ideally be static to this
48 module. However, this cannot be done yet. The cleanup continues! */
50 union block
*record_start
; /* start of record of archive */
51 union block
*record_end
; /* last+1 block of archive record */
52 union block
*current_block
; /* current block of archive */
53 enum access_mode access_mode
; /* how do we handle the archive */
54 off_t records_read
; /* number of records read from this archive */
55 off_t records_written
; /* likewise, for records written */
57 static off_t record_start_block
; /* block ordinal at record_start */
59 /* Where we write list messages (not errors, not interactions) to. */
62 static void backspace_output (void);
64 /* PID of child program, if compress_option or remote archive access. */
65 static pid_t child_pid
;
67 /* Error recovery stuff */
68 static int read_error_count
;
70 /* Have we hit EOF yet? */
73 /* Checkpointing counter */
74 static int checkpoint
;
76 static bool read_full_records
= false;
78 /* We're reading, but we just read the last block and it's time to update.
81 As least EXTERN like this one as possible. (?? --gray)
82 FIXME: Either eliminate it or move it to common.h.
84 extern bool time_to_start_writing
;
86 bool write_archive_to_stdout
;
88 void (*flush_write_ptr
) (size_t);
89 void (*flush_read_ptr
) (void);
93 char *continued_file_name
;
94 uintmax_t continued_file_size
;
95 uintmax_t continued_file_offset
;
98 static int volno
= 1; /* which volume of a multi-volume tape we're
100 static int global_volno
= 1; /* volume number to print in external
103 bool write_archive_to_stdout
;
105 /* Used by flush_read and flush_write to store the real info about saved
107 static char *real_s_name
;
108 static off_t real_s_totsize
;
109 static off_t real_s_sizeleft
;
112 /* Multi-volume tracking support */
113 static char *save_name
; /* name of the file we are currently writing */
114 static off_t save_totsize
; /* total size of file we are writing, only
115 valid if save_name is nonzero */
116 static off_t save_sizeleft
; /* where we are in the file we are writing,
117 only valid if save_name is nonzero */
120 mv_begin (struct tar_stat_info
*st
)
122 if (multi_volume_option
)
124 assign_string (&save_name
, st
->orig_file_name
);
125 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
132 if (multi_volume_option
)
133 assign_string (&save_name
, 0);
137 mv_total_size (off_t size
)
143 mv_size_left (off_t size
)
145 save_sizeleft
= size
;
152 clear_read_error_count (void)
154 read_error_count
= 0;
158 /* Time-related functions */
165 gettime (&start_time
);
173 duration
+= ((now
.tv_sec
- start_time
.tv_sec
)
174 + (now
.tv_nsec
- start_time
.tv_nsec
) / 1e9
);
179 /* Compression detection */
190 enum compress_type type
;
197 static struct zip_magic
const magic
[] = {
199 { ct_compress
, 2, "\037\235", "compress", "-Z" },
200 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
201 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
204 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
206 #define compress_option(t) magic[t].option
207 #define compress_program(t) magic[t].program
209 /* Check if the file ARCHIVE is a compressed archive. */
211 check_compressed_archive ()
213 struct zip_magic
const *p
;
216 /* Prepare global data needed for find_next_block: */
217 record_end
= record_start
; /* set up for 1st record = # 0 */
218 sfr
= read_full_records
;
219 read_full_records
= true; /* Suppress fatal error on reading a partial
223 /* Restore global values */
224 read_full_records
= sfr
;
226 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
227 /* Probably a valid header */
230 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
231 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
237 /* Open an archive named archive_name_array[0]. Detect if it is
238 a compressed archive of known type and use corresponding decompression
241 open_compressed_archive ()
243 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
244 MODE_RW
, rsh_command_option
);
248 if (!multi_volume_option
)
250 enum compress_type type
= check_compressed_archive ();
255 /* FD is not needed any more */
258 hit_eof
= false; /* It might have been set by find_next_block in
259 check_compressed_archive */
261 /* Open compressed archive */
262 use_compress_program_option
= compress_program (type
);
263 child_pid
= sys_child_open_for_uncompress ();
264 read_full_records
= true;
268 record_end
= record_start
; /* set up for 1st record = # 0 */
275 print_total_written (void)
277 tarlong written
= prev_written
+ bytes_written
;
278 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
279 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
280 char rate
[LONGEST_HUMAN_READABLE
+ 1];
282 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
284 sprintf (bytes
, TARLONG_FORMAT
, written
);
286 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
287 fprintf (stderr
, _("Total bytes written: %s (%s, %s/s)\n"), bytes
,
288 human_readable (written
, abbr
, human_opts
, 1, 1),
289 (0 < duration
&& written
/ duration
< (uintmax_t) -1
290 ? human_readable (written
/ duration
, rate
, human_opts
, 1, 1)
294 /* Compute and return the block ordinal at current_block. */
296 current_block_ordinal (void)
298 return record_start_block
+ (current_block
- record_start
);
301 /* If the EOF flag is set, reset it, as well as current_block, etc. */
308 current_block
= record_start
;
309 record_end
= record_start
+ blocking_factor
;
310 access_mode
= ACCESS_WRITE
;
314 /* Return the location of the next available input or output block.
315 Return zero for EOF. Once we have returned zero, we just keep returning
316 it, to avoid accidentally going on to the next file on the tape. */
318 find_next_block (void)
320 if (current_block
== record_end
)
325 if (current_block
== record_end
)
331 return current_block
;
334 /* Indicate that we have used all blocks up thru BLOCK. */
336 set_next_block_after (union block
*block
)
338 while (block
>= current_block
)
341 /* Do *not* flush the archive here. If we do, the same argument to
342 set_next_block_after could mean the next block (if the input record
343 is exactly one block long), which is not what is intended. */
345 if (current_block
> record_end
)
349 /* Return the number of bytes comprising the space between POINTER
350 through the end of the current buffer of blocks. This space is
351 available for filling with data, or taking data from. POINTER is
352 usually (but not always) the result of previous find_next_block call. */
354 available_space_after (union block
*pointer
)
356 return record_end
->buffer
- pointer
->buffer
;
359 /* Close file having descriptor FD, and abort if close unsuccessful. */
364 close_error (_("(pipe)"));
370 if (!record_buffer
[record_index
])
371 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
373 record_start
= record_buffer
[record_index
];
374 current_block
= record_start
;
375 record_end
= record_start
+ blocking_factor
;
378 /* Open an archive file. The argument specifies whether we are
379 reading or writing, or both. */
381 _open_archive (enum access_mode wanted_access
)
383 int backed_up_flag
= 0;
387 stdlis
= freopen (index_file_name
, "w", stdout
);
389 open_error (index_file_name
);
390 close_stdout_set_file_name (index_file_name
);
393 stdlis
= to_stdout_option
? stderr
: stdout
;
395 if (record_size
== 0)
396 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
398 if (archive_names
== 0)
399 FATAL_ERROR ((0, 0, _("No archive name given")));
401 tar_stat_destroy (¤t_stat_info
);
408 /* When updating the archive, we start with reading. */
409 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
411 read_full_records
= read_full_records_option
;
415 if (use_compress_program_option
)
417 switch (wanted_access
)
420 child_pid
= sys_child_open_for_uncompress ();
421 read_full_records
= true;
422 record_end
= record_start
; /* set up for 1st record = # 0 */
426 child_pid
= sys_child_open_for_compress ();
430 abort (); /* Should not happen */
434 if (wanted_access
== ACCESS_WRITE
435 && strcmp (archive_name_array
[0], "-") == 0)
438 else if (strcmp (archive_name_array
[0], "-") == 0)
440 read_full_records
= true; /* could be a pipe, be safe */
442 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
444 switch (wanted_access
)
448 enum compress_type type
;
450 archive
= STDIN_FILENO
;
452 type
= check_compressed_archive (archive
);
455 _("Archive is compressed. Use %s option"),
456 compress_option (type
)));
461 archive
= STDOUT_FILENO
;
466 archive
= STDIN_FILENO
;
468 write_archive_to_stdout
= true;
472 else if (verify_option
)
473 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
474 MODE_RW
, rsh_command_option
);
476 switch (wanted_access
)
479 archive
= open_compressed_archive ();
485 maybe_backup_file (archive_name_array
[0], 1);
488 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
493 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
494 MODE_RW
, rsh_command_option
);
499 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
501 int saved_errno
= errno
;
506 open_fatal (archive_name_array
[0]);
509 sys_detect_dev_null_output ();
510 sys_save_archive_dev_ino ();
511 SET_BINARY_MODE (archive
);
513 switch (wanted_access
)
517 record_end
= record_start
; /* set up for 1st record = # 0 */
520 find_next_block (); /* read it in, check for EOF */
529 /* Perform a write to flush the buffer. */
535 if (checkpoint_option
&& !(++checkpoint
% 10))
536 /* TRANSLATORS: This is a ``checkpoint of write operation'',
537 *not* ``Writing a checkpoint''.
538 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
539 *not* ``Escribiendo un punto de comprobaci@'on'' */
540 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
542 if (tape_length_option
&& tape_length_option
<= bytes_written
)
547 else if (dev_null_output
)
548 status
= record_size
;
550 status
= sys_write_archive_buffer ();
555 /* Handle write errors on the archive. Write errors are always fatal.
556 Hitting the end of a volume does not cause a write error unless the
557 write was the first record of the volume. */
559 archive_write_error (ssize_t status
)
561 /* It might be useful to know how much was written before the error
566 print_total_written ();
570 write_fatal_details (*archive_name_cursor
, status
, record_size
);
573 /* Handle read errors on the archive. If the read should be retried,
574 return to the caller. */
576 archive_read_error (void)
578 read_error (*archive_name_cursor
);
580 if (record_start_block
== 0)
581 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
583 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
584 then give up on reading the archive. */
586 if (read_error_count
++ > READ_ERROR_MAX
)
587 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
592 short_read (size_t status
)
594 size_t left
; /* bytes left */
595 char *more
; /* pointer to next byte to read */
597 more
= record_start
->buffer
+ status
;
598 left
= record_size
- status
;
600 while (left
% BLOCKSIZE
!= 0
601 || (left
&& status
&& read_full_records
))
604 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
605 archive_read_error ();
610 if (! read_full_records
)
612 unsigned long rest
= record_size
- left
;
615 ngettext ("Unaligned block (%lu byte) in archive",
616 "Unaligned block (%lu bytes) in archive",
621 /* User warned us about this. Fix up. */
627 /* FIXME: for size=0, multi-volume support. On the first record, warn
628 about the problem. */
630 if (!read_full_records
&& verbose_option
> 1
631 && record_start_block
== 0 && status
!= 0)
633 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
635 ngettext ("Record size = %lu block",
636 "Record size = %lu blocks",
641 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
645 /* Perform a read to flush the buffer. */
649 size_t status
; /* result from system call */
651 if (checkpoint_option
&& !(++checkpoint
% 10))
652 /* TRANSLATORS: This is a ``checkpoint of read operation'',
653 *not* ``Reading a checkpoint''.
654 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
655 *not* ``Leyendo un punto de comprobaci@'on'' */
656 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
658 /* Clear the count of errors. This only applies to a single call to
661 read_error_count
= 0; /* clear error count */
663 if (write_archive_to_stdout
&& record_start_block
!= 0)
665 archive
= STDOUT_FILENO
;
666 status
= sys_write_archive_buffer ();
667 archive
= STDIN_FILENO
;
668 if (status
!= record_size
)
669 archive_write_error (status
);
672 status
= rmtread (archive
, record_start
->buffer
, record_size
);
673 if (status
== record_size
)
678 /* Flush the current buffer to/from the archive. */
682 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
683 record_start_block
+= record_end
- record_start
;
684 current_block
= record_start
;
685 record_end
= record_start
+ blocking_factor
;
687 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
689 access_mode
= ACCESS_WRITE
;
690 time_to_start_writing
= false;
701 flush_write_ptr (buffer_level
);
709 /* Backspace the archive descriptor by one record worth. If it's a
710 tape, MTIOCTOP will work. If it's something else, try to seek on
711 it. If we can't seek, we lose! */
713 backspace_output (void)
717 struct mtop operation
;
719 operation
.mt_op
= MTBSR
;
720 operation
.mt_count
= 1;
721 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
723 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
729 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
731 /* Seek back to the beginning of this record and start writing there. */
733 position
-= record_size
;
736 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
738 /* Lseek failed. Try a different method. */
741 _("Cannot backspace archive file; it may be unreadable without -i")));
743 /* Replace the first part of the record with NULs. */
745 if (record_start
->buffer
!= output_start
)
746 memset (record_start
->buffer
, 0,
747 output_start
- record_start
->buffer
);
753 seek_archive (off_t size
)
755 off_t start
= current_block_ordinal ();
758 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
760 size
-= skipped
* BLOCKSIZE
;
762 if (size
< record_size
)
766 /* Compute number of records to skip */
767 nrec
= size
/ record_size
;
768 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
772 if (offset
% record_size
)
773 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
775 /* Convert to number of records */
777 /* Compute number of skipped blocks */
778 nblk
= offset
- start
;
780 /* Update buffering info */
781 records_read
+= nblk
/ blocking_factor
;
782 record_start_block
= offset
- blocking_factor
;
783 current_block
= record_end
;
788 /* Close the archive file. */
792 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
795 if (current_block
> record_start
)
799 sys_drain_input_pipe ();
805 if (rmtclose (archive
) != 0)
806 close_warn (*archive_name_cursor
);
808 sys_wait_for_child (child_pid
);
810 tar_stat_destroy (¤t_stat_info
);
815 free (record_buffer
[0]);
816 free (record_buffer
[1]);
819 /* Called to initialize the global volume number. */
821 init_volume_number (void)
823 FILE *file
= fopen (volno_file_option
, "r");
827 if (fscanf (file
, "%d", &global_volno
) != 1
829 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
830 quotearg_colon (volno_file_option
)));
832 read_error (volno_file_option
);
833 if (fclose (file
) != 0)
834 close_error (volno_file_option
);
836 else if (errno
!= ENOENT
)
837 open_error (volno_file_option
);
840 /* Called to write out the closing global volume number. */
842 closeout_volume_number (void)
844 FILE *file
= fopen (volno_file_option
, "w");
848 fprintf (file
, "%d\n", global_volno
);
850 write_error (volno_file_option
);
851 if (fclose (file
) != 0)
852 close_error (volno_file_option
);
855 open_error (volno_file_option
);
860 increase_volume_number ()
863 if (global_volno
< 0)
864 FATAL_ERROR ((0, 0, _("Volume number overflow")));
869 change_tape_menu (FILE *read_file
)
871 char *input_buffer
= NULL
;
876 fputc ('\007', stderr
);
878 _("Prepare volume #%d for %s and hit return: "),
879 global_volno
+ 1, quote (*archive_name_cursor
));
882 if (getline (&input_buffer
, &size
, read_file
) <= 0)
884 WARN ((0, 0, _("EOF where user reply was expected")));
886 if (subcommand_option
!= EXTRACT_SUBCOMMAND
887 && subcommand_option
!= LIST_SUBCOMMAND
888 && subcommand_option
!= DIFF_SUBCOMMAND
)
889 WARN ((0, 0, _("WARNING: Archive is incomplete")));
894 if (input_buffer
[0] == '\n'
895 || input_buffer
[0] == 'y'
896 || input_buffer
[0] == 'Y')
899 switch (input_buffer
[0])
903 fprintf (stderr
, _("\
904 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
906 y or newline Continue operation\n"));
907 if (!restrict_option
)
908 fprintf (stderr
, _(" ! Spawn a subshell\n"));
909 fprintf (stderr
, _(" ? Print this list\n"));
916 WARN ((0, 0, _("No new volume; exiting.\n")));
918 if (subcommand_option
!= EXTRACT_SUBCOMMAND
919 && subcommand_option
!= LIST_SUBCOMMAND
920 && subcommand_option
!= DIFF_SUBCOMMAND
)
921 WARN ((0, 0, _("WARNING: Archive is incomplete")));
926 /* Get new file name. */
932 for (name
= input_buffer
+ 1;
933 *name
== ' ' || *name
== '\t';
937 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
941 /* FIXME: the following allocation is never reclaimed. */
942 *archive_name_cursor
= xstrdup (name
);
947 if (!restrict_option
)
955 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
961 /* We've hit the end of the old volume. Close it and open the next one.
962 Return nonzero on success.
965 new_volume (enum access_mode mode
)
967 static FILE *read_file
;
971 if (!read_file
&& !info_script_option
)
972 /* FIXME: if fopen is used, it will never be closed. */
973 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
980 assign_string (&volume_label
, NULL
);
981 assign_string (&continued_file_name
, NULL
);
982 continued_file_size
= continued_file_offset
= 0;
984 if (rmtclose (archive
) != 0)
985 close_warn (*archive_name_cursor
);
987 archive_name_cursor
++;
988 if (archive_name_cursor
== archive_name_array
+ archive_names
)
990 archive_name_cursor
= archive_name_array
;
998 /* We have to prompt from now on. */
1000 if (info_script_option
)
1002 if (volno_file_option
)
1003 closeout_volume_number ();
1004 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1005 FATAL_ERROR ((0, 0, _("%s command failed"),
1006 quote (info_script_option
)));
1009 change_tape_menu (read_file
);
1012 if (strcmp (archive_name_cursor
[0], "-") == 0)
1014 read_full_records
= true;
1015 archive
= STDIN_FILENO
;
1017 else if (verify_option
)
1018 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1019 rsh_command_option
);
1024 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1025 rsh_command_option
);
1030 maybe_backup_file (*archive_name_cursor
, 1);
1031 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1032 rsh_command_option
);
1036 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1037 rsh_command_option
);
1043 open_warn (*archive_name_cursor
);
1044 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1045 undo_last_backup ();
1050 SET_BINARY_MODE (archive
);
1058 enum read_header rc
= read_header (false);
1060 if (rc
== HEADER_SUCCESS
)
1062 set_next_block_after (current_header
);
1065 ERROR ((0, 0, _("This does not look like a tar archive")));
1073 union block
*header
;
1075 switch (subcommand_option
)
1077 case APPEND_SUBCOMMAND
:
1078 case CAT_SUBCOMMAND
:
1079 case UPDATE_SUBCOMMAND
:
1080 if (!new_volume (ACCESS_UPDATE
))
1085 if (!new_volume (ACCESS_READ
))
1090 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1092 archive_read_error ();
1094 if (status
!= record_size
)
1095 short_read (status
);
1097 header
= find_next_block ();
1100 switch (header
->header
.typeflag
)
1104 struct tar_stat_info dummy
;
1105 if (!read_header0 ())
1107 tar_stat_init (&dummy
);
1108 xheader_decode (&dummy
); /* decodes values from the global header */
1109 tar_stat_destroy (&dummy
);
1112 /* We have read the extended header of the first member in
1113 this volume. Put it back, so next read_header works as
1115 current_block
= record_start
;
1120 case GNUTYPE_VOLHDR
:
1121 if (!read_header0 ())
1123 assign_string (&volume_label
, current_header
->header
.name
);
1124 set_next_block_after (header
);
1125 header
= find_next_block ();
1126 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1130 case GNUTYPE_MULTIVOL
:
1131 if (!read_header0 ())
1133 assign_string (&continued_file_name
, current_header
->header
.name
);
1134 continued_file_size
=
1135 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1136 continued_file_offset
=
1137 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1147 if (!continued_file_name
1148 || strcmp (continued_file_name
, real_s_name
))
1150 WARN ((0, 0, _("%s is not continued on this volume"),
1151 quote (real_s_name
)));
1155 s
= continued_file_size
+ continued_file_offset
;
1157 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1159 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1160 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1161 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1163 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1164 quote (continued_file_name
),
1165 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1166 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1167 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1171 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1173 WARN ((0, 0, _("This volume is out of sequence")));
1178 increase_volume_number ();
1183 /* Check the LABEL block against the volume label, seen as a globbing
1184 pattern. Return true if the pattern matches. In case of failure,
1185 retry matching a volume sequence number before giving up in
1186 multi-volume mode. */
1188 check_label_pattern (union block
*label
)
1193 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1196 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1199 if (!multi_volume_option
)
1202 string
= xmalloc (strlen (volume_label_option
)
1203 + sizeof VOLUME_LABEL_APPEND
+ 1);
1204 strcpy (string
, volume_label_option
);
1205 strcat (string
, VOLUME_LABEL_APPEND
);
1206 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1211 /* Check if the next block contains a volume label and if this matches
1212 the one given in the command line */
1214 match_volume_label (void)
1216 union block
*label
= find_next_block ();
1219 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1220 quote (volume_label_option
)));
1221 if (!check_label_pattern (label
))
1222 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1223 quote_n (0, label
->header
.name
),
1224 quote_n (1, volume_label_option
)));
1227 /* Mark the archive with volume label STR. */
1229 _write_volume_label (const char *str
)
1231 if (archive_format
== POSIX_FORMAT
)
1232 xheader_store ("GNU.volume.label", NULL
, str
);
1235 union block
*label
= find_next_block ();
1237 memset (label
, 0, BLOCKSIZE
);
1239 strcpy (label
->header
.name
, volume_label_option
);
1240 assign_string (¤t_stat_info
.file_name
,
1241 label
->header
.name
);
1242 current_stat_info
.had_trailing_slash
=
1243 strip_trailing_slashes (current_stat_info
.file_name
);
1245 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1246 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1247 finish_header (¤t_stat_info
, label
, -1);
1248 set_next_block_after (label
);
1252 #define VOL_SUFFIX "Volume"
1254 /* Add a volume label to a part of multi-volume archive */
1256 add_volume_label (void)
1258 char buf
[UINTMAX_STRSIZE_BOUND
];
1259 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1260 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1262 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1263 _write_volume_label (s
);
1270 if (archive_format
== POSIX_FORMAT
)
1272 off_t block_ordinal
;
1274 struct tar_stat_info st
;
1275 static size_t real_s_part_no
; /* FIXME */
1278 memset (&st
, 0, sizeof st
);
1279 st
.orig_file_name
= st
.file_name
= real_s_name
;
1280 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1281 st
.stat
.st_uid
= getuid ();
1282 st
.stat
.st_gid
= getgid ();
1283 st
.orig_file_name
= xheader_format_name (&st
,
1284 "%d/GNUFileParts.%p/%f.%n",
1286 st
.file_name
= st
.orig_file_name
;
1287 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1289 block_ordinal
= current_block_ordinal ();
1290 blk
= start_header (&st
);
1292 abort (); /* FIXME */
1293 finish_header (&st
, blk
, block_ordinal
);
1294 free (st
.orig_file_name
);
1299 /* Add a volume label to the current archive */
1301 write_volume_label (void)
1303 if (multi_volume_option
)
1304 add_volume_label ();
1306 _write_volume_label (volume_label_option
);
1309 /* Write GNU multi-volume header */
1311 gnu_add_multi_volume_header (void)
1314 union block
*block
= find_next_block ();
1316 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1318 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1319 quotearg_colon (real_s_name
)));
1321 memset (block
, 0, BLOCKSIZE
);
1323 /* FIXME: Michael P Urban writes: [a long name file] is being written
1324 when a new volume rolls around [...] Looks like the wrong value is
1325 being preserved in real_s_name, though. */
1327 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1328 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1330 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1331 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1332 block
->oldgnu_header
.offset
);
1334 tmp
= verbose_option
;
1336 finish_header (¤t_stat_info
, block
, -1);
1337 verbose_option
= tmp
;
1338 set_next_block_after (block
);
1341 /* Add a multi volume header to the current archive. The exact header format
1342 depends on the archive format. */
1344 add_multi_volume_header (void)
1346 if (archive_format
== POSIX_FORMAT
)
1348 off_t d
= real_s_totsize
- real_s_sizeleft
;
1349 xheader_store ("GNU.volume.filename", NULL
, real_s_name
);
1350 xheader_store ("GNU.volume.size", NULL
, &real_s_sizeleft
);
1351 xheader_store ("GNU.volume.offset", NULL
, &d
);
1354 gnu_add_multi_volume_header ();
1357 /* Synchronize multi-volume globals */
1359 multi_volume_sync ()
1361 if (multi_volume_option
)
1365 assign_string (&real_s_name
,
1366 safer_name_suffix (save_name
, false,
1367 absolute_names_option
));
1368 real_s_totsize
= save_totsize
;
1369 real_s_sizeleft
= save_sizeleft
;
1373 assign_string (&real_s_name
, 0);
1375 real_s_sizeleft
= 0;
1381 /* Low-level flush functions */
1383 /* Simple flush read (no multi-volume or label extensions) */
1385 simple_flush_read (void)
1387 size_t status
; /* result from system call */
1389 if (checkpoint_option
&& !(++checkpoint
% 10))
1390 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1391 *not* ``Reading a checkpoint''.
1392 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1393 *not* ``Leyendo un punto de comprobaci@'on'' */
1394 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1396 /* Clear the count of errors. This only applies to a single call to
1399 read_error_count
= 0; /* clear error count */
1401 if (write_archive_to_stdout
&& record_start_block
!= 0)
1403 archive
= STDOUT_FILENO
;
1404 status
= sys_write_archive_buffer ();
1405 archive
= STDIN_FILENO
;
1406 if (status
!= record_size
)
1407 archive_write_error (status
);
1412 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1413 if (status
== record_size
)
1418 if (status
== SAFE_READ_ERROR
)
1420 archive_read_error ();
1421 continue; /* try again */
1425 short_read (status
);
1428 /* Simple flush write (no multi-volume or label extensions) */
1430 simple_flush_write (size_t level
__attribute__((unused
)))
1434 status
= _flush_write ();
1435 if (status
!= record_size
)
1436 archive_write_error (status
);
1440 bytes_written
+= status
;
1445 /* GNU flush functions. These support multi-volume and archive labels in
1446 GNU and PAX archive formats. */
1449 _gnu_flush_read (void)
1451 size_t status
; /* result from system call */
1453 if (checkpoint_option
&& !(++checkpoint
% 10))
1454 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1455 *not* ``Reading a checkpoint''.
1456 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1457 *not* ``Leyendo un punto de comprobaci@'on'' */
1458 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1460 /* Clear the count of errors. This only applies to a single call to
1463 read_error_count
= 0; /* clear error count */
1465 if (write_archive_to_stdout
&& record_start_block
!= 0)
1467 archive
= STDOUT_FILENO
;
1468 status
= sys_write_archive_buffer ();
1469 archive
= STDIN_FILENO
;
1470 if (status
!= record_size
)
1471 archive_write_error (status
);
1474 multi_volume_sync ();
1478 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1479 if (status
== record_size
)
1485 /* The condition below used to include
1486 || (status > 0 && !read_full_records)
1487 This is incorrect since even if new_volume() succeeds, the
1488 subsequent call to rmtread will overwrite the chunk of data
1489 already read in the buffer, so the processing will fail */
1491 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1492 && multi_volume_option
)
1494 while (!try_new_volume ())
1498 else if (status
== SAFE_READ_ERROR
)
1500 archive_read_error ();
1505 short_read (status
);
1509 gnu_flush_read (void)
1511 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1513 flush_read_ptr
= gnu_flush_read
;
1517 _gnu_flush_write (size_t buffer_level
)
1520 union block
*header
;
1525 status
= _flush_write ();
1526 if (status
!= record_size
&& !multi_volume_option
)
1527 archive_write_error (status
);
1531 bytes_written
+= status
;
1534 if (status
== record_size
)
1536 multi_volume_sync ();
1540 /* In multi-volume mode. */
1541 /* ENXIO is for the UNIX PC. */
1542 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1543 archive_write_error (status
);
1545 if (!new_volume (ACCESS_WRITE
))
1548 xheader_destroy (&extended_header
);
1550 increase_volume_number ();
1552 prev_written
+= bytes_written
;
1555 copy_ptr
= record_start
->buffer
+ status
;
1556 copy_size
= buffer_level
- status
;
1557 /* Switch to the next buffer */
1558 record_index
= !record_index
;
1561 if (volume_label_option
)
1562 add_volume_label ();
1565 add_multi_volume_header ();
1567 write_extended (true, NULL
, find_next_block ());
1569 add_chunk_header ();
1570 header
= find_next_block ();
1571 bufsize
= available_space_after (header
);
1572 while (bufsize
< copy_size
)
1574 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1575 copy_ptr
+= bufsize
;
1576 copy_size
-= bufsize
;
1577 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1578 header
= find_next_block ();
1579 bufsize
= available_space_after (header
);
1581 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1582 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1583 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1588 gnu_flush_write (size_t buffer_level
)
1590 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1591 _gnu_flush_write (buffer_level
);
1592 flush_write_ptr
= gnu_flush_write
;
1604 flush_write_ptr (record_size
);
1608 open_archive (enum access_mode wanted_access
)
1610 flush_read_ptr
= gnu_flush_read
;
1611 flush_write_ptr
= gnu_flush_write
;
1613 _open_archive (wanted_access
);
1614 switch (wanted_access
)
1617 if (volume_label_option
)
1618 match_volume_label ();
1622 records_written
= 0;
1623 if (volume_label_option
)
1624 write_volume_label ();