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