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