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