From: Vsevolod Stakhov Date: Sat, 30 Apr 2022 12:42:38 +0000 (+0100) Subject: [Project] Implement more methods X-Git-Tag: 3.3~293^2~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d2f5129241523754c8f722648ca4331ce8d0a8bc;p=thirdparty%2Frspamd.git [Project] Implement more methods --- diff --git a/src/libserver/symcache/symcache_c.cxx b/src/libserver/symcache/symcache_c.cxx index 4547f23a4a..ba3ccd9a67 100644 --- a/src/libserver/symcache/symcache_c.cxx +++ b/src/libserver/symcache/symcache_c.cxx @@ -433,7 +433,7 @@ rspamd_symcache_set_cur_item(struct rspamd_task *task, struct rspamd_symcache_it auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); auto *real_item = C_API_SYMCACHE_ITEM(item); - return (struct rspamd_symcache_item *)cache_runtime->set_cur_item(real_item); + return (struct rspamd_symcache_item *) cache_runtime->set_cur_item(real_item); } void @@ -494,4 +494,38 @@ rspamd_symcache_item_async_dec_check_full(struct rspamd_task *task, } return FALSE; +} + +struct rspamd_abstract_callback_data * +rspamd_symcache_get_cbdata(struct rspamd_symcache *cache, + const gchar *symbol) +{ + auto *real_cache = C_API_SYMCACHE(cache); + + auto *item = real_cache->get_item_by_name(symbol, true); + + if (item) { + return (struct rspamd_abstract_callback_data *) item->get_cbdata(); + } + + return nullptr; +} + +void +rspamd_symcache_composites_foreach(struct rspamd_task *task, + struct rspamd_symcache *cache, + GHFunc func, + gpointer fd) +{ + auto *real_cache = C_API_SYMCACHE(cache); + auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime); + + real_cache->composites_foreach([&](const auto *item) { + auto *dyn_item = cache_runtime->get_dynamic_item(item->id, false); + + if (dyn_item->started) { + func((void *)item->get_name().c_str(), item->get_cbdata(), fd); + dyn_item->finished = true; + } + }); } \ No newline at end of file diff --git a/src/libserver/symcache/symcache_internal.hxx b/src/libserver/symcache/symcache_internal.hxx index 457dc58483..1b4d5a509e 100644 --- a/src/libserver/symcache/symcache_internal.hxx +++ b/src/libserver/symcache/symcache_internal.hxx @@ -355,6 +355,18 @@ public: } } + /** + * Iterate over all composites using a specific functor + * @tparam Functor + * @param f + */ + template + auto composites_foreach(Functor f) -> void { + for (const auto &sym_it : composites) { + f(sym_it.get()); + } + } + /** * Resort cache if anything has been changed since last time * @return diff --git a/src/libserver/symcache/symcache_item.hxx b/src/libserver/symcache/symcache_item.hxx index f4ed75eb1f..a194e9b0d5 100644 --- a/src/libserver/symcache/symcache_item.hxx +++ b/src/libserver/symcache/symcache_item.hxx @@ -97,6 +97,10 @@ public: return std::all_of(std::begin(conditions), std::end(conditions), [&](const auto &cond) { return cond.check(sym_name, task); }); } + + auto get_cbdata() const -> auto { + return user_data; + } }; class virtual_item { @@ -302,6 +306,20 @@ public: */ auto is_allowed(struct rspamd_task *task, bool exec_only) const -> bool; + /** + * Returns callback data + * @return + */ + auto get_cbdata() const -> void * { + if (std::holds_alternative(specific)) { + const auto &filter_data = std::get(specific); + + return filter_data.get_cbdata(); + } + + return nullptr; + } + private: /** * Constructor for a normal symbols with callback @@ -326,6 +344,7 @@ private: priority(_priority), specific(normal_item{func, user_data}) { + /* These structures are kept trivial, so they need to be explicitly reset */ forbidden_ids.reset(); allowed_ids.reset(); exec_only_ids.reset(); @@ -352,6 +371,7 @@ private: flags(_flags), specific(virtual_item{parent}) { + /* These structures are kept trivial, so they need to be explicitly reset */ forbidden_ids.reset(); allowed_ids.reset(); exec_only_ids.reset();