]> git.wh0rd.org Git - dump.git/blob - restore/tape.c
Discard newline when reading the tape path entered by the operator in restore.
[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.41 2001/07/18 11:46:31 stelian Exp $";
50 #endif /* not lint */
51
52 #include <config.h>
53 #include <compatlfs.h>
54 #include <errno.h>
55 #include <compaterr.h>
56 #include <setjmp.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 #include <sys/param.h>
63 #include <sys/file.h>
64 #include <sys/mtio.h>
65 #include <sys/stat.h>
66
67 #ifdef  __linux__
68 #include <sys/time.h>
69 #include <time.h>
70 #ifdef HAVE_EXT2FS_EXT2_FS_H
71 #include <ext2fs/ext2_fs.h>
72 #else
73 #include <linux/ext2_fs.h>
74 #endif
75 #include <ext2fs/ext2fs.h>
76 #include <bsdcompat.h>
77 #else   /* __linux__ */
78 #include <ufs/ufs/dinode.h>
79 #endif  /* __linux__ */
80 #include <protocols/dumprestore.h>
81
82 #ifdef HAVE_ZLIB
83 #include <zlib.h>
84 #endif /* HAVE_ZLIB */
85
86 #include "restore.h"
87 #include "extern.h"
88 #include "pathnames.h"
89
90 #ifdef USE_QFA
91 int             noresyncmesg = 0;
92 #endif /* USE_QFA */
93 static long     fssize = MAXBSIZE;
94 static int      mt = -1;
95 static int      pipein = 0;
96 static int      magtapein = 0;          /* input is from magtape */
97 static char     magtape[MAXPATHLEN];
98 static char     magtapeprefix[MAXPATHLEN];
99 static int      blkcnt;
100 static int      numtrec;
101 static char     *tapebuf;               /* input buffer for read */
102 static int      bufsize;                /* buffer size without prefix */
103 static char     *tbufptr = NULL;        /* active tape buffer */
104 #ifdef HAVE_ZLIB
105 static char     *comprbuf;              /* uncompress work buf */
106 static size_t   comprlen;               /* size including prefix */
107 #endif
108 static union    u_spcl endoftapemark;
109 static long     blksread;               /* blocks read since last header */
110 static long     tpblksread = 0;         /* TP_BSIZE blocks read */
111 static long     tapesread;
112 static sigjmp_buf       restart;
113 static int      gettingfile = 0;        /* restart has a valid frame */
114 static char     *host = NULL;
115
116 static int      ofile;
117 static char     *map;
118 static char     lnkbuf[MAXPATHLEN + 1];
119 static int      pathlen;
120
121 int             oldinofmt;      /* old inode format conversion required */
122 int             Bcvt;           /* Swap Bytes (for CCI or sun) */
123 static int      Qcvt;           /* Swap quads (for sun) */
124
125 #define FLUSHTAPEBUF()  blkcnt = ntrec + 1
126
127 static void      accthdr __P((struct s_spcl *));
128 static int       checksum __P((int *));
129 static void      findinode __P((struct s_spcl *));
130 static void      findtapeblksize __P((void));
131 static int       gethead __P((struct s_spcl *));
132 static void      readtape __P((char *));
133 static void      setdumpnum __P((void));
134 static u_int     swabi __P((u_int));
135 #if 0
136 static u_long    swabl __P((u_long));
137 #endif
138 static u_char   *swab64 __P((u_char *, int));
139 static u_char   *swab32 __P((u_char *, int));
140 static u_char   *swab16 __P((u_char *, int));
141 static void      terminateinput __P((void));
142 static void      xtrfile __P((char *, size_t));
143 static void      xtrlnkfile __P((char *, size_t));
144 static void      xtrlnkskip __P((char *, size_t));
145 static void      xtrmap __P((char *, size_t));
146 static void      xtrmapskip __P((char *, size_t));
147 static void      xtrskip __P((char *, size_t));
148 static void      setmagtapein __P((void));
149
150 #ifdef HAVE_ZLIB
151 static void     newcomprbuf __P((int));
152 static void     readtape_set __P((char *));
153 static void     readtape_uncompr __P((char *));
154 static void     readtape_comprfile __P((char *));
155 static void     readtape_comprtape __P((char *));
156 static char     *decompress_tapebuf __P((struct tapebuf *, int));
157 static void     msg_read_error __P((char *));
158 #endif
159 static int      read_a_block __P((int, char *, size_t, long *));
160 #define PREFIXSIZE      sizeof(struct tapebuf)
161
162 #define COMPARE_ONTHEFLY 1
163
164 #if COMPARE_ONTHEFLY
165 static int      ifile;          /* input file for compare */
166 static int      cmperror;       /* compare error */
167 static void     xtrcmpfile __P((char *, size_t));
168 static void     xtrcmpskip __P((char *, size_t));
169 #endif
170
171 static int readmapflag;
172
173 /*
174  * Set up an input source. This is called from main.c before setup() is.
175  */
176 void
177 setinput(char *source)
178 {
179         FLUSHTAPEBUF();
180         if (bflag)
181                 newtapebuf(ntrec);
182         else
183                 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
184         terminal = stdin;
185
186 #ifdef RRESTORE
187         if (strchr(source, ':')) {
188                 host = source;
189                 source = strchr(host, ':');
190                 *source++ = '\0';
191                 if (rmthost(host) == 0)
192                         exit(1);
193         } else
194 #endif
195         if (strcmp(source, "-") == 0) {
196                 /*
197                  * Since input is coming from a pipe we must establish
198                  * our own connection to the terminal.
199                  */
200                 terminal = fopen(_PATH_TTY, "r");
201                 if (terminal == NULL) {
202                         warn("cannot open %s", _PATH_TTY);
203                         terminal = fopen(_PATH_DEVNULL, "r");
204                         if (terminal == NULL)
205                                 err(1, "cannot open %s", _PATH_DEVNULL);
206                 }
207                 pipein++;
208         }
209         setuid(getuid());       /* no longer need or want root privileges */
210         if (Mflag) {
211                 strncpy(magtapeprefix, source, MAXPATHLEN);
212                 magtapeprefix[MAXPATHLEN-1] = '\0';
213                 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
214         }
215         else
216                 strncpy(magtape, source, MAXPATHLEN);
217         magtape[MAXPATHLEN - 1] = '\0';
218 }
219
220 void
221 newtapebuf(long size)
222 {
223         static int tapebufsize = -1;
224
225         ntrec = size;
226         bufsize = ntrec * TP_BSIZE;
227         if (size <= tapebufsize)
228                 return;
229         if (tapebuf != NULL)
230                 free(tapebuf);
231         tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
232         if (tapebuf == NULL)
233                 errx(1, "Cannot allocate space for tape buffer");
234         tapebufsize = size;
235 }
236
237 #ifdef HAVE_ZLIB
238 static void
239 newcomprbuf(int size)
240 {
241         size_t buf_size = (size+1) * TP_BSIZE + sizeof(struct tapebuf);
242         if (buf_size <= comprlen)
243                 return;
244         comprlen = buf_size;
245         if (comprbuf != NULL)
246                 free(comprbuf);
247         comprbuf = malloc(comprlen);
248         if (comprbuf == NULL)
249                 errx(1, "Cannot allocate space for decompress buffer");
250 }
251 #endif /* HAVE_ZLIB */
252
253 /*
254  * Verify that the tape drive can be accessed and
255  * that it actually is a dump tape.
256  */
257 void
258 setup(void)
259 {
260         int i, j, *ip;
261         struct STAT stbuf;
262
263         Vprintf(stdout, "Verify tape and initialize maps\n");
264 #ifdef RRESTORE
265         if (host)
266                 mt = rmtopen(magtape, 0);
267         else
268 #endif
269         if (pipein)
270                 mt = 0;
271         else
272                 mt = OPEN(magtape, O_RDONLY, 0);
273         if (mt < 0)
274                 err(1, "%s", magtape);
275         volno = 1;
276         setmagtapein();
277         setdumpnum();
278         FLUSHTAPEBUF();
279         findtapeblksize();
280         if (gethead(&spcl) == FAIL) {
281                 blkcnt--; /* push back this block */
282                 blksread--;
283                 tpblksread--;
284                 cvtflag++;
285                 if (gethead(&spcl) == FAIL)
286                         errx(1, "Tape is not a dump tape");
287                 fprintf(stderr, "Converting to new file system format.\n");
288         }
289
290         if (zflag) {
291                 fprintf(stderr, "Dump tape is compressed.\n");
292 #ifdef HAVE_ZLIB
293                 newcomprbuf(ntrec);
294 #else
295                 errx(1,"This restore version doesn't support decompression");
296 #endif /* HAVE_ZLIB */
297         }
298         if (pipein) {
299                 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
300                 endoftapemark.s_spcl.c_type = TS_END;
301                 ip = (int *)&endoftapemark;
302                 j = sizeof(union u_spcl) / sizeof(int);
303                 i = 0;
304                 do
305                         i += *ip++;
306                 while (--j);
307                 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
308         }
309         if (vflag || command == 't' || command == 'C')
310                 printdumpinfo();
311         if (filesys[0] == '\0') {
312                 char *dirptr;
313                 strncpy(filesys, spcl.c_filesys, NAMELEN);
314                 filesys[NAMELEN - 1] = '\0';
315                 dirptr = strstr(filesys, " (dir");
316                 if (dirptr != NULL)
317                         *dirptr = '\0';
318         }
319         dumptime = spcl.c_ddate;
320         dumpdate = spcl.c_date;
321         if (STAT(".", &stbuf) < 0)
322                 err(1, "cannot stat .");
323         if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
324                 fssize = TP_BSIZE;
325         if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
326                 fssize = stbuf.st_blksize;
327         if (((fssize - 1) & fssize) != 0)
328                 errx(1, "bad block size %ld", fssize);
329         if (spcl.c_volume != 1)
330                 errx(1, "Tape is not volume 1 of the dump");
331         if (gethead(&spcl) == FAIL) {
332                 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
333                 panic("no header after volume mark!\n");
334         }
335         findinode(&spcl);
336         if (spcl.c_type != TS_CLRI)
337                 errx(1, "Cannot find file removal list");
338         maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
339         Dprintf(stdout, "maxino = %ld\n", (long)maxino);
340         map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
341         if (map == NULL)
342                 errx(1, "no memory for active inode map");
343         usedinomap = map;
344         curfile.action = USING;
345         getfile(xtrmap, xtrmapskip);
346         if (spcl.c_type != TS_BITS)
347                 errx(1, "Cannot find file dump list");
348         map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
349         if (map == (char *)NULL)
350                 errx(1, "no memory for file dump list");
351         dumpmap = map;
352         curfile.action = USING;
353         getfile(xtrmap, xtrmapskip);
354         /*
355          * If there may be whiteout entries on the tape, pretend that the
356          * whiteout inode exists, so that the whiteout entries can be
357          * extracted.
358          */
359         if (oldinofmt == 0)
360                 SETINO(WINO, dumpmap);
361 }
362
363 /*
364  * Prompt user to load a new dump volume.
365  * "Nextvol" is the next suggested volume to use.
366  * This suggested volume is enforced when doing full
367  * or incremental restores, but can be overridden by
368  * the user when only extracting a subset of the files.
369  */
370 void
371 getvol(long nextvol)
372 {
373         long newvol = 0, savecnt = 0, wantnext = 0, i;
374         union u_spcl tmpspcl;
375 #       define tmpbuf tmpspcl.s_spcl
376         char buf[TP_BSIZE];
377         int haderror = 0;
378
379         if (nextvol == 1) {
380                 tapesread = 0;
381                 gettingfile = 0;
382         }
383         if (pipein) {
384                 if (nextvol != 1)
385                         panic("Changing volumes on pipe input?\n");
386                 if (volno == 1)
387                         return;
388                 goto gethdr;
389         }
390         savecnt = blksread;
391 again:
392         if (pipein)
393                 exit(1); /* pipes do not get a second chance */
394         if (command == 'R' || command == 'r' || curfile.action != SKIP) {
395                 newvol = nextvol;
396                 wantnext = 1;
397         } else {
398                 newvol = 0;
399                 wantnext = 0;
400         }
401         while (newvol <= 0) {
402                 if (tapesread == 0) {
403                         fprintf(stderr, "%s%s%s%s%s",
404                             "You have not read any tapes yet.\n",
405                             "Unless you know which volume your",
406                             " file(s) are on you should start\n",
407                             "with the last volume and work",
408                             " towards the first.\n");
409                 } else {
410                         fprintf(stderr, "You have read volumes");
411                         strcpy(buf, ": ");
412                         for (i = 1; i < 32; i++)
413                                 if (tapesread & (1 << i)) {
414                                         fprintf(stderr, "%s%ld", buf, (long)i);
415                                         strcpy(buf, ", ");
416                                 }
417                         fprintf(stderr, "\n");
418                 }
419                 do      {
420                         fprintf(stderr, "Specify next volume #: ");
421                         (void) fflush(stderr);
422                         (void) fgets(buf, TP_BSIZE, terminal);
423                 } while (!feof(terminal) && buf[0] == '\n');
424                 if (feof(terminal))
425                         exit(1);
426                 newvol = atoi(buf);
427                 if (newvol <= 0) {
428                         fprintf(stderr,
429                             "Volume numbers are positive numerics\n");
430                 }
431         }
432         if (newvol == volno) {
433                 tapesread |= 1 << volno;
434                 return;
435         }
436         closemt();
437         if (Mflag) {
438                 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
439                 magtape[MAXPATHLEN - 1] = '\0';
440         }
441         if (!Mflag || haderror) {
442                 haderror = 0;
443                 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
444                 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
445                 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
446                 (void) fflush(stderr);
447                 (void) fgets(buf, TP_BSIZE, terminal);
448                 if (feof(terminal))
449                         exit(1);
450                 if (!strcmp(buf, "none\n")) {
451                         terminateinput();
452                         return;
453                 }
454                 if (buf[0] != '\n') {
455                         char *pos;
456                         (void) strncpy(magtape, buf, sizeof(magtape));
457                         magtape[sizeof(magtape) - 1] = '\0';
458                         if ((pos = strchr(magtape, '\n'))) 
459                                 magtape[pos - magtape] = '\0';
460                 }
461         }
462 #ifdef RRESTORE
463         if (host)
464                 mt = rmtopen(magtape, 0);
465         else
466 #endif
467                 mt = OPEN(magtape, O_RDONLY, 0);
468
469         if (mt == -1) {
470                 fprintf(stderr, "Cannot open %s\n", magtape);
471                 volno = -1;
472                 haderror = 1;
473                 goto again;
474         }
475 gethdr:
476         setmagtapein();
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                 while (size > 0) {
847                         size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
848                         (*skip)(clearedbuf, skp);
849                         size -= skp;
850                 }
851                 last_write_was_hole = 1;
852         }
853         if (last_write_was_hole) {
854                 FTRUNCATE(ofile, origsize);
855         }
856         findinode(&spcl);
857         gettingfile = 0;
858 }
859
860 /*
861  * Write out the next block of a file.
862  */
863 static void
864 xtrfile(char *buf, size_t size)
865 {
866
867         if (Nflag)
868                 return;
869         if (write(ofile, buf, (int) size) == -1)
870                 err(1, "write error extracting inode %lu, name %s\nwrite",
871                         (unsigned long)curfile.ino, curfile.name);
872 }
873
874 /*
875  * Skip over a hole in a file.
876  */
877 /* ARGSUSED */
878 static void
879 xtrskip(char *buf, size_t size)
880 {
881
882         if (LSEEK(ofile, (off_t)size, SEEK_CUR) == -1)
883                 err(1, "seek error extracting inode %lu, name %s\nlseek",
884                         (unsigned long)curfile.ino, curfile.name);
885 }
886
887 /*
888  * Collect the next block of a symbolic link.
889  */
890 static void
891 xtrlnkfile(char *buf, size_t size)
892 {
893
894         pathlen += size;
895         if (pathlen > MAXPATHLEN)
896                 errx(1, "symbolic link name: %s->%s%s; too long %d",
897                     curfile.name, lnkbuf, buf, pathlen);
898         (void) strcat(lnkbuf, buf);
899 }
900
901 /*
902  * Skip over a hole in a symbolic link (should never happen).
903  */
904 /* ARGSUSED */
905 static void
906 xtrlnkskip(char *buf, size_t size)
907 {
908
909         errx(1, "unallocated block in symbolic link %s", curfile.name);
910 }
911
912 /*
913  * Collect the next block of a bit map.
914  */
915 static void
916 xtrmap(char *buf, size_t size)
917 {
918
919         memmove(map, buf, size);
920         map += size;
921 }
922
923 /*
924  * Skip over a hole in a bit map (should never happen).
925  */
926 /* ARGSUSED */
927 static void
928 xtrmapskip(char *buf, size_t size)
929 {
930
931         panic("hole in map\n");
932         map += size;
933 }
934
935 /*
936  * Noop, when an extraction function is not needed.
937  */
938 /* ARGSUSED */
939 void
940 xtrnull(char *buf, size_t size)
941 {
942
943         return;
944 }
945
946 #if COMPARE_ONTHEFLY
947 /*
948  * Compare the next block of a file.
949  */
950 static void
951 xtrcmpfile(char *buf, size_t size)
952 {
953         static char cmpbuf[MAXBSIZE];
954
955         if (cmperror)
956                 return;
957         
958         if (read(ifile, cmpbuf, size) != size) {
959                 fprintf(stderr, "%s: size has changed.\n", 
960                         curfile.name);
961                 cmperror = 1;
962                 return;
963         }
964         
965         if (memcmp(buf, cmpbuf, size) != 0) {
966                 fprintf(stderr, "%s: tape and disk copies are different\n",
967                         curfile.name);
968                 cmperror = 1;
969                 return;
970         }
971 }
972
973 /*
974  * Skip over a hole in a file.
975  */
976 static void
977 xtrcmpskip(char *buf, size_t size)
978 {
979         static char cmpbuf[MAXBSIZE];
980         int i;
981
982         if (cmperror)
983                 return;
984         
985         if (read(ifile, cmpbuf, size) != size) {
986                 fprintf(stderr, "%s: size has changed.\n", 
987                         curfile.name);
988                 cmperror = 1;
989                 return;
990         }
991
992         for (i = 0; i < size; ++i)
993                 if (cmpbuf[i] != '\0') {
994                         fprintf(stderr, "%s: tape and disk copies are different\n",
995                                 curfile.name);
996                         cmperror = 1;
997                         return;
998                 }
999 }
1000 #endif /* COMPARE_ONTHEFLY */
1001
1002 #if !COMPARE_ONTHEFLY
1003 static int
1004 do_cmpfiles(int fd_tape, int fd_disk, long size)
1005 {
1006         static char buf_tape[BUFSIZ];
1007         static char buf_disk[BUFSIZ];
1008         ssize_t n_tape;
1009         ssize_t n_disk;
1010
1011         while (size > 0) {
1012                 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1013                         close(fd_tape), close(fd_disk);
1014                         panic("do_cmpfiles: unexpected EOF[1]");
1015                 }
1016                 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1017                         close(fd_tape), close(fd_disk);
1018                         panic("do_cmpfiles: unexpected EOF[2]");
1019                 }
1020                 if (n_tape != n_disk) {
1021                         close(fd_tape), close(fd_disk);
1022                         panic("do_cmpfiles: sizes different!");
1023                 }
1024                 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1025                 size -= n_tape;
1026         }
1027         return (0);
1028 }
1029
1030 /* for debugging compare problems */
1031 #undef COMPARE_FAIL_KEEP_FILE
1032
1033 static
1034 #ifdef COMPARE_FAIL_KEEP_FILE
1035 /* return true if tapefile should be unlinked after compare */
1036 int
1037 #else
1038 void
1039 #endif
1040 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1041 {
1042         struct STAT sbuf_tape;
1043         int fd_tape, fd_disk;
1044
1045         if (STAT(tapefile, &sbuf_tape) != 0) {
1046                 panic("Can't lstat tmp file %s: %s\n", tapefile,
1047                       strerror(errno));
1048                 compare_errors = 1;
1049         }
1050
1051         if (sbuf_disk->st_size != sbuf_tape.st_size) {
1052                 fprintf(stderr,
1053                         "%s: size changed from %ld to %ld.\n",
1054                         diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1055                 compare_errors = 1;
1056 #ifdef COMPARE_FAIL_KEEP_FILE
1057                 return (0);
1058 #else
1059                 return;
1060 #endif
1061         }
1062
1063         if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1064                 panic("Can't open %s: %s\n", tapefile, strerror(errno));
1065                 compare_errors = 1;
1066         }
1067         if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1068                 close(fd_tape);
1069                 panic("Can't open %s: %s\n", diskfile, strerror(errno));
1070                 compare_errors = 1;
1071         }
1072
1073         if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1074                 fprintf(stderr, "%s: tape and disk copies are different\n",
1075                         diskfile);
1076                 close(fd_tape);
1077                 close(fd_disk);
1078                 compare_errors = 1;
1079 #ifdef COMPARE_FAIL_KEEP_FILE
1080                 /* rename the file to live in /tmp */
1081                 /* rename `tapefile' to /tmp/<basename of diskfile> */
1082                 {
1083                         char *p = strrchr(diskfile, '/');
1084                         char newname[MAXPATHLEN];
1085                         if (!p) {
1086                                 panic("can't find / in %s\n", diskfile);
1087                         }
1088                         snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1089                         if (rename(tapefile, newname)) {
1090                                 panic("rename from %s to %s failed: %s\n",
1091                                       tapefile, newname,
1092                                       strerror(errno));
1093                         } else {
1094                                 fprintf(stderr, "*** %s saved to %s\n",
1095                                         tapefile, newname);
1096                         }
1097                 }
1098                 
1099                 /* don't unlink the file (it's not there anymore */
1100                 /* anyway) */
1101                 return (0);
1102 #else
1103                 return;
1104 #endif
1105         }
1106         close(fd_tape);
1107         close(fd_disk);
1108 #ifdef COMPARE_FAIL_KEEP_FILE
1109         return (1);
1110 #endif
1111 }
1112 #endif /* !COMPARE_ONTHEFLY */
1113
1114 #if !COMPARE_ONTHEFLY
1115 static char tmpfilename[MAXPATHLEN];
1116 #endif
1117
1118 void
1119 comparefile(char *name)
1120 {
1121         int mode;
1122         struct STAT sb;
1123         int r;
1124 #if !COMPARE_ONTHEFLY
1125         static char *tmpfile = NULL;
1126         struct STAT stemp;
1127 #endif
1128
1129         if ((r = LSTAT(name, &sb)) != 0) {
1130                 warn("%s: does not exist (%d)", name, r);
1131                 compare_errors = 1;
1132                 skipfile();
1133                 return;
1134         }
1135
1136         curfile.name = name;
1137         curfile.action = USING;
1138         mode = curfile.dip->di_mode;
1139
1140         Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
1141                 (long)sb.st_size, mode);
1142
1143         if (sb.st_mode != mode) {
1144                 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1145                         name, mode & 07777, sb.st_mode & 07777);
1146                 compare_errors = 1;
1147         }
1148         switch (mode & IFMT) {
1149         default:
1150                 skipfile();
1151                 return;
1152
1153         case IFSOCK:
1154                 skipfile();
1155                 return;
1156
1157         case IFDIR:
1158                 skipfile();
1159                 return;
1160
1161         case IFLNK: {
1162                 char lbuf[MAXPATHLEN + 1];
1163                 int lsize;
1164
1165                 if (!(sb.st_mode & S_IFLNK)) {
1166                         fprintf(stderr, "%s: is no longer a symbolic link\n",
1167                                 name);
1168                         compare_errors = 1;
1169                         return;
1170                 }
1171                 lnkbuf[0] = '\0';
1172                 pathlen = 0;
1173                 getfile(xtrlnkfile, xtrlnkskip);
1174                 if (pathlen == 0) {
1175                         fprintf(stderr,
1176                                 "%s: zero length symbolic link (ignored)\n",
1177                                 name);
1178                         compare_errors = 1;
1179                         return;
1180                 }
1181                 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1182                         panic("readlink of %s failed: %s", name,
1183                               strerror(errno));
1184                         compare_errors = 1;
1185                 }
1186                 lbuf[lsize] = 0;
1187                 if (strcmp(lbuf, lnkbuf) != 0) {
1188                         fprintf(stderr,
1189                                 "%s: symbolic link changed from %s to %s.\n",
1190                                 name, lnkbuf, lbuf);
1191                         compare_errors = 1;
1192                         return;
1193                 }
1194                 return;
1195         }
1196
1197         case IFCHR:
1198         case IFBLK:
1199                 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1200                         fprintf(stderr, "%s: no longer a special file\n",
1201                                 name);
1202                         compare_errors = 1;
1203                         skipfile();
1204                         return;
1205                 }
1206
1207                 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1208                         fprintf(stderr,
1209                                 "%s: device changed from %d,%d to %d,%d.\n",
1210                                 name,
1211                                 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1212                                 (int)curfile.dip->di_rdev & 0xff,
1213                                 ((int)sb.st_rdev >> 8) & 0xff,
1214                                 (int)sb.st_rdev & 0xff);
1215                         compare_errors = 1;
1216                 }
1217                 skipfile();
1218                 return;
1219
1220         case IFREG:
1221 #if COMPARE_ONTHEFLY
1222                 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1223                         panic("Can't open %s: %s\n", name, strerror(errno));
1224                         skipfile();
1225                         compare_errors = 1;
1226                 }
1227                 else {
1228                         cmperror = 0;
1229                         getfile(xtrcmpfile, xtrcmpskip);
1230                         if (!cmperror) {
1231                                 char c;
1232                                 if (read(ifile, &c, 1) != 0) {
1233                                         fprintf(stderr, "%s: size has changed.\n", 
1234                                                 name);
1235                                         cmperror = 1;
1236                                 }
1237                         }
1238                         if (cmperror)
1239                                 compare_errors = 1;
1240                         close(ifile);
1241                 }
1242 #else
1243                 if (tmpfile == NULL) {
1244                         /* argument to mktemp() must not be in RO space: */
1245                         snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1246                         tmpfile = mktemp(&tmpfilename[0]);
1247                 }
1248                 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1249                         panic("cannot delete tmp file %s: %s\n",
1250                               tmpfile, strerror(errno));
1251                 }
1252                 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1253                         panic("cannot create file temp file %s: %s\n",
1254                               name, strerror(errno));
1255                 }
1256                 getfile(xtrfile, xtrskip);
1257                 (void) close(ofile);
1258 #ifdef COMPARE_FAIL_KEEP_FILE
1259                 if (cmpfiles(tmpfile, name, &sb))
1260                         unlink(tmpfile);
1261 #else
1262                 cmpfiles(tmpfile, name, &sb);
1263                 unlink(tmpfile);
1264 #endif
1265 #endif /* COMPARE_ONTHEFLY */
1266                 return;
1267         }
1268         /* NOTREACHED */
1269 }
1270
1271 #ifdef HAVE_ZLIB
1272 static void (*readtape_func)(char *) = readtape_set;
1273
1274 /*
1275  * Read TP_BSIZE blocks from the input.
1276  * Handle read errors, and end of media.
1277  * Decompress compressed blocks.
1278  */
1279 static void
1280 readtape(char *buf)
1281 {
1282         (*readtape_func)(buf);  /* call the actual processing routine */
1283 }
1284
1285 /*
1286  * Set function pointer for readtape() routine. zflag and magtapein must
1287  * be correctly set before the first call to readtape().
1288  */
1289 static void
1290 readtape_set(char *buf)
1291 {
1292         if (!zflag) 
1293                 readtape_func = readtape_uncompr;
1294         else {
1295                 newcomprbuf(ntrec);
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 get the blocksize from it. Test
1699  * for a compressed dump tape/file. setup() will make the final
1700  * 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 is not compressed
1705  * and does not have a prefix.
1706  */ 
1707 static void
1708 findtapeblksize(void)
1709 {
1710         long i;
1711         size_t len;
1712         struct tapebuf *tpb = (struct tapebuf *) tapebuf;
1713         struct s_spcl *spclpt = (struct s_spcl *) tapebuf;
1714
1715         for (i = 0; i < ntrec; i++)
1716                 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1717         blkcnt = 0;
1718         tbufptr = tapebuf;
1719         /*
1720          * For a pipe or file, read in the first record. For a tape, read
1721          * the first block.
1722          */
1723         len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
1724
1725         if (read_a_block(mt, tapebuf, len, &i) <= 0)
1726                 errx(1, "Tape read error on first record");
1727
1728         /*
1729          * If the input is from a file or a pipe, we read TP_BSIZE
1730          * bytes looking for a dump header. If the dump is compressed
1731          * we need to read in the rest of the block, as determined
1732          * by c_ntrec in the dump header. The first block of the
1733          * dump is not compressed and does not have a prefix.
1734          */
1735         if (!magtapein) {
1736                 if (spclpt->c_type == TS_TAPE
1737                     && spclpt->c_flags & DR_COMPRESSED) {
1738                         /* It's a compressed dump file, read in the */
1739                         /* rest of the block based on spclpt->c_ntrec. */
1740                         if (spclpt->c_ntrec > ntrec)
1741                                 errx(1, "Tape blocksize is too large, use "
1742                                      "\'-b %d\' ", spclpt->c_ntrec);
1743                         ntrec = spclpt->c_ntrec;
1744                         len = (ntrec - 1) * TP_BSIZE;
1745                         zflag = 1;   
1746                 }
1747                 else {
1748                         /* read in the rest of the block based on bufsize */
1749                         len = bufsize - TP_BSIZE;
1750                 }
1751                 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
1752                     || (i != len && i % TP_BSIZE != 0))
1753                         errx(1,"Error reading dump file header");
1754                 tbufptr = tapebuf;
1755                 numtrec = ntrec;
1756                 Vprintf(stdout, "Input block size is %ld\n", ntrec);
1757                 return;
1758         } /* if (!magtapein) */
1759
1760         /*
1761          * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
1762          * If the value of ntrec is too large, we read less than
1763          * what we asked for; adjust the value of ntrec and test for 
1764          * a compressed dump tape.
1765          */
1766
1767         if (i % TP_BSIZE != 0) {
1768                 /* may be old format compressed dump tape with a prefix */
1769                 spclpt = (struct s_spcl *) tpb->buf;
1770                 if (i % TP_BSIZE == PREFIXSIZE
1771                     && tpb->compressed == 0
1772                     && spclpt->c_type == TS_TAPE
1773                     && spclpt->c_flags & DR_COMPRESSED) {
1774                         zflag = 1;
1775                         tbufptr = tpb->buf;
1776                         if (tpb->length > bufsize)
1777                                 errx(1, "Tape blocksize is too large, use "
1778                                         "\'-b %d\' ", tpb->length / TP_BSIZE);
1779                 }
1780                 else
1781                         errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1782                                 i, TP_BSIZE);
1783         }
1784         ntrec = i / TP_BSIZE;
1785         if (spclpt->c_type == TS_TAPE) {
1786                 if (spclpt->c_flags & DR_COMPRESSED)
1787                         zflag = 1;
1788                 if (spclpt->c_ntrec > ntrec)
1789                         errx(1, "Tape blocksize is too large, use "
1790                                 "\'-b %d\' ", spclpt->c_ntrec);
1791         }
1792         numtrec = ntrec;
1793         Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1794 }
1795
1796 /*
1797  * Read a block of data handling all of the messy details.
1798  */
1799 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
1800 {
1801         long i = 1, size;
1802
1803         size = len;
1804         while (size > 0) {
1805 #ifdef RRESTORE
1806                 if (host)
1807                         i = rmtread(buf, size);
1808                 else
1809 #endif
1810                         i = read(fd, buf, size);                 
1811
1812                 if (i <= 0)
1813                         break; /* EOD or error */
1814                 size -= i;
1815                 if (magtapein)
1816                         break; /* block at a time for mt */
1817                 buf += i;
1818         }
1819         *lengthread = len - size;
1820         return i;
1821 }
1822
1823 void
1824 closemt(void)
1825 {
1826
1827         if (mt < 0)
1828                 return;
1829 #ifdef RRESTORE
1830         if (host)
1831                 rmtclose();
1832         else
1833 #endif
1834                 (void) close(mt);
1835 }
1836
1837 static void
1838 setmagtapein(void) {
1839         struct mtget mt_stat;
1840         static int done = 0;
1841         if (done)
1842                 return;
1843         done = 1;
1844         if (!pipein) {
1845                 /* need to know if input is really from a tape */
1846 #ifdef RRESTORE
1847                 if (host)
1848                         magtapein = rmtioctl(MTNOP, 1) != -1;
1849                 else
1850 #endif
1851                         magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
1852         }
1853
1854         Vprintf(stdout,"Input is from %s\n", magtapein? "tape": "file/pipe");
1855 }
1856
1857 /*
1858  * Read the next block from the tape.
1859  * Check to see if it is one of several vintage headers.
1860  * If it is an old style header, convert it to a new style header.
1861  * If it is not any valid header, return an error.
1862  */
1863 static int
1864 gethead(struct s_spcl *buf)
1865 {
1866         int32_t i;
1867         union {
1868                 quad_t  qval;
1869                 int32_t val[2];
1870         } qcvt;
1871         union u_ospcl {
1872                 char dummy[TP_BSIZE];
1873                 struct  s_ospcl {
1874                         int32_t c_type;
1875                         int32_t c_date;
1876                         int32_t c_ddate;
1877                         int32_t c_volume;
1878                         int32_t c_tapea;
1879                         u_int16_t c_inumber;
1880                         int32_t c_magic;
1881                         int32_t c_checksum;
1882                         struct odinode {
1883                                 u_int16_t odi_mode;
1884                                 u_int16_t odi_nlink;
1885                                 u_int16_t odi_uid;
1886                                 u_int16_t odi_gid;
1887                                 int32_t odi_size;
1888                                 int32_t odi_rdev;
1889                                 char    odi_addr[36];
1890                                 int32_t odi_atime;
1891                                 int32_t odi_mtime;
1892                                 int32_t odi_ctime;
1893                         } c_dinode;
1894                         int32_t c_count;
1895                         char    c_addr[256];
1896                 } s_ospcl;
1897         } u_ospcl;
1898
1899         if (!cvtflag) {
1900                 readtape((char *)buf);
1901                 if (buf->c_magic != NFS_MAGIC) {
1902                         if (swabi(buf->c_magic) != NFS_MAGIC)
1903                                 return (FAIL);
1904                         if (!Bcvt) {
1905                                 Vprintf(stdout, "Note: Doing Byte swapping\n");
1906                                 Bcvt = 1;
1907                         }
1908                 }
1909                 if (checksum((int *)buf) == FAIL)
1910                         return (FAIL);
1911                 if (Bcvt)
1912                         swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1913                 goto good;
1914         }
1915         readtape((char *)(&u_ospcl.s_ospcl));
1916         memset((char *)buf, 0, (long)TP_BSIZE);
1917         buf->c_type = u_ospcl.s_ospcl.c_type;
1918         buf->c_date = u_ospcl.s_ospcl.c_date;
1919         buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1920         buf->c_volume = u_ospcl.s_ospcl.c_volume;
1921         buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1922         buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1923         buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1924         buf->c_magic = u_ospcl.s_ospcl.c_magic;
1925         buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1926         buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1927         buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1928         buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1929         buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1930         buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1931 #ifdef  __linux__
1932         buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1933         buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1934         buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1935 #else   /* __linux__ */
1936         buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1937         buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1938         buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1939 #endif  /* __linux__ */
1940         buf->c_count = u_ospcl.s_ospcl.c_count;
1941         memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1942         if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1943             checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1944                 return(FAIL);
1945         buf->c_magic = NFS_MAGIC;
1946
1947 good:
1948         if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1949             (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1950                 qcvt.qval = buf->c_dinode.di_size;
1951                 if (qcvt.val[0] || qcvt.val[1]) {
1952                         printf("Note: Doing Quad swapping\n");
1953                         Qcvt = 1;
1954                 }
1955         }
1956         if (Qcvt) {
1957                 qcvt.qval = buf->c_dinode.di_size;
1958                 i = qcvt.val[1];
1959                 qcvt.val[1] = qcvt.val[0];
1960                 qcvt.val[0] = i;
1961                 buf->c_dinode.di_size = qcvt.qval;
1962         }
1963         readmapflag = 0;
1964
1965         switch (buf->c_type) {
1966
1967         case TS_CLRI:
1968         case TS_BITS:
1969                 /*
1970                  * Have to patch up missing information in bit map headers
1971                  */
1972                 buf->c_inumber = 0;
1973                 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1974                 if (buf->c_count > TP_NINDIR)
1975                         readmapflag = 1;
1976                 else 
1977                         for (i = 0; i < buf->c_count; i++)
1978                                 buf->c_addr[i]++;
1979                 break;
1980
1981         case TS_TAPE:
1982                 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1983                         oldinofmt = 1;
1984                 /* fall through */
1985         case TS_END:
1986                 buf->c_inumber = 0;
1987                 break;
1988
1989         case TS_INODE:
1990         case TS_ADDR:
1991                 break;
1992
1993         default:
1994                 panic("gethead: unknown inode type %d\n", buf->c_type);
1995                 break;
1996         }
1997         /*
1998          * If we are restoring a filesystem with old format inodes,
1999          * copy the uid/gid to the new location.
2000          */
2001         if (oldinofmt) {
2002                 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2003                 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2004         }
2005         if (dflag)
2006                 accthdr(buf);
2007         return(GOOD);
2008 }
2009
2010 /*
2011  * Check that a header is where it belongs and predict the next header
2012  */
2013 static void
2014 accthdr(struct s_spcl *header)
2015 {
2016         static dump_ino_t previno = 0x7fffffff;
2017         static int prevtype;
2018         static long predict;
2019         long blks, i;
2020
2021         if (header->c_type == TS_TAPE) {
2022                 fprintf(stderr, "Volume header (%s inode format) ",
2023                     oldinofmt ? "old" : "new");
2024                 if (header->c_firstrec)
2025                         fprintf(stderr, "begins with record %d",
2026                                 header->c_firstrec);
2027                 fprintf(stderr, "\n");
2028                 previno = 0x7fffffff;
2029                 return;
2030         }
2031         if (previno == 0x7fffffff)
2032                 goto newcalc;
2033         switch (prevtype) {
2034         case TS_BITS:
2035                 fprintf(stderr, "Dumped inodes map header");
2036                 break;
2037         case TS_CLRI:
2038                 fprintf(stderr, "Used inodes map header");
2039                 break;
2040         case TS_INODE:
2041                 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2042                 break;
2043         case TS_ADDR:
2044                 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2045                 break;
2046         case TS_END:
2047                 fprintf(stderr, "End of tape header");
2048                 break;
2049         }
2050         if (predict != blksread - 1)
2051                 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2052                         predict, blksread - 1);
2053         fprintf(stderr, "\n");
2054 newcalc:
2055         blks = 0;
2056         if (header->c_type != TS_END)
2057                 for (i = 0; i < header->c_count; i++)
2058                         if (readmapflag || header->c_addr[i] != 0)
2059                                 blks++;
2060         predict = blks;
2061         blksread = 0;
2062         prevtype = header->c_type;
2063         previno = header->c_inumber;
2064 }
2065
2066 /*
2067  * Find an inode header.
2068  * Complain if had to skip, and complain is set.
2069  */
2070 static void
2071 findinode(struct s_spcl *header)
2072 {
2073         static long skipcnt = 0;
2074         long i;
2075         char buf[TP_BSIZE];
2076
2077         curfile.name = "<name unknown>";
2078         curfile.action = UNKNOWN;
2079         curfile.dip = NULL;
2080         curfile.ino = 0;
2081         do {
2082                 if (header->c_magic != NFS_MAGIC) {
2083                         skipcnt++;
2084                         while (gethead(header) == FAIL ||
2085                             header->c_date != dumpdate)
2086                                 skipcnt++;
2087                 }
2088                 switch (header->c_type) {
2089
2090                 case TS_ADDR:
2091                         /*
2092                          * Skip up to the beginning of the next record
2093                          */
2094                         for (i = 0; i < header->c_count; i++)
2095                                 if (header->c_addr[i])
2096                                         readtape(buf);
2097                         while (gethead(header) == FAIL ||
2098                             header->c_date != dumpdate)
2099                                 skipcnt++;
2100                         break;
2101
2102                 case TS_INODE:
2103                         curfile.dip = &header->c_dinode;
2104                         curfile.ino = header->c_inumber;
2105                         break;
2106
2107                 case TS_END:
2108                         curfile.ino = maxino;
2109                         break;
2110
2111                 case TS_CLRI:
2112                         curfile.name = "<file removal list>";
2113                         break;
2114
2115                 case TS_BITS:
2116                         curfile.name = "<file dump list>";
2117                         break;
2118
2119                 case TS_TAPE:
2120                         panic("unexpected tape header\n");
2121                         /* NOTREACHED */
2122
2123                 default:
2124                         panic("unknown tape header type %d\n", spcl.c_type);
2125                         /* NOTREACHED */
2126
2127                 }
2128         } while (header->c_type == TS_ADDR);
2129         if (skipcnt > 0)
2130 #ifdef USE_QFA
2131                 if (!noresyncmesg)
2132 #endif
2133                         fprintf(stderr, "resync restore, skipped %ld blocks\n",
2134                                 skipcnt);
2135         skipcnt = 0;
2136 }
2137
2138 static int
2139 checksum(int *buf)
2140 {
2141         register int i, j;
2142
2143         j = sizeof(union u_spcl) / sizeof(int);
2144         i = 0;
2145         if(!Bcvt) {
2146                 do
2147                         i += *buf++;
2148                 while (--j);
2149         } else {
2150                 /* What happens if we want to read restore tapes
2151                         for a 16bit int machine??? */
2152                 do
2153                         i += swabi(*buf++);
2154                 while (--j);
2155         }
2156
2157         if (i != CHECKSUM) {
2158                 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2159                         (unsigned long)curfile.ino, curfile.name);
2160                 return(FAIL);
2161         }
2162         return(GOOD);
2163 }
2164
2165 #ifdef RRESTORE
2166 #ifdef __STDC__
2167 #include <stdarg.h>
2168 #else
2169 #include <varargs.h>
2170 #endif
2171
2172 void
2173 #ifdef __STDC__
2174 msg(const char *fmt, ...)
2175 #else
2176 msg(fmt, va_alist)
2177         char *fmt;
2178         va_dcl
2179 #endif
2180 {
2181         va_list ap;
2182 #ifdef __STDC__
2183         va_start(ap, fmt);
2184 #else
2185         va_start(ap);
2186 #endif
2187         (void)vfprintf(stderr, fmt, ap);
2188         va_end(ap);
2189 }
2190 #endif /* RRESTORE */
2191
2192 static u_char *
2193 swab16(u_char *sp, int n)
2194 {
2195         char c;
2196
2197         while (--n >= 0) {
2198                 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2199                 sp += 2;
2200         }
2201         return (sp);
2202 }
2203
2204 static u_char *
2205 swab32(u_char *sp, int n)
2206 {
2207         char c;
2208
2209         while (--n >= 0) {
2210                 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2211                 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2212                 sp += 4;
2213         }
2214         return (sp);
2215 }
2216
2217 static u_char *
2218 swab64(u_char *sp, int n)
2219 {
2220         char c;
2221
2222         while (--n >= 0) {
2223                 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2224                 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2225                 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2226                 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2227                 sp += 8;
2228         }
2229         return (sp);
2230 }
2231
2232 void
2233 swabst(u_char *cp, u_char *sp)
2234 {
2235         int n = 0;
2236
2237         while (*cp) {
2238                 switch (*cp) {
2239                 case '0': case '1': case '2': case '3': case '4':
2240                 case '5': case '6': case '7': case '8': case '9':
2241                         n = (n * 10) + (*cp++ - '0');
2242                         continue;
2243
2244                 case 's': case 'w': case 'h':
2245                         if (n == 0)
2246                                 n = 1;
2247                         sp = swab16(sp, n);
2248                         break;
2249
2250                 case 'i':
2251                         if (n == 0)
2252                                 n = 1;
2253                         sp = swab32(sp, n);
2254                         break;
2255
2256                 case 'l':
2257                         if (n == 0)
2258                                 n = 1;
2259                         sp = swab64(sp, n);
2260                         break;
2261
2262                 default: /* Any other character, like 'b' counts as byte. */
2263                         if (n == 0)
2264                                 n = 1;
2265                         sp += n;
2266                         break;
2267                 }
2268                 cp++;
2269                 n = 0;
2270         }
2271 }
2272
2273 static u_int
2274 swabi(u_int x)
2275 {
2276         swabst((u_char *)"i", (u_char *)&x);
2277         return (x);
2278 }
2279
2280 #if 0
2281 static u_long
2282 swabl(u_long x)
2283 {
2284         swabst((u_char *)"l", (u_char *)&x);
2285         return (x);
2286 }
2287 #endif
2288
2289 #ifdef USE_QFA
2290 /*
2291  * get the current position of the tape
2292  */
2293 int
2294 GetTapePos(long *pos)
2295 {
2296         int err = 0;
2297
2298         *pos = 0;
2299         if (ioctl(mt, MTIOCPOS, pos) == -1) {
2300                 err = errno;
2301                 fprintf(stdout, "[%ld] error: %d (getting tapepos: %ld)\n", 
2302                         (unsigned long)getpid(), err, *pos);
2303                 return err;
2304         }
2305         return err;
2306 }
2307
2308 typedef struct mt_pos {
2309         short    mt_op;
2310         int      mt_count;
2311 } MTPosRec, *MTPosPtr;
2312
2313 /*
2314  * go to specified position on tape
2315  */
2316 int
2317 GotoTapePos(long pos)
2318 {
2319         int err = 0;
2320         struct mt_pos buf;
2321
2322         buf.mt_op = MTSEEK;
2323         buf.mt_count = pos;
2324         if (ioctl(mt, MTIOCTOP, &buf) == -1) {
2325                 err = errno;
2326                 fprintf(stdout, "[%ld] error: %d (setting tapepos: %ld)\n", 
2327                         (unsigned long)getpid(), err, pos);
2328                 return err;
2329         }
2330         return err;
2331 }
2332
2333 /*
2334  * read next data from tape to re-sync
2335  */
2336 void
2337 ReReadFromTape(void)
2338 {
2339         FLUSHTAPEBUF();
2340         noresyncmesg = 1;
2341         if (gethead(&spcl) == FAIL) {
2342 #ifdef DEBUG_QFA
2343                 fprintf(stdout, "DEBUG 1 gethead failed\n");
2344 #endif
2345         }
2346         findinode(&spcl);
2347         noresyncmesg = 0;
2348 }
2349
2350 void
2351 RequestVol(long tnum)
2352 {
2353         FLUSHTAPEBUF();
2354         getvol(tnum);
2355 }
2356 #endif /* USE_QFA */