volser: Fix bad readlink usage
[openafs.git] / src / volser / restorevol.c
index 620e87b..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>
 
+#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];
@@ -147,7 +138,7 @@ ReadDumpHeader(struct DumpHeader *dh)
 {
     int i, done;
     char tag, c;
-    afs_int32 magic;
+    afs_int32 magic AFS_UNUSED;
 
 /*  memset(&dh, 0, sizeof(dh)); */
 
@@ -474,16 +465,16 @@ ReadVNode(afs_int32 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,
@@ -492,8 +483,8 @@ ReadVNode(afs_int32 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,
@@ -547,7 +538,7 @@ ReadVNode(afs_int32 count)
 
 
                buffer = NULL;
-               buffer = (char *)malloc(vn.dataSize);
+               buffer = malloc(vn.dataSize);
 
                readdata(buffer, vn.dataSize);
                page0 = (struct Page0 *)buffer;
@@ -573,12 +564,14 @@ ReadVNode(afs_int32 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);
@@ -597,8 +590,9 @@ ReadVNode(afs_int32 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,
@@ -609,12 +603,11 @@ ReadVNode(afs_int32 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) {
@@ -628,11 +621,11 @@ ReadVNode(afs_int32 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)) {
@@ -660,12 +653,12 @@ ReadVNode(afs_int32 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 */
                }
 
@@ -692,11 +685,10 @@ ReadVNode(afs_int32 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;
@@ -726,16 +718,16 @@ ReadVNode(afs_int32 count)
                 * 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 */
@@ -922,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);
            }
        }
@@ -946,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);
        }
     }