3e5111
From 5785b4ff7c33f5e2eb16530f20b6b45d8809aae7 Mon Sep 17 00:00:00 2001
3e5111
Message-Id: <5785b4ff7c33f5e2eb16530f20b6b45d8809aae7@dist-git>
3e5111
From: Jiri Denemark <jdenemar@redhat.com>
3e5111
Date: Fri, 2 Jun 2017 00:44:06 +0200
3e5111
Subject: [PATCH] conf: Introduce virSaveCookie
3e5111
3e5111
The code will be used by snapshots and domain save/restore code to store
3e5111
additional data for a saved running domain. It is analogous to migration
3e5111
cookies, but simple and one way only.
3e5111
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
3e5111
(cherry picked from commit e37daa1fb69ec30042cbc38d489217edb656bc30)
3e5111
3e5111
https://bugzilla.redhat.com/show_bug.cgi?id=1441662
3e5111
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
---
3e5111
 po/POTFILES.in           |   1 +
3e5111
 src/Makefile.am          |   1 +
3e5111
 src/conf/virsavecookie.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++
3e5111
 src/conf/virsavecookie.h |  62 ++++++++++++++++++++
3e5111
 src/libvirt_private.syms |   7 +++
3e5111
 5 files changed, 215 insertions(+)
3e5111
 create mode 100644 src/conf/virsavecookie.c
3e5111
 create mode 100644 src/conf/virsavecookie.h
3e5111
3e5111
diff --git a/po/POTFILES.in b/po/POTFILES.in
3e5111
index 064abd5bbb..cf64115719 100644
3e5111
--- a/po/POTFILES.in
3e5111
+++ b/po/POTFILES.in
3e5111
@@ -47,6 +47,7 @@ src/conf/virdomainobjlist.c
3e5111
 src/conf/virinterfaceobj.c
3e5111
 src/conf/virnodedeviceobj.c
3e5111
 src/conf/virnwfilterobj.c
3e5111
+src/conf/virsavecookie.c
3e5111
 src/conf/virsecretobj.c
3e5111
 src/conf/virstorageobj.c
3e5111
 src/cpu/cpu.c
3e5111
diff --git a/src/Makefile.am b/src/Makefile.am
3e5111
index 75e4344198..53550280bf 100644
3e5111
--- a/src/Makefile.am
3e5111
+++ b/src/Makefile.am
3e5111
@@ -336,6 +336,7 @@ DOMAIN_CONF_SOURCES =						\
3e5111
 		conf/domain_conf.c conf/domain_conf.h		\
3e5111
 		conf/domain_audit.c conf/domain_audit.h		\
3e5111
 		conf/domain_nwfilter.c conf/domain_nwfilter.h	\
3e5111
+		conf/virsavecookie.c conf/virsavecookie.h	\
3e5111
 		conf/snapshot_conf.c conf/snapshot_conf.h	\
3e5111
 		conf/numa_conf.c conf/numa_conf.h	\
3e5111
 		conf/virdomainobjlist.c conf/virdomainobjlist.h
3e5111
diff --git a/src/conf/virsavecookie.c b/src/conf/virsavecookie.c
3e5111
new file mode 100644
3e5111
index 0000000000..502c04d0f4
3e5111
--- /dev/null
3e5111
+++ b/src/conf/virsavecookie.c
3e5111
@@ -0,0 +1,144 @@
3e5111
+/**
3e5111
+ * virsavecookie.c: Save cookie handling
3e5111
+ *
3e5111
+ * Copyright (C) 2017 Red Hat, Inc.
3e5111
+ *
3e5111
+ * This library is free software; you can redistribute it and/or
3e5111
+ * modify it under the terms of the GNU Lesser General Public
3e5111
+ * License as published by the Free Software Foundation; either
3e5111
+ * version 2.1 of the License, or (at your option) any later version.
3e5111
+ *
3e5111
+ * This library is distributed in the hope that it will be useful,
3e5111
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3e5111
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3e5111
+ * Lesser General Public License for more details.
3e5111
+ *
3e5111
+ * You should have received a copy of the GNU Lesser General Public
3e5111
+ * License along with this library.  If not, see
3e5111
+ * <http://www.gnu.org/licenses/>.
3e5111
+ */
3e5111
+
3e5111
+#include <config.h>
3e5111
+
3e5111
+#include "virerror.h"
3e5111
+#include "virlog.h"
3e5111
+#include "virobject.h"
3e5111
+#include "virbuffer.h"
3e5111
+#include "virxml.h"
3e5111
+#include "virsavecookie.h"
3e5111
+
3e5111
+#define VIR_FROM_THIS VIR_FROM_CONF
3e5111
+
3e5111
+VIR_LOG_INIT("conf.savecookie");
3e5111
+
3e5111
+
3e5111
+static int
3e5111
+virSaveCookieParseNode(xmlXPathContextPtr ctxt,
3e5111
+                       virObjectPtr *obj,
3e5111
+                       virSaveCookieCallbacksPtr saveCookie)
3e5111
+{
3e5111
+    *obj = NULL;
3e5111
+
3e5111
+    if (!xmlStrEqual(ctxt->node->name, BAD_CAST "cookie")) {
3e5111
+        virReportError(VIR_ERR_XML_ERROR, "%s",
3e5111
+                       _("XML does not contain expected 'cookie' element"));
3e5111
+        return -1;
3e5111
+    }
3e5111
+
3e5111
+    if (!saveCookie || !saveCookie->parse)
3e5111
+        return 0;
3e5111
+
3e5111
+    return saveCookie->parse(ctxt, obj);
3e5111
+}
3e5111
+
3e5111
+
3e5111
+int
3e5111
+virSaveCookieParse(xmlXPathContextPtr ctxt,
3e5111
+                   virObjectPtr *obj,
3e5111
+                   virSaveCookieCallbacksPtr saveCookie)
3e5111
+{
3e5111
+    xmlNodePtr node = ctxt->node;
3e5111
+    int ret = -1;
3e5111
+
3e5111
+    *obj = NULL;
3e5111
+
3e5111
+    if (!(ctxt->node = virXPathNode("./cookie", ctxt))) {
3e5111
+        ret = 0;
3e5111
+        goto cleanup;
3e5111
+    }
3e5111
+
3e5111
+    ret = virSaveCookieParseNode(ctxt, obj, saveCookie);
3e5111
+
3e5111
+ cleanup:
3e5111
+    ctxt->node = node;
3e5111
+    return ret;
3e5111
+}
3e5111
+
3e5111
+
3e5111
+int
3e5111
+virSaveCookieParseString(const char *xml,
3e5111
+                         virObjectPtr *obj,
3e5111
+                         virSaveCookieCallbacksPtr saveCookie)
3e5111
+{
3e5111
+    xmlDocPtr doc = NULL;
3e5111
+    xmlXPathContextPtr ctxt = NULL;
3e5111
+    int ret = -1;
3e5111
+
3e5111
+    *obj = NULL;
3e5111
+
3e5111
+    if (!xml) {
3e5111
+        ret = 0;
3e5111
+        goto cleanup;
3e5111
+    }
3e5111
+
3e5111
+    if (!(doc = virXMLParseStringCtxt(xml, _("(save cookie)"), &ctxt)))
3e5111
+        goto cleanup;
3e5111
+
3e5111
+    ret = virSaveCookieParseNode(ctxt, obj, saveCookie);
3e5111
+
3e5111
+ cleanup:
3e5111
+    xmlXPathFreeContext(ctxt);
3e5111
+    xmlFreeDoc(doc);
3e5111
+    return ret;
3e5111
+}
3e5111
+
3e5111
+
3e5111
+int
3e5111
+virSaveCookieFormatBuf(virBufferPtr buf,
3e5111
+                       virObjectPtr obj,
3e5111
+                       virSaveCookieCallbacksPtr saveCookie)
3e5111
+{
3e5111
+    if (!obj || !saveCookie || !saveCookie->format)
3e5111
+        return 0;
3e5111
+
3e5111
+    virBufferAddLit(buf, "<cookie>\n");
3e5111
+    virBufferAdjustIndent(buf, 2);
3e5111
+
3e5111
+    if (saveCookie->format(buf, obj) < 0)
3e5111
+        return -1;
3e5111
+
3e5111
+    virBufferAdjustIndent(buf, -2);
3e5111
+    virBufferAddLit(buf, "</cookie>\n");
3e5111
+
3e5111
+    return 0;
3e5111
+}
3e5111
+
3e5111
+
3e5111
+char *
3e5111
+virSaveCookieFormat(virObjectPtr obj,
3e5111
+                    virSaveCookieCallbacksPtr saveCookie)
3e5111
+{
3e5111
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
3e5111
+
3e5111
+    if (virSaveCookieFormatBuf(&buf, obj, saveCookie) < 0)
3e5111
+        goto error;
3e5111
+
3e5111
+    if (virBufferCheckError(&buf) < 0)
3e5111
+        goto error;
3e5111
+
3e5111
+    return virBufferContentAndReset(&buf;;
3e5111
+
3e5111
+ error:
3e5111
+    virBufferFreeAndReset(&buf;;
3e5111
+    return NULL;
3e5111
+}
3e5111
diff --git a/src/conf/virsavecookie.h b/src/conf/virsavecookie.h
3e5111
new file mode 100644
3e5111
index 0000000000..4aed18466c
3e5111
--- /dev/null
3e5111
+++ b/src/conf/virsavecookie.h
3e5111
@@ -0,0 +1,62 @@
3e5111
+/**
3e5111
+ * virsavecookie.h: Save cookie handling
3e5111
+ *
3e5111
+ * Copyright (C) 2017 Red Hat, Inc.
3e5111
+ *
3e5111
+ * This library is free software; you can redistribute it and/or
3e5111
+ * modify it under the terms of the GNU Lesser General Public
3e5111
+ * License as published by the Free Software Foundation; either
3e5111
+ * version 2.1 of the License, or (at your option) any later version.
3e5111
+ *
3e5111
+ * This library is distributed in the hope that it will be useful,
3e5111
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3e5111
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3e5111
+ * Lesser General Public License for more details.
3e5111
+ *
3e5111
+ * You should have received a copy of the GNU Lesser General Public
3e5111
+ * License along with this library.  If not, see
3e5111
+ * <http://www.gnu.org/licenses/>.
3e5111
+ */
3e5111
+#ifndef __VIR_SAVE_COOKIE_H__
3e5111
+# define __VIR_SAVE_COOKIE_H__
3e5111
+
3e5111
+# include <libxml/xpath.h>
3e5111
+
3e5111
+# include "internal.h"
3e5111
+# include "virobject.h"
3e5111
+# include "virbuffer.h"
3e5111
+
3e5111
+
3e5111
+typedef int (*virSaveCookieParseFunc)(xmlXPathContextPtr ctxt,
3e5111
+                                      virObjectPtr *obj);
3e5111
+typedef int (*virSaveCookieFormatFunc)(virBufferPtr buf,
3e5111
+                                       virObjectPtr obj);
3e5111
+
3e5111
+typedef struct _virSaveCookieCallbacks virSaveCookieCallbacks;
3e5111
+typedef virSaveCookieCallbacks *virSaveCookieCallbacksPtr;
3e5111
+struct _virSaveCookieCallbacks {
3e5111
+    virSaveCookieParseFunc parse;
3e5111
+    virSaveCookieFormatFunc format;
3e5111
+};
3e5111
+
3e5111
+
3e5111
+int
3e5111
+virSaveCookieParse(xmlXPathContextPtr ctxt,
3e5111
+                   virObjectPtr *obj,
3e5111
+                   virSaveCookieCallbacksPtr saveCookie);
3e5111
+
3e5111
+int
3e5111
+virSaveCookieParseString(const char *xml,
3e5111
+                         virObjectPtr *obj,
3e5111
+                         virSaveCookieCallbacksPtr saveCookie);
3e5111
+
3e5111
+int
3e5111
+virSaveCookieFormatBuf(virBufferPtr buf,
3e5111
+                       virObjectPtr obj,
3e5111
+                       virSaveCookieCallbacksPtr saveCookie);
3e5111
+
3e5111
+char *
3e5111
+virSaveCookieFormat(virObjectPtr obj,
3e5111
+                    virSaveCookieCallbacksPtr saveCookie);
3e5111
+
3e5111
+#endif /*__VIR_SAVE_COOKIE_H__ */
3e5111
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
3e5111
index a578dfba34..b4769080e8 100644
3e5111
--- a/src/libvirt_private.syms
3e5111
+++ b/src/libvirt_private.syms
3e5111
@@ -971,6 +971,13 @@ virNWFilterObjTestUnassignDef;
3e5111
 virNWFilterObjUnlock;
3e5111
 
3e5111
 
3e5111
+# conf/virsavecookie.h
3e5111
+virSaveCookieFormat;
3e5111
+virSaveCookieFormatBuf;
3e5111
+virSaveCookieParse;
3e5111
+virSaveCookieParseString;
3e5111
+
3e5111
+
3e5111
 # conf/virsecretobj.h
3e5111
 virSecretLoadAllConfigs;
3e5111
 virSecretObjDeleteConfig;
3e5111
-- 
3e5111
2.13.1
3e5111