update: convert upserver and client from LWP to pthreads
[openafs.git] / src / update / client.c
index 333e1eb..5bc0ec0 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
@@ -9,37 +9,22 @@
 
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
+#include <afs/procmgmt.h>
+#include <roken.h>
+#include <afs/opr.h>
 
-#include <afs/stds.h>
-#ifdef AFS_AIX32_ENV
-#include <signal.h>
-#endif
-#include <sys/types.h>
-#include <sys/stat.h>
 #ifdef AFS_NT40_ENV
-#include <fcntl.h>
-#include <winsock2.h>
 #include <WINNT/afsevent.h>
 #include <sys/utime.h>
-#include <afs/dirent.h>
 #include <direct.h>
-#include <process.h>
-#include <io.h>
-#include <afs/procmgmt.h>
-#else
-#include <sys/file.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <sys/time.h>
-#include <dirent.h>
 #endif
-#include <string.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
+
+#ifdef AFS_AIX_ENV
+#include <sys/statfs.h>
 #endif
-#include <stdio.h>
-#include <errno.h>
+
 #include <rx/xdr.h>
 #include <rx/rx.h>
 #include <rx/rxkad.h>
@@ -47,9 +32,7 @@
 #include <afs/cellconfig.h>
 #include <afs/afsutil.h>
 #include <afs/fileutil.h>
-#ifdef AFS_AIX_ENV
-#include <sys/statfs.h>
-#endif
+
 #include "update.h"
 #include "global.h"
 #include "update_internal.h"
@@ -71,7 +54,7 @@ int update_ReceiveFile(int, struct rx_call *, struct stat *);
 afs_int32
 GetServer(char *aname)
 {
-    register struct hostent *th;
+    struct hostent *th;
     afs_int32 addr;
 
     th = gethostbyname(aname);
@@ -111,7 +94,7 @@ main(int argc, char **argv)
 
     afs_uint32 mode;
     int error;
-    char hostname[MAXSIZE];
+    char hostname[MAXFNSIZE];
     FILE *stream;
     afs_int32 time, length, atime;
     struct filestr *df;
@@ -122,12 +105,12 @@ main(int argc, char **argv)
     int a, cnt;
     rxkad_level level;
 
-    char dirbuf[MAXSIZE], filename[MAXSIZE];
+    char dirbuf[MAXFNSIZE], filename[MAXFNSIZE];
     struct filestr *dirname, *ModFiles, *okhostfiles;
 #ifdef AFS_AIX32_ENV
     /*
-     * The following signal action for AIX is necessary so that in case of a 
-     * crash (i.e. core is generated) we can include the user's data section 
+     * The following signal action for AIX is necessary so that in case of a
+     * crash (i.e. core is generated) we can include the user's data section
      * in the core dump. Unfortunately, by default, only a partial core is
      * generated which, in many cases, isn't too useful.
      */
@@ -194,10 +177,18 @@ main(int argc, char **argv)
                    ("Usage: upclient <hostname> [-crypt] [-clear] [-t <retry time>] [-verbose]* <dir>+ [-help]\n");
                exit(1);
            }
-       } else if (strlen(hostname) == 0)
-           strcpy(hostname, argv[a]);
-       else {
-           strcpy(filename, argv[a]);
+       } else if (strlen(hostname) == 0) {
+           if (strlcpy(hostname, argv[a], sizeof(hostname))
+                   >= sizeof(hostname)) {
+               fprintf(stderr, "Supplied hostname is too long\n");
+               exit(1);
+           }
+       } else {
+           if (strlcpy(filename, argv[a], sizeof(filename))
+                   >= sizeof(filename)) {
+               fprintf(stderr, "Supplied filename is too long\n");
+               exit(1);
+           }
            FilepathNormalize(filename);
            AddToList(&dirname, filename);
        }
@@ -320,13 +311,13 @@ main(int argc, char **argv)
                if (verbose >= 3)
                    printf("    checking %s\n", filename);
                if (!IsCompatible(filename, time, length)) {
-                   /* if the file info has changed , record all the 
+                   /* if the file info has changed , record all the
                     *changed files in the ModFiles array*/
                    if (verbose >= 2)
                        printf("  getting %s\n", filename);
                    AddToList(&ModFiles, filename);
 
-                   /* now get the file from the server. The received 
+                   /* now get the file from the server. The received
                     * file is created under the name filename.NEW */
                    errcode =
                        GetFileFromUpServer(conn, filename, uid, gid, mode,
@@ -347,7 +338,7 @@ main(int argc, char **argv)
            {                   /*delete all the redundant files on the client */
                DIR *dirp;
                struct dirent *dp;
-               char filename[MAXSIZE];
+               char filename[MAXFNSIZE];
 
                dirp = opendir(curDir);
                if (dirp == 0) {
@@ -384,14 +375,14 @@ main(int argc, char **argv)
            free(curDir);
        }                       /* end for each dir loop */
        /*delete the file with info on files in directory df->name */
-       IOMGR_Sleep(interval);
+       sleep(interval);
        continue;
 
       fail_dirbuf:
        fclose(stream);
        unlink(dirbuf);
       fail:
-       IOMGR_Sleep(retrytime);
+       sleep(retrytime);
        if (cnt > 10) {
            rx_DestroyConnection(conn);
            goto again;
@@ -404,7 +395,7 @@ main(int argc, char **argv)
 /* returns 1 if the file is upto date else returns 0*/
 /*check the dir case more carefully */
 int
-IsCompatible(char *filename, afs_int32 time, afs_int32 length) 
+IsCompatible(char *filename, afs_int32 time, afs_int32 length)
 {
     struct stat status;
     afs_int32 error;
@@ -466,11 +457,11 @@ FetchFile(struct rx_call *call, char *remoteFile, char *localFile, int dirFlag)
 
 
 int
-update_ReceiveFile(register int fd, register struct rx_call *call, register struct stat *status)
+update_ReceiveFile(int fd, struct rx_call *call, struct stat *status)
 {
-    register char *buffer = (char *)0;
+    char *buffer = (char *)0;
     afs_int32 length;
-    register int blockSize;
+    int blockSize;
     afs_int32 error = 0, len;
 #ifdef AFS_AIX_ENV
     struct statfs tstatfs;
@@ -489,13 +480,13 @@ update_ReceiveFile(register int fd, register struct rx_call *call, register stru
 #else
     blockSize = status->st_blksize;
 #endif
-    buffer = (char *)malloc(blockSize);
+    buffer = malloc(blockSize);
     if (!buffer) {
        printf("malloc failed\n");
        return UPDATE_ERROR;
     }
     while (!error && length) {
-       register int nbytes = (length > blockSize ? blockSize : length);
+       int nbytes = (length > blockSize ? blockSize : length);
        nbytes = rx_Read(call, buffer, nbytes);
        if (!nbytes)
            error = UPDATE_ERROR;
@@ -516,28 +507,45 @@ update_ReceiveFile(register int fd, register struct rx_call *call, register stru
 
 /*
  * PathsAreEquivalent() -- determine if paths are equivalent
+ * Returns 1 if yes, 0 if no, -1 on error.
  */
 static int
 PathsAreEquivalent(char *path1, char *path2)
 {
-    int areEq = 0;
-    char pathNorm1[AFSDIR_PATH_MAX], pathNorm2[AFSDIR_PATH_MAX];
+    int areEq = 0, code;
+    char *pathNorm1, *pathNorm2;
 
 #ifdef AFS_NT40_ENV
     /* case-insensitive comparison of normalized, same-flavor (short) paths */
     DWORD status;
 
+    pathNorm1 = malloc(AFSDIR_PATH_MAX);
+    if (pathNorm1 == NULL)
+       return -1;
     status = GetShortPathName(path1, pathNorm1, AFSDIR_PATH_MAX);
     if (status == 0 || status > AFSDIR_PATH_MAX) {
        /* can't convert path to short version; just use long version */
-       strcpy(pathNorm1, path1);
+       free(pathNorm1);
+       pathNorm1 = strdup(path1);
+       if (pathNorm1 == NULL)
+           return -1;
     }
     FilepathNormalize(pathNorm1);
 
+    pathNorm2 = malloc(AFSDIR_PATH_MAX);
+    if (pathNorm2 == NULL) {
+       code = -1;
+       goto out;
+    }
     status = GetShortPathName(path2, pathNorm2, AFSDIR_PATH_MAX);
     if (status == 0 || status > AFSDIR_PATH_MAX) {
        /* can't convert path to short version; just use long version */
-       strcpy(pathNorm2, path2);
+       free(pathNorm2);
+       pathNorm2 = strdup(path2);
+       if (pathNorm2 == NULL) {
+           code = -1;
+           goto out;
+       }
     }
     FilepathNormalize(pathNorm2);
 
@@ -546,17 +554,27 @@ PathsAreEquivalent(char *path1, char *path2)
     }
 #else
     /* case-sensitive comparison of normalized paths */
-    strcpy(pathNorm1, path1);
+    pathNorm1 = strdup(path1);
+    if (pathNorm1 == NULL)
+       return -1;
     FilepathNormalize(pathNorm1);
 
-    strcpy(pathNorm2, path2);
+    pathNorm2 = strdup(path2);
+    if (pathNorm2 == NULL) {
+       code = -1;
+       goto out;
+    }
     FilepathNormalize(pathNorm2);
 
     if (strcmp(pathNorm1, pathNorm2) == 0) {
        areEq = 1;
     }
 #endif /* AFS_NT40_ENV */
-    return areEq;
+    code = 0;
+out:
+    free(pathNorm1);
+    free(pathNorm2);
+    return (code != 0) ? code : areEq;
 }
 
 
@@ -586,11 +604,12 @@ NotOnHost(char *filename, struct filestr *okhostfiles)
            afs_com_err(whoami, rc, "Unable to construct local path");
            return -1;
        }
-       if (PathsAreEquivalent(hostfile, filename)) {
-           free(hostfile);
-           return 0;
-       }
+       rc = PathsAreEquivalent(hostfile, filename);
        free(hostfile);
+       if (rc < 0)
+           return -1;
+       if (rc)
+           return 0;
     }
     return 1;
 }
@@ -602,7 +621,7 @@ NotOnHost(char *filename, struct filestr *okhostfiles)
 static int
 RenameNewFiles(struct filestr *modFiles)
 {
-    char newname[MAXSIZE];
+    char newname[MAXFNSIZE];
     char *fname;
     int errcode = 0;
     struct filestr *tf;
@@ -617,7 +636,7 @@ RenameNewFiles(struct filestr *modFiles)
        strcat(newname, ".NEW");
        if (verbose >= 2)
            printf("  renaming %s\n", newname);
-       errcode = renamefile(newname, fname);
+       errcode = rk_rename(newname, fname);
        if (errcode) {
            printf("could not rename %s to %s\n", newname, fname);
            afs_com_err(whoami, errno, "could not rename %s to %s", newname,
@@ -631,12 +650,12 @@ RenameNewFiles(struct filestr *modFiles)
 
 
 /* GetFileFromUpServer() - Makes the FetchFile() call and gets the
- * file from the upserver. 
+ * file from the upserver.
  * Return Values:
  *   0 -  Alls well
  *   -1 - Serious error. Quit right away.
- *   1  - Error, but keep trying for the other files 
- * 
+ *   1  - Error, but keep trying for the other files
+ *
  * The file obtained is written to the localized version of the filename.NEW
  * and the uid, gid, file mode, access and modification times will be set to
  * the passed in values.
@@ -656,7 +675,7 @@ GetFileFromUpServer(struct rx_connection *conn,     /* handle for upserver */
 #else
     struct timeval tvp[2];
 #endif
-    char newfile[MAXSIZE];
+    char newfile[MAXFNSIZE];
 
     /* construct local path from canonical (wire-format) path */
     errcode = ConstructLocalPath(filename, "/", &lfile);