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