]> Dogcows Code - chaz/tar/blob - src/buffer.c
Update
[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 Free Software Foundation, Inc.
5
6 Written by John Gilmore, on 1985-08-25.
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21
22 #include <system.h>
23
24 #include <signal.h>
25
26 #include <fnmatch.h>
27 #include <human.h>
28 #include <quotearg.h>
29
30 #include "common.h"
31 #include <rmt.h>
32
33 /* Number of retries before giving up on read. */
34 #define READ_ERROR_MAX 10
35
36 /* Globbing pattern to append to volume label if initial match failed. */
37 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
38 \f
39 /* Variables. */
40
41 static tarlong prev_written; /* bytes written on previous volumes */
42 static tarlong bytes_written; /* bytes written on this volume */
43 static void *record_buffer; /* allocated memory */
44
45 /* FIXME: The following variables should ideally be static to this
46 module. However, this cannot be done yet. The cleanup continues! */
47
48 union block *record_start; /* start of record of archive */
49 union block *record_end; /* last+1 block of archive record */
50 union block *current_block; /* current block of archive */
51 enum access_mode access_mode; /* how do we handle the archive */
52 off_t records_read; /* number of records read from this archive */
53 off_t records_written; /* likewise, for records written */
54
55 static off_t record_start_block; /* block ordinal at record_start */
56
57 /* Where we write list messages (not errors, not interactions) to. */
58 FILE *stdlis;
59
60 static void backspace_output (void);
61 static bool new_volume (enum access_mode);
62
63 /* PID of child program, if compress_option or remote archive access. */
64 static pid_t child_pid;
65
66 /* Error recovery stuff */
67 static int read_error_count;
68
69 /* Have we hit EOF yet? */
70 static bool hit_eof;
71
72 /* Checkpointing counter */
73 static int checkpoint;
74
75 static bool read_full_records = false;
76
77 /* We're reading, but we just read the last block and it's time to update.
78 Declared in update.c
79
80 As least EXTERN like this one as possible. (?? --gray)
81 FIXME: Either eliminate it or move it to common.h.
82 */
83 extern bool time_to_start_writing;
84
85 static int volno = 1; /* which volume of a multi-volume tape we're
86 on */
87 static int global_volno = 1; /* volume number to print in external
88 messages */
89
90 /* The pointer save_name, which is set in function dump_file() of module
91 create.c, points to the original long filename instead of the new,
92 shorter mangled name that is set in start_header() of module create.c.
93 The pointer save_name is only used in multi-volume mode when the file
94 being processed is non-sparse; if a file is split between volumes, the
95 save_name is used in generating the LF_MULTIVOL record on the second
96 volume. (From Pierce Cantrell, 1991-08-13.) */
97
98 char *save_name; /* name of the file we are currently writing */
99 off_t save_totsize; /* total size of file we are writing, only
100 valid if save_name is nonzero */
101 off_t save_sizeleft; /* where we are in the file we are writing,
102 only valid if save_name is nonzero */
103
104 bool write_archive_to_stdout;
105
106 /* Used by flush_read and flush_write to store the real info about saved
107 names. */
108 static char *real_s_name;
109 static off_t real_s_totsize;
110 static off_t real_s_sizeleft;
111 \f
112 /* Functions. */
113
114 void
115 clear_read_error_count (void)
116 {
117 read_error_count = 0;
118 }
119
120 \f
121 /* Time-related functions */
122
123 double duration;
124
125 void
126 set_start_time ()
127 {
128 gettime (&start_time);
129 }
130
131 void
132 compute_duration ()
133 {
134 struct timespec now;
135 gettime (&now);
136 duration += ((now.tv_sec - start_time.tv_sec)
137 + (now.tv_nsec - start_time.tv_nsec) / 1e9);
138 set_start_time ();
139 }
140
141 \f
142 /* Compression detection */
143
144 enum compress_type {
145 ct_none,
146 ct_compress,
147 ct_gzip,
148 ct_bzip2
149 };
150
151 struct zip_magic
152 {
153 enum compress_type type;
154 size_t length;
155 char *magic;
156 char *program;
157 char *option;
158 };
159
160 static struct zip_magic const magic[] = {
161 { ct_none, },
162 { ct_compress, 2, "\037\235", "compress", "-Z" },
163 { ct_gzip, 2, "\037\213", "gzip", "-z" },
164 { ct_bzip2, 3, "BZh", "bzip2", "-j" },
165 };
166
167 #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
168
169 #define compress_option(t) magic[t].option
170 #define compress_program(t) magic[t].program
171
172 /* Check if the file ARCHIVE is a compressed archive. */
173 enum compress_type
174 check_compressed_archive ()
175 {
176 struct zip_magic const *p;
177 bool sfr;
178
179 /* Prepare global data needed for find_next_block: */
180 record_end = record_start; /* set up for 1st record = # 0 */
181 sfr = read_full_records;
182 read_full_records = true; /* Suppress fatal error on reading a partial
183 record */
184 find_next_block ();
185
186 /* Restore global values */
187 read_full_records = sfr;
188
189 if (tar_checksum (record_start, true) == HEADER_SUCCESS)
190 /* Probably a valid header */
191 return ct_none;
192
193 for (p = magic + 1; p < magic + NMAGIC; p++)
194 if (memcmp (record_start->buffer, p->magic, p->length) == 0)
195 return p->type;
196
197 return ct_none;
198 }
199
200 /* Open an archive named archive_name_array[0]. Detect if it is
201 a compressed archive of known type and use corresponding decompression
202 program if so */
203 int
204 open_compressed_archive ()
205 {
206 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
207 MODE_RW, rsh_command_option);
208 if (archive == -1)
209 return archive;
210
211 if (!multi_volume_option)
212 {
213 enum compress_type type = check_compressed_archive ();
214
215 if (type == ct_none)
216 return archive;
217
218 /* FD is not needed any more */
219 rmtclose (archive);
220
221 hit_eof = false; /* It might have been set by find_next_block in
222 check_compressed_archive */
223
224 /* Open compressed archive */
225 use_compress_program_option = compress_program (type);
226 child_pid = sys_child_open_for_uncompress ();
227 read_full_records = true;
228 }
229
230 records_read = 0;
231 record_end = record_start; /* set up for 1st record = # 0 */
232
233 return archive;
234 }
235 \f
236
237 void
238 print_total_written (void)
239 {
240 tarlong written = prev_written + bytes_written;
241 char bytes[sizeof (tarlong) * CHAR_BIT];
242 char abbr[LONGEST_HUMAN_READABLE + 1];
243 char rate[LONGEST_HUMAN_READABLE + 1];
244
245 int human_opts = human_autoscale | human_base_1024 | human_SI | human_B;
246
247 sprintf (bytes, TARLONG_FORMAT, written);
248
249 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
250 fprintf (stderr, _("Total bytes written: %s (%s, %s/s)\n"), bytes,
251 human_readable (written, abbr, human_opts, 1, 1),
252 (0 < duration && written / duration < (uintmax_t) -1
253 ? human_readable (written / duration, rate, human_opts, 1, 1)
254 : "?"));
255 }
256
257 /* Compute and return the block ordinal at current_block. */
258 off_t
259 current_block_ordinal (void)
260 {
261 return record_start_block + (current_block - record_start);
262 }
263
264 /* If the EOF flag is set, reset it, as well as current_block, etc. */
265 void
266 reset_eof (void)
267 {
268 if (hit_eof)
269 {
270 hit_eof = false;
271 current_block = record_start;
272 record_end = record_start + blocking_factor;
273 access_mode = ACCESS_WRITE;
274 }
275 }
276
277 /* Return the location of the next available input or output block.
278 Return zero for EOF. Once we have returned zero, we just keep returning
279 it, to avoid accidentally going on to the next file on the tape. */
280 union block *
281 find_next_block (void)
282 {
283 if (current_block == record_end)
284 {
285 if (hit_eof)
286 return 0;
287 flush_archive ();
288 if (current_block == record_end)
289 {
290 hit_eof = true;
291 return 0;
292 }
293 }
294 return current_block;
295 }
296
297 /* Indicate that we have used all blocks up thru BLOCK. */
298 void
299 set_next_block_after (union block *block)
300 {
301 while (block >= current_block)
302 current_block++;
303
304 /* Do *not* flush the archive here. If we do, the same argument to
305 set_next_block_after could mean the next block (if the input record
306 is exactly one block long), which is not what is intended. */
307
308 if (current_block > record_end)
309 abort ();
310 }
311
312 /* Return the number of bytes comprising the space between POINTER
313 through the end of the current buffer of blocks. This space is
314 available for filling with data, or taking data from. POINTER is
315 usually (but not always) the result of previous find_next_block call. */
316 size_t
317 available_space_after (union block *pointer)
318 {
319 return record_end->buffer - pointer->buffer;
320 }
321
322 /* Close file having descriptor FD, and abort if close unsuccessful. */
323 void
324 xclose (int fd)
325 {
326 if (close (fd) != 0)
327 close_error (_("(pipe)"));
328 }
329
330 /* Check the LABEL block against the volume label, seen as a globbing
331 pattern. Return true if the pattern matches. In case of failure,
332 retry matching a volume sequence number before giving up in
333 multi-volume mode. */
334 static bool
335 check_label_pattern (union block *label)
336 {
337 char *string;
338 bool result;
339
340 if (! memchr (label->header.name, '\0', sizeof label->header.name))
341 return false;
342
343 if (fnmatch (volume_label_option, label->header.name, 0) == 0)
344 return true;
345
346 if (!multi_volume_option)
347 return false;
348
349 string = xmalloc (strlen (volume_label_option)
350 + sizeof VOLUME_LABEL_APPEND + 1);
351 strcpy (string, volume_label_option);
352 strcat (string, VOLUME_LABEL_APPEND);
353 result = fnmatch (string, label->header.name, 0) == 0;
354 free (string);
355 return result;
356 }
357
358 /* Open an archive file. The argument specifies whether we are
359 reading or writing, or both. */
360 void
361 open_archive (enum access_mode wanted_access)
362 {
363 int backed_up_flag = 0;
364
365 if (index_file_name)
366 {
367 stdlis = fopen (index_file_name, "w");
368 if (! stdlis)
369 open_error (index_file_name);
370 }
371 else
372 stdlis = to_stdout_option ? stderr : stdout;
373
374 if (record_size == 0)
375 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
376
377 if (archive_names == 0)
378 FATAL_ERROR ((0, 0, _("No archive name given")));
379
380 tar_stat_destroy (&current_stat_info);
381 save_name = 0;
382 real_s_name = 0;
383
384 record_start =
385 page_aligned_alloc (&record_buffer,
386 (record_size
387 + (multi_volume_option ? 2 * BLOCKSIZE : 0)));
388 if (multi_volume_option)
389 record_start += 2;
390
391 current_block = record_start;
392 record_end = record_start + blocking_factor;
393 /* When updating the archive, we start with reading. */
394 access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
395
396 read_full_records = read_full_records_option;
397
398 records_read = 0;
399
400 if (use_compress_program_option)
401 {
402 switch (wanted_access)
403 {
404 case ACCESS_READ:
405 child_pid = sys_child_open_for_uncompress ();
406 read_full_records = true;
407 record_end = record_start; /* set up for 1st record = # 0 */
408 break;
409
410 case ACCESS_WRITE:
411 child_pid = sys_child_open_for_compress ();
412 break;
413
414 case ACCESS_UPDATE:
415 abort (); /* Should not happen */
416 break;
417 }
418
419 if (wanted_access == ACCESS_WRITE
420 && strcmp (archive_name_array[0], "-") == 0)
421 stdlis = stderr;
422 }
423 else if (strcmp (archive_name_array[0], "-") == 0)
424 {
425 read_full_records = true; /* could be a pipe, be safe */
426 if (verify_option)
427 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
428
429 switch (wanted_access)
430 {
431 case ACCESS_READ:
432 {
433 enum compress_type type;
434
435 archive = STDIN_FILENO;
436
437 type = check_compressed_archive (archive);
438 if (type != ct_none)
439 FATAL_ERROR ((0, 0,
440 _("Archive is compressed. Use %s option"),
441 compress_option (type)));
442 }
443 break;
444
445 case ACCESS_WRITE:
446 archive = STDOUT_FILENO;
447 stdlis = stderr;
448 break;
449
450 case ACCESS_UPDATE:
451 archive = STDIN_FILENO;
452 stdlis = stderr;
453 write_archive_to_stdout = true;
454 break;
455 }
456 }
457 else if (verify_option)
458 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
459 MODE_RW, rsh_command_option);
460 else
461 switch (wanted_access)
462 {
463 case ACCESS_READ:
464 archive = open_compressed_archive ();
465 break;
466
467 case ACCESS_WRITE:
468 if (backup_option)
469 {
470 maybe_backup_file (archive_name_array[0], 1);
471 backed_up_flag = 1;
472 }
473 archive = rmtcreat (archive_name_array[0], MODE_RW,
474 rsh_command_option);
475 break;
476
477 case ACCESS_UPDATE:
478 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
479 MODE_RW, rsh_command_option);
480 break;
481 }
482
483 if (archive < 0
484 || (! _isrmt (archive) && !sys_get_archive_stat ()))
485 {
486 int saved_errno = errno;
487
488 if (backed_up_flag)
489 undo_last_backup ();
490 errno = saved_errno;
491 open_fatal (archive_name_array[0]);
492 }
493
494 sys_detect_dev_null_output ();
495 sys_save_archive_dev_ino ();
496 SET_BINARY_MODE (archive);
497
498 switch (wanted_access)
499 {
500 case ACCESS_UPDATE:
501 records_written = 0;
502 record_end = record_start; /* set up for 1st record = # 0 */
503
504 case ACCESS_READ:
505 find_next_block (); /* read it in, check for EOF */
506
507 if (volume_label_option)
508 {
509 union block *label = find_next_block ();
510
511 if (!label)
512 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
513 quote (volume_label_option)));
514 if (!check_label_pattern (label))
515 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
516 quote_n (0, label->header.name),
517 quote_n (1, volume_label_option)));
518 }
519 break;
520
521 case ACCESS_WRITE:
522 records_written = 0;
523 if (volume_label_option)
524 {
525 memset (record_start, 0, BLOCKSIZE);
526 if (multi_volume_option)
527 sprintf (record_start->header.name, "%s Volume 1",
528 volume_label_option);
529 else
530 strcpy (record_start->header.name, volume_label_option);
531
532 assign_string (&current_stat_info.file_name,
533 record_start->header.name);
534 current_stat_info.had_trailing_slash =
535 strip_trailing_slashes (current_stat_info.file_name);
536
537 record_start->header.typeflag = GNUTYPE_VOLHDR;
538 TIME_TO_CHARS (start_time.tv_sec, record_start->header.mtime);
539 finish_header (&current_stat_info, record_start, -1);
540 }
541 break;
542 }
543 }
544
545 /* Perform a write to flush the buffer. */
546 void
547 flush_write (void)
548 {
549 int copy_back;
550 ssize_t status;
551
552 if (checkpoint_option && !(++checkpoint % 10))
553 /* TRANSLATORS: This is a ``checkpoint of write operation'',
554 *not* ``Writing a checkpoint''.
555 E.g. in Spanish ``Punto de comprobaci@'on de escritura'',
556 *not* ``Escribiendo un punto de comprobaci@'on'' */
557 WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
558
559 if (tape_length_option && tape_length_option <= bytes_written)
560 {
561 errno = ENOSPC;
562 status = 0;
563 }
564 else if (dev_null_output)
565 status = record_size;
566 else
567 status = sys_write_archive_buffer ();
568 if (status != record_size && !multi_volume_option)
569 archive_write_error (status);
570
571 if (status > 0)
572 {
573 records_written++;
574 bytes_written += status;
575 }
576
577 if (status == record_size)
578 {
579 if (multi_volume_option)
580 {
581 if (save_name)
582 {
583 assign_string (&real_s_name,
584 safer_name_suffix (save_name, false,
585 absolute_names_option));
586 real_s_totsize = save_totsize;
587 real_s_sizeleft = save_sizeleft;
588 }
589 else
590 {
591 assign_string (&real_s_name, 0);
592 real_s_totsize = 0;
593 real_s_sizeleft = 0;
594 }
595 }
596 return;
597 }
598
599 /* We're multivol. Panic if we didn't get the right kind of response. */
600
601 /* ENXIO is for the UNIX PC. */
602 if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
603 archive_write_error (status);
604
605 /* If error indicates a short write, we just move to the next tape. */
606
607 if (!new_volume (ACCESS_WRITE))
608 return;
609
610 if (totals_option)
611 prev_written += bytes_written;
612 bytes_written = 0;
613
614 if (volume_label_option && real_s_name)
615 {
616 copy_back = 2;
617 record_start -= 2;
618 }
619 else if (volume_label_option || real_s_name)
620 {
621 copy_back = 1;
622 record_start--;
623 }
624 else
625 copy_back = 0;
626
627 if (volume_label_option)
628 {
629 memset (record_start, 0, BLOCKSIZE);
630 sprintf (record_start->header.name, "%s Volume %d",
631 volume_label_option, volno);
632 TIME_TO_CHARS (start_time.tv_sec, record_start->header.mtime);
633 record_start->header.typeflag = GNUTYPE_VOLHDR;
634 finish_header (&current_stat_info, record_start, -1);
635 }
636
637 if (real_s_name)
638 {
639 int tmp;
640
641 if (volume_label_option)
642 record_start++;
643
644 if (strlen (real_s_name) > NAME_FIELD_SIZE)
645 WARN ((0, 0,
646 _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
647 quotearg_colon (real_s_name)));
648
649 memset (record_start, 0, BLOCKSIZE);
650
651 /* FIXME: Michael P Urban writes: [a long name file] is being written
652 when a new volume rolls around [...] Looks like the wrong value is
653 being preserved in real_s_name, though. */
654
655 strncpy (record_start->header.name, real_s_name, NAME_FIELD_SIZE);
656 record_start->header.typeflag = GNUTYPE_MULTIVOL;
657
658 OFF_TO_CHARS (real_s_sizeleft, record_start->header.size);
659 OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
660 record_start->oldgnu_header.offset);
661
662 tmp = verbose_option;
663 verbose_option = 0;
664 finish_header (&current_stat_info, record_start, -1);
665 verbose_option = tmp;
666
667 if (volume_label_option)
668 record_start--;
669 }
670
671 status = sys_write_archive_buffer ();
672 if (status != record_size)
673 archive_write_error (status);
674
675 bytes_written += status;
676
677 if (copy_back)
678 {
679 record_start += copy_back;
680 memcpy (current_block,
681 record_start + blocking_factor - copy_back,
682 copy_back * BLOCKSIZE);
683 current_block += copy_back;
684
685 if (real_s_sizeleft >= copy_back * BLOCKSIZE)
686 real_s_sizeleft -= copy_back * BLOCKSIZE;
687 else if ((real_s_sizeleft + BLOCKSIZE - 1) / BLOCKSIZE <= copy_back)
688 assign_string (&real_s_name, 0);
689 else
690 {
691 assign_string (&real_s_name,
692 safer_name_suffix (save_name, false,
693 absolute_names_option));
694 real_s_sizeleft = save_sizeleft;
695 real_s_totsize = save_totsize;
696 }
697 copy_back = 0;
698 }
699 }
700
701 /* Handle write errors on the archive. Write errors are always fatal.
702 Hitting the end of a volume does not cause a write error unless the
703 write was the first record of the volume. */
704 void
705 archive_write_error (ssize_t status)
706 {
707 /* It might be useful to know how much was written before the error
708 occurred. */
709 if (totals_option)
710 {
711 int e = errno;
712 print_total_written ();
713 errno = e;
714 }
715
716 write_fatal_details (*archive_name_cursor, status, record_size);
717 }
718
719 /* Handle read errors on the archive. If the read should be retried,
720 return to the caller. */
721 void
722 archive_read_error (void)
723 {
724 read_error (*archive_name_cursor);
725
726 if (record_start_block == 0)
727 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
728
729 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
730 then give up on reading the archive. */
731
732 if (read_error_count++ > READ_ERROR_MAX)
733 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
734 return;
735 }
736
737 static void
738 short_read (size_t status)
739 {
740 size_t left; /* bytes left */
741 char *more; /* pointer to next byte to read */
742
743 more = record_start->buffer + status;
744 left = record_size - status;
745
746 while (left % BLOCKSIZE != 0
747 || (left && status && read_full_records))
748 {
749 if (status)
750 while ((status = rmtread (archive, more, left)) == SAFE_READ_ERROR)
751 archive_read_error ();
752
753 if (status == 0)
754 break;
755
756 if (! read_full_records)
757 {
758 unsigned long rest = record_size - left;
759
760 FATAL_ERROR ((0, 0,
761 ngettext ("Unaligned block (%lu byte) in archive",
762 "Unaligned block (%lu bytes) in archive",
763 rest),
764 rest));
765 }
766
767 /* User warned us about this. Fix up. */
768
769 left -= status;
770 more += status;
771 }
772
773 /* FIXME: for size=0, multi-volume support. On the first record, warn
774 about the problem. */
775
776 if (!read_full_records && verbose_option > 1
777 && record_start_block == 0 && status != 0)
778 {
779 unsigned long rsize = (record_size - left) / BLOCKSIZE;
780 WARN ((0, 0,
781 ngettext ("Record size = %lu block",
782 "Record size = %lu blocks",
783 rsize),
784 rsize));
785 }
786
787 record_end = record_start + (record_size - left) / BLOCKSIZE;
788 records_read++;
789 }
790
791 /* Perform a read to flush the buffer. */
792 void
793 flush_read (void)
794 {
795 size_t status; /* result from system call */
796
797 if (checkpoint_option && !(++checkpoint % 10))
798 /* TRANSLATORS: This is a ``checkpoint of read operation'',
799 *not* ``Reading a checkpoint''.
800 E.g. in Spanish ``Punto de comprobaci@'on de lectura'',
801 *not* ``Leyendo un punto de comprobaci@'on'' */
802 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
803
804 /* Clear the count of errors. This only applies to a single call to
805 flush_read. */
806
807 read_error_count = 0; /* clear error count */
808
809 if (write_archive_to_stdout && record_start_block != 0)
810 {
811 archive = STDOUT_FILENO;
812 status = sys_write_archive_buffer ();
813 archive = STDIN_FILENO;
814 if (status != record_size)
815 archive_write_error (status);
816 }
817 if (multi_volume_option)
818 {
819 if (save_name)
820 {
821 assign_string (&real_s_name,
822 safer_name_suffix (save_name, false,
823 absolute_names_option));
824 real_s_sizeleft = save_sizeleft;
825 real_s_totsize = save_totsize;
826 }
827 else
828 {
829 assign_string (&real_s_name, 0);
830 real_s_totsize = 0;
831 real_s_sizeleft = 0;
832 }
833 }
834
835 error_loop:
836 status = rmtread (archive, record_start->buffer, record_size);
837 if (status == record_size)
838 {
839 records_read++;
840 return;
841 }
842
843 /* The condition below used to include
844 || (status > 0 && !read_full_records)
845 This is incorrect since even if new_volume() succeeds, the
846 subsequent call to rmtread will overwrite the chunk of data
847 already read in the buffer, so the processing will fail */
848
849 if ((status == 0
850 || (status == SAFE_READ_ERROR && errno == ENOSPC))
851 && multi_volume_option)
852 {
853 union block *cursor;
854
855 try_volume:
856 switch (subcommand_option)
857 {
858 case APPEND_SUBCOMMAND:
859 case CAT_SUBCOMMAND:
860 case UPDATE_SUBCOMMAND:
861 if (!new_volume (ACCESS_UPDATE))
862 return;
863 break;
864
865 default:
866 if (!new_volume (ACCESS_READ))
867 return;
868 break;
869 }
870
871 while ((status = rmtread (archive, record_start->buffer, record_size))
872 == SAFE_READ_ERROR)
873 archive_read_error ();
874
875 if (status != record_size)
876 short_read (status);
877
878 cursor = record_start;
879
880 if (cursor->header.typeflag == GNUTYPE_VOLHDR)
881 {
882 if (volume_label_option)
883 {
884 if (!check_label_pattern (cursor))
885 {
886 WARN ((0, 0, _("Volume %s does not match %s"),
887 quote_n (0, cursor->header.name),
888 quote_n (1, volume_label_option)));
889 volno--;
890 global_volno--;
891 goto try_volume;
892 }
893 }
894 if (verbose_option)
895 fprintf (stdlis, _("Reading %s\n"), quote (cursor->header.name));
896 cursor++;
897 }
898 else if (volume_label_option)
899 WARN ((0, 0, _("WARNING: No volume header")));
900
901 if (real_s_name)
902 {
903 uintmax_t s1, s2;
904 if (cursor->header.typeflag != GNUTYPE_MULTIVOL
905 || strncmp (cursor->header.name, real_s_name, NAME_FIELD_SIZE))
906 {
907 WARN ((0, 0, _("%s is not continued on this volume"),
908 quote (real_s_name)));
909 volno--;
910 global_volno--;
911 goto try_volume;
912 }
913 s1 = UINTMAX_FROM_HEADER (cursor->header.size);
914 s2 = UINTMAX_FROM_HEADER (cursor->oldgnu_header.offset);
915 if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
916 {
917 char totsizebuf[UINTMAX_STRSIZE_BOUND];
918 char s1buf[UINTMAX_STRSIZE_BOUND];
919 char s2buf[UINTMAX_STRSIZE_BOUND];
920
921 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
922 quote (cursor->header.name),
923 STRINGIFY_BIGINT (save_totsize, totsizebuf),
924 STRINGIFY_BIGINT (s1, s1buf),
925 STRINGIFY_BIGINT (s2, s2buf)));
926 volno--;
927 global_volno--;
928 goto try_volume;
929 }
930 if (real_s_totsize - real_s_sizeleft
931 != OFF_FROM_HEADER (cursor->oldgnu_header.offset))
932 {
933 WARN ((0, 0, _("This volume is out of sequence")));
934 volno--;
935 global_volno--;
936 goto try_volume;
937 }
938 cursor++;
939 }
940 current_block = cursor;
941 records_read++;
942 return;
943 }
944 else if (status == SAFE_READ_ERROR)
945 {
946 archive_read_error ();
947 goto error_loop; /* try again */
948 }
949
950 short_read (status);
951 }
952
953 /* Flush the current buffer to/from the archive. */
954 void
955 flush_archive (void)
956 {
957 record_start_block += record_end - record_start;
958 current_block = record_start;
959 record_end = record_start + blocking_factor;
960
961 if (access_mode == ACCESS_READ && time_to_start_writing)
962 {
963 access_mode = ACCESS_WRITE;
964 time_to_start_writing = false;
965 backspace_output ();
966 }
967
968 switch (access_mode)
969 {
970 case ACCESS_READ:
971 flush_read ();
972 break;
973
974 case ACCESS_WRITE:
975 flush_write ();
976 break;
977
978 case ACCESS_UPDATE:
979 abort ();
980 }
981 }
982
983 /* Backspace the archive descriptor by one record worth. If it's a
984 tape, MTIOCTOP will work. If it's something else, try to seek on
985 it. If we can't seek, we lose! */
986 static void
987 backspace_output (void)
988 {
989 #ifdef MTIOCTOP
990 {
991 struct mtop operation;
992
993 operation.mt_op = MTBSR;
994 operation.mt_count = 1;
995 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
996 return;
997 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
998 return;
999 }
1000 #endif
1001
1002 {
1003 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
1004
1005 /* Seek back to the beginning of this record and start writing there. */
1006
1007 position -= record_size;
1008 if (position < 0)
1009 position = 0;
1010 if (rmtlseek (archive, position, SEEK_SET) != position)
1011 {
1012 /* Lseek failed. Try a different method. */
1013
1014 WARN ((0, 0,
1015 _("Cannot backspace archive file; it may be unreadable without -i")));
1016
1017 /* Replace the first part of the record with NULs. */
1018
1019 if (record_start->buffer != output_start)
1020 memset (record_start->buffer, 0,
1021 output_start - record_start->buffer);
1022 }
1023 }
1024 }
1025
1026 off_t
1027 seek_archive (off_t size)
1028 {
1029 off_t start = current_block_ordinal ();
1030 off_t offset;
1031 off_t nrec, nblk;
1032 off_t skipped = (blocking_factor - (current_block - record_start));
1033
1034 size -= skipped * BLOCKSIZE;
1035
1036 if (size < record_size)
1037 return 0;
1038 /* FIXME: flush? */
1039
1040 /* Compute number of records to skip */
1041 nrec = size / record_size;
1042 offset = rmtlseek (archive, nrec * record_size, SEEK_CUR);
1043 if (offset < 0)
1044 return offset;
1045
1046 if (offset % record_size)
1047 FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
1048
1049 /* Convert to number of records */
1050 offset /= BLOCKSIZE;
1051 /* Compute number of skipped blocks */
1052 nblk = offset - start;
1053
1054 /* Update buffering info */
1055 records_read += nblk / blocking_factor;
1056 record_start_block = offset - blocking_factor;
1057 current_block = record_end;
1058
1059 return nblk;
1060 }
1061
1062 /* Close the archive file. */
1063 void
1064 close_archive (void)
1065 {
1066 if (time_to_start_writing || access_mode == ACCESS_WRITE)
1067 flush_archive ();
1068
1069 sys_drain_input_pipe ();
1070
1071 compute_duration ();
1072 if (verify_option)
1073 verify_volume ();
1074
1075 if (rmtclose (archive) != 0)
1076 close_warn (*archive_name_cursor);
1077
1078 sys_wait_for_child (child_pid);
1079
1080 tar_stat_destroy (&current_stat_info);
1081 if (save_name)
1082 free (save_name);
1083 if (real_s_name)
1084 free (real_s_name);
1085 free (record_buffer);
1086 }
1087
1088 /* Called to initialize the global volume number. */
1089 void
1090 init_volume_number (void)
1091 {
1092 FILE *file = fopen (volno_file_option, "r");
1093
1094 if (file)
1095 {
1096 if (fscanf (file, "%d", &global_volno) != 1
1097 || global_volno < 0)
1098 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1099 quotearg_colon (volno_file_option)));
1100 if (ferror (file))
1101 read_error (volno_file_option);
1102 if (fclose (file) != 0)
1103 close_error (volno_file_option);
1104 }
1105 else if (errno != ENOENT)
1106 open_error (volno_file_option);
1107 }
1108
1109 /* Called to write out the closing global volume number. */
1110 void
1111 closeout_volume_number (void)
1112 {
1113 FILE *file = fopen (volno_file_option, "w");
1114
1115 if (file)
1116 {
1117 fprintf (file, "%d\n", global_volno);
1118 if (ferror (file))
1119 write_error (volno_file_option);
1120 if (fclose (file) != 0)
1121 close_error (volno_file_option);
1122 }
1123 else
1124 open_error (volno_file_option);
1125 }
1126
1127 /* We've hit the end of the old volume. Close it and open the next one.
1128 Return nonzero on success.
1129 */
1130 static bool
1131 new_volume (enum access_mode mode)
1132 {
1133 static FILE *read_file;
1134 static int looped;
1135
1136 if (!read_file && !info_script_option)
1137 /* FIXME: if fopen is used, it will never be closed. */
1138 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
1139
1140 if (now_verifying)
1141 return false;
1142 if (verify_option)
1143 verify_volume ();
1144
1145 if (rmtclose (archive) != 0)
1146 close_warn (*archive_name_cursor);
1147
1148 global_volno++;
1149 if (global_volno < 0)
1150 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1151 volno++;
1152 archive_name_cursor++;
1153 if (archive_name_cursor == archive_name_array + archive_names)
1154 {
1155 archive_name_cursor = archive_name_array;
1156 looped = 1;
1157 }
1158
1159 tryagain:
1160 if (looped)
1161 {
1162 /* We have to prompt from now on. */
1163
1164 if (info_script_option)
1165 {
1166 if (volno_file_option)
1167 closeout_volume_number ();
1168 if (system (info_script_option) != 0)
1169 FATAL_ERROR ((0, 0, _("%s command failed"),
1170 quote (info_script_option)));
1171 }
1172 else
1173 while (1)
1174 {
1175 char input_buffer[80];
1176
1177 fputc ('\007', stderr);
1178 fprintf (stderr,
1179 _("Prepare volume #%d for %s and hit return: "),
1180 global_volno, quote (*archive_name_cursor));
1181 fflush (stderr);
1182
1183 if (fgets (input_buffer, sizeof input_buffer, read_file) == 0)
1184 {
1185 WARN ((0, 0, _("EOF where user reply was expected")));
1186
1187 if (subcommand_option != EXTRACT_SUBCOMMAND
1188 && subcommand_option != LIST_SUBCOMMAND
1189 && subcommand_option != DIFF_SUBCOMMAND)
1190 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1191
1192 fatal_exit ();
1193 }
1194 if (input_buffer[0] == '\n'
1195 || input_buffer[0] == 'y'
1196 || input_buffer[0] == 'Y')
1197 break;
1198
1199 switch (input_buffer[0])
1200 {
1201 case '?':
1202 {
1203 /* FIXME: Might it be useful to disable the '!' command? */
1204 fprintf (stderr, _("\
1205 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1206 q Abort tar\n\
1207 ! Spawn a subshell\n\
1208 ? Print this list\n"));
1209 }
1210 break;
1211
1212 case 'q':
1213 /* Quit. */
1214
1215 WARN ((0, 0, _("No new volume; exiting.\n")));
1216
1217 if (subcommand_option != EXTRACT_SUBCOMMAND
1218 && subcommand_option != LIST_SUBCOMMAND
1219 && subcommand_option != DIFF_SUBCOMMAND)
1220 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1221
1222 fatal_exit ();
1223
1224 case 'n':
1225 /* Get new file name. */
1226
1227 {
1228 char *name = &input_buffer[1];
1229 char *cursor;
1230
1231 for (name = input_buffer + 1;
1232 *name == ' ' || *name == '\t';
1233 name++)
1234 ;
1235
1236 for (cursor = name; *cursor && *cursor != '\n'; cursor++)
1237 ;
1238 *cursor = '\0';
1239
1240 /* FIXME: the following allocation is never reclaimed. */
1241 *archive_name_cursor = xstrdup (name);
1242 }
1243 break;
1244
1245 case '!':
1246 sys_spawn_shell ();
1247 break;
1248 }
1249 }
1250 }
1251
1252 if (strcmp (archive_name_cursor[0], "-") == 0)
1253 {
1254 read_full_records = true;
1255 archive = STDIN_FILENO;
1256 }
1257 else if (verify_option)
1258 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1259 rsh_command_option);
1260 else
1261 switch (mode)
1262 {
1263 case ACCESS_READ:
1264 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1265 rsh_command_option);
1266 break;
1267
1268 case ACCESS_WRITE:
1269 if (backup_option)
1270 maybe_backup_file (*archive_name_cursor, 1);
1271 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1272 rsh_command_option);
1273 break;
1274
1275 case ACCESS_UPDATE:
1276 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1277 rsh_command_option);
1278 break;
1279 }
1280
1281 if (archive < 0)
1282 {
1283 open_warn (*archive_name_cursor);
1284 if (!verify_option && mode == ACCESS_WRITE && backup_option)
1285 undo_last_backup ();
1286 goto tryagain;
1287 }
1288
1289 SET_BINARY_MODE (archive);
1290
1291 return true;
1292 }
This page took 0.092236 seconds and 4 git commands to generate.