1 /* Buffer management for tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 3, 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>
35 /* Number of retries before giving up on read. */
36 #define READ_ERROR_MAX 10
38 /* Globbing pattern to append to volume label if initial match failed. */
39 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
43 static tarlong prev_written
; /* bytes written on previous volumes */
44 static tarlong bytes_written
; /* bytes written on this volume */
45 static void *record_buffer
[2]; /* allocated memory */
46 union block
*record_buffer_aligned
[2];
47 static int record_index
;
49 /* FIXME: The following variables should ideally be static to this
50 module. However, this cannot be done yet. The cleanup continues! */
52 union block
*record_start
; /* start of record of archive */
53 union block
*record_end
; /* last+1 block of archive record */
54 union block
*current_block
; /* current block of archive */
55 enum access_mode access_mode
; /* how do we handle the archive */
56 off_t records_read
; /* number of records read from this archive */
57 off_t records_written
; /* likewise, for records written */
58 extern off_t records_skipped
; /* number of records skipped at the start
59 of the archive, defined in delete.c */
61 static off_t record_start_block
; /* block ordinal at record_start */
63 /* Where we write list messages (not errors, not interactions) to. */
66 static void backspace_output (void);
68 /* PID of child program, if compress_option or remote archive access. */
69 static pid_t child_pid
;
71 /* Error recovery stuff */
72 static int read_error_count
;
74 /* Have we hit EOF yet? */
77 static bool read_full_records
= false;
79 /* We're reading, but we just read the last block and it's time to update.
82 As least EXTERN like this one as possible. (?? --gray)
83 FIXME: Either eliminate it or move it to common.h.
85 extern bool time_to_start_writing
;
87 bool write_archive_to_stdout
;
89 void (*flush_write_ptr
) (size_t);
90 void (*flush_read_ptr
) (void);
94 char *continued_file_name
;
95 uintmax_t continued_file_size
;
96 uintmax_t continued_file_offset
;
99 static int volno
= 1; /* which volume of a multi-volume tape we're
101 static int global_volno
= 1; /* volume number to print in external
104 bool write_archive_to_stdout
;
106 /* Used by flush_read and flush_write to store the real info about saved
108 static char *real_s_name
;
109 static off_t real_s_totsize
;
110 static off_t real_s_sizeleft
;
113 /* Multi-volume tracking support */
114 static char *save_name
; /* name of the file we are currently writing */
115 static off_t save_totsize
; /* total size of file we are writing, only
116 valid if save_name is nonzero */
117 static off_t save_sizeleft
; /* where we are in the file we are writing,
118 only valid if save_name is nonzero */
121 static struct tar_stat_info dummy
;
124 buffer_write_global_xheader ()
126 xheader_write_global (&dummy
.xhdr
);
130 mv_begin (struct tar_stat_info
*st
)
132 if (multi_volume_option
)
134 assign_string (&save_name
, st
->orig_file_name
);
135 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
142 if (multi_volume_option
)
143 assign_string (&save_name
, 0);
147 mv_total_size (off_t size
)
153 mv_size_left (off_t size
)
155 save_sizeleft
= size
;
162 clear_read_error_count (void)
164 read_error_count
= 0;
168 /* Time-related functions */
175 gettime (&start_time
);
176 volume_start_time
= start_time
;
177 last_stat_time
= start_time
;
181 set_volume_start_time ()
183 gettime (&volume_start_time
);
184 last_stat_time
= volume_start_time
;
192 duration
+= ((now
.tv_sec
- last_stat_time
.tv_sec
)
193 + (now
.tv_nsec
- last_stat_time
.tv_nsec
) / 1e9
);
194 gettime (&last_stat_time
);
198 /* Compression detection */
201 ct_tar
, /* Plain tar file */
202 ct_none
, /* Unknown compression type */
213 enum compress_type type
;
220 static struct zip_magic
const magic
[] = {
223 { ct_compress
, 2, "\037\235", COMPRESS_PROGRAM
, "-Z" },
224 { ct_gzip
, 2, "\037\213", GZIP_PROGRAM
, "-z" },
225 { ct_bzip2
, 3, "BZh", BZIP2_PROGRAM
, "-j" },
226 { ct_lzma
, 6, "\xFFLZMA", LZMA_PROGRAM
, "--lzma" },
227 { ct_lzop
, 4, "\211LZO", LZOP_PROGRAM
, "--lzop" },
228 { ct_xz
, 6, "\0xFD7zXZ", XZ_PROGRAM
, "-J" },
231 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
233 #define compress_option(t) magic[t].option
234 #define compress_program(t) magic[t].program
236 /* Check if the file ARCHIVE is a compressed archive. */
238 check_compressed_archive (bool *pshort
)
240 struct zip_magic
const *p
;
247 /* Prepare global data needed for find_next_block: */
248 record_end
= record_start
; /* set up for 1st record = # 0 */
249 sfr
= read_full_records
;
250 read_full_records
= true; /* Suppress fatal error on reading a partial
252 *pshort
= find_next_block () == 0;
254 /* Restore global values */
255 read_full_records
= sfr
;
257 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
258 /* Probably a valid header */
261 for (p
= magic
+ 2; p
< magic
+ NMAGIC
; p
++)
262 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
268 /* Guess if the archive is seekable. */
270 guess_seekable_archive ()
274 if (subcommand_option
== DELETE_SUBCOMMAND
)
276 /* The current code in delete.c is based on the assumption that
277 skip_member() reads all data from the archive. So, we should
278 make sure it won't use seeks. On the other hand, the same code
279 depends on the ability to backspace a record in the archive,
280 so setting seekable_archive to false is technically incorrect.
281 However, it is tested only in skip_member(), so it's not a
283 seekable_archive
= false;
286 if (seek_option
!= -1)
288 seekable_archive
= !!seek_option
;
292 if (!multi_volume_option
&& !use_compress_program_option
293 && fstat (archive
, &st
) == 0)
294 seekable_archive
= S_ISREG (st
.st_mode
);
296 seekable_archive
= false;
299 /* Open an archive named archive_name_array[0]. Detect if it is
300 a compressed archive of known type and use corresponding decompression
303 open_compressed_archive ()
305 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
306 MODE_RW
, rsh_command_option
);
310 if (!multi_volume_option
)
312 if (!use_compress_program_option
)
315 enum compress_type type
= check_compressed_archive (&shortfile
);
321 ERROR ((0, 0, _("This does not look like a tar archive")));
326 ERROR ((0, 0, _("This does not look like a tar archive")));
327 set_comression_program_by_suffix (archive_name_array
[0], NULL
);
328 if (!use_compress_program_option
)
333 use_compress_program_option
= compress_program (type
);
338 /* FD is not needed any more */
341 hit_eof
= false; /* It might have been set by find_next_block in
342 check_compressed_archive */
344 /* Open compressed archive */
345 child_pid
= sys_child_open_for_uncompress ();
346 read_full_records
= true;
350 record_end
= record_start
; /* set up for 1st record = # 0 */
357 print_stats (FILE *fp
, const char *text
, tarlong numbytes
)
359 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
360 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
361 char rate
[LONGEST_HUMAN_READABLE
+ 1];
363 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
365 sprintf (bytes
, TARLONG_FORMAT
, numbytes
);
367 fprintf (fp
, "%s: %s (%s, %s/s)\n",
369 human_readable (numbytes
, abbr
, human_opts
, 1, 1),
370 (0 < duration
&& numbytes
/ duration
< (uintmax_t) -1
371 ? human_readable (numbytes
/ duration
, rate
, human_opts
, 1, 1)
378 switch (subcommand_option
)
380 case CREATE_SUBCOMMAND
:
382 case UPDATE_SUBCOMMAND
:
383 case APPEND_SUBCOMMAND
:
384 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
385 print_stats (stderr
, _("Total bytes written"),
386 prev_written
+ bytes_written
);
389 case DELETE_SUBCOMMAND
:
391 char buf
[UINTMAX_STRSIZE_BOUND
];
392 print_stats (stderr
, _("Total bytes read"),
393 records_read
* record_size
);
394 print_stats (stderr
, _("Total bytes written"),
395 prev_written
+ bytes_written
);
396 fprintf (stderr
, _("Total bytes deleted: %s\n"),
397 STRINGIFY_BIGINT ((records_read
- records_skipped
)
399 - (prev_written
+ bytes_written
), buf
));
403 case EXTRACT_SUBCOMMAND
:
404 case LIST_SUBCOMMAND
:
405 case DIFF_SUBCOMMAND
:
406 print_stats (stderr
, _("Total bytes read"),
407 records_read
* record_size
);
415 /* Compute and return the block ordinal at current_block. */
417 current_block_ordinal (void)
419 return record_start_block
+ (current_block
- record_start
);
422 /* If the EOF flag is set, reset it, as well as current_block, etc. */
429 current_block
= record_start
;
430 record_end
= record_start
+ blocking_factor
;
431 access_mode
= ACCESS_WRITE
;
435 /* Return the location of the next available input or output block.
436 Return zero for EOF. Once we have returned zero, we just keep returning
437 it, to avoid accidentally going on to the next file on the tape. */
439 find_next_block (void)
441 if (current_block
== record_end
)
446 if (current_block
== record_end
)
452 return current_block
;
455 /* Indicate that we have used all blocks up thru BLOCK. */
457 set_next_block_after (union block
*block
)
459 while (block
>= current_block
)
462 /* Do *not* flush the archive here. If we do, the same argument to
463 set_next_block_after could mean the next block (if the input record
464 is exactly one block long), which is not what is intended. */
466 if (current_block
> record_end
)
470 /* Return the number of bytes comprising the space between POINTER
471 through the end of the current buffer of blocks. This space is
472 available for filling with data, or taking data from. POINTER is
473 usually (but not always) the result of previous find_next_block call. */
475 available_space_after (union block
*pointer
)
477 return record_end
->buffer
- pointer
->buffer
;
480 /* Close file having descriptor FD, and abort if close unsuccessful. */
485 close_error (_("(pipe)"));
491 if (! record_buffer_aligned
[record_index
])
492 record_buffer_aligned
[record_index
] =
493 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
495 record_start
= record_buffer_aligned
[record_index
];
496 current_block
= record_start
;
497 record_end
= record_start
+ blocking_factor
;
500 /* Open an archive file. The argument specifies whether we are
501 reading or writing, or both. */
503 _open_archive (enum access_mode wanted_access
)
505 int backed_up_flag
= 0;
507 if (record_size
== 0)
508 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
510 if (archive_names
== 0)
511 FATAL_ERROR ((0, 0, _("No archive name given")));
513 tar_stat_destroy (¤t_stat_info
);
520 /* When updating the archive, we start with reading. */
521 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
523 read_full_records
= read_full_records_option
;
527 if (use_compress_program_option
)
529 switch (wanted_access
)
532 child_pid
= sys_child_open_for_uncompress ();
533 read_full_records
= true;
534 record_end
= record_start
; /* set up for 1st record = # 0 */
538 child_pid
= sys_child_open_for_compress ();
542 abort (); /* Should not happen */
547 && wanted_access
== ACCESS_WRITE
548 && strcmp (archive_name_array
[0], "-") == 0)
551 else if (strcmp (archive_name_array
[0], "-") == 0)
553 read_full_records
= true; /* could be a pipe, be safe */
555 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
557 switch (wanted_access
)
562 enum compress_type type
;
564 archive
= STDIN_FILENO
;
566 type
= check_compressed_archive (&shortfile
);
567 if (type
!= ct_tar
&& type
!= ct_none
)
569 _("Archive is compressed. Use %s option"),
570 compress_option (type
)));
572 ERROR ((0, 0, _("This does not look like a tar archive")));
577 archive
= STDOUT_FILENO
;
578 if (!index_file_name
)
583 archive
= STDIN_FILENO
;
584 write_archive_to_stdout
= true;
585 record_end
= record_start
; /* set up for 1st record = # 0 */
586 if (!index_file_name
)
591 else if (verify_option
)
592 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
593 MODE_RW
, rsh_command_option
);
595 switch (wanted_access
)
598 archive
= open_compressed_archive ();
600 guess_seekable_archive ();
606 maybe_backup_file (archive_name_array
[0], 1);
609 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
614 archive
= rmtopen (archive_name_array
[0],
615 O_RDWR
| O_CREAT
| O_BINARY
,
616 MODE_RW
, rsh_command_option
);
618 switch (check_compressed_archive (NULL
))
626 _("Cannot update compressed archives")));
632 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
634 int saved_errno
= errno
;
639 open_fatal (archive_name_array
[0]);
642 sys_detect_dev_null_output ();
643 sys_save_archive_dev_ino ();
644 SET_BINARY_MODE (archive
);
646 switch (wanted_access
)
649 find_next_block (); /* read it in, check for EOF */
659 /* Perform a write to flush the buffer. */
665 checkpoint_run (true);
666 if (tape_length_option
&& tape_length_option
<= bytes_written
)
671 else if (dev_null_output
)
672 status
= record_size
;
674 status
= sys_write_archive_buffer ();
679 /* Handle write errors on the archive. Write errors are always fatal.
680 Hitting the end of a volume does not cause a write error unless the
681 write was the first record of the volume. */
683 archive_write_error (ssize_t status
)
685 /* It might be useful to know how much was written before the error
690 print_total_stats ();
694 write_fatal_details (*archive_name_cursor
, status
, record_size
);
697 /* Handle read errors on the archive. If the read should be retried,
698 return to the caller. */
700 archive_read_error (void)
702 read_error (*archive_name_cursor
);
704 if (record_start_block
== 0)
705 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
707 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
708 then give up on reading the archive. */
710 if (read_error_count
++ > READ_ERROR_MAX
)
711 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
720 if (fstat (archive
, &st
))
722 stat_diag (*archive_name_cursor
);
725 return S_ISBLK (st
.st_mode
) || S_ISCHR (st
.st_mode
);
729 short_read (size_t status
)
731 size_t left
; /* bytes left */
732 char *more
; /* pointer to next byte to read */
734 more
= record_start
->buffer
+ status
;
735 left
= record_size
- status
;
737 if (left
&& left
% BLOCKSIZE
== 0
739 && record_start_block
== 0 && status
!= 0
740 && archive_is_dev ())
742 unsigned long rsize
= status
/ BLOCKSIZE
;
744 ngettext ("Record size = %lu block",
745 "Record size = %lu blocks",
750 while (left
% BLOCKSIZE
!= 0
751 || (left
&& status
&& read_full_records
))
754 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
755 archive_read_error ();
760 if (! read_full_records
)
762 unsigned long rest
= record_size
- left
;
765 ngettext ("Unaligned block (%lu byte) in archive",
766 "Unaligned block (%lu bytes) in archive",
775 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
779 /* Flush the current buffer to/from the archive. */
783 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
784 record_start_block
+= record_end
- record_start
;
785 current_block
= record_start
;
786 record_end
= record_start
+ blocking_factor
;
788 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
790 access_mode
= ACCESS_WRITE
;
791 time_to_start_writing
= false;
802 flush_write_ptr (buffer_level
);
810 /* Backspace the archive descriptor by one record worth. If it's a
811 tape, MTIOCTOP will work. If it's something else, try to seek on
812 it. If we can't seek, we lose! */
814 backspace_output (void)
818 struct mtop operation
;
820 operation
.mt_op
= MTBSR
;
821 operation
.mt_count
= 1;
822 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
824 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
830 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
832 /* Seek back to the beginning of this record and start writing there. */
834 position
-= record_size
;
837 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
839 /* Lseek failed. Try a different method. */
842 _("Cannot backspace archive file; it may be unreadable without -i")));
844 /* Replace the first part of the record with NULs. */
846 if (record_start
->buffer
!= output_start
)
847 memset (record_start
->buffer
, 0,
848 output_start
- record_start
->buffer
);
854 seek_archive (off_t size
)
856 off_t start
= current_block_ordinal ();
859 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
861 size
-= skipped
* BLOCKSIZE
;
863 if (size
< record_size
)
867 /* Compute number of records to skip */
868 nrec
= size
/ record_size
;
869 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
873 if (offset
% record_size
)
874 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
876 /* Convert to number of records */
878 /* Compute number of skipped blocks */
879 nblk
= offset
- start
;
881 /* Update buffering info */
882 records_read
+= nblk
/ blocking_factor
;
883 record_start_block
= offset
- blocking_factor
;
884 current_block
= record_end
;
889 /* Close the archive file. */
893 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
896 if (current_block
> record_start
)
904 if (rmtclose (archive
) != 0)
905 close_error (*archive_name_cursor
);
907 sys_wait_for_child (child_pid
, hit_eof
);
909 tar_stat_destroy (¤t_stat_info
);
914 free (record_buffer
[0]);
915 free (record_buffer
[1]);
918 /* Called to initialize the global volume number. */
920 init_volume_number (void)
922 FILE *file
= fopen (volno_file_option
, "r");
926 if (fscanf (file
, "%d", &global_volno
) != 1
928 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
929 quotearg_colon (volno_file_option
)));
931 read_error (volno_file_option
);
932 if (fclose (file
) != 0)
933 close_error (volno_file_option
);
935 else if (errno
!= ENOENT
)
936 open_error (volno_file_option
);
939 /* Called to write out the closing global volume number. */
941 closeout_volume_number (void)
943 FILE *file
= fopen (volno_file_option
, "w");
947 fprintf (file
, "%d\n", global_volno
);
949 write_error (volno_file_option
);
950 if (fclose (file
) != 0)
951 close_error (volno_file_option
);
954 open_error (volno_file_option
);
959 increase_volume_number ()
962 if (global_volno
< 0)
963 FATAL_ERROR ((0, 0, _("Volume number overflow")));
968 change_tape_menu (FILE *read_file
)
970 char *input_buffer
= NULL
;
976 fputc ('\007', stderr
);
978 _("Prepare volume #%d for %s and hit return: "),
979 global_volno
+ 1, quote (*archive_name_cursor
));
982 if (getline (&input_buffer
, &size
, read_file
) <= 0)
984 WARN ((0, 0, _("EOF where user reply was expected")));
986 if (subcommand_option
!= EXTRACT_SUBCOMMAND
987 && subcommand_option
!= LIST_SUBCOMMAND
988 && subcommand_option
!= DIFF_SUBCOMMAND
)
989 WARN ((0, 0, _("WARNING: Archive is incomplete")));
994 if (input_buffer
[0] == '\n'
995 || input_buffer
[0] == 'y'
996 || input_buffer
[0] == 'Y')
999 switch (input_buffer
[0])
1003 fprintf (stderr
, _("\
1004 n name Give a new file name for the next (and subsequent) volume(s)\n\
1006 y or newline Continue operation\n"));
1007 if (!restrict_option
)
1008 fprintf (stderr
, _(" ! Spawn a subshell\n"));
1009 fprintf (stderr
, _(" ? Print this list\n"));
1016 WARN ((0, 0, _("No new volume; exiting.\n")));
1018 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1019 && subcommand_option
!= LIST_SUBCOMMAND
1020 && subcommand_option
!= DIFF_SUBCOMMAND
)
1021 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1026 /* Get new file name. */
1032 for (name
= input_buffer
+ 1;
1033 *name
== ' ' || *name
== '\t';
1037 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1043 /* FIXME: the following allocation is never reclaimed. */
1044 *archive_name_cursor
= xstrdup (name
);
1048 fprintf (stderr
, "%s",
1049 _("File name not specified. Try again.\n"));
1054 if (!restrict_option
)
1062 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
1065 free (input_buffer
);
1068 /* We've hit the end of the old volume. Close it and open the next one.
1069 Return nonzero on success.
1072 new_volume (enum access_mode mode
)
1074 static FILE *read_file
;
1078 if (!read_file
&& !info_script_option
)
1079 /* FIXME: if fopen is used, it will never be closed. */
1080 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1087 assign_string (&volume_label
, NULL
);
1088 assign_string (&continued_file_name
, NULL
);
1089 continued_file_size
= continued_file_offset
= 0;
1090 current_block
= record_start
;
1092 if (rmtclose (archive
) != 0)
1093 close_error (*archive_name_cursor
);
1095 archive_name_cursor
++;
1096 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1098 archive_name_cursor
= archive_name_array
;
1106 /* We have to prompt from now on. */
1108 if (info_script_option
)
1110 if (volno_file_option
)
1111 closeout_volume_number ();
1112 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1113 FATAL_ERROR ((0, 0, _("%s command failed"),
1114 quote (info_script_option
)));
1117 change_tape_menu (read_file
);
1120 if (strcmp (archive_name_cursor
[0], "-") == 0)
1122 read_full_records
= true;
1123 archive
= STDIN_FILENO
;
1125 else if (verify_option
)
1126 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1127 rsh_command_option
);
1132 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1133 rsh_command_option
);
1134 guess_seekable_archive ();
1139 maybe_backup_file (*archive_name_cursor
, 1);
1140 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1141 rsh_command_option
);
1145 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1146 rsh_command_option
);
1152 open_warn (*archive_name_cursor
);
1153 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1154 undo_last_backup ();
1159 SET_BINARY_MODE (archive
);
1165 read_header0 (struct tar_stat_info
*info
)
1167 enum read_header rc
;
1169 tar_stat_init (info
);
1170 rc
= read_header_primitive (false, info
);
1171 if (rc
== HEADER_SUCCESS
)
1173 set_next_block_after (current_header
);
1176 ERROR ((0, 0, _("This does not look like a tar archive")));
1184 union block
*header
;
1185 enum access_mode acc
;
1187 switch (subcommand_option
)
1189 case APPEND_SUBCOMMAND
:
1190 case CAT_SUBCOMMAND
:
1191 case UPDATE_SUBCOMMAND
:
1192 acc
= ACCESS_UPDATE
;
1200 if (!new_volume (acc
))
1203 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1205 archive_read_error ();
1207 if (status
!= record_size
)
1208 short_read (status
);
1210 header
= find_next_block ();
1214 switch (header
->header
.typeflag
)
1218 if (!read_header0 (&dummy
))
1220 xheader_decode (&dummy
); /* decodes values from the global header */
1221 tar_stat_destroy (&dummy
);
1224 /* We have read the extended header of the first member in
1225 this volume. Put it back, so next read_header works as
1227 current_block
= record_start
;
1232 case GNUTYPE_VOLHDR
:
1233 if (!read_header0 (&dummy
))
1235 tar_stat_destroy (&dummy
);
1236 assign_string (&volume_label
, current_header
->header
.name
);
1237 set_next_block_after (header
);
1238 header
= find_next_block ();
1239 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1243 case GNUTYPE_MULTIVOL
:
1244 if (!read_header0 (&dummy
))
1246 tar_stat_destroy (&dummy
);
1247 assign_string (&continued_file_name
, current_header
->header
.name
);
1248 continued_file_size
=
1249 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1250 continued_file_offset
=
1251 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1261 if (!continued_file_name
1262 || strcmp (continued_file_name
, real_s_name
))
1264 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1265 && strlen (real_s_name
) >= NAME_FIELD_SIZE
1266 && strncmp (continued_file_name
, real_s_name
,
1267 NAME_FIELD_SIZE
) == 0)
1269 _("%s is possibly continued on this volume: header contains truncated name"),
1270 quote (real_s_name
)));
1273 WARN ((0, 0, _("%s is not continued on this volume"),
1274 quote (real_s_name
)));
1279 s
= continued_file_size
+ continued_file_offset
;
1281 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1283 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1284 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1285 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1287 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1288 quote (continued_file_name
),
1289 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1290 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1291 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1295 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1297 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1298 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1299 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1301 WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
1302 STRINGIFY_BIGINT (real_s_totsize
, totsizebuf
),
1303 STRINGIFY_BIGINT (real_s_sizeleft
, s1buf
),
1304 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1310 increase_volume_number ();
1315 /* Check the LABEL block against the volume label, seen as a globbing
1316 pattern. Return true if the pattern matches. In case of failure,
1317 retry matching a volume sequence number before giving up in
1318 multi-volume mode. */
1320 check_label_pattern (union block
*label
)
1325 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1328 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1331 if (!multi_volume_option
)
1334 string
= xmalloc (strlen (volume_label_option
)
1335 + sizeof VOLUME_LABEL_APPEND
+ 1);
1336 strcpy (string
, volume_label_option
);
1337 strcat (string
, VOLUME_LABEL_APPEND
);
1338 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1343 /* Check if the next block contains a volume label and if this matches
1344 the one given in the command line */
1346 match_volume_label (void)
1348 union block
*label
= find_next_block ();
1351 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1352 quote (volume_label_option
)));
1353 if (!check_label_pattern (label
))
1354 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1355 quote_n (0, label
->header
.name
),
1356 quote_n (1, volume_label_option
)));
1359 /* Mark the archive with volume label STR. */
1361 _write_volume_label (const char *str
)
1363 if (archive_format
== POSIX_FORMAT
)
1364 xheader_store ("GNU.volume.label", &dummy
, str
);
1367 union block
*label
= find_next_block ();
1369 memset (label
, 0, BLOCKSIZE
);
1371 strcpy (label
->header
.name
, str
);
1372 assign_string (¤t_stat_info
.file_name
,
1373 label
->header
.name
);
1374 current_stat_info
.had_trailing_slash
=
1375 strip_trailing_slashes (current_stat_info
.file_name
);
1377 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1378 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1379 finish_header (¤t_stat_info
, label
, -1);
1380 set_next_block_after (label
);
1384 #define VOL_SUFFIX "Volume"
1386 /* Add a volume label to a part of multi-volume archive */
1388 add_volume_label (void)
1390 char buf
[UINTMAX_STRSIZE_BOUND
];
1391 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1392 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1394 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1395 _write_volume_label (s
);
1402 if (archive_format
== POSIX_FORMAT
)
1404 off_t block_ordinal
;
1406 struct tar_stat_info st
;
1407 static size_t real_s_part_no
; /* FIXME */
1410 memset (&st
, 0, sizeof st
);
1411 st
.orig_file_name
= st
.file_name
= real_s_name
;
1412 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1413 st
.stat
.st_uid
= getuid ();
1414 st
.stat
.st_gid
= getgid ();
1415 st
.orig_file_name
= xheader_format_name (&st
,
1416 "%d/GNUFileParts.%p/%f.%n",
1418 st
.file_name
= st
.orig_file_name
;
1419 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1421 block_ordinal
= current_block_ordinal ();
1422 blk
= start_header (&st
);
1424 abort (); /* FIXME */
1425 finish_header (&st
, blk
, block_ordinal
);
1426 free (st
.orig_file_name
);
1431 /* Add a volume label to the current archive */
1433 write_volume_label (void)
1435 if (multi_volume_option
)
1436 add_volume_label ();
1438 _write_volume_label (volume_label_option
);
1441 /* Write GNU multi-volume header */
1443 gnu_add_multi_volume_header (void)
1446 union block
*block
= find_next_block ();
1448 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1450 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1451 quotearg_colon (real_s_name
)));
1453 memset (block
, 0, BLOCKSIZE
);
1455 /* FIXME: Michael P Urban writes: [a long name file] is being written
1456 when a new volume rolls around [...] Looks like the wrong value is
1457 being preserved in real_s_name, though. */
1459 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1460 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1462 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1463 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1464 block
->oldgnu_header
.offset
);
1466 tmp
= verbose_option
;
1468 finish_header (¤t_stat_info
, block
, -1);
1469 verbose_option
= tmp
;
1470 set_next_block_after (block
);
1473 /* Add a multi volume header to the current archive. The exact header format
1474 depends on the archive format. */
1476 add_multi_volume_header (void)
1478 if (archive_format
== POSIX_FORMAT
)
1480 off_t d
= real_s_totsize
- real_s_sizeleft
;
1481 xheader_store ("GNU.volume.filename", &dummy
, real_s_name
);
1482 xheader_store ("GNU.volume.size", &dummy
, &real_s_sizeleft
);
1483 xheader_store ("GNU.volume.offset", &dummy
, &d
);
1486 gnu_add_multi_volume_header ();
1489 /* Synchronize multi-volume globals */
1491 multi_volume_sync ()
1493 if (multi_volume_option
)
1497 assign_string (&real_s_name
,
1498 safer_name_suffix (save_name
, false,
1499 absolute_names_option
));
1500 real_s_totsize
= save_totsize
;
1501 real_s_sizeleft
= save_sizeleft
;
1505 assign_string (&real_s_name
, 0);
1507 real_s_sizeleft
= 0;
1513 /* Low-level flush functions */
1515 /* Simple flush read (no multi-volume or label extensions) */
1517 simple_flush_read (void)
1519 size_t status
; /* result from system call */
1521 checkpoint_run (false);
1523 /* Clear the count of errors. This only applies to a single call to
1526 read_error_count
= 0; /* clear error count */
1528 if (write_archive_to_stdout
&& record_start_block
!= 0)
1530 archive
= STDOUT_FILENO
;
1531 status
= sys_write_archive_buffer ();
1532 archive
= STDIN_FILENO
;
1533 if (status
!= record_size
)
1534 archive_write_error (status
);
1539 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1540 if (status
== record_size
)
1545 if (status
== SAFE_READ_ERROR
)
1547 archive_read_error ();
1548 continue; /* try again */
1552 short_read (status
);
1555 /* Simple flush write (no multi-volume or label extensions) */
1557 simple_flush_write (size_t level
__attribute__((unused
)))
1561 status
= _flush_write ();
1562 if (status
!= record_size
)
1563 archive_write_error (status
);
1567 bytes_written
+= status
;
1572 /* GNU flush functions. These support multi-volume and archive labels in
1573 GNU and PAX archive formats. */
1576 _gnu_flush_read (void)
1578 size_t status
; /* result from system call */
1580 checkpoint_run (false);
1582 /* Clear the count of errors. This only applies to a single call to
1585 read_error_count
= 0; /* clear error count */
1587 if (write_archive_to_stdout
&& record_start_block
!= 0)
1589 archive
= STDOUT_FILENO
;
1590 status
= sys_write_archive_buffer ();
1591 archive
= STDIN_FILENO
;
1592 if (status
!= record_size
)
1593 archive_write_error (status
);
1596 multi_volume_sync ();
1600 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1601 if (status
== record_size
)
1607 /* The condition below used to include
1608 || (status > 0 && !read_full_records)
1609 This is incorrect since even if new_volume() succeeds, the
1610 subsequent call to rmtread will overwrite the chunk of data
1611 already read in the buffer, so the processing will fail */
1613 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1614 && multi_volume_option
)
1616 while (!try_new_volume ())
1618 if (current_block
== record_end
)
1619 /* Necessary for blocking_factor == 1 */
1623 else if (status
== SAFE_READ_ERROR
)
1625 archive_read_error ();
1630 short_read (status
);
1634 gnu_flush_read (void)
1636 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1638 flush_read_ptr
= gnu_flush_read
;
1642 _gnu_flush_write (size_t buffer_level
)
1645 union block
*header
;
1651 status
= _flush_write ();
1652 if (status
!= record_size
&& !multi_volume_option
)
1653 archive_write_error (status
);
1658 bytes_written
+= status
;
1661 if (status
== record_size
)
1663 multi_volume_sync ();
1667 if (status
% BLOCKSIZE
)
1669 ERROR ((0, 0, _("write did not end on a block boundary")));
1670 archive_write_error (status
);
1673 /* In multi-volume mode. */
1674 /* ENXIO is for the UNIX PC. */
1675 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1676 archive_write_error (status
);
1678 real_s_sizeleft
-= status
;
1679 if (!new_volume (ACCESS_WRITE
))
1682 tar_stat_destroy (&dummy
);
1684 increase_volume_number ();
1685 prev_written
+= bytes_written
;
1688 copy_ptr
= record_start
->buffer
+ status
;
1689 copy_size
= buffer_level
- status
;
1691 /* Switch to the next buffer */
1692 record_index
= !record_index
;
1695 if (volume_label_option
)
1696 add_volume_label ();
1699 add_multi_volume_header ();
1701 write_extended (true, &dummy
, find_next_block ());
1702 tar_stat_destroy (&dummy
);
1705 add_chunk_header ();
1706 wrt
= bytes_written
;
1707 header
= find_next_block ();
1708 bufsize
= available_space_after (header
);
1709 while (bufsize
< copy_size
)
1711 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1712 copy_ptr
+= bufsize
;
1713 copy_size
-= bufsize
;
1714 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1715 header
= find_next_block ();
1716 bufsize
= available_space_after (header
);
1718 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1719 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1720 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1721 if (multi_volume_option
&& wrt
< bytes_written
)
1723 /* The value of bytes_written has changed while moving data;
1724 that means that flush_archive was executed at least once in
1725 between, and, as a consequence, copy_size bytes were not written
1726 to disk. We need to update sizeleft variables to compensate for
1728 save_sizeleft
+= copy_size
;
1729 multi_volume_sync ();
1735 gnu_flush_write (size_t buffer_level
)
1737 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1738 _gnu_flush_write (buffer_level
);
1739 flush_write_ptr
= gnu_flush_write
;
1751 flush_write_ptr (record_size
);
1755 open_archive (enum access_mode wanted_access
)
1757 flush_read_ptr
= gnu_flush_read
;
1758 flush_write_ptr
= gnu_flush_write
;
1760 _open_archive (wanted_access
);
1761 switch (wanted_access
)
1764 if (volume_label_option
)
1765 match_volume_label ();
1769 records_written
= 0;
1770 if (volume_label_option
)
1771 write_volume_label ();
1777 set_volume_start_time ();