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