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