1 /* Buffer management for tar.
3 Copyright 1988, 1992-1994, 1996-1997, 1999-2010, 2013 Free Software
6 This file is part of GNU tar.
8 GNU tar is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GNU tar is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 Written by John Gilmore, on 1985-08-25. */
24 #include <system-ioctl.h>
36 /* Number of retries before giving up on read. */
37 #define READ_ERROR_MAX 10
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 union block
*record_buffer_aligned
[2];
45 static int record_index
;
47 /* FIXME: The following variables should ideally be static to this
48 module. However, this cannot be done yet. The cleanup continues! */
50 union block
*record_start
; /* start of record of archive */
51 union block
*record_end
; /* last+1 block of archive record */
52 union block
*current_block
; /* current block of archive */
53 enum access_mode access_mode
; /* how do we handle the archive */
54 off_t records_read
; /* number of records read from this archive */
55 off_t records_written
; /* likewise, for records written */
56 extern off_t records_skipped
; /* number of records skipped at the start
57 of the archive, defined in delete.c */
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 static bool read_full_records
= false;
77 /* We're reading, but we just read the last block and it's time to update.
80 FIXME: Either eliminate it or move it to common.h.
82 extern bool time_to_start_writing
;
84 bool write_archive_to_stdout
;
86 static void (*flush_write_ptr
) (size_t);
87 static void (*flush_read_ptr
) (void);
91 char *continued_file_name
;
92 uintmax_t continued_file_size
;
93 uintmax_t continued_file_offset
;
96 static int volno
= 1; /* which volume of a multi-volume tape we're
98 static int global_volno
= 1; /* volume number to print in external
101 bool write_archive_to_stdout
;
104 /* Multi-volume tracking support */
106 /* When creating a multi-volume archive, each 'bufmap' represents
107 a member stored (perhaps partly) in the current record buffer.
108 After flushing the record to the output media, all bufmaps that
109 represent fully written members are removed from the list, then
110 the sizeleft and start numbers in the remaining bufmaps are updated.
112 When reading from a multi-volume archive, the list degrades to a
113 single element, which keeps information about the member currently
119 struct bufmap
*next
; /* Pointer to the next map entry */
120 size_t start
; /* Offset of the first data block */
121 char *file_name
; /* Name of the stored file */
122 off_t sizetotal
; /* Size of the stored file */
123 off_t sizeleft
; /* Size left to read/write */
125 static struct bufmap
*bufmap_head
, *bufmap_tail
;
127 /* This variable, when set, inhibits updating the bufmap chain after
128 a write. This is necessary when writing extended POSIX headers. */
129 static int inhibit_map
;
132 mv_begin_write (const char *file_name
, off_t totsize
, off_t sizeleft
)
134 if (multi_volume_option
)
136 struct bufmap
*bp
= xmalloc (sizeof bp
[0]);
138 bufmap_tail
->next
= bp
;
144 bp
->start
= current_block
- record_start
;
145 bp
->file_name
= xstrdup (file_name
);
146 bp
->sizetotal
= totsize
;
147 bp
->sizeleft
= sizeleft
;
151 static struct bufmap
*
152 bufmap_locate (size_t off
)
156 for (map
= bufmap_head
; map
; map
= map
->next
)
159 || off
< map
->next
->start
* BLOCKSIZE
)
166 bufmap_free (struct bufmap
*mark
)
169 for (map
= bufmap_head
; map
&& map
!= mark
; )
171 struct bufmap
*next
= map
->next
;
172 free (map
->file_name
);
178 bufmap_tail
= bufmap_head
;
182 bufmap_reset (struct bufmap
*map
, ssize_t fixup
)
187 for (; map
; map
= map
->next
)
193 static struct tar_stat_info dummy
;
196 buffer_write_global_xheader (void)
198 xheader_write_global (&dummy
.xhdr
);
202 mv_begin_read (struct tar_stat_info
*st
)
204 mv_begin_write (st
->orig_file_name
, st
->stat
.st_size
, st
->stat
.st_size
);
210 if (multi_volume_option
)
215 mv_size_left (off_t size
)
218 bufmap_head
->sizeleft
= size
;
225 clear_read_error_count (void)
227 read_error_count
= 0;
231 /* Time-related functions */
233 static double duration
;
236 set_start_time (void)
238 gettime (&start_time
);
239 volume_start_time
= start_time
;
240 last_stat_time
= start_time
;
244 set_volume_start_time (void)
246 gettime (&volume_start_time
);
247 last_stat_time
= volume_start_time
;
251 compute_duration (void)
255 duration
+= ((now
.tv_sec
- last_stat_time
.tv_sec
)
256 + (now
.tv_nsec
- last_stat_time
.tv_nsec
) / 1e9
);
257 gettime (&last_stat_time
);
261 /* Compression detection */
264 ct_none
, /* Unknown compression type */
265 ct_tar
, /* Plain tar file */
275 static enum compress_type archive_compression_type
= ct_none
;
279 enum compress_type type
;
286 enum compress_type type
;
291 static struct zip_magic
const magic
[] = {
294 { ct_compress
, 2, "\037\235" },
295 { ct_gzip
, 2, "\037\213" },
296 { ct_bzip2
, 3, "BZh" },
297 { ct_lzip
, 4, "LZIP" },
298 { ct_lzma
, 6, "\xFFLZMA" },
299 { ct_lzop
, 4, "\211LZO" },
300 { ct_xz
, 6, "\xFD" "7zXZ" },
303 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
305 static struct zip_program zip_program
[] = {
306 { ct_compress
, COMPRESS_PROGRAM
, "-Z" },
307 { ct_compress
, GZIP_PROGRAM
, "-z" },
308 { ct_gzip
, GZIP_PROGRAM
, "-z" },
309 { ct_bzip2
, BZIP2_PROGRAM
, "-j" },
310 { ct_bzip2
, "lbzip2", "-j" },
311 { ct_lzip
, LZIP_PROGRAM
, "--lzip" },
312 { ct_lzma
, LZMA_PROGRAM
, "--lzma" },
313 { ct_lzma
, XZ_PROGRAM
, "-J" },
314 { ct_lzop
, LZOP_PROGRAM
, "--lzop" },
315 { ct_xz
, XZ_PROGRAM
, "-J" },
319 static struct zip_program
const *
320 find_zip_program (enum compress_type type
, int *pstate
)
324 for (i
= *pstate
; zip_program
[i
].type
!= ct_none
; i
++)
326 if (zip_program
[i
].type
== type
)
329 return zip_program
+ i
;
337 first_decompress_program (int *pstate
)
339 struct zip_program
const *zp
;
341 if (use_compress_program_option
)
342 return use_compress_program_option
;
344 if (archive_compression_type
== ct_none
)
348 zp
= find_zip_program (archive_compression_type
, pstate
);
349 return zp
? zp
->program
: NULL
;
353 next_decompress_program (int *pstate
)
355 struct zip_program
const *zp
;
357 if (use_compress_program_option
)
359 zp
= find_zip_program (archive_compression_type
, pstate
);
360 return zp
? zp
->program
: NULL
;
364 compress_option (enum compress_type type
)
366 struct zip_program
const *zp
;
368 zp
= find_zip_program (type
, &i
);
369 return zp
? zp
->option
: NULL
;
372 /* Check if the file ARCHIVE is a compressed archive. */
373 static enum compress_type
374 check_compressed_archive (bool *pshort
)
376 struct zip_magic
const *p
;
383 /* Prepare global data needed for find_next_block: */
384 record_end
= record_start
; /* set up for 1st record = # 0 */
385 sfr
= read_full_records
;
386 read_full_records
= true; /* Suppress fatal error on reading a partial
388 *pshort
= find_next_block () == 0;
390 /* Restore global values */
391 read_full_records
= sfr
;
393 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
394 /* Probably a valid header */
397 for (p
= magic
+ 2; p
< magic
+ NMAGIC
; p
++)
398 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
404 /* Guess if the archive is seekable. */
406 guess_seekable_archive (void)
410 if (subcommand_option
== DELETE_SUBCOMMAND
)
412 /* The current code in delete.c is based on the assumption that
413 skip_member() reads all data from the archive. So, we should
414 make sure it won't use seeks. On the other hand, the same code
415 depends on the ability to backspace a record in the archive,
416 so setting seekable_archive to false is technically incorrect.
417 However, it is tested only in skip_member(), so it's not a
419 seekable_archive
= false;
422 if (seek_option
!= -1)
424 seekable_archive
= !!seek_option
;
428 if (!multi_volume_option
&& !use_compress_program_option
429 && fstat (archive
, &st
) == 0)
430 seekable_archive
= S_ISREG (st
.st_mode
);
432 seekable_archive
= false;
435 /* Open an archive named archive_name_array[0]. Detect if it is
436 a compressed archive of known type and use corresponding decompression
439 open_compressed_archive (void)
441 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
442 MODE_RW
, rsh_command_option
);
446 if (!multi_volume_option
)
448 if (!use_compress_program_option
)
451 enum compress_type type
= check_compressed_archive (&shortfile
);
457 ERROR ((0, 0, _("This does not look like a tar archive")));
462 ERROR ((0, 0, _("This does not look like a tar archive")));
463 set_compression_program_by_suffix (archive_name_array
[0], NULL
);
464 if (!use_compress_program_option
)
469 archive_compression_type
= type
;
474 /* FD is not needed any more */
477 hit_eof
= false; /* It might have been set by find_next_block in
478 check_compressed_archive */
480 /* Open compressed archive */
481 child_pid
= sys_child_open_for_uncompress ();
482 read_full_records
= true;
486 record_end
= record_start
; /* set up for 1st record = # 0 */
493 print_stats (FILE *fp
, const char *text
, tarlong numbytes
)
495 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
496 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
497 char rate
[LONGEST_HUMAN_READABLE
+ 1];
499 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
501 sprintf (bytes
, TARLONG_FORMAT
, numbytes
);
503 fprintf (fp
, "%s: %s (%s, %s/s)\n",
505 human_readable (numbytes
, abbr
, human_opts
, 1, 1),
506 (0 < duration
&& numbytes
/ duration
< (uintmax_t) -1
507 ? human_readable (numbytes
/ duration
, rate
, human_opts
, 1, 1)
512 print_total_stats (void)
514 switch (subcommand_option
)
516 case CREATE_SUBCOMMAND
:
518 case UPDATE_SUBCOMMAND
:
519 case APPEND_SUBCOMMAND
:
520 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
521 print_stats (stderr
, _("Total bytes written"),
522 prev_written
+ bytes_written
);
525 case DELETE_SUBCOMMAND
:
527 char buf
[UINTMAX_STRSIZE_BOUND
];
528 print_stats (stderr
, _("Total bytes read"),
529 records_read
* record_size
);
530 print_stats (stderr
, _("Total bytes written"),
531 prev_written
+ bytes_written
);
532 fprintf (stderr
, _("Total bytes deleted: %s\n"),
533 STRINGIFY_BIGINT ((records_read
- records_skipped
)
535 - (prev_written
+ bytes_written
), buf
));
539 case EXTRACT_SUBCOMMAND
:
540 case LIST_SUBCOMMAND
:
541 case DIFF_SUBCOMMAND
:
542 print_stats (stderr
, _("Total bytes read"),
543 records_read
* record_size
);
551 /* Compute and return the block ordinal at current_block. */
553 current_block_ordinal (void)
555 return record_start_block
+ (current_block
- record_start
);
558 /* If the EOF flag is set, reset it, as well as current_block, etc. */
565 current_block
= record_start
;
566 record_end
= record_start
+ blocking_factor
;
567 access_mode
= ACCESS_WRITE
;
571 /* Return the location of the next available input or output block.
572 Return zero for EOF. Once we have returned zero, we just keep returning
573 it, to avoid accidentally going on to the next file on the tape. */
575 find_next_block (void)
577 if (current_block
== record_end
)
582 if (current_block
== record_end
)
588 return current_block
;
591 /* Indicate that we have used all blocks up thru BLOCK. */
593 set_next_block_after (union block
*block
)
595 while (block
>= current_block
)
598 /* Do *not* flush the archive here. If we do, the same argument to
599 set_next_block_after could mean the next block (if the input record
600 is exactly one block long), which is not what is intended. */
602 if (current_block
> record_end
)
606 /* Return the number of bytes comprising the space between POINTER
607 through the end of the current buffer of blocks. This space is
608 available for filling with data, or taking data from. POINTER is
609 usually (but not always) the result of previous find_next_block call. */
611 available_space_after (union block
*pointer
)
613 return record_end
->buffer
- pointer
->buffer
;
616 /* Close file having descriptor FD, and abort if close unsuccessful. */
621 close_error (_("(pipe)"));
627 if (! record_buffer_aligned
[record_index
])
628 record_buffer_aligned
[record_index
] =
629 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
631 record_start
= record_buffer_aligned
[record_index
];
632 current_block
= record_start
;
633 record_end
= record_start
+ blocking_factor
;
636 /* Open an archive file. The argument specifies whether we are
637 reading or writing, or both. */
639 _open_archive (enum access_mode wanted_access
)
641 int backed_up_flag
= 0;
643 if (record_size
== 0)
644 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
646 if (archive_names
== 0)
647 FATAL_ERROR ((0, 0, _("No archive name given")));
649 tar_stat_destroy (¤t_stat_info
);
654 /* When updating the archive, we start with reading. */
655 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
657 read_full_records
= read_full_records_option
;
661 if (use_compress_program_option
)
663 switch (wanted_access
)
666 child_pid
= sys_child_open_for_uncompress ();
667 read_full_records
= true;
668 record_end
= record_start
; /* set up for 1st record = # 0 */
672 child_pid
= sys_child_open_for_compress ();
676 abort (); /* Should not happen */
681 && wanted_access
== ACCESS_WRITE
682 && strcmp (archive_name_array
[0], "-") == 0)
685 else if (strcmp (archive_name_array
[0], "-") == 0)
687 read_full_records
= true; /* could be a pipe, be safe */
689 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
691 switch (wanted_access
)
696 enum compress_type type
;
698 archive
= STDIN_FILENO
;
700 type
= check_compressed_archive (&shortfile
);
701 if (type
!= ct_tar
&& type
!= ct_none
)
703 _("Archive is compressed. Use %s option"),
704 compress_option (type
)));
706 ERROR ((0, 0, _("This does not look like a tar archive")));
711 archive
= STDOUT_FILENO
;
712 if (!index_file_name
)
717 archive
= STDIN_FILENO
;
718 write_archive_to_stdout
= true;
719 record_end
= record_start
; /* set up for 1st record = # 0 */
720 if (!index_file_name
)
726 switch (wanted_access
)
729 archive
= open_compressed_archive ();
731 guess_seekable_archive ();
737 maybe_backup_file (archive_name_array
[0], 1);
741 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
742 MODE_RW
, rsh_command_option
);
744 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
749 archive
= rmtopen (archive_name_array
[0],
750 O_RDWR
| O_CREAT
| O_BINARY
,
751 MODE_RW
, rsh_command_option
);
753 switch (check_compressed_archive (NULL
))
761 _("Cannot update compressed archives")));
767 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
769 int saved_errno
= errno
;
774 open_fatal (archive_name_array
[0]);
777 sys_detect_dev_null_output ();
778 sys_save_archive_dev_ino ();
779 SET_BINARY_MODE (archive
);
781 switch (wanted_access
)
784 find_next_block (); /* read it in, check for EOF */
794 /* Perform a write to flush the buffer. */
800 checkpoint_run (true);
801 if (tape_length_option
&& tape_length_option
<= bytes_written
)
806 else if (dev_null_output
)
807 status
= record_size
;
809 status
= sys_write_archive_buffer ();
811 if (status
&& multi_volume_option
&& !inhibit_map
)
813 struct bufmap
*map
= bufmap_locate (status
);
816 size_t delta
= status
- map
->start
* BLOCKSIZE
;
817 if (delta
> map
->sizeleft
)
818 delta
= map
->sizeleft
;
819 map
->sizeleft
-= delta
;
820 if (map
->sizeleft
== 0)
822 bufmap_reset (map
, map
? (- map
->start
) : 0);
828 /* Handle write errors on the archive. Write errors are always fatal.
829 Hitting the end of a volume does not cause a write error unless the
830 write was the first record of the volume. */
832 archive_write_error (ssize_t status
)
834 /* It might be useful to know how much was written before the error
839 print_total_stats ();
843 write_fatal_details (*archive_name_cursor
, status
, record_size
);
846 /* Handle read errors on the archive. If the read should be retried,
847 return to the caller. */
849 archive_read_error (void)
851 read_error (*archive_name_cursor
);
853 if (record_start_block
== 0)
854 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
856 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
857 then give up on reading the archive. */
859 if (read_error_count
++ > READ_ERROR_MAX
)
860 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
865 archive_is_dev (void)
869 if (fstat (archive
, &st
))
871 stat_diag (*archive_name_cursor
);
874 return S_ISBLK (st
.st_mode
) || S_ISCHR (st
.st_mode
);
878 short_read (size_t status
)
880 size_t left
; /* bytes left */
881 char *more
; /* pointer to next byte to read */
883 more
= record_start
->buffer
+ status
;
884 left
= record_size
- status
;
886 if (left
&& left
% BLOCKSIZE
== 0
888 && record_start_block
== 0 && status
!= 0
889 && archive_is_dev ())
891 unsigned long rsize
= status
/ BLOCKSIZE
;
893 ngettext ("Record size = %lu block",
894 "Record size = %lu blocks",
899 while (left
% BLOCKSIZE
!= 0
900 || (left
&& status
&& read_full_records
))
903 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
904 archive_read_error ();
909 if (! read_full_records
)
911 unsigned long rest
= record_size
- left
;
914 ngettext ("Unaligned block (%lu byte) in archive",
915 "Unaligned block (%lu bytes) in archive",
924 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
928 /* Flush the current buffer to/from the archive. */
932 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
933 record_start_block
+= record_end
- record_start
;
934 current_block
= record_start
;
935 record_end
= record_start
+ blocking_factor
;
937 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
939 access_mode
= ACCESS_WRITE
;
940 time_to_start_writing
= false;
951 flush_write_ptr (buffer_level
);
959 /* Backspace the archive descriptor by one record worth. If it's a
960 tape, MTIOCTOP will work. If it's something else, try to seek on
961 it. If we can't seek, we lose! */
963 backspace_output (void)
967 struct mtop operation
;
969 operation
.mt_op
= MTBSR
;
970 operation
.mt_count
= 1;
971 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
973 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
979 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
981 /* Seek back to the beginning of this record and start writing there. */
983 position
-= record_size
;
986 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
988 /* Lseek failed. Try a different method. */
991 _("Cannot backspace archive file; it may be unreadable without -i")));
993 /* Replace the first part of the record with NULs. */
995 if (record_start
->buffer
!= output_start
)
996 memset (record_start
->buffer
, 0,
997 output_start
- record_start
->buffer
);
1003 seek_archive (off_t size
)
1005 off_t start
= current_block_ordinal ();
1008 off_t skipped
= (blocking_factor
- (current_block
- record_start
))
1011 if (size
<= skipped
)
1014 /* Compute number of records to skip */
1015 nrec
= (size
- skipped
) / record_size
;
1018 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
1022 if (offset
% record_size
)
1023 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
1025 /* Convert to number of records */
1026 offset
/= BLOCKSIZE
;
1027 /* Compute number of skipped blocks */
1028 nblk
= offset
- start
;
1030 /* Update buffering info */
1031 records_read
+= nblk
/ blocking_factor
;
1032 record_start_block
= offset
- blocking_factor
;
1033 current_block
= record_end
;
1038 /* Close the archive file. */
1040 close_archive (void)
1042 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1045 if (current_block
> record_start
)
1049 compute_duration ();
1053 if (rmtclose (archive
) != 0)
1054 close_error (*archive_name_cursor
);
1056 sys_wait_for_child (child_pid
, hit_eof
);
1058 tar_stat_destroy (¤t_stat_info
);
1059 free (record_buffer
[0]);
1060 free (record_buffer
[1]);
1064 /* Called to initialize the global volume number. */
1066 init_volume_number (void)
1068 FILE *file
= fopen (volno_file_option
, "r");
1072 if (fscanf (file
, "%d", &global_volno
) != 1
1073 || global_volno
< 0)
1074 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1075 quotearg_colon (volno_file_option
)));
1077 read_error (volno_file_option
);
1078 if (fclose (file
) != 0)
1079 close_error (volno_file_option
);
1081 else if (errno
!= ENOENT
)
1082 open_error (volno_file_option
);
1085 /* Called to write out the closing global volume number. */
1087 closeout_volume_number (void)
1089 FILE *file
= fopen (volno_file_option
, "w");
1093 fprintf (file
, "%d\n", global_volno
);
1095 write_error (volno_file_option
);
1096 if (fclose (file
) != 0)
1097 close_error (volno_file_option
);
1100 open_error (volno_file_option
);
1105 increase_volume_number (void)
1108 if (global_volno
< 0)
1109 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1114 change_tape_menu (FILE *read_file
)
1116 char *input_buffer
= NULL
;
1122 fputc ('\007', stderr
);
1124 _("Prepare volume #%d for %s and hit return: "),
1125 global_volno
+ 1, quote (*archive_name_cursor
));
1128 if (getline (&input_buffer
, &size
, read_file
) <= 0)
1130 WARN ((0, 0, _("EOF where user reply was expected")));
1132 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1133 && subcommand_option
!= LIST_SUBCOMMAND
1134 && subcommand_option
!= DIFF_SUBCOMMAND
)
1135 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1140 if (input_buffer
[0] == '\n'
1141 || input_buffer
[0] == 'y'
1142 || input_buffer
[0] == 'Y')
1145 switch (input_buffer
[0])
1149 fprintf (stderr
, _("\
1150 n name Give a new file name for the next (and subsequent) volume(s)\n\
1152 y or newline Continue operation\n"));
1153 if (!restrict_option
)
1154 fprintf (stderr
, _(" ! Spawn a subshell\n"));
1155 fprintf (stderr
, _(" ? Print this list\n"));
1162 WARN ((0, 0, _("No new volume; exiting.\n")));
1164 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1165 && subcommand_option
!= LIST_SUBCOMMAND
1166 && subcommand_option
!= DIFF_SUBCOMMAND
)
1167 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1172 /* Get new file name. */
1178 for (name
= input_buffer
+ 1;
1179 *name
== ' ' || *name
== '\t';
1183 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1189 /* FIXME: the following allocation is never reclaimed. */
1190 *archive_name_cursor
= xstrdup (name
);
1194 fprintf (stderr
, "%s",
1195 _("File name not specified. Try again.\n"));
1200 if (!restrict_option
)
1208 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
1211 free (input_buffer
);
1214 /* We've hit the end of the old volume. Close it and open the next one.
1215 Return nonzero on success.
1218 new_volume (enum access_mode mode
)
1220 static FILE *read_file
;
1224 if (!read_file
&& !info_script_option
)
1225 /* FIXME: if fopen is used, it will never be closed. */
1226 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1233 assign_string (&volume_label
, NULL
);
1234 assign_string (&continued_file_name
, NULL
);
1235 continued_file_size
= continued_file_offset
= 0;
1236 current_block
= record_start
;
1238 if (rmtclose (archive
) != 0)
1239 close_error (*archive_name_cursor
);
1241 archive_name_cursor
++;
1242 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1244 archive_name_cursor
= archive_name_array
;
1252 /* We have to prompt from now on. */
1254 if (info_script_option
)
1256 if (volno_file_option
)
1257 closeout_volume_number ();
1258 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1259 FATAL_ERROR ((0, 0, _("%s command failed"),
1260 quote (info_script_option
)));
1263 change_tape_menu (read_file
);
1266 if (strcmp (archive_name_cursor
[0], "-") == 0)
1268 read_full_records
= true;
1269 archive
= STDIN_FILENO
;
1271 else if (verify_option
)
1272 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1273 rsh_command_option
);
1278 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1279 rsh_command_option
);
1280 guess_seekable_archive ();
1285 maybe_backup_file (*archive_name_cursor
, 1);
1286 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1287 rsh_command_option
);
1291 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1292 rsh_command_option
);
1298 open_warn (*archive_name_cursor
);
1299 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1300 undo_last_backup ();
1305 SET_BINARY_MODE (archive
);
1311 read_header0 (struct tar_stat_info
*info
)
1313 enum read_header rc
;
1315 tar_stat_init (info
);
1316 rc
= read_header (¤t_header
, info
, read_header_auto
);
1317 if (rc
== HEADER_SUCCESS
)
1319 set_next_block_after (current_header
);
1322 ERROR ((0, 0, _("This does not look like a tar archive")));
1327 try_new_volume (void)
1330 union block
*header
;
1331 enum access_mode acc
;
1333 switch (subcommand_option
)
1335 case APPEND_SUBCOMMAND
:
1336 case CAT_SUBCOMMAND
:
1337 case UPDATE_SUBCOMMAND
:
1338 acc
= ACCESS_UPDATE
;
1346 if (!new_volume (acc
))
1349 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1351 archive_read_error ();
1353 if (status
!= record_size
)
1354 short_read (status
);
1356 header
= find_next_block ();
1360 switch (header
->header
.typeflag
)
1364 tar_stat_init (&dummy
);
1365 if (read_header (&header
, &dummy
, read_header_x_global
)
1366 != HEADER_SUCCESS_EXTENDED
)
1368 ERROR ((0, 0, _("This does not look like a tar archive")));
1372 xheader_decode (&dummy
); /* decodes values from the global header */
1373 tar_stat_destroy (&dummy
);
1375 /* The initial global header must be immediately followed by
1376 an extended PAX header for the first member in this volume.
1377 However, in some cases tar may split volumes in the middle
1378 of a PAX header. This is incorrect, and should be fixed
1379 in the future versions. In the meantime we must be
1380 prepared to correctly list and extract such archives.
1382 If this happens, the following call to read_header returns
1383 HEADER_FAILURE, which is ignored.
1385 See also tests/multiv07.at */
1387 switch (read_header (&header
, &dummy
, read_header_auto
))
1389 case HEADER_SUCCESS
:
1390 set_next_block_after (header
);
1393 case HEADER_FAILURE
:
1397 ERROR ((0, 0, _("This does not look like a tar archive")));
1403 case GNUTYPE_VOLHDR
:
1404 if (!read_header0 (&dummy
))
1406 tar_stat_destroy (&dummy
);
1407 assign_string (&volume_label
, current_header
->header
.name
);
1408 set_next_block_after (header
);
1409 header
= find_next_block ();
1410 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1414 case GNUTYPE_MULTIVOL
:
1415 if (!read_header0 (&dummy
))
1417 tar_stat_destroy (&dummy
);
1418 assign_string (&continued_file_name
, current_header
->header
.name
);
1419 continued_file_size
=
1420 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1421 continued_file_offset
=
1422 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1432 if (!continued_file_name
1433 || strcmp (continued_file_name
, bufmap_head
->file_name
))
1435 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1436 && strlen (bufmap_head
->file_name
) >= NAME_FIELD_SIZE
1437 && strncmp (continued_file_name
, bufmap_head
->file_name
,
1438 NAME_FIELD_SIZE
) == 0)
1440 _("%s is possibly continued on this volume: header contains truncated name"),
1441 quote (bufmap_head
->file_name
)));
1444 WARN ((0, 0, _("%s is not continued on this volume"),
1445 quote (bufmap_head
->file_name
)));
1450 s
= continued_file_size
+ continued_file_offset
;
1452 if (bufmap_head
->sizetotal
!= s
|| s
< continued_file_offset
)
1454 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1455 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1456 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1458 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1459 quote (continued_file_name
),
1460 STRINGIFY_BIGINT (bufmap_head
->sizetotal
, totsizebuf
),
1461 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1462 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1466 if (bufmap_head
->sizetotal
- bufmap_head
->sizeleft
!=
1467 continued_file_offset
)
1469 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1470 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1471 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1473 WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
1474 STRINGIFY_BIGINT (bufmap_head
->sizetotal
, totsizebuf
),
1475 STRINGIFY_BIGINT (bufmap_head
->sizeleft
, s1buf
),
1476 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1482 increase_volume_number ();
1487 #define VOLUME_TEXT " Volume "
1488 #define VOLUME_TEXT_LEN (sizeof VOLUME_TEXT - 1)
1491 drop_volume_label_suffix (const char *label
)
1494 size_t len
= strlen (label
);
1499 for (p
= label
+ len
- 1; p
> label
&& isdigit ((unsigned char) *p
); p
--)
1501 if (p
> label
&& p
- (VOLUME_TEXT_LEN
- 1) > label
)
1503 p
-= VOLUME_TEXT_LEN
- 1;
1504 if (memcmp (p
, VOLUME_TEXT
, VOLUME_TEXT_LEN
) == 0)
1506 char *s
= xmalloc ((len
= p
- label
) + 1);
1507 memcpy (s
, label
, len
);
1516 /* Check LABEL against the volume label, seen as a globbing
1517 pattern. Return true if the pattern matches. In case of failure,
1518 retry matching a volume sequence number before giving up in
1519 multi-volume mode. */
1521 check_label_pattern (const char *label
)
1524 bool result
= false;
1526 if (fnmatch (volume_label_option
, label
, 0) == 0)
1529 if (!multi_volume_option
)
1532 string
= drop_volume_label_suffix (label
);
1535 result
= fnmatch (string
, volume_label_option
, 0) == 0;
1541 /* Check if the next block contains a volume label and if this matches
1542 the one given in the command line */
1544 match_volume_label (void)
1548 union block
*label
= find_next_block ();
1551 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1552 quote (volume_label_option
)));
1553 if (label
->header
.typeflag
== GNUTYPE_VOLHDR
)
1555 if (memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1556 assign_string (&volume_label
, label
->header
.name
);
1559 volume_label
= xmalloc (sizeof (label
->header
.name
) + 1);
1560 memcpy (volume_label
, label
->header
.name
,
1561 sizeof (label
->header
.name
));
1562 volume_label
[sizeof (label
->header
.name
)] = 0;
1565 else if (label
->header
.typeflag
== XGLTYPE
)
1567 struct tar_stat_info st
;
1568 tar_stat_init (&st
);
1569 xheader_read (&st
.xhdr
, label
,
1570 OFF_FROM_HEADER (label
->header
.size
));
1571 xheader_decode (&st
);
1572 tar_stat_destroy (&st
);
1577 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1578 quote (volume_label_option
)));
1580 if (!check_label_pattern (volume_label
))
1581 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1582 quote_n (0, volume_label
),
1583 quote_n (1, volume_label_option
)));
1586 /* Mark the archive with volume label STR. */
1588 _write_volume_label (const char *str
)
1590 if (archive_format
== POSIX_FORMAT
)
1591 xheader_store ("GNU.volume.label", &dummy
, str
);
1594 union block
*label
= find_next_block ();
1596 memset (label
, 0, BLOCKSIZE
);
1598 strcpy (label
->header
.name
, str
);
1599 assign_string (¤t_stat_info
.file_name
,
1600 label
->header
.name
);
1601 current_stat_info
.had_trailing_slash
=
1602 strip_trailing_slashes (current_stat_info
.file_name
);
1604 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1605 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1606 finish_header (¤t_stat_info
, label
, -1);
1607 set_next_block_after (label
);
1611 #define VOL_SUFFIX "Volume"
1613 /* Add a volume label to a part of multi-volume archive */
1615 add_volume_label (void)
1617 char buf
[UINTMAX_STRSIZE_BOUND
];
1618 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1619 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1621 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1622 _write_volume_label (s
);
1627 add_chunk_header (struct bufmap
*map
)
1629 if (archive_format
== POSIX_FORMAT
)
1631 off_t block_ordinal
;
1633 struct tar_stat_info st
;
1635 memset (&st
, 0, sizeof st
);
1636 st
.orig_file_name
= st
.file_name
= map
->file_name
;
1637 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1638 st
.stat
.st_uid
= getuid ();
1639 st
.stat
.st_gid
= getgid ();
1640 st
.orig_file_name
= xheader_format_name (&st
,
1641 "%d/GNUFileParts.%p/%f.%n",
1643 st
.file_name
= st
.orig_file_name
;
1644 st
.archive_file_size
= st
.stat
.st_size
= map
->sizeleft
;
1646 block_ordinal
= current_block_ordinal ();
1647 blk
= start_header (&st
);
1649 abort (); /* FIXME */
1650 finish_header (&st
, blk
, block_ordinal
);
1651 free (st
.orig_file_name
);
1656 /* Add a volume label to the current archive */
1658 write_volume_label (void)
1660 if (multi_volume_option
)
1661 add_volume_label ();
1663 _write_volume_label (volume_label_option
);
1666 /* Write GNU multi-volume header */
1668 gnu_add_multi_volume_header (struct bufmap
*map
)
1671 union block
*block
= find_next_block ();
1673 if (strlen (map
->file_name
) > NAME_FIELD_SIZE
)
1675 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1676 quotearg_colon (map
->file_name
)));
1678 memset (block
, 0, BLOCKSIZE
);
1680 strncpy (block
->header
.name
, map
->file_name
, NAME_FIELD_SIZE
);
1681 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1683 OFF_TO_CHARS (map
->sizeleft
, block
->header
.size
);
1684 OFF_TO_CHARS (map
->sizetotal
- map
->sizeleft
,
1685 block
->oldgnu_header
.offset
);
1687 tmp
= verbose_option
;
1689 finish_header (¤t_stat_info
, block
, -1);
1690 verbose_option
= tmp
;
1691 set_next_block_after (block
);
1694 /* Add a multi volume header to the current archive. The exact header format
1695 depends on the archive format. */
1697 add_multi_volume_header (struct bufmap
*map
)
1699 if (archive_format
== POSIX_FORMAT
)
1701 off_t d
= map
->sizetotal
- map
->sizeleft
;
1702 xheader_store ("GNU.volume.filename", &dummy
, map
->file_name
);
1703 xheader_store ("GNU.volume.size", &dummy
, &map
->sizeleft
);
1704 xheader_store ("GNU.volume.offset", &dummy
, &d
);
1707 gnu_add_multi_volume_header (map
);
1711 /* Low-level flush functions */
1713 /* Simple flush read (no multi-volume or label extensions) */
1715 simple_flush_read (void)
1717 size_t status
; /* result from system call */
1719 checkpoint_run (false);
1721 /* Clear the count of errors. This only applies to a single call to
1724 read_error_count
= 0; /* clear error count */
1726 if (write_archive_to_stdout
&& record_start_block
!= 0)
1728 archive
= STDOUT_FILENO
;
1729 status
= sys_write_archive_buffer ();
1730 archive
= STDIN_FILENO
;
1731 if (status
!= record_size
)
1732 archive_write_error (status
);
1737 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1738 if (status
== record_size
)
1743 if (status
== SAFE_READ_ERROR
)
1745 archive_read_error ();
1746 continue; /* try again */
1750 short_read (status
);
1753 /* Simple flush write (no multi-volume or label extensions) */
1755 simple_flush_write (size_t level
__attribute__((unused
)))
1759 status
= _flush_write ();
1760 if (status
!= record_size
)
1761 archive_write_error (status
);
1765 bytes_written
+= status
;
1770 /* GNU flush functions. These support multi-volume and archive labels in
1771 GNU and PAX archive formats. */
1774 _gnu_flush_read (void)
1776 size_t status
; /* result from system call */
1778 checkpoint_run (false);
1780 /* Clear the count of errors. This only applies to a single call to
1783 read_error_count
= 0; /* clear error count */
1785 if (write_archive_to_stdout
&& record_start_block
!= 0)
1787 archive
= STDOUT_FILENO
;
1788 status
= sys_write_archive_buffer ();
1789 archive
= STDIN_FILENO
;
1790 if (status
!= record_size
)
1791 archive_write_error (status
);
1796 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1797 if (status
== record_size
)
1803 /* The condition below used to include
1804 || (status > 0 && !read_full_records)
1805 This is incorrect since even if new_volume() succeeds, the
1806 subsequent call to rmtread will overwrite the chunk of data
1807 already read in the buffer, so the processing will fail */
1809 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1810 && multi_volume_option
)
1812 while (!try_new_volume ())
1814 if (current_block
== record_end
)
1815 /* Necessary for blocking_factor == 1 */
1819 else if (status
== SAFE_READ_ERROR
)
1821 archive_read_error ();
1826 short_read (status
);
1830 gnu_flush_read (void)
1832 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1834 flush_read_ptr
= gnu_flush_read
;
1838 _gnu_flush_write (size_t buffer_level
)
1841 union block
*header
;
1847 status
= _flush_write ();
1848 if (status
!= record_size
&& !multi_volume_option
)
1849 archive_write_error (status
);
1854 bytes_written
+= status
;
1857 if (status
== record_size
)
1862 map
= bufmap_locate (status
);
1864 if (status
% BLOCKSIZE
)
1866 ERROR ((0, 0, _("write did not end on a block boundary")));
1867 archive_write_error (status
);
1870 /* In multi-volume mode. */
1871 /* ENXIO is for the UNIX PC. */
1872 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1873 archive_write_error (status
);
1875 if (!new_volume (ACCESS_WRITE
))
1878 tar_stat_destroy (&dummy
);
1880 increase_volume_number ();
1881 prev_written
+= bytes_written
;
1884 copy_ptr
= record_start
->buffer
+ status
;
1885 copy_size
= buffer_level
- status
;
1887 /* Switch to the next buffer */
1888 record_index
= !record_index
;
1893 if (volume_label_option
)
1894 add_volume_label ();
1897 add_multi_volume_header (map
);
1899 write_extended (true, &dummy
, find_next_block ());
1900 tar_stat_destroy (&dummy
);
1903 add_chunk_header (map
);
1904 header
= find_next_block ();
1905 bufmap_reset (map
, header
- record_start
);
1906 bufsize
= available_space_after (header
);
1908 while (bufsize
< copy_size
)
1910 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1911 copy_ptr
+= bufsize
;
1912 copy_size
-= bufsize
;
1913 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1914 header
= find_next_block ();
1915 bufsize
= available_space_after (header
);
1917 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1918 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1919 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1924 gnu_flush_write (size_t buffer_level
)
1926 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1927 _gnu_flush_write (buffer_level
);
1928 flush_write_ptr
= gnu_flush_write
;
1940 flush_write_ptr (record_size
);
1944 open_archive (enum access_mode wanted_access
)
1946 flush_read_ptr
= gnu_flush_read
;
1947 flush_write_ptr
= gnu_flush_write
;
1949 _open_archive (wanted_access
);
1950 switch (wanted_access
)
1954 if (volume_label_option
)
1955 match_volume_label ();
1959 records_written
= 0;
1960 if (volume_label_option
)
1961 write_volume_label ();
1964 set_volume_start_time ();