volser: Fix bad readlink usage
[openafs.git] / src / volser / restorevol.c
index 113956e..653a6ac 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
  *    uses your current umask. Mode bits for directories are 0777 (then
  *    AND'ed with the umask). Mode bits for files are the owner mode bits
  *    duplicated accross group and user (then AND'ed with the umask).
- * 5. For restores of full dumps, if a directory says it has a file and 
- *    the file is not found, then a symbolic link "AFSFile-<#>" will 
- *    appear in that restored tree. Restores of incremental dumps remove 
- *    all these files at the end (expensive because it is a tree search).  
- * 6. If a file or directory was found in the dump but found not to be 
- *    connected to the hierarchical tree, then the file or directory 
- *    will be connected at the root of the tree as "__ORPHANEDIR__.<#>" 
- *    or "__ORPHANFILE__.<#>".  
+ * 5. For restores of full dumps, if a directory says it has a file and
+ *    the file is not found, then a symbolic link "AFSFile-<#>" will
+ *    appear in that restored tree. Restores of incremental dumps remove
+ *    all these files at the end (expensive because it is a tree search).
+ * 6. If a file or directory was found in the dump but found not to be
+ *    connected to the hierarchical tree, then the file or directory
+ *    will be connected at the root of the tree as "__ORPHANEDIR__.<#>"
+ *    or "__ORPHANFILE__.<#>".
  * 7. ACLs are not restored.
  *
  */
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
 #include <afs/afsint.h>
 #include <afs/nfs.h>
+#include <rx/rx_queue.h>
 #include <lock.h>
 #include <afs/ihandle.h>
 #include <afs/vnode.h>
 #include <afs/volume.h>
-#include "volint.h"
-#include "dump.h"
 #include <afs/cmd.h>
 
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/uio.h>
-#include <stdio.h>
-#include <errno.h>
-#include <netinet/in.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <dirent.h>
-#include <string.h>
-
+#include "volint.h"
+#include "dump.h"
 
 char rootdir[MAXPATHLEN];
 char mntroot[MAXPATHLEN];
@@ -78,7 +67,7 @@ int inc_dump = 0;
 FILE *dumpfile;
 
 afs_int32
-readvalue(size)
+readvalue(int size)
 {
     afs_int32 value, s;
     int code;
@@ -101,11 +90,10 @@ readvalue(size)
 }
 
 char
-readchar()
+readchar(void)
 {
     char value;
     int code;
-    char *ptr;
 
     value = '\0';
     code = fread(&value, 1, 1, dumpfile);
@@ -119,9 +107,7 @@ readchar()
 char buf[BUFSIZE];
 
 void
-readdata(buffer, size)
-     char *buffer;
-     afs_sfsize_t size;
+readdata(char *buffer, afs_sfsize_t size)
 {
     int code;
     afs_int32 s;
@@ -148,12 +134,11 @@ readdata(buffer, size)
 }
 
 afs_int32
-ReadDumpHeader(dh)
-     struct DumpHeader *dh;    /* Defined in dump.h */
+ReadDumpHeader(struct DumpHeader *dh)
 {
-    int code, i, done;
+    int i, done;
     char tag, c;
-    afs_int32 magic;
+    afs_int32 magic AFS_UNUSED;
 
 /*  memset(&dh, 0, sizeof(dh)); */
 
@@ -222,11 +207,10 @@ struct volumeHeader {
 };
 
 afs_int32
-ReadVolumeHeader(count)
-     afs_int32 count;
+ReadVolumeHeader(afs_int32 count)
 {
     struct volumeHeader vh;
-    int code, i, done, entries;
+    int i, done;
     char tag, c;
 
 /*  memset(&vh, 0, sizeof(vh)); */
@@ -240,7 +224,7 @@ ReadVolumeHeader(count)
            break;
 
        case 'v':
-           ntohl(readvalue(4));        /* version stamp - ignore */
+           (void)ntohl(readvalue(4));  /* version stamp - ignore */
            break;
 
        case 'n':
@@ -392,18 +376,18 @@ struct vNode {
 #define MAXNAMELEN 256
 
 afs_int32
-ReadVNode(count)
-     afs_int32 count;
+ReadVNode(afs_int32 count)
 {
     struct vNode vn;
-    int code, i, done, entries;
-    char tag, c;
+    int code, i, done;
+    char tag;
     char dirname[MAXNAMELEN], linkname[MAXNAMELEN], lname[MAXNAMELEN];
     char parentdir[MAXNAMELEN], vflink[MAXNAMELEN];
     char filename[MAXNAMELEN], fname[MAXNAMELEN];
     int len;
     afs_int32 vnode;
     afs_int32 mode = 0;
+    afs_uint32 hi, lo;
 
 /*  memset(&vn, 0, sizeof(vn)); */
     vn.dataSize = 0;
@@ -462,16 +446,11 @@ ReadVNode(count)
            readdata(vn.acl, 192);      /* Skip ACL data */
            break;
 
-#ifdef AFS_LARGEFILE_ENV
        case 'h':
-           {
-               afs_uint32 hi, lo;
-               hi = ntohl(readvalue(4));
-               lo = ntohl(readvalue(4));
-               FillInt64(vn.dataSize, hi, lo);
-           }
+           hi = ntohl(readvalue(4));
+           lo = ntohl(readvalue(4));
+           FillInt64(vn.dataSize, hi, lo);
            goto common_vnode;
-#endif /* !AFS_LARGEFILE_ENV */
 
        case 'f':
            vn.dataSize = ntohl(readvalue(4));
@@ -486,16 +465,16 @@ ReadVNode(count)
            if (vnode == 1)
                strncpy(parentdir, rootdir, sizeof parentdir);
            else {
-               afs_snprintf(parentdir, sizeof parentdir, "%s/%s%d", rootdir,
-                            ADIR, vnode);
+               snprintf(parentdir, sizeof parentdir,
+                        "%s" OS_DIRSEP "%s%d", rootdir, ADIR, vnode);
 
                len = readlink(parentdir, linkname, MAXNAMELEN);
                if (len < 0) {
                    /* parentdir does not exist. So create an orphan dir.
                     * and then link the parentdir to the orphaned dir.
                     */
-                   afs_snprintf(linkname, sizeof linkname, "%s/%s%d",
-                                rootdir, ODIR, vnode);
+                   snprintf(linkname, sizeof linkname, "%s" OS_DIRSEP "%s%d",
+                            rootdir, ODIR, vnode);
                    code = mkdir(linkname, 0777);
                    if ((code < 0) && (errno != EEXIST)) {
                        fprintf(stderr,
@@ -504,8 +483,8 @@ ReadVNode(count)
                    }
 
                    /* Link the parentdir to it - now parentdir exists */
-                   afs_snprintf(linkname, sizeof linkname, "%s%d/", ODIR,
-                                vnode);
+                   snprintf(linkname, sizeof linkname, "%s%d/", ODIR,
+                            vnode);
                    code = symlink(linkname, parentdir);
                    if (code) {
                        fprintf(stderr,
@@ -559,7 +538,7 @@ ReadVNode(count)
 
 
                buffer = NULL;
-               buffer = (char *)malloc(vn.dataSize);
+               buffer = malloc(vn.dataSize);
 
                readdata(buffer, vn.dataSize);
                page0 = (struct Page0 *)buffer;
@@ -585,12 +564,14 @@ ReadVNode(count)
                        if (this_vn & 1) {
                             /*ADIRENTRY*/
                                /* dirname is the directory to create.
-                                * vflink is what will link to it. 
+                                * vflink is what will link to it.
                                 */
-                               afs_snprintf(dirname, sizeof dirname, "%s/%s",
-                                            parentdir, this_name);
-                           afs_snprintf(vflink, sizeof vflink, "%s/%s%d",
-                                        rootdir, ADIR, this_vn);
+                           snprintf(dirname, sizeof dirname,
+                                    "%s" OS_DIRSEP "%s",
+                                    parentdir, this_name);
+                           snprintf(vflink, sizeof vflink,
+                                    "%s" OS_DIRSEP "%s%d",
+                                    rootdir, ADIR, this_vn);
 
                            /* The link and directory may already exist */
                            len = readlink(vflink, linkname, MAXNAMELEN);
@@ -609,8 +590,9 @@ ReadVNode(count)
                                 * It was created originally as orphaned.
                                 */
                                linkname[len - 1] = '\0';       /* remove '/' at end */
-                               afs_snprintf(lname, sizeof lname, "%s/%s",
-                                            rootdir, linkname);
+                               snprintf(lname, sizeof lname,
+                                        "%s" OS_DIRSEP "%s",
+                                        rootdir, linkname);
                                code = rename(lname, dirname);
                                if (code) {
                                    fprintf(stderr,
@@ -621,12 +603,11 @@ ReadVNode(count)
 
                            /* Now create/update the link to the new/moved directory */
                            if (vn.vnode == 1)
-                               afs_snprintf(dirname, sizeof dirname, "%s/",
-                                            this_name);
+                               snprintf(dirname, sizeof dirname, "%s/",
+                                        this_name);
                            else
-                               afs_snprintf(dirname, sizeof dirname,
-                                            "%s%d/%s/", ADIR, vn.vnode,
-                                            this_name);
+                               snprintf(dirname, sizeof dirname, "%s%d/%s/",
+                                        ADIR, vn.vnode, this_name);
                            unlink(vflink);
                            code = symlink(dirname, vflink);
                            if (code) {
@@ -640,11 +621,11 @@ ReadVNode(count)
                             * by creating a link within the directory. Restoring
                             * the file will later remove the link.
                             */
-                           else {
-                            /*AFILEENTRY*/ afs_snprintf(vflink,
-                                                        sizeof vflink,
-                                                        "%s/%s%d", parentdir,
-                                                        AFILE, this_vn);
+                       else {
+                            /*AFILEENTRY*/
+                           snprintf(vflink, sizeof vflink,
+                                    "%s" OS_DIRSEP "%s%d", parentdir,
+                                    AFILE, this_vn);
 
                            code = symlink(this_name, vflink);
                            if ((code < 0) && (errno != EEXIST)) {
@@ -672,12 +653,12 @@ ReadVNode(count)
                 * then the file will be an orphaned file.
                 */
                lfile = 1;
-               afs_snprintf(filename, sizeof filename, "%s/%s%d", parentdir,
-                            AFILE, vn.vnode);
+               snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s%d",
+                        parentdir, AFILE, vn.vnode);
                len = readlink(filename, fname, MAXNAMELEN);
                if (len < 0) {
-                   afs_snprintf(filename, sizeof filename, "%s/%s%d",
-                                rootdir, OFILE, vn.vnode);
+                   snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s%d",
+                            rootdir, OFILE, vn.vnode);
                    lfile = 0;  /* no longer a linked file; a direct path */
                }
 
@@ -704,11 +685,10 @@ ReadVNode(count)
                                    errno);
                        else {
                            char tmp[100];
-                           (void)afs_snprintf(tmp, sizeof tmp,
-                                              "Read %llu bytes out of %llu",
-                                              (afs_uintmax_t) (vn.dataSize -
-                                                               size),
-                                              (afs_uintmax_t) vn.dataSize);
+                           snprintf(tmp, sizeof tmp,
+                                    "Read %llu bytes out of %llu",
+                                    (afs_uintmax_t) (vn.dataSize - size),
+                                    (afs_uintmax_t) vn.dataSize);
                            fprintf(stderr, "%s\n", tmp);
                        }
                        break;
@@ -732,23 +712,22 @@ ReadVNode(count)
                     * also be a mount point. If the volume is being restored to AFS, this
                     * will become a mountpoint. If not, it becomes a symlink to no-where.
                     */
-               int fid;
-               afs_int32 size, s;
+               afs_int32 s;
 
                /* Check if its vnode-file-link exists and create pathname
                 * of the symbolic link. If it doesn't exist,
                 * then the link will be an orphaned link.
                 */
-               afs_snprintf(linkname, sizeof linkname, "%s/%s%d", parentdir,
-                            AFILE, vn.vnode);
-               len = readlink(linkname, fname, MAXNAMELEN);
+               snprintf(linkname, sizeof linkname, "%s" OS_DIRSEP "%s%d",
+                        parentdir, AFILE, vn.vnode);
+               len = readlink(linkname, fname, MAXNAMELEN - 1);
                if (len < 0) {
-                   afs_snprintf(filename, sizeof filename, "%s/%s%d",
-                                rootdir, OFILE, vn.vnode);
+                   snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s%d",
+                            rootdir, OFILE, vn.vnode);
                } else {
                    fname[len] = '\0';
-                   afs_snprintf(filename, sizeof filename, "%s/%s",
-                                parentdir, fname);
+                   snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s",
+                            parentdir, fname);
                }
 
                /* Read the link in, delete it, and then create it */
@@ -796,12 +775,11 @@ ReadVNode(count)
 static int
 WorkerBee(struct cmd_syndesc *as, void *arock)
 {
-    int code = 0, c, len;
+    int code = 0, len;
     afs_int32 type, count, vcount;
     DIR *dirP, *dirQ;
     struct dirent *dirE, *dirF;
-    char fname[MAXNAMELEN], name[MAXNAMELEN], lname[MAXNAMELEN],
-       mname[MAXNAMELEN];
+    char name[MAXNAMELEN];
     char thisdir[MAXPATHLEN], *t;
     struct DumpHeader dh;      /* Defined in dump.h */
 #if 0/*ndef HAVE_GETCWD*/      /* XXX enable when autoconf happens */
@@ -936,20 +914,20 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
        dirP = opendir(rootdir);
        while (dirP && (dirE = readdir(dirP))) {
            if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
-               afs_snprintf(name, sizeof name, "%s/%s", rootdir,
-                            dirE->d_name);
+               snprintf(name, sizeof name, "%s" OS_DIRSEP "%s", rootdir,
+                        dirE->d_name);
                dirQ = opendir(name);
                while (dirQ && (dirF = readdir(dirQ))) {
                    if (strncmp(dirF->d_name, AFILE, strlen(AFILE)) == 0) {
-                       afs_snprintf(name, sizeof name, "%s/%s/%s", rootdir,
-                                    dirE->d_name, dirF->d_name);
+                       snprintf(name, sizeof name, "%s" OS_DIRSEP "%s/%s",
+                                rootdir, dirE->d_name, dirF->d_name);
                        unlink(name);
                    }
                }
                closedir(dirQ);
            } else if (strncmp(dirE->d_name, AFILE, strlen(AFILE)) == 0) {
-               afs_snprintf(name, sizeof name, "%s/%s", rootdir,
-                            dirE->d_name);
+               snprintf(name, sizeof name, "%s" OS_DIRSEP "%s", rootdir,
+                        dirE->d_name);
                unlink(name);
            }
        }
@@ -960,7 +938,8 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
     dirP = opendir(rootdir);
     while (dirP && (dirE = readdir(dirP))) {
        if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
-           afs_snprintf(name, sizeof name, "%s/%s", rootdir, dirE->d_name);
+           snprintf(name, sizeof name, "%s" OS_DIRSEP "%s", rootdir,
+                    dirE->d_name);
            unlink(name);
        }
     }
@@ -969,12 +948,10 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
     return (code);
 }
 
-main(argc, argv)
-     int argc;
-     char **argv;
+int
+main(int argc, char **argv)
 {
     struct cmd_syndesc *ts;
-    struct cmd_item *ti;
 
     setlinebuf(stdout);