mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-08 12:41:29 +00:00
virVBoxSnapshotConfGet(RW|RO)DisksPathsFromLibvirtXML: Refactor
virVBoxSnapshotConfGetRWDisksPathsFromLibvirtXML and virVBoxSnapshotConfGetRODisksPathsFromLibvirtXML were doing the same thing, except for one XPath query. Factor out the common code into a helper and bring it up to modern standard. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
366cb6d35a
commit
3761a67625
@ -1187,6 +1187,41 @@ virVBoxSnapshotConfIsCurrentSnapshot(virVBoxSnapshotConfMachine *machine,
|
|||||||
return STREQ(snapshot->uuid, machine->currentSnapshot);
|
return STREQ(snapshot->uuid, machine->currentSnapshot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
virVBoxSnapshotConfGetDisksPathsFromLibvirtXML(const char *filePath,
|
||||||
|
char ***disksPath,
|
||||||
|
const char *xpath)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
g_autoptr(xmlDoc) xml = NULL;
|
||||||
|
g_autoptr(xmlXPathContext) xPathContext = NULL;
|
||||||
|
g_autofree xmlNodePtr *nodes = NULL;
|
||||||
|
int nodeSize = 0;
|
||||||
|
|
||||||
|
*disksPath = NULL;
|
||||||
|
|
||||||
|
if (filePath == NULL) {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("filePath is null"));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(xml = virXMLParse(filePath, NULL, NULL, NULL, &xPathContext, NULL, false)))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if ((nodeSize = virXPathNodeSet(xpath, xPathContext, &nodes)) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
*disksPath = g_new0(char *, nodeSize);
|
||||||
|
|
||||||
|
for (i = 0; i < nodeSize; i++) {
|
||||||
|
xPathContext->node = nodes[i];
|
||||||
|
(*disksPath)[i] = virXPathString("string(./source/@file)", xPathContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodeSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*getRWDisksPathsFromLibvirtXML: Parse a libvirt XML snapshot file, allocates and
|
*getRWDisksPathsFromLibvirtXML: Parse a libvirt XML snapshot file, allocates and
|
||||||
*fills a list of read-write disk paths.
|
*fills a list of read-write disk paths.
|
||||||
@ -1196,51 +1231,11 @@ int
|
|||||||
virVBoxSnapshotConfGetRWDisksPathsFromLibvirtXML(const char *filePath,
|
virVBoxSnapshotConfGetRWDisksPathsFromLibvirtXML(const char *filePath,
|
||||||
char ***rwDisksPath)
|
char ***rwDisksPath)
|
||||||
{
|
{
|
||||||
int result = -1;
|
return virVBoxSnapshotConfGetDisksPathsFromLibvirtXML(filePath, rwDisksPath,
|
||||||
size_t i = 0;
|
"/domainsnapshot/disks/disk");
|
||||||
g_auto(GStrv) ret = NULL;
|
|
||||||
g_autoptr(xmlDoc) xml = NULL;
|
|
||||||
g_autoptr(xmlXPathContext) xPathContext = NULL;
|
|
||||||
xmlNodePtr *nodes = NULL;
|
|
||||||
int nodeSize = 0;
|
|
||||||
*rwDisksPath = NULL;
|
|
||||||
if (filePath == NULL) {
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
||||||
_("filePath is null"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
xml = virXMLParse(filePath, NULL, NULL, NULL, &xPathContext, NULL, false);
|
|
||||||
if (xml == NULL) {
|
|
||||||
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
||||||
_("Unable to parse the xml"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((nodeSize = virXPathNodeSet("/domainsnapshot/disks/disk",
|
|
||||||
xPathContext, &nodes)) < 0)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
ret = g_new0(char *, nodeSize);
|
|
||||||
|
|
||||||
for (i = 0; i < nodeSize; i++) {
|
|
||||||
xmlNodePtr node = nodes[i];
|
|
||||||
xmlNodePtr sourceNode;
|
|
||||||
|
|
||||||
xPathContext->node = node;
|
|
||||||
sourceNode = virXPathNode("./source", xPathContext);
|
|
||||||
if (sourceNode)
|
|
||||||
ret[i] = virXMLPropString(sourceNode, "file");
|
|
||||||
}
|
|
||||||
*rwDisksPath = g_steal_pointer(&ret);
|
|
||||||
result = 0;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
if (result < 0)
|
|
||||||
nodeSize = -1;
|
|
||||||
VIR_FREE(nodes);
|
|
||||||
return nodeSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*getRODisksPathsFromLibvirtXML: *Parse a libvirt XML snapshot file, allocates and fills
|
*getRODisksPathsFromLibvirtXML: *Parse a libvirt XML snapshot file, allocates and fills
|
||||||
*a list of read-only disk paths (the parents of the read-write disks).
|
*a list of read-only disk paths (the parents of the read-write disks).
|
||||||
@ -1250,50 +1245,11 @@ int
|
|||||||
virVBoxSnapshotConfGetRODisksPathsFromLibvirtXML(const char *filePath,
|
virVBoxSnapshotConfGetRODisksPathsFromLibvirtXML(const char *filePath,
|
||||||
char ***roDisksPath)
|
char ***roDisksPath)
|
||||||
{
|
{
|
||||||
int result = -1;
|
return virVBoxSnapshotConfGetDisksPathsFromLibvirtXML(filePath, roDisksPath,
|
||||||
size_t i = 0;
|
"/domainsnapshot/domain/devices/disk");
|
||||||
g_auto(GStrv) ret = NULL;
|
|
||||||
g_autoptr(xmlDoc) xml = NULL;
|
|
||||||
g_autoptr(xmlXPathContext) xPathContext = NULL;
|
|
||||||
xmlNodePtr *nodes = NULL;
|
|
||||||
int nodeSize = 0;
|
|
||||||
if (filePath == NULL) {
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
||||||
_("filePath is null"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
xml = virXMLParse(filePath, NULL, NULL, NULL, &xPathContext, NULL, false);
|
|
||||||
if (xml == NULL) {
|
|
||||||
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
||||||
_("Unable to parse the xml"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((nodeSize = virXPathNodeSet("/domainsnapshot/domain/devices/disk",
|
|
||||||
xPathContext,
|
|
||||||
&nodes)) < 0)
|
|
||||||
goto cleanup;
|
|
||||||
ret = g_new0(char *, nodeSize);
|
|
||||||
|
|
||||||
for (i = 0; i < nodeSize; i++) {
|
|
||||||
xmlNodePtr node = nodes[i];
|
|
||||||
xmlNodePtr sourceNode;
|
|
||||||
|
|
||||||
xPathContext->node = node;
|
|
||||||
sourceNode = virXPathNode("./source", xPathContext);
|
|
||||||
if (sourceNode)
|
|
||||||
ret[i] = virXMLPropString(sourceNode, "file");
|
|
||||||
}
|
|
||||||
*roDisksPath = g_steal_pointer(&ret);
|
|
||||||
result = 0;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
if (result < 0)
|
|
||||||
nodeSize = -1;
|
|
||||||
VIR_FREE(nodes);
|
|
||||||
return nodeSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*hardDiskUuidByLocation: Return the uuid of the hard disk whose location is 'location'
|
*hardDiskUuidByLocation: Return the uuid of the hard disk whose location is 'location'
|
||||||
*return a valid uuid, or NULL on failure
|
*return a valid uuid, or NULL on failure
|
||||||
|
Loading…
x
Reference in New Issue
Block a user