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