Blob Blame History Raw
From 5785b4ff7c33f5e2eb16530f20b6b45d8809aae7 Mon Sep 17 00:00:00 2001
Message-Id: <5785b4ff7c33f5e2eb16530f20b6b45d8809aae7@dist-git>
From: Jiri Denemark <jdenemar@redhat.com>
Date: Fri, 2 Jun 2017 00:44:06 +0200
Subject: [PATCH] conf: Introduce virSaveCookie

The code will be used by snapshots and domain save/restore code to store
additional data for a saved running domain. It is analogous to migration
cookies, but simple and one way only.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
(cherry picked from commit e37daa1fb69ec30042cbc38d489217edb656bc30)

https://bugzilla.redhat.com/show_bug.cgi?id=1441662

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 po/POTFILES.in           |   1 +
 src/Makefile.am          |   1 +
 src/conf/virsavecookie.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++
 src/conf/virsavecookie.h |  62 ++++++++++++++++++++
 src/libvirt_private.syms |   7 +++
 5 files changed, 215 insertions(+)
 create mode 100644 src/conf/virsavecookie.c
 create mode 100644 src/conf/virsavecookie.h

diff --git a/po/POTFILES.in b/po/POTFILES.in
index 064abd5bbb..cf64115719 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -47,6 +47,7 @@ src/conf/virdomainobjlist.c
 src/conf/virinterfaceobj.c
 src/conf/virnodedeviceobj.c
 src/conf/virnwfilterobj.c
+src/conf/virsavecookie.c
 src/conf/virsecretobj.c
 src/conf/virstorageobj.c
 src/cpu/cpu.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 75e4344198..53550280bf 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -336,6 +336,7 @@ DOMAIN_CONF_SOURCES =						\
 		conf/domain_conf.c conf/domain_conf.h		\
 		conf/domain_audit.c conf/domain_audit.h		\
 		conf/domain_nwfilter.c conf/domain_nwfilter.h	\
+		conf/virsavecookie.c conf/virsavecookie.h	\
 		conf/snapshot_conf.c conf/snapshot_conf.h	\
 		conf/numa_conf.c conf/numa_conf.h	\
 		conf/virdomainobjlist.c conf/virdomainobjlist.h
diff --git a/src/conf/virsavecookie.c b/src/conf/virsavecookie.c
new file mode 100644
index 0000000000..502c04d0f4
--- /dev/null
+++ b/src/conf/virsavecookie.c
@@ -0,0 +1,144 @@
+/**
+ * virsavecookie.c: Save cookie handling
+ *
+ * Copyright (C) 2017 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#include "virerror.h"
+#include "virlog.h"
+#include "virobject.h"
+#include "virbuffer.h"
+#include "virxml.h"
+#include "virsavecookie.h"
+
+#define VIR_FROM_THIS VIR_FROM_CONF
+
+VIR_LOG_INIT("conf.savecookie");
+
+
+static int
+virSaveCookieParseNode(xmlXPathContextPtr ctxt,
+                       virObjectPtr *obj,
+                       virSaveCookieCallbacksPtr saveCookie)
+{
+    *obj = NULL;
+
+    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "cookie")) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("XML does not contain expected 'cookie' element"));
+        return -1;
+    }
+
+    if (!saveCookie || !saveCookie->parse)
+        return 0;
+
+    return saveCookie->parse(ctxt, obj);
+}
+
+
+int
+virSaveCookieParse(xmlXPathContextPtr ctxt,
+                   virObjectPtr *obj,
+                   virSaveCookieCallbacksPtr saveCookie)
+{
+    xmlNodePtr node = ctxt->node;
+    int ret = -1;
+
+    *obj = NULL;
+
+    if (!(ctxt->node = virXPathNode("./cookie", ctxt))) {
+        ret = 0;
+        goto cleanup;
+    }
+
+    ret = virSaveCookieParseNode(ctxt, obj, saveCookie);
+
+ cleanup:
+    ctxt->node = node;
+    return ret;
+}
+
+
+int
+virSaveCookieParseString(const char *xml,
+                         virObjectPtr *obj,
+                         virSaveCookieCallbacksPtr saveCookie)
+{
+    xmlDocPtr doc = NULL;
+    xmlXPathContextPtr ctxt = NULL;
+    int ret = -1;
+
+    *obj = NULL;
+
+    if (!xml) {
+        ret = 0;
+        goto cleanup;
+    }
+
+    if (!(doc = virXMLParseStringCtxt(xml, _("(save cookie)"), &ctxt)))
+        goto cleanup;
+
+    ret = virSaveCookieParseNode(ctxt, obj, saveCookie);
+
+ cleanup:
+    xmlXPathFreeContext(ctxt);
+    xmlFreeDoc(doc);
+    return ret;
+}
+
+
+int
+virSaveCookieFormatBuf(virBufferPtr buf,
+                       virObjectPtr obj,
+                       virSaveCookieCallbacksPtr saveCookie)
+{
+    if (!obj || !saveCookie || !saveCookie->format)
+        return 0;
+
+    virBufferAddLit(buf, "<cookie>\n");
+    virBufferAdjustIndent(buf, 2);
+
+    if (saveCookie->format(buf, obj) < 0)
+        return -1;
+
+    virBufferAdjustIndent(buf, -2);
+    virBufferAddLit(buf, "</cookie>\n");
+
+    return 0;
+}
+
+
+char *
+virSaveCookieFormat(virObjectPtr obj,
+                    virSaveCookieCallbacksPtr saveCookie)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+
+    if (virSaveCookieFormatBuf(&buf, obj, saveCookie) < 0)
+        goto error;
+
+    if (virBufferCheckError(&buf) < 0)
+        goto error;
+
+    return virBufferContentAndReset(&buf);
+
+ error:
+    virBufferFreeAndReset(&buf);
+    return NULL;
+}
diff --git a/src/conf/virsavecookie.h b/src/conf/virsavecookie.h
new file mode 100644
index 0000000000..4aed18466c
--- /dev/null
+++ b/src/conf/virsavecookie.h
@@ -0,0 +1,62 @@
+/**
+ * virsavecookie.h: Save cookie handling
+ *
+ * Copyright (C) 2017 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+#ifndef __VIR_SAVE_COOKIE_H__
+# define __VIR_SAVE_COOKIE_H__
+
+# include <libxml/xpath.h>
+
+# include "internal.h"
+# include "virobject.h"
+# include "virbuffer.h"
+
+
+typedef int (*virSaveCookieParseFunc)(xmlXPathContextPtr ctxt,
+                                      virObjectPtr *obj);
+typedef int (*virSaveCookieFormatFunc)(virBufferPtr buf,
+                                       virObjectPtr obj);
+
+typedef struct _virSaveCookieCallbacks virSaveCookieCallbacks;
+typedef virSaveCookieCallbacks *virSaveCookieCallbacksPtr;
+struct _virSaveCookieCallbacks {
+    virSaveCookieParseFunc parse;
+    virSaveCookieFormatFunc format;
+};
+
+
+int
+virSaveCookieParse(xmlXPathContextPtr ctxt,
+                   virObjectPtr *obj,
+                   virSaveCookieCallbacksPtr saveCookie);
+
+int
+virSaveCookieParseString(const char *xml,
+                         virObjectPtr *obj,
+                         virSaveCookieCallbacksPtr saveCookie);
+
+int
+virSaveCookieFormatBuf(virBufferPtr buf,
+                       virObjectPtr obj,
+                       virSaveCookieCallbacksPtr saveCookie);
+
+char *
+virSaveCookieFormat(virObjectPtr obj,
+                    virSaveCookieCallbacksPtr saveCookie);
+
+#endif /*__VIR_SAVE_COOKIE_H__ */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a578dfba34..b4769080e8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -971,6 +971,13 @@ virNWFilterObjTestUnassignDef;
 virNWFilterObjUnlock;
 
 
+# conf/virsavecookie.h
+virSaveCookieFormat;
+virSaveCookieFormatBuf;
+virSaveCookieParse;
+virSaveCookieParseString;
+
+
 # conf/virsecretobj.h
 virSecretLoadAllConfigs;
 virSecretObjDeleteConfig;
-- 
2.13.1