386BSD 0.1 development
[unix-history] / usr / src / sbin / restore / dirs.c
index 701249c..b2c00f9 100644 (file)
-/* Copyright (c) 1983 Regents of the University of California */
+/*
+ * Copyright (c) 1983 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)dirs.c     3.1     (Berkeley)      83/02/18";
-#endif
+static char sccsid[] = "@(#)dirs.c     5.15 (Berkeley) 2/26/91";
+#endif /* not lint */
 
 #include "restore.h"
 
 #include "restore.h"
-#include <dumprestor.h>
-#include <file.h>
-#include <dir.h>
+#include <protocols/dumprestore.h>
+#include <sys/file.h>
+#include <ufs/dir.h>
+#include "pathnames.h"
 
 
+/*
+ * Symbol table of directories read from tape.
+ */
 #define HASHSIZE       1000
 #define HASHSIZE       1000
-
 #define INOHASH(val) (val % HASHSIZE)
 struct inotab {
        struct inotab *t_next;
        ino_t   t_ino;
        daddr_t t_seekpt;
        long t_size;
 #define INOHASH(val) (val % HASHSIZE)
 struct inotab {
        struct inotab *t_next;
        ino_t   t_ino;
        daddr_t t_seekpt;
        long t_size;
-} *inotab[HASHSIZE];
-struct inotab *inotablookup();
+};
+static struct inotab *inotab[HASHSIZE];
+extern struct inotab *inotablookup();
+extern struct inotab *allocinotab();
 
 
+/*
+ * Information retained about directories.
+ */
 struct modeinfo {
        ino_t ino;
 struct modeinfo {
        ino_t ino;
-       time_t timep[2];
+       struct timeval timep[2];
        short mode;
        short uid;
        short gid;
 };
 
        short mode;
        short uid;
        short gid;
 };
 
-daddr_t        seekpt;
-FILE   *df, *mf;
-DIR    *dirp;
-char   dirfile[] = "/tmp/rstaXXXXXX";
-extern ino_t search();
+/*
+ * Definitions for library routines operating on directories.
+ */
+#undef DIRBLKSIZ
+#define DIRBLKSIZ 1024
+struct dirdesc {
+       int     dd_fd;
+       long    dd_loc;
+       long    dd_size;
+       char    dd_buf[DIRBLKSIZ];
+};
+extern DIR *opendirfile();
+extern off_t rst_telldir();
+extern void rst_seekdir();
 
 
+/*
+ * Global variables for this file.
+ */
+static daddr_t seekpt;
+static FILE    *df, *mf;
+static DIR     *dirp;
+static char    dirfile[32] = "#";      /* No file */
+static char    modefile[32] = "#";     /* No file */
+static char    dot[2] = ".";           /* So it can be modified */
+extern ino_t   search();
+struct direct  *rst_readdir();
+extern void    rst_seekdir();
+
+/*
+ * Format of old style directories.
+ */
 #define ODIRSIZ 14
 struct odirect {
        u_short d_ino;
 #define ODIRSIZ 14
 struct odirect {
        u_short d_ino;
@@ -43,59 +107,75 @@ struct odirect {
 /*
  *     Extract directory contents, building up a directory structure
  *     on disk for extraction by name.
 /*
  *     Extract directory contents, building up a directory structure
  *     on disk for extraction by name.
- *     If modefile is requested, save mode, owner, and times for all
+ *     If genmode is requested, save mode, owner, and times for all
  *     directories on the tape.
  */
  *     directories on the tape.
  */
-extractdirs(modefile)
-       char *modefile;
+extractdirs(genmode)
+       int genmode;
 {
        register int i;
        register struct dinode *ip;
 {
        register int i;
        register struct dinode *ip;
+       struct inotab *itp;
        struct direct nulldir;
        int putdir(), null();
 
        vprintf(stdout, "Extract directories from tape\n");
        struct direct nulldir;
        int putdir(), null();
 
        vprintf(stdout, "Extract directories from tape\n");
-       mktemp(dirfile);
+       (void) sprintf(dirfile, "%s/rstdir%d", _PATH_TMP, dumpdate);
        df = fopen(dirfile, "w");
        if (df == 0) {
                fprintf(stderr,
        df = fopen(dirfile, "w");
        if (df == 0) {
                fprintf(stderr,
-                   "restor: %s - cannot create directory temporary\n",
+                   "restore: %s - cannot create directory temporary\n",
                    dirfile);
                perror("fopen");
                done(1);
        }
                    dirfile);
                perror("fopen");
                done(1);
        }
-       if (modefile != NULL) {
+       if (genmode != 0) {
+               (void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
                mf = fopen(modefile, "w");
                mf = fopen(modefile, "w");
-               if (df == 0) {
+               if (mf == 0) {
                        fprintf(stderr,
                        fprintf(stderr,
-                           "restor: %s - cannot create modefile \n",
+                           "restore: %s - cannot create modefile \n",
                            modefile);
                        perror("fopen");
                        done(1);
                }
        }
                            modefile);
                        perror("fopen");
                        done(1);
                }
        }
-       nulldir.d_ino = 1;
+       nulldir.d_ino = 0;
        nulldir.d_namlen = 1;
        nulldir.d_namlen = 1;
-       strncpy(nulldir.d_name, "/", nulldir.d_namlen);
+       (void) strcpy(nulldir.d_name, "/");
        nulldir.d_reclen = DIRSIZ(&nulldir);
        for (;;) {
                curfile.name = "<directory file - name unknown>";
                curfile.action = USING;
                ip = curfile.dip;
        nulldir.d_reclen = DIRSIZ(&nulldir);
        for (;;) {
                curfile.name = "<directory file - name unknown>";
                curfile.action = USING;
                ip = curfile.dip;
-               i = ip->di_mode & IFMT;
-               if (i != IFDIR) {
-                       fclose(df);
-                       dirp = opendir(dirfile);
+               if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
+                       (void) fclose(df);
+                       dirp = opendirfile(dirfile);
                        if (dirp == NULL)
                        if (dirp == NULL)
-                               perror("opendir");
+                               perror("opendirfile");
                        if (mf != NULL)
                        if (mf != NULL)
-                               fclose(mf);
+                               (void) fclose(mf);
+                       i = dirlookup(dot);
+                       if (i == 0)
+                               panic("Root directory is not on tape\n");
                        return;
                }
                        return;
                }
-               allocinotab(curfile.ino, ip, seekpt);
+               itp = allocinotab(curfile.ino, ip, seekpt);
                getfile(putdir, null);
                putent(&nulldir);
                flushent();
                getfile(putdir, null);
                putent(&nulldir);
                flushent();
+               itp->t_size = seekpt - itp->t_seekpt;
+       }
+}
+
+/*
+ * skip over all the directories on the tape
+ */
+skipdirs()
+{
+
+       while ((curfile.dip->di_mode & IFMT) == IFDIR) {
+               skipfile();
        }
 }
 
        }
 }
 
@@ -106,53 +186,63 @@ extractdirs(modefile)
 treescan(pname, ino, todo)
        char *pname;
        ino_t ino;
 treescan(pname, ino, todo)
        char *pname;
        ino_t ino;
-       void (*todo)();
+       long (*todo)();
 {
        register struct inotab *itp;
 {
        register struct inotab *itp;
+       register struct direct *dp;
+       register struct entry *np;
        int namelen;
        daddr_t bpt;
        int namelen;
        daddr_t bpt;
-       register struct direct *dp;
-       char locname[BUFSIZ + 1];
+       char locname[MAXPATHLEN + 1];
 
        itp = inotablookup(ino);
        if (itp == NULL) {
                /*
                 * Pname is name of a simple file or an unchanged directory.
                 */
 
        itp = inotablookup(ino);
        if (itp == NULL) {
                /*
                 * Pname is name of a simple file or an unchanged directory.
                 */
-               (*todo)(pname, ino, LEAF);
+               (void) (*todo)(pname, ino, LEAF);
                return;
        }
        /*
         * Pname is a dumped directory name.
         */
                return;
        }
        /*
         * Pname is a dumped directory name.
         */
-       (*todo)(pname, ino, NODE);
+       if ((*todo)(pname, ino, NODE) == FAIL)
+               return;
        /*
         * begin search through the directory
         * skipping over "." and ".."
         */
        /*
         * begin search through the directory
         * skipping over "." and ".."
         */
-       strncpy(locname, pname, BUFSIZ);
-       strncat(locname, "/", BUFSIZ);
+       (void) strncpy(locname, pname, MAXPATHLEN);
+       (void) strncat(locname, "/", MAXPATHLEN);
        namelen = strlen(locname);
        namelen = strlen(locname);
-       seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
-       dp = readdir(dirp); /* "." */
-       dp = readdir(dirp); /* ".." */
-       dp = readdir(dirp); /* first real entry */
-       bpt = telldir(dirp);
+       rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
+       dp = rst_readdir(dirp); /* "." */
+       if (dp != NULL && strcmp(dp->d_name, ".") == 0)
+               dp = rst_readdir(dirp); /* ".." */
+       else
+               fprintf(stderr, "Warning: `.' missing from directory %s\n",
+                       pname);
+       if (dp != NULL && strcmp(dp->d_name, "..") == 0)
+               dp = rst_readdir(dirp); /* first real entry */
+       else
+               fprintf(stderr, "Warning: `..' missing from directory %s\n",
+                       pname);
+       bpt = rst_telldir(dirp);
        /*
        /*
-        * "/" signals end of directory
+        * a zero inode signals end of directory
         */
         */
-       while (dp != NULL && !(dp->d_namlen == 1 && dp->d_name[0] == '/')) {
+       while (dp != NULL && dp->d_ino != 0) {
                locname[namelen] = '\0';
                locname[namelen] = '\0';
-               if (namelen + dp->d_namlen >= BUFSIZ) {
+               if (namelen + dp->d_namlen >= MAXPATHLEN) {
                        fprintf(stderr, "%s%s: name exceeds %d char\n",
                        fprintf(stderr, "%s%s: name exceeds %d char\n",
-                               locname, dp->d_name, BUFSIZ);
+                               locname, dp->d_name, MAXPATHLEN);
                } else {
                } else {
-                       strncat(locname, dp->d_name, dp->d_namlen);
+                       (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
                        treescan(locname, dp->d_ino, todo);
                        treescan(locname, dp->d_ino, todo);
-                       seekdir(dirp, bpt, itp->t_seekpt);
+                       rst_seekdir(dirp, bpt, itp->t_seekpt);
                }
                }
-               dp = readdir(dirp);
-               bpt = telldir(dirp);
+               dp = rst_readdir(dirp);
+               bpt = rst_telldir(dirp);
        }
        if (dp == NULL)
                fprintf(stderr, "corrupted directory: %s.\n", locname);
        }
        if (dp == NULL)
                fprintf(stderr, "corrupted directory: %s.\n", locname);
@@ -208,13 +298,13 @@ search(inum, cp)
        itp = inotablookup(inum);
        if (itp == NULL)
                return(0);
        itp = inotablookup(inum);
        if (itp == NULL)
                return(0);
-       seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
+       rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
        len = strlen(cp);
        do {
        len = strlen(cp);
        do {
-               dp = readdir(dirp);
-               if (dp->d_namlen == 1 && dp->d_name[0] == '/')
-                       return(0);
-       } while (dp->d_namlen != len || strncmp(dp->d_name, cp, len));
+               dp = rst_readdir(dirp);
+               if (dp == NULL || dp->d_ino == 0)
+                       return (0);
+       } while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0);
        return(dp->d_ino);
 }
 
        return(dp->d_ino);
 }
 
@@ -230,6 +320,7 @@ putdir(buf, size)
        struct odirect *eodp;
        register struct direct *dp;
        long loc, i;
        struct odirect *eodp;
        register struct direct *dp;
        long loc, i;
+       extern int Bcvt;
 
        if (cvtflag) {
                eodp = (struct odirect *)&buf[size];
 
        if (cvtflag) {
                eodp = (struct odirect *)&buf[size];
@@ -241,8 +332,15 @@ putdir(buf, size)
        } else {
                for (loc = 0; loc < size; ) {
                        dp = (struct direct *)(buf + loc);
        } else {
                for (loc = 0; loc < size; ) {
                        dp = (struct direct *)(buf + loc);
+                       if (Bcvt) {
+                               swabst("l2s", (char *) dp);
+                       }
                        i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
                        i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
-                       if (dp->d_reclen == 0 || dp->d_reclen > i) {
+                       if ((dp->d_reclen & 0x3) != 0 ||
+                           dp->d_reclen > i ||
+                           dp->d_reclen < DIRSIZ(dp) ||
+                           dp->d_namlen > MAXNAMLEN) {
+                               vprintf(stdout, "Mangled directory\n");
                                loc += i;
                                continue;
                        }
                                loc += i;
                                continue;
                        }
@@ -267,12 +365,11 @@ long prev = 0;
 putent(dp)
        struct direct *dp;
 {
 putent(dp)
        struct direct *dp;
 {
-       if (dp->d_ino == 0)
-               return;
+       dp->d_reclen = DIRSIZ(dp);
        if (dirloc + dp->d_reclen > DIRBLKSIZ) {
                ((struct direct *)(dirbuf + prev))->d_reclen =
                    DIRBLKSIZ - prev;
        if (dirloc + dp->d_reclen > DIRBLKSIZ) {
                ((struct direct *)(dirbuf + prev))->d_reclen =
                    DIRBLKSIZ - prev;
-               fwrite(dirbuf, 1, DIRBLKSIZ, df);
+               (void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
                dirloc = 0;
        }
        bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen);
                dirloc = 0;
        }
        bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen);
@@ -287,7 +384,7 @@ flushent()
 {
 
        ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
 {
 
        ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
-       fwrite(dirbuf, (int)dirloc, 1, df);
+       (void) fwrite(dirbuf, (int)dirloc, 1, df);
        seekpt = ftell(df);
        dirloc = 0;
 }
        seekpt = ftell(df);
        dirloc = 0;
 }
@@ -299,55 +396,29 @@ dcvt(odp, ndp)
 
        bzero((char *)ndp, (long)(sizeof *ndp));
        ndp->d_ino =  odp->d_ino;
 
        bzero((char *)ndp, (long)(sizeof *ndp));
        ndp->d_ino =  odp->d_ino;
-       strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
+       (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
        ndp->d_namlen = strlen(ndp->d_name);
        ndp->d_reclen = DIRSIZ(ndp);
        ndp->d_namlen = strlen(ndp->d_name);
        ndp->d_reclen = DIRSIZ(ndp);
-       /*
-        * this quickly calculates if this inode is a directory.
-        * Currently not maintained.
-        *
-       itp = inotablookup(odp->d_ino);
-       if (itp != NIL)
-               ndp->d_fmt = IFDIR;
-        */
-}
-
-/*
- * Open a directory.
- * Modified to allow any random file to be a legal directory.
- */
-DIR *
-opendir(name)
-       char *name;
-{
-       register DIR *dirp;
-
-       dirp = (DIR *)malloc((unsigned long)sizeof(DIR));
-       dirp->dd_fd = open(name, 0);
-       if (dirp->dd_fd == -1) {
-               free((char *)dirp);
-               return NULL;
-       }
-       dirp->dd_loc = 0;
-       return dirp;
 }
 
 /*
  * Seek to an entry in a directory.
 }
 
 /*
  * Seek to an entry in a directory.
- * Only values returned by ``telldir'' should be passed to seekdir.
- * Modified to have many directories based in one file.
+ * Only values returned by rst_telldir should be passed to rst_seekdir.
+ * This routine handles many directories in a single file.
+ * It takes the base of the directory in the file, plus
+ * the desired seek offset into it.
  */
 void
  */
 void
-seekdir(dirp, loc, base)
+rst_seekdir(dirp, loc, base)
        register DIR *dirp;
        daddr_t loc, base;
 {
 
        register DIR *dirp;
        daddr_t loc, base;
 {
 
-       if (loc == telldir(dirp))
+       if (loc == rst_telldir(dirp))
                return;
        loc -= base;
        if (loc < 0)
                return;
        loc -= base;
        if (loc < 0)
-               fprintf(stderr, "bad seek pointer to seekdir %d\n", loc);
+               fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
        (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
        dirp->dd_loc = loc & (DIRBLKSIZ - 1);
        if (dirp->dd_loc != 0)
        (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
        dirp->dd_loc = loc & (DIRBLKSIZ - 1);
        if (dirp->dd_loc != 0)
@@ -358,7 +429,7 @@ seekdir(dirp, loc, base)
  * get next entry in a directory.
  */
 struct direct *
  * get next entry in a directory.
  */
 struct direct *
-readdir(dirp)
+rst_readdir(dirp)
        register DIR *dirp;
 {
        register struct direct *dp;
        register DIR *dirp;
 {
        register struct direct *dp;
@@ -367,8 +438,10 @@ readdir(dirp)
                if (dirp->dd_loc == 0) {
                        dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 
                            DIRBLKSIZ);
                if (dirp->dd_loc == 0) {
                        dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 
                            DIRBLKSIZ);
-                       if (dirp->dd_size <= 0)
+                       if (dirp->dd_size <= 0) {
+                               dprintf(stderr, "error reading directory\n");
                                return NULL;
                                return NULL;
+                       }
                }
                if (dirp->dd_loc >= dirp->dd_size) {
                        dirp->dd_loc = 0;
                }
                if (dirp->dd_loc >= dirp->dd_size) {
                        dirp->dd_loc = 0;
@@ -376,20 +449,78 @@ readdir(dirp)
                }
                dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
                if (dp->d_reclen == 0 ||
                }
                dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
                if (dp->d_reclen == 0 ||
-                   dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc)
+                   dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
+                       dprintf(stderr, "corrupted directory: bad reclen %d\n",
+                               dp->d_reclen);
                        return NULL;
                        return NULL;
+               }
                dirp->dd_loc += dp->d_reclen;
                dirp->dd_loc += dp->d_reclen;
-               if (dp->d_ino == 0)
+               if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
+                       continue;
+               if (dp->d_ino >= maxino) {
+                       dprintf(stderr, "corrupted directory: bad inum %d\n",
+                               dp->d_ino);
                        continue;
                        continue;
+               }
                return (dp);
        }
 }
 
                return (dp);
        }
 }
 
+/*
+ * Simulate the opening of a directory
+ */
+DIR *
+rst_opendir(name)
+       char *name;
+{
+       struct inotab *itp;
+       ino_t ino;
+
+       if ((ino = dirlookup(name)) > 0 &&
+           (itp = inotablookup(ino)) != NULL) {
+               rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
+               return (dirp);
+       }
+       return (0);
+}
+
+/*
+ * Simulate finding the current offset in the directory.
+ */
+off_t
+rst_telldir(dirp)
+       DIR *dirp;
+{
+       off_t lseek();
+
+       return (lseek(dirp->dd_fd, 0L, 1) - dirp->dd_size + dirp->dd_loc);
+}
+
+/*
+ * Open a directory file.
+ */
+DIR *
+opendirfile(name)
+       char *name;
+{
+       register DIR *dirp;
+       register int fd;
+
+       if ((fd = open(name, 0)) == -1)
+               return NULL;
+       if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
+               close (fd);
+               return NULL;
+       }
+       dirp->dd_fd = fd;
+       dirp->dd_loc = 0;
+       return dirp;
+}
+
 /*
  * Set the mode, owner, and times for all new or changed directories
  */
 /*
  * Set the mode, owner, and times for all new or changed directories
  */
-setdirmodes(modefile)
-       char *modefile;
+setdirmodes()
 {
        FILE *mf;
        struct modeinfo node;
 {
        FILE *mf;
        struct modeinfo node;
@@ -397,26 +528,44 @@ setdirmodes(modefile)
        char *cp;
        
        vprintf(stdout, "Set directory mode, owner, and times.\n");
        char *cp;
        
        vprintf(stdout, "Set directory mode, owner, and times.\n");
+       (void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
        mf = fopen(modefile, "r");
        if (mf == NULL) {
                perror("fopen");
        mf = fopen(modefile, "r");
        if (mf == NULL) {
                perror("fopen");
-               panic("cannot open mode file %s\n", modefile);
+               fprintf(stderr, "cannot open mode file %s\n", modefile);
+               fprintf(stderr, "directory mode, owner, and times not set\n");
+               return;
        }
        clearerr(mf);
        }
        clearerr(mf);
-       fread((char *)&node, 1, sizeof(struct modeinfo), mf);
-       while (!feof(mf)) {
+       for (;;) {
+               (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
+               if (feof(mf))
+                       break;
                ep = lookupino(node.ino);
                ep = lookupino(node.ino);
-               if (ep == NIL)
+               if (command == 'i' || command == 'x') {
+                       if (ep == NIL)
+                               continue;
+                       if (ep->e_flags & EXISTED) {
+                               ep->e_flags &= ~NEW;
+                               continue;
+                       }
+                       if (node.ino == ROOTINO &&
+                           reply("set owner/mode for '.'") == FAIL)
+                               continue;
+               }
+               if (ep == NIL) {
                        panic("cannot find directory inode %d\n", node.ino);
                        panic("cannot find directory inode %d\n", node.ino);
-               cp = myname(ep);
-               chown(cp, node.uid, node.gid);
-               chmod(cp, node.mode);
-               utime(cp, node.timep);
-               fread((char *)&node, 1, sizeof(struct modeinfo), mf);
+               } else {
+                       cp = myname(ep);
+                       (void) chown(cp, node.uid, node.gid);
+                       (void) chmod(cp, node.mode);
+                       utimes(cp, node.timep);
+                       ep->e_flags &= ~NEW;
+               }
        }
        if (ferror(mf))
                panic("error setting directory modes\n");
        }
        if (ferror(mf))
                panic("error setting directory modes\n");
-       fclose(mf);
+       (void) fclose(mf);
 }
 
 /*
 }
 
 /*
@@ -432,12 +581,14 @@ genliteraldir(name, ino)
 
        itp = inotablookup(ino);
        if (itp == NULL)
 
        itp = inotablookup(ino);
        if (itp == NULL)
-               panic("cannot find directory inode %d named %s\n", ino, name);
-       if ((ofile = open(name, FWRONLY|FCREATE, 0666)) < 0) {
-               fprintf(stderr, "%s: cannot create file\n", name);
+               panic("Cannot find directory inode %d named %s\n", ino, name);
+       if ((ofile = creat(name, 0666)) < 0) {
+               fprintf(stderr, "%s: ", name);
+               (void) fflush(stderr);
+               perror("cannot create file");
                return (FAIL);
        }
                return (FAIL);
        }
-       seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
+       rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
        dp = dup(dirp->dd_fd);
        for (i = itp->t_size; i > 0; i -= BUFSIZ) {
                size = i < BUFSIZ ? i : BUFSIZ;
        dp = dup(dirp->dd_fd);
        for (i = itp->t_size; i > 0; i -= BUFSIZ) {
                size = i < BUFSIZ ? i : BUFSIZ;
@@ -448,7 +599,7 @@ genliteraldir(name, ino)
                        perror("read");
                        done(1);
                }
                        perror("read");
                        done(1);
                }
-               if (write(ofile, buf, (int) size) == -1) {
+               if (!Nflag && write(ofile, buf, (int) size) == -1) {
                        fprintf(stderr,
                                "write error extracting inode %d, name %s\n",
                                curfile.ino, curfile.name);
                        fprintf(stderr,
                                "write error extracting inode %d, name %s\n",
                                curfile.ino, curfile.name);
@@ -456,15 +607,30 @@ genliteraldir(name, ino)
                        done(1);
                }
        }
                        done(1);
                }
        }
-       close(dp);
-       close(ofile);
+       (void) close(dp);
+       (void) close(ofile);
        return (GOOD);
 }
 
        return (GOOD);
 }
 
+/*
+ * Determine the type of an inode
+ */
+inodetype(ino)
+       ino_t ino;
+{
+       struct inotab *itp;
+
+       itp = inotablookup(ino);
+       if (itp == NULL)
+               return (LEAF);
+       return (NODE);
+}
+
 /*
  * Allocate and initialize a directory inode entry.
  * If requested, save its pertinent mode, owner, and time info.
  */
 /*
  * Allocate and initialize a directory inode entry.
  * If requested, save its pertinent mode, owner, and time info.
  */
+struct inotab *
 allocinotab(ino, dip, seekpt)
        ino_t ino;
        struct dinode *dip;
 allocinotab(ino, dip, seekpt)
        ino_t ino;
        struct dinode *dip;
@@ -472,24 +638,26 @@ allocinotab(ino, dip, seekpt)
 {
        register struct inotab  *itp;
        struct modeinfo node;
 {
        register struct inotab  *itp;
        struct modeinfo node;
-       static int prevseekpt = 0;
 
        itp = (struct inotab *)calloc(1, sizeof(struct inotab));
 
        itp = (struct inotab *)calloc(1, sizeof(struct inotab));
+       if (itp == 0)
+               panic("no memory directory table\n");
        itp->t_next = inotab[INOHASH(ino)];
        inotab[INOHASH(ino)] = itp;
        itp->t_ino = ino;
        itp->t_seekpt = seekpt;
        itp->t_next = inotab[INOHASH(ino)];
        inotab[INOHASH(ino)] = itp;
        itp->t_ino = ino;
        itp->t_seekpt = seekpt;
-       itp->t_size = seekpt - prevseekpt;
-       prevseekpt = seekpt;
        if (mf == NULL)
        if (mf == NULL)
-               return;
+               return(itp);
        node.ino = ino;
        node.ino = ino;
-       node.timep[0] = dip->di_atime;
-       node.timep[1] = dip->di_mtime;
+       node.timep[0].tv_sec = dip->di_atime;
+       node.timep[0].tv_usec = 0;
+       node.timep[1].tv_sec = dip->di_mtime;
+       node.timep[1].tv_usec = 0;
        node.mode = dip->di_mode;
        node.uid = dip->di_uid;
        node.gid = dip->di_gid;
        node.mode = dip->di_mode;
        node.uid = dip->di_uid;
        node.gid = dip->di_gid;
-       fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
+       (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
+       return(itp);
 }
 
 /*
 }
 
 /*
@@ -515,6 +683,9 @@ done(exitcode)
 {
 
        closemt();
 {
 
        closemt();
-       unlink(dirfile);
+       if (modefile[0] != '#')
+               (void) unlink(modefile);
+       if (dirfile[0] != '#')
+               (void) unlink(dirfile);
        exit(exitcode);
 }
        exit(exitcode);
 }