virDomainDiskSourcePoolDefParse: Refactor cleanup

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>
This commit is contained in:
Peter Krempa 2023-10-12 15:07:52 +02:00
parent 31274a1eda
commit d8fd9904ff
2 changed files with 12 additions and 24 deletions

View File

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

View File

@ -498,6 +498,7 @@ virStorageSourceInitChainElement(virStorageSource *newelem,
void void
virStorageSourcePoolDefFree(virStorageSourcePoolDef *def); virStorageSourcePoolDefFree(virStorageSourcePoolDef *def);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virStorageSourcePoolDef, virStorageSourcePoolDefFree);
void void
virStorageSourceClear(virStorageSource *def); virStorageSourceClear(virStorageSource *def);