diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-03-12 14:21:24 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-03-12 14:21:24 +0000 |
commit | c451d5d7d3cb2247c6bec32a7f639e72ddd469c1 (patch) | |
tree | 71f30dbd46de1cb3397717b715de7fec66e12831 /src/libserver/roll_history.c | |
parent | 420cd8b263fc9b833c60de855b4bb2fe0f95f2ab (diff) | |
download | rspamd-c451d5d7d3cb2247c6bec32a7f639e72ddd469c1.tar.gz rspamd-c451d5d7d3cb2247c6bec32a7f639e72ddd469c1.zip |
Rework rolling history.
- Store inet_addr as string, not structure.
- Add history magic for versioning and sanity.
Diffstat (limited to 'src/libserver/roll_history.c')
-rw-r--r-- | src/libserver/roll_history.c | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c index 5edf5bea0..2f5c5ddb3 100644 --- a/src/libserver/roll_history.c +++ b/src/libserver/roll_history.c @@ -27,6 +27,7 @@ #include "main.h" #include "roll_history.h" +static const gchar rspamd_history_magic[] = {'r', 's', 'h', '1'}; /** * Returns new roll history @@ -108,7 +109,9 @@ rspamd_roll_history_update (struct roll_history *history, } /* Add information from task to roll history */ - memcpy (&row->from_addr, &task->from_addr, sizeof (row->from_addr)); + rspamd_strlcpy (row->from_addr, + rspamd_inet_address_to_string (task->from_addr), + sizeof (row->from_addr)); memcpy (&row->tv, &task->tv, sizeof (row->tv)); /* Strings */ @@ -161,6 +164,7 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename) { gint fd; struct stat st; + gchar magic[sizeof(rspamd_history_magic)]; if (stat (filename, &st) == -1) { msg_info ("cannot load history from %s: %s", filename, @@ -168,7 +172,7 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename) return FALSE; } - if (st.st_size != sizeof (history->rows)) { + if (st.st_size != sizeof (history->rows) + sizeof (rspamd_history_magic)) { msg_info ("cannot load history from %s: size mismatch", filename); return FALSE; } @@ -179,6 +183,19 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename) return FALSE; } + if (read (fd, magic, sizeof (magic)) == -1) { + close (fd); + msg_info ("cannot read history from %s: %s", filename, + strerror (errno)); + return FALSE; + } + + if (memcmp (magic, rspamd_history_magic, sizeof (magic)) != 0) { + close (fd); + msg_info ("cannot read history from %s: bad magic", filename); + return FALSE; + } + if (read (fd, history->rows, sizeof (history->rows)) == -1) { close (fd); msg_info ("cannot read history from %s: %s", filename, @@ -207,6 +224,12 @@ rspamd_roll_history_save (struct roll_history *history, const gchar *filename) return FALSE; } + if (write (fd, rspamd_history_magic, sizeof (rspamd_history_magic)) == -1) { + close (fd); + msg_info ("cannot write history to %s: %s", filename, strerror (errno)); + return FALSE; + } + if (write (fd, history->rows, sizeof (history->rows)) == -1) { close (fd); msg_info ("cannot write history to %s: %s", filename, strerror (errno)); |