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