[libvirt] [PATCH 3/5] snapshot: Factor out post-parse code

Eric Blake eblake at redhat.com
Tue Apr 16 13:53:22 UTC 2019


Move the non-deterministic code that sets snapshot properties
independently of what the incoming XML described to instead live in a
post-parse function common to virDomainMoment (as checkpoints will
also reuse it in later patches). This patch is just code motion, with
no difference to any callers; but the next patch will further refactor
things to call the post-process code from each driver, while the
testsuite performs its own deterministic actions, for better testsuite
coverage of parser/formatter code.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 src/conf/moment_conf.h   |  2 ++
 src/conf/moment_conf.c   | 19 +++++++++++++++++++
 src/conf/snapshot_conf.c | 10 ++--------
 3 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/src/conf/moment_conf.h b/src/conf/moment_conf.h
index 8af3fbc6ac..e06a4a7b3c 100644
--- a/src/conf/moment_conf.h
+++ b/src/conf/moment_conf.h
@@ -39,4 +39,6 @@ struct _virDomainMomentDef {

 void virDomainMomentDefClear(virDomainMomentDefPtr def);

+int virDomainMomentDefPostParse(virDomainMomentDefPtr def);
+
 #endif /* LIBVIRT_MOMENT_CONF_H */
diff --git a/src/conf/moment_conf.c b/src/conf/moment_conf.c
index 886f3a44b4..0eb32eeb52 100644
--- a/src/conf/moment_conf.c
+++ b/src/conf/moment_conf.c
@@ -21,11 +21,14 @@

 #include <config.h>

+#include <sys/time.h>
+
 #include "internal.h"
 #include "moment_conf.h"
 #include "domain_conf.h"
 #include "virlog.h"
 #include "viralloc.h"
+#include "virstring.h"

 #define VIR_FROM_THIS VIR_FROM_DOMAIN

@@ -38,3 +41,19 @@ void virDomainMomentDefClear(virDomainMomentDefPtr def)
     VIR_FREE(def->parent);
     virDomainDefFree(def->dom);
 }
+
+/* Provide defaults for creation time and moment name after parsing XML */
+int
+virDomainMomentDefPostParse(virDomainMomentDefPtr def)
+{
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+
+    if (!def->name &&
+        virAsprintf(&def->name, "%lld", (long long)tv.tv_sec) < 0)
+        return -1;
+
+    def->creationTime = tv.tv_sec;
+    return 0;
+}
diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
index 08f335646c..ef6eae3a51 100644
--- a/src/conf/snapshot_conf.c
+++ b/src/conf/snapshot_conf.c
@@ -23,7 +23,6 @@

 #include <fcntl.h>
 #include <sys/stat.h>
-#include <sys/time.h>
 #include <unistd.h>

 #include "internal.h"
@@ -199,7 +198,6 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
     size_t i;
     int n;
     char *creation = NULL, *state = NULL;
-    struct timeval tv;
     int active;
     char *tmp;
     char *memorySnapshot = NULL;
@@ -210,8 +208,6 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
     if (VIR_ALLOC(def) < 0)
         goto cleanup;

-    gettimeofday(&tv, NULL);
-
     def->common.name = virXPathString("string(./name)", ctxt);
     if (def->common.name == NULL) {
         if (flags & VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE) {
@@ -219,8 +215,6 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
                            _("a redefined snapshot must have a name"));
             goto cleanup;
         }
-        if (virAsprintf(&def->common.name, "%lld", (long long)tv.tv_sec) < 0)
-            goto cleanup;
     }

     def->common.description = virXPathString("string(./description)", ctxt);
@@ -276,8 +270,8 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt,
         } else {
             VIR_WARN("parsing older snapshot that lacks domain");
         }
-    } else {
-        def->common.creationTime = tv.tv_sec;
+    } else if (virDomainMomentDefPostParse(&def->common) < 0) {
+        goto cleanup;
     }

     memorySnapshot = virXPathString("string(./memory/@snapshot)", ctxt);
-- 
2.20.1




More information about the libvir-list mailing list