]> xenbits.xensource.com Git - libvirt.git/commitdiff
storage: Introduce virStoragePoolObjGetNames
authorJohn Ferlan <jferlan@redhat.com>
Tue, 21 Mar 2017 13:15:18 +0000 (09:15 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Tue, 11 Apr 2017 12:49:51 +0000 (08:49 -0400)
Mostly code motion to move storageConnectList[Defined]StoragePools
and similar test driver code into virstorageobj.c and rename to
virStoragePoolObjGetNames.

Also includes a couple of variable name adjustments to keep code consistent
with other drivers.

Signed-off-by: John Ferlan <jferlan@redhat.com>
src/conf/virstorageobj.c
src/conf/virstorageobj.h
src/libvirt_private.syms
src/storage/storage_driver.c
src/test/test_driver.c

index 5112bac92d49b3057cd52f48b6be02363071836c..b199a51a1a1a6911436906386f7f99c85429f3a0 100644 (file)
@@ -580,6 +580,42 @@ virStoragePoolObjNumOfStoragePools(virStoragePoolObjListPtr pools,
 }
 
 
+int
+virStoragePoolObjGetNames(virStoragePoolObjListPtr pools,
+                          virConnectPtr conn,
+                          bool wantActive,
+                          virStoragePoolObjListACLFilter aclfilter,
+                          char **const names,
+                          int maxnames)
+{
+    int nnames = 0;
+    size_t i;
+
+    for (i = 0; i < pools->count && nnames < maxnames; i++) {
+        virStoragePoolObjPtr obj = pools->objs[i];
+        virStoragePoolObjLock(obj);
+        if (!aclfilter || aclfilter(conn, obj->def)) {
+            if (wantActive == virStoragePoolObjIsActive(obj)) {
+                if (VIR_STRDUP(names[nnames], obj->def->name) < 0) {
+                    virStoragePoolObjUnlock(obj);
+                    goto failure;
+                }
+                nnames++;
+            }
+        }
+        virStoragePoolObjUnlock(obj);
+    }
+
+    return nnames;
+
+ failure:
+    while (--nnames >= 0)
+        VIR_FREE(names[nnames]);
+
+    return -1;
+}
+
+
 /*
  * virStoragePoolObjIsDuplicate:
  * @doms : virStoragePoolObjListPtr to search
index 5eeda1e43b0327756e0eb2e13b97704aca5b69bd..b65186502c313acf3d07767051ed98eef07da988 100644 (file)
@@ -155,6 +155,14 @@ virStoragePoolObjNumOfStoragePools(virStoragePoolObjListPtr pools,
                                    bool wantActive,
                                    virStoragePoolObjListACLFilter aclfilter);
 
+int
+virStoragePoolObjGetNames(virStoragePoolObjListPtr pools,
+                          virConnectPtr conn,
+                          bool wantActive,
+                          virStoragePoolObjListACLFilter aclfilter,
+                          char **const names,
+                          int maxnames);
+
 void
 virStoragePoolObjFree(virStoragePoolObjPtr pool);
 
index d8a9c1373de817a03c7fa15c118153d201cd3b81..8509f639e474e50415d6c4e7718a2467d845cdbd 100644 (file)
@@ -999,6 +999,7 @@ virStoragePoolObjClearVols;
 virStoragePoolObjDeleteDef;
 virStoragePoolObjFindByName;
 virStoragePoolObjFindByUUID;
+virStoragePoolObjGetNames;
 virStoragePoolObjIsDuplicate;
 virStoragePoolObjListExport;
 virStoragePoolObjListFree;
index 0ccca85ce57fbebdb289c3becb3b09a7ee4a5d75..ae96b86f04d37affe9bb37b13995b426616cd4f2 100644 (file)
@@ -490,40 +490,23 @@ storageConnectNumOfStoragePools(virConnectPtr conn)
     return nactive;
 }
 
+
 static int
 storageConnectListStoragePools(virConnectPtr conn,
                                char **const names,
-                               int nnames)
+                               int maxnames)
 {
     int got = 0;
-    size_t i;
 
     if (virConnectListStoragePoolsEnsureACL(conn) < 0)
         return -1;
 
     storageDriverLock();
-    for (i = 0; i < driver->pools.count && got < nnames; i++) {
-        virStoragePoolObjPtr obj = driver->pools.objs[i];
-        virStoragePoolObjLock(obj);
-        if (virConnectListStoragePoolsCheckACL(conn, obj->def) &&
-            virStoragePoolObjIsActive(obj)) {
-            if (VIR_STRDUP(names[got], obj->def->name) < 0) {
-                virStoragePoolObjUnlock(obj);
-                goto cleanup;
-            }
-            got++;
-        }
-        virStoragePoolObjUnlock(obj);
-    }
+    got = virStoragePoolObjGetNames(&driver->pools, conn, true,
+                                    virConnectListStoragePoolsCheckACL,
+                                    names, maxnames);
     storageDriverUnlock();
     return got;
-
- cleanup:
-    storageDriverUnlock();
-    for (i = 0; i < got; i++)
-        VIR_FREE(names[i]);
-    memset(names, 0, nnames * sizeof(*names));
-    return -1;
 }
 
 static int
@@ -542,40 +525,23 @@ storageConnectNumOfDefinedStoragePools(virConnectPtr conn)
     return nactive;
 }
 
+
 static int
 storageConnectListDefinedStoragePools(virConnectPtr conn,
                                       char **const names,
-                                      int nnames)
+                                      int maxnames)
 {
     int got = 0;
-    size_t i;
 
     if (virConnectListDefinedStoragePoolsEnsureACL(conn) < 0)
         return -1;
 
     storageDriverLock();
-    for (i = 0; i < driver->pools.count && got < nnames; i++) {
-        virStoragePoolObjPtr obj = driver->pools.objs[i];
-        virStoragePoolObjLock(obj);
-        if (virConnectListDefinedStoragePoolsCheckACL(conn, obj->def) &&
-            !virStoragePoolObjIsActive(obj)) {
-            if (VIR_STRDUP(names[got], obj->def->name) < 0) {
-                virStoragePoolObjUnlock(obj);
-                goto cleanup;
-            }
-            got++;
-        }
-        virStoragePoolObjUnlock(obj);
-    }
+    got = virStoragePoolObjGetNames(&driver->pools, conn, false,
+                                    virConnectListDefinedStoragePoolsCheckACL,
+                                    names, maxnames);
     storageDriverUnlock();
     return got;
-
- cleanup:
-    storageDriverUnlock();
-    for (i = 0; i < got; i++)
-        VIR_FREE(names[i]);
-    memset(names, 0, nnames * sizeof(*names));
-    return -1;
 }
 
 /* This method is required to be re-entrant / thread safe, so
index 523d7efe4bd256f7280b27d55dc1695a219c778b..6e4e9a0f3f25f4536c01839887e95407a1044733 100644 (file)
@@ -4117,35 +4117,21 @@ testConnectNumOfStoragePools(virConnectPtr conn)
     return numActive;
 }
 
+
 static int
 testConnectListStoragePools(virConnectPtr conn,
                             char **const names,
-                            int nnames)
+                            int maxnames)
 {
     testDriverPtr privconn = conn->privateData;
     int n = 0;
-    size_t i;
 
     testDriverLock(privconn);
-    memset(names, 0, sizeof(*names)*nnames);
-    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
-        virStoragePoolObjLock(privconn->pools.objs[i]);
-        if (virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
-            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
-            virStoragePoolObjUnlock(privconn->pools.objs[i]);
-            goto error;
-        }
-        virStoragePoolObjUnlock(privconn->pools.objs[i]);
-    }
+    n = virStoragePoolObjGetNames(&privconn->pools, conn, true, NULL,
+                                  names, maxnames);
     testDriverUnlock(privconn);
 
     return n;
-
- error:
-    for (n = 0; n < nnames; n++)
-        VIR_FREE(names[n]);
-    testDriverUnlock(privconn);
-    return -1;
 }
 
 
@@ -4163,35 +4149,21 @@ testConnectNumOfDefinedStoragePools(virConnectPtr conn)
     return numInactive;
 }
 
+
 static int
 testConnectListDefinedStoragePools(virConnectPtr conn,
                                    char **const names,
-                                   int nnames)
+                                   int maxnames)
 {
     testDriverPtr privconn = conn->privateData;
     int n = 0;
-    size_t i;
 
     testDriverLock(privconn);
-    memset(names, 0, sizeof(*names)*nnames);
-    for (i = 0; i < privconn->pools.count && n < nnames; i++) {
-        virStoragePoolObjLock(privconn->pools.objs[i]);
-        if (!virStoragePoolObjIsActive(privconn->pools.objs[i]) &&
-            VIR_STRDUP(names[n++], privconn->pools.objs[i]->def->name) < 0) {
-            virStoragePoolObjUnlock(privconn->pools.objs[i]);
-            goto error;
-        }
-        virStoragePoolObjUnlock(privconn->pools.objs[i]);
-    }
+    n = virStoragePoolObjGetNames(&privconn->pools, conn, false, NULL,
+                                  names, maxnames);
     testDriverUnlock(privconn);
 
     return n;
-
- error:
-    for (n = 0; n < nnames; n++)
-        VIR_FREE(names[n]);
-    testDriverUnlock(privconn);
-    return -1;
 }
 
 static int