mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
util: auth: use VIR_AUTOFREE instead of VIR_FREE for scalar types
By making use of GNU C's cleanup attribute handled by the VIR_AUTOFREE macro for declaring scalar variables, majority of the VIR_FREE calls can be dropped, which in turn leads to getting rid of most of our cleanup sections. Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com> Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
parent
757c090899
commit
12614e7e25
@ -41,10 +41,9 @@ int
|
|||||||
virAuthGetConfigFilePathURI(virURIPtr uri,
|
virAuthGetConfigFilePathURI(virURIPtr uri,
|
||||||
char **path)
|
char **path)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
|
||||||
size_t i;
|
size_t i;
|
||||||
const char *authenv = virGetEnvBlockSUID("LIBVIRT_AUTH_FILE");
|
const char *authenv = virGetEnvBlockSUID("LIBVIRT_AUTH_FILE");
|
||||||
char *userdir = NULL;
|
VIR_AUTOFREE(char *) userdir = NULL;
|
||||||
|
|
||||||
*path = NULL;
|
*path = NULL;
|
||||||
|
|
||||||
@ -53,7 +52,7 @@ virAuthGetConfigFilePathURI(virURIPtr uri,
|
|||||||
if (authenv) {
|
if (authenv) {
|
||||||
VIR_DEBUG("Using path from env '%s'", authenv);
|
VIR_DEBUG("Using path from env '%s'", authenv);
|
||||||
if (VIR_STRDUP(*path, authenv) < 0)
|
if (VIR_STRDUP(*path, authenv) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,17 +62,17 @@ virAuthGetConfigFilePathURI(virURIPtr uri,
|
|||||||
uri->params[i].value) {
|
uri->params[i].value) {
|
||||||
VIR_DEBUG("Using path from URI '%s'", uri->params[i].value);
|
VIR_DEBUG("Using path from URI '%s'", uri->params[i].value);
|
||||||
if (VIR_STRDUP(*path, uri->params[i].value) < 0)
|
if (VIR_STRDUP(*path, uri->params[i].value) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(userdir = virGetUserConfigDirectory()))
|
if (!(userdir = virGetUserConfigDirectory()))
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
if (virAsprintf(path, "%s/auth.conf", userdir) < 0)
|
if (virAsprintf(path, "%s/auth.conf", userdir) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
VIR_DEBUG("Checking for readability of '%s'", *path);
|
VIR_DEBUG("Checking for readability of '%s'", *path);
|
||||||
if (access(*path, R_OK) == 0)
|
if (access(*path, R_OK) == 0)
|
||||||
@ -82,7 +81,7 @@ virAuthGetConfigFilePathURI(virURIPtr uri,
|
|||||||
VIR_FREE(*path);
|
VIR_FREE(*path);
|
||||||
|
|
||||||
if (VIR_STRDUP(*path, SYSCONFDIR "/libvirt/auth.conf") < 0)
|
if (VIR_STRDUP(*path, SYSCONFDIR "/libvirt/auth.conf") < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
VIR_DEBUG("Checking for readability of '%s'", *path);
|
VIR_DEBUG("Checking for readability of '%s'", *path);
|
||||||
if (access(*path, R_OK) == 0)
|
if (access(*path, R_OK) == 0)
|
||||||
@ -91,13 +90,9 @@ virAuthGetConfigFilePathURI(virURIPtr uri,
|
|||||||
VIR_FREE(*path);
|
VIR_FREE(*path);
|
||||||
|
|
||||||
done:
|
done:
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
VIR_DEBUG("Using auth file '%s'", NULLSTR(*path));
|
VIR_DEBUG("Using auth file '%s'", NULLSTR(*path));
|
||||||
cleanup:
|
|
||||||
VIR_FREE(userdir);
|
|
||||||
|
|
||||||
return ret;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -155,7 +150,7 @@ virAuthGetUsernamePath(const char *path,
|
|||||||
{
|
{
|
||||||
unsigned int ncred;
|
unsigned int ncred;
|
||||||
virConnectCredential cred;
|
virConnectCredential cred;
|
||||||
char *prompt;
|
VIR_AUTOFREE(char *) prompt = NULL;
|
||||||
char *ret = NULL;
|
char *ret = NULL;
|
||||||
|
|
||||||
if (virAuthGetCredential(servicename, hostname, "username", path, &ret) < 0)
|
if (virAuthGetCredential(servicename, hostname, "username", path, &ret) < 0)
|
||||||
@ -192,8 +187,6 @@ virAuthGetUsernamePath(const char *path,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
VIR_FREE(prompt);
|
|
||||||
|
|
||||||
return cred.result;
|
return cred.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,18 +198,13 @@ virAuthGetUsername(virConnectPtr conn,
|
|||||||
const char *defaultUsername,
|
const char *defaultUsername,
|
||||||
const char *hostname)
|
const char *hostname)
|
||||||
{
|
{
|
||||||
char *ret;
|
VIR_AUTOFREE(char *) path = NULL;
|
||||||
char *path;
|
|
||||||
|
|
||||||
if (virAuthGetConfigFilePath(conn, &path) < 0)
|
if (virAuthGetConfigFilePath(conn, &path) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
ret = virAuthGetUsernamePath(path, auth, servicename,
|
return virAuthGetUsernamePath(path, auth, servicename,
|
||||||
defaultUsername, hostname);
|
defaultUsername, hostname);
|
||||||
|
|
||||||
VIR_FREE(path);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -229,7 +217,7 @@ virAuthGetPasswordPath(const char *path,
|
|||||||
{
|
{
|
||||||
unsigned int ncred;
|
unsigned int ncred;
|
||||||
virConnectCredential cred;
|
virConnectCredential cred;
|
||||||
char *prompt;
|
VIR_AUTOFREE(char *) prompt = NULL;
|
||||||
char *ret = NULL;
|
char *ret = NULL;
|
||||||
|
|
||||||
if (virAuthGetCredential(servicename, hostname, "password", path, &ret) < 0)
|
if (virAuthGetCredential(servicename, hostname, "password", path, &ret) < 0)
|
||||||
@ -263,8 +251,6 @@ virAuthGetPasswordPath(const char *path,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
VIR_FREE(prompt);
|
|
||||||
|
|
||||||
return cred.result;
|
return cred.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,15 +262,10 @@ virAuthGetPassword(virConnectPtr conn,
|
|||||||
const char *username,
|
const char *username,
|
||||||
const char *hostname)
|
const char *hostname)
|
||||||
{
|
{
|
||||||
char *ret;
|
VIR_AUTOFREE(char *) path = NULL;
|
||||||
char *path;
|
|
||||||
|
|
||||||
if (virAuthGetConfigFilePath(conn, &path) < 0)
|
if (virAuthGetConfigFilePath(conn, &path) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
ret = virAuthGetPasswordPath(path, auth, servicename, username, hostname);
|
return virAuthGetPasswordPath(path, auth, servicename, username, hostname);
|
||||||
|
|
||||||
VIR_FREE(path);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user