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