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, 2010 Free Software
7 Written by John Gilmore, on 1985-08-25.
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any later
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include <system-ioctl.h>
36 /* Number of retries before giving up on read. */
37 #define READ_ERROR_MAX 10
39 /* Globbing pattern to append to volume label if initial match failed. */
40 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
44 static tarlong prev_written
; /* bytes written on previous volumes */
45 static tarlong bytes_written
; /* bytes written on this volume */
46 static void *record_buffer
[2]; /* allocated memory */
47 union block
*record_buffer_aligned
[2];
48 static int record_index
;
50 /* FIXME: The following variables should ideally be static to this
51 module. However, this cannot be done yet. The cleanup continues! */
53 union block
*record_start
; /* start of record of archive */
54 union block
*record_end
; /* last+1 block of archive record */
55 union block
*current_block
; /* current block of archive */
56 enum access_mode access_mode
; /* how do we handle the archive */
57 off_t records_read
; /* number of records read from this archive */
58 off_t records_written
; /* likewise, for records written */
59 extern off_t records_skipped
; /* number of records skipped at the start
60 of the archive, defined in delete.c */
62 static off_t record_start_block
; /* block ordinal at record_start */
64 /* Where we write list messages (not errors, not interactions) to. */
67 static void backspace_output (void);
69 /* PID of child program, if compress_option or remote archive access. */
70 static pid_t child_pid
;
72 /* Error recovery stuff */
73 static int read_error_count
;
75 /* Have we hit EOF yet? */
78 static bool read_full_records
= false;
80 /* We're reading, but we just read the last block and it's time to update.
83 As least EXTERN like this one as possible. (?? --gray)
84 FIXME: Either eliminate it or move it to common.h.
86 extern bool time_to_start_writing
;
88 bool write_archive_to_stdout
;
90 void (*flush_write_ptr
) (size_t);
91 void (*flush_read_ptr
) (void);
95 char *continued_file_name
;
96 uintmax_t continued_file_size
;
97 uintmax_t continued_file_offset
;
100 static int volno
= 1; /* which volume of a multi-volume tape we're
102 static int global_volno
= 1; /* volume number to print in external
105 bool write_archive_to_stdout
;
107 /* Used by flush_read and flush_write to store the real info about saved
109 static char *real_s_name
;
110 static off_t real_s_totsize
;
111 static off_t real_s_sizeleft
;
114 /* Multi-volume tracking support */
115 static char *save_name
; /* name of the file we are currently writing */
116 static off_t save_totsize
; /* total size of file we are writing, only
117 valid if save_name is nonzero */
118 static off_t save_sizeleft
; /* where we are in the file we are writing,
119 only valid if save_name is nonzero */
122 static struct tar_stat_info dummy
;
125 buffer_write_global_xheader ()
127 xheader_write_global (&dummy
.xhdr
);
131 mv_begin (struct tar_stat_info
*st
)
133 if (multi_volume_option
)
135 assign_string (&save_name
, st
->orig_file_name
);
136 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
143 if (multi_volume_option
)
144 assign_string (&save_name
, 0);
148 mv_total_size (off_t size
)
154 mv_size_left (off_t size
)
156 save_sizeleft
= size
;
163 clear_read_error_count (void)
165 read_error_count
= 0;
169 /* Time-related functions */
176 gettime (&start_time
);
177 volume_start_time
= start_time
;
178 last_stat_time
= start_time
;
182 set_volume_start_time ()
184 gettime (&volume_start_time
);
185 last_stat_time
= volume_start_time
;
193 duration
+= ((now
.tv_sec
- last_stat_time
.tv_sec
)
194 + (now
.tv_nsec
- last_stat_time
.tv_nsec
) / 1e9
);
195 gettime (&last_stat_time
);
199 /* Compression detection */
202 ct_tar
, /* Plain tar file */
203 ct_none
, /* Unknown compression type */
214 enum compress_type type
;
221 static struct zip_magic
const magic
[] = {
224 { ct_compress
, 2, "\037\235", COMPRESS_PROGRAM
, "-Z" },
225 { ct_gzip
, 2, "\037\213", GZIP_PROGRAM
, "-z" },
226 { ct_bzip2
, 3, "BZh", BZIP2_PROGRAM
, "-j" },
227 { ct_lzma
, 6, "\xFFLZMA", LZMA_PROGRAM
, "--lzma" },
228 { ct_lzop
, 4, "\211LZO", LZOP_PROGRAM
, "--lzop" },
229 { ct_xz
, 6, "\0xFD7zXZ", XZ_PROGRAM
, "-J" },
232 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
234 #define compress_option(t) magic[t].option
235 #define compress_program(t) magic[t].program
237 /* Check if the file ARCHIVE is a compressed archive. */
239 check_compressed_archive (bool *pshort
)
241 struct zip_magic
const *p
;
248 /* Prepare global data needed for find_next_block: */
249 record_end
= record_start
; /* set up for 1st record = # 0 */
250 sfr
= read_full_records
;
251 read_full_records
= true; /* Suppress fatal error on reading a partial
253 *pshort
= find_next_block () == 0;
255 /* Restore global values */
256 read_full_records
= sfr
;
258 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
259 /* Probably a valid header */
262 for (p
= magic
+ 2; p
< magic
+ NMAGIC
; p
++)
263 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
269 /* Guess if the archive is seekable. */
271 guess_seekable_archive ()
275 if (subcommand_option
== DELETE_SUBCOMMAND
)
277 /* The current code in delete.c is based on the assumption that
278 skip_member() reads all data from the archive. So, we should
279 make sure it won't use seeks. On the other hand, the same code
280 depends on the ability to backspace a record in the archive,
281 so setting seekable_archive to false is technically incorrect.
282 However, it is tested only in skip_member(), so it's not a
284 seekable_archive
= false;
287 if (seek_option
!= -1)
289 seekable_archive
= !!seek_option
;
293 if (!multi_volume_option
&& !use_compress_program_option
294 && fstat (archive
, &st
) == 0)
295 seekable_archive
= S_ISREG (st
.st_mode
);
297 seekable_archive
= false;
300 /* Open an archive named archive_name_array[0]. Detect if it is
301 a compressed archive of known type and use corresponding decompression
304 open_compressed_archive ()
306 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
307 MODE_RW
, rsh_command_option
);
311 if (!multi_volume_option
)
313 if (!use_compress_program_option
)
316 enum compress_type type
= check_compressed_archive (&shortfile
);
322 ERROR ((0, 0, _("This does not look like a tar archive")));
327 ERROR ((0, 0, _("This does not look like a tar archive")));
328 set_comression_program_by_suffix (archive_name_array
[0], NULL
);
329 if (!use_compress_program_option
)
334 use_compress_program_option
= compress_program (type
);
339 /* FD is not needed any more */
342 hit_eof
= false; /* It might have been set by find_next_block in
343 check_compressed_archive */
345 /* Open compressed archive */
346 child_pid
= sys_child_open_for_uncompress ();
347 read_full_records
= true;
351 record_end
= record_start
; /* set up for 1st record = # 0 */
358 print_stats (FILE *fp
, const char *text
, tarlong numbytes
)
360 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
361 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
362 char rate
[LONGEST_HUMAN_READABLE
+ 1];
364 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
366 sprintf (bytes
, TARLONG_FORMAT
, numbytes
);
368 fprintf (fp
, "%s: %s (%s, %s/s)\n",
370 human_readable (numbytes
, abbr
, human_opts
, 1, 1),
371 (0 < duration
&& numbytes
/ duration
< (uintmax_t) -1
372 ? human_readable (numbytes
/ duration
, rate
, human_opts
, 1, 1)
379 switch (subcommand_option
)
381 case CREATE_SUBCOMMAND
:
383 case UPDATE_SUBCOMMAND
:
384 case APPEND_SUBCOMMAND
:
385 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
386 print_stats (stderr
, _("Total bytes written"),
387 prev_written
+ bytes_written
);
390 case DELETE_SUBCOMMAND
:
392 char buf
[UINTMAX_STRSIZE_BOUND
];
393 print_stats (stderr
, _("Total bytes read"),
394 records_read
* record_size
);
395 print_stats (stderr
, _("Total bytes written"),
396 prev_written
+ bytes_written
);
397 fprintf (stderr
, _("Total bytes deleted: %s\n"),
398 STRINGIFY_BIGINT ((records_read
- records_skipped
)
400 - (prev_written
+ bytes_written
), buf
));
404 case EXTRACT_SUBCOMMAND
:
405 case LIST_SUBCOMMAND
:
406 case DIFF_SUBCOMMAND
:
407 print_stats (stderr
, _("Total bytes read"),
408 records_read
* record_size
);
416 /* Compute and return the block ordinal at current_block. */
418 current_block_ordinal (void)
420 return record_start_block
+ (current_block
- record_start
);
423 /* If the EOF flag is set, reset it, as well as current_block, etc. */
430 current_block
= record_start
;
431 record_end
= record_start
+ blocking_factor
;
432 access_mode
= ACCESS_WRITE
;
436 /* Return the location of the next available input or output block.
437 Return zero for EOF. Once we have returned zero, we just keep returning
438 it, to avoid accidentally going on to the next file on the tape. */
440 find_next_block (void)
442 if (current_block
== record_end
)
447 if (current_block
== record_end
)
453 return current_block
;
456 /* Indicate that we have used all blocks up thru BLOCK. */
458 set_next_block_after (union block
*block
)
460 while (block
>= current_block
)
463 /* Do *not* flush the archive here. If we do, the same argument to
464 set_next_block_after could mean the next block (if the input record
465 is exactly one block long), which is not what is intended. */
467 if (current_block
> record_end
)
471 /* Return the number of bytes comprising the space between POINTER
472 through the end of the current buffer of blocks. This space is
473 available for filling with data, or taking data from. POINTER is
474 usually (but not always) the result of previous find_next_block call. */
476 available_space_after (union block
*pointer
)
478 return record_end
->buffer
- pointer
->buffer
;
481 /* Close file having descriptor FD, and abort if close unsuccessful. */
486 close_error (_("(pipe)"));
492 if (! record_buffer_aligned
[record_index
])
493 record_buffer_aligned
[record_index
] =
494 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
496 record_start
= record_buffer_aligned
[record_index
];
497 current_block
= record_start
;
498 record_end
= record_start
+ blocking_factor
;
501 /* Open an archive file. The argument specifies whether we are
502 reading or writing, or both. */
504 _open_archive (enum access_mode wanted_access
)
506 int backed_up_flag
= 0;
508 if (record_size
== 0)
509 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
511 if (archive_names
== 0)
512 FATAL_ERROR ((0, 0, _("No archive name given")));
514 tar_stat_destroy (¤t_stat_info
);
521 /* When updating the archive, we start with reading. */
522 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
524 read_full_records
= read_full_records_option
;
528 if (use_compress_program_option
)
530 switch (wanted_access
)
533 child_pid
= sys_child_open_for_uncompress ();
534 read_full_records
= true;
535 record_end
= record_start
; /* set up for 1st record = # 0 */
539 child_pid
= sys_child_open_for_compress ();
543 abort (); /* Should not happen */
548 && wanted_access
== ACCESS_WRITE
549 && strcmp (archive_name_array
[0], "-") == 0)
552 else if (strcmp (archive_name_array
[0], "-") == 0)
554 read_full_records
= true; /* could be a pipe, be safe */
556 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
558 switch (wanted_access
)
563 enum compress_type type
;
565 archive
= STDIN_FILENO
;
567 type
= check_compressed_archive (&shortfile
);
568 if (type
!= ct_tar
&& type
!= ct_none
)
570 _("Archive is compressed. Use %s option"),
571 compress_option (type
)));
573 ERROR ((0, 0, _("This does not look like a tar archive")));
578 archive
= STDOUT_FILENO
;
579 if (!index_file_name
)
584 archive
= STDIN_FILENO
;
585 write_archive_to_stdout
= true;
586 record_end
= record_start
; /* set up for 1st record = # 0 */
587 if (!index_file_name
)
592 else if (verify_option
)
593 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
594 MODE_RW
, rsh_command_option
);
596 switch (wanted_access
)
599 archive
= open_compressed_archive ();
601 guess_seekable_archive ();
607 maybe_backup_file (archive_name_array
[0], 1);
610 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
615 archive
= rmtopen (archive_name_array
[0],
616 O_RDWR
| O_CREAT
| O_BINARY
,
617 MODE_RW
, rsh_command_option
);
619 switch (check_compressed_archive (NULL
))
627 _("Cannot update compressed archives")));
633 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
635 int saved_errno
= errno
;
640 open_fatal (archive_name_array
[0]);
643 sys_detect_dev_null_output ();
644 sys_save_archive_dev_ino ();
645 SET_BINARY_MODE (archive
);
647 switch (wanted_access
)
650 find_next_block (); /* read it in, check for EOF */
660 /* Perform a write to flush the buffer. */
666 checkpoint_run (true);
667 if (tape_length_option
&& tape_length_option
<= bytes_written
)
672 else if (dev_null_output
)
673 status
= record_size
;
675 status
= sys_write_archive_buffer ();
680 /* Handle write errors on the archive. Write errors are always fatal.
681 Hitting the end of a volume does not cause a write error unless the
682 write was the first record of the volume. */
684 archive_write_error (ssize_t status
)
686 /* It might be useful to know how much was written before the error
691 print_total_stats ();
695 write_fatal_details (*archive_name_cursor
, status
, record_size
);
698 /* Handle read errors on the archive. If the read should be retried,
699 return to the caller. */
701 archive_read_error (void)
703 read_error (*archive_name_cursor
);
705 if (record_start_block
== 0)
706 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
708 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
709 then give up on reading the archive. */
711 if (read_error_count
++ > READ_ERROR_MAX
)
712 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
721 if (fstat (archive
, &st
))
723 stat_diag (*archive_name_cursor
);
726 return S_ISBLK (st
.st_mode
) || S_ISCHR (st
.st_mode
);
730 short_read (size_t status
)
732 size_t left
; /* bytes left */
733 char *more
; /* pointer to next byte to read */
735 more
= record_start
->buffer
+ status
;
736 left
= record_size
- status
;
738 if (left
&& left
% BLOCKSIZE
== 0
740 && record_start_block
== 0 && status
!= 0
741 && archive_is_dev ())
743 unsigned long rsize
= status
/ BLOCKSIZE
;
745 ngettext ("Record size = %lu block",
746 "Record size = %lu blocks",
751 while (left
% BLOCKSIZE
!= 0
752 || (left
&& status
&& read_full_records
))
755 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
756 archive_read_error ();
761 if (! read_full_records
)
763 unsigned long rest
= record_size
- left
;
766 ngettext ("Unaligned block (%lu byte) in archive",
767 "Unaligned block (%lu bytes) in archive",
776 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
780 /* Flush the current buffer to/from the archive. */
784 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
785 record_start_block
+= record_end
- record_start
;
786 current_block
= record_start
;
787 record_end
= record_start
+ blocking_factor
;
789 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
791 access_mode
= ACCESS_WRITE
;
792 time_to_start_writing
= false;
803 flush_write_ptr (buffer_level
);
811 /* Backspace the archive descriptor by one record worth. If it's a
812 tape, MTIOCTOP will work. If it's something else, try to seek on
813 it. If we can't seek, we lose! */
815 backspace_output (void)
819 struct mtop operation
;
821 operation
.mt_op
= MTBSR
;
822 operation
.mt_count
= 1;
823 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
825 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
831 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
833 /* Seek back to the beginning of this record and start writing there. */
835 position
-= record_size
;
838 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
840 /* Lseek failed. Try a different method. */
843 _("Cannot backspace archive file; it may be unreadable without -i")));
845 /* Replace the first part of the record with NULs. */
847 if (record_start
->buffer
!= output_start
)
848 memset (record_start
->buffer
, 0,
849 output_start
- record_start
->buffer
);
855 seek_archive (off_t size
)
857 off_t start
= current_block_ordinal ();
860 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
862 size
-= skipped
* BLOCKSIZE
;
864 if (size
< record_size
)
868 /* Compute number of records to skip */
869 nrec
= size
/ record_size
;
870 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
874 if (offset
% record_size
)
875 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
877 /* Convert to number of records */
879 /* Compute number of skipped blocks */
880 nblk
= offset
- start
;
882 /* Update buffering info */
883 records_read
+= nblk
/ blocking_factor
;
884 record_start_block
= offset
- blocking_factor
;
885 current_block
= record_end
;
890 /* Close the archive file. */
894 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
897 if (current_block
> record_start
)
905 if (rmtclose (archive
) != 0)
906 close_error (*archive_name_cursor
);
908 sys_wait_for_child (child_pid
, hit_eof
);
910 tar_stat_destroy (¤t_stat_info
);
915 free (record_buffer
[0]);
916 free (record_buffer
[1]);
919 /* Called to initialize the global volume number. */
921 init_volume_number (void)
923 FILE *file
= fopen (volno_file_option
, "r");
927 if (fscanf (file
, "%d", &global_volno
) != 1
929 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
930 quotearg_colon (volno_file_option
)));
932 read_error (volno_file_option
);
933 if (fclose (file
) != 0)
934 close_error (volno_file_option
);
936 else if (errno
!= ENOENT
)
937 open_error (volno_file_option
);
940 /* Called to write out the closing global volume number. */
942 closeout_volume_number (void)
944 FILE *file
= fopen (volno_file_option
, "w");
948 fprintf (file
, "%d\n", global_volno
);
950 write_error (volno_file_option
);
951 if (fclose (file
) != 0)
952 close_error (volno_file_option
);
955 open_error (volno_file_option
);
960 increase_volume_number ()
963 if (global_volno
< 0)
964 FATAL_ERROR ((0, 0, _("Volume number overflow")));
969 change_tape_menu (FILE *read_file
)
971 char *input_buffer
= NULL
;
977 fputc ('\007', stderr
);
979 _("Prepare volume #%d for %s and hit return: "),
980 global_volno
+ 1, quote (*archive_name_cursor
));
983 if (getline (&input_buffer
, &size
, read_file
) <= 0)
985 WARN ((0, 0, _("EOF where user reply was expected")));
987 if (subcommand_option
!= EXTRACT_SUBCOMMAND
988 && subcommand_option
!= LIST_SUBCOMMAND
989 && subcommand_option
!= DIFF_SUBCOMMAND
)
990 WARN ((0, 0, _("WARNING: Archive is incomplete")));
995 if (input_buffer
[0] == '\n'
996 || input_buffer
[0] == 'y'
997 || input_buffer
[0] == 'Y')
1000 switch (input_buffer
[0])
1004 fprintf (stderr
, _("\
1005 n name Give a new file name for the next (and subsequent) volume(s)\n\
1007 y or newline Continue operation\n"));
1008 if (!restrict_option
)
1009 fprintf (stderr
, _(" ! Spawn a subshell\n"));
1010 fprintf (stderr
, _(" ? Print this list\n"));
1017 WARN ((0, 0, _("No new volume; exiting.\n")));
1019 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1020 && subcommand_option
!= LIST_SUBCOMMAND
1021 && subcommand_option
!= DIFF_SUBCOMMAND
)
1022 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1027 /* Get new file name. */
1033 for (name
= input_buffer
+ 1;
1034 *name
== ' ' || *name
== '\t';
1038 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1044 /* FIXME: the following allocation is never reclaimed. */
1045 *archive_name_cursor
= xstrdup (name
);
1049 fprintf (stderr
, "%s",
1050 _("File name not specified. Try again.\n"));
1055 if (!restrict_option
)
1063 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
1066 free (input_buffer
);
1069 /* We've hit the end of the old volume. Close it and open the next one.
1070 Return nonzero on success.
1073 new_volume (enum access_mode mode
)
1075 static FILE *read_file
;
1079 if (!read_file
&& !info_script_option
)
1080 /* FIXME: if fopen is used, it will never be closed. */
1081 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1088 assign_string (&volume_label
, NULL
);
1089 assign_string (&continued_file_name
, NULL
);
1090 continued_file_size
= continued_file_offset
= 0;
1091 current_block
= record_start
;
1093 if (rmtclose (archive
) != 0)
1094 close_error (*archive_name_cursor
);
1096 archive_name_cursor
++;
1097 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1099 archive_name_cursor
= archive_name_array
;
1107 /* We have to prompt from now on. */
1109 if (info_script_option
)
1111 if (volno_file_option
)
1112 closeout_volume_number ();
1113 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1114 FATAL_ERROR ((0, 0, _("%s command failed"),
1115 quote (info_script_option
)));
1118 change_tape_menu (read_file
);
1121 if (strcmp (archive_name_cursor
[0], "-") == 0)
1123 read_full_records
= true;
1124 archive
= STDIN_FILENO
;
1126 else if (verify_option
)
1127 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1128 rsh_command_option
);
1133 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1134 rsh_command_option
);
1135 guess_seekable_archive ();
1140 maybe_backup_file (*archive_name_cursor
, 1);
1141 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1142 rsh_command_option
);
1146 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1147 rsh_command_option
);
1153 open_warn (*archive_name_cursor
);
1154 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1155 undo_last_backup ();
1160 SET_BINARY_MODE (archive
);
1166 read_header0 (struct tar_stat_info
*info
)
1168 enum read_header rc
;
1170 tar_stat_init (info
);
1171 rc
= read_header (¤t_header
, info
, false);
1172 if (rc
== HEADER_SUCCESS
)
1174 set_next_block_after (current_header
);
1177 ERROR ((0, 0, _("This does not look like a tar archive")));
1185 union block
*header
;
1186 enum access_mode acc
;
1188 switch (subcommand_option
)
1190 case APPEND_SUBCOMMAND
:
1191 case CAT_SUBCOMMAND
:
1192 case UPDATE_SUBCOMMAND
:
1193 acc
= ACCESS_UPDATE
;
1201 if (!new_volume (acc
))
1204 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1206 archive_read_error ();
1208 if (status
!= record_size
)
1209 short_read (status
);
1211 header
= find_next_block ();
1215 switch (header
->header
.typeflag
)
1219 if (!read_header0 (&dummy
))
1221 xheader_decode (&dummy
); /* decodes values from the global header */
1222 tar_stat_destroy (&dummy
);
1225 /* We have read the extended header of the first member in
1226 this volume. Put it back, so next read_header works as
1228 current_block
= record_start
;
1233 case GNUTYPE_VOLHDR
:
1234 if (!read_header0 (&dummy
))
1236 tar_stat_destroy (&dummy
);
1237 assign_string (&volume_label
, current_header
->header
.name
);
1238 set_next_block_after (header
);
1239 header
= find_next_block ();
1240 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1244 case GNUTYPE_MULTIVOL
:
1245 if (!read_header0 (&dummy
))
1247 tar_stat_destroy (&dummy
);
1248 assign_string (&continued_file_name
, current_header
->header
.name
);
1249 continued_file_size
=
1250 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1251 continued_file_offset
=
1252 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1262 if (!continued_file_name
1263 || strcmp (continued_file_name
, real_s_name
))
1265 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1266 && strlen (real_s_name
) >= NAME_FIELD_SIZE
1267 && strncmp (continued_file_name
, real_s_name
,
1268 NAME_FIELD_SIZE
) == 0)
1270 _("%s is possibly continued on this volume: header contains truncated name"),
1271 quote (real_s_name
)));
1274 WARN ((0, 0, _("%s is not continued on this volume"),
1275 quote (real_s_name
)));
1280 s
= continued_file_size
+ continued_file_offset
;
1282 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1284 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1285 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1286 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1288 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1289 quote (continued_file_name
),
1290 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1291 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1292 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1296 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1298 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1299 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1300 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1302 WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
1303 STRINGIFY_BIGINT (real_s_totsize
, totsizebuf
),
1304 STRINGIFY_BIGINT (real_s_sizeleft
, s1buf
),
1305 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1311 increase_volume_number ();
1316 /* Check LABEL against the volume label, seen as a globbing
1317 pattern. Return true if the pattern matches. In case of failure,
1318 retry matching a volume sequence number before giving up in
1319 multi-volume mode. */
1321 check_label_pattern (const char *label
)
1326 if (fnmatch (volume_label_option
, label
, 0) == 0)
1329 if (!multi_volume_option
)
1332 string
= xmalloc (strlen (volume_label_option
)
1333 + sizeof VOLUME_LABEL_APPEND
+ 1);
1334 strcpy (string
, volume_label_option
);
1335 strcat (string
, VOLUME_LABEL_APPEND
);
1336 result
= fnmatch (string
, label
, 0) == 0;
1341 /* Check if the next block contains a volume label and if this matches
1342 the one given in the command line */
1344 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 (label
->header
.typeflag
== GNUTYPE_VOLHDR
)
1355 if (memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1356 assign_string (&volume_label
, label
->header
.name
);
1359 volume_label
= xmalloc (sizeof (label
->header
.name
) + 1);
1360 memcpy (volume_label
, label
->header
.name
,
1361 sizeof (label
->header
.name
));
1362 volume_label
[sizeof (label
->header
.name
)] = 0;
1365 else if (label
->header
.typeflag
== XGLTYPE
)
1367 struct tar_stat_info st
;
1368 tar_stat_init (&st
);
1369 xheader_read (&st
.xhdr
, label
,
1370 OFF_FROM_HEADER (label
->header
.size
));
1371 xheader_decode (&st
);
1372 tar_stat_destroy (&st
);
1377 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1378 quote (volume_label_option
)));
1380 if (!check_label_pattern (volume_label
))
1381 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1382 quote_n (0, volume_label
),
1383 quote_n (1, volume_label_option
)));
1386 /* Mark the archive with volume label STR. */
1388 _write_volume_label (const char *str
)
1390 if (archive_format
== POSIX_FORMAT
)
1391 xheader_store ("GNU.volume.label", &dummy
, str
);
1394 union block
*label
= find_next_block ();
1396 memset (label
, 0, BLOCKSIZE
);
1398 strcpy (label
->header
.name
, str
);
1399 assign_string (¤t_stat_info
.file_name
,
1400 label
->header
.name
);
1401 current_stat_info
.had_trailing_slash
=
1402 strip_trailing_slashes (current_stat_info
.file_name
);
1404 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1405 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1406 finish_header (¤t_stat_info
, label
, -1);
1407 set_next_block_after (label
);
1411 #define VOL_SUFFIX "Volume"
1413 /* Add a volume label to a part of multi-volume archive */
1415 add_volume_label (void)
1417 char buf
[UINTMAX_STRSIZE_BOUND
];
1418 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1419 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1421 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1422 _write_volume_label (s
);
1429 if (archive_format
== POSIX_FORMAT
)
1431 off_t block_ordinal
;
1433 struct tar_stat_info st
;
1434 static size_t real_s_part_no
; /* FIXME */
1437 memset (&st
, 0, sizeof st
);
1438 st
.orig_file_name
= st
.file_name
= real_s_name
;
1439 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1440 st
.stat
.st_uid
= getuid ();
1441 st
.stat
.st_gid
= getgid ();
1442 st
.orig_file_name
= xheader_format_name (&st
,
1443 "%d/GNUFileParts.%p/%f.%n",
1445 st
.file_name
= st
.orig_file_name
;
1446 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1448 block_ordinal
= current_block_ordinal ();
1449 blk
= start_header (&st
);
1451 abort (); /* FIXME */
1452 finish_header (&st
, blk
, block_ordinal
);
1453 free (st
.orig_file_name
);
1458 /* Add a volume label to the current archive */
1460 write_volume_label (void)
1462 if (multi_volume_option
)
1463 add_volume_label ();
1465 _write_volume_label (volume_label_option
);
1468 /* Write GNU multi-volume header */
1470 gnu_add_multi_volume_header (void)
1473 union block
*block
= find_next_block ();
1475 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1477 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1478 quotearg_colon (real_s_name
)));
1480 memset (block
, 0, BLOCKSIZE
);
1482 /* FIXME: Michael P Urban writes: [a long name file] is being written
1483 when a new volume rolls around [...] Looks like the wrong value is
1484 being preserved in real_s_name, though. */
1486 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1487 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1489 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1490 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1491 block
->oldgnu_header
.offset
);
1493 tmp
= verbose_option
;
1495 finish_header (¤t_stat_info
, block
, -1);
1496 verbose_option
= tmp
;
1497 set_next_block_after (block
);
1500 /* Add a multi volume header to the current archive. The exact header format
1501 depends on the archive format. */
1503 add_multi_volume_header (void)
1505 if (archive_format
== POSIX_FORMAT
)
1507 off_t d
= real_s_totsize
- real_s_sizeleft
;
1508 xheader_store ("GNU.volume.filename", &dummy
, real_s_name
);
1509 xheader_store ("GNU.volume.size", &dummy
, &real_s_sizeleft
);
1510 xheader_store ("GNU.volume.offset", &dummy
, &d
);
1513 gnu_add_multi_volume_header ();
1516 /* Synchronize multi-volume globals */
1518 multi_volume_sync ()
1520 if (multi_volume_option
)
1524 assign_string (&real_s_name
,
1525 safer_name_suffix (save_name
, false,
1526 absolute_names_option
));
1527 real_s_totsize
= save_totsize
;
1528 real_s_sizeleft
= save_sizeleft
;
1532 assign_string (&real_s_name
, 0);
1534 real_s_sizeleft
= 0;
1540 /* Low-level flush functions */
1542 /* Simple flush read (no multi-volume or label extensions) */
1544 simple_flush_read (void)
1546 size_t status
; /* result from system call */
1548 checkpoint_run (false);
1550 /* Clear the count of errors. This only applies to a single call to
1553 read_error_count
= 0; /* clear error count */
1555 if (write_archive_to_stdout
&& record_start_block
!= 0)
1557 archive
= STDOUT_FILENO
;
1558 status
= sys_write_archive_buffer ();
1559 archive
= STDIN_FILENO
;
1560 if (status
!= record_size
)
1561 archive_write_error (status
);
1566 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1567 if (status
== record_size
)
1572 if (status
== SAFE_READ_ERROR
)
1574 archive_read_error ();
1575 continue; /* try again */
1579 short_read (status
);
1582 /* Simple flush write (no multi-volume or label extensions) */
1584 simple_flush_write (size_t level
__attribute__((unused
)))
1588 status
= _flush_write ();
1589 if (status
!= record_size
)
1590 archive_write_error (status
);
1594 bytes_written
+= status
;
1599 /* GNU flush functions. These support multi-volume and archive labels in
1600 GNU and PAX archive formats. */
1603 _gnu_flush_read (void)
1605 size_t status
; /* result from system call */
1607 checkpoint_run (false);
1609 /* Clear the count of errors. This only applies to a single call to
1612 read_error_count
= 0; /* clear error count */
1614 if (write_archive_to_stdout
&& record_start_block
!= 0)
1616 archive
= STDOUT_FILENO
;
1617 status
= sys_write_archive_buffer ();
1618 archive
= STDIN_FILENO
;
1619 if (status
!= record_size
)
1620 archive_write_error (status
);
1623 multi_volume_sync ();
1627 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1628 if (status
== record_size
)
1634 /* The condition below used to include
1635 || (status > 0 && !read_full_records)
1636 This is incorrect since even if new_volume() succeeds, the
1637 subsequent call to rmtread will overwrite the chunk of data
1638 already read in the buffer, so the processing will fail */
1640 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1641 && multi_volume_option
)
1643 while (!try_new_volume ())
1645 if (current_block
== record_end
)
1646 /* Necessary for blocking_factor == 1 */
1650 else if (status
== SAFE_READ_ERROR
)
1652 archive_read_error ();
1657 short_read (status
);
1661 gnu_flush_read (void)
1663 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1665 flush_read_ptr
= gnu_flush_read
;
1669 _gnu_flush_write (size_t buffer_level
)
1672 union block
*header
;
1678 status
= _flush_write ();
1679 if (status
!= record_size
&& !multi_volume_option
)
1680 archive_write_error (status
);
1685 bytes_written
+= status
;
1688 if (status
== record_size
)
1690 multi_volume_sync ();
1694 if (status
% BLOCKSIZE
)
1696 ERROR ((0, 0, _("write did not end on a block boundary")));
1697 archive_write_error (status
);
1700 /* In multi-volume mode. */
1701 /* ENXIO is for the UNIX PC. */
1702 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1703 archive_write_error (status
);
1705 real_s_sizeleft
-= status
;
1706 if (!new_volume (ACCESS_WRITE
))
1709 tar_stat_destroy (&dummy
);
1711 increase_volume_number ();
1712 prev_written
+= bytes_written
;
1715 copy_ptr
= record_start
->buffer
+ status
;
1716 copy_size
= buffer_level
- status
;
1718 /* Switch to the next buffer */
1719 record_index
= !record_index
;
1722 if (volume_label_option
)
1723 add_volume_label ();
1726 add_multi_volume_header ();
1728 write_extended (true, &dummy
, find_next_block ());
1729 tar_stat_destroy (&dummy
);
1732 add_chunk_header ();
1733 wrt
= bytes_written
;
1734 header
= find_next_block ();
1735 bufsize
= available_space_after (header
);
1736 while (bufsize
< copy_size
)
1738 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1739 copy_ptr
+= bufsize
;
1740 copy_size
-= bufsize
;
1741 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1742 header
= find_next_block ();
1743 bufsize
= available_space_after (header
);
1745 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1746 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1747 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1748 if (multi_volume_option
&& wrt
< bytes_written
)
1750 /* The value of bytes_written has changed while moving data;
1751 that means that flush_archive was executed at least once in
1752 between, and, as a consequence, copy_size bytes were not written
1753 to disk. We need to update sizeleft variables to compensate for
1755 save_sizeleft
+= copy_size
;
1756 multi_volume_sync ();
1762 gnu_flush_write (size_t buffer_level
)
1764 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1765 _gnu_flush_write (buffer_level
);
1766 flush_write_ptr
= gnu_flush_write
;
1778 flush_write_ptr (record_size
);
1782 open_archive (enum access_mode wanted_access
)
1784 flush_read_ptr
= gnu_flush_read
;
1785 flush_write_ptr
= gnu_flush_write
;
1787 _open_archive (wanted_access
);
1788 switch (wanted_access
)
1791 if (volume_label_option
)
1792 match_volume_label ();
1796 records_written
= 0;
1797 if (volume_label_option
)
1798 write_volume_label ();
1804 set_volume_start_time ();