]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/rules kr_rules_init(): allow not overwriting the DB
authorVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 11 Mar 2024 07:09:38 +0000 (08:09 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Tue, 2 Jul 2024 12:07:48 +0000 (14:07 +0200)
daemon/lua/kres-gen-30.lua
daemon/lua/kres-gen-31.lua
daemon/lua/kres-gen-32.lua
lib/rules/api.c
lib/rules/api.h
lib/rules/impl.h

index 36e3e40545d3abcbbb499f592c14f5743bdc2b42..56f7abb11b2539a1d5b03e74a8fbbeb23c9f1159 100644 (file)
@@ -497,7 +497,7 @@ int kr_cache_remove(struct kr_cache *, const knot_dname_t *, uint16_t);
 int kr_cache_remove_subtree(struct kr_cache *, const knot_dname_t *, _Bool, int);
 int kr_cache_commit(struct kr_cache *);
 uint32_t packet_ttl(const knot_pkt_t *);
-int kr_rules_init(const char *, size_t);
+int kr_rules_init(const char *, size_t, _Bool);
 int kr_rules_commit(_Bool);
 int kr_view_insert_action(const char *, const char *, kr_proto_set, const char *);
 int kr_view_select_action(const struct kr_request *, knot_db_val_t *);
index beeec867560a0505d063c74e4fb7a7d958676a4c..1855c6d8bc1dd929b66b35f7cb411091ab180beb 100644 (file)
@@ -497,7 +497,7 @@ int kr_cache_remove(struct kr_cache *, const knot_dname_t *, uint16_t);
 int kr_cache_remove_subtree(struct kr_cache *, const knot_dname_t *, _Bool, int);
 int kr_cache_commit(struct kr_cache *);
 uint32_t packet_ttl(const knot_pkt_t *);
-int kr_rules_init(const char *, size_t);
+int kr_rules_init(const char *, size_t, _Bool);
 int kr_rules_commit(_Bool);
 int kr_view_insert_action(const char *, const char *, kr_proto_set, const char *);
 int kr_view_select_action(const struct kr_request *, knot_db_val_t *);
index 9edee2de41e4e0f87dfd771595e7cf67bce329e3..9fe1698153d7e57e0017083ce3e4ab6c39aa8da0 100644 (file)
@@ -498,7 +498,7 @@ int kr_cache_remove(struct kr_cache *, const knot_dname_t *, uint16_t);
 int kr_cache_remove_subtree(struct kr_cache *, const knot_dname_t *, _Bool, int);
 int kr_cache_commit(struct kr_cache *);
 uint32_t packet_ttl(const knot_pkt_t *);
-int kr_rules_init(const char *, size_t);
+int kr_rules_init(const char *, size_t, _Bool);
 int kr_rules_commit(_Bool);
 int kr_view_insert_action(const char *, const char *, kr_proto_set, const char *);
 int kr_view_select_action(const struct kr_request *, knot_db_val_t *);
index 8e908a7afda19fcb84d52db11ac134fdfaf2ab8c..8d2c102849d8540b707aaba11c15201bcf43cb9f 100644 (file)
@@ -141,9 +141,9 @@ int kr_rules_init_ensure(void)
 {
        if (the_rules)
                return kr_ok();
-       return kr_rules_init(NULL, 0);
+       return kr_rules_init(NULL, 0, true);
 }
-int kr_rules_init(const char *path, size_t maxsize)
+int kr_rules_init(const char *path, size_t maxsize, bool overwrite)
 {
        if (the_rules)
                return kr_error(EINVAL);
@@ -157,22 +157,17 @@ int kr_rules_init(const char *path, size_t maxsize)
                // FIXME: the file will be sparse, but we still need to choose its size somehow.
                // Later we might improve it to auto-resize in case of running out of space.
                // Caveat: mdb_env_set_mapsize() can only be called without transactions open.
-               .maxsize = maxsize ? maxsize :
-                       (size_t)(sizeof(size_t) > 4 ? 2048 : 500) * 1024*1024,
+               .maxsize = !overwrite ? 0 :
+                       (maxsize ? maxsize : (size_t)(sizeof(size_t) > 4 ? 2048 : 500) * 1024*1024),
        };
        int ret = the_rules->api->open(&the_rules->db, &the_rules->stats, &opts, NULL);
-       /* No persistence - we always refill from config for now.
-        * LATER:
-        *  - Make it include versioning?
-        *  - "\0stamp" key when loading config(s)?
-        *  - Don't clear ruleset data that doesn't come directly from config;
-        *    and add marks for that, etc.
-        *    (after there actually are any kinds of rules like that)
-        */
-       if (ret == 0) ret = ruledb_op(clear);
+
+       if (ret == 0 && overwrite) ret = ruledb_op(clear);
        if (ret != 0) goto failure;
        kr_require(the_rules->db);
 
+       if (!overwrite) return kr_ok(); // we assume that the caller ensured OK contents
+
        ret = tag_names_default();
        if (ret != 0) goto failure;
 
index 1069ef4d4ab96b49d6aa77473208e6ace0f8469a..f7f3b4660f8c2fb9659367331e49208cab383e21 100644 (file)
@@ -19,11 +19,13 @@ typedef uint64_t kr_rule_tags_t;
 
 /** Open the rule DB.
  *
- * You can call this to override the path or size (NULL/0 -> default).
- * Not allowed if already open (EINVAL), so this optional call has to come
- * before writing anything into the DB. */
+ * You can call this to override the path or size (NULL/0 -> default)
+ * or choose not to overwrite the DB with just the defaults.
+ *
+ * \return error code.  Not allowed if already open (EINVAL),
+ * so this optional call has to come before writing anything into the DB. */
 KR_EXPORT
-int kr_rules_init(const char *path, size_t maxsize);
+int kr_rules_init(const char *path, size_t maxsize, bool overwrite);
 /** kr_rules_init() but OK if already open, and not allowing to override defaults. */
 KR_EXPORT
 int kr_rules_init_ensure(void);
index 0d7de513ad981a6ded10c46c20557b24f21d4188..1a2ee4dda4300d0b7237f8c2b1682e9faedffd24 100644 (file)
@@ -20,7 +20,7 @@ extern struct kr_rules *the_rules;
 
 #define ENSURE_the_rules \
        if (!the_rules) { \
-               int ret = kr_rules_init(NULL, 0); \
+               int ret = kr_rules_init(NULL, 0, true); \
                if (ret) return ret; \
        }