In virsh we quite often get the domain XML just to initialize the XPath
parser so that we can extract information.
Add helpers which will simplify this by wrapping the getting of the XML
and parsing it along with error reporting.
Additionally a second helper also gets the domain object from the
parameters and releases it so that functions which need the XML as only
source of data can be simplified further.
---
tools/virsh-util.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
tools/virsh-util.h | 20 ++++++++++++++++++++
2 files changed, 68 insertions(+)
diff --git a/tools/virsh-util.c b/tools/virsh-util.c
index 79a38bb23..4b86e29cb 100644
--- a/tools/virsh-util.c
+++ b/tools/virsh-util.c
@@ -22,6 +22,7 @@
#include "virfile.h"
#include "virstring.h"
+#include "viralloc.h"
static virDomainPtr
virshLookupDomainInternal(vshControl *ctl,
@@ -172,3 +173,50 @@ virshDomainSnapshotFree(virDomainSnapshotPtr snap)
vshSaveLibvirtHelperError();
virDomainSnapshotFree(snap); /* sc_prohibit_obj_free_apis_in_virsh */
}
+
+
+int
+virshDomainGetXMLFromDom(vshControl *ctl,
+ virDomainPtr dom,
+ unsigned int flags,
+ xmlDocPtr *xml,
+ xmlXPathContextPtr *ctxt)
+{
+ char *desc = NULL;
+
+ if (!(desc = virDomainGetXMLDesc(dom, flags))) {
+ vshError(ctl, _("Failed to get domain description xml"));
+ return -1;
+ }
+
+ *xml = virXMLParseStringCtxt(desc, _("(domain_definition)"), ctxt);
+ VIR_FREE(desc);
+
+ if (!(*xml)) {
+ vshError(ctl, _("Failed to parse domain description xml"));
+ return -1;
+ }
+
+ return 0;
+}
+
+
+int
+virshDomainGetXML(vshControl *ctl,
+ const vshCmd *cmd,
+ unsigned int flags,
+ xmlDocPtr *xml,
+ xmlXPathContextPtr *ctxt)
+{
+ virDomainPtr dom;
+ int ret;
+
+ if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+ return -1;
+
+ ret = virshDomainGetXMLFromDom(ctl, dom, flags, xml, ctxt);
+
+ virshDomainFree(dom);
+
+ return ret;
+}
diff --git a/tools/virsh-util.h b/tools/virsh-util.h
index 2b5aedfba..64cef23c0 100644
--- a/tools/virsh-util.h
+++ b/tools/virsh-util.h
@@ -21,6 +21,8 @@
# include "virsh.h"
+# include <libxml/parser.h>
+# include <libxml/xpath.h>
virDomainPtr
virshLookupDomainBy(vshControl *ctl,
@@ -55,4 +57,22 @@ virshStreamSink(virStreamPtr st,
size_t nbytes,
void *opaque);
+int
+virshDomainGetXMLFromDom(vshControl *ctl,
+ virDomainPtr dom,
+ unsigned int flags,
+ xmlDocPtr *xml,
+ xmlXPathContextPtr *ctxt)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4)
+ ATTRIBUTE_NONNULL(5) ATTRIBUTE_RETURN_CHECK;
+
+int
+virshDomainGetXML(vshControl *ctl,
+ const vshCmd *cmd,
+ unsigned int flags,
+ xmlDocPtr *xml,
+ xmlXPathContextPtr *ctxt)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4)
+ ATTRIBUTE_NONNULL(5) ATTRIBUTE_RETURN_CHECK;
+
#endif /* VIRSH_UTIL_H */
--
2.12.2