]> Dogcows Code - chaz/tar/blob - src/buffer.c
(change_tape_menu): Break the loop after obtaining new archive name. Check for empty...
[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 int 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 /* Perform a write to flush the buffer. */
534 ssize_t
535 _flush_write (void)
536 {
537 ssize_t status;
538
539 if (checkpoint_option && !(++checkpoint % 10))
540 /* TRANSLATORS: This is a ``checkpoint of write operation'',
541 *not* ``Writing a checkpoint''.
542 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
543 *not* ``Escribiendo un punto de comprobaci@'on'' */
544 WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
545
546 if (tape_length_option && tape_length_option <= bytes_written)
547 {
548 errno = ENOSPC;
549 status = 0;
550 }
551 else if (dev_null_output)
552 status = record_size;
553 else
554 status = sys_write_archive_buffer ();
555
556 return status;
557 }
558
559 /* Handle write errors on the archive. Write errors are always fatal.
560 Hitting the end of a volume does not cause a write error unless the
561 write was the first record of the volume. */
562 void
563 archive_write_error (ssize_t status)
564 {
565 /* It might be useful to know how much was written before the error
566 occurred. */
567 if (totals_option)
568 {
569 int e = errno;
570 print_total_written ();
571 errno = e;
572 }
573
574 write_fatal_details (*archive_name_cursor, status, record_size);
575 }
576
577 /* Handle read errors on the archive. If the read should be retried,
578 return to the caller. */
579 void
580 archive_read_error (void)
581 {
582 read_error (*archive_name_cursor);
583
584 if (record_start_block == 0)
585 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
586
587 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
588 then give up on reading the archive. */
589
590 if (read_error_count++ > READ_ERROR_MAX)
591 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
592 return;
593 }
594
595 static void
596 short_read (size_t status)
597 {
598 size_t left; /* bytes left */
599 char *more; /* pointer to next byte to read */
600
601 more = record_start->buffer + status;
602 left = record_size - status;
603
604 while (left % BLOCKSIZE != 0
605 || (left && status && read_full_records))
606 {
607 if (status)
608 while ((status = rmtread (archive, more, left)) == SAFE_READ_ERROR)
609 archive_read_error ();
610
611 if (status == 0)
612 break;
613
614 if (! read_full_records)
615 {
616 unsigned long rest = record_size - left;
617
618 FATAL_ERROR ((0, 0,
619 ngettext ("Unaligned block (%lu byte) in archive",
620 "Unaligned block (%lu bytes) in archive",
621 rest),
622 rest));
623 }
624
625 /* User warned us about this. Fix up. */
626
627 left -= status;
628 more += status;
629 }
630
631 /* FIXME: for size=0, multi-volume support. On the first record, warn
632 about the problem. */
633
634 if (!read_full_records && verbose_option > 1
635 && record_start_block == 0 && status != 0)
636 {
637 unsigned long rsize = (record_size - left) / BLOCKSIZE;
638 WARN ((0, 0,
639 ngettext ("Record size = %lu block",
640 "Record size = %lu blocks",
641 rsize),
642 rsize));
643 }
644
645 record_end = record_start + (record_size - left) / BLOCKSIZE;
646 records_read++;
647 }
648
649 /* Perform a read to flush the buffer. */
650 size_t
651 _flush_read (void)
652 {
653 size_t status; /* result from system call */
654
655 if (checkpoint_option && !(++checkpoint % 10))
656 /* TRANSLATORS: This is a ``checkpoint of read operation'',
657 *not* ``Reading a checkpoint''.
658 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
659 *not* ``Leyendo un punto de comprobaci@'on'' */
660 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
661
662 /* Clear the count of errors. This only applies to a single call to
663 flush_read. */
664
665 read_error_count = 0; /* clear error count */
666
667 if (write_archive_to_stdout && record_start_block != 0)
668 {
669 archive = STDOUT_FILENO;
670 status = sys_write_archive_buffer ();
671 archive = STDIN_FILENO;
672 if (status != record_size)
673 archive_write_error (status);
674 }
675
676 status = rmtread (archive, record_start->buffer, record_size);
677 if (status == record_size)
678 records_read++;
679 return status;
680 }
681
682 /* Flush the current buffer to/from the archive. */
683 void
684 flush_archive (void)
685 {
686 size_t buffer_level = current_block->buffer - record_start->buffer;
687 record_start_block += record_end - record_start;
688 current_block = record_start;
689 record_end = record_start + blocking_factor;
690
691 if (access_mode == ACCESS_READ && time_to_start_writing)
692 {
693 access_mode = ACCESS_WRITE;
694 time_to_start_writing = false;
695 backspace_output ();
696 }
697
698 switch (access_mode)
699 {
700 case ACCESS_READ:
701 flush_read ();
702 break;
703
704 case ACCESS_WRITE:
705 flush_write_ptr (buffer_level);
706 break;
707
708 case ACCESS_UPDATE:
709 abort ();
710 }
711 }
712
713 /* Backspace the archive descriptor by one record worth. If it's a
714 tape, MTIOCTOP will work. If it's something else, try to seek on
715 it. If we can't seek, we lose! */
716 static void
717 backspace_output (void)
718 {
719 #ifdef MTIOCTOP
720 {
721 struct mtop operation;
722
723 operation.mt_op = MTBSR;
724 operation.mt_count = 1;
725 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
726 return;
727 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
728 return;
729 }
730 #endif
731
732 {
733 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
734
735 /* Seek back to the beginning of this record and start writing there. */
736
737 position -= record_size;
738 if (position < 0)
739 position = 0;
740 if (rmtlseek (archive, position, SEEK_SET) != position)
741 {
742 /* Lseek failed. Try a different method. */
743
744 WARN ((0, 0,
745 _("Cannot backspace archive file; it may be unreadable without -i")));
746
747 /* Replace the first part of the record with NULs. */
748
749 if (record_start->buffer != output_start)
750 memset (record_start->buffer, 0,
751 output_start - record_start->buffer);
752 }
753 }
754 }
755
756 off_t
757 seek_archive (off_t size)
758 {
759 off_t start = current_block_ordinal ();
760 off_t offset;
761 off_t nrec, nblk;
762 off_t skipped = (blocking_factor - (current_block - record_start));
763
764 size -= skipped * BLOCKSIZE;
765
766 if (size < record_size)
767 return 0;
768 /* FIXME: flush? */
769
770 /* Compute number of records to skip */
771 nrec = size / record_size;
772 offset = rmtlseek (archive, nrec * record_size, SEEK_CUR);
773 if (offset < 0)
774 return offset;
775
776 if (offset % record_size)
777 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
778
779 /* Convert to number of records */
780 offset /= BLOCKSIZE;
781 /* Compute number of skipped blocks */
782 nblk = offset - start;
783
784 /* Update buffering info */
785 records_read += nblk / blocking_factor;
786 record_start_block = offset - blocking_factor;
787 current_block = record_end;
788
789 return nblk;
790 }
791
792 /* Close the archive file. */
793 void
794 close_archive (void)
795 {
796 if (time_to_start_writing || access_mode == ACCESS_WRITE)
797 {
798 flush_archive ();
799 if (current_block > record_start)
800 flush_archive ();
801 }
802
803 sys_drain_input_pipe ();
804
805 compute_duration ();
806 if (verify_option)
807 verify_volume ();
808
809 if (rmtclose (archive) != 0)
810 close_warn (*archive_name_cursor);
811
812 sys_wait_for_child (child_pid);
813
814 tar_stat_destroy (&current_stat_info);
815 if (save_name)
816 free (save_name);
817 if (real_s_name)
818 free (real_s_name);
819 free (record_buffer[0]);
820 free (record_buffer[1]);
821 }
822
823 /* Called to initialize the global volume number. */
824 void
825 init_volume_number (void)
826 {
827 FILE *file = fopen (volno_file_option, "r");
828
829 if (file)
830 {
831 if (fscanf (file, "%d", &global_volno) != 1
832 || global_volno < 0)
833 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
834 quotearg_colon (volno_file_option)));
835 if (ferror (file))
836 read_error (volno_file_option);
837 if (fclose (file) != 0)
838 close_error (volno_file_option);
839 }
840 else if (errno != ENOENT)
841 open_error (volno_file_option);
842 }
843
844 /* Called to write out the closing global volume number. */
845 void
846 closeout_volume_number (void)
847 {
848 FILE *file = fopen (volno_file_option, "w");
849
850 if (file)
851 {
852 fprintf (file, "%d\n", global_volno);
853 if (ferror (file))
854 write_error (volno_file_option);
855 if (fclose (file) != 0)
856 close_error (volno_file_option);
857 }
858 else
859 open_error (volno_file_option);
860 }
861
862 \f
863 static void
864 increase_volume_number ()
865 {
866 global_volno++;
867 if (global_volno < 0)
868 FATAL_ERROR ((0, 0, _("Volume number overflow")));
869 volno++;
870 }
871
872 void
873 change_tape_menu (FILE *read_file)
874 {
875 char *input_buffer = NULL;
876 size_t size = 0;
877 bool stop;
878
879 while (!stop)
880 {
881 fputc ('\007', stderr);
882 fprintf (stderr,
883 _("Prepare volume #%d for %s and hit return: "),
884 global_volno + 1, quote (*archive_name_cursor));
885 fflush (stderr);
886
887 if (getline (&input_buffer, &size, read_file) <= 0)
888 {
889 WARN ((0, 0, _("EOF where user reply was expected")));
890
891 if (subcommand_option != EXTRACT_SUBCOMMAND
892 && subcommand_option != LIST_SUBCOMMAND
893 && subcommand_option != DIFF_SUBCOMMAND)
894 WARN ((0, 0, _("WARNING: Archive is incomplete")));
895
896 fatal_exit ();
897 }
898
899 if (input_buffer[0] == '\n'
900 || input_buffer[0] == 'y'
901 || input_buffer[0] == 'Y')
902 break;
903
904 switch (input_buffer[0])
905 {
906 case '?':
907 {
908 fprintf (stderr, _("\
909 n name Give a new file name for the next (and subsequent) volume(s)\n\
910 q Abort tar\n\
911 y or newline Continue operation\n"));
912 if (!restrict_option)
913 fprintf (stderr, _(" ! Spawn a subshell\n"));
914 fprintf (stderr, _(" ? Print this list\n"));
915 }
916 break;
917
918 case 'q':
919 /* Quit. */
920
921 WARN ((0, 0, _("No new volume; exiting.\n")));
922
923 if (subcommand_option != EXTRACT_SUBCOMMAND
924 && subcommand_option != LIST_SUBCOMMAND
925 && subcommand_option != DIFF_SUBCOMMAND)
926 WARN ((0, 0, _("WARNING: Archive is incomplete")));
927
928 fatal_exit ();
929
930 case 'n':
931 /* Get new file name. */
932
933 {
934 char *name;
935 char *cursor;
936
937 for (name = input_buffer + 1;
938 *name == ' ' || *name == '\t';
939 name++)
940 ;
941
942 for (cursor = name; *cursor && *cursor != '\n'; cursor++)
943 ;
944 *cursor = '\0';
945
946 if (name[0])
947 {
948 /* FIXME: the following allocation is never reclaimed. */
949 *archive_name_cursor = xstrdup (name);
950 stop = true;
951 }
952 else
953 fprintf (stderr, "%s",
954 _("File name not specified. Try again.\n"));
955 }
956 break;
957
958 case '!':
959 if (!restrict_option)
960 {
961 sys_spawn_shell ();
962 break;
963 }
964 /* FALL THROUGH */
965
966 default:
967 fprintf (stderr, _("Invalid input. Type ? for help.\n"));
968 }
969 }
970 free (input_buffer);
971 }
972
973 /* We've hit the end of the old volume. Close it and open the next one.
974 Return nonzero on success.
975 */
976 static bool
977 new_volume (enum access_mode mode)
978 {
979 static FILE *read_file;
980 static int looped;
981 int prompt;
982
983 if (!read_file && !info_script_option)
984 /* FIXME: if fopen is used, it will never be closed. */
985 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
986
987 if (now_verifying)
988 return false;
989 if (verify_option)
990 verify_volume ();
991
992 assign_string (&volume_label, NULL);
993 assign_string (&continued_file_name, NULL);
994 continued_file_size = continued_file_offset = 0;
995
996 if (rmtclose (archive) != 0)
997 close_warn (*archive_name_cursor);
998
999 archive_name_cursor++;
1000 if (archive_name_cursor == archive_name_array + archive_names)
1001 {
1002 archive_name_cursor = archive_name_array;
1003 looped = 1;
1004 }
1005 prompt = looped;
1006
1007 tryagain:
1008 if (prompt)
1009 {
1010 /* We have to prompt from now on. */
1011
1012 if (info_script_option)
1013 {
1014 if (volno_file_option)
1015 closeout_volume_number ();
1016 if (sys_exec_info_script (archive_name_cursor, global_volno+1))
1017 FATAL_ERROR ((0, 0, _("%s command failed"),
1018 quote (info_script_option)));
1019 }
1020 else
1021 change_tape_menu (read_file);
1022 }
1023
1024 if (strcmp (archive_name_cursor[0], "-") == 0)
1025 {
1026 read_full_records = true;
1027 archive = STDIN_FILENO;
1028 }
1029 else if (verify_option)
1030 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1031 rsh_command_option);
1032 else
1033 switch (mode)
1034 {
1035 case ACCESS_READ:
1036 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1037 rsh_command_option);
1038 break;
1039
1040 case ACCESS_WRITE:
1041 if (backup_option)
1042 maybe_backup_file (*archive_name_cursor, 1);
1043 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1044 rsh_command_option);
1045 break;
1046
1047 case ACCESS_UPDATE:
1048 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1049 rsh_command_option);
1050 break;
1051 }
1052
1053 if (archive < 0)
1054 {
1055 open_warn (*archive_name_cursor);
1056 if (!verify_option && mode == ACCESS_WRITE && backup_option)
1057 undo_last_backup ();
1058 prompt = 1;
1059 goto tryagain;
1060 }
1061
1062 SET_BINARY_MODE (archive);
1063
1064 return true;
1065 }
1066
1067 static bool
1068 read_header0 (struct tar_stat_info *info)
1069 {
1070 enum read_header rc;
1071
1072 tar_stat_init (info);
1073 rc = read_header_primitive (false, info);
1074 if (rc == HEADER_SUCCESS)
1075 {
1076 set_next_block_after (current_header);
1077 return true;
1078 }
1079 ERROR ((0, 0, _("This does not look like a tar archive")));
1080 return false;
1081 }
1082
1083 bool
1084 try_new_volume ()
1085 {
1086 size_t status;
1087 union block *header;
1088 struct tar_stat_info dummy;
1089
1090 switch (subcommand_option)
1091 {
1092 case APPEND_SUBCOMMAND:
1093 case CAT_SUBCOMMAND:
1094 case UPDATE_SUBCOMMAND:
1095 if (!new_volume (ACCESS_UPDATE))
1096 return true;
1097 break;
1098
1099 default:
1100 if (!new_volume (ACCESS_READ))
1101 return true;
1102 break;
1103 }
1104
1105 while ((status = rmtread (archive, record_start->buffer, record_size))
1106 == SAFE_READ_ERROR)
1107 archive_read_error ();
1108
1109 if (status != record_size)
1110 short_read (status);
1111
1112 header = find_next_block ();
1113 if (!header)
1114 return false;
1115
1116 switch (header->header.typeflag)
1117 {
1118 case XGLTYPE:
1119 {
1120 if (!read_header0 (&dummy))
1121 return false;
1122 xheader_decode (&dummy); /* decodes values from the global header */
1123 tar_stat_destroy (&dummy);
1124 if (!real_s_name)
1125 {
1126 /* We have read the extended header of the first member in
1127 this volume. Put it back, so next read_header works as
1128 expected. */
1129 current_block = record_start;
1130 }
1131 break;
1132 }
1133
1134 case GNUTYPE_VOLHDR:
1135 if (!read_header0 (&dummy))
1136 return false;
1137 tar_stat_destroy (&dummy);
1138 assign_string (&volume_label, current_header->header.name);
1139 set_next_block_after (header);
1140 header = find_next_block ();
1141 if (header->header.typeflag != GNUTYPE_MULTIVOL)
1142 break;
1143 /* FALL THROUGH */
1144
1145 case GNUTYPE_MULTIVOL:
1146 if (!read_header0 (&dummy))
1147 return false;
1148 tar_stat_destroy (&dummy);
1149 assign_string (&continued_file_name, current_header->header.name);
1150 continued_file_size =
1151 UINTMAX_FROM_HEADER (current_header->header.size);
1152 continued_file_offset =
1153 UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset);
1154 break;
1155
1156 default:
1157 break;
1158 }
1159
1160 if (real_s_name)
1161 {
1162 uintmax_t s;
1163 if (!continued_file_name
1164 || strcmp (continued_file_name, real_s_name))
1165 {
1166 if ((archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
1167 && strlen (real_s_name) >= NAME_FIELD_SIZE
1168 && strncmp (continued_file_name, real_s_name,
1169 NAME_FIELD_SIZE) == 0)
1170 WARN ((0, 0,
1171 _("%s is possibly continued on this volume: header contains truncated name"),
1172 quote (real_s_name)));
1173 else
1174 {
1175 WARN ((0, 0, _("%s is not continued on this volume"),
1176 quote (real_s_name)));
1177 return false;
1178 }
1179 }
1180
1181 s = continued_file_size + continued_file_offset;
1182
1183 if (real_s_totsize != s || s < continued_file_offset)
1184 {
1185 char totsizebuf[UINTMAX_STRSIZE_BOUND];
1186 char s1buf[UINTMAX_STRSIZE_BOUND];
1187 char s2buf[UINTMAX_STRSIZE_BOUND];
1188
1189 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1190 quote (continued_file_name),
1191 STRINGIFY_BIGINT (save_totsize, totsizebuf),
1192 STRINGIFY_BIGINT (continued_file_size, s1buf),
1193 STRINGIFY_BIGINT (continued_file_offset, s2buf)));
1194 return false;
1195 }
1196
1197 if (real_s_totsize - real_s_sizeleft != continued_file_offset)
1198 {
1199 WARN ((0, 0, _("This volume is out of sequence")));
1200 return false;
1201 }
1202 }
1203
1204 increase_volume_number ();
1205 return true;
1206 }
1207
1208 \f
1209 /* Check the LABEL block against the volume label, seen as a globbing
1210 pattern. Return true if the pattern matches. In case of failure,
1211 retry matching a volume sequence number before giving up in
1212 multi-volume mode. */
1213 static bool
1214 check_label_pattern (union block *label)
1215 {
1216 char *string;
1217 bool result;
1218
1219 if (! memchr (label->header.name, '\0', sizeof label->header.name))
1220 return false;
1221
1222 if (fnmatch (volume_label_option, label->header.name, 0) == 0)
1223 return true;
1224
1225 if (!multi_volume_option)
1226 return false;
1227
1228 string = xmalloc (strlen (volume_label_option)
1229 + sizeof VOLUME_LABEL_APPEND + 1);
1230 strcpy (string, volume_label_option);
1231 strcat (string, VOLUME_LABEL_APPEND);
1232 result = fnmatch (string, label->header.name, 0) == 0;
1233 free (string);
1234 return result;
1235 }
1236
1237 /* Check if the next block contains a volume label and if this matches
1238 the one given in the command line */
1239 static void
1240 match_volume_label (void)
1241 {
1242 union block *label = find_next_block ();
1243
1244 if (!label)
1245 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
1246 quote (volume_label_option)));
1247 if (!check_label_pattern (label))
1248 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
1249 quote_n (0, label->header.name),
1250 quote_n (1, volume_label_option)));
1251 }
1252
1253 /* Mark the archive with volume label STR. */
1254 static void
1255 _write_volume_label (const char *str)
1256 {
1257 if (archive_format == POSIX_FORMAT)
1258 xheader_store ("GNU.volume.label", NULL, str);
1259 else
1260 {
1261 union block *label = find_next_block ();
1262
1263 memset (label, 0, BLOCKSIZE);
1264
1265 strcpy (label->header.name, volume_label_option);
1266 assign_string (&current_stat_info.file_name,
1267 label->header.name);
1268 current_stat_info.had_trailing_slash =
1269 strip_trailing_slashes (current_stat_info.file_name);
1270
1271 label->header.typeflag = GNUTYPE_VOLHDR;
1272 TIME_TO_CHARS (start_time.tv_sec, label->header.mtime);
1273 finish_header (&current_stat_info, label, -1);
1274 set_next_block_after (label);
1275 }
1276 }
1277
1278 #define VOL_SUFFIX "Volume"
1279
1280 /* Add a volume label to a part of multi-volume archive */
1281 static void
1282 add_volume_label (void)
1283 {
1284 char buf[UINTMAX_STRSIZE_BOUND];
1285 char *p = STRINGIFY_BIGINT (volno, buf);
1286 char *s = xmalloc (strlen (volume_label_option) + sizeof VOL_SUFFIX
1287 + strlen (p) + 2);
1288 sprintf (s, "%s %s %s", volume_label_option, VOL_SUFFIX, p);
1289 _write_volume_label (s);
1290 free (s);
1291 }
1292
1293 static void
1294 add_chunk_header ()
1295 {
1296 if (archive_format == POSIX_FORMAT)
1297 {
1298 off_t block_ordinal;
1299 union block *blk;
1300 struct tar_stat_info st;
1301 static size_t real_s_part_no; /* FIXME */
1302
1303 real_s_part_no++;
1304 memset (&st, 0, sizeof st);
1305 st.orig_file_name = st.file_name = real_s_name;
1306 st.stat.st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
1307 st.stat.st_uid = getuid ();
1308 st.stat.st_gid = getgid ();
1309 st.orig_file_name = xheader_format_name (&st,
1310 "%d/GNUFileParts.%p/%f.%n",
1311 real_s_part_no);
1312 st.file_name = st.orig_file_name;
1313 st.archive_file_size = st.stat.st_size = real_s_sizeleft;
1314
1315 block_ordinal = current_block_ordinal ();
1316 blk = start_header (&st);
1317 if (!blk)
1318 abort (); /* FIXME */
1319 finish_header (&st, blk, block_ordinal);
1320 free (st.orig_file_name);
1321 }
1322 }
1323
1324
1325 /* Add a volume label to the current archive */
1326 static void
1327 write_volume_label (void)
1328 {
1329 if (multi_volume_option)
1330 add_volume_label ();
1331 else
1332 _write_volume_label (volume_label_option);
1333 }
1334
1335 /* Write GNU multi-volume header */
1336 static void
1337 gnu_add_multi_volume_header (void)
1338 {
1339 int tmp;
1340 union block *block = find_next_block ();
1341
1342 if (strlen (real_s_name) > NAME_FIELD_SIZE)
1343 WARN ((0, 0,
1344 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
1345 quotearg_colon (real_s_name)));
1346
1347 memset (block, 0, BLOCKSIZE);
1348
1349 /* FIXME: Michael P Urban writes: [a long name file] is being written
1350 when a new volume rolls around [...] Looks like the wrong value is
1351 being preserved in real_s_name, though. */
1352
1353 strncpy (block->header.name, real_s_name, NAME_FIELD_SIZE);
1354 block->header.typeflag = GNUTYPE_MULTIVOL;
1355
1356 OFF_TO_CHARS (real_s_sizeleft, block->header.size);
1357 OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
1358 block->oldgnu_header.offset);
1359
1360 tmp = verbose_option;
1361 verbose_option = 0;
1362 finish_header (&current_stat_info, block, -1);
1363 verbose_option = tmp;
1364 set_next_block_after (block);
1365 }
1366
1367 /* Add a multi volume header to the current archive. The exact header format
1368 depends on the archive format. */
1369 static void
1370 add_multi_volume_header (void)
1371 {
1372 if (archive_format == POSIX_FORMAT)
1373 {
1374 off_t d = real_s_totsize - real_s_sizeleft;
1375 xheader_store ("GNU.volume.filename", NULL, real_s_name);
1376 xheader_store ("GNU.volume.size", NULL, &real_s_sizeleft);
1377 xheader_store ("GNU.volume.offset", NULL, &d);
1378 }
1379 else
1380 gnu_add_multi_volume_header ();
1381 }
1382
1383 /* Synchronize multi-volume globals */
1384 static void
1385 multi_volume_sync ()
1386 {
1387 if (multi_volume_option)
1388 {
1389 if (save_name)
1390 {
1391 assign_string (&real_s_name,
1392 safer_name_suffix (save_name, false,
1393 absolute_names_option));
1394 real_s_totsize = save_totsize;
1395 real_s_sizeleft = save_sizeleft;
1396 }
1397 else
1398 {
1399 assign_string (&real_s_name, 0);
1400 real_s_totsize = 0;
1401 real_s_sizeleft = 0;
1402 }
1403 }
1404 }
1405
1406 \f
1407 /* Low-level flush functions */
1408
1409 /* Simple flush read (no multi-volume or label extensions) */
1410 static void
1411 simple_flush_read (void)
1412 {
1413 size_t status; /* result from system call */
1414
1415 if (checkpoint_option && !(++checkpoint % 10))
1416 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1417 *not* ``Reading a checkpoint''.
1418 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1419 *not* ``Leyendo un punto de comprobaci@'on'' */
1420 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
1421
1422 /* Clear the count of errors. This only applies to a single call to
1423 flush_read. */
1424
1425 read_error_count = 0; /* clear error count */
1426
1427 if (write_archive_to_stdout && record_start_block != 0)
1428 {
1429 archive = STDOUT_FILENO;
1430 status = sys_write_archive_buffer ();
1431 archive = STDIN_FILENO;
1432 if (status != record_size)
1433 archive_write_error (status);
1434 }
1435
1436 for (;;)
1437 {
1438 status = rmtread (archive, record_start->buffer, record_size);
1439 if (status == record_size)
1440 {
1441 records_read++;
1442 return;
1443 }
1444 if (status == SAFE_READ_ERROR)
1445 {
1446 archive_read_error ();
1447 continue; /* try again */
1448 }
1449 break;
1450 }
1451 short_read (status);
1452 }
1453
1454 /* Simple flush write (no multi-volume or label extensions) */
1455 static void
1456 simple_flush_write (size_t level __attribute__((unused)))
1457 {
1458 ssize_t status;
1459
1460 status = _flush_write ();
1461 if (status != record_size)
1462 archive_write_error (status);
1463 else
1464 {
1465 records_written++;
1466 bytes_written += status;
1467 }
1468 }
1469
1470 \f
1471 /* GNU flush functions. These support multi-volume and archive labels in
1472 GNU and PAX archive formats. */
1473
1474 static void
1475 _gnu_flush_read (void)
1476 {
1477 size_t status; /* result from system call */
1478
1479 if (checkpoint_option && !(++checkpoint % 10))
1480 /* TRANSLATORS: This is a ``checkpoint of read operation'',
1481 *not* ``Reading a checkpoint''.
1482 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
1483 *not* ``Leyendo un punto de comprobaci@'on'' */
1484 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
1485
1486 /* Clear the count of errors. This only applies to a single call to
1487 flush_read. */
1488
1489 read_error_count = 0; /* clear error count */
1490
1491 if (write_archive_to_stdout && record_start_block != 0)
1492 {
1493 archive = STDOUT_FILENO;
1494 status = sys_write_archive_buffer ();
1495 archive = STDIN_FILENO;
1496 if (status != record_size)
1497 archive_write_error (status);
1498 }
1499
1500 multi_volume_sync ();
1501
1502 for (;;)
1503 {
1504 status = rmtread (archive, record_start->buffer, record_size);
1505 if (status == record_size)
1506 {
1507 records_read++;
1508 return;
1509 }
1510
1511 /* The condition below used to include
1512 || (status > 0 && !read_full_records)
1513 This is incorrect since even if new_volume() succeeds, the
1514 subsequent call to rmtread will overwrite the chunk of data
1515 already read in the buffer, so the processing will fail */
1516 if ((status == 0
1517 || (status == SAFE_READ_ERROR && errno == ENOSPC))
1518 && multi_volume_option)
1519 {
1520 while (!try_new_volume ())
1521 ;
1522 return;
1523 }
1524 else if (status == SAFE_READ_ERROR)
1525 {
1526 archive_read_error ();
1527 continue;
1528 }
1529 break;
1530 }
1531 short_read (status);
1532 }
1533
1534 static void
1535 gnu_flush_read (void)
1536 {
1537 flush_read_ptr = simple_flush_read; /* Avoid recursion */
1538 _gnu_flush_read ();
1539 flush_read_ptr = gnu_flush_read;
1540 }
1541
1542 static void
1543 _gnu_flush_write (size_t buffer_level)
1544 {
1545 ssize_t status;
1546 union block *header;
1547 char *copy_ptr;
1548 size_t copy_size;
1549 size_t bufsize;
1550
1551 status = _flush_write ();
1552 if (status != record_size && !multi_volume_option)
1553 archive_write_error (status);
1554 else
1555 {
1556 records_written++;
1557 bytes_written += status;
1558 }
1559
1560 if (status == record_size)
1561 {
1562 multi_volume_sync ();
1563 return;
1564 }
1565
1566 /* In multi-volume mode. */
1567 /* ENXIO is for the UNIX PC. */
1568 if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
1569 archive_write_error (status);
1570
1571 if (!new_volume (ACCESS_WRITE))
1572 return;
1573
1574 xheader_destroy (&extended_header);
1575
1576 increase_volume_number ();
1577 if (totals_option)
1578 prev_written += bytes_written;
1579 bytes_written = 0;
1580
1581 copy_ptr = record_start->buffer + status;
1582 copy_size = buffer_level - status;
1583 /* Switch to the next buffer */
1584 record_index = !record_index;
1585 init_buffer ();
1586
1587 if (volume_label_option)
1588 add_volume_label ();
1589
1590 if (real_s_name)
1591 add_multi_volume_header ();
1592
1593 write_extended (true, NULL, find_next_block ());
1594 if (real_s_name)
1595 add_chunk_header ();
1596 header = find_next_block ();
1597 bufsize = available_space_after (header);
1598 while (bufsize < copy_size)
1599 {
1600 memcpy (header->buffer, copy_ptr, bufsize);
1601 copy_ptr += bufsize;
1602 copy_size -= bufsize;
1603 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
1604 header = find_next_block ();
1605 bufsize = available_space_after (header);
1606 }
1607 memcpy (header->buffer, copy_ptr, copy_size);
1608 memset (header->buffer + copy_size, 0, bufsize - copy_size);
1609 set_next_block_after (header + (copy_size - 1) / BLOCKSIZE);
1610 find_next_block ();
1611 }
1612
1613 static void
1614 gnu_flush_write (size_t buffer_level)
1615 {
1616 flush_write_ptr = simple_flush_write; /* Avoid recursion */
1617 _gnu_flush_write (buffer_level);
1618 flush_write_ptr = gnu_flush_write;
1619 }
1620
1621 void
1622 flush_read ()
1623 {
1624 flush_read_ptr ();
1625 }
1626
1627 void
1628 flush_write ()
1629 {
1630 flush_write_ptr (record_size);
1631 }
1632
1633 void
1634 open_archive (enum access_mode wanted_access)
1635 {
1636 flush_read_ptr = gnu_flush_read;
1637 flush_write_ptr = gnu_flush_write;
1638
1639 _open_archive (wanted_access);
1640 switch (wanted_access)
1641 {
1642 case ACCESS_READ:
1643 if (volume_label_option)
1644 match_volume_label ();
1645 break;
1646
1647 case ACCESS_WRITE:
1648 records_written = 0;
1649 if (volume_label_option)
1650 write_volume_label ();
1651 break;
1652
1653 default:
1654 break;
1655 }
1656 }
This page took 0.100929 seconds and 5 git commands to generate.