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