[libvirt] [PATCH v4 04/14] domain_conf: introduce virDomainGraphicsAddListenAddr

Pavel Hrdina phrdina at redhat.com
Thu May 19 11:35:23 UTC 2016


Move code that decide whether we print the 'listen' attribute or not
into virDomainGraphicsAddListenAddr() function.

Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 src/conf/domain_conf.c | 59 +++++++++++++++++++++++++++++---------------------
 1 file changed, 34 insertions(+), 25 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index b1b2bb9..b3b60f1 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -21397,13 +21397,43 @@ virDomainGraphicsListenDefFormat(virBufferPtr buf,
 }
 
 
+/**
+ * virDomainGraphicsAddListenAddr:
+ * @buf: buffer where the output XML is written
+ * @glisten: first listen element
+ * @flags: bit-wise or of VIR_DOMAIN_DEF_FORMAT_*
+ *
+ * This is used to add a legacy 'listen' attribute into <graphics> element to
+ * improve backward compatibility.
+ */
+static void
+virDomainGraphicsAddListenAddr(virBufferPtr buf,
+                               virDomainGraphicsListenDefPtr glisten,
+                               unsigned int flags)
+{
+    if (!glisten)
+        return;
+
+    if (flags & VIR_DOMAIN_DEF_FORMAT_MIGRATABLE && glisten->fromConfig)
+        return;
+
+    if (glisten->type == VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK &&
+        flags & (VIR_DOMAIN_DEF_FORMAT_INACTIVE |
+                 VIR_DOMAIN_DEF_FORMAT_MIGRATABLE))
+        return;
+
+    if (glisten->address)
+        virBufferAsprintf(buf, " listen='%s'", glisten->address);
+}
+
+
 static int
 virDomainGraphicsDefFormat(virBufferPtr buf,
                            virDomainGraphicsDefPtr def,
                            unsigned int flags)
 {
+    virDomainGraphicsListenDefPtr glisten = virDomainGraphicsGetListen(def, 0);
     const char *type = virDomainGraphicsTypeToString(def->type);
-    const char *listenAddr = NULL;
     bool children = false;
     size_t i;
 
@@ -21413,24 +21443,6 @@ virDomainGraphicsDefFormat(virBufferPtr buf,
         return -1;
     }
 
-    /* find the first listen subelement with a valid address and
-    * duplicate its address attribute as the listen attribute of
-    * <graphics>. This is done to improve backward compatibility.
-    */
-    for (i = 0; i < def->nListens; i++) {
-        if (flags & VIR_DOMAIN_DEF_FORMAT_MIGRATABLE &&
-            def->listens[i].fromConfig)
-            continue;
-
-        if (def->listens[i].type == VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK &&
-            flags & (VIR_DOMAIN_DEF_FORMAT_INACTIVE |
-                     VIR_DOMAIN_DEF_FORMAT_MIGRATABLE))
-            continue;
-
-        if ((listenAddr = def->listens[i].address))
-            break;
-    }
-
     virBufferAsprintf(buf, "<graphics type='%s'", type);
 
     switch (def->type) {
@@ -21455,8 +21467,7 @@ virDomainGraphicsDefFormat(virBufferPtr buf,
             if (def->data.vnc.websocket)
                 virBufferAsprintf(buf, " websocket='%d'", def->data.vnc.websocket);
 
-            if (listenAddr)
-                virBufferAsprintf(buf, " listen='%s'", listenAddr);
+            virDomainGraphicsAddListenAddr(buf, glisten, flags);
         }
 
         if (def->data.vnc.keymap)
@@ -21500,8 +21511,7 @@ virDomainGraphicsDefFormat(virBufferPtr buf,
         if (def->data.rdp.multiUser)
             virBufferAddLit(buf, " multiUser='yes'");
 
-        if (listenAddr)
-            virBufferAsprintf(buf, " listen='%s'", listenAddr);
+        virDomainGraphicsAddListenAddr(buf, glisten, flags);
 
         break;
 
@@ -21527,8 +21537,7 @@ virDomainGraphicsDefFormat(virBufferPtr buf,
         virBufferAsprintf(buf, " autoport='%s'",
                           def->data.spice.autoport ? "yes" : "no");
 
-        if (listenAddr)
-            virBufferAsprintf(buf, " listen='%s'", listenAddr);
+        virDomainGraphicsAddListenAddr(buf, glisten, flags);
 
         if (def->data.spice.keymap)
             virBufferEscapeString(buf, " keymap='%s'",
-- 
2.8.2




More information about the libvir-list mailing list