]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virStorageBackendISCSIDirectFindPoolSources: Use allocated virStoragePoolSourceList
authorPeter Krempa <pkrempa@redhat.com>
Fri, 18 Jun 2021 12:01:17 +0000 (14:01 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 21 Jun 2021 08:46:35 +0000 (10:46 +0200)
Using an allocated version together with copying the
host/initiator/device portions into it allows us to switch to automatic
clearing rather than open-coding it.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/storage/storage_backend_iscsi_direct.c

index e4a14c3fd640900762976f7111d3349546d3bd29..263db835ae29042b222793f32ce60fd76b0a9ba8 100644 (file)
@@ -495,23 +495,21 @@ virStorageBackendISCSIDirectFindPoolSources(const char *srcSpec,
     char **targets = NULL;
     char *ret = NULL;
     size_t i;
-    virStoragePoolSourceList list = {
-        .type = VIR_STORAGE_POOL_ISCSI_DIRECT,
-        .nsources = 0,
-        .sources = NULL
-    };
+    g_autoptr(virStoragePoolSourceList) list = g_new0(virStoragePoolSourceList, 1);
     g_autofree char *portal = NULL;
     g_autoptr(virStoragePoolSource) source = NULL;
 
     virCheckFlags(0, NULL);
 
+    list->type = VIR_STORAGE_POOL_ISCSI_DIRECT;
+
     if (!srcSpec) {
         virReportError(VIR_ERR_INVALID_ARG, "%s",
                        _("hostname must be specified for iscsi sources"));
         return NULL;
     }
 
-    if (!(source = virStoragePoolDefParseSourceString(srcSpec, list.type)))
+    if (!(source = virStoragePoolDefParseSourceString(srcSpec, list->type)))
         return NULL;
 
     if (source->nhost != 1) {
@@ -532,30 +530,28 @@ virStorageBackendISCSIDirectFindPoolSources(const char *srcSpec,
     if (virISCSIDirectScanTargets(source->initiator.iqn, portal, &ntargets, &targets) < 0)
         goto cleanup;
 
-    list.sources = g_new0(virStoragePoolSource, ntargets);
+    list->sources = g_new0(virStoragePoolSource, ntargets);
 
     for (i = 0; i < ntargets; i++) {
-        list.sources[i].devices = g_new0(virStoragePoolSourceDevice, 1);
-        list.sources[i].hosts = g_new0(virStoragePoolSourceHost, 1);
-        list.sources[i].nhost = 1;
-        list.sources[i].hosts[0] = source->hosts[0];
-        list.sources[i].initiator = source->initiator;
-        list.sources[i].ndevice = 1;
-        list.sources[i].devices[0].path = targets[i];
-        list.nsources++;
+        list->sources[i].hosts = g_new0(virStoragePoolSourceHost, 1);
+        list->sources[i].nhost = 1;
+        list->sources[i].hosts[0].name = g_strdup(source->hosts[0].name);
+        list->sources[i].hosts[0].port = source->hosts[0].port;
+
+        virStorageSourceInitiatorCopy(&list->sources[i].initiator,
+                                      &source->initiator);
+
+        list->sources[i].devices = g_new0(virStoragePoolSourceDevice, 1);
+        list->sources[i].ndevice = 1;
+        list->sources[i].devices[0].path = g_strdup(targets[i]);
+
+        list->nsources++;
     }
 
-    if (!(ret = virStoragePoolSourceListFormat(&list)))
+    if (!(ret = virStoragePoolSourceListFormat(list)))
         goto cleanup;
 
  cleanup:
-    if (list.sources) {
-        for (i = 0; i < ntargets; i++) {
-            VIR_FREE(list.sources[i].hosts);
-            VIR_FREE(list.sources[i].devices);
-        }
-        VIR_FREE(list.sources);
-    }
     for (i = 0; i < ntargets; i++)
         VIR_FREE(targets[i]);
     VIR_FREE(targets);