Blob Blame History Raw
From 969efd354611526e1c718c1cba9f188aa96f24ce Mon Sep 17 00:00:00 2001
Message-Id: <969efd354611526e1c718c1cba9f188aa96f24ce@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Wed, 26 Feb 2014 14:54:43 +0100
Subject: [PATCH] conf: Support disk source formatting without needing a
 virDomainDiskDefPtr

https://bugzilla.redhat.com/show_bug.cgi?id=1032370

The <source> element formatting function was expecting a
virDomainDiskDefPtr to store the data. As snapshots are not using this
data structure to hold the data, we need to add an internal function
which splits out individual fields separately.

(cherry picked from commit d6b4c2cca9886720c7ada718acdde1254ae1e25a)

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/conf/domain_conf.c | 129 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 78 insertions(+), 51 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 409fcda..7032b2b 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -14463,28 +14463,38 @@ virDomainDiskBlockIoDefFormat(virBufferPtr buf,
 }
 
 static int
-virDomainDiskSourceDefFormat(virBufferPtr buf,
-                             virDomainDiskDefPtr def,
-                             unsigned int flags)
+virDomainDiskSourceDefFormatInternal(virBufferPtr buf,
+                                     int type,
+                                     const char *src,
+                                     int policy,
+                                     int protocol,
+                                     size_t nhosts,
+                                     virDomainDiskHostDefPtr hosts,
+                                     size_t nseclabels,
+                                     virSecurityDeviceLabelDefPtr *seclabels,
+                                     virDomainDiskSourcePoolDefPtr srcpool,
+                                     unsigned int flags)
 {
-    int n;
-    const char *startupPolicy = virDomainStartupPolicyTypeToString(def->startupPolicy);
+    size_t n;
+    const char *startupPolicy = NULL;
 
-    if (def->src || def->nhosts > 0 || def->srcpool ||
-        def->startupPolicy) {
-        switch (def->type) {
+    if (policy)
+        startupPolicy = virDomainStartupPolicyTypeToString(policy);
+
+    if (src || nhosts > 0 || srcpool || startupPolicy) {
+        switch (type) {
         case VIR_DOMAIN_DISK_TYPE_FILE:
             virBufferAddLit(buf, "      <source");
-            if (def->src)
-                virBufferEscapeString(buf, " file='%s'", def->src);
-            if (def->startupPolicy)
+            if (src)
+                virBufferEscapeString(buf, " file='%s'", src);
+            if (startupPolicy)
                 virBufferEscapeString(buf, " startupPolicy='%s'",
                                       startupPolicy);
-            if (def->nseclabels) {
+            if (nseclabels) {
                 virBufferAddLit(buf, ">\n");
                 virBufferAdjustIndent(buf, 8);
-                for (n = 0; n < def->nseclabels; n++)
-                    virSecurityDeviceLabelDefFormat(buf, def->seclabels[n],
+                for (n = 0; n < nseclabels; n++)
+                    virSecurityDeviceLabelDefFormat(buf, seclabels[n],
                                                     flags);
                 virBufferAdjustIndent(buf, -8);
                 virBufferAddLit(buf, "      </source>\n");
@@ -14494,15 +14504,15 @@ virDomainDiskSourceDefFormat(virBufferPtr buf,
             break;
         case VIR_DOMAIN_DISK_TYPE_BLOCK:
             virBufferAddLit(buf, "      <source");
-            virBufferEscapeString(buf, " dev='%s'", def->src);
-            if (def->startupPolicy)
+            virBufferEscapeString(buf, " dev='%s'", src);
+            if (startupPolicy)
                 virBufferEscapeString(buf, " startupPolicy='%s'",
                                       startupPolicy);
-            if (def->nseclabels) {
+            if (nseclabels) {
                 virBufferAddLit(buf, ">\n");
                 virBufferAdjustIndent(buf, 8);
-                for (n = 0; n < def->nseclabels; n++)
-                    virSecurityDeviceLabelDefFormat(buf, def->seclabels[n],
+                for (n = 0; n < nseclabels; n++)
+                    virSecurityDeviceLabelDefFormat(buf, seclabels[n],
                                                     flags);
                 virBufferAdjustIndent(buf, -8);
                 virBufferAddLit(buf, "      </source>\n");
@@ -14511,41 +14521,38 @@ virDomainDiskSourceDefFormat(virBufferPtr buf,
             }
             break;
         case VIR_DOMAIN_DISK_TYPE_DIR:
-            virBufferEscapeString(buf, "      <source dir='%s'",
-                                  def->src);
-            if (def->startupPolicy)
+            virBufferEscapeString(buf, "      <source dir='%s'", src);
+            if (startupPolicy)
                 virBufferEscapeString(buf, " startupPolicy='%s'",
                                       startupPolicy);
             virBufferAddLit(buf, "/>\n");
             break;
         case VIR_DOMAIN_DISK_TYPE_NETWORK:
             virBufferAsprintf(buf, "      <source protocol='%s'",
-                              virDomainDiskProtocolTypeToString(def->protocol));
-            if (def->src) {
-                virBufferEscapeString(buf, " name='%s'", def->src);
-            }
-            if (def->nhosts == 0) {
+                              virDomainDiskProtocolTypeToString(protocol));
+            if (src)
+                virBufferEscapeString(buf, " name='%s'", src);
+
+            if (nhosts == 0) {
                 virBufferAddLit(buf, "/>\n");
             } else {
-                size_t i;
-
                 virBufferAddLit(buf, ">\n");
-                for (i = 0; i < def->nhosts; i++) {
+                for (n = 0; n < nhosts; n++) {
                     virBufferAddLit(buf, "        <host");
-                    if (def->hosts[i].name) {
-                        virBufferEscapeString(buf, " name='%s'", def->hosts[i].name);
-                    }
-                    if (def->hosts[i].port) {
+                    if (hosts[n].name)
+                        virBufferEscapeString(buf, " name='%s'", hosts[n].name);
+
+                    if (hosts[n].port)
                         virBufferEscapeString(buf, " port='%s'",
-                                              def->hosts[i].port);
-                    }
-                    if (def->hosts[i].transport) {
+                                              hosts[n].port);
+
+                    if (hosts[n].transport)
                         virBufferAsprintf(buf, " transport='%s'",
-                                          virDomainDiskProtocolTransportTypeToString(def->hosts[i].transport));
-                    }
-                    if (def->hosts[i].socket) {
-                        virBufferEscapeString(buf, " socket='%s'", def->hosts[i].socket);
-                    }
+                                          virDomainDiskProtocolTransportTypeToString(hosts[n].transport));
+
+                    if (hosts[n].socket)
+                        virBufferEscapeString(buf, " socket='%s'", hosts[n].socket);
+
                     virBufferAddLit(buf, "/>\n");
                 }
                 virBufferAddLit(buf, "      </source>\n");
@@ -14554,21 +14561,21 @@ virDomainDiskSourceDefFormat(virBufferPtr buf,
         case VIR_DOMAIN_DISK_TYPE_VOLUME:
             virBufferAddLit(buf, "      <source");
 
-            if (def->srcpool) {
+            if (srcpool) {
                 virBufferAsprintf(buf, " pool='%s' volume='%s'",
-                                  def->srcpool->pool, def->srcpool->volume);
-                if (def->srcpool->mode)
+                                  srcpool->pool, srcpool->volume);
+                if (srcpool->mode)
                     virBufferAsprintf(buf, " mode='%s'",
-                                      virDomainDiskSourcePoolModeTypeToString(def->srcpool->mode));
+                                      virDomainDiskSourcePoolModeTypeToString(srcpool->mode));
             }
-            if (def->startupPolicy)
+            if (startupPolicy)
                 virBufferEscapeString(buf, " startupPolicy='%s'", startupPolicy);
 
-            if (def->nseclabels) {
+            if (nseclabels) {
                 virBufferAddLit(buf, ">\n");
                 virBufferAdjustIndent(buf, 8);
-                for (n = 0; n < def->nseclabels; n++)
-                    virSecurityDeviceLabelDefFormat(buf, def->seclabels[n],
+                for (n = 0; n < nseclabels; n++)
+                    virSecurityDeviceLabelDefFormat(buf, seclabels[n],
                                                     flags);
                 virBufferAdjustIndent(buf, -8);
                 virBufferAddLit(buf, "      </source>\n");
@@ -14579,7 +14586,7 @@ virDomainDiskSourceDefFormat(virBufferPtr buf,
         default:
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("unexpected disk type %s"),
-                           virDomainDiskTypeToString(def->type));
+                           virDomainDiskTypeToString(type));
             return -1;
         }
     }
@@ -14587,6 +14594,26 @@ virDomainDiskSourceDefFormat(virBufferPtr buf,
     return 0;
 }
 
+
+static int
+virDomainDiskSourceDefFormat(virBufferPtr buf,
+                             virDomainDiskDefPtr def,
+                             unsigned int flags)
+{
+    return virDomainDiskSourceDefFormatInternal(buf,
+                                                def->type,
+                                                def->src,
+                                                def->startupPolicy,
+                                                def->protocol,
+                                                def->nhosts,
+                                                def->hosts,
+                                                def->nseclabels,
+                                                def->seclabels,
+                                                def->srcpool,
+                                                flags);
+}
+
+
 static int
 virDomainDiskDefFormat(virBufferPtr buf,
                        virDomainDiskDefPtr def,
-- 
1.9.0