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