提交 d7da92a9 编写于 作者: J Jonathon Jongsma 提交者: Erik Skultety

nodedev: Use autofree for more local variables

Simplify function logic by using g_autofree to free local variables so
that we can remove some goto statements that are used for cleanup.

Introduce a g_autoptr cleanup function for virNodeDeviceDef.
Signed-off-by: NJonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: NErik Skultety <eskultet@redhat.com>
上级 153fd683
...@@ -344,6 +344,8 @@ virNodeDeviceGetWWNs(virNodeDeviceDefPtr def, ...@@ -344,6 +344,8 @@ virNodeDeviceGetWWNs(virNodeDeviceDefPtr def,
void void
virNodeDeviceDefFree(virNodeDeviceDefPtr def); virNodeDeviceDefFree(virNodeDeviceDefPtr def);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNodeDeviceDef, virNodeDeviceDefFree);
void void
virNodeDevCapsDefFree(virNodeDevCapsDefPtr caps); virNodeDevCapsDefFree(virNodeDevCapsDefPtr caps);
......
...@@ -106,10 +106,9 @@ int nodeConnectIsAlive(virConnectPtr conn G_GNUC_UNUSED) ...@@ -106,10 +106,9 @@ int nodeConnectIsAlive(virConnectPtr conn G_GNUC_UNUSED)
static int static int
nodeDeviceUpdateDriverName(virNodeDeviceDefPtr def) nodeDeviceUpdateDriverName(virNodeDeviceDefPtr def)
{ {
char *driver_link = NULL; g_autofree char *driver_link = NULL;
char *devpath = NULL; g_autofree char *devpath = NULL;
char *p; char *p;
int ret = -1;
VIR_FREE(def->driver); VIR_FREE(def->driver);
...@@ -117,26 +116,20 @@ nodeDeviceUpdateDriverName(virNodeDeviceDefPtr def) ...@@ -117,26 +116,20 @@ nodeDeviceUpdateDriverName(virNodeDeviceDefPtr def)
/* Some devices don't have an explicit driver, so just return /* Some devices don't have an explicit driver, so just return
without a name */ without a name */
if (access(driver_link, R_OK) < 0) { if (access(driver_link, R_OK) < 0)
ret = 0; return 0;
goto cleanup;
}
if (virFileResolveLink(driver_link, &devpath) < 0) { if (virFileResolveLink(driver_link, &devpath) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot resolve driver link %s"), driver_link); _("cannot resolve driver link %s"), driver_link);
goto cleanup; return -1;
} }
p = strrchr(devpath, '/'); p = strrchr(devpath, '/');
if (p) if (p)
def->driver = g_strdup(p + 1); def->driver = g_strdup(p + 1);
ret = 0;
cleanup: return 0;
VIR_FREE(driver_link);
VIR_FREE(devpath);
return ret;
} }
#else #else
/* XXX: Implement me for non-linux */ /* XXX: Implement me for non-linux */
...@@ -468,8 +461,9 @@ nodeDeviceCreateXML(virConnectPtr conn, ...@@ -468,8 +461,9 @@ nodeDeviceCreateXML(virConnectPtr conn,
const char *xmlDesc, const char *xmlDesc,
unsigned int flags) unsigned int flags)
{ {
virNodeDeviceDefPtr def = NULL; g_autoptr(virNodeDeviceDef) def = NULL;
char *wwnn = NULL, *wwpn = NULL; g_autofree char *wwnn = NULL;
g_autofree char *wwpn = NULL;
int parent_host = -1; int parent_host = -1;
virNodeDevicePtr device = NULL; virNodeDevicePtr device = NULL;
const char *virt_type = NULL; const char *virt_type = NULL;
...@@ -478,19 +472,19 @@ nodeDeviceCreateXML(virConnectPtr conn, ...@@ -478,19 +472,19 @@ nodeDeviceCreateXML(virConnectPtr conn,
virt_type = virConnectGetType(conn); virt_type = virConnectGetType(conn);
if (!(def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, virt_type))) if (!(def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, virt_type)))
goto cleanup; return NULL;
if (virNodeDeviceCreateXMLEnsureACL(conn, def) < 0) if (virNodeDeviceCreateXMLEnsureACL(conn, def) < 0)
goto cleanup; return NULL;
if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
goto cleanup; return NULL;
if ((parent_host = virNodeDeviceObjListGetParentHost(driver->devs, def)) < 0) if ((parent_host = virNodeDeviceObjListGetParentHost(driver->devs, def)) < 0)
goto cleanup; return NULL;
if (virVHBAManageVport(parent_host, wwpn, wwnn, VPORT_CREATE) < 0) if (virVHBAManageVport(parent_host, wwpn, wwnn, VPORT_CREATE) < 0)
goto cleanup; return NULL;
device = nodeDeviceFindNewDevice(conn, wwnn, wwpn); device = nodeDeviceFindNewDevice(conn, wwnn, wwpn);
/* We don't check the return value, because one way or another, /* We don't check the return value, because one way or another,
...@@ -501,10 +495,7 @@ nodeDeviceCreateXML(virConnectPtr conn, ...@@ -501,10 +495,7 @@ nodeDeviceCreateXML(virConnectPtr conn,
_("no node device for '%s' with matching " _("no node device for '%s' with matching "
"wwnn '%s' and wwpn '%s'"), "wwnn '%s' and wwpn '%s'"),
def->name, wwnn, wwpn); def->name, wwnn, wwpn);
cleanup:
virNodeDeviceDefFree(def);
VIR_FREE(wwnn);
VIR_FREE(wwpn);
return device; return device;
} }
...@@ -515,8 +506,9 @@ nodeDeviceDestroy(virNodeDevicePtr device) ...@@ -515,8 +506,9 @@ nodeDeviceDestroy(virNodeDevicePtr device)
int ret = -1; int ret = -1;
virNodeDeviceObjPtr obj = NULL; virNodeDeviceObjPtr obj = NULL;
virNodeDeviceDefPtr def; virNodeDeviceDefPtr def;
char *parent = NULL; g_autofree char *parent = NULL;
char *wwnn = NULL, *wwpn = NULL; g_autofree char *wwnn = NULL;
g_autofree char *wwpn = NULL;
unsigned int parent_host; unsigned int parent_host;
if (!(obj = nodeDeviceObjFindByName(device->name))) if (!(obj = nodeDeviceObjFindByName(device->name)))
...@@ -554,9 +546,6 @@ nodeDeviceDestroy(virNodeDevicePtr device) ...@@ -554,9 +546,6 @@ nodeDeviceDestroy(virNodeDevicePtr device)
cleanup: cleanup:
virNodeDeviceObjEndAPI(&obj); virNodeDeviceObjEndAPI(&obj);
VIR_FREE(parent);
VIR_FREE(wwnn);
VIR_FREE(wwpn);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册