1 /* Buffer management for tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-08-25.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include <system-ioctl.h>
36 /* Number of retries before giving up on read. */
37 #define READ_ERROR_MAX 10
39 /* Globbing pattern to append to volume label if initial match failed. */
40 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
44 static tarlong prev_written
; /* bytes written on previous volumes */
45 static tarlong bytes_written
; /* bytes written on this volume */
46 static void *record_buffer
[2]; /* allocated memory */
47 union block
*record_buffer_aligned
[2];
48 static int record_index
;
50 /* FIXME: The following variables should ideally be static to this
51 module. However, this cannot be done yet. The cleanup continues! */
53 union block
*record_start
; /* start of record of archive */
54 union block
*record_end
; /* last+1 block of archive record */
55 union block
*current_block
; /* current block of archive */
56 enum access_mode access_mode
; /* how do we handle the archive */
57 off_t records_read
; /* number of records read from this archive */
58 off_t records_written
; /* likewise, for records written */
60 static off_t record_start_block
; /* block ordinal at record_start */
62 /* Where we write list messages (not errors, not interactions) to. */
65 static void backspace_output (void);
67 /* PID of child program, if compress_option or remote archive access. */
68 static pid_t child_pid
;
70 /* Error recovery stuff */
71 static int read_error_count
;
73 /* Have we hit EOF yet? */
76 /* Checkpointing counter */
77 static int checkpoint
;
79 static bool read_full_records
= false;
81 /* We're reading, but we just read the last block and it's time to update.
84 As least EXTERN like this one as possible. (?? --gray)
85 FIXME: Either eliminate it or move it to common.h.
87 extern bool time_to_start_writing
;
89 bool write_archive_to_stdout
;
91 void (*flush_write_ptr
) (size_t);
92 void (*flush_read_ptr
) (void);
96 char *continued_file_name
;
97 uintmax_t continued_file_size
;
98 uintmax_t continued_file_offset
;
101 static int volno
= 1; /* which volume of a multi-volume tape we're
103 static int global_volno
= 1; /* volume number to print in external
106 bool write_archive_to_stdout
;
108 /* Used by flush_read and flush_write to store the real info about saved
110 static char *real_s_name
;
111 static off_t real_s_totsize
;
112 static off_t real_s_sizeleft
;
115 /* Multi-volume tracking support */
116 static char *save_name
; /* name of the file we are currently writing */
117 static off_t save_totsize
; /* total size of file we are writing, only
118 valid if save_name is nonzero */
119 static off_t save_sizeleft
; /* where we are in the file we are writing,
120 only valid if save_name is nonzero */
123 mv_begin (struct tar_stat_info
*st
)
125 if (multi_volume_option
)
127 assign_string (&save_name
, st
->orig_file_name
);
128 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
135 if (multi_volume_option
)
136 assign_string (&save_name
, 0);
140 mv_total_size (off_t size
)
146 mv_size_left (off_t size
)
148 save_sizeleft
= size
;
155 clear_read_error_count (void)
157 read_error_count
= 0;
161 /* Time-related functions */
168 gettime (&start_time
);
176 duration
+= ((now
.tv_sec
- start_time
.tv_sec
)
177 + (now
.tv_nsec
- start_time
.tv_nsec
) / 1e9
);
182 /* Compression detection */
193 enum compress_type type
;
200 static struct zip_magic
const magic
[] = {
202 { ct_compress
, 2, "\037\235", "compress", "-Z" },
203 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
204 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
207 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
209 #define compress_option(t) magic[t].option
210 #define compress_program(t) magic[t].program
212 /* Check if the file ARCHIVE is a compressed archive. */
214 check_compressed_archive ()
216 struct zip_magic
const *p
;
219 /* Prepare global data needed for find_next_block: */
220 record_end
= record_start
; /* set up for 1st record = # 0 */
221 sfr
= read_full_records
;
222 read_full_records
= true; /* Suppress fatal error on reading a partial
226 /* Restore global values */
227 read_full_records
= sfr
;
229 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
230 /* Probably a valid header */
233 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
234 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
240 /* Open an archive named archive_name_array[0]. Detect if it is
241 a compressed archive of known type and use corresponding decompression
244 open_compressed_archive ()
246 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
247 MODE_RW
, rsh_command_option
);
251 if (!multi_volume_option
)
253 enum compress_type type
= check_compressed_archive ();
258 /* FD is not needed any more */
261 hit_eof
= false; /* It might have been set by find_next_block in
262 check_compressed_archive */
264 /* Open compressed archive */
265 use_compress_program_option
= compress_program (type
);
266 child_pid
= sys_child_open_for_uncompress ();
267 read_full_records
= true;
271 record_end
= record_start
; /* set up for 1st record = # 0 */
278 print_total_written (void)
280 tarlong written
= prev_written
+ bytes_written
;
281 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
282 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
283 char rate
[LONGEST_HUMAN_READABLE
+ 1];
285 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
287 sprintf (bytes
, TARLONG_FORMAT
, written
);
289 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
290 fprintf (stderr
, _("Total bytes written: %s (%s, %s/s)\n"), bytes
,
291 human_readable (written
, abbr
, human_opts
, 1, 1),
292 (0 < duration
&& written
/ duration
< (uintmax_t) -1
293 ? human_readable (written
/ duration
, rate
, human_opts
, 1, 1)
297 /* Compute and return the block ordinal at current_block. */
299 current_block_ordinal (void)
301 return record_start_block
+ (current_block
- record_start
);
304 /* If the EOF flag is set, reset it, as well as current_block, etc. */
311 current_block
= record_start
;
312 record_end
= record_start
+ blocking_factor
;
313 access_mode
= ACCESS_WRITE
;
317 /* Return the location of the next available input or output block.
318 Return zero for EOF. Once we have returned zero, we just keep returning
319 it, to avoid accidentally going on to the next file on the tape. */
321 find_next_block (void)
323 if (current_block
== record_end
)
328 if (current_block
== record_end
)
334 return current_block
;
337 /* Indicate that we have used all blocks up thru BLOCK. */
339 set_next_block_after (union block
*block
)
341 while (block
>= current_block
)
344 /* Do *not* flush the archive here. If we do, the same argument to
345 set_next_block_after could mean the next block (if the input record
346 is exactly one block long), which is not what is intended. */
348 if (current_block
> record_end
)
352 /* Return the number of bytes comprising the space between POINTER
353 through the end of the current buffer of blocks. This space is
354 available for filling with data, or taking data from. POINTER is
355 usually (but not always) the result of previous find_next_block call. */
357 available_space_after (union block
*pointer
)
359 return record_end
->buffer
- pointer
->buffer
;
362 /* Close file having descriptor FD, and abort if close unsuccessful. */
367 close_error (_("(pipe)"));
373 if (! record_buffer_aligned
[record_index
])
374 record_buffer_aligned
[record_index
] =
375 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
377 record_start
= record_buffer_aligned
[record_index
];
378 current_block
= record_start
;
379 record_end
= record_start
+ blocking_factor
;
382 /* Open an archive file. The argument specifies whether we are
383 reading or writing, or both. */
385 _open_archive (enum access_mode wanted_access
)
387 int backed_up_flag
= 0;
391 stdlis
= freopen (index_file_name
, "w", stdout
);
393 open_error (index_file_name
);
394 close_stdout_set_file_name (index_file_name
);
397 stdlis
= to_stdout_option
? stderr
: stdout
;
399 if (record_size
== 0)
400 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
402 if (archive_names
== 0)
403 FATAL_ERROR ((0, 0, _("No archive name given")));
405 tar_stat_destroy (¤t_stat_info
);
412 /* When updating the archive, we start with reading. */
413 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
415 read_full_records
= read_full_records_option
;
419 if (use_compress_program_option
)
421 switch (wanted_access
)
424 child_pid
= sys_child_open_for_uncompress ();
425 read_full_records
= true;
426 record_end
= record_start
; /* set up for 1st record = # 0 */
430 child_pid
= sys_child_open_for_compress ();
434 abort (); /* Should not happen */
438 if (wanted_access
== ACCESS_WRITE
439 && strcmp (archive_name_array
[0], "-") == 0)
442 else if (strcmp (archive_name_array
[0], "-") == 0)
444 read_full_records
= true; /* could be a pipe, be safe */
446 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
448 switch (wanted_access
)
452 enum compress_type type
;
454 archive
= STDIN_FILENO
;
456 type
= check_compressed_archive ();
459 _("Archive is compressed. Use %s option"),
460 compress_option (type
)));
465 archive
= STDOUT_FILENO
;
470 archive
= STDIN_FILENO
;
472 write_archive_to_stdout
= true;
476 else if (verify_option
)
477 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
478 MODE_RW
, rsh_command_option
);
480 switch (wanted_access
)
483 archive
= open_compressed_archive ();
489 maybe_backup_file (archive_name_array
[0], 1);
492 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
497 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
498 MODE_RW
, rsh_command_option
);
503 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
505 int saved_errno
= errno
;
510 open_fatal (archive_name_array
[0]);
513 sys_detect_dev_null_output ();
514 sys_save_archive_dev_ino ();
515 SET_BINARY_MODE (archive
);
517 switch (wanted_access
)
521 record_end
= record_start
; /* set up for 1st record = # 0 */
524 find_next_block (); /* read it in, check for EOF */
533 /* Perform a write to flush the buffer. */
539 if (checkpoint_option
&& !(++checkpoint
% 10))
540 /* TRANSLATORS: This is a ``checkpoint of write operation'',
541 *not* ``Writing a checkpoint''.
542 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
543 *not* ``Escribiendo un punto de comprobaci@'on'' */
544 WARN ((0, 0, _("Write checkpoint %d"), checkpoint
));
546 if (tape_length_option
&& tape_length_option
<= bytes_written
)
551 else if (dev_null_output
)
552 status
= record_size
;
554 status
= sys_write_archive_buffer ();
559 /* Handle write errors on the archive. Write errors are always fatal.
560 Hitting the end of a volume does not cause a write error unless the
561 write was the first record of the volume. */
563 archive_write_error (ssize_t status
)
565 /* It might be useful to know how much was written before the error
570 print_total_written ();
574 write_fatal_details (*archive_name_cursor
, status
, record_size
);
577 /* Handle read errors on the archive. If the read should be retried,
578 return to the caller. */
580 archive_read_error (void)
582 read_error (*archive_name_cursor
);
584 if (record_start_block
== 0)
585 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
587 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
588 then give up on reading the archive. */
590 if (read_error_count
++ > READ_ERROR_MAX
)
591 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
596 short_read (size_t status
)
598 size_t left
; /* bytes left */
599 char *more
; /* pointer to next byte to read */
601 more
= record_start
->buffer
+ status
;
602 left
= record_size
- status
;
604 while (left
% BLOCKSIZE
!= 0
605 || (left
&& status
&& read_full_records
))
608 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
609 archive_read_error ();
614 if (! read_full_records
)
616 unsigned long rest
= record_size
- left
;
619 ngettext ("Unaligned block (%lu byte) in archive",
620 "Unaligned block (%lu bytes) in archive",
625 /* User warned us about this. Fix up. */
631 /* FIXME: for size=0, multi-volume support. On the first record, warn
632 about the problem. */
634 if (!read_full_records
&& verbose_option
> 1
635 && record_start_block
== 0 && status
!= 0)
637 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
639 ngettext ("Record size = %lu block",
640 "Record size = %lu blocks",
645 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
649 /* Perform a read to flush the buffer. */
653 size_t status
; /* result from system call */
655 if (checkpoint_option
&& !(++checkpoint
% 10))
656 /* TRANSLATORS: This is a ``checkpoint of read operation'',
657 *not* ``Reading a checkpoint''.
658 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
659 *not* ``Leyendo un punto de comprobaci@'on'' */
660 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
662 /* Clear the count of errors. This only applies to a single call to
665 read_error_count
= 0; /* clear error count */
667 if (write_archive_to_stdout
&& record_start_block
!= 0)
669 archive
= STDOUT_FILENO
;
670 status
= sys_write_archive_buffer ();
671 archive
= STDIN_FILENO
;
672 if (status
!= record_size
)
673 archive_write_error (status
);
676 status
= rmtread (archive
, record_start
->buffer
, record_size
);
677 if (status
== record_size
)
682 /* Flush the current buffer to/from the archive. */
686 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
687 record_start_block
+= record_end
- record_start
;
688 current_block
= record_start
;
689 record_end
= record_start
+ blocking_factor
;
691 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
693 access_mode
= ACCESS_WRITE
;
694 time_to_start_writing
= false;
705 flush_write_ptr (buffer_level
);
713 /* Backspace the archive descriptor by one record worth. If it's a
714 tape, MTIOCTOP will work. If it's something else, try to seek on
715 it. If we can't seek, we lose! */
717 backspace_output (void)
721 struct mtop operation
;
723 operation
.mt_op
= MTBSR
;
724 operation
.mt_count
= 1;
725 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
727 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
733 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
735 /* Seek back to the beginning of this record and start writing there. */
737 position
-= record_size
;
740 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
742 /* Lseek failed. Try a different method. */
745 _("Cannot backspace archive file; it may be unreadable without -i")));
747 /* Replace the first part of the record with NULs. */
749 if (record_start
->buffer
!= output_start
)
750 memset (record_start
->buffer
, 0,
751 output_start
- record_start
->buffer
);
757 seek_archive (off_t size
)
759 off_t start
= current_block_ordinal ();
762 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
764 size
-= skipped
* BLOCKSIZE
;
766 if (size
< record_size
)
770 /* Compute number of records to skip */
771 nrec
= size
/ record_size
;
772 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
776 if (offset
% record_size
)
777 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
779 /* Convert to number of records */
781 /* Compute number of skipped blocks */
782 nblk
= offset
- start
;
784 /* Update buffering info */
785 records_read
+= nblk
/ blocking_factor
;
786 record_start_block
= offset
- blocking_factor
;
787 current_block
= record_end
;
792 /* Close the archive file. */
796 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
799 if (current_block
> record_start
)
803 sys_drain_input_pipe ();
809 if (rmtclose (archive
) != 0)
810 close_warn (*archive_name_cursor
);
812 sys_wait_for_child (child_pid
);
814 tar_stat_destroy (¤t_stat_info
);
819 free (record_buffer
[0]);
820 free (record_buffer
[1]);
823 /* Called to initialize the global volume number. */
825 init_volume_number (void)
827 FILE *file
= fopen (volno_file_option
, "r");
831 if (fscanf (file
, "%d", &global_volno
) != 1
833 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
834 quotearg_colon (volno_file_option
)));
836 read_error (volno_file_option
);
837 if (fclose (file
) != 0)
838 close_error (volno_file_option
);
840 else if (errno
!= ENOENT
)
841 open_error (volno_file_option
);
844 /* Called to write out the closing global volume number. */
846 closeout_volume_number (void)
848 FILE *file
= fopen (volno_file_option
, "w");
852 fprintf (file
, "%d\n", global_volno
);
854 write_error (volno_file_option
);
855 if (fclose (file
) != 0)
856 close_error (volno_file_option
);
859 open_error (volno_file_option
);
864 increase_volume_number ()
867 if (global_volno
< 0)
868 FATAL_ERROR ((0, 0, _("Volume number overflow")));
873 change_tape_menu (FILE *read_file
)
875 char *input_buffer
= NULL
;
880 fputc ('\007', stderr
);
882 _("Prepare volume #%d for %s and hit return: "),
883 global_volno
+ 1, quote (*archive_name_cursor
));
886 if (getline (&input_buffer
, &size
, read_file
) <= 0)
888 WARN ((0, 0, _("EOF where user reply was expected")));
890 if (subcommand_option
!= EXTRACT_SUBCOMMAND
891 && subcommand_option
!= LIST_SUBCOMMAND
892 && subcommand_option
!= DIFF_SUBCOMMAND
)
893 WARN ((0, 0, _("WARNING: Archive is incomplete")));
898 if (input_buffer
[0] == '\n'
899 || input_buffer
[0] == 'y'
900 || input_buffer
[0] == 'Y')
903 switch (input_buffer
[0])
907 fprintf (stderr
, _("\
908 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
910 y or newline Continue operation\n"));
911 if (!restrict_option
)
912 fprintf (stderr
, _(" ! Spawn a subshell\n"));
913 fprintf (stderr
, _(" ? Print this list\n"));
920 WARN ((0, 0, _("No new volume; exiting.\n")));
922 if (subcommand_option
!= EXTRACT_SUBCOMMAND
923 && subcommand_option
!= LIST_SUBCOMMAND
924 && subcommand_option
!= DIFF_SUBCOMMAND
)
925 WARN ((0, 0, _("WARNING: Archive is incomplete")));
930 /* Get new file name. */
936 for (name
= input_buffer
+ 1;
937 *name
== ' ' || *name
== '\t';
941 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
945 /* FIXME: the following allocation is never reclaimed. */
946 *archive_name_cursor
= xstrdup (name
);
951 if (!restrict_option
)
959 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
965 /* We've hit the end of the old volume. Close it and open the next one.
966 Return nonzero on success.
969 new_volume (enum access_mode mode
)
971 static FILE *read_file
;
975 if (!read_file
&& !info_script_option
)
976 /* FIXME: if fopen is used, it will never be closed. */
977 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
984 assign_string (&volume_label
, NULL
);
985 assign_string (&continued_file_name
, NULL
);
986 continued_file_size
= continued_file_offset
= 0;
988 if (rmtclose (archive
) != 0)
989 close_warn (*archive_name_cursor
);
991 archive_name_cursor
++;
992 if (archive_name_cursor
== archive_name_array
+ archive_names
)
994 archive_name_cursor
= archive_name_array
;
1002 /* We have to prompt from now on. */
1004 if (info_script_option
)
1006 if (volno_file_option
)
1007 closeout_volume_number ();
1008 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1009 FATAL_ERROR ((0, 0, _("%s command failed"),
1010 quote (info_script_option
)));
1013 change_tape_menu (read_file
);
1016 if (strcmp (archive_name_cursor
[0], "-") == 0)
1018 read_full_records
= true;
1019 archive
= STDIN_FILENO
;
1021 else if (verify_option
)
1022 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1023 rsh_command_option
);
1028 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1029 rsh_command_option
);
1034 maybe_backup_file (*archive_name_cursor
, 1);
1035 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1036 rsh_command_option
);
1040 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1041 rsh_command_option
);
1047 open_warn (*archive_name_cursor
);
1048 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1049 undo_last_backup ();
1054 SET_BINARY_MODE (archive
);
1060 read_header0 (struct tar_stat_info
*info
)
1062 enum read_header rc
;
1064 tar_stat_init (info
);
1065 rc
= read_header_primitive (false, info
);
1066 if (rc
== HEADER_SUCCESS
)
1068 set_next_block_after (current_header
);
1071 ERROR ((0, 0, _("This does not look like a tar archive")));
1079 union block
*header
;
1080 struct tar_stat_info dummy
;
1082 switch (subcommand_option
)
1084 case APPEND_SUBCOMMAND
:
1085 case CAT_SUBCOMMAND
:
1086 case UPDATE_SUBCOMMAND
:
1087 if (!new_volume (ACCESS_UPDATE
))
1092 if (!new_volume (ACCESS_READ
))
1097 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1099 archive_read_error ();
1101 if (status
!= record_size
)
1102 short_read (status
);
1104 header
= find_next_block ();
1108 switch (header
->header
.typeflag
)
1112 if (!read_header0 (&dummy
))
1114 xheader_decode (&dummy
); /* decodes values from the global header */
1115 tar_stat_destroy (&dummy
);
1118 /* We have read the extended header of the first member in
1119 this volume. Put it back, so next read_header works as
1121 current_block
= record_start
;
1126 case GNUTYPE_VOLHDR
:
1127 if (!read_header0 (&dummy
))
1129 tar_stat_destroy (&dummy
);
1130 assign_string (&volume_label
, current_header
->header
.name
);
1131 set_next_block_after (header
);
1132 header
= find_next_block ();
1133 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1137 case GNUTYPE_MULTIVOL
:
1138 if (!read_header0 (&dummy
))
1140 tar_stat_destroy (&dummy
);
1141 assign_string (&continued_file_name
, current_header
->header
.name
);
1142 continued_file_size
=
1143 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1144 continued_file_offset
=
1145 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1155 if (!continued_file_name
1156 || strcmp (continued_file_name
, real_s_name
))
1158 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1159 && strlen (real_s_name
) >= NAME_FIELD_SIZE
1160 && strncmp (continued_file_name
, real_s_name
,
1161 NAME_FIELD_SIZE
) == 0)
1163 _("%s is possibly continued on this volume: header contains truncated name"),
1164 quote (real_s_name
)));
1167 WARN ((0, 0, _("%s is not continued on this volume"),
1168 quote (real_s_name
)));
1173 s
= continued_file_size
+ continued_file_offset
;
1175 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1177 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1178 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1179 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1181 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1182 quote (continued_file_name
),
1183 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1184 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1185 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1189 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1191 WARN ((0, 0, _("This volume is out of sequence")));
1196 increase_volume_number ();
1201 /* Check the LABEL block against the volume label, seen as a globbing
1202 pattern. Return true if the pattern matches. In case of failure,
1203 retry matching a volume sequence number before giving up in
1204 multi-volume mode. */
1206 check_label_pattern (union block
*label
)
1211 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1214 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1217 if (!multi_volume_option
)
1220 string
= xmalloc (strlen (volume_label_option
)
1221 + sizeof VOLUME_LABEL_APPEND
+ 1);
1222 strcpy (string
, volume_label_option
);
1223 strcat (string
, VOLUME_LABEL_APPEND
);
1224 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1229 /* Check if the next block contains a volume label and if this matches
1230 the one given in the command line */
1232 match_volume_label (void)
1234 union block
*label
= find_next_block ();
1237 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1238 quote (volume_label_option
)));
1239 if (!check_label_pattern (label
))
1240 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1241 quote_n (0, label
->header
.name
),
1242 quote_n (1, volume_label_option
)));
1245 /* Mark the archive with volume label STR. */
1247 _write_volume_label (const char *str
)
1249 if (archive_format
== POSIX_FORMAT
)
1250 xheader_store ("GNU.volume.label", NULL
, str
);
1253 union block
*label
= find_next_block ();
1255 memset (label
, 0, BLOCKSIZE
);
1257 strcpy (label
->header
.name
, volume_label_option
);
1258 assign_string (¤t_stat_info
.file_name
,
1259 label
->header
.name
);
1260 current_stat_info
.had_trailing_slash
=
1261 strip_trailing_slashes (current_stat_info
.file_name
);
1263 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1264 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1265 finish_header (¤t_stat_info
, label
, -1);
1266 set_next_block_after (label
);
1270 #define VOL_SUFFIX "Volume"
1272 /* Add a volume label to a part of multi-volume archive */
1274 add_volume_label (void)
1276 char buf
[UINTMAX_STRSIZE_BOUND
];
1277 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1278 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1280 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1281 _write_volume_label (s
);
1288 if (archive_format
== POSIX_FORMAT
)
1290 off_t block_ordinal
;
1292 struct tar_stat_info st
;
1293 static size_t real_s_part_no
; /* FIXME */
1296 memset (&st
, 0, sizeof st
);
1297 st
.orig_file_name
= st
.file_name
= real_s_name
;
1298 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1299 st
.stat
.st_uid
= getuid ();
1300 st
.stat
.st_gid
= getgid ();
1301 st
.orig_file_name
= xheader_format_name (&st
,
1302 "%d/GNUFileParts.%p/%f.%n",
1304 st
.file_name
= st
.orig_file_name
;
1305 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1307 block_ordinal
= current_block_ordinal ();
1308 blk
= start_header (&st
);
1310 abort (); /* FIXME */
1311 finish_header (&st
, blk
, block_ordinal
);
1312 free (st
.orig_file_name
);
1317 /* Add a volume label to the current archive */
1319 write_volume_label (void)
1321 if (multi_volume_option
)
1322 add_volume_label ();
1324 _write_volume_label (volume_label_option
);
1327 /* Write GNU multi-volume header */
1329 gnu_add_multi_volume_header (void)
1332 union block
*block
= find_next_block ();
1334 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1336 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1337 quotearg_colon (real_s_name
)));
1339 memset (block
, 0, BLOCKSIZE
);
1341 /* FIXME: Michael P Urban writes: [a long name file] is being written
1342 when a new volume rolls around [...] Looks like the wrong value is
1343 being preserved in real_s_name, though. */
1345 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1346 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1348 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1349 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1350 block
->oldgnu_header
.offset
);
1352 tmp
= verbose_option
;
1354 finish_header (¤t_stat_info
, block
, -1);
1355 verbose_option
= tmp
;
1356 set_next_block_after (block
);
1359 /* Add a multi volume header to the current archive. The exact header format
1360 depends on the archive format. */
1362 add_multi_volume_header (void)
1364 if (archive_format
== POSIX_FORMAT
)
1366 off_t d
= real_s_totsize
- real_s_sizeleft
;
1367 xheader_store ("GNU.volume.filename", NULL
, real_s_name
);
1368 xheader_store ("GNU.volume.size", NULL
, &real_s_sizeleft
);
1369 xheader_store ("GNU.volume.offset", NULL
, &d
);
1372 gnu_add_multi_volume_header ();
1375 /* Synchronize multi-volume globals */
1377 multi_volume_sync ()
1379 if (multi_volume_option
)
1383 assign_string (&real_s_name
,
1384 safer_name_suffix (save_name
, false,
1385 absolute_names_option
));
1386 real_s_totsize
= save_totsize
;
1387 real_s_sizeleft
= save_sizeleft
;
1391 assign_string (&real_s_name
, 0);
1393 real_s_sizeleft
= 0;
1399 /* Low-level flush functions */
1401 /* Simple flush read (no multi-volume or label extensions) */
1403 simple_flush_read (void)
1405 size_t status
; /* result from system call */
1407 if (checkpoint_option
&& !(++checkpoint
% 10))
1408 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1409 *not* ``Reading a checkpoint''.
1410 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1411 *not* ``Leyendo un punto de comprobaci@'on'' */
1412 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1414 /* Clear the count of errors. This only applies to a single call to
1417 read_error_count
= 0; /* clear error count */
1419 if (write_archive_to_stdout
&& record_start_block
!= 0)
1421 archive
= STDOUT_FILENO
;
1422 status
= sys_write_archive_buffer ();
1423 archive
= STDIN_FILENO
;
1424 if (status
!= record_size
)
1425 archive_write_error (status
);
1430 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1431 if (status
== record_size
)
1436 if (status
== SAFE_READ_ERROR
)
1438 archive_read_error ();
1439 continue; /* try again */
1443 short_read (status
);
1446 /* Simple flush write (no multi-volume or label extensions) */
1448 simple_flush_write (size_t level
__attribute__((unused
)))
1452 status
= _flush_write ();
1453 if (status
!= record_size
)
1454 archive_write_error (status
);
1458 bytes_written
+= status
;
1463 /* GNU flush functions. These support multi-volume and archive labels in
1464 GNU and PAX archive formats. */
1467 _gnu_flush_read (void)
1469 size_t status
; /* result from system call */
1471 if (checkpoint_option
&& !(++checkpoint
% 10))
1472 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1473 *not* ``Reading a checkpoint''.
1474 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1475 *not* ``Leyendo un punto de comprobaci@'on'' */
1476 WARN ((0, 0, _("Read checkpoint %d"), checkpoint
));
1478 /* Clear the count of errors. This only applies to a single call to
1481 read_error_count
= 0; /* clear error count */
1483 if (write_archive_to_stdout
&& record_start_block
!= 0)
1485 archive
= STDOUT_FILENO
;
1486 status
= sys_write_archive_buffer ();
1487 archive
= STDIN_FILENO
;
1488 if (status
!= record_size
)
1489 archive_write_error (status
);
1492 multi_volume_sync ();
1496 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1497 if (status
== record_size
)
1503 /* The condition below used to include
1504 || (status > 0 && !read_full_records)
1505 This is incorrect since even if new_volume() succeeds, the
1506 subsequent call to rmtread will overwrite the chunk of data
1507 already read in the buffer, so the processing will fail */
1509 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1510 && multi_volume_option
)
1512 while (!try_new_volume ())
1516 else if (status
== SAFE_READ_ERROR
)
1518 archive_read_error ();
1523 short_read (status
);
1527 gnu_flush_read (void)
1529 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1531 flush_read_ptr
= gnu_flush_read
;
1535 _gnu_flush_write (size_t buffer_level
)
1538 union block
*header
;
1543 status
= _flush_write ();
1544 if (status
!= record_size
&& !multi_volume_option
)
1545 archive_write_error (status
);
1549 bytes_written
+= status
;
1552 if (status
== record_size
)
1554 multi_volume_sync ();
1558 /* In multi-volume mode. */
1559 /* ENXIO is for the UNIX PC. */
1560 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1561 archive_write_error (status
);
1563 if (!new_volume (ACCESS_WRITE
))
1566 xheader_destroy (&extended_header
);
1568 increase_volume_number ();
1570 prev_written
+= bytes_written
;
1573 copy_ptr
= record_start
->buffer
+ status
;
1574 copy_size
= buffer_level
- status
;
1575 /* Switch to the next buffer */
1576 record_index
= !record_index
;
1579 if (volume_label_option
)
1580 add_volume_label ();
1583 add_multi_volume_header ();
1585 write_extended (true, NULL
, find_next_block ());
1587 add_chunk_header ();
1588 header
= find_next_block ();
1589 bufsize
= available_space_after (header
);
1590 while (bufsize
< copy_size
)
1592 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1593 copy_ptr
+= bufsize
;
1594 copy_size
-= bufsize
;
1595 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1596 header
= find_next_block ();
1597 bufsize
= available_space_after (header
);
1599 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1600 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1601 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1606 gnu_flush_write (size_t buffer_level
)
1608 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1609 _gnu_flush_write (buffer_level
);
1610 flush_write_ptr
= gnu_flush_write
;
1622 flush_write_ptr (record_size
);
1626 open_archive (enum access_mode wanted_access
)
1628 flush_read_ptr
= gnu_flush_read
;
1629 flush_write_ptr
= gnu_flush_write
;
1631 _open_archive (wanted_access
);
1632 switch (wanted_access
)
1635 if (volume_label_option
)
1636 match_volume_label ();
1640 records_written
= 0;
1641 if (volume_label_option
)
1642 write_volume_label ();