]> git.wh0rd.org Git - dump.git/blob - restore/tape.c
'M' multi-volume added 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@cybercable.fr>, 1999 
6  */
7
8 /*
9  * Copyright (c) 1983, 1993
10  *      The Regents of the University of California.  All rights reserved.
11  * (c) UNIX System Laboratories, Inc.
12  * All or some portions of this file are derived from material licensed
13  * to the University of California by American Telephone and Telegraph
14  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
15  * the permission of UNIX System Laboratories, Inc.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. All advertising materials mentioning features or use of this software
26  *    must display the following acknowledgement:
27  *      This product includes software developed by the University of
28  *      California, Berkeley and its contributors.
29  * 4. Neither the name of the University nor the names of its contributors
30  *    may be used to endorse or promote products derived from this software
31  *    without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  */
45
46 #ifndef lint
47 static const char rcsid[] =
48         "$Id: tape.c,v 1.9 1999/11/22 21:39:42 tiniou Exp $";
49 #endif /* not lint */
50
51 #include <sys/param.h>
52 #include <sys/file.h>
53 #include <sys/mtio.h>
54 #include <sys/stat.h>
55
56 #ifdef  __linux__
57 #include <sys/time.h>
58 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
60 #else   /* __linux__ */
61 #include <ufs/ufs/dinode.h>
62 #endif  /* __linux__ */
63 #include <protocols/dumprestore.h>
64
65 #include <errno.h>
66 #include <compaterr.h>
67 #include <setjmp.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72
73 #ifdef  __linux__
74 #include <ext2fs/ext2fs.h>
75 #endif
76
77 #include "restore.h"
78 #include "extern.h"
79 #include "pathnames.h"
80
81 static long     fssize = MAXBSIZE;
82 static int      mt = -1;
83 static int      pipein = 0;
84 static char     magtape[NAME_MAX];
85 static char     magtapeprefix[NAME_MAX];
86 static int      blkcnt;
87 static int      numtrec;
88 static char     *tapebuf;
89 static union    u_spcl endoftapemark;
90 static long     blksread;               /* blocks read since last header */
91 static long     tpblksread = 0;         /* TP_BSIZE blocks read */
92 static long     tapesread;
93 static sigjmp_buf       restart;
94 static int      gettingfile = 0;        /* restart has a valid frame */
95 static char     *host = NULL;
96
97 static int      ofile;
98 static char     *map;
99 static char     lnkbuf[MAXPATHLEN + 1];
100 static int      pathlen;
101
102 int             oldinofmt;      /* old inode format conversion required */
103 int             Bcvt;           /* Swap Bytes (for CCI or sun) */
104 static int      Qcvt;           /* Swap quads (for sun) */
105
106 #define FLUSHTAPEBUF()  blkcnt = ntrec + 1
107
108 static void      accthdr __P((struct s_spcl *));
109 static int       checksum __P((int *));
110 static void      findinode __P((struct s_spcl *));
111 static void      findtapeblksize __P((void));
112 static int       gethead __P((struct s_spcl *));
113 static void      readtape __P((char *));
114 static void      setdumpnum __P((void));
115 static u_int     swabi __P((u_int));
116 static u_long    swabl __P((u_long));
117 static u_char   *swab64 __P((u_char *, int));
118 static u_char   *swab32 __P((u_char *, int));
119 static u_char   *swab16 __P((u_char *, int));
120 static void      terminateinput __P((void));
121 static void      xtrfile __P((char *, size_t));
122 static void      xtrlnkfile __P((char *, size_t));
123 static void      xtrlnkskip __P((char *, size_t));
124 static void      xtrmap __P((char *, size_t));
125 static void      xtrmapskip __P((char *, size_t));
126 static void      xtrskip __P((char *, size_t));
127
128 static int readmapflag;
129
130 /*
131  * Set up an input source
132  */
133 void
134 setinput(char *source)
135 {
136         FLUSHTAPEBUF();
137         if (bflag)
138                 newtapebuf(ntrec);
139         else
140                 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
141         terminal = stdin;
142
143 #ifdef RRESTORE
144         if (strchr(source, ':')) {
145                 host = source;
146                 source = strchr(host, ':');
147                 *source++ = '\0';
148                 if (rmthost(host) == 0)
149                         exit(1);
150         } else
151 #endif
152         if (strcmp(source, "-") == 0) {
153                 /*
154                  * Since input is coming from a pipe we must establish
155                  * our own connection to the terminal.
156                  */
157                 terminal = fopen(_PATH_TTY, "r");
158                 if (terminal == NULL) {
159                         warn("cannot open %s", _PATH_TTY);
160                         terminal = fopen(_PATH_DEVNULL, "r");
161                         if (terminal == NULL)
162                                 err(1, "cannot open %s", _PATH_DEVNULL);
163                 }
164                 pipein++;
165         }
166         setuid(getuid());       /* no longer need or want root privileges */
167         if (Mflag) {
168                 strncpy(magtapeprefix, source, NAME_MAX);
169                 magtapeprefix[NAME_MAX-1] = '\0';
170                 snprintf(magtape, NAME_MAX, "%s%03d", source, 1);
171         }
172         else
173                 strncpy(magtape, source, NAME_MAX);
174         magtape[NAME_MAX - 1] = '\0';
175 }
176
177 void
178 newtapebuf(long size)
179 {
180         static int tapebufsize = -1;
181
182         ntrec = size;
183         if (size <= tapebufsize)
184                 return;
185         if (tapebuf != NULL)
186                 free(tapebuf);
187         tapebuf = malloc(size * TP_BSIZE);
188         if (tapebuf == NULL)
189                 errx(1, "Cannot allocate space for tape buffer");
190         tapebufsize = size;
191 }
192
193 /*
194  * Verify that the tape drive can be accessed and
195  * that it actually is a dump tape.
196  */
197 void
198 setup(void)
199 {
200         int i, j, *ip;
201         struct stat stbuf;
202
203         Vprintf(stdout, "Verify tape and initialize maps\n");
204 #ifdef RRESTORE
205         if (host)
206                 mt = rmtopen(magtape, 0);
207         else
208 #endif
209         if (pipein)
210                 mt = 0;
211         else
212                 mt = open(magtape, O_RDONLY, 0);
213         if (mt < 0)
214                 err(1, "%s", magtape);
215         volno = 1;
216         setdumpnum();
217         FLUSHTAPEBUF();
218         if (!pipein && !bflag)
219                 findtapeblksize();
220         if (gethead(&spcl) == FAIL) {
221                 blkcnt--; /* push back this block */
222                 blksread--;
223                 tpblksread--;
224                 cvtflag++;
225                 if (gethead(&spcl) == FAIL)
226                         errx(1, "Tape is not a dump tape");
227                 fprintf(stderr, "Converting to new file system format.\n");
228         }
229         if (pipein) {
230                 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
231                 endoftapemark.s_spcl.c_type = TS_END;
232                 ip = (int *)&endoftapemark;
233                 j = sizeof(union u_spcl) / sizeof(int);
234                 i = 0;
235                 do
236                         i += *ip++;
237                 while (--j);
238                 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
239         }
240         if (vflag || command == 't' || command == 'C')
241                 printdumpinfo();
242         if (filesys == NULL) {
243                 filesys = spcl.c_filesys;
244         }
245         dumptime = spcl.c_ddate;
246         dumpdate = spcl.c_date;
247         if (stat(".", &stbuf) < 0)
248                 err(1, "cannot stat .");
249         if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
250                 fssize = TP_BSIZE;
251         if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
252                 fssize = stbuf.st_blksize;
253         if (((fssize - 1) & fssize) != 0)
254                 errx(1, "bad block size %ld", fssize);
255         if (spcl.c_volume != 1)
256                 errx(1, "Tape is not volume 1 of the dump");
257         if (gethead(&spcl) == FAIL) {
258                 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
259                 panic("no header after volume mark!\n");
260         }
261         findinode(&spcl);
262         if (spcl.c_type != TS_CLRI)
263                 errx(1, "Cannot find file removal list");
264         maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
265         Dprintf(stdout, "maxino = %ld\n", maxino);
266         map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
267         if (map == NULL)
268                 panic("no memory for active inode map\n");
269         usedinomap = map;
270         curfile.action = USING;
271         getfile(xtrmap, xtrmapskip);
272         if (spcl.c_type != TS_BITS)
273                 errx(1, "Cannot find file dump list");
274         map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
275         if (map == (char *)NULL)
276                 panic("no memory for file dump list\n");
277         dumpmap = map;
278         curfile.action = USING;
279         getfile(xtrmap, xtrmapskip);
280         /*
281          * If there may be whiteout entries on the tape, pretend that the
282          * whiteout inode exists, so that the whiteout entries can be
283          * extracted.
284          */
285         if (oldinofmt == 0)
286                 SETINO(WINO, dumpmap);
287 }
288
289 /*
290  * Prompt user to load a new dump volume.
291  * "Nextvol" is the next suggested volume to use.
292  * This suggested volume is enforced when doing full
293  * or incremental restores, but can be overridden by
294  * the user when only extracting a subset of the files.
295  */
296 void
297 getvol(long nextvol)
298 {
299         long newvol = 0, savecnt = 0, wantnext = 0, i;
300         union u_spcl tmpspcl;
301 #       define tmpbuf tmpspcl.s_spcl
302         char buf[TP_BSIZE];
303         int haderror = 0;
304
305         if (nextvol == 1) {
306                 tapesread = 0;
307                 gettingfile = 0;
308         }
309         if (pipein) {
310                 if (nextvol != 1)
311                         panic("Changing volumes on pipe input?\n");
312                 if (volno == 1)
313                         return;
314                 goto gethdr;
315         }
316         savecnt = blksread;
317 again:
318         if (pipein)
319                 exit(1); /* pipes do not get a second chance */
320         if (command == 'R' || command == 'r' || curfile.action != SKIP) {
321                 newvol = nextvol;
322                 wantnext = 1;
323         } else {
324                 newvol = 0;
325                 wantnext = 0;
326         }
327         while (newvol <= 0) {
328                 if (tapesread == 0) {
329                         fprintf(stderr, "%s%s%s%s%s",
330                             "You have not read any tapes yet.\n",
331                             "Unless you know which volume your",
332                             " file(s) are on you should start\n",
333                             "with the last volume and work",
334                             " towards the first.\n");
335                 } else {
336                         fprintf(stderr, "You have read volumes");
337                         strcpy(buf, ": ");
338                         for (i = 1; i < 32; i++)
339                                 if (tapesread & (1 << i)) {
340                                         fprintf(stderr, "%s%ld", buf, (long)i);
341                                         strcpy(buf, ", ");
342                                 }
343                         fprintf(stderr, "\n");
344                 }
345                 do      {
346                         fprintf(stderr, "Specify next volume #: ");
347                         (void) fflush(stderr);
348                         (void) fgets(buf, BUFSIZ, terminal);
349                 } while (!feof(terminal) && buf[0] == '\n');
350                 if (feof(terminal))
351                         exit(1);
352                 newvol = atoi(buf);
353                 if (newvol <= 0) {
354                         fprintf(stderr,
355                             "Volume numbers are positive numerics\n");
356                 }
357         }
358         if (newvol == volno) {
359                 tapesread |= 1 << volno;
360                 return;
361         }
362         closemt();
363         if (Mflag) {
364                 snprintf(magtape, NAME_MAX, "%s%03ld", magtapeprefix, newvol);
365                 magtape[NAME_MAX - 1] = '\0';
366         }
367         if (!Mflag || haderror) {
368                 haderror = 0;
369                 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
370                 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
371                 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
372                 (void) fflush(stderr);
373                 (void) fgets(buf, BUFSIZ, terminal);
374                 if (feof(terminal))
375                         exit(1);
376                 if (!strcmp(buf, "none\n")) {
377                         terminateinput();
378                         return;
379                 }
380                 if (buf[0] != '\n') {
381                         (void) strcpy(magtape, buf);
382                         magtape[strlen(magtape) - 1] = '\0';
383                 }
384         }
385 #ifdef RRESTORE
386         if (host)
387                 mt = rmtopen(magtape, 0);
388         else
389 #endif
390                 mt = open(magtape, O_RDONLY, 0);
391
392         if (mt == -1) {
393                 fprintf(stderr, "Cannot open %s\n", magtape);
394                 volno = -1;
395                 haderror = 1;
396                 goto again;
397         }
398 gethdr:
399         volno = newvol;
400         setdumpnum();
401         FLUSHTAPEBUF();
402         if (gethead(&tmpbuf) == FAIL) {
403                 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
404                 fprintf(stderr, "tape is not dump tape\n");
405                 volno = 0;
406                 haderror = 1;
407                 goto again;
408         }
409         if (tmpbuf.c_volume != volno) {
410                 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
411                 volno = 0;
412                 haderror = 1;
413                 goto again;
414         }
415         if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
416 #ifdef  __linux__
417                 fprintf(stderr, "Wrong dump date\n\tgot: %s",
418                         ctime4(&tmpbuf.c_date));
419                 fprintf(stderr, "\twanted: %s", ctime4(&dumpdate));
420 #else
421                 fprintf(stderr, "Wrong dump date\n\tgot: %s",
422                         ctime(&tmpbuf.c_date));
423                 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
424 #endif
425                 volno = 0;
426                 haderror = 1;
427                 goto again;
428         }
429         tapesread |= 1 << volno;
430         blksread = savecnt;
431         /*
432          * If continuing from the previous volume, skip over any
433          * blocks read already at the end of the previous volume.
434          *
435          * If coming to this volume at random, skip to the beginning
436          * of the next record.
437          */
438         Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
439                 tpblksread, (long)tmpbuf.c_firstrec);
440         if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
441                 if (!wantnext) {
442                         tpblksread = tmpbuf.c_firstrec;
443                         for (i = tmpbuf.c_count; i > 0; i--)
444                                 readtape(buf);
445                 } else if (tmpbuf.c_firstrec > 0 &&
446                            tmpbuf.c_firstrec < tpblksread - 1) {
447                         /*
448                          * -1 since we've read the volume header
449                          */
450                         i = tpblksread - tmpbuf.c_firstrec - 1;
451                         Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
452                                 (long)i, i > 1 ? "s" : "");
453                         while (--i >= 0)
454                                 readtape(buf);
455                 }
456         }
457         if (curfile.action == USING) {
458                 if (volno == 1)
459                         panic("active file into volume 1\n");
460                 return;
461         }
462         /*
463          * Skip up to the beginning of the next record
464          */
465         if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
466                 for (i = tmpbuf.c_count; i > 0; i--)
467                         readtape(buf);
468         (void) gethead(&spcl);
469         findinode(&spcl);
470         if (gettingfile) {
471                 gettingfile = 0;
472                 siglongjmp(restart, 1);
473         }
474 }
475
476 /*
477  * Handle unexpected EOF.
478  */
479 static void
480 terminateinput(void)
481 {
482
483         if (gettingfile && curfile.action == USING) {
484                 printf("Warning: %s %s\n",
485                     "End-of-input encountered while extracting", curfile.name);
486         }
487         curfile.name = "<name unknown>";
488         curfile.action = UNKNOWN;
489         curfile.dip = NULL;
490         curfile.ino = maxino;
491         if (gettingfile) {
492                 gettingfile = 0;
493                 siglongjmp(restart, 1);
494         }
495 }
496
497 /*
498  * handle multiple dumps per tape by skipping forward to the
499  * appropriate one.
500  */
501 static void
502 setdumpnum(void)
503 {
504         struct mtop tcom;
505
506         if (dumpnum == 1 || volno != 1)
507                 return;
508         if (pipein)
509                 errx(1, "Cannot have multiple dumps on pipe input");
510         tcom.mt_op = MTFSF;
511         tcom.mt_count = dumpnum - 1;
512 #ifdef RRESTORE
513         if (host)
514                 rmtioctl(MTFSF, dumpnum - 1);
515         else
516 #endif
517                 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
518                         warn("ioctl MTFSF");
519 }
520
521 void
522 printdumpinfo(void)
523 {
524 #ifdef  __linux__
525         fprintf(stdout, "Dump   date: %s", ctime4(&spcl.c_date));
526         fprintf(stdout, "Dumped from: %s",
527             (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
528 #else
529         fprintf(stdout, "Dump   date: %s", ctime(&spcl.c_date));
530         fprintf(stdout, "Dumped from: %s",
531             (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
532 #endif
533         if (spcl.c_host[0] == '\0')
534                 return;
535         fprintf(stderr, "Level %d dump of %s on %s:%s\n",
536                 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
537         fprintf(stderr, "Label: %s\n", spcl.c_label);
538 }
539
540 int
541 extractfile(char *name)
542 {
543         unsigned int flags;
544         mode_t mode;
545         struct timeval timep[2];
546         struct entry *ep;
547
548         curfile.name = name;
549         curfile.action = USING;
550 #ifdef  __linux__
551         timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
552         timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
553         timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
554         timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
555 #else   /* __linux__ */
556         timep[0].tv_sec = curfile.dip->di_atime;
557         timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
558         timep[1].tv_sec = curfile.dip->di_mtime;
559         timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
560 #endif  /* __linux__ */
561         mode = curfile.dip->di_mode;
562         flags = curfile.dip->di_flags;
563         switch (mode & IFMT) {
564
565         default:
566                 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
567                 skipfile();
568                 return (FAIL);
569
570         case IFSOCK:
571                 Vprintf(stdout, "skipped socket %s\n", name);
572                 skipfile();
573                 return (GOOD);
574
575         case IFDIR:
576                 if (mflag) {
577                         ep = lookupname(name);
578                         if (ep == NULL || ep->e_flags & EXTRACT)
579                                 panic("unextracted directory %s\n", name);
580                         skipfile();
581                         return (GOOD);
582                 }
583                 Vprintf(stdout, "extract file %s\n", name);
584                 return (genliteraldir(name, curfile.ino));
585
586         case IFLNK:
587         {
588 #ifdef HAVE_LCHOWN
589                 uid_t luid = curfile.dip->di_uid;
590                 gid_t lgid = curfile.dip->di_gid;
591 #endif
592                 lnkbuf[0] = '\0';
593                 pathlen = 0;
594                 getfile(xtrlnkfile, xtrlnkskip);
595                 if (pathlen == 0) {
596                         Vprintf(stdout,
597                             "%s: zero length symbolic link (ignored)\n", name);
598                         return (GOOD);
599                 }
600                 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
601                         return (FAIL);
602 #ifdef HAVE_LCHOWN
603                 (void) lchown(name, luid, lgid);
604 #endif
605                 return (GOOD);
606         }
607
608         case IFIFO:
609                 Vprintf(stdout, "extract fifo %s\n", name);
610                 if (Nflag) {
611                         skipfile();
612                         return (GOOD);
613                 }
614                 if (uflag && !Nflag)
615                         (void)unlink(name);
616                 if (mkfifo(name, mode) < 0) {
617                         warn("%s: cannot create fifo", name);
618                         skipfile();
619                         return (FAIL);
620                 }
621                 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
622                 (void) chmod(name, mode);
623                 if (flags)
624 #ifdef  __linux__
625                         (void) fsetflags(name, flags);
626 #else
627                         (void) chflags(name, flags);
628 #endif
629                 skipfile();
630                 utimes(name, timep);
631                 return (GOOD);
632
633         case IFCHR:
634         case IFBLK:
635                 Vprintf(stdout, "extract special file %s\n", name);
636                 if (Nflag) {
637                         skipfile();
638                         return (GOOD);
639                 }
640                 if (uflag)
641                         (void)unlink(name);
642                 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
643                         warn("%s: cannot create special file", name);
644                         skipfile();
645                         return (FAIL);
646                 }
647                 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
648                 (void) chmod(name, mode);
649                 if (flags)
650 #ifdef  __linux__
651                         {
652                         warn("%s: fsetflags called on a special file", name);
653                         (void) fsetflags(name, flags);
654                         }
655 #else
656                         (void) chflags(name, flags);
657 #endif
658                 skipfile();
659                 utimes(name, timep);
660                 return (GOOD);
661
662         case IFREG:
663                 Vprintf(stdout, "extract file %s\n", name);
664                 if (Nflag) {
665                         skipfile();
666                         return (GOOD);
667                 }
668                 if (uflag)
669                         (void)unlink(name);
670                 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
671                     0666)) < 0) {
672                         warn("%s: cannot create file", name);
673                         skipfile();
674                         return (FAIL);
675                 }
676                 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
677                 (void) fchmod(ofile, mode);
678                 if (flags)
679 #ifdef  __linux__
680                         (void) setflags(ofile, flags);
681 #else
682                         (void) fchflags(ofile, flags);
683 #endif
684                 getfile(xtrfile, xtrskip);
685                 (void) close(ofile);
686                 utimes(name, timep);
687                 return (GOOD);
688         }
689         /* NOTREACHED */
690 }
691
692 /*
693  * skip over bit maps on the tape
694  */
695 void
696 skipmaps(void)
697 {
698
699         while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
700                 skipfile();
701 }
702
703 /*
704  * skip over a file on the tape
705  */
706 void
707 skipfile(void)
708 {
709
710         curfile.action = SKIP;
711         getfile(xtrnull, xtrnull);
712 }
713
714 /*
715  * Extract a file from the tape.
716  * When an allocated block is found it is passed to the fill function;
717  * when an unallocated block (hole) is found, a zeroed buffer is passed
718  * to the skip function.
719  */
720 void
721 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
722 {
723         register int i;
724         volatile int curblk = 0;
725         volatile quad_t size = spcl.c_dinode.di_size;
726         volatile int last_write_was_hole = 0;
727         quad_t origsize = size;
728         static char clearedbuf[MAXBSIZE];
729         char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
730         char junk[TP_BSIZE];
731
732         if (spcl.c_type == TS_END)
733                 panic("ran off end of tape\n");
734         if (spcl.c_magic != NFS_MAGIC)
735                 panic("not at beginning of a file\n");
736         if (!gettingfile && setjmp(restart) != 0)
737                 return;
738         gettingfile++;
739 loop:
740         for (i = 0; i < spcl.c_count; i++) {
741                 if (readmapflag || spcl.c_addr[i]) {
742                         readtape(&buf[curblk++][0]);
743                         if (curblk == fssize / TP_BSIZE) {
744                                 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
745                                      fssize : (curblk - 1) * TP_BSIZE + size));
746                                 curblk = 0;
747                                 last_write_was_hole = 0;
748                         }
749                 } else {
750                         if (curblk > 0) {
751                                 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
752                                      curblk * TP_BSIZE :
753                                      (curblk - 1) * TP_BSIZE + size));
754                                 curblk = 0;
755                         }
756                         (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
757                                 TP_BSIZE : size));
758                         last_write_was_hole = 1;
759                 }
760                 if ((size -= TP_BSIZE) <= 0) {
761                         for (i++; i < spcl.c_count; i++)
762                                 if (readmapflag || spcl.c_addr[i])
763                                         readtape(junk);
764                         break;
765                 }
766         }
767         if (gethead(&spcl) == GOOD && size > 0) {
768                 if (spcl.c_type == TS_ADDR)
769                         goto loop;
770                 Dprintf(stdout,
771                         "Missing address (header) block for %s at %ld blocks\n",
772                         curfile.name, (long)blksread);
773         }
774         if (curblk > 0) {
775                 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
776                 last_write_was_hole = 0;
777         }
778         if (last_write_was_hole) {
779                 ftruncate(ofile, origsize);
780         }
781         findinode(&spcl);
782         gettingfile = 0;
783 }
784
785 /*
786  * Write out the next block of a file.
787  */
788 static void
789 xtrfile(char *buf, size_t size)
790 {
791
792         if (Nflag)
793                 return;
794         if (write(ofile, buf, (int) size) == -1)
795                 err(1, "write error extracting inode %lu, name %s\nwrite",
796                         (unsigned long)curfile.ino, curfile.name);
797 }
798
799 /*
800  * Skip over a hole in a file.
801  */
802 /* ARGSUSED */
803 static void
804 xtrskip(char *buf, size_t size)
805 {
806
807         if (lseek(ofile, (off_t)size, SEEK_CUR) == -1)
808                 err(1, "seek error extracting inode %lu, name %s\nlseek",
809                         (unsigned long)curfile.ino, curfile.name);
810 }
811
812 /*
813  * Collect the next block of a symbolic link.
814  */
815 static void
816 xtrlnkfile(char *buf, size_t size)
817 {
818
819         pathlen += size;
820         if (pathlen > MAXPATHLEN)
821                 errx(1, "symbolic link name: %s->%s%s; too long %d",
822                     curfile.name, lnkbuf, buf, pathlen);
823         (void) strcat(lnkbuf, buf);
824 }
825
826 /*
827  * Skip over a hole in a symbolic link (should never happen).
828  */
829 /* ARGSUSED */
830 static void
831 xtrlnkskip(char *buf, size_t size)
832 {
833
834         errx(1, "unallocated block in symbolic link %s", curfile.name);
835 }
836
837 /*
838  * Collect the next block of a bit map.
839  */
840 static void
841 xtrmap(char *buf, size_t size)
842 {
843
844         memmove(map, buf, size);
845         map += size;
846 }
847
848 /*
849  * Skip over a hole in a bit map (should never happen).
850  */
851 /* ARGSUSED */
852 static void
853 xtrmapskip(char *buf, size_t size)
854 {
855
856         panic("hole in map\n");
857         map += size;
858 }
859
860 /*
861  * Noop, when an extraction function is not needed.
862  */
863 /* ARGSUSED */
864 void
865 xtrnull(char *buf, size_t size)
866 {
867
868         return;
869 }
870
871 static int
872 do_cmpfiles(int fd_tape, int fd_disk, long size)
873 {
874         static char buf_tape[BUFSIZ];
875         static char buf_disk[BUFSIZ];
876         ssize_t n_tape;
877         ssize_t n_disk;
878
879         while (size > 0) {
880                 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
881                         close(fd_tape), close(fd_disk);
882                         panic("do_cmpfiles: unexpected EOF[1]");
883                 }
884                 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
885                         close(fd_tape), close(fd_disk);
886                         panic("do_cmpfiles: unexpected EOF[2]");
887                 }
888                 if (n_tape != n_disk) {
889                         close(fd_tape), close(fd_disk);
890                         panic("do_cmpfiles: sizes different!");
891                 }
892                 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
893                 size -= n_tape;
894         }
895         return (0);
896 }
897
898 /* for debugging compare problems */
899 #undef COMPARE_FAIL_KEEP_FILE
900
901 static
902 #ifdef COMPARE_FAIL_KEEP_FILE
903 /* return true if tapefile should be unlinked after compare */
904 int
905 #else
906 void
907 #endif
908 cmpfiles(char *tapefile, char *diskfile, struct stat *sbuf_disk)
909 {
910         struct stat sbuf_tape;
911         int fd_tape, fd_disk;
912
913         if (stat(tapefile, &sbuf_tape) != 0) {
914                 panic("Can't lstat tmp file %s: %s\n", tapefile,
915                       strerror(errno));
916         }
917
918         if (sbuf_disk->st_size != sbuf_tape.st_size) {
919                 fprintf(stderr,
920                         "%s: size changed from %ld to %ld.\n",
921                         diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
922 #ifdef COMPARE_FAIL_KEEP_FILE
923                 return (0);
924 #else
925                 return;
926 #endif
927         }
928
929         if ((fd_tape = open(tapefile, O_RDONLY)) < 0) {
930                 panic("Can't open %s: %s\n", tapefile, strerror(errno));
931         }
932         if ((fd_disk = open(diskfile, O_RDONLY)) < 0) {
933                 close(fd_tape);
934                 panic("Can't open %s: %s\n", diskfile, strerror(errno));
935         }
936
937         if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
938                 fprintf(stderr, "%s: tape and disk copies are different\n",
939                         diskfile);
940                 close(fd_tape);
941                 close(fd_disk);
942 #ifdef COMPARE_FAIL_KEEP_FILE
943                 /* rename the file to live in /tmp */
944                 /* rename `tapefile' to /tmp/<basename of diskfile> */
945                 {
946                         char *p = strrchr(diskfile, '/');
947                         char newname[MAXPATHLEN];
948                         if (!p) {
949                                 panic("can't find / in %s\n", diskfile);
950                         }
951                         snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
952                         if (rename(tapefile, newname)) {
953                                 panic("rename from %s to %s failed: %s\n",
954                                       tapefile, newname,
955                                       strerror(errno));
956                         } else {
957                                 fprintf(stderr, "*** %s saved to %s\n",
958                                         tapefile, newname);
959                         }
960                 }
961                 
962                 /* don't unlink the file (it's not there anymore */
963                 /* anyway) */
964                 return (0);
965 #else
966                 return;
967 #endif
968         }
969         close(fd_tape);
970         close(fd_disk);
971 #ifdef COMPARE_FAIL_KEEP_FILE
972         return (1);
973 #endif
974 }
975
976 static char tmpfilename[MAXPATHLEN];
977
978 void
979 comparefile(char *name)
980 {
981         static char *tmpfile = NULL;
982         int mode;
983         struct stat sb, stemp;
984         int r;
985
986         if ((r = lstat(name, &sb)) != 0) {
987                 warn("%s: does not exist (%d)", name, r);
988                 skipfile();
989                 return;
990         }
991
992         curfile.name = name;
993         curfile.action = USING;
994         mode = curfile.dip->di_mode;
995
996         Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
997                 (long)sb.st_size, mode);
998
999         if (sb.st_mode != mode) {
1000                 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1001                         name, mode & 07777, sb.st_mode & 07777);
1002         }
1003         switch (mode & IFMT) {
1004         default:
1005                 skipfile();
1006                 return;
1007
1008         case IFSOCK:
1009                 skipfile();
1010                 return;
1011
1012         case IFDIR:
1013                 skipfile();
1014                 return;
1015
1016         case IFLNK: {
1017                 char lbuf[MAXPATHLEN + 1];
1018                 int lsize;
1019
1020                 if (!(sb.st_mode & S_IFLNK)) {
1021                         fprintf(stderr, "%s: is no longer a symbolic link\n",
1022                                 name);
1023                         return;
1024                 }
1025                 lnkbuf[0] = '\0';
1026                 pathlen = 0;
1027                 getfile(xtrlnkfile, xtrlnkskip);
1028                 if (pathlen == 0) {
1029                         fprintf(stderr,
1030                                 "%s: zero length symbolic link (ignored)\n",
1031                                 name);
1032                         return;
1033                 }
1034                 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1035                         panic("readlink of %s failed: %s", name,
1036                               strerror(errno));
1037                 }
1038                 lbuf[lsize] = 0;
1039                 if (strcmp(lbuf, lnkbuf) != 0) {
1040                         fprintf(stderr,
1041                                 "%s: symbolic link changed from %s to %s.\n",
1042                                 name, lnkbuf, lbuf);
1043                         return;
1044                 }
1045                 return;
1046         }
1047
1048         case IFCHR:
1049         case IFBLK:
1050                 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1051                         fprintf(stderr, "%s: no longer a special file\n",
1052                                 name);
1053                         skipfile();
1054                         return;
1055                 }
1056
1057                 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1058                         fprintf(stderr,
1059                                 "%s: device changed from %d,%d to %d,%d.\n",
1060                                 name,
1061                                 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1062                                 (int)curfile.dip->di_rdev & 0xff,
1063                                 ((int)sb.st_rdev >> 8) & 0xff,
1064                                 (int)sb.st_rdev & 0xff);
1065                 }
1066                 skipfile();
1067                 return;
1068
1069         case IFREG:
1070                 if (tmpfile == NULL) {
1071                         /* argument to mktemp() must not be in RO space: */
1072                         snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1073                         tmpfile = mktemp(&tmpfilename[0]);
1074                 }
1075                 if ((stat(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1076                         panic("cannot delete tmp file %s: %s\n",
1077                               tmpfile, strerror(errno));
1078                 }
1079                 if ((ofile = creat(tmpfile, 0600)) < 0) {
1080                         panic("cannot create file temp file %s: %s\n",
1081                               name, strerror(errno));
1082                 }
1083                 getfile(xtrfile, xtrskip);
1084                 (void) close(ofile);
1085 #ifdef COMPARE_FAIL_KEEP_FILE
1086                 if (cmpfiles(tmpfile, name, &sb))
1087                         unlink(tmpfile);
1088 #else
1089                 cmpfiles(tmpfile, name, &sb);
1090                 unlink(tmpfile);
1091 #endif
1092                 return;
1093         }
1094         /* NOTREACHED */
1095 }
1096
1097 /*
1098  * Read TP_BSIZE blocks from the input.
1099  * Handle read errors, and end of media.
1100  */
1101 static void
1102 readtape(char *buf)
1103 {
1104         ssize_t rd, newvol, i;
1105         int cnt, seek_failed;
1106
1107         if (blkcnt < numtrec) {
1108                 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1109                 blksread++;
1110                 tpblksread++;
1111                 return;
1112         }
1113         for (i = 0; i < ntrec; i++)
1114                 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1115         if (numtrec == 0)
1116                 numtrec = ntrec;
1117         cnt = ntrec * TP_BSIZE;
1118         rd = 0;
1119 getmore:
1120 #ifdef RRESTORE
1121         if (host)
1122                 i = rmtread(&tapebuf[rd], cnt);
1123         else
1124 #endif
1125                 i = read(mt, &tapebuf[rd], cnt);
1126         /*
1127          * Check for mid-tape short read error.
1128          * If found, skip rest of buffer and start with the next.
1129          */
1130         if (!pipein && numtrec < ntrec && i > 0) {
1131                 Dprintf(stdout, "mid-media short read error.\n");
1132                 numtrec = ntrec;
1133         }
1134         /*
1135          * Handle partial block read.
1136          */
1137         if (pipein && i == 0 && rd > 0)
1138                 i = rd;
1139         else if (i > 0 && i != ntrec * TP_BSIZE) {
1140                 if (pipein) {
1141                         rd += i;
1142                         cnt -= i;
1143                         if (cnt > 0)
1144                                 goto getmore;
1145                         i = rd;
1146                 } else {
1147                         /*
1148                          * Short read. Process the blocks read.
1149                          */
1150                         if (i % TP_BSIZE != 0)
1151                                 Vprintf(stdout,
1152                                     "partial block read: %ld should be %ld\n",
1153                                     (long)i, ntrec * TP_BSIZE);
1154                         numtrec = i / TP_BSIZE;
1155                 }
1156         }
1157         /*
1158          * Handle read error.
1159          */
1160         if (i < 0) {
1161                 fprintf(stderr, "Tape read error while ");
1162                 switch (curfile.action) {
1163                 default:
1164                         fprintf(stderr, "trying to set up tape\n");
1165                         break;
1166                 case UNKNOWN:
1167                         fprintf(stderr, "trying to resynchronize\n");
1168                         break;
1169                 case USING:
1170                         fprintf(stderr, "restoring %s\n", curfile.name);
1171                         break;
1172                 case SKIP:
1173                         fprintf(stderr, "skipping over inode %lu\n",
1174                                 (unsigned long)curfile.ino);
1175                         break;
1176                 }
1177                 if (!yflag && !reply("continue"))
1178                         exit(1);
1179                 i = ntrec * TP_BSIZE;
1180                 memset(tapebuf, 0, (size_t)i);
1181 #ifdef RRESTORE
1182                 if (host)
1183                         seek_failed = (rmtseek(i, 1) < 0);
1184                 else
1185 #endif
1186                         seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1187
1188                 if (seek_failed)
1189                         err(1, "continuation failed");
1190         }
1191         /*
1192          * Handle end of tape.
1193          */
1194         if (i == 0) {
1195                 Vprintf(stdout, "End-of-tape encountered\n");
1196                 if (!pipein) {
1197                         newvol = volno + 1;
1198                         volno = 0;
1199                         numtrec = 0;
1200                         getvol(newvol);
1201                         readtape(buf);
1202                         return;
1203                 }
1204                 if (rd % TP_BSIZE != 0)
1205                         panic("partial block read: %d should be %d\n",
1206                                 rd, ntrec * TP_BSIZE);
1207                 terminateinput();
1208                 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1209         }
1210         blkcnt = 0;
1211         memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1212         blksread++;
1213         tpblksread++;
1214 }
1215
1216 static void
1217 findtapeblksize(void)
1218 {
1219         register long i;
1220
1221         for (i = 0; i < ntrec; i++)
1222                 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1223         blkcnt = 0;
1224 #ifdef RRESTORE
1225         if (host)
1226                 i = rmtread(tapebuf, (size_t)(ntrec * TP_BSIZE));
1227         else
1228 #endif
1229                 i = read(mt, tapebuf, (size_t)(ntrec * TP_BSIZE));
1230
1231         if (i <= 0)
1232                 err(1, "tape read error");
1233         if (i % TP_BSIZE != 0)
1234                 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)", 
1235                                 (long)i, TP_BSIZE);
1236         ntrec = i / TP_BSIZE;
1237         numtrec = ntrec;
1238         Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1239 }
1240
1241 void
1242 closemt(void)
1243 {
1244
1245         if (mt < 0)
1246                 return;
1247 #ifdef RRESTORE
1248         if (host)
1249                 rmtclose();
1250         else
1251 #endif
1252                 (void) close(mt);
1253 }
1254
1255 /*
1256  * Read the next block from the tape.
1257  * Check to see if it is one of several vintage headers.
1258  * If it is an old style header, convert it to a new style header.
1259  * If it is not any valid header, return an error.
1260  */
1261 static int
1262 gethead(struct s_spcl *buf)
1263 {
1264         int32_t i;
1265         union {
1266                 quad_t  qval;
1267                 int32_t val[2];
1268         } qcvt;
1269         union u_ospcl {
1270                 char dummy[TP_BSIZE];
1271                 struct  s_ospcl {
1272                         int32_t c_type;
1273                         int32_t c_date;
1274                         int32_t c_ddate;
1275                         int32_t c_volume;
1276                         int32_t c_tapea;
1277                         u_int16_t c_inumber;
1278                         int32_t c_magic;
1279                         int32_t c_checksum;
1280                         struct odinode {
1281                                 u_int16_t odi_mode;
1282                                 u_int16_t odi_nlink;
1283                                 u_int16_t odi_uid;
1284                                 u_int16_t odi_gid;
1285                                 int32_t odi_size;
1286                                 int32_t odi_rdev;
1287                                 char    odi_addr[36];
1288                                 int32_t odi_atime;
1289                                 int32_t odi_mtime;
1290                                 int32_t odi_ctime;
1291                         } c_dinode;
1292                         int32_t c_count;
1293                         char    c_addr[256];
1294                 } s_ospcl;
1295         } u_ospcl;
1296
1297         if (!cvtflag) {
1298                 readtape((char *)buf);
1299                 if (buf->c_magic != NFS_MAGIC) {
1300                         if (swabi(buf->c_magic) != NFS_MAGIC)
1301                                 return (FAIL);
1302                         if (!Bcvt) {
1303                                 Vprintf(stdout, "Note: Doing Byte swapping\n");
1304                                 Bcvt = 1;
1305                         }
1306                 }
1307                 if (checksum((int *)buf) == FAIL)
1308                         return (FAIL);
1309                 if (Bcvt)
1310                         swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1311                 goto good;
1312         }
1313         readtape((char *)(&u_ospcl.s_ospcl));
1314         memset((char *)buf, 0, (long)TP_BSIZE);
1315         buf->c_type = u_ospcl.s_ospcl.c_type;
1316         buf->c_date = u_ospcl.s_ospcl.c_date;
1317         buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1318         buf->c_volume = u_ospcl.s_ospcl.c_volume;
1319         buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1320         buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1321         buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1322         buf->c_magic = u_ospcl.s_ospcl.c_magic;
1323         buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1324         buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1325         buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1326         buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1327         buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1328         buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1329 #ifdef  __linux__
1330         buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1331         buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1332         buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1333 #else   /* __linux__ */
1334         buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1335         buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1336         buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1337 #endif  /* __linux__ */
1338         buf->c_count = u_ospcl.s_ospcl.c_count;
1339         memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1340         if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1341             checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1342                 return(FAIL);
1343         buf->c_magic = NFS_MAGIC;
1344
1345 good:
1346         if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1347             (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1348                 qcvt.qval = buf->c_dinode.di_size;
1349                 if (qcvt.val[0] || qcvt.val[1]) {
1350                         printf("Note: Doing Quad swapping\n");
1351                         Qcvt = 1;
1352                 }
1353         }
1354         if (Qcvt) {
1355                 qcvt.qval = buf->c_dinode.di_size;
1356                 i = qcvt.val[1];
1357                 qcvt.val[1] = qcvt.val[0];
1358                 qcvt.val[0] = i;
1359                 buf->c_dinode.di_size = qcvt.qval;
1360         }
1361         readmapflag = 0;
1362
1363         switch (buf->c_type) {
1364
1365         case TS_CLRI:
1366         case TS_BITS:
1367                 /*
1368                  * Have to patch up missing information in bit map headers
1369                  */
1370                 buf->c_inumber = 0;
1371                 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1372                 if (buf->c_count > TP_NINDIR)
1373                         readmapflag = 1;
1374                 else 
1375                         for (i = 0; i < buf->c_count; i++)
1376                                 buf->c_addr[i]++;
1377                 break;
1378
1379         case TS_TAPE:
1380                 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1381                         oldinofmt = 1;
1382                 /* fall through */
1383         case TS_END:
1384                 buf->c_inumber = 0;
1385                 break;
1386
1387         case TS_INODE:
1388         case TS_ADDR:
1389                 break;
1390
1391         default:
1392                 panic("gethead: unknown inode type %d\n", buf->c_type);
1393                 break;
1394         }
1395         /*
1396          * If we are restoring a filesystem with old format inodes,
1397          * copy the uid/gid to the new location.
1398          */
1399         if (oldinofmt) {
1400                 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1401                 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1402         }
1403         if (dflag)
1404                 accthdr(buf);
1405         return(GOOD);
1406 }
1407
1408 /*
1409  * Check that a header is where it belongs and predict the next header
1410  */
1411 static void
1412 accthdr(struct s_spcl *header)
1413 {
1414         static ino_t previno = 0x7fffffff;
1415         static int prevtype;
1416         static long predict;
1417         long blks, i;
1418
1419         if (header->c_type == TS_TAPE) {
1420                 fprintf(stderr, "Volume header (%s inode format) ",
1421                     oldinofmt ? "old" : "new");
1422                 if (header->c_firstrec)
1423                         fprintf(stderr, "begins with record %d",
1424                                 header->c_firstrec);
1425                 fprintf(stderr, "\n");
1426                 previno = 0x7fffffff;
1427                 return;
1428         }
1429         if (previno == 0x7fffffff)
1430                 goto newcalc;
1431         switch (prevtype) {
1432         case TS_BITS:
1433                 fprintf(stderr, "Dumped inodes map header");
1434                 break;
1435         case TS_CLRI:
1436                 fprintf(stderr, "Used inodes map header");
1437                 break;
1438         case TS_INODE:
1439                 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
1440                 break;
1441         case TS_ADDR:
1442                 fprintf(stderr, "File continuation header, ino %ld", previno);
1443                 break;
1444         case TS_END:
1445                 fprintf(stderr, "End of tape header");
1446                 break;
1447         }
1448         if (predict != blksread - 1)
1449                 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1450                         predict, blksread - 1);
1451         fprintf(stderr, "\n");
1452 newcalc:
1453         blks = 0;
1454         if (header->c_type != TS_END)
1455                 for (i = 0; i < header->c_count; i++)
1456                         if (readmapflag || header->c_addr[i] != 0)
1457                                 blks++;
1458         predict = blks;
1459         blksread = 0;
1460         prevtype = header->c_type;
1461         previno = header->c_inumber;
1462 }
1463
1464 /*
1465  * Find an inode header.
1466  * Complain if had to skip, and complain is set.
1467  */
1468 static void
1469 findinode(struct s_spcl *header)
1470 {
1471         static long skipcnt = 0;
1472         long i;
1473         char buf[TP_BSIZE];
1474
1475         curfile.name = "<name unknown>";
1476         curfile.action = UNKNOWN;
1477         curfile.dip = NULL;
1478         curfile.ino = 0;
1479         do {
1480                 if (header->c_magic != NFS_MAGIC) {
1481                         skipcnt++;
1482                         while (gethead(header) == FAIL ||
1483                             header->c_date != dumpdate)
1484                                 skipcnt++;
1485                 }
1486                 switch (header->c_type) {
1487
1488                 case TS_ADDR:
1489                         /*
1490                          * Skip up to the beginning of the next record
1491                          */
1492                         for (i = 0; i < header->c_count; i++)
1493                                 if (header->c_addr[i])
1494                                         readtape(buf);
1495                         while (gethead(header) == FAIL ||
1496                             header->c_date != dumpdate)
1497                                 skipcnt++;
1498                         break;
1499
1500                 case TS_INODE:
1501                         curfile.dip = &header->c_dinode;
1502                         curfile.ino = header->c_inumber;
1503                         break;
1504
1505                 case TS_END:
1506                         curfile.ino = maxino;
1507                         break;
1508
1509                 case TS_CLRI:
1510                         curfile.name = "<file removal list>";
1511                         break;
1512
1513                 case TS_BITS:
1514                         curfile.name = "<file dump list>";
1515                         break;
1516
1517                 case TS_TAPE:
1518                         panic("unexpected tape header\n");
1519                         /* NOTREACHED */
1520
1521                 default:
1522                         panic("unknown tape header type %d\n", spcl.c_type);
1523                         /* NOTREACHED */
1524
1525                 }
1526         } while (header->c_type == TS_ADDR);
1527         if (skipcnt > 0)
1528                 fprintf(stderr, "resync restore, skipped %ld blocks\n",
1529                     skipcnt);
1530         skipcnt = 0;
1531 }
1532
1533 static int
1534 checksum(int *buf)
1535 {
1536         register int i, j;
1537
1538         j = sizeof(union u_spcl) / sizeof(int);
1539         i = 0;
1540         if(!Bcvt) {
1541                 do
1542                         i += *buf++;
1543                 while (--j);
1544         } else {
1545                 /* What happens if we want to read restore tapes
1546                         for a 16bit int machine??? */
1547                 do
1548                         i += swabi(*buf++);
1549                 while (--j);
1550         }
1551
1552         if (i != CHECKSUM) {
1553                 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
1554                         (unsigned long)curfile.ino, curfile.name);
1555                 return(FAIL);
1556         }
1557         return(GOOD);
1558 }
1559
1560 #ifdef RRESTORE
1561 #ifdef __STDC__
1562 #include <stdarg.h>
1563 #else
1564 #include <varargs.h>
1565 #endif
1566
1567 void
1568 #ifdef __STDC__
1569 msg(const char *fmt, ...)
1570 #else
1571 msg(fmt, va_alist)
1572         char *fmt;
1573         va_dcl
1574 #endif
1575 {
1576         va_list ap;
1577 #ifdef __STDC__
1578         va_start(ap, fmt);
1579 #else
1580         va_start(ap);
1581 #endif
1582         (void)vfprintf(stderr, fmt, ap);
1583         va_end(ap);
1584 }
1585 #endif /* RRESTORE */
1586
1587 static u_char *
1588 swab16(u_char *sp, int n)
1589 {
1590         char c;
1591
1592         while (--n >= 0) {
1593                 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1594                 sp += 2;
1595         }
1596         return (sp);
1597 }
1598
1599 static u_char *
1600 swab32(u_char *sp, int n)
1601 {
1602         char c;
1603
1604         while (--n >= 0) {
1605                 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1606                 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
1607                 sp += 4;
1608         }
1609         return (sp);
1610 }
1611
1612 static u_char *
1613 swab64(u_char *sp, int n)
1614 {
1615         char c;
1616
1617         while (--n >= 0) {
1618                 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1619                 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1620                 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1621                 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1622                 sp += 8;
1623         }
1624         return (sp);
1625 }
1626
1627 void
1628 swabst(u_char *cp, u_char *sp)
1629 {
1630         int n = 0;
1631
1632         while (*cp) {
1633                 switch (*cp) {
1634                 case '0': case '1': case '2': case '3': case '4':
1635                 case '5': case '6': case '7': case '8': case '9':
1636                         n = (n * 10) + (*cp++ - '0');
1637                         continue;
1638
1639                 case 's': case 'w': case 'h':
1640                         if (n == 0)
1641                                 n = 1;
1642                         sp = swab16(sp, n);
1643                         break;
1644
1645                 case 'i':
1646                         if (n == 0)
1647                                 n = 1;
1648                         sp = swab32(sp, n);
1649                         break;
1650
1651                 case 'l':
1652                         if (n == 0)
1653                                 n = 1;
1654                         sp = swab64(sp, n);
1655                         break;
1656
1657                 default: /* Any other character, like 'b' counts as byte. */
1658                         if (n == 0)
1659                                 n = 1;
1660                         sp += n;
1661                         break;
1662                 }
1663                 cp++;
1664                 n = 0;
1665         }
1666 }
1667
1668 static u_int
1669 swabi(u_int x)
1670 {
1671         swabst((u_char *)"i", (u_char *)&x);
1672         return (x);
1673 }
1674
1675 static u_long
1676 swabl(u_long x)
1677 {
1678         swabst((u_char *)"l", (u_char *)&x);
1679         return (x);
1680 }