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