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