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