From a09c5b3cc21486481d4a584936b318723670decd Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 20 Jan 2022 11:36:29 +0100 Subject: [PATCH] storageDriverAutostartCallback: Refactor control flow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use early returns to decrease the indentation level and make it more obvious that the 'cleanup' path is a noop in those cases. 'virStoragePoolObjSetStarting' was called only when the code wanted to start the pool, so if that was skipped, cleanup is noop as it's conditional on the return value of 'virStoragePoolObjIsStarting'. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/storage/storage_driver.c | 49 ++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c index 8dfa2497fc..97e0d9b3a0 100644 --- a/src/storage/storage_driver.c +++ b/src/storage/storage_driver.c @@ -194,38 +194,39 @@ storageDriverAutostartCallback(virStoragePoolObj *obj, { virStoragePoolDef *def = virStoragePoolObjGetDef(obj); virStorageBackend *backend; - bool started = false; + g_autofree char *stateFile = NULL; if (!(backend = virStorageBackendForType(def->type))) return; - if (virStoragePoolObjIsAutostart(obj) && - !virStoragePoolObjIsActive(obj)) { + if (!virStoragePoolObjIsAutostart(obj)) + return; - virStoragePoolObjSetStarting(obj, true); - if (backend->startPool && - backend->startPool(obj) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to autostart storage pool '%s': %s"), - def->name, virGetLastErrorMessage()); - goto cleanup; - } - started = true; + if (virStoragePoolObjIsActive(obj)) + return; + + VIR_DEBUG("autostarting storage pool '%s'", def->name); + + virStoragePoolObjSetStarting(obj, true); + + if (backend->startPool && + backend->startPool(obj) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to autostart storage pool '%s': %s"), + def->name, virGetLastErrorMessage()); + goto cleanup; } - if (started) { - g_autofree char *stateFile = NULL; + stateFile = virFileBuildPath(driver->stateDir, def->name, ".xml"); - stateFile = virFileBuildPath(driver->stateDir, def->name, ".xml"); - if (!stateFile || - virStoragePoolSaveState(stateFile, def) < 0 || - storagePoolRefreshImpl(backend, obj, stateFile) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to autostart storage pool '%s': %s"), - def->name, virGetLastErrorMessage()); - } else { - virStoragePoolObjSetActive(obj, true); - } + if (!stateFile || + virStoragePoolSaveState(stateFile, def) < 0 || + storagePoolRefreshImpl(backend, obj, stateFile) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to autostart storage pool '%s': %s"), + def->name, virGetLastErrorMessage()); + } else { + virStoragePoolObjSetActive(obj, true); } cleanup: