[libvirt] [PATCH 07/14] virsh-domain: use virXMLNodeDump instead of xmlNodeDump

Peter Krempa pkrempa at redhat.com
Tue Sep 10 10:15:43 UTC 2013


---
 tools/virsh-domain.c | 117 ++++++++++++++-------------------------------------
 1 file changed, 31 insertions(+), 86 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 727a42a..0d00440 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -2359,7 +2359,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;
@@ -2464,18 +2464,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 = virXMLNodeDump(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 {
@@ -2487,10 +2482,8 @@ cleanup:
     xmlXPathFreeObject(obj);
     xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
-    xmlBufferFree(xml_buf);
-
-    if (dom)
-        virDomainFree(dom);
+    VIR_FREE(xml_buf);
+    virDomainFree(dom);

     return ret;
 }
@@ -6080,11 +6073,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)
@@ -6100,17 +6092,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."));
-            goto cleanup;
-        }
-
-        if (xmlNodeDump(xml_buf, xml, node, 0, 0) < 0) {
-            vshError(ctl, _("Failed to extract CPU element snippet from domain XML."));
+        if (!(snippet = virXMLNodeDump(xml, node))) {
+            vshSaveLibvirtError();
             goto cleanup;
         }
-
-        snippet = (const char *) xmlBufferContent(xml_buf);
     } else {
         vshError(ctl, _("File '%s' does not contain a <cpu> element or is not "
                         "a valid domain or capabilities XML"), from);
@@ -6146,7 +6131,7 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd)

 cleanup:
     VIR_FREE(buffer);
-    xmlBufferFree(xml_buf);
+    VIR_FREE(snippet);
     xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);

@@ -6193,7 +6178,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;

@@ -6229,18 +6213,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 <cpu> element"));
+        if (!(list[i] = virXMLNodeDump(xml, node_list[i]))) {
+            vshSaveLibvirtError();
             goto cleanup;
         }
-
-        list[i] = vshStrdup(ctl, (const char *)xmlBufferContent(xml_buf));
     }

     result = virConnectBaselineCPU(ctl->conn,
@@ -6254,7 +6231,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++)
@@ -9630,7 +9606,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];
@@ -9723,25 +9699,16 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
     }

- hit:
-    xml_buf = xmlBufferCreate();
-    if (!xml_buf) {
-        vshError(ctl, "%s", _("Failed to allocate memory"));
-        goto cleanup;
-    }
-
-    if (xmlNodeDump(xml_buf, xml, matchNode, 0, 0) < 0) {
-        vshError(ctl, "%s", _("Failed to create XML"));
+hit:
+    if (!(detach_xml = virXMLNodeDump(xml, matchNode))) {
+        vshSaveLibvirtError();
         goto cleanup;
     }

-    if (flags != 0) {
-        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"));
@@ -9750,13 +9717,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;
 }

@@ -9874,21 +9841,14 @@ vshPrepareDiskXML(xmlNodePtr disk_node,
                   int type)
 {
     xmlNodePtr cur = NULL;
-    xmlBufferPtr xml_buf = NULL;
-    char *disk_type = NULL;
-    char *device_type = NULL;
+    const char *disk_type = NULL;
+    const char *device_type = NULL;
     xmlNodePtr new_node = NULL;
     char *ret = NULL;

     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") ||
@@ -9910,7 +9870,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) {
@@ -9922,10 +9882,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;
             }
         }

@@ -9933,7 +9893,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. */
@@ -9949,30 +9909,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 = virXMLNodeDump(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;
 }


-- 
1.8.3.2




More information about the libvir-list mailing list