提交 c8a637c7 编写于 作者: P Peter Krempa

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.
上级 4f4c3b13
......@@ -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;
}
......@@ -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 */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册