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