1 /* Buffer management for tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006 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 2, 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>
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 /* Checkpointing counter */
79 static unsigned checkpoint
;
81 static bool read_full_records
= false;
83 /* We're reading, but we just read the last block and it's time to update.
86 As least EXTERN like this one as possible. (?? --gray)
87 FIXME: Either eliminate it or move it to common.h.
89 extern bool time_to_start_writing
;
91 bool write_archive_to_stdout
;
93 void (*flush_write_ptr
) (size_t);
94 void (*flush_read_ptr
) (void);
98 char *continued_file_name
;
99 uintmax_t continued_file_size
;
100 uintmax_t continued_file_offset
;
103 static int volno
= 1; /* which volume of a multi-volume tape we're
105 static int global_volno
= 1; /* volume number to print in external
108 bool write_archive_to_stdout
;
110 /* Used by flush_read and flush_write to store the real info about saved
112 static char *real_s_name
;
113 static off_t real_s_totsize
;
114 static off_t real_s_sizeleft
;
117 /* Multi-volume tracking support */
118 static char *save_name
; /* name of the file we are currently writing */
119 static off_t save_totsize
; /* total size of file we are writing, only
120 valid if save_name is nonzero */
121 static off_t save_sizeleft
; /* where we are in the file we are writing,
122 only valid if save_name is nonzero */
125 mv_begin (struct tar_stat_info
*st
)
127 if (multi_volume_option
)
129 assign_string (&save_name
, st
->orig_file_name
);
130 save_totsize
= save_sizeleft
= st
->stat
.st_size
;
137 if (multi_volume_option
)
138 assign_string (&save_name
, 0);
142 mv_total_size (off_t size
)
148 mv_size_left (off_t size
)
150 save_sizeleft
= size
;
157 clear_read_error_count (void)
159 read_error_count
= 0;
163 /* Time-related functions */
170 gettime (&start_time
);
171 volume_start_time
= start_time
;
172 last_stat_time
= start_time
;
176 set_volume_start_time ()
178 gettime (&volume_start_time
);
179 last_stat_time
= volume_start_time
;
187 duration
+= ((now
.tv_sec
- last_stat_time
.tv_sec
)
188 + (now
.tv_nsec
- last_stat_time
.tv_nsec
) / 1e9
);
189 gettime (&last_stat_time
);
193 /* Compression detection */
204 enum compress_type type
;
211 static struct zip_magic
const magic
[] = {
213 { ct_compress
, 2, "\037\235", "compress", "-Z" },
214 { ct_gzip
, 2, "\037\213", "gzip", "-z" },
215 { ct_bzip2
, 3, "BZh", "bzip2", "-j" },
218 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
220 #define compress_option(t) magic[t].option
221 #define compress_program(t) magic[t].program
223 /* Check if the file ARCHIVE is a compressed archive. */
225 check_compressed_archive ()
227 struct zip_magic
const *p
;
230 /* Prepare global data needed for find_next_block: */
231 record_end
= record_start
; /* set up for 1st record = # 0 */
232 sfr
= read_full_records
;
233 read_full_records
= true; /* Suppress fatal error on reading a partial
237 /* Restore global values */
238 read_full_records
= sfr
;
240 if (tar_checksum (record_start
, true) == HEADER_SUCCESS
)
241 /* Probably a valid header */
244 for (p
= magic
+ 1; p
< magic
+ NMAGIC
; p
++)
245 if (memcmp (record_start
->buffer
, p
->magic
, p
->length
) == 0)
251 /* Open an archive named archive_name_array[0]. Detect if it is
252 a compressed archive of known type and use corresponding decompression
255 open_compressed_archive ()
257 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
258 MODE_RW
, rsh_command_option
);
262 if (!multi_volume_option
)
264 enum compress_type type
= check_compressed_archive ();
269 /* FD is not needed any more */
272 hit_eof
= false; /* It might have been set by find_next_block in
273 check_compressed_archive */
275 /* Open compressed archive */
276 use_compress_program_option
= compress_program (type
);
277 child_pid
= sys_child_open_for_uncompress ();
278 read_full_records
= true;
282 record_end
= record_start
; /* set up for 1st record = # 0 */
289 print_stats (FILE *fp
, const char *text
, tarlong numbytes
)
291 char bytes
[sizeof (tarlong
) * CHAR_BIT
];
292 char abbr
[LONGEST_HUMAN_READABLE
+ 1];
293 char rate
[LONGEST_HUMAN_READABLE
+ 1];
295 int human_opts
= human_autoscale
| human_base_1024
| human_SI
| human_B
;
297 sprintf (bytes
, TARLONG_FORMAT
, numbytes
);
299 fprintf (fp
, "%s: %s (%s, %s/s)\n",
301 human_readable (numbytes
, abbr
, human_opts
, 1, 1),
302 (0 < duration
&& numbytes
/ duration
< (uintmax_t) -1
303 ? human_readable (numbytes
/ duration
, rate
, human_opts
, 1, 1)
310 switch (subcommand_option
)
312 case CREATE_SUBCOMMAND
:
314 case UPDATE_SUBCOMMAND
:
315 case APPEND_SUBCOMMAND
:
316 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
317 print_stats (stderr
, _("Total bytes written"),
318 prev_written
+ bytes_written
);
321 case DELETE_SUBCOMMAND
:
323 char buf
[UINTMAX_STRSIZE_BOUND
];
324 print_stats (stderr
, _("Total bytes read"),
325 records_read
* record_size
);
326 print_stats (stderr
, _("Total bytes written"),
327 prev_written
+ bytes_written
);
328 fprintf (stderr
, _("Total bytes deleted: %s\n"),
329 STRINGIFY_BIGINT ((records_read
- records_skipped
)
331 - (prev_written
+ bytes_written
), buf
));
335 case EXTRACT_SUBCOMMAND
:
336 case LIST_SUBCOMMAND
:
337 case DIFF_SUBCOMMAND
:
338 print_stats (stderr
, _("Total bytes read"),
339 records_read
* record_size
);
344 /* Compute and return the block ordinal at current_block. */
346 current_block_ordinal (void)
348 return record_start_block
+ (current_block
- record_start
);
351 /* If the EOF flag is set, reset it, as well as current_block, etc. */
358 current_block
= record_start
;
359 record_end
= record_start
+ blocking_factor
;
360 access_mode
= ACCESS_WRITE
;
364 /* Return the location of the next available input or output block.
365 Return zero for EOF. Once we have returned zero, we just keep returning
366 it, to avoid accidentally going on to the next file on the tape. */
368 find_next_block (void)
370 if (current_block
== record_end
)
375 if (current_block
== record_end
)
381 return current_block
;
384 /* Indicate that we have used all blocks up thru BLOCK. */
386 set_next_block_after (union block
*block
)
388 while (block
>= current_block
)
391 /* Do *not* flush the archive here. If we do, the same argument to
392 set_next_block_after could mean the next block (if the input record
393 is exactly one block long), which is not what is intended. */
395 if (current_block
> record_end
)
399 /* Return the number of bytes comprising the space between POINTER
400 through the end of the current buffer of blocks. This space is
401 available for filling with data, or taking data from. POINTER is
402 usually (but not always) the result of previous find_next_block call. */
404 available_space_after (union block
*pointer
)
406 return record_end
->buffer
- pointer
->buffer
;
409 /* Close file having descriptor FD, and abort if close unsuccessful. */
414 close_error (_("(pipe)"));
420 if (! record_buffer_aligned
[record_index
])
421 record_buffer_aligned
[record_index
] =
422 page_aligned_alloc (&record_buffer
[record_index
], record_size
);
424 record_start
= record_buffer_aligned
[record_index
];
425 current_block
= record_start
;
426 record_end
= record_start
+ blocking_factor
;
429 /* Open an archive file. The argument specifies whether we are
430 reading or writing, or both. */
432 _open_archive (enum access_mode wanted_access
)
434 int backed_up_flag
= 0;
438 stdlis
= freopen (index_file_name
, "w", stdout
);
440 open_error (index_file_name
);
441 close_stdout_set_file_name (index_file_name
);
444 stdlis
= to_stdout_option
? stderr
: stdout
;
446 if (record_size
== 0)
447 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
449 if (archive_names
== 0)
450 FATAL_ERROR ((0, 0, _("No archive name given")));
452 tar_stat_destroy (¤t_stat_info
);
459 /* When updating the archive, we start with reading. */
460 access_mode
= wanted_access
== ACCESS_UPDATE
? ACCESS_READ
: wanted_access
;
462 read_full_records
= read_full_records_option
;
466 if (use_compress_program_option
)
468 switch (wanted_access
)
471 child_pid
= sys_child_open_for_uncompress ();
472 read_full_records
= true;
473 record_end
= record_start
; /* set up for 1st record = # 0 */
477 child_pid
= sys_child_open_for_compress ();
481 abort (); /* Should not happen */
485 if (wanted_access
== ACCESS_WRITE
486 && strcmp (archive_name_array
[0], "-") == 0)
489 else if (strcmp (archive_name_array
[0], "-") == 0)
491 read_full_records
= true; /* could be a pipe, be safe */
493 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
495 switch (wanted_access
)
499 enum compress_type type
;
501 archive
= STDIN_FILENO
;
503 type
= check_compressed_archive ();
506 _("Archive is compressed. Use %s option"),
507 compress_option (type
)));
512 archive
= STDOUT_FILENO
;
517 archive
= STDIN_FILENO
;
519 write_archive_to_stdout
= true;
520 record_end
= record_start
; /* set up for 1st record = # 0 */
524 else if (verify_option
)
525 archive
= rmtopen (archive_name_array
[0], O_RDWR
| O_CREAT
| O_BINARY
,
526 MODE_RW
, rsh_command_option
);
528 switch (wanted_access
)
531 archive
= open_compressed_archive ();
537 maybe_backup_file (archive_name_array
[0], 1);
540 archive
= rmtcreat (archive_name_array
[0], MODE_RW
,
545 archive
= rmtopen (archive_name_array
[0],
546 O_RDWR
| O_CREAT
| O_BINARY
,
547 MODE_RW
, rsh_command_option
);
549 if (check_compressed_archive () != ct_none
)
551 _("Cannot update compressed archives")));
556 || (! _isrmt (archive
) && !sys_get_archive_stat ()))
558 int saved_errno
= errno
;
563 open_fatal (archive_name_array
[0]);
566 sys_detect_dev_null_output ();
567 sys_save_archive_dev_ino ();
568 SET_BINARY_MODE (archive
);
570 switch (wanted_access
)
573 find_next_block (); /* read it in, check for EOF */
584 do_checkpoint (bool write
)
586 if (checkpoint_option
&& !(++checkpoint
% checkpoint_option
))
588 switch (checkpoint_style
)
595 case checkpoint_text
:
597 /* TRANSLATORS: This is a ``checkpoint of write operation'',
598 *not* ``Writing a checkpoint''.
599 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
600 *not* ``Escribiendo un punto de comprobaci@'on'' */
601 WARN ((0, 0, _("Write checkpoint %u"), checkpoint
));
603 /* TRANSLATORS: This is a ``checkpoint of read operation'',
604 *not* ``Reading a checkpoint''.
605 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
606 *not* ``Leyendo un punto de comprobaci@'on'' */
607 WARN ((0, 0, _("Read checkpoint %u"), checkpoint
));
613 /* Perform a write to flush the buffer. */
619 do_checkpoint (true);
620 if (tape_length_option
&& tape_length_option
<= bytes_written
)
625 else if (dev_null_output
)
626 status
= record_size
;
628 status
= sys_write_archive_buffer ();
633 /* Handle write errors on the archive. Write errors are always fatal.
634 Hitting the end of a volume does not cause a write error unless the
635 write was the first record of the volume. */
637 archive_write_error (ssize_t status
)
639 /* It might be useful to know how much was written before the error
644 print_total_stats ();
648 write_fatal_details (*archive_name_cursor
, status
, record_size
);
651 /* Handle read errors on the archive. If the read should be retried,
652 return to the caller. */
654 archive_read_error (void)
656 read_error (*archive_name_cursor
);
658 if (record_start_block
== 0)
659 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
661 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
662 then give up on reading the archive. */
664 if (read_error_count
++ > READ_ERROR_MAX
)
665 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
670 short_read (size_t status
)
672 size_t left
; /* bytes left */
673 char *more
; /* pointer to next byte to read */
675 more
= record_start
->buffer
+ status
;
676 left
= record_size
- status
;
678 while (left
% BLOCKSIZE
!= 0
679 || (left
&& status
&& read_full_records
))
682 while ((status
= rmtread (archive
, more
, left
)) == SAFE_READ_ERROR
)
683 archive_read_error ();
688 if (! read_full_records
)
690 unsigned long rest
= record_size
- left
;
693 ngettext ("Unaligned block (%lu byte) in archive",
694 "Unaligned block (%lu bytes) in archive",
699 /* User warned us about this. Fix up. */
705 /* FIXME: for size=0, multi-volume support. On the first record, warn
706 about the problem. */
708 if (!read_full_records
&& verbose_option
> 1
709 && record_start_block
== 0 && status
!= 0)
711 unsigned long rsize
= (record_size
- left
) / BLOCKSIZE
;
713 ngettext ("Record size = %lu block",
714 "Record size = %lu blocks",
719 record_end
= record_start
+ (record_size
- left
) / BLOCKSIZE
;
723 /* Flush the current buffer to/from the archive. */
727 size_t buffer_level
= current_block
->buffer
- record_start
->buffer
;
728 record_start_block
+= record_end
- record_start
;
729 current_block
= record_start
;
730 record_end
= record_start
+ blocking_factor
;
732 if (access_mode
== ACCESS_READ
&& time_to_start_writing
)
734 access_mode
= ACCESS_WRITE
;
735 time_to_start_writing
= false;
746 flush_write_ptr (buffer_level
);
754 /* Backspace the archive descriptor by one record worth. If it's a
755 tape, MTIOCTOP will work. If it's something else, try to seek on
756 it. If we can't seek, we lose! */
758 backspace_output (void)
762 struct mtop operation
;
764 operation
.mt_op
= MTBSR
;
765 operation
.mt_count
= 1;
766 if (rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
768 if (errno
== EIO
&& rmtioctl (archive
, MTIOCTOP
, (char *) &operation
) >= 0)
774 off_t position
= rmtlseek (archive
, (off_t
) 0, SEEK_CUR
);
776 /* Seek back to the beginning of this record and start writing there. */
778 position
-= record_size
;
781 if (rmtlseek (archive
, position
, SEEK_SET
) != position
)
783 /* Lseek failed. Try a different method. */
786 _("Cannot backspace archive file; it may be unreadable without -i")));
788 /* Replace the first part of the record with NULs. */
790 if (record_start
->buffer
!= output_start
)
791 memset (record_start
->buffer
, 0,
792 output_start
- record_start
->buffer
);
798 seek_archive (off_t size
)
800 off_t start
= current_block_ordinal ();
803 off_t skipped
= (blocking_factor
- (current_block
- record_start
));
805 size
-= skipped
* BLOCKSIZE
;
807 if (size
< record_size
)
811 /* Compute number of records to skip */
812 nrec
= size
/ record_size
;
813 offset
= rmtlseek (archive
, nrec
* record_size
, SEEK_CUR
);
817 if (offset
% record_size
)
818 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
820 /* Convert to number of records */
822 /* Compute number of skipped blocks */
823 nblk
= offset
- start
;
825 /* Update buffering info */
826 records_read
+= nblk
/ blocking_factor
;
827 record_start_block
= offset
- blocking_factor
;
828 current_block
= record_end
;
833 /* Close the archive file. */
837 if (time_to_start_writing
|| access_mode
== ACCESS_WRITE
)
840 if (current_block
> record_start
)
844 sys_drain_input_pipe ();
850 if (rmtclose (archive
) != 0)
851 close_warn (*archive_name_cursor
);
853 sys_wait_for_child (child_pid
);
855 tar_stat_destroy (¤t_stat_info
);
860 free (record_buffer
[0]);
861 free (record_buffer
[1]);
864 /* Called to initialize the global volume number. */
866 init_volume_number (void)
868 FILE *file
= fopen (volno_file_option
, "r");
872 if (fscanf (file
, "%d", &global_volno
) != 1
874 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
875 quotearg_colon (volno_file_option
)));
877 read_error (volno_file_option
);
878 if (fclose (file
) != 0)
879 close_error (volno_file_option
);
881 else if (errno
!= ENOENT
)
882 open_error (volno_file_option
);
885 /* Called to write out the closing global volume number. */
887 closeout_volume_number (void)
889 FILE *file
= fopen (volno_file_option
, "w");
893 fprintf (file
, "%d\n", global_volno
);
895 write_error (volno_file_option
);
896 if (fclose (file
) != 0)
897 close_error (volno_file_option
);
900 open_error (volno_file_option
);
905 increase_volume_number ()
908 if (global_volno
< 0)
909 FATAL_ERROR ((0, 0, _("Volume number overflow")));
914 change_tape_menu (FILE *read_file
)
916 char *input_buffer
= NULL
;
922 fputc ('\007', stderr
);
924 _("Prepare volume #%d for %s and hit return: "),
925 global_volno
+ 1, quote (*archive_name_cursor
));
928 if (getline (&input_buffer
, &size
, read_file
) <= 0)
930 WARN ((0, 0, _("EOF where user reply was expected")));
932 if (subcommand_option
!= EXTRACT_SUBCOMMAND
933 && subcommand_option
!= LIST_SUBCOMMAND
934 && subcommand_option
!= DIFF_SUBCOMMAND
)
935 WARN ((0, 0, _("WARNING: Archive is incomplete")));
940 if (input_buffer
[0] == '\n'
941 || input_buffer
[0] == 'y'
942 || input_buffer
[0] == 'Y')
945 switch (input_buffer
[0])
949 fprintf (stderr
, _("\
950 n name Give a new file name for the next (and subsequent) volume(s)\n\
952 y or newline Continue operation\n"));
953 if (!restrict_option
)
954 fprintf (stderr
, _(" ! Spawn a subshell\n"));
955 fprintf (stderr
, _(" ? Print this list\n"));
962 WARN ((0, 0, _("No new volume; exiting.\n")));
964 if (subcommand_option
!= EXTRACT_SUBCOMMAND
965 && subcommand_option
!= LIST_SUBCOMMAND
966 && subcommand_option
!= DIFF_SUBCOMMAND
)
967 WARN ((0, 0, _("WARNING: Archive is incomplete")));
972 /* Get new file name. */
978 for (name
= input_buffer
+ 1;
979 *name
== ' ' || *name
== '\t';
983 for (cursor
= name
; *cursor
&& *cursor
!= '\n'; cursor
++)
989 /* FIXME: the following allocation is never reclaimed. */
990 *archive_name_cursor
= xstrdup (name
);
994 fprintf (stderr
, "%s",
995 _("File name not specified. Try again.\n"));
1000 if (!restrict_option
)
1008 fprintf (stderr
, _("Invalid input. Type ? for help.\n"));
1011 free (input_buffer
);
1014 /* We've hit the end of the old volume. Close it and open the next one.
1015 Return nonzero on success.
1018 new_volume (enum access_mode mode
)
1020 static FILE *read_file
;
1024 if (!read_file
&& !info_script_option
)
1025 /* FIXME: if fopen is used, it will never be closed. */
1026 read_file
= archive
== STDIN_FILENO
? fopen (TTY_NAME
, "r") : stdin
;
1033 assign_string (&volume_label
, NULL
);
1034 assign_string (&continued_file_name
, NULL
);
1035 continued_file_size
= continued_file_offset
= 0;
1037 if (rmtclose (archive
) != 0)
1038 close_warn (*archive_name_cursor
);
1040 archive_name_cursor
++;
1041 if (archive_name_cursor
== archive_name_array
+ archive_names
)
1043 archive_name_cursor
= archive_name_array
;
1051 /* We have to prompt from now on. */
1053 if (info_script_option
)
1055 if (volno_file_option
)
1056 closeout_volume_number ();
1057 if (sys_exec_info_script (archive_name_cursor
, global_volno
+1))
1058 FATAL_ERROR ((0, 0, _("%s command failed"),
1059 quote (info_script_option
)));
1062 change_tape_menu (read_file
);
1065 if (strcmp (archive_name_cursor
[0], "-") == 0)
1067 read_full_records
= true;
1068 archive
= STDIN_FILENO
;
1070 else if (verify_option
)
1071 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1072 rsh_command_option
);
1077 archive
= rmtopen (*archive_name_cursor
, O_RDONLY
, MODE_RW
,
1078 rsh_command_option
);
1083 maybe_backup_file (*archive_name_cursor
, 1);
1084 archive
= rmtcreat (*archive_name_cursor
, MODE_RW
,
1085 rsh_command_option
);
1089 archive
= rmtopen (*archive_name_cursor
, O_RDWR
| O_CREAT
, MODE_RW
,
1090 rsh_command_option
);
1096 open_warn (*archive_name_cursor
);
1097 if (!verify_option
&& mode
== ACCESS_WRITE
&& backup_option
)
1098 undo_last_backup ();
1103 SET_BINARY_MODE (archive
);
1109 read_header0 (struct tar_stat_info
*info
)
1111 enum read_header rc
;
1113 tar_stat_init (info
);
1114 rc
= read_header_primitive (false, info
);
1115 if (rc
== HEADER_SUCCESS
)
1117 set_next_block_after (current_header
);
1120 ERROR ((0, 0, _("This does not look like a tar archive")));
1128 union block
*header
;
1129 struct tar_stat_info dummy
;
1131 switch (subcommand_option
)
1133 case APPEND_SUBCOMMAND
:
1134 case CAT_SUBCOMMAND
:
1135 case UPDATE_SUBCOMMAND
:
1136 if (!new_volume (ACCESS_UPDATE
))
1141 if (!new_volume (ACCESS_READ
))
1146 while ((status
= rmtread (archive
, record_start
->buffer
, record_size
))
1148 archive_read_error ();
1150 if (status
!= record_size
)
1151 short_read (status
);
1153 header
= find_next_block ();
1157 switch (header
->header
.typeflag
)
1161 if (!read_header0 (&dummy
))
1163 xheader_decode (&dummy
); /* decodes values from the global header */
1164 tar_stat_destroy (&dummy
);
1167 /* We have read the extended header of the first member in
1168 this volume. Put it back, so next read_header works as
1170 current_block
= record_start
;
1175 case GNUTYPE_VOLHDR
:
1176 if (!read_header0 (&dummy
))
1178 tar_stat_destroy (&dummy
);
1179 assign_string (&volume_label
, current_header
->header
.name
);
1180 set_next_block_after (header
);
1181 header
= find_next_block ();
1182 if (header
->header
.typeflag
!= GNUTYPE_MULTIVOL
)
1186 case GNUTYPE_MULTIVOL
:
1187 if (!read_header0 (&dummy
))
1189 tar_stat_destroy (&dummy
);
1190 assign_string (&continued_file_name
, current_header
->header
.name
);
1191 continued_file_size
=
1192 UINTMAX_FROM_HEADER (current_header
->header
.size
);
1193 continued_file_offset
=
1194 UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
);
1204 if (!continued_file_name
1205 || strcmp (continued_file_name
, real_s_name
))
1207 if ((archive_format
== GNU_FORMAT
|| archive_format
== OLDGNU_FORMAT
)
1208 && strlen (real_s_name
) >= NAME_FIELD_SIZE
1209 && strncmp (continued_file_name
, real_s_name
,
1210 NAME_FIELD_SIZE
) == 0)
1212 _("%s is possibly continued on this volume: header contains truncated name"),
1213 quote (real_s_name
)));
1216 WARN ((0, 0, _("%s is not continued on this volume"),
1217 quote (real_s_name
)));
1222 s
= continued_file_size
+ continued_file_offset
;
1224 if (real_s_totsize
!= s
|| s
< continued_file_offset
)
1226 char totsizebuf
[UINTMAX_STRSIZE_BOUND
];
1227 char s1buf
[UINTMAX_STRSIZE_BOUND
];
1228 char s2buf
[UINTMAX_STRSIZE_BOUND
];
1230 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1231 quote (continued_file_name
),
1232 STRINGIFY_BIGINT (save_totsize
, totsizebuf
),
1233 STRINGIFY_BIGINT (continued_file_size
, s1buf
),
1234 STRINGIFY_BIGINT (continued_file_offset
, s2buf
)));
1238 if (real_s_totsize
- real_s_sizeleft
!= continued_file_offset
)
1240 WARN ((0, 0, _("This volume is out of sequence")));
1245 increase_volume_number ();
1250 /* Check the LABEL block against the volume label, seen as a globbing
1251 pattern. Return true if the pattern matches. In case of failure,
1252 retry matching a volume sequence number before giving up in
1253 multi-volume mode. */
1255 check_label_pattern (union block
*label
)
1260 if (! memchr (label
->header
.name
, '\0', sizeof label
->header
.name
))
1263 if (fnmatch (volume_label_option
, label
->header
.name
, 0) == 0)
1266 if (!multi_volume_option
)
1269 string
= xmalloc (strlen (volume_label_option
)
1270 + sizeof VOLUME_LABEL_APPEND
+ 1);
1271 strcpy (string
, volume_label_option
);
1272 strcat (string
, VOLUME_LABEL_APPEND
);
1273 result
= fnmatch (string
, label
->header
.name
, 0) == 0;
1278 /* Check if the next block contains a volume label and if this matches
1279 the one given in the command line */
1281 match_volume_label (void)
1283 union block
*label
= find_next_block ();
1286 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1287 quote (volume_label_option
)));
1288 if (!check_label_pattern (label
))
1289 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1290 quote_n (0, label
->header
.name
),
1291 quote_n (1, volume_label_option
)));
1294 /* Mark the archive with volume label STR. */
1296 _write_volume_label (const char *str
)
1298 if (archive_format
== POSIX_FORMAT
)
1299 xheader_store ("GNU.volume.label", NULL
, str
);
1302 union block
*label
= find_next_block ();
1304 memset (label
, 0, BLOCKSIZE
);
1306 strcpy (label
->header
.name
, volume_label_option
);
1307 assign_string (¤t_stat_info
.file_name
,
1308 label
->header
.name
);
1309 current_stat_info
.had_trailing_slash
=
1310 strip_trailing_slashes (current_stat_info
.file_name
);
1312 label
->header
.typeflag
= GNUTYPE_VOLHDR
;
1313 TIME_TO_CHARS (start_time
.tv_sec
, label
->header
.mtime
);
1314 finish_header (¤t_stat_info
, label
, -1);
1315 set_next_block_after (label
);
1319 #define VOL_SUFFIX "Volume"
1321 /* Add a volume label to a part of multi-volume archive */
1323 add_volume_label (void)
1325 char buf
[UINTMAX_STRSIZE_BOUND
];
1326 char *p
= STRINGIFY_BIGINT (volno
, buf
);
1327 char *s
= xmalloc (strlen (volume_label_option
) + sizeof VOL_SUFFIX
1329 sprintf (s
, "%s %s %s", volume_label_option
, VOL_SUFFIX
, p
);
1330 _write_volume_label (s
);
1337 if (archive_format
== POSIX_FORMAT
)
1339 off_t block_ordinal
;
1341 struct tar_stat_info st
;
1342 static size_t real_s_part_no
; /* FIXME */
1345 memset (&st
, 0, sizeof st
);
1346 st
.orig_file_name
= st
.file_name
= real_s_name
;
1347 st
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
1348 st
.stat
.st_uid
= getuid ();
1349 st
.stat
.st_gid
= getgid ();
1350 st
.orig_file_name
= xheader_format_name (&st
,
1351 "%d/GNUFileParts.%p/%f.%n",
1353 st
.file_name
= st
.orig_file_name
;
1354 st
.archive_file_size
= st
.stat
.st_size
= real_s_sizeleft
;
1356 block_ordinal
= current_block_ordinal ();
1357 blk
= start_header (&st
);
1359 abort (); /* FIXME */
1360 finish_header (&st
, blk
, block_ordinal
);
1361 free (st
.orig_file_name
);
1366 /* Add a volume label to the current archive */
1368 write_volume_label (void)
1370 if (multi_volume_option
)
1371 add_volume_label ();
1373 _write_volume_label (volume_label_option
);
1376 /* Write GNU multi-volume header */
1378 gnu_add_multi_volume_header (void)
1381 union block
*block
= find_next_block ();
1383 if (strlen (real_s_name
) > NAME_FIELD_SIZE
)
1385 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1386 quotearg_colon (real_s_name
)));
1388 memset (block
, 0, BLOCKSIZE
);
1390 /* FIXME: Michael P Urban writes: [a long name file] is being written
1391 when a new volume rolls around [...] Looks like the wrong value is
1392 being preserved in real_s_name, though. */
1394 strncpy (block
->header
.name
, real_s_name
, NAME_FIELD_SIZE
);
1395 block
->header
.typeflag
= GNUTYPE_MULTIVOL
;
1397 OFF_TO_CHARS (real_s_sizeleft
, block
->header
.size
);
1398 OFF_TO_CHARS (real_s_totsize
- real_s_sizeleft
,
1399 block
->oldgnu_header
.offset
);
1401 tmp
= verbose_option
;
1403 finish_header (¤t_stat_info
, block
, -1);
1404 verbose_option
= tmp
;
1405 set_next_block_after (block
);
1408 /* Add a multi volume header to the current archive. The exact header format
1409 depends on the archive format. */
1411 add_multi_volume_header (void)
1413 if (archive_format
== POSIX_FORMAT
)
1415 off_t d
= real_s_totsize
- real_s_sizeleft
;
1416 xheader_store ("GNU.volume.filename", NULL
, real_s_name
);
1417 xheader_store ("GNU.volume.size", NULL
, &real_s_sizeleft
);
1418 xheader_store ("GNU.volume.offset", NULL
, &d
);
1421 gnu_add_multi_volume_header ();
1424 /* Synchronize multi-volume globals */
1426 multi_volume_sync ()
1428 if (multi_volume_option
)
1432 assign_string (&real_s_name
,
1433 safer_name_suffix (save_name
, false,
1434 absolute_names_option
));
1435 real_s_totsize
= save_totsize
;
1436 real_s_sizeleft
= save_sizeleft
;
1440 assign_string (&real_s_name
, 0);
1442 real_s_sizeleft
= 0;
1448 /* Low-level flush functions */
1450 /* Simple flush read (no multi-volume or label extensions) */
1452 simple_flush_read (void)
1454 size_t status
; /* result from system call */
1456 do_checkpoint (false);
1458 /* Clear the count of errors. This only applies to a single call to
1461 read_error_count
= 0; /* clear error count */
1463 if (write_archive_to_stdout
&& record_start_block
!= 0)
1465 archive
= STDOUT_FILENO
;
1466 status
= sys_write_archive_buffer ();
1467 archive
= STDIN_FILENO
;
1468 if (status
!= record_size
)
1469 archive_write_error (status
);
1474 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1475 if (status
== record_size
)
1480 if (status
== SAFE_READ_ERROR
)
1482 archive_read_error ();
1483 continue; /* try again */
1487 short_read (status
);
1490 /* Simple flush write (no multi-volume or label extensions) */
1492 simple_flush_write (size_t level
__attribute__((unused
)))
1496 status
= _flush_write ();
1497 if (status
!= record_size
)
1498 archive_write_error (status
);
1502 bytes_written
+= status
;
1507 /* GNU flush functions. These support multi-volume and archive labels in
1508 GNU and PAX archive formats. */
1511 _gnu_flush_read (void)
1513 size_t status
; /* result from system call */
1515 do_checkpoint (false);
1517 /* Clear the count of errors. This only applies to a single call to
1520 read_error_count
= 0; /* clear error count */
1522 if (write_archive_to_stdout
&& record_start_block
!= 0)
1524 archive
= STDOUT_FILENO
;
1525 status
= sys_write_archive_buffer ();
1526 archive
= STDIN_FILENO
;
1527 if (status
!= record_size
)
1528 archive_write_error (status
);
1531 multi_volume_sync ();
1535 status
= rmtread (archive
, record_start
->buffer
, record_size
);
1536 if (status
== record_size
)
1542 /* The condition below used to include
1543 || (status > 0 && !read_full_records)
1544 This is incorrect since even if new_volume() succeeds, the
1545 subsequent call to rmtread will overwrite the chunk of data
1546 already read in the buffer, so the processing will fail */
1548 || (status
== SAFE_READ_ERROR
&& errno
== ENOSPC
))
1549 && multi_volume_option
)
1551 while (!try_new_volume ())
1555 else if (status
== SAFE_READ_ERROR
)
1557 archive_read_error ();
1562 short_read (status
);
1566 gnu_flush_read (void)
1568 flush_read_ptr
= simple_flush_read
; /* Avoid recursion */
1570 flush_read_ptr
= gnu_flush_read
;
1574 _gnu_flush_write (size_t buffer_level
)
1577 union block
*header
;
1582 status
= _flush_write ();
1583 if (status
!= record_size
&& !multi_volume_option
)
1584 archive_write_error (status
);
1588 bytes_written
+= status
;
1591 if (status
== record_size
)
1593 multi_volume_sync ();
1597 /* In multi-volume mode. */
1598 /* ENXIO is for the UNIX PC. */
1599 if (status
< 0 && errno
!= ENOSPC
&& errno
!= EIO
&& errno
!= ENXIO
)
1600 archive_write_error (status
);
1602 if (!new_volume (ACCESS_WRITE
))
1605 xheader_destroy (&extended_header
);
1607 increase_volume_number ();
1608 prev_written
+= bytes_written
;
1611 copy_ptr
= record_start
->buffer
+ status
;
1612 copy_size
= buffer_level
- status
;
1613 /* Switch to the next buffer */
1614 record_index
= !record_index
;
1617 if (volume_label_option
)
1618 add_volume_label ();
1621 add_multi_volume_header ();
1623 write_extended (true, NULL
, find_next_block ());
1625 add_chunk_header ();
1626 header
= find_next_block ();
1627 bufsize
= available_space_after (header
);
1628 while (bufsize
< copy_size
)
1630 memcpy (header
->buffer
, copy_ptr
, bufsize
);
1631 copy_ptr
+= bufsize
;
1632 copy_size
-= bufsize
;
1633 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
1634 header
= find_next_block ();
1635 bufsize
= available_space_after (header
);
1637 memcpy (header
->buffer
, copy_ptr
, copy_size
);
1638 memset (header
->buffer
+ copy_size
, 0, bufsize
- copy_size
);
1639 set_next_block_after (header
+ (copy_size
- 1) / BLOCKSIZE
);
1644 gnu_flush_write (size_t buffer_level
)
1646 flush_write_ptr
= simple_flush_write
; /* Avoid recursion */
1647 _gnu_flush_write (buffer_level
);
1648 flush_write_ptr
= gnu_flush_write
;
1660 flush_write_ptr (record_size
);
1664 open_archive (enum access_mode wanted_access
)
1666 flush_read_ptr
= gnu_flush_read
;
1667 flush_write_ptr
= gnu_flush_write
;
1669 _open_archive (wanted_access
);
1670 switch (wanted_access
)
1673 if (volume_label_option
)
1674 match_volume_label ();
1678 records_written
= 0;
1679 if (volume_label_option
)
1680 write_volume_label ();
1686 set_volume_start_time ();