xref: /dragonfly/sbin/restore/dirs.c (revision f746689a)
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  * @(#)dirs.c	8.7 (Berkeley) 5/1/95
39  * $FreeBSD: src/sbin/restore/dirs.c,v 1.14.2.5 2001/10/15 13:44:45 dd Exp $
40  * $DragonFly: src/sbin/restore/dirs.c,v 1.10 2006/04/03 01:58:49 dillon Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/file.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/dir.h>
50 #include <protocols/dumprestore.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <paths.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include "restore.h"
61 #include "extern.h"
62 
63 /*
64  * Symbol table of directories read from tape.
65  */
66 #define HASHSIZE	1000
67 #define INOHASH(val) (val % HASHSIZE)
68 struct inotab {
69 	struct	inotab *t_next;
70 	ufs1_ino_t	t_ino;
71 	int32_t	t_seekpt;
72 	int32_t	t_size;
73 };
74 static struct inotab *inotab[HASHSIZE];
75 
76 /*
77  * Information retained about directories.
78  */
79 struct modeinfo {
80 	ufs1_ino_t ino;
81 	struct timeval timep[2];
82 	mode_t mode;
83 	uid_t uid;
84 	gid_t gid;
85 	int flags;
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 	int32_t	dd_loc;
96 	int32_t	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[MAXPATHLEN] = "#";	/* No file */
107 static char	modefile[MAXPATHLEN] = "#";	/* 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(ufs1_ino_t, struct ufs1_dinode *, long);
120 static void		 dcvt(struct odirect *, struct direct *);
121 static void		 flushent(void);
122 static struct inotab	*inotablookup(ufs1_ino_t);
123 static RST_DIR		*opendirfile(const char *);
124 static void		 putdir(char *, long);
125 static void		 putent(struct direct *);
126 static void		 rst_seekdir(RST_DIR *, long, long);
127 static long		 rst_telldir(RST_DIR *);
128 static struct direct	*searchdir(ufs1_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(int genmode)
138 {
139 	int i;
140 	struct ufs1_dinode *ip;
141 	struct inotab *itp;
142 	struct direct nulldir;
143 	int fd;
144 	const char *tmpdir;
145 
146 	vprintf(stdout, "Extract directories from tape\n");
147 	if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
148 		tmpdir = _PATH_TMP;
149 	sprintf(dirfile, "%s/rstdir%ld", tmpdir, (long)dumpdate);
150 	if (command != 'r' && command != 'R') {
151 		(void *) strcat(dirfile, "-XXXXXX");
152 		fd = mkstemp(dirfile);
153 	} else
154 		fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
155 	if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
156 		if (fd != -1)
157 			close(fd);
158 		warn("%s - cannot create directory temporary\nfopen", dirfile);
159 		done(1);
160 	}
161 	if (genmode != 0) {
162 		sprintf(modefile, "%s/rstmode%ld", tmpdir, (long)dumpdate);
163 		if (command != 'r' && command != 'R') {
164 			(void *) strcat(modefile, "-XXXXXX");
165 			fd = mkstemp(modefile);
166 		} else
167 			fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
168 		if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
169 			if (fd != -1)
170 				close(fd);
171 			warn("%s - cannot create modefile\nfopen", modefile);
172 			done(1);
173 		}
174 	}
175 	nulldir.d_ino = 0;
176 	nulldir.d_type = DT_DIR;
177 	nulldir.d_namlen = 1;
178 	strcpy(nulldir.d_name, "/");
179 	nulldir.d_reclen = DIRSIZ(0, &nulldir);
180 	for (;;) {
181 		curfile.name = "<directory file - name unknown>";
182 		curfile.action = USING;
183 		ip = curfile.dip;
184 		if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
185 			fclose(df);
186 			dirp = opendirfile(dirfile);
187 			if (dirp == NULL)
188 				fprintf(stderr, "opendirfile: %s\n",
189 				    strerror(errno));
190 			if (mf != NULL)
191 				fclose(mf);
192 			i = dirlookup(dot);
193 			if (i == 0)
194 				panic("Root directory is not on tape\n");
195 			return;
196 		}
197 		itp = allocinotab(curfile.ino, ip, seekpt);
198 		getfile(putdir, xtrnull);
199 		putent(&nulldir);
200 		flushent();
201 		itp->t_size = seekpt - itp->t_seekpt;
202 	}
203 }
204 
205 /*
206  * skip over all the directories on the tape
207  */
208 void
209 skipdirs(void)
210 {
211 
212 	while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
213 		skipfile();
214 	}
215 }
216 
217 /*
218  *	Recursively find names and inumbers of all files in subtree
219  *	pname and pass them off to be processed.
220  */
221 void
222 treescan(char *pname, ufs1_ino_t ino, long (*todo) (char *, ufs1_ino_t, int))
223 {
224 	struct inotab *itp;
225 	struct direct *dp;
226 	int namelen;
227 	long bpt;
228 	char locname[MAXPATHLEN + 1];
229 
230 	itp = inotablookup(ino);
231 	if (itp == NULL) {
232 		/*
233 		 * Pname is name of a simple file or an unchanged directory.
234 		 */
235 		(*todo)(pname, ino, LEAF);
236 		return;
237 	}
238 	/*
239 	 * Pname is a dumped directory name.
240 	 */
241 	if ((*todo)(pname, ino, NODE) == FAIL)
242 		return;
243 	/*
244 	 * begin search through the directory
245 	 * skipping over "." and ".."
246 	 */
247 	strncpy(locname, pname, sizeof(locname) - 1);
248 	locname[sizeof(locname) - 1] = '\0';
249 	strncat(locname, "/", sizeof(locname) - strlen(locname));
250 	namelen = strlen(locname);
251 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
252 	dp = rst_readdir(dirp); /* "." */
253 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
254 		dp = rst_readdir(dirp); /* ".." */
255 	else
256 		fprintf(stderr, "Warning: `.' missing from directory %s\n",
257 			pname);
258 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
259 		dp = rst_readdir(dirp); /* first real entry */
260 	else
261 		fprintf(stderr, "Warning: `..' missing from directory %s\n",
262 			pname);
263 	bpt = rst_telldir(dirp);
264 	/*
265 	 * a zero inode signals end of directory
266 	 */
267 	while (dp != NULL) {
268 		locname[namelen] = '\0';
269 		if (namelen + dp->d_namlen >= sizeof(locname)) {
270 			fprintf(stderr, "%s%s: name exceeds %d char\n",
271 				locname, dp->d_name, sizeof(locname) - 1);
272 		} else {
273 			strncat(locname, dp->d_name, (int)dp->d_namlen);
274 			treescan(locname, dp->d_ino, todo);
275 			rst_seekdir(dirp, bpt, itp->t_seekpt);
276 		}
277 		dp = rst_readdir(dirp);
278 		bpt = rst_telldir(dirp);
279 	}
280 }
281 
282 /*
283  * Lookup a pathname which is always assumed to start from the ROOTINO.
284  */
285 struct direct *
286 pathsearch(const char *pathname)
287 {
288 	ufs1_ino_t ino;
289 	struct direct *dp;
290 	char *path, *name, buffer[MAXPATHLEN];
291 
292 	strcpy(buffer, pathname);
293 	path = buffer;
294 	ino = ROOTINO;
295 	while (*path == '/')
296 		path++;
297 	dp = NULL;
298 	while ((name = strsep(&path, "/")) != NULL && *name != '\0') {
299 		if ((dp = searchdir(ino, name)) == NULL)
300 			return (NULL);
301 		ino = dp->d_ino;
302 	}
303 	return (dp);
304 }
305 
306 /*
307  * Lookup the requested name in directory inum.
308  * Return its inode number if found, zero if it does not exist.
309  */
310 static struct direct *
311 searchdir(ufs1_ino_t inum, char *name)
312 {
313 	struct direct *dp;
314 	struct inotab *itp;
315 	int len;
316 
317 	itp = inotablookup(inum);
318 	if (itp == NULL)
319 		return (NULL);
320 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
321 	len = strlen(name);
322 	do {
323 		dp = rst_readdir(dirp);
324 		if (dp == NULL)
325 			return (NULL);
326 	} while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
327 	return (dp);
328 }
329 
330 /*
331  * Put the directory entries in the directory file
332  */
333 static void
334 putdir(char *buf, long size)
335 {
336 	struct direct cvtbuf;
337 	struct odirect *odp;
338 	struct odirect *eodp;
339 	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 (Bcvt)
353 				swabst((u_char *)"ls", (u_char *) dp);
354 			if (oldinofmt && dp->d_ino != 0) {
355 #				if BYTE_ORDER == BIG_ENDIAN
356 					if (Bcvt)
357 						dp->d_namlen = dp->d_type;
358 #				else
359 					if (!Bcvt)
360 						dp->d_namlen = dp->d_type;
361 #				endif
362 				dp->d_type = DT_UNKNOWN;
363 			}
364 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
365 			if ((dp->d_reclen & 0x3) != 0 ||
366 			    dp->d_reclen > i ||
367 			    dp->d_reclen < DIRSIZ(0, dp) ||
368 			    dp->d_namlen > NAME_MAX) {
369 				vprintf(stdout, "Mangled directory: ");
370 				if ((dp->d_reclen & 0x3) != 0)
371 					vprintf(stdout,
372 					   "reclen not multiple of 4 ");
373 				if (dp->d_reclen < DIRSIZ(0, dp))
374 					vprintf(stdout,
375 					   "reclen less than DIRSIZ (%d < %d) ",
376 					   dp->d_reclen, DIRSIZ(0, dp));
377 				if (dp->d_namlen > NAME_MAX)
378 					vprintf(stdout,
379 					   "reclen name too big (%d > %d) ",
380 					   dp->d_namlen, NAME_MAX);
381 				vprintf(stdout, "\n");
382 				loc += i;
383 				continue;
384 			}
385 			loc += dp->d_reclen;
386 			if (dp->d_ino != 0) {
387 				putent(dp);
388 			}
389 		}
390 	}
391 }
392 
393 /*
394  * These variables are "local" to the following two functions.
395  */
396 char dirbuf[DIRBLKSIZ];
397 long dirloc = 0;
398 long prev = 0;
399 
400 /*
401  * add a new directory entry to a file.
402  */
403 static void
404 putent(struct direct *dp)
405 {
406 	dp->d_reclen = DIRSIZ(0, dp);
407 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
408 		((struct direct *)(dirbuf + prev))->d_reclen =
409 		    DIRBLKSIZ - prev;
410 		fwrite(dirbuf, 1, DIRBLKSIZ, df);
411 		dirloc = 0;
412 	}
413 	memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
414 	prev = dirloc;
415 	dirloc += dp->d_reclen;
416 }
417 
418 /*
419  * flush out a directory that is finished.
420  */
421 static void
422 flushent(void)
423 {
424 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
425 	fwrite(dirbuf, (int)dirloc, 1, df);
426 	seekpt = ftell(df);
427 	dirloc = 0;
428 }
429 
430 static void
431 dcvt(struct odirect *odp, struct direct *ndp)
432 {
433 
434 	memset(ndp, 0, (long)(sizeof *ndp));
435 	ndp->d_ino =  odp->d_ino;
436 	ndp->d_type = DT_UNKNOWN;
437 	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(RST_DIR *dirp, long loc, long base)
451 {
452 
453 	if (loc == rst_telldir(dirp))
454 		return;
455 	loc -= base;
456 	if (loc < 0)
457 		fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
458 	lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
459 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
460 	if (dirp->dd_loc != 0)
461 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
462 }
463 
464 /*
465  * get next entry in a directory.
466  */
467 struct direct *
468 rst_readdir(RST_DIR *dirp)
469 {
470 	struct direct *dp;
471 
472 	for (;;) {
473 		if (dirp->dd_loc == 0) {
474 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
475 			    DIRBLKSIZ);
476 			if (dirp->dd_size <= 0) {
477 				dprintf(stderr, "error reading directory\n");
478 				return (NULL);
479 			}
480 		}
481 		if (dirp->dd_loc >= dirp->dd_size) {
482 			dirp->dd_loc = 0;
483 			continue;
484 		}
485 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
486 		if (dp->d_reclen == 0 ||
487 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
488 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
489 				dp->d_reclen);
490 			return (NULL);
491 		}
492 		dirp->dd_loc += dp->d_reclen;
493 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
494 			return (NULL);
495 		if (dp->d_ino >= maxino) {
496 			dprintf(stderr, "corrupted directory: bad inum %d\n",
497 				dp->d_ino);
498 			continue;
499 		}
500 		return (dp);
501 	}
502 }
503 
504 /*
505  * Simulate the opening of a directory
506  */
507 RST_DIR *
508 rst_opendir(const char *name)
509 {
510 	struct inotab *itp;
511 	RST_DIR *dirp;
512 	ufs1_ino_t ino;
513 
514 	if ((ino = dirlookup(name)) > 0 &&
515 	    (itp = inotablookup(ino)) != NULL) {
516 		dirp = opendirfile(dirfile);
517 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
518 		return (dirp);
519 	}
520 	return (NULL);
521 }
522 
523 /*
524  * In our case, there is nothing to do when closing a directory.
525  */
526 void
527 rst_closedir(RST_DIR *dirp)
528 {
529 
530 	close(dirp->dd_fd);
531 	free(dirp);
532 	return;
533 }
534 
535 /*
536  * Simulate finding the current offset in the directory.
537  */
538 static long
539 rst_telldir(RST_DIR *dirp)
540 {
541 	return ((long)lseek(dirp->dd_fd,
542 	    (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
543 }
544 
545 /*
546  * Open a directory file.
547  */
548 static RST_DIR *
549 opendirfile(const char *name)
550 {
551 	RST_DIR *dirp;
552 	int fd;
553 
554 	if ((fd = open(name, O_RDONLY)) == -1)
555 		return (NULL);
556 	if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
557 		close(fd);
558 		return (NULL);
559 	}
560 	dirp->dd_fd = fd;
561 	dirp->dd_loc = 0;
562 	return (dirp);
563 }
564 
565 /*
566  * Set the mode, owner, and times for all new or changed directories
567  */
568 void
569 setdirmodes(int flags)
570 {
571 	FILE *mf;
572 	struct modeinfo node;
573 	struct entry *ep;
574 	char *cp;
575 	const char *tmpdir;
576 
577 	vprintf(stdout, "Set directory mode, owner, and times.\n");
578 	if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
579 		tmpdir = _PATH_TMP;
580 	if (command == 'r' || command == 'R')
581 		sprintf(modefile, "%s/rstmode%ld", tmpdir, (long)dumpdate);
582 	if (modefile[0] == '#') {
583 		panic("modefile not defined\n");
584 		fprintf(stderr, "directory mode, owner, and times not set\n");
585 		return;
586 	}
587 	mf = fopen(modefile, "r");
588 	if (mf == NULL) {
589 		fprintf(stderr, "fopen: %s\n", strerror(errno));
590 		fprintf(stderr, "cannot open mode file %s\n", modefile);
591 		fprintf(stderr, "directory mode, owner, and times not set\n");
592 		return;
593 	}
594 	clearerr(mf);
595 	for (;;) {
596 		fread((char *)&node, 1, sizeof(struct modeinfo), mf);
597 		if (feof(mf))
598 			break;
599 		ep = lookupino(node.ino);
600 		if (command == 'i' || command == 'x') {
601 			if (ep == NULL)
602 				continue;
603 			if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
604 				ep->e_flags &= ~NEW;
605 				continue;
606 			}
607 			if (node.ino == ROOTINO &&
608 		   	    reply("set owner/mode for '.'") == FAIL)
609 				continue;
610 		}
611 		if (ep == NULL) {
612 			panic("cannot find directory inode %d\n", node.ino);
613 		} else {
614 			cp = myname(ep);
615 			if (!Nflag) {
616 				chown(cp, node.uid, node.gid);
617 				chmod(cp, node.mode);
618 				utimes(cp, node.timep);
619 				chflags(cp, node.flags);
620 			}
621 			ep->e_flags &= ~NEW;
622 		}
623 	}
624 	if (ferror(mf))
625 		panic("error setting directory modes\n");
626 	fclose(mf);
627 }
628 
629 /*
630  * Generate a literal copy of a directory.
631  */
632 int
633 genliteraldir(char *name, ufs1_ino_t ino)
634 {
635 	struct inotab *itp;
636 	int ofile, dp, i, size;
637 	char buf[BUFSIZ];
638 
639 	itp = inotablookup(ino);
640 	if (itp == NULL)
641 		panic("Cannot find directory inode %d named %s\n", ino, name);
642 	if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
643 		fprintf(stderr, "%s: ", name);
644 		fflush(stderr);
645 		fprintf(stderr, "cannot create file: %s\n", strerror(errno));
646 		return (FAIL);
647 	}
648 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
649 	dp = dup(dirp->dd_fd);
650 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
651 		size = i < BUFSIZ ? i : BUFSIZ;
652 		if (read(dp, buf, (int) size) == -1) {
653 			fprintf(stderr,
654 				"write error extracting inode %d, name %s\n",
655 				curfile.ino, curfile.name);
656 			fprintf(stderr, "read: %s\n", strerror(errno));
657 			done(1);
658 		}
659 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
660 			fprintf(stderr,
661 				"write error extracting inode %d, name %s\n",
662 				curfile.ino, curfile.name);
663 			fprintf(stderr, "write: %s\n", strerror(errno));
664 			done(1);
665 		}
666 	}
667 	close(dp);
668 	close(ofile);
669 	return (GOOD);
670 }
671 
672 /*
673  * Determine the type of an inode
674  */
675 int
676 inodetype(ufs1_ino_t ino)
677 {
678 	struct inotab *itp;
679 
680 	itp = inotablookup(ino);
681 	if (itp == NULL)
682 		return (LEAF);
683 	return (NODE);
684 }
685 
686 /*
687  * Allocate and initialize a directory inode entry.
688  * If requested, save its pertinent mode, owner, and time info.
689  */
690 static struct inotab *
691 allocinotab(ufs1_ino_t ino, struct ufs1_dinode *dip, long seekpt)
692 {
693 	struct inotab	*itp;
694 	struct modeinfo node;
695 
696 	itp = calloc(1, sizeof(struct inotab));
697 	if (itp == NULL)
698 		panic("no memory directory table\n");
699 	itp->t_next = inotab[INOHASH(ino)];
700 	inotab[INOHASH(ino)] = itp;
701 	itp->t_ino = ino;
702 	itp->t_seekpt = seekpt;
703 	if (mf == NULL)
704 		return (itp);
705 	node.ino = ino;
706 	node.timep[0].tv_sec = dip->di_atime;
707 	node.timep[0].tv_usec = dip->di_atimensec / 1000;
708 	node.timep[1].tv_sec = dip->di_mtime;
709 	node.timep[1].tv_usec = dip->di_mtimensec / 1000;
710 	node.mode = dip->di_mode;
711 	node.flags = dip->di_flags;
712 	node.uid = dip->di_uid;
713 	node.gid = dip->di_gid;
714 	fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
715 	return (itp);
716 }
717 
718 /*
719  * Look up an inode in the table of directories
720  */
721 static struct inotab *
722 inotablookup(ufs1_ino_t ino)
723 {
724 	struct inotab *itp;
725 
726 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
727 		if (itp->t_ino == ino)
728 			return (itp);
729 	return (NULL);
730 }
731 
732 /*
733  * Clean up and exit
734  */
735 void
736 done(int exitcode)
737 {
738 
739 	closemt();
740 	if (modefile[0] != '#')
741 		unlink(modefile);
742 	if (dirfile[0] != '#')
743 		unlink(dirfile);
744 	exit(exitcode);
745 }
746