X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=lib%2Fovsdb-data.c;h=42fdf1ea4f305a1e043b83f03157b14c24cea2c1;hb=7103dec49eb569c3196239da6c178a29c3003e2b;hp=47d1ea7462bd450838c4d225aabe754998125bfc;hpb=fbf925e45da7100e5018bd3ad49532e407666f77;p=openvswitch diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c index 47d1ea74..42fdf1ea 100644 --- a/lib/ovsdb-data.c +++ b/lib/ovsdb-data.c @@ -577,30 +577,6 @@ check_string_constraints(const char *s, "length %u", s, n_chars, c->maxLen); } -#if HAVE_PCRE - if (c->re) { - int retval; - - retval = pcre_exec(c->re, NULL, s, strlen(s), 0, - PCRE_ANCHORED | PCRE_NO_UTF8_CHECK, NULL, 0); - if (retval == PCRE_ERROR_NOMATCH) { - if (c->reComment) { - return ovsdb_error("constraint violation", - "\"%s\" is not a %s", s, c->reComment); - } else { - return ovsdb_error("constraint violation", - "\"%s\" does not match regular expression " - "/%s/", s, c->reMatch); - } - } else if (retval < 0) { - /* PCRE doesn't have a function to translate an error code to a - * description. Bizarre. See pcreapi(3) for error details. */ - return ovsdb_error("internal error", "PCRE returned error %d", - retval); - } - } -#endif /* HAVE_PCRE */ - return NULL; } @@ -614,6 +590,25 @@ struct ovsdb_error * ovsdb_atom_check_constraints(const union ovsdb_atom *atom, const struct ovsdb_base_type *base) { + if (base->enum_ + && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) { + struct ovsdb_error *error; + struct ds actual = DS_EMPTY_INITIALIZER; + struct ds valid = DS_EMPTY_INITIALIZER; + + ovsdb_atom_to_string(atom, base->type, &actual); + ovsdb_datum_to_string(base->enum_, + ovsdb_base_type_get_enum_type(base->type), + &valid); + error = ovsdb_error("constraint violation", + "%s is not one of the allowed values (%s)", + ds_cstr(&actual), ds_cstr(&valid)); + ds_destroy(&actual); + ds_destroy(&valid); + + return error; + } + switch (base->type) { case OVSDB_TYPE_VOID: NOT_REACHED(); @@ -801,7 +796,7 @@ ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b) } struct ovsdb_datum_sort_cbdata { - const struct ovsdb_type *type; + enum ovsdb_atomic_type key_type; struct ovsdb_datum *datum; }; @@ -812,7 +807,7 @@ ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_) return ovsdb_atom_compare_3way(&cbdata->datum->keys[a], &cbdata->datum->keys[b], - cbdata->type->key.type); + cbdata->key_type); } static void @@ -821,13 +816,13 @@ ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_) struct ovsdb_datum_sort_cbdata *cbdata = cbdata_; ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]); - if (cbdata->type->value.type != OVSDB_TYPE_VOID) { + if (cbdata->datum->values) { ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]); } } struct ovsdb_error * -ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type) +ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type) { if (datum->n < 2) { return NULL; @@ -835,15 +830,15 @@ ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type) struct ovsdb_datum_sort_cbdata cbdata; size_t i; - cbdata.type = type; + cbdata.key_type = key_type; cbdata.datum = datum; sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb, &cbdata); for (i = 0; i < datum->n - 1; i++) { if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1], - type->key.type)) { - if (ovsdb_type_is_map(type)) { + key_type)) { + if (datum->values) { return ovsdb_error(NULL, "map contains duplicate key"); } else { return ovsdb_error(NULL, "set contains duplicate"); @@ -855,6 +850,16 @@ ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type) } } +void +ovsdb_datum_sort_assert(struct ovsdb_datum *datum, + enum ovsdb_atomic_type key_type) +{ + struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type); + if (error) { + NOT_REACHED(); + } +} + /* Checks that each of the atoms in 'datum' conforms to the constraints * specified by its 'type'. Returns an error if a constraint is violated, * otherwise a null pointer. @@ -897,26 +902,17 @@ ovsdb_datum_from_json(struct ovsdb_datum *datum, { struct ovsdb_error *error; - if (ovsdb_type_is_scalar(type)) { - datum->n = 1; - datum->keys = xmalloc(sizeof *datum->keys); - datum->values = NULL; - - error = ovsdb_atom_from_json(&datum->keys[0], &type->key, - json, symtab); - if (error) { - free(datum->keys); - } - return error; - } else { + if (ovsdb_type_is_map(type) + || (json->type == JSON_ARRAY + && json->u.array.n > 0 + && json->u.array.elems[0]->type == JSON_STRING + && !strcmp(json->u.array.elems[0]->u.string, "set"))) { bool is_map = ovsdb_type_is_map(type); const char *class = is_map ? "map" : "set"; const struct json *inner; unsigned int i; size_t n; - assert(is_map || ovsdb_type_is_set(type)); - error = unwrap_json(json, class, JSON_ARRAY, &inner); if (error) { return error; @@ -964,7 +960,7 @@ ovsdb_datum_from_json(struct ovsdb_datum *datum, datum->n++; } - error = ovsdb_datum_sort(datum, type); + error = ovsdb_datum_sort(datum, type->key.type); if (error) { goto error; } @@ -974,6 +970,17 @@ ovsdb_datum_from_json(struct ovsdb_datum *datum, error: ovsdb_datum_destroy(datum, type); return error; + } else { + datum->n = 1; + datum->keys = xmalloc(sizeof *datum->keys); + datum->values = NULL; + + error = ovsdb_atom_from_json(&datum->keys[0], &type->key, + json, symtab); + if (error) { + free(datum->keys); + } + return error; } } @@ -983,7 +990,7 @@ ovsdb_datum_to_json(const struct ovsdb_datum *datum, { /* These tests somewhat tolerate a 'datum' that does not exactly match * 'type', in particular a datum with 'n' not in the allowed range. */ - if (datum->n == 1 && ovsdb_type_is_scalar(type)) { + if (datum->n == 1 && !ovsdb_type_is_map(type)) { return ovsdb_atom_to_json(&datum->keys[0], type->key.type); } else if (type->value.type == OVSDB_TYPE_VOID) { struct json **elems; @@ -1150,7 +1157,7 @@ ovsdb_datum_from_string(struct ovsdb_datum *datum, goto error; } - dberror = ovsdb_datum_sort(datum, type); + dberror = ovsdb_datum_sort(datum, type->key.type); if (dberror) { ovsdb_error_destroy(dberror); if (ovsdb_type_is_map(type)) { @@ -1446,7 +1453,7 @@ ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b, if (n != a->n) { struct ovsdb_error *error; a->n = n; - error = ovsdb_datum_sort(a, type); + error = ovsdb_datum_sort(a, type->key.type); assert(!error); } } @@ -1474,8 +1481,7 @@ ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type, } } if (changed) { - struct ovsdb_error *error = ovsdb_datum_sort(a, a_type); - assert(!error); + ovsdb_datum_sort_assert(a, a_type->key.type); } }