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