[libvirt] [PATCH 2/4] virsh: Use vshFindDisk and vshPrepareDiskXML in cmdDetachDisk

Osier Yang jyang at redhat.com
Fri Feb 3 09:23:10 UTC 2012


The first use of the two new helper functions.
---
 tools/virsh.c |   62 ++++++++------------------------------------------------
 1 files changed, 9 insertions(+), 53 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index ca69f30..5dad1d6 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -14436,18 +14436,14 @@ static const vshCmdOptDef opts_detach_disk[] = {
 static bool
 cmdDetachDisk(vshControl *ctl, const vshCmd *cmd)
 {
-    xmlDocPtr xml = NULL;
-    xmlXPathObjectPtr obj=NULL;
-    xmlXPathContextPtr ctxt = NULL;
-    xmlNodePtr cur = NULL;
-    xmlBufferPtr xml_buf = NULL;
+    char *disk_xml = NULL;
     virDomainPtr dom = NULL;
     const char *target = NULL;
     char *doc;
-    int i = 0, diff_tgt;
     int ret;
     bool functionReturn = false;
     unsigned int flags;
+    xmlNodePtr disk_node = NULL;
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         goto cleanup;
@@ -14462,60 +14458,21 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd)
     if (!doc)
         goto cleanup;
 
-    xml = virXMLParseStringCtxt(doc, _("(domain_definition)"), &ctxt);
-    VIR_FREE(doc);
-    if (!xml) {
-        vshError(ctl, "%s", _("Failed to get disk information"));
-        goto cleanup;
-    }
-
-    obj = xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt);
-    if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
-        (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr == 0)) {
-        vshError(ctl, "%s", _("Failed to get disk information"));
-        goto cleanup;
-    }
-
-    /* search target */
-    for (; i < obj->nodesetval->nodeNr; i++) {
-        cur = obj->nodesetval->nodeTab[i]->children;
-        while (cur != NULL) {
-            if (cur->type == XML_ELEMENT_NODE &&
-                xmlStrEqual(cur->name, BAD_CAST "target")) {
-                char *tmp_tgt = virXMLPropString(cur, "dev");
-                diff_tgt = STREQ(tmp_tgt, target);
-                VIR_FREE(tmp_tgt);
-                if (diff_tgt) {
-                    goto hit;
-                }
-            }
-            cur = cur->next;
-        }
-    }
-    vshError(ctl, _("No found disk whose target is %s"), target);
-    goto cleanup;
-
- hit:
-    xml_buf = xmlBufferCreate();
-    if (!xml_buf) {
-        vshError(ctl, "%s", _("Failed to allocate memory"));
+    if (!(disk_node = vshFindDisk(doc, target, 0)))
         goto cleanup;
-    }
 
-    if (xmlNodeDump(xml_buf, xml, obj->nodesetval->nodeTab[i], 0, 0) < 0) {
-        vshError(ctl, "%s", _("Failed to create XML"));
+    if (!(disk_xml = vshPrepareDiskXML(disk_node, NULL, NULL, 0)))
         goto cleanup;
-    }
 
     if (vshCommandOptBool(cmd, "persistent")) {
         flags = VIR_DOMAIN_AFFECT_CONFIG;
         if (virDomainIsActive(dom) == 1)
             flags |= VIR_DOMAIN_AFFECT_LIVE;
         ret = virDomainDetachDeviceFlags(dom,
-                                         (char *)xmlBufferContent(xml_buf),
+                                         disk_xml,
                                          flags);
     } else {
-        ret = virDomainDetachDevice(dom, (char *)xmlBufferContent(xml_buf));
+        ret = virDomainDetachDevice(dom, disk_xml);
     }
 
     if (ret != 0) {
@@ -14526,10 +14483,9 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd)
     }
 
  cleanup:
-    xmlXPathFreeObject(obj);
-    xmlXPathFreeContext(ctxt);
-    xmlFreeDoc(xml);
-    xmlBufferFree(xml_buf);
+    xmlFreeNode(disk_node);
+    VIR_FREE(disk_xml);
+    VIR_FREE(doc);
     if (dom)
         virDomainFree(dom);
     return functionReturn;
-- 
1.7.7.3




More information about the libvir-list mailing list