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