[PATCH 3/5] storageDriverAutostartCallback: Refactor control flow

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 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




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux