From: Peter Krempa Date: Thu, 15 Sep 2011 11:51:01 +0000 (+0200) Subject: virsh: Allow using complete elements with cpu-baseline X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=b0889eae6a526f6097dd397d5ee68bb78a1f454b;p=libvirt.git virsh: Allow using complete elements with cpu-baseline This patch cleans the cpu baseline function using new libvirt helper functions and fixes XPath expression that selects elements from the source file, that can contain concatenated XMLs, domain XMLs and bare elements. The fixed XPath expression ensures not to select NUMA conn)) return false; @@ -12025,69 +12009,57 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd) if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) return false; - doc = xmlNewDoc(NULL); - if (doc == NULL) + /* add an separate container around the xml */ + virBufferStrcat(&buf, "", buffer, "", NULL); + if (virBufferError(&buf)) goto no_memory; - res = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0, - (const xmlChar *)buffer, &node_list); - if (res != 0) { - vshError(ctl, _("Failed to parse XML fragment %s"), from); - ret = false; + VIR_FREE(buffer); + buffer = virBufferContentAndReset(&buf); + + + if (!(xml = virXMLParseStringCtxt(buffer, from, &ctxt))) + goto cleanup; + + if ((count = virXPathNodeSet("//cpu[not(ancestor::cpus)]", + ctxt, &node_list)) == -1) + goto cleanup; + + if (count == 0) { + vshError(ctl, _("No host CPU specified in '%s'"), from); goto cleanup; } - xmlAddChildList((xmlNodePtr) doc, node_list); + list = vshCalloc(ctl, count, sizeof(const char *)); - ctxt = xmlXPathNewContext(doc); - if (!ctxt) + if (!(xml_buf = xmlBufferCreate())) goto no_memory; - obj = xmlXPathEval(BAD_CAST "//cpu[not(ancestor::cpu)]", ctxt); - if ((obj == NULL) || (obj->nodesetval == NULL) || - (obj->nodesetval->nodeTab == NULL)) - goto cleanup; + for (i = 0; i < count; i++) { + xmlBufferEmpty(xml_buf); - for (i = 0;i < obj->nodesetval->nodeNr;i++) { - buf = xmlBufferCreate(); - if (buf == NULL) - goto no_memory; - sctxt = xmlSaveToBuffer(buf, NULL, 0); - if (sctxt == NULL) { - xmlBufferFree(buf); - goto no_memory; + if (xmlNodeDump(xml_buf, xml, node_list[i], 0, 0) < 0) { + vshError(ctl, _("Failed to extract element")); + goto cleanup; } - xmlSaveTree(sctxt, obj->nodesetval->nodeTab[i]); - xmlSaveClose(sctxt); - - list = vshRealloc(ctl, list, sizeof(char *) * (count + 1)); - list[count++] = (char *) buf->content; - buf->content = NULL; - xmlBufferFree(buf); - buf = NULL; - } - - if (count == 0) { - vshError(ctl, _("No host CPU specified in '%s'"), from); - ret = false; - goto cleanup; + list[i] = vshStrdup(ctl, (const char *)xmlBufferContent(xml_buf)); } result = virConnectBaselineCPU(ctl->conn, list, count, 0); - if (result) + if (result) { vshPrint(ctl, "%s", result); - else - ret = false; + ret = true; + } cleanup: - xmlXPathFreeObject(obj); xmlXPathFreeContext(ctxt); - xmlFreeDoc(doc); + xmlFreeDoc(xml); + xmlBufferFree(xml_buf); VIR_FREE(result); if ((list != NULL) && (count > 0)) { - for (i = 0;i < count;i++) + for (i = 0; i < count; i++) VIR_FREE(list[i]); } VIR_FREE(list);