[PATCH v2 09/31] qemu: Refactor virQEMUCapsCacheLookup

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

 



The function is made a little bit more readable and the code which
refreshes cached capabilities if they are not valid any more was moved
into a separate function (virQEMUCapsCacheValidate) so that it can be
reused in other places.

Signed-off-by: Jiri Denemark <jdenemar@xxxxxxxxxx>
---
 src/qemu/qemu_capabilities.c | 59 +++++++++++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 23 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 75d635a..904289b 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -4535,6 +4535,37 @@ virQEMUCapsCacheNew(const char *libDir,
     return NULL;
 }
 
+
+static void ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
+virQEMUCapsCacheValidate(virQEMUCapsCachePtr cache,
+                         const char *binary,
+                         virCapsPtr caps,
+                         virQEMUCapsPtr *qemuCaps)
+{
+    if (*qemuCaps &&
+        !virQEMUCapsIsValid(*qemuCaps, 0, cache->runUid, cache->runGid)) {
+        VIR_DEBUG("Cached capabilities %p no longer valid for %s",
+                  *qemuCaps, binary);
+        virHashRemoveEntry(cache->binaries, binary);
+        *qemuCaps = NULL;
+    }
+
+    if (!*qemuCaps) {
+        VIR_DEBUG("Creating capabilities for %s", binary);
+        *qemuCaps = virQEMUCapsNewForBinary(caps, binary,
+                                            cache->libDir, cache->cacheDir,
+                                            cache->runUid, cache->runGid);
+        if (*qemuCaps) {
+            VIR_DEBUG("Caching capabilities %p for %s", *qemuCaps, binary);
+            if (virHashAddEntry(cache->binaries, binary, *qemuCaps) < 0) {
+                virObjectUnref(*qemuCaps);
+                *qemuCaps = NULL;
+            }
+        }
+    }
+}
+
+
 const char *qemuTestCapsName;
 
 virQEMUCapsPtr
@@ -4549,32 +4580,14 @@ virQEMUCapsCacheLookup(virCapsPtr caps,
         binary = qemuTestCapsName;
 
     virMutexLock(&cache->lock);
+
     ret = virHashLookup(cache->binaries, binary);
-    if (ret &&
-        !virQEMUCapsIsValid(ret, 0, cache->runUid, cache->runGid)) {
-        VIR_DEBUG("Cached capabilities %p no longer valid for %s",
-                  ret, binary);
-        virHashRemoveEntry(cache->binaries, binary);
-        ret = NULL;
-    }
-    if (!ret) {
-        VIR_DEBUG("Creating capabilities for %s",
-                  binary);
-        ret = virQEMUCapsNewForBinary(caps, binary, cache->libDir,
-                                      cache->cacheDir,
-                                      cache->runUid, cache->runGid);
-        if (ret) {
-            VIR_DEBUG("Caching capabilities %p for %s",
-                      ret, binary);
-            if (virHashAddEntry(cache->binaries, binary, ret) < 0) {
-                virObjectUnref(ret);
-                ret = NULL;
-            }
-        }
-    }
-    VIR_DEBUG("Returning caps %p for %s", ret, binary);
+    virQEMUCapsCacheValidate(cache, binary, caps, &ret);
     virObjectRef(ret);
+
     virMutexUnlock(&cache->lock);
+
+    VIR_DEBUG("Returning caps %p for %s", ret, binary);
     return ret;
 }
 
-- 
2.10.2

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