xref: /csrg-svn/sbin/restore/dirs.c (revision 46564)
121165Sdist /*
236105Sbostic  * Copyright (c) 1983 The Regents of the University of California.
336105Sbostic  * All rights reserved.
436105Sbostic  *
542708Sbostic  * %sccs.include.redist.c%
621165Sdist  */
721165Sdist 
811127Smckusick #ifndef lint
9*46564Smckusick static char sccsid[] = "@(#)dirs.c	5.13 (Berkeley) 02/22/91";
1036105Sbostic #endif /* not lint */
1111127Smckusick 
1211127Smckusick #include "restore.h"
1323545Smckusick #include <protocols/dumprestore.h>
1411309Smckusick #include <sys/file.h>
1540094Smckusick #include <ufs/dir.h>
1637952Sbostic #include "pathnames.h"
1711127Smckusick 
1811992Smckusick /*
1911992Smckusick  * Symbol table of directories read from tape.
2011992Smckusick  */
2111127Smckusick #define HASHSIZE	1000
2211127Smckusick #define INOHASH(val) (val % HASHSIZE)
2311127Smckusick struct inotab {
2411127Smckusick 	struct inotab *t_next;
2511127Smckusick 	ino_t	t_ino;
2611127Smckusick 	daddr_t	t_seekpt;
2711127Smckusick 	long t_size;
2811309Smckusick };
2911309Smckusick static struct inotab *inotab[HASHSIZE];
3011309Smckusick extern struct inotab *inotablookup();
3111322Smckusick extern struct inotab *allocinotab();
3211127Smckusick 
3311992Smckusick /*
3411992Smckusick  * Information retained about directories.
3511992Smckusick  */
3611127Smckusick struct modeinfo {
3711127Smckusick 	ino_t ino;
3839471Smckusick 	struct timeval timep[2];
3911127Smckusick 	short mode;
4011127Smckusick 	short uid;
4111127Smckusick 	short gid;
4211127Smckusick };
4311127Smckusick 
4411992Smckusick /*
4540094Smckusick  * Definitions for library routines operating on directories.
4640094Smckusick  */
47*46564Smckusick #undef DIRBLKSIZ
48*46564Smckusick #define DIRBLKSIZ 1024
4940094Smckusick struct dirdesc {
5040094Smckusick 	int	dd_fd;
5140094Smckusick 	long	dd_loc;
5240094Smckusick 	long	dd_size;
5340094Smckusick 	char	dd_buf[DIRBLKSIZ];
5440094Smckusick };
5540094Smckusick extern DIR *opendirfile();
5640094Smckusick extern long rst_telldir();
5740094Smckusick extern void rst_seekdir();
5840094Smckusick 
5940094Smckusick /*
6011992Smckusick  * Global variables for this file.
6111992Smckusick  */
6211309Smckusick static daddr_t	seekpt;
6311309Smckusick static FILE	*df, *mf;
6411309Smckusick static DIR	*dirp;
6511992Smckusick static char	dirfile[32] = "#";	/* No file */
6611992Smckusick static char	modefile[32] = "#";	/* No file */
6711309Smckusick extern ino_t	search();
6812556Smckusick struct direct 	*rst_readdir();
6912556Smckusick extern void 	rst_seekdir();
7011127Smckusick 
7111992Smckusick /*
7211992Smckusick  * Format of old style directories.
7311992Smckusick  */
7411127Smckusick #define ODIRSIZ 14
7511127Smckusick struct odirect {
7611127Smckusick 	u_short	d_ino;
7711127Smckusick 	char	d_name[ODIRSIZ];
7811127Smckusick };
7911127Smckusick 
8011127Smckusick /*
8111127Smckusick  *	Extract directory contents, building up a directory structure
8211127Smckusick  *	on disk for extraction by name.
8311992Smckusick  *	If genmode is requested, save mode, owner, and times for all
8411127Smckusick  *	directories on the tape.
8511127Smckusick  */
8611992Smckusick extractdirs(genmode)
8711992Smckusick 	int genmode;
8811127Smckusick {
8911127Smckusick 	register int i;
9011127Smckusick 	register struct dinode *ip;
9111322Smckusick 	struct inotab *itp;
9211127Smckusick 	struct direct nulldir;
9311127Smckusick 	int putdir(), null();
9411127Smckusick 
9511127Smckusick 	vprintf(stdout, "Extract directories from tape\n");
9637952Sbostic 	(void) sprintf(dirfile, "%s/rstdir%d", _PATH_TMP, dumpdate);
9711127Smckusick 	df = fopen(dirfile, "w");
9811127Smckusick 	if (df == 0) {
9911127Smckusick 		fprintf(stderr,
10015779Smckusick 		    "restore: %s - cannot create directory temporary\n",
10111127Smckusick 		    dirfile);
10211127Smckusick 		perror("fopen");
10311127Smckusick 		done(1);
10411127Smckusick 	}
10511992Smckusick 	if (genmode != 0) {
10637952Sbostic 		(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
10711127Smckusick 		mf = fopen(modefile, "w");
10811309Smckusick 		if (mf == 0) {
10911127Smckusick 			fprintf(stderr,
11015779Smckusick 			    "restore: %s - cannot create modefile \n",
11111127Smckusick 			    modefile);
11211127Smckusick 			perror("fopen");
11311127Smckusick 			done(1);
11411127Smckusick 		}
11511127Smckusick 	}
11611322Smckusick 	nulldir.d_ino = 0;
11711127Smckusick 	nulldir.d_namlen = 1;
11812453Smckusick 	(void) strcpy(nulldir.d_name, "/");
11911127Smckusick 	nulldir.d_reclen = DIRSIZ(&nulldir);
12011127Smckusick 	for (;;) {
12111127Smckusick 		curfile.name = "<directory file - name unknown>";
12211127Smckusick 		curfile.action = USING;
12311127Smckusick 		ip = curfile.dip;
12417948Smckusick 		if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
12511732Smckusick 			(void) fclose(df);
12640064Smckusick 			dirp = opendirfile(dirfile);
12711127Smckusick 			if (dirp == NULL)
12840064Smckusick 				perror("opendirfile");
12911127Smckusick 			if (mf != NULL)
13011732Smckusick 				(void) fclose(mf);
13111992Smckusick 			i = dirlookup(".");
13211992Smckusick 			if (i == 0)
13311421Smckusick 				panic("Root directory is not on tape\n");
13411127Smckusick 			return;
13511127Smckusick 		}
13611322Smckusick 		itp = allocinotab(curfile.ino, ip, seekpt);
13711127Smckusick 		getfile(putdir, null);
13811127Smckusick 		putent(&nulldir);
13911127Smckusick 		flushent();
14011322Smckusick 		itp->t_size = seekpt - itp->t_seekpt;
14111127Smckusick 	}
14211127Smckusick }
14311127Smckusick 
14411127Smckusick /*
14511322Smckusick  * skip over all the directories on the tape
14611322Smckusick  */
14711322Smckusick skipdirs()
14811322Smckusick {
14911322Smckusick 
15011322Smckusick 	while ((curfile.dip->di_mode & IFMT) == IFDIR) {
15111322Smckusick 		skipfile();
15211322Smckusick 	}
15311322Smckusick }
15411322Smckusick 
15511322Smckusick /*
15611127Smckusick  *	Recursively find names and inumbers of all files in subtree
15711127Smckusick  *	pname and pass them off to be processed.
15811127Smckusick  */
15911127Smckusick treescan(pname, ino, todo)
16011127Smckusick 	char *pname;
16111127Smckusick 	ino_t ino;
16211744Smckusick 	long (*todo)();
16311127Smckusick {
16411127Smckusick 	register struct inotab *itp;
16512453Smckusick 	register struct direct *dp;
16612453Smckusick 	register struct entry *np;
16711127Smckusick 	int namelen;
16811127Smckusick 	daddr_t bpt;
16940064Smckusick 	off_t rst_telldir();
17011644Smckusick 	char locname[MAXPATHLEN + 1];
17111127Smckusick 
17211127Smckusick 	itp = inotablookup(ino);
17311127Smckusick 	if (itp == NULL) {
17411127Smckusick 		/*
17511127Smckusick 		 * Pname is name of a simple file or an unchanged directory.
17611127Smckusick 		 */
17711744Smckusick 		(void) (*todo)(pname, ino, LEAF);
17811127Smckusick 		return;
17911127Smckusick 	}
18011127Smckusick 	/*
18111127Smckusick 	 * Pname is a dumped directory name.
18211127Smckusick 	 */
18311744Smckusick 	if ((*todo)(pname, ino, NODE) == FAIL)
18411744Smckusick 		return;
18511127Smckusick 	/*
18611127Smckusick 	 * begin search through the directory
18711127Smckusick 	 * skipping over "." and ".."
18811127Smckusick 	 */
18911992Smckusick 	(void) strncpy(locname, pname, MAXPATHLEN);
19011992Smckusick 	(void) strncat(locname, "/", MAXPATHLEN);
19111127Smckusick 	namelen = strlen(locname);
19212556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
19312556Smckusick 	dp = rst_readdir(dirp); /* "." */
19427263Smckusick 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
19512556Smckusick 		dp = rst_readdir(dirp); /* ".." */
19627263Smckusick 	else
19727263Smckusick 		fprintf(stderr, "Warning: `.' missing from directory %s\n",
19827263Smckusick 			pname);
19927263Smckusick 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
20012556Smckusick 		dp = rst_readdir(dirp); /* first real entry */
20127263Smckusick 	else
20227263Smckusick 		fprintf(stderr, "Warning: `..' missing from directory %s\n",
20327263Smckusick 			pname);
20440064Smckusick 	bpt = rst_telldir(dirp);
20511127Smckusick 	/*
20612453Smckusick 	 * a zero inode signals end of directory
20711127Smckusick 	 */
20812453Smckusick 	while (dp != NULL && dp->d_ino != 0) {
20911127Smckusick 		locname[namelen] = '\0';
21011644Smckusick 		if (namelen + dp->d_namlen >= MAXPATHLEN) {
21111127Smckusick 			fprintf(stderr, "%s%s: name exceeds %d char\n",
21211644Smckusick 				locname, dp->d_name, MAXPATHLEN);
21311127Smckusick 		} else {
21411992Smckusick 			(void) strncat(locname, dp->d_name, (int)dp->d_namlen);
21511127Smckusick 			treescan(locname, dp->d_ino, todo);
21612556Smckusick 			rst_seekdir(dirp, bpt, itp->t_seekpt);
21711127Smckusick 		}
21812556Smckusick 		dp = rst_readdir(dirp);
21940064Smckusick 		bpt = rst_telldir(dirp);
22011127Smckusick 	}
22111127Smckusick 	if (dp == NULL)
22211127Smckusick 		fprintf(stderr, "corrupted directory: %s.\n", locname);
22311127Smckusick }
22411127Smckusick 
22511127Smckusick /*
22611127Smckusick  * Search the directory tree rooted at inode ROOTINO
22711127Smckusick  * for the path pointed at by n
22811127Smckusick  */
22911127Smckusick ino_t
23011127Smckusick psearch(n)
23111127Smckusick 	char	*n;
23211127Smckusick {
23311127Smckusick 	register char *cp, *cp1;
23411127Smckusick 	ino_t ino;
23511127Smckusick 	char c;
23611127Smckusick 
23711127Smckusick 	ino = ROOTINO;
23811127Smckusick 	if (*(cp = n) == '/')
23911127Smckusick 		cp++;
24011127Smckusick next:
24111127Smckusick 	cp1 = cp + 1;
24211127Smckusick 	while (*cp1 != '/' && *cp1)
24311127Smckusick 		cp1++;
24411127Smckusick 	c = *cp1;
24511127Smckusick 	*cp1 = 0;
24611127Smckusick 	ino = search(ino, cp);
24711127Smckusick 	if (ino == 0) {
24811127Smckusick 		*cp1 = c;
24911127Smckusick 		return(0);
25011127Smckusick 	}
25111127Smckusick 	*cp1 = c;
25211127Smckusick 	if (c == '/') {
25311127Smckusick 		cp = cp1+1;
25411127Smckusick 		goto next;
25511127Smckusick 	}
25611127Smckusick 	return(ino);
25711127Smckusick }
25811127Smckusick 
25911127Smckusick /*
26011127Smckusick  * search the directory inode ino
26111127Smckusick  * looking for entry cp
26211127Smckusick  */
26311127Smckusick ino_t
26411127Smckusick search(inum, cp)
26511127Smckusick 	ino_t	inum;
26611127Smckusick 	char	*cp;
26711127Smckusick {
26811127Smckusick 	register struct direct *dp;
26911127Smckusick 	register struct inotab *itp;
27011127Smckusick 	int len;
27111127Smckusick 
27211127Smckusick 	itp = inotablookup(inum);
27311127Smckusick 	if (itp == NULL)
27411127Smckusick 		return(0);
27512556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
27611127Smckusick 	len = strlen(cp);
27711127Smckusick 	do {
27812556Smckusick 		dp = rst_readdir(dirp);
27912453Smckusick 		if (dp == NULL || dp->d_ino == 0)
28012453Smckusick 			return (0);
28111992Smckusick 	} while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0);
28211127Smckusick 	return(dp->d_ino);
28311127Smckusick }
28411127Smckusick 
28511127Smckusick /*
28611127Smckusick  * Put the directory entries in the directory file
28711127Smckusick  */
28811127Smckusick putdir(buf, size)
28911127Smckusick 	char *buf;
29011127Smckusick 	int size;
29111127Smckusick {
29211127Smckusick 	struct direct cvtbuf;
29311127Smckusick 	register struct odirect *odp;
29411127Smckusick 	struct odirect *eodp;
29511127Smckusick 	register struct direct *dp;
29611127Smckusick 	long loc, i;
29726941Ssklower 	extern int Bcvt;
29811127Smckusick 
29911127Smckusick 	if (cvtflag) {
30011127Smckusick 		eodp = (struct odirect *)&buf[size];
30111127Smckusick 		for (odp = (struct odirect *)buf; odp < eodp; odp++)
30211127Smckusick 			if (odp->d_ino != 0) {
30311127Smckusick 				dcvt(odp, &cvtbuf);
30411127Smckusick 				putent(&cvtbuf);
30511127Smckusick 			}
30611127Smckusick 	} else {
30711127Smckusick 		for (loc = 0; loc < size; ) {
30811127Smckusick 			dp = (struct direct *)(buf + loc);
30926941Ssklower 			if (Bcvt) {
31026941Ssklower 				swabst("l2s", (char *) dp);
31126941Ssklower 			}
31211127Smckusick 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
313*46564Smckusick 			if ((dp->d_reclen & 0x3) != 0 ||
314*46564Smckusick 			    dp->d_reclen > i ||
315*46564Smckusick 			    dp->d_reclen < DIRSIZ(dp) ||
316*46564Smckusick 			    dp->d_namlen > MAXNAMLEN) {
317*46564Smckusick 				vprintf(stdout, "Mangled directory\n");
31811127Smckusick 				loc += i;
31911127Smckusick 				continue;
32011127Smckusick 			}
32111127Smckusick 			loc += dp->d_reclen;
32211127Smckusick 			if (dp->d_ino != 0) {
32311127Smckusick 				putent(dp);
32411127Smckusick 			}
32511127Smckusick 		}
32611127Smckusick 	}
32711127Smckusick }
32811127Smckusick 
32911127Smckusick /*
33011127Smckusick  * These variables are "local" to the following two functions.
33111127Smckusick  */
33211127Smckusick char dirbuf[DIRBLKSIZ];
33311127Smckusick long dirloc = 0;
33411127Smckusick long prev = 0;
33511127Smckusick 
33611127Smckusick /*
33711127Smckusick  * add a new directory entry to a file.
33811127Smckusick  */
33911127Smckusick putent(dp)
34011127Smckusick 	struct direct *dp;
34111127Smckusick {
34211322Smckusick 	dp->d_reclen = DIRSIZ(dp);
34311127Smckusick 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
34411127Smckusick 		((struct direct *)(dirbuf + prev))->d_reclen =
34511127Smckusick 		    DIRBLKSIZ - prev;
34611732Smckusick 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
34711127Smckusick 		dirloc = 0;
34811127Smckusick 	}
34911127Smckusick 	bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen);
35011127Smckusick 	prev = dirloc;
35111127Smckusick 	dirloc += dp->d_reclen;
35211127Smckusick }
35311127Smckusick 
35411127Smckusick /*
35511127Smckusick  * flush out a directory that is finished.
35611127Smckusick  */
35711127Smckusick flushent()
35811127Smckusick {
35911127Smckusick 
36011127Smckusick 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
36111732Smckusick 	(void) fwrite(dirbuf, (int)dirloc, 1, df);
36211127Smckusick 	seekpt = ftell(df);
36311127Smckusick 	dirloc = 0;
36411127Smckusick }
36511127Smckusick 
36611127Smckusick dcvt(odp, ndp)
36711127Smckusick 	register struct odirect *odp;
36811127Smckusick 	register struct direct *ndp;
36911127Smckusick {
37011127Smckusick 
37111127Smckusick 	bzero((char *)ndp, (long)(sizeof *ndp));
37211127Smckusick 	ndp->d_ino =  odp->d_ino;
37311992Smckusick 	(void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
37411127Smckusick 	ndp->d_namlen = strlen(ndp->d_name);
37511127Smckusick 	ndp->d_reclen = DIRSIZ(ndp);
37611127Smckusick }
37711127Smckusick 
37811127Smckusick /*
37911127Smckusick  * Seek to an entry in a directory.
38040064Smckusick  * Only values returned by rst_telldir should be passed to rst_seekdir.
38111732Smckusick  * This routine handles many directories in a single file.
38211732Smckusick  * It takes the base of the directory in the file, plus
38311732Smckusick  * the desired seek offset into it.
38411127Smckusick  */
38511127Smckusick void
38612556Smckusick rst_seekdir(dirp, loc, base)
38711127Smckusick 	register DIR *dirp;
38811127Smckusick 	daddr_t loc, base;
38911127Smckusick {
39040064Smckusick 	off_t rst_telldir();
39111127Smckusick 
39240064Smckusick 	if (loc == rst_telldir(dirp))
39311127Smckusick 		return;
39411127Smckusick 	loc -= base;
39511127Smckusick 	if (loc < 0)
39612556Smckusick 		fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
39711127Smckusick 	(void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
39811127Smckusick 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
39911127Smckusick 	if (dirp->dd_loc != 0)
40011127Smckusick 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
40111127Smckusick }
40211127Smckusick 
40311127Smckusick /*
40411127Smckusick  * get next entry in a directory.
40511127Smckusick  */
40611127Smckusick struct direct *
40712556Smckusick rst_readdir(dirp)
40811127Smckusick 	register DIR *dirp;
40911127Smckusick {
41011127Smckusick 	register struct direct *dp;
41111127Smckusick 
41211127Smckusick 	for (;;) {
41311127Smckusick 		if (dirp->dd_loc == 0) {
41411127Smckusick 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
41511127Smckusick 			    DIRBLKSIZ);
41612453Smckusick 			if (dirp->dd_size <= 0) {
41712453Smckusick 				dprintf(stderr, "error reading directory\n");
41811127Smckusick 				return NULL;
41912453Smckusick 			}
42011127Smckusick 		}
42111127Smckusick 		if (dirp->dd_loc >= dirp->dd_size) {
42211127Smckusick 			dirp->dd_loc = 0;
42311127Smckusick 			continue;
42411127Smckusick 		}
42511127Smckusick 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
42611127Smckusick 		if (dp->d_reclen == 0 ||
42712453Smckusick 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
42812453Smckusick 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
42912453Smckusick 				dp->d_reclen);
43011127Smckusick 			return NULL;
43112453Smckusick 		}
43211127Smckusick 		dirp->dd_loc += dp->d_reclen;
43312453Smckusick 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
43412453Smckusick 			continue;
43512556Smckusick 		if (dp->d_ino >= maxino) {
43612453Smckusick 			dprintf(stderr, "corrupted directory: bad inum %d\n",
43712453Smckusick 				dp->d_ino);
43812453Smckusick 			continue;
43912453Smckusick 		}
44011127Smckusick 		return (dp);
44111127Smckusick 	}
44211127Smckusick }
44311127Smckusick 
44411127Smckusick /*
44517753Smckusick  * Simulate the opening of a directory
44617753Smckusick  */
44717753Smckusick DIR *
44817753Smckusick rst_opendir(name)
44917753Smckusick 	char *name;
45017753Smckusick {
45117753Smckusick 	struct inotab *itp;
45217753Smckusick 	ino_t ino;
45317753Smckusick 
45417753Smckusick 	if ((ino = dirlookup(name)) > 0 &&
45517753Smckusick 	    (itp = inotablookup(ino)) != NULL) {
45617753Smckusick 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
45717753Smckusick 		return (dirp);
45817753Smckusick 	}
45917753Smckusick 	return (0);
46017753Smckusick }
46117753Smckusick 
46217753Smckusick /*
46340064Smckusick  * Simulate finding the current offset in the directory.
46440064Smckusick  */
46540064Smckusick off_t
46640064Smckusick rst_telldir(dirp)
46740064Smckusick 	DIR *dirp;
46840064Smckusick {
46940064Smckusick 	off_t lseek();
47040064Smckusick 
47140064Smckusick 	return (lseek(dirp->dd_fd, 0L, 1) - dirp->dd_size + dirp->dd_loc);
47240064Smckusick }
47340064Smckusick 
47440064Smckusick /*
47540064Smckusick  * Open a directory file.
47640064Smckusick  */
47740064Smckusick DIR *
47840064Smckusick opendirfile(name)
47940064Smckusick 	char *name;
48040064Smckusick {
48140064Smckusick 	register DIR *dirp;
48240064Smckusick 	register int fd;
48340064Smckusick 
48440064Smckusick 	if ((fd = open(name, 0)) == -1)
48540064Smckusick 		return NULL;
48640064Smckusick 	if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
48740064Smckusick 		close (fd);
48840064Smckusick 		return NULL;
48940064Smckusick 	}
49040064Smckusick 	dirp->dd_fd = fd;
49140064Smckusick 	dirp->dd_loc = 0;
49240064Smckusick 	return dirp;
49340064Smckusick }
49440064Smckusick 
49540064Smckusick /*
49611127Smckusick  * Set the mode, owner, and times for all new or changed directories
49711127Smckusick  */
49811992Smckusick setdirmodes()
49911127Smckusick {
50011127Smckusick 	FILE *mf;
50111127Smckusick 	struct modeinfo node;
50211127Smckusick 	struct entry *ep;
50311127Smckusick 	char *cp;
50411127Smckusick 
50511127Smckusick 	vprintf(stdout, "Set directory mode, owner, and times.\n");
50637952Sbostic 	(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
50711127Smckusick 	mf = fopen(modefile, "r");
50811127Smckusick 	if (mf == NULL) {
50911127Smckusick 		perror("fopen");
51015779Smckusick 		fprintf(stderr, "cannot open mode file %s\n", modefile);
51115779Smckusick 		fprintf(stderr, "directory mode, owner, and times not set\n");
51215779Smckusick 		return;
51311127Smckusick 	}
51411127Smckusick 	clearerr(mf);
51511309Smckusick 	for (;;) {
51611732Smckusick 		(void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
51711309Smckusick 		if (feof(mf))
51811309Smckusick 			break;
51911127Smckusick 		ep = lookupino(node.ino);
52014453Smckusick 		if (command == 'i' || command == 'x') {
52114453Smckusick 			if (ep == NIL)
52211309Smckusick 				continue;
52318008Smckusick 			if (ep->e_flags & EXISTED) {
52418008Smckusick 				ep->e_flags &= ~NEW;
52518008Smckusick 				continue;
52618008Smckusick 			}
52714453Smckusick 			if (node.ino == ROOTINO &&
52814453Smckusick 		   	    reply("set owner/mode for '.'") == FAIL)
52914453Smckusick 				continue;
53014453Smckusick 		}
53142862Smckusick 		if (ep == NIL) {
53211127Smckusick 			panic("cannot find directory inode %d\n", node.ino);
53342862Smckusick 		} else {
53442862Smckusick 			cp = myname(ep);
53542862Smckusick 			(void) chown(cp, node.uid, node.gid);
53642862Smckusick 			(void) chmod(cp, node.mode);
53742862Smckusick 			utimes(cp, node.timep);
53842862Smckusick 			ep->e_flags &= ~NEW;
53942862Smckusick 		}
54011127Smckusick 	}
54111127Smckusick 	if (ferror(mf))
54211127Smckusick 		panic("error setting directory modes\n");
54311732Smckusick 	(void) fclose(mf);
54411127Smckusick }
54511127Smckusick 
54611127Smckusick /*
54711127Smckusick  * Generate a literal copy of a directory.
54811127Smckusick  */
54911127Smckusick genliteraldir(name, ino)
55011127Smckusick 	char *name;
55111127Smckusick 	ino_t ino;
55211127Smckusick {
55311127Smckusick 	register struct inotab *itp;
55411127Smckusick 	int ofile, dp, i, size;
55511127Smckusick 	char buf[BUFSIZ];
55611127Smckusick 
55711127Smckusick 	itp = inotablookup(ino);
55811127Smckusick 	if (itp == NULL)
55911322Smckusick 		panic("Cannot find directory inode %d named %s\n", ino, name);
56012893Ssam 	if ((ofile = creat(name, 0666)) < 0) {
56112556Smckusick 		fprintf(stderr, "%s: ", name);
56212556Smckusick 		(void) fflush(stderr);
56312556Smckusick 		perror("cannot create file");
56411127Smckusick 		return (FAIL);
56511127Smckusick 	}
56612556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
56711127Smckusick 	dp = dup(dirp->dd_fd);
56811127Smckusick 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
56911127Smckusick 		size = i < BUFSIZ ? i : BUFSIZ;
57011127Smckusick 		if (read(dp, buf, (int) size) == -1) {
57111127Smckusick 			fprintf(stderr,
57211127Smckusick 				"write error extracting inode %d, name %s\n",
57311127Smckusick 				curfile.ino, curfile.name);
57411127Smckusick 			perror("read");
57511127Smckusick 			done(1);
57611127Smckusick 		}
57734268Smckusick 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
57811127Smckusick 			fprintf(stderr,
57911127Smckusick 				"write error extracting inode %d, name %s\n",
58011127Smckusick 				curfile.ino, curfile.name);
58111127Smckusick 			perror("write");
58211127Smckusick 			done(1);
58311127Smckusick 		}
58411127Smckusick 	}
58511732Smckusick 	(void) close(dp);
58611732Smckusick 	(void) close(ofile);
58711127Smckusick 	return (GOOD);
58811127Smckusick }
58911127Smckusick 
59011127Smckusick /*
59111992Smckusick  * Determine the type of an inode
59211992Smckusick  */
59311992Smckusick inodetype(ino)
59411992Smckusick 	ino_t ino;
59511992Smckusick {
59611992Smckusick 	struct inotab *itp;
59711992Smckusick 
59811992Smckusick 	itp = inotablookup(ino);
59911992Smckusick 	if (itp == NULL)
60011992Smckusick 		return (LEAF);
60111992Smckusick 	return (NODE);
60211992Smckusick }
60311992Smckusick 
60411992Smckusick /*
60511127Smckusick  * Allocate and initialize a directory inode entry.
60611127Smckusick  * If requested, save its pertinent mode, owner, and time info.
60711127Smckusick  */
60811322Smckusick struct inotab *
60911127Smckusick allocinotab(ino, dip, seekpt)
61011127Smckusick 	ino_t ino;
61111127Smckusick 	struct dinode *dip;
61211127Smckusick 	daddr_t seekpt;
61311127Smckusick {
61411127Smckusick 	register struct inotab	*itp;
61511127Smckusick 	struct modeinfo node;
61611127Smckusick 
61711127Smckusick 	itp = (struct inotab *)calloc(1, sizeof(struct inotab));
61813859Smckusick 	if (itp == 0)
61913859Smckusick 		panic("no memory directory table\n");
62011127Smckusick 	itp->t_next = inotab[INOHASH(ino)];
62111127Smckusick 	inotab[INOHASH(ino)] = itp;
62211127Smckusick 	itp->t_ino = ino;
62311127Smckusick 	itp->t_seekpt = seekpt;
62411127Smckusick 	if (mf == NULL)
62511322Smckusick 		return(itp);
62611127Smckusick 	node.ino = ino;
62739471Smckusick 	node.timep[0].tv_sec = dip->di_atime;
62839471Smckusick 	node.timep[0].tv_usec = 0;
62939471Smckusick 	node.timep[1].tv_sec = dip->di_mtime;
63039471Smckusick 	node.timep[1].tv_usec = 0;
63111127Smckusick 	node.mode = dip->di_mode;
63211127Smckusick 	node.uid = dip->di_uid;
63311127Smckusick 	node.gid = dip->di_gid;
63411732Smckusick 	(void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
63511322Smckusick 	return(itp);
63611127Smckusick }
63711127Smckusick 
63811127Smckusick /*
63911127Smckusick  * Look up an inode in the table of directories
64011127Smckusick  */
64111127Smckusick struct inotab *
64211127Smckusick inotablookup(ino)
64311127Smckusick 	ino_t	ino;
64411127Smckusick {
64511127Smckusick 	register struct inotab *itp;
64611127Smckusick 
64711127Smckusick 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
64811127Smckusick 		if (itp->t_ino == ino)
64911127Smckusick 			return(itp);
65011127Smckusick 	return ((struct inotab *)0);
65111127Smckusick }
65211127Smckusick 
65311127Smckusick /*
65411127Smckusick  * Clean up and exit
65511127Smckusick  */
65611127Smckusick done(exitcode)
65711127Smckusick 	int exitcode;
65811127Smckusick {
65911127Smckusick 
66011127Smckusick 	closemt();
66111992Smckusick 	if (modefile[0] != '#')
66211992Smckusick 		(void) unlink(modefile);
66311992Smckusick 	if (dirfile[0] != '#')
66411992Smckusick 		(void) unlink(dirfile);
66511127Smckusick 	exit(exitcode);
66611127Smckusick }
667