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