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