]> git.wh0rd.org Git - dump.git/blob - restore/tape.c
Cleanups.
[dump.git] / restore / tape.c
1 /*
2  *      Ported to Linux's Second Extended File System as part of the
3  *      dump and restore backup suit
4  *      Remy Card <card@Linux.EU.Org>, 1994-1997
5  *      Stelian Pop <pop@noos.fr>, 1999-2000
6  *      Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
7  */
8
9 /*
10  * Copyright (c) 1983, 1993
11  *      The Regents of the University of California.  All rights reserved.
12  * (c) UNIX System Laboratories, Inc.
13  * All or some portions of this file are derived from material licensed
14  * to the University of California by American Telephone and Telegraph
15  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16  * the permission of UNIX System Laboratories, Inc.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. All advertising materials mentioning features or use of this software
27  *    must display the following acknowledgement:
28  *      This product includes software developed by the University of
29  *      California, Berkeley and its contributors.
30  * 4. Neither the name of the University nor the names of its contributors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  */
46
47 #ifndef lint
48 static const char rcsid[] =
49         "$Id: tape.c,v 1.36 2001/04/27 12:23:23 stelian Exp $";
50 #endif /* not lint */
51
52 #include <config.h>
53 #include <errno.h>
54 #include <compaterr.h>
55 #include <setjmp.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include <sys/param.h>
62 #include <sys/file.h>
63 #include <sys/mtio.h>
64 #include <sys/stat.h>
65
66 #ifdef  __linux__
67 #include <sys/time.h>
68 #include <time.h>
69 #include <linux/ext2_fs.h>
70 #include <ext2fs/ext2fs.h>
71 #include <bsdcompat.h>
72 #else   /* __linux__ */
73 #include <ufs/ufs/dinode.h>
74 #endif  /* __linux__ */
75 #include <protocols/dumprestore.h>
76
77 #ifdef HAVE_ZLIB
78 #include <zlib.h>
79 #endif /* HAVE_ZLIB */
80
81 #include "restore.h"
82 #include "extern.h"
83 #include "pathnames.h"
84
85 #ifdef USE_QFA
86 int             noresyncmesg = 0;
87 #endif /* USE_QFA */
88 static long     fssize = MAXBSIZE;
89 static int      mt = -1;
90 static int      pipein = 0;
91 static int      magtapein = 0;          /* input is from magtape */
92 static char     magtape[MAXPATHLEN];
93 static char     magtapeprefix[MAXPATHLEN];
94 static int      blkcnt;
95 static int      numtrec;
96 static char     *tapebuf;               /* input buffer for read */
97 static int      bufsize;                /* buffer size without prefix */
98 static char     *tbufptr = NULL;        /* active tape buffer */
99 #ifdef HAVE_ZLIB
100 static char     *comprbuf;              /* uncompress work buf */
101 static size_t   comprlen;               /* size including prefix */
102 #endif
103 static union    u_spcl endoftapemark;
104 static long     blksread;               /* blocks read since last header */
105 static long     tpblksread = 0;         /* TP_BSIZE blocks read */
106 static long     tapesread;
107 static sigjmp_buf       restart;
108 static int      gettingfile = 0;        /* restart has a valid frame */
109 static char     *host = NULL;
110
111 static int      ofile;
112 static char     *map;
113 static char     lnkbuf[MAXPATHLEN + 1];
114 static int      pathlen;
115
116 int             oldinofmt;      /* old inode format conversion required */
117 int             Bcvt;           /* Swap Bytes (for CCI or sun) */
118 static int      Qcvt;           /* Swap quads (for sun) */
119
120 #define FLUSHTAPEBUF()  blkcnt = ntrec + 1
121
122 static void      accthdr __P((struct s_spcl *));
123 static int       checksum __P((int *));
124 static void      findinode __P((struct s_spcl *));
125 static void      findtapeblksize __P((void));
126 static int       gethead __P((struct s_spcl *));
127 static void      readtape __P((char *));
128 static void      setdumpnum __P((void));
129 static u_int     swabi __P((u_int));
130 #if 0
131 static u_long    swabl __P((u_long));
132 #endif
133 static u_char   *swab64 __P((u_char *, int));
134 static u_char   *swab32 __P((u_char *, int));
135 static u_char   *swab16 __P((u_char *, int));
136 static void      terminateinput __P((void));
137 static void      xtrfile __P((char *, size_t));
138 static void      xtrlnkfile __P((char *, size_t));
139 static void      xtrlnkskip __P((char *, size_t));
140 static void      xtrmap __P((char *, size_t));
141 static void      xtrmapskip __P((char *, size_t));
142 static void      xtrskip __P((char *, size_t));
143
144 #ifdef HAVE_ZLIB
145 static void     newcomprbuf __P((long));
146 static void     readtape_set __P((char *));
147 static void     readtape_uncompr __P((char *));
148 static void     readtape_comprfile __P((char *));
149 static void     readtape_comprtape __P((char *));
150 static char     *decompress_tapebuf __P((struct tapebuf *, int));
151 static void     msg_read_error __P((char *));
152 #endif
153 static int      read_a_block __P((int, char *, size_t, long *));
154 #define PREFIXSIZE      sizeof(struct tapebuf)
155
156 #define COMPARE_ONTHEFLY 1
157
158 #if COMPARE_ONTHEFLY
159 static int      ifile;          /* input file for compare */
160 static int      cmperror;       /* compare error */
161 static void     xtrcmpfile __P((char *, size_t));
162 static void     xtrcmpskip __P((char *, size_t));
163 #endif
164
165 static int readmapflag;
166
167 /*
168  * Set up an input source. This is called from main.c before setup() is.
169  */
170 void
171 setinput(char *source)
172 {
173         FLUSHTAPEBUF();
174         if (bflag)
175                 newtapebuf(ntrec);
176         else
177                 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
178         terminal = stdin;
179
180 #ifdef RRESTORE
181         if (strchr(source, ':')) {
182                 host = source;
183                 source = strchr(host, ':');
184                 *source++ = '\0';
185                 if (rmthost(host) == 0)
186                         exit(1);
187         } else
188 #endif
189         if (strcmp(source, "-") == 0) {
190                 /*
191                  * Since input is coming from a pipe we must establish
192                  * our own connection to the terminal.
193                  */
194                 terminal = fopen(_PATH_TTY, "r");
195                 if (terminal == NULL) {
196                         warn("cannot open %s", _PATH_TTY);
197                         terminal = fopen(_PATH_DEVNULL, "r");
198                         if (terminal == NULL)
199                                 err(1, "cannot open %s", _PATH_DEVNULL);
200                 }
201                 pipein++;
202         }
203         setuid(getuid());       /* no longer need or want root privileges */
204         if (Mflag) {
205                 strncpy(magtapeprefix, source, MAXPATHLEN);
206                 magtapeprefix[MAXPATHLEN-1] = '\0';
207                 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
208         }
209         else
210                 strncpy(magtape, source, MAXPATHLEN);
211         magtape[MAXPATHLEN - 1] = '\0';
212 }
213
214 void
215 newtapebuf(long size)
216 {
217         static int tapebufsize = -1;
218
219         ntrec = size;
220         bufsize = ntrec * TP_BSIZE;
221         if (size <= tapebufsize)
222                 return;
223         if (tapebuf != NULL)
224                 free(tapebuf);
225         tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
226         if (tapebuf == NULL)
227                 errx(1, "Cannot allocate space for tape buffer");
228         tapebufsize = size;
229 }
230
231 #ifdef HAVE_ZLIB
232 static void
233 newcomprbuf(long size)
234 {
235         if (size <= comprlen)
236                 return;
237         comprlen = size + sizeof(struct tapebuf);
238         if (comprbuf != NULL)
239                 free(comprbuf);
240         comprbuf = malloc(comprlen);
241         if (comprbuf == NULL)
242                 errx(1, "Cannot allocate space for decompress buffer");
243 }
244 #endif /* HAVE_ZLIB */
245
246 /*
247  * Verify that the tape drive can be accessed and
248  * that it actually is a dump tape.
249  */
250 void
251 setup(void)
252 {
253         int i, j, *ip;
254         struct stat stbuf;
255         struct mtget mt_stat;
256
257         Vprintf(stdout, "Verify tape and initialize maps\n");
258 #ifdef RRESTORE
259         if (host)
260                 mt = rmtopen(magtape, 0);
261         else
262 #endif
263         if (pipein)
264                 mt = 0;
265         else
266                 mt = open(magtape, O_RDONLY, 0);
267         if (mt < 0)
268                 err(1, "%s", magtape);
269         volno = 1;
270         if (!pipein) {
271                 /* need to know if input is really from a tape */
272 #ifdef RRESTORE
273                 if (host)
274                         magtapein = rmtioctl(MTNOP, 1) != -1;
275                 else
276 #endif
277                         if (ioctl(mt, MTIOCGET, (char *) &mt_stat) == 0) {
278                                 if (mt_stat.mt_dsreg & 0xffff)
279                                         magtapein = 1; /* fixed blocksize */
280                                 else
281                                         magtapein = 2; /* variable blocksize */
282                         }
283         }
284
285         Vprintf(stdout,"Input is from %s\n", magtapein? "tape": "file/pipe");
286         setdumpnum();
287         FLUSHTAPEBUF();
288         findtapeblksize();
289         if (gethead(&spcl) == FAIL) {
290                 blkcnt--; /* push back this block */
291                 blksread--;
292                 tpblksread--;
293                 cvtflag++;
294                 if (gethead(&spcl) == FAIL)
295                         errx(1, "Tape is not a dump tape");
296                 fprintf(stderr, "Converting to new file system format.\n");
297         }
298
299         if (zflag) {
300                 fprintf(stderr, "Dump tape is compressed.\n");
301 #ifdef HAVE_ZLIB
302                 newcomprbuf(bufsize);
303 #else
304                 errx(1,"This restore version doesn't support decompression");
305 #endif /* HAVE_ZLIB */
306         }
307         if (pipein) {
308                 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
309                 endoftapemark.s_spcl.c_type = TS_END;
310                 ip = (int *)&endoftapemark;
311                 j = sizeof(union u_spcl) / sizeof(int);
312                 i = 0;
313                 do
314                         i += *ip++;
315                 while (--j);
316                 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
317         }
318         if (vflag || command == 't' || command == 'C')
319                 printdumpinfo();
320         if (filesys[0] == '\0') {
321                 char *dirptr;
322                 strncpy(filesys, spcl.c_filesys, NAMELEN);
323                 filesys[NAMELEN - 1] = '\0';
324                 dirptr = strstr(filesys, " (dir");
325                 if (dirptr != NULL)
326                         *dirptr = '\0';
327         }
328         dumptime = spcl.c_ddate;
329         dumpdate = spcl.c_date;
330         if (stat(".", &stbuf) < 0)
331                 err(1, "cannot stat .");
332         if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
333                 fssize = TP_BSIZE;
334         if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
335                 fssize = stbuf.st_blksize;
336         if (((fssize - 1) & fssize) != 0)
337                 errx(1, "bad block size %ld", fssize);
338         if (spcl.c_volume != 1)
339                 errx(1, "Tape is not volume 1 of the dump");
340         if (gethead(&spcl) == FAIL) {
341                 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
342                 panic("no header after volume mark!\n");
343         }
344         findinode(&spcl);
345         if (spcl.c_type != TS_CLRI)
346                 errx(1, "Cannot find file removal list");
347         maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
348         Dprintf(stdout, "maxino = %ld\n", (long)maxino);
349         map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
350         if (map == NULL)
351                 errx(1, "no memory for active inode map");
352         usedinomap = map;
353         curfile.action = USING;
354         getfile(xtrmap, xtrmapskip);
355         if (spcl.c_type != TS_BITS)
356                 errx(1, "Cannot find file dump list");
357         map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
358         if (map == (char *)NULL)
359                 errx(1, "no memory for file dump list");
360         dumpmap = map;
361         curfile.action = USING;
362         getfile(xtrmap, xtrmapskip);
363         /*
364          * If there may be whiteout entries on the tape, pretend that the
365          * whiteout inode exists, so that the whiteout entries can be
366          * extracted.
367          */
368         if (oldinofmt == 0)
369                 SETINO(WINO, dumpmap);
370 }
371
372 /*
373  * Prompt user to load a new dump volume.
374  * "Nextvol" is the next suggested volume to use.
375  * This suggested volume is enforced when doing full
376  * or incremental restores, but can be overridden by
377  * the user when only extracting a subset of the files.
378  */
379 void
380 getvol(long nextvol)
381 {
382         long newvol = 0, savecnt = 0, wantnext = 0, i;
383         union u_spcl tmpspcl;
384 #       define tmpbuf tmpspcl.s_spcl
385         char buf[TP_BSIZE];
386         int haderror = 0;
387
388         if (nextvol == 1) {
389                 tapesread = 0;
390                 gettingfile = 0;
391         }
392         if (pipein) {
393                 if (nextvol != 1)
394                         panic("Changing volumes on pipe input?\n");
395                 if (volno == 1)
396                         return;
397                 goto gethdr;
398         }
399         savecnt = blksread;
400 again:
401         if (pipein)
402                 exit(1); /* pipes do not get a second chance */
403         if (command == 'R' || command == 'r' || curfile.action != SKIP) {
404                 newvol = nextvol;
405                 wantnext = 1;
406         } else {
407                 newvol = 0;
408                 wantnext = 0;
409         }
410         while (newvol <= 0) {
411                 if (tapesread == 0) {
412                         fprintf(stderr, "%s%s%s%s%s",
413                             "You have not read any tapes yet.\n",
414                             "Unless you know which volume your",
415                             " file(s) are on you should start\n",
416                             "with the last volume and work",
417                             " towards the first.\n");
418                 } else {
419                         fprintf(stderr, "You have read volumes");
420                         strcpy(buf, ": ");
421                         for (i = 1; i < 32; i++)
422                                 if (tapesread & (1 << i)) {
423                                         fprintf(stderr, "%s%ld", buf, (long)i);
424                                         strcpy(buf, ", ");
425                                 }
426                         fprintf(stderr, "\n");
427                 }
428                 do      {
429                         fprintf(stderr, "Specify next volume #: ");
430                         (void) fflush(stderr);
431                         (void) fgets(buf, TP_BSIZE, terminal);
432                 } while (!feof(terminal) && buf[0] == '\n');
433                 if (feof(terminal))
434                         exit(1);
435                 newvol = atoi(buf);
436                 if (newvol <= 0) {
437                         fprintf(stderr,
438                             "Volume numbers are positive numerics\n");
439                 }
440         }
441         if (newvol == volno) {
442                 tapesread |= 1 << volno;
443                 return;
444         }
445         closemt();
446         if (Mflag) {
447                 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
448                 magtape[MAXPATHLEN - 1] = '\0';
449         }
450         if (!Mflag || haderror) {
451                 haderror = 0;
452                 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
453                 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
454                 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
455                 (void) fflush(stderr);
456                 (void) fgets(buf, TP_BSIZE, terminal);
457                 if (feof(terminal))
458                         exit(1);
459                 if (!strcmp(buf, "none\n")) {
460                         terminateinput();
461                         return;
462                 }
463                 if (buf[0] != '\n') {
464                         (void) strcpy(magtape, buf);
465                         magtape[strlen(magtape) - 1] = '\0';
466                 }
467         }
468 #ifdef RRESTORE
469         if (host)
470                 mt = rmtopen(magtape, 0);
471         else
472 #endif
473                 mt = open(magtape, O_RDONLY, 0);
474
475         if (mt == -1) {
476                 fprintf(stderr, "Cannot open %s\n", magtape);
477                 volno = -1;
478                 haderror = 1;
479                 goto again;
480         }
481 gethdr:
482         volno = newvol;
483         setdumpnum();
484         FLUSHTAPEBUF();
485         if (gethead(&tmpbuf) == FAIL) {
486                 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
487                 fprintf(stderr, "tape is not dump tape\n");
488                 volno = 0;
489                 haderror = 1;
490                 goto again;
491         }
492         if (tmpbuf.c_volume != volno) {
493                 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
494                 volno = 0;
495                 haderror = 1;
496                 goto again;
497         }
498         if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
499                 fprintf(stderr, "Wrong dump date\n\tgot: %s",
500                         ctime4(&tmpbuf.c_date));
501                 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
502                 volno = 0;
503                 haderror = 1;
504                 goto again;
505         }
506         tapesread |= 1 << volno;
507         blksread = savecnt;
508         /*
509          * If continuing from the previous volume, skip over any
510          * blocks read already at the end of the previous volume.
511          *
512          * If coming to this volume at random, skip to the beginning
513          * of the next record.
514          */
515         Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
516                 tpblksread, (long)tmpbuf.c_firstrec);
517         if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
518                 if (!wantnext) {
519                         tpblksread = tmpbuf.c_firstrec;
520                         for (i = tmpbuf.c_count; i > 0; i--)
521                                 readtape(buf);
522                 } else if (tmpbuf.c_firstrec > 0 &&
523                            tmpbuf.c_firstrec < tpblksread - 1) {
524                         /*
525                          * -1 since we've read the volume header
526                          */
527                         i = tpblksread - tmpbuf.c_firstrec - 1;
528                         Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
529                                 (long)i, i > 1 ? "s" : "");
530                         while (--i >= 0)
531                                 readtape(buf);
532                 }
533         }
534         if (curfile.action == USING) {
535                 if (volno == 1)
536                         panic("active file into volume 1\n");
537                 return;
538         }
539         /*
540          * Skip up to the beginning of the next record
541          */
542         if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
543                 for (i = tmpbuf.c_count; i > 0; i--)
544                         readtape(buf);
545         (void) gethead(&spcl);
546         findinode(&spcl);
547         if (gettingfile) {
548                 gettingfile = 0;
549                 siglongjmp(restart, 1);
550         }
551 }
552
553 /*
554  * Handle unexpected EOF.
555  */
556 static void
557 terminateinput(void)
558 {
559
560         if (gettingfile && curfile.action == USING) {
561                 printf("Warning: %s %s\n",
562                     "End-of-input encountered while extracting", curfile.name);
563         }
564         curfile.name = "<name unknown>";
565         curfile.action = UNKNOWN;
566         curfile.dip = NULL;
567         curfile.ino = maxino;
568         if (gettingfile) {
569                 gettingfile = 0;
570                 siglongjmp(restart, 1);
571         }
572 }
573
574 /*
575  * handle multiple dumps per tape by skipping forward to the
576  * appropriate one.
577  */
578 static void
579 setdumpnum(void)
580 {
581         struct mtop tcom;
582
583         if (dumpnum == 1 || volno != 1)
584                 return;
585         if (pipein)
586                 errx(1, "Cannot have multiple dumps on pipe input");
587         tcom.mt_op = MTFSF;
588         tcom.mt_count = dumpnum - 1;
589 #ifdef RRESTORE
590         if (host)
591                 rmtioctl(MTFSF, dumpnum - 1);
592         else
593 #endif
594                 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
595                         warn("ioctl MTFSF");
596 }
597
598 void
599 printdumpinfo(void)
600 {
601         fprintf(stdout, "Dump   date: %s", ctime4(&spcl.c_date));
602         fprintf(stdout, "Dumped from: %s",
603             (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
604         if (spcl.c_host[0] == '\0')
605                 return;
606         fprintf(stdout, "Level %d dump of %s on %s:%s\n",
607                 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
608         fprintf(stdout, "Label: %s\n", spcl.c_label);
609 }
610
611 int
612 extractfile(char *name)
613 {
614         unsigned int flags;
615         mode_t mode;
616         struct timeval timep[2];
617         struct entry *ep;
618
619         curfile.name = name;
620         curfile.action = USING;
621 #ifdef  __linux__
622         timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
623         timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
624         timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
625         timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
626 #else   /* __linux__ */
627         timep[0].tv_sec = curfile.dip->di_atime;
628         timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
629         timep[1].tv_sec = curfile.dip->di_mtime;
630         timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
631 #endif  /* __linux__ */
632         mode = curfile.dip->di_mode;
633         flags = curfile.dip->di_flags;
634         switch (mode & IFMT) {
635
636         default:
637                 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
638                 skipfile();
639                 return (FAIL);
640
641         case IFSOCK:
642                 Vprintf(stdout, "skipped socket %s\n", name);
643                 skipfile();
644                 return (GOOD);
645
646         case IFDIR:
647                 if (mflag) {
648                         ep = lookupname(name);
649                         if (ep == NULL || ep->e_flags & EXTRACT)
650                                 panic("unextracted directory %s\n", name);
651                         skipfile();
652                         return (GOOD);
653                 }
654                 Vprintf(stdout, "extract file %s\n", name);
655                 return (genliteraldir(name, curfile.ino));
656
657         case IFLNK:
658         {
659 #ifdef HAVE_LCHOWN
660                 uid_t luid = curfile.dip->di_uid;
661                 gid_t lgid = curfile.dip->di_gid;
662 #endif
663                 lnkbuf[0] = '\0';
664                 pathlen = 0;
665                 getfile(xtrlnkfile, xtrlnkskip);
666                 if (pathlen == 0) {
667                         Vprintf(stdout,
668                             "%s: zero length symbolic link (ignored)\n", name);
669                         return (GOOD);
670                 }
671                 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
672                         return (FAIL);
673 #ifdef HAVE_LCHOWN
674                 (void) lchown(name, luid, lgid);
675 #endif
676                 return (GOOD);
677         }
678
679         case IFIFO:
680                 Vprintf(stdout, "extract fifo %s\n", name);
681                 if (Nflag) {
682                         skipfile();
683                         return (GOOD);
684                 }
685                 if (uflag && !Nflag)
686                         (void)unlink(name);
687                 if (mkfifo(name, mode) < 0) {
688                         warn("%s: cannot create fifo", name);
689                         skipfile();
690                         return (FAIL);
691                 }
692                 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
693                 (void) chmod(name, mode);
694                 if (flags)
695 #ifdef  __linux__
696                         (void) fsetflags(name, flags);
697 #else
698                         (void) chflags(name, flags);
699 #endif
700                 skipfile();
701                 utimes(name, timep);
702                 return (GOOD);
703
704         case IFCHR:
705         case IFBLK:
706                 Vprintf(stdout, "extract special file %s\n", name);
707                 if (Nflag) {
708                         skipfile();
709                         return (GOOD);
710                 }
711                 if (uflag)
712                         (void)unlink(name);
713                 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
714                         warn("%s: cannot create special file", name);
715                         skipfile();
716                         return (FAIL);
717                 }
718                 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
719                 (void) chmod(name, mode);
720                 if (flags)
721 #ifdef  __linux__
722                         {
723                         warn("%s: fsetflags called on a special file", name);
724                         (void) fsetflags(name, flags);
725                         }
726 #else
727                         (void) chflags(name, flags);
728 #endif
729                 skipfile();
730                 utimes(name, timep);
731                 return (GOOD);
732
733         case IFREG:
734                 Vprintf(stdout, "extract file %s\n", name);
735                 if (Nflag) {
736                         skipfile();
737                         return (GOOD);
738                 }
739                 if (uflag)
740                         (void)unlink(name);
741                 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
742                     0666)) < 0) {
743                         warn("%s: cannot create file", name);
744                         skipfile();
745                         return (FAIL);
746                 }
747                 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
748                 (void) fchmod(ofile, mode);
749                 if (flags)
750 #ifdef  __linux__
751                         (void) setflags(ofile, flags);
752 #else
753                         (void) fchflags(ofile, flags);
754 #endif
755                 getfile(xtrfile, xtrskip);
756                 (void) close(ofile);
757                 utimes(name, timep);
758                 return (GOOD);
759         }
760         /* NOTREACHED */
761 }
762
763 /*
764  * skip over bit maps on the tape
765  */
766 void
767 skipmaps(void)
768 {
769
770         while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
771                 skipfile();
772 }
773
774 /*
775  * skip over a file on the tape
776  */
777 void
778 skipfile(void)
779 {
780
781         curfile.action = SKIP;
782         getfile(xtrnull, xtrnull);
783 }
784
785 /*
786  * Extract a file from the tape.
787  * When an allocated block is found it is passed to the fill function;
788  * when an unallocated block (hole) is found, a zeroed buffer is passed
789  * to the skip function.
790  */
791 void
792 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
793 {
794         register int i;
795         volatile int curblk = 0;
796         volatile quad_t size = spcl.c_dinode.di_size;
797         volatile int last_write_was_hole = 0;
798         quad_t origsize = size;
799         static char clearedbuf[MAXBSIZE];
800         char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
801         char junk[TP_BSIZE];
802
803         if (spcl.c_type == TS_END)
804                 panic("ran off end of tape\n");
805         if (spcl.c_magic != NFS_MAGIC)
806                 panic("not at beginning of a file\n");
807         if (!gettingfile && setjmp(restart) != 0)
808                 return;
809         gettingfile++;
810 loop:
811         for (i = 0; i < spcl.c_count; i++) {
812                 if (readmapflag || spcl.c_addr[i]) {
813                         readtape(&buf[curblk++][0]);
814                         if (curblk == fssize / TP_BSIZE) {
815                                 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
816                                      fssize : (curblk - 1) * TP_BSIZE + size));
817                                 curblk = 0;
818                                 last_write_was_hole = 0;
819                         }
820                 } else {
821                         if (curblk > 0) {
822                                 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
823                                      curblk * TP_BSIZE :
824                                      (curblk - 1) * TP_BSIZE + size));
825                                 curblk = 0;
826                         }
827                         (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
828                                 TP_BSIZE : size));
829                         last_write_was_hole = 1;
830                 }
831                 if ((size -= TP_BSIZE) <= 0) {
832                         for (i++; i < spcl.c_count; i++)
833                                 if (readmapflag || spcl.c_addr[i])
834                                         readtape(junk);
835                         break;
836                 }
837         }
838         if (gethead(&spcl) == GOOD && size > 0) {
839                 if (spcl.c_type == TS_ADDR)
840                         goto loop;
841                 Dprintf(stdout,
842                         "Missing address (header) block for %s at %ld blocks\n",
843                         curfile.name, (long)blksread);
844         }
845         if (curblk > 0) {
846                 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
847                 last_write_was_hole = 0;
848         }
849         if (size > 0) {
850                 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
851                 (*skip)(clearedbuf, size);
852                 last_write_was_hole = 1;
853         }
854         if (last_write_was_hole) {
855                 ftruncate(ofile, origsize);
856         }
857         findinode(&spcl);
858         gettingfile = 0;
859 }
860
861 /*
862  * Write out the next block of a file.
863  */
864 static void
865 xtrfile(char *buf, size_t size)
866 {
867
868         if (Nflag)
869                 return;
870         if (write(ofile, buf, (int) size) == -1)
871                 err(1, "write error extracting inode %lu, name %s\nwrite",
872                         (unsigned long)curfile.ino, curfile.name);
873 }
874
875 /*
876  * Skip over a hole in a file.
877  */
878 /* ARGSUSED */
879 static void
880 xtrskip(char *buf, size_t size)
881 {
882
883         if (lseek(ofile, (off_t)size, SEEK_CUR) == -1)
884                 err(1, "seek error extracting inode %lu, name %s\nlseek",
885                         (unsigned long)curfile.ino, curfile.name);
886 }
887
888 /*
889  * Collect the next block of a symbolic link.
890  */
891 static void
892 xtrlnkfile(char *buf, size_t size)
893 {
894
895         pathlen += size;
896         if (pathlen > MAXPATHLEN)
897                 errx(1, "symbolic link name: %s->%s%s; too long %d",
898                     curfile.name, lnkbuf, buf, pathlen);
899         (void) strcat(lnkbuf, buf);
900 }
901
902 /*
903  * Skip over a hole in a symbolic link (should never happen).
904  */
905 /* ARGSUSED */
906 static void
907 xtrlnkskip(char *buf, size_t size)
908 {
909
910         errx(1, "unallocated block in symbolic link %s", curfile.name);
911 }
912
913 /*
914  * Collect the next block of a bit map.
915  */
916 static void
917 xtrmap(char *buf, size_t size)
918 {
919
920         memmove(map, buf, size);
921         map += size;
922 }
923
924 /*
925  * Skip over a hole in a bit map (should never happen).
926  */
927 /* ARGSUSED */
928 static void
929 xtrmapskip(char *buf, size_t size)
930 {
931
932         panic("hole in map\n");
933         map += size;
934 }
935
936 /*
937  * Noop, when an extraction function is not needed.
938  */
939 /* ARGSUSED */
940 void
941 xtrnull(char *buf, size_t size)
942 {
943
944         return;
945 }
946
947 #if COMPARE_ONTHEFLY
948 /*
949  * Compare the next block of a file.
950  */
951 static void
952 xtrcmpfile(char *buf, size_t size)
953 {
954         static char cmpbuf[MAXBSIZE];
955
956         if (cmperror)
957                 return;
958         
959         if (read(ifile, cmpbuf, size) != size) {
960                 fprintf(stderr, "%s: size has changed.\n", 
961                         curfile.name);
962                 cmperror = 1;
963                 return;
964         }
965         
966         if (memcmp(buf, cmpbuf, size) != 0) {
967                 fprintf(stderr, "%s: tape and disk copies are different\n",
968                         curfile.name);
969                 cmperror = 1;
970                 return;
971         }
972 }
973
974 /*
975  * Skip over a hole in a file.
976  */
977 static void
978 xtrcmpskip(char *buf, size_t size)
979 {
980         static char cmpbuf[MAXBSIZE];
981         int i;
982
983         if (cmperror)
984                 return;
985         
986         if (read(ifile, cmpbuf, size) != size) {
987                 fprintf(stderr, "%s: size has changed.\n", 
988                         curfile.name);
989                 cmperror = 1;
990                 return;
991         }
992
993         for (i = 0; i < size; ++i)
994                 if (cmpbuf[i] != '\0') {
995                         fprintf(stderr, "%s: tape and disk copies are different\n",
996                                 curfile.name);
997                         cmperror = 1;
998                         return;
999                 }
1000 }
1001 #endif /* COMPARE_ONTHEFLY */
1002
1003 #if !COMPARE_ONTHEFLY
1004 static int
1005 do_cmpfiles(int fd_tape, int fd_disk, long size)
1006 {
1007         static char buf_tape[BUFSIZ];
1008         static char buf_disk[BUFSIZ];
1009         ssize_t n_tape;
1010         ssize_t n_disk;
1011
1012         while (size > 0) {
1013                 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1014                         close(fd_tape), close(fd_disk);
1015                         panic("do_cmpfiles: unexpected EOF[1]");
1016                 }
1017                 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1018                         close(fd_tape), close(fd_disk);
1019                         panic("do_cmpfiles: unexpected EOF[2]");
1020                 }
1021                 if (n_tape != n_disk) {
1022                         close(fd_tape), close(fd_disk);
1023                         panic("do_cmpfiles: sizes different!");
1024                 }
1025                 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1026                 size -= n_tape;
1027         }
1028         return (0);
1029 }
1030
1031 /* for debugging compare problems */
1032 #undef COMPARE_FAIL_KEEP_FILE
1033
1034 static
1035 #ifdef COMPARE_FAIL_KEEP_FILE
1036 /* return true if tapefile should be unlinked after compare */
1037 int
1038 #else
1039 void
1040 #endif
1041 cmpfiles(char *tapefile, char *diskfile, struct stat *sbuf_disk)
1042 {
1043         struct stat sbuf_tape;
1044         int fd_tape, fd_disk;
1045
1046         if (stat(tapefile, &sbuf_tape) != 0) {
1047                 panic("Can't lstat tmp file %s: %s\n", tapefile,
1048                       strerror(errno));
1049                 compare_errors = 1;
1050         }
1051
1052         if (sbuf_disk->st_size != sbuf_tape.st_size) {
1053                 fprintf(stderr,
1054                         "%s: size changed from %ld to %ld.\n",
1055                         diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1056                 compare_errors = 1;
1057 #ifdef COMPARE_FAIL_KEEP_FILE
1058                 return (0);
1059 #else
1060                 return;
1061 #endif
1062         }
1063
1064         if ((fd_tape = open(tapefile, O_RDONLY)) < 0) {
1065                 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1066                 compare_errors = 1;
1067         }
1068         if ((fd_disk = open(diskfile, O_RDONLY)) < 0) {
1069                 close(fd_tape);
1070                 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1071                 compare_errors = 1;
1072         }
1073
1074         if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1075                 fprintf(stderr, "%s: tape and disk copies are different\n",
1076                         diskfile);
1077                 close(fd_tape);
1078                 close(fd_disk);
1079                 compare_errors = 1;
1080 #ifdef COMPARE_FAIL_KEEP_FILE
1081                 /* rename the file to live in /tmp */
1082                 /* rename `tapefile' to /tmp/<basename of diskfile> */
1083                 {
1084                         char *p = strrchr(diskfile, '/');
1085                         char newname[MAXPATHLEN];
1086                         if (!p) {
1087                                 panic("can't find / in %s\n", diskfile);
1088                         }
1089                         snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1090                         if (rename(tapefile, newname)) {
1091                                 panic("rename from %s to %s failed: %s\n",
1092                                       tapefile, newname,
1093                                       strerror(errno));
1094                         } else {
1095                                 fprintf(stderr, "*** %s saved to %s\n",
1096                                         tapefile, newname);
1097                         }
1098                 }
1099                 
1100                 /* don't unlink the file (it's not there anymore */
1101                 /* anyway) */
1102                 return (0);
1103 #else
1104                 return;
1105 #endif
1106         }
1107         close(fd_tape);
1108         close(fd_disk);
1109 #ifdef COMPARE_FAIL_KEEP_FILE
1110         return (1);
1111 #endif
1112 }
1113 #endif /* !COMPARE_ONTHEFLY */
1114
1115 #if !COMPARE_ONTHEFLY
1116 static char tmpfilename[MAXPATHLEN];
1117 #endif
1118
1119 void
1120 comparefile(char *name)
1121 {
1122         int mode;
1123         struct stat sb;
1124         int r;
1125 #if !COMPARE_ONTHEFLY
1126         static char *tmpfile = NULL;
1127         struct stat stemp;
1128 #endif
1129
1130         if ((r = lstat(name, &sb)) != 0) {
1131                 warn("%s: does not exist (%d)", name, r);
1132                 compare_errors = 1;
1133                 skipfile();
1134                 return;
1135         }
1136
1137         curfile.name = name;
1138         curfile.action = USING;
1139         mode = curfile.dip->di_mode;
1140
1141         Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1142                 (long)sb.st_size, mode);
1143
1144         if (sb.st_mode != mode) {
1145                 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1146                         name, mode & 07777, sb.st_mode & 07777);
1147                 compare_errors = 1;
1148         }
1149         switch (mode & IFMT) {
1150         default:
1151                 skipfile();
1152                 return;
1153
1154         case IFSOCK:
1155                 skipfile();
1156                 return;
1157
1158         case IFDIR:
1159                 skipfile();
1160                 return;
1161
1162         case IFLNK: {
1163                 char lbuf[MAXPATHLEN + 1];
1164                 int lsize;
1165
1166                 if (!(sb.st_mode & S_IFLNK)) {
1167                         fprintf(stderr, "%s: is no longer a symbolic link\n",
1168                                 name);
1169                         compare_errors = 1;
1170                         return;
1171                 }
1172                 lnkbuf[0] = '\0';
1173                 pathlen = 0;
1174                 getfile(xtrlnkfile, xtrlnkskip);
1175                 if (pathlen == 0) {
1176                         fprintf(stderr,
1177                                 "%s: zero length symbolic link (ignored)\n",
1178                                 name);
1179                         compare_errors = 1;
1180                         return;
1181                 }
1182                 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1183                         panic("readlink of %s failed: %s", name,
1184                               strerror(errno));
1185                         compare_errors = 1;
1186                 }
1187                 lbuf[lsize] = 0;
1188                 if (strcmp(lbuf, lnkbuf) != 0) {
1189                         fprintf(stderr,
1190                                 "%s: symbolic link changed from %s to %s.\n",
1191                                 name, lnkbuf, lbuf);
1192                         compare_errors = 1;
1193                         return;
1194                 }
1195                 return;
1196         }
1197
1198         case IFCHR:
1199         case IFBLK:
1200                 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1201                         fprintf(stderr, "%s: no longer a special file\n",
1202                                 name);
1203                         compare_errors = 1;
1204                         skipfile();
1205                         return;
1206                 }
1207
1208                 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1209                         fprintf(stderr,
1210                                 "%s: device changed from %d,%d to %d,%d.\n",
1211                                 name,
1212                                 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1213                                 (int)curfile.dip->di_rdev & 0xff,
1214                                 ((int)sb.st_rdev >> 8) & 0xff,
1215                                 (int)sb.st_rdev & 0xff);
1216                         compare_errors = 1;
1217                 }
1218                 skipfile();
1219                 return;
1220
1221         case IFREG:
1222 #if COMPARE_ONTHEFLY
1223                 if ((ifile = open(name, O_RDONLY)) < 0) {
1224                         panic("Can't open %s: %s\n", name, strerror(errno));
1225                         skipfile();
1226                         compare_errors = 1;
1227                 }
1228                 else {
1229                         cmperror = 0;
1230                         getfile(xtrcmpfile, xtrcmpskip);
1231                         if (!cmperror) {
1232                                 char c;
1233                                 if (read(ifile, &c, 1) != 0) {
1234                                         fprintf(stderr, "%s: size has changed.\n", 
1235                                                 name);
1236                                         cmperror = 1;
1237                                 }
1238                         }
1239                         if (cmperror)
1240                                 compare_errors = 1;
1241                         close(ifile);
1242                 }
1243 #else
1244                 if (tmpfile == NULL) {
1245                         /* argument to mktemp() must not be in RO space: */
1246                         snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1247                         tmpfile = mktemp(&tmpfilename[0]);
1248                 }
1249                 if ((stat(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1250                         panic("cannot delete tmp file %s: %s\n",
1251                               tmpfile, strerror(errno));
1252                 }
1253                 if ((ofile = open(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1254                         panic("cannot create file temp file %s: %s\n",
1255                               name, strerror(errno));
1256                 }
1257                 getfile(xtrfile, xtrskip);
1258                 (void) close(ofile);
1259 #ifdef COMPARE_FAIL_KEEP_FILE
1260                 if (cmpfiles(tmpfile, name, &sb))
1261                         unlink(tmpfile);
1262 #else
1263                 cmpfiles(tmpfile, name, &sb);
1264                 unlink(tmpfile);
1265 #endif
1266 #endif /* COMPARE_ONTHEFLY */
1267                 return;
1268         }
1269         /* NOTREACHED */
1270 }
1271
1272 #ifdef HAVE_ZLIB
1273 static void (*readtape_func)(char *) = readtape_set;
1274
1275 /*
1276  * Read TP_BSIZE blocks from the input.
1277  * Handle read errors, and end of media.
1278  * Decompress compressed blocks.
1279  */
1280 static void
1281 readtape(char *buf)
1282 {
1283         (*readtape_func)(buf);  /* call the actual processing routine */
1284 }
1285
1286 /*
1287  * Set function pointer for readtape() routine. zflag and magtapein must
1288  * be correctly set before the first call to readtape().
1289  */
1290 static void
1291 readtape_set(char *buf)
1292 {
1293         if (!zflag) 
1294                 readtape_func = readtape_uncompr;
1295         else {
1296                 if (magtapein)
1297                         readtape_func = readtape_comprtape;
1298                 else
1299                         readtape_func = readtape_comprfile;
1300         }
1301         readtape(buf);
1302 }
1303
1304 #endif /* HAVE_ZLIB */
1305
1306 /*
1307  * This is the original readtape(), it's used for reading uncompressed input.
1308  * Read TP_BSIZE blocks from the input.
1309  * Handle read errors, and end of media.
1310  */
1311 static void
1312 #ifdef HAVE_ZLIB
1313 readtape_uncompr(char *buf)
1314 #else
1315 readtape(char *buf)
1316 #endif
1317 {
1318         ssize_t rd, newvol, i;
1319         int cnt, seek_failed;
1320
1321         if (blkcnt < numtrec) {
1322                 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1323                 blksread++;
1324                 tpblksread++;
1325                 return;
1326         }
1327         tbufptr = tapebuf;
1328         for (i = 0; i < ntrec; i++)
1329                 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1330         if (numtrec == 0)
1331                 numtrec = ntrec;
1332         cnt = ntrec * TP_BSIZE;
1333         rd = 0;
1334 getmore:
1335 #ifdef RRESTORE
1336         if (host)
1337                 i = rmtread(&tapebuf[rd], cnt);
1338         else
1339 #endif
1340                 i = read(mt, &tapebuf[rd], cnt);
1341
1342         /*
1343          * Check for mid-tape short read error.
1344          * If found, skip rest of buffer and start with the next.
1345          */
1346         if (!pipein && numtrec < ntrec && i > 0) {
1347                 Dprintf(stdout, "mid-media short read error.\n");
1348                 numtrec = ntrec;
1349         }
1350         /*
1351          * Handle partial block read.
1352          */
1353         if (pipein && i == 0 && rd > 0)
1354                 i = rd;
1355         else if (i > 0 && i != ntrec * TP_BSIZE) {
1356                 if (pipein) {
1357                         rd += i;
1358                         cnt -= i;
1359                         if (cnt > 0)
1360                                 goto getmore;
1361                         i = rd;
1362                 } else {
1363                         /*
1364                          * Short read. Process the blocks read.
1365                          */
1366                         if (i % TP_BSIZE != 0)
1367                                 Vprintf(stdout,
1368                                     "partial block read: %ld should be %ld\n",
1369                                     (long)i, ntrec * TP_BSIZE);
1370                         numtrec = i / TP_BSIZE;
1371                 }
1372         }
1373         /*
1374          * Handle read error.
1375          */
1376         if (i < 0) {
1377                 fprintf(stderr, "Tape read error while ");
1378                 switch (curfile.action) {
1379                 default:
1380                         fprintf(stderr, "trying to set up tape\n");
1381                         break;
1382                 case UNKNOWN:
1383                         fprintf(stderr, "trying to resynchronize\n");
1384                         break;
1385                 case USING:
1386                         fprintf(stderr, "restoring %s\n", curfile.name);
1387                         break;
1388                 case SKIP:
1389                         fprintf(stderr, "skipping over inode %lu\n",
1390                                 (unsigned long)curfile.ino);
1391                         break;
1392                 }
1393                 if (!yflag && !reply("continue"))
1394                         exit(1);
1395                 i = ntrec * TP_BSIZE;
1396                 memset(tapebuf, 0, (size_t)i);
1397 #ifdef RRESTORE
1398                 if (host)
1399                         seek_failed = (rmtseek(i, 1) < 0);
1400                 else
1401 #endif
1402                         seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1403
1404                 if (seek_failed) {
1405                         warn("continuation failed");
1406                         if (!yflag && !reply("assume end-of-tape and continue"))
1407                                 exit(1);
1408                         i = 0;
1409                 }
1410         }
1411         /*
1412          * Handle end of tape.
1413          */
1414         if (i == 0) {
1415                 Vprintf(stdout, "End-of-tape encountered\n");
1416                 if (!pipein) {
1417                         newvol = volno + 1;
1418                         volno = 0;
1419                         numtrec = 0;
1420                         getvol(newvol);
1421                         readtape(buf);
1422                         return;
1423                 }
1424                 if (rd % TP_BSIZE != 0)
1425                         panic("partial block read: %d should be %d\n",
1426                                 rd, ntrec * TP_BSIZE);
1427                 terminateinput();
1428                 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1429         }
1430         blkcnt = 0;
1431         memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1432         blksread++;
1433         tpblksread++;
1434 }
1435
1436 #ifdef HAVE_ZLIB
1437
1438 /*
1439  * Read a compressed format block from a file or pipe and uncompress it.
1440  * Attempt to handle read errors, and end of file. 
1441  */
1442 static void
1443 readtape_comprfile(char *buf)
1444 {
1445         long rl, size, i, ret;
1446         int newvol; 
1447         struct tapebuf *tpb;
1448
1449         if (blkcnt < numtrec) {
1450                 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1451                 blksread++;
1452                 tpblksread++;
1453                 return;
1454         }
1455         /* need to read the next block */
1456         tbufptr = tapebuf;
1457         for (i = 0; i < ntrec; i++)
1458                 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1459         numtrec = ntrec;
1460         tpb = (struct tapebuf *) tapebuf;
1461
1462         /* read the block prefix */
1463         ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
1464         if (ret <= 0)
1465                 goto readerr;
1466
1467         /* read the data */
1468         size = tpb->length;
1469         if (size > bufsize)  {
1470                 /* something's wrong */
1471                 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
1472                         bufsize, size);
1473                 size = bufsize;
1474                 tpb->length = bufsize;
1475         }
1476         ret = read_a_block(mt, tpb->buf, size, &rl);
1477         if (ret <= 0)
1478                 goto readerr;
1479
1480         tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
1481         if (tbufptr == NULL) {
1482                 msg_read_error("File decompression error while");
1483                 if (!yflag && !reply("continue"))
1484                         exit(1);
1485                 memset(tapebuf, 0, bufsize);
1486                 tbufptr = tapebuf;
1487         }
1488
1489         blkcnt = 0;
1490         memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1491         blksread++;
1492         tpblksread++;
1493         return;
1494
1495 readerr:
1496         /* Errors while reading from a file or pipe are catastrophic. Since
1497          * there are no block boundaries, it's impossible to bypass the
1498          * block in error and find the start of the next block.
1499          */
1500         if (ret == 0) {
1501                 /* It's possible to have multiple input files using -M
1502                  * and -f file1,file2...
1503                  */
1504                 Vprintf(stdout, "End-of-File encountered\n");
1505                 if (!pipein) {
1506                         newvol = volno + 1;
1507                         volno = 0;
1508                         numtrec = 0;
1509                         getvol(newvol);
1510                         readtape(buf);
1511                         return;
1512                 }
1513         }
1514         msg_read_error("Read error while");
1515         /* if (!yflag && !reply("continue")) */
1516                 exit(1);
1517 }
1518
1519 /*
1520  * Read compressed data from a tape and uncompress it.
1521  * Handle read errors, and end of media.
1522  * Since a tape consists of separate physical blocks, we try
1523  * to recover from errors by repositioning the tape to the next
1524  * block.
1525  */
1526 static void
1527 readtape_comprtape(char *buf)
1528 {
1529         long rl, size, i;
1530         int ret, newvol;
1531         struct tapebuf *tpb;
1532         struct mtop tcom;
1533
1534         if (blkcnt < numtrec) {
1535                 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1536                 blksread++;
1537                 tpblksread++;
1538                 return;
1539         }
1540         /* need to read the next block */
1541         tbufptr = tapebuf;
1542         for (i = 0; i < ntrec; i++)
1543                 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1544         numtrec = ntrec;
1545         tpb = (struct tapebuf *) tapebuf;
1546
1547         /* read the block */
1548         size = bufsize + PREFIXSIZE;
1549         ret = read_a_block(mt, tapebuf, size, &rl);
1550         if (ret <= 0)
1551                 goto readerr;
1552
1553         tbufptr = decompress_tapebuf(tpb, rl);
1554         if (tbufptr == NULL) {
1555                 msg_read_error("Tape decompression error while");
1556                 if (!yflag && !reply("continue"))
1557                         exit(1);
1558                 memset(tapebuf, 0, PREFIXSIZE + bufsize);
1559                 tbufptr = tapebuf;
1560         }
1561         goto moverecord;
1562
1563 readerr:
1564         /* Handle errors: EOT switches to the next volume, other errors
1565          * attempt to position the tape to the next block.
1566          */
1567         if (ret == 0) {
1568                 Vprintf(stdout, "End-of-tape encountered\n");
1569                 newvol = volno + 1;
1570                 volno = 0;
1571                 numtrec = 0;
1572                 getvol(newvol);
1573                 readtape(buf);
1574                 return;
1575         }
1576
1577         msg_read_error("Tape read error while");
1578         if (!yflag && !reply("continue"))
1579                 exit(1);
1580         memset(tapebuf, 0, PREFIXSIZE + bufsize);
1581         tbufptr = tapebuf;
1582
1583 #ifdef RRESTORE
1584         if (host)
1585                 rl = rmtioctl(MTFSR, 1);
1586         else
1587 #endif
1588         {
1589                 tcom.mt_op = MTFSR;
1590                 tcom.mt_count = 1;
1591                 rl = ioctl(mt, MTIOCTOP, &tcom);
1592         }
1593
1594         if (rl < 0) {
1595                 warn("continuation failed");
1596                 if (!yflag && !reply("assume end-of-tape and continue"))
1597                         exit(1);
1598                 ret = 0;         /* end of tape */
1599                 goto readerr;
1600         }
1601
1602 moverecord:
1603         blkcnt = 0;
1604         memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1605         blksread++;
1606         tpblksread++;
1607 }
1608
1609 /*
1610  *  Decompress a struct tapebuf into a buffer. readsize is the size read
1611  *  from the tape/file and is used for error messages. Returns a pointer
1612  *  to the location of the uncompressed buffer or NULL on errors.
1613  *  Adjust numtrec and complain for a short block.
1614  */
1615 static char *
1616 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
1617 {
1618         /* If zflag is on, all blocks have a struct tapebuf prefix */
1619         /* zflag gets set in setup() from the dump header          */
1620         int cresult, blocklen;        
1621         unsigned long worklen;
1622         char *output = NULL,*reason = NULL, *lengtherr = NULL;              
1623        
1624         /* build a length error message */
1625         blocklen = tpbin->length;
1626         if (readsize < blocklen + PREFIXSIZE)
1627                 lengtherr = "short";
1628         else
1629                 if (readsize > blocklen + PREFIXSIZE)
1630                         lengtherr = "long";
1631
1632         worklen = comprlen;
1633         cresult = Z_OK;
1634         if (tpbin->compressed) {
1635                 /* uncompress whatever we read, if it fails, complain later */
1636                 cresult = uncompress(comprbuf, &worklen, tpbin->buf, blocklen);
1637                 output = comprbuf;
1638         }
1639         else {
1640                 output = tpbin->buf;
1641                 worklen = blocklen;
1642         }
1643         switch (cresult) {
1644                 case Z_OK:
1645                         numtrec = worklen / TP_BSIZE;
1646                         if (worklen % TP_BSIZE != 0)
1647                                 reason = "length mismatch";
1648                         break;
1649                 case Z_MEM_ERROR:
1650                         reason = "not enough memory";
1651                         break;
1652                 case Z_BUF_ERROR:
1653                         reason = "buffer too small";
1654                         break;
1655                 case Z_DATA_ERROR:
1656                         reason = "data error";
1657                         break;
1658                 default:
1659                         reason = "unknown";
1660         } /*switch */
1661         if (reason) {
1662                 if (lengtherr)
1663                         fprintf(stderr, "%s compressed block: %d expected: %d\n",
1664                                 lengtherr, readsize, tpbin->length + PREFIXSIZE);
1665                 fprintf(stderr, "decompression error, block %ld: %s\n",
1666                         tpblksread+1, reason);
1667                 if (cresult != Z_OK)   output = NULL;
1668         }
1669         return output;
1670 }
1671
1672 /*
1673  * Print an error message for a read error.
1674  * This was exteracted from the original readtape().
1675  */
1676 static void
1677 msg_read_error(char *m)
1678 {
1679         switch (curfile.action) {
1680                 default:
1681                         fprintf(stderr, "%s trying to set up tape\n", m);
1682                         break;
1683                 case UNKNOWN:
1684                         fprintf(stderr, "%s trying to resynchronize\n", m);
1685                         break;
1686                 case USING:
1687                         fprintf(stderr, "%s restoring %s\n", m, curfile.name);
1688                         break;
1689                 case SKIP:
1690                         fprintf(stderr, "%s skipping over inode %lu\n", m,
1691                                 (unsigned long)curfile.ino);
1692                         break;
1693         }
1694 }
1695 #endif /* HAVE_ZLIB */
1696
1697 /*
1698  * Read the first block and set the blocksize from its length. Test
1699  * if the block looks like a compressed dump tape. setup() will make
1700  * the final determination by checking the compressed flag if gethead()
1701  * finds a valid header. The test here is necessary to offset the buffer
1702  * by the size of the compressed prefix. zflag is set here so that
1703  * readtape_set can set the correct function pointer for readtape().
1704  * Note that the first block of each tape/file will not be compressed.
1705  */ 
1706 static void
1707 findtapeblksize(void)
1708 {
1709         long i;
1710         size_t len;
1711         struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1712         struct s_spcl *spclpt = (struct s_spcl *) tpb->buf;
1713
1714         for (i = 0; i < ntrec; i++)
1715                 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1716         blkcnt = 0;
1717         tbufptr = tapebuf;
1718         /*
1719          * For a pipe or file, read in the first record. For a tape, read
1720          * the first block.
1721          */
1722         if (magtapein == 1)     /* fixed blocksize tape, not compressed */
1723                 len = ntrec * TP_BSIZE;
1724         else if (magtapein == 2)/* variable blocksize tape */
1725                 len = bufsize + PREFIXSIZE;
1726         else                    /* not mag tape */
1727                 len = TP_BSIZE;
1728
1729         if (read_a_block(mt, tapebuf, len, &i) <= 0)
1730                 errx(1, "Tape read error on first record");
1731
1732         /*
1733          * If the input is from a file or a pipe, we read TP_BSIZE
1734          * bytes looking for a compressed dump header, we then
1735          * need to read in the rest of the record, as determined by
1736          * tpb->length or bufsize. The first block of the dump is
1737          * guaranteed to not be compressed so we look at the header.
1738          */
1739         if (!magtapein) {
1740                 if (tpb->length % TP_BSIZE == 0
1741                     && tpb->length <= bufsize
1742                     && tpb->compressed == 0
1743                     && spclpt->c_type == TS_TAPE 
1744                     && spclpt->c_flags & DR_COMPRESSED) {
1745                         /* Looks like it's a compressed dump block prefix, */
1746                         /* read in the rest of the block based on tpb->length. */
1747                         len = tpb->length - TP_BSIZE + PREFIXSIZE;
1748                         if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) <= 0
1749                             || i != len)
1750                                 errx(1,"Error reading dump file header");
1751                         tbufptr = tpb->buf;
1752                         numtrec = ntrec = tpb->length / TP_BSIZE;
1753                         zflag = 1;   
1754                 }
1755                 else {
1756                         /* read in the rest of the block based on bufsize */
1757                         len = bufsize - TP_BSIZE;
1758                         if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1759                             || (i != len && i % TP_BSIZE != 0))
1760                                 errx(1,"Error reading dump file header");
1761                         tbufptr = tapebuf;
1762                         numtrec = ntrec;
1763                 }
1764                 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1765                 return;
1766         }
1767
1768         /*
1769          * If the input is a variable block size tape, we tried to
1770          * read PREFIXSIZE + ntrec * TP_BSIZE bytes. 
1771          * If it's not a compressed dump tape or the value of ntrec is 
1772          * too large, we have read less than * what we asked for; 
1773          * adjust the value of ntrec and test for * a compressed dump 
1774          * tape prefix.
1775          */
1776
1777         if (i % TP_BSIZE != 0) {
1778                 if (i % TP_BSIZE == PREFIXSIZE
1779                     && tpb->compressed == 0
1780                     && spclpt->c_type == TS_TAPE
1781                     && spclpt->c_flags & DR_COMPRESSED) {
1782
1783                         zflag = 1;
1784                         tbufptr = tpb->buf;
1785                         if (tpb->length > bufsize)
1786                                 errx(1, "Tape blocksize is too large, use "
1787                                         "\'-b %d\' ", tpb->length / TP_BSIZE);
1788                 }
1789                 else
1790                         errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1791                                 i, TP_BSIZE);
1792         }
1793         ntrec = i / TP_BSIZE;
1794         numtrec = ntrec;
1795         Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1796 }
1797
1798 /*
1799  * Read a block of data handling all of the messy details.
1800  */
1801 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
1802 {
1803         long i = 1, size;
1804
1805         size = len;
1806         while (size > 0) {
1807 #ifdef RRESTORE
1808                 if (host)
1809                         i = rmtread(buf, size);
1810                 else
1811 #endif
1812                         i = read(fd, buf, size);                 
1813
1814                 if (i <= 0)
1815                         break; /* EOD or error */
1816                 size -= i;
1817                 if (magtapein)
1818                         break; /* block at a time for mt */
1819                 buf += i;
1820         }
1821         *lengthread = len - size;
1822         return i;
1823 }
1824
1825 void
1826 closemt(void)
1827 {
1828
1829         if (mt < 0)
1830                 return;
1831 #ifdef RRESTORE
1832         if (host)
1833                 rmtclose();
1834         else
1835 #endif
1836                 (void) close(mt);
1837 }
1838
1839 /*
1840  * Read the next block from the tape.
1841  * Check to see if it is one of several vintage headers.
1842  * If it is an old style header, convert it to a new style header.
1843  * If it is not any valid header, return an error.
1844  */
1845 static int
1846 gethead(struct s_spcl *buf)
1847 {
1848         int32_t i;
1849         union {
1850                 quad_t  qval;
1851                 int32_t val[2];
1852         } qcvt;
1853         union u_ospcl {
1854                 char dummy[TP_BSIZE];
1855                 struct  s_ospcl {
1856                         int32_t c_type;
1857                         int32_t c_date;
1858                         int32_t c_ddate;
1859                         int32_t c_volume;
1860                         int32_t c_tapea;
1861                         u_int16_t c_inumber;
1862                         int32_t c_magic;
1863                         int32_t c_checksum;
1864                         struct odinode {
1865                                 u_int16_t odi_mode;
1866                                 u_int16_t odi_nlink;
1867                                 u_int16_t odi_uid;
1868                                 u_int16_t odi_gid;
1869                                 int32_t odi_size;
1870                                 int32_t odi_rdev;
1871                                 char    odi_addr[36];
1872                                 int32_t odi_atime;
1873                                 int32_t odi_mtime;
1874                                 int32_t odi_ctime;
1875                         } c_dinode;
1876                         int32_t c_count;
1877                         char    c_addr[256];
1878                 } s_ospcl;
1879         } u_ospcl;
1880
1881         if (!cvtflag) {
1882                 readtape((char *)buf);
1883                 if (buf->c_magic != NFS_MAGIC) {
1884                         if (swabi(buf->c_magic) != NFS_MAGIC)
1885                                 return (FAIL);
1886                         if (!Bcvt) {
1887                                 Vprintf(stdout, "Note: Doing Byte swapping\n");
1888                                 Bcvt = 1;
1889                         }
1890                 }
1891                 if (checksum((int *)buf) == FAIL)
1892                         return (FAIL);
1893                 if (Bcvt)
1894                         swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1895                 goto good;
1896         }
1897         readtape((char *)(&u_ospcl.s_ospcl));
1898         memset((char *)buf, 0, (long)TP_BSIZE);
1899         buf->c_type = u_ospcl.s_ospcl.c_type;
1900         buf->c_date = u_ospcl.s_ospcl.c_date;
1901         buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1902         buf->c_volume = u_ospcl.s_ospcl.c_volume;
1903         buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1904         buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1905         buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1906         buf->c_magic = u_ospcl.s_ospcl.c_magic;
1907         buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1908         buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1909         buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1910         buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1911         buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1912         buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1913 #ifdef  __linux__
1914         buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1915         buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1916         buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1917 #else   /* __linux__ */
1918         buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1919         buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1920         buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1921 #endif  /* __linux__ */
1922         buf->c_count = u_ospcl.s_ospcl.c_count;
1923         memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1924         if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1925             checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1926                 return(FAIL);
1927         buf->c_magic = NFS_MAGIC;
1928
1929 good:
1930         if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1931             (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1932                 qcvt.qval = buf->c_dinode.di_size;
1933                 if (qcvt.val[0] || qcvt.val[1]) {
1934                         printf("Note: Doing Quad swapping\n");
1935                         Qcvt = 1;
1936                 }
1937         }
1938         if (Qcvt) {
1939                 qcvt.qval = buf->c_dinode.di_size;
1940                 i = qcvt.val[1];
1941                 qcvt.val[1] = qcvt.val[0];
1942                 qcvt.val[0] = i;
1943                 buf->c_dinode.di_size = qcvt.qval;
1944         }
1945         readmapflag = 0;
1946
1947         switch (buf->c_type) {
1948
1949         case TS_CLRI:
1950         case TS_BITS:
1951                 /*
1952                  * Have to patch up missing information in bit map headers
1953                  */
1954                 buf->c_inumber = 0;
1955                 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1956                 if (buf->c_count > TP_NINDIR)
1957                         readmapflag = 1;
1958                 else 
1959                         for (i = 0; i < buf->c_count; i++)
1960                                 buf->c_addr[i]++;
1961                 break;
1962
1963         case TS_TAPE:
1964                 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1965                         oldinofmt = 1;
1966                 /* fall through */
1967         case TS_END:
1968                 buf->c_inumber = 0;
1969                 break;
1970
1971         case TS_INODE:
1972         case TS_ADDR:
1973                 break;
1974
1975         default:
1976                 panic("gethead: unknown inode type %d\n", buf->c_type);
1977                 break;
1978         }
1979         /*
1980          * If we are restoring a filesystem with old format inodes,
1981          * copy the uid/gid to the new location.
1982          */
1983         if (oldinofmt) {
1984                 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1985                 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1986         }
1987         if (dflag)
1988                 accthdr(buf);
1989         return(GOOD);
1990 }
1991
1992 /*
1993  * Check that a header is where it belongs and predict the next header
1994  */
1995 static void
1996 accthdr(struct s_spcl *header)
1997 {
1998         static dump_ino_t previno = 0x7fffffff;
1999         static int prevtype;
2000         static long predict;
2001         long blks, i;
2002
2003         if (header->c_type == TS_TAPE) {
2004                 fprintf(stderr, "Volume header (%s inode format) ",
2005                     oldinofmt ? "old" : "new");
2006                 if (header->c_firstrec)
2007                         fprintf(stderr, "begins with record %d",
2008                                 header->c_firstrec);
2009                 fprintf(stderr, "\n");
2010                 previno = 0x7fffffff;
2011                 return;
2012         }
2013         if (previno == 0x7fffffff)
2014                 goto newcalc;
2015         switch (prevtype) {
2016         case TS_BITS:
2017                 fprintf(stderr, "Dumped inodes map header");
2018                 break;
2019         case TS_CLRI:
2020                 fprintf(stderr, "Used inodes map header");
2021                 break;
2022         case TS_INODE:
2023                 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2024                 break;
2025         case TS_ADDR:
2026                 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2027                 break;
2028         case TS_END:
2029                 fprintf(stderr, "End of tape header");
2030                 break;
2031         }
2032         if (predict != blksread - 1)
2033                 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2034                         predict, blksread - 1);
2035         fprintf(stderr, "\n");
2036 newcalc:
2037         blks = 0;
2038         if (header->c_type != TS_END)
2039                 for (i = 0; i < header->c_count; i++)
2040                         if (readmapflag || header->c_addr[i] != 0)
2041                                 blks++;
2042         predict = blks;
2043         blksread = 0;
2044         prevtype = header->c_type;
2045         previno = header->c_inumber;
2046 }
2047
2048 /*
2049  * Find an inode header.
2050  * Complain if had to skip, and complain is set.
2051  */
2052 static void
2053 findinode(struct s_spcl *header)
2054 {
2055         static long skipcnt = 0;
2056         long i;
2057         char buf[TP_BSIZE];
2058
2059         curfile.name = "<name unknown>";
2060         curfile.action = UNKNOWN;
2061         curfile.dip = NULL;
2062         curfile.ino = 0;
2063         do {
2064                 if (header->c_magic != NFS_MAGIC) {
2065                         skipcnt++;
2066                         while (gethead(header) == FAIL ||
2067                             header->c_date != dumpdate)
2068                                 skipcnt++;
2069                 }
2070                 switch (header->c_type) {
2071
2072                 case TS_ADDR:
2073                         /*
2074                          * Skip up to the beginning of the next record
2075                          */
2076                         for (i = 0; i < header->c_count; i++)
2077                                 if (header->c_addr[i])
2078                                         readtape(buf);
2079                         while (gethead(header) == FAIL ||
2080                             header->c_date != dumpdate)
2081                                 skipcnt++;
2082                         break;
2083
2084                 case TS_INODE:
2085                         curfile.dip = &header->c_dinode;
2086                         curfile.ino = header->c_inumber;
2087                         break;
2088
2089                 case TS_END:
2090                         curfile.ino = maxino;
2091                         break;
2092
2093                 case TS_CLRI:
2094                         curfile.name = "<file removal list>";
2095                         break;
2096
2097                 case TS_BITS:
2098                         curfile.name = "<file dump list>";
2099                         break;
2100
2101                 case TS_TAPE:
2102                         panic("unexpected tape header\n");
2103                         /* NOTREACHED */
2104
2105                 default:
2106                         panic("unknown tape header type %d\n", spcl.c_type);
2107                         /* NOTREACHED */
2108
2109                 }
2110         } while (header->c_type == TS_ADDR);
2111         if (skipcnt > 0)
2112 #ifdef USE_QFA
2113                 if (!noresyncmesg)
2114 #endif
2115                         fprintf(stderr, "resync restore, skipped %ld blocks\n",
2116                                 skipcnt);
2117         skipcnt = 0;
2118 }
2119
2120 static int
2121 checksum(int *buf)
2122 {
2123         register int i, j;
2124
2125         j = sizeof(union u_spcl) / sizeof(int);
2126         i = 0;
2127         if(!Bcvt) {
2128                 do
2129                         i += *buf++;
2130                 while (--j);
2131         } else {
2132                 /* What happens if we want to read restore tapes
2133                         for a 16bit int machine??? */
2134                 do
2135                         i += swabi(*buf++);
2136                 while (--j);
2137         }
2138
2139         if (i != CHECKSUM) {
2140                 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2141                         (unsigned long)curfile.ino, curfile.name);
2142                 return(FAIL);
2143         }
2144         return(GOOD);
2145 }
2146
2147 #ifdef RRESTORE
2148 #ifdef __STDC__
2149 #include <stdarg.h>
2150 #else
2151 #include <varargs.h>
2152 #endif
2153
2154 void
2155 #ifdef __STDC__
2156 msg(const char *fmt, ...)
2157 #else
2158 msg(fmt, va_alist)
2159         char *fmt;
2160         va_dcl
2161 #endif
2162 {
2163         va_list ap;
2164 #ifdef __STDC__
2165         va_start(ap, fmt);
2166 #else
2167         va_start(ap);
2168 #endif
2169         (void)vfprintf(stderr, fmt, ap);
2170         va_end(ap);
2171 }
2172 #endif /* RRESTORE */
2173
2174 static u_char *
2175 swab16(u_char *sp, int n)
2176 {
2177         char c;
2178
2179         while (--n >= 0) {
2180                 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2181                 sp += 2;
2182         }
2183         return (sp);
2184 }
2185
2186 static u_char *
2187 swab32(u_char *sp, int n)
2188 {
2189         char c;
2190
2191         while (--n >= 0) {
2192                 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2193                 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2194                 sp += 4;
2195         }
2196         return (sp);
2197 }
2198
2199 static u_char *
2200 swab64(u_char *sp, int n)
2201 {
2202         char c;
2203
2204         while (--n >= 0) {
2205                 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2206                 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2207                 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2208                 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2209                 sp += 8;
2210         }
2211         return (sp);
2212 }
2213
2214 void
2215 swabst(u_char *cp, u_char *sp)
2216 {
2217         int n = 0;
2218
2219         while (*cp) {
2220                 switch (*cp) {
2221                 case '0': case '1': case '2': case '3': case '4':
2222                 case '5': case '6': case '7': case '8': case '9':
2223                         n = (n * 10) + (*cp++ - '0');
2224                         continue;
2225
2226                 case 's': case 'w': case 'h':
2227                         if (n == 0)
2228                                 n = 1;
2229                         sp = swab16(sp, n);
2230                         break;
2231
2232                 case 'i':
2233                         if (n == 0)
2234                                 n = 1;
2235                         sp = swab32(sp, n);
2236                         break;
2237
2238                 case 'l':
2239                         if (n == 0)
2240                                 n = 1;
2241                         sp = swab64(sp, n);
2242                         break;
2243
2244                 default: /* Any other character, like 'b' counts as byte. */
2245                         if (n == 0)
2246                                 n = 1;
2247                         sp += n;
2248                         break;
2249                 }
2250                 cp++;
2251                 n = 0;
2252         }
2253 }
2254
2255 static u_int
2256 swabi(u_int x)
2257 {
2258         swabst((u_char *)"i", (u_char *)&x);
2259         return (x);
2260 }
2261
2262 #if 0
2263 static u_long
2264 swabl(u_long x)
2265 {
2266         swabst((u_char *)"l", (u_char *)&x);
2267         return (x);
2268 }
2269 #endif
2270
2271 #ifdef USE_QFA
2272 /*
2273  * get the current position of the tape
2274  */
2275 int
2276 GetTapePos(long *pos)
2277 {
2278         int err = 0;
2279
2280         *pos = 0;
2281         if (ioctl(mt, MTIOCPOS, pos) == -1) {
2282                 err = errno;
2283                 fprintf(stdout, "[%ld] error: %d (getting tapepos: %ld)\n", 
2284                         (unsigned long)getpid(), err, *pos);
2285                 return err;
2286         }
2287         return err;
2288 }
2289
2290 typedef struct mt_pos {
2291         short    mt_op;
2292         int      mt_count;
2293 } MTPosRec, *MTPosPtr;
2294
2295 /*
2296  * go to specified position on tape
2297  */
2298 int
2299 GotoTapePos(long pos)
2300 {
2301         int err = 0;
2302         struct mt_pos buf;
2303
2304         buf.mt_op = MTSEEK;
2305         buf.mt_count = pos;
2306         if (ioctl(mt, MTIOCTOP, &buf) == -1) {
2307                 err = errno;
2308                 fprintf(stdout, "[%ld] error: %d (setting tapepos: %ld)\n", 
2309                         (unsigned long)getpid(), err, pos);
2310                 return err;
2311         }
2312         return err;
2313 }
2314
2315 /*
2316  * read next data from tape to re-sync
2317  */
2318 void
2319 ReReadFromTape(void)
2320 {
2321         FLUSHTAPEBUF();
2322         noresyncmesg = 1;
2323         if (gethead(&spcl) == FAIL) {
2324 #ifdef DEBUG_QFA
2325                 fprintf(stdout, "DEBUG 1 gethead failed\n");
2326 #endif
2327         }
2328         findinode(&spcl);
2329         noresyncmesg = 0;
2330 }
2331
2332 void
2333 RequestVol(long tnum)
2334 {
2335         FLUSHTAPEBUF();
2336         getvol(tnum);
2337 }
2338 #endif /* USE_QFA */