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