xref: /csrg-svn/sbin/restore/dirs.c (revision 51625)
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*51625Sbostic static char sccsid[] = "@(#)dirs.c	5.18 (Berkeley) 11/11/91";
1036105Sbostic #endif /* not lint */
1111127Smckusick 
1211127Smckusick #include "restore.h"
1323545Smckusick #include <protocols/dumprestore.h>
1411309Smckusick #include <sys/file.h>
15*51625Sbostic #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;
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);
13146566Smckusick 			i = dirlookup(dot);
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;
16850662Smckusick 	long bpt;
16911644Smckusick 	char locname[MAXPATHLEN + 1];
17011127Smckusick 
17111127Smckusick 	itp = inotablookup(ino);
17211127Smckusick 	if (itp == NULL) {
17311127Smckusick 		/*
17411127Smckusick 		 * Pname is name of a simple file or an unchanged directory.
17511127Smckusick 		 */
17611744Smckusick 		(void) (*todo)(pname, ino, LEAF);
17711127Smckusick 		return;
17811127Smckusick 	}
17911127Smckusick 	/*
18011127Smckusick 	 * Pname is a dumped directory name.
18111127Smckusick 	 */
18211744Smckusick 	if ((*todo)(pname, ino, NODE) == FAIL)
18311744Smckusick 		return;
18411127Smckusick 	/*
18511127Smckusick 	 * begin search through the directory
18611127Smckusick 	 * skipping over "." and ".."
18711127Smckusick 	 */
18811992Smckusick 	(void) strncpy(locname, pname, MAXPATHLEN);
18911992Smckusick 	(void) strncat(locname, "/", MAXPATHLEN);
19011127Smckusick 	namelen = strlen(locname);
19112556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
19212556Smckusick 	dp = rst_readdir(dirp); /* "." */
19327263Smckusick 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
19412556Smckusick 		dp = rst_readdir(dirp); /* ".." */
19527263Smckusick 	else
19627263Smckusick 		fprintf(stderr, "Warning: `.' missing from directory %s\n",
19727263Smckusick 			pname);
19827263Smckusick 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
19912556Smckusick 		dp = rst_readdir(dirp); /* first real entry */
20027263Smckusick 	else
20127263Smckusick 		fprintf(stderr, "Warning: `..' missing from directory %s\n",
20227263Smckusick 			pname);
20340064Smckusick 	bpt = rst_telldir(dirp);
20411127Smckusick 	/*
20512453Smckusick 	 * a zero inode signals end of directory
20611127Smckusick 	 */
20712453Smckusick 	while (dp != NULL && dp->d_ino != 0) {
20811127Smckusick 		locname[namelen] = '\0';
20911644Smckusick 		if (namelen + dp->d_namlen >= MAXPATHLEN) {
21011127Smckusick 			fprintf(stderr, "%s%s: name exceeds %d char\n",
21111644Smckusick 				locname, dp->d_name, MAXPATHLEN);
21211127Smckusick 		} else {
21311992Smckusick 			(void) strncat(locname, dp->d_name, (int)dp->d_namlen);
21411127Smckusick 			treescan(locname, dp->d_ino, todo);
21512556Smckusick 			rst_seekdir(dirp, bpt, itp->t_seekpt);
21611127Smckusick 		}
21712556Smckusick 		dp = rst_readdir(dirp);
21840064Smckusick 		bpt = rst_telldir(dirp);
21911127Smckusick 	}
22011127Smckusick 	if (dp == NULL)
22111127Smckusick 		fprintf(stderr, "corrupted directory: %s.\n", locname);
22211127Smckusick }
22311127Smckusick 
22411127Smckusick /*
22511127Smckusick  * Search the directory tree rooted at inode ROOTINO
22611127Smckusick  * for the path pointed at by n
22711127Smckusick  */
22811127Smckusick ino_t
22911127Smckusick psearch(n)
23011127Smckusick 	char	*n;
23111127Smckusick {
23211127Smckusick 	register char *cp, *cp1;
23311127Smckusick 	ino_t ino;
23411127Smckusick 	char c;
23511127Smckusick 
23611127Smckusick 	ino = ROOTINO;
23711127Smckusick 	if (*(cp = n) == '/')
23811127Smckusick 		cp++;
23911127Smckusick next:
24011127Smckusick 	cp1 = cp + 1;
24111127Smckusick 	while (*cp1 != '/' && *cp1)
24211127Smckusick 		cp1++;
24311127Smckusick 	c = *cp1;
24411127Smckusick 	*cp1 = 0;
24511127Smckusick 	ino = search(ino, cp);
24611127Smckusick 	if (ino == 0) {
24711127Smckusick 		*cp1 = c;
24811127Smckusick 		return(0);
24911127Smckusick 	}
25011127Smckusick 	*cp1 = c;
25111127Smckusick 	if (c == '/') {
25211127Smckusick 		cp = cp1+1;
25311127Smckusick 		goto next;
25411127Smckusick 	}
25511127Smckusick 	return(ino);
25611127Smckusick }
25711127Smckusick 
25811127Smckusick /*
25911127Smckusick  * search the directory inode ino
26011127Smckusick  * looking for entry cp
26111127Smckusick  */
26211127Smckusick ino_t
26311127Smckusick search(inum, cp)
26411127Smckusick 	ino_t	inum;
26511127Smckusick 	char	*cp;
26611127Smckusick {
26711127Smckusick 	register struct direct *dp;
26811127Smckusick 	register struct inotab *itp;
26911127Smckusick 	int len;
27011127Smckusick 
27111127Smckusick 	itp = inotablookup(inum);
27211127Smckusick 	if (itp == NULL)
27311127Smckusick 		return(0);
27412556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
27511127Smckusick 	len = strlen(cp);
27611127Smckusick 	do {
27712556Smckusick 		dp = rst_readdir(dirp);
27812453Smckusick 		if (dp == NULL || dp->d_ino == 0)
27912453Smckusick 			return (0);
28011992Smckusick 	} while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0);
28111127Smckusick 	return(dp->d_ino);
28211127Smckusick }
28311127Smckusick 
28411127Smckusick /*
28511127Smckusick  * Put the directory entries in the directory file
28611127Smckusick  */
28711127Smckusick putdir(buf, size)
28811127Smckusick 	char *buf;
28911127Smckusick 	int size;
29011127Smckusick {
29111127Smckusick 	struct direct cvtbuf;
29211127Smckusick 	register struct odirect *odp;
29311127Smckusick 	struct odirect *eodp;
29411127Smckusick 	register struct direct *dp;
29511127Smckusick 	long loc, i;
29626941Ssklower 	extern int Bcvt;
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);
30826941Ssklower 			if (Bcvt) {
30926941Ssklower 				swabst("l2s", (char *) dp);
31026941Ssklower 			}
31111127Smckusick 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
31246564Smckusick 			if ((dp->d_reclen & 0x3) != 0 ||
31346564Smckusick 			    dp->d_reclen > i ||
31446564Smckusick 			    dp->d_reclen < DIRSIZ(dp) ||
31550662Smckusick 			    dp->d_namlen > NAME_MAX) {
31650662Smckusick 				vprintf(stdout, "Mangled directory: ");
31750662Smckusick 				if ((dp->d_reclen & 0x3) != 0)
31850662Smckusick 					vprintf(stdout,
31950662Smckusick 					   "reclen not multiple of 4 ");
32050662Smckusick 				if (dp->d_reclen < DIRSIZ(dp))
32150662Smckusick 					vprintf(stdout,
32250662Smckusick 					   "reclen less than DIRSIZ (%d < %d) ",
32350662Smckusick 					   dp->d_reclen, DIRSIZ(dp));
32450662Smckusick 				if (dp->d_namlen > NAME_MAX)
32550662Smckusick 					vprintf(stdout,
32650662Smckusick 					   "reclen name too big (%d > %d) ",
32750662Smckusick 					   dp->d_namlen, NAME_MAX);
32850662Smckusick 				vprintf(stdout, "\n");
32911127Smckusick 				loc += i;
33011127Smckusick 				continue;
33111127Smckusick 			}
33211127Smckusick 			loc += dp->d_reclen;
33311127Smckusick 			if (dp->d_ino != 0) {
33411127Smckusick 				putent(dp);
33511127Smckusick 			}
33611127Smckusick 		}
33711127Smckusick 	}
33811127Smckusick }
33911127Smckusick 
34011127Smckusick /*
34111127Smckusick  * These variables are "local" to the following two functions.
34211127Smckusick  */
34311127Smckusick char dirbuf[DIRBLKSIZ];
34411127Smckusick long dirloc = 0;
34511127Smckusick long prev = 0;
34611127Smckusick 
34711127Smckusick /*
34811127Smckusick  * add a new directory entry to a file.
34911127Smckusick  */
35011127Smckusick putent(dp)
35111127Smckusick 	struct direct *dp;
35211127Smckusick {
35311322Smckusick 	dp->d_reclen = DIRSIZ(dp);
35411127Smckusick 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
35511127Smckusick 		((struct direct *)(dirbuf + prev))->d_reclen =
35611127Smckusick 		    DIRBLKSIZ - prev;
35711732Smckusick 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
35811127Smckusick 		dirloc = 0;
35911127Smckusick 	}
36011127Smckusick 	bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen);
36111127Smckusick 	prev = dirloc;
36211127Smckusick 	dirloc += dp->d_reclen;
36311127Smckusick }
36411127Smckusick 
36511127Smckusick /*
36611127Smckusick  * flush out a directory that is finished.
36711127Smckusick  */
36811127Smckusick flushent()
36911127Smckusick {
37011127Smckusick 
37111127Smckusick 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
37211732Smckusick 	(void) fwrite(dirbuf, (int)dirloc, 1, df);
37311127Smckusick 	seekpt = ftell(df);
37411127Smckusick 	dirloc = 0;
37511127Smckusick }
37611127Smckusick 
37711127Smckusick dcvt(odp, ndp)
37811127Smckusick 	register struct odirect *odp;
37911127Smckusick 	register struct direct *ndp;
38011127Smckusick {
38111127Smckusick 
38211127Smckusick 	bzero((char *)ndp, (long)(sizeof *ndp));
38311127Smckusick 	ndp->d_ino =  odp->d_ino;
38411992Smckusick 	(void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
38511127Smckusick 	ndp->d_namlen = strlen(ndp->d_name);
38611127Smckusick 	ndp->d_reclen = DIRSIZ(ndp);
38711127Smckusick }
38811127Smckusick 
38911127Smckusick /*
39011127Smckusick  * Seek to an entry in a directory.
39140064Smckusick  * Only values returned by rst_telldir should be passed to rst_seekdir.
39211732Smckusick  * This routine handles many directories in a single file.
39311732Smckusick  * It takes the base of the directory in the file, plus
39411732Smckusick  * the desired seek offset into it.
39511127Smckusick  */
39611127Smckusick void
39712556Smckusick rst_seekdir(dirp, loc, base)
39850657Smckusick 	register RST_DIR *dirp;
39950662Smckusick 	long loc, base;
40011127Smckusick {
40111127Smckusick 
40240064Smckusick 	if (loc == rst_telldir(dirp))
40311127Smckusick 		return;
40411127Smckusick 	loc -= base;
40511127Smckusick 	if (loc < 0)
40612556Smckusick 		fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
40711127Smckusick 	(void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
40811127Smckusick 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
40911127Smckusick 	if (dirp->dd_loc != 0)
41011127Smckusick 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
41111127Smckusick }
41211127Smckusick 
41311127Smckusick /*
41411127Smckusick  * get next entry in a directory.
41511127Smckusick  */
41611127Smckusick struct direct *
41712556Smckusick rst_readdir(dirp)
41850657Smckusick 	register RST_DIR *dirp;
41911127Smckusick {
42011127Smckusick 	register struct direct *dp;
42111127Smckusick 
42211127Smckusick 	for (;;) {
42311127Smckusick 		if (dirp->dd_loc == 0) {
42411127Smckusick 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
42511127Smckusick 			    DIRBLKSIZ);
42612453Smckusick 			if (dirp->dd_size <= 0) {
42712453Smckusick 				dprintf(stderr, "error reading directory\n");
42811127Smckusick 				return NULL;
42912453Smckusick 			}
43011127Smckusick 		}
43111127Smckusick 		if (dirp->dd_loc >= dirp->dd_size) {
43211127Smckusick 			dirp->dd_loc = 0;
43311127Smckusick 			continue;
43411127Smckusick 		}
43511127Smckusick 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
43611127Smckusick 		if (dp->d_reclen == 0 ||
43712453Smckusick 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
43812453Smckusick 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
43912453Smckusick 				dp->d_reclen);
44011127Smckusick 			return NULL;
44112453Smckusick 		}
44211127Smckusick 		dirp->dd_loc += dp->d_reclen;
44312453Smckusick 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
44412453Smckusick 			continue;
44512556Smckusick 		if (dp->d_ino >= maxino) {
44612453Smckusick 			dprintf(stderr, "corrupted directory: bad inum %d\n",
44712453Smckusick 				dp->d_ino);
44812453Smckusick 			continue;
44912453Smckusick 		}
45011127Smckusick 		return (dp);
45111127Smckusick 	}
45211127Smckusick }
45311127Smckusick 
45411127Smckusick /*
45517753Smckusick  * Simulate the opening of a directory
45617753Smckusick  */
45750657Smckusick RST_DIR *
45817753Smckusick rst_opendir(name)
45917753Smckusick 	char *name;
46017753Smckusick {
46117753Smckusick 	struct inotab *itp;
46217753Smckusick 	ino_t ino;
46317753Smckusick 
46417753Smckusick 	if ((ino = dirlookup(name)) > 0 &&
46517753Smckusick 	    (itp = inotablookup(ino)) != NULL) {
46617753Smckusick 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
46717753Smckusick 		return (dirp);
46817753Smckusick 	}
46917753Smckusick 	return (0);
47017753Smckusick }
47117753Smckusick 
47217753Smckusick /*
47340064Smckusick  * Simulate finding the current offset in the directory.
47440064Smckusick  */
47550662Smckusick long
47640064Smckusick rst_telldir(dirp)
47750657Smckusick 	RST_DIR *dirp;
47840064Smckusick {
47950662Smckusick 	long lseek();
48040064Smckusick 
48140064Smckusick 	return (lseek(dirp->dd_fd, 0L, 1) - dirp->dd_size + dirp->dd_loc);
48240064Smckusick }
48340064Smckusick 
48440064Smckusick /*
48540064Smckusick  * Open a directory file.
48640064Smckusick  */
48750657Smckusick RST_DIR *
48840064Smckusick opendirfile(name)
48940064Smckusick 	char *name;
49040064Smckusick {
49150657Smckusick 	register RST_DIR *dirp;
49240064Smckusick 	register int fd;
49340064Smckusick 
49440064Smckusick 	if ((fd = open(name, 0)) == -1)
49540064Smckusick 		return NULL;
49650657Smckusick 	if ((dirp = (RST_DIR *)malloc(sizeof(RST_DIR))) == NULL) {
49740064Smckusick 		close (fd);
49840064Smckusick 		return NULL;
49940064Smckusick 	}
50040064Smckusick 	dirp->dd_fd = fd;
50140064Smckusick 	dirp->dd_loc = 0;
50240064Smckusick 	return dirp;
50340064Smckusick }
50440064Smckusick 
50540064Smckusick /*
50611127Smckusick  * Set the mode, owner, and times for all new or changed directories
50711127Smckusick  */
50811992Smckusick setdirmodes()
50911127Smckusick {
51011127Smckusick 	FILE *mf;
51111127Smckusick 	struct modeinfo node;
51211127Smckusick 	struct entry *ep;
51311127Smckusick 	char *cp;
51411127Smckusick 
51511127Smckusick 	vprintf(stdout, "Set directory mode, owner, and times.\n");
51637952Sbostic 	(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
51711127Smckusick 	mf = fopen(modefile, "r");
51811127Smckusick 	if (mf == NULL) {
51911127Smckusick 		perror("fopen");
52015779Smckusick 		fprintf(stderr, "cannot open mode file %s\n", modefile);
52115779Smckusick 		fprintf(stderr, "directory mode, owner, and times not set\n");
52215779Smckusick 		return;
52311127Smckusick 	}
52411127Smckusick 	clearerr(mf);
52511309Smckusick 	for (;;) {
52611732Smckusick 		(void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
52711309Smckusick 		if (feof(mf))
52811309Smckusick 			break;
52911127Smckusick 		ep = lookupino(node.ino);
53014453Smckusick 		if (command == 'i' || command == 'x') {
53114453Smckusick 			if (ep == NIL)
53211309Smckusick 				continue;
53318008Smckusick 			if (ep->e_flags & EXISTED) {
53418008Smckusick 				ep->e_flags &= ~NEW;
53518008Smckusick 				continue;
53618008Smckusick 			}
53714453Smckusick 			if (node.ino == ROOTINO &&
53814453Smckusick 		   	    reply("set owner/mode for '.'") == FAIL)
53914453Smckusick 				continue;
54014453Smckusick 		}
54142862Smckusick 		if (ep == NIL) {
54211127Smckusick 			panic("cannot find directory inode %d\n", node.ino);
54342862Smckusick 		} else {
54442862Smckusick 			cp = myname(ep);
54542862Smckusick 			(void) chown(cp, node.uid, node.gid);
54642862Smckusick 			(void) chmod(cp, node.mode);
54742862Smckusick 			utimes(cp, node.timep);
54842862Smckusick 			ep->e_flags &= ~NEW;
54942862Smckusick 		}
55011127Smckusick 	}
55111127Smckusick 	if (ferror(mf))
55211127Smckusick 		panic("error setting directory modes\n");
55311732Smckusick 	(void) fclose(mf);
55411127Smckusick }
55511127Smckusick 
55611127Smckusick /*
55711127Smckusick  * Generate a literal copy of a directory.
55811127Smckusick  */
55911127Smckusick genliteraldir(name, ino)
56011127Smckusick 	char *name;
56111127Smckusick 	ino_t ino;
56211127Smckusick {
56311127Smckusick 	register struct inotab *itp;
56411127Smckusick 	int ofile, dp, i, size;
56511127Smckusick 	char buf[BUFSIZ];
56611127Smckusick 
56711127Smckusick 	itp = inotablookup(ino);
56811127Smckusick 	if (itp == NULL)
56911322Smckusick 		panic("Cannot find directory inode %d named %s\n", ino, name);
57012893Ssam 	if ((ofile = creat(name, 0666)) < 0) {
57112556Smckusick 		fprintf(stderr, "%s: ", name);
57212556Smckusick 		(void) fflush(stderr);
57312556Smckusick 		perror("cannot create file");
57411127Smckusick 		return (FAIL);
57511127Smckusick 	}
57612556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
57711127Smckusick 	dp = dup(dirp->dd_fd);
57811127Smckusick 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
57911127Smckusick 		size = i < BUFSIZ ? i : BUFSIZ;
58011127Smckusick 		if (read(dp, buf, (int) size) == -1) {
58111127Smckusick 			fprintf(stderr,
58211127Smckusick 				"write error extracting inode %d, name %s\n",
58311127Smckusick 				curfile.ino, curfile.name);
58411127Smckusick 			perror("read");
58511127Smckusick 			done(1);
58611127Smckusick 		}
58734268Smckusick 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
58811127Smckusick 			fprintf(stderr,
58911127Smckusick 				"write error extracting inode %d, name %s\n",
59011127Smckusick 				curfile.ino, curfile.name);
59111127Smckusick 			perror("write");
59211127Smckusick 			done(1);
59311127Smckusick 		}
59411127Smckusick 	}
59511732Smckusick 	(void) close(dp);
59611732Smckusick 	(void) close(ofile);
59711127Smckusick 	return (GOOD);
59811127Smckusick }
59911127Smckusick 
60011127Smckusick /*
60111992Smckusick  * Determine the type of an inode
60211992Smckusick  */
60311992Smckusick inodetype(ino)
60411992Smckusick 	ino_t ino;
60511992Smckusick {
60611992Smckusick 	struct inotab *itp;
60711992Smckusick 
60811992Smckusick 	itp = inotablookup(ino);
60911992Smckusick 	if (itp == NULL)
61011992Smckusick 		return (LEAF);
61111992Smckusick 	return (NODE);
61211992Smckusick }
61311992Smckusick 
61411992Smckusick /*
61511127Smckusick  * Allocate and initialize a directory inode entry.
61611127Smckusick  * If requested, save its pertinent mode, owner, and time info.
61711127Smckusick  */
61811322Smckusick struct inotab *
61911127Smckusick allocinotab(ino, dip, seekpt)
62011127Smckusick 	ino_t ino;
62111127Smckusick 	struct dinode *dip;
62250662Smckusick 	long seekpt;
62311127Smckusick {
62411127Smckusick 	register struct inotab	*itp;
62511127Smckusick 	struct modeinfo node;
62611127Smckusick 
62711127Smckusick 	itp = (struct inotab *)calloc(1, sizeof(struct inotab));
62813859Smckusick 	if (itp == 0)
62913859Smckusick 		panic("no memory directory table\n");
63011127Smckusick 	itp->t_next = inotab[INOHASH(ino)];
63111127Smckusick 	inotab[INOHASH(ino)] = itp;
63211127Smckusick 	itp->t_ino = ino;
63311127Smckusick 	itp->t_seekpt = seekpt;
63411127Smckusick 	if (mf == NULL)
63511322Smckusick 		return(itp);
63611127Smckusick 	node.ino = ino;
63739471Smckusick 	node.timep[0].tv_sec = dip->di_atime;
63839471Smckusick 	node.timep[0].tv_usec = 0;
63939471Smckusick 	node.timep[1].tv_sec = dip->di_mtime;
64039471Smckusick 	node.timep[1].tv_usec = 0;
64111127Smckusick 	node.mode = dip->di_mode;
64211127Smckusick 	node.uid = dip->di_uid;
64311127Smckusick 	node.gid = dip->di_gid;
64411732Smckusick 	(void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
64511322Smckusick 	return(itp);
64611127Smckusick }
64711127Smckusick 
64811127Smckusick /*
64911127Smckusick  * Look up an inode in the table of directories
65011127Smckusick  */
65111127Smckusick struct inotab *
65211127Smckusick inotablookup(ino)
65311127Smckusick 	ino_t	ino;
65411127Smckusick {
65511127Smckusick 	register struct inotab *itp;
65611127Smckusick 
65711127Smckusick 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
65811127Smckusick 		if (itp->t_ino == ino)
65911127Smckusick 			return(itp);
66011127Smckusick 	return ((struct inotab *)0);
66111127Smckusick }
66211127Smckusick 
66311127Smckusick /*
66411127Smckusick  * Clean up and exit
66511127Smckusick  */
66611127Smckusick done(exitcode)
66711127Smckusick 	int exitcode;
66811127Smckusick {
66911127Smckusick 
67011127Smckusick 	closemt();
67111992Smckusick 	if (modefile[0] != '#')
67211992Smckusick 		(void) unlink(modefile);
67311992Smckusick 	if (dirfile[0] != '#')
67411992Smckusick 		(void) unlink(dirfile);
67511127Smckusick 	exit(exitcode);
67611127Smckusick }
677