dict.c revision 02c335c23bf5fa225a467c19f2c063fb0dc7b8c3
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2005-2016 Dovecot authors, see the included COPYING file */
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainenstatic struct dict_iterate_context dict_iter_unsupported;
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenstatic struct dict *dict_driver_lookup(const char *name)
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen if (dict_driver_lookup(driver->name) != NULL) {
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen i_fatal("dict_driver_register(%s): Already registered",
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenvoid dict_driver_unregister(struct dict *driver)
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen idx = array_foreach_idx(&dict_drivers, dicts);
10399559650f552a23949772be79eb6a80198c5aTimo Sirainenint dict_init(const char *uri, enum dict_data_type value_type,
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen const char *username, const char *base_dir, struct dict **dict_r,
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen const char **error_r)
39ea5717264668e2c7f9f7986eb821d21785f47fTimo Sirainen return dict_init_full(uri, &set, dict_r, error_r);
39ea5717264668e2c7f9f7986eb821d21785f47fTimo Sirainenint dict_init_full(const char *uri, const struct dict_settings *set,
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen *error_r = t_strdup_printf("Dictionary URI is missing ':': %s",
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen *error_r = t_strdup_printf("Unknown dict module: %s", name);
39ea5717264668e2c7f9f7986eb821d21785f47fTimo Sirainen if (dict->v.init(dict, p+1, set, dict_r, &error) < 0) {
eca38954bcf972618f6b85932a3690acbd2b673aTimo Sirainen *error_r = t_strdup_printf("dict %s: %s", name, error);
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen return dict->v.wait == NULL ? 1 : dict->v.wait(dict);
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainenstatic bool dict_key_prefix_is_valid(const char *key)
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen return strncmp(key, DICT_PATH_SHARED, strlen(DICT_PATH_SHARED)) == 0 ||
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen strncmp(key, DICT_PATH_PRIVATE, strlen(DICT_PATH_PRIVATE)) == 0;
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainenint dict_lookup(struct dict *dict, pool_t pool, const char *key,
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen const char **value_r)
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen return dict->v.lookup(dict, pool, key, value_r);
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainenvoid dict_lookup_async(struct dict *dict, const char *key,
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen dict_lookup_callback_t *callback, void *context)
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainen result.ret = dict_lookup(dict, pool_datastack_create(),
f990dde096949bd2b76aab28936211689bd6cadcTimo Sirainen dict->v.lookup_async(dict, key, callback, context);
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainendict_iterate_init(struct dict *dict, const char *path,
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen return dict_iterate_init_multiple(dict, paths, flags);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainendict_iterate_init_multiple(struct dict *dict, const char *const *paths,
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen unsigned int i;
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen /* not supported by backend */
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen i_error("%s: dict iteration not supported", dict->name);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen return dict->v.iterate_init(dict, paths, flags);
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainenbool dict_iterate(struct dict_iterate_context *ctx,
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen return ctx == &dict_iter_unsupported ? FALSE :
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainenvoid dict_iterate_set_async_callback(struct dict_iterate_context *ctx,
d694b6009574ee6e3cfaee3834cbdbcd431affb0Timo Sirainenbool dict_iterate_has_more(struct dict_iterate_context *ctx)
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainenint dict_iterate_deinit(struct dict_iterate_context **_ctx)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstruct dict_transaction_context *dict_transaction_begin(struct dict *dict)
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainenint dict_transaction_commit(struct dict_transaction_context **_ctx)
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen return ctx->dict->v.transaction_commit(ctx, FALSE, NULL, NULL);
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainenvoid dict_transaction_commit_async(struct dict_transaction_context **_ctx,
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen ctx->dict->v.transaction_commit(ctx, TRUE, callback, context);
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainenvoid dict_transaction_rollback(struct dict_transaction_context **_ctx)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_set(struct dict_transaction_context *ctx,
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainenvoid dict_unset(struct dict_transaction_context *ctx,
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen const char *key)
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainenvoid dict_append(struct dict_transaction_context *ctx,
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_atomic_inc(struct dict_transaction_context *ctx,
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainenconst char *dict_escape_string(const char *str)
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen const char *p;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* see if we need to escape it */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '\0')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen for (; *p != '\0'; p++) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen switch (*p) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainenconst char *dict_unescape_string(const char *str)
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen const char *p;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* see if we need to unescape it */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '\\')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '\0')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* unescape */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen ret = t_str_new((size_t) (p - str) + strlen(p) + 1);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen for (; *p != '\0'; p++) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p != '\\')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*++p == '|')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen else if (*p == '\0')