]> git.wh0rd.org Git - dump.git/blob - restore/dirs.c
Initial revision
[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, 1995, 1996
5  *
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 char sccsid[] = "@(#)dirs.c      8.7 (Berkeley) 5/1/95";
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/time.h>
54
55 #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 #include <ufs/ffs/fs.h>
62 #endif  /* __linux__ */
63 #include <protocols/dumprestore.h>
64
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         long    t_seekpt;
90         long    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         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         long    dd_loc;
114         long    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[32] = "#";      /* No file */
125 static char     modefile[32] = "#";     /* 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 *, long));
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(genmode)
160         int genmode;
161 {
162         register int i;
163 #ifdef  __linux__
164         register struct new_bsd_inode *ip;
165 #else
166         register struct dinode *ip;
167 #endif
168         struct inotab *itp;
169         struct direct nulldir;
170
171         vprintf(stdout, "Extract directories from tape\n");
172         (void) sprintf(dirfile, "%s/rstdir%d", tmpdir, dumpdate);
173         df = fopen(dirfile, "w");
174         if (df == NULL) {
175                 fprintf(stderr,
176                     "restore: %s - cannot create directory temporary\n",
177                     dirfile);
178                 fprintf(stderr, "fopen: %s\n", strerror(errno));
179                 done(1);
180         }
181         if (genmode != 0) {
182                 (void) sprintf(modefile, "%s/rstmode%d", tmpdir, dumpdate);
183                 mf = fopen(modefile, "w");
184                 if (mf == NULL) {
185                         fprintf(stderr,
186                             "restore: %s - cannot create modefile \n",
187                             modefile);
188                         fprintf(stderr, "fopen: %s\n", strerror(errno));
189                         done(1);
190                 }
191         }
192         nulldir.d_ino = 0;
193 #ifdef  __linux__
194         nulldir.d_type = DT_DIR;
195 #endif
196         nulldir.d_namlen = 1;
197         (void) strcpy(nulldir.d_name, "/");
198         nulldir.d_reclen = DIRSIZ(0, &nulldir);
199         for (;;) {
200                 curfile.name = "<directory file - name unknown>";
201                 curfile.action = USING;
202                 ip = curfile.dip;
203                 if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
204                         (void) fclose(df);
205                         dirp = opendirfile(dirfile);
206                         if (dirp == NULL)
207                                 fprintf(stderr, "opendirfile: %s\n",
208                                     strerror(errno));
209                         if (mf != NULL)
210                                 (void) fclose(mf);
211                         i = dirlookup(dot);
212                         if (i == 0)
213                                 panic("Root directory is not on tape\n");
214                         return;
215                 }
216                 itp = allocinotab(curfile.ino, ip, seekpt);
217                 getfile(putdir, xtrnull);
218                 putent(&nulldir);
219                 flushent();
220                 itp->t_size = seekpt - itp->t_seekpt;
221         }
222 }
223
224 /*
225  * skip over all the directories on the tape
226  */
227 void
228 skipdirs()
229 {
230
231         while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
232                 skipfile();
233         }
234 }
235
236 /*
237  *      Recursively find names and inumbers of all files in subtree 
238  *      pname and pass them off to be processed.
239  */
240 void
241 treescan(pname, ino, todo)
242         char *pname;
243         ino_t ino;
244         long (*todo) __P((char *, ino_t, int));
245 {
246         register struct inotab *itp;
247         register struct direct *dp;
248         int namelen;
249         long bpt;
250         char locname[MAXPATHLEN + 1];
251
252         itp = inotablookup(ino);
253         if (itp == NULL) {
254                 /*
255                  * Pname is name of a simple file or an unchanged directory.
256                  */
257                 (void) (*todo)(pname, ino, LEAF);
258                 return;
259         }
260         /*
261          * Pname is a dumped directory name.
262          */
263         if ((*todo)(pname, ino, NODE) == FAIL)
264                 return;
265         /*
266          * begin search through the directory
267          * skipping over "." and ".."
268          */
269         (void) strncpy(locname, pname, MAXPATHLEN);
270         (void) strncat(locname, "/", MAXPATHLEN);
271         namelen = strlen(locname);
272         rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
273         dp = rst_readdir(dirp); /* "." */
274         if (dp != NULL && strcmp(dp->d_name, ".") == 0)
275                 dp = rst_readdir(dirp); /* ".." */
276         else
277                 fprintf(stderr, "Warning: `.' missing from directory %s\n",
278                         pname);
279         if (dp != NULL && strcmp(dp->d_name, "..") == 0)
280                 dp = rst_readdir(dirp); /* first real entry */
281         else
282                 fprintf(stderr, "Warning: `..' missing from directory %s\n",
283                         pname);
284         bpt = rst_telldir(dirp);
285         /*
286          * a zero inode signals end of directory
287          */
288         while (dp != NULL) {
289                 locname[namelen] = '\0';
290                 if (namelen + dp->d_namlen >= MAXPATHLEN) {
291                         fprintf(stderr, "%s%s: name exceeds %d char\n",
292                                 locname, dp->d_name, MAXPATHLEN);
293                 } else {
294                         (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
295                         treescan(locname, dp->d_ino, todo);
296                         rst_seekdir(dirp, bpt, itp->t_seekpt);
297                 }
298                 dp = rst_readdir(dirp);
299                 bpt = rst_telldir(dirp);
300         }
301 }
302
303 /*
304  * Lookup a pathname which is always assumed to start from the ROOTINO.
305  */
306 struct direct *
307 pathsearch(pathname)
308         const char *pathname;
309 {
310         ino_t ino;
311         struct direct *dp;
312         char *path, *name, buffer[MAXPATHLEN];
313
314         strcpy(buffer, pathname);
315         path = buffer;
316         ino = ROOTINO;
317         while (*path == '/')
318                 path++;
319         dp = NULL;
320         while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
321                 if ((dp = searchdir(ino, name)) == NULL)
322                         return (NULL);
323                 ino = dp->d_ino;
324         }
325         return (dp);
326 }
327
328 /*
329  * Lookup the requested name in directory inum.
330  * Return its inode number if found, zero if it does not exist.
331  */
332 static struct direct *
333 searchdir(inum, name)
334         ino_t   inum;
335         char    *name;
336 {
337         register struct direct *dp;
338         register struct inotab *itp;
339         int len;
340
341         itp = inotablookup(inum);
342         if (itp == NULL)
343                 return (NULL);
344         rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
345         len = strlen(name);
346         do {
347                 dp = rst_readdir(dirp);
348                 if (dp == NULL)
349                         return (NULL);
350         } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
351         return (dp);
352 }
353
354 /*
355  * Put the directory entries in the directory file
356  */
357 static void
358 putdir(buf, size)
359         char *buf;
360         long size;
361 {
362         struct direct cvtbuf;
363         register struct odirect *odp;
364         struct odirect *eodp;
365         register struct direct *dp;
366         long loc, i;
367
368         if (cvtflag) {
369                 eodp = (struct odirect *)&buf[size];
370                 for (odp = (struct odirect *)buf; odp < eodp; odp++)
371                         if (odp->d_ino != 0) {
372                                 dcvt(odp, &cvtbuf);
373                                 putent(&cvtbuf);
374                         }
375         } else {
376                 for (loc = 0; loc < size; ) {
377                         dp = (struct direct *)(buf + loc);
378 #ifdef  DIRDEBUG
379                         printf ("reclen = %d, namlen = %d, type = %d\n",
380                                 dp->d_reclen, dp->d_namlen, dp->d_type);
381 #endif
382                         if (Bcvt)
383                                 swabst((u_char *)"ls", (u_char *) dp);
384                         if (oldinofmt && dp->d_ino != 0) {
385 #ifdef  __linux__
386                                 if (Bcvt)
387                                         swabst((u_char *)"s", (u_char *)&dp->d_namlen);
388 #else
389 #                               if BYTE_ORDER == BIG_ENDIAN
390                                         if (Bcvt)
391                                                 dp->d_namlen = dp->d_type;
392 #                               else
393                                         if (!Bcvt)
394                                                 dp->d_namlen = dp->d_type;
395 #                               endif
396 #endif  /* __linux__ */
397                                 dp->d_type = DT_UNKNOWN;
398                         }
399 #ifdef  __linux__
400                         /*
401                          * Horrible hack to read FreeBSD 2.0 dumps
402                          */
403                         if (!oldinofmt)
404                                 swabst((u_char *)"6bs", (u_char *) dp);
405 #endif  /* __linux__ */
406 #ifdef  DIRDEBUG
407                         printf ("reclen = %d, namlen = %d, type = %d\n",
408                                 dp->d_reclen, dp->d_namlen, dp->d_type);
409 #endif
410                         i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
411                         if ((dp->d_reclen & 0x3) != 0 ||
412                             dp->d_reclen > i ||
413                             dp->d_reclen < DIRSIZ(0, dp) ||
414                             dp->d_namlen > NAME_MAX) {
415                                 vprintf(stdout, "Mangled directory: ");
416                                 if ((dp->d_reclen & 0x3) != 0)
417                                         vprintf(stdout,
418                                            "reclen not multiple of 4 ");
419                                 if (dp->d_reclen < DIRSIZ(0, dp))
420                                         vprintf(stdout,
421                                            "reclen less than DIRSIZ (%d < %d) ",
422                                            dp->d_reclen, DIRSIZ(0, dp));
423                                 if (dp->d_namlen > NAME_MAX)
424                                         vprintf(stdout,
425                                            "reclen name too big (%d > %d) ",
426                                            dp->d_namlen, NAME_MAX);
427                                 vprintf(stdout, "\n");
428                                 loc += i;
429                                 continue;
430                         }
431                         loc += dp->d_reclen;
432                         if (dp->d_ino != 0) {
433                                 putent(dp);
434                         }
435                 }
436         }
437 }
438
439 /*
440  * These variables are "local" to the following two functions.
441  */
442 char dirbuf[DIRBLKSIZ];
443 long dirloc = 0;
444 long prev = 0;
445
446 /*
447  * add a new directory entry to a file.
448  */
449 static void
450 putent(dp)
451         struct direct *dp;
452 {
453         dp->d_reclen = DIRSIZ(0, dp);
454         if (dirloc + dp->d_reclen > DIRBLKSIZ) {
455                 ((struct direct *)(dirbuf + prev))->d_reclen =
456                     DIRBLKSIZ - prev;
457                 (void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
458                 dirloc = 0;
459         }
460         memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
461         prev = dirloc;
462         dirloc += dp->d_reclen;
463 }
464
465 /*
466  * flush out a directory that is finished.
467  */
468 static void
469 flushent()
470 {
471         ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
472         (void) fwrite(dirbuf, (int)dirloc, 1, df);
473         seekpt = ftell(df);
474         dirloc = 0;
475 }
476
477 static void
478 dcvt(odp, ndp)
479         register struct odirect *odp;
480         register struct direct *ndp;
481 {
482
483         memset(ndp, 0, (long)(sizeof *ndp));
484         ndp->d_ino =  odp->d_ino;
485 #ifdef  __linux__
486         ndp->d_type = DT_UNKNOWN;
487 #endif
488         (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
489         ndp->d_namlen = strlen(ndp->d_name);
490         ndp->d_reclen = DIRSIZ(0, ndp);
491 }
492
493 /*
494  * Seek to an entry in a directory.
495  * Only values returned by rst_telldir should be passed to rst_seekdir.
496  * This routine handles many directories in a single file.
497  * It takes the base of the directory in the file, plus
498  * the desired seek offset into it.
499  */
500 static void
501 rst_seekdir(dirp, loc, base)
502         register RST_DIR *dirp;
503         long loc, base;
504 {
505
506         if (loc == rst_telldir(dirp))
507                 return;
508         loc -= base;
509         if (loc < 0)
510                 fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
511         (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
512         dirp->dd_loc = loc & (DIRBLKSIZ - 1);
513         if (dirp->dd_loc != 0)
514                 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
515 }
516
517 /*
518  * get next entry in a directory.
519  */
520 struct direct *
521 rst_readdir(dirp)
522         register RST_DIR *dirp;
523 {
524         register struct direct *dp;
525
526         for (;;) {
527                 if (dirp->dd_loc == 0) {
528                         dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 
529                             DIRBLKSIZ);
530                         if (dirp->dd_size <= 0) {
531                                 dprintf(stderr, "error reading directory\n");
532                                 return (NULL);
533                         }
534                 }
535                 if (dirp->dd_loc >= dirp->dd_size) {
536                         dirp->dd_loc = 0;
537                         continue;
538                 }
539                 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
540                 if (dp->d_reclen == 0 ||
541                     dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
542                         dprintf(stderr, "corrupted directory: bad reclen %d\n"
543                                 "dd_loc = %d, dd_size = %d\n",
544                                 dp->d_reclen, dirp->dd_loc, dirp->dd_size);
545                         return (NULL);
546                 }
547                 dirp->dd_loc += dp->d_reclen;
548                 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
549                         return (NULL);
550                 if (dp->d_ino >= maxino) {
551                         dprintf(stderr, "corrupted directory: bad inum %d\n",
552                                 dp->d_ino);
553                         continue;
554                 }
555                 return (dp);
556         }
557 }
558
559 /*
560  * Simulate the opening of a directory
561  */
562 RST_DIR *
563 rst_opendir(name)
564         const char *name;
565 {
566         struct inotab *itp;
567         RST_DIR *dirp;
568         ino_t ino;
569
570         if ((ino = dirlookup(name)) > 0 &&
571             (itp = inotablookup(ino)) != NULL) {
572                 dirp = opendirfile(dirfile);
573                 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
574                 return (dirp);
575         }
576         return (NULL);
577 }
578
579 /*
580  * In our case, there is nothing to do when closing a directory.
581  */
582 void
583 rst_closedir(dirp)
584         RST_DIR *dirp;
585 {
586
587         (void)close(dirp->dd_fd);
588         free(dirp);
589         return;
590 }
591
592 /*
593  * Simulate finding the current offset in the directory.
594  */
595 static long
596 rst_telldir(dirp)
597         RST_DIR *dirp;
598 {
599         return ((long)lseek(dirp->dd_fd,
600             (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
601 }
602
603 /*
604  * Open a directory file.
605  */
606 static RST_DIR *
607 opendirfile(name)
608         const char *name;
609 {
610         register RST_DIR *dirp;
611         register int fd;
612
613         if ((fd = open(name, O_RDONLY)) == -1)
614                 return (NULL);
615         if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
616                 (void)close(fd);
617                 return (NULL);
618         }
619         dirp->dd_fd = fd;
620         dirp->dd_loc = 0;
621         return (dirp);
622 }
623
624 /*
625  * Set the mode, owner, and times for all new or changed directories
626  */
627 void
628 setdirmodes(flags)
629         int flags;
630 {
631         FILE *mf;
632         struct modeinfo node;
633         struct entry *ep;
634         char *cp;
635         
636         vprintf(stdout, "Set directory mode, owner, and times.\n");
637         (void) sprintf(modefile, "%s/rstmode%d", tmpdir, dumpdate);
638         mf = fopen(modefile, "r");
639         if (mf == NULL) {
640                 fprintf(stderr, "fopen: %s\n", strerror(errno));
641                 fprintf(stderr, "cannot open mode file %s\n", modefile);
642                 fprintf(stderr, "directory mode, owner, and times not set\n");
643                 return;
644         }
645         clearerr(mf);
646         for (;;) {
647                 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
648                 if (feof(mf))
649                         break;
650                 ep = lookupino(node.ino);
651                 if (command == 'i' || command == 'x') {
652                         if (ep == NULL)
653                                 continue;
654                         if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
655                                 ep->e_flags &= ~NEW;
656                                 continue;
657                         }
658                         if (node.ino == ROOTINO &&
659                             reply("set owner/mode for '.'") == FAIL)
660                                 continue;
661                 }
662                 if (ep == NULL) {
663                         panic("cannot find directory inode %d\n", node.ino);
664                 } else {
665                         cp = myname(ep);
666                         (void) chown(cp, node.uid, node.gid);
667                         (void) chmod(cp, node.mode);
668 #ifdef  __linux__
669                         (void) setflags(cp, node.flags);
670 #else
671                         (void) chflags(cp, node.flags);
672 #endif
673                         utimes(cp, node.timep);
674                         ep->e_flags &= ~NEW;
675                 }
676         }
677         if (ferror(mf))
678                 panic("error setting directory modes\n");
679         (void) fclose(mf);
680 }
681
682 /*
683  * Generate a literal copy of a directory.
684  */
685 int
686 genliteraldir(name, ino)
687         char *name;
688         ino_t ino;
689 {
690         register struct inotab *itp;
691         int ofile, dp, i, size;
692         char buf[BUFSIZ];
693
694         itp = inotablookup(ino);
695         if (itp == NULL)
696                 panic("Cannot find directory inode %d named %s\n", ino, name);
697         if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
698                 fprintf(stderr, "%s: ", name);
699                 (void) fflush(stderr);
700                 fprintf(stderr, "cannot create file: %s\n", strerror(errno));
701                 return (FAIL);
702         }
703         rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
704         dp = dup(dirp->dd_fd);
705         for (i = itp->t_size; i > 0; i -= BUFSIZ) {
706                 size = i < BUFSIZ ? i : BUFSIZ;
707                 if (read(dp, buf, (int) size) == -1) {
708                         fprintf(stderr,
709                                 "write error extracting inode %d, name %s\n",
710                                 curfile.ino, curfile.name);
711                         fprintf(stderr, "read: %s\n", strerror(errno));
712                         done(1);
713                 }
714                 if (!Nflag && write(ofile, buf, (int) size) == -1) {
715                         fprintf(stderr,
716                                 "write error extracting inode %d, name %s\n",
717                                 curfile.ino, curfile.name);
718                         fprintf(stderr, "write: %s\n", strerror(errno));
719                         done(1);
720                 }
721         }
722         (void) close(dp);
723         (void) close(ofile);
724         return (GOOD);
725 }
726
727 /*
728  * Determine the type of an inode
729  */
730 int
731 inodetype(ino)
732         ino_t ino;
733 {
734         struct inotab *itp;
735
736         itp = inotablookup(ino);
737         if (itp == NULL)
738                 return (LEAF);
739         return (NODE);
740 }
741
742 /*
743  * Allocate and initialize a directory inode entry.
744  * If requested, save its pertinent mode, owner, and time info.
745  */
746 static struct inotab *
747 allocinotab(ino, dip, seekpt)
748         ino_t ino;
749 #ifdef  __linux__
750         struct new_bsd_inode *dip;
751 #else
752         struct dinode *dip;
753 #endif
754         long seekpt;
755 {
756         register struct inotab  *itp;
757         struct modeinfo node;
758
759         itp = calloc(1, sizeof(struct inotab));
760         if (itp == NULL)
761                 panic("no memory directory table\n");
762         itp->t_next = inotab[INOHASH(ino)];
763         inotab[INOHASH(ino)] = itp;
764         itp->t_ino = ino;
765         itp->t_seekpt = seekpt;
766         if (mf == NULL)
767                 return (itp);
768         node.ino = ino;
769 #ifdef  __linux__
770         node.timep[0].tv_sec = dip->di_atime.tv_sec;
771         node.timep[0].tv_usec = dip->di_atime.tv_usec;
772         node.timep[1].tv_sec = dip->di_mtime.tv_sec;
773         node.timep[1].tv_usec = dip->di_mtime.tv_usec;
774 #else   /* __linux__ */
775         node.timep[0].tv_sec = dip->di_atime;
776         node.timep[0].tv_usec = dip->di_atimensec / 1000;
777         node.timep[1].tv_sec = dip->di_mtime;
778         node.timep[1].tv_usec = dip->di_mtimensec / 1000;
779 #endif  /* __linux__ */
780         node.mode = dip->di_mode;
781         node.flags = dip->di_flags;
782         node.uid = dip->di_uid;
783         node.gid = dip->di_gid;
784         (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
785         return (itp);
786 }
787
788 /*
789  * Look up an inode in the table of directories
790  */
791 static struct inotab *
792 inotablookup(ino)
793         ino_t   ino;
794 {
795         register struct inotab *itp;
796
797         for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
798                 if (itp->t_ino == ino)
799                         return (itp);
800         return (NULL);
801 }
802
803 /*
804  * Clean up and exit
805  */
806 __dead void
807 done(exitcode)
808         int exitcode;
809 {
810
811         closemt();
812         if (modefile[0] != '#')
813                 (void) unlink(modefile);
814         if (dirfile[0] != '#')
815                 (void) unlink(dirfile);
816         exit(exitcode);
817 }