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