[PATCH 08/11] qemu: separate virQEMUCapsInitCached out of virQEMUCapsNewForBinaryInternal

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

 



Preparation for switching to virFileCache where there are two callbacks,
one to get a new data and second one to load a cached data.

This also removes virQEMUCapsReset which is no longer required.

Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx>
---
 src/qemu/qemu_capabilities.c | 141 +++++++++++++++++++------------------------
 src/qemu/qemu_capspriv.h     |   1 -
 tests/qemucapsprobe.c        |   2 +-
 3 files changed, 63 insertions(+), 81 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 4d26e04fc3..1f3240b942 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -4251,37 +4251,6 @@ virQEMUCapsRememberCached(virQEMUCapsPtr qemuCaps, const char *cacheDir)
 }
 
 
-static void
-virQEMUCapsReset(virQEMUCapsPtr qemuCaps)
-{
-    size_t i;
-
-    virBitmapClearAll(qemuCaps->flags);
-    qemuCaps->version = qemuCaps->kvmVersion = 0;
-    VIR_FREE(qemuCaps->package);
-    qemuCaps->arch = VIR_ARCH_NONE;
-    qemuCaps->usedQMP = false;
-
-    virObjectUnref(qemuCaps->kvmCPUModels);
-    qemuCaps->kvmCPUModels = NULL;
-    virObjectUnref(qemuCaps->tcgCPUModels);
-    qemuCaps->tcgCPUModels = NULL;
-
-    for (i = 0; i < qemuCaps->nmachineTypes; i++) {
-        VIR_FREE(qemuCaps->machineTypes[i].name);
-        VIR_FREE(qemuCaps->machineTypes[i].alias);
-    }
-    VIR_FREE(qemuCaps->machineTypes);
-    qemuCaps->nmachineTypes = 0;
-
-    VIR_FREE(qemuCaps->gicCapabilities);
-    qemuCaps->ngicCapabilities = 0;
-
-    virQEMUCapsHostCPUDataClear(&qemuCaps->kvmCPU);
-    virQEMUCapsHostCPUDataClear(&qemuCaps->tcgCPU);
-}
-
-
 static bool
 virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps,
                    uid_t runUid,
@@ -4347,7 +4316,8 @@ virQEMUCapsIsValid(virQEMUCapsPtr qemuCaps,
 
 static int
 virQEMUCapsInitCached(virCapsPtr caps,
-                      virQEMUCapsPtr qemuCaps,
+                      virQEMUCapsPtr *qemuCaps,
+                      const char *binary,
                       const char *cacheDir,
                       uid_t runUid,
                       gid_t runGid)
@@ -4357,13 +4327,12 @@ virQEMUCapsInitCached(virCapsPtr caps,
     int ret = -1;
     char *binaryhash = NULL;
     struct stat sb;
+    virQEMUCapsPtr qemuCapsNew = NULL;
 
     if (virAsprintf(&capsdir, "%s/capabilities", cacheDir) < 0)
         goto cleanup;
 
-    if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256,
-                            qemuCaps->binary,
-                            &binaryhash) < 0)
+    if (virCryptoHashString(VIR_CRYPTO_HASH_SHA256, binary, &binaryhash) < 0)
         goto cleanup;
 
     if (virAsprintf(&capsfile, "%s/%s.xml", capsdir, binaryhash) < 0)
@@ -4379,31 +4348,38 @@ virQEMUCapsInitCached(virCapsPtr caps,
     if (stat(capsfile, &sb) < 0) {
         if (errno == ENOENT) {
             VIR_DEBUG("No cached capabilities '%s' for '%s'",
-                      capsfile, qemuCaps->binary);
+                      capsfile, binary);
             ret = 0;
             goto cleanup;
         }
         virReportSystemError(errno,
                              _("Unable to access cache '%s' for '%s'"),
-                             capsfile, qemuCaps->binary);
+                             capsfile, binary);
         goto cleanup;
     }
 
-    if (virQEMUCapsLoadCache(caps, qemuCaps, capsfile) < 0) {
+    if (!(qemuCapsNew = virQEMUCapsNew()))
+        goto cleanup;
+
+    if (VIR_STRDUP(qemuCapsNew->binary, binary) < 0)
+        goto discard;
+
+    if (virQEMUCapsLoadCache(caps, qemuCapsNew, capsfile) < 0) {
         VIR_WARN("Failed to load cached caps from '%s' for '%s': %s",
-                 capsfile, qemuCaps->binary, virGetLastErrorMessage());
+                 capsfile, qemuCapsNew->binary, virGetLastErrorMessage());
         virResetLastError();
         goto discard;
     }
 
-    if (!virQEMUCapsIsValid(qemuCaps, runUid, runGid))
+    if (!virQEMUCapsIsValid(qemuCapsNew, runUid, runGid))
         goto discard;
 
     VIR_DEBUG("Loaded '%s' for '%s' ctime %lld usedQMP=%d",
-              capsfile, qemuCaps->binary,
-              (long long)qemuCaps->ctime, qemuCaps->usedQMP);
+              capsfile, qemuCapsNew->binary,
+              (long long)qemuCapsNew->ctime, qemuCapsNew->usedQMP);
 
     ret = 1;
+    *qemuCaps = qemuCapsNew;
  cleanup:
     VIR_FREE(binaryhash);
     VIR_FREE(capsfile);
@@ -4412,9 +4388,9 @@ virQEMUCapsInitCached(virCapsPtr caps,
 
  discard:
     VIR_DEBUG("Dropping cached capabilities '%s' for '%s'",
-              capsfile, qemuCaps->binary);
+              capsfile, qemuCapsNew->binary);
     ignore_value(unlink(capsfile));
-    virQEMUCapsReset(qemuCaps);
+    virObjectUnref(qemuCapsNew);
     ret = 0;
     goto cleanup;
 }
@@ -5230,14 +5206,12 @@ virQEMUCapsPtr
 virQEMUCapsNewForBinaryInternal(virCapsPtr caps,
                                 const char *binary,
                                 const char *libDir,
-                                const char *cacheDir,
                                 uid_t runUid,
                                 gid_t runGid,
                                 bool qmpOnly)
 {
     virQEMUCapsPtr qemuCaps;
     struct stat sb;
-    int rv;
     char *qmperr = NULL;
 
     if (!(qemuCaps = virQEMUCapsNew()))
@@ -5265,42 +5239,30 @@ virQEMUCapsNewForBinaryInternal(virCapsPtr caps,
         goto error;
     }
 
-    if (!cacheDir)
-        rv = 0;
-    else if ((rv = virQEMUCapsInitCached(caps, qemuCaps, cacheDir,
-                                         runUid, runGid)) < 0)
+    if (virQEMUCapsInitQMP(qemuCaps, libDir, runUid, runGid, &qmperr) < 0) {
+        virQEMUCapsLogProbeFailure(binary);
         goto error;
+    }
 
-    if (rv == 0) {
-        if (virQEMUCapsInitQMP(qemuCaps, libDir, runUid, runGid, &qmperr) < 0) {
-            virQEMUCapsLogProbeFailure(binary);
-            goto error;
-        }
-
-        if (qmpOnly && !qemuCaps->usedQMP) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("Failed to probe QEMU binary with QMP: %s"),
-                           qmperr ? qmperr : _("unknown error"));
-            virQEMUCapsLogProbeFailure(binary);
-            goto error;
-        }
-
-        if (!qemuCaps->usedQMP &&
-            virQEMUCapsInitHelp(qemuCaps, runUid, runGid, qmperr) < 0) {
-            virQEMUCapsLogProbeFailure(binary);
-            goto error;
-        }
+    if (qmpOnly && !qemuCaps->usedQMP) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Failed to probe QEMU binary with QMP: %s"),
+                       qmperr ? qmperr : _("unknown error"));
+        virQEMUCapsLogProbeFailure(binary);
+        goto error;
+    }
 
-        qemuCaps->libvirtCtime = virGetSelfLastChanged();
-        qemuCaps->libvirtVersion = LIBVIR_VERSION_NUMBER;
+    if (!qemuCaps->usedQMP &&
+        virQEMUCapsInitHelp(qemuCaps, runUid, runGid, qmperr) < 0) {
+        virQEMUCapsLogProbeFailure(binary);
+        goto error;
+    }
 
-        if (cacheDir &&
-            virQEMUCapsRememberCached(qemuCaps, cacheDir) < 0)
-            goto error;
+    qemuCaps->libvirtCtime = virGetSelfLastChanged();
+    qemuCaps->libvirtVersion = LIBVIR_VERSION_NUMBER;
 
-        virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_KVM);
-        virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_QEMU);
-    }
+    virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_KVM);
+    virQEMUCapsInitHostCPUModel(qemuCaps, caps, VIR_DOMAIN_VIRT_QEMU);
 
  cleanup:
     VIR_FREE(qmperr);
@@ -5320,8 +5282,29 @@ virQEMUCapsNewForBinary(virCapsPtr caps,
                         uid_t runUid,
                         gid_t runGid)
 {
-    return virQEMUCapsNewForBinaryInternal(caps, binary, libDir, cacheDir,
-                                           runUid, runGid, false);
+    int rv;
+    virQEMUCapsPtr qemuCaps = NULL;
+
+    if ((rv = virQEMUCapsInitCached(caps, &qemuCaps, binary, cacheDir,
+                                    runUid, runGid)) < 0)
+        goto error;
+
+    if (rv == 0) {
+        if (!(qemuCaps = virQEMUCapsNewForBinaryInternal(caps, binary,
+                                                         libDir, runUid,
+                                                         runGid, false))) {
+            goto error;
+        }
+
+        if (virQEMUCapsRememberCached(qemuCaps, cacheDir) < 0)
+            goto error;
+    }
+
+    return qemuCaps;
+
+ error:
+    virObjectUnref(qemuCaps);
+    return NULL;
 }
 
 
diff --git a/src/qemu/qemu_capspriv.h b/src/qemu/qemu_capspriv.h
index 1162e0b284..3458fc0700 100644
--- a/src/qemu/qemu_capspriv.h
+++ b/src/qemu/qemu_capspriv.h
@@ -43,7 +43,6 @@ virQEMUCapsPtr
 virQEMUCapsNewForBinaryInternal(virCapsPtr caps,
                                 const char *binary,
                                 const char *libDir,
-                                const char *cacheDir,
                                 uid_t runUid,
                                 gid_t runGid,
                                 bool qmpOnly);
diff --git a/tests/qemucapsprobe.c b/tests/qemucapsprobe.c
index 662c7036ed..581ac38465 100644
--- a/tests/qemucapsprobe.c
+++ b/tests/qemucapsprobe.c
@@ -70,7 +70,7 @@ main(int argc, char **argv)
     if (virThreadCreate(&thread, false, eventLoop, NULL) < 0)
         return EXIT_FAILURE;
 
-    if (!(caps = virQEMUCapsNewForBinaryInternal(NULL, argv[1], "/tmp", NULL,
+    if (!(caps = virQEMUCapsNewForBinaryInternal(NULL, argv[1], "/tmp",
                                                  -1, -1, true)))
         return EXIT_FAILURE;
 
-- 
2.13.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