]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virVBoxSnapshotConfGet(RW|RO)DisksPathsFromLibvirtXML: Refactor
authorPeter Krempa <pkrempa@redhat.com>
Thu, 22 Sep 2022 13:26:13 +0000 (15:26 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 6 Oct 2022 08:54:24 +0000 (10:54 +0200)
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>
src/vbox/vbox_snapshot_conf.c

index e175f1964e7f5376e38fc7ae7d6f68ec8dd3d172..2f3f48d68846c8451c2cda6ffc8352fa4c2c15ca 100644 (file)
@@ -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