diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-07-23 12:45:28 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-07-23 12:45:28 +0100 |
commit | e0483657ff6cf1adc828ccce457814d61fe90a0d (patch) | |
tree | 5183e4163f40b81b3e7d5f51488d360883782154 /src/kvstorage_config.c | |
parent | 7962087e808fb824aa3af6d41d02abc92916ba1e (diff) | |
download | rspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.tar.gz rspamd-e0483657ff6cf1adc828ccce457814d61fe90a0d.zip |
Unify code style.
Diffstat (limited to 'src/kvstorage_config.c')
-rw-r--r-- | src/kvstorage_config.c | 252 |
1 files changed, 158 insertions, 94 deletions
diff --git a/src/kvstorage_config.c b/src/kvstorage_config.c index 362a47f49..a29ae2454 100644 --- a/src/kvstorage_config.c +++ b/src/kvstorage_config.c @@ -21,9 +21,9 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "cfg_xml.h" #include "kvstorage_config.h" #include "main.h" -#include "cfg_xml.h" #ifdef WITH_DB #include "kvstorage_bdb.h" #endif @@ -68,7 +68,7 @@ struct kvstorage_config_parser { static void kvstorage_config_destroy (gpointer k) { - struct kvstorage_config *kconf = k; + struct kvstorage_config *kconf = k; if (kconf->name) { g_free (kconf->name); @@ -83,12 +83,14 @@ kvstorage_config_destroy (gpointer k) /* Init kvstorage */ static void -kvstorage_init_callback (const gpointer key, const gpointer value, gpointer unused) +kvstorage_init_callback (const gpointer key, + const gpointer value, + gpointer unused) { - struct kvstorage_config *kconf = value; - struct rspamd_kv_cache *cache; - struct rspamd_kv_backend *backend = NULL; - struct rspamd_kv_expire *expire = NULL; + struct kvstorage_config *kconf = value; + struct rspamd_kv_cache *cache; + struct rspamd_kv_backend *backend = NULL; + struct rspamd_kv_expire *expire = NULL; switch (kconf->cache.type) { case KVSTORAGE_TYPE_CACHE_HASH: @@ -113,17 +115,22 @@ kvstorage_init_callback (const gpointer key, const gpointer value, gpointer unus backend = NULL; break; case KVSTORAGE_TYPE_BACKEND_FILE: - backend = rspamd_kv_file_new (kconf->backend.filename, kconf->backend.sync_ops, - FILE_STORAGE_LEVELS, kconf->backend.do_fsync, kconf->backend.do_ref); + backend = rspamd_kv_file_new (kconf->backend.filename, + kconf->backend.sync_ops, + FILE_STORAGE_LEVELS, + kconf->backend.do_fsync, + kconf->backend.do_ref); break; #ifdef WITH_DB case KVSTORAGE_TYPE_BACKEND_BDB: - backend = rspamd_kv_bdb_new (kconf->backend.filename, kconf->backend.sync_ops); + backend = rspamd_kv_bdb_new (kconf->backend.filename, + kconf->backend.sync_ops); break; #endif #ifdef WITH_SQLITE case KVSTORAGE_TYPE_BACKEND_SQLITE: - backend = rspamd_kv_sqlite_new (kconf->backend.filename, kconf->backend.sync_ops); + backend = rspamd_kv_sqlite_new (kconf->backend.filename, + kconf->backend.sync_ops); break; #endif } @@ -134,20 +141,27 @@ kvstorage_init_callback (const gpointer key, const gpointer value, gpointer unus break; } - kconf->storage = rspamd_kv_storage_new (kconf->id, kconf->name, cache, backend, expire, - kconf->cache.max_elements, kconf->cache.max_memory, kconf->cache.no_overwrite); + kconf->storage = rspamd_kv_storage_new (kconf->id, + kconf->name, + cache, + backend, + expire, + kconf->cache.max_elements, + kconf->cache.max_memory, + kconf->cache.no_overwrite); } /* XML parse callbacks */ /* Called for open tags <foo bar="baz"> */ -void kvstorage_xml_start_element (GMarkupParseContext *context, - const gchar *element_name, - const gchar **attribute_names, - const gchar **attribute_values, - gpointer user_data, - GError **error) +void +kvstorage_xml_start_element (GMarkupParseContext *context, + const gchar *element_name, + const gchar **attribute_names, + const gchar **attribute_values, + gpointer user_data, + GError **error) { - struct kvstorage_config_parser *kv_parser = user_data; + struct kvstorage_config_parser *kv_parser = user_data; switch (kv_parser->state) { case KVSTORAGE_STATE_INIT: @@ -159,10 +173,12 @@ void kvstorage_xml_start_element (GMarkupParseContext *context, if (kv_parser->pool != NULL) { rspamd_mempool_delete (kv_parser->pool); } - kv_parser->pool = rspamd_mempool_new (rspamd_mempool_suggest_size ()); + kv_parser->pool = + rspamd_mempool_new (rspamd_mempool_suggest_size ()); /* Create new kvstorage_config */ - kv_parser->current_storage = g_malloc0 (sizeof (struct kvstorage_config)); + kv_parser->current_storage = + g_malloc0 (sizeof (struct kvstorage_config)); kv_parser->current_storage->id = last_id++; } if (g_ascii_strcasecmp (element_name, "type") == 0) { @@ -199,8 +215,9 @@ void kvstorage_xml_start_element (GMarkupParseContext *context, } else { if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected", - element_name); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected", + element_name); } kv_parser->state = KVSTORAGE_STATE_ERROR; } @@ -228,8 +245,9 @@ void kvstorage_xml_start_element (GMarkupParseContext *context, } else { if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected in backend definition", - element_name); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected in backend definition", + element_name); } kv_parser->state = KVSTORAGE_STATE_ERROR; } @@ -241,8 +259,9 @@ void kvstorage_xml_start_element (GMarkupParseContext *context, } else { if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected in expire definition", - element_name); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected in expire definition", + element_name); } kv_parser->state = KVSTORAGE_STATE_ERROR; } @@ -254,24 +273,27 @@ void kvstorage_xml_start_element (GMarkupParseContext *context, } -#define CHECK_TAG(s) \ -do { \ -if (g_ascii_strcasecmp (element_name, kv_parser->cur_elt) == 0) { \ - kv_parser->state = (s); \ -} \ -else { \ - if (*error == NULL) *error = g_error_new (xml_error_quark (), XML_UNMATCHED_TAG, "element %s is unexpected in this state, expected %s", element_name, kv_parser->cur_elt); \ - kv_parser->state = KVSTORAGE_STATE_ERROR; \ -} \ -} while (0) +#define CHECK_TAG(s) \ + do { \ + if (g_ascii_strcasecmp (element_name, kv_parser->cur_elt) == 0) { \ + kv_parser->state = (s); \ + } \ + else { \ + if (*error == NULL) *error = g_error_new ( \ + xml_error_quark (), XML_UNMATCHED_TAG, "element %s is unexpected in this state, expected %s", element_name, \ + kv_parser->cur_elt); \ + kv_parser->state = KVSTORAGE_STATE_ERROR; \ + } \ + } while (0) /* Called for close tags </foo> */ -void kvstorage_xml_end_element (GMarkupParseContext *context, - const gchar *element_name, - gpointer user_data, - GError **error) +void +kvstorage_xml_end_element (GMarkupParseContext *context, + const gchar *element_name, + gpointer user_data, + GError **error) { - struct kvstorage_config_parser *kv_parser = user_data; + struct kvstorage_config_parser *kv_parser = user_data; switch (kv_parser->state) { case KVSTORAGE_STATE_INIT: @@ -281,8 +303,9 @@ void kvstorage_xml_end_element (GMarkupParseContext *context, return; } if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "end element %s is unexpected, expected start element", - element_name); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "end element %s is unexpected, expected start element", + element_name); } kv_parser->state = KVSTORAGE_STATE_ERROR; break; @@ -310,8 +333,9 @@ void kvstorage_xml_end_element (GMarkupParseContext *context, } else { if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected", - element_name); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected", + element_name); } kv_parser->state = KVSTORAGE_STATE_ERROR; } @@ -322,8 +346,9 @@ void kvstorage_xml_end_element (GMarkupParseContext *context, } else { if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected", - element_name); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected", + element_name); } kv_parser->state = KVSTORAGE_STATE_ERROR; } @@ -336,18 +361,19 @@ void kvstorage_xml_end_element (GMarkupParseContext *context, #undef CHECK_TAG /* text is not nul-terminated */ -void kvstorage_xml_text (GMarkupParseContext *context, - const gchar *text, - gsize text_len, - gpointer user_data, - GError **error) +void +kvstorage_xml_text (GMarkupParseContext *context, + const gchar *text, + gsize text_len, + gpointer user_data, + GError **error) { - struct kvstorage_config_parser *kv_parser = user_data; - gchar *err_str; + struct kvstorage_config_parser *kv_parser = user_data; + gchar *err_str; /* Strip space symbols */ while (*text && g_ascii_isspace (*text)) { - text ++; + text++; } if (*text == '\0') { /* Skip empty text */ @@ -358,7 +384,9 @@ void kvstorage_xml_text (GMarkupParseContext *context, case KVSTORAGE_STATE_INIT: case KVSTORAGE_STATE_PARAM: if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "text is unexpected, expected start element"); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, + "text is unexpected, expected start element"); } kv_parser->state = KVSTORAGE_STATE_ERROR; break; @@ -366,12 +394,14 @@ void kvstorage_xml_text (GMarkupParseContext *context, kv_parser->current_storage->id = strtoul (text, &err_str, 10); if ((gsize)(err_str - text) != text_len) { if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "invalid number: %*s", (int)text_len, text); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "invalid number: %*s", + (int)text_len, text); } kv_parser->state = KVSTORAGE_STATE_ERROR; } else { - last_id ++; + last_id++; last_id = MAX (kv_parser->current_storage->id, last_id); } break; @@ -380,77 +410,103 @@ void kvstorage_xml_text (GMarkupParseContext *context, rspamd_strlcpy (kv_parser->current_storage->name, text, text_len + 1); break; case KVSTORAGE_STATE_CACHE_MAX_ELTS: - kv_parser->current_storage->cache.max_elements = rspamd_config_parse_limit (text, text_len); + kv_parser->current_storage->cache.max_elements = + rspamd_config_parse_limit (text, text_len); break; case KVSTORAGE_STATE_CACHE_MAX_MEM: - kv_parser->current_storage->cache.max_memory = rspamd_config_parse_limit (text, text_len); + kv_parser->current_storage->cache.max_memory = + rspamd_config_parse_limit (text, text_len); break; case KVSTORAGE_STATE_CACHE_NO_OVERWRITE: - kv_parser->current_storage->cache.no_overwrite = rspamd_config_parse_flag (text); + kv_parser->current_storage->cache.no_overwrite = + rspamd_config_parse_flag (text); break; case KVSTORAGE_STATE_CACHE_TYPE: - if (g_ascii_strncasecmp (text, "hash", MIN (text_len, sizeof ("hash") - 1)) == 0) { + if (g_ascii_strncasecmp (text, "hash", + MIN (text_len, sizeof ("hash") - 1)) == 0) { kv_parser->current_storage->cache.type = KVSTORAGE_TYPE_CACHE_HASH; } - else if (g_ascii_strncasecmp (text, "radix", MIN (text_len, sizeof ("radix") - 1)) == 0) { + else if (g_ascii_strncasecmp (text, "radix", + MIN (text_len, sizeof ("radix") - 1)) == 0) { kv_parser->current_storage->cache.type = KVSTORAGE_TYPE_CACHE_RADIX; } #ifdef WITH_JUDY - else if (g_ascii_strncasecmp (text, "judy", MIN (text_len, sizeof ("judy") - 1)) == 0) { + else if (g_ascii_strncasecmp (text, "judy", + MIN (text_len, sizeof ("judy") - 1)) == 0) { kv_parser->current_storage->cache.type = KVSTORAGE_TYPE_CACHE_JUDY; } #endif else { if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "invalid cache type: %*s", (int)text_len, text); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "invalid cache type: %*s", + (int)text_len, text); } kv_parser->state = KVSTORAGE_STATE_ERROR; } break; case KVSTORAGE_STATE_BACKEND_TYPE: - if (g_ascii_strncasecmp (text, "null", MIN (text_len, sizeof ("null") - 1)) == 0) { - kv_parser->current_storage->backend.type = KVSTORAGE_TYPE_BACKEND_NULL; + if (g_ascii_strncasecmp (text, "null", + MIN (text_len, sizeof ("null") - 1)) == 0) { + kv_parser->current_storage->backend.type = + KVSTORAGE_TYPE_BACKEND_NULL; } - else if (g_ascii_strncasecmp (text, "file", MIN (text_len, sizeof ("file") - 1)) == 0) { - kv_parser->current_storage->backend.type = KVSTORAGE_TYPE_BACKEND_FILE; + else if (g_ascii_strncasecmp (text, "file", + MIN (text_len, sizeof ("file") - 1)) == 0) { + kv_parser->current_storage->backend.type = + KVSTORAGE_TYPE_BACKEND_FILE; } #ifdef WITH_DB - else if (g_ascii_strncasecmp (text, "bdb", MIN (text_len, sizeof ("bdb") - 1)) == 0) { - kv_parser->current_storage->backend.type = KVSTORAGE_TYPE_BACKEND_BDB; + else if (g_ascii_strncasecmp (text, "bdb", + MIN (text_len, sizeof ("bdb") - 1)) == 0) { + kv_parser->current_storage->backend.type = + KVSTORAGE_TYPE_BACKEND_BDB; } #endif #ifdef WITH_SQLITE - else if (g_ascii_strncasecmp (text, "sqlite", MIN (text_len, sizeof ("sqlite") - 1)) == 0) { - kv_parser->current_storage->backend.type = KVSTORAGE_TYPE_BACKEND_SQLITE; + else if (g_ascii_strncasecmp (text, "sqlite", + MIN (text_len, sizeof ("sqlite") - 1)) == 0) { + kv_parser->current_storage->backend.type = + KVSTORAGE_TYPE_BACKEND_SQLITE; } #endif else { if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "invalid backend type: %*s", (int)text_len, text); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "invalid backend type: %*s", + (int)text_len, text); } kv_parser->state = KVSTORAGE_STATE_ERROR; } break; case KVSTORAGE_STATE_BACKEND_FILENAME: kv_parser->current_storage->backend.filename = g_malloc (text_len + 1); - rspamd_strlcpy (kv_parser->current_storage->backend.filename, text, text_len + 1); + rspamd_strlcpy (kv_parser->current_storage->backend.filename, + text, + text_len + 1); break; case KVSTORAGE_STATE_BACKEND_SYNC_OPS: - kv_parser->current_storage->backend.sync_ops = rspamd_config_parse_limit (text, text_len); + kv_parser->current_storage->backend.sync_ops = + rspamd_config_parse_limit (text, text_len); break; case KVSTORAGE_STATE_BACKEND_DO_FSYNC: - kv_parser->current_storage->backend.do_fsync = rspamd_config_parse_flag (text); + kv_parser->current_storage->backend.do_fsync = + rspamd_config_parse_flag (text); break; case KVSTORAGE_STATE_BACKEND_DO_REF: - kv_parser->current_storage->backend.do_ref = rspamd_config_parse_flag (text); + kv_parser->current_storage->backend.do_ref = rspamd_config_parse_flag ( + text); break; case KVSTORAGE_STATE_EXPIRE_TYPE: - if (g_ascii_strncasecmp (text, "lru", MIN (text_len, sizeof ("lru") - 1)) == 0) { + if (g_ascii_strncasecmp (text, "lru", + MIN (text_len, sizeof ("lru") - 1)) == 0) { kv_parser->current_storage->expire.type = KVSTORAGE_TYPE_EXPIRE_LRU; } else { if (*error == NULL) { - *error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "invalid expire type: %*s", (int)text_len, text); + *error = g_error_new ( + xml_error_quark (), XML_EXTRA_ELEMENT, "invalid expire type: %*s", + (int)text_len, text); } kv_parser->state = KVSTORAGE_STATE_ERROR; } @@ -463,12 +519,12 @@ void kvstorage_xml_text (GMarkupParseContext *context, } /* Called on error, including one set by other -* methods in the vtable. The GError should not be freed. -*/ + * methods in the vtable. The GError should not be freed. + */ void -kvstorage_xml_error (GMarkupParseContext *context, - GError *error, - gpointer user_data) +kvstorage_xml_error (GMarkupParseContext *context, + GError *error, + gpointer user_data) { msg_err ("kvstorage xml parser error: %s", error->message); } @@ -479,9 +535,11 @@ kvstorage_xml_error (GMarkupParseContext *context, static void kvstorage_cleanup (gpointer ud) { - struct kvstorage_config_parser *kv_parser = ud; + struct kvstorage_config_parser *kv_parser = ud; - g_hash_table_insert (storages, &kv_parser->current_storage->id, kv_parser->current_storage); + g_hash_table_insert (storages, + &kv_parser->current_storage->id, + kv_parser->current_storage); kv_parser->state = KVSTORAGE_STATE_INIT; g_hash_table_foreach (storages, kvstorage_init_callback, NULL); kv_parser->current_storage = NULL; @@ -493,16 +551,22 @@ kvstorage_cleanup (gpointer ud) void init_kvstorage_config (void) { - GMarkupParser *parser; - struct kvstorage_config_parser *kv_parser; + GMarkupParser *parser; + struct kvstorage_config_parser *kv_parser; if (storages == NULL) { - storages = g_hash_table_new_full (g_int_hash, g_int_equal, NULL, kvstorage_config_destroy); + storages = g_hash_table_new_full (g_int_hash, + g_int_equal, + NULL, + kvstorage_config_destroy); } else { /* Create new global table */ g_hash_table_destroy (storages); - storages = g_hash_table_new_full (g_int_hash, g_int_equal, NULL, kvstorage_config_destroy); + storages = g_hash_table_new_full (g_int_hash, + g_int_equal, + NULL, + kvstorage_config_destroy); } /* Create and register subparser */ @@ -520,7 +584,7 @@ init_kvstorage_config (void) } /* Get configuration for kvstorage with specified ID */ -struct kvstorage_config* +struct kvstorage_config * get_kvstorage_config (gint id) { if (storages == NULL) { |