On 05/15/2018 01:43 PM, Daniel P. Berrangé wrote:
Introduce a new struct to act as the stateful owner of the
virNWFilterBindingDefPtr objects.
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
src/conf/Makefile.inc.am | 2 +
src/conf/virnwfilterbindingobj.c | 260 +++++++++++++++++++++++++++++++
src/conf/virnwfilterbindingobj.h | 60 +++++++
src/libvirt_private.syms | 10 ++
4 files changed, 332 insertions(+)
create mode 100644 src/conf/virnwfilterbindingobj.c
create mode 100644 src/conf/virnwfilterbindingobj.h
[...]
diff --git a/src/conf/virnwfilterbindingobj.c
b/src/conf/virnwfilterbindingobj.c
new file mode 100644
index 0000000000..15aaf89b5c
--- /dev/null
+++ b/src/conf/virnwfilterbindingobj.c
@@ -0,0 +1,260 @@
+/*
+ * virnwfilterbindingobj.c: network filter binding XML processing
binding object processing
+ *
+ * Copyright (C) 2018 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 "viralloc.h"
+#include "virerror.h"
+#include "virstring.h"
+#include "nwfilter_params.h"
+#include "virnwfilterbindingobj.h"
+#include "viruuid.h"
+#include "virfile.h"
+
+
+#define VIR_FROM_THIS VIR_FROM_NWFILTER
+
+static virClassPtr virNWFilterBindingObjClass;
+static void virNWFilterBindingObjDispose(void *obj);
+
+static int virNWFilterBindingObjOnceInit(void)
static int
virNWFilterBindingObjOnceInit(void)
+{
+ if (!VIR_CLASS_NEW(virNWFilterBindingObj, virClassForObjectLockable()))
+ return -1;
+
+ return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virNWFilterBindingObj)
+
+virNWFilterBindingObjPtr
+virNWFilterBindingObjNew(void)
+{
+ if (virNWFilterBindingObjInitialize() < 0)
+ return NULL;
+
+ return virObjectNew(virNWFilterBindingObjClass);
+}
+
+void
static void
+virNWFilterBindingObjDispose(void *obj)
+{
+ virNWFilterBindingObjPtr bobj = obj;
+
+ virNWFilterBindingDefFree(bobj->def);
+}
+
+
+/**
+ * virNWFilterBindingnObjEndAPI:
BindingObj (e.g. no 'n' between g and O)
+ * @obj: binding object
+ *
+ * Finish working with a binding object in an API. This function
+ * clears whatever was left of a domain that was gathered using
+ * virNWFilterBindingObjListFindByPortDev(). Currently that means
+ * only unlocking and decrementing the reference counter of that
+ * object. And in order to make sure the caller does not access
+ * the object, the pointer is cleared.
+ */
[...]
+static virNWFilterBindingObjPtr
+virNWFilterBindingObjParseXML(xmlDocPtr doc,
+ xmlXPathContextPtr ctxt)
+{
+ virNWFilterBindingObjPtr ret;
+ xmlNodePtr node;
+
+ if (VIR_ALLOC(ret) < 0)
Use virNWFilterBindingObjNew especially since it's Unref'd later.
+ return NULL;
+
+ if (!(node = virXPathNode("./filterbinding", ctxt))) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ "%s", _("filter binding status missing
binding"));
error message reads strange with binding duplicated.... usually I see
the "%s" on the previous line too.
+ goto cleanup;
+ }
+
+ if (!(ret->def = virNWFilterBindingDefParseNode(doc, node)))
+ goto cleanup;
+
+ return ret;
+
+ cleanup:
+ virObjectUnref(ret);
+ return NULL;
+}
+
+
+static virNWFilterBindingObjPtr
+virNWFilterBindingObjParseNode(xmlDocPtr doc,
+ xmlNodePtr root)
+{
+ xmlXPathContextPtr ctxt = NULL;
+ virNWFilterBindingObjPtr obj = NULL;
+
+ if (STRNEQ((const char *)root->name, "filterbindingb")) {
filterbinding (e.g. no ending b)
+ virReportError(VIR_ERR_XML_ERROR,
+ "%s",
+ _("unknown root element for nw filter"));
%s on previous line and the message probably needs to be "unknown root
element for filter binding"
+ goto cleanup;
+ }
+
+ ctxt = xmlXPathNewContext(doc);
+ if (ctxt == NULL) {
+ virReportOOMError();
+ goto cleanup;
+ }
+
+ ctxt->node = root;
+ obj = virNWFilterBindingObjParseXML(doc, ctxt);
+
+ cleanup:
+ xmlXPathFreeContext(ctxt);
+ return obj;
+}
+
+
+static virNWFilterBindingObjPtr
+virNWFilterBindingObjParse(const char *xmlStr,
+ const char *filename)
+{
+ virNWFilterBindingObjPtr obj = NULL;
+ xmlDocPtr xml;
+
+ if ((xml = virXMLParse(filename, xmlStr, _("(nwfilterbinding_status)"))))
{
+ obj = virNWFilterBindingObjParseNode(xml, xmlDocGetRootElement(xml));
+ xmlFreeDoc(xml);
+ }
+
+ return obj;
+}
+
+
+
1 less empty line.
+virNWFilterBindingObjPtr
+virNWFilterBindingObjParseFile(const char *filename)
+{
+ return virNWFilterBindingObjParse(NULL, filename);
+}
+
+
+char *
+virNWFilterBindingObjFormat(const virNWFilterBindingObj *obj)
Even by the end of the series, the only caller to this
(virNWFilterBindingObjSave) is from within this module... so probably
could just become static and move up above... and remove from .h file.
+{
+ virBuffer buf = VIR_BUFFER_INITIALIZER;
+
+ virBufferAddLit(&buf, "<filterbindingstatus>\n");
+
+ virBufferAdjustIndent(&buf, 2);
+
+ if (virNWFilterBindingDefFormatBuf(&buf, obj->def) < 0) {
+ virBufferFreeAndReset(&buf);
+ return NULL;
+ }
+
+ virBufferAdjustIndent(&buf, -2);
+ virBufferAddLit(&buf, "</filterbindingstatus>\n");
+
+ if (virBufferCheckError(&buf) < 0)
+ return NULL;
+
+ return virBufferContentAndReset(&buf);
+}
diff --git a/src/conf/virnwfilterbindingobj.h b/src/conf/virnwfilterbindingobj.h
new file mode 100644
index 0000000000..e0f1806ca0
--- /dev/null
+++ b/src/conf/virnwfilterbindingobj.h
@@ -0,0 +1,60 @@
+/*
+ * virnwfilterbindingobj.h: network filter binding XML processing
binding object processing
+ *
+ * Copyright (C) 2018 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_NWFILTER_BINDING_OBJ_H
+# define VIR_NWFILTER_BINDING_OBJ_H
+
+# include "internal.h"
+# include "virnwfilterbindingdef.h"
+# include "virobject.h"
+
+typedef struct _virNWFilterBindingObj virNWFilterBindingObj;
+typedef virNWFilterBindingObj *virNWFilterBindingObjPtr;
+
+struct _virNWFilterBindingObj {
+ virObjectLockable parent;
+
+ bool removing;
+ virNWFilterBindingDefPtr def;
+};
This should be inside the .c file to avoid consumers from referencing it
directly and accessors created for *GetDef and *IsRemoving.
+
+virNWFilterBindingObjPtr
+virNWFilterBindingObjNew(void);
[...]
John