]> Dogcows Code - chaz/tar/blob - src/buffer.c
Use ngettext where appropriate.
[chaz/tar] / src / buffer.c
1 /* Buffer management for tar.
2
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003 Free Software Foundation, Inc.
5
6 Written by John Gilmore, on 1985-08-25.
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "system.h"
23
24 #include <signal.h>
25
26 #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. */
70 FILE *stdlis;
71
72 static void backspace_output (void);
73 static int new_volume (enum access_mode);
74 static void archive_write_error (ssize_t) __attribute__ ((noreturn));
75 static void archive_read_error (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 bool 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 int human_opts = human_autoscale | human_base_1024 | human_SI | human_B;
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 (%s, %s/s)\n"), bytes,
154 human_readable (written, abbr, human_opts, 1, 1),
155 (0 < seconds && written / seconds < (uintmax_t) -1
156 ? human_readable (written / seconds, rate, human_opts, 1, 1)
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 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 if (index_file_name)
658 {
659 stdlis = fopen (index_file_name, "w");
660 if (! stdlis)
661 open_error (index_file_name);
662 }
663 else
664 stdlis = to_stdout_option ? stderr : stdout;
665
666 if (record_size == 0)
667 FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
668
669 if (archive_names == 0)
670 FATAL_ERROR ((0, 0, _("No archive name given")));
671
672 destroy_stat (&current_stat_info);
673 save_name = 0;
674 real_s_name = 0;
675
676 if (multi_volume_option)
677 {
678 if (verify_option)
679 FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
680 record_start = valloc (record_size + (2 * BLOCKSIZE));
681 if (record_start)
682 record_start += 2;
683 }
684 else
685 record_start = valloc (record_size);
686 if (!record_start)
687 FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
688 blocking_factor));
689
690 current_block = record_start;
691 record_end = record_start + blocking_factor;
692 /* When updating the archive, we start with reading. */
693 access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
694
695 if (use_compress_program_option)
696 {
697 if (multi_volume_option)
698 FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
699 if (verify_option)
700 FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
701
702 switch (wanted_access)
703 {
704 case ACCESS_READ:
705 child_open_for_uncompress ();
706 break;
707
708 case ACCESS_WRITE:
709 child_open_for_compress ();
710 break;
711
712 case ACCESS_UPDATE:
713 FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
714 break;
715 }
716
717 if (wanted_access == ACCESS_WRITE
718 && strcmp (archive_name_array[0], "-") == 0)
719 stdlis = stderr;
720 }
721 else if (strcmp (archive_name_array[0], "-") == 0)
722 {
723 read_full_records_option = 1; /* could be a pipe, be safe */
724 if (verify_option)
725 FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
726
727 switch (wanted_access)
728 {
729 case ACCESS_READ:
730 archive = STDIN_FILENO;
731 break;
732
733 case ACCESS_WRITE:
734 archive = STDOUT_FILENO;
735 stdlis = stderr;
736 break;
737
738 case ACCESS_UPDATE:
739 archive = STDIN_FILENO;
740 stdlis = stderr;
741 write_archive_to_stdout = 1;
742 break;
743 }
744 }
745 else if (verify_option)
746 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
747 MODE_RW, rsh_command_option);
748 else
749 switch (wanted_access)
750 {
751 case ACCESS_READ:
752 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
753 MODE_RW, rsh_command_option);
754 break;
755
756 case ACCESS_WRITE:
757 if (backup_option)
758 {
759 maybe_backup_file (archive_name_array[0], 1);
760 backed_up_flag = 1;
761 }
762 archive = rmtcreat (archive_name_array[0], MODE_RW,
763 rsh_command_option);
764 break;
765
766 case ACCESS_UPDATE:
767 archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
768 MODE_RW, rsh_command_option);
769 break;
770 }
771
772 if (archive < 0
773 || (! _isrmt (archive) && fstat (archive, &archive_stat) < 0))
774 {
775 int saved_errno = errno;
776
777 if (backed_up_flag)
778 undo_last_backup ();
779 errno = saved_errno;
780 open_fatal (archive_name_array[0]);
781 }
782
783 #if !MSDOS
784
785 /* Detect if outputting to "/dev/null". */
786 {
787 static char const dev_null[] = "/dev/null";
788 struct stat dev_null_stat;
789
790 dev_null_output =
791 (strcmp (archive_name_array[0], dev_null) == 0
792 || (! _isrmt (archive)
793 && S_ISCHR (archive_stat.st_mode)
794 && stat (dev_null, &dev_null_stat) == 0
795 && archive_stat.st_dev == dev_null_stat.st_dev
796 && archive_stat.st_ino == dev_null_stat.st_ino));
797 }
798
799 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
800 {
801 ar_dev = archive_stat.st_dev;
802 ar_ino = archive_stat.st_ino;
803 }
804 else
805 ar_dev = 0;
806
807 #endif /* not MSDOS */
808
809 #if MSDOS
810 setmode (archive, O_BINARY);
811 #endif
812
813 switch (wanted_access)
814 {
815 case ACCESS_UPDATE:
816 records_written = 0;
817 case ACCESS_READ:
818 records_read = 0;
819 record_end = record_start; /* set up for 1st record = # 0 */
820 find_next_block (); /* read it in, check for EOF */
821
822 if (volume_label_option)
823 {
824 union block *label = find_next_block ();
825
826 if (!label)
827 FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
828 quote (volume_label_option)));
829 if (!check_label_pattern (label))
830 FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
831 quote_n (0, label->header.name),
832 quote_n (1, volume_label_option)));
833 }
834 break;
835
836 case ACCESS_WRITE:
837 records_written = 0;
838 if (volume_label_option)
839 {
840 memset (record_start, 0, BLOCKSIZE);
841 if (multi_volume_option)
842 sprintf (record_start->header.name, "%s Volume 1",
843 volume_label_option);
844 else
845 strcpy (record_start->header.name, volume_label_option);
846
847 assign_string (&current_stat_info.file_name, record_start->header.name);
848 current_stat_info.had_trailing_slash = strip_trailing_slashes (current_stat_info.file_name);
849
850 record_start->header.typeflag = GNUTYPE_VOLHDR;
851 TIME_TO_CHARS (start_time, record_start->header.mtime);
852 finish_header (record_start, -1);
853 #if 0
854 current_block++;
855 #endif
856 }
857 break;
858 }
859 }
860
861 /* Perform a write to flush the buffer. */
862 void
863 flush_write (void)
864 {
865 int copy_back;
866 ssize_t status;
867
868 if (checkpoint_option && !(++checkpoint % 10))
869 WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
870
871 if (tape_length_option && tape_length_option <= bytes_written)
872 {
873 errno = ENOSPC;
874 status = 0;
875 }
876 else if (dev_null_output)
877 status = record_size;
878 else
879 status = write_archive_buffer ();
880 if (status != record_size && !multi_volume_option)
881 archive_write_error (status);
882
883 if (status > 0)
884 {
885 records_written++;
886 bytes_written += status;
887 }
888
889 if (status == record_size)
890 {
891 if (multi_volume_option)
892 {
893 if (save_name)
894 {
895 assign_string (&real_s_name, safer_name_suffix (save_name, 0));
896 real_s_totsize = save_totsize;
897 real_s_sizeleft = save_sizeleft;
898 }
899 else
900 {
901 assign_string (&real_s_name, 0);
902 real_s_totsize = 0;
903 real_s_sizeleft = 0;
904 }
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, -1);
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, -1);
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 assign_string (&real_s_name, safer_name_suffix (save_name, 0));
995 real_s_sizeleft = save_sizeleft;
996 real_s_totsize = save_totsize;
997 }
998 copy_back = 0;
999 }
1000 }
1001
1002 /* Handle write errors on the archive. Write errors are always fatal.
1003 Hitting the end of a volume does not cause a write error unless the
1004 write was the first record of the volume. */
1005 static void
1006 archive_write_error (ssize_t status)
1007 {
1008 /* It might be useful to know how much was written before the error
1009 occurred. */
1010 if (totals_option)
1011 {
1012 int e = errno;
1013 print_total_written ();
1014 errno = e;
1015 }
1016
1017 write_fatal_details (*archive_name_cursor, status, record_size);
1018 }
1019
1020 /* Handle read errors on the archive. If the read should be retried,
1021 return to the caller. */
1022 static void
1023 archive_read_error (void)
1024 {
1025 read_error (*archive_name_cursor);
1026
1027 if (record_start_block == 0)
1028 FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
1029
1030 /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
1031 then give up on reading the archive. */
1032
1033 if (read_error_count++ > READ_ERROR_MAX)
1034 FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
1035 return;
1036 }
1037
1038 static void
1039 short_read (ssize_t status)
1040 {
1041 size_t left; /* bytes left */
1042 char *more; /* pointer to next byte to read */
1043
1044 more = record_start->buffer + status;
1045 left = record_size - status;
1046
1047 while (left % BLOCKSIZE != 0
1048 || (left && status && read_full_records_option))
1049 {
1050 if (status)
1051 while ((status = rmtread (archive, more, left)) < 0)
1052 archive_read_error ();
1053
1054 if (status == 0)
1055 break;
1056
1057 if (! read_full_records_option)
1058 {
1059 unsigned long rest = record_size - left;
1060
1061 FATAL_ERROR ((0, 0,
1062 ngettext ("Unaligned block (%lu byte) in archive",
1063 "Unaligned block (%lu bytes) in archive",
1064 rest),
1065 rest));
1066 }
1067
1068 /* User warned us about this. Fix up. */
1069
1070 left -= status;
1071 more += status;
1072 }
1073
1074 /* FIXME: for size=0, multi-volume support. On the first record, warn
1075 about the problem. */
1076
1077 if (!read_full_records_option && verbose_option
1078 && record_start_block == 0 && status > 0)
1079 {
1080 unsigned long rsize = (record_size - left) / BLOCKSIZE;
1081 WARN ((0, 0,
1082 ngettext ("Record size = %lu block",
1083 "Record size = %lu blocks",
1084 rsize),
1085 rsize));
1086 }
1087
1088 record_end = record_start + (record_size - left) / BLOCKSIZE;
1089 records_read++;
1090 }
1091
1092 /* Perform a read to flush the buffer. */
1093 void
1094 flush_read (void)
1095 {
1096 ssize_t status; /* result from system call */
1097
1098 if (checkpoint_option && !(++checkpoint % 10))
1099 WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
1100
1101 /* Clear the count of errors. This only applies to a single call to
1102 flush_read. */
1103
1104 read_error_count = 0; /* clear error count */
1105
1106 if (write_archive_to_stdout && record_start_block != 0)
1107 {
1108 archive = STDOUT_FILENO;
1109 status = write_archive_buffer ();
1110 archive = STDIN_FILENO;
1111 if (status != record_size)
1112 archive_write_error (status);
1113 }
1114 if (multi_volume_option)
1115 {
1116 if (save_name)
1117 {
1118 assign_string (&real_s_name, safer_name_suffix (save_name, 0));
1119 real_s_sizeleft = save_sizeleft;
1120 real_s_totsize = save_totsize;
1121 }
1122 else
1123 {
1124 assign_string (&real_s_name, 0);
1125 real_s_totsize = 0;
1126 real_s_sizeleft = 0;
1127 }
1128 }
1129
1130 error_loop:
1131 status = rmtread (archive, record_start->buffer, record_size);
1132 if (status == record_size)
1133 {
1134 records_read++;
1135 return;
1136 }
1137
1138 if ((status == 0
1139 || (status < 0 && errno == ENOSPC)
1140 || (status > 0 && !read_full_records_option))
1141 && multi_volume_option)
1142 {
1143 union block *cursor;
1144
1145 try_volume:
1146 switch (subcommand_option)
1147 {
1148 case APPEND_SUBCOMMAND:
1149 case CAT_SUBCOMMAND:
1150 case UPDATE_SUBCOMMAND:
1151 if (!new_volume (ACCESS_UPDATE))
1152 return;
1153 break;
1154
1155 default:
1156 if (!new_volume (ACCESS_READ))
1157 return;
1158 break;
1159 }
1160
1161 vol_error:
1162 status = rmtread (archive, record_start->buffer, record_size);
1163 if (status < 0)
1164 {
1165 archive_read_error ();
1166 goto vol_error;
1167 }
1168 if (status != record_size)
1169 short_read (status);
1170
1171 cursor = record_start;
1172
1173 if (cursor->header.typeflag == GNUTYPE_VOLHDR)
1174 {
1175 if (volume_label_option)
1176 {
1177 if (!check_label_pattern (cursor))
1178 {
1179 WARN ((0, 0, _("Volume %s does not match %s"),
1180 quote_n (0, cursor->header.name),
1181 quote_n (1, volume_label_option)));
1182 volno--;
1183 global_volno--;
1184 goto try_volume;
1185 }
1186 }
1187 if (verbose_option)
1188 fprintf (stdlis, _("Reading %s\n"), quote (cursor->header.name));
1189 cursor++;
1190 }
1191 else if (volume_label_option)
1192 WARN ((0, 0, _("WARNING: No volume header")));
1193
1194 if (real_s_name)
1195 {
1196 uintmax_t s1, s2;
1197 if (cursor->header.typeflag != GNUTYPE_MULTIVOL
1198 || strcmp (cursor->header.name, real_s_name))
1199 {
1200 WARN ((0, 0, _("%s is not continued on this volume"),
1201 quote (real_s_name)));
1202 volno--;
1203 global_volno--;
1204 goto try_volume;
1205 }
1206 s1 = UINTMAX_FROM_HEADER (cursor->header.size);
1207 s2 = UINTMAX_FROM_HEADER (cursor->oldgnu_header.offset);
1208 if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
1209 {
1210 char totsizebuf[UINTMAX_STRSIZE_BOUND];
1211 char s1buf[UINTMAX_STRSIZE_BOUND];
1212 char s2buf[UINTMAX_STRSIZE_BOUND];
1213
1214 WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
1215 quote (cursor->header.name),
1216 STRINGIFY_BIGINT (save_totsize, totsizebuf),
1217 STRINGIFY_BIGINT (s1, s1buf),
1218 STRINGIFY_BIGINT (s2, s2buf)));
1219 volno--;
1220 global_volno--;
1221 goto try_volume;
1222 }
1223 if (real_s_totsize - real_s_sizeleft
1224 != OFF_FROM_HEADER (cursor->oldgnu_header.offset))
1225 {
1226 WARN ((0, 0, _("This volume is out of sequence")));
1227 volno--;
1228 global_volno--;
1229 goto try_volume;
1230 }
1231 cursor++;
1232 }
1233 current_block = cursor;
1234 records_read++;
1235 return;
1236 }
1237 else if (status < 0)
1238 {
1239 archive_read_error ();
1240 goto error_loop; /* try again */
1241 }
1242
1243 short_read (status);
1244 }
1245
1246 /* Flush the current buffer to/from the archive. */
1247 void
1248 flush_archive (void)
1249 {
1250 record_start_block += record_end - record_start;
1251 current_block = record_start;
1252 record_end = record_start + blocking_factor;
1253
1254 if (access_mode == ACCESS_READ && time_to_start_writing)
1255 {
1256 access_mode = ACCESS_WRITE;
1257 time_to_start_writing = 0;
1258
1259 if (file_to_switch_to >= 0)
1260 {
1261 if (rmtclose (archive) != 0)
1262 close_warn (*archive_name_cursor);
1263
1264 archive = file_to_switch_to;
1265 }
1266 else
1267 backspace_output ();
1268 }
1269
1270 switch (access_mode)
1271 {
1272 case ACCESS_READ:
1273 flush_read ();
1274 break;
1275
1276 case ACCESS_WRITE:
1277 flush_write ();
1278 break;
1279
1280 case ACCESS_UPDATE:
1281 abort ();
1282 }
1283 }
1284
1285 /* Backspace the archive descriptor by one record worth. If it's a
1286 tape, MTIOCTOP will work. If it's something else, try to seek on
1287 it. If we can't seek, we lose! */
1288 static void
1289 backspace_output (void)
1290 {
1291 #ifdef MTIOCTOP
1292 {
1293 struct mtop operation;
1294
1295 operation.mt_op = MTBSR;
1296 operation.mt_count = 1;
1297 if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1298 return;
1299 if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
1300 return;
1301 }
1302 #endif
1303
1304 {
1305 off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
1306
1307 /* Seek back to the beginning of this record and start writing there. */
1308
1309 position -= record_size;
1310 if (position < 0)
1311 position = 0;
1312 if (rmtlseek (archive, position, SEEK_SET) != position)
1313 {
1314 /* Lseek failed. Try a different method. */
1315
1316 WARN ((0, 0,
1317 _("Cannot backspace archive file; it may be unreadable without -i")));
1318
1319 /* Replace the first part of the record with NULs. */
1320
1321 if (record_start->buffer != output_start)
1322 memset (record_start->buffer, 0,
1323 output_start - record_start->buffer);
1324 }
1325 }
1326 }
1327
1328 /* Close the archive file. */
1329 void
1330 close_archive (void)
1331 {
1332 if (time_to_start_writing || access_mode == ACCESS_WRITE)
1333 flush_archive ();
1334
1335 #if !MSDOS
1336
1337 /* Manage to fully drain a pipe we might be reading, so to not break it on
1338 the producer after the EOF block. FIXME: one of these days, GNU tar
1339 might become clever enough to just stop working, once there is no more
1340 work to do, we might have to revise this area in such time. */
1341
1342 if (access_mode == ACCESS_READ
1343 && ! _isrmt (archive)
1344 && (S_ISFIFO (archive_stat.st_mode) || S_ISSOCK (archive_stat.st_mode)))
1345 while (rmtread (archive, record_start->buffer, record_size) > 0)
1346 continue;
1347 #endif
1348
1349 if (verify_option)
1350 verify_volume ();
1351
1352 if (rmtclose (archive) != 0)
1353 close_warn (*archive_name_cursor);
1354
1355 #if !MSDOS
1356
1357 if (child_pid)
1358 {
1359 int wait_status;
1360
1361 while (waitpid (child_pid, &wait_status, 0) == -1)
1362 if (errno != EINTR)
1363 {
1364 waitpid_error (use_compress_program_option);
1365 break;
1366 }
1367
1368 if (WIFSIGNALED (wait_status))
1369 ERROR ((0, 0, _("Child died with signal %d"),
1370 WTERMSIG (wait_status)));
1371 else if (WEXITSTATUS (wait_status) != 0)
1372 ERROR ((0, 0, _("Child returned status %d"),
1373 WEXITSTATUS (wait_status)));
1374 }
1375 #endif /* !MSDOS */
1376
1377 destroy_stat (&current_stat_info);
1378 if (save_name)
1379 free (save_name);
1380 if (real_s_name)
1381 free (real_s_name);
1382 free (multi_volume_option ? record_start - 2 : record_start);
1383 }
1384
1385 /* Called to initialize the global volume number. */
1386 void
1387 init_volume_number (void)
1388 {
1389 FILE *file = fopen (volno_file_option, "r");
1390
1391 if (file)
1392 {
1393 if (fscanf (file, "%d", &global_volno) != 1
1394 || global_volno < 0)
1395 FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
1396 quotearg_colon (volno_file_option)));
1397 if (ferror (file))
1398 read_error (volno_file_option);
1399 if (fclose (file) != 0)
1400 close_error (volno_file_option);
1401 }
1402 else if (errno != ENOENT)
1403 open_error (volno_file_option);
1404 }
1405
1406 /* Called to write out the closing global volume number. */
1407 void
1408 closeout_volume_number (void)
1409 {
1410 FILE *file = fopen (volno_file_option, "w");
1411
1412 if (file)
1413 {
1414 fprintf (file, "%d\n", global_volno);
1415 if (ferror (file))
1416 write_error (volno_file_option);
1417 if (fclose (file) != 0)
1418 close_error (volno_file_option);
1419 }
1420 else
1421 open_error (volno_file_option);
1422 }
1423
1424 /* We've hit the end of the old volume. Close it and open the next one.
1425 Return nonzero on success. */
1426 static int
1427 new_volume (enum access_mode access)
1428 {
1429 static FILE *read_file;
1430 static int looped;
1431
1432 if (!read_file && !info_script_option)
1433 /* FIXME: if fopen is used, it will never be closed. */
1434 read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
1435
1436 if (now_verifying)
1437 return 0;
1438 if (verify_option)
1439 verify_volume ();
1440
1441 if (rmtclose (archive) != 0)
1442 close_warn (*archive_name_cursor);
1443
1444 global_volno++;
1445 if (global_volno < 0)
1446 FATAL_ERROR ((0, 0, _("Volume number overflow")));
1447 volno++;
1448 archive_name_cursor++;
1449 if (archive_name_cursor == archive_name_array + archive_names)
1450 {
1451 archive_name_cursor = archive_name_array;
1452 looped = 1;
1453 }
1454
1455 tryagain:
1456 if (looped)
1457 {
1458 /* We have to prompt from now on. */
1459
1460 if (info_script_option)
1461 {
1462 if (volno_file_option)
1463 closeout_volume_number ();
1464 if (system (info_script_option) != 0)
1465 FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option));
1466 }
1467 else
1468 while (1)
1469 {
1470 char input_buffer[80];
1471
1472 fputc ('\007', stderr);
1473 fprintf (stderr,
1474 _("Prepare volume #%d for %s and hit return: "),
1475 global_volno, quote (*archive_name_cursor));
1476 fflush (stderr);
1477
1478 if (fgets (input_buffer, sizeof input_buffer, read_file) == 0)
1479 {
1480 WARN ((0, 0, _("EOF where user reply was expected")));
1481
1482 if (subcommand_option != EXTRACT_SUBCOMMAND
1483 && subcommand_option != LIST_SUBCOMMAND
1484 && subcommand_option != DIFF_SUBCOMMAND)
1485 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1486
1487 fatal_exit ();
1488 }
1489 if (input_buffer[0] == '\n'
1490 || input_buffer[0] == 'y'
1491 || input_buffer[0] == 'Y')
1492 break;
1493
1494 switch (input_buffer[0])
1495 {
1496 case '?':
1497 {
1498 fprintf (stderr, _("\
1499 n [name] Give a new file name for the next (and subsequent) volume(s)\n\
1500 q Abort tar\n\
1501 ! Spawn a subshell\n\
1502 ? Print this list\n"));
1503 }
1504 break;
1505
1506 case 'q':
1507 /* Quit. */
1508
1509 WARN ((0, 0, _("No new volume; exiting.\n")));
1510
1511 if (subcommand_option != EXTRACT_SUBCOMMAND
1512 && subcommand_option != LIST_SUBCOMMAND
1513 && subcommand_option != DIFF_SUBCOMMAND)
1514 WARN ((0, 0, _("WARNING: Archive is incomplete")));
1515
1516 fatal_exit ();
1517
1518 case 'n':
1519 /* Get new file name. */
1520
1521 {
1522 char *name = &input_buffer[1];
1523 char *cursor;
1524
1525 while (*name == ' ' || *name == '\t')
1526 name++;
1527 cursor = name;
1528 while (*cursor && *cursor != '\n')
1529 cursor++;
1530 *cursor = '\0';
1531
1532 /* FIXME: the following allocation is never reclaimed. */
1533 *archive_name_cursor = xstrdup (name);
1534 }
1535 break;
1536
1537 case '!':
1538 #if MSDOS
1539 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
1540 #else /* not MSDOS */
1541 {
1542 pid_t child;
1543 const char *shell = getenv ("SHELL");
1544 if (! shell)
1545 shell = "/bin/sh";
1546 child = xfork ();
1547 if (child == 0)
1548 {
1549 execlp (shell, "-sh", "-i", 0);
1550 exec_fatal (shell);
1551 }
1552 else
1553 {
1554 int wait_status;
1555 while (waitpid (child, &wait_status, 0) == -1)
1556 if (errno != EINTR)
1557 {
1558 waitpid_error (shell);
1559 break;
1560 }
1561 }
1562 }
1563 #endif /* not MSDOS */
1564 break;
1565 }
1566 }
1567 }
1568
1569 if (strcmp (archive_name_cursor[0], "-") == 0)
1570 {
1571 read_full_records_option = true;
1572 archive = STDIN_FILENO;
1573 }
1574 else if (verify_option)
1575 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1576 rsh_command_option);
1577 else
1578 switch (access)
1579 {
1580 case ACCESS_READ:
1581 archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
1582 rsh_command_option);
1583 break;
1584
1585 case ACCESS_WRITE:
1586 if (backup_option)
1587 maybe_backup_file (*archive_name_cursor, 1);
1588 archive = rmtcreat (*archive_name_cursor, MODE_RW,
1589 rsh_command_option);
1590 break;
1591
1592 case ACCESS_UPDATE:
1593 archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
1594 rsh_command_option);
1595 break;
1596 }
1597
1598 if (archive < 0)
1599 {
1600 open_warn (*archive_name_cursor);
1601 if (!verify_option && access == ACCESS_WRITE && backup_option)
1602 undo_last_backup ();
1603 goto tryagain;
1604 }
1605
1606 #if MSDOS
1607 setmode (archive, O_BINARY);
1608 #endif
1609
1610 return 1;
1611 }
1612
This page took 0.099448 seconds and 5 git commands to generate.