X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fovsdb-data.c;h=b89aa57a6fd742055568ff2ed270f9aa87dfc298;hb=8f7501e83f4aa3d395287ef91f4910e32b55643e;hp=65fd43e693b78c56cae8a8d1bd8c97e082f67925;hpb=c532bf9dd4e684bc583debc9618e3210334f8081;p=openvswitch diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c index 65fd43e6..b89aa57a 100644 --- a/lib/ovsdb-data.c +++ b/lib/ovsdb-data.c @@ -18,8 +18,12 @@ #include "ovsdb-data.h" #include +#include +#include +#include #include +#include "dynamic-string.h" #include "hash.h" #include "ovsdb-error.h" #include "json.h" @@ -351,6 +355,164 @@ ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type) NOT_REACHED(); } } + +/* Initializes 'atom' to a value of the given 'type' parsed from 's', which + * takes one of the following forms: + * + * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign. + * + * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by + * strtod(). + * + * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false", + * "no", "off", or "0" for false. + * + * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise + * an arbitrary string. + * + * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. + * + * Returns a null pointer if successful, otherwise an error message describing + * the problem. The caller is responsible for freeing the error. + */ +char * +ovsdb_atom_from_string(union ovsdb_atom *atom, enum ovsdb_atomic_type type, + const char *s) +{ + switch (type) { + case OVSDB_TYPE_VOID: + NOT_REACHED(); + + case OVSDB_TYPE_INTEGER: { + long long int integer; + if (!str_to_llong(s, 10, &integer)) { + return xasprintf("\"%s\" is not a valid integer", s); + } + atom->integer = integer; + } + break; + + case OVSDB_TYPE_REAL: + if (!str_to_double(s, &atom->real)) { + return xasprintf("\"%s\" is not a valid real number", s); + } + break; + + case OVSDB_TYPE_BOOLEAN: + if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on") + || !strcmp(s, "1")) { + atom->boolean = true; + } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off") + || !strcmp(s, "0")) { + atom->boolean = false; + } else { + return xasprintf("\"%s\" is not a valid boolean " + "(use \"true\" or \"false\")", s); + } + break; + + case OVSDB_TYPE_STRING: + if (*s == '\0') { + return xstrdup("An empty string is not valid as input; " + "use \"\" to represent the empty string"); + } else if (*s == '"') { + size_t s_len = strlen(s); + + if (s_len < 2 || s[s_len - 1] != '"') { + return xasprintf("%s: missing quote at end of " + "quoted string", s); + } else if (!json_string_unescape(s + 1, s_len - 2, + &atom->string)) { + char *error = xasprintf("%s: %s", s, atom->string); + free(atom->string); + return error; + } + } else { + atom->string = xstrdup(s); + } + break; + + case OVSDB_TYPE_UUID: + if (!uuid_from_string(&atom->uuid, s)) { + return xasprintf("\"%s\" is not a valid UUID", s); + } + break; + + case OVSDB_N_TYPES: + default: + NOT_REACHED(); + } + + return NULL; +} + +static bool +string_needs_quotes(const char *s) +{ + const char *p = s; + unsigned char c; + + c = *p++; + if (!isalpha(c) && c != '_') { + return true; + } + + while ((c = *p++) != '\0') { + if (!isalpha(c) && c != '_' && c != '-' && c != '.') { + return true; + } + } + + if (!strcmp(s, "true") || !strcmp(s, "false")) { + return true; + } + + return false; +} + +/* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable + * to ovsdb_atom_from_string(). */ +void +ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, + struct ds *out) +{ + switch (type) { + case OVSDB_TYPE_VOID: + NOT_REACHED(); + + case OVSDB_TYPE_INTEGER: + ds_put_format(out, "%"PRId64, atom->integer); + break; + + case OVSDB_TYPE_REAL: + ds_put_format(out, "%.*g", DBL_DIG, atom->real); + break; + + case OVSDB_TYPE_BOOLEAN: + ds_put_cstr(out, atom->boolean ? "true" : "false"); + break; + + case OVSDB_TYPE_STRING: + if (string_needs_quotes(atom->string)) { + struct json json; + + json.type = JSON_STRING; + json.u.string = atom->string; + json_to_ds(&json, 0, out); + } else { + ds_put_cstr(out, atom->string); + } + break; + + case OVSDB_TYPE_UUID: + ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid)); + break; + + case OVSDB_N_TYPES: + default: + NOT_REACHED(); + } +} static union ovsdb_atom * alloc_default_atoms(enum ovsdb_atomic_type type, size_t n) @@ -371,6 +533,14 @@ alloc_default_atoms(enum ovsdb_atomic_type type, size_t n) } } +void +ovsdb_datum_init_empty(struct ovsdb_datum *datum) +{ + datum->n = 0; + datum->keys = NULL; + datum->values = NULL; +} + void ovsdb_datum_init_default(struct ovsdb_datum *datum, const struct ovsdb_type *type) @@ -635,6 +805,189 @@ ovsdb_datum_to_json(const struct ovsdb_datum *datum, } } +static const char * +skip_spaces(const char *p) +{ + return p + strspn(p, " "); +} + +static char * +parse_atom_token(const char **s, enum ovsdb_atomic_type type, + union ovsdb_atom *atom) +{ + char *token, *error; + + error = ovsdb_token_parse(s, &token); + if (!error) { + error = ovsdb_atom_from_string(atom, type, token); + free(token); + } + return error; +} + + +static char * +parse_key_value(const char **s, const struct ovsdb_type *type, + union ovsdb_atom *key, union ovsdb_atom *value) +{ + const char *start = *s; + char *error; + + error = parse_atom_token(s, type->key_type, key); + if (!error && type->value_type != OVSDB_TYPE_VOID) { + if (**s == '=') { + (*s)++; + error = parse_atom_token(s, type->value_type, value); + } else { + error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"", + start, **s); + } + if (error) { + ovsdb_atom_destroy(key, type->key_type); + } + } + return error; +} + +static void +free_key_value(const struct ovsdb_type *type, + union ovsdb_atom *key, union ovsdb_atom *value) +{ + ovsdb_atom_destroy(key, type->key_type); + if (type->value_type != OVSDB_TYPE_VOID) { + ovsdb_atom_destroy(value, type->value_type); + } +} + +/* Initializes 'datum' as a datum of the given 'type', parsing its contents + * from 's'. The format of 's' is a series of space or comma separated atoms + * or, for a map, '='-delimited pairs of atoms. Each atom must in a format + * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed + * in "[]" or a map in "{}"; for an empty set or map these punctuators are + * required. */ +char * +ovsdb_datum_from_string(struct ovsdb_datum *datum, + const struct ovsdb_type *type, const char *s) +{ + bool is_map = ovsdb_type_is_map(type); + struct ovsdb_error *dberror; + const char *p; + int end_delim; + char *error; + + ovsdb_datum_init_empty(datum); + + /* Swallow a leading delimiter if there is one. */ + p = skip_spaces(s); + if (*p == (is_map ? '{' : '[')) { + end_delim = is_map ? '}' : ']'; + p = skip_spaces(p + 1); + } else if (!*p) { + if (is_map) { + return xstrdup("use \"{}\" to specify the empty map"); + } else { + return xstrdup("use \"[]\" to specify the empty set"); + } + } else { + end_delim = 0; + } + + while (*p && *p != end_delim) { + union ovsdb_atom key, value; + + if (ovsdb_token_is_delim(*p)) { + error = xasprintf("%s: unexpected \"%c\" parsing %s", + s, *p, ovsdb_type_to_english(type)); + goto error; + } + + /* Add to datum. */ + error = parse_key_value(&p, type, &key, &value); + if (error) { + goto error; + } + ovsdb_datum_add_unsafe(datum, &key, &value, type); + free_key_value(type, &key, &value); + + /* Skip optional white space and comma. */ + p = skip_spaces(p); + if (*p == ',') { + p = skip_spaces(p + 1); + } + } + + if (*p != end_delim) { + error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim); + goto error; + } + if (end_delim) { + p = skip_spaces(p + 1); + if (*p) { + error = xasprintf("%s: trailing garbage after \"%c\"", + s, end_delim); + goto error; + } + } + + if (datum->n < type->n_min) { + error = xasprintf("%s: %u %s specified but the minimum number is %u", + s, datum->n, is_map ? "pair(s)" : "value(s)", + type->n_min); + goto error; + } else if (datum->n > type->n_max) { + error = xasprintf("%s: %u %s specified but the maximum number is %u", + s, datum->n, is_map ? "pair(s)" : "value(s)", + type->n_max); + goto error; + } + + dberror = ovsdb_datum_sort(datum, type); + if (dberror) { + ovsdb_error_destroy(dberror); + if (ovsdb_type_is_map(type)) { + error = xasprintf("%s: map contains duplicate key", s); + } else { + error = xasprintf("%s: set contains duplicate value", s); + } + goto error; + } + + return NULL; + +error: + ovsdb_datum_destroy(datum, type); + ovsdb_datum_init_empty(datum); + return error; +} + +/* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable + * to ovsdb_datum_from_string(). */ +void +ovsdb_datum_to_string(const struct ovsdb_datum *datum, + const struct ovsdb_type *type, struct ds *out) +{ + bool is_map = ovsdb_type_is_map(type); + size_t i; + + if (type->n_max > 1 || !datum->n) { + ds_put_char(out, is_map ? '{' : '['); + } + for (i = 0; i < datum->n; i++) { + if (i > 0) { + ds_put_cstr(out, ", "); + } + + ovsdb_atom_to_string(&datum->keys[i], type->key_type, out); + if (is_map) { + ds_put_char(out, '='); + ovsdb_atom_to_string(&datum->values[i], type->value_type, out); + } + } + if (type->n_max > 1 || !datum->n) { + ds_put_char(out, is_map ? '}' : ']'); + } +} + static uint32_t hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms, unsigned int n, uint32_t basis) @@ -661,9 +1014,9 @@ ovsdb_datum_hash(const struct ovsdb_datum *datum, static int atom_arrays_compare_3way(const union ovsdb_atom *a, - const union ovsdb_atom *b, - enum ovsdb_atomic_type type, - size_t n) + const union ovsdb_atom *b, + enum ovsdb_atomic_type type, + size_t n) { unsigned int i; @@ -706,6 +1059,52 @@ ovsdb_datum_compare_3way(const struct ovsdb_datum *a, a->n)); } +/* If 'key' is one of the keys in 'datum', returns its index within 'datum', + * otherwise UINT_MAX. 'key_type' must be the type of the atoms stored in the + * 'keys' array in 'datum'. + */ +unsigned int +ovsdb_datum_find_key(const struct ovsdb_datum *datum, + const union ovsdb_atom *key, + enum ovsdb_atomic_type key_type) +{ + unsigned int low = 0; + unsigned int high = datum->n; + while (low < high) { + unsigned int idx = (low + high) / 2; + int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type); + if (cmp < 0) { + high = idx; + } else if (cmp > 0) { + low = idx + 1; + } else { + return idx; + } + } + return UINT_MAX; +} + +/* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its + * index within 'datum', otherwise UINT_MAX. 'key_type' must be the type of + * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the + * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys. + */ +unsigned int +ovsdb_datum_find_key_value(const struct ovsdb_datum *datum, + const union ovsdb_atom *key, + enum ovsdb_atomic_type key_type, + const union ovsdb_atom *value, + enum ovsdb_atomic_type value_type) +{ + unsigned int idx = ovsdb_datum_find_key(datum, key, key_type); + if (idx != UINT_MAX + && value_type != OVSDB_TYPE_VOID + && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) { + idx = UINT_MAX; + } + return idx; +} + /* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise * UINT_MAX. 'type' must be the type of 'a' and 'b', except that * type->value_type may be set to OVSDB_TYPE_VOID to compare keys but not @@ -715,24 +1114,10 @@ ovsdb_datum_find(const struct ovsdb_datum *a, int i, const struct ovsdb_datum *b, const struct ovsdb_type *type) { - int low = 0; - int high = b->n; - while (low < high) { - int j = (low + high) / 2; - int cmp = ovsdb_atom_compare_3way(&a->keys[i], &b->keys[j], - type->key_type); - if (cmp < 0) { - high = j; - } else if (cmp > 0) { - low = j + 1; - } else { - bool eq_value = (type->value_type == OVSDB_TYPE_VOID - || ovsdb_atom_equals(&a->values[i], &b->values[j], - type->value_type)); - return eq_value ? j : UINT_MAX; - } - } - return UINT_MAX; + return ovsdb_datum_find_key_value(b, + &a->keys[i], type->key_type, + a->values ? &a->values[i] : NULL, + type->value_type); } /* Returns true if every element in 'a' is also in 'b', false otherwise. */ @@ -777,41 +1162,76 @@ ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type, } } -static void -ovsdb_datum_remove(struct ovsdb_datum *a, size_t i, - const struct ovsdb_type *type) +/* Removes the element with index 'idx' from 'datum', which has type 'type'. + * If 'idx' is not the last element in 'datum', then the removed element is + * replaced by the (former) last element. + * + * This function does not maintain ovsdb_datum invariants. Use + * ovsdb_datum_sort() to check and restore these invariants. */ +void +ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx, + const struct ovsdb_type *type) +{ + ovsdb_atom_destroy(&datum->keys[idx], type->key_type); + datum->keys[idx] = datum->keys[datum->n - 1]; + if (type->value_type != OVSDB_TYPE_VOID) { + ovsdb_atom_destroy(&datum->values[idx], type->value_type); + datum->values[idx] = datum->values[datum->n - 1]; + } + datum->n--; +} + +/* Adds the element with the given 'key' and 'value' to 'datum', which must + * have the specified 'type'. + * + * This function always allocates memory, so it is not an efficient way to add + * a number of elements to a datum. + * + * This function does not maintain ovsdb_datum invariants. Use + * ovsdb_datum_sort() to check and restore these invariants. (But a datum with + * 0 or 1 elements cannot violate the invariants anyhow.) */ +void +ovsdb_datum_add_unsafe(struct ovsdb_datum *datum, + const union ovsdb_atom *key, + const union ovsdb_atom *value, + const struct ovsdb_type *type) { - ovsdb_atom_destroy(&a->keys[i], type->key_type); - a->keys[i] = a->keys[a->n - 1]; + size_t idx = datum->n++; + datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys); + ovsdb_atom_clone(&datum->keys[idx], key, type->key_type); if (type->value_type != OVSDB_TYPE_VOID) { - ovsdb_atom_destroy(&a->values[i], type->value_type); - a->values[i] = a->values[a->n - 1]; + datum->values = xrealloc(datum->values, + datum->n * sizeof *datum->values); + ovsdb_atom_clone(&datum->values[idx], value, type->value_type); } - a->n--; } void -ovsdb_datum_union(struct ovsdb_datum *a, - const struct ovsdb_datum *b, const struct ovsdb_type *type) +ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b, + const struct ovsdb_type *type, bool replace) { - struct ovsdb_type type_without_value; unsigned int n; - size_t i; + size_t bi; - type_without_value = *type; - type_without_value.value_type = OVSDB_TYPE_VOID; n = a->n; - for (i = 0; i < b->n; i++) { - if (ovsdb_datum_find(b, i, a, &type_without_value) == UINT_MAX) { + for (bi = 0; bi < b->n; bi++) { + unsigned int ai; + + ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key_type); + if (ai == UINT_MAX) { if (n == a->n) { - ovsdb_datum_reallocate(a, type, a->n + (b->n - i)); + ovsdb_datum_reallocate(a, type, a->n + (b->n - bi)); } - ovsdb_atom_clone(&a->keys[n], &b->keys[i], type->key_type); + ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key_type); if (type->value_type != OVSDB_TYPE_VOID) { - ovsdb_atom_clone(&a->values[n], &b->values[i], + ovsdb_atom_clone(&a->values[n], &b->values[bi], type->value_type); } n++; + } else if (replace && type->value_type != OVSDB_TYPE_VOID) { + ovsdb_atom_destroy(&a->values[ai], type->value_type); + ovsdb_atom_clone(&a->values[ai], &b->values[bi], + type->value_type); } } if (n != a->n) { @@ -839,7 +1259,7 @@ ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type, unsigned int idx = ovsdb_datum_find(a, i, b, b_type); if (idx != UINT_MAX) { changed = true; - ovsdb_datum_remove(a, i, a_type); + ovsdb_datum_remove_unsafe(a, i, a_type); } else { i++; } @@ -897,3 +1317,69 @@ ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name, symbol->used = used; shash_add(&symtab->sh, name, symbol); } + +/* Extracts a token from the beginning of 's' and returns a pointer just after + * the token. Stores the token itself into '*outp', which the caller is + * responsible for freeing (with free()). + * + * If 's[0]' is a delimiter, the returned token is the empty string. + * + * A token extends from 's' to the first delimiter, as defined by + * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be + * escaped with a backslash, in which case the backslash does not appear in the + * output. Double quotes also cause delimiters to be ignored, but the double + * quotes are retained in the output. (Backslashes inside double quotes are + * not removed, either.) + */ +char * +ovsdb_token_parse(const char **s, char **outp) +{ + const char *p; + struct ds out; + bool in_quotes; + char *error; + + ds_init(&out); + in_quotes = false; + for (p = *s; *p != '\0'; ) { + int c = *p++; + if (c == '\\') { + if (in_quotes) { + ds_put_char(&out, '\\'); + } + if (!*p) { + error = xasprintf("%s: backslash at end of argument", *s); + goto error; + } + ds_put_char(&out, *p++); + } else if (!in_quotes && ovsdb_token_is_delim(c)) { + p--; + break; + } else { + ds_put_char(&out, c); + if (c == '"') { + in_quotes = !in_quotes; + } + } + } + if (in_quotes) { + error = xasprintf("%s: quoted string extends past end of argument", + *s); + goto error; + } + *outp = ds_cstr(&out); + *s = p; + return NULL; + +error: + ds_destroy(&out); + *outp = NULL; + return error; +} + +/* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */ +bool +ovsdb_token_is_delim(unsigned char c) +{ + return strchr(":=, []{}", c) != NULL; +}