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