xref: /onnv-gate/usr/src/cmd/backup/restore/dirs.c (revision 1053:667012633a0d)
10Sstevel@tonic-gate /*
2426Srm88369  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3426Srm88369  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
70Sstevel@tonic-gate /*	  All Rights Reserved	*/
80Sstevel@tonic-gate 
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
110Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
120Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
130Sstevel@tonic-gate  */
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
160Sstevel@tonic-gate 
170Sstevel@tonic-gate #include "restore.h"
180Sstevel@tonic-gate #include <byteorder.h>
190Sstevel@tonic-gate #include <stdlib.h>
200Sstevel@tonic-gate #include <unistd.h>
210Sstevel@tonic-gate #include <utime.h>
220Sstevel@tonic-gate 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Symbol table of directories read from tape.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate #define	HASHSIZE	1000
270Sstevel@tonic-gate #define	INOHASH(val) (val % HASHSIZE)
280Sstevel@tonic-gate struct inotab {
290Sstevel@tonic-gate 	struct inotab *t_next;
300Sstevel@tonic-gate 	ino_t	t_ino;
310Sstevel@tonic-gate 	offset_t t_seekpt;
320Sstevel@tonic-gate 	offset_t t_size;
330Sstevel@tonic-gate 	struct inotab *t_xattr;
340Sstevel@tonic-gate };
350Sstevel@tonic-gate static struct inotab *inotab[HASHSIZE];
360Sstevel@tonic-gate static struct inotab *xattrlist = NULL;
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * Information retained about directories.
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate static struct modeinfo {
420Sstevel@tonic-gate 	ino_t	ino;
430Sstevel@tonic-gate 	time_t	timep[2];
440Sstevel@tonic-gate 	mode_t	mode;
450Sstevel@tonic-gate 	uid_t	uid;
460Sstevel@tonic-gate 	gid_t	gid;
470Sstevel@tonic-gate 	size_t	metasize;
480Sstevel@tonic-gate } node;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Global variables for this file.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate static off64_t	g_seekpt;		/* some people have a local seekpt */
540Sstevel@tonic-gate static FILE	*df, *mf;
550Sstevel@tonic-gate static char	dirfile[MAXPATHLEN] = "#";	/* No file */
560Sstevel@tonic-gate static char	modefile[MAXPATHLEN] = "#";	/* No file */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static RST_DIR	*dirp;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #define	INIT_TEMPFILE(name, type) \
610Sstevel@tonic-gate 	if (name[0] == '#') { \
620Sstevel@tonic-gate 		if (tmpdir == (char *)NULL) /* can't happen; be paranoid */ \
630Sstevel@tonic-gate 			tmpdir = "/tmp"; \
640Sstevel@tonic-gate 		(void) snprintf(name, sizeof (name), \
650Sstevel@tonic-gate 		    "%s/rst" type "%ld.XXXXXX", tmpdir, dumpdate); \
660Sstevel@tonic-gate 		(void) mktemp(name); \
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	INIT_DIRFILE()	INIT_TEMPFILE(dirfile, "dir")
700Sstevel@tonic-gate #define	INIT_MODEFILE()	INIT_TEMPFILE(modefile, "mode")
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Format of old style directories.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate #define	ODIRSIZ 14
760Sstevel@tonic-gate struct odirect {
770Sstevel@tonic-gate 	ushort_t d_ino;
780Sstevel@tonic-gate 	char	d_name[ODIRSIZ];
790Sstevel@tonic-gate };
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #ifdef __STDC__
820Sstevel@tonic-gate static ino_t search(ino_t, char	*);
830Sstevel@tonic-gate static void putdir(char *, size_t);
840Sstevel@tonic-gate static void putent(struct direct *);
850Sstevel@tonic-gate static void skipmetadata(FILE *, size_t);
860Sstevel@tonic-gate static void flushent(void);
870Sstevel@tonic-gate static void dcvt(struct odirect *, struct direct *);
880Sstevel@tonic-gate static RST_DIR *rst_initdirfile(char *);
890Sstevel@tonic-gate static offset_t rst_telldir(RST_DIR *);
900Sstevel@tonic-gate static void rst_seekdir(RST_DIR *, offset_t, offset_t);
910Sstevel@tonic-gate static struct inotab *allocinotab(ino_t, struct dinode *, off64_t);
920Sstevel@tonic-gate static void nodeflush(void);
930Sstevel@tonic-gate static struct inotab *inotablookup(ino_t);
940Sstevel@tonic-gate #else
950Sstevel@tonic-gate static ino_t search();
960Sstevel@tonic-gate static void putdir();
970Sstevel@tonic-gate static void putent();
980Sstevel@tonic-gate static void skipmetadata();
990Sstevel@tonic-gate static void flushent();
1000Sstevel@tonic-gate static void dcvt();
1010Sstevel@tonic-gate static RST_DIR *rst_initdirfile();
1020Sstevel@tonic-gate static offset_t rst_telldir();
1030Sstevel@tonic-gate static void rst_seekdir();
1040Sstevel@tonic-gate static struct inotab *allocinotab();
1050Sstevel@tonic-gate static void nodeflush();
1060Sstevel@tonic-gate static struct inotab *inotablookup();
1070Sstevel@tonic-gate #endif
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  *	Extract directory contents, building up a directory structure
1110Sstevel@tonic-gate  *	on disk for extraction by name.
1120Sstevel@tonic-gate  *	If genmode is requested, save mode, owner, and times for all
1130Sstevel@tonic-gate  *	directories on the tape.
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate void
extractdirs(int genmode)116*1053Smaheshvs extractdirs(int genmode)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	int ts;
1190Sstevel@tonic-gate 	struct dinode *ip;
1200Sstevel@tonic-gate 	int saverr;
1210Sstevel@tonic-gate 	struct inotab *itp;
1220Sstevel@tonic-gate 	struct direct nulldir;
1230Sstevel@tonic-gate 	static char dotname[] = "."; /* dirlookup/psearch writes to its arg */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	vprintf(stdout, gettext("Extract directories from tape\n"));
1260Sstevel@tonic-gate 	INIT_DIRFILE();
1270Sstevel@tonic-gate 	if ((df = safe_fopen(dirfile, "w", 0600)) == (FILE *)NULL) {
1280Sstevel@tonic-gate 		saverr = errno;
1290Sstevel@tonic-gate 		(void) fprintf(stderr,
1300Sstevel@tonic-gate 		    gettext("%s: %s - cannot create directory temporary\n"),
1310Sstevel@tonic-gate 			progname, dirfile);
1320Sstevel@tonic-gate 		errno = saverr;
1330Sstevel@tonic-gate 		perror("fopen");
1340Sstevel@tonic-gate 		done(1);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 	if (genmode != 0) {
1370Sstevel@tonic-gate 		INIT_MODEFILE();
1380Sstevel@tonic-gate 		if ((mf = safe_fopen(modefile, "w", 0600)) == (FILE *)NULL) {
1390Sstevel@tonic-gate 			saverr = errno;
1400Sstevel@tonic-gate 			(void) fprintf(stderr,
1410Sstevel@tonic-gate 			    gettext("%s: %s - cannot create modefile \n"),
1420Sstevel@tonic-gate 				progname, modefile);
1430Sstevel@tonic-gate 			errno = saverr;
1440Sstevel@tonic-gate 			perror("fopen");
1450Sstevel@tonic-gate 			done(1);
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	nulldir.d_ino = 0;
1490Sstevel@tonic-gate 	nulldir.d_namlen = 1;
1500Sstevel@tonic-gate 	(void) strcpy(nulldir.d_name, "/");
1510Sstevel@tonic-gate 	/* LINTED DIRSIZ will always fit into a ushort_t */
1520Sstevel@tonic-gate 	nulldir.d_reclen = (ushort_t)DIRSIZ(&nulldir);
1530Sstevel@tonic-gate 	/* LINTED sign extension ok in assert */
1540Sstevel@tonic-gate 	assert(DIRSIZ(&nulldir) == (ulong_t)nulldir.d_reclen);
1550Sstevel@tonic-gate 	for (;;) {
1560Sstevel@tonic-gate 		curfile.name = gettext("<directory file - name unknown>");
1570Sstevel@tonic-gate 		curfile.action = USING;
1580Sstevel@tonic-gate 		ip = curfile.dip;
1590Sstevel@tonic-gate 		ts = curfile.ts;
1600Sstevel@tonic-gate 		if (ts != TS_END && ts != TS_INODE) {
1610Sstevel@tonic-gate 			getfile(null, null);
1620Sstevel@tonic-gate 			continue;
1630Sstevel@tonic-gate 		}
1640Sstevel@tonic-gate 		if (ts == TS_INODE && ip == NULL) {
1650Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1660Sstevel@tonic-gate "%s: extractdirs: Failed internal consistency check, curfile.dip is NULL\n"),
1670Sstevel@tonic-gate 			    progname);
1680Sstevel@tonic-gate 			done(1);
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 		if ((ts == TS_INODE && (ip->di_mode & IFMT) != IFDIR &&
1710Sstevel@tonic-gate 		    (ip->di_mode & IFMT) != IFATTRDIR) ||
1720Sstevel@tonic-gate 		    (ts == TS_END)) {
1730Sstevel@tonic-gate 			(void) fflush(df);
1740Sstevel@tonic-gate 			/* XXX Legitimate error, bad complaint string */
1750Sstevel@tonic-gate 			if (ferror(df))
1760Sstevel@tonic-gate 				panic("%s: %s\n", dirfile, strerror(errno));
1770Sstevel@tonic-gate 			(void) fclose(df);
1780Sstevel@tonic-gate 			rst_closedir(dirp);
1790Sstevel@tonic-gate 			dirp = rst_initdirfile(dirfile);
1800Sstevel@tonic-gate 			if (dirp == NULL)
1810Sstevel@tonic-gate 				perror("initdirfile");
1820Sstevel@tonic-gate 			if (mf != NULL) {
1830Sstevel@tonic-gate 				(void) fflush(mf);
1840Sstevel@tonic-gate 				/* XXX Legitimate error, bad complaint string */
1850Sstevel@tonic-gate 				if (ferror(mf))
1860Sstevel@tonic-gate 					panic("%s: %s\n",
1870Sstevel@tonic-gate 					    modefile, strerror(errno));
1880Sstevel@tonic-gate 				(void) fclose(mf);
1890Sstevel@tonic-gate 			}
1900Sstevel@tonic-gate 			if (dirlookup(dotname) == 0) {
1910Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1920Sstevel@tonic-gate 				    "Root directory is not on tape\n"));
1930Sstevel@tonic-gate 				done(1);
1940Sstevel@tonic-gate 			}
1950Sstevel@tonic-gate 			return;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 		itp = allocinotab(curfile.ino, ip, g_seekpt);
1980Sstevel@tonic-gate 		getfile(putdir, null);
1990Sstevel@tonic-gate 		if (mf != NULL)
2000Sstevel@tonic-gate 			nodeflush();
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		putent(&nulldir);
2030Sstevel@tonic-gate 		flushent();
2040Sstevel@tonic-gate 		itp->t_size = g_seekpt - itp->t_seekpt;
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  * skip over all the directories on the tape
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate void
skipdirs()2120Sstevel@tonic-gate skipdirs()
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	while (curfile.dip != NULL &&
2150Sstevel@tonic-gate 		((curfile.dip->di_mode & IFMT) == IFDIR ||
2160Sstevel@tonic-gate 		(curfile.dip->di_mode & IFMT) == IFATTRDIR)) {
2170Sstevel@tonic-gate 		skipfile();
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate  *	Recursively find names and inumbers of all files in subtree
2230Sstevel@tonic-gate  *	pname and pass them off to be processed.
2240Sstevel@tonic-gate  */
2250Sstevel@tonic-gate void
treescan(char * pname,ino_t ino,long (* todo)())226*1053Smaheshvs treescan(char *pname, ino_t ino, long (*todo)())
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	struct inotab *itp;
2290Sstevel@tonic-gate 	struct direct *dp;
2300Sstevel@tonic-gate 	uint_t loclen;
2310Sstevel@tonic-gate 	offset_t bpt;
2320Sstevel@tonic-gate 	char locname[MAXCOMPLEXLEN];
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	itp = inotablookup(ino);
2350Sstevel@tonic-gate 	if (itp == NULL) {
2360Sstevel@tonic-gate 		/*
2370Sstevel@tonic-gate 		 * Pname is name of a simple file or an unchanged directory.
2380Sstevel@tonic-gate 		 */
2390Sstevel@tonic-gate 		(void) (*todo)(pname, ino, LEAF);
2400Sstevel@tonic-gate 		return;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * Pname is a dumped directory name.
2440Sstevel@tonic-gate 	 */
2450Sstevel@tonic-gate 	if ((*todo)(pname, ino, NODE) == FAIL)
2460Sstevel@tonic-gate 		return;
2470Sstevel@tonic-gate 	/*
2480Sstevel@tonic-gate 	 * begin search through the directory
2490Sstevel@tonic-gate 	 * skipping over "." and ".."
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate 	loclen = complexcpy(locname, pname, MAXCOMPLEXLEN);
2520Sstevel@tonic-gate 	locname[loclen-1] = '/';
2530Sstevel@tonic-gate 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
2540Sstevel@tonic-gate 	dp = rst_readdir(dirp); /* "." */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
2570Sstevel@tonic-gate 		dp = rst_readdir(dirp); /* ".." */
2580Sstevel@tonic-gate 	else
2590Sstevel@tonic-gate 		(void) fprintf(stderr,
2600Sstevel@tonic-gate 		    gettext("Warning: `.' missing from directory %s\n"),
2610Sstevel@tonic-gate 			pname);
2620Sstevel@tonic-gate 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
2630Sstevel@tonic-gate 		dp = rst_readdir(dirp); /* first real entry */
2640Sstevel@tonic-gate 	else
2650Sstevel@tonic-gate 		(void) fprintf(stderr,
2660Sstevel@tonic-gate 		    gettext("Warning: `..' missing from directory %s\n"),
2670Sstevel@tonic-gate 			pname);
2680Sstevel@tonic-gate 	bpt = rst_telldir(dirp);
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * a zero inode signals end of directory
2710Sstevel@tonic-gate 	 */
2720Sstevel@tonic-gate 	while (dp != NULL && dp->d_ino != 0) {
2730Sstevel@tonic-gate 		locname[loclen] = '\0';
2740Sstevel@tonic-gate 		if ((loclen + dp->d_namlen) >= (sizeof (locname) - 2)) {
2750Sstevel@tonic-gate 			(void) fprintf(stderr,
2760Sstevel@tonic-gate 			    gettext(
2770Sstevel@tonic-gate 				"%s%s: ignoring name that exceeds %d char\n"),
2780Sstevel@tonic-gate 			    locname, dp->d_name, MAXCOMPLEXLEN);
2790Sstevel@tonic-gate 		} else {
2800Sstevel@tonic-gate 			/* Always fits by if() condition */
2810Sstevel@tonic-gate 			(void) strcpy(locname + loclen, dp->d_name);
2820Sstevel@tonic-gate 			/* put a double null on string for lookupname() */
2830Sstevel@tonic-gate 			locname[loclen+dp->d_namlen+1] = '\0';
2840Sstevel@tonic-gate 			treescan(locname, dp->d_ino, todo);
2850Sstevel@tonic-gate 			rst_seekdir(dirp, bpt, itp->t_seekpt);
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 		dp = rst_readdir(dirp);
2880Sstevel@tonic-gate 		bpt = rst_telldir(dirp);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 	if (dp == NULL)
2910Sstevel@tonic-gate 		(void) fprintf(stderr,
2920Sstevel@tonic-gate 			gettext("corrupted directory: %s.\n"), locname);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate  * Scan the directory table looking for extended attribute trees.
2970Sstevel@tonic-gate  * Recursively find names and inumbers in each tree and pass them
2980Sstevel@tonic-gate  * off to be processed.  If the always parameter is not set, only
2990Sstevel@tonic-gate  * process the attribute tree if the attribute tree parent is to
3000Sstevel@tonic-gate  * be extracted.
3010Sstevel@tonic-gate  */
3020Sstevel@tonic-gate void
attrscan(int always,long (* todo)())303*1053Smaheshvs attrscan(int always, long (*todo)())
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	struct inotab *itp;
3060Sstevel@tonic-gate 	struct entry *ep, *parent;
3070Sstevel@tonic-gate 	struct direct *dp;
3080Sstevel@tonic-gate 	char name[MAXCOMPLEXLEN];
3090Sstevel@tonic-gate 	int len;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	for (itp = xattrlist; itp != NULL; itp = itp->t_xattr) {
3120Sstevel@tonic-gate 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
3130Sstevel@tonic-gate 		if ((dp = rst_readdir(dirp)) != NULL &&	/* "." */
3140Sstevel@tonic-gate 		    (dp = rst_readdir(dirp)) != NULL &&	/* ".." */
3150Sstevel@tonic-gate 		    strcmp(dp->d_name, "..") == 0) {
3160Sstevel@tonic-gate 			if ((parent = lookupino(dp->d_ino)) != NULL) {
3170Sstevel@tonic-gate 				if (!always &&
3180Sstevel@tonic-gate 				    (parent->e_flags & (NEW|EXTRACT)) == 0)
3190Sstevel@tonic-gate 					continue;
3200Sstevel@tonic-gate 				len = complexcpy(name, myname(parent),
3210Sstevel@tonic-gate 							MAXCOMPLEXLEN - 3);
3220Sstevel@tonic-gate 				name[len] = '.';
3230Sstevel@tonic-gate 				name[len+1] = '\0';
3240Sstevel@tonic-gate 				name[len+2] = '\0';
3250Sstevel@tonic-gate 				inattrspace = 1;
3260Sstevel@tonic-gate 				if ((ep = lookupino(itp->t_ino)) == NULL) {
3270Sstevel@tonic-gate 					ep = addentry(name, itp->t_ino,
3280Sstevel@tonic-gate 								NODE|ROOT);
3290Sstevel@tonic-gate 				}
3300Sstevel@tonic-gate 				ep->e_flags |= XATTRROOT;
3310Sstevel@tonic-gate 				treescan(name, itp->t_ino, todo);
3320Sstevel@tonic-gate 				inattrspace = 0;
3330Sstevel@tonic-gate 			} else {
3340Sstevel@tonic-gate 				(void) fprintf(stderr,
3350Sstevel@tonic-gate 			gettext("Warning: orphaned attribute directory\n"));
3360Sstevel@tonic-gate 			}
3370Sstevel@tonic-gate 		} else {
3380Sstevel@tonic-gate 			(void) fprintf(stderr,
3390Sstevel@tonic-gate 	    gettext("Warning: `..' missing from attribute directory\n"));
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate  * Search the directory tree rooted at inode ROOTINO
3460Sstevel@tonic-gate  * for the path pointed at by n.  Note that n must be
3470Sstevel@tonic-gate  * modifiable, although it is returned in the same
3480Sstevel@tonic-gate  * condition it was given to us in.
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate ino_t
psearch(char * n)351*1053Smaheshvs psearch(char *n)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	char *cp, *cp1;
3540Sstevel@tonic-gate 	ino_t ino;
3550Sstevel@tonic-gate 	char c;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	ino = ROOTINO;
3580Sstevel@tonic-gate 	if (*(cp = n) == '/')
3590Sstevel@tonic-gate 		cp++;
3600Sstevel@tonic-gate next:
3610Sstevel@tonic-gate 	cp1 = cp + 1;
3620Sstevel@tonic-gate 	while (*cp1 != '/' && *cp1)
3630Sstevel@tonic-gate 		cp1++;
3640Sstevel@tonic-gate 	c = *cp1;
3650Sstevel@tonic-gate 	*cp1 = 0;
3660Sstevel@tonic-gate 	ino = search(ino, cp);
3670Sstevel@tonic-gate 	if (ino == 0) {
3680Sstevel@tonic-gate 		*cp1 = c;
3690Sstevel@tonic-gate 		return (0);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 	*cp1 = c;
3720Sstevel@tonic-gate 	if (c == '/') {
3730Sstevel@tonic-gate 		cp = cp1+1;
3740Sstevel@tonic-gate 		goto next;
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 	return (ino);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate  * search the directory inode ino
3810Sstevel@tonic-gate  * looking for entry cp
3820Sstevel@tonic-gate  */
3830Sstevel@tonic-gate static ino_t
search(ino_t inum,char * cp)384*1053Smaheshvs search(ino_t inum, char *cp)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	struct direct *dp;
3870Sstevel@tonic-gate 	struct inotab *itp;
3880Sstevel@tonic-gate 	uint_t len;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	itp = inotablookup(inum);
3910Sstevel@tonic-gate 	if (itp == NULL)
3920Sstevel@tonic-gate 		return (0);
3930Sstevel@tonic-gate 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
3940Sstevel@tonic-gate 	len = strlen(cp);
3950Sstevel@tonic-gate 	do {
3960Sstevel@tonic-gate 		dp = rst_readdir(dirp);
3970Sstevel@tonic-gate 		if (dp == NULL || dp->d_ino == 0)
3980Sstevel@tonic-gate 			return (0);
3990Sstevel@tonic-gate 	} while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0);
4000Sstevel@tonic-gate 	return (dp->d_ino);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate  * Put the directory entries in the directory file
4050Sstevel@tonic-gate  */
4060Sstevel@tonic-gate static void
putdir(char * buf,size_t size)407*1053Smaheshvs putdir(char *buf, size_t size)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	struct direct cvtbuf;
4100Sstevel@tonic-gate 	struct odirect *odp;
4110Sstevel@tonic-gate 	struct odirect *eodp;
4120Sstevel@tonic-gate 	struct direct *dp;
4130Sstevel@tonic-gate 	size_t loc, i;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (cvtflag) {
4160Sstevel@tonic-gate 		/*LINTED [buf is char[] in getfile, size % fs_fsize == 0]*/
4170Sstevel@tonic-gate 		eodp = (struct odirect *)&buf[size];
4180Sstevel@tonic-gate 		/*LINTED [buf is char[] in getfile]*/
4190Sstevel@tonic-gate 		for (odp = (struct odirect *)buf; odp < eodp; odp++)
4200Sstevel@tonic-gate 			if (odp->d_ino != 0) {
4210Sstevel@tonic-gate 				dcvt(odp, &cvtbuf);
4220Sstevel@tonic-gate 				putent(&cvtbuf);
4230Sstevel@tonic-gate 			}
4240Sstevel@tonic-gate 	} else {
4250Sstevel@tonic-gate 		loc = 0;
4260Sstevel@tonic-gate 		while (loc < size) {
4270Sstevel@tonic-gate 			/*LINTED [buf is char[] in getfile, loc % 4 == 0]*/
4280Sstevel@tonic-gate 			dp = (struct direct *)(buf + loc);
4290Sstevel@tonic-gate 			normdirect(byteorder, dp);
4300Sstevel@tonic-gate 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
4310Sstevel@tonic-gate 			if (dp->d_reclen == 0 || (long)dp->d_reclen > i) {
4320Sstevel@tonic-gate 				loc += i;
4330Sstevel@tonic-gate 				continue;
4340Sstevel@tonic-gate 			}
4350Sstevel@tonic-gate 			loc += dp->d_reclen;
4360Sstevel@tonic-gate 			if (dp->d_ino != 0) {
4370Sstevel@tonic-gate 				putent(dp);
4380Sstevel@tonic-gate 			}
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * These variables are "local" to the following two functions.
4450Sstevel@tonic-gate  */
4460Sstevel@tonic-gate static char dirbuf[DIRBLKSIZ];
4470Sstevel@tonic-gate static int32_t dirloc = 0;
4480Sstevel@tonic-gate static int32_t prev = 0;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate  * add a new directory entry to a file.
4520Sstevel@tonic-gate  */
4530Sstevel@tonic-gate static void
putent(struct direct * dp)454*1053Smaheshvs putent(struct direct *dp)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	/* LINTED DIRSIZ will always fit in a ushort_t */
4570Sstevel@tonic-gate 	dp->d_reclen = (ushort_t)DIRSIZ(dp);
4580Sstevel@tonic-gate 	/* LINTED sign extension ok in assert */
4590Sstevel@tonic-gate 	assert(DIRSIZ(dp) == (ulong_t)dp->d_reclen);
4600Sstevel@tonic-gate 	if (dirloc + (long)dp->d_reclen > DIRBLKSIZ) {
4610Sstevel@tonic-gate 		/*LINTED [prev += dp->d_reclen, prev % 4 == 0]*/
4620Sstevel@tonic-gate 		((struct direct *)(dirbuf + prev))->d_reclen =
4630Sstevel@tonic-gate 		    DIRBLKSIZ - prev;
4640Sstevel@tonic-gate 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
4650Sstevel@tonic-gate 		if (ferror(df))
4660Sstevel@tonic-gate 			panic("%s: %s\n", dirfile, strerror(errno));
4670Sstevel@tonic-gate 		dirloc = 0;
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 	bcopy((char *)dp, dirbuf + dirloc, (size_t)dp->d_reclen);
4700Sstevel@tonic-gate 	prev = dirloc;
4710Sstevel@tonic-gate 	dirloc += dp->d_reclen;
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * flush out a directory that is finished.
4760Sstevel@tonic-gate  */
4770Sstevel@tonic-gate static void
4780Sstevel@tonic-gate #ifdef __STDC__
flushent(void)4790Sstevel@tonic-gate flushent(void)
4800Sstevel@tonic-gate #else
4810Sstevel@tonic-gate flushent()
4820Sstevel@tonic-gate #endif
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/* LINTED prev += dp->d_reclen, prev % 4 == 0 */
4860Sstevel@tonic-gate 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
4870Sstevel@tonic-gate 	(void) fwrite(dirbuf, (size_t)dirloc, 1, df);
4880Sstevel@tonic-gate 	if (ferror(df))
4890Sstevel@tonic-gate 		panic("%s: %s\n", dirfile, strerror(errno));
4900Sstevel@tonic-gate 	g_seekpt = ftello64(df);
4910Sstevel@tonic-gate 	dirloc = 0;
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate static void
dcvt(struct odirect * odp,struct direct * ndp)495*1053Smaheshvs dcvt(struct odirect *odp, struct direct *ndp)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	(void) bzero((char *)ndp, sizeof (*ndp));
4990Sstevel@tonic-gate 	ndp->d_ino =  odp->d_ino;
5000Sstevel@tonic-gate 	/* Note that odp->d_name may not be null-terminated */
5010Sstevel@tonic-gate 	/* LINTED assertion always true */
5020Sstevel@tonic-gate 	assert(sizeof (ndp->d_name) > sizeof (odp->d_name));
5030Sstevel@tonic-gate 	(void) strncpy(ndp->d_name, odp->d_name, sizeof (odp->d_name));
5040Sstevel@tonic-gate 	ndp->d_name[sizeof (odp->d_name)] = '\0';
5050Sstevel@tonic-gate 	/* LINTED: strlen will fit into d_namlen */
5060Sstevel@tonic-gate 	ndp->d_namlen = strlen(ndp->d_name);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/* LINTED sign extension ok in assert */
5090Sstevel@tonic-gate 	assert(DIRSIZ(ndp) == (ulong_t)ndp->d_reclen);
5100Sstevel@tonic-gate 	/* LINTED DIRSIZ always fits in ushort_t */
5110Sstevel@tonic-gate 	ndp->d_reclen = (ushort_t)DIRSIZ(ndp);
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate /*
5150Sstevel@tonic-gate  * Initialize the directory file
5160Sstevel@tonic-gate  */
5170Sstevel@tonic-gate static RST_DIR *
rst_initdirfile(char * name)518*1053Smaheshvs rst_initdirfile(char *name)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate 	RST_DIR *dp;
5210Sstevel@tonic-gate 	int fd;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if ((fd = open(name, O_RDONLY | O_LARGEFILE)) == -1)
5240Sstevel@tonic-gate 		return ((RST_DIR *)0);
5250Sstevel@tonic-gate 	if ((dp = (RST_DIR *)malloc(sizeof (*dp))) == NULL) {
5260Sstevel@tonic-gate 		(void) close(fd);
5270Sstevel@tonic-gate 		return ((RST_DIR *)0);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 	dp->dd_fd = fd;
5300Sstevel@tonic-gate 	dp->dd_loc = 0;
5310Sstevel@tonic-gate 	dp->dd_refcnt = 1;
5320Sstevel@tonic-gate 	return (dp);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate /*
5360Sstevel@tonic-gate  * Simulate the opening of a directory
5370Sstevel@tonic-gate  */
5380Sstevel@tonic-gate RST_DIR *
rst_opendir(char * name)539*1053Smaheshvs rst_opendir(char *name)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate 	struct inotab *itp;
5420Sstevel@tonic-gate 	ino_t ino;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	if ((ino = dirlookup(name)) > 0 &&
5450Sstevel@tonic-gate 	    (itp = inotablookup(ino)) != NULL) {
5460Sstevel@tonic-gate 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
5470Sstevel@tonic-gate 		dirp->dd_refcnt++;
5480Sstevel@tonic-gate 		return (dirp);
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 	return ((RST_DIR *)0);
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*
5540Sstevel@tonic-gate  * Releases the hidden state created by rst_opendir().
5550Sstevel@tonic-gate  * Specifically, the dirp it provided to the caller is malloc'd.
5560Sstevel@tonic-gate  */
5570Sstevel@tonic-gate void
rst_closedir(RST_DIR * cdirp)558*1053Smaheshvs rst_closedir(RST_DIR *cdirp)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate 	if ((cdirp != NULL) && (--(cdirp->dd_refcnt) < 1))
5610Sstevel@tonic-gate 		free(cdirp);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * return a pointer into a directory
5660Sstevel@tonic-gate  */
5670Sstevel@tonic-gate static offset_t
rst_telldir(RST_DIR * tdirp)568*1053Smaheshvs rst_telldir(RST_DIR *tdirp)
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate 	offset_t pos = llseek(tdirp->dd_fd, (offset_t)0, SEEK_CUR);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (pos == (offset_t)-1) {
5730Sstevel@tonic-gate 		perror("Could not determine position in directory file");
5740Sstevel@tonic-gate 		done(1);
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	return ((pos - tdirp->dd_size) + tdirp->dd_loc);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * Seek to an entry in a directory.
5820Sstevel@tonic-gate  * Only values returned by ``rst_telldir'' should be passed to rst_seekdir.
5830Sstevel@tonic-gate  * This routine handles many directories in a single file.
5840Sstevel@tonic-gate  * It takes the base of the directory in the file, plus
5850Sstevel@tonic-gate  * the desired seek offset into it.
5860Sstevel@tonic-gate  */
5870Sstevel@tonic-gate static void
rst_seekdir(RST_DIR * sdirp,offset_t loc,offset_t base)588*1053Smaheshvs rst_seekdir(RST_DIR *sdirp, offset_t loc, offset_t base)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	if (loc == rst_telldir(sdirp))
5920Sstevel@tonic-gate 		return;
5930Sstevel@tonic-gate 	loc -= base;
5940Sstevel@tonic-gate 	if (loc < 0)
5950Sstevel@tonic-gate 		(void) fprintf(stderr,
5960Sstevel@tonic-gate 			gettext("bad seek pointer to rst_seekdir %d\n"), loc);
5970Sstevel@tonic-gate 	(void) llseek(sdirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
5980Sstevel@tonic-gate 	sdirp->dd_loc = loc & (DIRBLKSIZ - 1);
5990Sstevel@tonic-gate 	if (sdirp->dd_loc != 0)
6000Sstevel@tonic-gate 		sdirp->dd_size = read(sdirp->dd_fd, sdirp->dd_buf, DIRBLKSIZ);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate  * get next entry in a directory.
6050Sstevel@tonic-gate  */
6060Sstevel@tonic-gate struct direct *
rst_readdir(RST_DIR * rdirp)607*1053Smaheshvs rst_readdir(RST_DIR *rdirp)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate 	struct direct *dp;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	for (;;) {
6120Sstevel@tonic-gate 		if (rdirp->dd_loc == 0) {
6130Sstevel@tonic-gate 			rdirp->dd_size = read(rdirp->dd_fd, rdirp->dd_buf,
6140Sstevel@tonic-gate 			    DIRBLKSIZ);
6150Sstevel@tonic-gate 			if (rdirp->dd_size <= 0) {
6160Sstevel@tonic-gate 				dprintf(stderr,
6170Sstevel@tonic-gate 					gettext("error reading directory\n"));
6180Sstevel@tonic-gate 				return ((struct direct *)0);
6190Sstevel@tonic-gate 			}
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 		if (rdirp->dd_loc >= rdirp->dd_size) {
6220Sstevel@tonic-gate 			rdirp->dd_loc = 0;
6230Sstevel@tonic-gate 			continue;
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 		/*LINTED [rvalue will be aligned on int boundary]*/
6260Sstevel@tonic-gate 		dp = (struct direct *)(rdirp->dd_buf + rdirp->dd_loc);
6270Sstevel@tonic-gate 		if (dp->d_reclen == 0 ||
6280Sstevel@tonic-gate 		    (long)dp->d_reclen > (DIRBLKSIZ + 1 - rdirp->dd_loc)) {
6290Sstevel@tonic-gate 			dprintf(stderr,
6300Sstevel@tonic-gate 			    gettext("corrupted directory: bad reclen %d\n"),
6310Sstevel@tonic-gate 				dp->d_reclen);
6320Sstevel@tonic-gate 			return ((struct direct *)0);
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 		rdirp->dd_loc += dp->d_reclen;
6350Sstevel@tonic-gate 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
6360Sstevel@tonic-gate 			continue;
6370Sstevel@tonic-gate 		if ((ino_t)(dp->d_ino) >= maxino) {
6380Sstevel@tonic-gate 			dprintf(stderr,
6390Sstevel@tonic-gate 				gettext("corrupted directory: bad inum %lu\n"),
6400Sstevel@tonic-gate 				dp->d_ino);
6410Sstevel@tonic-gate 			continue;
6420Sstevel@tonic-gate 		}
6430Sstevel@tonic-gate 		return (dp);
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate /*
6480Sstevel@tonic-gate  * Set the mode, owner, and times for all new or changed directories
6490Sstevel@tonic-gate  */
6500Sstevel@tonic-gate void
6510Sstevel@tonic-gate #ifdef __STDC__
setdirmodes(void)6520Sstevel@tonic-gate setdirmodes(void)
6530Sstevel@tonic-gate #else
6540Sstevel@tonic-gate setdirmodes()
6550Sstevel@tonic-gate #endif
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	FILE *smf;
6580Sstevel@tonic-gate 	struct entry *ep;
6590Sstevel@tonic-gate 	char *cp, *metadata = NULL;
6600Sstevel@tonic-gate 	size_t metasize = 0;
6610Sstevel@tonic-gate 	int override = -1;
6620Sstevel@tonic-gate 	int saverr;
6630Sstevel@tonic-gate 	static int complained_chown = 0;
6640Sstevel@tonic-gate 	static int complained_chmod = 0;
6650Sstevel@tonic-gate 	int dfd;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	vprintf(stdout, gettext("Set directory mode, owner, and times.\n"));
6680Sstevel@tonic-gate 	/* XXX if modefile[0] == '#', shouldn't we just bail here? */
6690Sstevel@tonic-gate 	/* XXX why isn't it set already? */
6700Sstevel@tonic-gate 	INIT_MODEFILE();
6710Sstevel@tonic-gate 	smf = fopen64(modefile, "r");
6720Sstevel@tonic-gate 	if (smf == NULL) {
6730Sstevel@tonic-gate 		perror("fopen");
6740Sstevel@tonic-gate 		(void) fprintf(stderr,
6750Sstevel@tonic-gate 			gettext("cannot open mode file %s\n"), modefile);
6760Sstevel@tonic-gate 		(void) fprintf(stderr,
6770Sstevel@tonic-gate 			gettext("directory mode, owner, and times not set\n"));
6780Sstevel@tonic-gate 		return;
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 	clearerr(smf);
6810Sstevel@tonic-gate 	for (;;) {
6820Sstevel@tonic-gate 		(void) fread((char *)&node, 1, sizeof (node), smf);
6830Sstevel@tonic-gate 		if (feof(smf))
6840Sstevel@tonic-gate 			break;
6850Sstevel@tonic-gate 		ep = lookupino(node.ino);
6860Sstevel@tonic-gate 		if (command == 'i' || command == 'x') {
6870Sstevel@tonic-gate 			if (ep == NIL) {
6880Sstevel@tonic-gate 				skipmetadata(smf, node.metasize);
6890Sstevel@tonic-gate 				continue;
6900Sstevel@tonic-gate 			}
6910Sstevel@tonic-gate 			if (ep->e_flags & EXISTED) {
6920Sstevel@tonic-gate 				if (override < 0) {
6930Sstevel@tonic-gate 					if (reply(gettext(
6940Sstevel@tonic-gate 				"Directories already exist, set modes anyway"))
6950Sstevel@tonic-gate 					    == FAIL)
6960Sstevel@tonic-gate 						override = 0;
6970Sstevel@tonic-gate 					else
6980Sstevel@tonic-gate 						override = 1;
6990Sstevel@tonic-gate 				}
7000Sstevel@tonic-gate 				if (override == 0) {
7010Sstevel@tonic-gate 					/* LINTED: result fits into short */
7020Sstevel@tonic-gate 					ep->e_flags &= ~NEW;
7030Sstevel@tonic-gate 					skipmetadata(smf, node.metasize);
7040Sstevel@tonic-gate 					continue;
7050Sstevel@tonic-gate 				}
7060Sstevel@tonic-gate 			}
7070Sstevel@tonic-gate 			if (node.ino == ROOTINO &&
7080Sstevel@tonic-gate 			    reply(gettext("set owner/mode for '.'")) == FAIL) {
7090Sstevel@tonic-gate 				skipmetadata(smf, node.metasize);
7100Sstevel@tonic-gate 				continue;
7110Sstevel@tonic-gate 			}
7120Sstevel@tonic-gate 		}
7130Sstevel@tonic-gate 		if (ep == NIL) {
7140Sstevel@tonic-gate 			panic(gettext("cannot find directory inode %d\n"),
7150Sstevel@tonic-gate 				node.ino);
7160Sstevel@tonic-gate 			skipmetadata(smf, node.metasize);
7170Sstevel@tonic-gate 			continue;
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 		cp = myname(ep);
7200Sstevel@tonic-gate 		resolve(myname(ep), &dfd, &cp);
7210Sstevel@tonic-gate 		if (dfd != AT_FDCWD) {
7220Sstevel@tonic-gate 			if (fchdir(dfd) < 0) {
7230Sstevel@tonic-gate 				saverr = errno;
7240Sstevel@tonic-gate 				(void) fprintf(stderr,
7250Sstevel@tonic-gate 			    gettext("Can not set attribute context: %s\n"),
7260Sstevel@tonic-gate 					strerror(saverr));
7270Sstevel@tonic-gate 				(void) close(dfd);
7280Sstevel@tonic-gate 				continue;
7290Sstevel@tonic-gate 			}
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 		if (chmod(cp, node.mode) < 0 && !complained_chmod) {
7320Sstevel@tonic-gate 			saverr = errno;
7330Sstevel@tonic-gate 			(void) fprintf(stderr,
7340Sstevel@tonic-gate 			gettext("Can not set directory permissions: %s\n"),
7350Sstevel@tonic-gate 				strerror(saverr));
7360Sstevel@tonic-gate 			complained_chmod = 1;
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 		if (node.metasize != 0) {
7390Sstevel@tonic-gate 			if (node.metasize > metasize)
7400Sstevel@tonic-gate 				metadata = realloc(metadata,
7410Sstevel@tonic-gate 				    metasize = node.metasize);
7420Sstevel@tonic-gate 			if (metadata == NULL) {
7430Sstevel@tonic-gate 				(void) fprintf(stderr,
7440Sstevel@tonic-gate 					gettext("Cannot malloc metadata\n"));
7450Sstevel@tonic-gate 				done(1);
7460Sstevel@tonic-gate 			}
7470Sstevel@tonic-gate 			(void) fread(metadata, 1, node.metasize, smf);
7480Sstevel@tonic-gate 			metaproc(cp, metadata, node.metasize);
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 		/*
7520Sstevel@tonic-gate 		 * BUG 4302943
7530Sstevel@tonic-gate 		 * Since the ACLs must be set before fixing the ownership,
7540Sstevel@tonic-gate 		 * chown should be called only after metaproc
7550Sstevel@tonic-gate 		 */
7560Sstevel@tonic-gate 		if (chown(cp, node.uid, node.gid) < 0 && !complained_chown) {
7570Sstevel@tonic-gate 			saverr = errno;
7580Sstevel@tonic-gate 			(void) fprintf(stderr,
7590Sstevel@tonic-gate 			    gettext("Can not set directory ownership: %s\n"),
7600Sstevel@tonic-gate 			    strerror(saverr));
7610Sstevel@tonic-gate 			complained_chown = 1;
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 		utime(cp, (struct utimbuf *)node.timep);
7640Sstevel@tonic-gate 		/* LINTED: result fits into short */
7650Sstevel@tonic-gate 		ep->e_flags &= ~NEW;
7660Sstevel@tonic-gate 		if (dfd != AT_FDCWD) {
7670Sstevel@tonic-gate 			fchdir(savepwd);
7680Sstevel@tonic-gate 			(void) close(dfd);
7690Sstevel@tonic-gate 		}
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 	if (ferror(smf))
7720Sstevel@tonic-gate 		panic(gettext("error setting directory modes\n"));
7730Sstevel@tonic-gate 	if (metadata != NULL)
7740Sstevel@tonic-gate 		(void) free(metadata);
7750Sstevel@tonic-gate 	(void) fclose(smf);
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate void
skipmetadata(FILE * f,size_t size)779*1053Smaheshvs skipmetadata(FILE *f, size_t size)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	/* XXX should we bail if this doesn't work? */
7820Sstevel@tonic-gate 	/* LINTED unsigned -> signed conversion ok here */
7830Sstevel@tonic-gate 	(void) fseeko(f, (off_t)size, SEEK_CUR);
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate /*
7870Sstevel@tonic-gate  * Generate a literal copy of a directory.
7880Sstevel@tonic-gate  */
789*1053Smaheshvs int
genliteraldir(char * name,ino_t ino)790*1053Smaheshvs genliteraldir(char *name, ino_t ino)
7910Sstevel@tonic-gate {
7920Sstevel@tonic-gate 	struct inotab *itp;
7930Sstevel@tonic-gate 	int ofile, dp;
7940Sstevel@tonic-gate 	off64_t i;
7950Sstevel@tonic-gate 	size_t size;
7960Sstevel@tonic-gate 	char buf[BUFSIZ];
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	itp = inotablookup(ino);
7990Sstevel@tonic-gate 	if (itp == NULL) {
8000Sstevel@tonic-gate 		(void) fprintf(stderr,
8010Sstevel@tonic-gate 		    gettext("Cannot find directory inode %d named %s\n"),
8020Sstevel@tonic-gate 		    ino, name);
8030Sstevel@tonic-gate 		return (FAIL);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 	if ((ofile = creat(name, 0666)) < 0) {
8060Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", name);
8070Sstevel@tonic-gate 		(void) fflush(stderr);
8080Sstevel@tonic-gate 		perror(gettext("cannot create file"));
8090Sstevel@tonic-gate 		return (FAIL);
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
8120Sstevel@tonic-gate 	dp = dup(dirp->dd_fd);
8130Sstevel@tonic-gate 	if (dp < 0) {
8140Sstevel@tonic-gate 		perror(gettext("dup(2) failed"));
8150Sstevel@tonic-gate 		(void) close(ofile);
8160Sstevel@tonic-gate 		(void) unlink(name);
8170Sstevel@tonic-gate 		return (FAIL);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 	for (i = itp->t_size; i != 0; i -= size) {
8200Sstevel@tonic-gate 		/* LINTED cast is safe due to comparison */
8210Sstevel@tonic-gate 		size = i < BUFSIZ ? (size_t)i : BUFSIZ;
8220Sstevel@tonic-gate 		/* XXX instead of done(), clean up and return FAIL? */
8230Sstevel@tonic-gate 		if (read(dp, buf, size) == -1) {
8240Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
8250Sstevel@tonic-gate 				"read error extracting inode %d, name %s\n"),
8260Sstevel@tonic-gate 				curfile.ino, curfile.name);
8270Sstevel@tonic-gate 			perror("read");
8280Sstevel@tonic-gate 			done(1);
8290Sstevel@tonic-gate 		}
8300Sstevel@tonic-gate 		if (write(ofile, buf, size) == -1) {
8310Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
8320Sstevel@tonic-gate 				"write error extracting inode %d, name %s\n"),
8330Sstevel@tonic-gate 				curfile.ino, curfile.name);
8340Sstevel@tonic-gate 			perror("write");
8350Sstevel@tonic-gate 			done(1);
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 	(void) close(dp);
8390Sstevel@tonic-gate 	(void) close(ofile);
8400Sstevel@tonic-gate 	return (GOOD);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate /*
8440Sstevel@tonic-gate  * Determine the type of an inode
8450Sstevel@tonic-gate  */
846*1053Smaheshvs int
inodetype(ino_t ino)847*1053Smaheshvs inodetype(ino_t ino)
8480Sstevel@tonic-gate {
8490Sstevel@tonic-gate 	struct inotab *itp;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	itp = inotablookup(ino);
8520Sstevel@tonic-gate 	if (itp == NULL)
8530Sstevel@tonic-gate 		return (LEAF);
8540Sstevel@tonic-gate 	return (NODE);
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate /*
8580Sstevel@tonic-gate  * Allocate and initialize a directory inode entry.
8590Sstevel@tonic-gate  * If requested, save its pertinent mode, owner, and time info.
8600Sstevel@tonic-gate  */
8610Sstevel@tonic-gate static struct inotab *
allocinotab(ino_t ino,struct dinode * dip,off64_t seekpt)862*1053Smaheshvs allocinotab(ino_t ino, struct dinode *dip, off64_t seekpt)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate 	struct inotab	*itp;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	itp = (struct inotab *)calloc(1, sizeof (*itp));
8670Sstevel@tonic-gate 	if (itp == 0) {
8680Sstevel@tonic-gate 		(void) fprintf(stderr,
8690Sstevel@tonic-gate 		    gettext("no memory for directory table\n"));
8700Sstevel@tonic-gate 		done(1);
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 	itp->t_next = inotab[INOHASH(ino)];
8730Sstevel@tonic-gate 	inotab[INOHASH(ino)] = itp;
8740Sstevel@tonic-gate 	itp->t_ino = ino;
8750Sstevel@tonic-gate 	itp->t_seekpt = seekpt;
8760Sstevel@tonic-gate 	if ((dip->di_mode & IFMT) == IFATTRDIR) {
8770Sstevel@tonic-gate 		itp->t_xattr = xattrlist;
8780Sstevel@tonic-gate 		xattrlist = itp;
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 	if (mf == NULL)
8810Sstevel@tonic-gate 		return (itp);
8820Sstevel@tonic-gate 	node.ino = ino;
8830Sstevel@tonic-gate 	node.timep[0] = dip->di_atime;
8840Sstevel@tonic-gate 	node.timep[1] = dip->di_mtime;
8850Sstevel@tonic-gate 	node.mode = dip->di_mode;
8860Sstevel@tonic-gate 	node.uid =
8870Sstevel@tonic-gate 		dip->di_suid == UID_LONG ? dip->di_uid : (uid_t)dip->di_suid;
8880Sstevel@tonic-gate 	node.gid =
8890Sstevel@tonic-gate 		dip->di_sgid == GID_LONG ? dip->di_gid : (gid_t)dip->di_sgid;
8900Sstevel@tonic-gate 	return (itp);
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate void
nodeflush()8940Sstevel@tonic-gate nodeflush()
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	char *metadata;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	if (mf == NULL) {
8990Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
9000Sstevel@tonic-gate 		    "Inconsistency detected: modefile pointer is NULL\n"));
9010Sstevel@tonic-gate 		done(1);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 	metaget(&metadata, &(node.metasize));
9040Sstevel@tonic-gate 	(void) fwrite((char *)&node, 1, sizeof (node), mf);
9050Sstevel@tonic-gate 	if (node.metasize != 0)
9060Sstevel@tonic-gate 		(void) fwrite(metadata, 1, node.metasize, mf);
9070Sstevel@tonic-gate 	if (ferror(mf))
9080Sstevel@tonic-gate 		panic("%s: %s\n", modefile, strerror(errno));
9090Sstevel@tonic-gate }
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate /*
9120Sstevel@tonic-gate  * Look up an inode in the table of directories
9130Sstevel@tonic-gate  */
9140Sstevel@tonic-gate static struct inotab *
inotablookup(ino_t ino)915*1053Smaheshvs inotablookup(ino_t ino)
9160Sstevel@tonic-gate {
9170Sstevel@tonic-gate 	struct inotab *itp;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
9200Sstevel@tonic-gate 		if (itp->t_ino == ino)
9210Sstevel@tonic-gate 			return (itp);
9220Sstevel@tonic-gate 	return ((struct inotab *)0);
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate  * Clean up and exit
9270Sstevel@tonic-gate  */
9280Sstevel@tonic-gate void
done(int exitcode)929*1053Smaheshvs done(int exitcode)
9300Sstevel@tonic-gate {
931426Srm88369 	closemt(ALLOW_OFFLINE);		/* don't force offline on exit */
9320Sstevel@tonic-gate 	if (modefile[0] != '#')
9330Sstevel@tonic-gate 		(void) unlink(modefile);
9340Sstevel@tonic-gate 	if (dirfile[0] != '#')
9350Sstevel@tonic-gate 		(void) unlink(dirfile);
9360Sstevel@tonic-gate 	exit(exitcode);
9370Sstevel@tonic-gate }
938