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")));
866 /* We've hit the end of the old volume. Close it and open the next one.
867 Return nonzero on success.
870 new_volume (enum access_mode mode
)
872 static FILE *read_file
;
875 if (!read_file
&& !info_script_option
)
876 /* FIXME: if fopen is used, it will never be closed. */
877 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
884 assign_string (&volume_label
, NULL
);
885 assign_string (&continued_file_name
, NULL
);
886 continued_file_size
= continued_file_offset
= 0;
888 if (rmtclose (archive
) != 0)
889 close_warn (*archive_name_cursor
);
891 archive_name_cursor
++;
892 if (archive_name_cursor
== archive_name_array
+ archive_names
)
894 archive_name_cursor
= archive_name_array
;
901 /* We have to prompt from now on. */
903 if (info_script_option
)
905 if (volno_file_option
)
906 closeout_volume_number ();
907 if (system (info_script_option
) != 0)
908 FATAL_ERROR ((0, 0, _("%s command failed"),
909 quote (info_script_option
)));
914 char input_buffer
[80];
916 fputc ('\007', stderr
);
918 _("Prepare volume #%d for %s and hit return: "),
919 global_volno
+ 1, quote (*archive_name_cursor
));
922 if (fgets (input_buffer
, sizeof input_buffer
, read_file
) == 0)
924 WARN ((0, 0, _("EOF where user reply was expected")));
926 if (subcommand_option
!= EXTRACT_SUBCOMMAND
927 && subcommand_option
!= LIST_SUBCOMMAND
928 && subcommand_option
!= DIFF_SUBCOMMAND
)
929 WARN ((0, 0, _("WARNING: Archive is incomplete")));
933 if (input_buffer
[0] == '\n'
934 || input_buffer
[0] == 'y'
935 || input_buffer
[0] == 'Y')
938 switch (input_buffer
[0])
942 /* FIXME: Might it be useful to disable the '!' command? */
943 fprintf (stderr
, _("\
944 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
946 ! Spawn a subshell\n\
947 ? Print this list\n"));
954 WARN ((0, 0, _("No new volume; exiting.\n")));
956 if (subcommand_option
!= EXTRACT_SUBCOMMAND
957 && subcommand_option
!= LIST_SUBCOMMAND
958 && subcommand_option
!= DIFF_SUBCOMMAND
)
959 WARN ((0, 0, _("WARNING: Archive is incomplete")));
964 /* Get new file name. */
967 char *name
= &input_buffer
[1];
970 for (name
= input_buffer
+ 1;
971 *name
== ' ' || *name
== '\t';
975 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
979 /* FIXME: the following allocation is never reclaimed. */
980 *archive_name_cursor
= xstrdup (name
);
991 if (strcmp (archive_name_cursor
[0], "-") == 0)
993 read_full_records
= true;
994 archive
= STDIN_FILENO
;
996 else if (verify_option
)
997 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1003 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1004 rsh_command_option
);
1009 maybe_backup_file (*archive_name_cursor
, 1);
1010 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1011 rsh_command_option
);
1015 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1016 rsh_command_option
);
1022 open_warn (*archive_name_cursor
);
1023 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1024 undo_last_backup ();
1028 SET_BINARY_MODE (archive
);
1036 enum read_header rc
= read_header (false);
1038 if (rc
== HEADER_SUCCESS
)
1040 set_next_block_after (current_header
);
1043 ERROR ((0, 0, _("This does not look like a tar archive")));
1051 union block
*header
;
1053 switch (subcommand_option
)
1055 case APPEND_SUBCOMMAND
:
1056 case CAT_SUBCOMMAND
:
1057 case UPDATE_SUBCOMMAND
:
1058 if (!new_volume (ACCESS_UPDATE
))
1063 if (!new_volume (ACCESS_READ
))
1068 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1070 archive_read_error ();
1072 if (status
!= record_size
)
1073 short_read (status
);
1075 header
= find_next_block ();
1078 switch (header
->header
.typeflag
)
1082 struct tar_stat_info dummy
;
1083 if (!read_header0 ())
1085 tar_stat_init (&dummy
);
1086 xheader_decode (&dummy
); /* decodes values from the global header */
1087 tar_stat_destroy (&dummy
);
1090 /* We have read the extended header of the first member in
1091 this volume. Put it back, so next read_header works as
1093 current_block
= record_start
;
1098 case GNUTYPE_VOLHDR
:
1099 if (!read_header0 ())
1101 assign_string (&volume_label
, current_header
->header
.name
);
1102 set_next_block_after (header
);
1103 header
= find_next_block ();
1104 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1108 case GNUTYPE_MULTIVOL
:
1109 if (!read_header0 ())
1111 assign_string (&continued_file_name
, current_header
->header
.name
);
1112 continued_file_size
=
1113 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1114 continued_file_offset
=
1115 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1125 if (!continued_file_name
1126 || strcmp (continued_file_name
, real_s_name
))
1128 WARN ((0, 0, _("%s is not continued on this volume"),
1129 quote (real_s_name
)));
1133 s
= continued_file_size
+ continued_file_offset
;
1135 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1137 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1138 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1139 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1141 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1142 quote (continued_file_name
),
1143 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1144 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1145 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1149 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1151 WARN ((0, 0, _("This volume is out of sequence")));
1156 increase_volume_number ();
1161 /* Check the LABEL block against the volume label, seen as a globbing
1162 pattern. Return true if the pattern matches. In case of failure,
1163 retry matching a volume sequence number before giving up in
1164 multi-volume mode. */
1166 check_label_pattern (union block
*label
)
1171 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1174 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1177 if (!multi_volume_option
)
1180 string
= xmalloc (strlen (volume_label_option
)
1181 + sizeof VOLUME_LABEL_APPEND
+ 1);
1182 strcpy (string
, volume_label_option
);
1183 strcat (string
, VOLUME_LABEL_APPEND
);
1184 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1189 /* Check if the next block contains a volume label and if this matches
1190 the one given in the command line */
1192 match_volume_label (void)
1194 union block
*label
= find_next_block ();
1197 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1198 quote (volume_label_option
)));
1199 if (!check_label_pattern (label
))
1200 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1201 quote_n (0, label
->header
.name
),
1202 quote_n (1, volume_label_option
)));
1205 /* Mark the archive with volume label STR. */
1207 _write_volume_label (const char *str
)
1209 if (archive_format
== POSIX_FORMAT
)
1210 xheader_store ("GNU.volume.label", NULL
, str
);
1213 union block
*label
= find_next_block ();
1215 memset (label
, 0, BLOCKSIZE
);
1217 strcpy (label
->header
.name
, volume_label_option
);
1218 assign_string (¤t_stat_info
.file_name
,
1219 label
->header
.name
);
1220 current_stat_info
.had_trailing_slash
=
1221 strip_trailing_slashes (current_stat_info
.file_name
);
1223 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1224 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1225 finish_header (¤t_stat_info
, label
, -1);
1226 set_next_block_after (label
);
1230 #define VOL_SUFFIX "Volume"
1232 /* Add a volume label to a part of multi-volume archive */
1234 add_volume_label (void)
1236 char buf
[UINTMAX_STRSIZE_BOUND
];
1237 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1238 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1240 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1241 _write_volume_label (s
);
1248 if (archive_format
== POSIX_FORMAT
)
1250 off_t block_ordinal
;
1252 struct tar_stat_info st
;
1253 static size_t real_s_part_no
; /* FIXME */
1256 memset (&st
, 0, sizeof st
);
1257 st
.orig_file_name
= st
.file_name
= real_s_name
;
1258 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1259 st
.stat
.st_uid
= getuid ();
1260 st
.stat
.st_gid
= getgid ();
1261 st
.orig_file_name
= xheader_format_name (&st
,
1262 "%d/GNUFileParts.%p/%f.%n",
1264 st
.file_name
= st
.orig_file_name
;
1265 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1267 block_ordinal
= current_block_ordinal ();
1268 blk
= start_header (&st
);
1269 free (st
.orig_file_name
);
1271 abort (); /* FIXME */
1272 finish_header (&st
, blk
, block_ordinal
);
1277 /* Add a volume label to the current archive */
1279 write_volume_label (void)
1281 if (multi_volume_option
)
1282 add_volume_label ();
1284 _write_volume_label (volume_label_option
);
1287 /* Write GNU multi-volume header */
1289 gnu_add_multi_volume_header (void)
1292 union block
*block
= find_next_block ();
1294 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1296 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1297 quotearg_colon (real_s_name
)));
1299 memset (block
, 0, BLOCKSIZE
);
1301 /* FIXME: Michael P Urban writes: [a long name file] is being written
1302 when a new volume rolls around [...] Looks like the wrong value is
1303 being preserved in real_s_name, though. */
1305 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1306 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1308 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1309 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1310 block
->oldgnu_header
.offset
);
1312 tmp
= verbose_option
;
1314 finish_header (¤t_stat_info
, block
, -1);
1315 verbose_option
= tmp
;
1316 set_next_block_after (block
);
1319 /* Add a multi volume header to the current archive. The exact header format
1320 depends on the archive format. */
1322 add_multi_volume_header (void)
1324 if (archive_format
== POSIX_FORMAT
)
1326 off_t d
= real_s_totsize
- real_s_sizeleft
;
1327 xheader_store ("GNU.volume.filename", NULL
, real_s_name
);
1328 xheader_store ("GNU.volume.size", NULL
, &real_s_sizeleft
);
1329 xheader_store ("GNU.volume.offset", NULL
, &d
);
1332 gnu_add_multi_volume_header ();
1335 /* Synchronize multi-volume globals */
1337 multi_volume_sync ()
1339 if (multi_volume_option
)
1343 assign_string (&real_s_name
,
1344 safer_name_suffix (save_name
, false,
1345 absolute_names_option
));
1346 real_s_totsize
= save_totsize
;
1347 real_s_sizeleft
= save_sizeleft
;
1351 assign_string (&real_s_name
, 0);
1353 real_s_sizeleft
= 0;
1359 /* Low-level flush functions */
1361 /* Simple flush read (no multi-volume or label extensions) */
1363 simple_flush_read (void)
1365 size_t status
; /* result from system call */
1367 if (checkpoint_option
&& !(++checkpoint
% 10))
1368 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1369 *not* ``Reading a checkpoint''.
1370 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1371 *not* ``Leyendo un punto de comprobaci@'on'' */
1372 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1374 /* Clear the count of errors. This only applies to a single call to
1377 read_error_count
= 0; /* clear error count */
1379 if (write_archive_to_stdout
&& record_start_block
!= 0)
1381 archive
= STDOUT_FILENO
;
1382 status
= sys_write_archive_buffer ();
1383 archive
= STDIN_FILENO
;
1384 if (status
!= record_size
)
1385 archive_write_error (status
);
1390 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1391 if (status
== record_size
)
1396 if (status
== SAFE_READ_ERROR
)
1398 archive_read_error ();
1399 continue; /* try again */
1403 short_read (status
);
1406 /* Simple flush write (no multi-volume or label extensions) */
1408 simple_flush_write (size_t level
__attribute__((unused
)))
1412 status
= _flush_write ();
1413 if (status
!= record_size
)
1414 archive_write_error (status
);
1418 bytes_written
+= status
;
1423 /* GNU flush functions. These support multi-volume and archive labels in
1424 GNU and PAX archive formats. */
1427 _gnu_flush_read (void)
1429 size_t status
; /* result from system call */
1431 if (checkpoint_option
&& !(++checkpoint
% 10))
1432 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1433 *not* ``Reading a checkpoint''.
1434 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1435 *not* ``Leyendo un punto de comprobaci@'on'' */
1436 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1438 /* Clear the count of errors. This only applies to a single call to
1441 read_error_count
= 0; /* clear error count */
1443 if (write_archive_to_stdout
&& record_start_block
!= 0)
1445 archive
= STDOUT_FILENO
;
1446 status
= sys_write_archive_buffer ();
1447 archive
= STDIN_FILENO
;
1448 if (status
!= record_size
)
1449 archive_write_error (status
);
1452 multi_volume_sync ();
1456 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1457 if (status
== record_size
)
1463 /* The condition below used to include
1464 || (status > 0 && !read_full_records)
1465 This is incorrect since even if new_volume() succeeds, the
1466 subsequent call to rmtread will overwrite the chunk of data
1467 already read in the buffer, so the processing will fail */
1469 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1470 && multi_volume_option
)
1472 while (!try_new_volume ())
1476 else if (status
== SAFE_READ_ERROR
)
1478 archive_read_error ();
1483 short_read (status
);
1487 gnu_flush_read (void)
1489 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1491 flush_read_ptr
= gnu_flush_read
;
1495 _gnu_flush_write (size_t buffer_level
)
1498 union block
*header
;
1503 status
= _flush_write ();
1504 if (status
!= record_size
&& !multi_volume_option
)
1505 archive_write_error (status
);
1509 bytes_written
+= status
;
1512 if (status
== record_size
)
1514 multi_volume_sync ();
1518 /* In multi-volume mode. */
1519 /* ENXIO is for the UNIX PC. */
1520 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1521 archive_write_error (status
);
1523 if (!new_volume (ACCESS_WRITE
))
1526 xheader_destroy (&extended_header
);
1528 increase_volume_number ();
1530 prev_written
+= bytes_written
;
1533 copy_ptr
= record_start
->buffer
+ status
;
1534 copy_size
= buffer_level
- status
;
1535 /* Switch to the next buffer */
1536 record_index
= !record_index
;
1539 if (volume_label_option
)
1540 add_volume_label ();
1543 add_multi_volume_header ();
1545 write_extended (true, NULL
, find_next_block ());
1547 add_chunk_header ();
1548 header
= find_next_block ();
1549 bufsize
= available_space_after (header
);
1550 while (bufsize
< copy_size
)
1552 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1553 copy_ptr
+= bufsize
;
1554 copy_size
-= bufsize
;
1555 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1556 header
= find_next_block ();
1557 bufsize
= available_space_after (header
);
1559 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1560 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1561 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1566 gnu_flush_write (size_t buffer_level
)
1568 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1569 _gnu_flush_write (buffer_level
);
1570 flush_write_ptr
= gnu_flush_write
;
1582 flush_write_ptr (record_size
);
1586 open_archive (enum access_mode wanted_access
)
1588 flush_read_ptr
= gnu_flush_read
;
1589 flush_write_ptr
= gnu_flush_write
;
1591 _open_archive (wanted_access
);
1592 switch (wanted_access
)
1595 if (volume_label_option
)
1596 match_volume_label ();
1600 records_written
= 0;
1601 if (volume_label_option
)
1602 write_volume_label ();