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