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