]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Project] Fix on conditions
authorVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 4 Apr 2022 20:33:46 +0000 (21:33 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 4 Apr 2022 20:33:46 +0000 (21:33 +0100)
src/libserver/symcache/symcache_impl.cxx
src/libserver/symcache/symcache_internal.hxx

index 76bbf70b4b1812a35267c60164fcc49b6723b362..e1f3cafbb233a115b8310e752f49e20cdb791e29 100644 (file)
@@ -57,27 +57,26 @@ auto symcache::init() -> bool
        delayed_deps.reset();
 
 
-       cur = cache->delayed_conditions;
-       while (cur) {
-               dcond = cur->data;
+       /* Deal with the delayed conditions */
+       for (const auto &delayed_cond : *delayed_conditions) {
+               auto it = get_item_by_name_mut(delayed_cond.sym, true);
 
-               it = rspamd_symcache_find_filter(cache, dcond->sym, true);
-
-               if (it == NULL) {
+               if (it == nullptr) {
                        msg_err_cache (
                                        "cannot register delayed condition for %s",
-                                       dcond->sym);
-                       luaL_unref(dcond->L, LUA_REGISTRYINDEX, dcond->cbref);
+                                       delayed_cond.sym.c_str());
+                       luaL_unref(delayed_cond.L, LUA_REGISTRYINDEX, delayed_cond.cbref);
                }
                else {
-                       struct rspamd_symcache_condition *ncond = rspamd_mempool_alloc0 (cache->static_pool,
-                                       sizeof(*ncond));
-                       ncond->cb = dcond->cbref;
-                       DL_APPEND(it->specific.normal.conditions, ncond);
+                       if (!it->add_condition(delayed_cond.L, delayed_cond.cbref)) {
+                               msg_err_cache (
+                                               "cannot register delayed condition for %s: virtual parent; qed",
+                                               delayed_cond.sym.c_str());
+                               g_abort();
+                       }
                }
-
-               cur = g_list_next (cur);
        }
+       delayed_conditions.reset();
 
        PTR_ARRAY_FOREACH (cache->items_by_id, i, it) {
 
@@ -353,6 +352,21 @@ auto symcache::get_item_by_name(std::string_view name, bool resolve_parent) cons
        return it->second.get();
 }
 
+auto symcache::get_item_by_name_mut(std::string_view name, bool resolve_parent) const -> cache_item *
+{
+       auto it = items_by_symbol.find(name);
+
+       if (it == items_by_symbol.end()) {
+               return nullptr;
+       }
+
+       if (resolve_parent && it->second->is_virtual()) {
+               return (cache_item *) it->second->get_parent(*this);
+       }
+
+       return it->second.get();
+}
+
 auto symcache::add_dependency(int id_from, std::string_view to, int virtual_id_from)-> void
 {
        g_assert (id_from >= 0 && id_from < (gint)items_by_id.size());
@@ -382,6 +396,7 @@ auto symcache::add_dependency(int id_from, std::string_view to, int virtual_id_f
 }
 
 
+
 auto cache_item::get_parent(const symcache &cache) const -> const cache_item *
 {
        if (is_virtual()) {
index b41c4a3dfa024b16a35bfeef0b9b2a797ef4464b..b7e18ec1eb2acfe4d5917e01128f562a4adbf749 100644 (file)
@@ -177,14 +177,14 @@ class symcache;
 
 struct item_condition {
 private:
-       gint cb;
        lua_State *L;
+       int cb;
 public:
-       item_condition() {
-               // TODO
-       }
+       item_condition(lua_State *_L, int _cb) : L(_L), cb(_cb) {}
        virtual ~item_condition() {
-               // TODO
+               if (cb != -1 && L != nullptr) {
+                       luaL_unref(L, LUA_REGISTRYINDEX, cb);
+               }
        }
 };
 
@@ -197,8 +197,8 @@ public:
        explicit normal_item() {
                // TODO
        }
-       auto add_condition() -> void {
-               // TODO
+       auto add_condition(lua_State *L, int cbref) -> void {
+               conditions.emplace_back(L, cbref);
        }
        auto call() -> void {
                // TODO
@@ -261,6 +261,16 @@ struct cache_item {
 
        auto is_virtual() const -> bool { return std::holds_alternative<virtual_item>(specific); }
        auto get_parent(const symcache &cache) const -> const cache_item *;
+       auto add_condition(lua_State *L, int cbref) -> bool {
+               if (!is_virtual()) {
+                       auto &normal = std::get<normal_item>(specific);
+                       normal.add_condition(L, cbref);
+
+                       return true;
+               }
+
+               return false;
+       }
 };
 
 struct delayed_cache_dependency {
@@ -350,6 +360,13 @@ public:
         * @return
         */
        auto get_item_by_name(std::string_view name, bool resolve_parent) const -> const cache_item *;
+       /**
+        * Get an item by it's name, mutable pointer
+        * @param name
+        * @param resolve_parent
+        * @return
+        */
+       auto get_item_by_name_mut(std::string_view name, bool resolve_parent) const -> cache_item *;
 
        /**
         * Add a direct dependency