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