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