manual page distributed with 4.1BSD
[unix-history] / usr / src / sbin / restore / dirs.c
index 30e9280..97db9c8 100644 (file)
@@ -1,13 +1,12 @@
-/* Copyright (c) 1983 Regents of the University of California */
-
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)dirs.c     3.9     (Berkeley)      83/05/03";
+static char sccsid[] = "@(#)dirs.c     3.20    (Berkeley)      85/02/18";
 #endif
 
 #endif
 
+/* Copyright (c) 1983 Regents of the University of California */
+
 #include "restore.h"
 #include <dumprestor.h>
 #include <sys/file.h>
 #include "restore.h"
 #include <dumprestor.h>
 #include <sys/file.h>
-#include <dir.h>
 
 /*
  * Symbol table of directories read from tape.
 
 /*
  * Symbol table of directories read from tape.
@@ -44,6 +43,8 @@ static DIR    *dirp;
 static char    dirfile[32] = "#";      /* No file */
 static char    modefile[32] = "#";     /* No file */
 extern ino_t   search();
 static char    dirfile[32] = "#";      /* No file */
 static char    modefile[32] = "#";     /* No file */
 extern ino_t   search();
+struct direct  *rst_readdir();
+extern void    rst_seekdir();
 
 /*
  * Format of old style directories.
 
 /*
  * Format of old style directories.
@@ -54,18 +55,6 @@ struct odirect {
        char    d_name[ODIRSIZ];
 };
 
        char    d_name[ODIRSIZ];
 };
 
-/*
- * Structure and routines associated with listing directories.
- */
-struct afile {
-       ino_t   fnum;           /* inode number of file */
-       char    *fname;         /* file name */
-       short   fflags;         /* extraction flags, if any */
-       char    ftype;          /* file type, e.g. LEAF or NODE */
-};
-extern int fcmp();
-extern char *fmtentry();
-
 /*
  *     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.
@@ -86,7 +75,7 @@ extractdirs(genmode)
        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);
@@ -96,7 +85,7 @@ extractdirs(genmode)
                mf = fopen(modefile, "w");
                if (mf == 0) {
                        fprintf(stderr,
                mf = fopen(modefile, "w");
                if (mf == 0) {
                        fprintf(stderr,
-                           "restor: %s - cannot create modefile \n",
+                           "restore: %s - cannot create modefile \n",
                            modefile);
                        perror("fopen");
                        done(1);
                            modefile);
                        perror("fopen");
                        done(1);
@@ -104,14 +93,13 @@ extractdirs(genmode)
        }
        nulldir.d_ino = 0;
        nulldir.d_namlen = 1;
        }
        nulldir.d_ino = 0;
        nulldir.d_namlen = 1;
-       (void) strncpy(nulldir.d_name, "/", (int)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) {
+               if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
                        (void) fclose(df);
                        dirp = opendir(dirfile);
                        if (dirp == NULL)
                        (void) fclose(df);
                        dirp = opendir(dirfile);
                        if (dirp == NULL)
@@ -152,9 +140,10 @@ treescan(pname, ino, todo)
        long (*todo)();
 {
        register struct inotab *itp;
        long (*todo)();
 {
        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[MAXPATHLEN + 1];
 
        itp = inotablookup(ino);
        char locname[MAXPATHLEN + 1];
 
        itp = inotablookup(ino);
@@ -177,15 +166,29 @@ treescan(pname, ino, todo)
        (void) strncpy(locname, pname, MAXPATHLEN);
        (void) strncat(locname, "/", MAXPATHLEN);
        namelen = strlen(locname);
        (void) strncpy(locname, pname, MAXPATHLEN);
        (void) strncat(locname, "/", MAXPATHLEN);
        namelen = strlen(locname);
-       seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
-       dp = readdir(dirp); /* "." */
-       dp = readdir(dirp); /* ".." */
-       dp = readdir(dirp); /* first real entry */
+       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 {
+               np = lookupino(ino);
+               if (np == NULL)
+                       panic("corrupted symbol table\n");
+               fprintf(stderr, ". missing from directory %s\n", myname(np));
+       }
+       if (dp != NULL && strcmp(dp->d_name, "..") == 0) {
+               dp = rst_readdir(dirp); /* first real entry */
+       } else {
+               np = lookupino(ino);
+               if (np == NULL)
+                       panic("corrupted symbol table\n");
+               fprintf(stderr, ".. missing from directory %s\n", myname(np));
+       }
        bpt = telldir(dirp);
        /*
        bpt = 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';
                if (namelen + dp->d_namlen >= MAXPATHLEN) {
                        fprintf(stderr, "%s%s: name exceeds %d char\n",
                locname[namelen] = '\0';
                if (namelen + dp->d_namlen >= MAXPATHLEN) {
                        fprintf(stderr, "%s%s: name exceeds %d char\n",
@@ -193,9 +196,9 @@ treescan(pname, ino, todo)
                } else {
                        (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
                        treescan(locname, dp->d_ino, todo);
                } else {
                        (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
                        treescan(locname, dp->d_ino, todo);
-                       seekdir(dirp, bpt, itp->t_seekpt);
+                       rst_seekdir(dirp, bpt, itp->t_seekpt);
                }
                }
-               dp = readdir(dirp);
+               dp = rst_readdir(dirp);
                bpt = telldir(dirp);
        }
        if (dp == NULL)
                bpt = telldir(dirp);
        }
        if (dp == NULL)
@@ -252,12 +255,12 @@ 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);
+               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);
 }
        } while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0);
        return(dp->d_ino);
 }
@@ -349,13 +352,13 @@ dcvt(odp, ndp)
 
 /*
  * Seek to an entry in a directory.
 
 /*
  * Seek to an entry in a directory.
- * Only values returned by ``telldir'' should be passed to seekdir.
+ * Only values returned by ``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
  * 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
-seekdir(dirp, loc, base)
+rst_seekdir(dirp, loc, base)
        register DIR *dirp;
        daddr_t loc, base;
 {
        register DIR *dirp;
        daddr_t loc, base;
 {
@@ -364,7 +367,7 @@ seekdir(dirp, loc, base)
                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)
@@ -375,7 +378,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;
@@ -384,8 +387,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;
@@ -393,13 +398,41 @@ 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 && strcmp(dp->d_name, "/") != 0)
+                       continue;
+               if (dp->d_ino >= maxino) {
+                       dprintf(stderr, "corrupted directory: bad inum %d\n",
+                               dp->d_ino);
+                       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);
+}
+
 /*
  * Set the mode, owner, and times for all new or changed directories
  */
 /*
  * Set the mode, owner, and times for all new or changed directories
  */
@@ -411,10 +444,13 @@ setdirmodes()
        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, "/tmp/rstmode%d", 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);
        for (;;) {
        }
        clearerr(mf);
        for (;;) {
@@ -422,11 +458,19 @@ setdirmodes()
                if (feof(mf))
                        break;
                ep = lookupino(node.ino);
                if (feof(mf))
                        break;
                ep = lookupino(node.ino);
-               if (ep == NIL) {
-                       if (command != 'r' && command != 'R')
+               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;
                                continue;
-                       panic("cannot find directory inode %d\n", node.ino);
                }
                }
+               if (ep == NIL)
+                       panic("cannot find directory inode %d\n", node.ino);
                cp = myname(ep);
                (void) chown(cp, node.uid, node.gid);
                (void) chmod(cp, node.mode);
                cp = myname(ep);
                (void) chown(cp, node.uid, node.gid);
                (void) chmod(cp, node.mode);
@@ -452,11 +496,13 @@ genliteraldir(name, ino)
        itp = inotablookup(ino);
        if (itp == NULL)
                panic("Cannot find directory inode %d named %s\n", ino, name);
        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);
+       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;
@@ -480,169 +526,6 @@ genliteraldir(name, ino)
        return (GOOD);
 }
 
        return (GOOD);
 }
 
-/*
- * Do an "ls" style listing of a directory
- */
-printlist(name, ino)
-       char *name;
-       ino_t ino;
-{
-       register struct afile *fp;
-       register struct inotab *itp;
-       struct afile *dfp0, *dfplast;
-       struct afile single;
-
-       itp = inotablookup(ino);
-       if (itp == NULL) {
-               single.fnum = ino;
-               single.fname = savename(rindex(name, '/') + 1);
-               dfp0 = &single;
-               dfplast = dfp0 + 1;
-       } else {
-               seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
-               if (getdir(dirp, &dfp0, &dfplast) == FAIL)
-                       return;
-       }
-       qsort((char *)dfp0, dfplast - dfp0, sizeof (struct afile), fcmp);
-       formatf(dfp0, dfplast);
-       for (fp = dfp0; fp < dfplast; fp++)
-               freename(fp->fname);
-}
-
-/*
- * Read the contents of a directory.
- */
-getdir(dirp, pfp0, pfplast)
-       DIR *dirp;
-       struct afile **pfp0, **pfplast;
-{
-       register struct afile *fp;
-       register struct direct *dp;
-       static struct afile *basefp = NULL;
-       static long nent = 20;
-
-       if (basefp == NULL)
-               basefp = (struct afile *)calloc((unsigned)nent,
-                       sizeof (struct afile));
-       fp = *pfp0 = basefp;
-       *pfplast = *pfp0 + nent;
-       while (dp = readdir(dirp)) {
-               if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
-                       break;
-               if (BIT(dp->d_ino, dumpmap) == 0)
-                       continue;
-               if (vflag == 0 &&
-                   (strcmp(dp->d_name, ".") == 0 ||
-                    strcmp(dp->d_name, "..") == 0))
-                       continue;
-               fp->fnum = dp->d_ino;
-               fp->fname = savename(dp->d_name);
-               fp++;
-               if (fp == *pfplast) {
-                       basefp = (struct afile *)realloc((char *)basefp,
-                           (unsigned)(2 * nent * sizeof (struct afile)));
-                       if (basefp == 0) {
-                               fprintf(stderr, "ls: out of memory\n");
-                               return (FAIL);
-                       }
-                       *pfp0 = basefp;
-                       fp = *pfp0 + nent;
-                       *pfplast = fp + nent;
-                       nent *= 2;
-               }
-       }
-       *pfplast = fp;
-       return (GOOD);
-}
-
-/*
- * Print out a pretty listing of a directory
- */
-formatf(fp0, fplast)
-       struct afile *fp0, *fplast;
-{
-       register struct afile *fp;
-       struct entry *np;
-       int width = 0, w, nentry = fplast - fp0;
-       int i, j, len, columns, lines;
-       char *cp;
-
-       if (fp0 == fplast)
-               return;
-       for (fp = fp0; fp < fplast; fp++) {
-               fp->ftype = inodetype(fp->fnum);
-               np = lookupino(fp->fnum);
-               if (np != NIL)
-                       fp->fflags = np->e_flags;
-               else
-                       fp->fflags = 0;
-               len = strlen(fmtentry(fp));
-               if (len > width)
-                       width = len;
-       }
-       width += 2;
-       columns = 80 / width;
-       if (columns == 0)
-               columns = 1;
-       lines = (nentry + columns - 1) / columns;
-       for (i = 0; i < lines; i++) {
-               for (j = 0; j < columns; j++) {
-                       fp = fp0 + j * lines + i;
-                       cp = fmtentry(fp);
-                       fprintf(stderr, "%s", cp);
-                       if (fp + lines >= fplast) {
-                               fprintf(stderr, "\n");
-                               break;
-                       }
-                       w = strlen(cp);
-                       while (w < width) {
-                               w++;
-                               fprintf(stderr, " ");
-                       }
-               }
-       }
-}
-
-/*
- * Comparison routine for qsort.
- */
-fcmp(f1, f2)
-       register struct afile *f1, *f2;
-{
-
-       return (strcmp(f1->fname, f2->fname));
-}
-
-/*
- * Format a directory entry.
- */
-char *
-fmtentry(fp)
-       register struct afile *fp;
-{
-       static char fmtres[BUFSIZ];
-       register char *cp, *dp;
-
-       if (vflag)
-               (void) sprintf(fmtres, "%5d ", fp->fnum);
-       else
-               fmtres[0] = '\0';
-       dp = &fmtres[strlen(fmtres)];
-       if ((fp->fflags & NEW) != 0)
-               *dp++ = '*';
-       else
-               *dp++ = ' ';
-       for (cp = fp->fname; *cp; cp++)
-               if (!vflag && (*cp < ' ' || *cp >= 0177))
-                       *dp++ = '?';
-               else
-                       *dp++ = *cp;
-       if (fp->ftype == NODE)
-               *dp++ = '/';
-       *dp++ = 0;
-       return (fmtres);
-}
-
 /*
  * Determine the type of an inode
  */
 /*
  * Determine the type of an inode
  */
@@ -671,6 +554,8 @@ allocinotab(ino, dip, seekpt)
        struct modeinfo node;
 
        itp = (struct inotab *)calloc(1, sizeof(struct inotab));
        struct modeinfo node;
 
        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_next = inotab[INOHASH(ino)];
        inotab[INOHASH(ino)] = itp;
        itp->t_ino = ino;