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