backup deletedump: Change -port to -portoffset
[openafs.git] / src / bucoord / commands.c
index 324510d..daeb791 100644 (file)
@@ -9,43 +9,32 @@
 
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
 #include <roken.h>
 
-#include <afs/stds.h>
-#if defined(AFS_LINUX24_ENV)
-#define _REGEX_RE_COMP
+#ifdef HAVE_POSIX_REGEX                /* use POSIX regexp library */
+#include <regex.h>
 #endif
-#include <sys/types.h>
+
 #include <afs/cmd.h>
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#else
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#endif
-#include <errno.h>
 #include <afs/com_err.h>
 #include <afs/afsutil.h>
 #include <afs/budb.h>
 #include <afs/budb_prototypes.h>
 #include <afs/butc.h>
 #include <afs/bubasics.h>      /* PA */
+#include <afs/afsint.h>
 #include <afs/volser.h>
 #include <afs/voldefs.h>       /* PA */
 #include <afs/vldbint.h>       /* PA */
 #include <afs/ktime.h>         /* PA */
 #include <ubik.h>
-#include <time.h>
 #include <lock.h>
-#include <afs/butc.h>
 #include <afs/tcdata.h>
 #include <afs/butx.h>
 #include <afs/vsutils_prototypes.h>
-#ifdef HAVE_POSIX_REGEX                /* use POSIX regexp library */
-#include <regex.h>
-#endif
+
 #include "bc.h"
 #include "error_macros.h"
 #include "bucoord_internal.h"
@@ -119,13 +108,12 @@ getSPEntries(afs_uint32 server, afs_int32 partition,
     }
     /* No server entry added. Add one */
     if (!(*ss)) {
-       *ss = (struct serversort *)malloc(sizeof(struct serversort));
+       *ss = calloc(1, sizeof(struct serversort));
        if (!(*ss)) {
            afs_com_err(whoami, BC_NOMEM, NULL);
            *ss = 0;
            return (BC_NOMEM);
        }
-       memset(*ss, 0, sizeof(struct serversort));
        (*ss)->ipaddr = server;
        (*ss)->next = *serverlist;
        *serverlist = *ss;
@@ -140,7 +128,7 @@ getSPEntries(afs_uint32 server, afs_int32 partition,
     }
     /* No partition entry added. Add one */
     if (!(*ps)) {
-       *ps = (struct partitionsort *)malloc(sizeof(struct partitionsort));
+       *ps = calloc(1, sizeof(struct partitionsort));
        if (!(*ps)) {
            afs_com_err(whoami, BC_NOMEM, NULL);
            free(*ss);
@@ -148,7 +136,6 @@ getSPEntries(afs_uint32 server, afs_int32 partition,
            *ss = 0;
            return (BC_NOMEM);
        }
-       memset(*ps, 0, sizeof(struct partitionsort));
        (*ps)->part = partition;
        (*ps)->next = (*ss)->partitions;
        (*ss)->partitions = *ps;
@@ -244,7 +231,7 @@ EvalVolumeSet2(struct bc_config *aconfig,
            bulkentries.nbulkentries_val = 0;
            nsi = -1;
            tcode =
-               ubik_Call(VL_ListAttributesN2, uclient, 0, &attributes,
+               ubik_VL_ListAttributesN2(uclient, 0, &attributes,
                          tve->name, si, &nentries, &bulkentries, &nsi);
            if (tcode)
                ERROR(tcode);
@@ -259,6 +246,11 @@ EvalVolumeSet2(struct bc_config *aconfig,
            if (nentries == 0)
                ERROR(RXGEN_OPCODE);    /* Use EvalVolumeSet1 */
 
+           if (nentries < 0)
+               nentries = 0;
+           if (nentries > bulkentries.nbulkentries_len)
+               nentries = bulkentries.nbulkentries_len;
+
            /* Step through each entry and add it to the list of volumes */
            entries = bulkentries.nbulkentries_val;
            for (e = 0; e < nentries; e++) {
@@ -316,15 +308,13 @@ EvalVolumeSet2(struct bc_config *aconfig,
 
                if (add) {
                    /* Allocate a volume dump structure and its name */
-                   tvd = (struct bc_volumeDump *)
-                       malloc(sizeof(struct bc_volumeDump));
+                   tvd = calloc(1, sizeof(struct bc_volumeDump));
                    if (!tvd) {
                        afs_com_err(whoami, BC_NOMEM, NULL);
                        ERROR(BC_NOMEM);
                    }
-                   memset(tvd, 0, sizeof(*tvd));
 
-                   tvd->name = (char *)malloc(strlen(entries[e].name) + 10);
+                   tvd->name = malloc(strlen(entries[e].name) + 10);
                    if (!(tvd->name)) {
                        afs_com_err(whoami, BC_NOMEM, NULL);
                        free(tvd);
@@ -360,7 +350,7 @@ EvalVolumeSet2(struct bc_config *aconfig,
 
            /* Free memory allocated during VL call */
            if (bulkentries.nbulkentries_val) {
-               free((char *)bulkentries.nbulkentries_val);
+               free(bulkentries.nbulkentries_val);
                bulkentries.nbulkentries_val = 0;
                entries = 0;
            }
@@ -373,7 +363,7 @@ EvalVolumeSet2(struct bc_config *aconfig,
 
   error_exit:
     if (bulkentries.nbulkentries_val) {
-       free((char *)bulkentries.nbulkentries_val);
+       free(bulkentries.nbulkentries_val);
     }
     return (code);
 }                              /*EvalVolumeSet2 */
@@ -456,8 +446,7 @@ EvalVolumeSet1(struct bc_config *aconfig,
      */
     for (index = 0; 1; index = next_index) {   /*w */
        memset(&entry, 0, sizeof(entry));
-       code = ubik_Call(VL_ListEntry,  /*Routine to invoke */
-                        uclient,       /*Ubik client structure */
+       code = ubik_VL_ListEntry(uclient,       /*Ubik client structure */
                         0,     /*Ubik flags */
                         index, /*Current index */
                         &count,        /*Ptr to working variable */
@@ -587,15 +576,13 @@ EvalVolumeSet1(struct bc_config *aconfig,
                }
 
                total++;
-               tvd = (struct bc_volumeDump *)
-                   malloc(sizeof(struct bc_volumeDump));
+               tvd = calloc(1, sizeof(struct bc_volumeDump));
                if (!tvd) {
                    afs_com_err(whoami, BC_NOMEM, NULL);
                    return (BC_NOMEM);
                }
-               memset(tvd, 0, sizeof(*tvd));
 
-               tvd->name = (char *)malloc(strlen(entry.name) + 10);
+               tvd->name = malloc(strlen(entry.name) + 10);
                if (!(tvd->name)) {
                    afs_com_err(whoami, BC_NOMEM, NULL);
                    free(tvd);
@@ -737,18 +724,15 @@ bc_FloatATOI(char *anum)
 char *
 bc_CopyString(char *astring)
 {
-    afs_int32 tlen;
     char *tp;
 
     if (!astring)
        return (NULL);          /* propagate null strings easily */
-    tlen = strlen(astring);
-    tp = (char *)malloc(tlen + 1);     /* don't forget the terminating null */
+    tp = strdup(astring);
     if (!tp) {
        afs_com_err(whoami, BC_NOMEM, NULL);
        return (tp);
     }
-    strcpy(tp, astring);
     return tp;
 }
 
@@ -776,7 +760,7 @@ concatParams(struct cmd_item *itemPtr)
        return (NULL);
     }
 
-    string = (char *)malloc(length);   /* allocate the string */
+    string = malloc(length);   /* allocate the string */
     if (!string) {
        afs_com_err(whoami, BC_NOMEM, NULL);
        return (NULL);
@@ -922,11 +906,10 @@ bc_WaitForNoJobs(void)
 {
     int i;
     int usefulJobRunning = 1;
+    int printWaiting = 1;
 
     extern dlqlinkT statusHead;
 
-    afs_com_err(whoami, 0, "waiting for job termination");
-
     while (usefulJobRunning) {
        usefulJobRunning = (dlqEmpty(&statusHead) ? 0 : 1);
        if (dispatchLock.excl_locked)
@@ -937,8 +920,13 @@ bc_WaitForNoJobs(void)
        }
 
        /* Wait 5 seconds and check again */
-       if (usefulJobRunning)
+       if (usefulJobRunning) {
+            if (printWaiting) {
+                afs_com_err(whoami, 0, "waiting for job termination");
+                printWaiting = 0;
+            }
            IOMGR_Sleep(5);
+        }
     }
     return (lastTaskCode);
 }
@@ -1174,33 +1162,28 @@ bc_VolRestoreCmd(struct cmd_syndesc *as, void *arock)
     }
 
     /* specified other destination host */
-    if (as->parms[0].items) {
-       tp = as->parms[0].items->data;
-       if (bc_ParseHost(tp, &destServ)) {
-           afs_com_err(whoami, 0, "Failed to locate destination host '%s'", tp);
-           return -1;
-       }
+    tp = as->parms[0].items->data;
+    if (bc_ParseHost(tp, &destServ)) {
+       afs_com_err(whoami, 0, "Failed to locate destination host '%s'", tp);
+       return -1;
     }
 
     /* specified other destination partition */
-    if (as->parms[1].items) {
-       tp = as->parms[1].items->data;
-       if (bc_GetPartitionID(tp, &destPartition)) {
-           afs_com_err(whoami, 0, "Can't parse destination partition '%s'", tp);
-           return -1;
-       }
+    tp = as->parms[1].items->data;
+    if (bc_GetPartitionID(tp, &destPartition)) {
+       afs_com_err(whoami, 0, "Can't parse destination partition '%s'", tp);
+       return -1;
     }
 
     for (ti = as->parms[2].items; ti; ti = ti->next) {
        /* build list of volume items */
-       tvol = (struct bc_volumeDump *)malloc(sizeof(struct bc_volumeDump));
+       tvol = calloc(1, sizeof(struct bc_volumeDump));
        if (!tvol) {
            afs_com_err(whoami, BC_NOMEM, NULL);
            return BC_NOMEM;
        }
-       memset(tvol, 0, sizeof(struct bc_volumeDump));
 
-       tvol->name = (char *)malloc(VOLSER_MAXVOLNAME + 1);
+       tvol->name = malloc(VOLSER_MAXVOLNAME + 1);
        if (!tvol->name) {
            afs_com_err(whoami, BC_NOMEM, NULL);
            return BC_NOMEM;
@@ -1240,7 +1223,7 @@ bc_VolRestoreCmd(struct cmd_syndesc *as, void *arock)
     if (as->parms[5].items) {
        for (ti = as->parms[5].items; ti; ti = ti->next)
            portCount++;
-       ports = (afs_int32 *) malloc(portCount * sizeof(afs_int32));
+       ports = malloc(portCount * sizeof(afs_int32));
        if (!ports) {
            afs_com_err(whoami, BC_NOMEM, NULL);
            return BC_NOMEM;
@@ -1370,7 +1353,7 @@ bc_DiskRestoreCmd(struct cmd_syndesc *as, void *arock)
     if (as->parms[2].items) {
        for (ti = as->parms[2].items; ti; ti = ti->next)
            portCount++;
-       ports = (afs_int32 *) malloc(portCount * sizeof(afs_int32));
+       ports = malloc(portCount * sizeof(afs_int32));
        if (!ports) {
            afs_com_err(whoami, BC_NOMEM, NULL);
            return BC_NOMEM;
@@ -1537,11 +1520,9 @@ bc_VolsetRestoreCmd(struct cmd_syndesc *as, void *arock)
            }
 
            /* Allocate a volumeDump structure and link it in */
-           tvol =
-               (struct bc_volumeDump *)malloc(sizeof(struct bc_volumeDump));
-           memset(tvol, 0, sizeof(struct bc_volumeDump));
+           tvol = calloc(1, sizeof(struct bc_volumeDump));
 
-           tvol->name = (char *)malloc(VOLSER_MAXVOLNAME + 1);
+           tvol->name = malloc(VOLSER_MAXVOLNAME + 1);
            if (!tvol->name) {
                afs_com_err(whoami, BC_NOMEM, NULL);
                return BC_NOMEM;
@@ -1567,7 +1548,7 @@ bc_VolsetRestoreCmd(struct cmd_syndesc *as, void *arock)
     if (as->parms[2].items) {
        for (ti = as->parms[2].items; ti; ti = ti->next)
            portCount++;
-       ports = (afs_int32 *) malloc(portCount * sizeof(afs_int32));
+       ports = malloc(portCount * sizeof(afs_int32));
        if (!ports) {
            afs_com_err(whoami, BC_NOMEM, NULL);
            return BC_NOMEM;
@@ -1722,7 +1703,7 @@ bc_DumpCmd(struct cmd_syndesc *as, void *arock)
 
        /* get the port number, if one was specified */
        if (as->parms[2].items) {
-           portp = (afs_int32 *) malloc(sizeof(afs_int32));
+           portp = malloc(sizeof(afs_int32));
            if (!portp) {
                afs_com_err(whoami, BC_NOMEM, NULL);
                return BC_NOMEM;
@@ -1803,7 +1784,7 @@ bc_DumpCmd(struct cmd_syndesc *as, void *arock)
            sprintf(statusPtr->taskName, "Scheduled Dump");
            statusPtr->jobNumber = bc_jobNumber();
            statusPtr->scheduledDump = atTime;
-           statusPtr->cmdLine = (char *)malloc(length);
+           statusPtr->cmdLine = malloc(length);
            if (!statusPtr->cmdLine) {
                afs_com_err(whoami, BC_NOMEM, NULL);
                return BC_NOMEM;
@@ -1855,12 +1836,11 @@ bc_DumpCmd(struct cmd_syndesc *as, void *arock)
      * global variables so this can take place in main.
      */
     if (loadfile) {
-       loadFile = (char *)malloc(strlen(as->parms[6].items->data) + 1);
+       loadFile = strdup(as->parms[6].items->data);
        if (!loadFile) {
            afs_com_err(whoami, BC_NOMEM, NULL);
            return BC_NOMEM;
        }
-       strcpy(loadFile, as->parms[6].items->data);
        return 0;
     }
 
@@ -2278,7 +2258,7 @@ bc_dbVerifyCmd(struct cmd_syndesc *as, void *arock)
     detail = (as->parms[0].items ? 1 : 0);     /* print more details */
 
     code =
-       ubik_Call(BUDB_DbVerify, udbHandle.uh_client, 0, &status, &orphans,
+       ubik_BUDB_DbVerify(udbHandle.uh_client, 0, &status, &orphans,
                  &host);
 
     if (code) {
@@ -2465,7 +2445,7 @@ bc_deleteDumpCmd(struct cmd_syndesc *as, void *arock)
        havetime = 1;
     }
 
-    port = (as->parms[3].items ? getPortOffset(as->parms[3].items->data) : 0); /* -port */
+    port = (as->parms[3].items ? getPortOffset(as->parms[3].items->data) : 0); /* -portoffset */
     if (as->parms[5].items)    /* -dbonly */
        port = -1;
 
@@ -2475,7 +2455,12 @@ bc_deleteDumpCmd(struct cmd_syndesc *as, void *arock)
     if (havegroupid)
        groupId = atoi(as->parms[4].items->data);
 
-    noexecute = (as->parms[7].items ? 1 : 0);
+    if (as->parms[7].items || as->parms[8].items) {
+        /* -noexecute (hidden) or -dryrun used */
+       noexecute = 1;
+    } else {
+       noexecute = 0;
+    }
 
     /* Get the time to delete to */
     if (as->parms[2].items) {  /* -to */
@@ -2764,12 +2749,10 @@ DBLookupByVolume(char *volumeName)
            for (i = 0; i < numEntries; i++) {  /*f */
                struct dumpedVol *insPtr, **prevPtr;
 
-               tempPtr =
-                   (struct dumpedVol *)malloc(sizeof(struct dumpedVol));
+               tempPtr = calloc(1, sizeof(struct dumpedVol));
                if (!tempPtr)
                    ERROR(BC_NOMEM);
 
-               memset(tempPtr, 0, sizeof(*tempPtr));
                tempPtr->incTime = volumeEntry[i].clone;
                tempPtr->dumpID = volumeEntry[i].dump;
                strncpy(tempPtr->tapeName, volumeEntry[i].tape,
@@ -2922,13 +2905,12 @@ dumpInfo(afs_int32 dumpid, afs_int32 detailFlag)
 
     /* now get the list of tapes */
     for (tapeNumber = dumpEntry.tapes.b; tapeNumber <= dumpEntry.tapes.maxTapes; tapeNumber++) {       /*f */
-       tapeLinkPtr = (struct tapeLink *)malloc(sizeof(struct tapeLink));
+       tapeLinkPtr = calloc(1, sizeof(struct tapeLink));
        if (!tapeLinkPtr) {
            afs_com_err(whoami, BC_NOMEM, NULL);
            ERROR(BC_NOMEM);
        }
 
-       memset(tapeLinkPtr, 0, sizeof(*tapeLinkPtr));
        code = bcdb_FindTapeSeq(dumpid, tapeNumber, &tapeLinkPtr->tapeEntry);
        if (code) {
            code = 0;
@@ -2973,13 +2955,11 @@ dumpInfo(afs_int32 dumpid, afs_int32 detailFlag)
            for (i = 0; i < vl.budb_volumeList_len; i++) {
                link = &tapeLinkPtr->firstVolume;
 
-               volumeLinkPtr =
-                   (struct volumeLink *)malloc(sizeof(struct volumeLink));
+               volumeLinkPtr = calloc(1, sizeof(struct volumeLink));
                if (!volumeLinkPtr) {
                    afs_com_err(whoami, BC_NOMEM, NULL);
                    ERROR(BC_NOMEM);
                }
-               memset(volumeLinkPtr, 0, sizeof(*volumeLinkPtr));
 
                memcpy(&volumeLinkPtr->volumeEntry,
                       &vl.budb_volumeList_val[i],
@@ -3093,7 +3073,7 @@ printRecentDumps(int ndumps)
        dl.budb_dumpList_val = 0;
 
        /* outline algorithm */
-       code = ubik_Call(BUDB_GetDumps, udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_NPREVIOUS, "",       /* no name */
+       code = ubik_BUDB_GetDumps(udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_NPREVIOUS, "",     /* no name */
                         0,     /* start */
                         ndumps,        /* end */
                         index, /* index */