[libvirt] [PATCH v2 3/5] uml: Create accessors to virDomainObjListFindByUUID

John Ferlan jferlan at redhat.com
Mon Apr 2 13:06:14 UTC 2018


Rather than repeat code throughout, create and use a couple of
accessors in order to lookup by UUID. This will also generate
a common error message including the failed uuidstr for lookup
rather than just returning nothing in some instances.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/uml/uml_driver.c | 244 +++++++++++++++------------------------------------
 1 file changed, 70 insertions(+), 174 deletions(-)

diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c
index b84585d728..ad526a1e6b 100644
--- a/src/uml/uml_driver.c
+++ b/src/uml/uml_driver.c
@@ -163,6 +163,39 @@ umlVMDriverUnlock(void)
     umlDriverUnlock(uml_driver);
 }
 
+
+static virDomainObjPtr
+umlDomObjFromDomainLocked(struct uml_driver *driver,
+                          const unsigned char *uuid)
+{
+    virDomainObjPtr vm;
+    char uuidstr[VIR_UUID_STRING_BUFLEN];
+
+    if (!(vm = virDomainObjListFindByUUID(driver->domains, uuid))) {
+        virUUIDFormat(uuid, uuidstr);
+
+        virReportError(VIR_ERR_NO_DOMAIN,
+                       _("no domain with matching uuid '%s'"), uuidstr);
+        return NULL;
+    }
+
+    return vm;
+}
+
+
+static virDomainObjPtr
+umlDomObjFromDomain(struct uml_driver *driver,
+                    const unsigned char *uuid)
+{
+    virDomainObjPtr vm;
+
+    umlDriverLock(driver);
+    vm = umlDomObjFromDomainLocked(driver, uuid);
+    umlDriverUnlock(driver);
+    return vm;
+}
+
+
 static virNWFilterCallbackDriver umlCallbackDriver = {
     .name = "UML",
     .vmFilterRebuild = umlVMFilterRebuild,
@@ -1376,20 +1409,14 @@ static virDomainPtr umlDomainLookupByID(virConnectPtr conn,
 }
 
 static virDomainPtr umlDomainLookupByUUID(virConnectPtr conn,
-                                            const unsigned char *uuid)
+                                          const unsigned char *uuid)
 {
     struct uml_driver *driver = (struct uml_driver *)conn->privateData;
     virDomainObjPtr vm;
     virDomainPtr dom = NULL;
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, uuid);
-    umlDriverUnlock(driver);
-
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, NULL);
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, uuid)))
+        return NULL;
 
     if (virDomainLookupByUUIDEnsureACL(conn, vm->def) < 0)
         goto cleanup;
@@ -1435,13 +1462,8 @@ static int umlDomainIsActive(virDomainPtr dom)
     virDomainObjPtr obj;
     int ret = -1;
 
-    umlDriverLock(driver);
-    obj = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-    if (!obj) {
-        virReportError(VIR_ERR_NO_DOMAIN, NULL);
-        goto cleanup;
-    }
+    if (!(obj = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainIsActiveEnsureACL(dom->conn, obj->def) < 0)
         goto cleanup;
@@ -1461,13 +1483,8 @@ static int umlDomainIsPersistent(virDomainPtr dom)
     virDomainObjPtr obj;
     int ret = -1;
 
-    umlDriverLock(driver);
-    obj = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-    if (!obj) {
-        virReportError(VIR_ERR_NO_DOMAIN, NULL);
-        goto cleanup;
-    }
+    if (!(obj = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainIsPersistentEnsureACL(dom->conn, obj->def) < 0)
         goto cleanup;
@@ -1486,13 +1503,8 @@ static int umlDomainIsUpdated(virDomainPtr dom)
     virDomainObjPtr obj;
     int ret = -1;
 
-    umlDriverLock(driver);
-    obj = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-    if (!obj) {
-        virReportError(VIR_ERR_NO_DOMAIN, NULL);
-        goto cleanup;
-    }
+    if (!(obj = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainIsUpdatedEnsureACL(dom->conn, obj->def) < 0)
         goto cleanup;
@@ -1645,14 +1657,8 @@ static int umlDomainShutdownFlags(virDomainPtr dom,
 
     virCheckFlags(0, -1);
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN,
-                       _("no domain with matching id %d"), dom->id);
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainShutdownFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
         goto cleanup;
@@ -1691,12 +1697,8 @@ umlDomainDestroyFlags(virDomainPtr dom,
     virCheckFlags(0, -1);
 
     umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN,
-                       _("no domain with matching id %d"), dom->id);
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
+        return -1;
 
     if (virDomainDestroyFlagsEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -1734,14 +1736,8 @@ static char *umlDomainGetOSType(virDomainPtr dom) {
     virDomainObjPtr vm;
     char *type = NULL;
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, "%s",
-                       _("no domain with matching uuid"));
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
+        return NULL;
 
     if (virDomainGetOSTypeEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -1763,18 +1759,8 @@ umlDomainGetMaxMemory(virDomainPtr dom)
     virDomainObjPtr vm;
     unsigned long long ret = 0;
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-
-    if (!vm) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-
-        virUUIDFormat(dom->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_DOMAIN,
-                       _("no domain with matching uuid '%s'"), uuidstr);
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainGetMaxMemoryEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -1793,18 +1779,8 @@ static int umlDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax)
     virDomainObjPtr vm;
     int ret = -1;
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-
-    if (!vm) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-
-        virUUIDFormat(dom->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_DOMAIN,
-                       _("no domain with matching uuid '%s'"), uuidstr);
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainSetMaxMemoryEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -1830,18 +1806,8 @@ static int umlDomainSetMemory(virDomainPtr dom, unsigned long newmem)
     virDomainObjPtr vm;
     int ret = -1;
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-
-    if (!vm) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-
-        virUUIDFormat(dom->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_DOMAIN,
-                       _("no domain with matching uuid '%s'"), uuidstr);
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainSetMemoryEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -1874,15 +1840,8 @@ static int umlDomainGetInfo(virDomainPtr dom,
     virDomainObjPtr vm;
     int ret = -1;
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, "%s",
-                       _("no domain with matching uuid"));
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainGetInfoEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -1923,15 +1882,8 @@ umlDomainGetState(virDomainPtr dom,
 
     virCheckFlags(0, -1);
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, "%s",
-                       _("no domain with matching uuid"));
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainGetStateEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -1956,14 +1908,8 @@ static char *umlDomainGetXMLDesc(virDomainPtr dom,
     /* Flags checked by virDomainDefFormat */
 
     umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, "%s",
-                       _("no domain with matching uuid"));
+    if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
         goto cleanup;
-    }
 
     if (virDomainGetXMLDescEnsureACL(dom->conn, vm->def, flags) < 0)
         goto cleanup;
@@ -2023,13 +1969,8 @@ static int umlDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
 
     virNWFilterReadLockFilterUpdates();
     umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, "%s",
-                       _("no domain with matching uuid"));
+    if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
         goto cleanup;
-    }
 
     if (virDomainCreateWithFlagsEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -2123,12 +2064,8 @@ static int umlDomainUndefineFlags(virDomainPtr dom,
     virCheckFlags(0, -1);
 
     umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, "%s",
-                       _("no domain with matching uuid"));
+    if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
         goto cleanup;
-    }
 
     if (virDomainUndefineFlagsEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -2221,14 +2158,8 @@ static int umlDomainAttachDevice(virDomainPtr dom, const char *xml)
 
     umlDriverLock(driver);
 
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    if (!vm) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-        virUUIDFormat(dom->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_DOMAIN,
-                       _("no domain with matching uuid '%s'"), uuidstr);
+    if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
         goto cleanup;
-    }
 
     if (virDomainAttachDeviceEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -2341,14 +2272,8 @@ static int umlDomainDetachDevice(virDomainPtr dom, const char *xml)
     int ret = -1;
 
     umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    if (!vm) {
-        char uuidstr[VIR_UUID_STRING_BUFLEN];
-        virUUIDFormat(dom->uuid, uuidstr);
-        virReportError(VIR_ERR_NO_DOMAIN,
-                       _("no domain with matching uuid '%s'"), uuidstr);
+    if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
         goto cleanup;
-    }
 
     if (virDomainDetachDeviceEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -2411,13 +2336,8 @@ static int umlDomainGetAutostart(virDomainPtr dom,
     int ret = -1;
 
     umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, "%s",
-                       _("no domain with matching uuid"));
+    if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
         goto cleanup;
-    }
 
     if (virDomainGetAutostartEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -2441,13 +2361,8 @@ static int umlDomainSetAutostart(virDomainPtr dom,
     int ret = -1;
 
     umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, "%s",
-                       _("no domain with matching uuid"));
+    if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
         goto cleanup;
-    }
 
     if (virDomainSetAutostartEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -2517,15 +2432,8 @@ umlDomainBlockPeek(virDomainPtr dom,
 
     virCheckFlags(0, -1);
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, "%s",
-                       _("no domain with matching uuid"));
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainBlockPeekEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -2581,7 +2489,6 @@ umlDomainOpenConsole(virDomainPtr dom,
 {
     struct uml_driver *driver = dom->conn->privateData;
     virDomainObjPtr vm = NULL;
-    char uuidstr[VIR_UUID_STRING_BUFLEN];
     int ret = -1;
     virDomainChrDefPtr chr = NULL;
     size_t i;
@@ -2589,13 +2496,8 @@ umlDomainOpenConsole(virDomainPtr dom,
     virCheckFlags(0, -1);
 
     umlDriverLock(driver);
-    virUUIDFormat(dom->uuid, uuidstr);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN,
-                       _("no domain with matching uuid '%s'"), uuidstr);
+    if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
         goto cleanup;
-    }
 
     if (virDomainOpenConsoleEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
@@ -2932,14 +2834,8 @@ umlDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
 
     virCheckFlags(0, -1);
 
-    umlDriverLock(driver);
-    vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
-    umlDriverUnlock(driver);
-
-    if (!vm) {
-        virReportError(VIR_ERR_NO_DOMAIN, NULL);
-        goto cleanup;
-    }
+    if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
+        return -1;
 
     if (virDomainHasManagedSaveImageEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;
-- 
2.13.6




More information about the libvir-list mailing list