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