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 * @verbatim
* Revision History * Revision History
* *
* Date Who Description * Date Who Description
* 23/06/2013 Mark Riddoch Initial implementation * 23/06/2013 Mark Riddoch Initial implementation
* 08/01/2014 Massimiliano Pinto In user_alloc now we can pass function pointers for * 08/01/2014 Massimiliano Pinto In user_alloc now we can pass function pointers for
* copying/freeing keys and values independently via * copying/freeing keys and values independently via
* hashtable_memory_fns() routine * hashtable_memory_fns() routine
* *
* @endverbatim * @endverbatim
*/ */
@ -45,62 +45,68 @@
USERS * USERS *
users_alloc() users_alloc()
{ {
USERS *rval; USERS *rval;
if ((rval = calloc(1, sizeof(USERS))) == NULL) if ((rval = calloc(1, sizeof(USERS))) == NULL)
{ {
MXS_ERROR("[%s:%d]: Memory allocation failed.", __FUNCTION__, __LINE__); MXS_ERROR("[%s:%d]: Memory allocation failed.", __FUNCTION__, __LINE__);
return NULL; return NULL;
} }
if ((rval->data = hashtable_alloc(USERS_HASHTABLE_DEFAULT_SIZE, simple_str_hash, strcmp)) == NULL) if ((rval->data = hashtable_alloc(USERS_HASHTABLE_DEFAULT_SIZE, simple_str_hash, strcmp)) == NULL)
{ {
MXS_ERROR("[%s:%d]: Memory allocation failed.", __FUNCTION__, __LINE__); MXS_ERROR("[%s:%d]: Memory allocation failed.", __FUNCTION__, __LINE__);
free(rval); free(rval);
return NULL; 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 * Remove the users table
* *
* @param users The users table to remove * @param users The users table to remove
*/ */
void void
users_free(USERS *users) users_free(USERS *users)
{ {
if(users == NULL) if (users == NULL)
{ {
MXS_ERROR("[%s:%d]: NULL parameter.", __FUNCTION__, __LINE__); MXS_ERROR("[%s:%d]: NULL parameter.", __FUNCTION__, __LINE__);
return; return;
} }
if(users->data) if (users->data)
hashtable_free(users->data); {
hashtable_free(users->data);
}
free(users); free(users);
} }
/** /**
* Add a new user to the user table. The user name must be unique * Add a new user to the user table. The user name must be unique
* *
* @param users The users table * @param users The users table
* @param user The user name * @param user The user name
* @param auth The authentication data * @param auth The authentication data
* @return The number of users added to the table * @return The number of users added to the table
*/ */
int int
users_add(USERS *users, char *user, char *auth) users_add(USERS *users, char *user, char *auth)
{ {
int add; int add;
atomic_add(&users->stats.n_adds, 1); atomic_add(&users->stats.n_adds, 1);
add = hashtable_add(users->data, user, auth); add = hashtable_add(users->data, user, auth);
atomic_add(&users->stats.n_entries, add); atomic_add(&users->stats.n_entries, add);
return add; return add;
} }
/** /**
@ -108,116 +114,123 @@ int add;
* *
* The last user in the table can not be deleted * The last user in the table can not be deleted
* *
* @param users The users table * @param users The users table
* @param user The user name * @param user The user name
* @return The number of users deleted from the table * @return The number of users deleted from the table
*/ */
int int
users_delete(USERS *users, char *user) users_delete(USERS *users, char *user)
{ {
int del; int del;
if (users->stats.n_entries == 1) { if (users->stats.n_entries == 1)
return 0; {
} return 0;
atomic_add(&users->stats.n_deletes, 1); }
del = hashtable_delete(users->data, user); atomic_add(&users->stats.n_deletes, 1);
atomic_add(&users->stats.n_entries, -del); del = hashtable_delete(users->data, user);
return del; atomic_add(&users->stats.n_entries, -del);
return del;
} }
/** /**
* Fetch the authentication data for a particular user from the users table * Fetch the authentication data for a particular user from the users table
* *
* @param users The users table * @param users The users table
* @param user The user name * @param user The user name
* @return The authentication data or NULL on error * @return The authentication data or NULL on error
*/ */
char char
*users_fetch(USERS *users, char *user) *users_fetch(USERS *users, char *user)
{ {
atomic_add(&users->stats.n_fetches, 1); atomic_add(&users->stats.n_fetches, 1);
return hashtable_fetch(users->data, user); return hashtable_fetch(users->data, user);
} }
/** /**
* Change the password data associated with a user in the users * Change the password data associated with a user in the users
* table. * table.
* *
* @param users The users table * @param users The users table
* @param user The user name * @param user The user name
* @param auth The new authentication details * @param auth The new authentication details
* @return Number of users updated * @return Number of users updated
*/ */
int int
users_update(USERS *users, char *user, char *auth) users_update(USERS *users, char *user, char *auth)
{ {
if (hashtable_delete(users->data, user) == 0) if (hashtable_delete(users->data, user) == 0)
return 0; {
return hashtable_add(users->data, user, auth); return 0;
}
return hashtable_add(users->data, user, auth);
} }
/** /**
* Print details of the users storage mechanism * Print details of the users storage mechanism
* *
* @param users The users table * @param users The users table
*/ */
void void
usersPrint(USERS *users) usersPrint(USERS *users)
{ {
printf("Users table data\n"); printf("Users table data\n");
hashtable_stats(users->data); hashtable_stats(users->data);
} }
/** /**
* Print details of the users storage mechanism to a DCB * Print details of the users storage mechanism to a DCB
* *
* @param dcb DCB to print to * @param dcb DCB to print to
* @param users The users table * @param users The users table
*/ */
void void
dcb_usersPrint(DCB *dcb, USERS *users) dcb_usersPrint(DCB *dcb, USERS *users)
{ {
HASHITERATOR *iter; HASHITERATOR *iter;
char *sep; char *sep;
void *user; void *user;
dcb_printf(dcb, "Users table data\n"); 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 = "";
if (users->usersCustomUserFormat != NULL) { if (users == NULL || users->data == NULL)
while ((user = hashtable_next(iter)) != NULL) {
{ dcb_printf(dcb, "Users table is empty\n");
char *custom_user; }
custom_user = users->usersCustomUserFormat(user); else
if (custom_user) { {
dcb_printf(dcb, "%s%s", sep, custom_user); dcb_hashtable_stats(dcb, users->data);
free(custom_user);
sep = ", ";
}
}
} else {
while ((user = hashtable_next(iter)) != NULL)
{
dcb_printf(dcb, "%s%s", sep, (char *)user);
sep = ", ";
}
}
hashtable_iterator_free(iter); if ((iter = hashtable_iterator(users->data)) != NULL)
} {
} dcb_printf(dcb, "User names: ");
dcb_printf(dcb, "\n"); 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 * @verbatim
* Revision History * Revision History
* *
* Date Who Description * Date Who Description
* 23/06/13 Mark Riddoch Initial implementation * 23/06/13 Mark Riddoch Initial implementation
* 26/02/14 Massimiliano Pinto Added checksum to users' table with SHA1 * 26/02/14 Massimiliano Pinto Added checksum to users' table with SHA1
* 27/02/14 Massimiliano Pinto Added USERS_HASHTABLE_DEFAULT_SIZE * 27/02/14 Massimiliano Pinto Added USERS_HASHTABLE_DEFAULT_SIZE
* 28/02/14 Massimiliano Pinto Added usersCustomUserFormat, optional username format routine * 28/02/14 Massimiliano Pinto Added usersCustomUserFormat, optional username format routine
* *
* @endverbatim * @endverbatim
*/ */
@ -42,31 +42,34 @@
/** /**
* The users table statistics structure * The users table statistics structure
*/ */
typedef struct { typedef struct
int n_entries; /**< The number of entries */ {
int n_adds; /**< The number of inserts */ int n_entries; /**< The number of entries */
int n_deletes; /**< The number of deletes */ int n_adds; /**< The number of inserts */
int n_fetches; /**< The number of fetchs */ int n_deletes; /**< The number of deletes */
int n_fetches; /**< The number of fetchs */
} USERS_STATS; } USERS_STATS;
/** /**
* The user table, this contains the username and authentication data required * The user table, this contains the username and authentication data required
* for the authentication implementation within the gateway. * for the authentication implementation within the gateway.
*/ */
typedef struct users { typedef struct users
HASHTABLE *data; /**< The hashtable containing the actual data */ {
char *(*usersCustomUserFormat)(void *); /**< Optional username format routine */ HASHTABLE *data; /**< The hashtable containing the actual data */
USERS_STATS stats; /**< The statistics for the users table */ char *(*usersCustomUserFormat)(void *); /**< Optional username format routine */
unsigned char USERS_STATS stats; /**< The statistics for the users table */
cksum[SHA_DIGEST_LENGTH]; /**< The users' table ckecksum */ unsigned char cksum[SHA_DIGEST_LENGTH]; /**< The users' table ckecksum */
} USERS; } USERS;
extern USERS *users_alloc(); /**< Allocate a users table */ extern USERS *users_alloc(); /**< Allocate a users table */
extern void users_free(USERS *); /**< Free 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_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 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 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 int users_update(USERS *, char *, char *); /**< Change the password data for a user in
extern void usersPrint(USERS *); /**< Print data about the users loaded */ the users table */
extern void dcb_usersPrint(DCB *, USERS *); /**< Print data about the users loaded */ extern void usersPrint(USERS *); /**< Print data about the users loaded */
extern void dcb_usersPrint(DCB *, USERS *); /**< Print data about the users loaded */
#endif #endif