tape.c revision 92837
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
4037906Scharnier#if 0
4123685Speterstatic char sccsid[] = "@(#)tape.c	8.9 (Berkeley) 5/1/95";
4237906Scharnier#endif
4337906Scharnierstatic const char rcsid[] =
4450476Speter  "$FreeBSD: head/sbin/restore/tape.c 92837 2002-03-20 22:49:40Z imp $";
451558Srgrimes#endif /* not lint */
461558Srgrimes
471558Srgrimes#include <sys/param.h>
481558Srgrimes#include <sys/file.h>
491558Srgrimes#include <sys/mtio.h>
501558Srgrimes#include <sys/stat.h>
5166907Swollman#include <sys/time.h>
521558Srgrimes
531558Srgrimes#include <ufs/ufs/dinode.h>
541558Srgrimes#include <protocols/dumprestore.h>
551558Srgrimes
561558Srgrimes#include <errno.h>
5773986Sobrien#include <paths.h>
581558Srgrimes#include <setjmp.h>
591558Srgrimes#include <stdio.h>
601558Srgrimes#include <stdlib.h>
611558Srgrimes#include <string.h>
6266907Swollman#include <time.h>
631558Srgrimes#include <unistd.h>
641558Srgrimes
651558Srgrimes#include "restore.h"
661558Srgrimes#include "extern.h"
671558Srgrimes
681558Srgrimesstatic long	fssize = MAXBSIZE;
691558Srgrimesstatic int	mt = -1;
701558Srgrimesstatic int	pipein = 0;
7121174Sguidostatic char	*magtape;
721558Srgrimesstatic int	blkcnt;
731558Srgrimesstatic int	numtrec;
741558Srgrimesstatic char	*tapebuf;
751558Srgrimesstatic union	u_spcl endoftapemark;
761558Srgrimesstatic long	blksread;		/* blocks read since last header */
7790827Siedowsestatic long	tapeaddr = 0;		/* current TP_BSIZE tape record */
781558Srgrimesstatic long	tapesread;
791558Srgrimesstatic jmp_buf	restart;
801558Srgrimesstatic int	gettingfile = 0;	/* restart has a valid frame */
811558Srgrimesstatic char	*host = NULL;
821558Srgrimes
831558Srgrimesstatic int	ofile;
841558Srgrimesstatic char	*map;
851558Srgrimesstatic char	lnkbuf[MAXPATHLEN + 1];
861558Srgrimesstatic int	pathlen;
871558Srgrimes
881558Srgrimesint		oldinofmt;	/* old inode format conversion required */
891558Srgrimesint		Bcvt;		/* Swap Bytes (for CCI or sun) */
901558Srgrimesstatic int	Qcvt;		/* Swap quads (for sun) */
911558Srgrimes
921558Srgrimes#define	FLUSHTAPEBUF()	blkcnt = ntrec + 1
931558Srgrimes
9492837Simpstatic void	 accthdr(struct s_spcl *);
9592837Simpstatic int	 checksum(int *);
9692837Simpstatic void	 findinode(struct s_spcl *);
9792837Simpstatic void	 findtapeblksize(void);
9892837Simpstatic int	 gethead(struct s_spcl *);
9992837Simpstatic void	 readtape(char *);
10092837Simpstatic void	 setdumpnum(void);
10192837Simpstatic u_long	 swabl(u_long);
10292837Simpstatic u_char	*swablong(u_char *, int);
10392837Simpstatic u_char	*swabshort(u_char *, int);
10492837Simpstatic void	 terminateinput(void);
10592837Simpstatic void	 xtrfile(char *, long);
10692837Simpstatic void	 xtrlnkfile(char *, long);
10792837Simpstatic void	 xtrlnkskip(char *, long);
10892837Simpstatic void	 xtrmap(char *, long);
10992837Simpstatic void	 xtrmapskip(char *, long);
11092837Simpstatic void	 xtrskip(char *, long);
1111558Srgrimes
11237923Simpstatic int readmapflag;
11337923Simp
1141558Srgrimes/*
1151558Srgrimes * Set up an input source
1161558Srgrimes */
1171558Srgrimesvoid
11892837Simpsetinput(char *source)
1191558Srgrimes{
1201558Srgrimes	FLUSHTAPEBUF();
1211558Srgrimes	if (bflag)
1221558Srgrimes		newtapebuf(ntrec);
1231558Srgrimes	else
1241558Srgrimes		newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
1251558Srgrimes	terminal = stdin;
1261558Srgrimes
1271558Srgrimes#ifdef RRESTORE
12823685Speter	if (strchr(source, ':')) {
1291558Srgrimes		host = source;
13023685Speter		source = strchr(host, ':');
1311558Srgrimes		*source++ = '\0';
1321558Srgrimes		if (rmthost(host) == 0)
1331558Srgrimes			done(1);
1341558Srgrimes	} else
1351558Srgrimes#endif
1361558Srgrimes	if (strcmp(source, "-") == 0) {
1371558Srgrimes		/*
1381558Srgrimes		 * Since input is coming from a pipe we must establish
1391558Srgrimes		 * our own connection to the terminal.
1401558Srgrimes		 */
1411558Srgrimes		terminal = fopen(_PATH_TTY, "r");
1421558Srgrimes		if (terminal == NULL) {
1431558Srgrimes			(void)fprintf(stderr, "cannot open %s: %s\n",
1441558Srgrimes			    _PATH_TTY, strerror(errno));
1451558Srgrimes			terminal = fopen(_PATH_DEVNULL, "r");
1461558Srgrimes			if (terminal == NULL) {
1471558Srgrimes				(void)fprintf(stderr, "cannot open %s: %s\n",
1481558Srgrimes				    _PATH_DEVNULL, strerror(errno));
1491558Srgrimes				done(1);
1501558Srgrimes			}
1511558Srgrimes		}
1521558Srgrimes		pipein++;
1531558Srgrimes	}
1541558Srgrimes	setuid(getuid());	/* no longer need or want root privileges */
15521174Sguido	magtape = strdup(source);
15621174Sguido	if (magtape == NULL) {
15721174Sguido		fprintf(stderr, "Cannot allocate space for magtape buffer\n");
15821174Sguido		done(1);
15921174Sguido	}
1601558Srgrimes}
1611558Srgrimes
1621558Srgrimesvoid
16392837Simpnewtapebuf(long size)
1641558Srgrimes{
16592837Simp	static int tapebufsize = -1;
1661558Srgrimes
1671558Srgrimes	ntrec = size;
1681558Srgrimes	if (size <= tapebufsize)
1691558Srgrimes		return;
1701558Srgrimes	if (tapebuf != NULL)
1711558Srgrimes		free(tapebuf);
1721558Srgrimes	tapebuf = malloc(size * TP_BSIZE);
1731558Srgrimes	if (tapebuf == NULL) {
1741558Srgrimes		fprintf(stderr, "Cannot allocate space for tape buffer\n");
1751558Srgrimes		done(1);
1761558Srgrimes	}
1771558Srgrimes	tapebufsize = size;
1781558Srgrimes}
1791558Srgrimes
1801558Srgrimes/*
1811558Srgrimes * Verify that the tape drive can be accessed and
1821558Srgrimes * that it actually is a dump tape.
1831558Srgrimes */
1841558Srgrimesvoid
18592837Simpsetup(void)
1861558Srgrimes{
1871558Srgrimes	int i, j, *ip;
1881558Srgrimes	struct stat stbuf;
1891558Srgrimes
1901558Srgrimes	vprintf(stdout, "Verify tape and initialize maps\n");
1911558Srgrimes#ifdef RRESTORE
1921558Srgrimes	if (host)
1931558Srgrimes		mt = rmtopen(magtape, 0);
1941558Srgrimes	else
1951558Srgrimes#endif
1961558Srgrimes	if (pipein)
1971558Srgrimes		mt = 0;
1981558Srgrimes	else
1991558Srgrimes		mt = open(magtape, O_RDONLY, 0);
2001558Srgrimes	if (mt < 0) {
2011558Srgrimes		fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
2021558Srgrimes		done(1);
2031558Srgrimes	}
2041558Srgrimes	volno = 1;
2051558Srgrimes	setdumpnum();
2061558Srgrimes	FLUSHTAPEBUF();
2071558Srgrimes	if (!pipein && !bflag)
2081558Srgrimes		findtapeblksize();
2091558Srgrimes	if (gethead(&spcl) == FAIL) {
2101558Srgrimes		blkcnt--; /* push back this block */
2111558Srgrimes		blksread--;
2121558Srgrimes		cvtflag++;
2131558Srgrimes		if (gethead(&spcl) == FAIL) {
2141558Srgrimes			fprintf(stderr, "Tape is not a dump tape\n");
2151558Srgrimes			done(1);
2161558Srgrimes		}
2171558Srgrimes		fprintf(stderr, "Converting to new file system format.\n");
2181558Srgrimes	}
2191558Srgrimes	if (pipein) {
2201558Srgrimes		endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
2211558Srgrimes		endoftapemark.s_spcl.c_type = TS_END;
2221558Srgrimes		ip = (int *)&endoftapemark;
2231558Srgrimes		j = sizeof(union u_spcl) / sizeof(int);
2241558Srgrimes		i = 0;
2251558Srgrimes		do
2261558Srgrimes			i += *ip++;
2271558Srgrimes		while (--j);
2281558Srgrimes		endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
2291558Srgrimes	}
2301558Srgrimes	if (vflag || command == 't')
2311558Srgrimes		printdumpinfo();
23289572Sdillon	dumptime = _time32_to_time(spcl.c_ddate);
23389572Sdillon	dumpdate = _time32_to_time(spcl.c_date);
2341558Srgrimes	if (stat(".", &stbuf) < 0) {
2351558Srgrimes		fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
2361558Srgrimes		done(1);
2371558Srgrimes	}
23834851Sjkh	if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
23934851Sjkh		fssize = TP_BSIZE;
24034851Sjkh	if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
2411558Srgrimes		fssize = stbuf.st_blksize;
2421558Srgrimes	if (((fssize - 1) & fssize) != 0) {
24337240Sbde		fprintf(stderr, "bad block size %ld\n", fssize);
2441558Srgrimes		done(1);
2451558Srgrimes	}
2461558Srgrimes	if (spcl.c_volume != 1) {
2471558Srgrimes		fprintf(stderr, "Tape is not volume 1 of the dump\n");
2481558Srgrimes		done(1);
2491558Srgrimes	}
2501558Srgrimes	if (gethead(&spcl) == FAIL) {
25137240Sbde		dprintf(stdout, "header read failed at %ld blocks\n", blksread);
2521558Srgrimes		panic("no header after volume mark!\n");
2531558Srgrimes	}
2541558Srgrimes	findinode(&spcl);
2551558Srgrimes	if (spcl.c_type != TS_CLRI) {
2561558Srgrimes		fprintf(stderr, "Cannot find file removal list\n");
2571558Srgrimes		done(1);
2581558Srgrimes	}
2591558Srgrimes	maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
2601558Srgrimes	dprintf(stdout, "maxino = %d\n", maxino);
2611558Srgrimes	map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
2621558Srgrimes	if (map == NULL)
26323685Speter		panic("no memory for active inode map\n");
26423685Speter	usedinomap = map;
2651558Srgrimes	curfile.action = USING;
2661558Srgrimes	getfile(xtrmap, xtrmapskip);
2671558Srgrimes	if (spcl.c_type != TS_BITS) {
2681558Srgrimes		fprintf(stderr, "Cannot find file dump list\n");
2691558Srgrimes		done(1);
2701558Srgrimes	}
2711558Srgrimes	map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
2721558Srgrimes	if (map == (char *)NULL)
2731558Srgrimes		panic("no memory for file dump list\n");
2741558Srgrimes	dumpmap = map;
2751558Srgrimes	curfile.action = USING;
2761558Srgrimes	getfile(xtrmap, xtrmapskip);
27723685Speter	/*
27823685Speter	 * If there may be whiteout entries on the tape, pretend that the
27923685Speter	 * whiteout inode exists, so that the whiteout entries can be
28023685Speter	 * extracted.
28123685Speter	 */
28223685Speter	if (oldinofmt == 0)
28323685Speter		SETINO(WINO, dumpmap);
28490820Siedowse	/* 'r' restores don't call getvol() for tape 1, so mark it as read. */
28590820Siedowse	if (command == 'r')
28690820Siedowse		tapesread = 1;
2871558Srgrimes}
2881558Srgrimes
2891558Srgrimes/*
2901558Srgrimes * Prompt user to load a new dump volume.
2911558Srgrimes * "Nextvol" is the next suggested volume to use.
2921558Srgrimes * This suggested volume is enforced when doing full
29337906Scharnier * or incremental restores, but can be overridden by
2941558Srgrimes * the user when only extracting a subset of the files.
2951558Srgrimes */
2961558Srgrimesvoid
29792837Simpgetvol(long nextvol)
2981558Srgrimes{
29990827Siedowse	long newvol, prevtapea, savecnt, i;
3001558Srgrimes	union u_spcl tmpspcl;
3011558Srgrimes#	define tmpbuf tmpspcl.s_spcl
3021558Srgrimes	char buf[TP_BSIZE];
3031558Srgrimes
3041558Srgrimes	if (nextvol == 1) {
3051558Srgrimes		tapesread = 0;
3061558Srgrimes		gettingfile = 0;
3071558Srgrimes	}
30890827Siedowse	prevtapea = tapeaddr;
30990827Siedowse	savecnt = blksread;
3101558Srgrimes	if (pipein) {
31169906Siedowse		if (nextvol != 1) {
3121558Srgrimes			panic("Changing volumes on pipe input?\n");
31369906Siedowse			/* Avoid looping if we couldn't ask the user. */
31469906Siedowse			if (yflag || ferror(terminal) || feof(terminal))
31569906Siedowse				done(1);
31669906Siedowse		}
3171558Srgrimes		if (volno == 1)
3181558Srgrimes			return;
3191558Srgrimes		goto gethdr;
3201558Srgrimes	}
3211558Srgrimesagain:
3221558Srgrimes	if (pipein)
3231558Srgrimes		done(1); /* pipes do not get a second chance */
32490608Siedowse	if (command == 'R' || command == 'r' || curfile.action != SKIP)
3251558Srgrimes		newvol = nextvol;
32690608Siedowse	else
3271558Srgrimes		newvol = 0;
3281558Srgrimes	while (newvol <= 0) {
3291558Srgrimes		if (tapesread == 0) {
33090820Siedowse			fprintf(stderr, "%s%s%s%s%s%s%s",
3311558Srgrimes			    "You have not read any tapes yet.\n",
33290820Siedowse			    "If you are extracting just a few files,",
33390820Siedowse			    " start with the last volume\n",
33490820Siedowse			    "and work towards the first; restore",
33590820Siedowse			    " can quickly skip tapes that\n",
33690820Siedowse			    "have no further files to extract.",
33790820Siedowse			    " Otherwise, begin with volume 1.\n");
3381558Srgrimes		} else {
3391558Srgrimes			fprintf(stderr, "You have read volumes");
3401558Srgrimes			strcpy(buf, ": ");
34190820Siedowse			for (i = 0; i < 32; i++)
3421558Srgrimes				if (tapesread & (1 << i)) {
34390820Siedowse					fprintf(stderr, "%s%ld", buf, i + 1);
3441558Srgrimes					strcpy(buf, ", ");
3451558Srgrimes				}
3461558Srgrimes			fprintf(stderr, "\n");
3471558Srgrimes		}
3481558Srgrimes		do	{
3491558Srgrimes			fprintf(stderr, "Specify next volume #: ");
3501558Srgrimes			(void) fflush(stderr);
35169906Siedowse			if (fgets(buf, BUFSIZ, terminal) == NULL)
35269906Siedowse				done(1);
35369906Siedowse		} while (buf[0] == '\n');
3541558Srgrimes		newvol = atoi(buf);
3551558Srgrimes		if (newvol <= 0) {
3561558Srgrimes			fprintf(stderr,
3571558Srgrimes			    "Volume numbers are positive numerics\n");
3581558Srgrimes		}
3591558Srgrimes	}
3601558Srgrimes	if (newvol == volno) {
36190820Siedowse		tapesread |= 1 << (volno - 1);
3621558Srgrimes		return;
3631558Srgrimes	}
3641558Srgrimes	closemt();
36537240Sbde	fprintf(stderr, "Mount tape volume %ld\n", newvol);
3661558Srgrimes	fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
3671558Srgrimes	fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
3681558Srgrimes	(void) fflush(stderr);
36969906Siedowse	if (fgets(buf, BUFSIZ, terminal) == NULL)
3701558Srgrimes		done(1);
3711558Srgrimes	if (!strcmp(buf, "none\n")) {
3721558Srgrimes		terminateinput();
3731558Srgrimes		return;
3741558Srgrimes	}
3751558Srgrimes	if (buf[0] != '\n') {
3761558Srgrimes		(void) strcpy(magtape, buf);
3771558Srgrimes		magtape[strlen(magtape) - 1] = '\0';
3781558Srgrimes	}
3791558Srgrimes#ifdef RRESTORE
3801558Srgrimes	if (host)
3811558Srgrimes		mt = rmtopen(magtape, 0);
3821558Srgrimes	else
3831558Srgrimes#endif
3841558Srgrimes		mt = open(magtape, O_RDONLY, 0);
3851558Srgrimes
3861558Srgrimes	if (mt == -1) {
3871558Srgrimes		fprintf(stderr, "Cannot open %s\n", magtape);
3881558Srgrimes		volno = -1;
3891558Srgrimes		goto again;
3901558Srgrimes	}
3911558Srgrimesgethdr:
3921558Srgrimes	volno = newvol;
3931558Srgrimes	setdumpnum();
3941558Srgrimes	FLUSHTAPEBUF();
3951558Srgrimes	if (gethead(&tmpbuf) == FAIL) {
39637240Sbde		dprintf(stdout, "header read failed at %ld blocks\n", blksread);
3971558Srgrimes		fprintf(stderr, "tape is not dump tape\n");
3981558Srgrimes		volno = 0;
3991558Srgrimes		goto again;
4001558Srgrimes	}
4011558Srgrimes	if (tmpbuf.c_volume != volno) {
40237240Sbde		fprintf(stderr, "Wrong volume (%ld)\n", tmpbuf.c_volume);
4031558Srgrimes		volno = 0;
4041558Srgrimes		goto again;
4051558Srgrimes	}
40689572Sdillon	if (_time32_to_time(tmpbuf.c_date) != dumpdate ||
40789572Sdillon	    _time32_to_time(tmpbuf.c_ddate) != dumptime) {
40889572Sdillon		time_t t = _time32_to_time(tmpbuf.c_date);
40985635Sdillon		fprintf(stderr, "Wrong dump date\n\tgot: %s", ctime(&t));
4101558Srgrimes		fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
4111558Srgrimes		volno = 0;
4121558Srgrimes		goto again;
4131558Srgrimes	}
41490820Siedowse	tapesread |= 1 << (volno - 1);
4151558Srgrimes	blksread = savecnt;
4161558Srgrimes 	/*
4171558Srgrimes 	 * If continuing from the previous volume, skip over any
4181558Srgrimes 	 * blocks read already at the end of the previous volume.
4191558Srgrimes 	 *
4201558Srgrimes 	 * If coming to this volume at random, skip to the beginning
4211558Srgrimes 	 * of the next record.
4221558Srgrimes 	 */
42390827Siedowse	dprintf(stdout, "last rec %ld, tape starts with %ld\n", prevtapea,
42490827Siedowse	    tmpbuf.c_tapea);
4251558Srgrimes 	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
42690608Siedowse 		if (curfile.action != USING) {
42790608Siedowse			/*
42890608Siedowse			 * XXX Dump incorrectly sets c_count to 1 in the
42990608Siedowse			 * volume header of the first tape, so ignore
43090608Siedowse			 * c_count when volno == 1.
43190608Siedowse			 */
43290608Siedowse			if (volno != 1)
43390608Siedowse				for (i = tmpbuf.c_count; i > 0; i--)
43490608Siedowse					readtape(buf);
43590827Siedowse 		} else if (tmpbuf.c_tapea <= prevtapea) {
4361558Srgrimes			/*
43790827Siedowse			 * Normally the value of c_tapea in the volume
43890827Siedowse			 * header is the record number of the header itself.
43990827Siedowse			 * However in the volume header following an EOT-
44090827Siedowse			 * terminated tape, it is the record number of the
44190827Siedowse			 * first continuation data block (dump bug?).
44290827Siedowse			 *
44390827Siedowse			 * The next record we want is `prevtapea + 1'.
4441558Srgrimes			 */
44590827Siedowse 			i = prevtapea + 1 - tmpbuf.c_tapea;
44637240Sbde			dprintf(stderr, "Skipping %ld duplicate record%s.\n",
4471558Srgrimes				i, i > 1 ? "s" : "");
4481558Srgrimes 			while (--i >= 0)
4491558Srgrimes 				readtape(buf);
4501558Srgrimes 		}
4511558Srgrimes 	}
45290608Siedowse	if (curfile.action == USING) {
4531558Srgrimes		if (volno == 1)
4541558Srgrimes			panic("active file into volume 1\n");
4551558Srgrimes		return;
4561558Srgrimes	}
4571558Srgrimes	(void) gethead(&spcl);
4581558Srgrimes	findinode(&spcl);
4591558Srgrimes	if (gettingfile) {
4601558Srgrimes		gettingfile = 0;
4611558Srgrimes		longjmp(restart, 1);
4621558Srgrimes	}
4631558Srgrimes}
4641558Srgrimes
4651558Srgrimes/*
4661558Srgrimes * Handle unexpected EOF.
4671558Srgrimes */
4681558Srgrimesstatic void
46992837Simpterminateinput(void)
4701558Srgrimes{
4711558Srgrimes
4721558Srgrimes	if (gettingfile && curfile.action == USING) {
4731558Srgrimes		printf("Warning: %s %s\n",
4741558Srgrimes		    "End-of-input encountered while extracting", curfile.name);
4751558Srgrimes	}
4761558Srgrimes	curfile.name = "<name unknown>";
4771558Srgrimes	curfile.action = UNKNOWN;
4781558Srgrimes	curfile.dip = NULL;
4791558Srgrimes	curfile.ino = maxino;
4801558Srgrimes	if (gettingfile) {
4811558Srgrimes		gettingfile = 0;
4821558Srgrimes		longjmp(restart, 1);
4831558Srgrimes	}
4841558Srgrimes}
4851558Srgrimes
4861558Srgrimes/*
4871558Srgrimes * handle multiple dumps per tape by skipping forward to the
4881558Srgrimes * appropriate one.
4891558Srgrimes */
4901558Srgrimesstatic void
49192837Simpsetdumpnum(void)
4921558Srgrimes{
4931558Srgrimes	struct mtop tcom;
4941558Srgrimes
4951558Srgrimes	if (dumpnum == 1 || volno != 1)
4961558Srgrimes		return;
4971558Srgrimes	if (pipein) {
4981558Srgrimes		fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
4991558Srgrimes		done(1);
5001558Srgrimes	}
5011558Srgrimes	tcom.mt_op = MTFSF;
5021558Srgrimes	tcom.mt_count = dumpnum - 1;
5031558Srgrimes#ifdef RRESTORE
5041558Srgrimes	if (host)
5051558Srgrimes		rmtioctl(MTFSF, dumpnum - 1);
5068871Srgrimes	else
5071558Srgrimes#endif
50865786Smjacob		if (ioctl(mt, MTIOCTOP, (char *)&tcom) < 0)
5091558Srgrimes			fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
5101558Srgrimes}
5111558Srgrimes
5121558Srgrimesvoid
51392837Simpprintdumpinfo(void)
5141558Srgrimes{
51585635Sdillon	time_t t;
51689572Sdillon	t = _time32_to_time(spcl.c_date);
51785635Sdillon	fprintf(stdout, "Dump   date: %s", ctime(&t));
51889572Sdillon	t = _time32_to_time(spcl.c_ddate);
5191558Srgrimes	fprintf(stdout, "Dumped from: %s",
52085635Sdillon	    (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&t));
5211558Srgrimes	if (spcl.c_host[0] == '\0')
5221558Srgrimes		return;
52337240Sbde	fprintf(stderr, "Level %ld dump of %s on %s:%s\n",
5241558Srgrimes		spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
5251558Srgrimes	fprintf(stderr, "Label: %s\n", spcl.c_label);
5261558Srgrimes}
5271558Srgrimes
5281558Srgrimesint
52992837Simpextractfile(char *name)
5301558Srgrimes{
53123685Speter	int flags;
53223685Speter	mode_t mode;
5331558Srgrimes	struct timeval timep[2];
5341558Srgrimes	struct entry *ep;
5351558Srgrimes
5361558Srgrimes	curfile.name = name;
5371558Srgrimes	curfile.action = USING;
53823685Speter	timep[0].tv_sec = curfile.dip->di_atime;
53923685Speter	timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
54023685Speter	timep[1].tv_sec = curfile.dip->di_mtime;
54123685Speter	timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
5421558Srgrimes	mode = curfile.dip->di_mode;
54323685Speter	flags = curfile.dip->di_flags;
5441558Srgrimes	switch (mode & IFMT) {
5451558Srgrimes
5461558Srgrimes	default:
5471558Srgrimes		fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
5481558Srgrimes		skipfile();
5491558Srgrimes		return (FAIL);
5501558Srgrimes
5511558Srgrimes	case IFSOCK:
5521558Srgrimes		vprintf(stdout, "skipped socket %s\n", name);
5531558Srgrimes		skipfile();
5541558Srgrimes		return (GOOD);
5551558Srgrimes
5561558Srgrimes	case IFDIR:
5571558Srgrimes		if (mflag) {
5581558Srgrimes			ep = lookupname(name);
5591558Srgrimes			if (ep == NULL || ep->e_flags & EXTRACT)
5601558Srgrimes				panic("unextracted directory %s\n", name);
5611558Srgrimes			skipfile();
5621558Srgrimes			return (GOOD);
5631558Srgrimes		}
5641558Srgrimes		vprintf(stdout, "extract file %s\n", name);
5651558Srgrimes		return (genliteraldir(name, curfile.ino));
5661558Srgrimes
5671558Srgrimes	case IFLNK:
5681558Srgrimes		lnkbuf[0] = '\0';
5691558Srgrimes		pathlen = 0;
5701558Srgrimes		getfile(xtrlnkfile, xtrlnkskip);
5711558Srgrimes		if (pathlen == 0) {
5721558Srgrimes			vprintf(stdout,
5731558Srgrimes			    "%s: zero length symbolic link (ignored)\n", name);
5741558Srgrimes			return (GOOD);
5751558Srgrimes		}
5761558Srgrimes		return (linkit(lnkbuf, name, SYMLINK));
5771558Srgrimes
5786305Smartin	case IFIFO:
57923685Speter		vprintf(stdout, "extract fifo %s\n", name);
58023685Speter		if (Nflag) {
58123685Speter			skipfile();
58223685Speter			return (GOOD);
58323685Speter		}
58435852Sjkh		if (uflag && !Nflag)
58535852Sjkh			(void)unlink(name);
5866305Smartin		if (mkfifo(name, mode) < 0) {
58723685Speter			fprintf(stderr, "%s: cannot create fifo: %s\n",
58823685Speter			    name, strerror(errno));
5896305Smartin			skipfile();
5906305Smartin			return (FAIL);
5916305Smartin		}
5926305Smartin		(void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
5936305Smartin		(void) chmod(name, mode);
59463283Sdwmalone		utimes(name, timep);
59523685Speter		(void) chflags(name, flags);
5966305Smartin		skipfile();
5976305Smartin		return (GOOD);
5986305Smartin
5991558Srgrimes	case IFCHR:
6001558Srgrimes	case IFBLK:
6011558Srgrimes		vprintf(stdout, "extract special file %s\n", name);
6021558Srgrimes		if (Nflag) {
6031558Srgrimes			skipfile();
6041558Srgrimes			return (GOOD);
6051558Srgrimes		}
60635852Sjkh		if (uflag)
60735852Sjkh			(void)unlink(name);
6081558Srgrimes		if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
6091558Srgrimes			fprintf(stderr, "%s: cannot create special file: %s\n",
6101558Srgrimes			    name, strerror(errno));
6111558Srgrimes			skipfile();
6121558Srgrimes			return (FAIL);
6131558Srgrimes		}
6141558Srgrimes		(void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
6151558Srgrimes		(void) chmod(name, mode);
61663283Sdwmalone		utimes(name, timep);
61723685Speter		(void) chflags(name, flags);
6181558Srgrimes		skipfile();
6191558Srgrimes		return (GOOD);
6201558Srgrimes
6211558Srgrimes	case IFREG:
6221558Srgrimes		vprintf(stdout, "extract file %s\n", name);
6231558Srgrimes		if (Nflag) {
6241558Srgrimes			skipfile();
6251558Srgrimes			return (GOOD);
6261558Srgrimes		}
62735852Sjkh		if (uflag)
62835852Sjkh			(void)unlink(name);
62921149Simp		if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
63021149Simp		    0666)) < 0) {
6311558Srgrimes			fprintf(stderr, "%s: cannot create file: %s\n",
6321558Srgrimes			    name, strerror(errno));
6331558Srgrimes			skipfile();
6341558Srgrimes			return (FAIL);
6351558Srgrimes		}
6361558Srgrimes		(void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
6371558Srgrimes		(void) fchmod(ofile, mode);
6381558Srgrimes		getfile(xtrfile, xtrskip);
6391558Srgrimes		(void) close(ofile);
6401558Srgrimes		utimes(name, timep);
64163283Sdwmalone		(void) chflags(name, flags);
6421558Srgrimes		return (GOOD);
6431558Srgrimes	}
6441558Srgrimes	/* NOTREACHED */
6451558Srgrimes}
6461558Srgrimes
6471558Srgrimes/*
6481558Srgrimes * skip over bit maps on the tape
6491558Srgrimes */
6501558Srgrimesvoid
65192837Simpskipmaps(void)
6521558Srgrimes{
6531558Srgrimes
6541558Srgrimes	while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
6551558Srgrimes		skipfile();
6561558Srgrimes}
6571558Srgrimes
6581558Srgrimes/*
6591558Srgrimes * skip over a file on the tape
6601558Srgrimes */
6611558Srgrimesvoid
66292837Simpskipfile(void)
6631558Srgrimes{
6641558Srgrimes
6651558Srgrimes	curfile.action = SKIP;
6661558Srgrimes	getfile(xtrnull, xtrnull);
6671558Srgrimes}
6681558Srgrimes
6691558Srgrimes/*
6701558Srgrimes * Extract a file from the tape.
6711558Srgrimes * When an allocated block is found it is passed to the fill function;
6721558Srgrimes * when an unallocated block (hole) is found, a zeroed buffer is passed
6731558Srgrimes * to the skip function.
6741558Srgrimes */
6751558Srgrimesvoid
67692837Simpgetfile(void (*fill)(char *, long), void (*skip)(char *, long))
6771558Srgrimes{
67892806Sobrien	int i;
6791558Srgrimes	int curblk = 0;
68023685Speter	quad_t size = spcl.c_dinode.di_size;
6811558Srgrimes	static char clearedbuf[MAXBSIZE];
6821558Srgrimes	char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
6831558Srgrimes	char junk[TP_BSIZE];
6841558Srgrimes
6851558Srgrimes	if (spcl.c_type == TS_END)
6861558Srgrimes		panic("ran off end of tape\n");
6871558Srgrimes	if (spcl.c_magic != NFS_MAGIC)
6881558Srgrimes		panic("not at beginning of a file\n");
6891558Srgrimes	if (!gettingfile && setjmp(restart) != 0)
6901558Srgrimes		return;
6911558Srgrimes	gettingfile++;
6921558Srgrimesloop:
6931558Srgrimes	for (i = 0; i < spcl.c_count; i++) {
69437923Simp		if (readmapflag || spcl.c_addr[i]) {
6951558Srgrimes			readtape(&buf[curblk++][0]);
6961558Srgrimes			if (curblk == fssize / TP_BSIZE) {
69723685Speter				(*fill)((char *)buf, (long)(size > TP_BSIZE ?
69823685Speter				     fssize : (curblk - 1) * TP_BSIZE + size));
6991558Srgrimes				curblk = 0;
7001558Srgrimes			}
7011558Srgrimes		} else {
7021558Srgrimes			if (curblk > 0) {
70323685Speter				(*fill)((char *)buf, (long)(size > TP_BSIZE ?
70423685Speter				     curblk * TP_BSIZE :
70523685Speter				     (curblk - 1) * TP_BSIZE + size));
7061558Srgrimes				curblk = 0;
7071558Srgrimes			}
70823685Speter			(*skip)(clearedbuf, (long)(size > TP_BSIZE ?
70923685Speter				TP_BSIZE : size));
7101558Srgrimes		}
7111558Srgrimes		if ((size -= TP_BSIZE) <= 0) {
7121558Srgrimes			for (i++; i < spcl.c_count; i++)
71337923Simp				if (readmapflag || spcl.c_addr[i])
7141558Srgrimes					readtape(junk);
7151558Srgrimes			break;
7161558Srgrimes		}
7171558Srgrimes	}
7181558Srgrimes	if (gethead(&spcl) == GOOD && size > 0) {
7191558Srgrimes		if (spcl.c_type == TS_ADDR)
7201558Srgrimes			goto loop;
7211558Srgrimes		dprintf(stdout,
72237240Sbde			"Missing address (header) block for %s at %ld blocks\n",
7231558Srgrimes			curfile.name, blksread);
7241558Srgrimes	}
7251558Srgrimes	if (curblk > 0)
72623685Speter		(*fill)((char *)buf, (long)((curblk * TP_BSIZE) + size));
7271558Srgrimes	findinode(&spcl);
7281558Srgrimes	gettingfile = 0;
7291558Srgrimes}
7301558Srgrimes
7311558Srgrimes/*
7321558Srgrimes * Write out the next block of a file.
7331558Srgrimes */
7341558Srgrimesstatic void
73592837Simpxtrfile(char *buf, long	size)
7361558Srgrimes{
7371558Srgrimes
7381558Srgrimes	if (Nflag)
7391558Srgrimes		return;
7401558Srgrimes	if (write(ofile, buf, (int) size) == -1) {
7411558Srgrimes		fprintf(stderr,
7421558Srgrimes		    "write error extracting inode %d, name %s\nwrite: %s\n",
7431558Srgrimes			curfile.ino, curfile.name, strerror(errno));
7441558Srgrimes	}
7451558Srgrimes}
7461558Srgrimes
7471558Srgrimes/*
7481558Srgrimes * Skip over a hole in a file.
7491558Srgrimes */
7501558Srgrimes/* ARGSUSED */
7511558Srgrimesstatic void
75292837Simpxtrskip(char *buf, long size)
7531558Srgrimes{
7541558Srgrimes
7551558Srgrimes	if (lseek(ofile, size, SEEK_CUR) == -1) {
7561558Srgrimes		fprintf(stderr,
7571558Srgrimes		    "seek error extracting inode %d, name %s\nlseek: %s\n",
7581558Srgrimes			curfile.ino, curfile.name, strerror(errno));
7591558Srgrimes		done(1);
7601558Srgrimes	}
7611558Srgrimes}
7621558Srgrimes
7631558Srgrimes/*
7641558Srgrimes * Collect the next block of a symbolic link.
7651558Srgrimes */
7661558Srgrimesstatic void
76792837Simpxtrlnkfile(char *buf, long size)
7681558Srgrimes{
7691558Srgrimes
7701558Srgrimes	pathlen += size;
7711558Srgrimes	if (pathlen > MAXPATHLEN) {
7721558Srgrimes		fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
7731558Srgrimes		    curfile.name, lnkbuf, buf, pathlen);
7741558Srgrimes		done(1);
7751558Srgrimes	}
7761558Srgrimes	(void) strcat(lnkbuf, buf);
7771558Srgrimes}
7781558Srgrimes
7791558Srgrimes/*
7801558Srgrimes * Skip over a hole in a symbolic link (should never happen).
7811558Srgrimes */
7821558Srgrimes/* ARGSUSED */
7831558Srgrimesstatic void
78492837Simpxtrlnkskip(char *buf, long size)
7851558Srgrimes{
7861558Srgrimes
7871558Srgrimes	fprintf(stderr, "unallocated block in symbolic link %s\n",
7881558Srgrimes		curfile.name);
7891558Srgrimes	done(1);
7901558Srgrimes}
7911558Srgrimes
7921558Srgrimes/*
7931558Srgrimes * Collect the next block of a bit map.
7941558Srgrimes */
7951558Srgrimesstatic void
79692837Simpxtrmap(char *buf, long size)
7971558Srgrimes{
7981558Srgrimes
79923685Speter	memmove(map, buf, size);
8001558Srgrimes	map += size;
8011558Srgrimes}
8021558Srgrimes
8031558Srgrimes/*
8041558Srgrimes * Skip over a hole in a bit map (should never happen).
8051558Srgrimes */
8061558Srgrimes/* ARGSUSED */
8071558Srgrimesstatic void
80892837Simpxtrmapskip(char *buf, long size)
8091558Srgrimes{
8101558Srgrimes
8111558Srgrimes	panic("hole in map\n");
8121558Srgrimes	map += size;
8131558Srgrimes}
8141558Srgrimes
8151558Srgrimes/*
8161558Srgrimes * Noop, when an extraction function is not needed.
8171558Srgrimes */
8181558Srgrimes/* ARGSUSED */
8191558Srgrimesvoid
82092837Simpxtrnull(char *buf, long size)
8211558Srgrimes{
8221558Srgrimes
8231558Srgrimes	return;
8241558Srgrimes}
8251558Srgrimes
8261558Srgrimes/*
8271558Srgrimes * Read TP_BSIZE blocks from the input.
8281558Srgrimes * Handle read errors, and end of media.
8291558Srgrimes */
8301558Srgrimesstatic void
83192837Simpreadtape(char *buf)
8321558Srgrimes{
8331558Srgrimes	long rd, newvol, i;
8341558Srgrimes	int cnt, seek_failed;
8351558Srgrimes
8361558Srgrimes	if (blkcnt < numtrec) {
83723685Speter		memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
8381558Srgrimes		blksread++;
83990827Siedowse		tapeaddr++;
8401558Srgrimes		return;
8411558Srgrimes	}
8421558Srgrimes	for (i = 0; i < ntrec; i++)
8431558Srgrimes		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
8441558Srgrimes	if (numtrec == 0)
8451558Srgrimes		numtrec = ntrec;
8461558Srgrimes	cnt = ntrec * TP_BSIZE;
8471558Srgrimes	rd = 0;
8481558Srgrimesgetmore:
8491558Srgrimes#ifdef RRESTORE
8501558Srgrimes	if (host)
8511558Srgrimes		i = rmtread(&tapebuf[rd], cnt);
8521558Srgrimes	else
8531558Srgrimes#endif
8541558Srgrimes		i = read(mt, &tapebuf[rd], cnt);
8551558Srgrimes	/*
8561558Srgrimes	 * Check for mid-tape short read error.
8571558Srgrimes	 * If found, skip rest of buffer and start with the next.
8581558Srgrimes	 */
8591558Srgrimes	if (!pipein && numtrec < ntrec && i > 0) {
8601558Srgrimes		dprintf(stdout, "mid-media short read error.\n");
8611558Srgrimes		numtrec = ntrec;
8621558Srgrimes	}
8631558Srgrimes	/*
8641558Srgrimes	 * Handle partial block read.
8651558Srgrimes	 */
8661558Srgrimes	if (pipein && i == 0 && rd > 0)
8671558Srgrimes		i = rd;
8681558Srgrimes	else if (i > 0 && i != ntrec * TP_BSIZE) {
8691558Srgrimes		if (pipein) {
8701558Srgrimes			rd += i;
8711558Srgrimes			cnt -= i;
8721558Srgrimes			if (cnt > 0)
8731558Srgrimes				goto getmore;
8741558Srgrimes			i = rd;
8751558Srgrimes		} else {
8761558Srgrimes			/*
8771558Srgrimes			 * Short read. Process the blocks read.
8781558Srgrimes			 */
8791558Srgrimes			if (i % TP_BSIZE != 0)
8801558Srgrimes				vprintf(stdout,
88137240Sbde				    "partial block read: %ld should be %ld\n",
8821558Srgrimes				    i, ntrec * TP_BSIZE);
8831558Srgrimes			numtrec = i / TP_BSIZE;
8841558Srgrimes		}
8851558Srgrimes	}
8861558Srgrimes	/*
8871558Srgrimes	 * Handle read error.
8881558Srgrimes	 */
8891558Srgrimes	if (i < 0) {
8901558Srgrimes		fprintf(stderr, "Tape read error while ");
8911558Srgrimes		switch (curfile.action) {
8921558Srgrimes		default:
8931558Srgrimes			fprintf(stderr, "trying to set up tape\n");
8941558Srgrimes			break;
8951558Srgrimes		case UNKNOWN:
8961558Srgrimes			fprintf(stderr, "trying to resynchronize\n");
8971558Srgrimes			break;
8981558Srgrimes		case USING:
8991558Srgrimes			fprintf(stderr, "restoring %s\n", curfile.name);
9001558Srgrimes			break;
9011558Srgrimes		case SKIP:
9021558Srgrimes			fprintf(stderr, "skipping over inode %d\n",
9031558Srgrimes				curfile.ino);
9041558Srgrimes			break;
9051558Srgrimes		}
9061558Srgrimes		if (!yflag && !reply("continue"))
9071558Srgrimes			done(1);
9081558Srgrimes		i = ntrec * TP_BSIZE;
90923685Speter		memset(tapebuf, 0, i);
9101558Srgrimes#ifdef RRESTORE
9111558Srgrimes		if (host)
9121558Srgrimes			seek_failed = (rmtseek(i, 1) < 0);
9131558Srgrimes		else
9141558Srgrimes#endif
9151558Srgrimes			seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
9161558Srgrimes
9171558Srgrimes		if (seek_failed) {
9181558Srgrimes			fprintf(stderr,
9191558Srgrimes			    "continuation failed: %s\n", strerror(errno));
9201558Srgrimes			done(1);
9211558Srgrimes		}
9221558Srgrimes	}
9231558Srgrimes	/*
9241558Srgrimes	 * Handle end of tape.
9251558Srgrimes	 */
9261558Srgrimes	if (i == 0) {
9271558Srgrimes		vprintf(stdout, "End-of-tape encountered\n");
9281558Srgrimes		if (!pipein) {
9291558Srgrimes			newvol = volno + 1;
9301558Srgrimes			volno = 0;
9311558Srgrimes			numtrec = 0;
9321558Srgrimes			getvol(newvol);
9331558Srgrimes			readtape(buf);
9341558Srgrimes			return;
9351558Srgrimes		}
9361558Srgrimes		if (rd % TP_BSIZE != 0)
9371558Srgrimes			panic("partial block read: %d should be %d\n",
9381558Srgrimes				rd, ntrec * TP_BSIZE);
9391558Srgrimes		terminateinput();
94023685Speter		memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
9411558Srgrimes	}
9421558Srgrimes	blkcnt = 0;
94323685Speter	memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
9441558Srgrimes	blksread++;
94590827Siedowse	tapeaddr++;
9461558Srgrimes}
9471558Srgrimes
9481558Srgrimesstatic void
94992837Simpfindtapeblksize(void)
9501558Srgrimes{
95192806Sobrien	long i;
9521558Srgrimes
9531558Srgrimes	for (i = 0; i < ntrec; i++)
9541558Srgrimes		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
9551558Srgrimes	blkcnt = 0;
9561558Srgrimes#ifdef RRESTORE
9571558Srgrimes	if (host)
9581558Srgrimes		i = rmtread(tapebuf, ntrec * TP_BSIZE);
9591558Srgrimes	else
9601558Srgrimes#endif
9611558Srgrimes		i = read(mt, tapebuf, ntrec * TP_BSIZE);
9621558Srgrimes
9631558Srgrimes	if (i <= 0) {
9641558Srgrimes		fprintf(stderr, "tape read error: %s\n", strerror(errno));
9651558Srgrimes		done(1);
9661558Srgrimes	}
9671558Srgrimes	if (i % TP_BSIZE != 0) {
96837240Sbde		fprintf(stderr, "Tape block size (%ld) %s (%d)\n",
9691558Srgrimes			i, "is not a multiple of dump block size", TP_BSIZE);
9701558Srgrimes		done(1);
9711558Srgrimes	}
9721558Srgrimes	ntrec = i / TP_BSIZE;
9731558Srgrimes	numtrec = ntrec;
97437240Sbde	vprintf(stdout, "Tape block size is %ld\n", ntrec);
9751558Srgrimes}
9761558Srgrimes
9771558Srgrimesvoid
97892837Simpclosemt(void)
9791558Srgrimes{
9801558Srgrimes
9811558Srgrimes	if (mt < 0)
9821558Srgrimes		return;
9831558Srgrimes#ifdef RRESTORE
9841558Srgrimes	if (host)
9851558Srgrimes		rmtclose();
9861558Srgrimes	else
9871558Srgrimes#endif
9881558Srgrimes		(void) close(mt);
9891558Srgrimes}
9901558Srgrimes
9911558Srgrimes/*
9921558Srgrimes * Read the next block from the tape.
9931558Srgrimes * Check to see if it is one of several vintage headers.
9941558Srgrimes * If it is an old style header, convert it to a new style header.
9951558Srgrimes * If it is not any valid header, return an error.
9961558Srgrimes */
9971558Srgrimesstatic int
99892837Simpgethead(struct s_spcl *buf)
9991558Srgrimes{
10001558Srgrimes	long i;
10011558Srgrimes	union {
10021558Srgrimes		quad_t	qval;
100340668Sdima		int32_t	val[2];
10041558Srgrimes	} qcvt;
10051558Srgrimes	union u_ospcl {
10061558Srgrimes		char dummy[TP_BSIZE];
10071558Srgrimes		struct	s_ospcl {
100840668Sdima			int32_t	c_type;
100940668Sdima			int32_t	c_date;
101040668Sdima			int32_t	c_ddate;
101140668Sdima			int32_t	c_volume;
101240668Sdima			int32_t	c_tapea;
10131558Srgrimes			u_short	c_inumber;
101440668Sdima			int32_t	c_magic;
101540668Sdima			int32_t	c_checksum;
10161558Srgrimes			struct odinode {
10171558Srgrimes				unsigned short odi_mode;
10181558Srgrimes				u_short	odi_nlink;
10191558Srgrimes				u_short	odi_uid;
10201558Srgrimes				u_short	odi_gid;
102140668Sdima				int32_t	odi_size;
102240668Sdima				int32_t	odi_rdev;
10231558Srgrimes				char	odi_addr[36];
102440668Sdima				int32_t	odi_atime;
102540668Sdima				int32_t	odi_mtime;
102640668Sdima				int32_t	odi_ctime;
10271558Srgrimes			} c_dinode;
102840668Sdima			int32_t	c_count;
10291558Srgrimes			char	c_addr[256];
10301558Srgrimes		} s_ospcl;
10311558Srgrimes	} u_ospcl;
10321558Srgrimes
10331558Srgrimes	if (!cvtflag) {
10341558Srgrimes		readtape((char *)buf);
10351558Srgrimes		if (buf->c_magic != NFS_MAGIC) {
10361558Srgrimes			if (swabl(buf->c_magic) != NFS_MAGIC)
10371558Srgrimes				return (FAIL);
10381558Srgrimes			if (!Bcvt) {
10391558Srgrimes				vprintf(stdout, "Note: Doing Byte swapping\n");
10401558Srgrimes				Bcvt = 1;
10411558Srgrimes			}
10421558Srgrimes		}
10431558Srgrimes		if (checksum((int *)buf) == FAIL)
10441558Srgrimes			return (FAIL);
104523096Simp		if (Bcvt) {
10461558Srgrimes			swabst((u_char *)"8l4s31l", (u_char *)buf);
104723096Simp			swabst((u_char *)"l",(u_char *) &buf->c_level);
104823096Simp			swabst((u_char *)"2l",(u_char *) &buf->c_flags);
104923096Simp		}
10501558Srgrimes		goto good;
10511558Srgrimes	}
10521558Srgrimes	readtape((char *)(&u_ospcl.s_ospcl));
105323685Speter	memset(buf, 0, (long)TP_BSIZE);
10541558Srgrimes	buf->c_type = u_ospcl.s_ospcl.c_type;
10551558Srgrimes	buf->c_date = u_ospcl.s_ospcl.c_date;
10561558Srgrimes	buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
10571558Srgrimes	buf->c_volume = u_ospcl.s_ospcl.c_volume;
10581558Srgrimes	buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
10591558Srgrimes	buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
10601558Srgrimes	buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
10611558Srgrimes	buf->c_magic = u_ospcl.s_ospcl.c_magic;
10621558Srgrimes	buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
10631558Srgrimes	buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
10641558Srgrimes	buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
10651558Srgrimes	buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
10661558Srgrimes	buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
10671558Srgrimes	buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
106823685Speter	buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
106923685Speter	buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
107023685Speter	buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
10711558Srgrimes	buf->c_count = u_ospcl.s_ospcl.c_count;
107223685Speter	memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
10731558Srgrimes	if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
10741558Srgrimes	    checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
10751558Srgrimes		return(FAIL);
10761558Srgrimes	buf->c_magic = NFS_MAGIC;
10771558Srgrimes
10781558Srgrimesgood:
10791558Srgrimes	if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
10801558Srgrimes	    (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
10811558Srgrimes		qcvt.qval = buf->c_dinode.di_size;
10821558Srgrimes		if (qcvt.val[0] || qcvt.val[1]) {
10831558Srgrimes			printf("Note: Doing Quad swapping\n");
10841558Srgrimes			Qcvt = 1;
10851558Srgrimes		}
10861558Srgrimes	}
10871558Srgrimes	if (Qcvt) {
10881558Srgrimes		qcvt.qval = buf->c_dinode.di_size;
10891558Srgrimes		i = qcvt.val[1];
10901558Srgrimes		qcvt.val[1] = qcvt.val[0];
10911558Srgrimes		qcvt.val[0] = i;
10921558Srgrimes		buf->c_dinode.di_size = qcvt.qval;
10931558Srgrimes	}
109437923Simp	readmapflag = 0;
10951558Srgrimes
10961558Srgrimes	switch (buf->c_type) {
10971558Srgrimes
10981558Srgrimes	case TS_CLRI:
10991558Srgrimes	case TS_BITS:
11001558Srgrimes		/*
11011558Srgrimes		 * Have to patch up missing information in bit map headers
11021558Srgrimes		 */
11031558Srgrimes		buf->c_inumber = 0;
11041558Srgrimes		buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
110537923Simp		if (buf->c_count > TP_NINDIR)
110637923Simp			readmapflag = 1;
110737923Simp		else
110837923Simp			for (i = 0; i < buf->c_count; i++)
110937923Simp				buf->c_addr[i]++;
11101558Srgrimes		break;
11111558Srgrimes
11121558Srgrimes	case TS_TAPE:
11131558Srgrimes		if ((buf->c_flags & DR_NEWINODEFMT) == 0)
11141558Srgrimes			oldinofmt = 1;
11151558Srgrimes		/* fall through */
11161558Srgrimes	case TS_END:
11171558Srgrimes		buf->c_inumber = 0;
11181558Srgrimes		break;
11191558Srgrimes
11201558Srgrimes	case TS_INODE:
11211558Srgrimes	case TS_ADDR:
11221558Srgrimes		break;
11231558Srgrimes
11241558Srgrimes	default:
11251558Srgrimes		panic("gethead: unknown inode type %d\n", buf->c_type);
11261558Srgrimes		break;
11271558Srgrimes	}
11281558Srgrimes	/*
11298871Srgrimes	 * If we are restoring a filesystem with old format inodes,
11301558Srgrimes	 * copy the uid/gid to the new location.
11311558Srgrimes	 */
11321558Srgrimes	if (oldinofmt) {
11331558Srgrimes		buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
11341558Srgrimes		buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
11351558Srgrimes	}
113690827Siedowse	tapeaddr = buf->c_tapea;
11371558Srgrimes	if (dflag)
11381558Srgrimes		accthdr(buf);
11391558Srgrimes	return(GOOD);
11401558Srgrimes}
11411558Srgrimes
11421558Srgrimes/*
11431558Srgrimes * Check that a header is where it belongs and predict the next header
11441558Srgrimes */
11451558Srgrimesstatic void
114692837Simpaccthdr(struct s_spcl *header)
11471558Srgrimes{
11481558Srgrimes	static ino_t previno = 0x7fffffff;
11491558Srgrimes	static int prevtype;
11501558Srgrimes	static long predict;
11511558Srgrimes	long blks, i;
11521558Srgrimes
11531558Srgrimes	if (header->c_type == TS_TAPE) {
11541558Srgrimes		fprintf(stderr, "Volume header (%s inode format) ",
11551558Srgrimes		    oldinofmt ? "old" : "new");
11561558Srgrimes 		if (header->c_firstrec)
115737240Sbde 			fprintf(stderr, "begins with record %ld",
11581558Srgrimes 				header->c_firstrec);
11591558Srgrimes 		fprintf(stderr, "\n");
11601558Srgrimes		previno = 0x7fffffff;
11611558Srgrimes		return;
11621558Srgrimes	}
11631558Srgrimes	if (previno == 0x7fffffff)
11641558Srgrimes		goto newcalc;
11651558Srgrimes	switch (prevtype) {
11661558Srgrimes	case TS_BITS:
116723685Speter		fprintf(stderr, "Dumped inodes map header");
11681558Srgrimes		break;
11691558Srgrimes	case TS_CLRI:
117023685Speter		fprintf(stderr, "Used inodes map header");
11711558Srgrimes		break;
11721558Srgrimes	case TS_INODE:
11731558Srgrimes		fprintf(stderr, "File header, ino %d", previno);
11741558Srgrimes		break;
11751558Srgrimes	case TS_ADDR:
11761558Srgrimes		fprintf(stderr, "File continuation header, ino %d", previno);
11771558Srgrimes		break;
11781558Srgrimes	case TS_END:
11791558Srgrimes		fprintf(stderr, "End of tape header");
11801558Srgrimes		break;
11811558Srgrimes	}
11821558Srgrimes	if (predict != blksread - 1)
118337240Sbde		fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
11841558Srgrimes			predict, blksread - 1);
11851558Srgrimes	fprintf(stderr, "\n");
11861558Srgrimesnewcalc:
11871558Srgrimes	blks = 0;
11881558Srgrimes	if (header->c_type != TS_END)
11891558Srgrimes		for (i = 0; i < header->c_count; i++)
119037923Simp			if (readmapflag || header->c_addr[i] != 0)
11911558Srgrimes				blks++;
11921558Srgrimes	predict = blks;
11931558Srgrimes	blksread = 0;
11941558Srgrimes	prevtype = header->c_type;
11951558Srgrimes	previno = header->c_inumber;
11961558Srgrimes}
11971558Srgrimes
11981558Srgrimes/*
11991558Srgrimes * Find an inode header.
120090573Siedowse * Complain if had to skip.
12011558Srgrimes */
12021558Srgrimesstatic void
120392837Simpfindinode(struct s_spcl *header)
12041558Srgrimes{
12051558Srgrimes	static long skipcnt = 0;
12061558Srgrimes	long i;
12071558Srgrimes	char buf[TP_BSIZE];
120890608Siedowse	int htype;
12091558Srgrimes
12101558Srgrimes	curfile.name = "<name unknown>";
12111558Srgrimes	curfile.action = UNKNOWN;
12121558Srgrimes	curfile.dip = NULL;
12131558Srgrimes	curfile.ino = 0;
12141558Srgrimes	do {
12151558Srgrimes		if (header->c_magic != NFS_MAGIC) {
12161558Srgrimes			skipcnt++;
12171558Srgrimes			while (gethead(header) == FAIL ||
121889572Sdillon			    _time32_to_time(header->c_date) != dumpdate)
12191558Srgrimes				skipcnt++;
12201558Srgrimes		}
122190608Siedowse		htype = header->c_type;
122290608Siedowse		switch (htype) {
12231558Srgrimes
12241558Srgrimes		case TS_ADDR:
12251558Srgrimes			/*
12261558Srgrimes			 * Skip up to the beginning of the next record
12271558Srgrimes			 */
12281558Srgrimes			for (i = 0; i < header->c_count; i++)
12291558Srgrimes				if (header->c_addr[i])
12301558Srgrimes					readtape(buf);
12311558Srgrimes			while (gethead(header) == FAIL ||
123289572Sdillon			    _time32_to_time(header->c_date) != dumpdate)
12331558Srgrimes				skipcnt++;
12341558Srgrimes			break;
12351558Srgrimes
12361558Srgrimes		case TS_INODE:
12371558Srgrimes			curfile.dip = &header->c_dinode;
12381558Srgrimes			curfile.ino = header->c_inumber;
12391558Srgrimes			break;
12401558Srgrimes
12411558Srgrimes		case TS_END:
124290820Siedowse			/* If we missed some tapes, get another volume. */
124390820Siedowse			if (tapesread & (tapesread + 1)) {
124490820Siedowse				getvol(0);
124590820Siedowse				continue;
124690820Siedowse			}
12471558Srgrimes			curfile.ino = maxino;
12481558Srgrimes			break;
12491558Srgrimes
12501558Srgrimes		case TS_CLRI:
12511558Srgrimes			curfile.name = "<file removal list>";
12521558Srgrimes			break;
12531558Srgrimes
12541558Srgrimes		case TS_BITS:
12551558Srgrimes			curfile.name = "<file dump list>";
12561558Srgrimes			break;
12571558Srgrimes
12581558Srgrimes		case TS_TAPE:
12591558Srgrimes			panic("unexpected tape header\n");
12601558Srgrimes			/* NOTREACHED */
12611558Srgrimes
12621558Srgrimes		default:
12631558Srgrimes			panic("unknown tape header type %d\n", spcl.c_type);
12641558Srgrimes			/* NOTREACHED */
12651558Srgrimes
12661558Srgrimes		}
126790608Siedowse	} while (htype == TS_ADDR);
12681558Srgrimes	if (skipcnt > 0)
126937240Sbde		fprintf(stderr, "resync restore, skipped %ld blocks\n",
127037240Sbde		    skipcnt);
12711558Srgrimes	skipcnt = 0;
12721558Srgrimes}
12731558Srgrimes
12741558Srgrimesstatic int
127592837Simpchecksum(int *buf)
12761558Srgrimes{
127792806Sobrien	int i, j;
12781558Srgrimes
12791558Srgrimes	j = sizeof(union u_spcl) / sizeof(int);
12801558Srgrimes	i = 0;
12811558Srgrimes	if(!Bcvt) {
12821558Srgrimes		do
12831558Srgrimes			i += *buf++;
12841558Srgrimes		while (--j);
12851558Srgrimes	} else {
12861558Srgrimes		/* What happens if we want to read restore tapes
12871558Srgrimes			for a 16bit int machine??? */
12888871Srgrimes		do
12891558Srgrimes			i += swabl(*buf++);
12901558Srgrimes		while (--j);
12911558Srgrimes	}
12928871Srgrimes
12931558Srgrimes	if (i != CHECKSUM) {
12941558Srgrimes		fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
12951558Srgrimes			curfile.ino, curfile.name);
12961558Srgrimes		return(FAIL);
12971558Srgrimes	}
12981558Srgrimes	return(GOOD);
12991558Srgrimes}
13001558Srgrimes
13011558Srgrimes#ifdef RRESTORE
13021558Srgrimes#include <stdarg.h>
13031558Srgrimes
13041558Srgrimesvoid
13051558Srgrimesmsg(const char *fmt, ...)
13061558Srgrimes{
13071558Srgrimes	va_list ap;
13081558Srgrimes	va_start(ap, fmt);
13091558Srgrimes	(void)vfprintf(stderr, fmt, ap);
13101558Srgrimes	va_end(ap);
13111558Srgrimes}
13121558Srgrimes#endif /* RRESTORE */
13131558Srgrimes
13141558Srgrimesstatic u_char *
131592837Simpswabshort(u_char *sp, int n)
13161558Srgrimes{
13171558Srgrimes	char c;
13181558Srgrimes
13191558Srgrimes	while (--n >= 0) {
13201558Srgrimes		c = sp[0]; sp[0] = sp[1]; sp[1] = c;
13211558Srgrimes		sp += 2;
13221558Srgrimes	}
13231558Srgrimes	return (sp);
13241558Srgrimes}
13251558Srgrimes
13261558Srgrimesstatic u_char *
132792837Simpswablong(u_char *sp, int n)
13281558Srgrimes{
13291558Srgrimes	char c;
13301558Srgrimes
13311558Srgrimes	while (--n >= 0) {
13321558Srgrimes		c = sp[0]; sp[0] = sp[3]; sp[3] = c;
13331558Srgrimes		c = sp[2]; sp[2] = sp[1]; sp[1] = c;
13341558Srgrimes		sp += 4;
13351558Srgrimes	}
13361558Srgrimes	return (sp);
13371558Srgrimes}
13381558Srgrimes
13391558Srgrimesvoid
134092837Simpswabst(u_char *cp, u_char *sp)
13411558Srgrimes{
13421558Srgrimes	int n = 0;
13431558Srgrimes
13441558Srgrimes	while (*cp) {
13451558Srgrimes		switch (*cp) {
13461558Srgrimes		case '0': case '1': case '2': case '3': case '4':
13471558Srgrimes		case '5': case '6': case '7': case '8': case '9':
13481558Srgrimes			n = (n * 10) + (*cp++ - '0');
13491558Srgrimes			continue;
13508871Srgrimes
13511558Srgrimes		case 's': case 'w': case 'h':
13521558Srgrimes			if (n == 0)
13531558Srgrimes				n = 1;
13541558Srgrimes			sp = swabshort(sp, n);
13551558Srgrimes			break;
13561558Srgrimes
13571558Srgrimes		case 'l':
13581558Srgrimes			if (n == 0)
13591558Srgrimes				n = 1;
13601558Srgrimes			sp = swablong(sp, n);
13611558Srgrimes			break;
13621558Srgrimes
13631558Srgrimes		default: /* Any other character, like 'b' counts as byte. */
13641558Srgrimes			if (n == 0)
13651558Srgrimes				n = 1;
13661558Srgrimes			sp += n;
13671558Srgrimes			break;
13681558Srgrimes		}
13691558Srgrimes		cp++;
13701558Srgrimes		n = 0;
13711558Srgrimes	}
13721558Srgrimes}
13731558Srgrimes
13741558Srgrimesstatic u_long
137592837Simpswabl(u_long x)
13761558Srgrimes{
13771558Srgrimes	swabst((u_char *)"l", (u_char *)&x);
13781558Srgrimes	return (x);
13791558Srgrimes}
1380