virvhba.c: use g_autofree

Change strings to use g_autofree.

Signed-off-by: Maxim Kostin <ttxinee@outlook.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
ttxine 2022-10-16 13:47:49 +03:00 committed by Michal Privoznik
parent babcbf2d5c
commit c90ee1441f

View File

@ -49,7 +49,7 @@ bool
virVHBAPathExists(const char *sysfs_prefix,
int host)
{
char *sysfs_path = NULL;
g_autofree char *sysfs_path = NULL;
bool ret = false;
sysfs_path = g_strdup_printf("%s/host%d",
@ -58,7 +58,6 @@ virVHBAPathExists(const char *sysfs_prefix,
if (virFileExists(sysfs_path))
ret = true;
VIR_FREE(sysfs_path);
return ret;
}
@ -79,8 +78,8 @@ bool
virVHBAIsVportCapable(const char *sysfs_prefix,
int host)
{
char *scsi_host_path = NULL;
char *fc_host_path = NULL;
g_autofree char *scsi_host_path = NULL;
g_autofree char *fc_host_path = NULL;
bool ret = false;
fc_host_path = g_strdup_printf("%s/host%d/%s",
@ -94,8 +93,6 @@ virVHBAIsVportCapable(const char *sysfs_prefix,
if (virFileExists(fc_host_path) || virFileExists(scsi_host_path))
ret = true;
VIR_FREE(fc_host_path);
VIR_FREE(scsi_host_path);
return ret;
}
@ -115,9 +112,9 @@ virVHBAGetConfig(const char *sysfs_prefix,
int host,
const char *entry)
{
char *sysfs_path = NULL;
g_autofree char *sysfs_path = NULL;
char *p = NULL;
char *buf = NULL;
g_autofree char *buf = NULL;
char *result = NULL;
sysfs_path = g_strdup_printf("%s/host%d/%s",
@ -140,8 +137,6 @@ virVHBAGetConfig(const char *sysfs_prefix,
result = g_strdup(p);
cleanup:
VIR_FREE(sysfs_path);
VIR_FREE(buf);
return result;
}
@ -160,15 +155,14 @@ virVHBAFindVportHost(const char *sysfs_prefix)
const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH;
g_autoptr(DIR) dir = NULL;
struct dirent *entry = NULL;
char *max_vports = NULL;
char *vports = NULL;
char *state = NULL;
char *ret = NULL;
if (virDirOpen(&dir, prefix) < 0)
return NULL;
while (virDirRead(dir, &entry, prefix) > 0) {
g_autofree char *state = NULL;
g_autofree char *max_vports = NULL;
g_autofree char *vports = NULL;
unsigned int host;
char *p = NULL;
@ -189,10 +183,8 @@ virVHBAFindVportHost(const char *sysfs_prefix)
/* Skip the not online FC host */
if (STRNEQ(state, PORT_STATE_ONLINE)) {
VIR_FREE(state);
continue;
}
VIR_FREE(state);
if (!(max_vports = virVHBAGetConfig(prefix, host, "max_npiv_vports"))) {
VIR_DEBUG("Failed to read max_npiv_vports for host%d", host);
@ -201,7 +193,6 @@ virVHBAFindVportHost(const char *sysfs_prefix)
if (!(vports = virVHBAGetConfig(prefix, host, "npiv_vports_inuse"))) {
VIR_DEBUG("Failed to read npiv_vports_inuse for host%d", host);
VIR_FREE(max_vports);
continue;
}
@ -211,18 +202,11 @@ virVHBAFindVportHost(const char *sysfs_prefix)
if ((strlen(max_vports) >= strlen(vports)) ||
((strlen(max_vports) == strlen(vports)) &&
strcmp(max_vports, vports) > 0)) {
ret = g_strdup(entry->d_name);
goto cleanup;
return g_strdup(entry->d_name);
}
VIR_FREE(max_vports);
VIR_FREE(vports);
}
cleanup:
VIR_FREE(max_vports);
VIR_FREE(vports);
return ret;
return NULL;
}
/* virVHBAManageVport:
@ -240,8 +224,8 @@ virVHBAManageVport(const int parent_host,
const char *wwnn,
int operation)
{
int ret = -1;
char *operation_path = NULL, *vport_name = NULL;
g_autofree char *operation_path = NULL;
g_autofree char *vport_name = NULL;
const char *operation_file = NULL;
switch (operation) {
@ -254,7 +238,7 @@ virVHBAManageVport(const int parent_host,
default:
virReportError(VIR_ERR_OPERATION_INVALID,
_("Invalid vport operation (%d)"), operation);
goto cleanup;
return -1;
}
operation_path = g_strdup_printf("%s/host%d/%s", SYSFS_FC_HOST_PATH,
@ -270,7 +254,7 @@ virVHBAManageVport(const int parent_host,
_("vport operation '%s' is not supported "
"for host%d"),
operation_file, parent_host);
goto cleanup;
return -1;
}
}
@ -282,18 +266,14 @@ virVHBAManageVport(const int parent_host,
* lifecycle event for applications to consume. */
vport_name = g_strdup_printf("%s:%s", wwpn, wwnn);
if (virFileWriteStr(operation_path, vport_name, 0) == 0)
ret = 0;
else
if (virFileWriteStr(operation_path, vport_name, 0) < 0) {
virReportSystemError(errno,
_("Write of '%s' to '%s' during "
"vport create/delete failed"),
_("Write of '%s' to '%s' during vport create/delete failed"),
vport_name, operation_path);
return -1;
}
cleanup:
VIR_FREE(vport_name);
VIR_FREE(operation_path);
return ret;
return 0;
}
@ -315,8 +295,8 @@ vhbaReadCompareWWN(const char *prefix,
const char *f_name,
const char *wwn)
{
char *path;
char *buf = NULL;
g_autofree char *path = NULL;
g_autofree char *buf = NULL;
char *p;
int ret = -1;
@ -343,8 +323,6 @@ vhbaReadCompareWWN(const char *prefix,
ret = 1;
cleanup:
VIR_FREE(path);
VIR_FREE(buf);
return ret;
}
@ -407,17 +385,14 @@ virVHBAGetHostByFabricWWN(const char *sysfs_prefix,
const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH;
struct dirent *entry = NULL;
g_autoptr(DIR) dir = NULL;
char *vport_create_path = NULL;
char *ret = NULL;
if (virDirOpen(&dir, prefix) < 0)
return NULL;
while (virDirRead(dir, &entry, prefix) > 0) {
g_autofree char *vport_create_path = NULL;
int rc;
VIR_FREE(vport_create_path);
/* Existing vHBA's will have the same fabric_name, but won't
* have the vport_create file - so we check for both */
vport_create_path = g_strdup_printf("%s/%s/vport_create", prefix,
@ -428,18 +403,15 @@ virVHBAGetHostByFabricWWN(const char *sysfs_prefix,
if ((rc = vhbaReadCompareWWN(prefix, entry->d_name,
"fabric_name", fabric_wwn)) < 0)
goto cleanup;
return NULL;
if (rc == 0)
continue;
ret = g_strdup(entry->d_name);
break;
return g_strdup(entry->d_name);
}
cleanup:
VIR_FREE(vport_create_path);
return ret;
return NULL;
}
#else