2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012 - 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Sorcery Data Access Layer API
23 * \author Joshua Colp <jcolp@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/logger.h"
35 #include "asterisk/sorcery.h"
36 #include "asterisk/astobj2.h"
37 #include "asterisk/strings.h"
38 #include "asterisk/config_options.h"
39 #include "asterisk/netsock2.h"
40 #include "asterisk/module.h"
42 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
46 /*! \brief Number of buckets for wizards (should be prime for performance reasons) */
47 #define WIZARD_BUCKETS 7
49 /*! \brief Number of buckets for types (should be prime for performance reasons) */
50 #define TYPE_BUCKETS 53
52 /*! \brief Maximum length of an object field name */
53 #define MAX_OBJECT_FIELD 128
55 /*! \brief Structure for registered object type */
56 struct ast_sorcery_object_type {
57 /*! \brief Unique name of the object type */
58 char name[MAX_OBJECT_TYPE];
60 /*! \brief Optional transformation callback */
61 sorcery_transform_handler transform;
63 /*! \brief Optional object set apply callback */
64 sorcery_apply_handler apply;
66 /*! \brief Optional object copy callback */
67 sorcery_copy_handler copy;
69 /*! \brief Optional object diff callback */
70 sorcery_diff_handler diff;
72 /*! \brief Wizard instances */
73 struct ao2_container *wizards;
75 /*! \brief Object fields */
76 struct ao2_container *fields;
78 /*! \brief Configuration framework general information */
79 struct aco_info *info;
81 /*! \brief Configuration framework file information */
82 struct aco_file *file;
84 /*! \brief Type details */
88 /*! \brief Structure for registered object field */
89 struct ast_sorcery_object_field {
90 /*! \brief Name of the field */
91 char name[MAX_OBJECT_FIELD];
93 /*! \brief Callback function for translation of a single value */
94 sorcery_field_handler handler;
96 /*! \brief Callback function for translation of multiple values */
97 sorcery_fields_handler multiple_handler;
99 /*! \brief Position of the field */
103 /*! \brief Structure for a wizard instance which operates on objects */
104 struct ast_sorcery_object_wizard {
105 /*! \brief Wizard interface itself */
106 struct ast_sorcery_wizard *wizard;
108 /*! \brief Unique data for the wizard */
111 /*! \brief Wizard is acting as an object cache */
112 unsigned int caching:1;
115 /*! \brief Full structure for sorcery */
117 /*! \brief Container for known object types */
118 struct ao2_container *types;
121 /*! \brief Structure for passing load/reload details */
122 struct sorcery_load_details {
123 /*! \brief Sorcery structure in use */
124 const struct ast_sorcery *sorcery;
126 /*! \brief Type of object being loaded */
129 /*! \brief Whether this is a reload or not */
130 unsigned int reload:1;
133 /*! \brief Registered sorcery wizards */
134 struct ao2_container *wizards;
136 static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
138 int *field = (int *)(obj + args[0]);
139 return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
142 static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
144 unsigned int *field = (unsigned int *)(obj + args[0]);
145 return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
148 static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
150 double *field = (double *)(obj + args[0]);
151 return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
154 static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
156 ast_string_field *field = (const char **)(obj + args[0]);
157 return !(*buf = ast_strdup(*field)) ? -1 : 0;
160 static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
162 unsigned int *field = (unsigned int *)(obj + args[0]);
163 return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
166 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
168 struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
169 return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
172 static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
174 char *field = (char *)(obj + args[0]);
175 return !(*buf = ast_strdup(field)) ? -1 : 0;
178 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
181 case OPT_BOOL_T: return bool_handler_fn;
182 case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
183 case OPT_DOUBLE_T: return double_handler_fn;
184 case OPT_INT_T: return int_handler_fn;
185 case OPT_SOCKADDR_T: return sockaddr_handler_fn;
186 case OPT_STRINGFIELD_T: return stringfield_handler_fn;
187 case OPT_UINT_T: return uint_handler_fn;
190 case OPT_CUSTOM_T: return NULL;
196 /*! \brief Hashing function for sorcery wizards */
197 static int sorcery_wizard_hash(const void *obj, const int flags)
199 const struct ast_sorcery_wizard *wizard = obj;
200 const char *name = obj;
202 return ast_str_hash(flags & OBJ_KEY ? name : wizard->name);
205 /*! \brief Comparator function for sorcery wizards */
206 static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
208 struct ast_sorcery_wizard *wizard1 = obj, *wizard2 = arg;
209 const char *name = arg;
211 return !strcmp(wizard1->name, flags & OBJ_KEY ? name : wizard2->name) ? CMP_MATCH | CMP_STOP : 0;
214 int ast_sorcery_init(void)
216 ast_assert(wizards == NULL);
218 if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
225 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
227 struct ast_sorcery_wizard *wizard;
230 ast_assert(!ast_strlen_zero(interface->name));
234 if ((wizard = ao2_find(wizards, interface->name, OBJ_KEY | OBJ_NOLOCK))) {
235 ast_log(LOG_WARNING, "Attempted to register sorcery wizard '%s' twice\n",
240 if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
244 *wizard = *interface;
245 wizard->module = module;
247 ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
250 ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
259 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
261 RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
264 ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
271 /*! \brief Destructor called when sorcery structure is destroyed */
272 static void sorcery_destructor(void *obj)
274 struct ast_sorcery *sorcery = obj;
276 ao2_cleanup(sorcery->types);
279 /*! \brief Hashing function for sorcery types */
280 static int sorcery_type_hash(const void *obj, const int flags)
282 const struct ast_sorcery_object_type *type = obj;
283 const char *name = obj;
285 return ast_str_hash(flags & OBJ_KEY ? name : type->name);
288 /*! \brief Comparator function for sorcery types */
289 static int sorcery_type_cmp(void *obj, void *arg, int flags)
291 struct ast_sorcery_object_type *type1 = obj, *type2 = arg;
292 const char *name = arg;
294 return !strcmp(type1->name, flags & OBJ_KEY ? name : type2->name) ? CMP_MATCH | CMP_STOP : 0;
297 struct ast_sorcery *ast_sorcery_open(void)
299 struct ast_sorcery *sorcery;
301 if (!(sorcery = ao2_alloc(sizeof(*sorcery), sorcery_destructor))) {
305 if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
306 ao2_ref(sorcery, -1);
313 /*! \brief Destructor function for object types */
314 static void sorcery_object_type_destructor(void *obj)
316 struct ast_sorcery_object_type *object_type = obj;
318 ao2_cleanup(object_type->wizards);
319 ao2_cleanup(object_type->fields);
321 if (object_type->info) {
322 aco_info_destroy(object_type->info);
323 ast_free(object_type->info);
326 ast_free(object_type->file);
329 /*! \brief Internal function which allocates an object type structure */
330 static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
332 struct ast_sorcery_object_type *object_type;
334 if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
338 /* Order matters for object wizards */
339 if (!(object_type->wizards = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
340 ao2_ref(object_type, -1);
344 if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
345 ao2_ref(object_type, -1);
349 if (!(object_type->info = ast_calloc(1, sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0])))) {
350 ao2_ref(object_type, -1);
354 if (!(object_type->file = ast_calloc(1, sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0])))) {
355 ao2_ref(object_type, -1);
359 object_type->info->files[0] = object_type->file;
360 object_type->info->files[1] = NULL;
361 object_type->info->module = module;
363 ast_copy_string(object_type->name, type, sizeof(object_type->name));
368 /*! \brief Object wizard destructor */
369 static void sorcery_object_wizard_destructor(void *obj)
371 struct ast_sorcery_object_wizard *object_wizard = obj;
373 if (object_wizard->data) {
374 object_wizard->wizard->close(object_wizard->data);
377 if (object_wizard->wizard) {
378 ast_module_unref(object_wizard->wizard->module);
382 /*! \brief Internal function which creates an object type and adds a wizard mapping */
383 static int sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data, unsigned int caching)
385 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
386 RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
387 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
390 if (!wizard || !object_wizard) {
395 if (!(object_type = sorcery_object_type_alloc(type, module))) {
401 if (wizard->open && !(object_wizard->data = wizard->open(data))) {
405 ast_module_ref(wizard->module);
407 object_wizard->wizard = wizard;
408 object_wizard->caching = caching;
410 ao2_link(object_type->wizards, object_wizard);
413 ao2_link(sorcery->types, object_type);
419 int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
421 struct ast_flags flags = { 0 };
422 struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
423 struct ast_variable *mapping;
426 if (!config || (config == CONFIG_STATUS_FILEMISSING) || (config == CONFIG_STATUS_FILEINVALID)) {
430 for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
431 RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
432 RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
433 char *options = mapping_name, *name = strsep(&options, "/");
434 char *data = mapping_value, *wizard = strsep(&data, ",");
435 unsigned int caching = 0;
437 /* If no wizard exists just skip, nothing we can do */
438 if (ast_strlen_zero(wizard)) {
442 /* If the wizard is configured as a cache treat it as such */
443 if (!ast_strlen_zero(options) && strstr(options, "cache")) {
447 /* Any error immediately causes us to stop */
448 if ((res = sorcery_apply_wizard_mapping(sorcery, name, module, wizard, data, caching))) {
453 ast_config_destroy(config);
458 int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
460 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
462 /* Defaults can not be added if any existing mapping exists */
467 return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
470 int ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, aco_type_item_alloc alloc, sorcery_transform_handler transform, sorcery_apply_handler apply)
472 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
474 if (!object_type || object_type->type.item_alloc) {
478 object_type->type.name = object_type->name;
479 object_type->type.type = ACO_ITEM;
480 object_type->type.category = "";
481 object_type->type.item_alloc = alloc;
483 object_type->transform = transform;
484 object_type->apply = apply;
485 object_type->file->types[0] = &object_type->type;
486 object_type->file->types[1] = NULL;
488 if (aco_info_init(object_type->info)) {
495 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
497 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
503 object_type->copy = copy;
506 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
508 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
514 object_type->diff = diff;
517 int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *type, const char *regex, aco_option_handler config_handler, sorcery_fields_handler sorcery_handler)
519 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
520 RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
522 if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
526 ast_copy_string(object_field->name, regex, sizeof(object_field->name));
527 object_field->multiple_handler = sorcery_handler;
529 ao2_link(object_type->fields, object_field);
530 __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 0);
535 int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type, const char *name, const char *default_val, enum aco_option_type opt_type,
536 aco_option_handler config_handler, sorcery_field_handler sorcery_handler, unsigned int flags, size_t argc, ...)
538 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
539 RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
543 if (!object_type || !object_type->type.item_alloc) {
547 if (!sorcery_handler) {
548 sorcery_handler = sorcery_field_default_handler(opt_type);
551 if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
555 ast_copy_string(object_field->name, name, sizeof(object_field->name));
556 object_field->handler = sorcery_handler;
558 va_start(args, argc);
559 for (pos = 0; pos < argc; pos++) {
560 object_field->args[pos] = va_arg(args, size_t);
564 ao2_link(object_type->fields, object_field);
566 /* TODO: Improve this hack */
568 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, argc);
569 } else if (argc == 1) {
570 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, argc,
571 object_field->args[0]);
572 } else if (argc == 2) {
573 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, argc,
574 object_field->args[0], object_field->args[1]);
575 } else if (argc == 3) {
576 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, argc,
577 object_field->args[0], object_field->args[1], object_field->args[2]);
579 ast_assert(0); /* The hack... she does us no good for this */
585 static int sorcery_wizard_load(void *obj, void *arg, int flags)
587 struct ast_sorcery_object_wizard *wizard = obj;
588 struct sorcery_load_details *details = arg;
589 void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
591 load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
594 load(wizard->data, details->sorcery, details->type);
600 static int sorcery_object_load(void *obj, void *arg, int flags)
602 struct ast_sorcery_object_type *type = obj;
603 struct sorcery_load_details *details = arg;
605 details->type = type->name;
606 ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
611 void ast_sorcery_load(const struct ast_sorcery *sorcery)
613 struct sorcery_load_details details = {
618 ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
621 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
623 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
624 struct sorcery_load_details details = {
633 sorcery_object_load(object_type, &details, 0);
636 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
638 struct sorcery_load_details details = {
643 ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
646 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
648 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
649 struct sorcery_load_details details = {
658 sorcery_object_load(object_type, &details, 0);
661 void ast_sorcery_ref(struct ast_sorcery *sorcery)
663 ao2_ref(sorcery, +1);
666 struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object)
668 const struct ast_sorcery_object_details *details = object;
669 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
670 struct ao2_iterator i;
671 struct ast_sorcery_object_field *object_field;
672 struct ast_variable *fields = NULL;
679 i = ao2_iterator_init(object_type->fields, 0);
681 for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
682 struct ast_variable *tmp = NULL;
684 if (object_field->multiple_handler) {
685 if ((res = object_field->multiple_handler(object, &tmp))) {
686 ast_variables_destroy(tmp);
688 } else if (object_field->handler) {
691 if ((res = object_field->handler(object, object_field->args, &buf)) ||
692 !(tmp = ast_variable_new(object_field->name, S_OR(buf, ""), ""))) {
707 ao2_iterator_destroy(&i);
709 /* If any error occurs we destroy all fields handled before so a partial objectset is not returned */
711 ast_variables_destroy(fields);
718 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
720 const struct ast_sorcery_object_details *details = object;
721 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
722 RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
723 struct ast_variable *field;
730 if (object_type->transform && (transformed = object_type->transform(objectset))) {
736 for (; field; field = field->next) {
737 if ((res = aco_process_var(&object_type->type, details->id, field, object))) {
742 if (!res && object_type->apply) {
743 object_type->apply(sorcery, object);
749 static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
751 const struct ast_variable *field;
753 /* Search the linked list of fields to find the correct one */
754 for (field = fields; field; field = field->next) {
755 if (!strcmp(field->name, name)) {
763 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
765 const struct ast_variable *field;
770 /* Unless the ast_variable list changes when examined... it can't differ from itself */
771 if (original == modified) {
775 for (field = modified; field; field = field->next) {
776 const struct ast_variable *old = sorcery_find_field(original, field->name);
778 if (!old || strcmp(old->value, field->value)) {
779 struct ast_variable *tmp;
781 if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
786 tmp->next = *changes;
791 /* If an error occurred do not return a partial changeset */
793 ast_variables_destroy(*changes);
800 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
802 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
803 struct ast_sorcery_object_details *details;
805 if (!object_type || !object_type->type.item_alloc ||
806 !(details = object_type->type.item_alloc(""))) {
810 if (ast_strlen_zero(id)) {
811 ast_uuid_generate_str(details->id, sizeof(details->id));
813 ast_copy_string(details->id, id, sizeof(details->id));
816 ast_copy_string(details->type, type, sizeof(details->type));
818 if (aco_set_defaults(&object_type->type, id, details)) {
819 ao2_ref(details, -1);
826 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
828 const struct ast_sorcery_object_details *details = object;
829 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
830 struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->type, details->id);
831 RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
836 } else if (object_type->copy) {
837 res = object_type->copy(object, copy);
838 } else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
839 res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
841 /* No native copy available and could not create an objectset, this copy has failed */
853 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
855 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
859 if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
863 if (original == modified) {
865 } else if (!object_type->diff) {
866 RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
867 RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
869 objectset1 = ast_sorcery_objectset_create(sorcery, original);
870 objectset2 = ast_sorcery_objectset_create(sorcery, modified);
872 return ast_sorcery_changeset_create(objectset1, objectset2, changes);
874 return object_type->diff(original, modified, changes);
878 /*! \brief Structure used when calling create, update, or delete */
879 struct sorcery_details {
880 /*! \brief Pointer to the sorcery instance */
881 const struct ast_sorcery *sorcery;
882 /*! \brief Pointer to the object itself */
886 /*! \brief Internal function used to create an object in caching wizards */
887 static int sorcery_cache_create(void *obj, void *arg, int flags)
889 const struct ast_sorcery_object_wizard *object_wizard = obj;
890 const struct sorcery_details *details = arg;
892 if (!object_wizard->caching || !object_wizard->wizard->create) {
896 object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
901 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
903 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
905 struct ao2_iterator i;
906 struct ast_sorcery_object_wizard *wizard;
907 unsigned int cached = 0;
909 if (!object_type || ast_strlen_zero(id)) {
913 i = ao2_iterator_init(object_type->wizards, 0);
914 for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
915 if (wizard->wizard->retrieve_id &&
916 !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
920 cached = wizard->caching;
925 ao2_iterator_destroy(&i);
927 if (!cached && object) {
928 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
934 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
936 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
938 struct ao2_iterator i;
939 struct ast_sorcery_object_wizard *wizard;
940 unsigned int cached = 0;
946 /* If returning multiple objects create a container to store them in */
947 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
948 if (!(object = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
953 /* Inquire with the available wizards for retrieval */
954 i = ao2_iterator_init(object_type->wizards, 0);
955 for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
956 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
957 if (wizard->wizard->retrieve_multiple) {
958 wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
960 } else if (fields && wizard->wizard->retrieve_fields) {
961 if (wizard->wizard->retrieve_fields) {
962 object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
966 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
970 cached = wizard->caching;
975 ao2_iterator_destroy(&i);
977 /* If we are returning a single object and it came from a non-cache source create it in any caches */
978 if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
979 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
985 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
987 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
988 struct ao2_container *objects;
989 struct ao2_iterator i;
990 struct ast_sorcery_object_wizard *wizard;
992 if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
996 i = ao2_iterator_init(object_type->wizards, 0);
997 for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
998 if (!wizard->wizard->retrieve_regex) {
1002 wizard->wizard->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
1004 ao2_iterator_destroy(&i);
1009 /*! \brief Internal function which returns if the wizard has created the object */
1010 static int sorcery_wizard_create(void *obj, void *arg, int flags)
1012 const struct ast_sorcery_object_wizard *object_wizard = obj;
1013 const struct sorcery_details *details = arg;
1015 return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
1018 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
1020 const struct ast_sorcery_object_details *details = object;
1021 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
1022 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1023 struct sorcery_details sdetails = {
1032 object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails);
1034 return object_wizard ? 0 : -1;
1037 /*! \brief Internal function which returns if a wizard has updated the object */
1038 static int sorcery_wizard_update(void *obj, void *arg, int flags)
1040 const struct ast_sorcery_object_wizard *object_wizard = obj;
1041 const struct sorcery_details *details = arg;
1043 return (object_wizard->wizard->update && !object_wizard->wizard->update(details->sorcery, object_wizard->data, details->obj) &&
1044 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1047 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
1049 const struct ast_sorcery_object_details *details = object;
1050 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
1051 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1052 struct sorcery_details sdetails = {
1061 object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails);
1063 return object_wizard ? 0 : -1;
1066 /*! \brief Internal function which returns if a wizard has deleted the object */
1067 static int sorcery_wizard_delete(void *obj, void *arg, int flags)
1069 const struct ast_sorcery_object_wizard *object_wizard = obj;
1070 const struct sorcery_details *details = arg;
1072 return (object_wizard->wizard->delete && !object_wizard->wizard->delete(details->sorcery, object_wizard->data, details->obj) &&
1073 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1076 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
1078 const struct ast_sorcery_object_details *details = object;
1079 RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->type, OBJ_KEY), ao2_cleanup);
1080 RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1081 struct sorcery_details sdetails = {
1090 object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails);
1092 return object_wizard ? 0 : -1;
1095 void ast_sorcery_unref(struct ast_sorcery *sorcery)
1097 ao2_cleanup(sorcery);
1100 const char *ast_sorcery_object_get_id(const void *object)
1102 const struct ast_sorcery_object_details *details = object;
1106 const char *ast_sorcery_object_get_type(const void *object)
1108 const struct ast_sorcery_object_details *details = object;
1109 return details->type;