Reindent server/core/users.c

This commit is contained in:
Johan Wikman 2015-11-30 20:29:03 +02:00
parent 8bbc3e8086
commit fbecf128dd
2 changed files with 141 additions and 125 deletions

View File

@ -28,11 +28,11 @@
* @verbatim
* Revision History
*
* Date Who Description
* 23/06/2013 Mark Riddoch Initial implementation
* 08/01/2014 Massimiliano Pinto In user_alloc now we can pass function pointers for
* copying/freeing keys and values independently via
* hashtable_memory_fns() routine
* Date Who Description
* 23/06/2013 Mark Riddoch Initial implementation
* 08/01/2014 Massimiliano Pinto In user_alloc now we can pass function pointers for
* copying/freeing keys and values independently via
* hashtable_memory_fns() routine
*
* @endverbatim
*/
@ -45,62 +45,68 @@
USERS *
users_alloc()
{
USERS *rval;
USERS *rval;
if ((rval = calloc(1, sizeof(USERS))) == NULL)
{
MXS_ERROR("[%s:%d]: Memory allocation failed.", __FUNCTION__, __LINE__);
return NULL;
}
if ((rval = calloc(1, sizeof(USERS))) == NULL)
{
MXS_ERROR("[%s:%d]: Memory allocation failed.", __FUNCTION__, __LINE__);
return NULL;
}
if ((rval->data = hashtable_alloc(USERS_HASHTABLE_DEFAULT_SIZE, simple_str_hash, strcmp)) == NULL)
{
MXS_ERROR("[%s:%d]: Memory allocation failed.", __FUNCTION__, __LINE__);
free(rval);
return NULL;
}
if ((rval->data = hashtable_alloc(USERS_HASHTABLE_DEFAULT_SIZE, simple_str_hash, strcmp)) == NULL)
{
MXS_ERROR("[%s:%d]: Memory allocation failed.", __FUNCTION__, __LINE__);
free(rval);
return NULL;
}
hashtable_memory_fns(rval->data, (HASHMEMORYFN)strdup, (HASHMEMORYFN)strdup, (HASHMEMORYFN)free, (HASHMEMORYFN)free);
hashtable_memory_fns(rval->data,
(HASHMEMORYFN)strdup,
(HASHMEMORYFN)strdup,
(HASHMEMORYFN)free,
(HASHMEMORYFN)free);
return rval;
return rval;
}
/**
* Remove the users table
*
* @param users The users table to remove
* @param users The users table to remove
*/
void
users_free(USERS *users)
{
if(users == NULL)
if (users == NULL)
{
MXS_ERROR("[%s:%d]: NULL parameter.", __FUNCTION__, __LINE__);
return;
MXS_ERROR("[%s:%d]: NULL parameter.", __FUNCTION__, __LINE__);
return;
}
if(users->data)
hashtable_free(users->data);
if (users->data)
{
hashtable_free(users->data);
}
free(users);
}
/**
* Add a new user to the user table. The user name must be unique
*
* @param users The users table
* @param user The user name
* @param auth The authentication data
* @return The number of users added to the table
* @param users The users table
* @param user The user name
* @param auth The authentication data
* @return The number of users added to the table
*/
int
users_add(USERS *users, char *user, char *auth)
{
int add;
int add;
atomic_add(&users->stats.n_adds, 1);
add = hashtable_add(users->data, user, auth);
atomic_add(&users->stats.n_entries, add);
return add;
atomic_add(&users->stats.n_adds, 1);
add = hashtable_add(users->data, user, auth);
atomic_add(&users->stats.n_entries, add);
return add;
}
/**
@ -108,116 +114,123 @@ int add;
*
* The last user in the table can not be deleted
*
* @param users The users table
* @param user The user name
* @return The number of users deleted from the table
* @param users The users table
* @param user The user name
* @return The number of users deleted from the table
*/
int
users_delete(USERS *users, char *user)
{
int del;
int del;
if (users->stats.n_entries == 1) {
return 0;
}
atomic_add(&users->stats.n_deletes, 1);
del = hashtable_delete(users->data, user);
atomic_add(&users->stats.n_entries, -del);
return del;
if (users->stats.n_entries == 1)
{
return 0;
}
atomic_add(&users->stats.n_deletes, 1);
del = hashtable_delete(users->data, user);
atomic_add(&users->stats.n_entries, -del);
return del;
}
/**
* Fetch the authentication data for a particular user from the users table
*
* @param users The users table
* @param user The user name
* @return The authentication data or NULL on error
* @param users The users table
* @param user The user name
* @return The authentication data or NULL on error
*/
char
*users_fetch(USERS *users, char *user)
*users_fetch(USERS *users, char *user)
{
atomic_add(&users->stats.n_fetches, 1);
return hashtable_fetch(users->data, user);
atomic_add(&users->stats.n_fetches, 1);
return hashtable_fetch(users->data, user);
}
/**
* Change the password data associated with a user in the users
* table.
*
* @param users The users table
* @param user The user name
* @param auth The new authentication details
* @param users The users table
* @param user The user name
* @param auth The new authentication details
* @return Number of users updated
*/
int
users_update(USERS *users, char *user, char *auth)
{
if (hashtable_delete(users->data, user) == 0)
return 0;
return hashtable_add(users->data, user, auth);
if (hashtable_delete(users->data, user) == 0)
{
return 0;
}
return hashtable_add(users->data, user, auth);
}
/**
* Print details of the users storage mechanism
*
* @param users The users table
* @param users The users table
*/
void
usersPrint(USERS *users)
{
printf("Users table data\n");
hashtable_stats(users->data);
printf("Users table data\n");
hashtable_stats(users->data);
}
/**
* Print details of the users storage mechanism to a DCB
*
* @param dcb DCB to print to
* @param users The users table
* @param dcb DCB to print to
* @param users The users table
*/
void
dcb_usersPrint(DCB *dcb, USERS *users)
{
HASHITERATOR *iter;
char *sep;
void *user;
HASHITERATOR *iter;
char *sep;
void *user;
dcb_printf(dcb, "Users table data\n");
if (users == NULL || users->data == NULL)
{
dcb_printf(dcb, "Users table is empty\n");
}
else
{
dcb_hashtable_stats(dcb, users->data);
if ((iter = hashtable_iterator(users->data)) != NULL)
{
dcb_printf(dcb, "User names: ");
sep = "";
dcb_printf(dcb, "Users table data\n");
if (users->usersCustomUserFormat != NULL) {
while ((user = hashtable_next(iter)) != NULL)
{
char *custom_user;
custom_user = users->usersCustomUserFormat(user);
if (custom_user) {
dcb_printf(dcb, "%s%s", sep, custom_user);
free(custom_user);
sep = ", ";
}
}
} else {
while ((user = hashtable_next(iter)) != NULL)
{
dcb_printf(dcb, "%s%s", sep, (char *)user);
sep = ", ";
}
}
if (users == NULL || users->data == NULL)
{
dcb_printf(dcb, "Users table is empty\n");
}
else
{
dcb_hashtable_stats(dcb, users->data);
hashtable_iterator_free(iter);
}
}
dcb_printf(dcb, "\n");
if ((iter = hashtable_iterator(users->data)) != NULL)
{
dcb_printf(dcb, "User names: ");
sep = "";
if (users->usersCustomUserFormat != NULL)
{
while ((user = hashtable_next(iter)) != NULL)
{
char *custom_user;
custom_user = users->usersCustomUserFormat(user);
if (custom_user)
{
dcb_printf(dcb, "%s%s", sep, custom_user);
free(custom_user);
sep = ", ";
}
}
}
else
{
while ((user = hashtable_next(iter)) != NULL)
{
dcb_printf(dcb, "%s%s", sep, (char *)user);
sep = ", ";
}
}
hashtable_iterator_free(iter);
}
}
dcb_printf(dcb, "\n");
}

View File

@ -28,11 +28,11 @@
* @verbatim
* Revision History
*
* Date Who Description
* 23/06/13 Mark Riddoch Initial implementation
* 26/02/14 Massimiliano Pinto Added checksum to users' table with SHA1
* 27/02/14 Massimiliano Pinto Added USERS_HASHTABLE_DEFAULT_SIZE
* 28/02/14 Massimiliano Pinto Added usersCustomUserFormat, optional username format routine
* Date Who Description
* 23/06/13 Mark Riddoch Initial implementation
* 26/02/14 Massimiliano Pinto Added checksum to users' table with SHA1
* 27/02/14 Massimiliano Pinto Added USERS_HASHTABLE_DEFAULT_SIZE
* 28/02/14 Massimiliano Pinto Added usersCustomUserFormat, optional username format routine
*
* @endverbatim
*/
@ -42,31 +42,34 @@
/**
* The users table statistics structure
*/
typedef struct {
int n_entries; /**< The number of entries */
int n_adds; /**< The number of inserts */
int n_deletes; /**< The number of deletes */
int n_fetches; /**< The number of fetchs */
typedef struct
{
int n_entries; /**< The number of entries */
int n_adds; /**< The number of inserts */
int n_deletes; /**< The number of deletes */
int n_fetches; /**< The number of fetchs */
} USERS_STATS;
/**
* The user table, this contains the username and authentication data required
* for the authentication implementation within the gateway.
*/
typedef struct users {
HASHTABLE *data; /**< The hashtable containing the actual data */
char *(*usersCustomUserFormat)(void *); /**< Optional username format routine */
USERS_STATS stats; /**< The statistics for the users table */
unsigned char
cksum[SHA_DIGEST_LENGTH]; /**< The users' table ckecksum */
typedef struct users
{
HASHTABLE *data; /**< The hashtable containing the actual data */
char *(*usersCustomUserFormat)(void *); /**< Optional username format routine */
USERS_STATS stats; /**< The statistics for the users table */
unsigned char cksum[SHA_DIGEST_LENGTH]; /**< The users' table ckecksum */
} USERS;
extern USERS *users_alloc(); /**< Allocate a users table */
extern void users_free(USERS *); /**< Free a users table */
extern int users_add(USERS *, char *, char *); /**< Add a user to the users table */
extern int users_delete(USERS *, char *); /**< Delete a user from the users table */
extern char *users_fetch(USERS *, char *); /**< Fetch the authentication data for a user */
extern int users_update(USERS *, char *, char *); /**< Change the password data for a user in the users table */
extern void usersPrint(USERS *); /**< Print data about the users loaded */
extern void dcb_usersPrint(DCB *, USERS *); /**< Print data about the users loaded */
extern USERS *users_alloc(); /**< Allocate a users table */
extern void users_free(USERS *); /**< Free a users table */
extern int users_add(USERS *, char *, char *); /**< Add a user to the users table */
extern int users_delete(USERS *, char *); /**< Delete a user from the users table */
extern char *users_fetch(USERS *, char *); /**< Fetch the authentication data for a user */
extern int users_update(USERS *, char *, char *); /**< Change the password data for a user in
the users table */
extern void usersPrint(USERS *); /**< Print data about the users loaded */
extern void dcb_usersPrint(DCB *, USERS *); /**< Print data about the users loaded */
#endif