]> Dogcows Code - chaz/tar/blob - src/buffer.c
(new_volume): Stop if the script exits with an error.
[chaz/tar] / src / buffer.c
1 /* Buffer management for tar.
2
3 Copyright 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001 Free
4 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 #if MSDOS
27 # include <process.h>
28 #endif
29
30 #if XENIX
31 # include <sys/inode.h>
32 #endif
33
34 #include <fnmatch.h>
35 #include <human.h>
36 #include <quotearg.h>
37
38 #include "common.h"
39 #include "rmt.h"
40
41 #define PREAD 0 /* read file descriptor from pipe() */
42 #define PWRITE 1 /* write file descriptor from pipe() */
43
44 /* Number of retries before giving up on read. */
45 #define READ_ERROR_MAX 10
46
47 /* Globbing pattern to append to volume label if initial match failed. */
48 #define VOLUME_LABEL_APPEND " Volume [1-9]*"
49 \f
50 /* Variables. */
51
52 static tarlong prev_written; /* bytes written on previous volumes */
53 static tarlong bytes_written; /* bytes written on this volume */
54
55 /* FIXME: The following four variables should ideally be static to this
56 module. However, this cannot be done yet, as update.c uses the first
57 three a lot, and compare.c uses the fourth. The cleanup continues! */
58
59 union block *record_start; /* start of record of archive */
60 union block *record_end; /* last+1 block of archive record */
61 union block *current_block; /* current block of archive */
62 enum access_mode access_mode; /* how do we handle the archive */
63 static struct stat archive_stat; /* stat block for archive file */
64
65 static off_t record_start_block; /* block ordinal at record_start */
66
67 /* Where we write list messages (not errors, not interactions) to. Stdout
68 unless we're writing a pipe, in which case stderr. */
69 FILE *stdlis;
70
71 static void backspace_output PARAMS ((void));
72 static int new_volume PARAMS ((enum access_mode));
73 static void archive_write_error PARAMS ((ssize_t)) __attribute__ ((noreturn));
74 static void archive_read_error PARAMS ((void));
75
76 #if !MSDOS
77 /* Obnoxious test to see if dimwit is trying to dump the archive. */
78 dev_t ar_dev;
79 ino_t ar_ino;
80 #endif
81
82 /* PID of child program, if compress_option or remote archive access. */
83 static pid_t child_pid;
84
85 /* Error recovery stuff */
86 static int read_error_count;
87
88 /* Have we hit EOF yet? */
89 static int hit_eof;
90
91 /* Checkpointing counter */
92 static int checkpoint;
93
94 /* We're reading, but we just read the last block and its time to update. */
95 /* As least EXTERN like this one as possible. FIXME! */
96 extern int time_to_start_writing;
97
98 int file_to_switch_to = -1; /* if remote update, close archive, and use
99 this descriptor to write to */
100
101 static int volno = 1; /* which volume of a multi-volume tape we're
102 on */
103 static int global_volno = 1; /* volume number to print in external
104 messages */
105
106 /* The pointer save_name, which is set in function dump_file() of module
107 create.c, points to the original long filename instead of the new,
108 shorter mangled name that is set in start_header() of module create.c.
109 The pointer save_name is only used in multi-volume mode when the file
110 being processed is non-sparse; if a file is split between volumes, the
111 save_name is used in generating the LF_MULTIVOL record on the second
112 volume. (From Pierce Cantrell, 1991-08-13.) */
113
114 char *save_name; /* name of the file we are currently writing */
115 off_t save_totsize; /* total size of file we are writing, only
116 valid if save_name is nonzero */
117 off_t save_sizeleft; /* where we are in the file we are writing,
118 only valid if save_name is nonzero */
119
120 int write_archive_to_stdout;
121
122 /* Used by flush_read and flush_write to store the real info about saved
123 names. */
124 static char *real_s_name;
125 static off_t real_s_totsize;
126 static off_t real_s_sizeleft;
127 \f
128 /* Functions. */
129
130 void
131 print_total_written (void)
132 {
133 tarlong written = prev_written + bytes_written;
134 char bytes[sizeof (tarlong) * CHAR_BIT];
135 char abbr[LONGEST_HUMAN_READABLE + 1];
136 char rate[LONGEST_HUMAN_READABLE + 1];
137 double seconds;
138
139 #if HAVE_CLOCK_GETTIME
140 struct timespec now;
141 if (clock_gettime (CLOCK_REALTIME, &now) == 0)
142 seconds = ((now.tv_sec - start_timespec.tv_sec)
143 + (now.tv_nsec - start_timespec.tv_nsec) / 1e9);
144 else
145 #endif
146 seconds = time (0) - start_time;
147
148 sprintf (bytes, TARLONG_FORMAT, written);
149
150 /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
151 fprintf (stderr, _("Total bytes written: %s (%sB, %sB/s)\n"), bytes,
152 human_readable ((uintmax_t) written, abbr, 1, -1024),
153 (0 < seconds && written / seconds < (uintmax_t) -1
154 ? human_readable ((uintmax_t) (written / seconds), rate, 1, -1024)
155 : "?"));
156 }
157
158 /* Compute and return the block ordinal at current_block. */
159 off_t
160 current_block_ordinal (void)
161 {
162 return record_start_block + (current_block - record_start);
163 }
164
165 /* If the EOF flag is set, reset it, as well as current_block, etc. */
166 void
167 reset_eof (void)
168 {
169 if (hit_eof)
170 {
171 hit_eof = 0;
172 current_block = record_start;
173 record_end = record_start + blocking_factor;
174 access_mode = ACCESS_WRITE;
175 }
176 }
177
178 /* Return the location of the next available input or output block.
179 Return zero for EOF. Once we have returned zero, we just keep returning
180 it, to avoid accidentally going on to the next file on the tape. */
181 union block *
182 find_next_block (void)
183 {
184 if (current_block == record_end)
185 {
186 if (hit_eof)
187 return 0;
188 flush_archive ();
189 if (current_block == record_end)
190 {
191 hit_eof = 1;
192 return 0;
193 }
194 }
195 return current_block;
196 }
197
198 /* Indicate that we have used all blocks up thru BLOCK.
199 FIXME: should the arg have an off-by-1? */
200 void
201 set_next_block_after (union block *block)
202 {
203 while (block >= current_block)
204 current_block++;
205
206 /* Do *not* flush the archive here. If we do, the same argument to
207 set_next_block_after could mean the next block (if the input record
208 is exactly one block long), which is not what is intended. */
209
210 if (current_block > record_end)
211 abort ();
212 }
213
214 /* Return the number of bytes comprising the space between POINTER
215 through the end of the current buffer of blocks. This space is
216 available for filling with data, or taking data from. POINTER is
217 usually (but not always) the result previous find_next_block call. */
218 size_t
219 available_space_after (union block *pointer)
220 {
221 return record_end->buffer - pointer->buffer;
222 }
223
224 /* Close file having descriptor FD, and abort if close unsuccessful. */
225 static void
226 xclose (int fd)
227 {
228 if (close (fd) != 0)
229 close_error (_("(pipe)"));
230 }
231
232 /* Duplicate file descriptor FROM into becoming INTO.
233 INTO is closed first and has to be the next available slot. */
234 static void
235 xdup2 (int from, int into)
236 {
237 if (from != into)
238 {
239 int status = close (into);
240
241 if (status != 0 && errno != EBADF)
242 {
243 int e = errno;
244 FATAL_ERROR ((0, e, _("Cannot close")));
245 }
246 status = dup (from);
247 if (status != into)
248 {
249 if (status < 0)
250 {
251 int e = errno;
252 FATAL_ERROR ((0, e, _("Cannot dup")));
253 }
254 abort ();
255 }
256 xclose (from);
257 }
258 }
259
260 #if MSDOS
261
262 /* Set ARCHIVE for writing, then compressing an archive. */
263 static void
264 child_open_for_compress (void)
265 {
266 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
267 }
268
269 /* Set ARCHIVE for uncompressing, then reading an archive. */
270 static void
271 child_open_for_uncompress (void)
272 {
273 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
274 }
275
276 #else /* not MSDOS */
277
278 /* Return nonzero if NAME is the name of a regular file, or if the file
279 does not exist (so it would be created as a regular file). */
280 static int
281 is_regular_file (const char *name)
282 {
283 struct stat stbuf;
284
285 if (stat (name, &stbuf) == 0)
286 return S_ISREG (stbuf.st_mode);
287 else
288 return errno == ENOENT;
289 }
290
291 static ssize_t
292 write_archive_buffer (void)
293 {
294 ssize_t status;
295 ssize_t written = 0;
296
297 while (0 <= (status = rmtwrite (archive, record_start->buffer + written,
298 record_size - written)))
299 {
300 written += status;
301 if (written == record_size
302 || _isrmt (archive) || ! S_ISFIFO (archive_stat.st_mode))
303 break;
304 }
305
306 return written ? written : status;
307 }
308
309 /* Set ARCHIVE for writing, then compressing an archive. */
310 static void
311 child_open_for_compress (void)
312 {
313 int parent_pipe[2];
314 int child_pipe[2];
315 pid_t grandchild_pid;
316 int wait_status;
317
318 xpipe (parent_pipe);
319 child_pid = xfork ();
320
321 if (child_pid > 0)
322 {
323 /* The parent tar is still here! Just clean up. */
324
325 archive = parent_pipe[PWRITE];
326 xclose (parent_pipe[PREAD]);
327 return;
328 }
329
330 /* The new born child tar is here! */
331
332 program_name = _("tar (child)");
333
334 xdup2 (parent_pipe[PREAD], STDIN_FILENO);
335 xclose (parent_pipe[PWRITE]);
336
337 /* Check if we need a grandchild tar. This happens only if either:
338 a) we are writing stdout: to force reblocking;
339 b) the file is to be accessed by rmt: compressor doesn't know how;
340 c) the file is not a plain file. */
341
342 if (strcmp (archive_name_array[0], "-") != 0
343 && !_remdev (archive_name_array[0])
344 && is_regular_file (archive_name_array[0]))
345 {
346 if (backup_option)
347 maybe_backup_file (archive_name_array[0], 1);
348
349 /* We don't need a grandchild tar. Open the archive and launch the
350 compressor. */
351
352 archive = creat (archive_name_array[0], MODE_RW);
353 if (archive < 0)
354 {
355 int saved_errno = errno;
356
357 if (backup_option)
358 undo_last_backup ();
359 errno = saved_errno;
360 open_fatal (archive_name_array[0]);
361 }
362 xdup2 (archive, STDOUT_FILENO);
363 execlp (use_compress_program_option, use_compress_program_option,
364 (char *) 0);
365 exec_fatal (use_compress_program_option);
366 }
367
368 /* We do need a grandchild tar. */
369
370 xpipe (child_pipe);
371 grandchild_pid = xfork ();
372
373 if (grandchild_pid == 0)
374 {
375 /* The newborn grandchild tar is here! Launch the compressor. */
376
377 program_name = _("tar (grandchild)");
378
379 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
380 xclose (child_pipe[PREAD]);
381 execlp (use_compress_program_option, use_compress_program_option,
382 (char *) 0);
383 exec_fatal (use_compress_program_option);
384 }
385
386 /* The child tar is still here! */
387
388 /* Prepare for reblocking the data from the compressor into the archive. */
389
390 xdup2 (child_pipe[PREAD], STDIN_FILENO);
391 xclose (child_pipe[PWRITE]);
392
393 if (strcmp (archive_name_array[0], "-") == 0)
394 archive = STDOUT_FILENO;
395 else
396 {
397 archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
398 if (archive < 0)
399 open_fatal (archive_name_array[0]);
400 }
401
402 /* Let's read out of the stdin pipe and write an archive. */
403
404 while (1)
405 {
406 ssize_t status = 0;
407 char *cursor;
408 size_t length;
409
410 /* Assemble a record. */
411
412 for (length = 0, cursor = record_start->buffer;
413 length < record_size;
414 length += status, cursor += status)
415 {
416 size_t size = record_size - length;
417
418 if (size < BLOCKSIZE)
419 size = BLOCKSIZE;
420 status = safe_read (STDIN_FILENO, cursor, size);
421 if (status <= 0)
422 break;
423 }
424
425 if (status < 0)
426 read_fatal (use_compress_program_option);
427
428 /* Copy the record. */
429
430 if (status == 0)
431 {
432 /* We hit the end of the file. Write last record at
433 full length, as the only role of the grandchild is
434 doing proper reblocking. */
435
436 if (length > 0)
437 {
438 memset (record_start->buffer + length, 0, record_size - length);
439 status = write_archive_buffer ();
440 if (status != record_size)
441 archive_write_error (status);
442 }
443
444 /* There is nothing else to read, break out. */
445 break;
446 }
447
448 status = write_archive_buffer ();
449 if (status != record_size)
450 archive_write_error (status);
451 }
452
453 #if 0
454 close_archive ();
455 #endif
456
457 /* Propagate any failure of the grandchild back to the parent. */
458
459 while (waitpid (grandchild_pid, &wait_status, 0) == -1)
460 if (errno != EINTR)
461 {
462 waitpid_error (use_compress_program_option);
463 break;
464 }
465
466 if (WIFSIGNALED (wait_status))
467 {
468 kill (child_pid, WTERMSIG (wait_status));
469 exit_status = TAREXIT_FAILURE;
470 }
471 else if (WEXITSTATUS (wait_status) != 0)
472 exit_status = WEXITSTATUS (wait_status);
473
474 exit (exit_status);
475 }
476
477 /* Set ARCHIVE for uncompressing, then reading an archive. */
478 static void
479 child_open_for_uncompress (void)
480 {
481 int parent_pipe[2];
482 int child_pipe[2];
483 pid_t grandchild_pid;
484 int wait_status;
485
486 xpipe (parent_pipe);
487 child_pid = xfork ();
488
489 if (child_pid > 0)
490 {
491 /* The parent tar is still here! Just clean up. */
492
493 read_full_records_option = 1;
494 archive = parent_pipe[PREAD];
495 xclose (parent_pipe[PWRITE]);
496 return;
497 }
498
499 /* The newborn child tar is here! */
500
501 program_name = _("tar (child)");
502
503 xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
504 xclose (parent_pipe[PREAD]);
505
506 /* Check if we need a grandchild tar. This happens only if either:
507 a) we're reading stdin: to force unblocking;
508 b) the file is to be accessed by rmt: compressor doesn't know how;
509 c) the file is not a plain file. */
510
511 if (strcmp (archive_name_array[0], "-") != 0
512 && !_remdev (archive_name_array[0])
513 && is_regular_file (archive_name_array[0]))
514 {
515 /* We don't need a grandchild tar. Open the archive and lauch the
516 uncompressor. */
517
518 archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
519 if (archive < 0)
520 open_fatal (archive_name_array[0]);
521 xdup2 (archive, STDIN_FILENO);
522 execlp (use_compress_program_option, use_compress_program_option,
523 "-d", (char *) 0);
524 exec_fatal (use_compress_program_option);
525 }
526
527 /* We do need a grandchild tar. */
528
529 xpipe (child_pipe);
530 grandchild_pid = xfork ();
531
532 if (grandchild_pid == 0)
533 {
534 /* The newborn grandchild tar is here! Launch the uncompressor. */
535
536 program_name = _("tar (grandchild)");
537
538 xdup2 (child_pipe[PREAD], STDIN_FILENO);
539 xclose (child_pipe[PWRITE]);
540 execlp (use_compress_program_option, use_compress_program_option,
541 "-d", (char *) 0);
542 exec_fatal (use_compress_program_option);
543 }
544
545 /* The child tar is still here! */
546
547 /* Prepare for unblocking the data from the archive into the
548 uncompressor. */
549
550 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
551 xclose (child_pipe[PREAD]);
552
553 if (strcmp (archive_name_array[0], "-") == 0)
554 archive = STDIN_FILENO;
555 else
556 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
557 MODE_RW, rsh_command_option);
558 if (archive < 0)
559 open_fatal (archive_name_array[0]);
560
561 /* Let's read the archive and pipe it into stdout. */
562
563 while (1)
564 {
565 char *cursor;
566 size_t maximum;
567 size_t count;
568 ssize_t status;
569
570 read_error_count = 0;
571
572 error_loop:
573 status = rmtread (archive, record_start->buffer, record_size);
574 if (status < 0)
575 {
576 archive_read_error ();
577 goto error_loop;
578 }
579 if (status == 0)
580 break;
581 cursor = record_start->buffer;
582 maximum = status;
583 while (maximum)
584 {
585 count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
586 if (full_write (STDOUT_FILENO, cursor, count) != count)
587 write_error (use_compress_program_option);
588 cursor += count;
589 maximum -= count;
590 }
591 }
592
593 xclose (STDOUT_FILENO);
594 #if 0
595 close_archive ();
596 #endif
597
598 /* Propagate any failure of the grandchild back to the parent. */
599
600 while (waitpid (grandchild_pid, &wait_status, 0) == -1)
601 if (errno != EINTR)
602 {
603 waitpid_error (use_compress_program_option);
604 break;
605 }
606
607 if (WIFSIGNALED (wait_status))
608 {
609 kill (child_pid, WTERMSIG (wait_status));
610 exit_status = TAREXIT_FAILURE;
611 }
612 else if (WEXITSTATUS (wait_status) != 0)
613 exit_status = WEXITSTATUS (wait_status);
614
615 exit (exit_status);
616 }
617
618 #endif /* not MSDOS */
619
620 /* Check the LABEL block against the volume label, seen as a globbing
621 pattern. Return true if the pattern matches. In case of failure,
622 retry matching a volume sequence number before giving up in
623 multi-volume mode. */
624 static int
625 check_label_pattern (union block *label)
626 {
627 char *string;
628 int result;
629
630 if (! memchr (label->header.name, '\0', sizeof label->header.name))
631 return 0;
632
633 if (fnmatch (volume_label_option, label->header.name, 0) == 0)
634 return 1;
635
636 if (!multi_volume_option)
637 return 0;
638
639 string = xmalloc (strlen (volume_label_option)
640 + sizeof VOLUME_LABEL_APPEND + 1);
641 strcpy (string, volume_label_option);
642 strcat (string, VOLUME_LABEL_APPEND);
643 result = fnmatch (string, label->header.name, 0) == 0;
644 free (string);
645 return result;
646 }
647
648 /* Open an archive file. The argument specifies whether we are
649 reading or writing, or both. */
650 void
651 open_archive (enum access_mode wanted_access)
652 {
653 int backed_up_flag = 0;
654
655 stdlis = to_stdout_option ? stderr : stdout;
656
657 if (record_size == 0)
658 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
659
660 if (archive_names == 0)
661 FATAL_ERROR ((0, 0, _("No archive name given")));
662
663 current_file_name = 0;
664 current_link_name = 0;
665 save_name = 0;
666 real_s_name = 0;
667
668 if (multi_volume_option)
669 {
670 if (verify_option)
671 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
672 record_start = valloc (record_size + (2 * BLOCKSIZE));
673 if (record_start)
674 record_start += 2;
675 }
676 else
677 record_start = valloc (record_size);
678 if (!record_start)
679 FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
680 blocking_factor));
681
682 current_block = record_start;
683 record_end = record_start + blocking_factor;
684 /* When updating the archive, we start with reading. */
685 access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
686
687 if (use_compress_program_option)
688 {
689 if (multi_volume_option)
690 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
691 if (verify_option)
692 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
693
694 switch (wanted_access)
695 {
696 case ACCESS_READ:
697 child_open_for_uncompress ();
698 break;
699
700 case ACCESS_WRITE:
701 child_open_for_compress ();
702 break;
703
704 case ACCESS_UPDATE:
705 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
706 break;
707 }
708
709 if (wanted_access == ACCESS_WRITE
710 && strcmp (archive_name_array[0], "-") == 0)
711 stdlis = stderr;
712 }
713 else if (strcmp (archive_name_array[0], "-") == 0)
714 {
715 read_full_records_option = 1; /* could be a pipe, be safe */
716 if (verify_option)
717 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
718
719 switch (wanted_access)
720 {
721 case ACCESS_READ:
722 archive = STDIN_FILENO;
723 break;
724
725 case ACCESS_WRITE:
726 archive = STDOUT_FILENO;
727 stdlis = stderr;
728 break;
729
730 case ACCESS_UPDATE:
731 archive = STDIN_FILENO;
732 stdlis = stderr;
733 write_archive_to_stdout = 1;
734 break;
735 }
736 }
737 else if (verify_option)
738 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
739 MODE_RW, rsh_command_option);
740 else
741 switch (wanted_access)
742 {
743 case ACCESS_READ:
744 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
745 MODE_RW, rsh_command_option);
746 break;
747
748 case ACCESS_WRITE:
749 if (backup_option)
750 {
751 maybe_backup_file (archive_name_array[0], 1);
752 backed_up_flag = 1;
753 }
754 archive = rmtcreat (archive_name_array[0], MODE_RW,
755 rsh_command_option);
756 break;
757
758 case ACCESS_UPDATE:
759 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
760 MODE_RW, rsh_command_option);
761 break;
762 }
763
764 if (archive < 0
765 || (! _isrmt (archive) && fstat (archive, &archive_stat) < 0))
766 {
767 int saved_errno = errno;
768
769 if (backed_up_flag)
770 undo_last_backup ();
771 errno = saved_errno;
772 open_fatal (archive_name_array[0]);
773 }
774
775 #if !MSDOS
776
777 /* Detect if outputting to "/dev/null". */
778 {
779 static char const dev_null[] = "/dev/null";
780 struct stat dev_null_stat;
781
782 dev_null_output =
783 (strcmp (archive_name_array[0], dev_null) == 0
784 || (! _isrmt (archive)
785 && S_ISCHR (archive_stat.st_mode)
786 && stat (dev_null, &dev_null_stat) == 0
787 && archive_stat.st_dev == dev_null_stat.st_dev
788 && archive_stat.st_ino == dev_null_stat.st_ino));
789 }
790
791 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
792 {
793 ar_dev = archive_stat.st_dev;
794 ar_ino = archive_stat.st_ino;
795 }
796 else
797 ar_dev = 0;
798
799 #endif /* not MSDOS */
800
801 #if MSDOS
802 setmode (archive, O_BINARY);
803 #endif
804
805 switch (wanted_access)
806 {
807 case ACCESS_READ:
808 case ACCESS_UPDATE:
809 record_end = record_start; /* set up for 1st record = # 0 */
810 find_next_block (); /* read it in, check for EOF */
811
812 if (volume_label_option)
813 {
814 union block *label = find_next_block ();
815
816 if (!label)
817 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
818 quote (volume_label_option)));
819 if (!check_label_pattern (label))
820 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
821 quote_n (0, label->header.name),
822 quote_n (1, volume_label_option)));
823 }
824 break;
825
826 case ACCESS_WRITE:
827 if (volume_label_option)
828 {
829 memset (record_start, 0, BLOCKSIZE);
830 if (multi_volume_option)
831 sprintf (record_start->header.name, "%s Volume 1",
832 volume_label_option);
833 else
834 strcpy (record_start->header.name, volume_label_option);
835
836 assign_string (&current_file_name, record_start->header.name);
837
838 record_start->header.typeflag = GNUTYPE_VOLHDR;
839 TIME_TO_CHARS (start_time, record_start->header.mtime);
840 finish_header (record_start);
841 #if 0
842 current_block++;
843 #endif
844 }
845 break;
846 }
847 }
848
849 /* Perform a write to flush the buffer. */
850 void
851 flush_write (void)
852 {
853 int copy_back;
854 ssize_t status;
855
856 if (checkpoint_option && !(++checkpoint % 10))
857 WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
858
859 if (tape_length_option && tape_length_option <= bytes_written)
860 {
861 errno = ENOSPC;
862 status = 0;
863 }
864 else if (dev_null_output)
865 status = record_size;
866 else
867 status = write_archive_buffer ();
868 if (status != record_size && !multi_volume_option)
869 archive_write_error (status);
870
871 if (status > 0)
872 bytes_written += status;
873
874 if (status == record_size)
875 {
876 if (multi_volume_option)
877 {
878 char *cursor;
879
880 if (!save_name)
881 {
882 assign_string (&real_s_name, 0);
883 real_s_totsize = 0;
884 real_s_sizeleft = 0;
885 return;
886 }
887
888 cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
889 while (ISSLASH (*cursor))
890 cursor++;
891
892 assign_string (&real_s_name, cursor);
893 real_s_totsize = save_totsize;
894 real_s_sizeleft = save_sizeleft;
895 }
896 return;
897 }
898
899 /* We're multivol. Panic if we didn't get the right kind of response. */
900
901 /* ENXIO is for the UNIX PC. */
902 if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
903 archive_write_error (status);
904
905 /* If error indicates a short write, we just move to the next tape. */
906
907 if (!new_volume (ACCESS_WRITE))
908 return;
909
910 if (totals_option)
911 prev_written += bytes_written;
912 bytes_written = 0;
913
914 if (volume_label_option && real_s_name)
915 {
916 copy_back = 2;
917 record_start -= 2;
918 }
919 else if (volume_label_option || real_s_name)
920 {
921 copy_back = 1;
922 record_start--;
923 }
924 else
925 copy_back = 0;
926
927 if (volume_label_option)
928 {
929 memset (record_start, 0, BLOCKSIZE);
930 sprintf (record_start->header.name, "%s Volume %d",
931 volume_label_option, volno);
932 TIME_TO_CHARS (start_time, record_start->header.mtime);
933 record_start->header.typeflag = GNUTYPE_VOLHDR;
934 finish_header (record_start);
935 }
936
937 if (real_s_name)
938 {
939 int tmp;
940
941 if (volume_label_option)
942 record_start++;
943
944 memset (record_start, 0, BLOCKSIZE);
945
946 /* FIXME: Michael P Urban writes: [a long name file] is being written
947 when a new volume rolls around [...] Looks like the wrong value is
948 being preserved in real_s_name, though. */
949
950 strcpy (record_start->header.name, real_s_name);
951 record_start->header.typeflag = GNUTYPE_MULTIVOL;
952 OFF_TO_CHARS (real_s_sizeleft, record_start->header.size);
953 OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
954 record_start->oldgnu_header.offset);
955 tmp = verbose_option;
956 verbose_option = 0;
957 finish_header (record_start);
958 verbose_option = tmp;
959
960 if (volume_label_option)
961 record_start--;
962 }
963
964 status = write_archive_buffer ();
965 if (status != record_size)
966 archive_write_error (status);
967
968 bytes_written += status;
969
970 if (copy_back)
971 {
972 record_start += copy_back;
973 memcpy (current_block,
974 record_start + blocking_factor - copy_back,
975 copy_back * BLOCKSIZE);
976 current_block += copy_back;
977
978 if (real_s_sizeleft >= copy_back * BLOCKSIZE)
979 real_s_sizeleft -= copy_back * BLOCKSIZE;
980 else if ((real_s_sizeleft + BLOCKSIZE - 1) / BLOCKSIZE <= copy_back)
981 assign_string (&real_s_name, 0);
982 else
983 {
984 char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
985
986 while (ISSLASH (*cursor))
987 cursor++;
988
989 assign_string (&real_s_name, cursor);
990 real_s_sizeleft = save_sizeleft;
991 real_s_totsize = save_totsize;
992 }
993 copy_back = 0;
994 }
995 }
996
997 /* Handle write errors on the archive. Write errors are always fatal.
998 Hitting the end of a volume does not cause a write error unless the
999 write was the first record of the volume. */
1000 static void
1001 archive_write_error (ssize_t status)
1002 {
1003 /* It might be useful to know how much was written before the error
1004 occurred. */
1005 if (totals_option)
1006 {
1007 int e = errno;
1008 print_total_written ();
1009 errno = e;
1010 }
1011
1012 write_fatal_details (*archive_name_cursor, status, record_size);
1013 }
1014
1015 /* Handle read errors on the archive. If the read should be retried,
1016 return to the caller. */
1017 static void
1018 archive_read_error (void)
1019 {
1020 read_error (*archive_name_cursor);
1021
1022 if (record_start_block == 0)
1023 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1024
1025 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1026 then give up on reading the archive. */
1027
1028 if (read_error_count++ > READ_ERROR_MAX)
1029 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1030 return;
1031 }
1032
1033 /* Perform a read to flush the buffer. */
1034 void
1035 flush_read (void)
1036 {
1037 ssize_t status; /* result from system call */
1038 size_t left; /* bytes left */
1039 char *more; /* pointer to next byte to read */
1040
1041 if (checkpoint_option && !(++checkpoint % 10))
1042 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
1043
1044 /* Clear the count of errors. This only applies to a single call to
1045 flush_read. */
1046
1047 read_error_count = 0; /* clear error count */
1048
1049 if (write_archive_to_stdout && record_start_block != 0)
1050 {
1051 archive = STDOUT_FILENO;
1052 status = write_archive_buffer ();
1053 archive = STDIN_FILENO;
1054 if (status != record_size)
1055 archive_write_error (status);
1056 }
1057 if (multi_volume_option)
1058 {
1059 if (save_name)
1060 {
1061 char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
1062
1063 while (ISSLASH (*cursor))
1064 cursor++;
1065
1066 assign_string (&real_s_name, cursor);
1067 real_s_sizeleft = save_sizeleft;
1068 real_s_totsize = save_totsize;
1069 }
1070 else
1071 {
1072 assign_string (&real_s_name, 0);
1073 real_s_totsize = 0;
1074 real_s_sizeleft = 0;
1075 }
1076 }
1077
1078 error_loop:
1079 status = rmtread (archive, record_start->buffer, record_size);
1080 if (status == record_size)
1081 return;
1082
1083 if ((status == 0
1084 || (status < 0 && errno == ENOSPC)
1085 || (status > 0 && !read_full_records_option))
1086 && multi_volume_option)
1087 {
1088 union block *cursor;
1089
1090 try_volume:
1091 switch (subcommand_option)
1092 {
1093 case APPEND_SUBCOMMAND:
1094 case CAT_SUBCOMMAND:
1095 case UPDATE_SUBCOMMAND:
1096 if (!new_volume (ACCESS_UPDATE))
1097 return;
1098 break;
1099
1100 default:
1101 if (!new_volume (ACCESS_READ))
1102 return;
1103 break;
1104 }
1105
1106 vol_error:
1107 status = rmtread (archive, record_start->buffer, record_size);
1108 if (status < 0)
1109 {
1110 archive_read_error ();
1111 goto vol_error;
1112 }
1113 if (status != record_size)
1114 goto short_read;
1115
1116 cursor = record_start;
1117
1118 if (cursor->header.typeflag == GNUTYPE_VOLHDR)
1119 {
1120 if (volume_label_option)
1121 {
1122 if (!check_label_pattern (cursor))
1123 {
1124 WARN ((0, 0, _("Volume %s does not match %s"),
1125 quote_n (0, cursor->header.name),
1126 quote_n (1, volume_label_option)));
1127 volno--;
1128 global_volno--;
1129 goto try_volume;
1130 }
1131 }
1132 if (verbose_option)
1133 fprintf (stdlis, _("Reading %s\n"), quote (cursor->header.name));
1134 cursor++;
1135 }
1136 else if (volume_label_option)
1137 WARN ((0, 0, _("WARNING: No volume header")));
1138
1139 if (real_s_name)
1140 {
1141 uintmax_t s1, s2;
1142 if (cursor->header.typeflag != GNUTYPE_MULTIVOL
1143 || strcmp (cursor->header.name, real_s_name))
1144 {
1145 WARN ((0, 0, _("%s is not continued on this volume"),
1146 quote (real_s_name)));
1147 volno--;
1148 global_volno--;
1149 goto try_volume;
1150 }
1151 s1 = UINTMAX_FROM_HEADER (cursor->header.size);
1152 s2 = UINTMAX_FROM_HEADER (cursor->oldgnu_header.offset);
1153 if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
1154 {
1155 char totsizebuf[UINTMAX_STRSIZE_BOUND];
1156 char s1buf[UINTMAX_STRSIZE_BOUND];
1157 char s2buf[UINTMAX_STRSIZE_BOUND];
1158
1159 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1160 quote (cursor->header.name),
1161 STRINGIFY_BIGINT (save_totsize, totsizebuf),
1162 STRINGIFY_BIGINT (s1, s1buf),
1163 STRINGIFY_BIGINT (s2, s2buf)));
1164 volno--;
1165 global_volno--;
1166 goto try_volume;
1167 }
1168 if (real_s_totsize - real_s_sizeleft
1169 != OFF_FROM_HEADER (cursor->oldgnu_header.offset))
1170 {
1171 WARN ((0, 0, _("This volume is out of sequence")));
1172 volno--;
1173 global_volno--;
1174 goto try_volume;
1175 }
1176 cursor++;
1177 }
1178 current_block = cursor;
1179 return;
1180 }
1181 else if (status < 0)
1182 {
1183 archive_read_error ();
1184 goto error_loop; /* try again */
1185 }
1186
1187 short_read:
1188 more = record_start->buffer + status;
1189 left = record_size - status;
1190
1191 while (left % BLOCKSIZE != 0
1192 || (left && status && read_full_records_option))
1193 {
1194 if (status)
1195 while ((status = rmtread (archive, more, left)) < 0)
1196 archive_read_error ();
1197
1198 if (status == 0)
1199 {
1200 if (left % BLOCKSIZE != 0)
1201 ERROR ((0, 0, _("%d garbage bytes ignored at end of archive"),
1202 (int) ((record_size - left) % BLOCKSIZE)));
1203 break;
1204 }
1205
1206 if (! read_full_records_option)
1207 FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
1208 (unsigned long) (record_size - left)));
1209
1210 /* User warned us about this. Fix up. */
1211
1212 left -= status;
1213 more += status;
1214 }
1215
1216 /* FIXME: for size=0, multi-volume support. On the first record, warn
1217 about the problem. */
1218
1219 if (!read_full_records_option && verbose_option
1220 && record_start_block == 0 && status > 0)
1221 WARN ((0, 0, _("Record size = %lu blocks"),
1222 (unsigned long) ((record_size - left) / BLOCKSIZE)));
1223
1224 record_end = record_start + (record_size - left) / BLOCKSIZE;
1225 }
1226
1227 /* Flush the current buffer to/from the archive. */
1228 void
1229 flush_archive (void)
1230 {
1231 record_start_block += record_end - record_start;
1232 current_block = record_start;
1233 record_end = record_start + blocking_factor;
1234
1235 if (access_mode == ACCESS_READ && time_to_start_writing)
1236 {
1237 access_mode = ACCESS_WRITE;
1238 time_to_start_writing = 0;
1239
1240 if (file_to_switch_to >= 0)
1241 {
1242 if (rmtclose (archive) != 0)
1243 close_warn (*archive_name_cursor);
1244
1245 archive = file_to_switch_to;
1246 }
1247 else
1248 backspace_output ();
1249 }
1250
1251 switch (access_mode)
1252 {
1253 case ACCESS_READ:
1254 flush_read ();
1255 break;
1256
1257 case ACCESS_WRITE:
1258 flush_write ();
1259 break;
1260
1261 case ACCESS_UPDATE:
1262 abort ();
1263 }
1264 }
1265
1266 /* Backspace the archive descriptor by one record worth. If it's a
1267 tape, MTIOCTOP will work. If it's something else, try to seek on
1268 it. If we can't seek, we lose! */
1269 static void
1270 backspace_output (void)
1271 {
1272 #ifdef MTIOCTOP
1273 {
1274 struct mtop operation;
1275
1276 operation.mt_op = MTBSR;
1277 operation.mt_count = 1;
1278 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1279 return;
1280 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1281 return;
1282 }
1283 #endif
1284
1285 {
1286 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
1287
1288 /* Seek back to the beginning of this record and start writing there. */
1289
1290 position -= record_size;
1291 if (position < 0)
1292 position = 0;
1293 if (rmtlseek (archive, position, SEEK_SET) != position)
1294 {
1295 /* Lseek failed. Try a different method. */
1296
1297 WARN ((0, 0,
1298 _("Cannot backspace archive file; it may be unreadable without -i")));
1299
1300 /* Replace the first part of the record with NULs. */
1301
1302 if (record_start->buffer != output_start)
1303 memset (record_start->buffer, 0,
1304 output_start - record_start->buffer);
1305 }
1306 }
1307 }
1308
1309 /* Close the archive file. */
1310 void
1311 close_archive (void)
1312 {
1313 if (time_to_start_writing || access_mode == ACCESS_WRITE)
1314 flush_archive ();
1315
1316 #if !MSDOS
1317
1318 /* Manage to fully drain a pipe we might be reading, so to not break it on
1319 the producer after the EOF block. FIXME: one of these days, GNU tar
1320 might become clever enough to just stop working, once there is no more
1321 work to do, we might have to revise this area in such time. */
1322
1323 if (access_mode == ACCESS_READ
1324 && ! _isrmt (archive)
1325 && S_ISFIFO (archive_stat.st_mode))
1326 while (rmtread (archive, record_start->buffer, record_size) > 0)
1327 continue;
1328 #endif
1329
1330 if (verify_option)
1331 verify_volume ();
1332
1333 if (rmtclose (archive) != 0)
1334 close_warn (*archive_name_cursor);
1335
1336 #if !MSDOS
1337
1338 if (child_pid)
1339 {
1340 int wait_status;
1341
1342 while (waitpid (child_pid, &wait_status, 0) == -1)
1343 if (errno != EINTR)
1344 {
1345 waitpid_error (use_compress_program_option);
1346 break;
1347 }
1348
1349 if (WIFSIGNALED (wait_status))
1350 ERROR ((0, 0, _("Child died with signal %d"),
1351 WTERMSIG (wait_status)));
1352 else if (WEXITSTATUS (wait_status) != 0)
1353 ERROR ((0, 0, _("Child returned status %d"),
1354 WEXITSTATUS (wait_status)));
1355 }
1356 #endif /* !MSDOS */
1357
1358 if (current_file_name)
1359 free (current_file_name);
1360 if (current_link_name)
1361 free (current_link_name);
1362 if (save_name)
1363 free (save_name);
1364 if (real_s_name)
1365 free (real_s_name);
1366 free (multi_volume_option ? record_start - 2 : record_start);
1367 }
1368
1369 /* Called to initialize the global volume number. */
1370 void
1371 init_volume_number (void)
1372 {
1373 FILE *file = fopen (volno_file_option, "r");
1374
1375 if (file)
1376 {
1377 if (fscanf (file, "%d", &global_volno) != 1
1378 || global_volno < 0)
1379 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1380 quotearg_colon (volno_file_option)));
1381 if (ferror (file))
1382 read_error (volno_file_option);
1383 if (fclose (file) != 0)
1384 close_error (volno_file_option);
1385 }
1386 else if (errno != ENOENT)
1387 open_error (volno_file_option);
1388 }
1389
1390 /* Called to write out the closing global volume number. */
1391 void
1392 closeout_volume_number (void)
1393 {
1394 FILE *file = fopen (volno_file_option, "w");
1395
1396 if (file)
1397 {
1398 fprintf (file, "%d\n", global_volno);
1399 if (ferror (file))
1400 write_error (volno_file_option);
1401 if (fclose (file) != 0)
1402 close_error (volno_file_option);
1403 }
1404 else
1405 open_error (volno_file_option);
1406 }
1407
1408 /* We've hit the end of the old volume. Close it and open the next one.
1409 Return nonzero on success. */
1410 static int
1411 new_volume (enum access_mode access)
1412 {
1413 static FILE *read_file;
1414 static int looped;
1415
1416 if (!read_file && !info_script_option)
1417 /* FIXME: if fopen is used, it will never be closed. */
1418 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
1419
1420 if (now_verifying)
1421 return 0;
1422 if (verify_option)
1423 verify_volume ();
1424
1425 if (rmtclose (archive) != 0)
1426 close_warn (*archive_name_cursor);
1427
1428 global_volno++;
1429 if (global_volno < 0)
1430 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1431 volno++;
1432 archive_name_cursor++;
1433 if (archive_name_cursor == archive_name_array + archive_names)
1434 {
1435 archive_name_cursor = archive_name_array;
1436 looped = 1;
1437 }
1438
1439 tryagain:
1440 if (looped)
1441 {
1442 /* We have to prompt from now on. */
1443
1444 if (info_script_option)
1445 {
1446 if (volno_file_option)
1447 closeout_volume_number ();
1448 if (system (info_script_option) != 0)
1449 FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option));
1450 }
1451 else
1452 while (1)
1453 {
1454 char input_buffer[80];
1455
1456 fputc ('\007', stderr);
1457 fprintf (stderr,
1458 _("Prepare volume #%d for %s and hit return: "),
1459 global_volno, quote (*archive_name_cursor));
1460 fflush (stderr);
1461
1462 if (fgets (input_buffer, sizeof input_buffer, read_file) == 0)
1463 {
1464 WARN ((0, 0, _("EOF where user reply was expected")));
1465
1466 if (subcommand_option != EXTRACT_SUBCOMMAND
1467 && subcommand_option != LIST_SUBCOMMAND
1468 && subcommand_option != DIFF_SUBCOMMAND)
1469 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1470
1471 fatal_exit ();
1472 }
1473 if (input_buffer[0] == '\n'
1474 || input_buffer[0] == 'y'
1475 || input_buffer[0] == 'Y')
1476 break;
1477
1478 switch (input_buffer[0])
1479 {
1480 case '?':
1481 {
1482 fprintf (stderr, _("\
1483 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1484 q Abort tar\n\
1485 ! Spawn a subshell\n\
1486 ? Print this list\n"));
1487 }
1488 break;
1489
1490 case 'q':
1491 /* Quit. */
1492
1493 WARN ((0, 0, _("No new volume; exiting.\n")));
1494
1495 if (subcommand_option != EXTRACT_SUBCOMMAND
1496 && subcommand_option != LIST_SUBCOMMAND
1497 && subcommand_option != DIFF_SUBCOMMAND)
1498 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1499
1500 fatal_exit ();
1501
1502 case 'n':
1503 /* Get new file name. */
1504
1505 {
1506 char *name = &input_buffer[1];
1507 char *cursor;
1508
1509 while (*name == ' ' || *name == '\t')
1510 name++;
1511 cursor = name;
1512 while (*cursor && *cursor != '\n')
1513 cursor++;
1514 *cursor = '\0';
1515
1516 /* FIXME: the following allocation is never reclaimed. */
1517 *archive_name_cursor = xstrdup (name);
1518 }
1519 break;
1520
1521 case '!':
1522 #if MSDOS
1523 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
1524 #else /* not MSDOS */
1525 {
1526 pid_t child;
1527 const char *shell = getenv ("SHELL");
1528 if (! shell)
1529 shell = "/bin/sh";
1530 child = xfork ();
1531 if (child == 0)
1532 {
1533 execlp (shell, "-sh", "-i", 0);
1534 exec_fatal (shell);
1535 }
1536 else
1537 {
1538 int wait_status;
1539 while (waitpid (child, &wait_status, 0) == -1)
1540 if (errno != EINTR)
1541 {
1542 waitpid_error (shell);
1543 break;
1544 }
1545 }
1546 }
1547 #endif /* not MSDOS */
1548 break;
1549 }
1550 }
1551 }
1552
1553 if (verify_option)
1554 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1555 rsh_command_option);
1556 else
1557 switch (access)
1558 {
1559 case ACCESS_READ:
1560 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1561 rsh_command_option);
1562 break;
1563
1564 case ACCESS_WRITE:
1565 if (backup_option)
1566 maybe_backup_file (*archive_name_cursor, 1);
1567 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1568 rsh_command_option);
1569 break;
1570
1571 case ACCESS_UPDATE:
1572 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1573 rsh_command_option);
1574 break;
1575 }
1576
1577 if (archive < 0)
1578 {
1579 open_warn (*archive_name_cursor);
1580 if (!verify_option && access == ACCESS_WRITE && backup_option)
1581 undo_last_backup ();
1582 goto tryagain;
1583 }
1584
1585 #if MSDOS
1586 setmode (archive, O_BINARY);
1587 #endif
1588
1589 return 1;
1590 }
This page took 0.099527 seconds and 5 git commands to generate.