[Libvirt-cim] [PATCH V2 05/48] Fix xml parsing algorithm for parse_block_device()

Xu Wang cngesaint at gmail.com
Mon Oct 28 02:45:34 UTC 2013


Signed-off-by: Xu Wang <gesaint at linux.vnet.ibm.com>
---
 libxkutil/device_parsing.c |  168 ++++++++++++++++++++++++++++++++++++--------
 1 files changed, 137 insertions(+), 31 deletions(-)

diff --git a/libxkutil/device_parsing.c b/libxkutil/device_parsing.c
index adb3146..6ab8131 100644
--- a/libxkutil/device_parsing.c
+++ b/libxkutil/device_parsing.c
@@ -824,7 +824,8 @@ static int parse_block_device(xmlNode *dnode, struct virt_device **vdevs)
 {
         struct virt_device *vdev = NULL;
         struct disk_device *ddev = NULL;
-        xmlNode * child = NULL;
+
+        CU_DEBUG("Enter parse_block_device().");
 
         vdev = calloc(1, sizeof(*vdev));
         if (vdev == NULL)
@@ -832,45 +833,150 @@ static int parse_block_device(xmlNode *dnode, struct virt_device **vdevs)
 
         ddev = &(vdev->dev.disk);
 
-        ddev->type = get_attr_value(dnode, "type");
-        if (ddev->type == NULL)
+        ddev->others = parse_data_to_others(ddev->others,
+                                            dnode,
+                                            0,
+                                            BAD_CAST "devices");
+        if (ddev->others == NULL) {
+                CU_DEBUG("xml file parse failed.");
+                goto err;
+        }
+
+        /* fetch out <disk> tag from others. It will be removed
+         * after others management finished. */
+        fetch_from_others(&ddev->others,
+                          -1,
+                          "disk",
+                          TYPE_NODE,
+                          -1,
+                          "devices");
+
+        ddev->type = fetch_from_others(&ddev->others,
+                                       -1,
+                                       "type",
+                                       TYPE_PROP,
+                                       -1,
+                                       (char *)dnode->name);
+        if (ddev->type == NULL) {
+                CU_DEBUG("no type");
                 goto err;
+        }
 
-        ddev->device = get_attr_value(dnode, "device");
-        if (ddev->device == NULL)
+        ddev->device = fetch_from_others(&ddev->others,
+                                         -1,
+                                         "device",
+                                         TYPE_PROP,
+                                         -1,
+                                         (char *)dnode->name);
+        if (ddev->device == NULL) {
+                CU_DEBUG("no device");
                 goto err;
+        }
 
-        for (child = dnode->children; child != NULL; child = child->next) {
-                if (XSTREQ(child->name, "driver")) {
-                        ddev->driver = get_attr_value(child, "name");
-                        if (ddev->driver == NULL)
-                                goto err;
-                        ddev->driver_type = get_attr_value(child, "type");
-                        ddev->cache = get_attr_value(child, "cache");
-                } else if (XSTREQ(child->name, "source")) {
-                        ddev->source = get_attr_value(child, "file");
-                        if (ddev->source) {
-                                ddev->disk_type = DISK_FILE;
-                                continue;
-                        }
-                        ddev->source = get_attr_value(child, "dev");
-                        if (ddev->source) {
-                                ddev->disk_type = DISK_PHY;
-                                continue;
-                        }
+        if (seek_in_others(&ddev->others,
+                           -1,
+                           "driver",
+                           TYPE_NODE,
+                           -1,
+                           (char *)dnode->name)) {
+                ddev->driver = fetch_from_others(&ddev->others,
+                                                 -1,
+                                                 "name",
+                                                 TYPE_PROP,
+                                                 -1,
+                                                 "driver");
+
+                if (ddev->driver == NULL) {
+                        CU_DEBUG("no driver name");
                         goto err;
-                } else if (XSTREQ(child->name, "target")) {
-                        ddev->virtual_dev = get_attr_value(child, "dev");
-                        if (ddev->virtual_dev == NULL)
+                }
+
+                ddev->driver_type = fetch_from_others(&ddev->others,
+                                                      -1,
+                                                      "type",
+                                                      TYPE_PROP,
+                                                      -1,
+                                                      "driver");
+
+                ddev->cache = fetch_from_others(&ddev->others,
+                                                -1,
+                                                "cache",
+                                                TYPE_PROP,
+                                                -1,
+                                                "driver");
+        }
+
+        if (seek_in_others(&ddev->others,
+                           -1,
+                           "source",
+                           TYPE_NODE,
+                           -1,
+                           (char *)dnode->name)) {
+                ddev->source = fetch_from_others(&ddev->others,
+                                                 -1,
+                                                 "file",
+                                                 TYPE_PROP,
+                                                 -1,
+                                                 "source");
+                if (ddev->source == NULL) {
+                        ddev->source = fetch_from_others(&ddev->others,
+                                                         -1,
+                                                         "dev",
+                                                         TYPE_PROP,
+                                                         -1,
+                                                         "source");
+
+                        if (ddev->source == NULL) {
+                                CU_DEBUG("no source file/dev");
                                 goto err;
-                        ddev->bus_type = get_attr_value(child, "bus");
-                } else if (XSTREQ(child->name, "readonly")) {
-                        ddev->readonly = true;
-                } else if (XSTREQ(child->name, "shareable")) {
-                        ddev->shareable = true;
+                        } else {
+                                ddev->disk_type == DISK_PHY;
+                        }
+                } else {
+                        ddev->disk_type = DISK_FILE;
                 }
         }
 
+        if (seek_in_others(&ddev->others,
+                           -1,
+                           "target",
+                           TYPE_NODE,
+                           -1,
+                           (char *)dnode->name)) {
+                ddev->virtual_dev = fetch_from_others(&ddev->others,
+                                                      -1,
+                                                      "dev",
+                                                      TYPE_PROP,
+                                                      -1,
+                                                      "target");
+
+                if (ddev->virtual_dev == NULL) {
+                        CU_DEBUG("no target dev");
+                        goto err;
+                }
+
+                ddev->bus_type = fetch_from_others(&ddev->others,
+                                                   -1,
+                                                   "bus",
+                                                   TYPE_PROP,
+                                                   -1,
+                                                   "target");
+        }
+
+        ddev->readonly = seek_in_others(&ddev->others,
+                                        -1,
+                                        "readonly",
+                                        TYPE_NODE,
+                                        -1,
+                                        (char *)dnode->name);
+
+        ddev->shareable = seek_in_others(&ddev->others,
+                                         -1,
+                                         "shareable",
+                                         TYPE_NODE,
+                                         -1,
+                                         (char *)dnode->name);
+
         /* handle the situation that a cdrom device have no disk in it, no ISO file */
         if ((XSTREQ(ddev->device, "cdrom")) && (ddev->source == NULL)) {
                 ddev->source = strdup("");
-- 
1.7.1




More information about the Libvirt-cim mailing list