1 /* Buffer management for tar.
3 Copyright 1988, 1992-1994, 1996-1997, 1999-2010, 2013-2014 Free
4 Software Foundation, Inc.
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
);
262 /* Compression detection */
265 ct_none
, /* Unknown compression type */
266 ct_tar
, /* Plain tar file */
276 static enum compress_type archive_compression_type
= ct_none
;
280 enum compress_type type
;
287 enum compress_type type
;
292 static struct zip_magic
const magic
[] = {
295 { ct_compress
, 2, "\037\235" },
296 { ct_gzip
, 2, "\037\213" },
297 { ct_bzip2
, 3, "BZh" },
298 { ct_lzip
, 4, "LZIP" },
299 { ct_lzma
, 6, "\xFFLZMA" },
300 { ct_lzop
, 4, "\211LZO" },
301 { ct_xz
, 6, "\xFD" "7zXZ" },
304 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
306 static struct zip_program zip_program
[] = {
307 { ct_compress
, COMPRESS_PROGRAM
, "-Z" },
308 { ct_compress
, GZIP_PROGRAM
, "-z" },
309 { ct_gzip
, GZIP_PROGRAM
, "-z" },
310 { ct_bzip2
, BZIP2_PROGRAM
, "-j" },
311 { ct_bzip2
, "lbzip2", "-j" },
312 { ct_lzip
, LZIP_PROGRAM
, "--lzip" },
313 { ct_lzma
, LZMA_PROGRAM
, "--lzma" },
314 { ct_lzma
, XZ_PROGRAM
, "-J" },
315 { ct_lzop
, LZOP_PROGRAM
, "--lzop" },
316 { ct_xz
, XZ_PROGRAM
, "-J" },
320 static struct zip_program
const *
321 find_zip_program (enum compress_type type
, int *pstate
)
325 for (i
= *pstate
; zip_program
[i
].type
!= ct_none
; i
++)
327 if (zip_program
[i
].type
== type
)
330 return zip_program
+ i
;
338 first_decompress_program (int *pstate
)
340 struct zip_program
const *zp
;
342 if (use_compress_program_option
)
343 return use_compress_program_option
;
345 if (archive_compression_type
== ct_none
)
349 zp
= find_zip_program (archive_compression_type
, pstate
);
350 return zp
? zp
->program
: NULL
;
354 next_decompress_program (int *pstate
)
356 struct zip_program
const *zp
;
358 if (use_compress_program_option
)
360 zp
= find_zip_program (archive_compression_type
, pstate
);
361 return zp
? zp
->program
: NULL
;
365 compress_option (enum compress_type type
)
367 struct zip_program
const *zp
;
369 zp
= find_zip_program (type
, &i
);
370 return zp
? zp
->option
: NULL
;
373 /* Check if the file ARCHIVE is a compressed archive. */
374 static enum compress_type
375 check_compressed_archive (bool *pshort
)
377 struct zip_magic
const *p
;
384 /* Prepare global data needed for find_next_block: */
385 record_end
= record_start
; /* set up for 1st record = # 0 */
386 sfr
= read_full_records
;
387 read_full_records
= true; /* Suppress fatal error on reading a partial
389 *pshort
= find_next_block () == 0;
391 /* Restore global values */
392 read_full_records
= sfr
;
394 if ((strcmp (record_start
->header
.magic
, TMAGIC
) == 0 ||
395 strcmp (record_start
->buffer
+ offsetof (struct posix_header
, magic
),
396 OLDGNU_MAGIC
) == 0) &&
397 tar_checksum (record_start
, true) == HEADER_SUCCESS
)
398 /* Probably a valid header */
401 for (p
= magic
+ 2; p
< magic
+ NMAGIC
; p
++)
402 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
408 /* Guess if the archive is seekable. */
410 guess_seekable_archive (void)
414 if (subcommand_option
== DELETE_SUBCOMMAND
)
416 /* The current code in delete.c is based on the assumption that
417 skip_member() reads all data from the archive. So, we should
418 make sure it won't use seeks. On the other hand, the same code
419 depends on the ability to backspace a record in the archive,
420 so setting seekable_archive to false is technically incorrect.
421 However, it is tested only in skip_member(), so it's not a
423 seekable_archive
= false;
426 if (seek_option
!= -1)
428 seekable_archive
= !!seek_option
;
432 if (!multi_volume_option
&& !use_compress_program_option
433 && fstat (archive
, &st
) == 0)
434 seekable_archive
= S_ISREG (st
.st_mode
);
436 seekable_archive
= false;
439 /* Open an archive named archive_name_array[0]. Detect if it is
440 a compressed archive of known type and use corresponding decompression
443 open_compressed_archive (void)
445 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
446 MODE_RW
, rsh_command_option
);
450 if (!multi_volume_option
)
452 if (!use_compress_program_option
)
455 enum compress_type type
= check_compressed_archive (&shortfile
);
461 ERROR ((0, 0, _("This does not look like a tar archive")));
466 ERROR ((0, 0, _("This does not look like a tar archive")));
467 set_compression_program_by_suffix (archive_name_array
[0], NULL
);
468 if (!use_compress_program_option
)
473 archive_compression_type
= type
;
478 /* FD is not needed any more */
481 hit_eof
= false; /* It might have been set by find_next_block in
482 check_compressed_archive */
484 /* Open compressed archive */
485 child_pid
= sys_child_open_for_uncompress ();
486 read_full_records
= true;
490 record_end
= record_start
; /* set up for 1st record = # 0 */
496 print_stats (FILE *fp
, const char *text
, tarlong numbytes
)
498 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
499 char rate
[LONGEST_HUMAN_READABLE
+ 1];
502 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
505 n
+= fprintf (fp
, "%s: ", gettext (text
));
506 return n
+ fprintf (fp
, TARLONG_FORMAT
" (%s, %s/s)",
508 human_readable (numbytes
, abbr
, human_opts
, 1, 1),
509 (0 < duration
&& numbytes
/ duration
< (uintmax_t) -1
510 ? human_readable (numbytes
/ duration
, rate
, human_opts
, 1, 1)
514 /* Format totals to file FP. FORMATS is an array of strings to output
515 before each data item (bytes read, written, deleted, in that order).
516 EOR is a delimiter to output after each item (used only if deleting
517 from the archive), EOL is a delimiter to add at the end of the output
520 format_total_stats (FILE *fp
, const char **formats
, int eor
, int eol
)
524 switch (subcommand_option
)
526 case CREATE_SUBCOMMAND
:
528 case UPDATE_SUBCOMMAND
:
529 case APPEND_SUBCOMMAND
:
530 n
= print_stats (fp
, formats
[TF_WRITE
],
531 prev_written
+ bytes_written
);
534 case DELETE_SUBCOMMAND
:
536 char buf
[UINTMAX_STRSIZE_BOUND
];
537 n
= print_stats (fp
, formats
[TF_READ
],
538 records_read
* record_size
);
543 n
+= print_stats (fp
, formats
[TF_WRITE
],
544 prev_written
+ bytes_written
);
549 if (formats
[TF_DELETED
] && formats
[TF_DELETED
][0])
550 n
+= fprintf (fp
, "%s: ", gettext (formats
[TF_DELETED
]));
551 n
+= fprintf (fp
, "%s",
552 STRINGIFY_BIGINT ((records_read
- records_skipped
)
554 - (prev_written
+ bytes_written
), buf
));
558 case EXTRACT_SUBCOMMAND
:
559 case LIST_SUBCOMMAND
:
560 case DIFF_SUBCOMMAND
:
561 n
= print_stats (fp
, _(formats
[TF_READ
]),
562 records_read
* record_size
);
576 const char *default_total_format
[] = {
577 N_("Total bytes read"),
578 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
579 N_("Total bytes written"),
580 N_("Total bytes deleted")
584 print_total_stats (void)
586 format_total_stats (stderr
, default_total_format
, '\n', '\n');
589 /* Compute and return the block ordinal at current_block. */
591 current_block_ordinal (void)
593 return record_start_block
+ (current_block
- record_start
);
596 /* If the EOF flag is set, reset it, as well as current_block, etc. */
603 current_block
= record_start
;
604 record_end
= record_start
+ blocking_factor
;
605 access_mode
= ACCESS_WRITE
;
609 /* Return the location of the next available input or output block.
610 Return zero for EOF. Once we have returned zero, we just keep returning
611 it, to avoid accidentally going on to the next file on the tape. */
613 find_next_block (void)
615 if (current_block
== record_end
)
620 if (current_block
== record_end
)
626 return current_block
;
629 /* Indicate that we have used all blocks up thru BLOCK. */
631 set_next_block_after (union block
*block
)
633 while (block
>= current_block
)
636 /* Do *not* flush the archive here. If we do, the same argument to
637 set_next_block_after could mean the next block (if the input record
638 is exactly one block long), which is not what is intended. */
640 if (current_block
> record_end
)
644 /* Return the number of bytes comprising the space between POINTER
645 through the end of the current buffer of blocks. This space is
646 available for filling with data, or taking data from. POINTER is
647 usually (but not always) the result of previous find_next_block call. */
649 available_space_after (union block
*pointer
)
651 return record_end
->buffer
- pointer
->buffer
;
654 /* Close file having descriptor FD, and abort if close unsuccessful. */
659 close_error (_("(pipe)"));
665 if (! record_buffer_aligned
[record_index
])
666 record_buffer_aligned
[record_index
] =
667 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
669 record_start
= record_buffer_aligned
[record_index
];
670 current_block
= record_start
;
671 record_end
= record_start
+ blocking_factor
;
675 check_tty (enum access_mode mode
)
677 /* Refuse to read archive from and write it to a tty. */
678 if (strcmp (archive_name_array
[0], "-") == 0
679 && isatty (mode
== ACCESS_READ
? STDIN_FILENO
: STDOUT_FILENO
))
683 ? _("Refusing to read archive contents from terminal "
684 "(missing -f option?)")
685 : _("Refusing to write archive contents to terminal "
686 "(missing -f option?)")));
690 /* Open an archive file. The argument specifies whether we are
691 reading or writing, or both. */
693 _open_archive (enum access_mode wanted_access
)
695 int backed_up_flag
= 0;
697 if (record_size
== 0)
698 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
700 if (archive_names
== 0)
701 FATAL_ERROR ((0, 0, _("No archive name given")));
703 tar_stat_destroy (¤t_stat_info
);
708 /* When updating the archive, we start with reading. */
709 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
710 check_tty (access_mode
);
712 read_full_records
= read_full_records_option
;
716 if (use_compress_program_option
)
718 switch (wanted_access
)
721 child_pid
= sys_child_open_for_uncompress ();
722 read_full_records
= true;
723 record_end
= record_start
; /* set up for 1st record = # 0 */
727 child_pid
= sys_child_open_for_compress ();
731 abort (); /* Should not happen */
736 && wanted_access
== ACCESS_WRITE
737 && strcmp (archive_name_array
[0], "-") == 0)
740 else if (strcmp (archive_name_array
[0], "-") == 0)
742 read_full_records
= true; /* could be a pipe, be safe */
744 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
746 switch (wanted_access
)
751 enum compress_type type
;
753 archive
= STDIN_FILENO
;
754 type
= check_compressed_archive (&shortfile
);
755 if (type
!= ct_tar
&& type
!= ct_none
)
757 _("Archive is compressed. Use %s option"),
758 compress_option (type
)));
760 ERROR ((0, 0, _("This does not look like a tar archive")));
765 archive
= STDOUT_FILENO
;
766 if (!index_file_name
)
771 archive
= STDIN_FILENO
;
772 write_archive_to_stdout
= true;
773 record_end
= record_start
; /* set up for 1st record = # 0 */
774 if (!index_file_name
)
780 switch (wanted_access
)
783 archive
= open_compressed_archive ();
785 guess_seekable_archive ();
791 maybe_backup_file (archive_name_array
[0], 1);
795 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
796 MODE_RW
, rsh_command_option
);
798 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
803 archive
= rmtopen (archive_name_array
[0],
804 O_RDWR
| O_CREAT
| O_BINARY
,
805 MODE_RW
, rsh_command_option
);
807 switch (check_compressed_archive (NULL
))
815 _("Cannot update compressed archives")));
821 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
823 int saved_errno
= errno
;
828 open_fatal (archive_name_array
[0]);
831 sys_detect_dev_null_output ();
832 sys_save_archive_dev_ino ();
833 SET_BINARY_MODE (archive
);
835 switch (wanted_access
)
838 find_next_block (); /* read it in, check for EOF */
848 /* Perform a write to flush the buffer. */
854 checkpoint_run (true);
855 if (tape_length_option
&& tape_length_option
<= bytes_written
)
860 else if (dev_null_output
)
861 status
= record_size
;
863 status
= sys_write_archive_buffer ();
865 if (status
&& multi_volume_option
&& !inhibit_map
)
867 struct bufmap
*map
= bufmap_locate (status
);
870 size_t delta
= status
- map
->start
* BLOCKSIZE
;
871 if (delta
> map
->sizeleft
)
872 delta
= map
->sizeleft
;
873 map
->sizeleft
-= delta
;
874 if (map
->sizeleft
== 0)
876 bufmap_reset (map
, map
? (- map
->start
) : 0);
882 /* Handle write errors on the archive. Write errors are always fatal.
883 Hitting the end of a volume does not cause a write error unless the
884 write was the first record of the volume. */
886 archive_write_error (ssize_t status
)
888 /* It might be useful to know how much was written before the error
893 print_total_stats ();
897 write_fatal_details (*archive_name_cursor
, status
, record_size
);
900 /* Handle read errors on the archive. If the read should be retried,
901 return to the caller. */
903 archive_read_error (void)
905 read_error (*archive_name_cursor
);
907 if (record_start_block
== 0)
908 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
910 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
911 then give up on reading the archive. */
913 if (read_error_count
++ > READ_ERROR_MAX
)
914 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
919 archive_is_dev (void)
923 if (fstat (archive
, &st
))
925 stat_diag (*archive_name_cursor
);
928 return S_ISBLK (st
.st_mode
) || S_ISCHR (st
.st_mode
);
932 short_read (size_t status
)
934 size_t left
; /* bytes left */
935 char *more
; /* pointer to next byte to read */
937 more
= record_start
->buffer
+ status
;
938 left
= record_size
- status
;
940 if (left
&& left
% BLOCKSIZE
== 0
941 && (warning_option
& WARN_RECORD_SIZE
)
942 && record_start_block
== 0 && status
!= 0
943 && archive_is_dev ())
945 unsigned long rsize
= status
/ BLOCKSIZE
;
947 ngettext ("Record size = %lu block",
948 "Record size = %lu blocks",
953 while (left
% BLOCKSIZE
!= 0
954 || (left
&& status
&& read_full_records
))
957 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
958 archive_read_error ();
963 if (! read_full_records
)
965 unsigned long rest
= record_size
- left
;
968 ngettext ("Unaligned block (%lu byte) in archive",
969 "Unaligned block (%lu bytes) in archive",
978 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
982 /* Flush the current buffer to/from the archive. */
986 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
987 record_start_block
+= record_end
- record_start
;
988 current_block
= record_start
;
989 record_end
= record_start
+ blocking_factor
;
991 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
993 access_mode
= ACCESS_WRITE
;
994 time_to_start_writing
= false;
1005 flush_write_ptr (buffer_level
);
1013 /* Backspace the archive descriptor by one record worth. If it's a
1014 tape, MTIOCTOP will work. If it's something else, try to seek on
1015 it. If we can't seek, we lose! */
1017 backspace_output (void)
1021 struct mtop operation
;
1023 operation
.mt_op
= MTBSR
;
1024 operation
.mt_count
= 1;
1025 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1027 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
1033 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
1035 /* Seek back to the beginning of this record and start writing there. */
1037 position
-= record_size
;
1040 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
1042 /* Lseek failed. Try a different method. */
1045 _("Cannot backspace archive file; it may be unreadable without -i")));
1047 /* Replace the first part of the record with NULs. */
1049 if (record_start
->buffer
!= output_start
)
1050 memset (record_start
->buffer
, 0,
1051 output_start
- record_start
->buffer
);
1057 seek_archive (off_t size
)
1059 off_t start
= current_block_ordinal ();
1062 off_t skipped
= (blocking_factor
- (current_block
- record_start
))
1065 if (size
<= skipped
)
1068 /* Compute number of records to skip */
1069 nrec
= (size
- skipped
) / record_size
;
1072 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
1076 if (offset
% record_size
)
1077 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
1079 /* Convert to number of records */
1080 offset
/= BLOCKSIZE
;
1081 /* Compute number of skipped blocks */
1082 nblk
= offset
- start
;
1084 /* Update buffering info */
1085 records_read
+= nblk
/ blocking_factor
;
1086 record_start_block
= offset
- blocking_factor
;
1087 current_block
= record_end
;
1092 /* Close the archive file. */
1094 close_archive (void)
1096 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
1099 if (current_block
> record_start
)
1103 compute_duration ();
1107 if (rmtclose (archive
) != 0)
1108 close_error (*archive_name_cursor
);
1110 sys_wait_for_child (child_pid
, hit_eof
);
1112 tar_stat_destroy (¤t_stat_info
);
1113 free (record_buffer
[0]);
1114 free (record_buffer
[1]);
1118 /* Called to initialize the global volume number. */
1120 init_volume_number (void)
1122 FILE *file
= fopen (volno_file_option
, "r");
1126 if (fscanf (file
, "%d", &global_volno
) != 1
1127 || global_volno
< 0)
1128 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1129 quotearg_colon (volno_file_option
)));
1131 read_error (volno_file_option
);
1132 if (fclose (file
) != 0)
1133 close_error (volno_file_option
);
1135 else if (errno
!= ENOENT
)
1136 open_error (volno_file_option
);
1139 /* Called to write out the closing global volume number. */
1141 closeout_volume_number (void)
1143 FILE *file
= fopen (volno_file_option
, "w");
1147 fprintf (file
, "%d\n", global_volno
);
1149 write_error (volno_file_option
);
1150 if (fclose (file
) != 0)
1151 close_error (volno_file_option
);
1154 open_error (volno_file_option
);
1159 increase_volume_number (void)
1162 if (global_volno
< 0)
1163 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1168 change_tape_menu (FILE *read_file
)
1170 char *input_buffer
= NULL
;
1176 fputc ('\007', stderr
);
1178 _("Prepare volume #%d for %s and hit return: "),
1179 global_volno
+ 1, quote (*archive_name_cursor
));
1182 if (getline (&input_buffer
, &size
, read_file
) <= 0)
1184 WARN ((0, 0, _("EOF where user reply was expected")));
1186 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1187 && subcommand_option
!= LIST_SUBCOMMAND
1188 && subcommand_option
!= DIFF_SUBCOMMAND
)
1189 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1194 if (input_buffer
[0] == '\n'
1195 || input_buffer
[0] == 'y'
1196 || input_buffer
[0] == 'Y')
1199 switch (input_buffer
[0])
1203 fprintf (stderr
, _("\
1204 n name Give a new file name for the next (and subsequent) volume(s)\n\
1206 y or newline Continue operation\n"));
1207 if (!restrict_option
)
1208 fprintf (stderr
, _(" ! Spawn a subshell\n"));
1209 fprintf (stderr
, _(" ? Print this list\n"));
1216 WARN ((0, 0, _("No new volume; exiting.\n")));
1218 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1219 && subcommand_option
!= LIST_SUBCOMMAND
1220 && subcommand_option
!= DIFF_SUBCOMMAND
)
1221 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1226 /* Get new file name. */
1232 for (name
= input_buffer
+ 1;
1233 *name
== ' ' || *name
== '\t';
1237 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1243 /* FIXME: the following allocation is never reclaimed. */
1244 *archive_name_cursor
= xstrdup (name
);
1248 fprintf (stderr
, "%s",
1249 _("File name not specified. Try again.\n"));
1254 if (!restrict_option
)
1262 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
1265 free (input_buffer
);
1268 /* We've hit the end of the old volume. Close it and open the next one.
1269 Return nonzero on success.
1272 new_volume (enum access_mode mode
)
1274 static FILE *read_file
;
1278 if (!read_file
&& !info_script_option
)
1279 /* FIXME: if fopen is used, it will never be closed. */
1280 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1287 assign_string (&volume_label
, NULL
);
1288 assign_string (&continued_file_name
, NULL
);
1289 continued_file_size
= continued_file_offset
= 0;
1290 current_block
= record_start
;
1292 if (rmtclose (archive
) != 0)
1293 close_error (*archive_name_cursor
);
1295 archive_name_cursor
++;
1296 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1298 archive_name_cursor
= archive_name_array
;
1306 /* We have to prompt from now on. */
1308 if (info_script_option
)
1310 if (volno_file_option
)
1311 closeout_volume_number ();
1312 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1313 FATAL_ERROR ((0, 0, _("%s command failed"),
1314 quote (info_script_option
)));
1317 change_tape_menu (read_file
);
1320 if (strcmp (archive_name_cursor
[0], "-") == 0)
1322 read_full_records
= true;
1323 archive
= STDIN_FILENO
;
1325 else if (verify_option
)
1326 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1327 rsh_command_option
);
1332 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1333 rsh_command_option
);
1334 guess_seekable_archive ();
1339 maybe_backup_file (*archive_name_cursor
, 1);
1340 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1341 rsh_command_option
);
1345 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1346 rsh_command_option
);
1352 open_warn (*archive_name_cursor
);
1353 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1354 undo_last_backup ();
1359 SET_BINARY_MODE (archive
);
1365 read_header0 (struct tar_stat_info
*info
)
1367 enum read_header rc
;
1369 tar_stat_init (info
);
1370 rc
= read_header (¤t_header
, info
, read_header_auto
);
1371 if (rc
== HEADER_SUCCESS
)
1373 set_next_block_after (current_header
);
1376 ERROR ((0, 0, _("This does not look like a tar archive")));
1381 try_new_volume (void)
1384 union block
*header
;
1385 enum access_mode acc
;
1387 switch (subcommand_option
)
1389 case APPEND_SUBCOMMAND
:
1390 case CAT_SUBCOMMAND
:
1391 case UPDATE_SUBCOMMAND
:
1392 acc
= ACCESS_UPDATE
;
1400 if (!new_volume (acc
))
1403 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1405 archive_read_error ();
1407 if (status
!= record_size
)
1408 short_read (status
);
1410 header
= find_next_block ();
1414 switch (header
->header
.typeflag
)
1418 tar_stat_init (&dummy
);
1419 if (read_header (&header
, &dummy
, read_header_x_global
)
1420 != HEADER_SUCCESS_EXTENDED
)
1422 ERROR ((0, 0, _("This does not look like a tar archive")));
1426 xheader_decode (&dummy
); /* decodes values from the global header */
1427 tar_stat_destroy (&dummy
);
1429 /* The initial global header must be immediately followed by
1430 an extended PAX header for the first member in this volume.
1431 However, in some cases tar may split volumes in the middle
1432 of a PAX header. This is incorrect, and should be fixed
1433 in the future versions. In the meantime we must be
1434 prepared to correctly list and extract such archives.
1436 If this happens, the following call to read_header returns
1437 HEADER_FAILURE, which is ignored.
1439 See also tests/multiv07.at */
1441 switch (read_header (&header
, &dummy
, read_header_auto
))
1443 case HEADER_SUCCESS
:
1444 set_next_block_after (header
);
1447 case HEADER_FAILURE
:
1451 ERROR ((0, 0, _("This does not look like a tar archive")));
1457 case GNUTYPE_VOLHDR
:
1458 if (!read_header0 (&dummy
))
1460 tar_stat_destroy (&dummy
);
1461 assign_string (&volume_label
, current_header
->header
.name
);
1462 set_next_block_after (header
);
1463 header
= find_next_block ();
1464 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1468 case GNUTYPE_MULTIVOL
:
1469 if (!read_header0 (&dummy
))
1471 tar_stat_destroy (&dummy
);
1472 assign_string (&continued_file_name
, current_header
->header
.name
);
1473 continued_file_size
=
1474 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1475 continued_file_offset
=
1476 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1486 if (!continued_file_name
1487 || strcmp (continued_file_name
, bufmap_head
->file_name
))
1489 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1490 && strlen (bufmap_head
->file_name
) >= NAME_FIELD_SIZE
1491 && strncmp (continued_file_name
, bufmap_head
->file_name
,
1492 NAME_FIELD_SIZE
) == 0)
1494 _("%s is possibly continued on this volume: header contains truncated name"),
1495 quote (bufmap_head
->file_name
)));
1498 WARN ((0, 0, _("%s is not continued on this volume"),
1499 quote (bufmap_head
->file_name
)));
1504 s
= continued_file_size
+ continued_file_offset
;
1506 if (bufmap_head
->sizetotal
!= s
|| s
< continued_file_offset
)
1508 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1509 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1510 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1512 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1513 quote (continued_file_name
),
1514 STRINGIFY_BIGINT (bufmap_head
->sizetotal
, totsizebuf
),
1515 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1516 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1520 if (bufmap_head
->sizetotal
- bufmap_head
->sizeleft
!=
1521 continued_file_offset
)
1523 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1524 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1525 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1527 WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
1528 STRINGIFY_BIGINT (bufmap_head
->sizetotal
, totsizebuf
),
1529 STRINGIFY_BIGINT (bufmap_head
->sizeleft
, s1buf
),
1530 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1536 increase_volume_number ();
1541 #define VOLUME_TEXT " Volume "
1542 #define VOLUME_TEXT_LEN (sizeof VOLUME_TEXT - 1)
1545 drop_volume_label_suffix (const char *label
)
1548 size_t len
= strlen (label
);
1553 for (p
= label
+ len
- 1; p
> label
&& isdigit ((unsigned char) *p
); p
--)
1555 if (p
> label
&& p
- (VOLUME_TEXT_LEN
- 1) > label
)
1557 p
-= VOLUME_TEXT_LEN
- 1;
1558 if (memcmp (p
, VOLUME_TEXT
, VOLUME_TEXT_LEN
) == 0)
1560 char *s
= xmalloc ((len
= p
- label
) + 1);
1561 memcpy (s
, label
, len
);
1570 /* Check LABEL against the volume label, seen as a globbing
1571 pattern. Return true if the pattern matches. In case of failure,
1572 retry matching a volume sequence number before giving up in
1573 multi-volume mode. */
1575 check_label_pattern (const char *label
)
1578 bool result
= false;
1580 if (fnmatch (volume_label_option
, label
, 0) == 0)
1583 if (!multi_volume_option
)
1586 string
= drop_volume_label_suffix (label
);
1589 result
= fnmatch (string
, volume_label_option
, 0) == 0;
1595 /* Check if the next block contains a volume label and if this matches
1596 the one given in the command line */
1598 match_volume_label (void)
1602 union block
*label
= find_next_block ();
1605 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1606 quote (volume_label_option
)));
1607 if (label
->header
.typeflag
== GNUTYPE_VOLHDR
)
1609 if (memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1610 assign_string (&volume_label
, label
->header
.name
);
1613 volume_label
= xmalloc (sizeof (label
->header
.name
) + 1);
1614 memcpy (volume_label
, label
->header
.name
,
1615 sizeof (label
->header
.name
));
1616 volume_label
[sizeof (label
->header
.name
)] = 0;
1619 else if (label
->header
.typeflag
== XGLTYPE
)
1621 struct tar_stat_info st
;
1622 tar_stat_init (&st
);
1623 xheader_read (&st
.xhdr
, label
,
1624 OFF_FROM_HEADER (label
->header
.size
));
1625 xheader_decode (&st
);
1626 tar_stat_destroy (&st
);
1631 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1632 quote (volume_label_option
)));
1634 if (!check_label_pattern (volume_label
))
1635 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1636 quote_n (0, volume_label
),
1637 quote_n (1, volume_label_option
)));
1640 /* Mark the archive with volume label STR. */
1642 _write_volume_label (const char *str
)
1644 if (archive_format
== POSIX_FORMAT
)
1645 xheader_store ("GNU.volume.label", &dummy
, str
);
1648 union block
*label
= find_next_block ();
1650 memset (label
, 0, BLOCKSIZE
);
1652 strcpy (label
->header
.name
, str
);
1653 assign_string (¤t_stat_info
.file_name
,
1654 label
->header
.name
);
1655 current_stat_info
.had_trailing_slash
=
1656 strip_trailing_slashes (current_stat_info
.file_name
);
1658 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1659 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1660 finish_header (¤t_stat_info
, label
, -1);
1661 set_next_block_after (label
);
1665 #define VOL_SUFFIX "Volume"
1667 /* Add a volume label to a part of multi-volume archive */
1669 add_volume_label (void)
1671 char buf
[UINTMAX_STRSIZE_BOUND
];
1672 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1673 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1675 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1676 _write_volume_label (s
);
1681 add_chunk_header (struct bufmap
*map
)
1683 if (archive_format
== POSIX_FORMAT
)
1685 off_t block_ordinal
;
1687 struct tar_stat_info st
;
1689 memset (&st
, 0, sizeof st
);
1690 st
.orig_file_name
= st
.file_name
= map
->file_name
;
1691 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1692 st
.stat
.st_uid
= getuid ();
1693 st
.stat
.st_gid
= getgid ();
1694 st
.orig_file_name
= xheader_format_name (&st
,
1695 "%d/GNUFileParts.%p/%f.%n",
1697 st
.file_name
= st
.orig_file_name
;
1698 st
.archive_file_size
= st
.stat
.st_size
= map
->sizeleft
;
1700 block_ordinal
= current_block_ordinal ();
1701 blk
= start_header (&st
);
1703 abort (); /* FIXME */
1704 finish_header (&st
, blk
, block_ordinal
);
1705 free (st
.orig_file_name
);
1710 /* Add a volume label to the current archive */
1712 write_volume_label (void)
1714 if (multi_volume_option
)
1715 add_volume_label ();
1717 _write_volume_label (volume_label_option
);
1720 /* Write GNU multi-volume header */
1722 gnu_add_multi_volume_header (struct bufmap
*map
)
1725 union block
*block
= find_next_block ();
1727 if (strlen (map
->file_name
) > NAME_FIELD_SIZE
)
1729 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1730 quotearg_colon (map
->file_name
)));
1732 memset (block
, 0, BLOCKSIZE
);
1734 strncpy (block
->header
.name
, map
->file_name
, NAME_FIELD_SIZE
);
1735 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1737 OFF_TO_CHARS (map
->sizeleft
, block
->header
.size
);
1738 OFF_TO_CHARS (map
->sizetotal
- map
->sizeleft
,
1739 block
->oldgnu_header
.offset
);
1741 tmp
= verbose_option
;
1743 finish_header (¤t_stat_info
, block
, -1);
1744 verbose_option
= tmp
;
1745 set_next_block_after (block
);
1748 /* Add a multi volume header to the current archive. The exact header format
1749 depends on the archive format. */
1751 add_multi_volume_header (struct bufmap
*map
)
1753 if (archive_format
== POSIX_FORMAT
)
1755 off_t d
= map
->sizetotal
- map
->sizeleft
;
1756 xheader_store ("GNU.volume.filename", &dummy
, map
->file_name
);
1757 xheader_store ("GNU.volume.size", &dummy
, &map
->sizeleft
);
1758 xheader_store ("GNU.volume.offset", &dummy
, &d
);
1761 gnu_add_multi_volume_header (map
);
1765 /* Low-level flush functions */
1767 /* Simple flush read (no multi-volume or label extensions) */
1769 simple_flush_read (void)
1771 size_t status
; /* result from system call */
1773 checkpoint_run (false);
1775 /* Clear the count of errors. This only applies to a single call to
1778 read_error_count
= 0; /* clear error count */
1780 if (write_archive_to_stdout
&& record_start_block
!= 0)
1782 archive
= STDOUT_FILENO
;
1783 status
= sys_write_archive_buffer ();
1784 archive
= STDIN_FILENO
;
1785 if (status
!= record_size
)
1786 archive_write_error (status
);
1791 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1792 if (status
== record_size
)
1797 if (status
== SAFE_READ_ERROR
)
1799 archive_read_error ();
1800 continue; /* try again */
1804 short_read (status
);
1807 /* Simple flush write (no multi-volume or label extensions) */
1809 simple_flush_write (size_t level
__attribute__((unused
)))
1813 status
= _flush_write ();
1814 if (status
!= record_size
)
1815 archive_write_error (status
);
1819 bytes_written
+= status
;
1824 /* GNU flush functions. These support multi-volume and archive labels in
1825 GNU and PAX archive formats. */
1828 _gnu_flush_read (void)
1830 size_t status
; /* result from system call */
1832 checkpoint_run (false);
1834 /* Clear the count of errors. This only applies to a single call to
1837 read_error_count
= 0; /* clear error count */
1839 if (write_archive_to_stdout
&& record_start_block
!= 0)
1841 archive
= STDOUT_FILENO
;
1842 status
= sys_write_archive_buffer ();
1843 archive
= STDIN_FILENO
;
1844 if (status
!= record_size
)
1845 archive_write_error (status
);
1850 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1851 if (status
== record_size
)
1857 /* The condition below used to include
1858 || (status > 0 && !read_full_records)
1859 This is incorrect since even if new_volume() succeeds, the
1860 subsequent call to rmtread will overwrite the chunk of data
1861 already read in the buffer, so the processing will fail */
1863 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1864 && multi_volume_option
)
1866 while (!try_new_volume ())
1868 if (current_block
== record_end
)
1869 /* Necessary for blocking_factor == 1 */
1873 else if (status
== SAFE_READ_ERROR
)
1875 archive_read_error ();
1880 short_read (status
);
1884 gnu_flush_read (void)
1886 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1888 flush_read_ptr
= gnu_flush_read
;
1892 _gnu_flush_write (size_t buffer_level
)
1895 union block
*header
;
1901 status
= _flush_write ();
1902 if (status
!= record_size
&& !multi_volume_option
)
1903 archive_write_error (status
);
1908 bytes_written
+= status
;
1911 if (status
== record_size
)
1916 map
= bufmap_locate (status
);
1918 if (status
% BLOCKSIZE
)
1920 ERROR ((0, 0, _("write did not end on a block boundary")));
1921 archive_write_error (status
);
1924 /* In multi-volume mode. */
1925 /* ENXIO is for the UNIX PC. */
1926 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1927 archive_write_error (status
);
1929 if (!new_volume (ACCESS_WRITE
))
1932 tar_stat_destroy (&dummy
);
1934 increase_volume_number ();
1935 prev_written
+= bytes_written
;
1938 copy_ptr
= record_start
->buffer
+ status
;
1939 copy_size
= buffer_level
- status
;
1941 /* Switch to the next buffer */
1942 record_index
= !record_index
;
1947 if (volume_label_option
)
1948 add_volume_label ();
1951 add_multi_volume_header (map
);
1953 write_extended (true, &dummy
, find_next_block ());
1954 tar_stat_destroy (&dummy
);
1957 add_chunk_header (map
);
1958 header
= find_next_block ();
1959 bufmap_reset (map
, header
- record_start
);
1960 bufsize
= available_space_after (header
);
1962 while (bufsize
< copy_size
)
1964 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1965 copy_ptr
+= bufsize
;
1966 copy_size
-= bufsize
;
1967 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1968 header
= find_next_block ();
1969 bufsize
= available_space_after (header
);
1971 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1972 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1973 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1978 gnu_flush_write (size_t buffer_level
)
1980 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1981 _gnu_flush_write (buffer_level
);
1982 flush_write_ptr
= gnu_flush_write
;
1994 flush_write_ptr (record_size
);
1998 open_archive (enum access_mode wanted_access
)
2000 flush_read_ptr
= gnu_flush_read
;
2001 flush_write_ptr
= gnu_flush_write
;
2003 _open_archive (wanted_access
);
2004 switch (wanted_access
)
2008 if (volume_label_option
)
2009 match_volume_label ();
2013 records_written
= 0;
2014 if (volume_label_option
)
2015 write_volume_label ();
2018 set_volume_start_time ();