xref: /csrg-svn/sbin/restore/dirs.c (revision 56567)
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*56567Sbostic static char sccsid[] = "@(#)dirs.c	5.24 (Berkeley) 10/16/92";
1036105Sbostic #endif /* not lint */
1111127Smckusick 
12*56567Sbostic #include <sys/param.h>
1311309Smckusick #include <sys/file.h>
14*56567Sbostic #include <sys/stat.h>
15*56567Sbostic #include <sys/time.h>
16*56567Sbostic 
17*56567Sbostic #include <ufs/ffs/fs.h>
18*56567Sbostic #include <ufs/ufs/dinode.h>
1951625Sbostic #include <ufs/ufs/dir.h>
20*56567Sbostic #include <protocols/dumprestore.h>
21*56567Sbostic 
22*56567Sbostic #include <errno.h>
23*56567Sbostic #include <stdio.h>
24*56567Sbostic #include <stdlib.h>
25*56567Sbostic #include <string.h>
26*56567Sbostic #include <unistd.h>
27*56567Sbostic 
2837952Sbostic #include "pathnames.h"
29*56567Sbostic #include "restore.h"
30*56567Sbostic #include "extern.h"
3111127Smckusick 
3211992Smckusick /*
3311992Smckusick  * Symbol table of directories read from tape.
3411992Smckusick  */
3511127Smckusick #define HASHSIZE	1000
3611127Smckusick #define INOHASH(val) (val % HASHSIZE)
3711127Smckusick struct inotab {
3850662Smckusick 	struct	inotab *t_next;
3911127Smckusick 	ino_t	t_ino;
4050662Smckusick 	long	t_seekpt;
4150662Smckusick 	long	t_size;
4211309Smckusick };
4311309Smckusick static struct inotab *inotab[HASHSIZE];
4411127Smckusick 
4511992Smckusick /*
4611992Smckusick  * Information retained about directories.
4711992Smckusick  */
4811127Smckusick struct modeinfo {
4911127Smckusick 	ino_t ino;
5039471Smckusick 	struct timeval timep[2];
5111127Smckusick 	short mode;
5211127Smckusick 	short uid;
5311127Smckusick 	short gid;
5411127Smckusick };
5511127Smckusick 
5611992Smckusick /*
5740094Smckusick  * Definitions for library routines operating on directories.
5840094Smckusick  */
5946564Smckusick #undef DIRBLKSIZ
6046564Smckusick #define DIRBLKSIZ 1024
6150657Smckusick struct rstdirdesc {
6240094Smckusick 	int	dd_fd;
6340094Smckusick 	long	dd_loc;
6440094Smckusick 	long	dd_size;
6540094Smckusick 	char	dd_buf[DIRBLKSIZ];
6640094Smckusick };
6740094Smckusick 
6840094Smckusick /*
6911992Smckusick  * Global variables for this file.
7011992Smckusick  */
7150662Smckusick static long	seekpt;
7211309Smckusick static FILE	*df, *mf;
7350657Smckusick static RST_DIR	*dirp;
7411992Smckusick static char	dirfile[32] = "#";	/* No file */
7511992Smckusick static char	modefile[32] = "#";	/* No file */
7646566Smckusick static char	dot[2] = ".";		/* So it can be modified */
7711127Smckusick 
7811992Smckusick /*
7911992Smckusick  * Format of old style directories.
8011992Smckusick  */
8111127Smckusick #define ODIRSIZ 14
8211127Smckusick struct odirect {
8311127Smckusick 	u_short	d_ino;
8411127Smckusick 	char	d_name[ODIRSIZ];
8511127Smckusick };
8611127Smckusick 
87*56567Sbostic static struct inotab	*allocinotab __P((ino_t, struct dinode *, long));
88*56567Sbostic static void		 dcvt __P((struct odirect *, struct direct *));
89*56567Sbostic static void		 flushent __P((void));
90*56567Sbostic static struct inotab	*inotablookup __P((ino_t));
91*56567Sbostic static RST_DIR		*opendirfile __P((char *));
92*56567Sbostic static void		 putdir __P((char *, long));
93*56567Sbostic static void		 putent __P((struct direct *));
94*56567Sbostic static void		 rst_seekdir __P((RST_DIR *, long, long));
95*56567Sbostic static long		 rst_telldir __P((RST_DIR *));
96*56567Sbostic static ino_t		 searchdir __P((ino_t, char *));
97*56567Sbostic 
9811127Smckusick /*
9911127Smckusick  *	Extract directory contents, building up a directory structure
10011127Smckusick  *	on disk for extraction by name.
10111992Smckusick  *	If genmode is requested, save mode, owner, and times for all
10211127Smckusick  *	directories on the tape.
10311127Smckusick  */
104*56567Sbostic void
10511992Smckusick extractdirs(genmode)
10611992Smckusick 	int genmode;
10711127Smckusick {
10811127Smckusick 	register int i;
10911127Smckusick 	register struct dinode *ip;
11011322Smckusick 	struct inotab *itp;
11111127Smckusick 	struct direct nulldir;
11211127Smckusick 
11311127Smckusick 	vprintf(stdout, "Extract directories from tape\n");
11437952Sbostic 	(void) sprintf(dirfile, "%s/rstdir%d", _PATH_TMP, dumpdate);
11511127Smckusick 	df = fopen(dirfile, "w");
11611127Smckusick 	if (df == 0) {
11711127Smckusick 		fprintf(stderr,
11815779Smckusick 		    "restore: %s - cannot create directory temporary\n",
11911127Smckusick 		    dirfile);
120*56567Sbostic 		fprintf(stderr, "fopen: %s\n", strerror(errno));
12111127Smckusick 		done(1);
12211127Smckusick 	}
12311992Smckusick 	if (genmode != 0) {
12437952Sbostic 		(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
12511127Smckusick 		mf = fopen(modefile, "w");
12611309Smckusick 		if (mf == 0) {
12711127Smckusick 			fprintf(stderr,
12815779Smckusick 			    "restore: %s - cannot create modefile \n",
12911127Smckusick 			    modefile);
130*56567Sbostic 			fprintf(stderr, "fopen: %s\n", strerror(errno));
13111127Smckusick 			done(1);
13211127Smckusick 		}
13311127Smckusick 	}
13411322Smckusick 	nulldir.d_ino = 0;
13554582Smckusick 	nulldir.d_type = DT_DIR;
13611127Smckusick 	nulldir.d_namlen = 1;
13712453Smckusick 	(void) strcpy(nulldir.d_name, "/");
13854582Smckusick 	nulldir.d_reclen = DIRSIZ(0, &nulldir);
13911127Smckusick 	for (;;) {
14011127Smckusick 		curfile.name = "<directory file - name unknown>";
14111127Smckusick 		curfile.action = USING;
14211127Smckusick 		ip = curfile.dip;
14317948Smckusick 		if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
14411732Smckusick 			(void) fclose(df);
14540064Smckusick 			dirp = opendirfile(dirfile);
14611127Smckusick 			if (dirp == NULL)
147*56567Sbostic 				fprintf(stderr, "opendirfile: %s\n",
148*56567Sbostic 				    strerror(errno));
14911127Smckusick 			if (mf != NULL)
15011732Smckusick 				(void) fclose(mf);
15146566Smckusick 			i = dirlookup(dot);
15211992Smckusick 			if (i == 0)
15311421Smckusick 				panic("Root directory is not on tape\n");
15411127Smckusick 			return;
15511127Smckusick 		}
15611322Smckusick 		itp = allocinotab(curfile.ino, ip, seekpt);
15756434Smckusick 		getfile(putdir, xtrnull);
15811127Smckusick 		putent(&nulldir);
15911127Smckusick 		flushent();
16011322Smckusick 		itp->t_size = seekpt - itp->t_seekpt;
16111127Smckusick 	}
16211127Smckusick }
16311127Smckusick 
16411127Smckusick /*
16511322Smckusick  * skip over all the directories on the tape
16611322Smckusick  */
167*56567Sbostic void
16811322Smckusick skipdirs()
16911322Smckusick {
17011322Smckusick 
17111322Smckusick 	while ((curfile.dip->di_mode & IFMT) == IFDIR) {
17211322Smckusick 		skipfile();
17311322Smckusick 	}
17411322Smckusick }
17511322Smckusick 
17611322Smckusick /*
17711127Smckusick  *	Recursively find names and inumbers of all files in subtree
17811127Smckusick  *	pname and pass them off to be processed.
17911127Smckusick  */
180*56567Sbostic void
18111127Smckusick treescan(pname, ino, todo)
18211127Smckusick 	char *pname;
18311127Smckusick 	ino_t ino;
184*56567Sbostic 	long (*todo) __P((char *, ino_t, int));
18511127Smckusick {
18611127Smckusick 	register struct inotab *itp;
18712453Smckusick 	register struct direct *dp;
18811127Smckusick 	int namelen;
18950662Smckusick 	long bpt;
19011644Smckusick 	char locname[MAXPATHLEN + 1];
19111127Smckusick 
19211127Smckusick 	itp = inotablookup(ino);
19311127Smckusick 	if (itp == NULL) {
19411127Smckusick 		/*
19511127Smckusick 		 * Pname is name of a simple file or an unchanged directory.
19611127Smckusick 		 */
19711744Smckusick 		(void) (*todo)(pname, ino, LEAF);
19811127Smckusick 		return;
19911127Smckusick 	}
20011127Smckusick 	/*
20111127Smckusick 	 * Pname is a dumped directory name.
20211127Smckusick 	 */
20311744Smckusick 	if ((*todo)(pname, ino, NODE) == FAIL)
20411744Smckusick 		return;
20511127Smckusick 	/*
20611127Smckusick 	 * begin search through the directory
20711127Smckusick 	 * skipping over "." and ".."
20811127Smckusick 	 */
20911992Smckusick 	(void) strncpy(locname, pname, MAXPATHLEN);
21011992Smckusick 	(void) strncat(locname, "/", MAXPATHLEN);
21111127Smckusick 	namelen = strlen(locname);
21212556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
21312556Smckusick 	dp = rst_readdir(dirp); /* "." */
21427263Smckusick 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
21512556Smckusick 		dp = rst_readdir(dirp); /* ".." */
21627263Smckusick 	else
21727263Smckusick 		fprintf(stderr, "Warning: `.' missing from directory %s\n",
21827263Smckusick 			pname);
21927263Smckusick 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
22012556Smckusick 		dp = rst_readdir(dirp); /* first real entry */
22127263Smckusick 	else
22227263Smckusick 		fprintf(stderr, "Warning: `..' missing from directory %s\n",
22327263Smckusick 			pname);
22440064Smckusick 	bpt = rst_telldir(dirp);
22511127Smckusick 	/*
22612453Smckusick 	 * a zero inode signals end of directory
22711127Smckusick 	 */
22812453Smckusick 	while (dp != NULL && dp->d_ino != 0) {
22911127Smckusick 		locname[namelen] = '\0';
23011644Smckusick 		if (namelen + dp->d_namlen >= MAXPATHLEN) {
23111127Smckusick 			fprintf(stderr, "%s%s: name exceeds %d char\n",
23211644Smckusick 				locname, dp->d_name, MAXPATHLEN);
23311127Smckusick 		} else {
23411992Smckusick 			(void) strncat(locname, dp->d_name, (int)dp->d_namlen);
23511127Smckusick 			treescan(locname, dp->d_ino, todo);
23612556Smckusick 			rst_seekdir(dirp, bpt, itp->t_seekpt);
23711127Smckusick 		}
23812556Smckusick 		dp = rst_readdir(dirp);
23940064Smckusick 		bpt = rst_telldir(dirp);
24011127Smckusick 	}
24111127Smckusick 	if (dp == NULL)
24211127Smckusick 		fprintf(stderr, "corrupted directory: %s.\n", locname);
24311127Smckusick }
24411127Smckusick 
24511127Smckusick /*
24656434Smckusick  * Lookup a pathname which is always assumed to start from the ROOTINO.
24711127Smckusick  */
24811127Smckusick ino_t
24956434Smckusick pathsearch(pathname)
25056434Smckusick 	char *pathname;
25111127Smckusick {
25211127Smckusick 	ino_t ino;
25356434Smckusick 	char *name, buffer[MAXPATHLEN];
25411127Smckusick 
25556434Smckusick 	strcpy(buffer, pathname);
25656434Smckusick 	pathname = buffer;
25711127Smckusick 	ino = ROOTINO;
25856434Smckusick 	while (*pathname == '/')
25956434Smckusick 		pathname++;
26056434Smckusick 	while ((name = strsep(&pathname, "/")) != NULL)
26156434Smckusick 		if ((ino = searchdir(ino, name)) == 0)
26256434Smckusick 			return ((ino_t)0);
26356434Smckusick 	return (ino);
26411127Smckusick }
26511127Smckusick 
26611127Smckusick /*
26756434Smckusick  * Lookup the requested name in directory inum.
26856434Smckusick  * Return its inode number if found, zero if it does not exist.
26911127Smckusick  */
270*56567Sbostic static ino_t
27156434Smckusick searchdir(inum, name)
27211127Smckusick 	ino_t	inum;
27356434Smckusick 	char	*name;
27411127Smckusick {
27511127Smckusick 	register struct direct *dp;
27611127Smckusick 	register struct inotab *itp;
27711127Smckusick 	int len;
27811127Smckusick 
27911127Smckusick 	itp = inotablookup(inum);
28011127Smckusick 	if (itp == NULL)
28111127Smckusick 		return(0);
28212556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
28356434Smckusick 	len = strlen(name);
28411127Smckusick 	do {
28512556Smckusick 		dp = rst_readdir(dirp);
28612453Smckusick 		if (dp == NULL || dp->d_ino == 0)
28712453Smckusick 			return (0);
28856434Smckusick 	} while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
28911127Smckusick 	return(dp->d_ino);
29011127Smckusick }
29111127Smckusick 
29211127Smckusick /*
29311127Smckusick  * Put the directory entries in the directory file
29411127Smckusick  */
295*56567Sbostic static void
29611127Smckusick putdir(buf, size)
29711127Smckusick 	char *buf;
298*56567Sbostic 	long size;
29911127Smckusick {
30011127Smckusick 	struct direct cvtbuf;
30111127Smckusick 	register struct odirect *odp;
30211127Smckusick 	struct odirect *eodp;
30311127Smckusick 	register struct direct *dp;
30411127Smckusick 	long loc, i;
30511127Smckusick 
30611127Smckusick 	if (cvtflag) {
30711127Smckusick 		eodp = (struct odirect *)&buf[size];
30811127Smckusick 		for (odp = (struct odirect *)buf; odp < eodp; odp++)
30911127Smckusick 			if (odp->d_ino != 0) {
31011127Smckusick 				dcvt(odp, &cvtbuf);
31111127Smckusick 				putent(&cvtbuf);
31211127Smckusick 			}
31311127Smckusick 	} else {
31411127Smckusick 		for (loc = 0; loc < size; ) {
31511127Smckusick 			dp = (struct direct *)(buf + loc);
31654582Smckusick 			if (oldinofmt) {
31754582Smckusick 				if (Bcvt) {
318*56567Sbostic 					swabst((u_char *)"l2s", (u_char *) dp);
31954582Smckusick 				}
32054582Smckusick 			} else {
32154582Smckusick 				if (Bcvt) {
322*56567Sbostic 					swabst((u_char *)"ls", (u_char *) dp);
32354582Smckusick 				}
32426941Ssklower 			}
32511127Smckusick 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
32646564Smckusick 			if ((dp->d_reclen & 0x3) != 0 ||
32746564Smckusick 			    dp->d_reclen > i ||
32854582Smckusick 			    dp->d_reclen < DIRSIZ(0, dp) ||
32950662Smckusick 			    dp->d_namlen > NAME_MAX) {
33050662Smckusick 				vprintf(stdout, "Mangled directory: ");
33150662Smckusick 				if ((dp->d_reclen & 0x3) != 0)
33250662Smckusick 					vprintf(stdout,
33350662Smckusick 					   "reclen not multiple of 4 ");
33454582Smckusick 				if (dp->d_reclen < DIRSIZ(0, dp))
33550662Smckusick 					vprintf(stdout,
33650662Smckusick 					   "reclen less than DIRSIZ (%d < %d) ",
33754582Smckusick 					   dp->d_reclen, DIRSIZ(0, dp));
33850662Smckusick 				if (dp->d_namlen > NAME_MAX)
33950662Smckusick 					vprintf(stdout,
34050662Smckusick 					   "reclen name too big (%d > %d) ",
34150662Smckusick 					   dp->d_namlen, NAME_MAX);
34250662Smckusick 				vprintf(stdout, "\n");
34311127Smckusick 				loc += i;
34411127Smckusick 				continue;
34511127Smckusick 			}
34611127Smckusick 			loc += dp->d_reclen;
34711127Smckusick 			if (dp->d_ino != 0) {
34811127Smckusick 				putent(dp);
34911127Smckusick 			}
35011127Smckusick 		}
35111127Smckusick 	}
35211127Smckusick }
35311127Smckusick 
35411127Smckusick /*
35511127Smckusick  * These variables are "local" to the following two functions.
35611127Smckusick  */
35711127Smckusick char dirbuf[DIRBLKSIZ];
35811127Smckusick long dirloc = 0;
35911127Smckusick long prev = 0;
36011127Smckusick 
36111127Smckusick /*
36211127Smckusick  * add a new directory entry to a file.
36311127Smckusick  */
364*56567Sbostic static void
36511127Smckusick putent(dp)
36611127Smckusick 	struct direct *dp;
36711127Smckusick {
36854582Smckusick 	dp->d_reclen = DIRSIZ(0, dp);
36911127Smckusick 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
37011127Smckusick 		((struct direct *)(dirbuf + prev))->d_reclen =
37111127Smckusick 		    DIRBLKSIZ - prev;
37211732Smckusick 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
37311127Smckusick 		dirloc = 0;
37411127Smckusick 	}
37511127Smckusick 	bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen);
37611127Smckusick 	prev = dirloc;
37711127Smckusick 	dirloc += dp->d_reclen;
37811127Smckusick }
37911127Smckusick 
38011127Smckusick /*
38111127Smckusick  * flush out a directory that is finished.
38211127Smckusick  */
383*56567Sbostic static void
38411127Smckusick flushent()
38511127Smckusick {
38611127Smckusick 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
38711732Smckusick 	(void) fwrite(dirbuf, (int)dirloc, 1, df);
38811127Smckusick 	seekpt = ftell(df);
38911127Smckusick 	dirloc = 0;
39011127Smckusick }
39111127Smckusick 
392*56567Sbostic static void
39311127Smckusick dcvt(odp, ndp)
39411127Smckusick 	register struct odirect *odp;
39511127Smckusick 	register struct direct *ndp;
39611127Smckusick {
39711127Smckusick 
39811127Smckusick 	bzero((char *)ndp, (long)(sizeof *ndp));
39911127Smckusick 	ndp->d_ino =  odp->d_ino;
40054582Smckusick 	ndp->d_type = DT_UNKNOWN;
40111992Smckusick 	(void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
40211127Smckusick 	ndp->d_namlen = strlen(ndp->d_name);
40354582Smckusick 	ndp->d_reclen = DIRSIZ(0, ndp);
40411127Smckusick }
40511127Smckusick 
40611127Smckusick /*
40711127Smckusick  * Seek to an entry in a directory.
40840064Smckusick  * Only values returned by rst_telldir should be passed to rst_seekdir.
40911732Smckusick  * This routine handles many directories in a single file.
41011732Smckusick  * It takes the base of the directory in the file, plus
41111732Smckusick  * the desired seek offset into it.
41211127Smckusick  */
413*56567Sbostic static void
41412556Smckusick rst_seekdir(dirp, loc, base)
41550657Smckusick 	register RST_DIR *dirp;
41650662Smckusick 	long loc, base;
41711127Smckusick {
41811127Smckusick 
41940064Smckusick 	if (loc == rst_telldir(dirp))
42011127Smckusick 		return;
42111127Smckusick 	loc -= base;
42211127Smckusick 	if (loc < 0)
42312556Smckusick 		fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
424*56567Sbostic 	(void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
42511127Smckusick 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
42611127Smckusick 	if (dirp->dd_loc != 0)
42711127Smckusick 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
42811127Smckusick }
42911127Smckusick 
43011127Smckusick /*
43111127Smckusick  * get next entry in a directory.
43211127Smckusick  */
43311127Smckusick struct direct *
43412556Smckusick rst_readdir(dirp)
43550657Smckusick 	register RST_DIR *dirp;
43611127Smckusick {
43711127Smckusick 	register struct direct *dp;
43811127Smckusick 
43911127Smckusick 	for (;;) {
44011127Smckusick 		if (dirp->dd_loc == 0) {
44111127Smckusick 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
44211127Smckusick 			    DIRBLKSIZ);
44312453Smckusick 			if (dirp->dd_size <= 0) {
44412453Smckusick 				dprintf(stderr, "error reading directory\n");
445*56567Sbostic 				return (NULL);
44612453Smckusick 			}
44711127Smckusick 		}
44811127Smckusick 		if (dirp->dd_loc >= dirp->dd_size) {
44911127Smckusick 			dirp->dd_loc = 0;
45011127Smckusick 			continue;
45111127Smckusick 		}
45211127Smckusick 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
45311127Smckusick 		if (dp->d_reclen == 0 ||
45412453Smckusick 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
45512453Smckusick 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
45612453Smckusick 				dp->d_reclen);
457*56567Sbostic 			return (NULL);
45812453Smckusick 		}
45911127Smckusick 		dirp->dd_loc += dp->d_reclen;
46012453Smckusick 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
46112453Smckusick 			continue;
46212556Smckusick 		if (dp->d_ino >= maxino) {
46312453Smckusick 			dprintf(stderr, "corrupted directory: bad inum %d\n",
46412453Smckusick 				dp->d_ino);
46512453Smckusick 			continue;
46612453Smckusick 		}
46711127Smckusick 		return (dp);
46811127Smckusick 	}
46911127Smckusick }
47011127Smckusick 
47111127Smckusick /*
47217753Smckusick  * Simulate the opening of a directory
47317753Smckusick  */
47450657Smckusick RST_DIR *
47517753Smckusick rst_opendir(name)
47617753Smckusick 	char *name;
47717753Smckusick {
47817753Smckusick 	struct inotab *itp;
47917753Smckusick 	ino_t ino;
48017753Smckusick 
48117753Smckusick 	if ((ino = dirlookup(name)) > 0 &&
48217753Smckusick 	    (itp = inotablookup(ino)) != NULL) {
48317753Smckusick 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
48417753Smckusick 		return (dirp);
48517753Smckusick 	}
48617753Smckusick 	return (0);
48717753Smckusick }
48817753Smckusick 
48917753Smckusick /*
49040064Smckusick  * Simulate finding the current offset in the directory.
49140064Smckusick  */
492*56567Sbostic static long
49340064Smckusick rst_telldir(dirp)
49450657Smckusick 	RST_DIR *dirp;
49540064Smckusick {
496*56567Sbostic 	return ((long)lseek(dirp->dd_fd,
497*56567Sbostic 	    (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
49840064Smckusick }
49940064Smckusick 
50040064Smckusick /*
50140064Smckusick  * Open a directory file.
50240064Smckusick  */
503*56567Sbostic static RST_DIR *
50440064Smckusick opendirfile(name)
50540064Smckusick 	char *name;
50640064Smckusick {
50750657Smckusick 	register RST_DIR *dirp;
50840064Smckusick 	register int fd;
50940064Smckusick 
510*56567Sbostic 	if ((fd = open(name, O_RDONLY)) == -1)
511*56567Sbostic 		return (NULL);
512*56567Sbostic 	if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
513*56567Sbostic 		(void)close(fd);
514*56567Sbostic 		return (NULL);
51540064Smckusick 	}
51640064Smckusick 	dirp->dd_fd = fd;
51740064Smckusick 	dirp->dd_loc = 0;
518*56567Sbostic 	return (dirp);
51940064Smckusick }
52040064Smckusick 
52140064Smckusick /*
52211127Smckusick  * Set the mode, owner, and times for all new or changed directories
52311127Smckusick  */
524*56567Sbostic void
52555880Smckusick setdirmodes(flags)
52655880Smckusick 	int flags;
52711127Smckusick {
52811127Smckusick 	FILE *mf;
52911127Smckusick 	struct modeinfo node;
53011127Smckusick 	struct entry *ep;
53111127Smckusick 	char *cp;
53211127Smckusick 
53311127Smckusick 	vprintf(stdout, "Set directory mode, owner, and times.\n");
53437952Sbostic 	(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
53511127Smckusick 	mf = fopen(modefile, "r");
53611127Smckusick 	if (mf == NULL) {
537*56567Sbostic 		fprintf(stderr, "fopen: %s\n", strerror(errno));
53815779Smckusick 		fprintf(stderr, "cannot open mode file %s\n", modefile);
53915779Smckusick 		fprintf(stderr, "directory mode, owner, and times not set\n");
54015779Smckusick 		return;
54111127Smckusick 	}
54211127Smckusick 	clearerr(mf);
54311309Smckusick 	for (;;) {
54411732Smckusick 		(void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
54511309Smckusick 		if (feof(mf))
54611309Smckusick 			break;
54711127Smckusick 		ep = lookupino(node.ino);
54814453Smckusick 		if (command == 'i' || command == 'x') {
549*56567Sbostic 			if (ep == NULL)
55011309Smckusick 				continue;
55155880Smckusick 			if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
55218008Smckusick 				ep->e_flags &= ~NEW;
55318008Smckusick 				continue;
55418008Smckusick 			}
55514453Smckusick 			if (node.ino == ROOTINO &&
55614453Smckusick 		   	    reply("set owner/mode for '.'") == FAIL)
55714453Smckusick 				continue;
55814453Smckusick 		}
559*56567Sbostic 		if (ep == NULL) {
56011127Smckusick 			panic("cannot find directory inode %d\n", node.ino);
56142862Smckusick 		} else {
56242862Smckusick 			cp = myname(ep);
56342862Smckusick 			(void) chown(cp, node.uid, node.gid);
56442862Smckusick 			(void) chmod(cp, node.mode);
56542862Smckusick 			utimes(cp, node.timep);
56642862Smckusick 			ep->e_flags &= ~NEW;
56742862Smckusick 		}
56811127Smckusick 	}
56911127Smckusick 	if (ferror(mf))
57011127Smckusick 		panic("error setting directory modes\n");
57111732Smckusick 	(void) fclose(mf);
57211127Smckusick }
57311127Smckusick 
57411127Smckusick /*
57511127Smckusick  * Generate a literal copy of a directory.
57611127Smckusick  */
577*56567Sbostic int
57811127Smckusick genliteraldir(name, ino)
57911127Smckusick 	char *name;
58011127Smckusick 	ino_t ino;
58111127Smckusick {
58211127Smckusick 	register struct inotab *itp;
58311127Smckusick 	int ofile, dp, i, size;
58411127Smckusick 	char buf[BUFSIZ];
58511127Smckusick 
58611127Smckusick 	itp = inotablookup(ino);
58711127Smckusick 	if (itp == NULL)
58811322Smckusick 		panic("Cannot find directory inode %d named %s\n", ino, name);
58912893Ssam 	if ((ofile = creat(name, 0666)) < 0) {
59012556Smckusick 		fprintf(stderr, "%s: ", name);
59112556Smckusick 		(void) fflush(stderr);
592*56567Sbostic 		fprintf(stderr, "cannot create file: %s\n", strerror(errno));
59311127Smckusick 		return (FAIL);
59411127Smckusick 	}
59512556Smckusick 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
59611127Smckusick 	dp = dup(dirp->dd_fd);
59711127Smckusick 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
59811127Smckusick 		size = i < BUFSIZ ? i : BUFSIZ;
59911127Smckusick 		if (read(dp, buf, (int) size) == -1) {
60011127Smckusick 			fprintf(stderr,
60111127Smckusick 				"write error extracting inode %d, name %s\n",
60211127Smckusick 				curfile.ino, curfile.name);
603*56567Sbostic 			fprintf(stderr, "read: %s\n", strerror(errno));
60411127Smckusick 			done(1);
60511127Smckusick 		}
60634268Smckusick 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
60711127Smckusick 			fprintf(stderr,
60811127Smckusick 				"write error extracting inode %d, name %s\n",
60911127Smckusick 				curfile.ino, curfile.name);
610*56567Sbostic 			fprintf(stderr, "write: %s\n", strerror(errno));
61111127Smckusick 			done(1);
61211127Smckusick 		}
61311127Smckusick 	}
61411732Smckusick 	(void) close(dp);
61511732Smckusick 	(void) close(ofile);
61611127Smckusick 	return (GOOD);
61711127Smckusick }
61811127Smckusick 
61911127Smckusick /*
62011992Smckusick  * Determine the type of an inode
62111992Smckusick  */
622*56567Sbostic int
62311992Smckusick inodetype(ino)
62411992Smckusick 	ino_t ino;
62511992Smckusick {
62611992Smckusick 	struct inotab *itp;
62711992Smckusick 
62811992Smckusick 	itp = inotablookup(ino);
62911992Smckusick 	if (itp == NULL)
63011992Smckusick 		return (LEAF);
63111992Smckusick 	return (NODE);
63211992Smckusick }
63311992Smckusick 
63411992Smckusick /*
63511127Smckusick  * Allocate and initialize a directory inode entry.
63611127Smckusick  * If requested, save its pertinent mode, owner, and time info.
63711127Smckusick  */
638*56567Sbostic static struct inotab *
63911127Smckusick allocinotab(ino, dip, seekpt)
64011127Smckusick 	ino_t ino;
64111127Smckusick 	struct dinode *dip;
64250662Smckusick 	long seekpt;
64311127Smckusick {
64411127Smckusick 	register struct inotab	*itp;
64511127Smckusick 	struct modeinfo node;
64611127Smckusick 
647*56567Sbostic 	itp = calloc(1, sizeof(struct inotab));
648*56567Sbostic 	if (itp == NULL)
64913859Smckusick 		panic("no memory directory table\n");
65011127Smckusick 	itp->t_next = inotab[INOHASH(ino)];
65111127Smckusick 	inotab[INOHASH(ino)] = itp;
65211127Smckusick 	itp->t_ino = ino;
65311127Smckusick 	itp->t_seekpt = seekpt;
65411127Smckusick 	if (mf == NULL)
65511322Smckusick 		return(itp);
65611127Smckusick 	node.ino = ino;
65754156Smckusick 	node.timep[0].tv_sec = dip->di_atime.ts_sec;
65854156Smckusick 	node.timep[0].tv_usec = dip->di_atime.ts_nsec / 1000;
65954156Smckusick 	node.timep[1].tv_sec = dip->di_mtime.ts_sec;
66054156Smckusick 	node.timep[1].tv_usec = dip->di_mtime.ts_nsec / 1000;
66111127Smckusick 	node.mode = dip->di_mode;
66211127Smckusick 	node.uid = dip->di_uid;
66311127Smckusick 	node.gid = dip->di_gid;
66411732Smckusick 	(void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
66511322Smckusick 	return(itp);
66611127Smckusick }
66711127Smckusick 
66811127Smckusick /*
66911127Smckusick  * Look up an inode in the table of directories
67011127Smckusick  */
671*56567Sbostic static struct inotab *
67211127Smckusick inotablookup(ino)
67311127Smckusick 	ino_t	ino;
67411127Smckusick {
67511127Smckusick 	register struct inotab *itp;
67611127Smckusick 
67711127Smckusick 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
67811127Smckusick 		if (itp->t_ino == ino)
67911127Smckusick 			return(itp);
680*56567Sbostic 	return (NULL);
68111127Smckusick }
68211127Smckusick 
68311127Smckusick /*
68411127Smckusick  * Clean up and exit
68511127Smckusick  */
686*56567Sbostic void
68711127Smckusick done(exitcode)
68811127Smckusick 	int exitcode;
68911127Smckusick {
69011127Smckusick 
69111127Smckusick 	closemt();
69211992Smckusick 	if (modefile[0] != '#')
69311992Smckusick 		(void) unlink(modefile);
69411992Smckusick 	if (dirfile[0] != '#')
69511992Smckusick 		(void) unlink(dirfile);
69611127Smckusick 	exit(exitcode);
69711127Smckusick }
698