[PATCH v3 05/18] snapshot: Refactor virDomainSnapshotDefFormat

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

 



Split out an internal helper that produces format into a
virBuffer, similar to what domain_conf.c does, and making
the next patch easier to write.

Signed-off-by: Eric Blake <eblake@xxxxxxxxxx>
Reviewed-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/conf/snapshot_conf.c | 106 +++++++++++++++++++++++----------------
 1 file changed, 62 insertions(+), 44 deletions(-)

diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
index 0f8c9cfc8c..bceebb2b1f 100644
--- a/src/conf/snapshot_conf.c
+++ b/src/conf/snapshot_conf.c
@@ -703,92 +703,110 @@ virDomainSnapshotDiskDefFormat(virBufferPtr buf,
 }


-char *
-virDomainSnapshotDefFormat(const char *uuidstr,
-                           virDomainSnapshotDefPtr def,
-                           virCapsPtr caps,
-                           virDomainXMLOptionPtr xmlopt,
-                           unsigned int flags)
+/* Append XML describing def into buf. Return 0 on success, or -1 on
+ * failure with buf cleared. */
+static int
+virDomainSnapshotDefFormatInternal(virBufferPtr buf,
+                                   const char *uuidstr,
+                                   virDomainSnapshotDefPtr def,
+                                   virCapsPtr caps,
+                                   virDomainXMLOptionPtr xmlopt,
+                                   unsigned int flags)
 {
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
     size_t i;
     int domainflags = VIR_DOMAIN_DEF_FORMAT_INACTIVE;

-    virCheckFlags(VIR_DOMAIN_SNAPSHOT_FORMAT_SECURE |
-                  VIR_DOMAIN_SNAPSHOT_FORMAT_INTERNAL, NULL);
-
     if (flags & VIR_DOMAIN_SNAPSHOT_FORMAT_SECURE)
         domainflags |= VIR_DOMAIN_DEF_FORMAT_SECURE;

-    virBufferAddLit(&buf, "<domainsnapshot>\n");
-    virBufferAdjustIndent(&buf, 2);
+    virBufferAddLit(buf, "<domainsnapshot>\n");
+    virBufferAdjustIndent(buf, 2);

-    virBufferEscapeString(&buf, "<name>%s</name>\n", def->name);
+    virBufferEscapeString(buf, "<name>%s</name>\n", def->name);
     if (def->description)
-        virBufferEscapeString(&buf, "<description>%s</description>\n",
+        virBufferEscapeString(buf, "<description>%s</description>\n",
                               def->description);
-    virBufferAsprintf(&buf, "<state>%s</state>\n",
+    virBufferAsprintf(buf, "<state>%s</state>\n",
                       virDomainSnapshotStateTypeToString(def->state));

     if (def->parent) {
-        virBufferAddLit(&buf, "<parent>\n");
-        virBufferAdjustIndent(&buf, 2);
-        virBufferEscapeString(&buf, "<name>%s</name>\n", def->parent);
-        virBufferAdjustIndent(&buf, -2);
-        virBufferAddLit(&buf, "</parent>\n");
+        virBufferAddLit(buf, "<parent>\n");
+        virBufferAdjustIndent(buf, 2);
+        virBufferEscapeString(buf, "<name>%s</name>\n", def->parent);
+        virBufferAdjustIndent(buf, -2);
+        virBufferAddLit(buf, "</parent>\n");
     }

-    virBufferAsprintf(&buf, "<creationTime>%lld</creationTime>\n",
+    virBufferAsprintf(buf, "<creationTime>%lld</creationTime>\n",
                       def->creationTime);

     if (def->memory) {
-        virBufferAsprintf(&buf, "<memory snapshot='%s'",
+        virBufferAsprintf(buf, "<memory snapshot='%s'",
                           virDomainSnapshotLocationTypeToString(def->memory));
-        virBufferEscapeString(&buf, " file='%s'", def->file);
-        virBufferAddLit(&buf, "/>\n");
+        virBufferEscapeString(buf, " file='%s'", def->file);
+        virBufferAddLit(buf, "/>\n");
     }

     if (def->ndisks) {
-        virBufferAddLit(&buf, "<disks>\n");
-        virBufferAdjustIndent(&buf, 2);
+        virBufferAddLit(buf, "<disks>\n");
+        virBufferAdjustIndent(buf, 2);
         for (i = 0; i < def->ndisks; i++) {
-            if (virDomainSnapshotDiskDefFormat(&buf, &def->disks[i], xmlopt) < 0)
+            if (virDomainSnapshotDiskDefFormat(buf, &def->disks[i], xmlopt) < 0)
                 goto error;
         }
-        virBufferAdjustIndent(&buf, -2);
-        virBufferAddLit(&buf, "</disks>\n");
+        virBufferAdjustIndent(buf, -2);
+        virBufferAddLit(buf, "</disks>\n");
     }

     if (def->dom) {
-        if (virDomainDefFormatInternal(def->dom, caps, domainflags, &buf,
+        if (virDomainDefFormatInternal(def->dom, caps, domainflags, buf,
                                        xmlopt) < 0)
             goto error;
     } else if (uuidstr) {
-        virBufferAddLit(&buf, "<domain>\n");
-        virBufferAdjustIndent(&buf, 2);
-        virBufferAsprintf(&buf, "<uuid>%s</uuid>\n", uuidstr);
-        virBufferAdjustIndent(&buf, -2);
-        virBufferAddLit(&buf, "</domain>\n");
+        virBufferAddLit(buf, "<domain>\n");
+        virBufferAdjustIndent(buf, 2);
+        virBufferAsprintf(buf, "<uuid>%s</uuid>\n", uuidstr);
+        virBufferAdjustIndent(buf, -2);
+        virBufferAddLit(buf, "</domain>\n");
     }

-    if (virSaveCookieFormatBuf(&buf, def->cookie,
+    if (virSaveCookieFormatBuf(buf, def->cookie,
                                virDomainXMLOptionGetSaveCookie(xmlopt)) < 0)
         goto error;

     if (flags & VIR_DOMAIN_SNAPSHOT_FORMAT_INTERNAL)
-        virBufferAsprintf(&buf, "<active>%d</active>\n", def->current);
+        virBufferAsprintf(buf, "<active>%d</active>\n", def->current);

-    virBufferAdjustIndent(&buf, -2);
-    virBufferAddLit(&buf, "</domainsnapshot>\n");
+    virBufferAdjustIndent(buf, -2);
+    virBufferAddLit(buf, "</domainsnapshot>\n");

-    if (virBufferCheckError(&buf) < 0)
-        return NULL;
+    if (virBufferCheckError(buf) < 0)
+        goto error;

-    return virBufferContentAndReset(&buf);
+    return 0;

  error:
-    virBufferFreeAndReset(&buf);
-    return NULL;
+    virBufferFreeAndReset(buf);
+    return -1;
+}
+
+
+char *
+virDomainSnapshotDefFormat(const char *uuidstr,
+                           virDomainSnapshotDefPtr def,
+                           virCapsPtr caps,
+                           virDomainXMLOptionPtr xmlopt,
+                           unsigned int flags)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+
+    virCheckFlags(VIR_DOMAIN_SNAPSHOT_FORMAT_SECURE |
+                  VIR_DOMAIN_SNAPSHOT_FORMAT_INTERNAL, NULL);
+    if (virDomainSnapshotDefFormatInternal(&buf, uuidstr, def, caps,
+                                           xmlopt, flags) < 0)
+        return NULL;
+
+    return virBufferContentAndReset(&buf);
 }

 /* Snapshot Obj functions */
-- 
2.20.1

--
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]

  Powered by Linux