提交 ff619e0e 编写于 作者: J John Ferlan

uml: Create accessors to virDomainObjListFindByUUID

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: NJohn Ferlan <jferlan@redhat.com>
Reviewed-by: NDaniel P. Berrangé <berrange@redhat.com>
上级 ee3a86d7
...@@ -163,6 +163,39 @@ umlVMDriverUnlock(void) ...@@ -163,6 +163,39 @@ umlVMDriverUnlock(void)
umlDriverUnlock(uml_driver); 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 = { static virNWFilterCallbackDriver umlCallbackDriver = {
.name = "UML", .name = "UML",
.vmFilterRebuild = umlVMFilterRebuild, .vmFilterRebuild = umlVMFilterRebuild,
...@@ -1368,20 +1401,14 @@ static virDomainPtr umlDomainLookupByID(virConnectPtr conn, ...@@ -1368,20 +1401,14 @@ static virDomainPtr umlDomainLookupByID(virConnectPtr conn,
} }
static virDomainPtr umlDomainLookupByUUID(virConnectPtr conn, static virDomainPtr umlDomainLookupByUUID(virConnectPtr conn,
const unsigned char *uuid) const unsigned char *uuid)
{ {
struct uml_driver *driver = (struct uml_driver *)conn->privateData; struct uml_driver *driver = (struct uml_driver *)conn->privateData;
virDomainObjPtr vm; virDomainObjPtr vm;
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, uuid)))
vm = virDomainObjListFindByUUID(driver->domains, uuid); return NULL;
umlDriverUnlock(driver);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, NULL);
goto cleanup;
}
if (virDomainLookupByUUIDEnsureACL(conn, vm->def) < 0) if (virDomainLookupByUUIDEnsureACL(conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -1427,13 +1454,8 @@ static int umlDomainIsActive(virDomainPtr dom) ...@@ -1427,13 +1454,8 @@ static int umlDomainIsActive(virDomainPtr dom)
virDomainObjPtr obj; virDomainObjPtr obj;
int ret = -1; int ret = -1;
umlDriverLock(driver); if (!(obj = umlDomObjFromDomain(driver, dom->uuid)))
obj = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
umlDriverUnlock(driver);
if (!obj) {
virReportError(VIR_ERR_NO_DOMAIN, NULL);
goto cleanup;
}
if (virDomainIsActiveEnsureACL(dom->conn, obj->def) < 0) if (virDomainIsActiveEnsureACL(dom->conn, obj->def) < 0)
goto cleanup; goto cleanup;
...@@ -1453,13 +1475,8 @@ static int umlDomainIsPersistent(virDomainPtr dom) ...@@ -1453,13 +1475,8 @@ static int umlDomainIsPersistent(virDomainPtr dom)
virDomainObjPtr obj; virDomainObjPtr obj;
int ret = -1; int ret = -1;
umlDriverLock(driver); if (!(obj = umlDomObjFromDomain(driver, dom->uuid)))
obj = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
umlDriverUnlock(driver);
if (!obj) {
virReportError(VIR_ERR_NO_DOMAIN, NULL);
goto cleanup;
}
if (virDomainIsPersistentEnsureACL(dom->conn, obj->def) < 0) if (virDomainIsPersistentEnsureACL(dom->conn, obj->def) < 0)
goto cleanup; goto cleanup;
...@@ -1478,13 +1495,8 @@ static int umlDomainIsUpdated(virDomainPtr dom) ...@@ -1478,13 +1495,8 @@ static int umlDomainIsUpdated(virDomainPtr dom)
virDomainObjPtr obj; virDomainObjPtr obj;
int ret = -1; int ret = -1;
umlDriverLock(driver); if (!(obj = umlDomObjFromDomain(driver, dom->uuid)))
obj = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
umlDriverUnlock(driver);
if (!obj) {
virReportError(VIR_ERR_NO_DOMAIN, NULL);
goto cleanup;
}
if (virDomainIsUpdatedEnsureACL(dom->conn, obj->def) < 0) if (virDomainIsUpdatedEnsureACL(dom->conn, obj->def) < 0)
goto cleanup; goto cleanup;
...@@ -1637,14 +1649,8 @@ static int umlDomainShutdownFlags(virDomainPtr dom, ...@@ -1637,14 +1649,8 @@ static int umlDomainShutdownFlags(virDomainPtr dom,
virCheckFlags(0, -1); virCheckFlags(0, -1);
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
umlDriverUnlock(driver);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN,
_("no domain with matching id %d"), dom->id);
goto cleanup;
}
if (virDomainShutdownFlagsEnsureACL(dom->conn, vm->def, flags) < 0) if (virDomainShutdownFlagsEnsureACL(dom->conn, vm->def, flags) < 0)
goto cleanup; goto cleanup;
...@@ -1683,12 +1689,8 @@ umlDomainDestroyFlags(virDomainPtr dom, ...@@ -1683,12 +1689,8 @@ umlDomainDestroyFlags(virDomainPtr dom,
virCheckFlags(0, -1); virCheckFlags(0, -1);
umlDriverLock(driver); umlDriverLock(driver);
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
if (!vm) { return -1;
virReportError(VIR_ERR_NO_DOMAIN,
_("no domain with matching id %d"), dom->id);
goto cleanup;
}
if (virDomainDestroyFlagsEnsureACL(dom->conn, vm->def) < 0) if (virDomainDestroyFlagsEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -1726,14 +1728,8 @@ static char *umlDomainGetOSType(virDomainPtr dom) { ...@@ -1726,14 +1728,8 @@ static char *umlDomainGetOSType(virDomainPtr dom) {
virDomainObjPtr vm; virDomainObjPtr vm;
char *type = NULL; char *type = NULL;
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); return NULL;
umlDriverUnlock(driver);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, "%s",
_("no domain with matching uuid"));
goto cleanup;
}
if (virDomainGetOSTypeEnsureACL(dom->conn, vm->def) < 0) if (virDomainGetOSTypeEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -1755,18 +1751,8 @@ umlDomainGetMaxMemory(virDomainPtr dom) ...@@ -1755,18 +1751,8 @@ umlDomainGetMaxMemory(virDomainPtr dom)
virDomainObjPtr vm; virDomainObjPtr vm;
unsigned long long ret = 0; unsigned long long ret = 0;
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
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 (virDomainGetMaxMemoryEnsureACL(dom->conn, vm->def) < 0) if (virDomainGetMaxMemoryEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -1785,18 +1771,8 @@ static int umlDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) ...@@ -1785,18 +1771,8 @@ static int umlDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax)
virDomainObjPtr vm; virDomainObjPtr vm;
int ret = -1; int ret = -1;
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
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 (virDomainSetMaxMemoryEnsureACL(dom->conn, vm->def) < 0) if (virDomainSetMaxMemoryEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -1822,18 +1798,8 @@ static int umlDomainSetMemory(virDomainPtr dom, unsigned long newmem) ...@@ -1822,18 +1798,8 @@ static int umlDomainSetMemory(virDomainPtr dom, unsigned long newmem)
virDomainObjPtr vm; virDomainObjPtr vm;
int ret = -1; int ret = -1;
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
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 (virDomainSetMemoryEnsureACL(dom->conn, vm->def) < 0) if (virDomainSetMemoryEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -1866,15 +1832,8 @@ static int umlDomainGetInfo(virDomainPtr dom, ...@@ -1866,15 +1832,8 @@ static int umlDomainGetInfo(virDomainPtr dom,
virDomainObjPtr vm; virDomainObjPtr vm;
int ret = -1; int ret = -1;
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
umlDriverUnlock(driver);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, "%s",
_("no domain with matching uuid"));
goto cleanup;
}
if (virDomainGetInfoEnsureACL(dom->conn, vm->def) < 0) if (virDomainGetInfoEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -1915,15 +1874,8 @@ umlDomainGetState(virDomainPtr dom, ...@@ -1915,15 +1874,8 @@ umlDomainGetState(virDomainPtr dom,
virCheckFlags(0, -1); virCheckFlags(0, -1);
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
umlDriverUnlock(driver);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, "%s",
_("no domain with matching uuid"));
goto cleanup;
}
if (virDomainGetStateEnsureACL(dom->conn, vm->def) < 0) if (virDomainGetStateEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -1948,14 +1900,8 @@ static char *umlDomainGetXMLDesc(virDomainPtr dom, ...@@ -1948,14 +1900,8 @@ static char *umlDomainGetXMLDesc(virDomainPtr dom,
/* Flags checked by virDomainDefFormat */ /* Flags checked by virDomainDefFormat */
umlDriverLock(driver); umlDriverLock(driver);
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
umlDriverUnlock(driver);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, "%s",
_("no domain with matching uuid"));
goto cleanup; goto cleanup;
}
if (virDomainGetXMLDescEnsureACL(dom->conn, vm->def, flags) < 0) if (virDomainGetXMLDescEnsureACL(dom->conn, vm->def, flags) < 0)
goto cleanup; goto cleanup;
...@@ -2015,13 +1961,8 @@ static int umlDomainCreateWithFlags(virDomainPtr dom, unsigned int flags) ...@@ -2015,13 +1961,8 @@ static int umlDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
virNWFilterReadLockFilterUpdates(); virNWFilterReadLockFilterUpdates();
umlDriverLock(driver); umlDriverLock(driver);
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, "%s",
_("no domain with matching uuid"));
goto cleanup; goto cleanup;
}
if (virDomainCreateWithFlagsEnsureACL(dom->conn, vm->def) < 0) if (virDomainCreateWithFlagsEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -2115,12 +2056,8 @@ static int umlDomainUndefineFlags(virDomainPtr dom, ...@@ -2115,12 +2056,8 @@ static int umlDomainUndefineFlags(virDomainPtr dom,
virCheckFlags(0, -1); virCheckFlags(0, -1);
umlDriverLock(driver); umlDriverLock(driver);
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, "%s",
_("no domain with matching uuid"));
goto cleanup; goto cleanup;
}
if (virDomainUndefineFlagsEnsureACL(dom->conn, vm->def) < 0) if (virDomainUndefineFlagsEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -2213,14 +2150,8 @@ static int umlDomainAttachDevice(virDomainPtr dom, const char *xml) ...@@ -2213,14 +2150,8 @@ static int umlDomainAttachDevice(virDomainPtr dom, const char *xml)
umlDriverLock(driver); umlDriverLock(driver);
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!(vm = umlDomObjFromDomainLocked(driver, 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);
goto cleanup; goto cleanup;
}
if (virDomainAttachDeviceEnsureACL(dom->conn, vm->def) < 0) if (virDomainAttachDeviceEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -2333,14 +2264,8 @@ static int umlDomainDetachDevice(virDomainPtr dom, const char *xml) ...@@ -2333,14 +2264,8 @@ static int umlDomainDetachDevice(virDomainPtr dom, const char *xml)
int ret = -1; int ret = -1;
umlDriverLock(driver); umlDriverLock(driver);
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!(vm = umlDomObjFromDomainLocked(driver, 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);
goto cleanup; goto cleanup;
}
if (virDomainDetachDeviceEnsureACL(dom->conn, vm->def) < 0) if (virDomainDetachDeviceEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -2403,13 +2328,8 @@ static int umlDomainGetAutostart(virDomainPtr dom, ...@@ -2403,13 +2328,8 @@ static int umlDomainGetAutostart(virDomainPtr dom,
int ret = -1; int ret = -1;
umlDriverLock(driver); umlDriverLock(driver);
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, "%s",
_("no domain with matching uuid"));
goto cleanup; goto cleanup;
}
if (virDomainGetAutostartEnsureACL(dom->conn, vm->def) < 0) if (virDomainGetAutostartEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -2433,13 +2353,8 @@ static int umlDomainSetAutostart(virDomainPtr dom, ...@@ -2433,13 +2353,8 @@ static int umlDomainSetAutostart(virDomainPtr dom,
int ret = -1; int ret = -1;
umlDriverLock(driver); umlDriverLock(driver);
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, "%s",
_("no domain with matching uuid"));
goto cleanup; goto cleanup;
}
if (virDomainSetAutostartEnsureACL(dom->conn, vm->def) < 0) if (virDomainSetAutostartEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -2509,15 +2424,8 @@ umlDomainBlockPeek(virDomainPtr dom, ...@@ -2509,15 +2424,8 @@ umlDomainBlockPeek(virDomainPtr dom,
virCheckFlags(0, -1); virCheckFlags(0, -1);
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
umlDriverUnlock(driver);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, "%s",
_("no domain with matching uuid"));
goto cleanup;
}
if (virDomainBlockPeekEnsureACL(dom->conn, vm->def) < 0) if (virDomainBlockPeekEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -2573,7 +2481,6 @@ umlDomainOpenConsole(virDomainPtr dom, ...@@ -2573,7 +2481,6 @@ umlDomainOpenConsole(virDomainPtr dom,
{ {
struct uml_driver *driver = dom->conn->privateData; struct uml_driver *driver = dom->conn->privateData;
virDomainObjPtr vm = NULL; virDomainObjPtr vm = NULL;
char uuidstr[VIR_UUID_STRING_BUFLEN];
int ret = -1; int ret = -1;
virDomainChrDefPtr chr = NULL; virDomainChrDefPtr chr = NULL;
size_t i; size_t i;
...@@ -2581,13 +2488,8 @@ umlDomainOpenConsole(virDomainPtr dom, ...@@ -2581,13 +2488,8 @@ umlDomainOpenConsole(virDomainPtr dom,
virCheckFlags(0, -1); virCheckFlags(0, -1);
umlDriverLock(driver); umlDriverLock(driver);
virUUIDFormat(dom->uuid, uuidstr); if (!(vm = umlDomObjFromDomainLocked(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN,
_("no domain with matching uuid '%s'"), uuidstr);
goto cleanup; goto cleanup;
}
if (virDomainOpenConsoleEnsureACL(dom->conn, vm->def) < 0) if (virDomainOpenConsoleEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
...@@ -2921,14 +2823,8 @@ umlDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags) ...@@ -2921,14 +2823,8 @@ umlDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
virCheckFlags(0, -1); virCheckFlags(0, -1);
umlDriverLock(driver); if (!(vm = umlDomObjFromDomain(driver, dom->uuid)))
vm = virDomainObjListFindByUUID(driver->domains, dom->uuid); return -1;
umlDriverUnlock(driver);
if (!vm) {
virReportError(VIR_ERR_NO_DOMAIN, NULL);
goto cleanup;
}
if (virDomainHasManagedSaveImageEnsureACL(dom->conn, vm->def) < 0) if (virDomainHasManagedSaveImageEnsureACL(dom->conn, vm->def) < 0)
goto cleanup; goto cleanup;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册