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 static int record_index
;
49 /* FIXME: The following variables should ideally be static to this
50 module. However, this cannot be done yet. The cleanup continues! */
52 union block
*record_start
; /* start of record of archive */
53 union block
*record_end
; /* last+1 block of archive record */
54 union block
*current_block
; /* current block of archive */
55 enum access_mode access_mode
; /* how do we handle the archive */
56 off_t records_read
; /* number of records read from this archive */
57 off_t records_written
; /* likewise, for records written */
59 static off_t record_start_block
; /* block ordinal at record_start */
61 /* Where we write list messages (not errors, not interactions) to. */
64 static void backspace_output (void);
66 /* PID of child program, if compress_option or remote archive access. */
67 static pid_t child_pid
;
69 /* Error recovery stuff */
70 static int read_error_count
;
72 /* Have we hit EOF yet? */
75 /* Checkpointing counter */
76 static int checkpoint
;
78 static bool read_full_records
= false;
80 /* We're reading, but we just read the last block and it's time to update.
83 As least EXTERN like this one as possible. (?? --gray)
84 FIXME: Either eliminate it or move it to common.h.
86 extern bool time_to_start_writing
;
88 bool write_archive_to_stdout
;
90 void (*flush_write_ptr
) (size_t);
91 void (*flush_read_ptr
) (void);
95 char *continued_file_name
;
96 uintmax_t continued_file_size
;
97 uintmax_t continued_file_offset
;
100 static int volno
= 1; /* which volume of a multi-volume tape we're
102 static int global_volno
= 1; /* volume number to print in external
105 bool write_archive_to_stdout
;
107 /* Used by flush_read and flush_write to store the real info about saved
109 static char *real_s_name
;
110 static off_t real_s_totsize
;
111 static off_t real_s_sizeleft
;
114 /* Multi-volume tracking support */
115 static char *save_name
; /* name of the file we are currently writing */
116 static off_t save_totsize
; /* total size of file we are writing, only
117 valid if save_name is nonzero */
118 static off_t save_sizeleft
; /* where we are in the file we are writing,
119 only valid if save_name is nonzero */
122 mv_begin (struct tar_stat_info
*st
)
124 if (multi_volume_option
)
126 assign_string (&save_name
, st
->orig_file_name
);
127 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
134 if (multi_volume_option
)
135 assign_string (&save_name
, 0);
139 mv_total_size (off_t size
)
145 mv_size_left (off_t size
)
147 save_sizeleft
= size
;
154 clear_read_error_count (void)
156 read_error_count
= 0;
160 /* Time-related functions */
167 gettime (&start_time
);
175 duration
+= ((now
.tv_sec
- start_time
.tv_sec
)
176 + (now
.tv_nsec
- start_time
.tv_nsec
) / 1e9
);
181 /* Compression detection */
192 enum compress_type type
;
199 static struct zip_magic
const magic
[] = {
201 { ct_compress
, 2, "\037\235", "compress", "-Z" },
202 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
203 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
206 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
208 #define compress_option(t) magic[t].option
209 #define compress_program(t) magic[t].program
211 /* Check if the file ARCHIVE is a compressed archive. */
213 check_compressed_archive ()
215 struct zip_magic
const *p
;
218 /* Prepare global data needed for find_next_block: */
219 record_end
= record_start
; /* set up for 1st record = # 0 */
220 sfr
= read_full_records
;
221 read_full_records
= true; /* Suppress fatal error on reading a partial
225 /* Restore global values */
226 read_full_records
= sfr
;
228 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
229 /* Probably a valid header */
232 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
233 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
239 /* Open an archive named archive_name_array[0]. Detect if it is
240 a compressed archive of known type and use corresponding decompression
243 open_compressed_archive ()
245 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
246 MODE_RW
, rsh_command_option
);
250 if (!multi_volume_option
)
252 enum compress_type type
= check_compressed_archive ();
257 /* FD is not needed any more */
260 hit_eof
= false; /* It might have been set by find_next_block in
261 check_compressed_archive */
263 /* Open compressed archive */
264 use_compress_program_option
= compress_program (type
);
265 child_pid
= sys_child_open_for_uncompress ();
266 read_full_records
= true;
270 record_end
= record_start
; /* set up for 1st record = # 0 */
277 print_total_written (void)
279 tarlong written
= prev_written
+ bytes_written
;
280 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
281 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
282 char rate
[LONGEST_HUMAN_READABLE
+ 1];
284 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
286 sprintf (bytes
, TARLONG_FORMAT
, written
);
288 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
289 fprintf (stderr
, _("Total bytes written: %s (%s, %s/s)\n"), bytes
,
290 human_readable (written
, abbr
, human_opts
, 1, 1),
291 (0 < duration
&& written
/ duration
< (uintmax_t) -1
292 ? human_readable (written
/ duration
, rate
, human_opts
, 1, 1)
296 /* Compute and return the block ordinal at current_block. */
298 current_block_ordinal (void)
300 return record_start_block
+ (current_block
- record_start
);
303 /* If the EOF flag is set, reset it, as well as current_block, etc. */
310 current_block
= record_start
;
311 record_end
= record_start
+ blocking_factor
;
312 access_mode
= ACCESS_WRITE
;
316 /* Return the location of the next available input or output block.
317 Return zero for EOF. Once we have returned zero, we just keep returning
318 it, to avoid accidentally going on to the next file on the tape. */
320 find_next_block (void)
322 if (current_block
== record_end
)
327 if (current_block
== record_end
)
333 return current_block
;
336 /* Indicate that we have used all blocks up thru BLOCK. */
338 set_next_block_after (union block
*block
)
340 while (block
>= current_block
)
343 /* Do *not* flush the archive here. If we do, the same argument to
344 set_next_block_after could mean the next block (if the input record
345 is exactly one block long), which is not what is intended. */
347 if (current_block
> record_end
)
351 /* Return the number of bytes comprising the space between POINTER
352 through the end of the current buffer of blocks. This space is
353 available for filling with data, or taking data from. POINTER is
354 usually (but not always) the result of previous find_next_block call. */
356 available_space_after (union block
*pointer
)
358 return record_end
->buffer
- pointer
->buffer
;
361 /* Close file having descriptor FD, and abort if close unsuccessful. */
366 close_error (_("(pipe)"));
372 if (!record_buffer
[record_index
])
373 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
375 record_start
= record_buffer
[record_index
];
376 current_block
= record_start
;
377 record_end
= record_start
+ blocking_factor
;
380 /* Open an archive file. The argument specifies whether we are
381 reading or writing, or both. */
383 _open_archive (enum access_mode wanted_access
)
385 int backed_up_flag
= 0;
389 stdlis
= freopen (index_file_name
, "w", stdout
);
391 open_error (index_file_name
);
392 close_stdout_set_file_name (index_file_name
);
395 stdlis
= to_stdout_option
? stderr
: stdout
;
397 if (record_size
== 0)
398 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
400 if (archive_names
== 0)
401 FATAL_ERROR ((0, 0, _("No archive name given")));
403 tar_stat_destroy (¤t_stat_info
);
410 /* When updating the archive, we start with reading. */
411 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
413 read_full_records
= read_full_records_option
;
417 if (use_compress_program_option
)
419 switch (wanted_access
)
422 child_pid
= sys_child_open_for_uncompress ();
423 read_full_records
= true;
424 record_end
= record_start
; /* set up for 1st record = # 0 */
428 child_pid
= sys_child_open_for_compress ();
432 abort (); /* Should not happen */
436 if (wanted_access
== ACCESS_WRITE
437 && strcmp (archive_name_array
[0], "-") == 0)
440 else if (strcmp (archive_name_array
[0], "-") == 0)
442 read_full_records
= true; /* could be a pipe, be safe */
444 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
446 switch (wanted_access
)
450 enum compress_type type
;
452 archive
= STDIN_FILENO
;
454 type
= check_compressed_archive (archive
);
457 _("Archive is compressed. Use %s option"),
458 compress_option (type
)));
463 archive
= STDOUT_FILENO
;
468 archive
= STDIN_FILENO
;
470 write_archive_to_stdout
= true;
474 else if (verify_option
)
475 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
476 MODE_RW
, rsh_command_option
);
478 switch (wanted_access
)
481 archive
= open_compressed_archive ();
487 maybe_backup_file (archive_name_array
[0], 1);
490 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
495 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
496 MODE_RW
, rsh_command_option
);
501 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
503 int saved_errno
= errno
;
508 open_fatal (archive_name_array
[0]);
511 sys_detect_dev_null_output ();
512 sys_save_archive_dev_ino ();
513 SET_BINARY_MODE (archive
);
515 switch (wanted_access
)
519 record_end
= record_start
; /* set up for 1st record = # 0 */
522 find_next_block (); /* read it in, check for EOF */
531 /* Perform a write to flush the buffer. */
537 if (checkpoint_option
&& !(++checkpoint
% 10))
538 /* TRANSLATORS: This is a ``checkpoint of write operation'',
539 *not* ``Writing a checkpoint''.
540 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
541 *not* ``Escribiendo un punto de comprobaci@'on'' */
542 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
544 if (tape_length_option
&& tape_length_option
<= bytes_written
)
549 else if (dev_null_output
)
550 status
= record_size
;
552 status
= sys_write_archive_buffer ();
557 /* Handle write errors on the archive. Write errors are always fatal.
558 Hitting the end of a volume does not cause a write error unless the
559 write was the first record of the volume. */
561 archive_write_error (ssize_t status
)
563 /* It might be useful to know how much was written before the error
568 print_total_written ();
572 write_fatal_details (*archive_name_cursor
, status
, record_size
);
575 /* Handle read errors on the archive. If the read should be retried,
576 return to the caller. */
578 archive_read_error (void)
580 read_error (*archive_name_cursor
);
582 if (record_start_block
== 0)
583 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
585 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
586 then give up on reading the archive. */
588 if (read_error_count
++ > READ_ERROR_MAX
)
589 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
594 short_read (size_t status
)
596 size_t left
; /* bytes left */
597 char *more
; /* pointer to next byte to read */
599 more
= record_start
->buffer
+ status
;
600 left
= record_size
- status
;
602 while (left
% BLOCKSIZE
!= 0
603 || (left
&& status
&& read_full_records
))
606 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
607 archive_read_error ();
612 if (! read_full_records
)
614 unsigned long rest
= record_size
- left
;
617 ngettext ("Unaligned block (%lu byte) in archive",
618 "Unaligned block (%lu bytes) in archive",
623 /* User warned us about this. Fix up. */
629 /* FIXME: for size=0, multi-volume support. On the first record, warn
630 about the problem. */
632 if (!read_full_records
&& verbose_option
> 1
633 && record_start_block
== 0 && status
!= 0)
635 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
637 ngettext ("Record size = %lu block",
638 "Record size = %lu blocks",
643 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
647 /* Perform a read to flush the buffer. */
651 size_t status
; /* result from system call */
653 if (checkpoint_option
&& !(++checkpoint
% 10))
654 /* TRANSLATORS: This is a ``checkpoint of read operation'',
655 *not* ``Reading a checkpoint''.
656 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
657 *not* ``Leyendo un punto de comprobaci@'on'' */
658 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
660 /* Clear the count of errors. This only applies to a single call to
663 read_error_count
= 0; /* clear error count */
665 if (write_archive_to_stdout
&& record_start_block
!= 0)
667 archive
= STDOUT_FILENO
;
668 status
= sys_write_archive_buffer ();
669 archive
= STDIN_FILENO
;
670 if (status
!= record_size
)
671 archive_write_error (status
);
674 status
= rmtread (archive
, record_start
->buffer
, record_size
);
675 if (status
== record_size
)
680 /* Flush the current buffer to/from the archive. */
684 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
685 record_start_block
+= record_end
- record_start
;
686 current_block
= record_start
;
687 record_end
= record_start
+ blocking_factor
;
689 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
691 access_mode
= ACCESS_WRITE
;
692 time_to_start_writing
= false;
703 flush_write_ptr (buffer_level
);
711 /* Backspace the archive descriptor by one record worth. If it's a
712 tape, MTIOCTOP will work. If it's something else, try to seek on
713 it. If we can't seek, we lose! */
715 backspace_output (void)
719 struct mtop operation
;
721 operation
.mt_op
= MTBSR
;
722 operation
.mt_count
= 1;
723 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
725 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
731 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
733 /* Seek back to the beginning of this record and start writing there. */
735 position
-= record_size
;
738 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
740 /* Lseek failed. Try a different method. */
743 _("Cannot backspace archive file; it may be unreadable without -i")));
745 /* Replace the first part of the record with NULs. */
747 if (record_start
->buffer
!= output_start
)
748 memset (record_start
->buffer
, 0,
749 output_start
- record_start
->buffer
);
755 seek_archive (off_t size
)
757 off_t start
= current_block_ordinal ();
760 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
762 size
-= skipped
* BLOCKSIZE
;
764 if (size
< record_size
)
768 /* Compute number of records to skip */
769 nrec
= size
/ record_size
;
770 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
774 if (offset
% record_size
)
775 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
777 /* Convert to number of records */
779 /* Compute number of skipped blocks */
780 nblk
= offset
- start
;
782 /* Update buffering info */
783 records_read
+= nblk
/ blocking_factor
;
784 record_start_block
= offset
- blocking_factor
;
785 current_block
= record_end
;
790 /* Close the archive file. */
794 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
797 if (current_block
> record_start
)
801 sys_drain_input_pipe ();
807 if (rmtclose (archive
) != 0)
808 close_warn (*archive_name_cursor
);
810 sys_wait_for_child (child_pid
);
812 tar_stat_destroy (¤t_stat_info
);
817 free (record_buffer
[0]);
818 free (record_buffer
[1]);
821 /* Called to initialize the global volume number. */
823 init_volume_number (void)
825 FILE *file
= fopen (volno_file_option
, "r");
829 if (fscanf (file
, "%d", &global_volno
) != 1
831 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
832 quotearg_colon (volno_file_option
)));
834 read_error (volno_file_option
);
835 if (fclose (file
) != 0)
836 close_error (volno_file_option
);
838 else if (errno
!= ENOENT
)
839 open_error (volno_file_option
);
842 /* Called to write out the closing global volume number. */
844 closeout_volume_number (void)
846 FILE *file
= fopen (volno_file_option
, "w");
850 fprintf (file
, "%d\n", global_volno
);
852 write_error (volno_file_option
);
853 if (fclose (file
) != 0)
854 close_error (volno_file_option
);
857 open_error (volno_file_option
);
862 increase_volume_number ()
865 if (global_volno
< 0)
866 FATAL_ERROR ((0, 0, _("Volume number overflow")));
871 change_tape_menu (FILE *read_file
)
873 char *input_buffer
= NULL
;
878 fputc ('\007', stderr
);
880 _("Prepare volume #%d for %s and hit return: "),
881 global_volno
+ 1, quote (*archive_name_cursor
));
884 if (getline (&input_buffer
, &size
, read_file
) <= 0)
886 WARN ((0, 0, _("EOF where user reply was expected")));
888 if (subcommand_option
!= EXTRACT_SUBCOMMAND
889 && subcommand_option
!= LIST_SUBCOMMAND
890 && subcommand_option
!= DIFF_SUBCOMMAND
)
891 WARN ((0, 0, _("WARNING: Archive is incomplete")));
896 if (input_buffer
[0] == '\n'
897 || input_buffer
[0] == 'y'
898 || input_buffer
[0] == 'Y')
901 switch (input_buffer
[0])
905 fprintf (stderr
, _("\
906 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
908 y or newline Continue operation\n"));
909 if (!restrict_option
)
910 fprintf (stderr
, _(" ! Spawn a subshell\n"));
911 fprintf (stderr
, _(" ? Print this list\n"));
918 WARN ((0, 0, _("No new volume; exiting.\n")));
920 if (subcommand_option
!= EXTRACT_SUBCOMMAND
921 && subcommand_option
!= LIST_SUBCOMMAND
922 && subcommand_option
!= DIFF_SUBCOMMAND
)
923 WARN ((0, 0, _("WARNING: Archive is incomplete")));
928 /* Get new file name. */
934 for (name
= input_buffer
+ 1;
935 *name
== ' ' || *name
== '\t';
939 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
943 /* FIXME: the following allocation is never reclaimed. */
944 *archive_name_cursor
= xstrdup (name
);
949 if (!restrict_option
)
957 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
963 /* We've hit the end of the old volume. Close it and open the next one.
964 Return nonzero on success.
967 new_volume (enum access_mode mode
)
969 static FILE *read_file
;
973 if (!read_file
&& !info_script_option
)
974 /* FIXME: if fopen is used, it will never be closed. */
975 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
982 assign_string (&volume_label
, NULL
);
983 assign_string (&continued_file_name
, NULL
);
984 continued_file_size
= continued_file_offset
= 0;
986 if (rmtclose (archive
) != 0)
987 close_warn (*archive_name_cursor
);
989 archive_name_cursor
++;
990 if (archive_name_cursor
== archive_name_array
+ archive_names
)
992 archive_name_cursor
= archive_name_array
;
1000 /* We have to prompt from now on. */
1002 if (info_script_option
)
1004 if (volno_file_option
)
1005 closeout_volume_number ();
1006 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1007 FATAL_ERROR ((0, 0, _("%s command failed"),
1008 quote (info_script_option
)));
1011 change_tape_menu (read_file
);
1014 if (strcmp (archive_name_cursor
[0], "-") == 0)
1016 read_full_records
= true;
1017 archive
= STDIN_FILENO
;
1019 else if (verify_option
)
1020 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1021 rsh_command_option
);
1026 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1027 rsh_command_option
);
1032 maybe_backup_file (*archive_name_cursor
, 1);
1033 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1034 rsh_command_option
);
1038 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1039 rsh_command_option
);
1045 open_warn (*archive_name_cursor
);
1046 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1047 undo_last_backup ();
1052 SET_BINARY_MODE (archive
);
1060 enum read_header rc
= read_header (false);
1062 if (rc
== HEADER_SUCCESS
)
1064 set_next_block_after (current_header
);
1067 ERROR ((0, 0, _("This does not look like a tar archive")));
1075 union block
*header
;
1077 switch (subcommand_option
)
1079 case APPEND_SUBCOMMAND
:
1080 case CAT_SUBCOMMAND
:
1081 case UPDATE_SUBCOMMAND
:
1082 if (!new_volume (ACCESS_UPDATE
))
1087 if (!new_volume (ACCESS_READ
))
1092 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1094 archive_read_error ();
1096 if (status
!= record_size
)
1097 short_read (status
);
1099 header
= find_next_block ();
1102 switch (header
->header
.typeflag
)
1106 struct tar_stat_info dummy
;
1107 if (!read_header0 ())
1109 tar_stat_init (&dummy
);
1110 xheader_decode (&dummy
); /* decodes values from the global header */
1111 tar_stat_destroy (&dummy
);
1114 /* We have read the extended header of the first member in
1115 this volume. Put it back, so next read_header works as
1117 current_block
= record_start
;
1122 case GNUTYPE_VOLHDR
:
1123 if (!read_header0 ())
1125 assign_string (&volume_label
, current_header
->header
.name
);
1126 set_next_block_after (header
);
1127 header
= find_next_block ();
1128 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1132 case GNUTYPE_MULTIVOL
:
1133 if (!read_header0 ())
1135 assign_string (&continued_file_name
, current_header
->header
.name
);
1136 continued_file_size
=
1137 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1138 continued_file_offset
=
1139 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1149 if (!continued_file_name
1150 || strcmp (continued_file_name
, real_s_name
))
1152 WARN ((0, 0, _("%s is not continued on this volume"),
1153 quote (real_s_name
)));
1157 s
= continued_file_size
+ continued_file_offset
;
1159 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1161 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1162 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1163 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1165 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1166 quote (continued_file_name
),
1167 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1168 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1169 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1173 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1175 WARN ((0, 0, _("This volume is out of sequence")));
1180 increase_volume_number ();
1185 /* Check the LABEL block against the volume label, seen as a globbing
1186 pattern. Return true if the pattern matches. In case of failure,
1187 retry matching a volume sequence number before giving up in
1188 multi-volume mode. */
1190 check_label_pattern (union block
*label
)
1195 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1198 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1201 if (!multi_volume_option
)
1204 string
= xmalloc (strlen (volume_label_option
)
1205 + sizeof VOLUME_LABEL_APPEND
+ 1);
1206 strcpy (string
, volume_label_option
);
1207 strcat (string
, VOLUME_LABEL_APPEND
);
1208 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1213 /* Check if the next block contains a volume label and if this matches
1214 the one given in the command line */
1216 match_volume_label (void)
1218 union block
*label
= find_next_block ();
1221 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1222 quote (volume_label_option
)));
1223 if (!check_label_pattern (label
))
1224 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1225 quote_n (0, label
->header
.name
),
1226 quote_n (1, volume_label_option
)));
1229 /* Mark the archive with volume label STR. */
1231 _write_volume_label (const char *str
)
1233 if (archive_format
== POSIX_FORMAT
)
1234 xheader_store ("GNU.volume.label", NULL
, str
);
1237 union block
*label
= find_next_block ();
1239 memset (label
, 0, BLOCKSIZE
);
1241 strcpy (label
->header
.name
, volume_label_option
);
1242 assign_string (¤t_stat_info
.file_name
,
1243 label
->header
.name
);
1244 current_stat_info
.had_trailing_slash
=
1245 strip_trailing_slashes (current_stat_info
.file_name
);
1247 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1248 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1249 finish_header (¤t_stat_info
, label
, -1);
1250 set_next_block_after (label
);
1254 #define VOL_SUFFIX "Volume"
1256 /* Add a volume label to a part of multi-volume archive */
1258 add_volume_label (void)
1260 char buf
[UINTMAX_STRSIZE_BOUND
];
1261 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1262 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1264 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1265 _write_volume_label (s
);
1272 if (archive_format
== POSIX_FORMAT
)
1274 off_t block_ordinal
;
1276 struct tar_stat_info st
;
1277 static size_t real_s_part_no
; /* FIXME */
1280 memset (&st
, 0, sizeof st
);
1281 st
.orig_file_name
= st
.file_name
= real_s_name
;
1282 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1283 st
.stat
.st_uid
= getuid ();
1284 st
.stat
.st_gid
= getgid ();
1285 st
.orig_file_name
= xheader_format_name (&st
,
1286 "%d/GNUFileParts.%p/%f.%n",
1288 st
.file_name
= st
.orig_file_name
;
1289 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1291 block_ordinal
= current_block_ordinal ();
1292 blk
= start_header (&st
);
1294 abort (); /* FIXME */
1295 finish_header (&st
, blk
, block_ordinal
);
1296 free (st
.orig_file_name
);
1301 /* Add a volume label to the current archive */
1303 write_volume_label (void)
1305 if (multi_volume_option
)
1306 add_volume_label ();
1308 _write_volume_label (volume_label_option
);
1311 /* Write GNU multi-volume header */
1313 gnu_add_multi_volume_header (void)
1316 union block
*block
= find_next_block ();
1318 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1320 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1321 quotearg_colon (real_s_name
)));
1323 memset (block
, 0, BLOCKSIZE
);
1325 /* FIXME: Michael P Urban writes: [a long name file] is being written
1326 when a new volume rolls around [...] Looks like the wrong value is
1327 being preserved in real_s_name, though. */
1329 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1330 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1332 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1333 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1334 block
->oldgnu_header
.offset
);
1336 tmp
= verbose_option
;
1338 finish_header (¤t_stat_info
, block
, -1);
1339 verbose_option
= tmp
;
1340 set_next_block_after (block
);
1343 /* Add a multi volume header to the current archive. The exact header format
1344 depends on the archive format. */
1346 add_multi_volume_header (void)
1348 if (archive_format
== POSIX_FORMAT
)
1350 off_t d
= real_s_totsize
- real_s_sizeleft
;
1351 xheader_store ("GNU.volume.filename", NULL
, real_s_name
);
1352 xheader_store ("GNU.volume.size", NULL
, &real_s_sizeleft
);
1353 xheader_store ("GNU.volume.offset", NULL
, &d
);
1356 gnu_add_multi_volume_header ();
1359 /* Synchronize multi-volume globals */
1361 multi_volume_sync ()
1363 if (multi_volume_option
)
1367 assign_string (&real_s_name
,
1368 safer_name_suffix (save_name
, false,
1369 absolute_names_option
));
1370 real_s_totsize
= save_totsize
;
1371 real_s_sizeleft
= save_sizeleft
;
1375 assign_string (&real_s_name
, 0);
1377 real_s_sizeleft
= 0;
1383 /* Low-level flush functions */
1385 /* Simple flush read (no multi-volume or label extensions) */
1387 simple_flush_read (void)
1389 size_t status
; /* result from system call */
1391 if (checkpoint_option
&& !(++checkpoint
% 10))
1392 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1393 *not* ``Reading a checkpoint''.
1394 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1395 *not* ``Leyendo un punto de comprobaci@'on'' */
1396 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1398 /* Clear the count of errors. This only applies to a single call to
1401 read_error_count
= 0; /* clear error count */
1403 if (write_archive_to_stdout
&& record_start_block
!= 0)
1405 archive
= STDOUT_FILENO
;
1406 status
= sys_write_archive_buffer ();
1407 archive
= STDIN_FILENO
;
1408 if (status
!= record_size
)
1409 archive_write_error (status
);
1414 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1415 if (status
== record_size
)
1420 if (status
== SAFE_READ_ERROR
)
1422 archive_read_error ();
1423 continue; /* try again */
1427 short_read (status
);
1430 /* Simple flush write (no multi-volume or label extensions) */
1432 simple_flush_write (size_t level
__attribute__((unused
)))
1436 status
= _flush_write ();
1437 if (status
!= record_size
)
1438 archive_write_error (status
);
1442 bytes_written
+= status
;
1447 /* GNU flush functions. These support multi-volume and archive labels in
1448 GNU and PAX archive formats. */
1451 _gnu_flush_read (void)
1453 size_t status
; /* result from system call */
1455 if (checkpoint_option
&& !(++checkpoint
% 10))
1456 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1457 *not* ``Reading a checkpoint''.
1458 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1459 *not* ``Leyendo un punto de comprobaci@'on'' */
1460 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1462 /* Clear the count of errors. This only applies to a single call to
1465 read_error_count
= 0; /* clear error count */
1467 if (write_archive_to_stdout
&& record_start_block
!= 0)
1469 archive
= STDOUT_FILENO
;
1470 status
= sys_write_archive_buffer ();
1471 archive
= STDIN_FILENO
;
1472 if (status
!= record_size
)
1473 archive_write_error (status
);
1476 multi_volume_sync ();
1480 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1481 if (status
== record_size
)
1487 /* The condition below used to include
1488 || (status > 0 && !read_full_records)
1489 This is incorrect since even if new_volume() succeeds, the
1490 subsequent call to rmtread will overwrite the chunk of data
1491 already read in the buffer, so the processing will fail */
1493 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1494 && multi_volume_option
)
1496 while (!try_new_volume ())
1500 else if (status
== SAFE_READ_ERROR
)
1502 archive_read_error ();
1507 short_read (status
);
1511 gnu_flush_read (void)
1513 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1515 flush_read_ptr
= gnu_flush_read
;
1519 _gnu_flush_write (size_t buffer_level
)
1522 union block
*header
;
1527 status
= _flush_write ();
1528 if (status
!= record_size
&& !multi_volume_option
)
1529 archive_write_error (status
);
1533 bytes_written
+= status
;
1536 if (status
== record_size
)
1538 multi_volume_sync ();
1542 /* In multi-volume mode. */
1543 /* ENXIO is for the UNIX PC. */
1544 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1545 archive_write_error (status
);
1547 if (!new_volume (ACCESS_WRITE
))
1550 xheader_destroy (&extended_header
);
1552 increase_volume_number ();
1554 prev_written
+= bytes_written
;
1557 copy_ptr
= record_start
->buffer
+ status
;
1558 copy_size
= buffer_level
- status
;
1559 /* Switch to the next buffer */
1560 record_index
= !record_index
;
1563 if (volume_label_option
)
1564 add_volume_label ();
1567 add_multi_volume_header ();
1569 write_extended (true, NULL
, find_next_block ());
1571 add_chunk_header ();
1572 header
= find_next_block ();
1573 bufsize
= available_space_after (header
);
1574 while (bufsize
< copy_size
)
1576 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1577 copy_ptr
+= bufsize
;
1578 copy_size
-= bufsize
;
1579 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1580 header
= find_next_block ();
1581 bufsize
= available_space_after (header
);
1583 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1584 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1585 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1590 gnu_flush_write (size_t buffer_level
)
1592 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1593 _gnu_flush_write (buffer_level
);
1594 flush_write_ptr
= gnu_flush_write
;
1606 flush_write_ptr (record_size
);
1610 open_archive (enum access_mode wanted_access
)
1612 flush_read_ptr
= gnu_flush_read
;
1613 flush_write_ptr
= gnu_flush_write
;
1615 _open_archive (wanted_access
);
1616 switch (wanted_access
)
1619 if (volume_label_option
)
1620 match_volume_label ();
1624 records_written
= 0;
1625 if (volume_label_option
)
1626 write_volume_label ();