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