]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: xml: Extract XPath evaluation for strings
authorPeter Krempa <pkrempa@redhat.com>
Wed, 5 Oct 2022 07:46:11 +0000 (09:46 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 1 Nov 2022 12:07:20 +0000 (13:07 +0100)
Extract the internals of virXPathString which evaluate the XPath and
validate that the returned object is a string into a new helper named
'virXPathEvalString'.

The function will be later reused in the number XPath evaluation
functions.

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

index e771471dd7a89d3be0f6c6c9bffdea6e28e9b0a7..aec475ccfdc12ffc717a143312efbef4f1fccd3d 100644 (file)
@@ -60,6 +60,34 @@ virXMLXPathContextNew(xmlDocPtr xml)
 }
 
 
+static xmlXPathObject *
+virXPathEvalString(const char *xpath,
+                   xmlXPathContextPtr ctxt)
+{
+    g_autoptr(xmlXPathObject) obj = NULL;
+
+    if (!xpath) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing XPath expression"));
+        return NULL;
+    }
+
+    if (!ctxt) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing XPath context"));
+        return NULL;
+    }
+
+    if (!(obj = xmlXPathEval(BAD_CAST xpath, ctxt)))
+        return NULL;
+
+    if (obj->type != XPATH_STRING ||
+        !obj->stringval ||
+        obj->stringval[0] == '\0')
+        return NULL;
+
+    return g_steal_pointer(&obj);
+}
+
+
 /**
  * virXPathString:
  * @xpath: the XPath string to evaluate
@@ -76,16 +104,9 @@ virXPathString(const char *xpath,
 {
     g_autoptr(xmlXPathObject) obj = NULL;
 
-    if ((ctxt == NULL) || (xpath == NULL)) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       "%s", _("Invalid parameter to virXPathString()"));
+    if (!(obj = virXPathEvalString(xpath, ctxt)))
         return NULL;
-    }
-    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
-    if ((obj == NULL) || (obj->type != XPATH_STRING) ||
-        (obj->stringval == NULL) || (obj->stringval[0] == 0)) {
-        return NULL;
-    }
+
     return g_strdup((char *)obj->stringval);
 }