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