diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-10-19 11:44:46 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-10-19 11:44:46 +0100 |
commit | 8650b54cda050ede64decf6bd61649573f4a071f (patch) | |
tree | 0a22a23339f2e9afd4c7d06f50c5ed0fdbc7cecf /src | |
parent | b3a343bf86468d88998c36c7363d13e3a0dc9cee (diff) | |
download | rspamd-8650b54cda050ede64decf6bd61649573f4a071f.tar.gz rspamd-8650b54cda050ede64decf6bd61649573f4a071f.zip |
[Project] Cdb: Fix configuration load
Diffstat (limited to 'src')
-rw-r--r-- | src/libstat/backends/cdb_backend.cxx | 86 |
1 files changed, 55 insertions, 31 deletions
diff --git a/src/libstat/backends/cdb_backend.cxx b/src/libstat/backends/cdb_backend.cxx index 46b1f8121..89d5be9b2 100644 --- a/src/libstat/backends/cdb_backend.cxx +++ b/src/libstat/backends/cdb_backend.cxx @@ -244,55 +244,79 @@ ro_backend::process_token(const rspamd_token_t *tok) const -> std::optional<floa auto open_cdb(struct rspamd_statfile *st) -> tl::expected<ro_backend, std::string> { + const char *path = nullptr; const auto *stf = st->stcf; - const auto *filename = ucl_object_lookup_any(stf->opts, - "filename", "path", "cdb", nullptr); + auto get_filename = [](const ucl_object_t *obj) -> const char * { + const auto *filename = ucl_object_lookup_any(obj, + "filename", "path", "cdb", nullptr); - if (filename && ucl_object_type(filename) == UCL_STRING) { - const auto *path = ucl_object_tostring(filename); + if (filename && ucl_object_type(filename) == UCL_STRING) { + return ucl_object_tostring(filename); + } - auto cached_cdb_maybe = cdb_shared_storage.get_cdb(path); - cdb_shared_storage::cdb_element_t cdbp; + return nullptr; + }; - if (!cached_cdb_maybe) { + /* First search in backend configuration */ + const auto *obj = ucl_object_lookup (st->classifier->cfg->opts, "backend"); + if (obj != NULL && ucl_object_type (obj) == UCL_OBJECT) { + path = get_filename(obj); + } - auto fd = rspamd_file_xopen(path, O_RDONLY, 0, true); + /* Now try statfiles config */ + if (!path && stf->opts) { + path = get_filename(stf->opts); + } - if (fd == -1) { - return tl::make_unexpected(fmt::format("cannot open {}: {}", - path, strerror(errno))); - } + /* Now try classifier config */ + if (!path && st->classifier->cfg->opts) { + path = get_filename(st->classifier->cfg->opts); + } - cdbp = cdb_shared_storage::new_cdb(); + if (!path) { + return tl::make_unexpected("missing/malformed filename attribute"); + } - if (cdb_init(cdbp.get(), fd) == -1) { - return tl::make_unexpected(fmt::format("cannot init cdb in {}: {}", - path, strerror(errno))); - } - } - else { - cdbp = cached_cdb_maybe.value(); - } + auto cached_cdb_maybe = cdb_shared_storage.get_cdb(path); + cdb_shared_storage::cdb_element_t cdbp; - if (!cdbp) { - return tl::make_unexpected(fmt::format("cannot init cdb in {}: internal error", - path)); - } + if (!cached_cdb_maybe) { + + auto fd = rspamd_file_xopen(path, O_RDONLY, 0, true); - ro_backend bk{st, cdbp}; + if (fd == -1) { + return tl::make_unexpected(fmt::format("cannot open {}: {}", + path, strerror(errno))); + } - auto res = bk.load_cdb(); + cdbp = cdb_shared_storage::new_cdb(); - if (!res) { - return tl::make_unexpected(res.error()); + if (cdb_init(cdbp.get(), fd) == -1) { + return tl::make_unexpected(fmt::format("cannot init cdb in {}: {}", + path, strerror(errno))); } - return bk; + cdbp = cdb_shared_storage.push_cdb(path, cdbp); } else { - return tl::make_unexpected("missing/malformed filename attribute"); + cdbp = cached_cdb_maybe.value(); + } + + if (!cdbp) { + return tl::make_unexpected(fmt::format("cannot init cdb in {}: internal error", + path)); } + + ro_backend bk{st, cdbp}; + + auto res = bk.load_cdb(); + + if (!res) { + return tl::make_unexpected(res.error()); + } + + return bk; } } |