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@xxxxxxxxxx> --- src/vbox/vbox_snapshot_conf.c | 116 +++++++++++----------------------- 1 file changed, 36 insertions(+), 80 deletions(-) diff --git a/src/vbox/vbox_snapshot_conf.c b/src/vbox/vbox_snapshot_conf.c index e175f1964e..2f3f48d688 100644 --- a/src/vbox/vbox_snapshot_conf.c +++ b/src/vbox/vbox_snapshot_conf.c @@ -1187,60 +1187,55 @@ virVBoxSnapshotConfIsCurrentSnapshot(virVBoxSnapshotConfMachine *machine, return STREQ(snapshot->uuid, machine->currentSnapshot); } -/* - *getRWDisksPathsFromLibvirtXML: Parse a libvirt XML snapshot file, allocates and - *fills a list of read-write disk paths. - *return array length on success, -1 on failure. - */ -int -virVBoxSnapshotConfGetRWDisksPathsFromLibvirtXML(const char *filePath, - char ***rwDisksPath) +static int +virVBoxSnapshotConfGetDisksPathsFromLibvirtXML(const char *filePath, + char ***disksPath, + const char *xpath) { - int result = -1; size_t i = 0; - g_auto(GStrv) ret = NULL; g_autoptr(xmlDoc) xml = NULL; g_autoptr(xmlXPathContext) xPathContext = NULL; - xmlNodePtr *nodes = NULL; + g_autofree xmlNodePtr *nodes = NULL; int nodeSize = 0; - *rwDisksPath = NULL; + + *disksPath = 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; + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("filePath is null")); + return -1; } - if ((nodeSize = virXPathNodeSet("/domainsnapshot/disks/disk", - xPathContext, &nodes)) < 0) - goto cleanup; + if (!(xml = virXMLParse(filePath, NULL, NULL, NULL, &xPathContext, NULL, false))) + return -1; + + if ((nodeSize = virXPathNodeSet(xpath, xPathContext, &nodes)) < 0) + return -1; - ret = g_new0(char *, nodeSize); + *disksPath = 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"); + xPathContext->node = nodes[i]; + (*disksPath)[i] = virXPathString("string(./source/@file)", xPathContext); } - *rwDisksPath = g_steal_pointer(&ret); - result = 0; - cleanup: - if (result < 0) - nodeSize = -1; - VIR_FREE(nodes); return nodeSize; } + +/* + *getRWDisksPathsFromLibvirtXML: Parse a libvirt XML snapshot file, allocates and + *fills a list of read-write disk paths. + *return array length on success, -1 on failure. + */ +int +virVBoxSnapshotConfGetRWDisksPathsFromLibvirtXML(const char *filePath, + char ***rwDisksPath) +{ + return virVBoxSnapshotConfGetDisksPathsFromLibvirtXML(filePath, rwDisksPath, + "/domainsnapshot/disks/disk"); +} + + /* *getRODisksPathsFromLibvirtXML: *Parse a libvirt XML snapshot file, allocates and fills *a list of read-only disk paths (the parents of the read-write disks). @@ -1250,50 +1245,11 @@ int virVBoxSnapshotConfGetRODisksPathsFromLibvirtXML(const char *filePath, char ***roDisksPath) { - int result = -1; - size_t i = 0; - 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; + return virVBoxSnapshotConfGetDisksPathsFromLibvirtXML(filePath, roDisksPath, + "/domainsnapshot/domain/devices/disk"); } + /* *hardDiskUuidByLocation: Return the uuid of the hard disk whose location is 'location' *return a valid uuid, or NULL on failure -- 2.37.3