From: Vsevolod Stakhov Date: Tue, 19 Oct 2021 10:44:46 +0000 (+0100) Subject: [Project] Cdb: Fix configuration load X-Git-Tag: 3.1~29 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8650b54cda050ede64decf6bd61649573f4a071f;p=rspamd.git [Project] Cdb: Fix configuration load --- 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 tl::expected { + 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; } }