386BSD 0.1 development
[unix-history] / usr / src / sbin / restore / dirs.c
index 54c2de6..b2c00f9 100644 (file)
@@ -2,26 +2,43 @@
  * Copyright (c) 1983 The Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983 The Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * 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.9 (Berkeley) %G%";
+static char sccsid[] = "@(#)dirs.c     5.15 (Berkeley) 2/26/91";
 #endif /* not lint */
 
 #include "restore.h"
 #include <protocols/dumprestore.h>
 #include <sys/file.h>
 #endif /* not lint */
 
 #include "restore.h"
 #include <protocols/dumprestore.h>
 #include <sys/file.h>
+#include <ufs/dir.h>
 #include "pathnames.h"
 
 /*
 #include "pathnames.h"
 
 /*
@@ -50,6 +67,21 @@ struct modeinfo {
        short gid;
 };
 
        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.
  */
@@ -58,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();
@@ -122,7 +155,7 @@ extractdirs(genmode)
                                perror("opendirfile");
                        if (mf != NULL)
                                (void) fclose(mf);
                                perror("opendirfile");
                        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;
@@ -160,7 +193,6 @@ treescan(pname, ino, todo)
        register struct entry *np;
        int namelen;
        daddr_t bpt;
        register struct entry *np;
        int namelen;
        daddr_t bpt;
-       off_t rst_telldir();
        char locname[MAXPATHLEN + 1];
 
        itp = inotablookup(ino);
        char locname[MAXPATHLEN + 1];
 
        itp = inotablookup(ino);
@@ -304,7 +336,11 @@ putdir(buf, size)
                                swabst("l2s", (char *) dp);
                        }
                        i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
                                swabst("l2s", (char *) dp);
                        }
                        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;
                        }
@@ -377,7 +413,6 @@ rst_seekdir(dirp, loc, base)
        register DIR *dirp;
        daddr_t loc, base;
 {
        register DIR *dirp;
        daddr_t loc, base;
 {
-       off_t rst_telldir();
 
        if (loc == rst_telldir(dirp))
                return;
 
        if (loc == rst_telldir(dirp))
                return;
@@ -518,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);
-               utimes(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");