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