[PATCH 11/14] snapshot: indent domain xml when nesting, round 11

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Networking definitions.  Fixing this involved some refactoring of
common code out of domain_conf and nwfilter_conf into nwfilter_params.

* src/conf/nwfilter_params.h (virNWFilterFormatParamAttributes):
Adjust signature.
* src/conf/nwfilter_params.c (_formatParameterAttrs)
(virNWFilterFormatParamAttributes): Adjust indentation.
* src/conf/domain_conf.c (virDomainNetDefFormat)
(virDomainActualNetDefFormat): Add parameter.
(virDomainDefFormatInternal): Adjust caller.
* src/conf/nwfilter_conf.c (virNWFilterIncludeDefFormat): Likewise.
---
 src/conf/domain_conf.c     |  127 +++++++++++++++++++++----------------------
 src/conf/nwfilter_conf.c   |   16 ++----
 src/conf/nwfilter_params.c |   41 ++++++++------
 src/conf/nwfilter_params.h |    8 ++-
 4 files changed, 97 insertions(+), 95 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index a9d274b..64bb337 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -9367,7 +9367,8 @@ virDomainFSDefFormat(virBufferPtr buf,

 static int
 virDomainActualNetDefFormat(virBufferPtr buf,
-                            virDomainActualNetDefPtr def)
+                            virDomainActualNetDefPtr def,
+                            int indent)
 {
     int ret = -1;
     const char *type;
@@ -9389,18 +9390,17 @@ virDomainActualNetDefFormat(virBufferPtr buf,
                              _("unexpected net type %s"), type);
         goto error;
     }
-    virBufferAsprintf(buf, "      <actual type='%s'>\n", type);
+    virBufferAsprintf(buf, "%*s<actual type='%s'>\n", indent, "", type);

     switch (def->type) {
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
-        if (def->data.bridge.brname) {
-            virBufferEscapeString(buf, "        <source bridge='%s'/>\n",
-                                  def->data.bridge.brname);
-        }
+        virBufferIndentEscapeString(buf, indent + 2,
+                                    "<source bridge='%s'/>\n",
+                                    def->data.bridge.brname);
         break;

     case VIR_DOMAIN_NET_TYPE_DIRECT:
-        virBufferAddLit(buf, "        <source");
+        virBufferIndentAddLit(buf, indent + 2, "<source");
         if (def->data.direct.linkdev)
             virBufferEscapeString(buf, " dev='%s'",
                                   def->data.direct.linkdev);
@@ -9413,16 +9413,17 @@ virDomainActualNetDefFormat(virBufferPtr buf,
             return ret;
         }
         virBufferAsprintf(buf, " mode='%s'/>\n", mode);
-        virVirtualPortProfileFormat(buf, def->data.direct.virtPortProfile, 8);
+        virVirtualPortProfileFormat(buf, def->data.direct.virtPortProfile,
+                                    indent + 2);
         break;
     default:
         break;
     }

-    if (virBandwidthDefFormat(buf, def->bandwidth, 6) < 0)
+    if (virBandwidthDefFormat(buf, def->bandwidth, indent + 2) < 0)
         goto error;

-    virBufferAddLit(buf, "      </actual>\n");
+    virBufferIndentAddLit(buf, indent, "</actual>\n");

     ret = 0;
 error:
@@ -9432,10 +9433,10 @@ error:
 static int
 virDomainNetDefFormat(virBufferPtr buf,
                       virDomainNetDefPtr def,
+                      int indent,
                       unsigned int flags)
 {
     const char *type = virDomainNetTypeToString(def->type);
-    char *attrs;

     if (!type) {
         virDomainReportError(VIR_ERR_INTERNAL_ERROR,
@@ -9443,74 +9444,74 @@ virDomainNetDefFormat(virBufferPtr buf,
         return -1;
     }

-    virBufferAsprintf(buf, "    <interface type='%s'>\n", type);
+    virBufferAsprintf(buf, "%*s<interface type='%s'>\n", indent, "", type);

     virBufferAsprintf(buf,
-                      "      <mac address='%02x:%02x:%02x:%02x:%02x:%02x'/>\n",
+                      "%*s<mac address='%02x:%02x:%02x:%02x:%02x:%02x'/>\n",
+                      indent + 2, "",
                       def->mac[0], def->mac[1], def->mac[2],
                       def->mac[3], def->mac[4], def->mac[5]);

     switch (def->type) {
     case VIR_DOMAIN_NET_TYPE_NETWORK:
-        virBufferEscapeString(buf, "      <source network='%s'",
-                              def->data.network.name);
-        if (def->data.network.portgroup) {
-           virBufferEscapeString(buf, " portgroup='%s'",
-                                 def->data.network.portgroup);
-        }
+        virBufferIndentEscapeString(buf, indent + 2, "<source network='%s'",
+                                    def->data.network.name);
+        virBufferEscapeString(buf, " portgroup='%s'",
+                              def->data.network.portgroup);
         virBufferAddLit(buf, "/>\n");
-        virVirtualPortProfileFormat(buf, def->data.network.virtPortProfile, 6);
+        virVirtualPortProfileFormat(buf, def->data.network.virtPortProfile,
+                                    indent + 2);
         if ((flags & VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET) &&
-            (virDomainActualNetDefFormat(buf, def->data.network.actual) < 0))
+            (virDomainActualNetDefFormat(buf, def->data.network.actual,
+                                         indent + 2) < 0))
             return -1;
         break;

     case VIR_DOMAIN_NET_TYPE_ETHERNET:
-        if (def->data.ethernet.dev)
-            virBufferEscapeString(buf, "      <source dev='%s'/>\n",
-                                  def->data.ethernet.dev);
+        virBufferIndentEscapeString(buf, indent + 2, "<source dev='%s'/>\n",
+                                    def->data.ethernet.dev);
         if (def->data.ethernet.ipaddr)
-            virBufferAsprintf(buf, "      <ip address='%s'/>\n",
+            virBufferAsprintf(buf, "%*s<ip address='%s'/>\n", indent + 2, "",
                               def->data.ethernet.ipaddr);
-        if (def->data.ethernet.script)
-            virBufferEscapeString(buf, "      <script path='%s'/>\n",
-                                  def->data.ethernet.script);
+        virBufferIndentEscapeString(buf, indent + 2, "<script path='%s'/>\n",
+                                    def->data.ethernet.script);
         break;

     case VIR_DOMAIN_NET_TYPE_BRIDGE:
-        virBufferEscapeString(buf, "      <source bridge='%s'/>\n",
-                              def->data.bridge.brname);
+        virBufferIndentEscapeString(buf, indent + 2, "<source bridge='%s'/>\n",
+                                    def->data.bridge.brname);
         if (def->data.bridge.ipaddr)
-            virBufferAsprintf(buf, "      <ip address='%s'/>\n",
+            virBufferAsprintf(buf, "%*s<ip address='%s'/>\n", indent + 2, "",
                               def->data.bridge.ipaddr);
-        if (def->data.bridge.script)
-            virBufferEscapeString(buf, "      <script path='%s'/>\n",
-                                  def->data.bridge.script);
+        virBufferIndentEscapeString(buf, indent + 2, "<script path='%s'/>\n",
+                                    def->data.bridge.script);
         break;

     case VIR_DOMAIN_NET_TYPE_SERVER:
     case VIR_DOMAIN_NET_TYPE_CLIENT:
     case VIR_DOMAIN_NET_TYPE_MCAST:
         if (def->data.socket.address)
-            virBufferAsprintf(buf, "      <source address='%s' port='%d'/>\n",
+            virBufferAsprintf(buf, "%*s<source address='%s' port='%d'/>\n",
+                              indent + 2, "",
                               def->data.socket.address, def->data.socket.port);
         else
-            virBufferAsprintf(buf, "      <source port='%d'/>\n",
+            virBufferAsprintf(buf, "%*s<source port='%d'/>\n", indent + 2, "",
                               def->data.socket.port);
         break;

     case VIR_DOMAIN_NET_TYPE_INTERNAL:
-        virBufferEscapeString(buf, "      <source name='%s'/>\n",
-                              def->data.internal.name);
+        virBufferIndentEscapeString(buf, indent + 2, "<source name='%s'/>\n",
+                                    def->data.internal.name);
         break;

     case VIR_DOMAIN_NET_TYPE_DIRECT:
-        virBufferEscapeString(buf, "      <source dev='%s'",
-                              def->data.direct.linkdev);
+        virBufferIndentEscapeString(buf, indent + 2, "<source dev='%s'",
+                                    def->data.direct.linkdev);
         virBufferAsprintf(buf, " mode='%s'",
-                   virMacvtapModeTypeToString(def->data.direct.mode));
+                          virMacvtapModeTypeToString(def->data.direct.mode));
         virBufferAddLit(buf, "/>\n");
-        virVirtualPortProfileFormat(buf, def->data.direct.virtPortProfile, 6);
+        virVirtualPortProfileFormat(buf, def->data.direct.virtPortProfile,
+                                    indent + 2);
         break;

     case VIR_DOMAIN_NET_TYPE_USER:
@@ -9523,15 +9524,15 @@ virDomainNetDefFormat(virBufferPtr buf,
         !((flags & VIR_DOMAIN_XML_INACTIVE) &&
           (STRPREFIX(def->ifname, VIR_NET_GENERATED_PREFIX)))) {
         /* Skip auto-generated target names for inactive config. */
-        virBufferEscapeString(buf, "      <target dev='%s'/>\n",
-                              def->ifname);
+        virBufferIndentEscapeString(buf, indent + 2, "<target dev='%s'/>\n",
+                                    def->ifname);
     }
     if (def->model) {
-        virBufferEscapeString(buf, "      <model type='%s'/>\n",
-                              def->model);
+        virBufferIndentEscapeString(buf, indent + 2, "<model type='%s'/>\n",
+                                    def->model);
         if (STREQ(def->model, "virtio") &&
             (def->driver.virtio.name || def->driver.virtio.txmode)) {
-            virBufferAddLit(buf, "      <driver");
+            virBufferIndentAddLit(buf, indent + 2, "<driver");
             if (def->driver.virtio.name) {
                 virBufferAsprintf(buf, " name='%s'",
                                   virDomainNetBackendTypeToString(def->driver.virtio.name));
@@ -9552,36 +9553,32 @@ virDomainNetDefFormat(virBufferPtr buf,
         }
     }
     if (def->filter) {
-        virBufferEscapeString(buf, "      <filterref filter='%s'",
-                              def->filter);
-        attrs = virNWFilterFormatParamAttributes(def->filterparams,
-                                                 "        ");
-        if (!attrs || strlen(attrs) <= 1)
-            virBufferAddLit(buf, "/>\n");
-        else
-            virBufferAsprintf(buf, ">\n%s      </filterref>\n", attrs);
-        VIR_FREE(attrs);
+        if (virNWFilterFormatParamAttributes(buf, def->filterparams,
+                                             def->filter, indent + 4) < 0)
+            return -1;
     }
     if (def->bootIndex)
-        virBufferAsprintf(buf, "      <boot order='%d'/>\n", def->bootIndex);
+        virBufferAsprintf(buf, "%*s<boot order='%d'/>\n", indent + 2, "",
+                          def->bootIndex);

     if (def->tune.sndbuf_specified) {
-        virBufferAddLit(buf,   "      <tune>\n");
-        virBufferAsprintf(buf, "        <sndbuf>%lu</sndbuf>\n", def->tune.sndbuf);
-        virBufferAddLit(buf,   "      </tune>\n");
+        virBufferIndentAddLit(buf, indent + 2, "<tune>\n");
+        virBufferAsprintf(buf, "%*s<sndbuf>%lu</sndbuf>\n", indent + 4, "",
+                          def->tune.sndbuf);
+        virBufferIndentAddLit(buf, indent + 2, "</tune>\n");
     }

     if (def->linkstate)
-        virBufferAsprintf(buf, "      <link state='%s'/>\n",
+        virBufferAsprintf(buf, "%*s<link state='%s'/>\n", indent + 2, "",
                           virDomainNetInterfaceLinkStateTypeToString(def->linkstate));

-    if (virBandwidthDefFormat(buf, def->bandwidth, 6) < 0)
+    if (virBandwidthDefFormat(buf, def->bandwidth, indent + 2) < 0)
         return -1;

-    if (virDomainDeviceInfoFormat(buf, &def->info, 6, flags) < 0)
+    if (virDomainDeviceInfoFormat(buf, &def->info, indent + 2, flags) < 0)
         return -1;

-    virBufferAddLit(buf, "    </interface>\n");
+    virBufferIndentAddLit(buf, indent, "</interface>\n");

     return 0;
 }
@@ -10804,7 +10801,7 @@ virDomainDefFormatInternal(virDomainDefPtr def,


     for (n = 0 ; n < def->nnets ; n++)
-        if (virDomainNetDefFormat(buf, def->nets[n], flags) < 0) /* XXX indent */
+        if (virDomainNetDefFormat(buf, def->nets[n], indent, flags) < 0)
             goto cleanup;

     for (n = 0 ; n < def->nsmartcards ; n++)
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index 08ede48..20f97f2 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -2849,19 +2849,13 @@ no_memory:
 static char *
 virNWFilterIncludeDefFormat(virNWFilterIncludeDefPtr inc)
 {
-    char *attrs;
     virBuffer buf = VIR_BUFFER_INITIALIZER;

-    virBufferAsprintf(&buf,"  <filterref filter='%s'",
-                      inc->filterref);
-
-    attrs = virNWFilterFormatParamAttributes(inc->params, "    ");
-
-    if (!attrs || strlen(attrs) <= 1)
-        virBufferAddLit(&buf, "/>\n");
-    else
-        virBufferAsprintf(&buf, ">\n%s  </filterref>\n", attrs);
-
+    if (virNWFilterFormatParamAttributes(&buf, inc->params, inc->filterref,
+                                         2) < 0) {
+        virBufferFreeAndReset(&buf);
+        return NULL;
+    }
     if (virBufferError(&buf)) {
         virReportOOMError();
         virBufferFreeAndReset(&buf);
diff --git a/src/conf/nwfilter_params.c b/src/conf/nwfilter_params.c
index ee10b21..601a61c 100644
--- a/src/conf/nwfilter_params.c
+++ b/src/conf/nwfilter_params.c
@@ -260,7 +260,7 @@ skip_entry:

 struct formatterParam {
     virBufferPtr buf;
-    const char *indent;
+    int indent;
 };


@@ -269,30 +269,37 @@ _formatParameterAttrs(void *payload, const void *name, void *data)
 {
     struct formatterParam *fp = (struct formatterParam *)data;

-    virBufferAsprintf(fp->buf, "%s<parameter name='%s' value='%s'/>\n",
-                      fp->indent,
+    virBufferAsprintf(fp->buf, "%*s<parameter name='%s' value='%s'/>\n",
+                      fp->indent, "",
                       (const char *)name,
                       (char *)payload);
 }


-char *
-virNWFilterFormatParamAttributes(virNWFilterHashTablePtr table,
-                                 const char *indent)
+int
+virNWFilterFormatParamAttributes(virBufferPtr buf,
+                                 virNWFilterHashTablePtr table,
+                                 const char *filterref,
+                                 int indent)
 {
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
     struct formatterParam fp = {
-        .buf = &buf,
-        .indent = indent,
+        .buf = buf,
+        .indent = indent + 2,
     };
+    int count = virHashSize(table->hashTable);

-    virHashForEach(table->hashTable, _formatParameterAttrs, &fp);
-
-    if (virBufferError(&buf)) {
-        virReportOOMError();
-        virBufferFreeAndReset(&buf);
-        return NULL;
+    if (count < 0) {
+        virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                               _("missing filter parameter table"));
+        return -1;
     }
-
-    return virBufferContentAndReset(&buf);
+    virBufferAsprintf(buf, "%*s<filterref filter='%s'", indent, "", filterref);
+    if (count) {
+        virBufferAddLit(buf, ">\n");
+        virHashForEach(table->hashTable, _formatParameterAttrs, &fp);
+        virBufferIndentAddLit(buf, indent, "</filterref>\n");
+    } else {
+        virBufferAddLit(buf, "/>\n");
+    }
+    return 0;
 }
diff --git a/src/conf/nwfilter_params.h b/src/conf/nwfilter_params.h
index 012d6a1..889c351 100644
--- a/src/conf/nwfilter_params.h
+++ b/src/conf/nwfilter_params.h
@@ -1,6 +1,7 @@
 /*
  * nwfilter_params.h: parsing and data maintenance of filter parameters
  *
+ * Copyright (C) 2011 Red Hat, Inc.
  * Copyright (C) 2010 IBM Corporation
  *
  * This library is free software; you can redistribute it and/or
@@ -23,6 +24,7 @@
 # define NWFILTER_PARAMS_H

 # include "hash.h"
+# include "buf.h"

 typedef struct _virNWFilterHashTable virNWFilterHashTable;
 typedef virNWFilterHashTable *virNWFilterHashTablePtr;
@@ -35,8 +37,10 @@ struct _virNWFilterHashTable {


 virNWFilterHashTablePtr virNWFilterParseParamAttributes(xmlNodePtr cur);
-char * virNWFilterFormatParamAttributes(virNWFilterHashTablePtr table,
-                                        const char *indent);
+int virNWFilterFormatParamAttributes(virBufferPtr buf,
+                                     virNWFilterHashTablePtr table,
+                                     const char *filterref,
+                                     int indent);

 virNWFilterHashTablePtr virNWFilterHashTableCreate(int n);
 void virNWFilterHashTableFree(virNWFilterHashTablePtr table);
-- 
1.7.4.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]