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