[PATCH 08/15] storage: Use VIR_AUTOFREE for storage driver

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

 



Let's make use of the auto __cleanup capabilities cleaning up any
now unnecessary goto paths.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/storage/storage_driver.c | 70 ++++++++++++------------------------
 1 file changed, 23 insertions(+), 47 deletions(-)

diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 7c377439dc..61e0756e3e 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -122,7 +122,7 @@ storagePoolUpdateStateCallback(virStoragePoolObjPtr obj,
     virStoragePoolDefPtr def = virStoragePoolObjGetDef(obj);
     bool active = false;
     virStorageBackendPtr backend;
-    char *stateFile;
+    VIR_AUTOFREE(char *) stateFile = NULL;
 
     if ((backend = virStorageBackendForType(def->type)) == NULL) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -164,8 +164,6 @@ storagePoolUpdateStateCallback(virStoragePoolObjPtr obj,
     if (!virStoragePoolObjIsActive(obj))
         virStoragePoolUpdateInactive(&obj);
 
-    VIR_FREE(stateFile);
-
     return;
 }
 
@@ -203,7 +201,7 @@ storageDriverAutostartCallback(virStoragePoolObjPtr obj,
     }
 
     if (started) {
-        char *stateFile;
+        VIR_AUTOFREE(char *) stateFile = NULL;
 
         virStoragePoolObjClearVols(obj);
         stateFile = virFileBuildPath(driver->stateDir, def->name, ".xml");
@@ -217,7 +215,6 @@ storageDriverAutostartCallback(virStoragePoolObjPtr obj,
         } else {
             virStoragePoolObjSetActive(obj, true);
         }
-        VIR_FREE(stateFile);
     }
 }
 
@@ -240,16 +237,15 @@ storageStateInitialize(bool privileged,
                        virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                        void *opaque ATTRIBUTE_UNUSED)
 {
-    int ret = -1;
-    char *configdir = NULL;
-    char *rundir = NULL;
+    VIR_AUTOFREE(char *) configdir = NULL;
+    VIR_AUTOFREE(char *) rundir = NULL;
 
     if (VIR_ALLOC(driver) < 0)
-        return ret;
+        return -1;
 
     if (virMutexInit(&driver->lock) < 0) {
         VIR_FREE(driver);
-        return ret;
+        return -1;
     }
     storageDriverLock();
 
@@ -302,16 +298,12 @@ storageStateInitialize(bool privileged,
 
     storageDriverUnlock();
 
-    ret = 0;
- cleanup:
-    VIR_FREE(configdir);
-    VIR_FREE(rundir);
-    return ret;
+    return 0;
 
  error:
     storageDriverUnlock();
     storageStateCleanup();
-    goto cleanup;
+    return -1;
 }
 
 /**
@@ -691,12 +683,12 @@ storagePoolCreateXML(virConnectPtr conn,
                      unsigned int flags)
 {
     VIR_AUTOPTR(virStoragePoolDef) newDef = NULL;
+    VIR_AUTOFREE(char *) stateFile = NULL;
     virStoragePoolObjPtr obj = NULL;
     virStoragePoolDefPtr def;
     virStoragePoolPtr pool = NULL;
     virStorageBackendPtr backend;
     virObjectEventPtr event = NULL;
-    char *stateFile = NULL;
     unsigned int build_flags = 0;
 
     virCheckFlags(VIR_STORAGE_POOL_CREATE_WITH_BUILD |
@@ -761,7 +753,6 @@ storagePoolCreateXML(virConnectPtr conn,
     pool = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL);
 
  cleanup:
-    VIR_FREE(stateFile);
     virObjectEventStateQueue(driver->storageEventState, event);
     virStoragePoolObjEndAPI(&obj);
     return pool;
@@ -892,7 +883,7 @@ storagePoolCreate(virStoragePoolPtr pool,
     virStorageBackendPtr backend;
     virObjectEventPtr event = NULL;
     int ret = -1;
-    char *stateFile = NULL;
+    VIR_AUTOFREE(char *) stateFile = NULL;
     unsigned int build_flags = 0;
 
     virCheckFlags(VIR_STORAGE_POOL_CREATE_WITH_BUILD |
@@ -955,7 +946,6 @@ storagePoolCreate(virStoragePoolPtr pool,
     ret = 0;
 
  cleanup:
-    VIR_FREE(stateFile);
     virObjectEventStateQueue(driver->storageEventState, event);
     virStoragePoolObjEndAPI(&obj);
     return ret;
@@ -1013,7 +1003,7 @@ storagePoolDestroy(virStoragePoolPtr pool)
     virStoragePoolDefPtr def;
     virStorageBackendPtr backend;
     virObjectEventPtr event = NULL;
-    char *stateFile = NULL;
+    VIR_AUTOFREE(char *) stateFile = NULL;
     int ret = -1;
 
     if (!(obj = storagePoolObjFindByUUID(pool->uuid, pool->name)))
@@ -1045,7 +1035,6 @@ storagePoolDestroy(virStoragePoolPtr pool)
         goto cleanup;
 
     unlink(stateFile);
-    VIR_FREE(stateFile);
 
     if (backend->stopPool &&
         backend->stopPool(obj) < 0)
@@ -1078,7 +1067,7 @@ storagePoolDelete(virStoragePoolPtr pool,
     virStoragePoolDefPtr def;
     virStorageBackendPtr backend;
     virObjectEventPtr event = NULL;
-    char *stateFile = NULL;
+    VIR_AUTOFREE(char *) stateFile = NULL;
     int ret = -1;
 
     if (!(obj = virStoragePoolObjFromStoragePool(pool)))
@@ -1111,7 +1100,6 @@ storagePoolDelete(virStoragePoolPtr pool,
         goto cleanup;
 
     unlink(stateFile);
-    VIR_FREE(stateFile);
 
     if (!backend->deletePool) {
         virReportError(VIR_ERR_NO_SUPPORT,
@@ -1172,10 +1160,10 @@ storagePoolRefresh(virStoragePoolPtr pool,
 
     virStoragePoolObjClearVols(obj);
     if (backend->refreshPool(obj) < 0) {
-        char *stateFile = virFileBuildPath(driver->stateDir, def->name, ".xml");
+        VIR_AUTOFREE(char *) stateFile = NULL;
 
+        stateFile = virFileBuildPath(driver->stateDir, def->name, ".xml");
         storagePoolRefreshFailCleanup(backend, obj, stateFile);
-        VIR_FREE(stateFile);
 
         event = virStoragePoolEventLifecycleNew(def->name,
                                                 def->uuid,
@@ -1540,7 +1528,7 @@ storageVolLookupByPathCallback(virStoragePoolObjPtr obj,
 {
     struct storageVolLookupData *data = (struct storageVolLookupData *)opaque;
     virStoragePoolDefPtr def;
-    char *stable_path = NULL;
+    VIR_AUTOFREE(char *) stable_path = NULL;
 
     if (!virStoragePoolObjIsActive(obj))
         return false;
@@ -1579,7 +1567,6 @@ storageVolLookupByPathCallback(virStoragePoolObjPtr obj,
     }
 
     data->voldef = virStorageVolDefFindByPath(obj, stable_path);
-    VIR_FREE(stable_path);
 
     return !!data->voldef;
 }
@@ -1649,7 +1636,7 @@ storagePoolLookupByTargetPath(virConnectPtr conn,
     virStoragePoolObjPtr obj;
     virStoragePoolDefPtr def;
     virStoragePoolPtr pool = NULL;
-    char *cleanpath;
+    VIR_AUTOFREE(char *) cleanpath = NULL;
 
     cleanpath = virFileSanitizePath(path);
     if (!cleanpath)
@@ -1660,7 +1647,7 @@ storagePoolLookupByTargetPath(virConnectPtr conn,
                                            cleanpath))) {
         def = virStoragePoolObjGetDef(obj);
         if (virStoragePoolLookupByTargetPathEnsureACL(conn, def) < 0)
-            goto cleanup;
+            return NULL;
 
         pool = virGetStoragePool(conn, def->name, def->uuid, NULL, NULL);
         virStoragePoolObjEndAPI(&obj);
@@ -1678,8 +1665,6 @@ storagePoolLookupByTargetPath(virConnectPtr conn,
         }
     }
 
- cleanup:
-    VIR_FREE(cleanpath);
     return pool;
 }
 
@@ -2206,40 +2191,31 @@ virStorageVolPoolRefreshDataFree(void *opaque)
 static int
 virStorageBackendPloopRestoreDesc(char *path)
 {
-    int ret = -1;
     VIR_AUTOPTR(virCommand) cmd = NULL;
-    char *refresh_tool = NULL;
-    char *desc = NULL;
+    VIR_AUTOFREE(char *) refresh_tool = NULL;
+    VIR_AUTOFREE(char *) desc = NULL;
 
     if (virAsprintf(&desc, "%s/DiskDescriptor.xml", path) < 0)
-        return ret;
+        return -1;
 
     if (virFileRemove(desc, 0, 0) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("refresh ploop failed:"
                          " unable to delete DiskDescriptor.xml"));
-        goto cleanup;
+        return -1;
     }
 
     refresh_tool = virFindFileInPath("ploop");
     if (!refresh_tool) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("unable to find ploop, please install ploop tools"));
-        goto cleanup;
+        return -1;
     }
 
     cmd = virCommandNewArgList(refresh_tool, "restore-descriptor",
                                path, NULL);
     virCommandAddArgFormat(cmd, "%s/root.hds", path);
-    if (virCommandRun(cmd, NULL) < 0)
-        goto cleanup;
-
-    ret = 0;
-
- cleanup:
-    VIR_FREE(refresh_tool);
-    VIR_FREE(desc);
-    return ret;
+    return virCommandRun(cmd, NULL);
 }
 
 
-- 
2.20.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[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