]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: pattern: implement pat_ref_load() to load a pattern at a given generation
authorWilly Tarreau <w@1wt.eu>
Thu, 29 Oct 2020 08:21:43 +0000 (09:21 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 5 Nov 2020 18:27:09 +0000 (19:27 +0100)
pat_ref_load() basically combines pat_ref_append() and pat_ref_commit().
It's very similar to pat_ref_add() except that it also allows to set the
generation ID and the line number. pat_ref_add() was modified to directly
rely on it to avoid code duplication. Note that a previous declaration
of pat_ref_load() was removed as it was just a leftover of an earlier
incarnation of something possibly similar, so no existing functionality
was changed here.

include/haproxy/pattern.h
src/pattern.c

index 40db3559a6a347d7e110eb36fda99a6f4083241e..ac06eeeaa96f6ede6cb794c807f1668df44a1e21 100644 (file)
@@ -182,6 +182,7 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned
 struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags);
 struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key);
 struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, const char *sample, int line);
+struct pat_ref_elt *pat_ref_load(struct pat_ref *ref, unsigned int gen, const char *pattern, const char *sample, int line, char **err);
 int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr, int patflags, char **err);
 int pat_ref_add(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
 int pat_ref_set(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
@@ -190,7 +191,6 @@ int pat_ref_delete(struct pat_ref *ref, const char *key);
 void pat_ref_delete_by_ptr(struct pat_ref *ref, struct pat_ref_elt *elt);
 int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt);
 int pat_ref_prune(struct pat_ref *ref);
-int pat_ref_load(struct pat_ref *ref, struct pattern_expr *expr, int patflags, int soe, char **err);
 int pat_ref_commit(struct pat_ref *ref, struct pat_ref_elt *elt, char **err);
 void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace);
 
index fe51769221f59759921214fa7f32e07848c74ab8..5613512ce0f873c2323e4004f9413afc040fee88 100644 (file)
@@ -1945,6 +1945,32 @@ int pat_ref_commit(struct pat_ref *ref, struct pat_ref_elt *elt, char **err)
        return 1;
 }
 
+/* Loads <pattern>:<sample> into <ref> for generation <gen>. <sample> may be
+ * NULL if none exists (e.g. ACL). If not needed, the generation number should
+ * be set to ref->curr_gen. The error pointer must initially point to NULL. The
+ * new entry will be propagated to all use places, involving allocation, parsing
+ * and indexing. On error (parsing, allocation), the operation will be rolled
+ * back, an error may be reported, and NULL will be reported. On success, the
+ * freshly allocated element will be returned. The PATREF lock on <ref> must be
+ * held during the operation.
+ */
+struct pat_ref_elt *pat_ref_load(struct pat_ref *ref, unsigned int gen,
+                                 const char *pattern, const char *sample,
+                                 int line, char **err)
+{
+       struct pat_ref_elt *elt;
+
+       elt = pat_ref_append(ref, pattern, sample, line);
+       if (elt) {
+               elt->gen_id = gen;
+               if (!pat_ref_commit(ref, elt, err))
+                       elt = NULL;
+       } else
+               memprintf(err, "out of memory error");
+
+       return elt;
+}
+
 /* This function adds entry to <ref>. It can fail on memory error. The new
  * entry is added at all the pattern_expr registered in this reference. The
  * function stops on the first error encountered. It returns 0 and <err> is
@@ -1955,14 +1981,7 @@ int pat_ref_add(struct pat_ref *ref,
                 const char *pattern, const char *sample,
                 char **err)
 {
-       struct pat_ref_elt *elt;
-
-       elt = pat_ref_append(ref, pattern, sample, -1);
-       if (!elt) {
-               memprintf(err, "out of memory error");
-               return 0;
-       }
-       return pat_ref_commit(ref, elt, err);
+       return !!pat_ref_load(ref, ref->curr_gen, pattern, sample, -1, err);
 }
 
 /* This function prunes <ref>, replaces all references by the references