From: Vsevolod Stakhov Date: Fri, 19 Dec 2014 15:45:11 +0000 (+0000) Subject: Add backend open. X-Git-Tag: 0.8.0~67 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cb2b69d24c8ca1ab987956717b9076ae43c4fee2;p=rspamd.git Add backend open. --- diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c index 41e844ff2..e4b0b4e6c 100644 --- a/src/fuzzy_storage.c +++ b/src/fuzzy_storage.c @@ -54,8 +54,7 @@ #define MAX_RETRIES 40 /* Weight of hash to consider it frequent */ #define DEFAULT_FREQUENT_SCORE 100 -/* Magic sequence for hashes file */ -#define FUZZY_FILE_MAGIC "rsh" + /* Current version of fuzzy hash file format */ #define CURRENT_FUZZY_VERSION 1 diff --git a/src/libserver/fuzzy_backend.c b/src/libserver/fuzzy_backend.c index 17f0eb802..9bdf9fc1c 100644 --- a/src/libserver/fuzzy_backend.c +++ b/src/libserver/fuzzy_backend.c @@ -26,14 +26,81 @@ #include "fuzzy_backend.h" #include "fuzzy_storage.h" +#include -struct rspamd_fuzzy_backend; +/* Magic sequence for hashes file */ +#define FUZZY_FILE_MAGIC "rsh" -struct -rspamd_fuzzy_backend* rspamd_fuzzy_backend_open (const gchar *path, - GError **err) +struct rspamd_fuzzy_backend { + sqlite3 *db; + const char *path; +}; + +static GQuark +rspamd_fuzzy_backend_quark(void) { + return g_quark_from_static_string ("fuzzy-storage-backend"); +} +struct rspamd_fuzzy_backend* +rspamd_fuzzy_backend_open (const gchar *path, GError **err) +{ + gchar *dir, header[4]; + gint fd, r; + struct rspamd_fuzzy_backend *res; + + /* First of all we check path for existence */ + dir = g_path_get_dirname (path); + if (dir == NULL) { + g_set_error (err, rspamd_fuzzy_backend_quark (), + errno, "Cannot get directory name for %s: %s", path, + strerror (errno)); + return NULL; + } + + if (access (path, W_OK) == -1 && access (dir, W_OK) == -1) { + g_set_error (err, rspamd_fuzzy_backend_quark (), + errno, "Cannot access directory %s to create database: %s", + dir, strerror (errno)); + g_free (dir); + + return NULL; + } + + g_free (dir); + if ((fd = open (path, O_RDONLY)) == -1) { + g_set_error (err, rspamd_fuzzy_backend_quark (), + errno, "Cannot open file %s: %s", + path, strerror (errno)); + + return NULL; + } + + /* Check for legacy format */ + if ((r = read (fd, header, sizeof (header))) == sizeof (header)) { + if (memcmp (header, FUZZY_FILE_MAGIC, sizeof (header) - 1) == 0) { + msg_info ("Trying to convert old fuzzy database"); + if (!rspamd_fuzzy_backend_convert (fd, err)) { + close (fd); + return NULL; + } + } + close (fd); + } + + /* Open database */ + if ((res = rspamd_fuzzy_backend_open_db (path, err)) == NULL) { + GError *tmp = NULL; + + if ((res = rspamd_fuzzy_backend_create_db (path, tmp)) == NULL) { + g_clear_error (err); + g_propagate_error (err, tmp); + return NULL; + } + g_clear_error (err); + } + + return res; } struct rspamd_fuzzy_reply