* src/test.c: converted to new XPath APIs

* src/xml.c: fixed a return comment
Daniel
This commit is contained in:
Daniel Veillard 2007-04-06 15:34:09 +00:00
parent 4bdac20ad6
commit 73dd7f0ff5
3 changed files with 107 additions and 152 deletions

View File

@ -1,3 +1,8 @@
Fri Apr 6 17:33:13 CEST 2007 Daniel Veillard <veillard@redhat.com>
* src/test.c: converted to new XPath APIs
* src/xml.c: fixed a return comment
Fri Apr 6 14:27:13 CEST 2007 Daniel Veillard <veillard@redhat.com> Fri Apr 6 14:27:13 CEST 2007 Daniel Veillard <veillard@redhat.com>
* src/xml.[ch]: first patch to clean up XPath accesses with new * src/xml.[ch]: first patch to clean up XPath accesses with new

View File

@ -261,7 +261,6 @@ static int testLoadDomain(virConnectPtr conn,
xmlDocPtr xml) { xmlDocPtr xml) {
xmlNodePtr root = NULL; xmlNodePtr root = NULL;
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
xmlXPathObjectPtr obj = NULL;
char *name = NULL; char *name = NULL;
unsigned char rawuuid[VIR_UUID_BUFLEN]; unsigned char rawuuid[VIR_UUID_BUFLEN];
char *dst_uuid; char *dst_uuid;
@ -270,8 +269,9 @@ static int testLoadDomain(virConnectPtr conn,
unsigned long memory = 0; unsigned long memory = 0;
unsigned long maxMem = 0; unsigned long maxMem = 0;
int nrVirtCpu; int nrVirtCpu;
char *conv; char *str;
int handle = -1, i; int handle = -1, i, ret;
long l;
virDomainRestart onReboot = VIR_DOMAIN_RESTART; virDomainRestart onReboot = VIR_DOMAIN_RESTART;
virDomainRestart onPoweroff = VIR_DOMAIN_DESTROY; virDomainRestart onPoweroff = VIR_DOMAIN_DESTROY;
virDomainRestart onCrash = VIR_DOMAIN_RENAME_RESTART; virDomainRestart onCrash = VIR_DOMAIN_RENAME_RESTART;
@ -294,102 +294,81 @@ static int testLoadDomain(virConnectPtr conn,
goto error; goto error;
} }
obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt); name = virXPathString("string(/domain/name[1])", ctxt);
if ((obj == NULL) || (obj->type != XPATH_STRING) || if (name == NULL) {
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
testError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("domain name")); testError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("domain name"));
goto error; goto error;
} }
name = strdup((const char *)obj->stringval);
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "string(/domain/uuid[1])", ctxt); str = virXPathString("string(/domain/uuid[1])", ctxt);
if ((obj == NULL) || (obj->type != XPATH_STRING) || if (str == NULL) {
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid")); testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
goto error; goto error;
} }
dst_uuid = (char *) &rawuuid[0]; dst_uuid = (char *) &rawuuid[0];
if (!(virParseUUID((char **)&dst_uuid, (const char *)obj->stringval))) { if (!(virParseUUID((char **)&dst_uuid, str))) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid")); testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain uuid"));
goto error; goto error;
} }
xmlXPathFreeObject(obj); free(str);
obj = xmlXPathEval(BAD_CAST "string(/domain/memory[1])", ctxt);
if ((obj == NULL) || (obj->type != XPATH_STRING) || ret = virXPathLong("string(/domain/memory[1])", ctxt, &l);
(obj->stringval == NULL) || (obj->stringval[0] == 0)) { if (ret != 0) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain memory")); testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain memory"));
goto error; goto error;
} }
maxMem = strtoll((const char*)obj->stringval, &conv, 10); maxMem = l;
if (conv == (const char*)obj->stringval) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain memory"));
goto error;
}
xmlXPathFreeObject(obj);
ret = virXPathLong("string(/domain/currentMemory[1])", ctxt, &l);
obj = xmlXPathEval(BAD_CAST "string(/domain/currentMemory[1])", ctxt); if (ret == -1) {
if ((obj == NULL) || (obj->type != XPATH_STRING) ||
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
memory = maxMem; memory = maxMem;
} else if (ret == -2) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain current memory"));
goto error;
} else { } else {
memory = strtoll((const char*)obj->stringval, &conv, 10); memory = l;
if (conv == (const char*)obj->stringval) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain current memory"));
goto error;
}
} }
if (obj)
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "string(/domain/vcpu[1])", ctxt); ret = virXPathLong("string(/domain/vcpu[1])", ctxt, &l);
if ((obj == NULL) || (obj->type != XPATH_STRING) || if (ret == -1) {
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
nrVirtCpu = 1; nrVirtCpu = 1;
} else if (ret == -2) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain vcpus"));
goto error;
} else { } else {
nrVirtCpu = strtoll((const char*)obj->stringval, &conv, 10); nrVirtCpu = l;
if (conv == (const char*)obj->stringval) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain vcpus"));
goto error;
}
} }
if (obj)
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "string(/domain/on_reboot[1])", ctxt); str = virXPathString("string(/domain/on_reboot[1])", ctxt);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (str != NULL) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { if (!(onReboot = testRestartStringToFlag(str))) {
if (!(onReboot = testRestartStringToFlag((const char *)obj->stringval))) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain reboot behaviour")); testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain reboot behaviour"));
free(str);
goto error; goto error;
} }
free(str);
} }
if (obj)
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "string(/domain/on_poweroff[1])", ctxt); str = virXPathString("string(/domain/on_poweroff[1])", ctxt);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (str != NULL) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { if (!(onReboot = testRestartStringToFlag(str))) {
if (!(onReboot = testRestartStringToFlag((const char *)obj->stringval))) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain poweroff behaviour")); testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain poweroff behaviour"));
free(str);
goto error; goto error;
} }
free(str);
} }
if (obj)
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "string(/domain/on_crash[1])", ctxt); str = virXPathString("string(/domain/on_crash[1])", ctxt);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (str != NULL) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { if (!(onReboot = testRestartStringToFlag(str))) {
if (!(onReboot = testRestartStringToFlag((const char *)obj->stringval))) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain crash behaviour")); testError(conn, NULL, VIR_ERR_XML_ERROR, _("domain crash behaviour"));
free(str);
goto error; goto error;
} }
free(str);
} }
if (obj)
xmlXPathFreeObject(obj);
priv = (testPrivatePtr) conn->privateData; priv = (testPrivatePtr) conn->privateData;
con = &node->connections[priv->handle]; con = &node->connections[priv->handle];
@ -427,8 +406,6 @@ static int testLoadDomain(virConnectPtr conn,
return (0); return (0);
error: error:
if (obj)
xmlXPathFreeObject(obj);
if (name) if (name)
free(name); free(name);
return (-1); return (-1);
@ -539,11 +516,13 @@ static char *testBuildFilename(const char *relativeTo,
static int testOpenFromFile(virConnectPtr conn, static int testOpenFromFile(virConnectPtr conn,
int connid, int connid,
const char *file) { const char *file) {
int fd, i; int fd, i, ret;
long l;
char *str;
xmlDocPtr xml; xmlDocPtr xml;
xmlNodePtr root = NULL; xmlNodePtr root = NULL;
xmlNodePtr *domains;
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
xmlXPathObjectPtr obj = NULL;
virNodeInfoPtr nodeInfo; virNodeInfoPtr nodeInfo;
testPrivatePtr priv = (testPrivatePtr) conn->privateData; testPrivatePtr priv = (testPrivatePtr) conn->privateData;
@ -579,108 +558,79 @@ static int testOpenFromFile(virConnectPtr conn,
memmove(&node->connections[connid].nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo)); memmove(&node->connections[connid].nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
nodeInfo = &node->connections[connid].nodeInfo; nodeInfo = &node->connections[connid].nodeInfo;
obj = xmlXPathEval(BAD_CAST "string(/node/cpu/nodes[1])", ctxt); ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (ret == 0) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { nodeInfo->nodes = l;
char *conv = NULL; } else if (ret == -2) {
nodeInfo->nodes = strtol((const char*)obj->stringval, &conv, 10); testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu numa nodes"));
if (conv == (const char*)obj->stringval) { goto error;
testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu numa nodes"));
goto error;
}
xmlXPathFreeObject(obj);
} }
obj = xmlXPathEval(BAD_CAST "string(/node/cpu/sockets[1])", ctxt); ret = virXPathLong("string(/node/cpu/sockets[1])", ctxt, &l);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (ret == 0) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { nodeInfo->sockets = l;
char *conv = NULL; } else if (ret == -2) {
nodeInfo->sockets = strtol((const char*)obj->stringval, &conv, 10); testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu sockets"));
if (conv == (const char*)obj->stringval) { goto error;
testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu sockets"));
goto error;
}
xmlXPathFreeObject(obj);
} }
obj = xmlXPathEval(BAD_CAST "string(/node/cpu/cores[1])", ctxt); ret = virXPathLong("string(/node/cpu/cores[1])", ctxt, &l);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (ret == 0) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { nodeInfo->cores = l;
char *conv = NULL; } else if (ret == -2) {
nodeInfo->cores = strtol((const char*)obj->stringval, &conv, 10); testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu cores"));
if (conv == (const char*)obj->stringval) { goto error;
testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu cores"));
goto error;
}
xmlXPathFreeObject(obj);
} }
obj = xmlXPathEval(BAD_CAST "string(/node/cpu/threads[1])", ctxt); ret = virXPathLong("string(/node/cpu/threads[1])", ctxt, &l);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (ret == 0) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { nodeInfo->threads = l;
char *conv = NULL; } else if (ret == -2) {
nodeInfo->threads = strtol((const char*)obj->stringval, &conv, 10); testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu threads"));
if (conv == (const char*)obj->stringval) { goto error;
testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu threads"));
goto error;
}
xmlXPathFreeObject(obj);
} }
nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes; nodeInfo->cpus = nodeInfo->cores * nodeInfo->threads * nodeInfo->sockets * nodeInfo->nodes;
obj = xmlXPathEval(BAD_CAST "string(/node/cpu/active[1])", ctxt); ret = virXPathLong("string(/node/cpu/active[1])", ctxt, &l);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (ret == 0) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { if (l < nodeInfo->cpus) {
char *conv = NULL; nodeInfo->cpus = l;
unsigned int active = strtol((const char*)obj->stringval, &conv, 10); }
if (conv == (const char*)obj->stringval) { } else if (ret == -2) {
testError(conn, NULL, VIR_ERR_XML_ERROR, _("node active cpu")); testError(conn, NULL, VIR_ERR_XML_ERROR, _("node active cpu"));
goto error; goto error;
}
if (active < nodeInfo->cpus) {
nodeInfo->cpus = active;
}
xmlXPathFreeObject(obj);
} }
obj = xmlXPathEval(BAD_CAST "string(/node/cpu/mhz[1])", ctxt); ret = virXPathLong("string(/node/cpu/mhz[1])", ctxt, &l);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (ret == 0) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { nodeInfo->mhz = l;
char *conv = NULL; } else if (ret == -2) {
nodeInfo->mhz = strtol((const char*)obj->stringval, &conv, 10); testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu mhz"));
if (conv == (const char*)obj->stringval) { goto error;
testError(conn, NULL, VIR_ERR_XML_ERROR, _("node cpu mhz"));
goto error;
}
xmlXPathFreeObject(obj);
} }
obj = xmlXPathEval(BAD_CAST "string(/node/cpu/model[1])", ctxt);
if ((obj != NULL) && (obj->type == XPATH_STRING) && str = virXPathString("string(/node/cpu/model[1])", ctxt);
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { if (str != NULL) {
strncpy(nodeInfo->model, (const char *)obj->stringval, sizeof(nodeInfo->model)-1); strncpy(nodeInfo->model, str, sizeof(nodeInfo->model)-1);
nodeInfo->model[sizeof(nodeInfo->model)-1] = '\0'; nodeInfo->model[sizeof(nodeInfo->model)-1] = '\0';
xmlXPathFreeObject(obj); free(str);
} }
obj = xmlXPathEval(BAD_CAST "string(/node/memory[1])", ctxt); ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
if ((obj != NULL) && (obj->type == XPATH_STRING) && if (ret == 0) {
(obj->stringval != NULL) && (obj->stringval[0] != 0)) { nodeInfo->memory = l;
char *conv = NULL; } else if (ret == -2) {
nodeInfo->memory = strtol((const char*)obj->stringval, &conv, 10); testError(conn, NULL, VIR_ERR_XML_ERROR, _("node memory"));
if (conv == (const char*)obj->stringval) { goto error;
testError(conn, NULL, VIR_ERR_XML_ERROR, _("node memory"));
goto error;
}
xmlXPathFreeObject(obj);
} }
obj = xmlXPathEval(BAD_CAST "/node/domain", ctxt); ret = virXPathNodeSet("/node/domain", ctxt, &domains);
if ((obj == NULL) || (obj->type != XPATH_NODESET) || if (ret < 0) {
(obj->nodesetval == NULL)) {
testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node domain list")); testError(NULL, NULL, VIR_ERR_XML_ERROR, _("node domain list"));
goto error; goto error;
} }
for (i = 0 ; i < obj->nodesetval->nodeNr ; i++) { for (i = 0 ; i < ret ; i++) {
xmlChar *domFile = xmlGetProp(obj->nodesetval->nodeTab[i], BAD_CAST "file"); xmlChar *domFile = xmlGetProp(domains[i], BAD_CAST "file");
char *absFile = testBuildFilename(file, (const char *)domFile); char *absFile = testBuildFilename(file, (const char *)domFile);
int domid = nextDomID++; int domid = nextDomID++;
free(domFile); free(domFile);
@ -695,8 +645,9 @@ static int testOpenFromFile(virConnectPtr conn,
free(absFile); free(absFile);
node->connections[connid].numDomains++; node->connections[connid].numDomains++;
} }
if (domains != NULL)
free(domains);
xmlXPathFreeObject(obj);
xmlFreeDoc(xml); xmlFreeDoc(xml);
return (0); return (0);
@ -709,8 +660,6 @@ static int testOpenFromFile(virConnectPtr conn,
node->connections[connid].numDomains = 0; node->connections[connid].numDomains = 0;
node->connections[connid].active = 0; node->connections[connid].active = 0;
} }
if (obj)
xmlXPathFreeObject(obj);
if (xml) if (xml)
xmlFreeDoc(xml); xmlFreeDoc(xml);
if (fd != -1) if (fd != -1)

View File

@ -120,7 +120,8 @@ virXPathNumber(const char *xpath, xmlXPathContextPtr ctxt, double *value) {
* Convenience function to evaluate an XPath number * Convenience function to evaluate an XPath number
* *
* Returns 0 in case of success in which case @value is set, * Returns 0 in case of success in which case @value is set,
* or -1 if the evaluation failed. * or -1 if the XPath evaluation failed or -2 if the
* value doesn't have a long format.
*/ */
int int
virXPathLong(const char *xpath, xmlXPathContextPtr ctxt, long *value) { virXPathLong(const char *xpath, xmlXPathContextPtr ctxt, long *value) {