]> Dogcows Code - chaz/tar/blob - src/buffer.c
Minor changes
[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 break;
627
628 if (! read_full_records_option)
629 {
630 unsigned long rest = record_size - left;
631
632 FATAL_ERROR ((0, 0,
633 ngettext ("Unaligned block (%lu byte) in archive",
634 "Unaligned block (%lu bytes) in archive",
635 rest),
636 rest));
637 }
638
639 /* User warned us about this. Fix up. */
640
641 left -= status;
642 more += status;
643 }
644
645 /* FIXME: for size=0, multi-volume support. On the first record, warn
646 about the problem. */
647
648 if (!read_full_records_option && verbose_option
649 && record_start_block == 0 && status > 0)
650 {
651 unsigned long rsize = (record_size - left) / BLOCKSIZE;
652 WARN ((0, 0,
653 ngettext ("Record size = %lu block",
654 "Record size = %lu blocks",
655 rsize),
656 rsize));
657 }
658
659 record_end = record_start + (record_size - left) / BLOCKSIZE;
660 records_read++;
661 }
662
663 /* Perform a read to flush the buffer. */
664 void
665 flush_read (void)
666 {
667 ssize_t status; /* result from system call */
668
669 if (checkpoint_option && !(++checkpoint % 10))
670 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
671
672 /* Clear the count of errors. This only applies to a single call to
673 flush_read. */
674
675 read_error_count = 0; /* clear error count */
676
677 if (write_archive_to_stdout && record_start_block != 0)
678 {
679 archive = STDOUT_FILENO;
680 status = sys_write_archive_buffer ();
681 archive = STDIN_FILENO;
682 if (status != record_size)
683 archive_write_error (status);
684 }
685 if (multi_volume_option)
686 {
687 if (save_name)
688 {
689 assign_string (&real_s_name, safer_name_suffix (save_name, 0));
690 real_s_sizeleft = save_sizeleft;
691 real_s_totsize = save_totsize;
692 }
693 else
694 {
695 assign_string (&real_s_name, 0);
696 real_s_totsize = 0;
697 real_s_sizeleft = 0;
698 }
699 }
700
701 error_loop:
702 status = rmtread (archive, record_start->buffer, record_size);
703 if (status == record_size)
704 {
705 records_read++;
706 return;
707 }
708
709 if ((status == 0
710 || (status < 0 && errno == ENOSPC)
711 || (status > 0 && !read_full_records_option))
712 && multi_volume_option)
713 {
714 union block *cursor;
715
716 try_volume:
717 switch (subcommand_option)
718 {
719 case APPEND_SUBCOMMAND:
720 case CAT_SUBCOMMAND:
721 case UPDATE_SUBCOMMAND:
722 if (!new_volume (ACCESS_UPDATE))
723 return;
724 break;
725
726 default:
727 if (!new_volume (ACCESS_READ))
728 return;
729 break;
730 }
731
732 vol_error:
733 status = rmtread (archive, record_start->buffer, record_size);
734 if (status < 0)
735 {
736 archive_read_error ();
737 goto vol_error;
738 }
739 if (status != record_size)
740 short_read (status);
741
742 cursor = record_start;
743
744 if (cursor->header.typeflag == GNUTYPE_VOLHDR)
745 {
746 if (volume_label_option)
747 {
748 if (!check_label_pattern (cursor))
749 {
750 WARN ((0, 0, _("Volume %s does not match %s"),
751 quote_n (0, cursor->header.name),
752 quote_n (1, volume_label_option)));
753 volno--;
754 global_volno--;
755 goto try_volume;
756 }
757 }
758 if (verbose_option)
759 fprintf (stdlis, _("Reading %s\n"), quote (cursor->header.name));
760 cursor++;
761 }
762 else if (volume_label_option)
763 WARN ((0, 0, _("WARNING: No volume header")));
764
765 if (real_s_name)
766 {
767 uintmax_t s1, s2;
768 if (cursor->header.typeflag != GNUTYPE_MULTIVOL
769 || strcmp (cursor->header.name, real_s_name))
770 {
771 WARN ((0, 0, _("%s is not continued on this volume"),
772 quote (real_s_name)));
773 volno--;
774 global_volno--;
775 goto try_volume;
776 }
777 s1 = UINTMAX_FROM_HEADER (cursor->header.size);
778 s2 = UINTMAX_FROM_HEADER (cursor->oldgnu_header.offset);
779 if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
780 {
781 char totsizebuf[UINTMAX_STRSIZE_BOUND];
782 char s1buf[UINTMAX_STRSIZE_BOUND];
783 char s2buf[UINTMAX_STRSIZE_BOUND];
784
785 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
786 quote (cursor->header.name),
787 STRINGIFY_BIGINT (save_totsize, totsizebuf),
788 STRINGIFY_BIGINT (s1, s1buf),
789 STRINGIFY_BIGINT (s2, s2buf)));
790 volno--;
791 global_volno--;
792 goto try_volume;
793 }
794 if (real_s_totsize - real_s_sizeleft
795 != OFF_FROM_HEADER (cursor->oldgnu_header.offset))
796 {
797 WARN ((0, 0, _("This volume is out of sequence")));
798 volno--;
799 global_volno--;
800 goto try_volume;
801 }
802 cursor++;
803 }
804 current_block = cursor;
805 records_read++;
806 return;
807 }
808 else if (status < 0)
809 {
810 archive_read_error ();
811 goto error_loop; /* try again */
812 }
813
814 short_read (status);
815 }
816
817 /* Flush the current buffer to/from the archive. */
818 void
819 flush_archive (void)
820 {
821 record_start_block += record_end - record_start;
822 current_block = record_start;
823 record_end = record_start + blocking_factor;
824
825 if (access_mode == ACCESS_READ && time_to_start_writing)
826 {
827 access_mode = ACCESS_WRITE;
828 time_to_start_writing = 0;
829
830 if (file_to_switch_to >= 0)
831 {
832 if (rmtclose (archive) != 0)
833 close_warn (*archive_name_cursor);
834
835 archive = file_to_switch_to;
836 }
837 else
838 backspace_output ();
839 }
840
841 switch (access_mode)
842 {
843 case ACCESS_READ:
844 flush_read ();
845 break;
846
847 case ACCESS_WRITE:
848 flush_write ();
849 break;
850
851 case ACCESS_UPDATE:
852 abort ();
853 }
854 }
855
856 /* Backspace the archive descriptor by one record worth. If it's a
857 tape, MTIOCTOP will work. If it's something else, try to seek on
858 it. If we can't seek, we lose! */
859 static void
860 backspace_output (void)
861 {
862 #ifdef MTIOCTOP
863 {
864 struct mtop operation;
865
866 operation.mt_op = MTBSR;
867 operation.mt_count = 1;
868 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
869 return;
870 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
871 return;
872 }
873 #endif
874
875 {
876 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
877
878 /* Seek back to the beginning of this record and start writing there. */
879
880 position -= record_size;
881 if (position < 0)
882 position = 0;
883 if (rmtlseek (archive, position, SEEK_SET) != position)
884 {
885 /* Lseek failed. Try a different method. */
886
887 WARN ((0, 0,
888 _("Cannot backspace archive file; it may be unreadable without -i")));
889
890 /* Replace the first part of the record with NULs. */
891
892 if (record_start->buffer != output_start)
893 memset (record_start->buffer, 0,
894 output_start - record_start->buffer);
895 }
896 }
897 }
898
899 /* Close the archive file. */
900 void
901 close_archive (void)
902 {
903 if (time_to_start_writing || access_mode == ACCESS_WRITE)
904 flush_archive ();
905
906 sys_drain_input_pipe ();
907
908 if (verify_option)
909 verify_volume ();
910
911 if (rmtclose (archive) != 0)
912 close_warn (*archive_name_cursor);
913
914 sys_wait_for_child (child_pid);
915
916 tar_stat_destroy (&current_stat_info);
917 if (save_name)
918 free (save_name);
919 if (real_s_name)
920 free (real_s_name);
921 free (multi_volume_option ? record_start - 2 : record_start);
922 }
923
924 /* Called to initialize the global volume number. */
925 void
926 init_volume_number (void)
927 {
928 FILE *file = fopen (volno_file_option, "r");
929
930 if (file)
931 {
932 if (fscanf (file, "%d", &global_volno) != 1
933 || global_volno < 0)
934 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
935 quotearg_colon (volno_file_option)));
936 if (ferror (file))
937 read_error (volno_file_option);
938 if (fclose (file) != 0)
939 close_error (volno_file_option);
940 }
941 else if (errno != ENOENT)
942 open_error (volno_file_option);
943 }
944
945 /* Called to write out the closing global volume number. */
946 void
947 closeout_volume_number (void)
948 {
949 FILE *file = fopen (volno_file_option, "w");
950
951 if (file)
952 {
953 fprintf (file, "%d\n", global_volno);
954 if (ferror (file))
955 write_error (volno_file_option);
956 if (fclose (file) != 0)
957 close_error (volno_file_option);
958 }
959 else
960 open_error (volno_file_option);
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 static int
966 new_volume (enum access_mode access)
967 {
968 static FILE *read_file;
969 static int looped;
970
971 if (!read_file && !info_script_option)
972 /* FIXME: if fopen is used, it will never be closed. */
973 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
974
975 if (now_verifying)
976 return 0;
977 if (verify_option)
978 verify_volume ();
979
980 if (rmtclose (archive) != 0)
981 close_warn (*archive_name_cursor);
982
983 global_volno++;
984 if (global_volno < 0)
985 FATAL_ERROR ((0, 0, _("Volume number overflow")));
986 volno++;
987 archive_name_cursor++;
988 if (archive_name_cursor == archive_name_array + archive_names)
989 {
990 archive_name_cursor = archive_name_array;
991 looped = 1;
992 }
993
994 tryagain:
995 if (looped)
996 {
997 /* We have to prompt from now on. */
998
999 if (info_script_option)
1000 {
1001 if (volno_file_option)
1002 closeout_volume_number ();
1003 if (system (info_script_option) != 0)
1004 FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option));
1005 }
1006 else
1007 while (1)
1008 {
1009 char input_buffer[80];
1010
1011 fputc ('\007', stderr);
1012 fprintf (stderr,
1013 _("Prepare volume #%d for %s and hit return: "),
1014 global_volno, quote (*archive_name_cursor));
1015 fflush (stderr);
1016
1017 if (fgets (input_buffer, sizeof input_buffer, read_file) == 0)
1018 {
1019 WARN ((0, 0, _("EOF where user reply was expected")));
1020
1021 if (subcommand_option != EXTRACT_SUBCOMMAND
1022 && subcommand_option != LIST_SUBCOMMAND
1023 && subcommand_option != DIFF_SUBCOMMAND)
1024 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1025
1026 fatal_exit ();
1027 }
1028 if (input_buffer[0] == '\n'
1029 || input_buffer[0] == 'y'
1030 || input_buffer[0] == 'Y')
1031 break;
1032
1033 switch (input_buffer[0])
1034 {
1035 case '?':
1036 {
1037 fprintf (stderr, _("\
1038 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1039 q Abort tar\n\
1040 ! Spawn a subshell\n\
1041 ? Print this list\n"));
1042 }
1043 break;
1044
1045 case 'q':
1046 /* Quit. */
1047
1048 WARN ((0, 0, _("No new volume; exiting.\n")));
1049
1050 if (subcommand_option != EXTRACT_SUBCOMMAND
1051 && subcommand_option != LIST_SUBCOMMAND
1052 && subcommand_option != DIFF_SUBCOMMAND)
1053 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1054
1055 fatal_exit ();
1056
1057 case 'n':
1058 /* Get new file name. */
1059
1060 {
1061 char *name = &input_buffer[1];
1062 char *cursor;
1063
1064 while (*name == ' ' || *name == '\t')
1065 name++;
1066 cursor = name;
1067 while (*cursor && *cursor != '\n')
1068 cursor++;
1069 *cursor = '\0';
1070
1071 /* FIXME: the following allocation is never reclaimed. */
1072 *archive_name_cursor = xstrdup (name);
1073 }
1074 break;
1075
1076 case '!':
1077 sys_spawn_shell ();
1078 break;
1079 }
1080 }
1081 }
1082
1083 if (strcmp (archive_name_cursor[0], "-") == 0)
1084 {
1085 read_full_records_option = true;
1086 archive = STDIN_FILENO;
1087 }
1088 else if (verify_option)
1089 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1090 rsh_command_option);
1091 else
1092 switch (access)
1093 {
1094 case ACCESS_READ:
1095 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1096 rsh_command_option);
1097 break;
1098
1099 case ACCESS_WRITE:
1100 if (backup_option)
1101 maybe_backup_file (*archive_name_cursor, 1);
1102 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1103 rsh_command_option);
1104 break;
1105
1106 case ACCESS_UPDATE:
1107 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1108 rsh_command_option);
1109 break;
1110 }
1111
1112 if (archive < 0)
1113 {
1114 open_warn (*archive_name_cursor);
1115 if (!verify_option && access == ACCESS_WRITE && backup_option)
1116 undo_last_backup ();
1117 goto tryagain;
1118 }
1119
1120 SET_BINARY_MODE (archive);
1121
1122 return 1;
1123 }
1124
This page took 0.083013 seconds and 5 git commands to generate.