[PATCH 7/7] storage: Create helpers to perform FindByUUID and FindByName

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

 



Create a couple of helpers that will perform the same call sequence.

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

diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 6b8bbb5..8b55acc 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -387,6 +387,56 @@ storageStateCleanup(void)
 }
 
 
+static virStoragePoolObjPtr
+storagePoolObjFindByUUID(const unsigned char *uuid,
+                         const char *name)
+{
+    virStoragePoolObjPtr pool;
+    char uuidstr[VIR_UUID_STRING_BUFLEN];
+
+    if (!(pool = virStoragePoolObjFindByUUID(&driver->pools, uuid))) {
+        virUUIDFormat(uuid, uuidstr);
+        if (name)
+            virReportError(VIR_ERR_NO_STORAGE_POOL,
+                           _("no storage pool with matching uuid '%s' (%s)"),
+                           uuidstr, name);
+        else
+            virReportError(VIR_ERR_NO_STORAGE_POOL,
+                           _("no storage pool with matching uuid '%s'"),
+                           uuidstr);
+    }
+
+    return pool;
+}
+
+
+static virStoragePoolObjPtr
+virStoragePoolObjFromStoragePool(virStoragePoolPtr pool)
+{
+    virStoragePoolObjPtr ret;
+
+    storageDriverLock();
+    ret = storagePoolObjFindByUUID(pool->uuid, pool->name);
+    storageDriverUnlock();
+
+    return ret;
+}
+
+
+static virStoragePoolObjPtr
+storagePoolObjFindByName(const char *name)
+{
+    virStoragePoolObjPtr pool;
+
+    storageDriverLock();
+    if (!(pool = virStoragePoolObjFindByName(&driver->pools, name)))
+        virReportError(VIR_ERR_NO_STORAGE_POOL,
+                       _("no storage pool with matching name '%s'"), name);
+    storageDriverUnlock();
+
+    return pool;
+}
+
 
 static virStoragePoolPtr
 storagePoolLookupByUUID(virConnectPtr conn,
@@ -396,16 +446,10 @@ storagePoolLookupByUUID(virConnectPtr conn,
     virStoragePoolPtr ret = NULL;
 
     storageDriverLock();
-    pool = virStoragePoolObjFindByUUID(&driver->pools, uuid);
+    pool = storagePoolObjFindByUUID(uuid, NULL);
     storageDriverUnlock();
-
-    if (!pool) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-        virUUIDFormat(uuid, uuidstr);
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching uuid '%s'"), uuidstr);
+    if (!pool)
         return NULL;
-    }
 
     if (virStoragePoolLookupByUUIDEnsureACL(conn, pool->def) < 0)
         goto cleanup;
@@ -425,15 +469,8 @@ storagePoolLookupByName(virConnectPtr conn,
     virStoragePoolObjPtr pool;
     virStoragePoolPtr ret = NULL;
 
-    storageDriverLock();
-    pool = virStoragePoolObjFindByName(&driver->pools, name);
-    storageDriverUnlock();
-
-    if (!pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"), name);
+    if (!(pool = storagePoolObjFindByName(name)))
         return NULL;
-    }
 
     if (virStoragePoolLookupByNameEnsureACL(conn, pool->def) < 0)
         goto cleanup;
@@ -452,16 +489,8 @@ storagePoolLookupByVolume(virStorageVolPtr vol)
     virStoragePoolObjPtr pool;
     virStoragePoolPtr ret = NULL;
 
-    storageDriverLock();
-    pool = virStoragePoolObjFindByName(&driver->pools, vol->pool);
-    storageDriverUnlock();
-
-    if (!pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"),
-                       vol->pool);
+    if (!(pool = storagePoolObjFindByName(vol->pool)))
         return NULL;
-    }
 
     if (virStoragePoolLookupByVolumeEnsureACL(vol->conn, pool->def) < 0)
         goto cleanup;
@@ -588,25 +617,6 @@ storageConnectFindStoragePoolSources(virConnectPtr conn,
 }
 
 
-static virStoragePoolObjPtr
-virStoragePoolObjFromStoragePool(virStoragePoolPtr pool)
-{
-    char uuidstr[VIR_UUID_STRING_BUFLEN];
-    virStoragePoolObjPtr ret;
-
-    storageDriverLock();
-    if (!(ret = virStoragePoolObjFindByUUID(&driver->pools, pool->uuid))) {
-        virUUIDFormat(pool->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching uuid '%s' (%s)"),
-                       uuidstr, pool->name);
-    }
-    storageDriverUnlock();
-
-    return ret;
-}
-
-
 static int storagePoolIsActive(virStoragePoolPtr pool)
 {
     virStoragePoolObjPtr obj;
@@ -813,14 +823,8 @@ storagePoolUndefine(virStoragePoolPtr obj)
     int ret = -1;
 
     storageDriverLock();
-    if (!(pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid))) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-        virUUIDFormat(obj->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching uuid '%s' (%s)"),
-                       uuidstr, obj->name);
+    if (!(pool = storagePoolObjFindByUUID(obj->uuid, obj->name)))
         goto cleanup;
-    }
 
     if (virStoragePoolUndefineEnsureACL(obj->conn, pool->def) < 0)
         goto cleanup;
@@ -999,14 +1003,8 @@ storagePoolDestroy(virStoragePoolPtr obj)
     int ret = -1;
 
     storageDriverLock();
-    if (!(pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid))) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-        virUUIDFormat(obj->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching uuid '%s' (%s)"),
-                       uuidstr, obj->name);
+    if (!(pool = storagePoolObjFindByUUID(obj->uuid, obj->name)))
         goto cleanup;
-    }
 
     if (virStoragePoolDestroyEnsureACL(obj->conn, pool->def) < 0)
         goto cleanup;
@@ -1133,14 +1131,8 @@ storagePoolRefresh(virStoragePoolPtr obj,
     virCheckFlags(0, -1);
 
     storageDriverLock();
-    if (!(pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid))) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-        virUUIDFormat(obj->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching uuid '%s' (%s)"),
-                       uuidstr, obj->name);
+    if (!(pool = storagePoolObjFindByUUID(obj->uuid, obj->name)))
         goto cleanup;
-    }
 
     if (virStoragePoolRefreshEnsureACL(obj->conn, pool->def) < 0)
         goto cleanup;
@@ -1283,16 +1275,8 @@ storagePoolSetAutostart(virStoragePoolPtr obj,
     int ret = -1;
 
     storageDriverLock();
-    pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
-
-    if (!pool) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-        virUUIDFormat(obj->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching uuid '%s' (%s)"),
-                       uuidstr, obj->name);
+    if (!(pool = storagePoolObjFindByUUID(obj->uuid, obj->name)))
         goto cleanup;
-    }
 
     if (virStoragePoolSetAutostartEnsureACL(obj->conn, pool->def) < 0)
         goto cleanup;
@@ -1713,18 +1697,8 @@ virStorageVolDefFromVol(virStorageVolPtr obj,
 {
     virStorageVolDefPtr vol = NULL;
 
-    *pool = NULL;
-
-    storageDriverLock();
-    *pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
-    storageDriverUnlock();
-
-    if (!*pool) {
-        virReportError(VIR_ERR_NO_STORAGE_POOL,
-                       _("no storage pool with matching name '%s'"),
-                       obj->pool);
+    if (!(*pool = storagePoolObjFindByName(obj->pool)))
         return NULL;
-    }
 
     if (!virStoragePoolObjIsActive(*pool)) {
         virReportError(VIR_ERR_OPERATION_INVALID,
-- 
2.9.3

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