[PATCH v3 06/18] snapshot: Add virDomainSnapshotObjListFormat

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

 



Add a new function to output all of the domain's snapshots in one
buffer.

Signed-off-by: Eric Blake <eblake@xxxxxxxxxx>
Reviewed-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/conf/snapshot_conf.h |  9 +++++-
 src/conf/snapshot_conf.c | 61 ++++++++++++++++++++++++++++++++++++++++
 src/libvirt_private.syms |  1 +
 3 files changed, 70 insertions(+), 1 deletion(-)

diff --git a/src/conf/snapshot_conf.h b/src/conf/snapshot_conf.h
index 1b8bdde4f5..69a7750b0b 100644
--- a/src/conf/snapshot_conf.h
+++ b/src/conf/snapshot_conf.h
@@ -1,7 +1,7 @@
 /*
  * snapshot_conf.h: domain snapshot XML processing
  *
- * Copyright (C) 2006-2014 Red Hat, Inc.
+ * Copyright (C) 2006-2019 Red Hat, Inc.
  * Copyright (C) 2006-2008 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -138,6 +138,13 @@ char *virDomainSnapshotDefFormat(const char *uuidstr,
                                  virCapsPtr caps,
                                  virDomainXMLOptionPtr xmlopt,
                                  unsigned int flags);
+int virDomainSnapshotObjListFormat(virBufferPtr buf,
+                                   const char *uuidstr,
+                                   virDomainSnapshotObjListPtr snapshots,
+                                   virDomainSnapshotObjPtr current_snapshot,
+                                   virCapsPtr caps,
+                                   virDomainXMLOptionPtr xmlopt,
+                                   unsigned int flags);
 int virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr snapshot,
                                 int default_snapshot,
                                 bool require_match);
diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
index bceebb2b1f..5641e57e43 100644
--- a/src/conf/snapshot_conf.c
+++ b/src/conf/snapshot_conf.c
@@ -809,6 +809,67 @@ virDomainSnapshotDefFormat(const char *uuidstr,
     return virBufferContentAndReset(&buf);
 }

+
+/* Struct and callback function used as a hash table callback; each call
+ * appends another snapshot XML to buf, with the caller clearing the
+ * buffer if any callback fails. */
+struct virDomainSnapshotFormatData {
+    virBufferPtr buf;
+    const char *uuidstr;
+    virCapsPtr caps;
+    virDomainXMLOptionPtr xmlopt;
+    unsigned int flags;
+};
+
+static int
+virDomainSnapshotFormatOne(void *payload,
+                           const void *name ATTRIBUTE_UNUSED,
+                           void *opaque)
+{
+    virDomainSnapshotObjPtr snap = payload;
+    struct virDomainSnapshotFormatData *data = opaque;
+    return virDomainSnapshotDefFormatInternal(data->buf, data->uuidstr,
+                                              snap->def, data->caps,
+                                              data->xmlopt, data->flags);
+}
+
+
+/* Format the XML for all snapshots in the list into buf. On error,
+ * clear the buffer and return -1. */
+int
+virDomainSnapshotObjListFormat(virBufferPtr buf,
+                               const char *uuidstr,
+                               virDomainSnapshotObjListPtr snapshots,
+                               virDomainSnapshotObjPtr current_snapshot,
+                               virCapsPtr caps,
+                               virDomainXMLOptionPtr xmlopt,
+                               unsigned int flags)
+{
+    struct virDomainSnapshotFormatData data = {
+        .buf = buf,
+        .uuidstr = uuidstr,
+        .caps = caps,
+        .xmlopt = xmlopt,
+        .flags = flags,
+    };
+
+    virBufferAddLit(buf, "<snapshots");
+    if (current_snapshot)
+        virBufferEscapeString(buf, " current='%s'",
+                              current_snapshot->def->name);
+    virBufferAddLit(buf, ">\n");
+    virBufferAdjustIndent(buf, 2);
+    if (virDomainSnapshotForEach(snapshots, virDomainSnapshotFormatOne,
+                                 &data) < 0) {
+        virBufferFreeAndReset(buf);
+        return -1;
+    }
+    virBufferAdjustIndent(buf, -2);
+    virBufferAddLit(buf, "</snapshots>\n");
+    return 0;
+}
+
+
 /* Snapshot Obj functions */
 static virDomainSnapshotObjPtr virDomainSnapshotObjNew(void)
 {
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7ce7c85b55..35e0c6d9dc 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -894,6 +894,7 @@ virDomainSnapshotFormatConvertXMLFlags;
 virDomainSnapshotIsExternal;
 virDomainSnapshotLocationTypeFromString;
 virDomainSnapshotLocationTypeToString;
+virDomainSnapshotObjListFormat;
 virDomainSnapshotObjListFree;
 virDomainSnapshotObjListGetNames;
 virDomainSnapshotObjListNew;
-- 
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