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