]> xenbits.xensource.com Git - libvirt.git/commitdiff
virDomainDiskSourcePoolDefParse: Refactor cleanup
authorPeter Krempa <pkrempa@redhat.com>
Thu, 12 Oct 2023 13:07:52 +0000 (15:07 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 17 Oct 2023 12:16:14 +0000 (14:16 +0200)
Register autoptr cleanup function for virStorageSourcePoolDef and
refactor the parser to simplify the logic.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/conf/domain_conf.c
src/conf/storage_source_conf.h

index d7f167a469991fa39d0080a1908dc54b653527a7..3e0989e2e8b9f4bc6dd059e61783f7b3f8fb9e2d 100644 (file)
@@ -7005,44 +7005,31 @@ virDomainLeaseDefParseXML(xmlNodePtr node,
     return NULL;
 }
 
-static int
-virDomainDiskSourcePoolDefParse(xmlNodePtr node,
-                                virStorageSourcePoolDef **srcpool)
+static virStorageSourcePoolDef *
+virDomainDiskSourcePoolDefParse(xmlNodePtr node)
 {
-    virStorageSourcePoolDef *source;
-    int ret = -1;
-
-    *srcpool = NULL;
-
-    source = g_new0(virStorageSourcePoolDef, 1);
+    g_autoptr(virStorageSourcePoolDef) source = g_new0(virStorageSourcePoolDef, 1);
 
     source->pool = virXMLPropString(node, "pool");
     source->volume = virXMLPropString(node, "volume");
 
-    /* CD-ROM and Floppy allows no source */
-    if (!source->pool && !source->volume) {
-        ret = 0;
-        goto cleanup;
-    }
+    /* CD-ROM and Floppy allows no source -> empty pool */
+    if (!source->pool && !source->volume)
+        return g_steal_pointer(&source);
 
     if (!source->pool || !source->volume) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("'pool' and 'volume' must be specified together for 'pool' type source"));
-        goto cleanup;
+        return NULL;
     }
 
     if (virXMLPropEnum(node, "mode",
                        virStorageSourcePoolModeTypeFromString,
                        VIR_XML_PROP_NONZERO,
                        &source->mode) < 0)
-        goto cleanup;
-
-    *srcpool = g_steal_pointer(&source);
-    ret = 0;
+        return NULL;
 
- cleanup:
-    virStorageSourcePoolDefFree(source);
-    return ret;
+    return g_steal_pointer(&source);
 }
 
 
@@ -7482,7 +7469,7 @@ virDomainStorageSourceParse(xmlNodePtr node,
             return -1;
         break;
     case VIR_STORAGE_TYPE_VOLUME:
-        if (virDomainDiskSourcePoolDefParse(node, &src->srcpool) < 0)
+        if (!(src->srcpool = virDomainDiskSourcePoolDefParse(node)))
             return -1;
         break;
     case VIR_STORAGE_TYPE_NVME:
@@ -8660,7 +8647,7 @@ virDomainFSDefParseXML(virDomainXMLOption *xmlopt,
             units = virXMLPropString(source_node, "units");
         } else if (def->type == VIR_DOMAIN_FS_TYPE_VOLUME) {
             def->src->type = VIR_STORAGE_TYPE_VOLUME;
-            if (virDomainDiskSourcePoolDefParse(source_node, &def->src->srcpool) < 0)
+            if (!(def->src->srcpool = virDomainDiskSourcePoolDefParse(source_node)))
                 goto error;
         }
     }
index bfa8d625e594f15edff4098c025b9fbdbb7e4457..0cd5cd01929246b7bda8c737545581f9054891b3 100644 (file)
@@ -498,6 +498,7 @@ virStorageSourceInitChainElement(virStorageSource *newelem,
 
 void
 virStorageSourcePoolDefFree(virStorageSourcePoolDef *def);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStorageSourcePoolDef, virStorageSourcePoolDefFree);
 
 void
 virStorageSourceClear(virStorageSource *def);