]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
src/driver.c: remove duplicated code in virGetConnect* functions
authorDaniel Henrique Barboza <danielhb413@gmail.com>
Tue, 8 Oct 2019 21:02:00 +0000 (18:02 -0300)
committerCole Robinson <crobinso@redhat.com>
Thu, 10 Oct 2019 17:51:18 +0000 (13:51 -0400)
All the 6 virGetConnect* functions in driver.c shares the
same code base. This patch creates a new static function
virGetConnectGeneric() that contains the common code to
be used with all other virGetConnect*.

Reviewed-by: Cole Robinson <crobinso@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
src/driver.c

index 305c4c624b82802e18e664be3513fdc7b1f33c06..4b526a73ae339000e87daa44e97508b84555baca 100644 (file)
@@ -135,112 +135,61 @@ virConnectCacheOnceInit(void)
 
 VIR_ONCE_GLOBAL_INIT(virConnectCache);
 
-virConnectPtr virGetConnectInterface(void)
+static virConnectPtr
+virGetConnectGeneric(virThreadLocalPtr threadPtr, const char *name)
 {
     virConnectPtr conn;
 
     if (virConnectCacheInitialize() < 0)
         return NULL;
 
-    conn = virThreadLocalGet(&connectInterface);
+    conn = virThreadLocalGet(threadPtr);
+
     if (conn) {
-        VIR_DEBUG("Return cached interface connection %p", conn);
+        VIR_DEBUG("Return cached %s connection %p", name, conn);
         virObjectRef(conn);
     } else {
-        conn = virConnectOpen(geteuid() == 0 ? "interface:///system" : "interface:///session");
-        VIR_DEBUG("Opened new interface connection %p", conn);
+        VIR_AUTOFREE(char *) uri = NULL;
+        const char *uriPath = geteuid() == 0 ? "/system" : "/session";
+
+        if (virAsprintf(&uri, "%s://%s", name, uriPath) < 0)
+            return NULL;
+
+        conn = virConnectOpen(uri);
+        VIR_DEBUG("Opened new %s connection %p", name, conn);
     }
     return conn;
 }
 
-virConnectPtr virGetConnectNetwork(void)
-{
-    virConnectPtr conn;
 
-    if (virConnectCacheInitialize() < 0)
-        return NULL;
+virConnectPtr virGetConnectInterface(void)
+{
+    return virGetConnectGeneric(&connectInterface, "interface");
+}
 
-    conn = virThreadLocalGet(&connectNetwork);
-    if (conn) {
-        VIR_DEBUG("Return cached network connection %p", conn);
-        virObjectRef(conn);
-    } else {
-        conn = virConnectOpen(geteuid() == 0 ? "network:///system" : "network:///session");
-        VIR_DEBUG("Opened new network connection %p", conn);
-    }
-    return conn;
+virConnectPtr virGetConnectNetwork(void)
+{
+    return virGetConnectGeneric(&connectNetwork, "network");
 }
 
 virConnectPtr virGetConnectNWFilter(void)
 {
-    virConnectPtr conn;
-
-    if (virConnectCacheInitialize() < 0)
-        return NULL;
-
-    conn = virThreadLocalGet(&connectNWFilter);
-    if (conn) {
-        VIR_DEBUG("Return cached nwfilter connection %p", conn);
-        virObjectRef(conn);
-    } else {
-        conn = virConnectOpen(geteuid() == 0 ? "nwfilter:///system" : "nwfilter:///session");
-        VIR_DEBUG("Opened new nwfilter connection %p", conn);
-    }
-    return conn;
+    return virGetConnectGeneric(&connectNWFilter, "nwfilter");
 }
 
 virConnectPtr virGetConnectNodeDev(void)
 {
-    virConnectPtr conn;
-
-    if (virConnectCacheInitialize() < 0)
-        return NULL;
-
-    conn = virThreadLocalGet(&connectNodeDev);
-    if (conn) {
-        VIR_DEBUG("Return cached nodedev connection %p", conn);
-        virObjectRef(conn);
-    } else {
-        conn = virConnectOpen(geteuid() == 0 ? "nodedev:///system" : "nodedev:///session");
-        VIR_DEBUG("Opened new nodedev connection %p", conn);
-    }
-    return conn;
+    return virGetConnectGeneric(&connectNodeDev, "nodedev");
 }
 
 virConnectPtr virGetConnectSecret(void)
 {
-    virConnectPtr conn;
-
-    if (virConnectCacheInitialize() < 0)
-        return NULL;
-
-    conn = virThreadLocalGet(&connectSecret);
-    if (conn) {
-        VIR_DEBUG("Return cached secret connection %p", conn);
-        virObjectRef(conn);
-    } else {
-        conn = virConnectOpen(geteuid() == 0 ? "secret:///system" : "secret:///session");
-        VIR_DEBUG("Opened new secret connection %p", conn);
-    }
-    return conn;
+    return virGetConnectGeneric(&connectSecret, "secret");
 }
 
 virConnectPtr virGetConnectStorage(void)
 {
-    virConnectPtr conn;
-
-    if (virConnectCacheInitialize() < 0)
-        return NULL;
-
-    conn = virThreadLocalGet(&connectStorage);
-    if (conn) {
-        VIR_DEBUG("Return cached storage connection %p", conn);
-        virObjectRef(conn);
-    } else {
-        conn = virConnectOpen(geteuid() == 0 ? "storage:///system" : "storage:///session");
-        VIR_DEBUG("Opened new storage connection %p", conn);
-    }
-    return conn;
+    return virGetConnectGeneric(&connectStorage, "storage");
 }