From: Peter Krempa Date: Tue, 11 Apr 2017 14:51:32 +0000 (+0200) Subject: virsh: add helpers for getting domain XML for XPath purposes X-Git-Tag: v3.3.0-rc1~190 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c8a637c7d03b0c9211cbe16d9d88911c495e7162;p=thirdparty%2Flibvirt.git virsh: add helpers for getting domain XML for XPath purposes 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. --- diff --git a/tools/virsh-util.c b/tools/virsh-util.c index 79a38bb234..4b86e29cbd 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 2b5aedfba5..64cef23c02 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -21,6 +21,8 @@ # include "virsh.h" +# include +# include 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 */