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