]> git.wh0rd.org - dump.git/blobdiff - restore/utilities.c
Added CVS Id.
[dump.git] / restore / utilities.c
index 54b7dc1fbc547538ad82714e2cb529af7c78a0ba..44cea0454c343c9d513b72da27444320384eb3ca 100644 (file)
@@ -1,7 +1,8 @@
 /*
  *     Ported to Linux's Second Extended File System as part of the
  *     dump and restore backup suit
- *     Remy Card <card@Linux.EU.Org>, 1994, 1995, 1996
+ *     Remy Card <card@Linux.EU.Org>, 1994-1997
+ *      Stelian Pop <pop@cybercable.fr>, 1999 
  *
  */
 
  * 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.
+ *
+ * $Id: utilities.c,v 1.5 1999/10/11 13:31:14 stelian Exp $
  */
 
-#ifndef lint
-static char sccsid[] = "@(#)utilities.c        8.5 (Berkeley) 4/28/95";
-#endif /* not lint */
-
 #include <sys/param.h>
 #include <sys/stat.h>
 
@@ -55,8 +54,8 @@ static char sccsid[] = "@(#)utilities.c       8.5 (Berkeley) 4/28/95";
 #endif /* __linux__ */
 
 #include <errno.h>
+#include <compaterr.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -71,14 +70,13 @@ static char sccsid[] = "@(#)utilities.c     8.5 (Berkeley) 4/28/95";
  * Insure that all the components of a pathname exist.
  */
 void
-pathcheck(name)
-       char *name;
+pathcheck(char *name)
 {
        register char *cp;
        struct entry *ep;
        char *start;
 
-       start = strrchr(name, '/');
+       start = strchr(name, '/');
        if (start == 0)
                return;
        for (cp = start; *cp != '\0'; cp++) {
@@ -100,8 +98,7 @@ pathcheck(name)
  * Change a name to a unique temporary name.
  */
 void
-mktempname(ep)
-       register struct entry *ep;
+mktempname(struct entry *ep)
 {
        char oldname[MAXPATHLEN];
 
@@ -119,8 +116,7 @@ mktempname(ep)
  * Generate a temporary name for an entry.
  */
 char *
-gentempname(ep)
-       struct entry *ep;
+gentempname(struct entry *ep)
 {
        static char name[MAXPATHLEN];
        struct entry *np;
@@ -131,7 +127,7 @@ gentempname(ep)
                i++;
        if (np == NULL)
                badentry(ep, "not on ino list");
-       (void) sprintf(name, "%s%d%d", TMPHDR, i, ep->e_ino);
+       (void) snprintf(name, sizeof(name), "%s%ld%lu", TMPHDR, i, (unsigned long)ep->e_ino);
        return (name);
 }
 
@@ -139,44 +135,40 @@ gentempname(ep)
  * Rename a file or directory.
  */
 void
-renameit(from, to)
-       char *from, *to;
+renameit(char *from, char *to)
 {
        if (!Nflag && rename(from, to) < 0) {
-               fprintf(stderr, "warning: cannot rename %s to %s: %s\n",
-                   from, to, strerror(errno));
+               warn("cannot rename %s to %s", from, to);
                return;
        }
-       vprintf(stdout, "rename %s to %s\n", from, to);
+       Vprintf(stdout, "rename %s to %s\n", from, to);
 }
 
 /*
  * Create a new node (directory).
  */
 void
-newnode(np)
-       struct entry *np;
+newnode(struct entry *np)
 {
        char *cp;
-
        if (np->e_type != NODE)
                badentry(np, "newnode: not a node");
        cp = myname(np);
        if (command == 'C') return;
-       if (!Nflag && mkdir(cp, 0777) < 0) {
+
+       if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
                np->e_flags |= EXISTED;
-               fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
+               warn("%s", cp);
                return;
        }
-       vprintf(stdout, "Make node %s\n", cp);
+       Vprintf(stdout, "Make node %s\n", cp);
 }
 
 /*
  * Remove an old node (directory).
  */
 void
-removenode(ep)
-       register struct entry *ep;
+removenode(struct entry *ep)
 {
        char *cp;
 
@@ -188,18 +180,17 @@ removenode(ep)
        ep->e_flags &= ~TMPNAME;
        cp = myname(ep);
        if (!Nflag && rmdir(cp) < 0) {
-               fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
+               warn("%s", cp);
                return;
        }
-       vprintf(stdout, "Remove node %s\n", cp);
+       Vprintf(stdout, "Remove node %s\n", cp);
 }
 
 /*
  * Remove a leaf.
  */
 void
-removeleaf(ep)
-       register struct entry *ep;
+removeleaf(struct entry *ep)
 {
        char *cp;
 
@@ -211,59 +202,75 @@ removeleaf(ep)
        ep->e_flags &= ~TMPNAME;
        cp = myname(ep);
        if (!Nflag && unlink(cp) < 0) {
-               fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
+               warn("%s", cp);
                return;
        }
-       vprintf(stdout, "Remove leaf %s\n", cp);
+       Vprintf(stdout, "Remove leaf %s\n", cp);
 }
 
 /*
  * Create a link.
  */
 int
-linkit(existing, new, type)
-       char *existing, *new;
-       int type;
+linkit(char *existing, char *new, int type)
 {
 
+       /* if we want to unlink first, do it now so *link() won't fail */
+       if (uflag && !Nflag)
+               (void)unlink(new);
+
        if (type == SYMLINK) {
                if (!Nflag && symlink(existing, new) < 0) {
-                       fprintf(stderr,
-                           "warning: cannot create symbolic link %s->%s: %s\n",
-                           new, existing, strerror(errno));
+                       warn("cannot create symbolic link %s->%s",
+                           new, existing);
                        return (FAIL);
                }
        } else if (type == HARDLINK) {
-               if (!Nflag && link(existing, new) < 0) {
-                       fprintf(stderr,
-                           "warning: cannot create hard link %s->%s: %s\n",
-                           new, existing, strerror(errno));
-                       return (FAIL);
+               int ret;
+
+               if (!Nflag && (ret = link(existing, new)) < 0) {
+
+#ifndef __linux__
+                       struct stat s;
+
+                       /*
+                        * Most likely, the schg flag is set.  Clear the
+                        * flags and try again.
+                        */
+                       if (stat(existing, &s) == 0 && s.st_flags != 0 &&
+                           chflags(existing, 0) == 0) {
+                               ret = link(existing, new);
+                               chflags(existing, s.st_flags);
+                       }
+#endif
+                       if (ret < 0) {
+                               warn("warning: cannot create hard link %s->%s",
+                                   new, existing);
+                               return (FAIL);
+                       }
                }
        } else {
                panic("linkit: unknown type %d\n", type);
                return (FAIL);
        }
-       vprintf(stdout, "Create %s link %s->%s\n",
+       Vprintf(stdout, "Create %s link %s->%s\n",
                type == SYMLINK ? "symbolic" : "hard", new, existing);
        return (GOOD);
 }
 
 #ifndef        __linux__
 /*
- * Create a whiteout
+ * Create a whiteout.
  */
 int
-addwhiteout(name)
-       char *name;
+addwhiteout(char *name)
 {
 
        if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
-               fprintf(stderr, "warning: cannot create whiteout %s: %s\n",
-                   name, strerror(errno));
+               warn("cannot create whiteout %s", name);
                return (FAIL);
        }
-       vprintf(stdout, "Create whiteout %s\n", name);
+       Vprintf(stdout, "Create whiteout %s\n", name);
        return (GOOD);
 }
 
@@ -271,8 +278,7 @@ addwhiteout(name)
  * Delete a whiteout.
  */
 void
-delwhiteout(ep)
-       register struct entry *ep;
+delwhiteout(struct entry *ep)
 {
        char *name;
 
@@ -282,11 +288,10 @@ delwhiteout(ep)
        ep->e_flags &= ~TMPNAME;
        name = myname(ep);
        if (!Nflag && undelete(name) < 0) {
-               fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
-                   name, strerror(errno));
+               warn("cannot delete whiteout %s", name);
                return;
        }
-       vprintf(stdout, "Delete whiteout %s\n", name);
+       Vprintf(stdout, "Delete whiteout %s\n", name);
 }
 #endif
 
@@ -294,8 +299,7 @@ delwhiteout(ep)
  * find lowest number file (above "start") that needs to be extracted
  */
 ino_t
-lowerbnd(start)
-       ino_t start;
+lowerbnd(ino_t start)
 {
        register struct entry *ep;
 
@@ -313,8 +317,7 @@ lowerbnd(start)
  * find highest number file (below "start") that needs to be extracted
  */
 ino_t
-upperbnd(start)
-       ino_t start;
+upperbnd(ino_t start)
 {
        register struct entry *ep;
 
@@ -332,9 +335,7 @@ upperbnd(start)
  * report on a badly formed entry
  */
 void
-badentry(ep, msg)
-       register struct entry *ep;
-       char *msg;
+badentry(struct entry *ep, const char *msg)
 {
 
        fprintf(stderr, "bad entry: %s\n", msg);
@@ -351,7 +352,7 @@ badentry(ep, msg)
                    "next hashchain name: %s\n", myname(ep->e_next));
        fprintf(stderr, "entry type: %s\n",
                ep->e_type == NODE ? "NODE" : "LEAF");
-       fprintf(stderr, "inode number: %ld\n", ep->e_ino);
+       fprintf(stderr, "inode number: %lu\n", (unsigned long)ep->e_ino);
        panic("flags: %s\n", flagvalues(ep));
 }
 
@@ -359,8 +360,7 @@ badentry(ep, msg)
  * Construct a string indicating the active flag bits of an entry.
  */
 char *
-flagvalues(ep)
-       register struct entry *ep;
+flagvalues(struct entry *ep)
 {
        static char flagbuf[BUFSIZ];
 
@@ -385,12 +385,11 @@ flagvalues(ep)
  * Check to see if a name is on a dump tape.
  */
 ino_t
-dirlookup(name)
-       const char *name;
+dirlookup(const char *name)
 {
        struct direct *dp;
        ino_t ino;
+
        ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
 
        if (ino == 0 || TSTINO(ino, dumpmap) == 0)
@@ -402,8 +401,7 @@ dirlookup(name)
  * Elicit a reply.
  */
 int
-reply(question)
-       char *question;
+reply(const char *question)
 {
        char c;
 
@@ -423,14 +421,14 @@ reply(question)
 /*
  * handle unexpected inconsistencies
  */
-#if __STDC__
+#ifdef __STDC__
 #include <stdarg.h>
 #else
 #include <varargs.h>
 #endif
 
 void
-#if __STDC__
+#ifdef __STDC__
 panic(const char *fmt, ...)
 #else
 panic(fmt, va_alist)
@@ -439,7 +437,7 @@ panic(fmt, va_alist)
 #endif
 {
        va_list ap;
-#if __STDC__
+#ifdef __STDC__
        va_start(ap, fmt);
 #else
        va_start(ap);
@@ -451,6 +449,6 @@ panic(fmt, va_alist)
        if (reply("abort") == GOOD) {
                if (reply("dump core") == GOOD)
                        abort();
-               done(1);
+               exit(1);
        }
 }