]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
snapshot: Factor out post-parse code
authorEric Blake <eblake@redhat.com>
Mon, 15 Apr 2019 22:25:52 +0000 (17:25 -0500)
committerEric Blake <eblake@redhat.com>
Wed, 17 Apr 2019 02:55:59 +0000 (21:55 -0500)
Move the non-deterministic code that sets snapshot properties
independently of what the incoming XML described to instead live in a
default 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 allow for a per-driver override, used by the
testsuite to perform deterministic post-parse actions for better
coverage of parser/formatter code.

Note that the post-parse code is intentionally not run during a
snapshot redefine, since that code path already requires a valid
snapshot name and creation time from the XML.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Cole Robinson <crobinso@redhat.com>
src/conf/moment_conf.c
src/conf/moment_conf.h
src/conf/snapshot_conf.c

index 886f3a44b4cc2a6b52d972965437e664833117c6..0eb32eeb520829394adefd19bc2c1826f56e142d 100644 (file)
 
 #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;
+}
index 8af3fbc6ac89e9352c43041bf05225043a15f9f0..e06a4a7b3c84300877917fbff5f8bdf6740c9a8c 100644 (file)
@@ -39,4 +39,6 @@ struct _virDomainMomentDef {
 
 void virDomainMomentDefClear(virDomainMomentDefPtr def);
 
+int virDomainMomentDefPostParse(virDomainMomentDefPtr def);
+
 #endif /* LIBVIRT_MOMENT_CONF_H */
index 08f335646c698295d9fad9fa870c93c08dd22ce6..ef6eae3a51d217b53a5b06b410f89343d7e553a9 100644 (file)
@@ -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);