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