xref: /csrg-svn/sbin/restore/dirs.c (revision 34268)
121165Sdist /*
221165Sdist  * Copyright (c) 1983 Regents of the University of California.
321165Sdist  * All rights reserved.  The Berkeley software License Agreement
421165Sdist  * specifies the terms and conditions for redistribution.
521165Sdist  */
621165Sdist 
711127Smckusick #ifndef lint
8*34268Smckusick static char sccsid[] = "@(#)dirs.c	5.5 (Berkeley) 05/13/88";
921165Sdist #endif not lint
1011127Smckusick 
1111127Smckusick #include "restore.h"
1223545Smckusick #include <protocols/dumprestore.h>
1311309Smckusick #include <sys/file.h>
1411127Smckusick 
1511992Smckusick /*
1611992Smckusick  * Symbol table of directories read from tape.
1711992Smckusick  */
1811127Smckusick #define HASHSIZE	1000
1911127Smckusick #define INOHASH(val) (val % HASHSIZE)
2011127Smckusick struct inotab {
2111127Smckusick 	struct inotab *t_next;
2211127Smckusick 	ino_t	t_ino;
2311127Smckusick 	daddr_t	t_seekpt;
2411127Smckusick 	long t_size;
2511309Smckusick };
2611309Smckusick static struct inotab *inotab[HASHSIZE];
2711309Smckusick extern struct inotab *inotablookup();
2811322Smckusick extern struct inotab *allocinotab();
2911127Smckusick 
3011992Smckusick /*
3111992Smckusick  * Information retained about directories.
3211992Smckusick  */
3311127Smckusick struct modeinfo {
3411127Smckusick 	ino_t ino;
3511127Smckusick 	time_t timep[2];
3611127Smckusick 	short mode;
3711127Smckusick 	short uid;
3811127Smckusick 	short gid;
3911127Smckusick };
4011127Smckusick 
4111992Smckusick /*
4211992Smckusick  * Global variables for this file.
4311992Smckusick  */
4411309Smckusick static daddr_t	seekpt;
4511309Smckusick static FILE	*df, *mf;
4611309Smckusick static DIR	*dirp;
4711992Smckusick static char	dirfile[32] = "#";	/* No file */
4811992Smckusick static char	modefile[32] = "#";	/* No file */
4911309Smckusick extern ino_t	search();
5012556Smckusick struct direct 	*rst_readdir();
5112556Smckusick extern void 	rst_seekdir();
5211127Smckusick 
5311992Smckusick /*
5411992Smckusick  * Format of old style directories.
5511992Smckusick  */
5611127Smckusick #define ODIRSIZ 14
5711127Smckusick struct odirect {
5811127Smckusick 	u_short	d_ino;
5911127Smckusick 	char	d_name[ODIRSIZ];
6011127Smckusick };
6111127Smckusick 
6211127Smckusick /*
6311127Smckusick  *	Extract directory contents, building up a directory structure
6411127Smckusick  *	on disk for extraction by name.
6511992Smckusick  *	If genmode is requested, save mode, owner, and times for all
6611127Smckusick  *	directories on the tape.
6711127Smckusick  */
6811992Smckusick extractdirs(genmode)
6911992Smckusick 	int genmode;
7011127Smckusick {
7111127Smckusick 	register int i;
7211127Smckusick 	register struct dinode *ip;
7311322Smckusick 	struct inotab *itp;
7411127Smckusick 	struct direct nulldir;
7511127Smckusick 	int putdir(), null();
7611127Smckusick 
7711127Smckusick 	vprintf(stdout, "Extract directories from tape\n");
7811992Smckusick 	(void) sprintf(dirfile, "/tmp/rstdir%d", dumpdate);
7911127Smckusick 	df = fopen(dirfile, "w");
8011127Smckusick 	if (df == 0) {
8111127Smckusick 		fprintf(stderr,
8215779Smckusick 		    "restore: %s - cannot create directory temporary\n",
8311127Smckusick 		    dirfile);
8411127Smckusick 		perror("fopen");
8511127Smckusick 		done(1);
8611127Smckusick 	}
8711992Smckusick 	if (genmode != 0) {
8811992Smckusick 		(void) sprintf(modefile, "/tmp/rstmode%d", dumpdate);
8911127Smckusick 		mf = fopen(modefile, "w");
9011309Smckusick 		if (mf == 0) {
9111127Smckusick 			fprintf(stderr,
9215779Smckusick 			    "restore: %s - cannot create modefile \n",
9311127Smckusick 			    modefile);
9411127Smckusick 			perror("fopen");
9511127Smckusick 			done(1);
9611127Smckusick 		}
9711127Smckusick 	}
9811322Smckusick 	nulldir.d_ino = 0;
9911127Smckusick 	nulldir.d_namlen = 1;
10012453Smckusick 	(void) strcpy(nulldir.d_name, "/");
10111127Smckusick 	nulldir.d_reclen = DIRSIZ(&nulldir);
10211127Smckusick 	for (;;) {
10311127Smckusick 		curfile.name = "<directory file - name unknown>";
10411127Smckusick 		curfile.action = USING;
10511127Smckusick 		ip = curfile.dip;
10617948Smckusick 		if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
10711732Smckusick 			(void) fclose(df);
10811127Smckusick 			dirp = opendir(dirfile);
10911127Smckusick 			if (dirp == NULL)
11011127Smckusick 				perror("opendir");
11111127Smckusick 			if (mf != NULL)
11211732Smckusick 				(void) fclose(mf);
11311992Smckusick 			i = dirlookup(".");
11411992Smckusick 			if (i == 0)
11511421Smckusick 				panic("Root directory is not on tape\n");
11611127Smckusick 			return;
11711127Smckusick 		}
11811322Smckusick 		itp = allocinotab(curfile.ino, ip, seekpt);
11911127Smckusick 		getfile(putdir, null);
12011127Smckusick 		putent(&nulldir);
12111127Smckusick 		flushent();
12211322Smckusick 		itp->t_size = seekpt - itp->t_seekpt;
12311127Smckusick 	}
12411127Smckusick }
12511127Smckusick 
12611127Smckusick /*
12711322Smckusick  * skip over all the directories on the tape
12811322Smckusick  */
12911322Smckusick skipdirs()
13011322Smckusick {
13111322Smckusick 
13211322Smckusick 	while ((curfile.dip->di_mode & IFMT) == IFDIR) {
13311322Smckusick 		skipfile();
13411322Smckusick 	}
13511322Smckusick }
13611322Smckusick 
13711322Smckusick /*
13811127Smckusick  *	Recursively find names and inumbers of all files in subtree
13911127Smckusick  *	pname and pass them off to be processed.
14011127Smckusick  */
14111127Smckusick treescan(pname, ino, todo)
14211127Smckusick 	char *pname;
14311127Smckusick 	ino_t ino;
14411744Smckusick 	long (*todo)();
14511127Smckusick {
14611127Smckusick 	register struct inotab *itp;
14712453Smckusick 	register struct direct *dp;
14812453Smckusick 	register struct entry *np;
14911127Smckusick 	int namelen;
15011127Smckusick 	daddr_t bpt;
15111644Smckusick 	char locname[MAXPATHLEN + 1];
15211127Smckusick 
15311127Smckusick 	itp = inotablookup(ino);
15411127Smckusick 	if (itp == NULL) {
15511127Smckusick 		/*
15611127Smckusick 		 * Pname is name of a simple file or an unchanged directory.
15711127Smckusick 		 */
15811744Smckusick 		(void) (*todo)(pname, ino, LEAF);
15911127Smckusick 		return;
16011127Smckusick 	}
16111127Smckusick 	/*
16211127Smckusick 	 * Pname is a dumped directory name.
16311127Smckusick 	 */
16411744Smckusick 	if ((*todo)(pname, ino, NODE) == FAIL)
16511744Smckusick 		return;
16611127Smckusick 	/*
16711127Smckusick 	 * begin search through the directory
16811127Smckusick 	 * skipping over "." and ".."
16911127Smckusick 	 */
17011992Smckusick 	(void) strncpy(locname, pname, MAXPATHLEN);
17111992Smckusick 	(void) strncat(locname, "/", MAXPATHLEN);
17211127Smckusick 	namelen = strlen(locname);
17312556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
17412556Smckusick 	dp = rst_readdir(dirp); /* "." */
17527263Smckusick 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
17612556Smckusick 		dp = rst_readdir(dirp); /* ".." */
17727263Smckusick 	else
17827263Smckusick 		fprintf(stderr, "Warning: `.' missing from directory %s\n",
17927263Smckusick 			pname);
18027263Smckusick 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
18112556Smckusick 		dp = rst_readdir(dirp); /* first real entry */
18227263Smckusick 	else
18327263Smckusick 		fprintf(stderr, "Warning: `..' missing from directory %s\n",
18427263Smckusick 			pname);
18511127Smckusick 	bpt = telldir(dirp);
18611127Smckusick 	/*
18712453Smckusick 	 * a zero inode signals end of directory
18811127Smckusick 	 */
18912453Smckusick 	while (dp != NULL && dp->d_ino != 0) {
19011127Smckusick 		locname[namelen] = '\0';
19111644Smckusick 		if (namelen + dp->d_namlen >= MAXPATHLEN) {
19211127Smckusick 			fprintf(stderr, "%s%s: name exceeds %d char\n",
19311644Smckusick 				locname, dp->d_name, MAXPATHLEN);
19411127Smckusick 		} else {
19511992Smckusick 			(void) strncat(locname, dp->d_name, (int)dp->d_namlen);
19611127Smckusick 			treescan(locname, dp->d_ino, todo);
19712556Smckusick 			rst_seekdir(dirp, bpt, itp->t_seekpt);
19811127Smckusick 		}
19912556Smckusick 		dp = rst_readdir(dirp);
20011127Smckusick 		bpt = telldir(dirp);
20111127Smckusick 	}
20211127Smckusick 	if (dp == NULL)
20311127Smckusick 		fprintf(stderr, "corrupted directory: %s.\n", locname);
20411127Smckusick }
20511127Smckusick 
20611127Smckusick /*
20711127Smckusick  * Search the directory tree rooted at inode ROOTINO
20811127Smckusick  * for the path pointed at by n
20911127Smckusick  */
21011127Smckusick ino_t
21111127Smckusick psearch(n)
21211127Smckusick 	char	*n;
21311127Smckusick {
21411127Smckusick 	register char *cp, *cp1;
21511127Smckusick 	ino_t ino;
21611127Smckusick 	char c;
21711127Smckusick 
21811127Smckusick 	ino = ROOTINO;
21911127Smckusick 	if (*(cp = n) == '/')
22011127Smckusick 		cp++;
22111127Smckusick next:
22211127Smckusick 	cp1 = cp + 1;
22311127Smckusick 	while (*cp1 != '/' && *cp1)
22411127Smckusick 		cp1++;
22511127Smckusick 	c = *cp1;
22611127Smckusick 	*cp1 = 0;
22711127Smckusick 	ino = search(ino, cp);
22811127Smckusick 	if (ino == 0) {
22911127Smckusick 		*cp1 = c;
23011127Smckusick 		return(0);
23111127Smckusick 	}
23211127Smckusick 	*cp1 = c;
23311127Smckusick 	if (c == '/') {
23411127Smckusick 		cp = cp1+1;
23511127Smckusick 		goto next;
23611127Smckusick 	}
23711127Smckusick 	return(ino);
23811127Smckusick }
23911127Smckusick 
24011127Smckusick /*
24111127Smckusick  * search the directory inode ino
24211127Smckusick  * looking for entry cp
24311127Smckusick  */
24411127Smckusick ino_t
24511127Smckusick search(inum, cp)
24611127Smckusick 	ino_t	inum;
24711127Smckusick 	char	*cp;
24811127Smckusick {
24911127Smckusick 	register struct direct *dp;
25011127Smckusick 	register struct inotab *itp;
25111127Smckusick 	int len;
25211127Smckusick 
25311127Smckusick 	itp = inotablookup(inum);
25411127Smckusick 	if (itp == NULL)
25511127Smckusick 		return(0);
25612556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
25711127Smckusick 	len = strlen(cp);
25811127Smckusick 	do {
25912556Smckusick 		dp = rst_readdir(dirp);
26012453Smckusick 		if (dp == NULL || dp->d_ino == 0)
26112453Smckusick 			return (0);
26211992Smckusick 	} while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0);
26311127Smckusick 	return(dp->d_ino);
26411127Smckusick }
26511127Smckusick 
26611127Smckusick /*
26711127Smckusick  * Put the directory entries in the directory file
26811127Smckusick  */
26911127Smckusick putdir(buf, size)
27011127Smckusick 	char *buf;
27111127Smckusick 	int size;
27211127Smckusick {
27311127Smckusick 	struct direct cvtbuf;
27411127Smckusick 	register struct odirect *odp;
27511127Smckusick 	struct odirect *eodp;
27611127Smckusick 	register struct direct *dp;
27711127Smckusick 	long loc, i;
27826941Ssklower 	extern int Bcvt;
27911127Smckusick 
28011127Smckusick 	if (cvtflag) {
28111127Smckusick 		eodp = (struct odirect *)&buf[size];
28211127Smckusick 		for (odp = (struct odirect *)buf; odp < eodp; odp++)
28311127Smckusick 			if (odp->d_ino != 0) {
28411127Smckusick 				dcvt(odp, &cvtbuf);
28511127Smckusick 				putent(&cvtbuf);
28611127Smckusick 			}
28711127Smckusick 	} else {
28811127Smckusick 		for (loc = 0; loc < size; ) {
28911127Smckusick 			dp = (struct direct *)(buf + loc);
29026941Ssklower 			if (Bcvt) {
29126941Ssklower 				swabst("l2s", (char *) dp);
29226941Ssklower 			}
29311127Smckusick 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
29411127Smckusick 			if (dp->d_reclen == 0 || dp->d_reclen > i) {
29511127Smckusick 				loc += i;
29611127Smckusick 				continue;
29711127Smckusick 			}
29811127Smckusick 			loc += dp->d_reclen;
29911127Smckusick 			if (dp->d_ino != 0) {
30011127Smckusick 				putent(dp);
30111127Smckusick 			}
30211127Smckusick 		}
30311127Smckusick 	}
30411127Smckusick }
30511127Smckusick 
30611127Smckusick /*
30711127Smckusick  * These variables are "local" to the following two functions.
30811127Smckusick  */
30911127Smckusick char dirbuf[DIRBLKSIZ];
31011127Smckusick long dirloc = 0;
31111127Smckusick long prev = 0;
31211127Smckusick 
31311127Smckusick /*
31411127Smckusick  * add a new directory entry to a file.
31511127Smckusick  */
31611127Smckusick putent(dp)
31711127Smckusick 	struct direct *dp;
31811127Smckusick {
31911322Smckusick 	dp->d_reclen = DIRSIZ(dp);
32011127Smckusick 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
32111127Smckusick 		((struct direct *)(dirbuf + prev))->d_reclen =
32211127Smckusick 		    DIRBLKSIZ - prev;
32311732Smckusick 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
32411127Smckusick 		dirloc = 0;
32511127Smckusick 	}
32611127Smckusick 	bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen);
32711127Smckusick 	prev = dirloc;
32811127Smckusick 	dirloc += dp->d_reclen;
32911127Smckusick }
33011127Smckusick 
33111127Smckusick /*
33211127Smckusick  * flush out a directory that is finished.
33311127Smckusick  */
33411127Smckusick flushent()
33511127Smckusick {
33611127Smckusick 
33711127Smckusick 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
33811732Smckusick 	(void) fwrite(dirbuf, (int)dirloc, 1, df);
33911127Smckusick 	seekpt = ftell(df);
34011127Smckusick 	dirloc = 0;
34111127Smckusick }
34211127Smckusick 
34311127Smckusick dcvt(odp, ndp)
34411127Smckusick 	register struct odirect *odp;
34511127Smckusick 	register struct direct *ndp;
34611127Smckusick {
34711127Smckusick 
34811127Smckusick 	bzero((char *)ndp, (long)(sizeof *ndp));
34911127Smckusick 	ndp->d_ino =  odp->d_ino;
35011992Smckusick 	(void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
35111127Smckusick 	ndp->d_namlen = strlen(ndp->d_name);
35211127Smckusick 	ndp->d_reclen = DIRSIZ(ndp);
35311127Smckusick }
35411127Smckusick 
35511127Smckusick /*
35611127Smckusick  * Seek to an entry in a directory.
35712556Smckusick  * Only values returned by ``telldir'' should be passed to rst_seekdir.
35811732Smckusick  * This routine handles many directories in a single file.
35911732Smckusick  * It takes the base of the directory in the file, plus
36011732Smckusick  * the desired seek offset into it.
36111127Smckusick  */
36211127Smckusick void
36312556Smckusick rst_seekdir(dirp, loc, base)
36411127Smckusick 	register DIR *dirp;
36511127Smckusick 	daddr_t loc, base;
36611127Smckusick {
36711127Smckusick 
36811127Smckusick 	if (loc == telldir(dirp))
36911127Smckusick 		return;
37011127Smckusick 	loc -= base;
37111127Smckusick 	if (loc < 0)
37212556Smckusick 		fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
37311127Smckusick 	(void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
37411127Smckusick 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
37511127Smckusick 	if (dirp->dd_loc != 0)
37611127Smckusick 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
37711127Smckusick }
37811127Smckusick 
37911127Smckusick /*
38011127Smckusick  * get next entry in a directory.
38111127Smckusick  */
38211127Smckusick struct direct *
38312556Smckusick rst_readdir(dirp)
38411127Smckusick 	register DIR *dirp;
38511127Smckusick {
38611127Smckusick 	register struct direct *dp;
38711127Smckusick 
38811127Smckusick 	for (;;) {
38911127Smckusick 		if (dirp->dd_loc == 0) {
39011127Smckusick 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
39111127Smckusick 			    DIRBLKSIZ);
39212453Smckusick 			if (dirp->dd_size <= 0) {
39312453Smckusick 				dprintf(stderr, "error reading directory\n");
39411127Smckusick 				return NULL;
39512453Smckusick 			}
39611127Smckusick 		}
39711127Smckusick 		if (dirp->dd_loc >= dirp->dd_size) {
39811127Smckusick 			dirp->dd_loc = 0;
39911127Smckusick 			continue;
40011127Smckusick 		}
40111127Smckusick 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
40211127Smckusick 		if (dp->d_reclen == 0 ||
40312453Smckusick 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
40412453Smckusick 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
40512453Smckusick 				dp->d_reclen);
40611127Smckusick 			return NULL;
40712453Smckusick 		}
40811127Smckusick 		dirp->dd_loc += dp->d_reclen;
40912453Smckusick 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
41012453Smckusick 			continue;
41112556Smckusick 		if (dp->d_ino >= maxino) {
41212453Smckusick 			dprintf(stderr, "corrupted directory: bad inum %d\n",
41312453Smckusick 				dp->d_ino);
41412453Smckusick 			continue;
41512453Smckusick 		}
41611127Smckusick 		return (dp);
41711127Smckusick 	}
41811127Smckusick }
41911127Smckusick 
42011127Smckusick /*
42117753Smckusick  * Simulate the opening of a directory
42217753Smckusick  */
42317753Smckusick DIR *
42417753Smckusick rst_opendir(name)
42517753Smckusick 	char *name;
42617753Smckusick {
42717753Smckusick 	struct inotab *itp;
42817753Smckusick 	ino_t ino;
42917753Smckusick 
43017753Smckusick 	if ((ino = dirlookup(name)) > 0 &&
43117753Smckusick 	    (itp = inotablookup(ino)) != NULL) {
43217753Smckusick 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
43317753Smckusick 		return (dirp);
43417753Smckusick 	}
43517753Smckusick 	return (0);
43617753Smckusick }
43717753Smckusick 
43817753Smckusick /*
43911127Smckusick  * Set the mode, owner, and times for all new or changed directories
44011127Smckusick  */
44111992Smckusick setdirmodes()
44211127Smckusick {
44311127Smckusick 	FILE *mf;
44411127Smckusick 	struct modeinfo node;
44511127Smckusick 	struct entry *ep;
44611127Smckusick 	char *cp;
44711127Smckusick 
44811127Smckusick 	vprintf(stdout, "Set directory mode, owner, and times.\n");
44915779Smckusick 	(void) sprintf(modefile, "/tmp/rstmode%d", dumpdate);
45011127Smckusick 	mf = fopen(modefile, "r");
45111127Smckusick 	if (mf == NULL) {
45211127Smckusick 		perror("fopen");
45315779Smckusick 		fprintf(stderr, "cannot open mode file %s\n", modefile);
45415779Smckusick 		fprintf(stderr, "directory mode, owner, and times not set\n");
45515779Smckusick 		return;
45611127Smckusick 	}
45711127Smckusick 	clearerr(mf);
45811309Smckusick 	for (;;) {
45911732Smckusick 		(void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
46011309Smckusick 		if (feof(mf))
46111309Smckusick 			break;
46211127Smckusick 		ep = lookupino(node.ino);
46314453Smckusick 		if (command == 'i' || command == 'x') {
46414453Smckusick 			if (ep == NIL)
46511309Smckusick 				continue;
46618008Smckusick 			if (ep->e_flags & EXISTED) {
46718008Smckusick 				ep->e_flags &= ~NEW;
46818008Smckusick 				continue;
46918008Smckusick 			}
47014453Smckusick 			if (node.ino == ROOTINO &&
47114453Smckusick 		   	    reply("set owner/mode for '.'") == FAIL)
47214453Smckusick 				continue;
47314453Smckusick 		}
47414453Smckusick 		if (ep == NIL)
47511127Smckusick 			panic("cannot find directory inode %d\n", node.ino);
47611127Smckusick 		cp = myname(ep);
47711732Smckusick 		(void) chown(cp, node.uid, node.gid);
47811732Smckusick 		(void) chmod(cp, node.mode);
47911127Smckusick 		utime(cp, node.timep);
48011992Smckusick 		ep->e_flags &= ~NEW;
48111127Smckusick 	}
48211127Smckusick 	if (ferror(mf))
48311127Smckusick 		panic("error setting directory modes\n");
48411732Smckusick 	(void) fclose(mf);
48511127Smckusick }
48611127Smckusick 
48711127Smckusick /*
48811127Smckusick  * Generate a literal copy of a directory.
48911127Smckusick  */
49011127Smckusick genliteraldir(name, ino)
49111127Smckusick 	char *name;
49211127Smckusick 	ino_t ino;
49311127Smckusick {
49411127Smckusick 	register struct inotab *itp;
49511127Smckusick 	int ofile, dp, i, size;
49611127Smckusick 	char buf[BUFSIZ];
49711127Smckusick 
49811127Smckusick 	itp = inotablookup(ino);
49911127Smckusick 	if (itp == NULL)
50011322Smckusick 		panic("Cannot find directory inode %d named %s\n", ino, name);
50112893Ssam 	if ((ofile = creat(name, 0666)) < 0) {
50212556Smckusick 		fprintf(stderr, "%s: ", name);
50312556Smckusick 		(void) fflush(stderr);
50412556Smckusick 		perror("cannot create file");
50511127Smckusick 		return (FAIL);
50611127Smckusick 	}
50712556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
50811127Smckusick 	dp = dup(dirp->dd_fd);
50911127Smckusick 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
51011127Smckusick 		size = i < BUFSIZ ? i : BUFSIZ;
51111127Smckusick 		if (read(dp, buf, (int) size) == -1) {
51211127Smckusick 			fprintf(stderr,
51311127Smckusick 				"write error extracting inode %d, name %s\n",
51411127Smckusick 				curfile.ino, curfile.name);
51511127Smckusick 			perror("read");
51611127Smckusick 			done(1);
51711127Smckusick 		}
518*34268Smckusick 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
51911127Smckusick 			fprintf(stderr,
52011127Smckusick 				"write error extracting inode %d, name %s\n",
52111127Smckusick 				curfile.ino, curfile.name);
52211127Smckusick 			perror("write");
52311127Smckusick 			done(1);
52411127Smckusick 		}
52511127Smckusick 	}
52611732Smckusick 	(void) close(dp);
52711732Smckusick 	(void) close(ofile);
52811127Smckusick 	return (GOOD);
52911127Smckusick }
53011127Smckusick 
53111127Smckusick /*
53211992Smckusick  * Determine the type of an inode
53311992Smckusick  */
53411992Smckusick inodetype(ino)
53511992Smckusick 	ino_t ino;
53611992Smckusick {
53711992Smckusick 	struct inotab *itp;
53811992Smckusick 
53911992Smckusick 	itp = inotablookup(ino);
54011992Smckusick 	if (itp == NULL)
54111992Smckusick 		return (LEAF);
54211992Smckusick 	return (NODE);
54311992Smckusick }
54411992Smckusick 
54511992Smckusick /*
54611127Smckusick  * Allocate and initialize a directory inode entry.
54711127Smckusick  * If requested, save its pertinent mode, owner, and time info.
54811127Smckusick  */
54911322Smckusick struct inotab *
55011127Smckusick allocinotab(ino, dip, seekpt)
55111127Smckusick 	ino_t ino;
55211127Smckusick 	struct dinode *dip;
55311127Smckusick 	daddr_t seekpt;
55411127Smckusick {
55511127Smckusick 	register struct inotab	*itp;
55611127Smckusick 	struct modeinfo node;
55711127Smckusick 
55811127Smckusick 	itp = (struct inotab *)calloc(1, sizeof(struct inotab));
55913859Smckusick 	if (itp == 0)
56013859Smckusick 		panic("no memory directory table\n");
56111127Smckusick 	itp->t_next = inotab[INOHASH(ino)];
56211127Smckusick 	inotab[INOHASH(ino)] = itp;
56311127Smckusick 	itp->t_ino = ino;
56411127Smckusick 	itp->t_seekpt = seekpt;
56511127Smckusick 	if (mf == NULL)
56611322Smckusick 		return(itp);
56711127Smckusick 	node.ino = ino;
56811127Smckusick 	node.timep[0] = dip->di_atime;
56911127Smckusick 	node.timep[1] = dip->di_mtime;
57011127Smckusick 	node.mode = dip->di_mode;
57111127Smckusick 	node.uid = dip->di_uid;
57211127Smckusick 	node.gid = dip->di_gid;
57311732Smckusick 	(void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
57411322Smckusick 	return(itp);
57511127Smckusick }
57611127Smckusick 
57711127Smckusick /*
57811127Smckusick  * Look up an inode in the table of directories
57911127Smckusick  */
58011127Smckusick struct inotab *
58111127Smckusick inotablookup(ino)
58211127Smckusick 	ino_t	ino;
58311127Smckusick {
58411127Smckusick 	register struct inotab *itp;
58511127Smckusick 
58611127Smckusick 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
58711127Smckusick 		if (itp->t_ino == ino)
58811127Smckusick 			return(itp);
58911127Smckusick 	return ((struct inotab *)0);
59011127Smckusick }
59111127Smckusick 
59211127Smckusick /*
59311127Smckusick  * Clean up and exit
59411127Smckusick  */
59511127Smckusick done(exitcode)
59611127Smckusick 	int exitcode;
59711127Smckusick {
59811127Smckusick 
59911127Smckusick 	closemt();
60011992Smckusick 	if (modefile[0] != '#')
60111992Smckusick 		(void) unlink(modefile);
60211992Smckusick 	if (dirfile[0] != '#')
60311992Smckusick 		(void) unlink(dirfile);
60411127Smckusick 	exit(exitcode);
60511127Smckusick }
606