[PATCH 3/5] storageDriverAutostartCallback: Refactor control flow

Peter Krempa pkrempa at redhat.com
Thu Jan 20 11:45:56 UTC 2022


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 <pkrempa at redhat.com>
---
 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:
-- 
2.34.1




More information about the libvir-list mailing list