1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-20 07:59:00 +00:00

virLockManagerSanlockAddDisk: Refactor cleanup

Use g_autofree to allow removal of 'cleanup:' and the 'ret' variable.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Peter Krempa 2021-02-03 13:25:46 +01:00
parent 30a42dcddc
commit 7d836cfba1

View File

@ -581,10 +581,9 @@ virLockManagerSanlockAddDisk(virLockManagerSanlockDriverPtr driver,
bool shared) bool shared)
{ {
virLockManagerSanlockPrivatePtr priv = lock->privateData; virLockManagerSanlockPrivatePtr priv = lock->privateData;
int ret = -1; g_autofree struct sanlk_resource *res = NULL;
struct sanlk_resource *res = NULL; g_autofree char *path = NULL;
char *path = NULL; g_autofree char *hash = NULL;
char *hash = NULL;
if (nparams) { if (nparams) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -593,17 +592,17 @@ virLockManagerSanlockAddDisk(virLockManagerSanlockDriverPtr driver,
} }
if (VIR_ALLOC_VAR(res, struct sanlk_disk, 1) < 0) if (VIR_ALLOC_VAR(res, struct sanlk_disk, 1) < 0)
goto cleanup; return -1;
res->flags = shared ? SANLK_RES_SHARED : 0; res->flags = shared ? SANLK_RES_SHARED : 0;
res->num_disks = 1; res->num_disks = 1;
if (virCryptoHashString(VIR_CRYPTO_HASH_MD5, name, &hash) < 0) if (virCryptoHashString(VIR_CRYPTO_HASH_MD5, name, &hash) < 0)
goto cleanup; return -1;
if (virStrcpy(res->name, hash, SANLK_NAME_LEN) < 0) { if (virStrcpy(res->name, hash, SANLK_NAME_LEN) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("MD5 hash '%s' unexpectedly larger than %d characters"), _("MD5 hash '%s' unexpectedly larger than %d characters"),
hash, (SANLK_NAME_LEN - 1)); hash, (SANLK_NAME_LEN - 1));
goto cleanup; return -1;
} }
path = g_strdup_printf("%s/%s", driver->autoDiskLeasePath, res->name); path = g_strdup_printf("%s/%s", driver->autoDiskLeasePath, res->name);
@ -611,7 +610,7 @@ virLockManagerSanlockAddDisk(virLockManagerSanlockDriverPtr driver,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Lease path '%s' exceeds %d characters"), _("Lease path '%s' exceeds %d characters"),
path, SANLK_PATH_LEN); path, SANLK_PATH_LEN);
goto cleanup; return -1;
} }
if (virStrcpy(res->lockspace_name, if (virStrcpy(res->lockspace_name,
@ -620,20 +619,13 @@ virLockManagerSanlockAddDisk(virLockManagerSanlockDriverPtr driver,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Resource lockspace '%s' exceeds %d characters"), _("Resource lockspace '%s' exceeds %d characters"),
VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE, SANLK_NAME_LEN); VIR_LOCK_MANAGER_SANLOCK_AUTO_DISK_LOCKSPACE, SANLK_NAME_LEN);
goto cleanup; return -1;
} }
priv->res_args[priv->res_count] = res; priv->res_args[priv->res_count] = g_steal_pointer(&res);
priv->res_count++; priv->res_count++;
ret = 0; return 0;
cleanup:
if (ret == -1)
VIR_FREE(res);
VIR_FREE(path);
VIR_FREE(hash);
return ret;
} }