ovsdb: Drop regular expression constraints.
[openvswitch] / lib / ovsdb-types.c
index ff819609f8e9734af2fb0557447b5b03665d2f50..5da71456bb05f17e6bdecd4611023b6e7fafc651 100644 (file)
@@ -134,14 +134,13 @@ ovsdb_base_type_init(struct ovsdb_base_type *base, enum ovsdb_atomic_type type)
         break;
 
     case OVSDB_TYPE_STRING:
-        base->u.string.re = NULL;
-        base->u.string.reMatch = NULL;
-        base->u.string.reComment = NULL;
         base->u.string.minLen = 0;
         base->u.string.maxLen = UINT_MAX;
         break;
 
     case OVSDB_TYPE_UUID:
+        base->u.uuid.refTableName = NULL;
+        base->u.uuid.refTable = NULL;
         break;
 
     case OVSDB_N_TYPES:
@@ -166,12 +165,12 @@ ovsdb_base_type_clone(struct ovsdb_base_type *dst,
         break;
 
     case OVSDB_TYPE_STRING:
-        if (dst->u.string.re) {
-            pcre_refcount(dst->u.string.re, 1);
-        }
         break;
 
     case OVSDB_TYPE_UUID:
+        if (dst->u.uuid.refTableName) {
+            dst->u.uuid.refTableName = xstrdup(dst->u.uuid.refTableName);
+        }
         break;
 
     case OVSDB_N_TYPES:
@@ -192,14 +191,10 @@ ovsdb_base_type_destroy(struct ovsdb_base_type *base)
             break;
 
         case OVSDB_TYPE_STRING:
-            if (base->u.string.re && !pcre_refcount(base->u.string.re, -1)) {
-                pcre_free(base->u.string.re);
-                free(base->u.string.reMatch);
-                free(base->u.string.reComment);
-            }
             break;
 
         case OVSDB_TYPE_UUID:
+            free(base->u.uuid.refTableName);
             break;
 
         case OVSDB_N_TYPES:
@@ -258,12 +253,10 @@ ovsdb_base_type_has_constraints(const struct ovsdb_base_type *base)
         return false;
 
     case OVSDB_TYPE_STRING:
-        return (base->u.string.reMatch != NULL
-                || base->u.string.minLen != 0
-                || base->u.string.maxLen != UINT_MAX);
+        return base->u.string.minLen != 0 || base->u.string.maxLen != UINT_MAX;
 
     case OVSDB_TYPE_UUID:
-        return false;
+        return base->u.uuid.refTableName != NULL;
 
     case OVSDB_N_TYPES:
         NOT_REACHED();
@@ -281,38 +274,6 @@ ovsdb_base_type_clear_constraints(struct ovsdb_base_type *base)
     ovsdb_base_type_init(base, type);
 }
 
-struct ovsdb_error *
-ovsdb_base_type_set_regex(struct ovsdb_base_type *base,
-                          const char *reMatch, const char *reComment)
-{
-    const char *errorString;
-    const char *pattern;
-    int errorOffset;
-
-    /* Compile pattern, anchoring it at both ends. */
-    pattern = reMatch;
-    if (pattern[0] == '\0' || strchr(pattern, '\0')[-1] != '$') {
-        pattern = xasprintf("%s$", pattern);
-    }
-    base->u.string.re = pcre_compile(pattern, (PCRE_ANCHORED | PCRE_UTF8
-                                               | PCRE_JAVASCRIPT_COMPAT),
-                                     &errorString, &errorOffset, NULL);
-    if (pattern != reMatch) {
-        free((char *) pattern);
-    }
-    if (!base->u.string.re) {
-        return ovsdb_syntax_error(NULL, "invalid regular expression",
-                                  "\"%s\" is not a valid regular "
-                                  "expression: %s", reMatch, errorString);
-    }
-
-    /* Save regular expression. */
-    pcre_refcount(base->u.string.re, 1);
-    base->u.string.reMatch = xstrdup(reMatch);
-    base->u.string.reComment = reComment ? xstrdup(reComment) : NULL;
-    return NULL;
-}
-
 static struct ovsdb_error *
 parse_optional_uint(struct ovsdb_parser *parser, const char *member,
                     unsigned int *uint)
@@ -385,20 +346,6 @@ ovsdb_base_type_from_json(struct ovsdb_base_type *base,
             error = ovsdb_syntax_error(json, NULL, "minReal exceeds maxReal");
         }
     } else if (base->type == OVSDB_TYPE_STRING) {
-        const struct json *reMatch;
-
-        reMatch = ovsdb_parser_member(&parser, "reMatch",
-                                      OP_STRING | OP_OPTIONAL);
-        if (reMatch) {
-            const struct json *reComment;
-
-            reComment = ovsdb_parser_member(&parser, "reComment",
-                                            OP_STRING | OP_OPTIONAL);
-            error = ovsdb_base_type_set_regex(
-                base, json_string(reMatch),
-                reComment ? json_string(reComment) : NULL);
-        }
-
         if (!error) {
             error = parse_optional_uint(&parser, "minLength",
                                         &base->u.string.minLen);
@@ -411,6 +358,17 @@ ovsdb_base_type_from_json(struct ovsdb_base_type *base,
             error = ovsdb_syntax_error(json, NULL,
                                        "minLength exceeds maxLength");
         }
+    } else if (base->type == OVSDB_TYPE_UUID) {
+        const struct json *refTable;
+
+        refTable = ovsdb_parser_member(&parser, "refTable",
+                                       OP_ID | OP_OPTIONAL);
+        if (refTable) {
+            base->u.uuid.refTableName = xstrdup(refTable->u.string);
+            /* We can't set base->u.uuid.refTable here because we don't have
+             * enough context (we might not even be running in ovsdb-server).
+             * ovsdb_create() will set refTable later. */
+        }
     }
 
     if (error) {
@@ -467,13 +425,6 @@ ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
         break;
 
     case OVSDB_TYPE_STRING:
-        if (base->u.string.reMatch) {
-            json_object_put_string(json, "reMatch", base->u.string.reMatch);
-            if (base->u.string.reComment) {
-                json_object_put_string(json, "reComment",
-                                       base->u.string.reComment);
-            }
-        }
         if (base->u.string.minLen != 0) {
             json_object_put(json, "minLength",
                             json_integer_create(base->u.string.minLen));
@@ -485,6 +436,10 @@ ovsdb_base_type_to_json(const struct ovsdb_base_type *base)
         break;
 
     case OVSDB_TYPE_UUID:
+        if (base->u.uuid.refTableName) {
+            json_object_put_string(json, "refTable",
+                                   base->u.uuid.refTableName);
+        }
         break;
 
     case OVSDB_N_TYPES:
@@ -522,7 +477,8 @@ ovsdb_type_is_valid(const struct ovsdb_type *type)
             && ovsdb_base_type_is_valid(&type->key)
             && ovsdb_base_type_is_valid(&type->value)
             && type->n_min <= 1
-            && type->n_min <= type->n_max);
+            && type->n_min <= type->n_max
+            && type->n_max >= 1);
 }
 
 static struct ovsdb_error *