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