Home | History | Annotate | Line # | Download | only in restore
dirs.c revision 1.7
      1 /*
      2  * Copyright (c) 1983, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  * (c) UNIX System Laboratories, Inc.
      5  * All or some portions of this file are derived from material licensed
      6  * to the University of California by American Telephone and Telegraph
      7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      8  * the permission of UNIX System Laboratories, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 /*static char sccsid[] = "from: @(#)dirs.c	8.2 (Berkeley) 1/21/94";*/
     41 static char *rcsid = "$Id: dirs.c,v 1.7 1994/06/08 19:33:33 mycroft Exp $";
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/file.h>
     46 #include <sys/stat.h>
     47 #include <sys/time.h>
     48 
     49 #include <ufs/ffs/fs.h>
     50 #include <ufs/ufs/dinode.h>
     51 #include <ufs/ufs/dir.h>
     52 #include <protocols/dumprestore.h>
     53 
     54 #include <errno.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 
     60 #include "pathnames.h"
     61 #include "restore.h"
     62 #include "extern.h"
     63 
     64 /*
     65  * Symbol table of directories read from tape.
     66  */
     67 #define HASHSIZE	1000
     68 #define INOHASH(val) (val % HASHSIZE)
     69 struct inotab {
     70 	struct	inotab *t_next;
     71 	ino_t	t_ino;
     72 	long	t_seekpt;
     73 	long	t_size;
     74 };
     75 static struct inotab *inotab[HASHSIZE];
     76 
     77 /*
     78  * Information retained about directories.
     79  */
     80 struct modeinfo {
     81 	ino_t ino;
     82 	struct timeval timep[2];
     83 	short mode;
     84 	short uid;
     85 	short gid;
     86 };
     87 
     88 /*
     89  * Definitions for library routines operating on directories.
     90  */
     91 #undef DIRBLKSIZ
     92 #define DIRBLKSIZ 1024
     93 struct rstdirdesc {
     94 	int	dd_fd;
     95 	long	dd_loc;
     96 	long	dd_size;
     97 	char	dd_buf[DIRBLKSIZ];
     98 };
     99 
    100 /*
    101  * Global variables for this file.
    102  */
    103 static long	seekpt;
    104 static FILE	*df, *mf;
    105 static RST_DIR	*dirp;
    106 static char	dirfile[32] = "#";	/* No file */
    107 static char	modefile[32] = "#";	/* No file */
    108 static char	dot[2] = ".";		/* So it can be modified */
    109 
    110 /*
    111  * Format of old style directories.
    112  */
    113 #define ODIRSIZ 14
    114 struct odirect {
    115 	u_short	d_ino;
    116 	char	d_name[ODIRSIZ];
    117 };
    118 
    119 static struct inotab	*allocinotab __P((ino_t, struct dinode *, long));
    120 static void		 dcvt __P((struct odirect *, struct direct *));
    121 static void		 flushent __P((void));
    122 static struct inotab	*inotablookup __P((ino_t));
    123 static RST_DIR		*opendirfile __P((const char *));
    124 static void		 putdir __P((char *, long));
    125 static void		 putent __P((struct direct *));
    126 static void		 rst_seekdir __P((RST_DIR *, long, long));
    127 static long		 rst_telldir __P((RST_DIR *));
    128 static struct direct	*searchdir __P((ino_t, char *));
    129 
    130 /*
    131  *	Extract directory contents, building up a directory structure
    132  *	on disk for extraction by name.
    133  *	If genmode is requested, save mode, owner, and times for all
    134  *	directories on the tape.
    135  */
    136 void
    137 extractdirs(genmode)
    138 	int genmode;
    139 {
    140 	register int i;
    141 	register struct dinode *ip;
    142 	struct inotab *itp;
    143 	struct direct nulldir;
    144 
    145 	vprintf(stdout, "Extract directories from tape\n");
    146 	(void) sprintf(dirfile, "%s/rstdir%d", _PATH_TMP, dumpdate);
    147 	df = fopen(dirfile, "w");
    148 	if (df == NULL) {
    149 		fprintf(stderr,
    150 		    "restore: %s - cannot create directory temporary\n",
    151 		    dirfile);
    152 		fprintf(stderr, "fopen: %s\n", strerror(errno));
    153 		done(1);
    154 	}
    155 	if (genmode != 0) {
    156 		(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
    157 		mf = fopen(modefile, "w");
    158 		if (mf == NULL) {
    159 			fprintf(stderr,
    160 			    "restore: %s - cannot create modefile \n",
    161 			    modefile);
    162 			fprintf(stderr, "fopen: %s\n", strerror(errno));
    163 			done(1);
    164 		}
    165 	}
    166 	nulldir.d_ino = 0;
    167 	nulldir.d_type = DT_DIR;
    168 	nulldir.d_namlen = 1;
    169 	(void) strcpy(nulldir.d_name, "/");
    170 	nulldir.d_reclen = DIRSIZ(0, &nulldir);
    171 	for (;;) {
    172 		curfile.name = "<directory file - name unknown>";
    173 		curfile.action = USING;
    174 		ip = curfile.dip;
    175 		if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
    176 			(void) fclose(df);
    177 			dirp = opendirfile(dirfile);
    178 			if (dirp == NULL)
    179 				fprintf(stderr, "opendirfile: %s\n",
    180 				    strerror(errno));
    181 			if (mf != NULL)
    182 				(void) fclose(mf);
    183 			i = dirlookup(dot);
    184 			if (i == 0)
    185 				panic("Root directory is not on tape\n");
    186 			return;
    187 		}
    188 		itp = allocinotab(curfile.ino, ip, seekpt);
    189 		getfile(putdir, xtrnull);
    190 		putent(&nulldir);
    191 		flushent();
    192 		itp->t_size = seekpt - itp->t_seekpt;
    193 	}
    194 }
    195 
    196 /*
    197  * skip over all the directories on the tape
    198  */
    199 void
    200 skipdirs()
    201 {
    202 
    203 	while ((curfile.dip->di_mode & IFMT) == IFDIR) {
    204 		skipfile();
    205 	}
    206 }
    207 
    208 /*
    209  *	Recursively find names and inumbers of all files in subtree
    210  *	pname and pass them off to be processed.
    211  */
    212 void
    213 treescan(pname, ino, todo)
    214 	char *pname;
    215 	ino_t ino;
    216 	long (*todo) __P((char *, ino_t, int));
    217 {
    218 	register struct inotab *itp;
    219 	register struct direct *dp;
    220 	int namelen;
    221 	long bpt;
    222 	char locname[MAXPATHLEN + 1];
    223 
    224 	itp = inotablookup(ino);
    225 	if (itp == NULL) {
    226 		/*
    227 		 * Pname is name of a simple file or an unchanged directory.
    228 		 */
    229 		(void) (*todo)(pname, ino, LEAF);
    230 		return;
    231 	}
    232 	/*
    233 	 * Pname is a dumped directory name.
    234 	 */
    235 	if ((*todo)(pname, ino, NODE) == FAIL)
    236 		return;
    237 	/*
    238 	 * begin search through the directory
    239 	 * skipping over "." and ".."
    240 	 */
    241 	(void) strncpy(locname, pname, MAXPATHLEN);
    242 	(void) strncat(locname, "/", MAXPATHLEN);
    243 	namelen = strlen(locname);
    244 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
    245 	dp = rst_readdir(dirp); /* "." */
    246 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
    247 		dp = rst_readdir(dirp); /* ".." */
    248 	else
    249 		fprintf(stderr, "Warning: `.' missing from directory %s\n",
    250 			pname);
    251 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
    252 		dp = rst_readdir(dirp); /* first real entry */
    253 	else
    254 		fprintf(stderr, "Warning: `..' missing from directory %s\n",
    255 			pname);
    256 	bpt = rst_telldir(dirp);
    257 	/*
    258 	 * a zero inode signals end of directory
    259 	 */
    260 	while (dp != NULL && dp->d_ino != 0) {
    261 		locname[namelen] = '\0';
    262 		if (namelen + dp->d_namlen >= MAXPATHLEN) {
    263 			fprintf(stderr, "%s%s: name exceeds %d char\n",
    264 				locname, dp->d_name, MAXPATHLEN);
    265 		} else {
    266 			(void) strncat(locname, dp->d_name, (int)dp->d_namlen);
    267 			treescan(locname, dp->d_ino, todo);
    268 			rst_seekdir(dirp, bpt, itp->t_seekpt);
    269 		}
    270 		dp = rst_readdir(dirp);
    271 		bpt = rst_telldir(dirp);
    272 	}
    273 	if (dp == NULL)
    274 		fprintf(stderr, "corrupted directory: %s.\n", locname);
    275 }
    276 
    277 /*
    278  * Lookup a pathname which is always assumed to start from the ROOTINO.
    279  */
    280 struct direct *
    281 pathsearch(pathname)
    282 	const char *pathname;
    283 {
    284 	ino_t ino;
    285 	struct direct *dp;
    286 	char *path, *name, buffer[MAXPATHLEN];
    287 
    288 	strcpy(buffer, pathname);
    289 	path = buffer;
    290 	ino = ROOTINO;
    291 	while (*path == '/')
    292 		path++;
    293 	dp = NULL;
    294 	while ((name = strsep(&path, "/")) != NULL && *name != NULL) {
    295 		if ((dp = searchdir(ino, name)) == NULL)
    296 			return (NULL);
    297 		ino = dp->d_ino;
    298 	}
    299 	return (dp);
    300 }
    301 
    302 /*
    303  * Lookup the requested name in directory inum.
    304  * Return its inode number if found, zero if it does not exist.
    305  */
    306 static struct direct *
    307 searchdir(inum, name)
    308 	ino_t	inum;
    309 	char	*name;
    310 {
    311 	register struct direct *dp;
    312 	register struct inotab *itp;
    313 	int len;
    314 
    315 	itp = inotablookup(inum);
    316 	if (itp == NULL)
    317 		return (NULL);
    318 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
    319 	len = strlen(name);
    320 	do {
    321 		dp = rst_readdir(dirp);
    322 		if (dp == NULL || dp->d_ino == 0)
    323 			return (NULL);
    324 	} while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
    325 	return (dp);
    326 }
    327 
    328 /*
    329  * Put the directory entries in the directory file
    330  */
    331 static void
    332 putdir(buf, size)
    333 	char *buf;
    334 	long size;
    335 {
    336 	struct direct cvtbuf;
    337 	register struct odirect *odp;
    338 	struct odirect *eodp;
    339 	register struct direct *dp;
    340 	long loc, i;
    341 
    342 	if (cvtflag) {
    343 		eodp = (struct odirect *)&buf[size];
    344 		for (odp = (struct odirect *)buf; odp < eodp; odp++)
    345 			if (odp->d_ino != 0) {
    346 				dcvt(odp, &cvtbuf);
    347 				putent(&cvtbuf);
    348 			}
    349 	} else {
    350 		for (loc = 0; loc < size; ) {
    351 			dp = (struct direct *)(buf + loc);
    352 			if (oldinofmt) {
    353 				if (Bcvt) {
    354 					swabst((u_char *)"l2s", (u_char *) dp);
    355 				}
    356 			} else {
    357 				if (Bcvt) {
    358 					swabst((u_char *)"ls", (u_char *) dp);
    359 				}
    360 			}
    361 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
    362 			if ((dp->d_reclen & 0x3) != 0 ||
    363 			    dp->d_reclen > i ||
    364 			    dp->d_reclen < DIRSIZ(0, dp) ||
    365 			    dp->d_namlen > NAME_MAX) {
    366 				vprintf(stdout, "Mangled directory: ");
    367 				if ((dp->d_reclen & 0x3) != 0)
    368 					vprintf(stdout,
    369 					   "reclen not multiple of 4 ");
    370 				if (dp->d_reclen < DIRSIZ(0, dp))
    371 					vprintf(stdout,
    372 					   "reclen less than DIRSIZ (%d < %d) ",
    373 					   dp->d_reclen, DIRSIZ(0, dp));
    374 				if (dp->d_namlen > NAME_MAX)
    375 					vprintf(stdout,
    376 					   "reclen name too big (%d > %d) ",
    377 					   dp->d_namlen, NAME_MAX);
    378 				vprintf(stdout, "\n");
    379 				loc += i;
    380 				continue;
    381 			}
    382 			loc += dp->d_reclen;
    383 			if (dp->d_ino != 0) {
    384 				putent(dp);
    385 			}
    386 		}
    387 	}
    388 }
    389 
    390 /*
    391  * These variables are "local" to the following two functions.
    392  */
    393 char dirbuf[DIRBLKSIZ];
    394 long dirloc = 0;
    395 long prev = 0;
    396 
    397 /*
    398  * add a new directory entry to a file.
    399  */
    400 static void
    401 putent(dp)
    402 	struct direct *dp;
    403 {
    404 	dp->d_reclen = DIRSIZ(0, dp);
    405 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
    406 		((struct direct *)(dirbuf + prev))->d_reclen =
    407 		    DIRBLKSIZ - prev;
    408 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
    409 		dirloc = 0;
    410 	}
    411 	bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen);
    412 	prev = dirloc;
    413 	dirloc += dp->d_reclen;
    414 }
    415 
    416 /*
    417  * flush out a directory that is finished.
    418  */
    419 static void
    420 flushent()
    421 {
    422 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
    423 	(void) fwrite(dirbuf, (int)dirloc, 1, df);
    424 	seekpt = ftell(df);
    425 	dirloc = 0;
    426 }
    427 
    428 static void
    429 dcvt(odp, ndp)
    430 	register struct odirect *odp;
    431 	register struct direct *ndp;
    432 {
    433 
    434 	bzero((char *)ndp, (long)(sizeof *ndp));
    435 	ndp->d_ino =  odp->d_ino;
    436 	ndp->d_type = DT_UNKNOWN;
    437 	(void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
    438 	ndp->d_namlen = strlen(ndp->d_name);
    439 	ndp->d_reclen = DIRSIZ(0, ndp);
    440 }
    441 
    442 /*
    443  * Seek to an entry in a directory.
    444  * Only values returned by rst_telldir should be passed to rst_seekdir.
    445  * This routine handles many directories in a single file.
    446  * It takes the base of the directory in the file, plus
    447  * the desired seek offset into it.
    448  */
    449 static void
    450 rst_seekdir(dirp, loc, base)
    451 	register RST_DIR *dirp;
    452 	long loc, base;
    453 {
    454 
    455 	if (loc == rst_telldir(dirp))
    456 		return;
    457 	loc -= base;
    458 	if (loc < 0)
    459 		fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
    460 	(void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
    461 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
    462 	if (dirp->dd_loc != 0)
    463 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
    464 }
    465 
    466 /*
    467  * get next entry in a directory.
    468  */
    469 struct direct *
    470 rst_readdir(dirp)
    471 	register RST_DIR *dirp;
    472 {
    473 	register struct direct *dp;
    474 
    475 	for (;;) {
    476 		if (dirp->dd_loc == 0) {
    477 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
    478 			    DIRBLKSIZ);
    479 			if (dirp->dd_size <= 0) {
    480 				dprintf(stderr, "error reading directory\n");
    481 				return (NULL);
    482 			}
    483 		}
    484 		if (dirp->dd_loc >= dirp->dd_size) {
    485 			dirp->dd_loc = 0;
    486 			continue;
    487 		}
    488 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
    489 		if (dp->d_reclen == 0 ||
    490 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
    491 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
    492 				dp->d_reclen);
    493 			return (NULL);
    494 		}
    495 		dirp->dd_loc += dp->d_reclen;
    496 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
    497 			continue;
    498 		if (dp->d_ino >= maxino) {
    499 			dprintf(stderr, "corrupted directory: bad inum %d\n",
    500 				dp->d_ino);
    501 			continue;
    502 		}
    503 		return (dp);
    504 	}
    505 }
    506 
    507 /*
    508  * Simulate the opening of a directory
    509  */
    510 RST_DIR *
    511 rst_opendir(name)
    512 	const char *name;
    513 {
    514 	struct inotab *itp;
    515 	RST_DIR *dirp;
    516 	ino_t ino;
    517 
    518 	if ((ino = dirlookup(name)) > 0 &&
    519 	    (itp = inotablookup(ino)) != NULL) {
    520 		dirp = opendirfile(dirfile);
    521 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
    522 		return (dirp);
    523 	}
    524 	return (NULL);
    525 }
    526 
    527 /*
    528  * In our case, there is nothing to do when closing a directory.
    529  */
    530 void
    531 rst_closedir(dirp)
    532 	RST_DIR *dirp;
    533 {
    534 
    535 	(void)close(dirp->dd_fd);
    536 	free(dirp);
    537 	return;
    538 }
    539 
    540 /*
    541  * Simulate finding the current offset in the directory.
    542  */
    543 static long
    544 rst_telldir(dirp)
    545 	RST_DIR *dirp;
    546 {
    547 	return ((long)lseek(dirp->dd_fd,
    548 	    (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
    549 }
    550 
    551 /*
    552  * Open a directory file.
    553  */
    554 static RST_DIR *
    555 opendirfile(name)
    556 	const char *name;
    557 {
    558 	register RST_DIR *dirp;
    559 	register int fd;
    560 
    561 	if ((fd = open(name, O_RDONLY)) == -1)
    562 		return (NULL);
    563 	if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
    564 		(void)close(fd);
    565 		return (NULL);
    566 	}
    567 	dirp->dd_fd = fd;
    568 	dirp->dd_loc = 0;
    569 	return (dirp);
    570 }
    571 
    572 /*
    573  * Set the mode, owner, and times for all new or changed directories
    574  */
    575 void
    576 setdirmodes(flags)
    577 	int flags;
    578 {
    579 	FILE *mf;
    580 	struct modeinfo node;
    581 	struct entry *ep;
    582 	char *cp;
    583 
    584 	vprintf(stdout, "Set directory mode, owner, and times.\n");
    585 	(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
    586 	mf = fopen(modefile, "r");
    587 	if (mf == NULL) {
    588 		fprintf(stderr, "fopen: %s\n", strerror(errno));
    589 		fprintf(stderr, "cannot open mode file %s\n", modefile);
    590 		fprintf(stderr, "directory mode, owner, and times not set\n");
    591 		return;
    592 	}
    593 	clearerr(mf);
    594 	for (;;) {
    595 		(void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
    596 		if (feof(mf))
    597 			break;
    598 		ep = lookupino(node.ino);
    599 		if (command == 'i' || command == 'x') {
    600 			if (ep == NULL)
    601 				continue;
    602 			if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
    603 				ep->e_flags &= ~NEW;
    604 				continue;
    605 			}
    606 			if (node.ino == ROOTINO &&
    607 		   	    reply("set owner/mode for '.'") == FAIL)
    608 				continue;
    609 		}
    610 		if (ep == NULL) {
    611 			panic("cannot find directory inode %d\n", node.ino);
    612 		} else {
    613 			cp = myname(ep);
    614 			(void) chown(cp, node.uid, node.gid);
    615 			(void) chmod(cp, node.mode);
    616 			utimes(cp, node.timep);
    617 			ep->e_flags &= ~NEW;
    618 		}
    619 	}
    620 	if (ferror(mf))
    621 		panic("error setting directory modes\n");
    622 	(void) fclose(mf);
    623 }
    624 
    625 /*
    626  * Generate a literal copy of a directory.
    627  */
    628 int
    629 genliteraldir(name, ino)
    630 	char *name;
    631 	ino_t ino;
    632 {
    633 	register struct inotab *itp;
    634 	int ofile, dp, i, size;
    635 	char buf[BUFSIZ];
    636 
    637 	itp = inotablookup(ino);
    638 	if (itp == NULL)
    639 		panic("Cannot find directory inode %d named %s\n", ino, name);
    640 	if ((ofile = creat(name, 0666)) < 0) {
    641 		fprintf(stderr, "%s: ", name);
    642 		(void) fflush(stderr);
    643 		fprintf(stderr, "cannot create file: %s\n", strerror(errno));
    644 		return (FAIL);
    645 	}
    646 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
    647 	dp = dup(dirp->dd_fd);
    648 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
    649 		size = i < BUFSIZ ? i : BUFSIZ;
    650 		if (read(dp, buf, (int) size) == -1) {
    651 			fprintf(stderr,
    652 				"write error extracting inode %d, name %s\n",
    653 				curfile.ino, curfile.name);
    654 			fprintf(stderr, "read: %s\n", strerror(errno));
    655 			done(1);
    656 		}
    657 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
    658 			fprintf(stderr,
    659 				"write error extracting inode %d, name %s\n",
    660 				curfile.ino, curfile.name);
    661 			fprintf(stderr, "write: %s\n", strerror(errno));
    662 			done(1);
    663 		}
    664 	}
    665 	(void) close(dp);
    666 	(void) close(ofile);
    667 	return (GOOD);
    668 }
    669 
    670 /*
    671  * Determine the type of an inode
    672  */
    673 int
    674 inodetype(ino)
    675 	ino_t ino;
    676 {
    677 	struct inotab *itp;
    678 
    679 	itp = inotablookup(ino);
    680 	if (itp == NULL)
    681 		return (LEAF);
    682 	return (NODE);
    683 }
    684 
    685 /*
    686  * Allocate and initialize a directory inode entry.
    687  * If requested, save its pertinent mode, owner, and time info.
    688  */
    689 static struct inotab *
    690 allocinotab(ino, dip, seekpt)
    691 	ino_t ino;
    692 	struct dinode *dip;
    693 	long seekpt;
    694 {
    695 	register struct inotab	*itp;
    696 	struct modeinfo node;
    697 
    698 	itp = calloc(1, sizeof(struct inotab));
    699 	if (itp == NULL)
    700 		panic("no memory directory table\n");
    701 	itp->t_next = inotab[INOHASH(ino)];
    702 	inotab[INOHASH(ino)] = itp;
    703 	itp->t_ino = ino;
    704 	itp->t_seekpt = seekpt;
    705 	if (mf == NULL)
    706 		return (itp);
    707 	node.ino = ino;
    708 	node.timep[0].tv_sec = dip->di_atime.ts_sec;
    709 	node.timep[0].tv_usec = dip->di_atime.ts_nsec / 1000;
    710 	node.timep[1].tv_sec = dip->di_mtime.ts_sec;
    711 	node.timep[1].tv_usec = dip->di_mtime.ts_nsec / 1000;
    712 	node.mode = dip->di_mode;
    713 	node.uid = dip->di_uid;
    714 	node.gid = dip->di_gid;
    715 	(void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
    716 	return (itp);
    717 }
    718 
    719 /*
    720  * Look up an inode in the table of directories
    721  */
    722 static struct inotab *
    723 inotablookup(ino)
    724 	ino_t	ino;
    725 {
    726 	register struct inotab *itp;
    727 
    728 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
    729 		if (itp->t_ino == ino)
    730 			return (itp);
    731 	return (NULL);
    732 }
    733 
    734 /*
    735  * Clean up and exit
    736  */
    737 __dead void
    738 done(exitcode)
    739 	int exitcode;
    740 {
    741 
    742 	closemt();
    743 	if (modefile[0] != '#')
    744 		(void) unlink(modefile);
    745 	if (dirfile[0] != '#')
    746 		(void) unlink(dirfile);
    747 	exit(exitcode);
    748 }
    749