]> xenbits.xensource.com Git - libvirt.git/commitdiff
snapshot: conf: Use common parsing and formatting functions for source
authorPeter Krempa <pkrempa@redhat.com>
Tue, 12 Nov 2013 10:37:04 +0000 (11:37 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 2 Dec 2013 13:28:20 +0000 (14:28 +0100)
Disk source elements for snapshots were using separate code from our
config parser. As snapshots can be stored on more than just regular
files, we will need the universal parser to allow us to expose a variety
of snapshot disk targets. This patch reuses the config parsers and
formatters to do the job.

This initial support only changes the code without any visible XML
change.

src/conf/snapshot_conf.c
src/conf/snapshot_conf.h

index 94a74d2d0704e8b2e6f5b19783a9084ba5d9d76d..67df442f3fcc6dea390ddd9a733ab70a6d9e803f 100644 (file)
@@ -128,27 +128,42 @@ virDomainSnapshotDiskDefParseXML(xmlNodePtr node,
         }
     }
 
-    cur = node->children;
-    while (cur) {
-        if (cur->type == XML_ELEMENT_NODE) {
-            if (!def->file &&
-                xmlStrEqual(cur->name, BAD_CAST "source")) {
-                def->file = virXMLPropString(cur, "file");
-            } else if (!def->format &&
-                       xmlStrEqual(cur->name, BAD_CAST "driver")) {
-                char *driver = virXMLPropString(cur, "type");
-                def->format = virStorageFileFormatTypeFromString(driver);
-                if (def->format <= 0) {
-                    virReportError(VIR_ERR_INTERNAL_ERROR,
-                                   _("unknown disk snapshot driver '%s'"),
-                                   driver);
-                    VIR_FREE(driver);
-                    goto cleanup;
-                }
+    def->type = -1;
+
+    for (cur = node->children; cur; cur = cur->next) {
+        if (cur->type != XML_ELEMENT_NODE)
+            continue;
+
+        if (!def->file &&
+            xmlStrEqual(cur->name, BAD_CAST "source")) {
+
+            int backingtype = def->type;
+
+            if (backingtype < 0)
+                backingtype = VIR_DOMAIN_DISK_TYPE_FILE;
+
+            if (virDomainDiskSourceDefParse(cur,
+                                            backingtype,
+                                            &def->file,
+                                            NULL,
+                                            NULL,
+                                            NULL,
+                                            NULL) < 0)
+                goto cleanup;
+
+        } else if (!def->format &&
+                   xmlStrEqual(cur->name, BAD_CAST "driver")) {
+            char *driver = virXMLPropString(cur, "type");
+            def->format = virStorageFileFormatTypeFromString(driver);
+            if (def->format <= 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("unknown disk snapshot driver '%s'"),
+                               driver);
                 VIR_FREE(driver);
+                goto cleanup;
             }
+            VIR_FREE(driver);
         }
-        cur = cur->next;
     }
 
     if (!def->snapshot && (def->file || def->format))
@@ -577,6 +592,8 @@ static void
 virDomainSnapshotDiskDefFormat(virBufferPtr buf,
                                virDomainSnapshotDiskDefPtr disk)
 {
+    int type = disk->type;
+
     if (!disk->name)
         return;
 
@@ -584,6 +601,10 @@ virDomainSnapshotDiskDefFormat(virBufferPtr buf,
     if (disk->snapshot > 0)
         virBufferAsprintf(buf, " snapshot='%s'",
                           virDomainSnapshotLocationTypeToString(disk->snapshot));
+
+    if (type < 0)
+        type = VIR_DOMAIN_DISK_TYPE_FILE;
+
     if (!disk->file && disk->format == 0) {
         virBufferAddLit(buf, "/>\n");
         return;
@@ -591,12 +612,14 @@ virDomainSnapshotDiskDefFormat(virBufferPtr buf,
 
     virBufferAddLit(buf, ">\n");
 
-    virBufferAdjustIndent(buf, 6);
     if (disk->format > 0)
-        virBufferEscapeString(buf, "<driver type='%s'/>\n",
+        virBufferEscapeString(buf, "      <driver type='%s'/>\n",
                               virStorageFileFormatTypeToString(disk->format));
-    virBufferEscapeString(buf, "<source file='%s'/>\n", disk->file);
-    virBufferAdjustIndent(buf, -6);
+    virDomainDiskSourceDefFormatInternal(buf,
+                                         type,
+                                         disk->file,
+                                         0, 0, 0, NULL, 0, NULL, NULL, 0);
+
     virBufferAddLit(buf, "    </disk>\n");
 }
 
index ff3dca2c4c6f634e37b59f1b419d5bad1d333f34..241d63cafe5b201281ccf080f0c2a6df4a8f9c9a 100644 (file)
@@ -51,6 +51,7 @@ struct _virDomainSnapshotDiskDef {
     char *name; /* name matching the <target dev='...' of the domain */
     int index; /* index within snapshot->dom->disks that matches name */
     int snapshot; /* enum virDomainSnapshotLocation */
+    int type; /* enum virDomainDiskType */
     char *file; /* new source file when snapshot is external */
     int format; /* enum virStorageFileFormat */
 };