]> xenbits.xensource.com Git - libvirt.git/commitdiff
storageDriverAutostartCallback: Refactor control flow
authorPeter Krempa <pkrempa@redhat.com>
Thu, 20 Jan 2022 10:36:29 +0000 (11:36 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 20 Jan 2022 13:23:56 +0000 (14:23 +0100)
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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/storage/storage_driver.c

index 8dfa2497fc88c3bd0b797e8257d4a3e9439f297f..97e0d9b3a0a1e6e038c67802d2919bacbf069d0a 100644 (file)
@@ -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: