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
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 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 As least EXTERN like this one as possible. (?? --gray)
81 FIXME: Either eliminate it or move it to common.h.
83 extern bool time_to_start_writing
;
85 bool write_archive_to_stdout
;
87 void (*flush_write_ptr
) (size_t);
88 void (*flush_read_ptr
) (void);
92 char *continued_file_name
;
93 uintmax_t continued_file_size
;
94 uintmax_t continued_file_offset
;
97 static int volno
= 1; /* which volume of a multi-volume tape we're
99 static int global_volno
= 1; /* volume number to print in external
102 bool write_archive_to_stdout
;
104 /* Used by flush_read and flush_write to store the real info about saved
106 static char *real_s_name
;
107 static off_t real_s_totsize
;
108 static off_t real_s_sizeleft
;
111 /* Multi-volume tracking support */
112 static char *save_name
; /* name of the file we are currently writing */
113 static off_t save_totsize
; /* total size of file we are writing, only
114 valid if save_name is nonzero */
115 static off_t save_sizeleft
; /* where we are in the file we are writing,
116 only valid if save_name is nonzero */
119 static struct tar_stat_info dummy
;
122 buffer_write_global_xheader ()
124 xheader_write_global (&dummy
.xhdr
);
128 mv_begin (struct tar_stat_info
*st
)
130 if (multi_volume_option
)
132 assign_string (&save_name
, st
->orig_file_name
);
133 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
140 if (multi_volume_option
)
141 assign_string (&save_name
, 0);
145 mv_total_size (off_t size
)
151 mv_size_left (off_t size
)
153 save_sizeleft
= size
;
160 clear_read_error_count (void)
162 read_error_count
= 0;
166 /* Time-related functions */
173 gettime (&start_time
);
174 volume_start_time
= start_time
;
175 last_stat_time
= start_time
;
179 set_volume_start_time ()
181 gettime (&volume_start_time
);
182 last_stat_time
= volume_start_time
;
190 duration
+= ((now
.tv_sec
- last_stat_time
.tv_sec
)
191 + (now
.tv_nsec
- last_stat_time
.tv_nsec
) / 1e9
);
192 gettime (&last_stat_time
);
196 /* Compression detection */
199 ct_tar
, /* Plain tar file */
200 ct_none
, /* Unknown compression type */
211 enum compress_type type
;
218 static struct zip_magic
const magic
[] = {
221 { ct_compress
, 2, "\037\235", COMPRESS_PROGRAM
, "-Z" },
222 { ct_gzip
, 2, "\037\213", GZIP_PROGRAM
, "-z" },
223 { ct_bzip2
, 3, "BZh", BZIP2_PROGRAM
, "-j" },
224 { ct_lzma
, 6, "\xFFLZMA", LZMA_PROGRAM
, "--lzma" },
225 { ct_lzop
, 4, "\211LZO", LZOP_PROGRAM
, "--lzop" },
226 { ct_xz
, 6, "\0xFD7zXZ", XZ_PROGRAM
, "-J" },
229 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
231 #define compress_option(t) magic[t].option
232 #define compress_program(t) magic[t].program
234 /* Check if the file ARCHIVE is a compressed archive. */
236 check_compressed_archive (bool *pshort
)
238 struct zip_magic
const *p
;
245 /* Prepare global data needed for find_next_block: */
246 record_end
= record_start
; /* set up for 1st record = # 0 */
247 sfr
= read_full_records
;
248 read_full_records
= true; /* Suppress fatal error on reading a partial
250 *pshort
= find_next_block () == 0;
252 /* Restore global values */
253 read_full_records
= sfr
;
255 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
256 /* Probably a valid header */
259 for (p
= magic
+ 2; p
< magic
+ NMAGIC
; p
++)
260 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
266 /* Guess if the archive is seekable. */
268 guess_seekable_archive ()
272 if (subcommand_option
== DELETE_SUBCOMMAND
)
274 /* The current code in delete.c is based on the assumption that
275 skip_member() reads all data from the archive. So, we should
276 make sure it won't use seeks. On the other hand, the same code
277 depends on the ability to backspace a record in the archive,
278 so setting seekable_archive to false is technically incorrect.
279 However, it is tested only in skip_member(), so it's not a
281 seekable_archive
= false;
284 if (seek_option
!= -1)
286 seekable_archive
= !!seek_option
;
290 if (!multi_volume_option
&& !use_compress_program_option
291 && fstat (archive
, &st
) == 0)
292 seekable_archive
= S_ISREG (st
.st_mode
);
294 seekable_archive
= false;
297 /* Open an archive named archive_name_array[0]. Detect if it is
298 a compressed archive of known type and use corresponding decompression
301 open_compressed_archive ()
303 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
304 MODE_RW
, rsh_command_option
);
308 if (!multi_volume_option
)
310 if (!use_compress_program_option
)
313 enum compress_type type
= check_compressed_archive (&shortfile
);
319 ERROR ((0, 0, _("This does not look like a tar archive")));
324 ERROR ((0, 0, _("This does not look like a tar archive")));
325 set_comression_program_by_suffix (archive_name_array
[0], NULL
);
326 if (!use_compress_program_option
)
331 use_compress_program_option
= compress_program (type
);
336 /* FD is not needed any more */
339 hit_eof
= false; /* It might have been set by find_next_block in
340 check_compressed_archive */
342 /* Open compressed archive */
343 child_pid
= sys_child_open_for_uncompress ();
344 read_full_records
= true;
348 record_end
= record_start
; /* set up for 1st record = # 0 */
355 print_stats (FILE *fp
, const char *text
, tarlong numbytes
)
357 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
358 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
359 char rate
[LONGEST_HUMAN_READABLE
+ 1];
361 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
363 sprintf (bytes
, TARLONG_FORMAT
, numbytes
);
365 fprintf (fp
, "%s: %s (%s, %s/s)\n",
367 human_readable (numbytes
, abbr
, human_opts
, 1, 1),
368 (0 < duration
&& numbytes
/ duration
< (uintmax_t) -1
369 ? human_readable (numbytes
/ duration
, rate
, human_opts
, 1, 1)
376 switch (subcommand_option
)
378 case CREATE_SUBCOMMAND
:
380 case UPDATE_SUBCOMMAND
:
381 case APPEND_SUBCOMMAND
:
382 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
383 print_stats (stderr
, _("Total bytes written"),
384 prev_written
+ bytes_written
);
387 case DELETE_SUBCOMMAND
:
389 char buf
[UINTMAX_STRSIZE_BOUND
];
390 print_stats (stderr
, _("Total bytes read"),
391 records_read
* record_size
);
392 print_stats (stderr
, _("Total bytes written"),
393 prev_written
+ bytes_written
);
394 fprintf (stderr
, _("Total bytes deleted: %s\n"),
395 STRINGIFY_BIGINT ((records_read
- records_skipped
)
397 - (prev_written
+ bytes_written
), buf
));
401 case EXTRACT_SUBCOMMAND
:
402 case LIST_SUBCOMMAND
:
403 case DIFF_SUBCOMMAND
:
404 print_stats (stderr
, _("Total bytes read"),
405 records_read
* record_size
);
413 /* Compute and return the block ordinal at current_block. */
415 current_block_ordinal (void)
417 return record_start_block
+ (current_block
- record_start
);
420 /* If the EOF flag is set, reset it, as well as current_block, etc. */
427 current_block
= record_start
;
428 record_end
= record_start
+ blocking_factor
;
429 access_mode
= ACCESS_WRITE
;
433 /* Return the location of the next available input or output block.
434 Return zero for EOF. Once we have returned zero, we just keep returning
435 it, to avoid accidentally going on to the next file on the tape. */
437 find_next_block (void)
439 if (current_block
== record_end
)
444 if (current_block
== record_end
)
450 return current_block
;
453 /* Indicate that we have used all blocks up thru BLOCK. */
455 set_next_block_after (union block
*block
)
457 while (block
>= current_block
)
460 /* Do *not* flush the archive here. If we do, the same argument to
461 set_next_block_after could mean the next block (if the input record
462 is exactly one block long), which is not what is intended. */
464 if (current_block
> record_end
)
468 /* Return the number of bytes comprising the space between POINTER
469 through the end of the current buffer of blocks. This space is
470 available for filling with data, or taking data from. POINTER is
471 usually (but not always) the result of previous find_next_block call. */
473 available_space_after (union block
*pointer
)
475 return record_end
->buffer
- pointer
->buffer
;
478 /* Close file having descriptor FD, and abort if close unsuccessful. */
483 close_error (_("(pipe)"));
489 if (! record_buffer_aligned
[record_index
])
490 record_buffer_aligned
[record_index
] =
491 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
493 record_start
= record_buffer_aligned
[record_index
];
494 current_block
= record_start
;
495 record_end
= record_start
+ blocking_factor
;
498 /* Open an archive file. The argument specifies whether we are
499 reading or writing, or both. */
501 _open_archive (enum access_mode wanted_access
)
503 int backed_up_flag
= 0;
505 if (record_size
== 0)
506 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
508 if (archive_names
== 0)
509 FATAL_ERROR ((0, 0, _("No archive name given")));
511 tar_stat_destroy (¤t_stat_info
);
518 /* When updating the archive, we start with reading. */
519 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
521 read_full_records
= read_full_records_option
;
525 if (use_compress_program_option
)
527 switch (wanted_access
)
530 child_pid
= sys_child_open_for_uncompress ();
531 read_full_records
= true;
532 record_end
= record_start
; /* set up for 1st record = # 0 */
536 child_pid
= sys_child_open_for_compress ();
540 abort (); /* Should not happen */
545 && wanted_access
== ACCESS_WRITE
546 && strcmp (archive_name_array
[0], "-") == 0)
549 else if (strcmp (archive_name_array
[0], "-") == 0)
551 read_full_records
= true; /* could be a pipe, be safe */
553 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
555 switch (wanted_access
)
560 enum compress_type type
;
562 archive
= STDIN_FILENO
;
564 type
= check_compressed_archive (&shortfile
);
565 if (type
!= ct_tar
&& type
!= ct_none
)
567 _("Archive is compressed. Use %s option"),
568 compress_option (type
)));
570 ERROR ((0, 0, _("This does not look like a tar archive")));
575 archive
= STDOUT_FILENO
;
576 if (!index_file_name
)
581 archive
= STDIN_FILENO
;
582 write_archive_to_stdout
= true;
583 record_end
= record_start
; /* set up for 1st record = # 0 */
584 if (!index_file_name
)
589 else if (verify_option
)
590 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
591 MODE_RW
, rsh_command_option
);
593 switch (wanted_access
)
596 archive
= open_compressed_archive ();
598 guess_seekable_archive ();
604 maybe_backup_file (archive_name_array
[0], 1);
607 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
612 archive
= rmtopen (archive_name_array
[0],
613 O_RDWR
| O_CREAT
| O_BINARY
,
614 MODE_RW
, rsh_command_option
);
616 switch (check_compressed_archive (NULL
))
624 _("Cannot update compressed archives")));
630 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
632 int saved_errno
= errno
;
637 open_fatal (archive_name_array
[0]);
640 sys_detect_dev_null_output ();
641 sys_save_archive_dev_ino ();
642 SET_BINARY_MODE (archive
);
644 switch (wanted_access
)
647 find_next_block (); /* read it in, check for EOF */
657 /* Perform a write to flush the buffer. */
663 checkpoint_run (true);
664 if (tape_length_option
&& tape_length_option
<= bytes_written
)
669 else if (dev_null_output
)
670 status
= record_size
;
672 status
= sys_write_archive_buffer ();
677 /* Handle write errors on the archive. Write errors are always fatal.
678 Hitting the end of a volume does not cause a write error unless the
679 write was the first record of the volume. */
681 archive_write_error (ssize_t status
)
683 /* It might be useful to know how much was written before the error
688 print_total_stats ();
692 write_fatal_details (*archive_name_cursor
, status
, record_size
);
695 /* Handle read errors on the archive. If the read should be retried,
696 return to the caller. */
698 archive_read_error (void)
700 read_error (*archive_name_cursor
);
702 if (record_start_block
== 0)
703 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
705 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
706 then give up on reading the archive. */
708 if (read_error_count
++ > READ_ERROR_MAX
)
709 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
718 if (fstat (archive
, &st
))
720 stat_diag (*archive_name_cursor
);
723 return S_ISBLK (st
.st_mode
) || S_ISCHR (st
.st_mode
);
727 short_read (size_t status
)
729 size_t left
; /* bytes left */
730 char *more
; /* pointer to next byte to read */
732 more
= record_start
->buffer
+ status
;
733 left
= record_size
- status
;
735 if (left
&& left
% BLOCKSIZE
== 0
737 && record_start_block
== 0 && status
!= 0
738 && archive_is_dev ())
740 unsigned long rsize
= status
/ BLOCKSIZE
;
742 ngettext ("Record size = %lu block",
743 "Record size = %lu blocks",
748 while (left
% BLOCKSIZE
!= 0
749 || (left
&& status
&& read_full_records
))
752 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
753 archive_read_error ();
758 if (! read_full_records
)
760 unsigned long rest
= record_size
- left
;
763 ngettext ("Unaligned block (%lu byte) in archive",
764 "Unaligned block (%lu bytes) in archive",
773 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
777 /* Flush the current buffer to/from the archive. */
781 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
782 record_start_block
+= record_end
- record_start
;
783 current_block
= record_start
;
784 record_end
= record_start
+ blocking_factor
;
786 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
788 access_mode
= ACCESS_WRITE
;
789 time_to_start_writing
= false;
800 flush_write_ptr (buffer_level
);
808 /* Backspace the archive descriptor by one record worth. If it's a
809 tape, MTIOCTOP will work. If it's something else, try to seek on
810 it. If we can't seek, we lose! */
812 backspace_output (void)
816 struct mtop operation
;
818 operation
.mt_op
= MTBSR
;
819 operation
.mt_count
= 1;
820 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
822 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
828 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
830 /* Seek back to the beginning of this record and start writing there. */
832 position
-= record_size
;
835 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
837 /* Lseek failed. Try a different method. */
840 _("Cannot backspace archive file; it may be unreadable without -i")));
842 /* Replace the first part of the record with NULs. */
844 if (record_start
->buffer
!= output_start
)
845 memset (record_start
->buffer
, 0,
846 output_start
- record_start
->buffer
);
852 seek_archive (off_t size
)
854 off_t start
= current_block_ordinal ();
857 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
859 size
-= skipped
* BLOCKSIZE
;
861 if (size
< record_size
)
865 /* Compute number of records to skip */
866 nrec
= size
/ record_size
;
867 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
871 if (offset
% record_size
)
872 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
874 /* Convert to number of records */
876 /* Compute number of skipped blocks */
877 nblk
= offset
- start
;
879 /* Update buffering info */
880 records_read
+= nblk
/ blocking_factor
;
881 record_start_block
= offset
- blocking_factor
;
882 current_block
= record_end
;
887 /* Close the archive file. */
891 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
894 if (current_block
> record_start
)
902 if (rmtclose (archive
) != 0)
903 close_error (*archive_name_cursor
);
905 sys_wait_for_child (child_pid
, hit_eof
);
907 tar_stat_destroy (¤t_stat_info
);
912 free (record_buffer
[0]);
913 free (record_buffer
[1]);
916 /* Called to initialize the global volume number. */
918 init_volume_number (void)
920 FILE *file
= fopen (volno_file_option
, "r");
924 if (fscanf (file
, "%d", &global_volno
) != 1
926 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
927 quotearg_colon (volno_file_option
)));
929 read_error (volno_file_option
);
930 if (fclose (file
) != 0)
931 close_error (volno_file_option
);
933 else if (errno
!= ENOENT
)
934 open_error (volno_file_option
);
937 /* Called to write out the closing global volume number. */
939 closeout_volume_number (void)
941 FILE *file
= fopen (volno_file_option
, "w");
945 fprintf (file
, "%d\n", global_volno
);
947 write_error (volno_file_option
);
948 if (fclose (file
) != 0)
949 close_error (volno_file_option
);
952 open_error (volno_file_option
);
957 increase_volume_number ()
960 if (global_volno
< 0)
961 FATAL_ERROR ((0, 0, _("Volume number overflow")));
966 change_tape_menu (FILE *read_file
)
968 char *input_buffer
= NULL
;
974 fputc ('\007', stderr
);
976 _("Prepare volume #%d for %s and hit return: "),
977 global_volno
+ 1, quote (*archive_name_cursor
));
980 if (getline (&input_buffer
, &size
, read_file
) <= 0)
982 WARN ((0, 0, _("EOF where user reply was expected")));
984 if (subcommand_option
!= EXTRACT_SUBCOMMAND
985 && subcommand_option
!= LIST_SUBCOMMAND
986 && subcommand_option
!= DIFF_SUBCOMMAND
)
987 WARN ((0, 0, _("WARNING: Archive is incomplete")));
992 if (input_buffer
[0] == '\n'
993 || input_buffer
[0] == 'y'
994 || input_buffer
[0] == 'Y')
997 switch (input_buffer
[0])
1001 fprintf (stderr
, _("\
1002 n name Give a new file name for the next (and subsequent) volume(s)\n\
1004 y or newline Continue operation\n"));
1005 if (!restrict_option
)
1006 fprintf (stderr
, _(" ! Spawn a subshell\n"));
1007 fprintf (stderr
, _(" ? Print this list\n"));
1014 WARN ((0, 0, _("No new volume; exiting.\n")));
1016 if (subcommand_option
!= EXTRACT_SUBCOMMAND
1017 && subcommand_option
!= LIST_SUBCOMMAND
1018 && subcommand_option
!= DIFF_SUBCOMMAND
)
1019 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1024 /* Get new file name. */
1030 for (name
= input_buffer
+ 1;
1031 *name
== ' ' || *name
== '\t';
1035 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
1041 /* FIXME: the following allocation is never reclaimed. */
1042 *archive_name_cursor
= xstrdup (name
);
1046 fprintf (stderr
, "%s",
1047 _("File name not specified. Try again.\n"));
1052 if (!restrict_option
)
1060 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
1063 free (input_buffer
);
1066 /* We've hit the end of the old volume. Close it and open the next one.
1067 Return nonzero on success.
1070 new_volume (enum access_mode mode
)
1072 static FILE *read_file
;
1076 if (!read_file
&& !info_script_option
)
1077 /* FIXME: if fopen is used, it will never be closed. */
1078 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1085 assign_string (&volume_label
, NULL
);
1086 assign_string (&continued_file_name
, NULL
);
1087 continued_file_size
= continued_file_offset
= 0;
1088 current_block
= record_start
;
1090 if (rmtclose (archive
) != 0)
1091 close_error (*archive_name_cursor
);
1093 archive_name_cursor
++;
1094 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1096 archive_name_cursor
= archive_name_array
;
1104 /* We have to prompt from now on. */
1106 if (info_script_option
)
1108 if (volno_file_option
)
1109 closeout_volume_number ();
1110 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1111 FATAL_ERROR ((0, 0, _("%s command failed"),
1112 quote (info_script_option
)));
1115 change_tape_menu (read_file
);
1118 if (strcmp (archive_name_cursor
[0], "-") == 0)
1120 read_full_records
= true;
1121 archive
= STDIN_FILENO
;
1123 else if (verify_option
)
1124 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1125 rsh_command_option
);
1130 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1131 rsh_command_option
);
1132 guess_seekable_archive ();
1137 maybe_backup_file (*archive_name_cursor
, 1);
1138 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1139 rsh_command_option
);
1143 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1144 rsh_command_option
);
1150 open_warn (*archive_name_cursor
);
1151 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1152 undo_last_backup ();
1157 SET_BINARY_MODE (archive
);
1163 read_header0 (struct tar_stat_info
*info
)
1165 enum read_header rc
;
1167 tar_stat_init (info
);
1168 rc
= read_header (¤t_header
, info
, false);
1169 if (rc
== HEADER_SUCCESS
)
1171 set_next_block_after (current_header
);
1174 ERROR ((0, 0, _("This does not look like a tar archive")));
1182 union block
*header
;
1183 enum access_mode acc
;
1185 switch (subcommand_option
)
1187 case APPEND_SUBCOMMAND
:
1188 case CAT_SUBCOMMAND
:
1189 case UPDATE_SUBCOMMAND
:
1190 acc
= ACCESS_UPDATE
;
1198 if (!new_volume (acc
))
1201 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1203 archive_read_error ();
1205 if (status
!= record_size
)
1206 short_read (status
);
1208 header
= find_next_block ();
1212 switch (header
->header
.typeflag
)
1216 if (!read_header0 (&dummy
))
1218 xheader_decode (&dummy
); /* decodes values from the global header */
1219 tar_stat_destroy (&dummy
);
1222 /* We have read the extended header of the first member in
1223 this volume. Put it back, so next read_header works as
1225 current_block
= record_start
;
1230 case GNUTYPE_VOLHDR
:
1231 if (!read_header0 (&dummy
))
1233 tar_stat_destroy (&dummy
);
1234 assign_string (&volume_label
, current_header
->header
.name
);
1235 set_next_block_after (header
);
1236 header
= find_next_block ();
1237 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1241 case GNUTYPE_MULTIVOL
:
1242 if (!read_header0 (&dummy
))
1244 tar_stat_destroy (&dummy
);
1245 assign_string (&continued_file_name
, current_header
->header
.name
);
1246 continued_file_size
=
1247 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1248 continued_file_offset
=
1249 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1259 if (!continued_file_name
1260 || strcmp (continued_file_name
, real_s_name
))
1262 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1263 && strlen (real_s_name
) >= NAME_FIELD_SIZE
1264 && strncmp (continued_file_name
, real_s_name
,
1265 NAME_FIELD_SIZE
) == 0)
1267 _("%s is possibly continued on this volume: header contains truncated name"),
1268 quote (real_s_name
)));
1271 WARN ((0, 0, _("%s is not continued on this volume"),
1272 quote (real_s_name
)));
1277 s
= continued_file_size
+ continued_file_offset
;
1279 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1281 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1282 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1283 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1285 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1286 quote (continued_file_name
),
1287 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1288 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1289 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1293 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1295 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1296 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1297 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1299 WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
1300 STRINGIFY_BIGINT (real_s_totsize
, totsizebuf
),
1301 STRINGIFY_BIGINT (real_s_sizeleft
, s1buf
),
1302 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1308 increase_volume_number ();
1313 #define VOLUME_TEXT " Volume "
1314 #define VOLUME_TEXT_LEN (sizeof VOLUME_TEXT - 1)
1317 drop_volume_label_suffix (const char *label
)
1320 size_t len
= strlen (label
);
1325 for (p
= label
+ len
- 1; p
> label
&& isdigit ((unsigned char) *p
); p
--)
1327 if (p
> label
&& p
- (VOLUME_TEXT_LEN
- 1) > label
)
1329 p
-= VOLUME_TEXT_LEN
- 1;
1330 if (memcmp (p
, VOLUME_TEXT
, VOLUME_TEXT_LEN
) == 0)
1332 char *s
= xmalloc ((len
= p
- label
) + 1);
1333 memcpy (s
, label
, len
);
1342 /* Check LABEL against the volume label, seen as a globbing
1343 pattern. Return true if the pattern matches. In case of failure,
1344 retry matching a volume sequence number before giving up in
1345 multi-volume mode. */
1347 check_label_pattern (const char *label
)
1352 if (fnmatch (volume_label_option
, label
, 0) == 0)
1355 if (!multi_volume_option
)
1358 string
= drop_volume_label_suffix (label
);
1361 result
= fnmatch (string
, volume_label_option
, 0) == 0;
1367 /* Check if the next block contains a volume label and if this matches
1368 the one given in the command line */
1370 match_volume_label (void)
1374 union block
*label
= find_next_block ();
1377 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1378 quote (volume_label_option
)));
1379 if (label
->header
.typeflag
== GNUTYPE_VOLHDR
)
1381 if (memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1382 assign_string (&volume_label
, label
->header
.name
);
1385 volume_label
= xmalloc (sizeof (label
->header
.name
) + 1);
1386 memcpy (volume_label
, label
->header
.name
,
1387 sizeof (label
->header
.name
));
1388 volume_label
[sizeof (label
->header
.name
)] = 0;
1391 else if (label
->header
.typeflag
== XGLTYPE
)
1393 struct tar_stat_info st
;
1394 tar_stat_init (&st
);
1395 xheader_read (&st
.xhdr
, label
,
1396 OFF_FROM_HEADER (label
->header
.size
));
1397 xheader_decode (&st
);
1398 tar_stat_destroy (&st
);
1403 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1404 quote (volume_label_option
)));
1406 if (!check_label_pattern (volume_label
))
1407 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1408 quote_n (0, volume_label
),
1409 quote_n (1, volume_label_option
)));
1412 /* Mark the archive with volume label STR. */
1414 _write_volume_label (const char *str
)
1416 if (archive_format
== POSIX_FORMAT
)
1417 xheader_store ("GNU.volume.label", &dummy
, str
);
1420 union block
*label
= find_next_block ();
1422 memset (label
, 0, BLOCKSIZE
);
1424 strcpy (label
->header
.name
, str
);
1425 assign_string (¤t_stat_info
.file_name
,
1426 label
->header
.name
);
1427 current_stat_info
.had_trailing_slash
=
1428 strip_trailing_slashes (current_stat_info
.file_name
);
1430 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1431 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1432 finish_header (¤t_stat_info
, label
, -1);
1433 set_next_block_after (label
);
1437 #define VOL_SUFFIX "Volume"
1439 /* Add a volume label to a part of multi-volume archive */
1441 add_volume_label (void)
1443 char buf
[UINTMAX_STRSIZE_BOUND
];
1444 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1445 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1447 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1448 _write_volume_label (s
);
1455 if (archive_format
== POSIX_FORMAT
)
1457 off_t block_ordinal
;
1459 struct tar_stat_info st
;
1460 static size_t real_s_part_no
; /* FIXME */
1463 memset (&st
, 0, sizeof st
);
1464 st
.orig_file_name
= st
.file_name
= real_s_name
;
1465 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1466 st
.stat
.st_uid
= getuid ();
1467 st
.stat
.st_gid
= getgid ();
1468 st
.orig_file_name
= xheader_format_name (&st
,
1469 "%d/GNUFileParts.%p/%f.%n",
1471 st
.file_name
= st
.orig_file_name
;
1472 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1474 block_ordinal
= current_block_ordinal ();
1475 blk
= start_header (&st
);
1477 abort (); /* FIXME */
1478 finish_header (&st
, blk
, block_ordinal
);
1479 free (st
.orig_file_name
);
1484 /* Add a volume label to the current archive */
1486 write_volume_label (void)
1488 if (multi_volume_option
)
1489 add_volume_label ();
1491 _write_volume_label (volume_label_option
);
1494 /* Write GNU multi-volume header */
1496 gnu_add_multi_volume_header (void)
1499 union block
*block
= find_next_block ();
1501 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1503 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1504 quotearg_colon (real_s_name
)));
1506 memset (block
, 0, BLOCKSIZE
);
1508 /* FIXME: Michael P Urban writes: [a long name file] is being written
1509 when a new volume rolls around [...] Looks like the wrong value is
1510 being preserved in real_s_name, though. */
1512 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1513 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1515 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1516 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1517 block
->oldgnu_header
.offset
);
1519 tmp
= verbose_option
;
1521 finish_header (¤t_stat_info
, block
, -1);
1522 verbose_option
= tmp
;
1523 set_next_block_after (block
);
1526 /* Add a multi volume header to the current archive. The exact header format
1527 depends on the archive format. */
1529 add_multi_volume_header (void)
1531 if (archive_format
== POSIX_FORMAT
)
1533 off_t d
= real_s_totsize
- real_s_sizeleft
;
1534 xheader_store ("GNU.volume.filename", &dummy
, real_s_name
);
1535 xheader_store ("GNU.volume.size", &dummy
, &real_s_sizeleft
);
1536 xheader_store ("GNU.volume.offset", &dummy
, &d
);
1539 gnu_add_multi_volume_header ();
1542 /* Synchronize multi-volume globals */
1544 multi_volume_sync ()
1546 if (multi_volume_option
)
1550 assign_string (&real_s_name
,
1551 safer_name_suffix (save_name
, false,
1552 absolute_names_option
));
1553 real_s_totsize
= save_totsize
;
1554 real_s_sizeleft
= save_sizeleft
;
1558 assign_string (&real_s_name
, 0);
1560 real_s_sizeleft
= 0;
1566 /* Low-level flush functions */
1568 /* Simple flush read (no multi-volume or label extensions) */
1570 simple_flush_read (void)
1572 size_t status
; /* result from system call */
1574 checkpoint_run (false);
1576 /* Clear the count of errors. This only applies to a single call to
1579 read_error_count
= 0; /* clear error count */
1581 if (write_archive_to_stdout
&& record_start_block
!= 0)
1583 archive
= STDOUT_FILENO
;
1584 status
= sys_write_archive_buffer ();
1585 archive
= STDIN_FILENO
;
1586 if (status
!= record_size
)
1587 archive_write_error (status
);
1592 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1593 if (status
== record_size
)
1598 if (status
== SAFE_READ_ERROR
)
1600 archive_read_error ();
1601 continue; /* try again */
1605 short_read (status
);
1608 /* Simple flush write (no multi-volume or label extensions) */
1610 simple_flush_write (size_t level
__attribute__((unused
)))
1614 status
= _flush_write ();
1615 if (status
!= record_size
)
1616 archive_write_error (status
);
1620 bytes_written
+= status
;
1625 /* GNU flush functions. These support multi-volume and archive labels in
1626 GNU and PAX archive formats. */
1629 _gnu_flush_read (void)
1631 size_t status
; /* result from system call */
1633 checkpoint_run (false);
1635 /* Clear the count of errors. This only applies to a single call to
1638 read_error_count
= 0; /* clear error count */
1640 if (write_archive_to_stdout
&& record_start_block
!= 0)
1642 archive
= STDOUT_FILENO
;
1643 status
= sys_write_archive_buffer ();
1644 archive
= STDIN_FILENO
;
1645 if (status
!= record_size
)
1646 archive_write_error (status
);
1649 multi_volume_sync ();
1653 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1654 if (status
== record_size
)
1660 /* The condition below used to include
1661 || (status > 0 && !read_full_records)
1662 This is incorrect since even if new_volume() succeeds, the
1663 subsequent call to rmtread will overwrite the chunk of data
1664 already read in the buffer, so the processing will fail */
1666 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1667 && multi_volume_option
)
1669 while (!try_new_volume ())
1671 if (current_block
== record_end
)
1672 /* Necessary for blocking_factor == 1 */
1676 else if (status
== SAFE_READ_ERROR
)
1678 archive_read_error ();
1683 short_read (status
);
1687 gnu_flush_read (void)
1689 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1691 flush_read_ptr
= gnu_flush_read
;
1695 _gnu_flush_write (size_t buffer_level
)
1698 union block
*header
;
1704 status
= _flush_write ();
1705 if (status
!= record_size
&& !multi_volume_option
)
1706 archive_write_error (status
);
1711 bytes_written
+= status
;
1714 if (status
== record_size
)
1716 multi_volume_sync ();
1720 if (status
% BLOCKSIZE
)
1722 ERROR ((0, 0, _("write did not end on a block boundary")));
1723 archive_write_error (status
);
1726 /* In multi-volume mode. */
1727 /* ENXIO is for the UNIX PC. */
1728 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1729 archive_write_error (status
);
1731 real_s_sizeleft
-= status
;
1732 if (!new_volume (ACCESS_WRITE
))
1735 tar_stat_destroy (&dummy
);
1737 increase_volume_number ();
1738 prev_written
+= bytes_written
;
1741 copy_ptr
= record_start
->buffer
+ status
;
1742 copy_size
= buffer_level
- status
;
1744 /* Switch to the next buffer */
1745 record_index
= !record_index
;
1748 if (volume_label_option
)
1749 add_volume_label ();
1752 add_multi_volume_header ();
1754 write_extended (true, &dummy
, find_next_block ());
1755 tar_stat_destroy (&dummy
);
1758 add_chunk_header ();
1759 wrt
= bytes_written
;
1760 header
= find_next_block ();
1761 bufsize
= available_space_after (header
);
1762 while (bufsize
< copy_size
)
1764 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1765 copy_ptr
+= bufsize
;
1766 copy_size
-= bufsize
;
1767 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1768 header
= find_next_block ();
1769 bufsize
= available_space_after (header
);
1771 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1772 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1773 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1774 if (multi_volume_option
&& wrt
< bytes_written
)
1776 /* The value of bytes_written has changed while moving data;
1777 that means that flush_archive was executed at least once in
1778 between, and, as a consequence, copy_size bytes were not written
1779 to disk. We need to update sizeleft variables to compensate for
1781 save_sizeleft
+= copy_size
;
1782 multi_volume_sync ();
1788 gnu_flush_write (size_t buffer_level
)
1790 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1791 _gnu_flush_write (buffer_level
);
1792 flush_write_ptr
= gnu_flush_write
;
1804 flush_write_ptr (record_size
);
1808 open_archive (enum access_mode wanted_access
)
1810 flush_read_ptr
= gnu_flush_read
;
1811 flush_write_ptr
= gnu_flush_write
;
1813 _open_archive (wanted_access
);
1814 switch (wanted_access
)
1817 if (volume_label_option
)
1818 match_volume_label ();
1822 records_written
= 0;
1823 if (volume_label_option
)
1824 write_volume_label ();
1830 set_volume_start_time ();