From 502cf493c7832a3ea5a4cd20dea289d291c7c56b Mon Sep 17 00:00:00 2001 Message-Id: <502cf493c7832a3ea5a4cd20dea289d291c7c56b@dist-git> From: Peter Krempa Date: Thu, 22 Jan 2015 15:53:43 +0100 Subject: [PATCH] virsh-domain: use virXMLNodeToString instead of xmlNodeDump https://bugzilla.redhat.com/show_bug.cgi?id=1184929 (cherry picked from commit 3df33d7ad52d27bf9a41ab07e2c0ab631364ae04) Conflicts: tools/virsh-domain.c - context Signed-off-by: Jiri Denemark --- tools/virsh-domain.c | 113 +++++++++++++-------------------------------------- 1 file changed, 29 insertions(+), 84 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 03f96fb..6076e8d 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -2356,7 +2356,7 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd) xmlXPathContextPtr ctxt = NULL; xmlXPathObjectPtr obj = NULL; xmlNodePtr cur = NULL; - xmlBufferPtr xml_buf = NULL; + char *xml_buf = NULL; if (!(dom = vshCommandOptDomain(ctl, cmd, NULL))) return false; @@ -2461,18 +2461,13 @@ hit: goto cleanup; } - xml_buf = xmlBufferCreate(); - if (!xml_buf) { - vshError(ctl, _("Failed to allocate memory")); - goto cleanup; - } - - if (xmlNodeDump(xml_buf, xml, obj->nodesetval->nodeTab[i], 0, 0) < 0) { + if (!(xml_buf = virXMLNodeToString(xml, obj->nodesetval->nodeTab[i]))) { + vshSaveLibvirtError(); vshError(ctl, _("Failed to create XML")); goto cleanup; } - if (virDomainUpdateDeviceFlags(dom, (char *)xmlBufferContent(xml_buf), flags) < 0) { + if (virDomainUpdateDeviceFlags(dom, xml_buf, flags) < 0) { vshError(ctl, _("Failed to update interface link state")); goto cleanup; } else { @@ -2484,10 +2479,8 @@ cleanup: xmlXPathFreeObject(obj); xmlXPathFreeContext(ctxt); xmlFreeDoc(xml); - xmlBufferFree(xml_buf); - - if (dom) - virDomainFree(dom); + VIR_FREE(xml_buf); + virDomainFree(dom); return ret; } @@ -6092,11 +6085,10 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd) bool ret = false; char *buffer; int result; - const char *snippet; + char *snippet = NULL; xmlDocPtr xml = NULL; xmlXPathContextPtr ctxt = NULL; - xmlBufferPtr xml_buf = NULL; xmlNodePtr node; if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) @@ -6112,17 +6104,10 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd) if ((node = virXPathNode("/cpu|" "/domain/cpu|" "/capabilities/host/cpu", ctxt))) { - if (!(xml_buf = xmlBufferCreate())) { - vshError(ctl, _("Can't create XML buffer to extract CPU element.")); + if (!(snippet = virXMLNodeToString(xml, node))) { + vshSaveLibvirtError(); goto cleanup; } - - if (xmlNodeDump(xml_buf, xml, node, 0, 0) < 0) { - vshError(ctl, _("Failed to extract CPU element snippet from domain XML.")); - goto cleanup; - } - - snippet = (const char *) xmlBufferContent(xml_buf); } else { vshError(ctl, _("File '%s' does not contain a element or is not " "a valid domain or capabilities XML"), from); @@ -6158,7 +6143,7 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd) cleanup: VIR_FREE(buffer); - xmlBufferFree(xml_buf); + VIR_FREE(snippet); xmlXPathFreeContext(ctxt); xmlFreeDoc(xml); @@ -6200,7 +6185,6 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd) xmlDocPtr xml = NULL; xmlNodePtr *node_list = NULL; xmlXPathContextPtr ctxt = NULL; - xmlBufferPtr xml_buf = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; size_t i; @@ -6233,18 +6217,11 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd) list = vshCalloc(ctl, count, sizeof(const char *)); - if (!(xml_buf = xmlBufferCreate())) - goto no_memory; - for (i = 0; i < count; i++) { - xmlBufferEmpty(xml_buf); - - if (xmlNodeDump(xml_buf, xml, node_list[i], 0, 0) < 0) { - vshError(ctl, _("Failed to extract element")); + if (!(list[i] = virXMLNodeToString(xml, node_list[i]))) { + vshSaveLibvirtError(); goto cleanup; } - - list[i] = vshStrdup(ctl, (const char *)xmlBufferContent(xml_buf)); } result = virConnectBaselineCPU(ctl->conn, list, count, 0); @@ -6257,7 +6234,6 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd) cleanup: xmlXPathFreeContext(ctxt); xmlFreeDoc(xml); - xmlBufferFree(xml_buf); VIR_FREE(result); if (list != NULL && count > 0) { for (i = 0; i < count; i++) @@ -9633,7 +9609,7 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd) xmlXPathObjectPtr obj=NULL; xmlXPathContextPtr ctxt = NULL; xmlNodePtr cur = NULL, matchNode = NULL; - xmlBufferPtr xml_buf = NULL; + char *detach_xml = NULL; const char *mac =NULL, *type = NULL; char *doc = NULL; char buf[64]; @@ -9726,25 +9702,16 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd) goto cleanup; } - hit: - xml_buf = xmlBufferCreate(); - if (!xml_buf) { - vshError(ctl, "%s", _("Failed to allocate memory")); +hit: + if (!(detach_xml = virXMLNodeToString(xml, matchNode))) { + vshSaveLibvirtError(); goto cleanup; } - if (xmlNodeDump(xml_buf, xml, matchNode, 0, 0) < 0) { - vshError(ctl, "%s", _("Failed to create XML")); - goto cleanup; - } - - if (flags != 0 || current) { - ret = virDomainDetachDeviceFlags(dom, - (char *)xmlBufferContent(xml_buf), - flags); - } else { - ret = virDomainDetachDevice(dom, (char *)xmlBufferContent(xml_buf)); - } + if (flags != 0) + ret = virDomainDetachDeviceFlags(dom, detach_xml, flags); + else + ret = virDomainDetachDevice(dom, detach_xml); if (ret != 0) { vshError(ctl, "%s", _("Failed to detach interface")); @@ -9753,13 +9720,13 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd) functionReturn = true; } - cleanup: +cleanup: VIR_FREE(doc); + VIR_FREE(detach_xml); virDomainFree(dom); xmlXPathFreeObject(obj); xmlXPathFreeContext(ctxt); xmlFreeDoc(xml); - xmlBufferFree(xml_buf); return functionReturn; } @@ -9877,7 +9844,6 @@ vshPrepareDiskXML(xmlNodePtr disk_node, int type) { xmlNodePtr cur = NULL; - xmlBufferPtr xml_buf = NULL; const char *disk_type = NULL; const char *device_type = NULL; xmlNodePtr new_node = NULL; @@ -9886,12 +9852,6 @@ vshPrepareDiskXML(xmlNodePtr disk_node, if (!disk_node) return NULL; - xml_buf = xmlBufferCreate(); - if (!xml_buf) { - vshError(NULL, "%s", _("Failed to allocate memory")); - return NULL; - } - device_type = virXMLPropString(disk_node, "device"); if (STREQ_NULLABLE(device_type, "cdrom") || @@ -9913,7 +9873,7 @@ vshPrepareDiskXML(xmlNodePtr disk_node, if (type == VSH_PREPARE_DISK_XML_EJECT) { vshError(NULL, _("The disk device '%s' doesn't have media"), path); - goto error; + goto cleanup; } if (source) { @@ -9925,10 +9885,10 @@ vshPrepareDiskXML(xmlNodePtr disk_node, xmlAddChild(disk_node, new_node); } else if (type == VSH_PREPARE_DISK_XML_INSERT) { vshError(NULL, _("No source is specified for inserting media")); - goto error; + goto cleanup; } else if (type == VSH_PREPARE_DISK_XML_UPDATE) { vshError(NULL, _("No source is specified for updating media")); - goto error; + goto cleanup; } } @@ -9936,7 +9896,7 @@ vshPrepareDiskXML(xmlNodePtr disk_node, if (type == VSH_PREPARE_DISK_XML_INSERT) { vshError(NULL, _("The disk device '%s' already has media"), path); - goto error; + goto cleanup; } /* Remove the source if it tends to eject/update media. */ @@ -9952,30 +9912,15 @@ vshPrepareDiskXML(xmlNodePtr disk_node, } } - if (xmlNodeDump(xml_buf, NULL, disk_node, 0, 0) < 0) { - vshError(NULL, "%s", _("Failed to create XML")); - goto error; + if (!(ret = virXMLNodeToString(NULL, disk_node))) { + vshSaveLibvirtError(); + goto cleanup; } - goto cleanup; - cleanup: VIR_FREE(device_type); VIR_FREE(disk_type); - if (xml_buf) { - int len = xmlBufferLength(xml_buf); - if (VIR_ALLOC_N(ret, len + 1) < 0) - return NULL; - memcpy(ret, (char *)xmlBufferContent(xml_buf), len); - ret[len] = '\0'; - xmlBufferFree(xml_buf); - } return ret; - -error: - xmlBufferFree(xml_buf); - xml_buf = NULL; - goto cleanup; } -- 2.2.1