}
}
- dump_dynamic_config (ctx->cfg);
- msg_info ("<%s> modified %d actions",
- rspamd_inet_address_to_string (session->from_addr),
- added);
+ if (dump_dynamic_config (ctx->cfg)) {
+ msg_info ("<%s> modified %d actions",
+ rspamd_inet_address_to_string (session->from_addr),
+ added);
- rspamd_controller_send_string (conn_ent, "{\"success\":true}");
+ rspamd_controller_send_string (conn_ent, "{\"success\":true}");
+ }
+ else {
+ rspamd_controller_send_error (conn_ent, 500, "Save error");
+ }
+
+ ucl_object_unref (obj);
return 0;
}
}
}
- dump_dynamic_config (ctx->cfg);
- msg_info ("<%s> modified %d symbols",
- rspamd_inet_address_to_string (session->from_addr),
- added);
+ if (added > 0) {
+ if (dump_dynamic_config (ctx->cfg)) {
+ msg_info ("<%s> modified %d symbols",
+ rspamd_inet_address_to_string (session->from_addr),
+ added);
- rspamd_controller_send_string (conn_ent, "{\"success\":true}");
+ rspamd_controller_send_string (conn_ent, "{\"success\":true}");
+ }
+ else {
+ rspamd_controller_send_error (conn_ent, 500, "Save error");
+ }
+ }
+ else {
+ msg_err ("no symbols to save");
+ rspamd_controller_send_error (conn_ent, 404, "No symbols to save");
+ }
+
+ ucl_object_unref (obj);
return 0;
}
#include "dynamic_cfg.h"
struct config_json_buf {
- gchar *buf;
- gchar *pos;
- size_t buflen;
+ GString *buf;
struct rspamd_config *cfg;
- ucl_object_t *obj;
};
/**
gint len,
struct map_cb_data *data)
{
- struct config_json_buf *jb;
- gint free, off;
+ struct config_json_buf *jb, *pd;
+
+ pd = data->prev_data;
+
+ g_assert (pd != NULL);
if (data->cur_data == NULL) {
- jb = g_malloc (sizeof (struct config_json_buf));
- jb->cfg = ((struct config_json_buf *)data->prev_data)->cfg;
- jb->buf = NULL;
- jb->pos = NULL;
- jb->obj = NULL;
+ jb = g_slice_alloc (sizeof (*jb));
+ jb->cfg = pd->cfg;
+ jb->buf = pd->buf;
data->cur_data = jb;
}
else {
if (jb->buf == NULL) {
/* Allocate memory for buffer */
- jb->buflen = len * 2;
- jb->buf = g_malloc (jb->buflen);
- jb->pos = jb->buf;
- }
-
- off = jb->pos - jb->buf;
- free = jb->buflen - off;
-
- if (free < len) {
- jb->buflen = MAX (jb->buflen * 2, jb->buflen + len * 2);
- jb->buf = g_realloc (jb->buf, jb->buflen);
- jb->pos = jb->buf + off;
+ jb->buf = g_string_sized_new (BUFSIZ);
}
- memcpy (jb->pos, chunk, len);
- jb->pos += len;
+ g_string_append_len (jb->buf, chunk, len);
- /* Say not to copy any part of this buffer */
return NULL;
}
if (data->prev_data) {
jb = data->prev_data;
/* Clean prev data */
- if (jb->buf) {
- g_free (jb->buf);
- }
- g_free (jb);
+ g_slice_free1 (sizeof (*jb), jb);
}
/* Now parse json */
msg_err ("no data read");
return;
}
- /* NULL terminate current buf */
- *jb->pos = '\0';
parser = ucl_parser_new (0);
- if (!ucl_parser_add_chunk (parser, jb->buf, jb->pos - jb->buf)) {
+
+ if (!ucl_parser_add_chunk (parser, jb->buf->str, jb->buf->len)) {
msg_err ("cannot load json data: parse error %s",
ucl_parser_get_error (parser));
ucl_parser_free (parser);
return;
}
- jb->cfg->current_dynamic_conf = NULL;
- ucl_object_unref (jb->obj);
- jb->obj = top;
-
- /*
- * Note about thread safety: we are updating values that are gdoubles so it is not atomic in general case
- * but on the other hand all that data is used only in the main thread, so why it is *likely* safe
- * to do this task in this way without explicit lock.
- */
- apply_dynamic_conf (jb->obj, jb->cfg);
-
- jb->cfg->current_dynamic_conf = jb->obj;
+ ucl_object_unref (jb->cfg->current_dynamic_conf);
+ apply_dynamic_conf (top, jb->cfg);
+ jb->cfg->current_dynamic_conf = top;
}
/**
}
/* Now try to add map with json data */
- jb = g_malloc0 (sizeof (struct config_json_buf));
+ jb = g_slice_alloc (sizeof (struct config_json_buf));
pjb = g_malloc (sizeof (struct config_json_buf *));
jb->buf = NULL;
jb->cfg = cfg;
*pjb = jb;
+ cfg->current_dynamic_conf = ucl_object_typed_new (UCL_ARRAY);
+
if (!rspamd_map_add (cfg, cfg->dynamic_conf, "Dynamic configuration map",
json_config_read_cb, json_config_fin_cb, (void **)pjb)) {
msg_err ("cannot add map for configuration %s", cfg->dynamic_conf);
if (cfg->dynamic_conf == NULL || cfg->current_dynamic_conf == NULL) {
/* No dynamic conf has been specified, so do not try to dump it */
+ msg_err ("cannot save dynamic conf as it is not specified");
return FALSE;
}