xref: /csrg-svn/sbin/restore/dirs.c (revision 69155)
121165Sdist /*
261536Sbostic  * Copyright (c) 1983, 1993
361536Sbostic  *	The Regents of the University of California.  All rights reserved.
465767Sbostic  * (c) UNIX System Laboratories, Inc.
565767Sbostic  * All or some portions of this file are derived from material licensed
665767Sbostic  * to the University of California by American Telephone and Telegraph
765767Sbostic  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
865767Sbostic  * the permission of UNIX System Laboratories, Inc.
936105Sbostic  *
1042708Sbostic  * %sccs.include.redist.c%
1121165Sdist  */
1221165Sdist 
1311127Smckusick #ifndef lint
14*69155Smckusick static char sccsid[] = "@(#)dirs.c	8.7 (Berkeley) 05/01/95";
1536105Sbostic #endif /* not lint */
1611127Smckusick 
1756567Sbostic #include <sys/param.h>
1811309Smckusick #include <sys/file.h>
1956567Sbostic #include <sys/stat.h>
2056567Sbostic #include <sys/time.h>
2156567Sbostic 
2256567Sbostic #include <ufs/ufs/dinode.h>
2351625Sbostic #include <ufs/ufs/dir.h>
24*69155Smckusick #include <ufs/ffs/fs.h>
2556567Sbostic #include <protocols/dumprestore.h>
2656567Sbostic 
2756567Sbostic #include <errno.h>
2856567Sbostic #include <stdio.h>
2956567Sbostic #include <stdlib.h>
3056567Sbostic #include <string.h>
3156567Sbostic #include <unistd.h>
3256567Sbostic 
3367439Smckusick #include <machine/endian.h>
3467439Smckusick 
3537952Sbostic #include "pathnames.h"
3656567Sbostic #include "restore.h"
3756567Sbostic #include "extern.h"
3811127Smckusick 
3911992Smckusick /*
4011992Smckusick  * Symbol table of directories read from tape.
4111992Smckusick  */
4211127Smckusick #define HASHSIZE	1000
4311127Smckusick #define INOHASH(val) (val % HASHSIZE)
4411127Smckusick struct inotab {
4550662Smckusick 	struct	inotab *t_next;
4611127Smckusick 	ino_t	t_ino;
4750662Smckusick 	long	t_seekpt;
4850662Smckusick 	long	t_size;
4911309Smckusick };
5011309Smckusick static struct inotab *inotab[HASHSIZE];
5111127Smckusick 
5211992Smckusick /*
5311992Smckusick  * Information retained about directories.
5411992Smckusick  */
5511127Smckusick struct modeinfo {
5611127Smckusick 	ino_t ino;
5739471Smckusick 	struct timeval timep[2];
5867738Smckusick 	mode_t mode;
5967738Smckusick 	uid_t uid;
6067738Smckusick 	gid_t gid;
6167738Smckusick 	int flags;
6211127Smckusick };
6311127Smckusick 
6411992Smckusick /*
6540094Smckusick  * Definitions for library routines operating on directories.
6640094Smckusick  */
6746564Smckusick #undef DIRBLKSIZ
6846564Smckusick #define DIRBLKSIZ 1024
6950657Smckusick struct rstdirdesc {
7040094Smckusick 	int	dd_fd;
7140094Smckusick 	long	dd_loc;
7240094Smckusick 	long	dd_size;
7340094Smckusick 	char	dd_buf[DIRBLKSIZ];
7440094Smckusick };
7540094Smckusick 
7640094Smckusick /*
7711992Smckusick  * Global variables for this file.
7811992Smckusick  */
7950662Smckusick static long	seekpt;
8011309Smckusick static FILE	*df, *mf;
8150657Smckusick static RST_DIR	*dirp;
8211992Smckusick static char	dirfile[32] = "#";	/* No file */
8311992Smckusick static char	modefile[32] = "#";	/* No file */
8446566Smckusick static char	dot[2] = ".";		/* So it can be modified */
8511127Smckusick 
8611992Smckusick /*
8711992Smckusick  * Format of old style directories.
8811992Smckusick  */
8911127Smckusick #define ODIRSIZ 14
9011127Smckusick struct odirect {
9111127Smckusick 	u_short	d_ino;
9211127Smckusick 	char	d_name[ODIRSIZ];
9311127Smckusick };
9411127Smckusick 
9556567Sbostic static struct inotab	*allocinotab __P((ino_t, struct dinode *, long));
9656567Sbostic static void		 dcvt __P((struct odirect *, struct direct *));
9756567Sbostic static void		 flushent __P((void));
9856567Sbostic static struct inotab	*inotablookup __P((ino_t));
9957896Sbostic static RST_DIR		*opendirfile __P((const char *));
10056567Sbostic static void		 putdir __P((char *, long));
10156567Sbostic static void		 putent __P((struct direct *));
10256567Sbostic static void		 rst_seekdir __P((RST_DIR *, long, long));
10356567Sbostic static long		 rst_telldir __P((RST_DIR *));
10456946Smckusick static struct direct	*searchdir __P((ino_t, char *));
10556567Sbostic 
10611127Smckusick /*
10711127Smckusick  *	Extract directory contents, building up a directory structure
10811127Smckusick  *	on disk for extraction by name.
10911992Smckusick  *	If genmode is requested, save mode, owner, and times for all
11011127Smckusick  *	directories on the tape.
11111127Smckusick  */
11256567Sbostic void
extractdirs(genmode)11311992Smckusick extractdirs(genmode)
11411992Smckusick 	int genmode;
11511127Smckusick {
11611127Smckusick 	register int i;
11711127Smckusick 	register struct dinode *ip;
11811322Smckusick 	struct inotab *itp;
11911127Smckusick 	struct direct nulldir;
12011127Smckusick 
12111127Smckusick 	vprintf(stdout, "Extract directories from tape\n");
12237952Sbostic 	(void) sprintf(dirfile, "%s/rstdir%d", _PATH_TMP, dumpdate);
12311127Smckusick 	df = fopen(dirfile, "w");
12457930Sbostic 	if (df == NULL) {
12511127Smckusick 		fprintf(stderr,
12615779Smckusick 		    "restore: %s - cannot create directory temporary\n",
12711127Smckusick 		    dirfile);
12856567Sbostic 		fprintf(stderr, "fopen: %s\n", strerror(errno));
12911127Smckusick 		done(1);
13011127Smckusick 	}
13111992Smckusick 	if (genmode != 0) {
13237952Sbostic 		(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
13311127Smckusick 		mf = fopen(modefile, "w");
13457930Sbostic 		if (mf == NULL) {
13511127Smckusick 			fprintf(stderr,
13615779Smckusick 			    "restore: %s - cannot create modefile \n",
13711127Smckusick 			    modefile);
13856567Sbostic 			fprintf(stderr, "fopen: %s\n", strerror(errno));
13911127Smckusick 			done(1);
14011127Smckusick 		}
14111127Smckusick 	}
14211322Smckusick 	nulldir.d_ino = 0;
14354582Smckusick 	nulldir.d_type = DT_DIR;
14411127Smckusick 	nulldir.d_namlen = 1;
14512453Smckusick 	(void) strcpy(nulldir.d_name, "/");
14654582Smckusick 	nulldir.d_reclen = DIRSIZ(0, &nulldir);
14711127Smckusick 	for (;;) {
14811127Smckusick 		curfile.name = "<directory file - name unknown>";
14911127Smckusick 		curfile.action = USING;
15011127Smckusick 		ip = curfile.dip;
15117948Smckusick 		if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
15211732Smckusick 			(void) fclose(df);
15340064Smckusick 			dirp = opendirfile(dirfile);
15411127Smckusick 			if (dirp == NULL)
15556567Sbostic 				fprintf(stderr, "opendirfile: %s\n",
15656567Sbostic 				    strerror(errno));
15711127Smckusick 			if (mf != NULL)
15811732Smckusick 				(void) fclose(mf);
15946566Smckusick 			i = dirlookup(dot);
16011992Smckusick 			if (i == 0)
16111421Smckusick 				panic("Root directory is not on tape\n");
16211127Smckusick 			return;
16311127Smckusick 		}
16411322Smckusick 		itp = allocinotab(curfile.ino, ip, seekpt);
16556434Smckusick 		getfile(putdir, xtrnull);
16611127Smckusick 		putent(&nulldir);
16711127Smckusick 		flushent();
16811322Smckusick 		itp->t_size = seekpt - itp->t_seekpt;
16911127Smckusick 	}
17011127Smckusick }
17111127Smckusick 
17211127Smckusick /*
17311322Smckusick  * skip over all the directories on the tape
17411322Smckusick  */
17556567Sbostic void
skipdirs()17611322Smckusick skipdirs()
17711322Smckusick {
17811322Smckusick 
17911322Smckusick 	while ((curfile.dip->di_mode & IFMT) == IFDIR) {
18011322Smckusick 		skipfile();
18111322Smckusick 	}
18211322Smckusick }
18311322Smckusick 
18411322Smckusick /*
18511127Smckusick  *	Recursively find names and inumbers of all files in subtree
18611127Smckusick  *	pname and pass them off to be processed.
18711127Smckusick  */
18856567Sbostic void
treescan(pname,ino,todo)18911127Smckusick treescan(pname, ino, todo)
19011127Smckusick 	char *pname;
19111127Smckusick 	ino_t ino;
19256567Sbostic 	long (*todo) __P((char *, ino_t, int));
19311127Smckusick {
19411127Smckusick 	register struct inotab *itp;
19512453Smckusick 	register struct direct *dp;
19611127Smckusick 	int namelen;
19750662Smckusick 	long bpt;
19811644Smckusick 	char locname[MAXPATHLEN + 1];
19911127Smckusick 
20011127Smckusick 	itp = inotablookup(ino);
20111127Smckusick 	if (itp == NULL) {
20211127Smckusick 		/*
20311127Smckusick 		 * Pname is name of a simple file or an unchanged directory.
20411127Smckusick 		 */
20511744Smckusick 		(void) (*todo)(pname, ino, LEAF);
20611127Smckusick 		return;
20711127Smckusick 	}
20811127Smckusick 	/*
20911127Smckusick 	 * Pname is a dumped directory name.
21011127Smckusick 	 */
21111744Smckusick 	if ((*todo)(pname, ino, NODE) == FAIL)
21211744Smckusick 		return;
21311127Smckusick 	/*
21411127Smckusick 	 * begin search through the directory
21511127Smckusick 	 * skipping over "." and ".."
21611127Smckusick 	 */
21711992Smckusick 	(void) strncpy(locname, pname, MAXPATHLEN);
21811992Smckusick 	(void) strncat(locname, "/", MAXPATHLEN);
21911127Smckusick 	namelen = strlen(locname);
22012556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
22112556Smckusick 	dp = rst_readdir(dirp); /* "." */
22227263Smckusick 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
22312556Smckusick 		dp = rst_readdir(dirp); /* ".." */
22427263Smckusick 	else
22527263Smckusick 		fprintf(stderr, "Warning: `.' missing from directory %s\n",
22627263Smckusick 			pname);
22727263Smckusick 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
22812556Smckusick 		dp = rst_readdir(dirp); /* first real entry */
22927263Smckusick 	else
23027263Smckusick 		fprintf(stderr, "Warning: `..' missing from directory %s\n",
23127263Smckusick 			pname);
23240064Smckusick 	bpt = rst_telldir(dirp);
23311127Smckusick 	/*
23412453Smckusick 	 * a zero inode signals end of directory
23511127Smckusick 	 */
23667755Smckusick 	while (dp != NULL) {
23711127Smckusick 		locname[namelen] = '\0';
23811644Smckusick 		if (namelen + dp->d_namlen >= MAXPATHLEN) {
23911127Smckusick 			fprintf(stderr, "%s%s: name exceeds %d char\n",
24011644Smckusick 				locname, dp->d_name, MAXPATHLEN);
24111127Smckusick 		} else {
24211992Smckusick 			(void) strncat(locname, dp->d_name, (int)dp->d_namlen);
24311127Smckusick 			treescan(locname, dp->d_ino, todo);
24412556Smckusick 			rst_seekdir(dirp, bpt, itp->t_seekpt);
24511127Smckusick 		}
24612556Smckusick 		dp = rst_readdir(dirp);
24740064Smckusick 		bpt = rst_telldir(dirp);
24811127Smckusick 	}
24911127Smckusick }
25011127Smckusick 
25111127Smckusick /*
25256434Smckusick  * Lookup a pathname which is always assumed to start from the ROOTINO.
25311127Smckusick  */
25456946Smckusick struct direct *
pathsearch(pathname)25556434Smckusick pathsearch(pathname)
25657896Sbostic 	const char *pathname;
25711127Smckusick {
25811127Smckusick 	ino_t ino;
25956946Smckusick 	struct direct *dp;
26057896Sbostic 	char *path, *name, buffer[MAXPATHLEN];
26111127Smckusick 
26256434Smckusick 	strcpy(buffer, pathname);
26357896Sbostic 	path = buffer;
26411127Smckusick 	ino = ROOTINO;
26557896Sbostic 	while (*path == '/')
26657896Sbostic 		path++;
26757930Sbostic 	dp = NULL;
26857896Sbostic 	while ((name = strsep(&path, "/")) != NULL && *name != NULL) {
26957930Sbostic 		if ((dp = searchdir(ino, name)) == NULL)
27056946Smckusick 			return (NULL);
27156946Smckusick 		ino = dp->d_ino;
27256946Smckusick 	}
27356946Smckusick 	return (dp);
27411127Smckusick }
27511127Smckusick 
27611127Smckusick /*
27756434Smckusick  * Lookup the requested name in directory inum.
27856434Smckusick  * Return its inode number if found, zero if it does not exist.
27911127Smckusick  */
28056946Smckusick static struct direct *
searchdir(inum,name)28156434Smckusick searchdir(inum, name)
28211127Smckusick 	ino_t	inum;
28356434Smckusick 	char	*name;
28411127Smckusick {
28511127Smckusick 	register struct direct *dp;
28611127Smckusick 	register struct inotab *itp;
28711127Smckusick 	int len;
28811127Smckusick 
28911127Smckusick 	itp = inotablookup(inum);
29011127Smckusick 	if (itp == NULL)
29157930Sbostic 		return (NULL);
29212556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
29356434Smckusick 	len = strlen(name);
29411127Smckusick 	do {
29512556Smckusick 		dp = rst_readdir(dirp);
29667755Smckusick 		if (dp == NULL)
29756946Smckusick 			return (NULL);
29856434Smckusick 	} while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
29956946Smckusick 	return (dp);
30011127Smckusick }
30111127Smckusick 
30211127Smckusick /*
30311127Smckusick  * Put the directory entries in the directory file
30411127Smckusick  */
30556567Sbostic static void
putdir(buf,size)30611127Smckusick putdir(buf, size)
30711127Smckusick 	char *buf;
30856567Sbostic 	long size;
30911127Smckusick {
31011127Smckusick 	struct direct cvtbuf;
31111127Smckusick 	register struct odirect *odp;
31211127Smckusick 	struct odirect *eodp;
31311127Smckusick 	register struct direct *dp;
31411127Smckusick 	long loc, i;
31511127Smckusick 
31611127Smckusick 	if (cvtflag) {
31711127Smckusick 		eodp = (struct odirect *)&buf[size];
31811127Smckusick 		for (odp = (struct odirect *)buf; odp < eodp; odp++)
31911127Smckusick 			if (odp->d_ino != 0) {
32011127Smckusick 				dcvt(odp, &cvtbuf);
32111127Smckusick 				putent(&cvtbuf);
32211127Smckusick 			}
32311127Smckusick 	} else {
32411127Smckusick 		for (loc = 0; loc < size; ) {
32511127Smckusick 			dp = (struct direct *)(buf + loc);
32667439Smckusick 			if (Bcvt)
32767439Smckusick 				swabst((u_char *)"ls", (u_char *) dp);
32867439Smckusick 			if (oldinofmt && dp->d_ino != 0) {
32967439Smckusick #				if BYTE_ORDER == BIG_ENDIAN
33067439Smckusick 					if (Bcvt)
33167439Smckusick 						dp->d_namlen = dp->d_type;
33267439Smckusick #				else
33367439Smckusick 					if (!Bcvt)
33467439Smckusick 						dp->d_namlen = dp->d_type;
33567439Smckusick #				endif
33667439Smckusick 				dp->d_type = DT_UNKNOWN;
33726941Ssklower 			}
33811127Smckusick 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
33946564Smckusick 			if ((dp->d_reclen & 0x3) != 0 ||
34046564Smckusick 			    dp->d_reclen > i ||
34154582Smckusick 			    dp->d_reclen < DIRSIZ(0, dp) ||
34250662Smckusick 			    dp->d_namlen > NAME_MAX) {
34350662Smckusick 				vprintf(stdout, "Mangled directory: ");
34450662Smckusick 				if ((dp->d_reclen & 0x3) != 0)
34550662Smckusick 					vprintf(stdout,
34650662Smckusick 					   "reclen not multiple of 4 ");
34754582Smckusick 				if (dp->d_reclen < DIRSIZ(0, dp))
34850662Smckusick 					vprintf(stdout,
34950662Smckusick 					   "reclen less than DIRSIZ (%d < %d) ",
35054582Smckusick 					   dp->d_reclen, DIRSIZ(0, dp));
35150662Smckusick 				if (dp->d_namlen > NAME_MAX)
35250662Smckusick 					vprintf(stdout,
35350662Smckusick 					   "reclen name too big (%d > %d) ",
35450662Smckusick 					   dp->d_namlen, NAME_MAX);
35550662Smckusick 				vprintf(stdout, "\n");
35611127Smckusick 				loc += i;
35711127Smckusick 				continue;
35811127Smckusick 			}
35911127Smckusick 			loc += dp->d_reclen;
36011127Smckusick 			if (dp->d_ino != 0) {
36111127Smckusick 				putent(dp);
36211127Smckusick 			}
36311127Smckusick 		}
36411127Smckusick 	}
36511127Smckusick }
36611127Smckusick 
36711127Smckusick /*
36811127Smckusick  * These variables are "local" to the following two functions.
36911127Smckusick  */
37011127Smckusick char dirbuf[DIRBLKSIZ];
37111127Smckusick long dirloc = 0;
37211127Smckusick long prev = 0;
37311127Smckusick 
37411127Smckusick /*
37511127Smckusick  * add a new directory entry to a file.
37611127Smckusick  */
37756567Sbostic static void
putent(dp)37811127Smckusick putent(dp)
37911127Smckusick 	struct direct *dp;
38011127Smckusick {
38154582Smckusick 	dp->d_reclen = DIRSIZ(0, dp);
38211127Smckusick 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
38311127Smckusick 		((struct direct *)(dirbuf + prev))->d_reclen =
38411127Smckusick 		    DIRBLKSIZ - prev;
38511732Smckusick 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
38611127Smckusick 		dirloc = 0;
38711127Smckusick 	}
38869001Sbostic 	memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
38911127Smckusick 	prev = dirloc;
39011127Smckusick 	dirloc += dp->d_reclen;
39111127Smckusick }
39211127Smckusick 
39311127Smckusick /*
39411127Smckusick  * flush out a directory that is finished.
39511127Smckusick  */
39656567Sbostic static void
flushent()39711127Smckusick flushent()
39811127Smckusick {
39911127Smckusick 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
40011732Smckusick 	(void) fwrite(dirbuf, (int)dirloc, 1, df);
40111127Smckusick 	seekpt = ftell(df);
40211127Smckusick 	dirloc = 0;
40311127Smckusick }
40411127Smckusick 
40556567Sbostic static void
dcvt(odp,ndp)40611127Smckusick dcvt(odp, ndp)
40711127Smckusick 	register struct odirect *odp;
40811127Smckusick 	register struct direct *ndp;
40911127Smckusick {
41011127Smckusick 
41169001Sbostic 	memset(ndp, 0, (long)(sizeof *ndp));
41211127Smckusick 	ndp->d_ino =  odp->d_ino;
41354582Smckusick 	ndp->d_type = DT_UNKNOWN;
41411992Smckusick 	(void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
41511127Smckusick 	ndp->d_namlen = strlen(ndp->d_name);
41654582Smckusick 	ndp->d_reclen = DIRSIZ(0, ndp);
41711127Smckusick }
41811127Smckusick 
41911127Smckusick /*
42011127Smckusick  * Seek to an entry in a directory.
42140064Smckusick  * Only values returned by rst_telldir should be passed to rst_seekdir.
42211732Smckusick  * This routine handles many directories in a single file.
42311732Smckusick  * It takes the base of the directory in the file, plus
42411732Smckusick  * the desired seek offset into it.
42511127Smckusick  */
42656567Sbostic static void
rst_seekdir(dirp,loc,base)42712556Smckusick rst_seekdir(dirp, loc, base)
42850657Smckusick 	register RST_DIR *dirp;
42950662Smckusick 	long loc, base;
43011127Smckusick {
43111127Smckusick 
43240064Smckusick 	if (loc == rst_telldir(dirp))
43311127Smckusick 		return;
43411127Smckusick 	loc -= base;
43511127Smckusick 	if (loc < 0)
43612556Smckusick 		fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
43756567Sbostic 	(void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
43811127Smckusick 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
43911127Smckusick 	if (dirp->dd_loc != 0)
44011127Smckusick 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
44111127Smckusick }
44211127Smckusick 
44311127Smckusick /*
44411127Smckusick  * get next entry in a directory.
44511127Smckusick  */
44611127Smckusick struct direct *
rst_readdir(dirp)44712556Smckusick rst_readdir(dirp)
44850657Smckusick 	register RST_DIR *dirp;
44911127Smckusick {
45011127Smckusick 	register struct direct *dp;
45111127Smckusick 
45211127Smckusick 	for (;;) {
45311127Smckusick 		if (dirp->dd_loc == 0) {
45411127Smckusick 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
45511127Smckusick 			    DIRBLKSIZ);
45612453Smckusick 			if (dirp->dd_size <= 0) {
45712453Smckusick 				dprintf(stderr, "error reading directory\n");
45856567Sbostic 				return (NULL);
45912453Smckusick 			}
46011127Smckusick 		}
46111127Smckusick 		if (dirp->dd_loc >= dirp->dd_size) {
46211127Smckusick 			dirp->dd_loc = 0;
46311127Smckusick 			continue;
46411127Smckusick 		}
46511127Smckusick 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
46611127Smckusick 		if (dp->d_reclen == 0 ||
46712453Smckusick 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
46812453Smckusick 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
46912453Smckusick 				dp->d_reclen);
47056567Sbostic 			return (NULL);
47112453Smckusick 		}
47211127Smckusick 		dirp->dd_loc += dp->d_reclen;
47367755Smckusick 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
47467755Smckusick 			return (NULL);
47512556Smckusick 		if (dp->d_ino >= maxino) {
47612453Smckusick 			dprintf(stderr, "corrupted directory: bad inum %d\n",
47712453Smckusick 				dp->d_ino);
47812453Smckusick 			continue;
47912453Smckusick 		}
48011127Smckusick 		return (dp);
48111127Smckusick 	}
48211127Smckusick }
48311127Smckusick 
48411127Smckusick /*
48517753Smckusick  * Simulate the opening of a directory
48617753Smckusick  */
48750657Smckusick RST_DIR *
rst_opendir(name)48817753Smckusick rst_opendir(name)
48957896Sbostic 	const char *name;
49017753Smckusick {
49117753Smckusick 	struct inotab *itp;
49256946Smckusick 	RST_DIR *dirp;
49317753Smckusick 	ino_t ino;
49417753Smckusick 
49517753Smckusick 	if ((ino = dirlookup(name)) > 0 &&
49617753Smckusick 	    (itp = inotablookup(ino)) != NULL) {
49756946Smckusick 		dirp = opendirfile(dirfile);
49817753Smckusick 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
49917753Smckusick 		return (dirp);
50017753Smckusick 	}
50157930Sbostic 	return (NULL);
50217753Smckusick }
50317753Smckusick 
50417753Smckusick /*
50556946Smckusick  * In our case, there is nothing to do when closing a directory.
50656946Smckusick  */
50756946Smckusick void
rst_closedir(dirp)50856946Smckusick rst_closedir(dirp)
50956946Smckusick 	RST_DIR *dirp;
51056946Smckusick {
51156946Smckusick 
51257896Sbostic 	(void)close(dirp->dd_fd);
51356946Smckusick 	free(dirp);
51456946Smckusick 	return;
51556946Smckusick }
51656946Smckusick 
51756946Smckusick /*
51840064Smckusick  * Simulate finding the current offset in the directory.
51940064Smckusick  */
52056567Sbostic static long
rst_telldir(dirp)52140064Smckusick rst_telldir(dirp)
52250657Smckusick 	RST_DIR *dirp;
52340064Smckusick {
52456567Sbostic 	return ((long)lseek(dirp->dd_fd,
52556567Sbostic 	    (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
52640064Smckusick }
52740064Smckusick 
52840064Smckusick /*
52940064Smckusick  * Open a directory file.
53040064Smckusick  */
53156567Sbostic static RST_DIR *
opendirfile(name)53240064Smckusick opendirfile(name)
53357896Sbostic 	const char *name;
53440064Smckusick {
53550657Smckusick 	register RST_DIR *dirp;
53640064Smckusick 	register int fd;
53740064Smckusick 
53856567Sbostic 	if ((fd = open(name, O_RDONLY)) == -1)
53956567Sbostic 		return (NULL);
54056567Sbostic 	if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
54156567Sbostic 		(void)close(fd);
54256567Sbostic 		return (NULL);
54340064Smckusick 	}
54440064Smckusick 	dirp->dd_fd = fd;
54540064Smckusick 	dirp->dd_loc = 0;
54656567Sbostic 	return (dirp);
54740064Smckusick }
54840064Smckusick 
54940064Smckusick /*
55011127Smckusick  * Set the mode, owner, and times for all new or changed directories
55111127Smckusick  */
55256567Sbostic void
setdirmodes(flags)55355880Smckusick setdirmodes(flags)
55455880Smckusick 	int flags;
55511127Smckusick {
55611127Smckusick 	FILE *mf;
55711127Smckusick 	struct modeinfo node;
55811127Smckusick 	struct entry *ep;
55911127Smckusick 	char *cp;
56011127Smckusick 
56111127Smckusick 	vprintf(stdout, "Set directory mode, owner, and times.\n");
56237952Sbostic 	(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
56311127Smckusick 	mf = fopen(modefile, "r");
56411127Smckusick 	if (mf == NULL) {
56556567Sbostic 		fprintf(stderr, "fopen: %s\n", strerror(errno));
56615779Smckusick 		fprintf(stderr, "cannot open mode file %s\n", modefile);
56715779Smckusick 		fprintf(stderr, "directory mode, owner, and times not set\n");
56815779Smckusick 		return;
56911127Smckusick 	}
57011127Smckusick 	clearerr(mf);
57111309Smckusick 	for (;;) {
57211732Smckusick 		(void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
57311309Smckusick 		if (feof(mf))
57411309Smckusick 			break;
57511127Smckusick 		ep = lookupino(node.ino);
57614453Smckusick 		if (command == 'i' || command == 'x') {
57756567Sbostic 			if (ep == NULL)
57811309Smckusick 				continue;
57955880Smckusick 			if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
58018008Smckusick 				ep->e_flags &= ~NEW;
58118008Smckusick 				continue;
58218008Smckusick 			}
58314453Smckusick 			if (node.ino == ROOTINO &&
58414453Smckusick 		   	    reply("set owner/mode for '.'") == FAIL)
58514453Smckusick 				continue;
58614453Smckusick 		}
58756567Sbostic 		if (ep == NULL) {
58811127Smckusick 			panic("cannot find directory inode %d\n", node.ino);
58942862Smckusick 		} else {
59042862Smckusick 			cp = myname(ep);
59142862Smckusick 			(void) chown(cp, node.uid, node.gid);
59242862Smckusick 			(void) chmod(cp, node.mode);
59367738Smckusick 			(void) chflags(cp, node.flags);
59442862Smckusick 			utimes(cp, node.timep);
59542862Smckusick 			ep->e_flags &= ~NEW;
59642862Smckusick 		}
59711127Smckusick 	}
59811127Smckusick 	if (ferror(mf))
59911127Smckusick 		panic("error setting directory modes\n");
60011732Smckusick 	(void) fclose(mf);
60111127Smckusick }
60211127Smckusick 
60311127Smckusick /*
60411127Smckusick  * Generate a literal copy of a directory.
60511127Smckusick  */
60656567Sbostic int
genliteraldir(name,ino)60711127Smckusick genliteraldir(name, ino)
60811127Smckusick 	char *name;
60911127Smckusick 	ino_t ino;
61011127Smckusick {
61111127Smckusick 	register struct inotab *itp;
61211127Smckusick 	int ofile, dp, i, size;
61311127Smckusick 	char buf[BUFSIZ];
61411127Smckusick 
61511127Smckusick 	itp = inotablookup(ino);
61611127Smckusick 	if (itp == NULL)
61711322Smckusick 		panic("Cannot find directory inode %d named %s\n", ino, name);
61869001Sbostic 	if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
61912556Smckusick 		fprintf(stderr, "%s: ", name);
62012556Smckusick 		(void) fflush(stderr);
62156567Sbostic 		fprintf(stderr, "cannot create file: %s\n", strerror(errno));
62211127Smckusick 		return (FAIL);
62311127Smckusick 	}
62412556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
62511127Smckusick 	dp = dup(dirp->dd_fd);
62611127Smckusick 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
62711127Smckusick 		size = i < BUFSIZ ? i : BUFSIZ;
62811127Smckusick 		if (read(dp, buf, (int) size) == -1) {
62911127Smckusick 			fprintf(stderr,
63011127Smckusick 				"write error extracting inode %d, name %s\n",
63111127Smckusick 				curfile.ino, curfile.name);
63256567Sbostic 			fprintf(stderr, "read: %s\n", strerror(errno));
63311127Smckusick 			done(1);
63411127Smckusick 		}
63534268Smckusick 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
63611127Smckusick 			fprintf(stderr,
63711127Smckusick 				"write error extracting inode %d, name %s\n",
63811127Smckusick 				curfile.ino, curfile.name);
63956567Sbostic 			fprintf(stderr, "write: %s\n", strerror(errno));
64011127Smckusick 			done(1);
64111127Smckusick 		}
64211127Smckusick 	}
64311732Smckusick 	(void) close(dp);
64411732Smckusick 	(void) close(ofile);
64511127Smckusick 	return (GOOD);
64611127Smckusick }
64711127Smckusick 
64811127Smckusick /*
64911992Smckusick  * Determine the type of an inode
65011992Smckusick  */
65156567Sbostic int
inodetype(ino)65211992Smckusick inodetype(ino)
65311992Smckusick 	ino_t ino;
65411992Smckusick {
65511992Smckusick 	struct inotab *itp;
65611992Smckusick 
65711992Smckusick 	itp = inotablookup(ino);
65811992Smckusick 	if (itp == NULL)
65911992Smckusick 		return (LEAF);
66011992Smckusick 	return (NODE);
66111992Smckusick }
66211992Smckusick 
66311992Smckusick /*
66411127Smckusick  * Allocate and initialize a directory inode entry.
66511127Smckusick  * If requested, save its pertinent mode, owner, and time info.
66611127Smckusick  */
66756567Sbostic static struct inotab *
allocinotab(ino,dip,seekpt)66811127Smckusick allocinotab(ino, dip, seekpt)
66911127Smckusick 	ino_t ino;
67011127Smckusick 	struct dinode *dip;
67150662Smckusick 	long seekpt;
67211127Smckusick {
67311127Smckusick 	register struct inotab	*itp;
67411127Smckusick 	struct modeinfo node;
67511127Smckusick 
67656567Sbostic 	itp = calloc(1, sizeof(struct inotab));
67756567Sbostic 	if (itp == NULL)
67813859Smckusick 		panic("no memory directory table\n");
67911127Smckusick 	itp->t_next = inotab[INOHASH(ino)];
68011127Smckusick 	inotab[INOHASH(ino)] = itp;
68111127Smckusick 	itp->t_ino = ino;
68211127Smckusick 	itp->t_seekpt = seekpt;
68311127Smckusick 	if (mf == NULL)
68457930Sbostic 		return (itp);
68511127Smckusick 	node.ino = ino;
686*69155Smckusick 	node.timep[0].tv_sec = dip->di_atime;
687*69155Smckusick 	node.timep[0].tv_usec = dip->di_atimensec / 1000;
688*69155Smckusick 	node.timep[1].tv_sec = dip->di_mtime;
689*69155Smckusick 	node.timep[1].tv_usec = dip->di_mtimensec / 1000;
69011127Smckusick 	node.mode = dip->di_mode;
69167738Smckusick 	node.flags = dip->di_flags;
69211127Smckusick 	node.uid = dip->di_uid;
69311127Smckusick 	node.gid = dip->di_gid;
69411732Smckusick 	(void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
69557930Sbostic 	return (itp);
69611127Smckusick }
69711127Smckusick 
69811127Smckusick /*
69911127Smckusick  * Look up an inode in the table of directories
70011127Smckusick  */
70156567Sbostic static struct inotab *
inotablookup(ino)70211127Smckusick inotablookup(ino)
70311127Smckusick 	ino_t	ino;
70411127Smckusick {
70511127Smckusick 	register struct inotab *itp;
70611127Smckusick 
70711127Smckusick 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
70811127Smckusick 		if (itp->t_ino == ino)
70957930Sbostic 			return (itp);
71056567Sbostic 	return (NULL);
71111127Smckusick }
71211127Smckusick 
71311127Smckusick /*
71411127Smckusick  * Clean up and exit
71511127Smckusick  */
71657930Sbostic __dead void
done(exitcode)71711127Smckusick done(exitcode)
71811127Smckusick 	int exitcode;
71911127Smckusick {
72011127Smckusick 
72111127Smckusick 	closemt();
72211992Smckusick 	if (modefile[0] != '#')
72311992Smckusick 		(void) unlink(modefile);
72411992Smckusick 	if (dirfile[0] != '#')
72511992Smckusick 		(void) unlink(dirfile);
72611127Smckusick 	exit(exitcode);
72711127Smckusick }
728