tape.c revision 1558
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1983, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes * (c) UNIX System Laboratories, Inc.
51558Srgrimes * All or some portions of this file are derived from material licensed
61558Srgrimes * to the University of California by American Telephone and Telegraph
71558Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81558Srgrimes * the permission of UNIX System Laboratories, Inc.
91558Srgrimes *
101558Srgrimes * Redistribution and use in source and binary forms, with or without
111558Srgrimes * modification, are permitted provided that the following conditions
121558Srgrimes * are met:
131558Srgrimes * 1. Redistributions of source code must retain the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer.
151558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161558Srgrimes *    notice, this list of conditions and the following disclaimer in the
171558Srgrimes *    documentation and/or other materials provided with the distribution.
181558Srgrimes * 3. All advertising materials mentioning features or use of this software
191558Srgrimes *    must display the following acknowledgement:
201558Srgrimes *	This product includes software developed by the University of
211558Srgrimes *	California, Berkeley and its contributors.
221558Srgrimes * 4. Neither the name of the University nor the names of its contributors
231558Srgrimes *    may be used to endorse or promote products derived from this software
241558Srgrimes *    without specific prior written permission.
251558Srgrimes *
261558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361558Srgrimes * SUCH DAMAGE.
371558Srgrimes */
381558Srgrimes
391558Srgrimes#ifndef lint
401558Srgrimesstatic char sccsid[] = "@(#)tape.c	8.3 (Berkeley) 4/1/94";
411558Srgrimes#endif /* not lint */
421558Srgrimes
431558Srgrimes#include <sys/param.h>
441558Srgrimes#include <sys/file.h>
451558Srgrimes#include <sys/ioctl.h>
461558Srgrimes#include <sys/mtio.h>
471558Srgrimes#include <sys/stat.h>
481558Srgrimes
491558Srgrimes#include <ufs/ufs/dinode.h>
501558Srgrimes#include <protocols/dumprestore.h>
511558Srgrimes
521558Srgrimes#include <errno.h>
531558Srgrimes#include <setjmp.h>
541558Srgrimes#include <stdio.h>
551558Srgrimes#include <stdlib.h>
561558Srgrimes#include <string.h>
571558Srgrimes#include <unistd.h>
581558Srgrimes
591558Srgrimes#include "restore.h"
601558Srgrimes#include "extern.h"
611558Srgrimes#include "pathnames.h"
621558Srgrimes
631558Srgrimesstatic long	fssize = MAXBSIZE;
641558Srgrimesstatic int	mt = -1;
651558Srgrimesstatic int	pipein = 0;
661558Srgrimesstatic char	magtape[BUFSIZ];
671558Srgrimesstatic int	blkcnt;
681558Srgrimesstatic int	numtrec;
691558Srgrimesstatic char	*tapebuf;
701558Srgrimesstatic union	u_spcl endoftapemark;
711558Srgrimesstatic long	blksread;		/* blocks read since last header */
721558Srgrimesstatic long	tpblksread = 0;		/* TP_BSIZE blocks read */
731558Srgrimesstatic long	tapesread;
741558Srgrimesstatic jmp_buf	restart;
751558Srgrimesstatic int	gettingfile = 0;	/* restart has a valid frame */
761558Srgrimesstatic char	*host = NULL;
771558Srgrimes
781558Srgrimesstatic int	ofile;
791558Srgrimesstatic char	*map;
801558Srgrimesstatic char	lnkbuf[MAXPATHLEN + 1];
811558Srgrimesstatic int	pathlen;
821558Srgrimes
831558Srgrimesint		oldinofmt;	/* old inode format conversion required */
841558Srgrimesint		Bcvt;		/* Swap Bytes (for CCI or sun) */
851558Srgrimesstatic int	Qcvt;		/* Swap quads (for sun) */
861558Srgrimes
871558Srgrimes#define	FLUSHTAPEBUF()	blkcnt = ntrec + 1
881558Srgrimes
891558Srgrimesstatic void	 accthdr __P((struct s_spcl *));
901558Srgrimesstatic int	 checksum __P((int *));
911558Srgrimesstatic void	 findinode __P((struct s_spcl *));
921558Srgrimesstatic void	 findtapeblksize __P((void));
931558Srgrimesstatic int	 gethead __P((struct s_spcl *));
941558Srgrimesstatic void	 readtape __P((char *));
951558Srgrimesstatic void	 setdumpnum __P((void));
961558Srgrimesstatic u_long	 swabl __P((u_long));
971558Srgrimesstatic u_char	*swablong __P((u_char *, int));
981558Srgrimesstatic u_char	*swabshort __P((u_char *, int));
991558Srgrimesstatic void	 terminateinput __P((void));
1001558Srgrimesstatic void	 xtrfile __P((char *, long));
1011558Srgrimesstatic void	 xtrlnkfile __P((char *, long));
1021558Srgrimesstatic void	 xtrlnkskip __P((char *, long));
1031558Srgrimesstatic void	 xtrmap __P((char *, long));
1041558Srgrimesstatic void	 xtrmapskip __P((char *, long));
1051558Srgrimesstatic void	 xtrskip __P((char *, long));
1061558Srgrimes
1071558Srgrimes/*
1081558Srgrimes * Set up an input source
1091558Srgrimes */
1101558Srgrimesvoid
1111558Srgrimessetinput(source)
1121558Srgrimes	char *source;
1131558Srgrimes{
1141558Srgrimes	FLUSHTAPEBUF();
1151558Srgrimes	if (bflag)
1161558Srgrimes		newtapebuf(ntrec);
1171558Srgrimes	else
1181558Srgrimes		newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
1191558Srgrimes	terminal = stdin;
1201558Srgrimes
1211558Srgrimes#ifdef RRESTORE
1221558Srgrimes	if (index(source, ':')) {
1231558Srgrimes		host = source;
1241558Srgrimes		source = index(host, ':');
1251558Srgrimes		*source++ = '\0';
1261558Srgrimes		if (rmthost(host) == 0)
1271558Srgrimes			done(1);
1281558Srgrimes	} else
1291558Srgrimes#endif
1301558Srgrimes	if (strcmp(source, "-") == 0) {
1311558Srgrimes		/*
1321558Srgrimes		 * Since input is coming from a pipe we must establish
1331558Srgrimes		 * our own connection to the terminal.
1341558Srgrimes		 */
1351558Srgrimes		terminal = fopen(_PATH_TTY, "r");
1361558Srgrimes		if (terminal == NULL) {
1371558Srgrimes			(void)fprintf(stderr, "cannot open %s: %s\n",
1381558Srgrimes			    _PATH_TTY, strerror(errno));
1391558Srgrimes			terminal = fopen(_PATH_DEVNULL, "r");
1401558Srgrimes			if (terminal == NULL) {
1411558Srgrimes				(void)fprintf(stderr, "cannot open %s: %s\n",
1421558Srgrimes				    _PATH_DEVNULL, strerror(errno));
1431558Srgrimes				done(1);
1441558Srgrimes			}
1451558Srgrimes		}
1461558Srgrimes		pipein++;
1471558Srgrimes	}
1481558Srgrimes	setuid(getuid());	/* no longer need or want root privileges */
1491558Srgrimes	(void) strcpy(magtape, source);
1501558Srgrimes}
1511558Srgrimes
1521558Srgrimesvoid
1531558Srgrimesnewtapebuf(size)
1541558Srgrimes	long size;
1551558Srgrimes{
1561558Srgrimes	static tapebufsize = -1;
1571558Srgrimes
1581558Srgrimes	ntrec = size;
1591558Srgrimes	if (size <= tapebufsize)
1601558Srgrimes		return;
1611558Srgrimes	if (tapebuf != NULL)
1621558Srgrimes		free(tapebuf);
1631558Srgrimes	tapebuf = malloc(size * TP_BSIZE);
1641558Srgrimes	if (tapebuf == NULL) {
1651558Srgrimes		fprintf(stderr, "Cannot allocate space for tape buffer\n");
1661558Srgrimes		done(1);
1671558Srgrimes	}
1681558Srgrimes	tapebufsize = size;
1691558Srgrimes}
1701558Srgrimes
1711558Srgrimes/*
1721558Srgrimes * Verify that the tape drive can be accessed and
1731558Srgrimes * that it actually is a dump tape.
1741558Srgrimes */
1751558Srgrimesvoid
1761558Srgrimessetup()
1771558Srgrimes{
1781558Srgrimes	int i, j, *ip;
1791558Srgrimes	struct stat stbuf;
1801558Srgrimes
1811558Srgrimes	vprintf(stdout, "Verify tape and initialize maps\n");
1821558Srgrimes#ifdef RRESTORE
1831558Srgrimes	if (host)
1841558Srgrimes		mt = rmtopen(magtape, 0);
1851558Srgrimes	else
1861558Srgrimes#endif
1871558Srgrimes	if (pipein)
1881558Srgrimes		mt = 0;
1891558Srgrimes	else
1901558Srgrimes		mt = open(magtape, O_RDONLY, 0);
1911558Srgrimes	if (mt < 0) {
1921558Srgrimes		fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
1931558Srgrimes		done(1);
1941558Srgrimes	}
1951558Srgrimes	volno = 1;
1961558Srgrimes	setdumpnum();
1971558Srgrimes	FLUSHTAPEBUF();
1981558Srgrimes	if (!pipein && !bflag)
1991558Srgrimes		findtapeblksize();
2001558Srgrimes	if (gethead(&spcl) == FAIL) {
2011558Srgrimes		blkcnt--; /* push back this block */
2021558Srgrimes		blksread--;
2031558Srgrimes		tpblksread--;
2041558Srgrimes		cvtflag++;
2051558Srgrimes		if (gethead(&spcl) == FAIL) {
2061558Srgrimes			fprintf(stderr, "Tape is not a dump tape\n");
2071558Srgrimes			done(1);
2081558Srgrimes		}
2091558Srgrimes		fprintf(stderr, "Converting to new file system format.\n");
2101558Srgrimes	}
2111558Srgrimes	if (pipein) {
2121558Srgrimes		endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
2131558Srgrimes		endoftapemark.s_spcl.c_type = TS_END;
2141558Srgrimes		ip = (int *)&endoftapemark;
2151558Srgrimes		j = sizeof(union u_spcl) / sizeof(int);
2161558Srgrimes		i = 0;
2171558Srgrimes		do
2181558Srgrimes			i += *ip++;
2191558Srgrimes		while (--j);
2201558Srgrimes		endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
2211558Srgrimes	}
2221558Srgrimes	if (vflag || command == 't')
2231558Srgrimes		printdumpinfo();
2241558Srgrimes	dumptime = spcl.c_ddate;
2251558Srgrimes	dumpdate = spcl.c_date;
2261558Srgrimes	if (stat(".", &stbuf) < 0) {
2271558Srgrimes		fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
2281558Srgrimes		done(1);
2291558Srgrimes	}
2301558Srgrimes	if (stbuf.st_blksize > 0 && stbuf.st_blksize <= MAXBSIZE)
2311558Srgrimes		fssize = stbuf.st_blksize;
2321558Srgrimes	if (((fssize - 1) & fssize) != 0) {
2331558Srgrimes		fprintf(stderr, "bad block size %d\n", fssize);
2341558Srgrimes		done(1);
2351558Srgrimes	}
2361558Srgrimes	if (spcl.c_volume != 1) {
2371558Srgrimes		fprintf(stderr, "Tape is not volume 1 of the dump\n");
2381558Srgrimes		done(1);
2391558Srgrimes	}
2401558Srgrimes	if (gethead(&spcl) == FAIL) {
2411558Srgrimes		dprintf(stdout, "header read failed at %d blocks\n", blksread);
2421558Srgrimes		panic("no header after volume mark!\n");
2431558Srgrimes	}
2441558Srgrimes	findinode(&spcl);
2451558Srgrimes	if (spcl.c_type != TS_CLRI) {
2461558Srgrimes		fprintf(stderr, "Cannot find file removal list\n");
2471558Srgrimes		done(1);
2481558Srgrimes	}
2491558Srgrimes	maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
2501558Srgrimes	dprintf(stdout, "maxino = %d\n", maxino);
2511558Srgrimes	map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
2521558Srgrimes	if (map == NULL)
2531558Srgrimes		panic("no memory for file removal list\n");
2541558Srgrimes	clrimap = map;
2551558Srgrimes	curfile.action = USING;
2561558Srgrimes	getfile(xtrmap, xtrmapskip);
2571558Srgrimes	if (spcl.c_type != TS_BITS) {
2581558Srgrimes		fprintf(stderr, "Cannot find file dump list\n");
2591558Srgrimes		done(1);
2601558Srgrimes	}
2611558Srgrimes	map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
2621558Srgrimes	if (map == (char *)NULL)
2631558Srgrimes		panic("no memory for file dump list\n");
2641558Srgrimes	dumpmap = map;
2651558Srgrimes	curfile.action = USING;
2661558Srgrimes	getfile(xtrmap, xtrmapskip);
2671558Srgrimes}
2681558Srgrimes
2691558Srgrimes/*
2701558Srgrimes * Prompt user to load a new dump volume.
2711558Srgrimes * "Nextvol" is the next suggested volume to use.
2721558Srgrimes * This suggested volume is enforced when doing full
2731558Srgrimes * or incremental restores, but can be overrridden by
2741558Srgrimes * the user when only extracting a subset of the files.
2751558Srgrimes */
2761558Srgrimesvoid
2771558Srgrimesgetvol(nextvol)
2781558Srgrimes	long nextvol;
2791558Srgrimes{
2801558Srgrimes	long newvol, savecnt, wantnext, i;
2811558Srgrimes	union u_spcl tmpspcl;
2821558Srgrimes#	define tmpbuf tmpspcl.s_spcl
2831558Srgrimes	char buf[TP_BSIZE];
2841558Srgrimes
2851558Srgrimes	if (nextvol == 1) {
2861558Srgrimes		tapesread = 0;
2871558Srgrimes		gettingfile = 0;
2881558Srgrimes	}
2891558Srgrimes	if (pipein) {
2901558Srgrimes		if (nextvol != 1)
2911558Srgrimes			panic("Changing volumes on pipe input?\n");
2921558Srgrimes		if (volno == 1)
2931558Srgrimes			return;
2941558Srgrimes		goto gethdr;
2951558Srgrimes	}
2961558Srgrimes	savecnt = blksread;
2971558Srgrimesagain:
2981558Srgrimes	if (pipein)
2991558Srgrimes		done(1); /* pipes do not get a second chance */
3001558Srgrimes	if (command == 'R' || command == 'r' || curfile.action != SKIP) {
3011558Srgrimes		newvol = nextvol;
3021558Srgrimes		wantnext = 1;
3031558Srgrimes	} else {
3041558Srgrimes		newvol = 0;
3051558Srgrimes		wantnext = 0;
3061558Srgrimes	}
3071558Srgrimes	while (newvol <= 0) {
3081558Srgrimes		if (tapesread == 0) {
3091558Srgrimes			fprintf(stderr, "%s%s%s%s%s",
3101558Srgrimes			    "You have not read any tapes yet.\n",
3111558Srgrimes			    "Unless you know which volume your",
3121558Srgrimes			    " file(s) are on you should start\n",
3131558Srgrimes			    "with the last volume and work",
3141558Srgrimes			    " towards towards the first.\n");
3151558Srgrimes		} else {
3161558Srgrimes			fprintf(stderr, "You have read volumes");
3171558Srgrimes			strcpy(buf, ": ");
3181558Srgrimes			for (i = 1; i < 32; i++)
3191558Srgrimes				if (tapesread & (1 << i)) {
3201558Srgrimes					fprintf(stderr, "%s%d", buf, i);
3211558Srgrimes					strcpy(buf, ", ");
3221558Srgrimes				}
3231558Srgrimes			fprintf(stderr, "\n");
3241558Srgrimes		}
3251558Srgrimes		do	{
3261558Srgrimes			fprintf(stderr, "Specify next volume #: ");
3271558Srgrimes			(void) fflush(stderr);
3281558Srgrimes			(void) fgets(buf, BUFSIZ, terminal);
3291558Srgrimes		} while (!feof(terminal) && buf[0] == '\n');
3301558Srgrimes		if (feof(terminal))
3311558Srgrimes			done(1);
3321558Srgrimes		newvol = atoi(buf);
3331558Srgrimes		if (newvol <= 0) {
3341558Srgrimes			fprintf(stderr,
3351558Srgrimes			    "Volume numbers are positive numerics\n");
3361558Srgrimes		}
3371558Srgrimes	}
3381558Srgrimes	if (newvol == volno) {
3391558Srgrimes		tapesread |= 1 << volno;
3401558Srgrimes		return;
3411558Srgrimes	}
3421558Srgrimes	closemt();
3431558Srgrimes	fprintf(stderr, "Mount tape volume %d\n", newvol);
3441558Srgrimes	fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
3451558Srgrimes	fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
3461558Srgrimes	(void) fflush(stderr);
3471558Srgrimes	(void) fgets(buf, BUFSIZ, terminal);
3481558Srgrimes	if (feof(terminal))
3491558Srgrimes		done(1);
3501558Srgrimes	if (!strcmp(buf, "none\n")) {
3511558Srgrimes		terminateinput();
3521558Srgrimes		return;
3531558Srgrimes	}
3541558Srgrimes	if (buf[0] != '\n') {
3551558Srgrimes		(void) strcpy(magtape, buf);
3561558Srgrimes		magtape[strlen(magtape) - 1] = '\0';
3571558Srgrimes	}
3581558Srgrimes#ifdef RRESTORE
3591558Srgrimes	if (host)
3601558Srgrimes		mt = rmtopen(magtape, 0);
3611558Srgrimes	else
3621558Srgrimes#endif
3631558Srgrimes		mt = open(magtape, O_RDONLY, 0);
3641558Srgrimes
3651558Srgrimes	if (mt == -1) {
3661558Srgrimes		fprintf(stderr, "Cannot open %s\n", magtape);
3671558Srgrimes		volno = -1;
3681558Srgrimes		goto again;
3691558Srgrimes	}
3701558Srgrimesgethdr:
3711558Srgrimes	volno = newvol;
3721558Srgrimes	setdumpnum();
3731558Srgrimes	FLUSHTAPEBUF();
3741558Srgrimes	if (gethead(&tmpbuf) == FAIL) {
3751558Srgrimes		dprintf(stdout, "header read failed at %d blocks\n", blksread);
3761558Srgrimes		fprintf(stderr, "tape is not dump tape\n");
3771558Srgrimes		volno = 0;
3781558Srgrimes		goto again;
3791558Srgrimes	}
3801558Srgrimes	if (tmpbuf.c_volume != volno) {
3811558Srgrimes		fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
3821558Srgrimes		volno = 0;
3831558Srgrimes		goto again;
3841558Srgrimes	}
3851558Srgrimes	if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
3861558Srgrimes		fprintf(stderr, "Wrong dump date\n\tgot: %s",
3871558Srgrimes			ctime(&tmpbuf.c_date));
3881558Srgrimes		fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
3891558Srgrimes		volno = 0;
3901558Srgrimes		goto again;
3911558Srgrimes	}
3921558Srgrimes	tapesread |= 1 << volno;
3931558Srgrimes	blksread = savecnt;
3941558Srgrimes 	/*
3951558Srgrimes 	 * If continuing from the previous volume, skip over any
3961558Srgrimes 	 * blocks read already at the end of the previous volume.
3971558Srgrimes 	 *
3981558Srgrimes 	 * If coming to this volume at random, skip to the beginning
3991558Srgrimes 	 * of the next record.
4001558Srgrimes 	 */
4011558Srgrimes	dprintf(stdout, "read %ld recs, tape starts with %ld\n",
4021558Srgrimes		tpblksread, tmpbuf.c_firstrec);
4031558Srgrimes 	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
4041558Srgrimes 		if (!wantnext) {
4051558Srgrimes 			tpblksread = tmpbuf.c_firstrec;
4061558Srgrimes 			for (i = tmpbuf.c_count; i > 0; i--)
4071558Srgrimes 				readtape(buf);
4081558Srgrimes 		} else if (tmpbuf.c_firstrec > 0 &&
4091558Srgrimes			   tmpbuf.c_firstrec < tpblksread - 1) {
4101558Srgrimes			/*
4111558Srgrimes			 * -1 since we've read the volume header
4121558Srgrimes			 */
4131558Srgrimes 			i = tpblksread - tmpbuf.c_firstrec - 1;
4141558Srgrimes			dprintf(stderr, "Skipping %d duplicate record%s.\n",
4151558Srgrimes				i, i > 1 ? "s" : "");
4161558Srgrimes 			while (--i >= 0)
4171558Srgrimes 				readtape(buf);
4181558Srgrimes 		}
4191558Srgrimes 	}
4201558Srgrimes	if (curfile.action == USING) {
4211558Srgrimes		if (volno == 1)
4221558Srgrimes			panic("active file into volume 1\n");
4231558Srgrimes		return;
4241558Srgrimes	}
4251558Srgrimes	/*
4261558Srgrimes	 * Skip up to the beginning of the next record
4271558Srgrimes	 */
4281558Srgrimes	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
4291558Srgrimes		for (i = tmpbuf.c_count; i > 0; i--)
4301558Srgrimes			readtape(buf);
4311558Srgrimes	(void) gethead(&spcl);
4321558Srgrimes	findinode(&spcl);
4331558Srgrimes	if (gettingfile) {
4341558Srgrimes		gettingfile = 0;
4351558Srgrimes		longjmp(restart, 1);
4361558Srgrimes	}
4371558Srgrimes}
4381558Srgrimes
4391558Srgrimes/*
4401558Srgrimes * Handle unexpected EOF.
4411558Srgrimes */
4421558Srgrimesstatic void
4431558Srgrimesterminateinput()
4441558Srgrimes{
4451558Srgrimes
4461558Srgrimes	if (gettingfile && curfile.action == USING) {
4471558Srgrimes		printf("Warning: %s %s\n",
4481558Srgrimes		    "End-of-input encountered while extracting", curfile.name);
4491558Srgrimes	}
4501558Srgrimes	curfile.name = "<name unknown>";
4511558Srgrimes	curfile.action = UNKNOWN;
4521558Srgrimes	curfile.dip = NULL;
4531558Srgrimes	curfile.ino = maxino;
4541558Srgrimes	if (gettingfile) {
4551558Srgrimes		gettingfile = 0;
4561558Srgrimes		longjmp(restart, 1);
4571558Srgrimes	}
4581558Srgrimes}
4591558Srgrimes
4601558Srgrimes/*
4611558Srgrimes * handle multiple dumps per tape by skipping forward to the
4621558Srgrimes * appropriate one.
4631558Srgrimes */
4641558Srgrimesstatic void
4651558Srgrimessetdumpnum()
4661558Srgrimes{
4671558Srgrimes	struct mtop tcom;
4681558Srgrimes
4691558Srgrimes	if (dumpnum == 1 || volno != 1)
4701558Srgrimes		return;
4711558Srgrimes	if (pipein) {
4721558Srgrimes		fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
4731558Srgrimes		done(1);
4741558Srgrimes	}
4751558Srgrimes	tcom.mt_op = MTFSF;
4761558Srgrimes	tcom.mt_count = dumpnum - 1;
4771558Srgrimes#ifdef RRESTORE
4781558Srgrimes	if (host)
4791558Srgrimes		rmtioctl(MTFSF, dumpnum - 1);
4801558Srgrimes	else
4811558Srgrimes#endif
4821558Srgrimes		if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
4831558Srgrimes			fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
4841558Srgrimes}
4851558Srgrimes
4861558Srgrimesvoid
4871558Srgrimesprintdumpinfo()
4881558Srgrimes{
4891558Srgrimes	fprintf(stdout, "Dump   date: %s", ctime(&spcl.c_date));
4901558Srgrimes	fprintf(stdout, "Dumped from: %s",
4911558Srgrimes	    (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
4921558Srgrimes	if (spcl.c_host[0] == '\0')
4931558Srgrimes		return;
4941558Srgrimes	fprintf(stderr, "Level %d dump of %s on %s:%s\n",
4951558Srgrimes		spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
4961558Srgrimes	fprintf(stderr, "Label: %s\n", spcl.c_label);
4971558Srgrimes}
4981558Srgrimes
4991558Srgrimesint
5001558Srgrimesextractfile(name)
5011558Srgrimes	char *name;
5021558Srgrimes{
5031558Srgrimes	int mode;
5041558Srgrimes	struct timeval timep[2];
5051558Srgrimes	struct entry *ep;
5061558Srgrimes
5071558Srgrimes	curfile.name = name;
5081558Srgrimes	curfile.action = USING;
5091558Srgrimes	timep[0].tv_sec = curfile.dip->di_atime.ts_sec;
5101558Srgrimes	timep[0].tv_usec = curfile.dip->di_atime.ts_nsec / 1000;
5111558Srgrimes	timep[1].tv_sec = curfile.dip->di_mtime.ts_sec;
5121558Srgrimes	timep[1].tv_usec = curfile.dip->di_mtime.ts_nsec / 1000;
5131558Srgrimes	mode = curfile.dip->di_mode;
5141558Srgrimes	switch (mode & IFMT) {
5151558Srgrimes
5161558Srgrimes	default:
5171558Srgrimes		fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
5181558Srgrimes		skipfile();
5191558Srgrimes		return (FAIL);
5201558Srgrimes
5211558Srgrimes	case IFSOCK:
5221558Srgrimes		vprintf(stdout, "skipped socket %s\n", name);
5231558Srgrimes		skipfile();
5241558Srgrimes		return (GOOD);
5251558Srgrimes
5261558Srgrimes	case IFDIR:
5271558Srgrimes		if (mflag) {
5281558Srgrimes			ep = lookupname(name);
5291558Srgrimes			if (ep == NULL || ep->e_flags & EXTRACT)
5301558Srgrimes				panic("unextracted directory %s\n", name);
5311558Srgrimes			skipfile();
5321558Srgrimes			return (GOOD);
5331558Srgrimes		}
5341558Srgrimes		vprintf(stdout, "extract file %s\n", name);
5351558Srgrimes		return (genliteraldir(name, curfile.ino));
5361558Srgrimes
5371558Srgrimes	case IFLNK:
5381558Srgrimes		lnkbuf[0] = '\0';
5391558Srgrimes		pathlen = 0;
5401558Srgrimes		getfile(xtrlnkfile, xtrlnkskip);
5411558Srgrimes		if (pathlen == 0) {
5421558Srgrimes			vprintf(stdout,
5431558Srgrimes			    "%s: zero length symbolic link (ignored)\n", name);
5441558Srgrimes			return (GOOD);
5451558Srgrimes		}
5461558Srgrimes		return (linkit(lnkbuf, name, SYMLINK));
5471558Srgrimes
5481558Srgrimes	case IFCHR:
5491558Srgrimes	case IFBLK:
5501558Srgrimes		vprintf(stdout, "extract special file %s\n", name);
5511558Srgrimes		if (Nflag) {
5521558Srgrimes			skipfile();
5531558Srgrimes			return (GOOD);
5541558Srgrimes		}
5551558Srgrimes		if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
5561558Srgrimes			fprintf(stderr, "%s: cannot create special file: %s\n",
5571558Srgrimes			    name, strerror(errno));
5581558Srgrimes			skipfile();
5591558Srgrimes			return (FAIL);
5601558Srgrimes		}
5611558Srgrimes		(void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
5621558Srgrimes		(void) chmod(name, mode);
5631558Srgrimes		skipfile();
5641558Srgrimes		utimes(name, timep);
5651558Srgrimes		return (GOOD);
5661558Srgrimes
5671558Srgrimes	case IFREG:
5681558Srgrimes		vprintf(stdout, "extract file %s\n", name);
5691558Srgrimes		if (Nflag) {
5701558Srgrimes			skipfile();
5711558Srgrimes			return (GOOD);
5721558Srgrimes		}
5731558Srgrimes		if ((ofile = creat(name, 0666)) < 0) {
5741558Srgrimes			fprintf(stderr, "%s: cannot create file: %s\n",
5751558Srgrimes			    name, strerror(errno));
5761558Srgrimes			skipfile();
5771558Srgrimes			return (FAIL);
5781558Srgrimes		}
5791558Srgrimes		(void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
5801558Srgrimes		(void) fchmod(ofile, mode);
5811558Srgrimes		getfile(xtrfile, xtrskip);
5821558Srgrimes		(void) close(ofile);
5831558Srgrimes		utimes(name, timep);
5841558Srgrimes		return (GOOD);
5851558Srgrimes	}
5861558Srgrimes	/* NOTREACHED */
5871558Srgrimes}
5881558Srgrimes
5891558Srgrimes/*
5901558Srgrimes * skip over bit maps on the tape
5911558Srgrimes */
5921558Srgrimesvoid
5931558Srgrimesskipmaps()
5941558Srgrimes{
5951558Srgrimes
5961558Srgrimes	while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
5971558Srgrimes		skipfile();
5981558Srgrimes}
5991558Srgrimes
6001558Srgrimes/*
6011558Srgrimes * skip over a file on the tape
6021558Srgrimes */
6031558Srgrimesvoid
6041558Srgrimesskipfile()
6051558Srgrimes{
6061558Srgrimes
6071558Srgrimes	curfile.action = SKIP;
6081558Srgrimes	getfile(xtrnull, xtrnull);
6091558Srgrimes}
6101558Srgrimes
6111558Srgrimes/*
6121558Srgrimes * Extract a file from the tape.
6131558Srgrimes * When an allocated block is found it is passed to the fill function;
6141558Srgrimes * when an unallocated block (hole) is found, a zeroed buffer is passed
6151558Srgrimes * to the skip function.
6161558Srgrimes */
6171558Srgrimesvoid
6181558Srgrimesgetfile(fill, skip)
6191558Srgrimes	void	(*fill) __P((char *, long));
6201558Srgrimes	void	(*skip) __P((char *, long));
6211558Srgrimes{
6221558Srgrimes	register int i;
6231558Srgrimes	int curblk = 0;
6241558Srgrimes	long size = spcl.c_dinode.di_size;
6251558Srgrimes	static char clearedbuf[MAXBSIZE];
6261558Srgrimes	char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
6271558Srgrimes	char junk[TP_BSIZE];
6281558Srgrimes
6291558Srgrimes	if (spcl.c_type == TS_END)
6301558Srgrimes		panic("ran off end of tape\n");
6311558Srgrimes	if (spcl.c_magic != NFS_MAGIC)
6321558Srgrimes		panic("not at beginning of a file\n");
6331558Srgrimes	if (!gettingfile && setjmp(restart) != 0)
6341558Srgrimes		return;
6351558Srgrimes	gettingfile++;
6361558Srgrimesloop:
6371558Srgrimes	for (i = 0; i < spcl.c_count; i++) {
6381558Srgrimes		if (spcl.c_addr[i]) {
6391558Srgrimes			readtape(&buf[curblk++][0]);
6401558Srgrimes			if (curblk == fssize / TP_BSIZE) {
6411558Srgrimes				(*fill)((char *)buf, size > TP_BSIZE ?
6421558Srgrimes				     (long) (fssize) :
6431558Srgrimes				     (curblk - 1) * TP_BSIZE + size);
6441558Srgrimes				curblk = 0;
6451558Srgrimes			}
6461558Srgrimes		} else {
6471558Srgrimes			if (curblk > 0) {
6481558Srgrimes				(*fill)((char *)buf, size > TP_BSIZE ?
6491558Srgrimes				     (long) (curblk * TP_BSIZE) :
6501558Srgrimes				     (curblk - 1) * TP_BSIZE + size);
6511558Srgrimes				curblk = 0;
6521558Srgrimes			}
6531558Srgrimes			(*skip)(clearedbuf, size > TP_BSIZE ?
6541558Srgrimes				(long) TP_BSIZE : size);
6551558Srgrimes		}
6561558Srgrimes		if ((size -= TP_BSIZE) <= 0) {
6571558Srgrimes			for (i++; i < spcl.c_count; i++)
6581558Srgrimes				if (spcl.c_addr[i])
6591558Srgrimes					readtape(junk);
6601558Srgrimes			break;
6611558Srgrimes		}
6621558Srgrimes	}
6631558Srgrimes	if (gethead(&spcl) == GOOD && size > 0) {
6641558Srgrimes		if (spcl.c_type == TS_ADDR)
6651558Srgrimes			goto loop;
6661558Srgrimes		dprintf(stdout,
6671558Srgrimes			"Missing address (header) block for %s at %d blocks\n",
6681558Srgrimes			curfile.name, blksread);
6691558Srgrimes	}
6701558Srgrimes	if (curblk > 0)
6711558Srgrimes		(*fill)((char *)buf, (curblk * TP_BSIZE) + size);
6721558Srgrimes	findinode(&spcl);
6731558Srgrimes	gettingfile = 0;
6741558Srgrimes}
6751558Srgrimes
6761558Srgrimes/*
6771558Srgrimes * Write out the next block of a file.
6781558Srgrimes */
6791558Srgrimesstatic void
6801558Srgrimesxtrfile(buf, size)
6811558Srgrimes	char	*buf;
6821558Srgrimes	long	size;
6831558Srgrimes{
6841558Srgrimes
6851558Srgrimes	if (Nflag)
6861558Srgrimes		return;
6871558Srgrimes	if (write(ofile, buf, (int) size) == -1) {
6881558Srgrimes		fprintf(stderr,
6891558Srgrimes		    "write error extracting inode %d, name %s\nwrite: %s\n",
6901558Srgrimes			curfile.ino, curfile.name, strerror(errno));
6911558Srgrimes		done(1);
6921558Srgrimes	}
6931558Srgrimes}
6941558Srgrimes
6951558Srgrimes/*
6961558Srgrimes * Skip over a hole in a file.
6971558Srgrimes */
6981558Srgrimes/* ARGSUSED */
6991558Srgrimesstatic void
7001558Srgrimesxtrskip(buf, size)
7011558Srgrimes	char *buf;
7021558Srgrimes	long size;
7031558Srgrimes{
7041558Srgrimes
7051558Srgrimes	if (lseek(ofile, size, SEEK_CUR) == -1) {
7061558Srgrimes		fprintf(stderr,
7071558Srgrimes		    "seek error extracting inode %d, name %s\nlseek: %s\n",
7081558Srgrimes			curfile.ino, curfile.name, strerror(errno));
7091558Srgrimes		done(1);
7101558Srgrimes	}
7111558Srgrimes}
7121558Srgrimes
7131558Srgrimes/*
7141558Srgrimes * Collect the next block of a symbolic link.
7151558Srgrimes */
7161558Srgrimesstatic void
7171558Srgrimesxtrlnkfile(buf, size)
7181558Srgrimes	char	*buf;
7191558Srgrimes	long	size;
7201558Srgrimes{
7211558Srgrimes
7221558Srgrimes	pathlen += size;
7231558Srgrimes	if (pathlen > MAXPATHLEN) {
7241558Srgrimes		fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
7251558Srgrimes		    curfile.name, lnkbuf, buf, pathlen);
7261558Srgrimes		done(1);
7271558Srgrimes	}
7281558Srgrimes	(void) strcat(lnkbuf, buf);
7291558Srgrimes}
7301558Srgrimes
7311558Srgrimes/*
7321558Srgrimes * Skip over a hole in a symbolic link (should never happen).
7331558Srgrimes */
7341558Srgrimes/* ARGSUSED */
7351558Srgrimesstatic void
7361558Srgrimesxtrlnkskip(buf, size)
7371558Srgrimes	char *buf;
7381558Srgrimes	long size;
7391558Srgrimes{
7401558Srgrimes
7411558Srgrimes	fprintf(stderr, "unallocated block in symbolic link %s\n",
7421558Srgrimes		curfile.name);
7431558Srgrimes	done(1);
7441558Srgrimes}
7451558Srgrimes
7461558Srgrimes/*
7471558Srgrimes * Collect the next block of a bit map.
7481558Srgrimes */
7491558Srgrimesstatic void
7501558Srgrimesxtrmap(buf, size)
7511558Srgrimes	char	*buf;
7521558Srgrimes	long	size;
7531558Srgrimes{
7541558Srgrimes
7551558Srgrimes	bcopy(buf, map, size);
7561558Srgrimes	map += size;
7571558Srgrimes}
7581558Srgrimes
7591558Srgrimes/*
7601558Srgrimes * Skip over a hole in a bit map (should never happen).
7611558Srgrimes */
7621558Srgrimes/* ARGSUSED */
7631558Srgrimesstatic void
7641558Srgrimesxtrmapskip(buf, size)
7651558Srgrimes	char *buf;
7661558Srgrimes	long size;
7671558Srgrimes{
7681558Srgrimes
7691558Srgrimes	panic("hole in map\n");
7701558Srgrimes	map += size;
7711558Srgrimes}
7721558Srgrimes
7731558Srgrimes/*
7741558Srgrimes * Noop, when an extraction function is not needed.
7751558Srgrimes */
7761558Srgrimes/* ARGSUSED */
7771558Srgrimesvoid
7781558Srgrimesxtrnull(buf, size)
7791558Srgrimes	char *buf;
7801558Srgrimes	long size;
7811558Srgrimes{
7821558Srgrimes
7831558Srgrimes	return;
7841558Srgrimes}
7851558Srgrimes
7861558Srgrimes/*
7871558Srgrimes * Read TP_BSIZE blocks from the input.
7881558Srgrimes * Handle read errors, and end of media.
7891558Srgrimes */
7901558Srgrimesstatic void
7911558Srgrimesreadtape(buf)
7921558Srgrimes	char *buf;
7931558Srgrimes{
7941558Srgrimes	long rd, newvol, i;
7951558Srgrimes	int cnt, seek_failed;
7961558Srgrimes
7971558Srgrimes	if (blkcnt < numtrec) {
7981558Srgrimes		bcopy(&tapebuf[(blkcnt++ * TP_BSIZE)], buf, (long)TP_BSIZE);
7991558Srgrimes		blksread++;
8001558Srgrimes		tpblksread++;
8011558Srgrimes		return;
8021558Srgrimes	}
8031558Srgrimes	for (i = 0; i < ntrec; i++)
8041558Srgrimes		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
8051558Srgrimes	if (numtrec == 0)
8061558Srgrimes		numtrec = ntrec;
8071558Srgrimes	cnt = ntrec * TP_BSIZE;
8081558Srgrimes	rd = 0;
8091558Srgrimesgetmore:
8101558Srgrimes#ifdef RRESTORE
8111558Srgrimes	if (host)
8121558Srgrimes		i = rmtread(&tapebuf[rd], cnt);
8131558Srgrimes	else
8141558Srgrimes#endif
8151558Srgrimes		i = read(mt, &tapebuf[rd], cnt);
8161558Srgrimes	/*
8171558Srgrimes	 * Check for mid-tape short read error.
8181558Srgrimes	 * If found, skip rest of buffer and start with the next.
8191558Srgrimes	 */
8201558Srgrimes	if (!pipein && numtrec < ntrec && i > 0) {
8211558Srgrimes		dprintf(stdout, "mid-media short read error.\n");
8221558Srgrimes		numtrec = ntrec;
8231558Srgrimes	}
8241558Srgrimes	/*
8251558Srgrimes	 * Handle partial block read.
8261558Srgrimes	 */
8271558Srgrimes	if (pipein && i == 0 && rd > 0)
8281558Srgrimes		i = rd;
8291558Srgrimes	else if (i > 0 && i != ntrec * TP_BSIZE) {
8301558Srgrimes		if (pipein) {
8311558Srgrimes			rd += i;
8321558Srgrimes			cnt -= i;
8331558Srgrimes			if (cnt > 0)
8341558Srgrimes				goto getmore;
8351558Srgrimes			i = rd;
8361558Srgrimes		} else {
8371558Srgrimes			/*
8381558Srgrimes			 * Short read. Process the blocks read.
8391558Srgrimes			 */
8401558Srgrimes			if (i % TP_BSIZE != 0)
8411558Srgrimes				vprintf(stdout,
8421558Srgrimes				    "partial block read: %d should be %d\n",
8431558Srgrimes				    i, ntrec * TP_BSIZE);
8441558Srgrimes			numtrec = i / TP_BSIZE;
8451558Srgrimes		}
8461558Srgrimes	}
8471558Srgrimes	/*
8481558Srgrimes	 * Handle read error.
8491558Srgrimes	 */
8501558Srgrimes	if (i < 0) {
8511558Srgrimes		fprintf(stderr, "Tape read error while ");
8521558Srgrimes		switch (curfile.action) {
8531558Srgrimes		default:
8541558Srgrimes			fprintf(stderr, "trying to set up tape\n");
8551558Srgrimes			break;
8561558Srgrimes		case UNKNOWN:
8571558Srgrimes			fprintf(stderr, "trying to resynchronize\n");
8581558Srgrimes			break;
8591558Srgrimes		case USING:
8601558Srgrimes			fprintf(stderr, "restoring %s\n", curfile.name);
8611558Srgrimes			break;
8621558Srgrimes		case SKIP:
8631558Srgrimes			fprintf(stderr, "skipping over inode %d\n",
8641558Srgrimes				curfile.ino);
8651558Srgrimes			break;
8661558Srgrimes		}
8671558Srgrimes		if (!yflag && !reply("continue"))
8681558Srgrimes			done(1);
8691558Srgrimes		i = ntrec * TP_BSIZE;
8701558Srgrimes		bzero(tapebuf, i);
8711558Srgrimes#ifdef RRESTORE
8721558Srgrimes		if (host)
8731558Srgrimes			seek_failed = (rmtseek(i, 1) < 0);
8741558Srgrimes		else
8751558Srgrimes#endif
8761558Srgrimes			seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
8771558Srgrimes
8781558Srgrimes		if (seek_failed) {
8791558Srgrimes			fprintf(stderr,
8801558Srgrimes			    "continuation failed: %s\n", strerror(errno));
8811558Srgrimes			done(1);
8821558Srgrimes		}
8831558Srgrimes	}
8841558Srgrimes	/*
8851558Srgrimes	 * Handle end of tape.
8861558Srgrimes	 */
8871558Srgrimes	if (i == 0) {
8881558Srgrimes		vprintf(stdout, "End-of-tape encountered\n");
8891558Srgrimes		if (!pipein) {
8901558Srgrimes			newvol = volno + 1;
8911558Srgrimes			volno = 0;
8921558Srgrimes			numtrec = 0;
8931558Srgrimes			getvol(newvol);
8941558Srgrimes			readtape(buf);
8951558Srgrimes			return;
8961558Srgrimes		}
8971558Srgrimes		if (rd % TP_BSIZE != 0)
8981558Srgrimes			panic("partial block read: %d should be %d\n",
8991558Srgrimes				rd, ntrec * TP_BSIZE);
9001558Srgrimes		terminateinput();
9011558Srgrimes		bcopy((char *)&endoftapemark, &tapebuf[rd], (long)TP_BSIZE);
9021558Srgrimes	}
9031558Srgrimes	blkcnt = 0;
9041558Srgrimes	bcopy(&tapebuf[(blkcnt++ * TP_BSIZE)], buf, (long)TP_BSIZE);
9051558Srgrimes	blksread++;
9061558Srgrimes	tpblksread++;
9071558Srgrimes}
9081558Srgrimes
9091558Srgrimesstatic void
9101558Srgrimesfindtapeblksize()
9111558Srgrimes{
9121558Srgrimes	register long i;
9131558Srgrimes
9141558Srgrimes	for (i = 0; i < ntrec; i++)
9151558Srgrimes		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
9161558Srgrimes	blkcnt = 0;
9171558Srgrimes#ifdef RRESTORE
9181558Srgrimes	if (host)
9191558Srgrimes		i = rmtread(tapebuf, ntrec * TP_BSIZE);
9201558Srgrimes	else
9211558Srgrimes#endif
9221558Srgrimes		i = read(mt, tapebuf, ntrec * TP_BSIZE);
9231558Srgrimes
9241558Srgrimes	if (i <= 0) {
9251558Srgrimes		fprintf(stderr, "tape read error: %s\n", strerror(errno));
9261558Srgrimes		done(1);
9271558Srgrimes	}
9281558Srgrimes	if (i % TP_BSIZE != 0) {
9291558Srgrimes		fprintf(stderr, "Tape block size (%d) %s (%d)\n",
9301558Srgrimes			i, "is not a multiple of dump block size", TP_BSIZE);
9311558Srgrimes		done(1);
9321558Srgrimes	}
9331558Srgrimes	ntrec = i / TP_BSIZE;
9341558Srgrimes	numtrec = ntrec;
9351558Srgrimes	vprintf(stdout, "Tape block size is %d\n", ntrec);
9361558Srgrimes}
9371558Srgrimes
9381558Srgrimesvoid
9391558Srgrimesclosemt()
9401558Srgrimes{
9411558Srgrimes
9421558Srgrimes	if (mt < 0)
9431558Srgrimes		return;
9441558Srgrimes#ifdef RRESTORE
9451558Srgrimes	if (host)
9461558Srgrimes		rmtclose();
9471558Srgrimes	else
9481558Srgrimes#endif
9491558Srgrimes		(void) close(mt);
9501558Srgrimes}
9511558Srgrimes
9521558Srgrimes/*
9531558Srgrimes * Read the next block from the tape.
9541558Srgrimes * Check to see if it is one of several vintage headers.
9551558Srgrimes * If it is an old style header, convert it to a new style header.
9561558Srgrimes * If it is not any valid header, return an error.
9571558Srgrimes */
9581558Srgrimesstatic int
9591558Srgrimesgethead(buf)
9601558Srgrimes	struct s_spcl *buf;
9611558Srgrimes{
9621558Srgrimes	long i;
9631558Srgrimes	union {
9641558Srgrimes		quad_t	qval;
9651558Srgrimes		long	val[2];
9661558Srgrimes	} qcvt;
9671558Srgrimes	union u_ospcl {
9681558Srgrimes		char dummy[TP_BSIZE];
9691558Srgrimes		struct	s_ospcl {
9701558Srgrimes			long	c_type;
9711558Srgrimes			long	c_date;
9721558Srgrimes			long	c_ddate;
9731558Srgrimes			long	c_volume;
9741558Srgrimes			long	c_tapea;
9751558Srgrimes			u_short	c_inumber;
9761558Srgrimes			long	c_magic;
9771558Srgrimes			long	c_checksum;
9781558Srgrimes			struct odinode {
9791558Srgrimes				unsigned short odi_mode;
9801558Srgrimes				u_short	odi_nlink;
9811558Srgrimes				u_short	odi_uid;
9821558Srgrimes				u_short	odi_gid;
9831558Srgrimes				long	odi_size;
9841558Srgrimes				long	odi_rdev;
9851558Srgrimes				char	odi_addr[36];
9861558Srgrimes				long	odi_atime;
9871558Srgrimes				long	odi_mtime;
9881558Srgrimes				long	odi_ctime;
9891558Srgrimes			} c_dinode;
9901558Srgrimes			long	c_count;
9911558Srgrimes			char	c_addr[256];
9921558Srgrimes		} s_ospcl;
9931558Srgrimes	} u_ospcl;
9941558Srgrimes
9951558Srgrimes	if (!cvtflag) {
9961558Srgrimes		readtape((char *)buf);
9971558Srgrimes		if (buf->c_magic != NFS_MAGIC) {
9981558Srgrimes			if (swabl(buf->c_magic) != NFS_MAGIC)
9991558Srgrimes				return (FAIL);
10001558Srgrimes			if (!Bcvt) {
10011558Srgrimes				vprintf(stdout, "Note: Doing Byte swapping\n");
10021558Srgrimes				Bcvt = 1;
10031558Srgrimes			}
10041558Srgrimes		}
10051558Srgrimes		if (checksum((int *)buf) == FAIL)
10061558Srgrimes			return (FAIL);
10071558Srgrimes		if (Bcvt)
10081558Srgrimes			swabst((u_char *)"8l4s31l", (u_char *)buf);
10091558Srgrimes		goto good;
10101558Srgrimes	}
10111558Srgrimes	readtape((char *)(&u_ospcl.s_ospcl));
10121558Srgrimes	bzero((char *)buf, (long)TP_BSIZE);
10131558Srgrimes	buf->c_type = u_ospcl.s_ospcl.c_type;
10141558Srgrimes	buf->c_date = u_ospcl.s_ospcl.c_date;
10151558Srgrimes	buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
10161558Srgrimes	buf->c_volume = u_ospcl.s_ospcl.c_volume;
10171558Srgrimes	buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
10181558Srgrimes	buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
10191558Srgrimes	buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
10201558Srgrimes	buf->c_magic = u_ospcl.s_ospcl.c_magic;
10211558Srgrimes	buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
10221558Srgrimes	buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
10231558Srgrimes	buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
10241558Srgrimes	buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
10251558Srgrimes	buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
10261558Srgrimes	buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
10271558Srgrimes	buf->c_dinode.di_atime.ts_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
10281558Srgrimes	buf->c_dinode.di_mtime.ts_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
10291558Srgrimes	buf->c_dinode.di_ctime.ts_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
10301558Srgrimes	buf->c_count = u_ospcl.s_ospcl.c_count;
10311558Srgrimes	bcopy(u_ospcl.s_ospcl.c_addr, buf->c_addr, (long)256);
10321558Srgrimes	if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
10331558Srgrimes	    checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
10341558Srgrimes		return(FAIL);
10351558Srgrimes	buf->c_magic = NFS_MAGIC;
10361558Srgrimes
10371558Srgrimesgood:
10381558Srgrimes	if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
10391558Srgrimes	    (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
10401558Srgrimes		qcvt.qval = buf->c_dinode.di_size;
10411558Srgrimes		if (qcvt.val[0] || qcvt.val[1]) {
10421558Srgrimes			printf("Note: Doing Quad swapping\n");
10431558Srgrimes			Qcvt = 1;
10441558Srgrimes		}
10451558Srgrimes	}
10461558Srgrimes	if (Qcvt) {
10471558Srgrimes		qcvt.qval = buf->c_dinode.di_size;
10481558Srgrimes		i = qcvt.val[1];
10491558Srgrimes		qcvt.val[1] = qcvt.val[0];
10501558Srgrimes		qcvt.val[0] = i;
10511558Srgrimes		buf->c_dinode.di_size = qcvt.qval;
10521558Srgrimes	}
10531558Srgrimes
10541558Srgrimes	switch (buf->c_type) {
10551558Srgrimes
10561558Srgrimes	case TS_CLRI:
10571558Srgrimes	case TS_BITS:
10581558Srgrimes		/*
10591558Srgrimes		 * Have to patch up missing information in bit map headers
10601558Srgrimes		 */
10611558Srgrimes		buf->c_inumber = 0;
10621558Srgrimes		buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
10631558Srgrimes		for (i = 0; i < buf->c_count; i++)
10641558Srgrimes			buf->c_addr[i]++;
10651558Srgrimes		break;
10661558Srgrimes
10671558Srgrimes	case TS_TAPE:
10681558Srgrimes		if ((buf->c_flags & DR_NEWINODEFMT) == 0)
10691558Srgrimes			oldinofmt = 1;
10701558Srgrimes		/* fall through */
10711558Srgrimes	case TS_END:
10721558Srgrimes		buf->c_inumber = 0;
10731558Srgrimes		break;
10741558Srgrimes
10751558Srgrimes	case TS_INODE:
10761558Srgrimes	case TS_ADDR:
10771558Srgrimes		break;
10781558Srgrimes
10791558Srgrimes	default:
10801558Srgrimes		panic("gethead: unknown inode type %d\n", buf->c_type);
10811558Srgrimes		break;
10821558Srgrimes	}
10831558Srgrimes	/*
10841558Srgrimes	 * If we are restoring a filesystem with old format inodes,
10851558Srgrimes	 * copy the uid/gid to the new location.
10861558Srgrimes	 */
10871558Srgrimes	if (oldinofmt) {
10881558Srgrimes		buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
10891558Srgrimes		buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
10901558Srgrimes	}
10911558Srgrimes	if (dflag)
10921558Srgrimes		accthdr(buf);
10931558Srgrimes	return(GOOD);
10941558Srgrimes}
10951558Srgrimes
10961558Srgrimes/*
10971558Srgrimes * Check that a header is where it belongs and predict the next header
10981558Srgrimes */
10991558Srgrimesstatic void
11001558Srgrimesaccthdr(header)
11011558Srgrimes	struct s_spcl *header;
11021558Srgrimes{
11031558Srgrimes	static ino_t previno = 0x7fffffff;
11041558Srgrimes	static int prevtype;
11051558Srgrimes	static long predict;
11061558Srgrimes	long blks, i;
11071558Srgrimes
11081558Srgrimes	if (header->c_type == TS_TAPE) {
11091558Srgrimes		fprintf(stderr, "Volume header (%s inode format) ",
11101558Srgrimes		    oldinofmt ? "old" : "new");
11111558Srgrimes 		if (header->c_firstrec)
11121558Srgrimes 			fprintf(stderr, "begins with record %d",
11131558Srgrimes 				header->c_firstrec);
11141558Srgrimes 		fprintf(stderr, "\n");
11151558Srgrimes		previno = 0x7fffffff;
11161558Srgrimes		return;
11171558Srgrimes	}
11181558Srgrimes	if (previno == 0x7fffffff)
11191558Srgrimes		goto newcalc;
11201558Srgrimes	switch (prevtype) {
11211558Srgrimes	case TS_BITS:
11221558Srgrimes		fprintf(stderr, "Dump mask header");
11231558Srgrimes		break;
11241558Srgrimes	case TS_CLRI:
11251558Srgrimes		fprintf(stderr, "Remove mask header");
11261558Srgrimes		break;
11271558Srgrimes	case TS_INODE:
11281558Srgrimes		fprintf(stderr, "File header, ino %d", previno);
11291558Srgrimes		break;
11301558Srgrimes	case TS_ADDR:
11311558Srgrimes		fprintf(stderr, "File continuation header, ino %d", previno);
11321558Srgrimes		break;
11331558Srgrimes	case TS_END:
11341558Srgrimes		fprintf(stderr, "End of tape header");
11351558Srgrimes		break;
11361558Srgrimes	}
11371558Srgrimes	if (predict != blksread - 1)
11381558Srgrimes		fprintf(stderr, "; predicted %d blocks, got %d blocks",
11391558Srgrimes			predict, blksread - 1);
11401558Srgrimes	fprintf(stderr, "\n");
11411558Srgrimesnewcalc:
11421558Srgrimes	blks = 0;
11431558Srgrimes	if (header->c_type != TS_END)
11441558Srgrimes		for (i = 0; i < header->c_count; i++)
11451558Srgrimes			if (header->c_addr[i] != 0)
11461558Srgrimes				blks++;
11471558Srgrimes	predict = blks;
11481558Srgrimes	blksread = 0;
11491558Srgrimes	prevtype = header->c_type;
11501558Srgrimes	previno = header->c_inumber;
11511558Srgrimes}
11521558Srgrimes
11531558Srgrimes/*
11541558Srgrimes * Find an inode header.
11551558Srgrimes * Complain if had to skip, and complain is set.
11561558Srgrimes */
11571558Srgrimesstatic void
11581558Srgrimesfindinode(header)
11591558Srgrimes	struct s_spcl *header;
11601558Srgrimes{
11611558Srgrimes	static long skipcnt = 0;
11621558Srgrimes	long i;
11631558Srgrimes	char buf[TP_BSIZE];
11641558Srgrimes
11651558Srgrimes	curfile.name = "<name unknown>";
11661558Srgrimes	curfile.action = UNKNOWN;
11671558Srgrimes	curfile.dip = NULL;
11681558Srgrimes	curfile.ino = 0;
11691558Srgrimes	do {
11701558Srgrimes		if (header->c_magic != NFS_MAGIC) {
11711558Srgrimes			skipcnt++;
11721558Srgrimes			while (gethead(header) == FAIL ||
11731558Srgrimes			    header->c_date != dumpdate)
11741558Srgrimes				skipcnt++;
11751558Srgrimes		}
11761558Srgrimes		switch (header->c_type) {
11771558Srgrimes
11781558Srgrimes		case TS_ADDR:
11791558Srgrimes			/*
11801558Srgrimes			 * Skip up to the beginning of the next record
11811558Srgrimes			 */
11821558Srgrimes			for (i = 0; i < header->c_count; i++)
11831558Srgrimes				if (header->c_addr[i])
11841558Srgrimes					readtape(buf);
11851558Srgrimes			while (gethead(header) == FAIL ||
11861558Srgrimes			    header->c_date != dumpdate)
11871558Srgrimes				skipcnt++;
11881558Srgrimes			break;
11891558Srgrimes
11901558Srgrimes		case TS_INODE:
11911558Srgrimes			curfile.dip = &header->c_dinode;
11921558Srgrimes			curfile.ino = header->c_inumber;
11931558Srgrimes			break;
11941558Srgrimes
11951558Srgrimes		case TS_END:
11961558Srgrimes			curfile.ino = maxino;
11971558Srgrimes			break;
11981558Srgrimes
11991558Srgrimes		case TS_CLRI:
12001558Srgrimes			curfile.name = "<file removal list>";
12011558Srgrimes			break;
12021558Srgrimes
12031558Srgrimes		case TS_BITS:
12041558Srgrimes			curfile.name = "<file dump list>";
12051558Srgrimes			break;
12061558Srgrimes
12071558Srgrimes		case TS_TAPE:
12081558Srgrimes			panic("unexpected tape header\n");
12091558Srgrimes			/* NOTREACHED */
12101558Srgrimes
12111558Srgrimes		default:
12121558Srgrimes			panic("unknown tape header type %d\n", spcl.c_type);
12131558Srgrimes			/* NOTREACHED */
12141558Srgrimes
12151558Srgrimes		}
12161558Srgrimes	} while (header->c_type == TS_ADDR);
12171558Srgrimes	if (skipcnt > 0)
12181558Srgrimes		fprintf(stderr, "resync restore, skipped %d blocks\n", skipcnt);
12191558Srgrimes	skipcnt = 0;
12201558Srgrimes}
12211558Srgrimes
12221558Srgrimesstatic int
12231558Srgrimeschecksum(buf)
12241558Srgrimes	register int *buf;
12251558Srgrimes{
12261558Srgrimes	register int i, j;
12271558Srgrimes
12281558Srgrimes	j = sizeof(union u_spcl) / sizeof(int);
12291558Srgrimes	i = 0;
12301558Srgrimes	if(!Bcvt) {
12311558Srgrimes		do
12321558Srgrimes			i += *buf++;
12331558Srgrimes		while (--j);
12341558Srgrimes	} else {
12351558Srgrimes		/* What happens if we want to read restore tapes
12361558Srgrimes			for a 16bit int machine??? */
12371558Srgrimes		do
12381558Srgrimes			i += swabl(*buf++);
12391558Srgrimes		while (--j);
12401558Srgrimes	}
12411558Srgrimes
12421558Srgrimes	if (i != CHECKSUM) {
12431558Srgrimes		fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
12441558Srgrimes			curfile.ino, curfile.name);
12451558Srgrimes		return(FAIL);
12461558Srgrimes	}
12471558Srgrimes	return(GOOD);
12481558Srgrimes}
12491558Srgrimes
12501558Srgrimes#ifdef RRESTORE
12511558Srgrimes#if __STDC__
12521558Srgrimes#include <stdarg.h>
12531558Srgrimes#else
12541558Srgrimes#include <varargs.h>
12551558Srgrimes#endif
12561558Srgrimes
12571558Srgrimesvoid
12581558Srgrimes#if __STDC__
12591558Srgrimesmsg(const char *fmt, ...)
12601558Srgrimes#else
12611558Srgrimesmsg(fmt, va_alist)
12621558Srgrimes	char *fmt;
12631558Srgrimes	va_dcl
12641558Srgrimes#endif
12651558Srgrimes{
12661558Srgrimes	va_list ap;
12671558Srgrimes#if __STDC__
12681558Srgrimes	va_start(ap, fmt);
12691558Srgrimes#else
12701558Srgrimes	va_start(ap);
12711558Srgrimes#endif
12721558Srgrimes	(void)vfprintf(stderr, fmt, ap);
12731558Srgrimes	va_end(ap);
12741558Srgrimes}
12751558Srgrimes#endif /* RRESTORE */
12761558Srgrimes
12771558Srgrimesstatic u_char *
12781558Srgrimesswabshort(sp, n)
12791558Srgrimes	register u_char *sp;
12801558Srgrimes	register int n;
12811558Srgrimes{
12821558Srgrimes	char c;
12831558Srgrimes
12841558Srgrimes	while (--n >= 0) {
12851558Srgrimes		c = sp[0]; sp[0] = sp[1]; sp[1] = c;
12861558Srgrimes		sp += 2;
12871558Srgrimes	}
12881558Srgrimes	return (sp);
12891558Srgrimes}
12901558Srgrimes
12911558Srgrimesstatic u_char *
12921558Srgrimesswablong(sp, n)
12931558Srgrimes	register u_char *sp;
12941558Srgrimes	register int n;
12951558Srgrimes{
12961558Srgrimes	char c;
12971558Srgrimes
12981558Srgrimes	while (--n >= 0) {
12991558Srgrimes		c = sp[0]; sp[0] = sp[3]; sp[3] = c;
13001558Srgrimes		c = sp[2]; sp[2] = sp[1]; sp[1] = c;
13011558Srgrimes		sp += 4;
13021558Srgrimes	}
13031558Srgrimes	return (sp);
13041558Srgrimes}
13051558Srgrimes
13061558Srgrimesvoid
13071558Srgrimesswabst(cp, sp)
13081558Srgrimes	register u_char *cp, *sp;
13091558Srgrimes{
13101558Srgrimes	int n = 0;
13111558Srgrimes
13121558Srgrimes	while (*cp) {
13131558Srgrimes		switch (*cp) {
13141558Srgrimes		case '0': case '1': case '2': case '3': case '4':
13151558Srgrimes		case '5': case '6': case '7': case '8': case '9':
13161558Srgrimes			n = (n * 10) + (*cp++ - '0');
13171558Srgrimes			continue;
13181558Srgrimes
13191558Srgrimes		case 's': case 'w': case 'h':
13201558Srgrimes			if (n == 0)
13211558Srgrimes				n = 1;
13221558Srgrimes			sp = swabshort(sp, n);
13231558Srgrimes			break;
13241558Srgrimes
13251558Srgrimes		case 'l':
13261558Srgrimes			if (n == 0)
13271558Srgrimes				n = 1;
13281558Srgrimes			sp = swablong(sp, n);
13291558Srgrimes			break;
13301558Srgrimes
13311558Srgrimes		default: /* Any other character, like 'b' counts as byte. */
13321558Srgrimes			if (n == 0)
13331558Srgrimes				n = 1;
13341558Srgrimes			sp += n;
13351558Srgrimes			break;
13361558Srgrimes		}
13371558Srgrimes		cp++;
13381558Srgrimes		n = 0;
13391558Srgrimes	}
13401558Srgrimes}
13411558Srgrimes
13421558Srgrimesstatic u_long
13431558Srgrimesswabl(x)
13441558Srgrimes	u_long x;
13451558Srgrimes{
13461558Srgrimes	swabst((u_char *)"l", (u_char *)&x);
13471558Srgrimes	return (x);
13481558Srgrimes}
1349