386BSD 0.1 development
[unix-history] / usr / src / sbin / restore / dirs.c
index 5e3ea03..b2c00f9 100644 (file)
@@ -1,16 +1,45 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * 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     5.1 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)dirs.c     5.15 (Berkeley) 2/26/91";
+#endif /* not lint */
 
 #include "restore.h"
 
 #include "restore.h"
-#include <dumprestor.h>
+#include <protocols/dumprestore.h>
 #include <sys/file.h>
 #include <sys/file.h>
+#include <ufs/dir.h>
+#include "pathnames.h"
 
 /*
  * Symbol table of directories read from tape.
 
 /*
  * Symbol table of directories read from tape.
@@ -32,12 +61,27 @@ extern struct inotab *allocinotab();
  */
 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;
 };
 
+/*
+ * 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.
  */
 /*
  * Global variables for this file.
  */
@@ -46,6 +90,7 @@ static FILE   *df, *mf;
 static DIR     *dirp;
 static char    dirfile[32] = "#";      /* No file */
 static char    modefile[32] = "#";     /* No file */
 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();
 extern ino_t   search();
 struct direct  *rst_readdir();
 extern void    rst_seekdir();
@@ -75,7 +120,7 @@ extractdirs(genmode)
        int putdir(), null();
 
        vprintf(stdout, "Extract directories from tape\n");
        int putdir(), null();
 
        vprintf(stdout, "Extract directories from tape\n");
-       (void) sprintf(dirfile, "/tmp/rstdir%d", dumpdate);
+       (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,
@@ -85,7 +130,7 @@ extractdirs(genmode)
                done(1);
        }
        if (genmode != 0) {
                done(1);
        }
        if (genmode != 0) {
-               (void) sprintf(modefile, "/tmp/rstmode%d", dumpdate);
+               (void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
                mf = fopen(modefile, "w");
                if (mf == 0) {
                        fprintf(stderr,
                mf = fopen(modefile, "w");
                if (mf == 0) {
                        fprintf(stderr,
@@ -105,12 +150,12 @@ extractdirs(genmode)
                ip = curfile.dip;
                if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
                        (void) fclose(df);
                ip = curfile.dip;
                if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
                        (void) fclose(df);
-                       dirp = opendir(dirfile);
+                       dirp = opendirfile(dirfile);
                        if (dirp == NULL)
                        if (dirp == NULL)
-                               perror("opendir");
+                               perror("opendirfile");
                        if (mf != NULL)
                                (void) fclose(mf);
                        if (mf != NULL)
                                (void) fclose(mf);
-                       i = dirlookup(".");
+                       i = dirlookup(dot);
                        if (i == 0)
                                panic("Root directory is not on tape\n");
                        return;
                        if (i == 0)
                                panic("Root directory is not on tape\n");
                        return;
@@ -172,23 +217,17 @@ treescan(pname, ino, todo)
        namelen = strlen(locname);
        rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
        dp = rst_readdir(dirp); /* "." */
        namelen = strlen(locname);
        rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
        dp = rst_readdir(dirp); /* "." */
-       if (dp != NULL && strcmp(dp->d_name, ".") == 0) {
+       if (dp != NULL && strcmp(dp->d_name, ".") == 0)
                dp = rst_readdir(dirp); /* ".." */
                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) {
+       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 */
                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);
+       else
+               fprintf(stderr, "Warning: `..' missing from directory %s\n",
+                       pname);
+       bpt = rst_telldir(dirp);
        /*
         * a zero inode signals end of directory
         */
        /*
         * a zero inode signals end of directory
         */
@@ -203,7 +242,7 @@ treescan(pname, ino, todo)
                        rst_seekdir(dirp, bpt, itp->t_seekpt);
                }
                dp = rst_readdir(dirp);
                        rst_seekdir(dirp, bpt, itp->t_seekpt);
                }
                dp = rst_readdir(dirp);
-               bpt = telldir(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);
@@ -281,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];
@@ -292,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;
                        }
@@ -356,7 +403,7 @@ 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 rst_seekdir.
+ * 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.
  * 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.
@@ -367,7 +414,7 @@ rst_seekdir(dirp, loc, base)
        daddr_t loc, base;
 {
 
        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)
@@ -437,6 +484,39 @@ rst_opendir(name)
        return (0);
 }
 
        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
  */
@@ -448,7 +528,7 @@ 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);
+       (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");
@@ -473,13 +553,15 @@ setdirmodes()
                            reply("set owner/mode for '.'") == FAIL)
                                continue;
                }
                            reply("set owner/mode for '.'") == FAIL)
                                continue;
                }
-               if (ep == NIL)
+               if (ep == NIL) {
                        panic("cannot find directory inode %d\n", node.ino);
                        panic("cannot find directory inode %d\n", node.ino);
-               cp = myname(ep);
-               (void) chown(cp, node.uid, node.gid);
-               (void) chmod(cp, node.mode);
-               utime(cp, node.timep);
-               ep->e_flags &= ~NEW;
+               } 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");
@@ -517,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);
@@ -567,8 +649,10 @@ allocinotab(ino, dip, seekpt)
        if (mf == NULL)
                return(itp);
        node.ino = ino;
        if (mf == NULL)
                return(itp);
        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;