[svn-commits] qwell: branch group/ast_storage r66605 - in /team/group/ast_storage: include/...

svn-commits at lists.digium.com svn-commits at lists.digium.com
Wed May 30 09:16:41 MST 2007


Author: qwell
Date: Wed May 30 11:16:40 2007
New Revision: 66605

URL: http://svn.digium.com/view/asterisk?view=rev&rev=66605
Log:
A few things I added during the drive home from astridevcon (no, I wasn't driving and coding)

Add a few fixes (ie; don't use malloc)
Add a new status function, so a storage backend can show additional information
Add a NULL backend
Add the ability to get the status of a single backend from the CLI

Modified:
    team/group/ast_storage/include/asterisk/storage.h
    team/group/ast_storage/main/storage.c
    team/group/ast_storage/res/res_storage_file.c
    team/group/ast_storage/res/res_storage_odbc.c

Modified: team/group/ast_storage/include/asterisk/storage.h
URL: http://svn.digium.com/view/asterisk/team/group/ast_storage/include/asterisk/storage.h?view=diff&rev=66605&r1=66604&r2=66605
==============================================================================
--- team/group/ast_storage/include/asterisk/storage.h (original)
+++ team/group/ast_storage/include/asterisk/storage.h Wed May 30 11:16:40 2007
@@ -43,7 +43,8 @@
 	void *(*opendir)(struct ast_storage *st, const char *objectpath);
 	int (*readdir)(struct ast_storage *st, void *dirptr, struct ast_storage_dirent *dirent);
 	int (*closedir)(struct ast_storage *st, void *dirptr);
-	AST_LIST_ENTRY(ast_storage_be) list;
+	int (*status)(struct ast_storage *st, int fd);
+	AST_RWLIST_ENTRY(ast_storage_be) list;
 	struct ast_module *module;
 };
 
@@ -124,6 +125,11 @@
  */
 int ast_storage_count(struct ast_storage *st, const char *objectname);
 
+/*! \brief Get storage engine by name
+ * \param name Name of storage engine
+ */
+struct ast_storage_be *ast_storage_getbyname(const char *name);
+
 /*! \brief Specify a backend specific parameter
  * \param st Storage instance
  * \param var Parameter name

Modified: team/group/ast_storage/main/storage.c
URL: http://svn.digium.com/view/asterisk/team/group/ast_storage/main/storage.c?view=diff&rev=66605&r1=66604&r2=66605
==============================================================================
--- team/group/ast_storage/main/storage.c (original)
+++ team/group/ast_storage/main/storage.c Wed May 30 11:16:40 2007
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2006, Digium, Inc.
+ * Copyright (C) 1999 - 2007, Digium, Inc.
  *
  * Mark Spencer <markster at digium.com>
  *
@@ -33,25 +33,25 @@
 #include "asterisk/linkedlists.h"
 #include "asterisk/module.h"
 
-static AST_LIST_HEAD_STATIC(storage_engines, ast_storage_be);
+static AST_RWLIST_HEAD_STATIC(storage_engines, ast_storage_be);
 
 int __ast_register_storage(const struct ast_storage_be *e, struct ast_module *mod)
 {
 	struct ast_storage_be *tmp;
 
-	if (AST_LIST_LOCK(&storage_engines)) {
-		ast_log(LOG_WARNING, "Unable to lock storage engine list\n");
-		return -1;
-	}
-	AST_LIST_TRAVERSE(&storage_engines, tmp, list) {
+	if (AST_RWLIST_WRLOCK(&storage_engines)) {
+		ast_log(LOG_WARNING, "Unable to lock storage engine list\n");
+		return -1;
+	}
+	AST_RWLIST_TRAVERSE(&storage_engines, tmp, list) {
 		if (!strcasecmp(e->name, tmp->name)) {
-			AST_LIST_UNLOCK(&storage_engines);
+			AST_RWLIST_UNLOCK(&storage_engines);
 			ast_log(LOG_WARNING, "Tried to register storage engine '%s', already registered\n", e->name);
 			return -1;
 		}
 	}
 	if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
-		AST_LIST_UNLOCK(&storage_engines);
+		AST_RWLIST_UNLOCK(&storage_engines);
 		return -1;
 	}
 	*tmp = *e;
@@ -59,8 +59,8 @@
 	
 	memset(&tmp->list, 0, sizeof(tmp->list));
 
-	AST_LIST_INSERT_HEAD(&storage_engines, tmp, list);
-	AST_LIST_UNLOCK(&storage_engines);
+	AST_RWLIST_INSERT_TAIL(&storage_engines, tmp, list);
+	AST_RWLIST_UNLOCK(&storage_engines);
 	if (option_verbose > 1)
 		ast_verbose(VERBOSE_PREFIX_2 "Registered storage engine '%s'\n", e->name);
 
@@ -72,19 +72,19 @@
 	struct ast_storage_be *tmp;
 	int res = -1;
 
-	if (AST_LIST_LOCK(&storage_engines)) {
-		ast_log(LOG_WARNING, "Unable to lock storage engine list\n");
-		return -1;
-	}
-	AST_LIST_TRAVERSE_SAFE_BEGIN(&storage_engines, tmp, list) {
+	if (AST_RWLIST_WRLOCK(&storage_engines)) {
+		ast_log(LOG_WARNING, "Unable to lock storage engine list\n");
+		return -1;
+	}
+	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&storage_engines, tmp, list) {
 		if (!strcasecmp(name, tmp->name)) {
-			AST_LIST_REMOVE_CURRENT(&storage_engines, list);
+			AST_RWLIST_REMOVE_CURRENT(&storage_engines, list);
 			free(tmp);
 			res = 0;
 		}
 	}
-	AST_LIST_TRAVERSE_SAFE_END
-	AST_LIST_UNLOCK(&storage_engines);
+	AST_RWLIST_TRAVERSE_SAFE_END
+	AST_RWLIST_UNLOCK(&storage_engines);
 
 	if (!res) {
 		if (option_verbose > 1)
@@ -99,7 +99,7 @@
 {
 	struct ast_storage_be *e;
 
-	AST_LIST_TRAVERSE(&storage_engines, e, list) {
+	AST_RWLIST_TRAVERSE(&storage_engines, e, list) {
 		if (!strcasecmp(e->name, storage_type))
 			return e->new(uri);
 	}
@@ -163,6 +163,22 @@
 	return count;
 }
 
+struct ast_storage_be *ast_storage_getbyname(const char *name)
+{
+	struct ast_storage_be *e;
+	if (AST_RWLIST_RDLOCK(&storage_engines)) {
+		ast_log(LOG_WARNING, "Unable to lock storage engine list\n");
+		return NULL;
+	}
+
+	AST_RWLIST_TRAVERSE(&storage_engines, e, list) {
+		if (!strcasecmp(name, e->name))
+			break;
+	}
+	AST_RWLIST_UNLOCK(&storage_engines);
+	return e;
+}
+
 static int show_storage_engines(int fd, int argc, char *argv[])
 {
 	struct ast_storage_be *e;
@@ -172,33 +188,90 @@
 		return RESULT_SHOWUSAGE;
 	ast_cli(fd, "Storage Engine\n");
 
-	if (AST_LIST_LOCK(&storage_engines)) {
-		ast_log(LOG_WARNING, "Unable to lock storage engine list\n");
-		return -1;
-	}
-
-	AST_LIST_TRAVERSE(&storage_engines, e, list) {
+	if (AST_RWLIST_RDLOCK(&storage_engines)) {
+		ast_log(LOG_WARNING, "Unable to lock storage engine list\n");
+		return RESULT_FAILURE;
+	}
+
+	AST_RWLIST_TRAVERSE(&storage_engines, e, list) {
 		ast_cli(fd, "%s\n", e->name);
 		count_se++;
 	}
-	AST_LIST_UNLOCK(&storage_engines);
-	ast_cli(fd, "%d storage engines registered.\n", count_se);
+	AST_RWLIST_UNLOCK(&storage_engines);
+	ast_cli(fd, "%d storage engine%s registered.\n", count_se, count_se == 1 ? "" : "s");
 	return RESULT_SUCCESS;
+}
+
+static int show_storage_engine(int fd, int argc, char *argv[])
+{
+	struct ast_storage_be *e;
+
+	if (argc != 5)
+		return RESULT_SHOWUSAGE;
+
+	if ((e = ast_storage_getbyname(argv[4]))) {
+		ast_cli(fd, "Storage Engine: %s\n", e->name);
+		ast_cli(fd, "Can get       : %s\n", e->get ? "yes" : "no");
+		ast_cli(fd, "Can put       : %s\n", e->put ? "yes" : "no");
+		ast_cli(fd, "Can delete    : %s\n", e->delete ? "yes" : "no");
+		ast_cli(fd, "Can opendir   : %s\n", e->opendir ? "yes" : "no");
+		ast_cli(fd, "Can readdir   : %s\n", e->readdir ? "yes" : "no");
+		ast_cli(fd, "Can closedir  : %s\n", e->closedir ? "yes" : "no");
+		if (e->status) {
+			ast_cli(fd, "\n");
+			ast_cli(fd, "Additional status\n");
+			e->status((struct ast_storage *)e, fd);
+		}
+	} else {
+		ast_cli(fd, "No storage engine named '%s' found.\n", argv[4]);
+	}
+	return RESULT_SUCCESS;
+}
+
+static char *complete_show_storage_engines(const char *line, const char *word, int pos, int state)
+{
+	struct ast_storage_be *e;
+	char *ret = NULL;
+	int which = 0;
+	int wordlen = strlen(word);
+
+	AST_RWLIST_RDLOCK(&storage_engines);
+	AST_RWLIST_TRAVERSE(&storage_engines, e, list) {
+		if (!strncasecmp(word, e->name, wordlen) && ++which > state) {
+			ret = strdup(e->name);
+			break;
+		}
+	}
+	AST_RWLIST_UNLOCK(&storage_engines);
+
+	return ret;
 }
 
 char show_storage_engines_usage[] = 
 "Usage: core show storage engines\n"
 "       Displays currently registered storage engines (if any)\n";
+
+char show_storage_engine_usage[] =
+"Usage: core show storage engine <engine>\n"
+"       Displays capabilities of requested storage engine\n";
 
 struct ast_cli_entry cli_storage[] = {
 	{ { "core", "show", "storage", "engines" },
 	show_storage_engines, "Displays storage engines",
 	show_storage_engines_usage },
+
+	{ { "core", "show", "storage", "engine", NULL },
+	show_storage_engine, "Displays storage engine capabilities",
+	show_storage_engine_usage, complete_show_storage_engines },
 };
 
+const struct ast_storage_be null_se = {
+	.name = "null",
+};
+
 int ast_storage_engine_init(void)
 {
+	__ast_register_storage(&null_se, NULL);
 	ast_cli_register_multiple(cli_storage, sizeof(cli_storage) / sizeof(struct ast_cli_entry));
 	return 0;
 }
-

Modified: team/group/ast_storage/res/res_storage_file.c
URL: http://svn.digium.com/view/asterisk/team/group/ast_storage/res/res_storage_file.c?view=diff&rev=66605&r1=66604&r2=66605
==============================================================================
--- team/group/ast_storage/res/res_storage_file.c (original)
+++ team/group/ast_storage/res/res_storage_file.c Wed May 30 11:16:40 2007
@@ -50,7 +50,7 @@
 
 static struct ast_storage *se_new_file(const char *uri)
 {
-	struct ast_storage_file *fst = malloc(sizeof(struct ast_storage_file));
+	struct ast_storage_file *fst = ast_calloc(1, sizeof(struct ast_storage_file));
 	ast_copy_string(fst->rootpath, uri + 6, sizeof(fst->rootpath));
 	fst->be = &file_se;
 	return ((struct ast_storage *)fst);
@@ -154,7 +154,7 @@
 static void *se_opendir_file(struct ast_storage *st, const char *objectpath)
 {
 	struct ast_storage_file *fst = (struct ast_storage_file *)st;
-	struct file_storage_dir *dir = malloc(sizeof *dir);
+	struct file_storage_dir *dir = ast_calloc(1, sizeof(*dir));
 
 	if (objectpath[0] == '/')
 		ast_copy_string(dir->path, objectpath, sizeof(dir->path));

Modified: team/group/ast_storage/res/res_storage_odbc.c
URL: http://svn.digium.com/view/asterisk/team/group/ast_storage/res/res_storage_odbc.c?view=diff&rev=66605&r1=66604&r2=66605
==============================================================================
--- team/group/ast_storage/res/res_storage_odbc.c (original)
+++ team/group/ast_storage/res/res_storage_odbc.c Wed May 30 11:16:40 2007
@@ -37,6 +37,7 @@
 #include "asterisk/res_odbc.h"
 #include "asterisk/app.h"
 #include "asterisk/config.h"
+#include "asterisk/cli.h"
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -579,6 +580,13 @@
 	return -1;
 }
 
+static int se_status_odbc(struct ast_storage *st, int fd)
+{
+	struct ast_storage_odbc *ost = (struct ast_storage_odbc *)st;
+	ast_cli(fd, "Connected: %s\n", (ost->conn && ost->conn->up) ? "yes" : "no");
+	return 0;
+}
+
 static const struct ast_storage_be odbc_se = {
 	.name = "odbc",
 	.new = se_new_odbc,
@@ -589,6 +597,7 @@
 	.opendir = se_opendir_odbc,
 	.readdir = se_readdir_odbc,
 	.closedir = se_closedir_odbc,
+	.status = se_status_odbc,
 };
 
 static int load_module(void)



More information about the svn-commits mailing list