1 /* Buffer management for tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-08-25.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
33 /* Number of retries before giving up on read. */
34 #define READ_ERROR_MAX 10
36 /* Globbing pattern to append to volume label if initial match failed. */
37 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
41 static tarlong prev_written
; /* bytes written on previous volumes */
42 static tarlong bytes_written
; /* bytes written on this volume */
43 static void *record_buffer
[2]; /* allocated memory */
44 static int record_index
;
46 /* FIXME: The following variables should ideally be static to this
47 module. However, this cannot be done yet. The cleanup continues! */
49 union block
*record_start
; /* start of record of archive */
50 union block
*record_end
; /* last+1 block of archive record */
51 union block
*current_block
; /* current block of archive */
52 enum access_mode access_mode
; /* how do we handle the archive */
53 off_t records_read
; /* number of records read from this archive */
54 off_t records_written
; /* likewise, for records written */
56 static off_t record_start_block
; /* block ordinal at record_start */
58 /* Where we write list messages (not errors, not interactions) to. */
61 static void backspace_output (void);
63 /* PID of child program, if compress_option or remote archive access. */
64 static pid_t child_pid
;
66 /* Error recovery stuff */
67 static int read_error_count
;
69 /* Have we hit EOF yet? */
72 /* Checkpointing counter */
73 static int checkpoint
;
75 static bool read_full_records
= false;
77 /* We're reading, but we just read the last block and it's time to update.
80 As least EXTERN like this one as possible. (?? --gray)
81 FIXME: Either eliminate it or move it to common.h.
83 extern bool time_to_start_writing
;
85 bool write_archive_to_stdout
;
87 void (*flush_write_ptr
) (size_t);
88 void (*flush_read_ptr
) (void);
92 char *continued_file_name
;
93 uintmax_t continued_file_size
;
94 uintmax_t continued_file_offset
;
97 static int volno
= 1; /* which volume of a multi-volume tape we're
99 static int global_volno
= 1; /* volume number to print in external
102 bool write_archive_to_stdout
;
104 /* Used by flush_read and flush_write to store the real info about saved
106 static char *real_s_name
;
107 static off_t real_s_totsize
;
108 static off_t real_s_sizeleft
;
111 /* Multi-volume tracking support */
112 static char *save_name
; /* name of the file we are currently writing */
113 static off_t save_totsize
; /* total size of file we are writing, only
114 valid if save_name is nonzero */
115 static off_t save_sizeleft
; /* where we are in the file we are writing,
116 only valid if save_name is nonzero */
119 mv_begin (struct tar_stat_info
*st
)
121 if (multi_volume_option
)
123 assign_string (&save_name
, st
->orig_file_name
);
124 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
131 if (multi_volume_option
)
132 assign_string (&save_name
, 0);
136 mv_total_size (off_t size
)
142 mv_size_left (off_t size
)
144 save_sizeleft
= size
;
151 clear_read_error_count (void)
153 read_error_count
= 0;
157 /* Time-related functions */
164 gettime (&start_time
);
172 duration
+= ((now
.tv_sec
- start_time
.tv_sec
)
173 + (now
.tv_nsec
- start_time
.tv_nsec
) / 1e9
);
178 /* Compression detection */
189 enum compress_type type
;
196 static struct zip_magic
const magic
[] = {
198 { ct_compress
, 2, "\037\235", "compress", "-Z" },
199 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
200 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
203 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
205 #define compress_option(t) magic[t].option
206 #define compress_program(t) magic[t].program
208 /* Check if the file ARCHIVE is a compressed archive. */
210 check_compressed_archive ()
212 struct zip_magic
const *p
;
215 /* Prepare global data needed for find_next_block: */
216 record_end
= record_start
; /* set up for 1st record = # 0 */
217 sfr
= read_full_records
;
218 read_full_records
= true; /* Suppress fatal error on reading a partial
222 /* Restore global values */
223 read_full_records
= sfr
;
225 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
226 /* Probably a valid header */
229 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
230 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
236 /* Open an archive named archive_name_array[0]. Detect if it is
237 a compressed archive of known type and use corresponding decompression
240 open_compressed_archive ()
242 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
243 MODE_RW
, rsh_command_option
);
247 if (!multi_volume_option
)
249 enum compress_type type
= check_compressed_archive ();
254 /* FD is not needed any more */
257 hit_eof
= false; /* It might have been set by find_next_block in
258 check_compressed_archive */
260 /* Open compressed archive */
261 use_compress_program_option
= compress_program (type
);
262 child_pid
= sys_child_open_for_uncompress ();
263 read_full_records
= true;
267 record_end
= record_start
; /* set up for 1st record = # 0 */
274 print_total_written (void)
276 tarlong written
= prev_written
+ bytes_written
;
277 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
278 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
279 char rate
[LONGEST_HUMAN_READABLE
+ 1];
281 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
283 sprintf (bytes
, TARLONG_FORMAT
, written
);
285 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
286 fprintf (stderr
, _("Total bytes written: %s (%s, %s/s)\n"), bytes
,
287 human_readable (written
, abbr
, human_opts
, 1, 1),
288 (0 < duration
&& written
/ duration
< (uintmax_t) -1
289 ? human_readable (written
/ duration
, rate
, human_opts
, 1, 1)
293 /* Compute and return the block ordinal at current_block. */
295 current_block_ordinal (void)
297 return record_start_block
+ (current_block
- record_start
);
300 /* If the EOF flag is set, reset it, as well as current_block, etc. */
307 current_block
= record_start
;
308 record_end
= record_start
+ blocking_factor
;
309 access_mode
= ACCESS_WRITE
;
313 /* Return the location of the next available input or output block.
314 Return zero for EOF. Once we have returned zero, we just keep returning
315 it, to avoid accidentally going on to the next file on the tape. */
317 find_next_block (void)
319 if (current_block
== record_end
)
324 if (current_block
== record_end
)
330 return current_block
;
333 /* Indicate that we have used all blocks up thru BLOCK. */
335 set_next_block_after (union block
*block
)
337 while (block
>= current_block
)
340 /* Do *not* flush the archive here. If we do, the same argument to
341 set_next_block_after could mean the next block (if the input record
342 is exactly one block long), which is not what is intended. */
344 if (current_block
> record_end
)
348 /* Return the number of bytes comprising the space between POINTER
349 through the end of the current buffer of blocks. This space is
350 available for filling with data, or taking data from. POINTER is
351 usually (but not always) the result of previous find_next_block call. */
353 available_space_after (union block
*pointer
)
355 return record_end
->buffer
- pointer
->buffer
;
358 /* Close file having descriptor FD, and abort if close unsuccessful. */
363 close_error (_("(pipe)"));
369 if (!record_buffer
[record_index
])
370 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
372 record_start
= record_buffer
[record_index
];
373 current_block
= record_start
;
374 record_end
= record_start
+ blocking_factor
;
377 /* Open an archive file. The argument specifies whether we are
378 reading or writing, or both. */
380 _open_archive (enum access_mode wanted_access
)
382 int backed_up_flag
= 0;
386 stdlis
= fopen (index_file_name
, "w");
388 open_error (index_file_name
);
391 stdlis
= to_stdout_option
? stderr
: stdout
;
393 if (record_size
== 0)
394 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
396 if (archive_names
== 0)
397 FATAL_ERROR ((0, 0, _("No archive name given")));
399 tar_stat_destroy (¤t_stat_info
);
406 /* When updating the archive, we start with reading. */
407 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
409 read_full_records
= read_full_records_option
;
413 if (use_compress_program_option
)
415 switch (wanted_access
)
418 child_pid
= sys_child_open_for_uncompress ();
419 read_full_records
= true;
420 record_end
= record_start
; /* set up for 1st record = # 0 */
424 child_pid
= sys_child_open_for_compress ();
428 abort (); /* Should not happen */
432 if (wanted_access
== ACCESS_WRITE
433 && strcmp (archive_name_array
[0], "-") == 0)
436 else if (strcmp (archive_name_array
[0], "-") == 0)
438 read_full_records
= true; /* could be a pipe, be safe */
440 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
442 switch (wanted_access
)
446 enum compress_type type
;
448 archive
= STDIN_FILENO
;
450 type
= check_compressed_archive (archive
);
453 _("Archive is compressed. Use %s option"),
454 compress_option (type
)));
459 archive
= STDOUT_FILENO
;
464 archive
= STDIN_FILENO
;
466 write_archive_to_stdout
= true;
470 else if (verify_option
)
471 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
472 MODE_RW
, rsh_command_option
);
474 switch (wanted_access
)
477 archive
= open_compressed_archive ();
483 maybe_backup_file (archive_name_array
[0], 1);
486 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
491 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
492 MODE_RW
, rsh_command_option
);
497 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
499 int saved_errno
= errno
;
504 open_fatal (archive_name_array
[0]);
507 sys_detect_dev_null_output ();
508 sys_save_archive_dev_ino ();
509 SET_BINARY_MODE (archive
);
511 switch (wanted_access
)
515 record_end
= record_start
; /* set up for 1st record = # 0 */
518 find_next_block (); /* read it in, check for EOF */
527 /* Perform a write to flush the buffer. */
533 if (checkpoint_option
&& !(++checkpoint
% 10))
534 /* TRANSLATORS: This is a ``checkpoint of write operation'',
535 *not* ``Writing a checkpoint''.
536 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
537 *not* ``Escribiendo un punto de comprobaci@'on'' */
538 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
540 if (tape_length_option
&& tape_length_option
<= bytes_written
)
545 else if (dev_null_output
)
546 status
= record_size
;
548 status
= sys_write_archive_buffer ();
553 /* Handle write errors on the archive. Write errors are always fatal.
554 Hitting the end of a volume does not cause a write error unless the
555 write was the first record of the volume. */
557 archive_write_error (ssize_t status
)
559 /* It might be useful to know how much was written before the error
564 print_total_written ();
568 write_fatal_details (*archive_name_cursor
, status
, record_size
);
571 /* Handle read errors on the archive. If the read should be retried,
572 return to the caller. */
574 archive_read_error (void)
576 read_error (*archive_name_cursor
);
578 if (record_start_block
== 0)
579 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
581 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
582 then give up on reading the archive. */
584 if (read_error_count
++ > READ_ERROR_MAX
)
585 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
590 short_read (size_t status
)
592 size_t left
; /* bytes left */
593 char *more
; /* pointer to next byte to read */
595 more
= record_start
->buffer
+ status
;
596 left
= record_size
- status
;
598 while (left
% BLOCKSIZE
!= 0
599 || (left
&& status
&& read_full_records
))
602 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
603 archive_read_error ();
608 if (! read_full_records
)
610 unsigned long rest
= record_size
- left
;
613 ngettext ("Unaligned block (%lu byte) in archive",
614 "Unaligned block (%lu bytes) in archive",
619 /* User warned us about this. Fix up. */
625 /* FIXME: for size=0, multi-volume support. On the first record, warn
626 about the problem. */
628 if (!read_full_records
&& verbose_option
> 1
629 && record_start_block
== 0 && status
!= 0)
631 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
633 ngettext ("Record size = %lu block",
634 "Record size = %lu blocks",
639 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
643 /* Perform a read to flush the buffer. */
647 size_t status
; /* result from system call */
649 if (checkpoint_option
&& !(++checkpoint
% 10))
650 /* TRANSLATORS: This is a ``checkpoint of read operation'',
651 *not* ``Reading a checkpoint''.
652 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
653 *not* ``Leyendo un punto de comprobaci@'on'' */
654 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
656 /* Clear the count of errors. This only applies to a single call to
659 read_error_count
= 0; /* clear error count */
661 if (write_archive_to_stdout
&& record_start_block
!= 0)
663 archive
= STDOUT_FILENO
;
664 status
= sys_write_archive_buffer ();
665 archive
= STDIN_FILENO
;
666 if (status
!= record_size
)
667 archive_write_error (status
);
670 status
= rmtread (archive
, record_start
->buffer
, record_size
);
671 if (status
== record_size
)
676 /* Flush the current buffer to/from the archive. */
680 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
681 record_start_block
+= record_end
- record_start
;
682 current_block
= record_start
;
683 record_end
= record_start
+ blocking_factor
;
685 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
687 access_mode
= ACCESS_WRITE
;
688 time_to_start_writing
= false;
699 flush_write_ptr (buffer_level
);
707 /* Backspace the archive descriptor by one record worth. If it's a
708 tape, MTIOCTOP will work. If it's something else, try to seek on
709 it. If we can't seek, we lose! */
711 backspace_output (void)
715 struct mtop operation
;
717 operation
.mt_op
= MTBSR
;
718 operation
.mt_count
= 1;
719 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
721 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
727 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
729 /* Seek back to the beginning of this record and start writing there. */
731 position
-= record_size
;
734 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
736 /* Lseek failed. Try a different method. */
739 _("Cannot backspace archive file; it may be unreadable without -i")));
741 /* Replace the first part of the record with NULs. */
743 if (record_start
->buffer
!= output_start
)
744 memset (record_start
->buffer
, 0,
745 output_start
- record_start
->buffer
);
751 seek_archive (off_t size
)
753 off_t start
= current_block_ordinal ();
756 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
758 size
-= skipped
* BLOCKSIZE
;
760 if (size
< record_size
)
764 /* Compute number of records to skip */
765 nrec
= size
/ record_size
;
766 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
770 if (offset
% record_size
)
771 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
773 /* Convert to number of records */
775 /* Compute number of skipped blocks */
776 nblk
= offset
- start
;
778 /* Update buffering info */
779 records_read
+= nblk
/ blocking_factor
;
780 record_start_block
= offset
- blocking_factor
;
781 current_block
= record_end
;
786 /* Close the archive file. */
790 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
793 if (current_block
> record_start
)
797 sys_drain_input_pipe ();
803 if (rmtclose (archive
) != 0)
804 close_warn (*archive_name_cursor
);
806 sys_wait_for_child (child_pid
);
808 tar_stat_destroy (¤t_stat_info
);
813 free (record_buffer
[0]);
814 free (record_buffer
[1]);
817 /* Called to initialize the global volume number. */
819 init_volume_number (void)
821 FILE *file
= fopen (volno_file_option
, "r");
825 if (fscanf (file
, "%d", &global_volno
) != 1
827 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
828 quotearg_colon (volno_file_option
)));
830 read_error (volno_file_option
);
831 if (fclose (file
) != 0)
832 close_error (volno_file_option
);
834 else if (errno
!= ENOENT
)
835 open_error (volno_file_option
);
838 /* Called to write out the closing global volume number. */
840 closeout_volume_number (void)
842 FILE *file
= fopen (volno_file_option
, "w");
846 fprintf (file
, "%d\n", global_volno
);
848 write_error (volno_file_option
);
849 if (fclose (file
) != 0)
850 close_error (volno_file_option
);
853 open_error (volno_file_option
);
858 increase_volume_number ()
861 if (global_volno
< 0)
862 FATAL_ERROR ((0, 0, _("Volume number overflow")));
867 change_tape_menu (FILE *read_file
)
869 char *input_buffer
= NULL
;
874 fputc ('\007', stderr
);
876 _("Prepare volume #%d for %s and hit return: "),
877 global_volno
+ 1, quote (*archive_name_cursor
));
880 if (getline (&input_buffer
, &size
, read_file
) <= 0)
882 WARN ((0, 0, _("EOF where user reply was expected")));
884 if (subcommand_option
!= EXTRACT_SUBCOMMAND
885 && subcommand_option
!= LIST_SUBCOMMAND
886 && subcommand_option
!= DIFF_SUBCOMMAND
)
887 WARN ((0, 0, _("WARNING: Archive is incomplete")));
892 if (input_buffer
[0] == '\n'
893 || input_buffer
[0] == 'y'
894 || input_buffer
[0] == 'Y')
897 switch (input_buffer
[0])
901 fprintf (stderr
, _("\
902 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
904 y or newline Continue operation\n"));
905 if (!restrict_option
)
906 fprintf (stderr
, _(" ! Spawn a subshell\n"));
907 fprintf (stderr
, _(" ? Print this list\n"));
914 WARN ((0, 0, _("No new volume; exiting.\n")));
916 if (subcommand_option
!= EXTRACT_SUBCOMMAND
917 && subcommand_option
!= LIST_SUBCOMMAND
918 && subcommand_option
!= DIFF_SUBCOMMAND
)
919 WARN ((0, 0, _("WARNING: Archive is incomplete")));
924 /* Get new file name. */
930 for (name
= input_buffer
+ 1;
931 *name
== ' ' || *name
== '\t';
935 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
939 /* FIXME: the following allocation is never reclaimed. */
940 *archive_name_cursor
= xstrdup (name
);
945 if (!restrict_option
)
953 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
959 /* We've hit the end of the old volume. Close it and open the next one.
960 Return nonzero on success.
963 new_volume (enum access_mode mode
)
965 static FILE *read_file
;
968 if (!read_file
&& !info_script_option
)
969 /* FIXME: if fopen is used, it will never be closed. */
970 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
977 assign_string (&volume_label
, NULL
);
978 assign_string (&continued_file_name
, NULL
);
979 continued_file_size
= continued_file_offset
= 0;
981 if (rmtclose (archive
) != 0)
982 close_warn (*archive_name_cursor
);
984 archive_name_cursor
++;
985 if (archive_name_cursor
== archive_name_array
+ archive_names
)
987 archive_name_cursor
= archive_name_array
;
994 /* We have to prompt from now on. */
996 if (info_script_option
)
998 if (volno_file_option
)
999 closeout_volume_number ();
1000 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1001 FATAL_ERROR ((0, 0, _("%s command failed"),
1002 quote (info_script_option
)));
1005 change_tape_menu (read_file
);
1008 if (strcmp (archive_name_cursor
[0], "-") == 0)
1010 read_full_records
= true;
1011 archive
= STDIN_FILENO
;
1013 else if (verify_option
)
1014 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1015 rsh_command_option
);
1020 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1021 rsh_command_option
);
1026 maybe_backup_file (*archive_name_cursor
, 1);
1027 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1028 rsh_command_option
);
1032 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1033 rsh_command_option
);
1039 open_warn (*archive_name_cursor
);
1040 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1041 undo_last_backup ();
1045 SET_BINARY_MODE (archive
);
1053 enum read_header rc
= read_header (false);
1055 if (rc
== HEADER_SUCCESS
)
1057 set_next_block_after (current_header
);
1060 ERROR ((0, 0, _("This does not look like a tar archive")));
1068 union block
*header
;
1070 switch (subcommand_option
)
1072 case APPEND_SUBCOMMAND
:
1073 case CAT_SUBCOMMAND
:
1074 case UPDATE_SUBCOMMAND
:
1075 if (!new_volume (ACCESS_UPDATE
))
1080 if (!new_volume (ACCESS_READ
))
1085 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1087 archive_read_error ();
1089 if (status
!= record_size
)
1090 short_read (status
);
1092 header
= find_next_block ();
1095 switch (header
->header
.typeflag
)
1099 struct tar_stat_info dummy
;
1100 if (!read_header0 ())
1102 tar_stat_init (&dummy
);
1103 xheader_decode (&dummy
); /* decodes values from the global header */
1104 tar_stat_destroy (&dummy
);
1107 /* We have read the extended header of the first member in
1108 this volume. Put it back, so next read_header works as
1110 current_block
= record_start
;
1115 case GNUTYPE_VOLHDR
:
1116 if (!read_header0 ())
1118 assign_string (&volume_label
, current_header
->header
.name
);
1119 set_next_block_after (header
);
1120 header
= find_next_block ();
1121 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1125 case GNUTYPE_MULTIVOL
:
1126 if (!read_header0 ())
1128 assign_string (&continued_file_name
, current_header
->header
.name
);
1129 continued_file_size
=
1130 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1131 continued_file_offset
=
1132 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1142 if (!continued_file_name
1143 || strcmp (continued_file_name
, real_s_name
))
1145 WARN ((0, 0, _("%s is not continued on this volume"),
1146 quote (real_s_name
)));
1150 s
= continued_file_size
+ continued_file_offset
;
1152 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1154 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1155 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1156 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1158 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1159 quote (continued_file_name
),
1160 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1161 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1162 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1166 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1168 WARN ((0, 0, _("This volume is out of sequence")));
1173 increase_volume_number ();
1178 /* Check the LABEL block against the volume label, seen as a globbing
1179 pattern. Return true if the pattern matches. In case of failure,
1180 retry matching a volume sequence number before giving up in
1181 multi-volume mode. */
1183 check_label_pattern (union block
*label
)
1188 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1191 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1194 if (!multi_volume_option
)
1197 string
= xmalloc (strlen (volume_label_option
)
1198 + sizeof VOLUME_LABEL_APPEND
+ 1);
1199 strcpy (string
, volume_label_option
);
1200 strcat (string
, VOLUME_LABEL_APPEND
);
1201 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1206 /* Check if the next block contains a volume label and if this matches
1207 the one given in the command line */
1209 match_volume_label (void)
1211 union block
*label
= find_next_block ();
1214 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1215 quote (volume_label_option
)));
1216 if (!check_label_pattern (label
))
1217 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1218 quote_n (0, label
->header
.name
),
1219 quote_n (1, volume_label_option
)));
1222 /* Mark the archive with volume label STR. */
1224 _write_volume_label (const char *str
)
1226 if (archive_format
== POSIX_FORMAT
)
1227 xheader_store ("GNU.volume.label", NULL
, str
);
1230 union block
*label
= find_next_block ();
1232 memset (label
, 0, BLOCKSIZE
);
1234 strcpy (label
->header
.name
, volume_label_option
);
1235 assign_string (¤t_stat_info
.file_name
,
1236 label
->header
.name
);
1237 current_stat_info
.had_trailing_slash
=
1238 strip_trailing_slashes (current_stat_info
.file_name
);
1240 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1241 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1242 finish_header (¤t_stat_info
, label
, -1);
1243 set_next_block_after (label
);
1247 #define VOL_SUFFIX "Volume"
1249 /* Add a volume label to a part of multi-volume archive */
1251 add_volume_label (void)
1253 char buf
[UINTMAX_STRSIZE_BOUND
];
1254 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1255 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1257 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1258 _write_volume_label (s
);
1265 if (archive_format
== POSIX_FORMAT
)
1267 off_t block_ordinal
;
1269 struct tar_stat_info st
;
1270 static size_t real_s_part_no
; /* FIXME */
1273 memset (&st
, 0, sizeof st
);
1274 st
.orig_file_name
= st
.file_name
= real_s_name
;
1275 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1276 st
.stat
.st_uid
= getuid ();
1277 st
.stat
.st_gid
= getgid ();
1278 st
.orig_file_name
= xheader_format_name (&st
,
1279 "%d/GNUFileParts.%p/%f.%n",
1281 st
.file_name
= st
.orig_file_name
;
1282 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1284 block_ordinal
= current_block_ordinal ();
1285 blk
= start_header (&st
);
1286 free (st
.orig_file_name
);
1288 abort (); /* FIXME */
1289 finish_header (&st
, blk
, block_ordinal
);
1294 /* Add a volume label to the current archive */
1296 write_volume_label (void)
1298 if (multi_volume_option
)
1299 add_volume_label ();
1301 _write_volume_label (volume_label_option
);
1304 /* Write GNU multi-volume header */
1306 gnu_add_multi_volume_header (void)
1309 union block
*block
= find_next_block ();
1311 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1313 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1314 quotearg_colon (real_s_name
)));
1316 memset (block
, 0, BLOCKSIZE
);
1318 /* FIXME: Michael P Urban writes: [a long name file] is being written
1319 when a new volume rolls around [...] Looks like the wrong value is
1320 being preserved in real_s_name, though. */
1322 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1323 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1325 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1326 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1327 block
->oldgnu_header
.offset
);
1329 tmp
= verbose_option
;
1331 finish_header (¤t_stat_info
, block
, -1);
1332 verbose_option
= tmp
;
1333 set_next_block_after (block
);
1336 /* Add a multi volume header to the current archive. The exact header format
1337 depends on the archive format. */
1339 add_multi_volume_header (void)
1341 if (archive_format
== POSIX_FORMAT
)
1343 off_t d
= real_s_totsize
- real_s_sizeleft
;
1344 xheader_store ("GNU.volume.filename", NULL
, real_s_name
);
1345 xheader_store ("GNU.volume.size", NULL
, &real_s_sizeleft
);
1346 xheader_store ("GNU.volume.offset", NULL
, &d
);
1349 gnu_add_multi_volume_header ();
1352 /* Synchronize multi-volume globals */
1354 multi_volume_sync ()
1356 if (multi_volume_option
)
1360 assign_string (&real_s_name
,
1361 safer_name_suffix (save_name
, false,
1362 absolute_names_option
));
1363 real_s_totsize
= save_totsize
;
1364 real_s_sizeleft
= save_sizeleft
;
1368 assign_string (&real_s_name
, 0);
1370 real_s_sizeleft
= 0;
1376 /* Low-level flush functions */
1378 /* Simple flush read (no multi-volume or label extensions) */
1380 simple_flush_read (void)
1382 size_t status
; /* result from system call */
1384 if (checkpoint_option
&& !(++checkpoint
% 10))
1385 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1386 *not* ``Reading a checkpoint''.
1387 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1388 *not* ``Leyendo un punto de comprobaci@'on'' */
1389 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1391 /* Clear the count of errors. This only applies to a single call to
1394 read_error_count
= 0; /* clear error count */
1396 if (write_archive_to_stdout
&& record_start_block
!= 0)
1398 archive
= STDOUT_FILENO
;
1399 status
= sys_write_archive_buffer ();
1400 archive
= STDIN_FILENO
;
1401 if (status
!= record_size
)
1402 archive_write_error (status
);
1407 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1408 if (status
== record_size
)
1413 if (status
== SAFE_READ_ERROR
)
1415 archive_read_error ();
1416 continue; /* try again */
1420 short_read (status
);
1423 /* Simple flush write (no multi-volume or label extensions) */
1425 simple_flush_write (size_t level
__attribute__((unused
)))
1429 status
= _flush_write ();
1430 if (status
!= record_size
)
1431 archive_write_error (status
);
1435 bytes_written
+= status
;
1440 /* GNU flush functions. These support multi-volume and archive labels in
1441 GNU and PAX archive formats. */
1444 _gnu_flush_read (void)
1446 size_t status
; /* result from system call */
1448 if (checkpoint_option
&& !(++checkpoint
% 10))
1449 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1450 *not* ``Reading a checkpoint''.
1451 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1452 *not* ``Leyendo un punto de comprobaci@'on'' */
1453 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1455 /* Clear the count of errors. This only applies to a single call to
1458 read_error_count
= 0; /* clear error count */
1460 if (write_archive_to_stdout
&& record_start_block
!= 0)
1462 archive
= STDOUT_FILENO
;
1463 status
= sys_write_archive_buffer ();
1464 archive
= STDIN_FILENO
;
1465 if (status
!= record_size
)
1466 archive_write_error (status
);
1469 multi_volume_sync ();
1473 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1474 if (status
== record_size
)
1480 /* The condition below used to include
1481 || (status > 0 && !read_full_records)
1482 This is incorrect since even if new_volume() succeeds, the
1483 subsequent call to rmtread will overwrite the chunk of data
1484 already read in the buffer, so the processing will fail */
1486 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1487 && multi_volume_option
)
1489 while (!try_new_volume ())
1493 else if (status
== SAFE_READ_ERROR
)
1495 archive_read_error ();
1500 short_read (status
);
1504 gnu_flush_read (void)
1506 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1508 flush_read_ptr
= gnu_flush_read
;
1512 _gnu_flush_write (size_t buffer_level
)
1515 union block
*header
;
1520 status
= _flush_write ();
1521 if (status
!= record_size
&& !multi_volume_option
)
1522 archive_write_error (status
);
1526 bytes_written
+= status
;
1529 if (status
== record_size
)
1531 multi_volume_sync ();
1535 /* In multi-volume mode. */
1536 /* ENXIO is for the UNIX PC. */
1537 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1538 archive_write_error (status
);
1540 if (!new_volume (ACCESS_WRITE
))
1543 xheader_destroy (&extended_header
);
1545 increase_volume_number ();
1547 prev_written
+= bytes_written
;
1550 copy_ptr
= record_start
->buffer
+ status
;
1551 copy_size
= buffer_level
- status
;
1552 /* Switch to the next buffer */
1553 record_index
= !record_index
;
1556 if (volume_label_option
)
1557 add_volume_label ();
1560 add_multi_volume_header ();
1562 write_extended (true, NULL
, find_next_block ());
1564 add_chunk_header ();
1565 header
= find_next_block ();
1566 bufsize
= available_space_after (header
);
1567 while (bufsize
< copy_size
)
1569 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1570 copy_ptr
+= bufsize
;
1571 copy_size
-= bufsize
;
1572 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1573 header
= find_next_block ();
1574 bufsize
= available_space_after (header
);
1576 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1577 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1578 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1583 gnu_flush_write (size_t buffer_level
)
1585 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1586 _gnu_flush_write (buffer_level
);
1587 flush_write_ptr
= gnu_flush_write
;
1599 flush_write_ptr (record_size
);
1603 open_archive (enum access_mode wanted_access
)
1605 flush_read_ptr
= gnu_flush_read
;
1606 flush_write_ptr
= gnu_flush_write
;
1608 _open_archive (wanted_access
);
1609 switch (wanted_access
)
1612 if (volume_label_option
)
1613 match_volume_label ();
1617 records_written
= 0;
1618 if (volume_label_option
)
1619 write_volume_label ();