add unlock support to afscp
authorDerrick Brashear <shadow@dementia.org>
Tue, 1 Dec 2009 21:17:49 +0000 (16:17 -0500)
committerDerrick Brashear <shadow|account-1000005@unknown>
Thu, 3 Dec 2009 18:44:40 +0000 (10:44 -0800)
make the afscp test client able to drop a lock on a file.
additionally, make it build again.

Change-Id: Ib4a5acf787f8f2e84699e1638a662068b993d965
Reviewed-on: http://gerrit.openafs.org/881
Tested-by: Derrick Brashear <shadow@dementia.org>
Reviewed-by: Derrick Brashear <shadow@dementia.org>

src/tests/afscp.c
src/tests/afscp_callback.c
src/tests/snprintf.c

index 0fdc55c..248879e 100644 (file)
@@ -18,7 +18,9 @@
 #include <sys/ioctl.h>
 #include <afs/venus.h>
 #include <afs/cellconfig.h>
-#include <afs/afs.h>
+#include <afs/sys_prototypes.h>
+
+#include <afs/afs_const.h>
 
 /*#include <rx/rxkad.h>*/
 #include <rx/rx_null.h>
@@ -42,7 +44,7 @@ statfile(char *path, char *cellname, afs_uint32 * server, struct AFSFid *f)
     int code;
 
     if (!strncmp(path, "@afs:", 5)) {
-       char *pdup, *p, *host, *id;
+       char *pdup, *p;
        struct hostent *he;
 
        pdup = strdup(path);
@@ -121,7 +123,7 @@ statfile(char *path, char *cellname, afs_uint32 * server, struct AFSFid *f)
 struct rx_securityClass *sc;
 
 extern int
-start_cb_server()
+start_cb_server(void)
 {
     struct rx_service *s;
 
@@ -194,7 +196,7 @@ main(int argc, char **argv)
 {
     char scell[MAXCELLCHARS], dcell[MAXCELLCHARS];
     afs_uint32 ssrv, dsrv;
-    char *databuffer, *srcf, *destd, *destf, *destpath;
+    char *databuffer, *srcf = NULL, *destd = NULL, *destf = NULL, *destpath = NULL;
     struct stat statbuf;
 
     struct AFSStoreStatus sst;
@@ -203,17 +205,17 @@ main(int argc, char **argv)
     struct AFSCallBack scb, dcb;
     struct AFSFid sf, dd, df;
 
-    int filesz;
+    int filesz = 0;
     int ch, blksize, bytesremaining, bytes;
     struct timeval start, finish;
     struct timezone tz;
     struct rx_securityClass *ssc = 0, *dsc = 0;
     int sscindex, dscindex;
-    struct rx_connection *sconn, *dconn;
-    struct rx_call *scall, *dcall;
-    int code, fetchcode, storecode, printcallerrs;
-    int slcl = 0, dlcl = 0;
-    int sfd, dfd, unauth = 0;
+    struct rx_connection *sconn = NULL, *dconn = NULL;
+    struct rx_call *scall = NULL, *dcall = NULL;
+    int code = 0, fetchcode, storecode, printcallerrs = 0;
+    int slcl = 0, dlcl = 0, unlock = 0;
+    int sfd = 0, dfd = 0, unauth = 0;
 
     struct AFSCBFids theFids;
     struct AFSCBs theCBs;
@@ -221,7 +223,7 @@ main(int argc, char **argv)
 
     blksize = 8 * 1024;
 
-    while ((ch = getopt(argc, argv, "ioub:")) != -1) {
+    while ((ch = getopt(argc, argv, "iouUb:")) != -1) {
        switch (ch) {
        case 'b':
            blksize = atoi(optarg);
@@ -235,6 +237,9 @@ main(int argc, char **argv)
        case 'u':
            unauth = 1;
            break;
+       case 'U':
+           unlock = 1;
+           break;
        default:
            printf("Unknown option '%c'\n", ch);
            exit(1);
@@ -242,37 +247,41 @@ main(int argc, char **argv)
     }
 
 
-    if (argc - optind < 2) {
+    if (argc - optind + unlock < 2) {
        fprintf(stderr,
-               "Usage: afscp [-i|-o]] [-b xfersz] [-u] source dest\n");
+               "Usage: afscp [-i|-o]] [-b xfersz] [-u] [-U] source [dest]\n");
        fprintf(stderr, "  -b   Set block size\n");
        fprintf(stderr, "  -i   Source is local (copy into AFS)\n");
        fprintf(stderr, "  -o   Dest is local (copy out of AFS)\n");
        fprintf(stderr, "  -u   Run unauthenticated\n");
+       fprintf(stderr, "  -U   Send an unlock request for source. (dest path not required)\n");
        fprintf(stderr, "source and dest can be paths or specified as:\n");
        fprintf(stderr, "     @afs:cellname:servername:volume:vnode:uniq\n");
        exit(1);
     }
     srcf = argv[optind++];
-    destpath = argv[optind++];
-    destd = strdup(destpath);
-    if (!destd) {
-       perror("strdup");
-       exit(1);
-    }
-    if ((destf = strrchr(destd, '/'))) {
-       *destf++ = 0;
-    } else {
-       destf = destd;
-       destd = ".";
+    if (!unlock) {
+       destpath = argv[optind++];
+       destd = strdup(destpath);
+       if (!destd) {
+           perror("strdup");
+           exit(1);
+       }
+       if ((destf = strrchr(destd, '/'))) {
+           *destf++ = 0;
+       } else {
+           destf = destd;
+           destd = ".";
+       }
+    } else if (slcl) {
+       fprintf(stderr, "-i and -U cannot be used together\n");
     }
 
-
     if (!slcl && statfile(srcf, scell, &ssrv, &sf)) {
        fprintf(stderr, "Cannot get attributes of %s\n", srcf);
        exit(1);
     }
-    if (!dlcl && statfile(destd, dcell, &dsrv, &dd)) {
+    if (!unlock && !dlcl && statfile(destd, dcell, &dsrv, &dd)) {
        fprintf(stderr, "Cannot get attributes of %s\n", destd);
        exit(1);
     }
@@ -311,7 +320,7 @@ main(int argc, char **argv)
        }
     }
 
-    if (!dlcl) {
+    if (!dlcl && !unlock) {
        if (!slcl && ssrv == dsrv) {
            dconn = sconn;
            dsc = NULL;
@@ -346,7 +355,7 @@ main(int argc, char **argv)
 
     memset(&sst, 0, sizeof(struct AFSStoreStatus));
 
-    if (dlcl) {
+    if (dlcl && !unlock) {
        dfd = open(destpath, O_RDWR | O_CREAT | O_EXCL, 0666);
        if (dfd < 0 && errno == EEXIST) {
            printf("%s already exists, overwriting\n", destpath);
@@ -361,7 +370,7 @@ main(int argc, char **argv)
                    afs_error_message(errno));
            goto Fail_dconn;
        }
-    } else {
+    } else if (!unlock) {
        if ((code =
             RXAFS_CreateFile(dconn, &dd, destf, &sst, &df, &fst, &dfst, &dcb,
                              &vs))) {
@@ -413,11 +422,24 @@ main(int argc, char **argv)
     printcallerrs = 0;
     fetchcode = 0;
     storecode = 0;
-    if (!slcl)
+    if (!slcl && !unlock)
        scall = rx_NewCall(sconn);
-    if (!dlcl)
+    if (!dlcl && !unlock)
        dcall = rx_NewCall(dconn);
     gettimeofday(&start, &tz);
+    if (unlock) {
+       if (fst.lockCount) {
+           printf("Sending 1 unlock for %s (%d locks)\n", srcf, fst.lockCount);
+           if ((code = RXAFS_ReleaseLock(sconn, &sf, &vs))) {
+               printf("Unable to unlock %s (%s)\n", srcf,
+                      afs_error_message(code));
+           }
+       } else {
+           printf("No locks for %s\n", srcf);
+       }
+       fetchcode = code;
+       goto Finish;
+    }
 
     if (!slcl) {
        if ((code = StartRXAFS_FetchData(scall, &sf, 0, filesz))) {
@@ -507,12 +529,12 @@ main(int argc, char **argv)
     if (dlcl) {
        if (close(dfd) && !storecode)
            storecode = errno;
-    } else {
+    } else if (!unlock) {
        storecode = rx_EndCall(dcall, storecode);
     }
     if (storecode && printcallerrs)
        printf("Error returned from store: %s\n", afs_error_message(storecode));
-
+Finish:
     gettimeofday(&finish, &tz);
 
     if (!slcl) {
@@ -543,7 +565,7 @@ main(int argc, char **argv)
        code = fetchcode;
 
   Fail_dconn:
-    if (!dlcl && (slcl || dconn != sconn))
+    if (!dlcl && !unlock && (slcl || dconn != sconn))
        rx_DestroyConnection(dconn);
   Fail_sconn:
     if (!slcl)
@@ -557,14 +579,14 @@ main(int argc, char **argv)
     rx_Finalize();
 
     free(databuffer);
-    if (printcallerrs) {
+    if (printcallerrs && !unlock) {
        double rate, size, time;
        if (finish.tv_sec == start.tv_sec) {
            printf("Copied %d bytes in %d microseconds\n", filesz,
                   finish.tv_usec - start.tv_usec);
        } else {
            printf("Copied %d bytes in %d seconds\n", filesz,
-                  finish.tv_sec - start.tv_sec);
+                  (int)(finish.tv_sec - start.tv_sec));
        }
 
        size = filesz / 1024.0;
index 6fcc9fa..4a9afa0 100644 (file)
@@ -1,14 +1,16 @@
 #include <afs/param.h>
 #include <afs/afscbint.h>      /*Callback interface defs */
+#include <afs/afsutil.h>
+#include <afs/afsutil_prototypes.h>
 int afs_cb_inited = 0;
 struct interfaceAddr afs_cb_interface;
 static int
-init_afs_cb()
+init_afs_cb(void)
 {
     int count;
 
     afs_uuid_create(&afs_cb_interface.uuid);
-    count = rx_getAllAddr(&afs_cb_interface.addr_in, AFS_MAX_INTERFACE_ADDR);
+    count = rx_getAllAddr((afs_uint32 *)&afs_cb_interface.addr_in, AFS_MAX_INTERFACE_ADDR);
     if (count <= 0)
        afs_cb_interface.numberOfInterfaces = 0;
     else
@@ -18,11 +20,8 @@ init_afs_cb()
 }
 
 afs_int32
-SRXAFSCB_CallBack(rxcall, Fids_Array, CallBack_Array)
-     struct rx_call *rxcall;
-     AFSCBFids *Fids_Array;
-     AFSCBs *CallBack_Array;
-
+SRXAFSCB_CallBack(struct rx_call *rxcall, AFSCBFids *Fids_Array,
+                 AFSCBs *CallBack_Array)
 {                              /*SRXAFSCB_CallBack */
     return (0);
 
@@ -30,87 +29,63 @@ SRXAFSCB_CallBack(rxcall, Fids_Array, CallBack_Array)
 
 
 afs_int32
-SRXAFSCB_InitCallBackState(rxcall)
-     struct rx_call *rxcall;
-
+SRXAFSCB_InitCallBackState(struct rx_call *rxcall)
 {                              /*SRXAFSCB_InitCallBackState */
     return (0);
 
 }                              /*SRXAFSCB_InitCallBackState */
 
 afs_int32
-SRXAFSCB_Probe(rxcall)
-     struct rx_call *rxcall;
-
+SRXAFSCB_Probe(struct rx_call *rxcall)
 {                              /*SRXAFSCB_Probe */
     return (0);
 
 }                              /*SRXAFSCB_Probe */
 
-
 afs_int32
-SRXAFSCB_GetCE(rxcall, index, ce)
-     struct rx_call *rxcall;
-     afs_int32 index;
-     AFSDBCacheEntry * ce;
+SRXAFSCB_GetCE(struct rx_call *rxcall, afs_int32 index, AFSDBCacheEntry * ce)
 {                              /*SRXAFSCB_GetCE */
     return (0);
 }                              /*SRXAFSCB_GetCE */
 
 
 afs_int32
-SRXAFSCB_GetCE64(rxcall, index, ce)
-     struct rx_call *rxcall;
-     afs_int32 index;
-     AFSDBCacheEntry64 *ce;
+SRXAFSCB_GetCE64(struct rx_call *rxcall, afs_int32 index, AFSDBCacheEntry64 *ce)
 {                              /*SRXAFSCB_GetCE64 */
     return (0);
 }                              /*SRXAFSCB_GetCE64 */
 
 
 afs_int32
-SRXAFSCB_GetLock(rxcall, index, lock)
-     struct rx_call *rxcall;
-     afs_int32 index;
-     AFSDBLock *lock;
+SRXAFSCB_GetLock(struct rx_call *rxcall, afs_int32 index, AFSDBLock *lock)
 {                              /*SRXAFSCB_GetLock */
     return (0);
 
 }                              /*SRXAFSCB_GetLock */
 
 afs_int32
-SRXAFSCB_XStatsVersion(rxcall, v)
-     struct rx_call *rxcall;
-     afs_int32 *v;
+SRXAFSCB_XStatsVersion(struct rx_call *rxcall, afs_int32 *v)
 {                              /*SRXAFSCB_XStatsVersion */
     return (0);
 
 }                              /*SRXAFSCB_XStatsVersion */
 
 afs_int32
-SRXAFSCB_GetXStats(rxcall, clientVersionNumber, collectionNumber, srvVersionNumberP, timeP, dataP)
-     struct rx_call *rxcall;
-     afs_int32 clientVersionNumber;
-     afs_int32 collectionNumber;
-     afs_int32 * srvVersionNumberP;
-     afs_int32 * timeP;
-     AFSCB_CollData * dataP;
+SRXAFSCB_GetXStats(struct rx_call *rxcall, afs_int32 clientVersionNumber,
+                  afs_int32 collectionNumber, afs_int32 * srvVersionNumberP,
+                  afs_int32 * timeP, AFSCB_CollData * dataP)
 {                              /*SRXAFSCB_GetXStats */
     return (0);
 }                              /*SRXAFSCB_GetXStats */
 
 int
-SRXAFSCB_InitCallBackState2(rxcall, addr)
-     struct rx_call *rxcall;
-     struct interfaceAddr *addr;
+SRXAFSCB_InitCallBackState2(struct rx_call *rxcall, struct interfaceAddr *addr)
 {
     return RXGEN_OPCODE;
 }
 
 int
-SRXAFSCB_WhoAreYou(rxcall, addr)
-     struct rx_call *rxcall;
-     struct interfaceAddr *addr;
+SRXAFSCB_WhoAreYou(struct rx_call *rxcall, struct interfaceAddr *addr)
 {
     if (rxcall && addr) {
        if (!afs_cb_inited)
@@ -121,17 +96,13 @@ SRXAFSCB_WhoAreYou(rxcall, addr)
 }
 
 int
-SRXAFSCB_InitCallBackState3(rxcall, uuidp)
-     struct rx_call *rxcall;
-     afsUUID *uuidp;
+SRXAFSCB_InitCallBackState3(struct rx_call *rxcall, afsUUID *uuidp)
 {
     return (0);
 }
 
 int
-SRXAFSCB_ProbeUuid(rxcall, uuidp)
-     struct rx_call *rxcall;
-     afsUUID *uuidp;
+SRXAFSCB_ProbeUuid(struct rx_call *rxcall, afsUUID *uuidp)
 {
     int code = 0;
     if (!afs_cb_inited)
@@ -142,63 +113,46 @@ SRXAFSCB_ProbeUuid(rxcall, uuidp)
 }
 
 afs_int32
-SRXAFSCB_GetServerPrefs(rxcall, serverIndex, srvrAddr, srvrRank)
-     struct rx_call *rxcall;
-     afs_int32 serverIndex;
-     afs_int32 *srvrAddr;
-     afs_int32 *srvrRank;
+SRXAFSCB_GetServerPrefs(struct rx_call *rxcall, afs_int32 serverIndex,
+                       afs_int32 *srvrAddr, afs_int32 *srvrRank)
 {
     return RXGEN_OPCODE;
 }
 
 
 afs_int32
-SRXAFSCB_GetCellServDB(rxcall, cellIndex, cellName, cellHosts)
-     struct rx_call *rxcall;
-     afs_int32 cellIndex;
-     char **cellName;
-     serverList *cellHosts;
+SRXAFSCB_GetCellServDB(struct rx_call *rxcall, afs_int32 cellIndex,
+                      char **cellName, serverList *cellHosts)
 {
     return RXGEN_OPCODE;
 }
 
 
 afs_int32
-SRXAFSCB_GetLocalCell(rxcall, cellName)
-     struct rx_call *rxcall;
-     char **cellName;
+SRXAFSCB_GetLocalCell(struct rx_call *rxcall, char **cellName)
 {
     return RXGEN_OPCODE;
 }
 
 
 afs_int32
-SRXAFSCB_GetCacheConfig(rxcall, callerVersion, serverVersion, configCount,
-                       config)
-     struct rx_call *rxcall;
-     afs_uint32 callerVersion;
-     afs_uint32 *serverVersion;
-     afs_uint32 *configCount;
-     cacheConfig *config;
+SRXAFSCB_GetCacheConfig(struct rx_call *rxcall, afs_uint32 callerVersion,
+                       afs_uint32 *serverVersion, afs_uint32 *configCount,
+                       cacheConfig *config)
 {
     return RXGEN_OPCODE;
 }
 
 afs_int32
-SRXAFSCB_GetCellByNum(rxcall, cellnum, cellname, cellhosts)
-     struct rx_call *rxcall;
-     afs_int32 cellnum;
-     char **cellname;
-     serverList *cellhosts;
+SRXAFSCB_GetCellByNum(struct rx_call *rxcall, afs_int32 cellnum,
+                     char **cellname, serverList *cellhosts)
 {
      return RXGEN_OPCODE;
 }
 
 afs_int32
-SRXAFSCB_TellMeAboutYourself(rxcall, addr, cap)
-     struct rx_call *rxcall;
-     struct interfaceAddr *addr;
-     Capabilities *cap;
+SRXAFSCB_TellMeAboutYourself(struct rx_call *rxcall,
+                            struct interfaceAddr *addr, Capabilities *cap)
 {
      return RXGEN_OPCODE;
 }
index dcf560a..ed86a12 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <afsconfig.h>
+
+#ifndef HAVE_VASNPRINTF
+int vasnprintf(char **ret, size_t max_sz, const char *format, va_list args);
+#endif
 
 #ifndef min
 #define min(a, b)               ((a) > (b) ? (b) : (a))