asterisk/res/res_sorcery_memory.c

243 lines
7.2 KiB
C
Raw Normal View History

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2012 - 2013, Digium, Inc.
*
* Joshua Colp <jcolp@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
*
* \brief Sorcery In-Memory Object Wizard
*
* \author Joshua Colp <jcolp@digium.com>
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
git migration: Refactor the ASTERISK_FILE_VERSION macro Git does not support the ability to replace a token with a version string during check-in. While it does have support for replacing a token on clone, this is somewhat sub-optimal: the token is replaced with the object hash, which is not particularly easy for human consumption. What's more, in practice, the source file version was often not terribly useful. Generally, when triaging bugs, the overall version of Asterisk is far more useful than an individual SVN version of a file. As a result, this patch removes Asterisk's support for showing source file versions. Specifically, it does the following: * Rename ASTERISK_FILE_VERSION macro to ASTERISK_REGISTER_FILE, and remove passing the version in with the macro. Other facilities than 'core show file version' make use of the file names, such as setting a debug level only on a specific file. As such, the act of registering source files with the Asterisk core still has use. The macro rename now reflects the new macro purpose. * main/asterisk: - Refactor the file_version structure to reflect that it no longer tracks a version field. - Remove the "core show file version" CLI command. Without the file version, it is no longer useful. - Remove the ast_file_version_find function. The file version is no longer tracked. - Rename ast_register_file_version/ast_unregister_file_version to ast_register_file/ast_unregister_file, respectively. * main/manager: Remove value from the Version key of the ModuleCheck Action. The actual key itself has not been removed, as doing so would absolutely constitute a backwards incompatible change. However, since the file version is no longer tracked, there is no need to attempt to include it in the Version key. * UPGRADE: Add notes for: - Modification to the ModuleCheck AMI Action - Removal of the "core show file version" CLI command Change-Id: I6cf0ff280e1668bf4957dc21f32a5ff43444a40e
2015-04-12 02:38:22 +00:00
ASTERISK_REGISTER_FILE()
#include <regex.h>
#include "asterisk/module.h"
#include "asterisk/sorcery.h"
#include "asterisk/astobj2.h"
/*! \brief Number of buckets for sorcery objects */
#define OBJECT_BUCKETS 53
static void *sorcery_memory_open(const char *data);
static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object);
static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
const struct ast_variable *fields);
static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object);
static int sorcery_memory_delete(const struct ast_sorcery *sorcery, void *data, void *object);
static void sorcery_memory_close(void *data);
static struct ast_sorcery_wizard memory_object_wizard = {
.name = "memory",
.open = sorcery_memory_open,
.create = sorcery_memory_create,
.retrieve_id = sorcery_memory_retrieve_id,
.retrieve_fields = sorcery_memory_retrieve_fields,
.retrieve_multiple = sorcery_memory_retrieve_multiple,
.retrieve_regex = sorcery_memory_retrieve_regex,
.update = sorcery_memory_update,
.delete = sorcery_memory_delete,
.close = sorcery_memory_close,
};
/*! \brief Structure used for fields comparison */
struct sorcery_memory_fields_cmp_params {
/*! \brief Pointer to the sorcery structure */
const struct ast_sorcery *sorcery;
/*! \brief Pointer to the fields to check */
const struct ast_variable *fields;
/*! \brief Regular expression for checking object id */
regex_t *regex;
/*! \brief Optional container to put object into */
struct ao2_container *container;
};
/*! \brief Hashing function for sorcery objects */
static int sorcery_memory_hash(const void *obj, const int flags)
{
const char *id = obj;
return ast_str_hash(flags & OBJ_KEY ? id : ast_sorcery_object_get_id(obj));
}
/*! \brief Comparator function for sorcery objects */
static int sorcery_memory_cmp(void *obj, void *arg, int flags)
{
const char *id = arg;
return !strcmp(ast_sorcery_object_get_id(obj), flags & OBJ_KEY ? id : ast_sorcery_object_get_id(arg)) ? CMP_MATCH | CMP_STOP : 0;
}
static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object)
{
ao2_link(data, object);
return 0;
}
static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
{
const struct sorcery_memory_fields_cmp_params *params = arg;
RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
RAII_VAR(struct ast_variable *, diff, NULL, ast_variables_destroy);
if (params->regex) {
/* If a regular expression has been provided see if it matches, otherwise move on */
if (!regexec(params->regex, ast_sorcery_object_get_id(obj), 0, NULL, 0)) {
ao2_link(params->container, obj);
}
return 0;
} else if (params->fields &&
(!(objset = ast_sorcery_objectset_create(params->sorcery, obj)) ||
(ast_sorcery_changeset_create(objset, params->fields, &diff)) ||
diff)) {
/* If we can't turn the object into an object set OR if differences exist between the fields
* passed in and what are present on the object they are not a match.
*/
return 0;
}
if (params->container) {
ao2_link(params->container, obj);
/* As multiple objects are being returned keep going */
return 0;
} else {
/* Immediately stop and return, we only want a single object */
return CMP_MATCH | CMP_STOP;
}
}
static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
{
struct sorcery_memory_fields_cmp_params params = {
.sorcery = sorcery,
.fields = fields,
.container = NULL,
};
/* If no fields are present return nothing, we require *something* */
if (!fields) {
return NULL;
}
return ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
}
static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
{
return ao2_find(data, id, OBJ_KEY);
}
static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
{
struct sorcery_memory_fields_cmp_params params = {
.sorcery = sorcery,
.fields = fields,
.container = objects,
};
ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
}
static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
{
regex_t expression;
struct sorcery_memory_fields_cmp_params params = {
.sorcery = sorcery,
.container = objects,
.regex = &expression,
};
if (regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
return;
}
ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
regfree(&expression);
}
static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object)
{
RAII_VAR(void *, existing, NULL, ao2_cleanup);
ao2_lock(data);
if (!(existing = ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK))) {
ao2_unlock(data);
return -1;
}
ao2_link(data, object);
ao2_unlock(data);
return 0;
}
static int sorcery_memory_delete(const struct ast_sorcery *sorcery, void *data, void *object)
{
RAII_VAR(void *, existing, ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
return existing ? 0 : -1;
}
static void *sorcery_memory_open(const char *data)
{
return ao2_container_alloc(OBJECT_BUCKETS, sorcery_memory_hash, sorcery_memory_cmp);
}
static void sorcery_memory_close(void *data)
{
ao2_ref(data, -1);
}
static int load_module(void)
{
if (ast_sorcery_wizard_register(&memory_object_wizard)) {
return AST_MODULE_LOAD_DECLINE;
}
return AST_MODULE_LOAD_SUCCESS;
}
static int unload_module(void)
{
ast_sorcery_wizard_unregister(&memory_object_wizard);
return 0;
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery In-Memory Object Wizard",
.support_level = AST_MODULE_SUPPORT_CORE,
.load = load_module,
.unload = unload_module,
.load_pri = AST_MODPRI_REALTIME_DRIVER,
);