dict.c revision 10399559650f552a23949772be79eb6a80198c5a
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2005-2012 Dovecot authors, see the included COPYING file */
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "lib.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "array.h"
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen#include "str.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "dict-sql.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "dict-private.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainenstatic ARRAY(struct dict *) dict_drivers;
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainenstatic struct dict_iterate_context dict_iter_unsupported;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenstatic struct dict *dict_driver_lookup(const char *name)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct dict *const *dicts;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen array_foreach(&dict_drivers, dicts) {
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen struct dict *dict = *dicts;
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen if (strcmp(dict->name, name) == 0)
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen return dict;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return NULL;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenvoid dict_driver_register(struct dict *driver)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen if (!array_is_created(&dict_drivers))
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen i_array_init(&dict_drivers, 8);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen if (dict_driver_lookup(driver->name) != NULL) {
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen i_fatal("dict_driver_register(%s): Already registered",
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen driver->name);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen array_append(&dict_drivers, &driver, 1);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenvoid dict_driver_unregister(struct dict *driver)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct dict *const *dicts;
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen unsigned int idx = -1U;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen array_foreach(&dict_drivers, dicts) {
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen if (*dicts == driver) {
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen idx = array_foreach_idx(&dict_drivers, dicts);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen break;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen i_assert(idx != -1U);
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen array_delete(&dict_drivers, idx, 1);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen if (array_count(&dict_drivers) == 0)
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen array_free(&dict_drivers);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainenvoid dict_drivers_register_builtin(void)
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen{
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen dict_driver_register(&dict_driver_client);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen dict_driver_register(&dict_driver_file);
fefd62f9aaccecbc6011ecae359a3389668cbff5Timo Sirainen dict_driver_register(&dict_driver_memcached);
75bb83681e30d6a86109bbafdfe6b513c11124bcTimo Sirainen dict_driver_register(&dict_driver_memcached_ascii);
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen dict_driver_register(&dict_driver_redis);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen}
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainenvoid dict_drivers_unregister_builtin(void)
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen{
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen dict_driver_unregister(&dict_driver_client);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen dict_driver_unregister(&dict_driver_file);
fefd62f9aaccecbc6011ecae359a3389668cbff5Timo Sirainen dict_driver_unregister(&dict_driver_memcached);
75bb83681e30d6a86109bbafdfe6b513c11124bcTimo Sirainen dict_driver_unregister(&dict_driver_memcached_ascii);
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen dict_driver_unregister(&dict_driver_redis);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen}
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen
10399559650f552a23949772be79eb6a80198c5aTimo Sirainenint dict_init(const char *uri, enum dict_data_type value_type,
10399559650f552a23949772be79eb6a80198c5aTimo Sirainen const char *username, const char *base_dir, struct dict **dict_r)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct dict *dict;
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen const char *p, *name;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
080a75584cfbe21ffd2d23c6bbb4cd8fdfd0990cTimo Sirainen i_assert(username != NULL);
080a75584cfbe21ffd2d23c6bbb4cd8fdfd0990cTimo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen p = strchr(uri, ':');
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (p == NULL) {
14cdb8cbe37616464c965aec90a8494b339db538Timo Sirainen i_error("Dictionary URI is missing ':': %s", uri);
10399559650f552a23949772be79eb6a80198c5aTimo Sirainen return -1;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen T_BEGIN {
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen name = t_strdup_until(uri, p);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen dict = dict_driver_lookup(name);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen if (dict == NULL)
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen i_error("Unknown dict module: %s", name);
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen } T_END;
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen
10399559650f552a23949772be79eb6a80198c5aTimo Sirainen if (dict == NULL)
10399559650f552a23949772be79eb6a80198c5aTimo Sirainen return -1;
10399559650f552a23949772be79eb6a80198c5aTimo Sirainen return dict->v.init(dict, p+1, value_type, username, base_dir, dict_r);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenvoid dict_deinit(struct dict **_dict)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen struct dict *dict = *_dict;
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen *_dict = NULL;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen dict->v.deinit(dict);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainenint dict_wait(struct dict *dict)
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen{
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen return dict->v.wait == NULL ? 1 : dict->v.wait(dict);
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen}
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainenstatic bool dict_key_prefix_is_valid(const char *key)
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen{
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;
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen}
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainenint dict_lookup(struct dict *dict, pool_t pool, const char *key,
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen const char **value_r)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen i_assert(dict_key_prefix_is_valid(key));
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen return dict->v.lookup(dict, pool, key, value_r);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstruct dict_iterate_context *
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainendict_iterate_init(struct dict *dict, const char *path,
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen enum dict_iterate_flags flags)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen const char *paths[2];
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen paths[0] = path;
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen paths[1] = NULL;
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen return dict_iterate_init_multiple(dict, paths, flags);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen}
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainenstruct dict_iterate_context *
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainendict_iterate_init_multiple(struct dict *dict, const char *const *paths,
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen enum dict_iterate_flags flags)
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen{
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen unsigned int i;
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen i_assert(paths[0] != NULL);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen for (i = 0; paths[i] != NULL; i++)
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen i_assert(dict_key_prefix_is_valid(paths[i]));
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen if (dict->v.iterate_init == NULL) {
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen /* not supported by backend */
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen i_error("%s: dict iteration not supported", dict->name);
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen return &dict_iter_unsupported;
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen }
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen return dict->v.iterate_init(dict, paths, flags);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainenbool dict_iterate(struct dict_iterate_context *ctx,
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainen const char **key_r, const char **value_r)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen return ctx == &dict_iter_unsupported ? FALSE :
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen ctx->dict->v.iterate(ctx, key_r, value_r);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainenint dict_iterate_deinit(struct dict_iterate_context **_ctx)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen struct dict_iterate_context *ctx = *_ctx;
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen *_ctx = NULL;
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen return ctx == &dict_iter_unsupported ? -1 :
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen ctx->dict->v.iterate_deinit(ctx);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstruct dict_transaction_context *dict_transaction_begin(struct dict *dict)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen return dict->v.transaction_init(dict);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainenint dict_transaction_commit(struct dict_transaction_context **_ctx)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen struct dict_transaction_context *ctx = *_ctx;
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen *_ctx = NULL;
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen return ctx->dict->v.transaction_commit(ctx, FALSE, NULL, NULL);
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen}
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainenvoid dict_transaction_commit_async(struct dict_transaction_context **_ctx,
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen dict_transaction_commit_callback_t *callback,
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen void *context)
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen{
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen struct dict_transaction_context *ctx = *_ctx;
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen *_ctx = NULL;
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen ctx->dict->v.transaction_commit(ctx, TRUE, callback, context);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainenvoid dict_transaction_rollback(struct dict_transaction_context **_ctx)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen struct dict_transaction_context *ctx = *_ctx;
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen *_ctx = NULL;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen ctx->dict->v.transaction_rollback(ctx);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_set(struct dict_transaction_context *ctx,
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen const char *key, const char *value)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen i_assert(dict_key_prefix_is_valid(key));
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen ctx->dict->v.set(ctx, key, value);
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo Sirainen ctx->changed = TRUE;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainenvoid dict_unset(struct dict_transaction_context *ctx,
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen const char *key)
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen{
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen i_assert(dict_key_prefix_is_valid(key));
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen ctx->dict->v.unset(ctx, key);
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen ctx->changed = TRUE;
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen}
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainenvoid dict_append(struct dict_transaction_context *ctx,
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen const char *key, const char *value)
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen{
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen i_assert(dict_key_prefix_is_valid(key));
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen ctx->dict->v.append(ctx, key, value);
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen ctx->changed = TRUE;
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen}
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid dict_atomic_inc(struct dict_transaction_context *ctx,
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen const char *key, long long diff)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen i_assert(dict_key_prefix_is_valid(key));
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo Sirainen if (diff != 0) {
52de9d38fcad80df481667bac821cb8222785fe6Timo Sirainen ctx->dict->v.atomic_inc(ctx, key, diff);
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo Sirainen ctx->changed = TRUE;
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo Sirainen }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainenconst char *dict_escape_string(const char *str)
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen{
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen const char *p;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen string_t *ret;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* see if we need to escape it */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen for (p = str; *p != '\0'; p++) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '/' || *p == '\\')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen break;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen }
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '\0')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen return str;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* escape */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen ret = t_str_new((size_t) (p - str) + 128);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_n(ret, str, (size_t) (p - str));
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen for (; *p != '\0'; p++) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen switch (*p) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen case '/':
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_c(ret, '\\');
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_c(ret, '|');
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen break;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen case '\\':
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_c(ret, '\\');
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_c(ret, '\\');
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen break;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen default:
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_c(ret, *p);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen break;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen }
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen }
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen return str_c(ret);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen}
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainenconst char *dict_unescape_string(const char *str)
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen{
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen const char *p;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen string_t *ret;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* see if we need to unescape it */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen for (p = str; *p != '\0'; p++) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '\\')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen break;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen }
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p == '\0')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen return str;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen /* unescape */
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen ret = t_str_new((size_t) (p - str) + strlen(p) + 1);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_n(ret, str, (size_t) (p - str));
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen for (; *p != '\0'; p++) {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*p != '\\')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_c(ret, *p);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen else {
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen if (*++p == '|')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_c(ret, '/');
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen else if (*p == '\0')
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen break;
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen else
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen str_append_c(ret, *p);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen }
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen }
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen return str_c(ret);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen}