virsh: virshMakeCloneXML: Use virXPathNode instead of xmlXPathEval

Refactor the code to use the XPath helpers instead of open-coding them.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-12-01 09:39:30 +01:00
parent 5f3d21abf8
commit e9b7f06140

View File

@ -505,28 +505,22 @@ cmdVolCreateFrom(vshControl *ctl, const vshCmd *cmd)
return true; return true;
} }
static xmlChar * static char *
virshMakeCloneXML(const char *origxml, const char *newname) virshMakeCloneXML(const char *origxml, const char *newname)
{ {
g_autoptr(xmlDoc) doc = NULL; g_autoptr(xmlDoc) doc = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL; g_autoptr(xmlXPathContext) ctxt = NULL;
g_autoptr(xmlXPathObject) obj = NULL; xmlNodePtr node;
xmlChar *newxml = NULL;
int size;
doc = virXMLParseStringCtxt(origxml, _("(volume_definition)"), &ctxt); if (!(doc = virXMLParseStringCtxt(origxml, _("(volume_definition)"), &ctxt)))
if (!doc)
return NULL; return NULL;
obj = xmlXPathEval(BAD_CAST "/volume/name", ctxt); if (!(node = virXPathNode("/volume/name", ctxt)))
if (obj == NULL || obj->nodesetval == NULL ||
obj->nodesetval->nodeTab == NULL)
return NULL; return NULL;
xmlNodeSetContent(obj->nodesetval->nodeTab[0], (const xmlChar *)newname); xmlNodeSetContent(node, (const xmlChar *)newname);
xmlDocDumpMemory(doc, &newxml, &size);
return newxml; return virXMLNodeToString(doc, doc->children);
} }
/* /*
@ -574,7 +568,7 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd)
g_autoptr(virshStorageVol) newvol = NULL; g_autoptr(virshStorageVol) newvol = NULL;
const char *name = NULL; const char *name = NULL;
g_autofree char *origxml = NULL; g_autofree char *origxml = NULL;
g_autofree xmlChar *newxml = NULL; g_autofree char *newxml = NULL;
unsigned int flags = 0; unsigned int flags = 0;
if (!(origvol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) if (!(origvol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL)))
@ -608,8 +602,7 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd)
return true; return true;
} }
if (!(newvol = virStorageVolCreateXMLFrom(origpool, (char *) newxml, if (!(newvol = virStorageVolCreateXMLFrom(origpool, newxml, origvol, flags))) {
origvol, flags))) {
vshError(ctl, _("Failed to clone vol from %s"), vshError(ctl, _("Failed to clone vol from %s"),
virStorageVolGetName(origvol)); virStorageVolGetName(origvol));
return false; return false;