]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: XML: Introduce automatic reset of XPath's current node
authorPeter Krempa <pkrempa@redhat.com>
Tue, 26 Feb 2019 16:34:57 +0000 (17:34 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 4 Mar 2019 12:04:20 +0000 (13:04 +0100)
Quite a few parts modify the XPath context current node to shift the
scope and allow easier queries. This also means that the node needs
to be restored afterwards.

Introduce a macro based on 'VIR_AUTOCLEAN' which adds a local structure
on the stack remembering the original node along with a function which
will make sure that the node is reset when the local structure leaves
scope.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/libvirt_private.syms
src/util/virxml.c
src/util/virxml.h

index e68e3f3a3b9caa9bf466fc48daf2f98c6c67c4c2..770a9213574decc3274f6da3fdc9ac56d18060cf 100644 (file)
@@ -3247,6 +3247,7 @@ virXMLValidatorFree;
 virXMLValidatorInit;
 virXMLValidatorValidate;
 virXPathBoolean;
+virXPathContextNodeRestore;
 virXPathInt;
 virXPathLong;
 virXPathLongHex;
index 8eb201fddfab123ddb028b8e1160820f694c6bde..f55b9a362c65c4c031ac1bc27ca8eba837e82db4 100644 (file)
@@ -1398,3 +1398,13 @@ virXMLFormatElement(virBufferPtr buf,
     virBufferFreeAndReset(childBuf);
     return ret;
 }
+
+
+void
+virXPathContextNodeRestore(virXPathContextNodeSavePtr save)
+{
+    if (!save->ctxt)
+        return;
+
+    save->ctxt->node = save->node;
+}
index 23a4da1b7e93367eccc4c42f5f15bb88f19c6c99..78a1e7fa5ef71736b17de85ab683d24c15eaca5b 100644 (file)
@@ -219,4 +219,28 @@ virXMLFormatElement(virBufferPtr buf,
                     virBufferPtr attrBuf,
                     virBufferPtr childBuf);
 
+struct _virXPathContextNodeSave {
+    xmlXPathContextPtr ctxt;
+    xmlNodePtr node;
+};
+typedef struct _virXPathContextNodeSave virXPathContextNodeSave;
+typedef virXPathContextNodeSave *virXPathContextNodeSavePtr;
+
+void
+virXPathContextNodeRestore(virXPathContextNodeSavePtr save);
+
+VIR_DEFINE_AUTOCLEAN_FUNC(virXPathContextNodeSave, virXPathContextNodeRestore);
+
+/**
+ * VIR_XPATH_NODE_AUTORESTORE:
+ * @ctxt: XML XPath context pointer
+ *
+ * This macro ensures that when the scope where it's used ends, @ctxt's current
+ * node pointer is reset to the original value when this macro was used.
+ */
+# define VIR_XPATH_NODE_AUTORESTORE(_ctxt) \
+    VIR_AUTOCLEAN(virXPathContextNodeSave) _ctxt ## CtxtSave = { .ctxt = _ctxt,\
+                                                                 .node = _ctxt->node}; \
+    ignore_value(&_ctxt ## CtxtSave)
+
 #endif /* LIBVIRT_VIRXML_H */