]> Dogcows Code - chaz/tar/blob - src/buffer.c
(new_volume): Update invocation
[chaz/tar] / src / buffer.c
1 /* Buffer management for tar.
2
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005 Free Software Foundation, Inc.
5
6 Written by John Gilmore, on 1985-08-25.
7
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
11 version.
12
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.
17
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. */
21
22 #include <system.h>
23
24 #include <signal.h>
25
26 #include <fnmatch.h>
27 #include <human.h>
28 #include <quotearg.h>
29
30 #include "common.h"
31 #include <rmt.h>
32
33 /* Number of retries before giving up on read. */
34 #define READ_ERROR_MAX 10
35
36 /* Globbing pattern to append to volume label if initial match failed. */
37 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
38
39 /* Variables. */
40
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 static int record_index;
45
46 /* FIXME: The following variables should ideally be static to this
47 module. However, this cannot be done yet. The cleanup continues! */
48
49 union block *record_start; /* start of record of archive */
50 union block *record_end; /* last+1 block of archive record */
51 union block *current_block; /* current block of archive */
52 enum access_mode access_mode; /* how do we handle the archive */
53 off_t records_read; /* number of records read from this archive */
54 off_t records_written; /* likewise, for records written */
55
56 static off_t record_start_block; /* block ordinal at record_start */
57
58 /* Where we write list messages (not errors, not interactions) to. */
59 FILE *stdlis;
60
61 static void backspace_output (void);
62
63 /* PID of child program, if compress_option or remote archive access. */
64 static pid_t child_pid;
65
66 /* Error recovery stuff */
67 static int read_error_count;
68
69 /* Have we hit EOF yet? */
70 static bool hit_eof;
71
72 /* Checkpointing counter */
73 static int checkpoint;
74
75 static bool read_full_records = false;
76
77 /* We're reading, but we just read the last block and it's time to update.
78 Declared in update.c
79
80 As least EXTERN like this one as possible. (?? --gray)
81 FIXME: Either eliminate it or move it to common.h.
82 */
83 extern bool time_to_start_writing;
84
85 bool write_archive_to_stdout;
86
87 void (*flush_write_ptr) (size_t);
88 void (*flush_read_ptr) (void);
89
90 \f
91 char *volume_label;
92 char *continued_file_name;
93 uintmax_t continued_file_size;
94 uintmax_t continued_file_offset;
95
96 \f
97 static int volno = 1; /* which volume of a multi-volume tape we're
98 on */
99 static int global_volno = 1; /* volume number to print in external
100 messages */
101
102 bool write_archive_to_stdout;
103
104 /* Used by flush_read and flush_write to store the real info about saved
105 names. */
106 static char *real_s_name;
107 static off_t real_s_totsize;
108 static off_t real_s_sizeleft;
109
110 \f
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 */
117
118 void
119 mv_begin (struct tar_stat_info *st)
120 {
121 if (multi_volume_option)
122 {
123 assign_string (&save_name, st->orig_file_name);
124 save_totsize = save_sizeleft = st->stat.st_size;
125 }
126 }
127
128 void
129 mv_end ()
130 {
131 if (multi_volume_option)
132 assign_string (&save_name, 0);
133 }
134
135 void
136 mv_total_size (off_t size)
137 {
138 save_totsize = size;
139 }
140
141 void
142 mv_size_left (off_t size)
143 {
144 save_sizeleft = size;
145 }
146
147 \f
148 /* Functions. */
149
150 void
151 clear_read_error_count (void)
152 {
153 read_error_count = 0;
154 }
155
156 \f
157 /* Time-related functions */
158
159 double duration;
160
161 void
162 set_start_time ()
163 {
164 gettime (&start_time);
165 }
166
167 void
168 compute_duration ()
169 {
170 struct timespec now;
171 gettime (&now);
172 duration += ((now.tv_sec - start_time.tv_sec)
173 + (now.tv_nsec - start_time.tv_nsec) / 1e9);
174 set_start_time ();
175 }
176
177 \f
178 /* Compression detection */
179
180 enum compress_type {
181 ct_none,
182 ct_compress,
183 ct_gzip,
184 ct_bzip2
185 };
186
187 struct zip_magic
188 {
189 enum compress_type type;
190 size_t length;
191 char *magic;
192 char *program;
193 char *option;
194 };
195
196 static struct zip_magic const magic[] = {
197 { ct_none, },
198 { ct_compress, 2, "\037\235", "compress", "-Z" },
199 { ct_gzip, 2, "\037\213", "gzip", "-z" },
200 { ct_bzip2, 3, "BZh", "bzip2", "-j" },
201 };
202
203 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
204
205 #define compress_option(t) magic[t].option
206 #define compress_program(t) magic[t].program
207
208 /* Check if the file ARCHIVE is a compressed archive. */
209 enum compress_type
210 check_compressed_archive ()
211 {
212 struct zip_magic const *p;
213 bool sfr;
214
215 /* Prepare global data needed for find_next_block: */
216 record_end = record_start; /* set up for 1st record = # 0 */
217 sfr = read_full_records;
218 read_full_records = true; /* Suppress fatal error on reading a partial
219 record */
220 find_next_block ();
221
222 /* Restore global values */
223 read_full_records = sfr;
224
225 if (tar_checksum (record_start, true) == HEADER_SUCCESS)
226 /* Probably a valid header */
227 return ct_none;
228
229 for (p = magic + 1; p < magic + NMAGIC; p++)
230 if (memcmp (record_start->buffer, p->magic, p->length) == 0)
231 return p->type;
232
233 return ct_none;
234 }
235
236 /* Open an archive named archive_name_array[0]. Detect if it is
237 a compressed archive of known type and use corresponding decompression
238 program if so */
239 int
240 open_compressed_archive ()
241 {
242 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
243 MODE_RW, rsh_command_option);
244 if (archive == -1)
245 return archive;
246
247 if (!multi_volume_option)
248 {
249 enum compress_type type = check_compressed_archive ();
250
251 if (type == ct_none)
252 return archive;
253
254 /* FD is not needed any more */
255 rmtclose (archive);
256
257 hit_eof = false; /* It might have been set by find_next_block in
258 check_compressed_archive */
259
260 /* Open compressed archive */
261 use_compress_program_option = compress_program (type);
262 child_pid = sys_child_open_for_uncompress ();
263 read_full_records = true;
264 }
265
266 records_read = 0;
267 record_end = record_start; /* set up for 1st record = # 0 */
268
269 return archive;
270 }
271 \f
272
273 void
274 print_total_written (void)
275 {
276 tarlong written = prev_written + bytes_written;
277 char bytes[sizeof (tarlong) * CHAR_BIT];
278 char abbr[LONGEST_HUMAN_READABLE + 1];
279 char rate[LONGEST_HUMAN_READABLE + 1];
280
281 int human_opts = human_autoscale | human_base_1024 | human_SI | human_B;
282
283 sprintf (bytes, TARLONG_FORMAT, written);
284
285 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
286 fprintf (stderr, _("Total bytes written: %s (%s, %s/s)\n"), bytes,
287 human_readable (written, abbr, human_opts, 1, 1),
288 (0 < duration && written / duration < (uintmax_t) -1
289 ? human_readable (written / duration, rate, human_opts, 1, 1)
290 : "?"));
291 }
292
293 /* Compute and return the block ordinal at current_block. */
294 off_t
295 current_block_ordinal (void)
296 {
297 return record_start_block + (current_block - record_start);
298 }
299
300 /* If the EOF flag is set, reset it, as well as current_block, etc. */
301 void
302 reset_eof (void)
303 {
304 if (hit_eof)
305 {
306 hit_eof = false;
307 current_block = record_start;
308 record_end = record_start + blocking_factor;
309 access_mode = ACCESS_WRITE;
310 }
311 }
312
313 /* Return the location of the next available input or output block.
314 Return zero for EOF. Once we have returned zero, we just keep returning
315 it, to avoid accidentally going on to the next file on the tape. */
316 union block *
317 find_next_block (void)
318 {
319 if (current_block == record_end)
320 {
321 if (hit_eof)
322 return 0;
323 flush_archive ();
324 if (current_block == record_end)
325 {
326 hit_eof = true;
327 return 0;
328 }
329 }
330 return current_block;
331 }
332
333 /* Indicate that we have used all blocks up thru BLOCK. */
334 void
335 set_next_block_after (union block *block)
336 {
337 while (block >= current_block)
338 current_block++;
339
340 /* Do *not* flush the archive here. If we do, the same argument to
341 set_next_block_after could mean the next block (if the input record
342 is exactly one block long), which is not what is intended. */
343
344 if (current_block > record_end)
345 abort ();
346 }
347
348 /* Return the number of bytes comprising the space between POINTER
349 through the end of the current buffer of blocks. This space is
350 available for filling with data, or taking data from. POINTER is
351 usually (but not always) the result of previous find_next_block call. */
352 size_t
353 available_space_after (union block *pointer)
354 {
355 return record_end->buffer - pointer->buffer;
356 }
357
358 /* Close file having descriptor FD, and abort if close unsuccessful. */
359 void
360 xclose (int fd)
361 {
362 if (close (fd) != 0)
363 close_error (_("(pipe)"));
364 }
365
366 static void
367 init_buffer ()
368 {
369 if (!record_buffer[record_index])
370 page_aligned_alloc (&record_buffer[record_index], record_size);
371
372 record_start = record_buffer[record_index];
373 current_block = record_start;
374 record_end = record_start + blocking_factor;
375 }
376
377 /* Open an archive file. The argument specifies whether we are
378 reading or writing, or both. */
379 static void
380 _open_archive (enum access_mode wanted_access)
381 {
382 int backed_up_flag = 0;
383
384 if (index_file_name)
385 {
386 stdlis = fopen (index_file_name, "w");
387 if (! stdlis)
388 open_error (index_file_name);
389 }
390 else
391 stdlis = to_stdout_option ? stderr : stdout;
392
393 if (record_size == 0)
394 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
395
396 if (archive_names == 0)
397 FATAL_ERROR ((0, 0, _("No archive name given")));
398
399 tar_stat_destroy (&current_stat_info);
400 save_name = 0;
401 real_s_name = 0;
402
403 record_index = 0;
404 init_buffer ();
405
406 /* When updating the archive, we start with reading. */
407 access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
408
409 read_full_records = read_full_records_option;
410
411 records_read = 0;
412
413 if (use_compress_program_option)
414 {
415 switch (wanted_access)
416 {
417 case ACCESS_READ:
418 child_pid = sys_child_open_for_uncompress ();
419 read_full_records = true;
420 record_end = record_start; /* set up for 1st record = # 0 */
421 break;
422
423 case ACCESS_WRITE:
424 child_pid = sys_child_open_for_compress ();
425 break;
426
427 case ACCESS_UPDATE:
428 abort (); /* Should not happen */
429 break;
430 }
431
432 if (wanted_access == ACCESS_WRITE
433 && strcmp (archive_name_array[0], "-") == 0)
434 stdlis = stderr;
435 }
436 else if (strcmp (archive_name_array[0], "-") == 0)
437 {
438 read_full_records = true; /* could be a pipe, be safe */
439 if (verify_option)
440 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
441
442 switch (wanted_access)
443 {
444 case ACCESS_READ:
445 {
446 enum compress_type type;
447
448 archive = STDIN_FILENO;
449
450 type = check_compressed_archive (archive);
451 if (type != ct_none)
452 FATAL_ERROR ((0, 0,
453 _("Archive is compressed. Use %s option"),
454 compress_option (type)));
455 }
456 break;
457
458 case ACCESS_WRITE:
459 archive = STDOUT_FILENO;
460 stdlis = stderr;
461 break;
462
463 case ACCESS_UPDATE:
464 archive = STDIN_FILENO;
465 stdlis = stderr;
466 write_archive_to_stdout = true;
467 break;
468 }
469 }
470 else if (verify_option)
471 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
472 MODE_RW, rsh_command_option);
473 else
474 switch (wanted_access)
475 {
476 case ACCESS_READ:
477 archive = open_compressed_archive ();
478 break;
479
480 case ACCESS_WRITE:
481 if (backup_option)
482 {
483 maybe_backup_file (archive_name_array[0], 1);
484 backed_up_flag = 1;
485 }
486 archive = rmtcreat (archive_name_array[0], MODE_RW,
487 rsh_command_option);
488 break;
489
490 case ACCESS_UPDATE:
491 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
492 MODE_RW, rsh_command_option);
493 break;
494 }
495
496 if (archive < 0
497 || (! _isrmt (archive) && !sys_get_archive_stat ()))
498 {
499 int saved_errno = errno;
500
501 if (backed_up_flag)
502 undo_last_backup ();
503 errno = saved_errno;
504 open_fatal (archive_name_array[0]);
505 }
506
507 sys_detect_dev_null_output ();
508 sys_save_archive_dev_ino ();
509 SET_BINARY_MODE (archive);
510
511 switch (wanted_access)
512 {
513 case ACCESS_UPDATE:
514 records_written = 0;
515 record_end = record_start; /* set up for 1st record = # 0 */
516
517 case ACCESS_READ:
518 find_next_block (); /* read it in, check for EOF */
519 break;
520
521 case ACCESS_WRITE:
522 records_written = 0;
523 break;
524 }
525 }
526
527 /* Perform a write to flush the buffer. */
528 ssize_t
529 _flush_write (void)
530 {
531 ssize_t status;
532
533 if (checkpoint_option && !(++checkpoint % 10))
534 /* TRANSLATORS: This is a ``checkpoint of write operation'',
535 *not* ``Writing a checkpoint''.
536 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
537 *not* ``Escribiendo un punto de comprobaci@'on'' */
538 WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
539
540 if (tape_length_option && tape_length_option <= bytes_written)
541 {
542 errno = ENOSPC;
543 status = 0;
544 }
545 else if (dev_null_output)
546 status = record_size;
547 else
548 status = sys_write_archive_buffer ();
549
550 return status;
551 }
552
553 /* Handle write errors on the archive. Write errors are always fatal.
554 Hitting the end of a volume does not cause a write error unless the
555 write was the first record of the volume. */
556 void
557 archive_write_error (ssize_t status)
558 {
559 /* It might be useful to know how much was written before the error
560 occurred. */
561 if (totals_option)
562 {
563 int e = errno;
564 print_total_written ();
565 errno = e;
566 }
567
568 write_fatal_details (*archive_name_cursor, status, record_size);
569 }
570
571 /* Handle read errors on the archive. If the read should be retried,
572 return to the caller. */
573 void
574 archive_read_error (void)
575 {
576 read_error (*archive_name_cursor);
577
578 if (record_start_block == 0)
579 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
580
581 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
582 then give up on reading the archive. */
583
584 if (read_error_count++ > READ_ERROR_MAX)
585 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
586 return;
587 }
588
589 static void
590 short_read (size_t status)
591 {
592 size_t left; /* bytes left */
593 char *more; /* pointer to next byte to read */
594
595 more = record_start->buffer + status;
596 left = record_size - status;
597
598 while (left % BLOCKSIZE != 0
599 || (left && status && read_full_records))
600 {
601 if (status)
602 while ((status = rmtread (archive, more, left)) == SAFE_READ_ERROR)
603 archive_read_error ();
604
605 if (status == 0)
606 break;
607
608 if (! read_full_records)
609 {
610 unsigned long rest = record_size - left;
611
612 FATAL_ERROR ((0, 0,
613 ngettext ("Unaligned block (%lu byte) in archive",
614 "Unaligned block (%lu bytes) in archive",
615 rest),
616 rest));
617 }
618
619 /* User warned us about this. Fix up. */
620
621 left -= status;
622 more += status;
623 }
624
625 /* FIXME: for size=0, multi-volume support. On the first record, warn
626 about the problem. */
627
628 if (!read_full_records && verbose_option > 1
629 && record_start_block == 0 && status != 0)
630 {
631 unsigned long rsize = (record_size - left) / BLOCKSIZE;
632 WARN ((0, 0,
633 ngettext ("Record size = %lu block",
634 "Record size = %lu blocks",
635 rsize),
636 rsize));
637 }
638
639 record_end = record_start + (record_size - left) / BLOCKSIZE;
640 records_read++;
641 }
642
643 /* Perform a read to flush the buffer. */
644 size_t
645 _flush_read (void)
646 {
647 size_t status; /* result from system call */
648
649 if (checkpoint_option && !(++checkpoint % 10))
650 /* TRANSLATORS: This is a ``checkpoint of read operation'',
651 *not* ``Reading a checkpoint''.
652 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
653 *not* ``Leyendo un punto de comprobaci@'on'' */
654 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
655
656 /* Clear the count of errors. This only applies to a single call to
657 flush_read. */
658
659 read_error_count = 0; /* clear error count */
660
661 if (write_archive_to_stdout && record_start_block != 0)
662 {
663 archive = STDOUT_FILENO;
664 status = sys_write_archive_buffer ();
665 archive = STDIN_FILENO;
666 if (status != record_size)
667 archive_write_error (status);
668 }
669
670 status = rmtread (archive, record_start->buffer, record_size);
671 if (status == record_size)
672 records_read++;
673 return status;
674 }
675
676 /* Flush the current buffer to/from the archive. */
677 void
678 flush_archive (void)
679 {
680 size_t buffer_level = current_block->buffer - record_start->buffer;
681 record_start_block += record_end - record_start;
682 current_block = record_start;
683 record_end = record_start + blocking_factor;
684
685 if (access_mode == ACCESS_READ && time_to_start_writing)
686 {
687 access_mode = ACCESS_WRITE;
688 time_to_start_writing = false;
689 backspace_output ();
690 }
691
692 switch (access_mode)
693 {
694 case ACCESS_READ:
695 flush_read ();
696 break;
697
698 case ACCESS_WRITE:
699 flush_write_ptr (buffer_level);
700 break;
701
702 case ACCESS_UPDATE:
703 abort ();
704 }
705 }
706
707 /* Backspace the archive descriptor by one record worth. If it's a
708 tape, MTIOCTOP will work. If it's something else, try to seek on
709 it. If we can't seek, we lose! */
710 static void
711 backspace_output (void)
712 {
713 #ifdef MTIOCTOP
714 {
715 struct mtop operation;
716
717 operation.mt_op = MTBSR;
718 operation.mt_count = 1;
719 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
720 return;
721 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
722 return;
723 }
724 #endif
725
726 {
727 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
728
729 /* Seek back to the beginning of this record and start writing there. */
730
731 position -= record_size;
732 if (position < 0)
733 position = 0;
734 if (rmtlseek (archive, position, SEEK_SET) != position)
735 {
736 /* Lseek failed. Try a different method. */
737
738 WARN ((0, 0,
739 _("Cannot backspace archive file; it may be unreadable without -i")));
740
741 /* Replace the first part of the record with NULs. */
742
743 if (record_start->buffer != output_start)
744 memset (record_start->buffer, 0,
745 output_start - record_start->buffer);
746 }
747 }
748 }
749
750 off_t
751 seek_archive (off_t size)
752 {
753 off_t start = current_block_ordinal ();
754 off_t offset;
755 off_t nrec, nblk;
756 off_t skipped = (blocking_factor - (current_block - record_start));
757
758 size -= skipped * BLOCKSIZE;
759
760 if (size < record_size)
761 return 0;
762 /* FIXME: flush? */
763
764 /* Compute number of records to skip */
765 nrec = size / record_size;
766 offset = rmtlseek (archive, nrec * record_size, SEEK_CUR);
767 if (offset < 0)
768 return offset;
769
770 if (offset % record_size)
771 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
772
773 /* Convert to number of records */
774 offset /= BLOCKSIZE;
775 /* Compute number of skipped blocks */
776 nblk = offset - start;
777
778 /* Update buffering info */
779 records_read += nblk / blocking_factor;
780 record_start_block = offset - blocking_factor;
781 current_block = record_end;
782
783 return nblk;
784 }
785
786 /* Close the archive file. */
787 void
788 close_archive (void)
789 {
790 if (time_to_start_writing || access_mode == ACCESS_WRITE)
791 {
792 flush_archive ();
793 if (current_block > record_start)
794 flush_archive ();
795 }
796
797 sys_drain_input_pipe ();
798
799 compute_duration ();
800 if (verify_option)
801 verify_volume ();
802
803 if (rmtclose (archive) != 0)
804 close_warn (*archive_name_cursor);
805
806 sys_wait_for_child (child_pid);
807
808 tar_stat_destroy (&current_stat_info);
809 if (save_name)
810 free (save_name);
811 if (real_s_name)
812 free (real_s_name);
813 free (record_buffer[0]);
814 free (record_buffer[1]);
815 }
816
817 /* Called to initialize the global volume number. */
818 void
819 init_volume_number (void)
820 {
821 FILE *file = fopen (volno_file_option, "r");
822
823 if (file)
824 {
825 if (fscanf (file, "%d", &global_volno) != 1
826 || global_volno < 0)
827 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
828 quotearg_colon (volno_file_option)));
829 if (ferror (file))
830 read_error (volno_file_option);
831 if (fclose (file) != 0)
832 close_error (volno_file_option);
833 }
834 else if (errno != ENOENT)
835 open_error (volno_file_option);
836 }
837
838 /* Called to write out the closing global volume number. */
839 void
840 closeout_volume_number (void)
841 {
842 FILE *file = fopen (volno_file_option, "w");
843
844 if (file)
845 {
846 fprintf (file, "%d\n", global_volno);
847 if (ferror (file))
848 write_error (volno_file_option);
849 if (fclose (file) != 0)
850 close_error (volno_file_option);
851 }
852 else
853 open_error (volno_file_option);
854 }
855
856 \f
857 static void
858 increase_volume_number ()
859 {
860 global_volno++;
861 if (global_volno < 0)
862 FATAL_ERROR ((0, 0, _("Volume number overflow")));
863 volno++;
864 }
865
866 void
867 change_tape_menu (FILE *read_file)
868 {
869 char *input_buffer = NULL;
870 size_t size = 0;
871
872 while (1)
873 {
874 fputc ('\007', stderr);
875 fprintf (stderr,
876 _("Prepare volume #%d for %s and hit return: "),
877 global_volno + 1, quote (*archive_name_cursor));
878 fflush (stderr);
879
880 if (getline (&input_buffer, &size, read_file) <= 0)
881 {
882 WARN ((0, 0, _("EOF where user reply was expected")));
883
884 if (subcommand_option != EXTRACT_SUBCOMMAND
885 && subcommand_option != LIST_SUBCOMMAND
886 && subcommand_option != DIFF_SUBCOMMAND)
887 WARN ((0, 0, _("WARNING: Archive is incomplete")));
888
889 fatal_exit ();
890 }
891
892 if (input_buffer[0] == '\n'
893 || input_buffer[0] == 'y'
894 || input_buffer[0] == 'Y')
895 break;
896
897 switch (input_buffer[0])
898 {
899 case '?':
900 {
901 fprintf (stderr, _("\
902 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
903 q Abort tar\n\
904 y or newline Continue operation\n"));
905 if (!restrict_option)
906 fprintf (stderr, _(" ! Spawn a subshell\n"));
907 fprintf (stderr, _(" ? Print this list\n"));
908 }
909 break;
910
911 case 'q':
912 /* Quit. */
913
914 WARN ((0, 0, _("No new volume; exiting.\n")));
915
916 if (subcommand_option != EXTRACT_SUBCOMMAND
917 && subcommand_option != LIST_SUBCOMMAND
918 && subcommand_option != DIFF_SUBCOMMAND)
919 WARN ((0, 0, _("WARNING: Archive is incomplete")));
920
921 fatal_exit ();
922
923 case 'n':
924 /* Get new file name. */
925
926 {
927 char *name;
928 char *cursor;
929
930 for (name = input_buffer + 1;
931 *name == ' ' || *name == '\t';
932 name++)
933 ;
934
935 for (cursor = name; *cursor && *cursor != '\n'; cursor++)
936 ;
937 *cursor = '\0';
938
939 /* FIXME: the following allocation is never reclaimed. */
940 *archive_name_cursor = xstrdup (name);
941 }
942 break;
943
944 case '!':
945 if (!restrict_option)
946 {
947 sys_spawn_shell ();
948 break;
949 }
950 /* FALL THROUGH */
951
952 default:
953 fprintf (stderr, _("Invalid input. Type ? for help.\n"));
954 }
955 }
956 free (input_buffer);
957 }
958
959 /* We've hit the end of the old volume. Close it and open the next one.
960 Return nonzero on success.
961 */
962 static bool
963 new_volume (enum access_mode mode)
964 {
965 static FILE *read_file;
966 static int looped;
967
968 if (!read_file && !info_script_option)
969 /* FIXME: if fopen is used, it will never be closed. */
970 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
971
972 if (now_verifying)
973 return false;
974 if (verify_option)
975 verify_volume ();
976
977 assign_string (&volume_label, NULL);
978 assign_string (&continued_file_name, NULL);
979 continued_file_size = continued_file_offset = 0;
980
981 if (rmtclose (archive) != 0)
982 close_warn (*archive_name_cursor);
983
984 archive_name_cursor++;
985 if (archive_name_cursor == archive_name_array + archive_names)
986 {
987 archive_name_cursor = archive_name_array;
988 looped = 1;
989 }
990
991 tryagain:
992 if (looped)
993 {
994 /* We have to prompt from now on. */
995
996 if (info_script_option)
997 {
998 if (volno_file_option)
999 closeout_volume_number ();
1000 if (sys_exec_info_script (archive_name_cursor, global_volno+1))
1001 FATAL_ERROR ((0, 0, _("%s command failed"),
1002 quote (info_script_option)));
1003 }
1004 else
1005 change_tape_menu (read_file);
1006 }
1007
1008 if (strcmp (archive_name_cursor[0], "-") == 0)
1009 {
1010 read_full_records = true;
1011 archive = STDIN_FILENO;
1012 }
1013 else if (verify_option)
1014 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1015 rsh_command_option);
1016 else
1017 switch (mode)
1018 {
1019 case ACCESS_READ:
1020 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1021 rsh_command_option);
1022 break;
1023
1024 case ACCESS_WRITE:
1025 if (backup_option)
1026 maybe_backup_file (*archive_name_cursor, 1);
1027 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1028 rsh_command_option);
1029 break;
1030
1031 case ACCESS_UPDATE:
1032 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1033 rsh_command_option);
1034 break;
1035 }
1036
1037 if (archive < 0)
1038 {
1039 open_warn (*archive_name_cursor);
1040 if (!verify_option && mode == ACCESS_WRITE && backup_option)
1041 undo_last_backup ();
1042 goto tryagain;
1043 }
1044
1045 SET_BINARY_MODE (archive);
1046
1047 return true;
1048 }
1049
1050 static bool
1051 read_header0 ()
1052 {
1053 enum read_header rc = read_header (false);
1054
1055 if (rc == HEADER_SUCCESS)
1056 {
1057 set_next_block_after (current_header);
1058 return true;
1059 }
1060 ERROR ((0, 0, _("This does not look like a tar archive")));
1061 return false;
1062 }
1063
1064 bool
1065 try_new_volume ()
1066 {
1067 size_t status;
1068 union block *header;
1069
1070 switch (subcommand_option)
1071 {
1072 case APPEND_SUBCOMMAND:
1073 case CAT_SUBCOMMAND:
1074 case UPDATE_SUBCOMMAND:
1075 if (!new_volume (ACCESS_UPDATE))
1076 return true;
1077 break;
1078
1079 default:
1080 if (!new_volume (ACCESS_READ))
1081 return true;
1082 break;
1083 }
1084
1085 while ((status = rmtread (archive, record_start->buffer, record_size))
1086 == SAFE_READ_ERROR)
1087 archive_read_error ();
1088
1089 if (status != record_size)
1090 short_read (status);
1091
1092 header = find_next_block ();
1093 if (!header)
1094 return false;
1095 switch (header->header.typeflag)
1096 {
1097 case XGLTYPE:
1098 {
1099 struct tar_stat_info dummy;
1100 if (!read_header0 ())
1101 return false;
1102 tar_stat_init (&dummy);
1103 xheader_decode (&dummy); /* decodes values from the global header */
1104 tar_stat_destroy (&dummy);
1105 if (!real_s_name)
1106 {
1107 /* We have read the extended header of the first member in
1108 this volume. Put it back, so next read_header works as
1109 expected. */
1110 current_block = record_start;
1111 }
1112 break;
1113 }
1114
1115 case GNUTYPE_VOLHDR:
1116 if (!read_header0 ())
1117 return false;
1118 assign_string (&volume_label, current_header->header.name);
1119 set_next_block_after (header);
1120 header = find_next_block ();
1121 if (header->header.typeflag != GNUTYPE_MULTIVOL)
1122 break;
1123 /* FALL THROUGH */
1124
1125 case GNUTYPE_MULTIVOL:
1126 if (!read_header0 ())
1127 return false;
1128 assign_string (&continued_file_name, current_header->header.name);
1129 continued_file_size =
1130 UINTMAX_FROM_HEADER (current_header->header.size);
1131 continued_file_offset =
1132 UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset);
1133 break;
1134
1135 default:
1136 break;
1137 }
1138
1139 if (real_s_name)
1140 {
1141 uintmax_t s;
1142 if (!continued_file_name
1143 || strcmp (continued_file_name, real_s_name))
1144 {
1145 WARN ((0, 0, _("%s is not continued on this volume"),
1146 quote (real_s_name)));
1147 return false;
1148 }
1149
1150 s = continued_file_size + continued_file_offset;
1151
1152 if (real_s_totsize != s || s < continued_file_offset)
1153 {
1154 char totsizebuf[UINTMAX_STRSIZE_BOUND];
1155 char s1buf[UINTMAX_STRSIZE_BOUND];
1156 char s2buf[UINTMAX_STRSIZE_BOUND];
1157
1158 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1159 quote (continued_file_name),
1160 STRINGIFY_BIGINT (save_totsize, totsizebuf),
1161 STRINGIFY_BIGINT (continued_file_size, s1buf),
1162 STRINGIFY_BIGINT (continued_file_offset, s2buf)));
1163 return false;
1164 }
1165
1166 if (real_s_totsize - real_s_sizeleft != continued_file_offset)
1167 {
1168 WARN ((0, 0, _("This volume is out of sequence")));
1169 return false;
1170 }
1171 }
1172
1173 increase_volume_number ();
1174 return true;
1175 }
1176
1177 \f
1178 /* Check the LABEL block against the volume label, seen as a globbing
1179 pattern. Return true if the pattern matches. In case of failure,
1180 retry matching a volume sequence number before giving up in
1181 multi-volume mode. */
1182 static bool
1183 check_label_pattern (union block *label)
1184 {
1185 char *string;
1186 bool result;
1187
1188 if (! memchr (label->header.name, '\0', sizeof label->header.name))
1189 return false;
1190
1191 if (fnmatch (volume_label_option, label->header.name, 0) == 0)
1192 return true;
1193
1194 if (!multi_volume_option)
1195 return false;
1196
1197 string = xmalloc (strlen (volume_label_option)
1198 + sizeof VOLUME_LABEL_APPEND + 1);
1199 strcpy (string, volume_label_option);
1200 strcat (string, VOLUME_LABEL_APPEND);
1201 result = fnmatch (string, label->header.name, 0) == 0;
1202 free (string);
1203 return result;
1204 }
1205
1206 /* Check if the next block contains a volume label and if this matches
1207 the one given in the command line */
1208 static void
1209 match_volume_label (void)
1210 {
1211 union block *label = find_next_block ();
1212
1213 if (!label)
1214 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1215 quote (volume_label_option)));
1216 if (!check_label_pattern (label))
1217 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1218 quote_n (0, label->header.name),
1219 quote_n (1, volume_label_option)));
1220 }
1221
1222 /* Mark the archive with volume label STR. */
1223 static void
1224 _write_volume_label (const char *str)
1225 {
1226 if (archive_format == POSIX_FORMAT)
1227 xheader_store ("GNU.volume.label", NULL, str);
1228 else
1229 {
1230 union block *label = find_next_block ();
1231
1232 memset (label, 0, BLOCKSIZE);
1233
1234 strcpy (label->header.name, volume_label_option);
1235 assign_string (&current_stat_info.file_name,
1236 label->header.name);
1237 current_stat_info.had_trailing_slash =
1238 strip_trailing_slashes (current_stat_info.file_name);
1239
1240 label->header.typeflag = GNUTYPE_VOLHDR;
1241 TIME_TO_CHARS (start_time.tv_sec, label->header.mtime);
1242 finish_header (&current_stat_info, label, -1);
1243 set_next_block_after (label);
1244 }
1245 }
1246
1247 #define VOL_SUFFIX "Volume"
1248
1249 /* Add a volume label to a part of multi-volume archive */
1250 static void
1251 add_volume_label (void)
1252 {
1253 char buf[UINTMAX_STRSIZE_BOUND];
1254 char *p = STRINGIFY_BIGINT (volno, buf);
1255 char *s = xmalloc (strlen (volume_label_option) + sizeof VOL_SUFFIX
1256 + strlen (p) + 2);
1257 sprintf (s, "%s %s %s", volume_label_option, VOL_SUFFIX, p);
1258 _write_volume_label (s);
1259 free (s);
1260 }
1261
1262 static void
1263 add_chunk_header ()
1264 {
1265 if (archive_format == POSIX_FORMAT)
1266 {
1267 off_t block_ordinal;
1268 union block *blk;
1269 struct tar_stat_info st;
1270 static size_t real_s_part_no; /* FIXME */
1271
1272 real_s_part_no++;
1273 memset (&st, 0, sizeof st);
1274 st.orig_file_name = st.file_name = real_s_name;
1275 st.stat.st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
1276 st.stat.st_uid = getuid ();
1277 st.stat.st_gid = getgid ();
1278 st.orig_file_name = xheader_format_name (&st,
1279 "%d/GNUFileParts.%p/%f.%n",
1280 real_s_part_no);
1281 st.file_name = st.orig_file_name;
1282 st.archive_file_size = st.stat.st_size = real_s_sizeleft;
1283
1284 block_ordinal = current_block_ordinal ();
1285 blk = start_header (&st);
1286 free (st.orig_file_name);
1287 if (!blk)
1288 abort (); /* FIXME */
1289 finish_header (&st, blk, block_ordinal);
1290 }
1291 }
1292
1293
1294 /* Add a volume label to the current archive */
1295 static void
1296 write_volume_label (void)
1297 {
1298 if (multi_volume_option)
1299 add_volume_label ();
1300 else
1301 _write_volume_label (volume_label_option);
1302 }
1303
1304 /* Write GNU multi-volume header */
1305 static void
1306 gnu_add_multi_volume_header (void)
1307 {
1308 int tmp;
1309 union block *block = find_next_block ();
1310
1311 if (strlen (real_s_name) > NAME_FIELD_SIZE)
1312 WARN ((0, 0,
1313 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1314 quotearg_colon (real_s_name)));
1315
1316 memset (block, 0, BLOCKSIZE);
1317
1318 /* FIXME: Michael P Urban writes: [a long name file] is being written
1319 when a new volume rolls around [...] Looks like the wrong value is
1320 being preserved in real_s_name, though. */
1321
1322 strncpy (block->header.name, real_s_name, NAME_FIELD_SIZE);
1323 block->header.typeflag = GNUTYPE_MULTIVOL;
1324
1325 OFF_TO_CHARS (real_s_sizeleft, block->header.size);
1326 OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
1327 block->oldgnu_header.offset);
1328
1329 tmp = verbose_option;
1330 verbose_option = 0;
1331 finish_header (&current_stat_info, block, -1);
1332 verbose_option = tmp;
1333 set_next_block_after (block);
1334 }
1335
1336 /* Add a multi volume header to the current archive. The exact header format
1337 depends on the archive format. */
1338 static void
1339 add_multi_volume_header (void)
1340 {
1341 if (archive_format == POSIX_FORMAT)
1342 {
1343 off_t d = real_s_totsize - real_s_sizeleft;
1344 xheader_store ("GNU.volume.filename", NULL, real_s_name);
1345 xheader_store ("GNU.volume.size", NULL, &real_s_sizeleft);
1346 xheader_store ("GNU.volume.offset", NULL, &d);
1347 }
1348 else
1349 gnu_add_multi_volume_header ();
1350 }
1351
1352 /* Synchronize multi-volume globals */
1353 static void
1354 multi_volume_sync ()
1355 {
1356 if (multi_volume_option)
1357 {
1358 if (save_name)
1359 {
1360 assign_string (&real_s_name,
1361 safer_name_suffix (save_name, false,
1362 absolute_names_option));
1363 real_s_totsize = save_totsize;
1364 real_s_sizeleft = save_sizeleft;
1365 }
1366 else
1367 {
1368 assign_string (&real_s_name, 0);
1369 real_s_totsize = 0;
1370 real_s_sizeleft = 0;
1371 }
1372 }
1373 }
1374
1375 \f
1376 /* Low-level flush functions */
1377
1378 /* Simple flush read (no multi-volume or label extensions) */
1379 static void
1380 simple_flush_read (void)
1381 {
1382 size_t status; /* result from system call */
1383
1384 if (checkpoint_option && !(++checkpoint % 10))
1385 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1386 *not* ``Reading a checkpoint''.
1387 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1388 *not* ``Leyendo un punto de comprobaci@'on'' */
1389 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
1390
1391 /* Clear the count of errors. This only applies to a single call to
1392 flush_read. */
1393
1394 read_error_count = 0; /* clear error count */
1395
1396 if (write_archive_to_stdout && record_start_block != 0)
1397 {
1398 archive = STDOUT_FILENO;
1399 status = sys_write_archive_buffer ();
1400 archive = STDIN_FILENO;
1401 if (status != record_size)
1402 archive_write_error (status);
1403 }
1404
1405 for (;;)
1406 {
1407 status = rmtread (archive, record_start->buffer, record_size);
1408 if (status == record_size)
1409 {
1410 records_read++;
1411 return;
1412 }
1413 if (status == SAFE_READ_ERROR)
1414 {
1415 archive_read_error ();
1416 continue; /* try again */
1417 }
1418 break;
1419 }
1420 short_read (status);
1421 }
1422
1423 /* Simple flush write (no multi-volume or label extensions) */
1424 static void
1425 simple_flush_write (size_t level __attribute__((unused)))
1426 {
1427 ssize_t status;
1428
1429 status = _flush_write ();
1430 if (status != record_size)
1431 archive_write_error (status);
1432 else
1433 {
1434 records_written++;
1435 bytes_written += status;
1436 }
1437 }
1438
1439 \f
1440 /* GNU flush functions. These support multi-volume and archive labels in
1441 GNU and PAX archive formats. */
1442
1443 static void
1444 _gnu_flush_read (void)
1445 {
1446 size_t status; /* result from system call */
1447
1448 if (checkpoint_option && !(++checkpoint % 10))
1449 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1450 *not* ``Reading a checkpoint''.
1451 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1452 *not* ``Leyendo un punto de comprobaci@'on'' */
1453 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
1454
1455 /* Clear the count of errors. This only applies to a single call to
1456 flush_read. */
1457
1458 read_error_count = 0; /* clear error count */
1459
1460 if (write_archive_to_stdout && record_start_block != 0)
1461 {
1462 archive = STDOUT_FILENO;
1463 status = sys_write_archive_buffer ();
1464 archive = STDIN_FILENO;
1465 if (status != record_size)
1466 archive_write_error (status);
1467 }
1468
1469 multi_volume_sync ();
1470
1471 for (;;)
1472 {
1473 status = rmtread (archive, record_start->buffer, record_size);
1474 if (status == record_size)
1475 {
1476 records_read++;
1477 return;
1478 }
1479
1480 /* The condition below used to include
1481 || (status > 0 && !read_full_records)
1482 This is incorrect since even if new_volume() succeeds, the
1483 subsequent call to rmtread will overwrite the chunk of data
1484 already read in the buffer, so the processing will fail */
1485 if ((status == 0
1486 || (status == SAFE_READ_ERROR && errno == ENOSPC))
1487 && multi_volume_option)
1488 {
1489 while (!try_new_volume ())
1490 ;
1491 return;
1492 }
1493 else if (status == SAFE_READ_ERROR)
1494 {
1495 archive_read_error ();
1496 continue;
1497 }
1498 break;
1499 }
1500 short_read (status);
1501 }
1502
1503 static void
1504 gnu_flush_read (void)
1505 {
1506 flush_read_ptr = simple_flush_read; /* Avoid recursion */
1507 _gnu_flush_read ();
1508 flush_read_ptr = gnu_flush_read;
1509 }
1510
1511 static void
1512 _gnu_flush_write (size_t buffer_level)
1513 {
1514 ssize_t status;
1515 union block *header;
1516 char *copy_ptr;
1517 size_t copy_size;
1518 size_t bufsize;
1519
1520 status = _flush_write ();
1521 if (status != record_size && !multi_volume_option)
1522 archive_write_error (status);
1523 else
1524 {
1525 records_written++;
1526 bytes_written += status;
1527 }
1528
1529 if (status == record_size)
1530 {
1531 multi_volume_sync ();
1532 return;
1533 }
1534
1535 /* In multi-volume mode. */
1536 /* ENXIO is for the UNIX PC. */
1537 if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
1538 archive_write_error (status);
1539
1540 if (!new_volume (ACCESS_WRITE))
1541 return;
1542
1543 xheader_destroy (&extended_header);
1544
1545 increase_volume_number ();
1546 if (totals_option)
1547 prev_written += bytes_written;
1548 bytes_written = 0;
1549
1550 copy_ptr = record_start->buffer + status;
1551 copy_size = buffer_level - status;
1552 /* Switch to the next buffer */
1553 record_index = !record_index;
1554 init_buffer ();
1555
1556 if (volume_label_option)
1557 add_volume_label ();
1558
1559 if (real_s_name)
1560 add_multi_volume_header ();
1561
1562 write_extended (true, NULL, find_next_block ());
1563 if (real_s_name)
1564 add_chunk_header ();
1565 header = find_next_block ();
1566 bufsize = available_space_after (header);
1567 while (bufsize < copy_size)
1568 {
1569 memcpy (header->buffer, copy_ptr, bufsize);
1570 copy_ptr += bufsize;
1571 copy_size -= bufsize;
1572 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
1573 header = find_next_block ();
1574 bufsize = available_space_after (header);
1575 }
1576 memcpy (header->buffer, copy_ptr, copy_size);
1577 memset (header->buffer + copy_size, 0, bufsize - copy_size);
1578 set_next_block_after (header + (copy_size - 1) / BLOCKSIZE);
1579 find_next_block ();
1580 }
1581
1582 static void
1583 gnu_flush_write (size_t buffer_level)
1584 {
1585 flush_write_ptr = simple_flush_write; /* Avoid recursion */
1586 _gnu_flush_write (buffer_level);
1587 flush_write_ptr = gnu_flush_write;
1588 }
1589
1590 void
1591 flush_read ()
1592 {
1593 flush_read_ptr ();
1594 }
1595
1596 void
1597 flush_write ()
1598 {
1599 flush_write_ptr (record_size);
1600 }
1601
1602 void
1603 open_archive (enum access_mode wanted_access)
1604 {
1605 flush_read_ptr = gnu_flush_read;
1606 flush_write_ptr = gnu_flush_write;
1607
1608 _open_archive (wanted_access);
1609 switch (wanted_access)
1610 {
1611 case ACCESS_READ:
1612 if (volume_label_option)
1613 match_volume_label ();
1614 break;
1615
1616 case ACCESS_WRITE:
1617 records_written = 0;
1618 if (volume_label_option)
1619 write_volume_label ();
1620 break;
1621 }
1622 }
1623
This page took 0.103297 seconds and 5 git commands to generate.