Fix listing of volume labels (in particular in PAX archives).
[tar.git] / src / buffer.c
blobed7530342398c15f316c3c2e372db25a3c81a7f3
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
5 Foundation, Inc.
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
12 version.
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. */
23 #include <system.h>
24 #include <system-ioctl.h>
26 #include <signal.h>
28 #include <closeout.h>
29 #include <fnmatch.h>
30 #include <human.h>
31 #include <quotearg.h>
33 #include "common.h"
34 #include <rmt.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]*"
42 /* Variables. */
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. */
65 FILE *stdlis;
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? */
76 static bool hit_eof;
78 static bool read_full_records = false;
80 /* We're reading, but we just read the last block and it's time to update.
81 Declared in update.c
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);
94 char *volume_label;
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
101 on */
102 static int global_volno = 1; /* volume number to print in external
103 messages */
105 bool write_archive_to_stdout;
107 /* Used by flush_read and flush_write to store the real info about saved
108 names. */
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;
124 void
125 buffer_write_global_xheader ()
127 xheader_write_global (&dummy.xhdr);
130 void
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;
140 void
141 mv_end ()
143 if (multi_volume_option)
144 assign_string (&save_name, 0);
147 void
148 mv_total_size (off_t size)
150 save_totsize = size;
153 void
154 mv_size_left (off_t size)
156 save_sizeleft = size;
160 /* Functions. */
162 void
163 clear_read_error_count (void)
165 read_error_count = 0;
169 /* Time-related functions */
171 double duration;
173 void
174 set_start_time ()
176 gettime (&start_time);
177 volume_start_time = start_time;
178 last_stat_time = start_time;
181 void
182 set_volume_start_time ()
184 gettime (&volume_start_time);
185 last_stat_time = volume_start_time;
188 void
189 compute_duration ()
191 struct timespec now;
192 gettime (&now);
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 */
201 enum compress_type {
202 ct_tar, /* Plain tar file */
203 ct_none, /* Unknown compression type */
204 ct_compress,
205 ct_gzip,
206 ct_bzip2,
207 ct_lzma,
208 ct_lzop,
209 ct_xz
212 struct zip_magic
214 enum compress_type type;
215 size_t length;
216 char *magic;
217 char *program;
218 char *option;
221 static struct zip_magic const magic[] = {
222 { ct_tar },
223 { ct_none, },
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. */
238 enum compress_type
239 check_compressed_archive (bool *pshort)
241 struct zip_magic const *p;
242 bool sfr;
243 bool temp;
245 if (!pshort)
246 pshort = &temp;
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
252 record */
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 */
260 return ct_tar;
262 for (p = magic + 2; p < magic + NMAGIC; p++)
263 if (memcmp (record_start->buffer, p->magic, p->length) == 0)
264 return p->type;
266 return ct_none;
269 /* Guess if the archive is seekable. */
270 static void
271 guess_seekable_archive ()
273 struct stat st;
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
283 problem. */
284 seekable_archive = false;
287 if (seek_option != -1)
289 seekable_archive = !!seek_option;
290 return;
293 if (!multi_volume_option && !use_compress_program_option
294 && fstat (archive, &st) == 0)
295 seekable_archive = S_ISREG (st.st_mode);
296 else
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
302 program if so */
304 open_compressed_archive ()
306 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
307 MODE_RW, rsh_command_option);
308 if (archive == -1)
309 return archive;
311 if (!multi_volume_option)
313 if (!use_compress_program_option)
315 bool shortfile;
316 enum compress_type type = check_compressed_archive (&shortfile);
318 switch (type)
320 case ct_tar:
321 if (shortfile)
322 ERROR ((0, 0, _("This does not look like a tar archive")));
323 return archive;
325 case ct_none:
326 if (shortfile)
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)
330 return archive;
331 break;
333 default:
334 use_compress_program_option = compress_program (type);
335 break;
339 /* FD is not needed any more */
340 rmtclose (archive);
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;
350 records_read = 0;
351 record_end = record_start; /* set up for 1st record = # 0 */
353 return archive;
357 static void
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",
369 text, bytes,
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)
373 : "?"));
376 void
377 print_total_stats ()
379 switch (subcommand_option)
381 case CREATE_SUBCOMMAND:
382 case CAT_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);
388 break;
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)
399 * record_size
400 - (prev_written + bytes_written), buf));
402 break;
404 case EXTRACT_SUBCOMMAND:
405 case LIST_SUBCOMMAND:
406 case DIFF_SUBCOMMAND:
407 print_stats (stderr, _("Total bytes read"),
408 records_read * record_size);
409 break;
411 default:
412 abort ();
416 /* Compute and return the block ordinal at current_block. */
417 off_t
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. */
424 void
425 reset_eof (void)
427 if (hit_eof)
429 hit_eof = false;
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. */
439 union block *
440 find_next_block (void)
442 if (current_block == record_end)
444 if (hit_eof)
445 return 0;
446 flush_archive ();
447 if (current_block == record_end)
449 hit_eof = true;
450 return 0;
453 return current_block;
456 /* Indicate that we have used all blocks up thru BLOCK. */
457 void
458 set_next_block_after (union block *block)
460 while (block >= current_block)
461 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)
468 abort ();
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. */
475 size_t
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. */
482 void
483 xclose (int fd)
485 if (close (fd) != 0)
486 close_error (_("(pipe)"));
489 static void
490 init_buffer ()
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. */
503 static void
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 (&current_stat_info);
515 save_name = 0;
516 real_s_name = 0;
518 record_index = 0;
519 init_buffer ();
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;
526 records_read = 0;
528 if (use_compress_program_option)
530 switch (wanted_access)
532 case ACCESS_READ:
533 child_pid = sys_child_open_for_uncompress ();
534 read_full_records = true;
535 record_end = record_start; /* set up for 1st record = # 0 */
536 break;
538 case ACCESS_WRITE:
539 child_pid = sys_child_open_for_compress ();
540 break;
542 case ACCESS_UPDATE:
543 abort (); /* Should not happen */
544 break;
547 if (!index_file_name
548 && wanted_access == ACCESS_WRITE
549 && strcmp (archive_name_array[0], "-") == 0)
550 stdlis = stderr;
552 else if (strcmp (archive_name_array[0], "-") == 0)
554 read_full_records = true; /* could be a pipe, be safe */
555 if (verify_option)
556 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
558 switch (wanted_access)
560 case ACCESS_READ:
562 bool shortfile;
563 enum compress_type type;
565 archive = STDIN_FILENO;
567 type = check_compressed_archive (&shortfile);
568 if (type != ct_tar && type != ct_none)
569 FATAL_ERROR ((0, 0,
570 _("Archive is compressed. Use %s option"),
571 compress_option (type)));
572 if (shortfile)
573 ERROR ((0, 0, _("This does not look like a tar archive")));
575 break;
577 case ACCESS_WRITE:
578 archive = STDOUT_FILENO;
579 if (!index_file_name)
580 stdlis = stderr;
581 break;
583 case ACCESS_UPDATE:
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)
588 stdlis = stderr;
589 break;
592 else if (verify_option)
593 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
594 MODE_RW, rsh_command_option);
595 else
596 switch (wanted_access)
598 case ACCESS_READ:
599 archive = open_compressed_archive ();
600 if (archive >= 0)
601 guess_seekable_archive ();
602 break;
604 case ACCESS_WRITE:
605 if (backup_option)
607 maybe_backup_file (archive_name_array[0], 1);
608 backed_up_flag = 1;
610 archive = rmtcreat (archive_name_array[0], MODE_RW,
611 rsh_command_option);
612 break;
614 case ACCESS_UPDATE:
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))
621 case ct_none:
622 case ct_tar:
623 break;
625 default:
626 FATAL_ERROR ((0, 0,
627 _("Cannot update compressed archives")));
629 break;
632 if (archive < 0
633 || (! _isrmt (archive) && !sys_get_archive_stat ()))
635 int saved_errno = errno;
637 if (backed_up_flag)
638 undo_last_backup ();
639 errno = saved_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)
649 case ACCESS_READ:
650 find_next_block (); /* read it in, check for EOF */
651 break;
653 case ACCESS_UPDATE:
654 case ACCESS_WRITE:
655 records_written = 0;
656 break;
660 /* Perform a write to flush the buffer. */
661 ssize_t
662 _flush_write (void)
664 ssize_t status;
666 checkpoint_run (true);
667 if (tape_length_option && tape_length_option <= bytes_written)
669 errno = ENOSPC;
670 status = 0;
672 else if (dev_null_output)
673 status = record_size;
674 else
675 status = sys_write_archive_buffer ();
677 return status;
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. */
683 void
684 archive_write_error (ssize_t status)
686 /* It might be useful to know how much was written before the error
687 occurred. */
688 if (totals_option)
690 int e = errno;
691 print_total_stats ();
692 errno = e;
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. */
700 void
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")));
713 return;
716 static bool
717 archive_is_dev ()
719 struct stat st;
721 if (fstat (archive, &st))
723 stat_diag (*archive_name_cursor);
724 return false;
726 return S_ISBLK (st.st_mode) || S_ISCHR (st.st_mode);
729 static void
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
739 && verbose_option
740 && record_start_block == 0 && status != 0
741 && archive_is_dev ())
743 unsigned long rsize = status / BLOCKSIZE;
744 WARN ((0, 0,
745 ngettext ("Record size = %lu block",
746 "Record size = %lu blocks",
747 rsize),
748 rsize));
751 while (left % BLOCKSIZE != 0
752 || (left && status && read_full_records))
754 if (status)
755 while ((status = rmtread (archive, more, left)) == SAFE_READ_ERROR)
756 archive_read_error ();
758 if (status == 0)
759 break;
761 if (! read_full_records)
763 unsigned long rest = record_size - left;
765 FATAL_ERROR ((0, 0,
766 ngettext ("Unaligned block (%lu byte) in archive",
767 "Unaligned block (%lu bytes) in archive",
768 rest),
769 rest));
772 left -= status;
773 more += status;
776 record_end = record_start + (record_size - left) / BLOCKSIZE;
777 records_read++;
780 /* Flush the current buffer to/from the archive. */
781 void
782 flush_archive (void)
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;
793 backspace_output ();
796 switch (access_mode)
798 case ACCESS_READ:
799 flush_read ();
800 break;
802 case ACCESS_WRITE:
803 flush_write_ptr (buffer_level);
804 break;
806 case ACCESS_UPDATE:
807 abort ();
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! */
814 static void
815 backspace_output (void)
817 #ifdef MTIOCTOP
819 struct mtop operation;
821 operation.mt_op = MTBSR;
822 operation.mt_count = 1;
823 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
824 return;
825 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
826 return;
828 #endif
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;
836 if (position < 0)
837 position = 0;
838 if (rmtlseek (archive, position, SEEK_SET) != position)
840 /* Lseek failed. Try a different method. */
842 WARN ((0, 0,
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);
854 off_t
855 seek_archive (off_t size)
857 off_t start = current_block_ordinal ();
858 off_t offset;
859 off_t nrec, nblk;
860 off_t skipped = (blocking_factor - (current_block - record_start));
862 size -= skipped * BLOCKSIZE;
864 if (size < record_size)
865 return 0;
866 /* FIXME: flush? */
868 /* Compute number of records to skip */
869 nrec = size / record_size;
870 offset = rmtlseek (archive, nrec * record_size, SEEK_CUR);
871 if (offset < 0)
872 return offset;
874 if (offset % record_size)
875 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
877 /* Convert to number of records */
878 offset /= BLOCKSIZE;
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;
887 return nblk;
890 /* Close the archive file. */
891 void
892 close_archive (void)
894 if (time_to_start_writing || access_mode == ACCESS_WRITE)
896 flush_archive ();
897 if (current_block > record_start)
898 flush_archive ();
901 compute_duration ();
902 if (verify_option)
903 verify_volume ();
905 if (rmtclose (archive) != 0)
906 close_error (*archive_name_cursor);
908 sys_wait_for_child (child_pid, hit_eof);
910 tar_stat_destroy (&current_stat_info);
911 if (save_name)
912 free (save_name);
913 if (real_s_name)
914 free (real_s_name);
915 free (record_buffer[0]);
916 free (record_buffer[1]);
919 /* Called to initialize the global volume number. */
920 void
921 init_volume_number (void)
923 FILE *file = fopen (volno_file_option, "r");
925 if (file)
927 if (fscanf (file, "%d", &global_volno) != 1
928 || global_volno < 0)
929 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
930 quotearg_colon (volno_file_option)));
931 if (ferror (file))
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. */
941 void
942 closeout_volume_number (void)
944 FILE *file = fopen (volno_file_option, "w");
946 if (file)
948 fprintf (file, "%d\n", global_volno);
949 if (ferror (file))
950 write_error (volno_file_option);
951 if (fclose (file) != 0)
952 close_error (volno_file_option);
954 else
955 open_error (volno_file_option);
959 static void
960 increase_volume_number ()
962 global_volno++;
963 if (global_volno < 0)
964 FATAL_ERROR ((0, 0, _("Volume number overflow")));
965 volno++;
968 void
969 change_tape_menu (FILE *read_file)
971 char *input_buffer = NULL;
972 size_t size = 0;
973 bool stop = false;
975 while (!stop)
977 fputc ('\007', stderr);
978 fprintf (stderr,
979 _("Prepare volume #%d for %s and hit return: "),
980 global_volno + 1, quote (*archive_name_cursor));
981 fflush (stderr);
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")));
992 fatal_exit ();
995 if (input_buffer[0] == '\n'
996 || input_buffer[0] == 'y'
997 || input_buffer[0] == 'Y')
998 break;
1000 switch (input_buffer[0])
1002 case '?':
1004 fprintf (stderr, _("\
1005 n name Give a new file name for the next (and subsequent) volume(s)\n\
1006 q Abort tar\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"));
1012 break;
1014 case 'q':
1015 /* Quit. */
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")));
1024 fatal_exit ();
1026 case 'n':
1027 /* Get new file name. */
1030 char *name;
1031 char *cursor;
1033 for (name = input_buffer + 1;
1034 *name == ' ' || *name == '\t';
1035 name++)
1038 for (cursor = name; *cursor && *cursor != '\n'; cursor++)
1040 *cursor = '\0';
1042 if (name[0])
1044 /* FIXME: the following allocation is never reclaimed. */
1045 *archive_name_cursor = xstrdup (name);
1046 stop = true;
1048 else
1049 fprintf (stderr, "%s",
1050 _("File name not specified. Try again.\n"));
1052 break;
1054 case '!':
1055 if (!restrict_option)
1057 sys_spawn_shell ();
1058 break;
1060 /* FALL THROUGH */
1062 default:
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.
1072 static bool
1073 new_volume (enum access_mode mode)
1075 static FILE *read_file;
1076 static int looped;
1077 int prompt;
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;
1083 if (now_verifying)
1084 return false;
1085 if (verify_option)
1086 verify_volume ();
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;
1100 looped = 1;
1102 prompt = looped;
1104 tryagain:
1105 if (prompt)
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)));
1117 else
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);
1129 else
1130 switch (mode)
1132 case ACCESS_READ:
1133 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1134 rsh_command_option);
1135 guess_seekable_archive ();
1136 break;
1138 case ACCESS_WRITE:
1139 if (backup_option)
1140 maybe_backup_file (*archive_name_cursor, 1);
1141 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1142 rsh_command_option);
1143 break;
1145 case ACCESS_UPDATE:
1146 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1147 rsh_command_option);
1148 break;
1151 if (archive < 0)
1153 open_warn (*archive_name_cursor);
1154 if (!verify_option && mode == ACCESS_WRITE && backup_option)
1155 undo_last_backup ();
1156 prompt = 1;
1157 goto tryagain;
1160 SET_BINARY_MODE (archive);
1162 return true;
1165 static bool
1166 read_header0 (struct tar_stat_info *info)
1168 enum read_header rc;
1170 tar_stat_init (info);
1171 rc = read_header (&current_header, info, false);
1172 if (rc == HEADER_SUCCESS)
1174 set_next_block_after (current_header);
1175 return true;
1177 ERROR ((0, 0, _("This does not look like a tar archive")));
1178 return false;
1181 bool
1182 try_new_volume ()
1184 size_t status;
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;
1194 break;
1196 default:
1197 acc = ACCESS_READ;
1198 break;
1201 if (!new_volume (acc))
1202 return true;
1204 while ((status = rmtread (archive, record_start->buffer, record_size))
1205 == SAFE_READ_ERROR)
1206 archive_read_error ();
1208 if (status != record_size)
1209 short_read (status);
1211 header = find_next_block ();
1212 if (!header)
1213 return false;
1215 switch (header->header.typeflag)
1217 case XGLTYPE:
1219 if (!read_header0 (&dummy))
1220 return false;
1221 xheader_decode (&dummy); /* decodes values from the global header */
1222 tar_stat_destroy (&dummy);
1223 if (!real_s_name)
1225 /* We have read the extended header of the first member in
1226 this volume. Put it back, so next read_header works as
1227 expected. */
1228 current_block = record_start;
1230 break;
1233 case GNUTYPE_VOLHDR:
1234 if (!read_header0 (&dummy))
1235 return false;
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)
1241 break;
1242 /* FALL THROUGH */
1244 case GNUTYPE_MULTIVOL:
1245 if (!read_header0 (&dummy))
1246 return false;
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);
1253 break;
1255 default:
1256 break;
1259 if (real_s_name)
1261 uintmax_t s;
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)
1269 WARN ((0, 0,
1270 _("%s is possibly continued on this volume: header contains truncated name"),
1271 quote (real_s_name)));
1272 else
1274 WARN ((0, 0, _("%s is not continued on this volume"),
1275 quote (real_s_name)));
1276 return false;
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)));
1293 return false;
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)));
1307 return false;
1311 increase_volume_number ();
1312 return true;
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. */
1320 static bool
1321 check_label_pattern (const char *label)
1323 char *string;
1324 bool result;
1326 if (fnmatch (volume_label_option, label, 0) == 0)
1327 return true;
1329 if (!multi_volume_option)
1330 return false;
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;
1337 free (string);
1338 return result;
1341 /* Check if the next block contains a volume label and if this matches
1342 the one given in the command line */
1343 static void
1344 match_volume_label (void)
1346 if (!volume_label)
1348 union block *label = find_next_block ();
1350 if (!label)
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);
1357 else
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);
1376 if (!volume_label)
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. */
1387 static void
1388 _write_volume_label (const char *str)
1390 if (archive_format == POSIX_FORMAT)
1391 xheader_store ("GNU.volume.label", &dummy, str);
1392 else
1394 union block *label = find_next_block ();
1396 memset (label, 0, BLOCKSIZE);
1398 strcpy (label->header.name, str);
1399 assign_string (&current_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 (&current_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 */
1414 static void
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
1420 + strlen (p) + 2);
1421 sprintf (s, "%s %s %s", volume_label_option, VOL_SUFFIX, p);
1422 _write_volume_label (s);
1423 free (s);
1426 static void
1427 add_chunk_header ()
1429 if (archive_format == POSIX_FORMAT)
1431 off_t block_ordinal;
1432 union block *blk;
1433 struct tar_stat_info st;
1434 static size_t real_s_part_no; /* FIXME */
1436 real_s_part_no++;
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",
1444 real_s_part_no);
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);
1450 if (!blk)
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 */
1459 static void
1460 write_volume_label (void)
1462 if (multi_volume_option)
1463 add_volume_label ();
1464 else
1465 _write_volume_label (volume_label_option);
1468 /* Write GNU multi-volume header */
1469 static void
1470 gnu_add_multi_volume_header (void)
1472 int tmp;
1473 union block *block = find_next_block ();
1475 if (strlen (real_s_name) > NAME_FIELD_SIZE)
1476 WARN ((0, 0,
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;
1494 verbose_option = 0;
1495 finish_header (&current_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. */
1502 static void
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);
1512 else
1513 gnu_add_multi_volume_header ();
1516 /* Synchronize multi-volume globals */
1517 static void
1518 multi_volume_sync ()
1520 if (multi_volume_option)
1522 if (save_name)
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;
1530 else
1532 assign_string (&real_s_name, 0);
1533 real_s_totsize = 0;
1534 real_s_sizeleft = 0;
1540 /* Low-level flush functions */
1542 /* Simple flush read (no multi-volume or label extensions) */
1543 static void
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
1551 flush_read. */
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);
1564 for (;;)
1566 status = rmtread (archive, record_start->buffer, record_size);
1567 if (status == record_size)
1569 records_read++;
1570 return;
1572 if (status == SAFE_READ_ERROR)
1574 archive_read_error ();
1575 continue; /* try again */
1577 break;
1579 short_read (status);
1582 /* Simple flush write (no multi-volume or label extensions) */
1583 static void
1584 simple_flush_write (size_t level __attribute__((unused)))
1586 ssize_t status;
1588 status = _flush_write ();
1589 if (status != record_size)
1590 archive_write_error (status);
1591 else
1593 records_written++;
1594 bytes_written += status;
1599 /* GNU flush functions. These support multi-volume and archive labels in
1600 GNU and PAX archive formats. */
1602 static void
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
1610 flush_read. */
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 ();
1625 for (;;)
1627 status = rmtread (archive, record_start->buffer, record_size);
1628 if (status == record_size)
1630 records_read++;
1631 return;
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 */
1639 if ((status == 0
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 */
1647 flush_archive();
1648 return;
1650 else if (status == SAFE_READ_ERROR)
1652 archive_read_error ();
1653 continue;
1655 break;
1657 short_read (status);
1660 static void
1661 gnu_flush_read (void)
1663 flush_read_ptr = simple_flush_read; /* Avoid recursion */
1664 _gnu_flush_read ();
1665 flush_read_ptr = gnu_flush_read;
1668 static void
1669 _gnu_flush_write (size_t buffer_level)
1671 ssize_t status;
1672 union block *header;
1673 char *copy_ptr;
1674 size_t copy_size;
1675 size_t bufsize;
1676 tarlong wrt;
1678 status = _flush_write ();
1679 if (status != record_size && !multi_volume_option)
1680 archive_write_error (status);
1681 else
1683 if (status)
1684 records_written++;
1685 bytes_written += status;
1688 if (status == record_size)
1690 multi_volume_sync ();
1691 return;
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))
1707 return;
1709 tar_stat_destroy (&dummy);
1711 increase_volume_number ();
1712 prev_written += bytes_written;
1713 bytes_written = 0;
1715 copy_ptr = record_start->buffer + status;
1716 copy_size = buffer_level - status;
1718 /* Switch to the next buffer */
1719 record_index = !record_index;
1720 init_buffer ();
1722 if (volume_label_option)
1723 add_volume_label ();
1725 if (real_s_name)
1726 add_multi_volume_header ();
1728 write_extended (true, &dummy, find_next_block ());
1729 tar_stat_destroy (&dummy);
1731 if (real_s_name)
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
1754 that. */
1755 save_sizeleft += copy_size;
1756 multi_volume_sync ();
1758 find_next_block ();
1761 static void
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;
1769 void
1770 flush_read ()
1772 flush_read_ptr ();
1775 void
1776 flush_write ()
1778 flush_write_ptr (record_size);
1781 void
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)
1790 case ACCESS_READ:
1791 if (volume_label_option)
1792 match_volume_label ();
1793 break;
1795 case ACCESS_WRITE:
1796 records_written = 0;
1797 if (volume_label_option)
1798 write_volume_label ();
1799 break;
1801 default:
1802 break;
1804 set_volume_start_time ();