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