]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: vars: add a VF_CREATEONLY flag for creation
authorWilly Tarreau <w@1wt.eu>
Wed, 8 Sep 2021 09:38:25 +0000 (11:38 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 8 Sep 2021 09:47:30 +0000 (11:47 +0200)
Passing this flag to var_set() will result in the variable to only be
created if it did not exist, otherwise nothing is done (it's not even
updated). This will be used for pre-registering names.

include/haproxy/vars-t.h
src/vars.c

index 0e35ade1988e3ea4efa642b11f11d6825ca6295e..06f2712e12b1dd36c1884b2baa8820408a5b08b0 100644 (file)
@@ -27,6 +27,7 @@
 
 /* flags used when setting/clearing variables */
 #define VF_UPDATEONLY 0x00000001   // SCOPE_PROC variables are only updated
+#define VF_CREATEONLY 0x00000002   // do nothing if the variable already exists
 
 enum vars_scope {
        SCOPE_SESS = 0,
index df26ac160a8eb6a16b043d32d12b96a60df68b7f..80a6a4520c6164e78afdcd286c2ca7f1c632c484 100644 (file)
@@ -356,6 +356,7 @@ static int smp_fetch_var(const struct arg *args, struct sample *smp, const char
  * Flags is a bitfield that may contain one of the following flags:
  *   - VF_UPDATEONLY: if the scope is SCOPE_PROC, the variable may only be
  *     updated but not created.
+ *   - VF_CREATEONLY: do nothing if the variable already exists (success).
  *
  * It returns 0 on failure, non-zero on success.
  */
@@ -375,6 +376,11 @@ static int var_set(const char *name, enum vars_scope scope, struct sample *smp,
        var = var_get(vars, name);
 
        if (var) {
+               if (flags & VF_CREATEONLY) {
+                       ret = 1;
+                       goto unlock;
+               }
+
                /* free its used memory. */
                if (var->data.type == SMP_T_STR ||
                    var->data.type == SMP_T_BIN) {