xref: /csrg-svn/sbin/restore/dirs.c (revision 55880)
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*55880Smckusick static char sccsid[] = "@(#)dirs.c	5.22 (Berkeley) 08/09/92";
1036105Sbostic #endif /* not lint */
1111127Smckusick 
1211127Smckusick #include "restore.h"
1323545Smckusick #include <protocols/dumprestore.h>
1411309Smckusick #include <sys/file.h>
1551625Sbostic #include <ufs/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 {
2450662Smckusick 	struct	inotab *t_next;
2511127Smckusick 	ino_t	t_ino;
2650662Smckusick 	long	t_seekpt;
2750662Smckusick 	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  */
4746564Smckusick #undef DIRBLKSIZ
4846564Smckusick #define DIRBLKSIZ 1024
4950657Smckusick struct rstdirdesc {
5040094Smckusick 	int	dd_fd;
5140094Smckusick 	long	dd_loc;
5240094Smckusick 	long	dd_size;
5340094Smckusick 	char	dd_buf[DIRBLKSIZ];
5440094Smckusick };
5550657Smckusick extern RST_DIR *opendirfile();
5650662Smckusick extern long rst_telldir();
5740094Smckusick extern void rst_seekdir();
5840094Smckusick 
5940094Smckusick /*
6011992Smckusick  * Global variables for this file.
6111992Smckusick  */
6250662Smckusick static long	seekpt;
6311309Smckusick static FILE	*df, *mf;
6450657Smckusick static RST_DIR	*dirp;
6511992Smckusick static char	dirfile[32] = "#";	/* No file */
6611992Smckusick static char	modefile[32] = "#";	/* No file */
6746566Smckusick static char	dot[2] = ".";		/* So it can be modified */
6811309Smckusick extern ino_t	search();
6912556Smckusick struct direct 	*rst_readdir();
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;
11754582Smckusick 	nulldir.d_type = DT_DIR;
11811127Smckusick 	nulldir.d_namlen = 1;
11912453Smckusick 	(void) strcpy(nulldir.d_name, "/");
12054582Smckusick 	nulldir.d_reclen = DIRSIZ(0, &nulldir);
12111127Smckusick 	for (;;) {
12211127Smckusick 		curfile.name = "<directory file - name unknown>";
12311127Smckusick 		curfile.action = USING;
12411127Smckusick 		ip = curfile.dip;
12517948Smckusick 		if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
12611732Smckusick 			(void) fclose(df);
12740064Smckusick 			dirp = opendirfile(dirfile);
12811127Smckusick 			if (dirp == NULL)
12940064Smckusick 				perror("opendirfile");
13011127Smckusick 			if (mf != NULL)
13111732Smckusick 				(void) fclose(mf);
13246566Smckusick 			i = dirlookup(dot);
13311992Smckusick 			if (i == 0)
13411421Smckusick 				panic("Root directory is not on tape\n");
13511127Smckusick 			return;
13611127Smckusick 		}
13711322Smckusick 		itp = allocinotab(curfile.ino, ip, seekpt);
13811127Smckusick 		getfile(putdir, null);
13911127Smckusick 		putent(&nulldir);
14011127Smckusick 		flushent();
14111322Smckusick 		itp->t_size = seekpt - itp->t_seekpt;
14211127Smckusick 	}
14311127Smckusick }
14411127Smckusick 
14511127Smckusick /*
14611322Smckusick  * skip over all the directories on the tape
14711322Smckusick  */
14811322Smckusick skipdirs()
14911322Smckusick {
15011322Smckusick 
15111322Smckusick 	while ((curfile.dip->di_mode & IFMT) == IFDIR) {
15211322Smckusick 		skipfile();
15311322Smckusick 	}
15411322Smckusick }
15511322Smckusick 
15611322Smckusick /*
15711127Smckusick  *	Recursively find names and inumbers of all files in subtree
15811127Smckusick  *	pname and pass them off to be processed.
15911127Smckusick  */
16011127Smckusick treescan(pname, ino, todo)
16111127Smckusick 	char *pname;
16211127Smckusick 	ino_t ino;
16311744Smckusick 	long (*todo)();
16411127Smckusick {
16511127Smckusick 	register struct inotab *itp;
16612453Smckusick 	register struct direct *dp;
16712453Smckusick 	register struct entry *np;
16811127Smckusick 	int namelen;
16950662Smckusick 	long bpt;
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;
29711127Smckusick 
29811127Smckusick 	if (cvtflag) {
29911127Smckusick 		eodp = (struct odirect *)&buf[size];
30011127Smckusick 		for (odp = (struct odirect *)buf; odp < eodp; odp++)
30111127Smckusick 			if (odp->d_ino != 0) {
30211127Smckusick 				dcvt(odp, &cvtbuf);
30311127Smckusick 				putent(&cvtbuf);
30411127Smckusick 			}
30511127Smckusick 	} else {
30611127Smckusick 		for (loc = 0; loc < size; ) {
30711127Smckusick 			dp = (struct direct *)(buf + loc);
30854582Smckusick 			if (oldinofmt) {
30954582Smckusick 				if (Bcvt) {
31054582Smckusick 					swabst("l2s", (char *) dp);
31154582Smckusick 				}
31254582Smckusick 			} else {
31354582Smckusick 				if (Bcvt) {
31454582Smckusick 					swabst("ls", (char *) dp);
31554582Smckusick 				}
31626941Ssklower 			}
31711127Smckusick 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
31846564Smckusick 			if ((dp->d_reclen & 0x3) != 0 ||
31946564Smckusick 			    dp->d_reclen > i ||
32054582Smckusick 			    dp->d_reclen < DIRSIZ(0, dp) ||
32150662Smckusick 			    dp->d_namlen > NAME_MAX) {
32250662Smckusick 				vprintf(stdout, "Mangled directory: ");
32350662Smckusick 				if ((dp->d_reclen & 0x3) != 0)
32450662Smckusick 					vprintf(stdout,
32550662Smckusick 					   "reclen not multiple of 4 ");
32654582Smckusick 				if (dp->d_reclen < DIRSIZ(0, dp))
32750662Smckusick 					vprintf(stdout,
32850662Smckusick 					   "reclen less than DIRSIZ (%d < %d) ",
32954582Smckusick 					   dp->d_reclen, DIRSIZ(0, dp));
33050662Smckusick 				if (dp->d_namlen > NAME_MAX)
33150662Smckusick 					vprintf(stdout,
33250662Smckusick 					   "reclen name too big (%d > %d) ",
33350662Smckusick 					   dp->d_namlen, NAME_MAX);
33450662Smckusick 				vprintf(stdout, "\n");
33511127Smckusick 				loc += i;
33611127Smckusick 				continue;
33711127Smckusick 			}
33811127Smckusick 			loc += dp->d_reclen;
33911127Smckusick 			if (dp->d_ino != 0) {
34011127Smckusick 				putent(dp);
34111127Smckusick 			}
34211127Smckusick 		}
34311127Smckusick 	}
34411127Smckusick }
34511127Smckusick 
34611127Smckusick /*
34711127Smckusick  * These variables are "local" to the following two functions.
34811127Smckusick  */
34911127Smckusick char dirbuf[DIRBLKSIZ];
35011127Smckusick long dirloc = 0;
35111127Smckusick long prev = 0;
35211127Smckusick 
35311127Smckusick /*
35411127Smckusick  * add a new directory entry to a file.
35511127Smckusick  */
35611127Smckusick putent(dp)
35711127Smckusick 	struct direct *dp;
35811127Smckusick {
35954582Smckusick 	dp->d_reclen = DIRSIZ(0, dp);
36011127Smckusick 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
36111127Smckusick 		((struct direct *)(dirbuf + prev))->d_reclen =
36211127Smckusick 		    DIRBLKSIZ - prev;
36311732Smckusick 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
36411127Smckusick 		dirloc = 0;
36511127Smckusick 	}
36611127Smckusick 	bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen);
36711127Smckusick 	prev = dirloc;
36811127Smckusick 	dirloc += dp->d_reclen;
36911127Smckusick }
37011127Smckusick 
37111127Smckusick /*
37211127Smckusick  * flush out a directory that is finished.
37311127Smckusick  */
37411127Smckusick flushent()
37511127Smckusick {
37611127Smckusick 
37711127Smckusick 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
37811732Smckusick 	(void) fwrite(dirbuf, (int)dirloc, 1, df);
37911127Smckusick 	seekpt = ftell(df);
38011127Smckusick 	dirloc = 0;
38111127Smckusick }
38211127Smckusick 
38311127Smckusick dcvt(odp, ndp)
38411127Smckusick 	register struct odirect *odp;
38511127Smckusick 	register struct direct *ndp;
38611127Smckusick {
38711127Smckusick 
38811127Smckusick 	bzero((char *)ndp, (long)(sizeof *ndp));
38911127Smckusick 	ndp->d_ino =  odp->d_ino;
39054582Smckusick 	ndp->d_type = DT_UNKNOWN;
39111992Smckusick 	(void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
39211127Smckusick 	ndp->d_namlen = strlen(ndp->d_name);
39354582Smckusick 	ndp->d_reclen = DIRSIZ(0, ndp);
39411127Smckusick }
39511127Smckusick 
39611127Smckusick /*
39711127Smckusick  * Seek to an entry in a directory.
39840064Smckusick  * Only values returned by rst_telldir should be passed to rst_seekdir.
39911732Smckusick  * This routine handles many directories in a single file.
40011732Smckusick  * It takes the base of the directory in the file, plus
40111732Smckusick  * the desired seek offset into it.
40211127Smckusick  */
40311127Smckusick void
40412556Smckusick rst_seekdir(dirp, loc, base)
40550657Smckusick 	register RST_DIR *dirp;
40650662Smckusick 	long loc, base;
40711127Smckusick {
40811127Smckusick 
40940064Smckusick 	if (loc == rst_telldir(dirp))
41011127Smckusick 		return;
41111127Smckusick 	loc -= base;
41211127Smckusick 	if (loc < 0)
41312556Smckusick 		fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
41411127Smckusick 	(void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
41511127Smckusick 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
41611127Smckusick 	if (dirp->dd_loc != 0)
41711127Smckusick 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
41811127Smckusick }
41911127Smckusick 
42011127Smckusick /*
42111127Smckusick  * get next entry in a directory.
42211127Smckusick  */
42311127Smckusick struct direct *
42412556Smckusick rst_readdir(dirp)
42550657Smckusick 	register RST_DIR *dirp;
42611127Smckusick {
42711127Smckusick 	register struct direct *dp;
42811127Smckusick 
42911127Smckusick 	for (;;) {
43011127Smckusick 		if (dirp->dd_loc == 0) {
43111127Smckusick 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
43211127Smckusick 			    DIRBLKSIZ);
43312453Smckusick 			if (dirp->dd_size <= 0) {
43412453Smckusick 				dprintf(stderr, "error reading directory\n");
43511127Smckusick 				return NULL;
43612453Smckusick 			}
43711127Smckusick 		}
43811127Smckusick 		if (dirp->dd_loc >= dirp->dd_size) {
43911127Smckusick 			dirp->dd_loc = 0;
44011127Smckusick 			continue;
44111127Smckusick 		}
44211127Smckusick 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
44311127Smckusick 		if (dp->d_reclen == 0 ||
44412453Smckusick 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
44512453Smckusick 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
44612453Smckusick 				dp->d_reclen);
44711127Smckusick 			return NULL;
44812453Smckusick 		}
44911127Smckusick 		dirp->dd_loc += dp->d_reclen;
45012453Smckusick 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
45112453Smckusick 			continue;
45212556Smckusick 		if (dp->d_ino >= maxino) {
45312453Smckusick 			dprintf(stderr, "corrupted directory: bad inum %d\n",
45412453Smckusick 				dp->d_ino);
45512453Smckusick 			continue;
45612453Smckusick 		}
45711127Smckusick 		return (dp);
45811127Smckusick 	}
45911127Smckusick }
46011127Smckusick 
46111127Smckusick /*
46217753Smckusick  * Simulate the opening of a directory
46317753Smckusick  */
46450657Smckusick RST_DIR *
46517753Smckusick rst_opendir(name)
46617753Smckusick 	char *name;
46717753Smckusick {
46817753Smckusick 	struct inotab *itp;
46917753Smckusick 	ino_t ino;
47017753Smckusick 
47117753Smckusick 	if ((ino = dirlookup(name)) > 0 &&
47217753Smckusick 	    (itp = inotablookup(ino)) != NULL) {
47317753Smckusick 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
47417753Smckusick 		return (dirp);
47517753Smckusick 	}
47617753Smckusick 	return (0);
47717753Smckusick }
47817753Smckusick 
47917753Smckusick /*
48040064Smckusick  * Simulate finding the current offset in the directory.
48140064Smckusick  */
48250662Smckusick long
48340064Smckusick rst_telldir(dirp)
48450657Smckusick 	RST_DIR *dirp;
48540064Smckusick {
48650662Smckusick 	long lseek();
48740064Smckusick 
48840064Smckusick 	return (lseek(dirp->dd_fd, 0L, 1) - dirp->dd_size + dirp->dd_loc);
48940064Smckusick }
49040064Smckusick 
49140064Smckusick /*
49240064Smckusick  * Open a directory file.
49340064Smckusick  */
49450657Smckusick RST_DIR *
49540064Smckusick opendirfile(name)
49640064Smckusick 	char *name;
49740064Smckusick {
49850657Smckusick 	register RST_DIR *dirp;
49940064Smckusick 	register int fd;
50040064Smckusick 
50140064Smckusick 	if ((fd = open(name, 0)) == -1)
50240064Smckusick 		return NULL;
50350657Smckusick 	if ((dirp = (RST_DIR *)malloc(sizeof(RST_DIR))) == NULL) {
50440064Smckusick 		close (fd);
50540064Smckusick 		return NULL;
50640064Smckusick 	}
50740064Smckusick 	dirp->dd_fd = fd;
50840064Smckusick 	dirp->dd_loc = 0;
50940064Smckusick 	return dirp;
51040064Smckusick }
51140064Smckusick 
51240064Smckusick /*
51311127Smckusick  * Set the mode, owner, and times for all new or changed directories
51411127Smckusick  */
515*55880Smckusick setdirmodes(flags)
516*55880Smckusick 	int flags;
51711127Smckusick {
51811127Smckusick 	FILE *mf;
51911127Smckusick 	struct modeinfo node;
52011127Smckusick 	struct entry *ep;
52111127Smckusick 	char *cp;
52211127Smckusick 
52311127Smckusick 	vprintf(stdout, "Set directory mode, owner, and times.\n");
52437952Sbostic 	(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
52511127Smckusick 	mf = fopen(modefile, "r");
52611127Smckusick 	if (mf == NULL) {
52711127Smckusick 		perror("fopen");
52815779Smckusick 		fprintf(stderr, "cannot open mode file %s\n", modefile);
52915779Smckusick 		fprintf(stderr, "directory mode, owner, and times not set\n");
53015779Smckusick 		return;
53111127Smckusick 	}
53211127Smckusick 	clearerr(mf);
53311309Smckusick 	for (;;) {
53411732Smckusick 		(void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
53511309Smckusick 		if (feof(mf))
53611309Smckusick 			break;
53711127Smckusick 		ep = lookupino(node.ino);
53814453Smckusick 		if (command == 'i' || command == 'x') {
53914453Smckusick 			if (ep == NIL)
54011309Smckusick 				continue;
541*55880Smckusick 			if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
54218008Smckusick 				ep->e_flags &= ~NEW;
54318008Smckusick 				continue;
54418008Smckusick 			}
54514453Smckusick 			if (node.ino == ROOTINO &&
54614453Smckusick 		   	    reply("set owner/mode for '.'") == FAIL)
54714453Smckusick 				continue;
54814453Smckusick 		}
54942862Smckusick 		if (ep == NIL) {
55011127Smckusick 			panic("cannot find directory inode %d\n", node.ino);
55142862Smckusick 		} else {
55242862Smckusick 			cp = myname(ep);
55342862Smckusick 			(void) chown(cp, node.uid, node.gid);
55442862Smckusick 			(void) chmod(cp, node.mode);
55542862Smckusick 			utimes(cp, node.timep);
55642862Smckusick 			ep->e_flags &= ~NEW;
55742862Smckusick 		}
55811127Smckusick 	}
55911127Smckusick 	if (ferror(mf))
56011127Smckusick 		panic("error setting directory modes\n");
56111732Smckusick 	(void) fclose(mf);
56211127Smckusick }
56311127Smckusick 
56411127Smckusick /*
56511127Smckusick  * Generate a literal copy of a directory.
56611127Smckusick  */
56711127Smckusick genliteraldir(name, ino)
56811127Smckusick 	char *name;
56911127Smckusick 	ino_t ino;
57011127Smckusick {
57111127Smckusick 	register struct inotab *itp;
57211127Smckusick 	int ofile, dp, i, size;
57311127Smckusick 	char buf[BUFSIZ];
57411127Smckusick 
57511127Smckusick 	itp = inotablookup(ino);
57611127Smckusick 	if (itp == NULL)
57711322Smckusick 		panic("Cannot find directory inode %d named %s\n", ino, name);
57812893Ssam 	if ((ofile = creat(name, 0666)) < 0) {
57912556Smckusick 		fprintf(stderr, "%s: ", name);
58012556Smckusick 		(void) fflush(stderr);
58112556Smckusick 		perror("cannot create file");
58211127Smckusick 		return (FAIL);
58311127Smckusick 	}
58412556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
58511127Smckusick 	dp = dup(dirp->dd_fd);
58611127Smckusick 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
58711127Smckusick 		size = i < BUFSIZ ? i : BUFSIZ;
58811127Smckusick 		if (read(dp, buf, (int) size) == -1) {
58911127Smckusick 			fprintf(stderr,
59011127Smckusick 				"write error extracting inode %d, name %s\n",
59111127Smckusick 				curfile.ino, curfile.name);
59211127Smckusick 			perror("read");
59311127Smckusick 			done(1);
59411127Smckusick 		}
59534268Smckusick 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
59611127Smckusick 			fprintf(stderr,
59711127Smckusick 				"write error extracting inode %d, name %s\n",
59811127Smckusick 				curfile.ino, curfile.name);
59911127Smckusick 			perror("write");
60011127Smckusick 			done(1);
60111127Smckusick 		}
60211127Smckusick 	}
60311732Smckusick 	(void) close(dp);
60411732Smckusick 	(void) close(ofile);
60511127Smckusick 	return (GOOD);
60611127Smckusick }
60711127Smckusick 
60811127Smckusick /*
60911992Smckusick  * Determine the type of an inode
61011992Smckusick  */
61111992Smckusick inodetype(ino)
61211992Smckusick 	ino_t ino;
61311992Smckusick {
61411992Smckusick 	struct inotab *itp;
61511992Smckusick 
61611992Smckusick 	itp = inotablookup(ino);
61711992Smckusick 	if (itp == NULL)
61811992Smckusick 		return (LEAF);
61911992Smckusick 	return (NODE);
62011992Smckusick }
62111992Smckusick 
62211992Smckusick /*
62311127Smckusick  * Allocate and initialize a directory inode entry.
62411127Smckusick  * If requested, save its pertinent mode, owner, and time info.
62511127Smckusick  */
62611322Smckusick struct inotab *
62711127Smckusick allocinotab(ino, dip, seekpt)
62811127Smckusick 	ino_t ino;
62911127Smckusick 	struct dinode *dip;
63050662Smckusick 	long seekpt;
63111127Smckusick {
63211127Smckusick 	register struct inotab	*itp;
63311127Smckusick 	struct modeinfo node;
63411127Smckusick 
63511127Smckusick 	itp = (struct inotab *)calloc(1, sizeof(struct inotab));
63613859Smckusick 	if (itp == 0)
63713859Smckusick 		panic("no memory directory table\n");
63811127Smckusick 	itp->t_next = inotab[INOHASH(ino)];
63911127Smckusick 	inotab[INOHASH(ino)] = itp;
64011127Smckusick 	itp->t_ino = ino;
64111127Smckusick 	itp->t_seekpt = seekpt;
64211127Smckusick 	if (mf == NULL)
64311322Smckusick 		return(itp);
64411127Smckusick 	node.ino = ino;
64554156Smckusick 	node.timep[0].tv_sec = dip->di_atime.ts_sec;
64654156Smckusick 	node.timep[0].tv_usec = dip->di_atime.ts_nsec / 1000;
64754156Smckusick 	node.timep[1].tv_sec = dip->di_mtime.ts_sec;
64854156Smckusick 	node.timep[1].tv_usec = dip->di_mtime.ts_nsec / 1000;
64911127Smckusick 	node.mode = dip->di_mode;
65011127Smckusick 	node.uid = dip->di_uid;
65111127Smckusick 	node.gid = dip->di_gid;
65211732Smckusick 	(void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
65311322Smckusick 	return(itp);
65411127Smckusick }
65511127Smckusick 
65611127Smckusick /*
65711127Smckusick  * Look up an inode in the table of directories
65811127Smckusick  */
65911127Smckusick struct inotab *
66011127Smckusick inotablookup(ino)
66111127Smckusick 	ino_t	ino;
66211127Smckusick {
66311127Smckusick 	register struct inotab *itp;
66411127Smckusick 
66511127Smckusick 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
66611127Smckusick 		if (itp->t_ino == ino)
66711127Smckusick 			return(itp);
66811127Smckusick 	return ((struct inotab *)0);
66911127Smckusick }
67011127Smckusick 
67111127Smckusick /*
67211127Smckusick  * Clean up and exit
67311127Smckusick  */
67411127Smckusick done(exitcode)
67511127Smckusick 	int exitcode;
67611127Smckusick {
67711127Smckusick 
67811127Smckusick 	closemt();
67911992Smckusick 	if (modefile[0] != '#')
68011992Smckusick 		(void) unlink(modefile);
68111992Smckusick 	if (dirfile[0] != '#')
68211992Smckusick 		(void) unlink(dirfile);
68311127Smckusick 	exit(exitcode);
68411127Smckusick }
685