]> git.wh0rd.org - dump.git/blobdiff - restore/utilities.c
kill "register".
[dump.git] / restore / utilities.c
index c93c8540fa29a634275326664dfc090279c7f8cd..627e147875ee44d5454aa4f8810f8462e852ed97 100644 (file)
@@ -2,8 +2,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-1997
- *      Stelian Pop <pop@cybercable.fr>, 1999 
- *
+ *     Stelian Pop <stelian@popies.net>, 1999-2000
+ *     Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
  */
 
 /*
  */
 
 #ifndef lint
-#if 0
-static char sccsid[] = "@(#)utilities.c        8.5 (Berkeley) 4/28/95";
-#endif
 static const char rcsid[] =
-       "$Id: utilities.c,v 1.2 1999/10/11 12:53:25 stelian Exp $";
+       "$Id: utilities.c,v 1.19 2002/01/25 15:09:00 stelian Exp $";
 #endif /* not lint */
 
+#include <config.h>
+#include <errno.h>
+#include <compaterr.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
 #include <sys/param.h>
 #include <sys/stat.h>
 
 #ifdef __linux__
 #include <sys/time.h>
+#include <time.h>
+#ifdef HAVE_EXT2FS_EXT2_FS_H
+#include <ext2fs/ext2_fs.h>
+#else
 #include <linux/ext2_fs.h>
+#endif
+#include <ext2fs/ext2fs.h>
 #include <bsdcompat.h>
 #else  /* __linux__ */
 #include <ufs/ufs/dinode.h>
 #include <ufs/ufs/dir.h>
 #endif /* __linux__ */
 
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#ifdef __linux__
-#include <ext2fs/ext2fs.h>
-#endif
-
 #include "restore.h"
 #include "extern.h"
 
@@ -75,10 +76,9 @@ static const char rcsid[] =
  * Insure that all the components of a pathname exist.
  */
 void
-pathcheck(name)
-       char *name;
+pathcheck(char *name)
 {
-       register char *cp;
+       char *cp;
        struct entry *ep;
        char *start;
 
@@ -104,8 +104,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];
 
@@ -123,8 +122,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;
@@ -135,7 +133,7 @@ gentempname(ep)
                i++;
        if (np == NULL)
                badentry(ep, "not on ino list");
-       (void) sprintf(name, "%s%ld%lu", TMPHDR, i, (u_long)ep->e_ino);
+       (void) snprintf(name, sizeof(name), "%s%ld%lu", TMPHDR, i, (unsigned long)ep->e_ino);
        return (name);
 }
 
@@ -143,26 +141,22 @@ 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);
@@ -170,18 +164,17 @@ newnode(np)
 
        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;
 
@@ -193,18 +186,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;
 
@@ -216,19 +208,17 @@ 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 */
@@ -237,9 +227,8 @@ linkit(existing, new, type)
 
        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) {
@@ -259,11 +248,22 @@ linkit(existing, new, type)
                                ret = link(existing, new);
                                chflags(existing, s.st_flags);
                        }
+#else
+                       unsigned long s;
+
+                       /*
+                        * Most likely, the immutable or append-only attribute
+                        * is set. Clear the attributes and try again.
+                        */
+                       if (fgetflags (existing, &s) != -1 &&
+                           fsetflags (existing, 0) != -1) {
+                               ret = link(existing, new);
+                               fsetflags(existing, s);
+                       }
 #endif
                        if (ret < 0) {
-                               fprintf(stderr, "warning: cannot create "
-                                   "hard link %s->%s: %s\n",
-                                   new, existing, strerror(errno));
+                               warn("warning: cannot create hard link %s->%s",
+                                   new, existing);
                                return (FAIL);
                        }
                }
@@ -271,7 +271,7 @@ linkit(existing, new, type)
                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);
 }
@@ -281,16 +281,14 @@ linkit(existing, new, type)
  * 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);
 }
 
@@ -298,8 +296,7 @@ addwhiteout(name)
  * Delete a whiteout.
  */
 void
-delwhiteout(ep)
-       register struct entry *ep;
+delwhiteout(struct entry *ep)
 {
        char *name;
 
@@ -309,22 +306,20 @@ 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
 
 /*
  * find lowest number file (above "start") that needs to be extracted
  */
-ino_t
-lowerbnd(start)
-       ino_t start;
+dump_ino_t
+lowerbnd(dump_ino_t start)
 {
-       register struct entry *ep;
+       struct entry *ep;
 
        for ( ; start < maxino; start++) {
                ep = lookupino(start);
@@ -339,11 +334,10 @@ lowerbnd(start)
 /*
  * find highest number file (below "start") that needs to be extracted
  */
-ino_t
-upperbnd(start)
-       ino_t start;
+dump_ino_t
+upperbnd(dump_ino_t start)
 {
-       register struct entry *ep;
+       struct entry *ep;
 
        for ( ; start > ROOTINO; start--) {
                ep = lookupino(start);
@@ -359,9 +353,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);
@@ -378,7 +370,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: %lu\n", (u_long)ep->e_ino);
+       fprintf(stderr, "inode number: %lu\n", (unsigned long)ep->e_ino);
        panic("flags: %s\n", flagvalues(ep));
 }
 
@@ -386,8 +378,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];
 
@@ -411,12 +402,11 @@ flagvalues(ep)
 /*
  * Check to see if a name is on a dump tape.
  */
-ino_t
-dirlookup(name)
-       const char *name;
+dump_ino_t
+dirlookup(const char *name)
 {
        struct direct *dp;
-       ino_t ino;
+       dump_ino_t ino;
 
        ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
 
@@ -429,8 +419,7 @@ dirlookup(name)
  * Elicit a reply.
  */
 int
-reply(question)
-       char *question;
+reply(const char *question)
 {
        char c;
 
@@ -450,14 +439,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)
@@ -466,7 +455,7 @@ panic(fmt, va_alist)
 #endif
 {
        va_list ap;
-#if __STDC__
+#ifdef __STDC__
        va_start(ap, fmt);
 #else
        va_start(ap);
@@ -478,6 +467,79 @@ panic(fmt, va_alist)
        if (reply("abort") == GOOD) {
                if (reply("dump core") == GOOD)
                        abort();
-               done(1);
+               exit(1);
+       }
+}
+
+#ifdef USE_QFA
+/*
+ * search for ino in QFA file
+ *
+ * if exactmatch:
+ * if ino found return tape number and tape position
+ * if ino not found return tnum=0 and tpos=0
+ *
+ * if not exactmatch:
+ * if ino found return tape number and tape position
+ * if ino not found return tape number and tape position of last smaller ino
+ * if no smaller inode found return tnum=0 and tpos=0
+ */
+int
+Inode2Tapepos(dump_ino_t ino, long *tnum, long long *tpos, int exactmatch)
+{
+       char *p, *pp;
+       char numbuff[32];
+       unsigned long tmpino;
+       long tmptnum;
+       long long tmptpos;
+
+       *tpos = 0;
+       *tnum = 0;
+       if (fseek(gTapeposfp, gSeekstart, SEEK_SET) == -1)
+               return errno;
+       while (fgets(gTps, sizeof(gTps), gTapeposfp) != NULL) {
+               gTps[strlen(gTps) - 1] = 0;     /* delete end of line */
+               p = gTps;
+               bzero(numbuff, sizeof(numbuff));
+               pp = numbuff;
+               /* read inode */
+               while ((*p != 0) && (*p != '\t'))
+                       *pp++ = *p++;
+               tmpino = atol(numbuff);
+               if (*p == 0)
+                       return 1;       /* may NOT happen */    
+               p++;
+               bzero(numbuff, sizeof(numbuff));
+               pp = numbuff;
+               /* read tapenum */
+               while ((*p != 0) && (*p != '\t'))
+                       *pp++ = *p++;
+               if (*p == 0)
+                       return 1;       /* may NOT happen */    
+               tmptnum = atol(numbuff);
+               p++;
+               bzero(numbuff, sizeof(numbuff));
+               pp = numbuff;
+               /* read tapepos */
+               while ((*p != 0) && (*p != '\t'))
+                       *pp++ = *p++;
+               tmptpos = atoll(numbuff);
+
+               if (exactmatch) {
+                       if (tmpino == ino)  {
+                               *tnum = tmptnum;
+                               *tpos = tmptpos;
+                               return 0;
+                       }
+               } else {
+                       if (tmpino > ino)  {
+                               return 0;
+                       } else {
+                               *tnum = tmptnum;
+                               *tpos = tmptpos;
+                       }
+               }
        }
+       return 0;
 }
+#endif /* USE_QFA */