]>
Commit | Line | Data |
---|---|---|
3865965d | 1 | /* Copyright (c) 2009, 2010, 2011, 2012, 2014, 2016, 2017 Nicira, Inc. |
f85f8ebb BP |
2 | * |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | * you may not use this file except in compliance with the License. | |
5 | * You may obtain a copy of the License at: | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
9 | * Unless required by applicable law or agreed to in writing, software | |
10 | * distributed under the License is distributed on an "AS IS" BASIS, | |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | * See the License for the specific language governing permissions and | |
13 | * limitations under the License. | |
14 | */ | |
15 | ||
16 | #include <config.h> | |
17 | ||
18 | #include "ovsdb-data.h" | |
19 | ||
0194f33a BP |
20 | #include <ctype.h> |
21 | #include <float.h> | |
22 | #include <inttypes.h> | |
e9f8f936 | 23 | #include <limits.h> |
f85f8ebb | 24 | |
3e8a2ad1 | 25 | #include "openvswitch/dynamic-string.h" |
f85f8ebb | 26 | #include "hash.h" |
21a0e105 | 27 | #include "ovs-thread.h" |
f85f8ebb | 28 | #include "ovsdb-error.h" |
d198c4be | 29 | #include "ovsdb-parser.h" |
ee89ea7b TW |
30 | #include "openvswitch/json.h" |
31 | #include "openvswitch/shash.h" | |
57c8677b | 32 | #include "smap.h" |
f85f8ebb | 33 | #include "sort.h" |
bd76d25d | 34 | #include "unicode.h" |
ee89ea7b | 35 | #include "util.h" |
f85f8ebb BP |
36 | |
37 | static struct json * | |
38 | wrap_json(const char *name, struct json *wrapped) | |
39 | { | |
40 | return json_array_create_2(json_string_create(name), wrapped); | |
41 | } | |
42 | ||
5413de95 BP |
43 | /* Initializes 'atom' with the default value of the given 'type'. |
44 | * | |
c70a7767 | 45 | * The default value for an atom is as defined in RFC 7047: |
5413de95 BP |
46 | * |
47 | * - "integer" or "real": 0 | |
48 | * | |
49 | * - "boolean": false | |
50 | * | |
51 | * - "string": "" (the empty string) | |
52 | * | |
53 | * - "uuid": 00000000-0000-0000-0000-000000000000 | |
54 | * | |
55 | * The caller must eventually arrange for 'atom' to be destroyed (with | |
56 | * ovsdb_atom_destroy()). */ | |
f85f8ebb BP |
57 | void |
58 | ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type) | |
59 | { | |
60 | switch (type) { | |
61 | case OVSDB_TYPE_VOID: | |
428b2edd | 62 | OVS_NOT_REACHED(); |
f85f8ebb BP |
63 | |
64 | case OVSDB_TYPE_INTEGER: | |
65 | atom->integer = 0; | |
66 | break; | |
67 | ||
68 | case OVSDB_TYPE_REAL: | |
69 | atom->real = 0.0; | |
70 | break; | |
71 | ||
72 | case OVSDB_TYPE_BOOLEAN: | |
73 | atom->boolean = false; | |
74 | break; | |
75 | ||
76 | case OVSDB_TYPE_STRING: | |
77 | atom->string = xmemdup("", 1); | |
78 | break; | |
79 | ||
80 | case OVSDB_TYPE_UUID: | |
81 | uuid_zero(&atom->uuid); | |
82 | break; | |
83 | ||
84 | case OVSDB_N_TYPES: | |
85 | default: | |
428b2edd | 86 | OVS_NOT_REACHED(); |
f85f8ebb BP |
87 | } |
88 | } | |
89 | ||
958ac03a BP |
90 | /* Returns a read-only atom of the given 'type' that has the default value for |
91 | * 'type'. The caller must not modify or free the returned atom. | |
92 | * | |
93 | * See ovsdb_atom_init_default() for an explanation of the default value of an | |
94 | * atom. */ | |
95 | const union ovsdb_atom * | |
96 | ovsdb_atom_default(enum ovsdb_atomic_type type) | |
97 | { | |
98 | static union ovsdb_atom default_atoms[OVSDB_N_TYPES]; | |
21a0e105 | 99 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
958ac03a | 100 | |
21a0e105 | 101 | if (ovsthread_once_start(&once)) { |
958ac03a BP |
102 | int i; |
103 | ||
104 | for (i = 0; i < OVSDB_N_TYPES; i++) { | |
105 | if (i != OVSDB_TYPE_VOID) { | |
106 | ovsdb_atom_init_default(&default_atoms[i], i); | |
107 | } | |
108 | } | |
21a0e105 | 109 | ovsthread_once_done(&once); |
958ac03a BP |
110 | } |
111 | ||
cb22974d | 112 | ovs_assert(ovsdb_atomic_type_is_valid(type)); |
958ac03a BP |
113 | return &default_atoms[type]; |
114 | } | |
5413de95 BP |
115 | |
116 | /* Returns true if 'atom', which must have the given 'type', has the default | |
117 | * value for that type. | |
118 | * | |
119 | * See ovsdb_atom_init_default() for an explanation of the default value of an | |
120 | * atom. */ | |
c532bf9d BP |
121 | bool |
122 | ovsdb_atom_is_default(const union ovsdb_atom *atom, | |
123 | enum ovsdb_atomic_type type) | |
124 | { | |
125 | switch (type) { | |
126 | case OVSDB_TYPE_VOID: | |
428b2edd | 127 | OVS_NOT_REACHED(); |
c532bf9d BP |
128 | |
129 | case OVSDB_TYPE_INTEGER: | |
130 | return atom->integer == 0; | |
131 | ||
132 | case OVSDB_TYPE_REAL: | |
133 | return atom->real == 0.0; | |
134 | ||
135 | case OVSDB_TYPE_BOOLEAN: | |
136 | return atom->boolean == false; | |
137 | ||
138 | case OVSDB_TYPE_STRING: | |
139 | return atom->string[0] == '\0'; | |
140 | ||
141 | case OVSDB_TYPE_UUID: | |
142 | return uuid_is_zero(&atom->uuid); | |
143 | ||
144 | case OVSDB_N_TYPES: | |
145 | default: | |
428b2edd | 146 | OVS_NOT_REACHED(); |
c532bf9d BP |
147 | } |
148 | } | |
149 | ||
5413de95 BP |
150 | /* Initializes 'new' as a copy of 'old', with the given 'type'. |
151 | * | |
152 | * The caller must eventually arrange for 'new' to be destroyed (with | |
153 | * ovsdb_atom_destroy()). */ | |
f85f8ebb BP |
154 | void |
155 | ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old, | |
156 | enum ovsdb_atomic_type type) | |
157 | { | |
158 | switch (type) { | |
159 | case OVSDB_TYPE_VOID: | |
428b2edd | 160 | OVS_NOT_REACHED(); |
f85f8ebb BP |
161 | |
162 | case OVSDB_TYPE_INTEGER: | |
163 | new->integer = old->integer; | |
164 | break; | |
165 | ||
166 | case OVSDB_TYPE_REAL: | |
167 | new->real = old->real; | |
168 | break; | |
169 | ||
170 | case OVSDB_TYPE_BOOLEAN: | |
171 | new->boolean = old->boolean; | |
172 | break; | |
173 | ||
174 | case OVSDB_TYPE_STRING: | |
175 | new->string = xstrdup(old->string); | |
176 | break; | |
177 | ||
178 | case OVSDB_TYPE_UUID: | |
179 | new->uuid = old->uuid; | |
180 | break; | |
181 | ||
182 | case OVSDB_N_TYPES: | |
183 | default: | |
428b2edd | 184 | OVS_NOT_REACHED(); |
f85f8ebb BP |
185 | } |
186 | } | |
187 | ||
5413de95 | 188 | /* Swaps the contents of 'a' and 'b', which need not have the same type. */ |
f85f8ebb BP |
189 | void |
190 | ovsdb_atom_swap(union ovsdb_atom *a, union ovsdb_atom *b) | |
191 | { | |
192 | union ovsdb_atom tmp = *a; | |
193 | *a = *b; | |
194 | *b = tmp; | |
195 | } | |
196 | ||
5413de95 BP |
197 | /* Returns a hash value for 'atom', which has the specified 'type', folding |
198 | * 'basis' into the calculation. */ | |
f85f8ebb BP |
199 | uint32_t |
200 | ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, | |
201 | uint32_t basis) | |
202 | { | |
203 | switch (type) { | |
204 | case OVSDB_TYPE_VOID: | |
428b2edd | 205 | OVS_NOT_REACHED(); |
f85f8ebb BP |
206 | |
207 | case OVSDB_TYPE_INTEGER: | |
208 | return hash_int(atom->integer, basis); | |
209 | ||
210 | case OVSDB_TYPE_REAL: | |
211 | return hash_double(atom->real, basis); | |
212 | ||
213 | case OVSDB_TYPE_BOOLEAN: | |
214 | return hash_boolean(atom->boolean, basis); | |
215 | ||
216 | case OVSDB_TYPE_STRING: | |
217 | return hash_string(atom->string, basis); | |
218 | ||
219 | case OVSDB_TYPE_UUID: | |
220 | return hash_int(uuid_hash(&atom->uuid), basis); | |
221 | ||
222 | case OVSDB_N_TYPES: | |
223 | default: | |
428b2edd | 224 | OVS_NOT_REACHED(); |
f85f8ebb BP |
225 | } |
226 | } | |
227 | ||
5413de95 BP |
228 | /* Compares 'a' and 'b', which both have type 'type', and returns a |
229 | * strcmp()-like result. */ | |
f85f8ebb BP |
230 | int |
231 | ovsdb_atom_compare_3way(const union ovsdb_atom *a, | |
232 | const union ovsdb_atom *b, | |
233 | enum ovsdb_atomic_type type) | |
234 | { | |
235 | switch (type) { | |
236 | case OVSDB_TYPE_VOID: | |
428b2edd | 237 | OVS_NOT_REACHED(); |
f85f8ebb BP |
238 | |
239 | case OVSDB_TYPE_INTEGER: | |
240 | return a->integer < b->integer ? -1 : a->integer > b->integer; | |
241 | ||
242 | case OVSDB_TYPE_REAL: | |
243 | return a->real < b->real ? -1 : a->real > b->real; | |
244 | ||
245 | case OVSDB_TYPE_BOOLEAN: | |
246 | return a->boolean - b->boolean; | |
247 | ||
248 | case OVSDB_TYPE_STRING: | |
249 | return strcmp(a->string, b->string); | |
250 | ||
251 | case OVSDB_TYPE_UUID: | |
252 | return uuid_compare_3way(&a->uuid, &b->uuid); | |
253 | ||
254 | case OVSDB_N_TYPES: | |
255 | default: | |
428b2edd | 256 | OVS_NOT_REACHED(); |
f85f8ebb BP |
257 | } |
258 | } | |
259 | ||
260 | static struct ovsdb_error * | |
261 | unwrap_json(const struct json *json, const char *name, | |
262 | enum json_type value_type, const struct json **value) | |
263 | { | |
264 | if (json->type != JSON_ARRAY | |
fa37affa BP |
265 | || json->array.n != 2 |
266 | || json->array.elems[0]->type != JSON_STRING | |
267 | || (name && strcmp(json->array.elems[0]->string, name)) | |
268 | || json->array.elems[1]->type != value_type) | |
f85f8ebb | 269 | { |
829d41bd | 270 | *value = NULL; |
f85f8ebb BP |
271 | return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name, |
272 | json_type_to_string(value_type)); | |
273 | } | |
fa37affa | 274 | *value = json->array.elems[1]; |
f85f8ebb BP |
275 | return NULL; |
276 | } | |
277 | ||
278 | static struct ovsdb_error * | |
279 | parse_json_pair(const struct json *json, | |
280 | const struct json **elem0, const struct json **elem1) | |
281 | { | |
fa37affa | 282 | if (json->type != JSON_ARRAY || json->array.n != 2) { |
f85f8ebb BP |
283 | return ovsdb_syntax_error(json, NULL, "expected 2-element array"); |
284 | } | |
fa37affa BP |
285 | *elem0 = json->array.elems[0]; |
286 | *elem1 = json->array.elems[1]; | |
f85f8ebb BP |
287 | return NULL; |
288 | } | |
289 | ||
c5f341ab BP |
290 | static void |
291 | ovsdb_symbol_referenced(struct ovsdb_symbol *symbol, | |
292 | const struct ovsdb_base_type *base) | |
293 | { | |
cb22974d | 294 | ovs_assert(base->type == OVSDB_TYPE_UUID); |
c5f341ab | 295 | |
fa37affa BP |
296 | if (base->uuid.refTableName) { |
297 | switch (base->uuid.refType) { | |
c5f341ab BP |
298 | case OVSDB_REF_STRONG: |
299 | symbol->strong_ref = true; | |
300 | break; | |
301 | case OVSDB_REF_WEAK: | |
302 | symbol->weak_ref = true; | |
303 | break; | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
1ab39058 LR |
308 | static union ovsdb_atom * |
309 | alloc_default_atoms(enum ovsdb_atomic_type type, size_t n) | |
310 | { | |
311 | if (type != OVSDB_TYPE_VOID && n) { | |
312 | union ovsdb_atom *atoms; | |
313 | unsigned int i; | |
314 | ||
315 | atoms = xmalloc(n * sizeof *atoms); | |
316 | for (i = 0; i < n; i++) { | |
317 | ovsdb_atom_init_default(&atoms[i], type); | |
318 | } | |
319 | return atoms; | |
320 | } else { | |
321 | /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is | |
322 | * treated as xmalloc(1). */ | |
323 | return NULL; | |
324 | } | |
325 | } | |
326 | ||
cab50449 | 327 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
f85f8ebb | 328 | ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, |
c5f341ab BP |
329 | struct ovsdb_symbol_table *symtab, |
330 | const struct ovsdb_base_type *base) | |
f85f8ebb BP |
331 | { |
332 | struct ovsdb_error *error0; | |
333 | const struct json *value; | |
334 | ||
335 | error0 = unwrap_json(json, "uuid", JSON_STRING, &value); | |
336 | if (!error0) { | |
337 | const char *uuid_string = json_string(value); | |
338 | if (!uuid_from_string(uuid, uuid_string)) { | |
339 | return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID", | |
340 | uuid_string); | |
341 | } | |
342 | } else if (symtab) { | |
343 | struct ovsdb_error *error1; | |
344 | ||
345 | error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value); | |
346 | if (!error1) { | |
c5f341ab | 347 | struct ovsdb_symbol *symbol; |
f85f8ebb BP |
348 | |
349 | ovsdb_error_destroy(error0); | |
d198c4be BP |
350 | if (!ovsdb_parser_is_id(json_string(value))) { |
351 | return ovsdb_syntax_error(json, NULL, "named-uuid string is " | |
352 | "not a valid <id>"); | |
353 | } | |
c5f341ab BP |
354 | |
355 | symbol = ovsdb_symbol_table_insert(symtab, json_string(value)); | |
356 | *uuid = symbol->uuid; | |
357 | ovsdb_symbol_referenced(symbol, base); | |
fbf925e4 | 358 | return NULL; |
f85f8ebb BP |
359 | } |
360 | ovsdb_error_destroy(error1); | |
361 | } | |
362 | ||
363 | return error0; | |
364 | } | |
365 | ||
cab50449 | 366 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
c5f341ab BP |
367 | ovsdb_atom_from_json__(union ovsdb_atom *atom, |
368 | const struct ovsdb_base_type *base, | |
bd76d25d | 369 | const struct json *json, |
fbf925e4 | 370 | struct ovsdb_symbol_table *symtab) |
f85f8ebb | 371 | { |
c5f341ab BP |
372 | enum ovsdb_atomic_type type = base->type; |
373 | ||
f85f8ebb BP |
374 | switch (type) { |
375 | case OVSDB_TYPE_VOID: | |
428b2edd | 376 | OVS_NOT_REACHED(); |
f85f8ebb BP |
377 | |
378 | case OVSDB_TYPE_INTEGER: | |
379 | if (json->type == JSON_INTEGER) { | |
fa37affa | 380 | atom->integer = json->integer; |
f85f8ebb BP |
381 | return NULL; |
382 | } | |
383 | break; | |
384 | ||
385 | case OVSDB_TYPE_REAL: | |
386 | if (json->type == JSON_INTEGER) { | |
fa37affa | 387 | atom->real = json->integer; |
f85f8ebb BP |
388 | return NULL; |
389 | } else if (json->type == JSON_REAL) { | |
fa37affa | 390 | atom->real = json->real; |
f85f8ebb BP |
391 | return NULL; |
392 | } | |
393 | break; | |
394 | ||
395 | case OVSDB_TYPE_BOOLEAN: | |
396 | if (json->type == JSON_TRUE) { | |
397 | atom->boolean = true; | |
398 | return NULL; | |
399 | } else if (json->type == JSON_FALSE) { | |
400 | atom->boolean = false; | |
401 | return NULL; | |
402 | } | |
403 | break; | |
404 | ||
405 | case OVSDB_TYPE_STRING: | |
406 | if (json->type == JSON_STRING) { | |
fa37affa | 407 | atom->string = xstrdup(json->string); |
f85f8ebb BP |
408 | return NULL; |
409 | } | |
410 | break; | |
411 | ||
412 | case OVSDB_TYPE_UUID: | |
c5f341ab | 413 | return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab, base); |
f85f8ebb BP |
414 | |
415 | case OVSDB_N_TYPES: | |
416 | default: | |
428b2edd | 417 | OVS_NOT_REACHED(); |
f85f8ebb BP |
418 | } |
419 | ||
420 | return ovsdb_syntax_error(json, NULL, "expected %s", | |
421 | ovsdb_atomic_type_to_string(type)); | |
422 | } | |
423 | ||
5413de95 BP |
424 | /* Parses 'json' as an atom of the type described by 'base'. If successful, |
425 | * returns NULL and initializes 'atom' with the parsed atom. On failure, | |
426 | * returns an error and the contents of 'atom' are indeterminate. The caller | |
427 | * is responsible for freeing the error or the atom that is returned. | |
428 | * | |
958ac03a BP |
429 | * Violations of constraints expressed by 'base' are treated as errors. |
430 | * | |
5413de95 | 431 | * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to |
c70a7767 BP |
432 | * RFC 7047 for information about this, and for the syntax that this function |
433 | * accepts. If 'base' is a reference and a symbol is parsed, then the symbol's | |
434 | * 'strong_ref' or 'weak_ref' member is set to true, as appropriate. */ | |
bd76d25d BP |
435 | struct ovsdb_error * |
436 | ovsdb_atom_from_json(union ovsdb_atom *atom, | |
437 | const struct ovsdb_base_type *base, | |
438 | const struct json *json, | |
fbf925e4 | 439 | struct ovsdb_symbol_table *symtab) |
bd76d25d BP |
440 | { |
441 | struct ovsdb_error *error; | |
442 | ||
c5f341ab | 443 | error = ovsdb_atom_from_json__(atom, base, json, symtab); |
bd76d25d BP |
444 | if (error) { |
445 | return error; | |
446 | } | |
447 | ||
448 | error = ovsdb_atom_check_constraints(atom, base); | |
449 | if (error) { | |
450 | ovsdb_atom_destroy(atom, base->type); | |
451 | } | |
452 | return error; | |
453 | } | |
454 | ||
5413de95 | 455 | /* Converts 'atom', of the specified 'type', to JSON format, and returns the |
958ac03a BP |
456 | * JSON. The caller is responsible for freeing the returned JSON. |
457 | * | |
c70a7767 | 458 | * Refer to RFC 7047 for the format of the JSON that this function produces. */ |
f85f8ebb BP |
459 | struct json * |
460 | ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type) | |
461 | { | |
462 | switch (type) { | |
463 | case OVSDB_TYPE_VOID: | |
428b2edd | 464 | OVS_NOT_REACHED(); |
f85f8ebb BP |
465 | |
466 | case OVSDB_TYPE_INTEGER: | |
467 | return json_integer_create(atom->integer); | |
468 | ||
469 | case OVSDB_TYPE_REAL: | |
470 | return json_real_create(atom->real); | |
471 | ||
472 | case OVSDB_TYPE_BOOLEAN: | |
473 | return json_boolean_create(atom->boolean); | |
474 | ||
475 | case OVSDB_TYPE_STRING: | |
476 | return json_string_create(atom->string); | |
477 | ||
478 | case OVSDB_TYPE_UUID: | |
479 | return wrap_json("uuid", json_string_create_nocopy( | |
480 | xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid)))); | |
481 | ||
482 | case OVSDB_N_TYPES: | |
483 | default: | |
428b2edd | 484 | OVS_NOT_REACHED(); |
f85f8ebb BP |
485 | } |
486 | } | |
0194f33a | 487 | |
bd76d25d | 488 | static char * |
c5f341ab | 489 | ovsdb_atom_from_string__(union ovsdb_atom *atom, |
1ab39058 | 490 | union ovsdb_atom **range_end_atom, |
c5f341ab BP |
491 | const struct ovsdb_base_type *base, const char *s, |
492 | struct ovsdb_symbol_table *symtab) | |
0194f33a | 493 | { |
c5f341ab BP |
494 | enum ovsdb_atomic_type type = base->type; |
495 | ||
0194f33a BP |
496 | switch (type) { |
497 | case OVSDB_TYPE_VOID: | |
428b2edd | 498 | OVS_NOT_REACHED(); |
0194f33a BP |
499 | |
500 | case OVSDB_TYPE_INTEGER: { | |
1ab39058 LR |
501 | long long int integer, end; |
502 | if (range_end_atom | |
503 | && str_to_llong_range(s, 10, &integer, &end)) { | |
504 | if (end < integer) { | |
505 | return xasprintf("\"%s\" is not a valid range. " | |
506 | "Range end cannot be before start.", s); | |
507 | } | |
508 | *range_end_atom = alloc_default_atoms(type, 1); | |
509 | if (!(*range_end_atom)) { | |
510 | return xasprintf("\"%s\" is not a valid range", s); | |
511 | } | |
512 | (*range_end_atom)->integer = end; | |
513 | } else if (!str_to_llong(s, 10, &integer)) { | |
514 | return xasprintf("\"%s\" is not a valid integer or range", s); | |
0194f33a BP |
515 | } |
516 | atom->integer = integer; | |
517 | } | |
518 | break; | |
519 | ||
520 | case OVSDB_TYPE_REAL: | |
521 | if (!str_to_double(s, &atom->real)) { | |
1bc6ff29 | 522 | return xasprintf("\"%s\" is not a valid real number", s); |
0194f33a | 523 | } |
54a687fd BP |
524 | /* Our JSON input routines map negative zero to zero, so do that here |
525 | * too for consistency. */ | |
526 | if (atom->real == 0.0) { | |
527 | atom->real = 0.0; | |
528 | } | |
0194f33a BP |
529 | break; |
530 | ||
531 | case OVSDB_TYPE_BOOLEAN: | |
532 | if (!strcmp(s, "true") || !strcmp(s, "yes") || !strcmp(s, "on") | |
533 | || !strcmp(s, "1")) { | |
534 | atom->boolean = true; | |
535 | } else if (!strcmp(s, "false") || !strcmp(s, "no") || !strcmp(s, "off") | |
536 | || !strcmp(s, "0")) { | |
537 | atom->boolean = false; | |
538 | } else { | |
1bc6ff29 BP |
539 | return xasprintf("\"%s\" is not a valid boolean " |
540 | "(use \"true\" or \"false\")", s); | |
0194f33a BP |
541 | } |
542 | break; | |
543 | ||
544 | case OVSDB_TYPE_STRING: | |
545 | if (*s == '\0') { | |
1bc6ff29 BP |
546 | return xstrdup("An empty string is not valid as input; " |
547 | "use \"\" to represent the empty string"); | |
0194f33a BP |
548 | } else if (*s == '"') { |
549 | size_t s_len = strlen(s); | |
550 | ||
551 | if (s_len < 2 || s[s_len - 1] != '"') { | |
1bc6ff29 BP |
552 | return xasprintf("%s: missing quote at end of " |
553 | "quoted string", s); | |
0194f33a BP |
554 | } else if (!json_string_unescape(s + 1, s_len - 2, |
555 | &atom->string)) { | |
1bc6ff29 BP |
556 | char *error = xasprintf("%s: %s", s, atom->string); |
557 | free(atom->string); | |
558 | return error; | |
0194f33a BP |
559 | } |
560 | } else { | |
561 | atom->string = xstrdup(s); | |
562 | } | |
563 | break; | |
564 | ||
565 | case OVSDB_TYPE_UUID: | |
ce5a3e38 | 566 | if (*s == '@') { |
c5f341ab BP |
567 | struct ovsdb_symbol *symbol = ovsdb_symbol_table_insert(symtab, s); |
568 | atom->uuid = symbol->uuid; | |
569 | ovsdb_symbol_referenced(symbol, base); | |
ce5a3e38 | 570 | } else if (!uuid_from_string(&atom->uuid, s)) { |
1bc6ff29 | 571 | return xasprintf("\"%s\" is not a valid UUID", s); |
0194f33a BP |
572 | } |
573 | break; | |
574 | ||
575 | case OVSDB_N_TYPES: | |
576 | default: | |
428b2edd | 577 | OVS_NOT_REACHED(); |
0194f33a | 578 | } |
1bc6ff29 BP |
579 | |
580 | return NULL; | |
0194f33a BP |
581 | } |
582 | ||
1ab39058 LR |
583 | /* Initializes 'atom' and optionally 'range_end_atom' to a value of type 'base' |
584 | * parsed from 's', which takes one of the following forms: | |
bd76d25d | 585 | * |
1ab39058 LR |
586 | * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign |
587 | * or two decimal integers optionally preceded by a sign and separated | |
588 | * by a hyphen, representing inclusive range of integers | |
589 | * ['atom', 'range_end_atom']. | |
bd76d25d BP |
590 | * |
591 | * - OVSDB_TYPE_REAL: A floating-point number in the format accepted by | |
592 | * strtod(). | |
593 | * | |
594 | * - OVSDB_TYPE_BOOLEAN: "true", "yes", "on", "1" for true, or "false", | |
595 | * "no", "off", or "0" for false. | |
596 | * | |
597 | * - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise | |
598 | * an arbitrary string. | |
599 | * | |
ce5a3e38 BP |
600 | * - OVSDB_TYPE_UUID: A UUID in RFC 4122 format. If 'symtab' is nonnull, |
601 | * then an identifier beginning with '@' is also acceptable. If the | |
602 | * named identifier is already in 'symtab', then the associated UUID is | |
603 | * used; otherwise, a new, random UUID is used and added to the symbol | |
c5f341ab BP |
604 | * table. If 'base' is a reference and a symbol is parsed, then the |
605 | * symbol's 'strong_ref' or 'weak_ref' member is set to true, as | |
606 | * appropriate. | |
bd76d25d BP |
607 | * |
608 | * Returns a null pointer if successful, otherwise an error message describing | |
5413de95 BP |
609 | * the problem. On failure, the contents of 'atom' are indeterminate. The |
610 | * caller is responsible for freeing the atom or the error. | |
1ab39058 LR |
611 | * |
612 | * Does not attempt to parse range if 'range_end_atom' is a null pointer. | |
613 | * Dynamically allocates ovdsb_atom and stores its address in '*range_end_atom' | |
614 | * if successfully parses range. Caller is responsible for deallocating | |
615 | * the memory by calling 'ovsdb_atom_destroy' and then 'free' on the address. | |
616 | * Does not allocate memory and sets '*range_end_atom' to a null pointer | |
617 | * if does not parse a range or fails for any reason. | |
bd76d25d BP |
618 | */ |
619 | char * | |
620 | ovsdb_atom_from_string(union ovsdb_atom *atom, | |
1ab39058 | 621 | union ovsdb_atom **range_end_atom, |
ce5a3e38 BP |
622 | const struct ovsdb_base_type *base, const char *s, |
623 | struct ovsdb_symbol_table *symtab) | |
bd76d25d BP |
624 | { |
625 | struct ovsdb_error *error; | |
626 | char *msg; | |
627 | ||
1ab39058 LR |
628 | if (range_end_atom) { |
629 | *range_end_atom = NULL; | |
630 | } | |
631 | ||
632 | msg = ovsdb_atom_from_string__(atom, range_end_atom, base, s, symtab); | |
bd76d25d BP |
633 | if (msg) { |
634 | return msg; | |
635 | } | |
636 | ||
637 | error = ovsdb_atom_check_constraints(atom, base); | |
1ab39058 LR |
638 | |
639 | if (!error && range_end_atom && *range_end_atom) { | |
640 | /* Check range constraints */ | |
641 | int64_t start = atom->integer; | |
642 | int64_t end = (*range_end_atom)->integer; | |
643 | if (base->enum_) { | |
644 | for (int64_t i = start + 1; i <= end; i++) { | |
645 | union ovsdb_atom ai = { .integer = i }; | |
646 | error = ovsdb_atom_check_constraints(&ai, base); | |
647 | if (error) { | |
648 | break; | |
649 | } | |
650 | } | |
651 | } else { | |
652 | error = ovsdb_atom_check_constraints(*range_end_atom, base); | |
653 | } | |
654 | ||
655 | if (!error) { | |
656 | error = ovsdb_atom_range_check_size(start, end); | |
657 | } | |
658 | } | |
659 | ||
bd76d25d | 660 | if (error) { |
e49190c4 | 661 | ovsdb_atom_destroy(atom, base->type); |
1ab39058 LR |
662 | if (range_end_atom && *range_end_atom) { |
663 | ovsdb_atom_destroy(*range_end_atom, base->type); | |
664 | free(*range_end_atom); | |
665 | *range_end_atom = NULL; | |
666 | } | |
3865965d | 667 | msg = ovsdb_error_to_string_free(error); |
bd76d25d BP |
668 | } |
669 | return msg; | |
670 | } | |
671 | ||
0194f33a BP |
672 | static bool |
673 | string_needs_quotes(const char *s) | |
674 | { | |
675 | const char *p = s; | |
676 | unsigned char c; | |
29004db2 | 677 | struct uuid uuid; |
0194f33a BP |
678 | |
679 | c = *p++; | |
680 | if (!isalpha(c) && c != '_') { | |
681 | return true; | |
682 | } | |
683 | ||
684 | while ((c = *p++) != '\0') { | |
29004db2 | 685 | if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-' && c != '.') { |
0194f33a BP |
686 | return true; |
687 | } | |
688 | } | |
689 | ||
690 | if (!strcmp(s, "true") || !strcmp(s, "false")) { | |
691 | return true; | |
692 | } | |
693 | ||
29004db2 IM |
694 | if (uuid_from_string(&uuid, s)) { |
695 | return true; | |
696 | } | |
697 | ||
0194f33a BP |
698 | return false; |
699 | } | |
700 | ||
701 | /* Appends 'atom' (which has the given 'type') to 'out', in a format acceptable | |
702 | * to ovsdb_atom_from_string(). */ | |
703 | void | |
704 | ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, | |
705 | struct ds *out) | |
706 | { | |
707 | switch (type) { | |
708 | case OVSDB_TYPE_VOID: | |
428b2edd | 709 | OVS_NOT_REACHED(); |
0194f33a BP |
710 | |
711 | case OVSDB_TYPE_INTEGER: | |
712 | ds_put_format(out, "%"PRId64, atom->integer); | |
713 | break; | |
714 | ||
715 | case OVSDB_TYPE_REAL: | |
716 | ds_put_format(out, "%.*g", DBL_DIG, atom->real); | |
717 | break; | |
718 | ||
719 | case OVSDB_TYPE_BOOLEAN: | |
720 | ds_put_cstr(out, atom->boolean ? "true" : "false"); | |
721 | break; | |
722 | ||
723 | case OVSDB_TYPE_STRING: | |
724 | if (string_needs_quotes(atom->string)) { | |
725 | struct json json; | |
726 | ||
727 | json.type = JSON_STRING; | |
fa37affa | 728 | json.string = atom->string; |
0194f33a BP |
729 | json_to_ds(&json, 0, out); |
730 | } else { | |
731 | ds_put_cstr(out, atom->string); | |
732 | } | |
733 | break; | |
734 | ||
735 | case OVSDB_TYPE_UUID: | |
736 | ds_put_format(out, UUID_FMT, UUID_ARGS(&atom->uuid)); | |
737 | break; | |
738 | ||
739 | case OVSDB_N_TYPES: | |
740 | default: | |
428b2edd | 741 | OVS_NOT_REACHED(); |
0194f33a BP |
742 | } |
743 | } | |
bd76d25d | 744 | |
c6a41252 BP |
745 | /* Appends 'atom' (which has the given 'type') to 'out', in a bare string |
746 | * format that cannot be parsed uniformly back into a datum but is easier for | |
747 | * shell scripts, etc., to deal with. */ | |
748 | void | |
749 | ovsdb_atom_to_bare(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, | |
750 | struct ds *out) | |
751 | { | |
752 | if (type == OVSDB_TYPE_STRING) { | |
753 | ds_put_cstr(out, atom->string); | |
754 | } else { | |
755 | ovsdb_atom_to_string(atom, type, out); | |
756 | } | |
757 | } | |
758 | ||
bd76d25d BP |
759 | static struct ovsdb_error * |
760 | check_string_constraints(const char *s, | |
761 | const struct ovsdb_string_constraints *c) | |
762 | { | |
763 | size_t n_chars; | |
764 | char *msg; | |
765 | ||
766 | msg = utf8_validate(s, &n_chars); | |
767 | if (msg) { | |
768 | struct ovsdb_error *error; | |
769 | ||
770 | error = ovsdb_error("constraint violation", | |
be44585c | 771 | "not a valid UTF-8 string: %s", msg); |
bd76d25d BP |
772 | free(msg); |
773 | return error; | |
774 | } | |
775 | ||
776 | if (n_chars < c->minLen) { | |
777 | return ovsdb_error( | |
778 | "constraint violation", | |
34582733 | 779 | "\"%s\" length %"PRIuSIZE" is less than minimum allowed " |
bd76d25d BP |
780 | "length %u", s, n_chars, c->minLen); |
781 | } else if (n_chars > c->maxLen) { | |
782 | return ovsdb_error( | |
783 | "constraint violation", | |
34582733 | 784 | "\"%s\" length %"PRIuSIZE" is greater than maximum allowed " |
bd76d25d BP |
785 | "length %u", s, n_chars, c->maxLen); |
786 | } | |
787 | ||
bd76d25d BP |
788 | return NULL; |
789 | } | |
790 | ||
791 | /* Checks whether 'atom' meets the constraints (if any) defined in 'base'. | |
792 | * (base->type must specify 'atom''s type.) Returns a null pointer if the | |
0d0f05b9 BP |
793 | * constraints are met, otherwise an error that explains the violation. |
794 | * | |
795 | * Checking UUID constraints is deferred to transaction commit time, so this | |
796 | * function does nothing for UUID constraints. */ | |
bd76d25d BP |
797 | struct ovsdb_error * |
798 | ovsdb_atom_check_constraints(const union ovsdb_atom *atom, | |
799 | const struct ovsdb_base_type *base) | |
800 | { | |
bfc96d9b BP |
801 | if (base->enum_ |
802 | && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) { | |
803 | struct ovsdb_error *error; | |
804 | struct ds actual = DS_EMPTY_INITIALIZER; | |
805 | struct ds valid = DS_EMPTY_INITIALIZER; | |
806 | ||
807 | ovsdb_atom_to_string(atom, base->type, &actual); | |
808 | ovsdb_datum_to_string(base->enum_, | |
809 | ovsdb_base_type_get_enum_type(base->type), | |
810 | &valid); | |
811 | error = ovsdb_error("constraint violation", | |
812 | "%s is not one of the allowed values (%s)", | |
813 | ds_cstr(&actual), ds_cstr(&valid)); | |
814 | ds_destroy(&actual); | |
815 | ds_destroy(&valid); | |
816 | ||
817 | return error; | |
818 | } | |
819 | ||
bd76d25d BP |
820 | switch (base->type) { |
821 | case OVSDB_TYPE_VOID: | |
428b2edd | 822 | OVS_NOT_REACHED(); |
bd76d25d BP |
823 | |
824 | case OVSDB_TYPE_INTEGER: | |
fa37affa BP |
825 | if (atom->integer >= base->integer.min |
826 | && atom->integer <= base->integer.max) { | |
bd76d25d | 827 | return NULL; |
fa37affa BP |
828 | } else if (base->integer.min != INT64_MIN) { |
829 | if (base->integer.max != INT64_MAX) { | |
bd76d25d BP |
830 | return ovsdb_error("constraint violation", |
831 | "%"PRId64" is not in the valid range " | |
832 | "%"PRId64" to %"PRId64" (inclusive)", | |
833 | atom->integer, | |
fa37affa | 834 | base->integer.min, base->integer.max); |
bd76d25d BP |
835 | } else { |
836 | return ovsdb_error("constraint violation", | |
837 | "%"PRId64" is less than minimum allowed " | |
838 | "value %"PRId64, | |
fa37affa | 839 | atom->integer, base->integer.min); |
bd76d25d BP |
840 | } |
841 | } else { | |
842 | return ovsdb_error("constraint violation", | |
843 | "%"PRId64" is greater than maximum allowed " | |
844 | "value %"PRId64, | |
fa37affa | 845 | atom->integer, base->integer.max); |
bd76d25d | 846 | } |
428b2edd | 847 | OVS_NOT_REACHED(); |
bd76d25d BP |
848 | |
849 | case OVSDB_TYPE_REAL: | |
fa37affa | 850 | if (atom->real >= base->real.min && atom->real <= base->real.max) { |
bd76d25d | 851 | return NULL; |
fa37affa BP |
852 | } else if (base->real.min != -DBL_MAX) { |
853 | if (base->real.max != DBL_MAX) { | |
bd76d25d BP |
854 | return ovsdb_error("constraint violation", |
855 | "%.*g is not in the valid range " | |
856 | "%.*g to %.*g (inclusive)", | |
857 | DBL_DIG, atom->real, | |
fa37affa BP |
858 | DBL_DIG, base->real.min, |
859 | DBL_DIG, base->real.max); | |
bd76d25d BP |
860 | } else { |
861 | return ovsdb_error("constraint violation", | |
862 | "%.*g is less than minimum allowed " | |
863 | "value %.*g", | |
864 | DBL_DIG, atom->real, | |
fa37affa | 865 | DBL_DIG, base->real.min); |
bd76d25d BP |
866 | } |
867 | } else { | |
868 | return ovsdb_error("constraint violation", | |
869 | "%.*g is greater than maximum allowed " | |
870 | "value %.*g", | |
871 | DBL_DIG, atom->real, | |
fa37affa | 872 | DBL_DIG, base->real.max); |
bd76d25d | 873 | } |
428b2edd | 874 | OVS_NOT_REACHED(); |
bd76d25d BP |
875 | |
876 | case OVSDB_TYPE_BOOLEAN: | |
877 | return NULL; | |
878 | ||
879 | case OVSDB_TYPE_STRING: | |
fa37affa | 880 | return check_string_constraints(atom->string, &base->string); |
bd76d25d BP |
881 | |
882 | case OVSDB_TYPE_UUID: | |
883 | return NULL; | |
884 | ||
885 | case OVSDB_N_TYPES: | |
886 | default: | |
428b2edd | 887 | OVS_NOT_REACHED(); |
bd76d25d BP |
888 | } |
889 | } | |
f85f8ebb | 890 | \f |
5413de95 BP |
891 | /* Initializes 'datum' as an empty datum. (An empty datum can be treated as |
892 | * any type.) */ | |
2f47998b BP |
893 | void |
894 | ovsdb_datum_init_empty(struct ovsdb_datum *datum) | |
895 | { | |
896 | datum->n = 0; | |
897 | datum->keys = NULL; | |
898 | datum->values = NULL; | |
899 | } | |
900 | ||
5413de95 BP |
901 | /* Initializes 'datum' as a datum that has the default value for 'type'. |
902 | * | |
c70a7767 | 903 | * The default value for a particular type is as defined in RFC 7047: |
5413de95 BP |
904 | * |
905 | * - If n_min is 0, then the default value is the empty set (or map). | |
906 | * | |
907 | * - If n_min is 1, the default value is a single value or a single | |
908 | * key-value pair, whose key and value are the defaults for their | |
909 | * atomic types. (See ovsdb_atom_init_default() for details.) | |
910 | * | |
911 | * - n_min > 1 is invalid. See ovsdb_type_is_valid(). | |
912 | */ | |
f85f8ebb BP |
913 | void |
914 | ovsdb_datum_init_default(struct ovsdb_datum *datum, | |
915 | const struct ovsdb_type *type) | |
916 | { | |
917 | datum->n = type->n_min; | |
bd76d25d BP |
918 | datum->keys = alloc_default_atoms(type->key.type, datum->n); |
919 | datum->values = alloc_default_atoms(type->value.type, datum->n); | |
f85f8ebb BP |
920 | } |
921 | ||
958ac03a BP |
922 | /* Returns a read-only datum of the given 'type' that has the default value for |
923 | * 'type'. The caller must not modify or free the returned datum. | |
924 | * | |
925 | * See ovsdb_datum_init_default() for an explanation of the default value of a | |
926 | * datum. */ | |
927 | const struct ovsdb_datum * | |
928 | ovsdb_datum_default(const struct ovsdb_type *type) | |
929 | { | |
930 | if (type->n_min == 0) { | |
931 | static const struct ovsdb_datum empty; | |
932 | return ∅ | |
933 | } else if (type->n_min == 1) { | |
934 | static struct ovsdb_datum default_data[OVSDB_N_TYPES][OVSDB_N_TYPES]; | |
935 | struct ovsdb_datum *d; | |
936 | int kt = type->key.type; | |
937 | int vt = type->value.type; | |
938 | ||
cb22974d | 939 | ovs_assert(ovsdb_type_is_valid(type)); |
958ac03a BP |
940 | |
941 | d = &default_data[kt][vt]; | |
942 | if (!d->n) { | |
943 | d->n = 1; | |
ebc56baa | 944 | d->keys = CONST_CAST(union ovsdb_atom *, ovsdb_atom_default(kt)); |
958ac03a | 945 | if (vt != OVSDB_TYPE_VOID) { |
ebc56baa BP |
946 | d->values = CONST_CAST(union ovsdb_atom *, |
947 | ovsdb_atom_default(vt)); | |
958ac03a BP |
948 | } |
949 | } | |
950 | return d; | |
951 | } else { | |
428b2edd | 952 | OVS_NOT_REACHED(); |
958ac03a BP |
953 | } |
954 | } | |
955 | ||
5413de95 BP |
956 | /* Returns true if 'datum', which must have the given 'type', has the default |
957 | * value for that type. | |
958 | * | |
959 | * See ovsdb_datum_init_default() for an explanation of the default value of a | |
960 | * datum. */ | |
c532bf9d BP |
961 | bool |
962 | ovsdb_datum_is_default(const struct ovsdb_datum *datum, | |
963 | const struct ovsdb_type *type) | |
964 | { | |
965 | size_t i; | |
966 | ||
967 | if (datum->n != type->n_min) { | |
968 | return false; | |
969 | } | |
970 | for (i = 0; i < datum->n; i++) { | |
bd76d25d | 971 | if (!ovsdb_atom_is_default(&datum->keys[i], type->key.type)) { |
c532bf9d BP |
972 | return false; |
973 | } | |
bd76d25d BP |
974 | if (type->value.type != OVSDB_TYPE_VOID |
975 | && !ovsdb_atom_is_default(&datum->values[i], type->value.type)) { | |
c532bf9d BP |
976 | return false; |
977 | } | |
978 | } | |
979 | ||
980 | return true; | |
981 | } | |
982 | ||
f85f8ebb BP |
983 | static union ovsdb_atom * |
984 | clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n) | |
985 | { | |
986 | if (type != OVSDB_TYPE_VOID && n) { | |
987 | union ovsdb_atom *new; | |
988 | unsigned int i; | |
989 | ||
990 | new = xmalloc(n * sizeof *new); | |
991 | for (i = 0; i < n; i++) { | |
992 | ovsdb_atom_clone(&new[i], &old[i], type); | |
993 | } | |
994 | return new; | |
995 | } else { | |
996 | /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is | |
997 | * treated as xmalloc(1). */ | |
998 | return NULL; | |
999 | } | |
1000 | } | |
1001 | ||
5413de95 BP |
1002 | /* Initializes 'new' as a copy of 'old', with the given 'type'. |
1003 | * | |
1004 | * The caller must eventually arrange for 'new' to be destroyed (with | |
1005 | * ovsdb_datum_destroy()). */ | |
f85f8ebb BP |
1006 | void |
1007 | ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old, | |
1008 | const struct ovsdb_type *type) | |
1009 | { | |
1010 | unsigned int n = old->n; | |
1011 | new->n = n; | |
bd76d25d BP |
1012 | new->keys = clone_atoms(old->keys, type->key.type, n); |
1013 | new->values = clone_atoms(old->values, type->value.type, n); | |
f85f8ebb BP |
1014 | } |
1015 | ||
1016 | static void | |
1017 | free_data(enum ovsdb_atomic_type type, | |
1018 | union ovsdb_atom *atoms, size_t n_atoms) | |
1019 | { | |
e3982750 WT |
1020 | if (!atoms) { |
1021 | return; | |
1022 | } | |
1023 | ||
f85f8ebb BP |
1024 | if (ovsdb_atom_needs_destruction(type)) { |
1025 | unsigned int i; | |
1026 | for (i = 0; i < n_atoms; i++) { | |
1027 | ovsdb_atom_destroy(&atoms[i], type); | |
1028 | } | |
1029 | } | |
1030 | free(atoms); | |
1031 | } | |
1032 | ||
5413de95 BP |
1033 | /* Frees the data owned by 'datum', which must have the given 'type'. |
1034 | * | |
1035 | * This does not actually call free(datum). If necessary, the caller must be | |
1036 | * responsible for that. */ | |
f85f8ebb BP |
1037 | void |
1038 | ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type) | |
1039 | { | |
bd76d25d BP |
1040 | free_data(type->key.type, datum->keys, datum->n); |
1041 | free_data(type->value.type, datum->values, datum->n); | |
f85f8ebb BP |
1042 | } |
1043 | ||
5413de95 | 1044 | /* Swaps the contents of 'a' and 'b', which need not have the same type. */ |
f85f8ebb BP |
1045 | void |
1046 | ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b) | |
1047 | { | |
1048 | struct ovsdb_datum tmp = *a; | |
1049 | *a = *b; | |
1050 | *b = tmp; | |
1051 | } | |
1052 | ||
1053 | struct ovsdb_datum_sort_cbdata { | |
bfc96d9b | 1054 | enum ovsdb_atomic_type key_type; |
2b66469b | 1055 | enum ovsdb_atomic_type value_type; |
f85f8ebb BP |
1056 | struct ovsdb_datum *datum; |
1057 | }; | |
1058 | ||
1059 | static int | |
1060 | ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_) | |
1061 | { | |
1062 | struct ovsdb_datum_sort_cbdata *cbdata = cbdata_; | |
2b66469b | 1063 | int retval; |
f85f8ebb | 1064 | |
2b66469b BP |
1065 | retval = ovsdb_atom_compare_3way(&cbdata->datum->keys[a], |
1066 | &cbdata->datum->keys[b], | |
1067 | cbdata->key_type); | |
1068 | if (retval || cbdata->value_type == OVSDB_TYPE_VOID) { | |
1069 | return retval; | |
1070 | } | |
1071 | ||
1072 | return ovsdb_atom_compare_3way(&cbdata->datum->values[a], | |
1073 | &cbdata->datum->values[b], | |
1074 | cbdata->value_type); | |
f85f8ebb BP |
1075 | } |
1076 | ||
1077 | static void | |
1078 | ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_) | |
1079 | { | |
1080 | struct ovsdb_datum_sort_cbdata *cbdata = cbdata_; | |
1081 | ||
1082 | ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]); | |
bfc96d9b | 1083 | if (cbdata->datum->values) { |
f85f8ebb BP |
1084 | ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]); |
1085 | } | |
1086 | } | |
1087 | ||
2b66469b BP |
1088 | static void |
1089 | ovsdb_datum_sort__(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type, | |
1090 | enum ovsdb_atomic_type value_type) | |
1091 | { | |
1092 | struct ovsdb_datum_sort_cbdata cbdata; | |
1093 | ||
1094 | cbdata.key_type = key_type; | |
1095 | cbdata.value_type = value_type; | |
1096 | cbdata.datum = datum; | |
1097 | sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb, | |
1098 | &cbdata); | |
1099 | } | |
1100 | ||
5413de95 BP |
1101 | /* The keys in an ovsdb_datum must be unique and in sorted order. Most |
1102 | * functions that modify an ovsdb_datum maintain these invariants. For those | |
1103 | * that don't, this function checks and restores these invariants for 'datum', | |
1104 | * whose keys are of type 'key_type'. | |
1105 | * | |
1106 | * This function returns NULL if successful, otherwise an error message. The | |
1107 | * caller must free the returned error when it is no longer needed. On error, | |
1108 | * 'datum' is sorted but not unique. */ | |
e9f8f936 | 1109 | struct ovsdb_error * |
bfc96d9b | 1110 | ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type) |
f85f8ebb | 1111 | { |
2b66469b BP |
1112 | size_t i; |
1113 | ||
f85f8ebb BP |
1114 | if (datum->n < 2) { |
1115 | return NULL; | |
2b66469b BP |
1116 | } |
1117 | ||
1118 | ovsdb_datum_sort__(datum, key_type, OVSDB_TYPE_VOID); | |
1119 | ||
1120 | for (i = 0; i < datum->n - 1; i++) { | |
1121 | if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1], | |
1122 | key_type)) { | |
1123 | if (datum->values) { | |
1124 | return ovsdb_error(NULL, "map contains duplicate key"); | |
1125 | } else { | |
1126 | return ovsdb_error(NULL, "set contains duplicate"); | |
f85f8ebb BP |
1127 | } |
1128 | } | |
f85f8ebb | 1129 | } |
2b66469b | 1130 | return NULL; |
f85f8ebb BP |
1131 | } |
1132 | ||
5413de95 BP |
1133 | /* This function is the same as ovsdb_datum_sort(), except that the caller |
1134 | * knows that 'datum' is unique. The operation therefore "cannot fail", so | |
1135 | * this function assert-fails if it actually does. */ | |
bfc96d9b BP |
1136 | void |
1137 | ovsdb_datum_sort_assert(struct ovsdb_datum *datum, | |
1138 | enum ovsdb_atomic_type key_type) | |
1139 | { | |
1140 | struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type); | |
1141 | if (error) { | |
428b2edd | 1142 | OVS_NOT_REACHED(); |
bfc96d9b BP |
1143 | } |
1144 | } | |
1145 | ||
2b66469b BP |
1146 | /* This is similar to ovsdb_datum_sort(), except that it drops duplicate keys |
1147 | * instead of reporting an error. In a map type, the smallest value among a | |
1148 | * group of duplicate pairs is retained and the others are dropped. | |
1149 | * | |
1150 | * Returns the number of keys (or pairs) that were dropped. */ | |
1151 | size_t | |
1152 | ovsdb_datum_sort_unique(struct ovsdb_datum *datum, | |
1153 | enum ovsdb_atomic_type key_type, | |
1154 | enum ovsdb_atomic_type value_type) | |
1155 | { | |
1156 | size_t src, dst; | |
1157 | ||
1158 | if (datum->n < 2) { | |
1159 | return 0; | |
1160 | } | |
1161 | ||
1162 | ovsdb_datum_sort__(datum, key_type, value_type); | |
1163 | ||
1164 | dst = 1; | |
1165 | for (src = 1; src < datum->n; src++) { | |
1166 | if (ovsdb_atom_equals(&datum->keys[src], &datum->keys[dst - 1], | |
1167 | key_type)) { | |
1168 | ovsdb_atom_destroy(&datum->keys[src], key_type); | |
1169 | if (value_type != OVSDB_TYPE_VOID) { | |
1170 | ovsdb_atom_destroy(&datum->values[src], value_type); | |
1171 | } | |
1172 | } else { | |
1173 | if (src != dst) { | |
1174 | datum->keys[dst] = datum->keys[src]; | |
1175 | if (value_type != OVSDB_TYPE_VOID) { | |
1176 | datum->values[dst] = datum->values[src]; | |
1177 | } | |
1178 | } | |
1179 | dst++; | |
1180 | } | |
1181 | } | |
1182 | datum->n = dst; | |
1183 | return datum->n - src; | |
1184 | } | |
1185 | ||
bd76d25d BP |
1186 | /* Checks that each of the atoms in 'datum' conforms to the constraints |
1187 | * specified by its 'type'. Returns an error if a constraint is violated, | |
1188 | * otherwise a null pointer. | |
1189 | * | |
1190 | * This function is not commonly useful because the most ordinary way to obtain | |
1191 | * a datum is ultimately via ovsdb_atom_from_string() or | |
1192 | * ovsdb_atom_from_json(), which check constraints themselves. */ | |
1193 | struct ovsdb_error * | |
1194 | ovsdb_datum_check_constraints(const struct ovsdb_datum *datum, | |
1195 | const struct ovsdb_type *type) | |
1196 | { | |
1197 | struct ovsdb_error *error; | |
1198 | unsigned int i; | |
1199 | ||
1200 | for (i = 0; i < datum->n; i++) { | |
1201 | error = ovsdb_atom_check_constraints(&datum->keys[i], &type->key); | |
1202 | if (error) { | |
1203 | return error; | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | if (type->value.type != OVSDB_TYPE_VOID) { | |
1208 | for (i = 0; i < datum->n; i++) { | |
1209 | error = ovsdb_atom_check_constraints(&datum->values[i], | |
1210 | &type->value); | |
1211 | if (error) { | |
1212 | return error; | |
1213 | } | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | return NULL; | |
1218 | } | |
1219 | ||
2b66469b BP |
1220 | static struct ovsdb_error * |
1221 | ovsdb_datum_from_json__(struct ovsdb_datum *datum, | |
1222 | const struct ovsdb_type *type, | |
1223 | const struct json *json, | |
1224 | struct ovsdb_symbol_table *symtab) | |
f85f8ebb BP |
1225 | { |
1226 | struct ovsdb_error *error; | |
1227 | ||
ae8f13e2 BP |
1228 | if (ovsdb_type_is_map(type) |
1229 | || (json->type == JSON_ARRAY | |
fa37affa BP |
1230 | && json->array.n > 0 |
1231 | && json->array.elems[0]->type == JSON_STRING | |
1232 | && !strcmp(json->array.elems[0]->string, "set"))) { | |
f85f8ebb BP |
1233 | bool is_map = ovsdb_type_is_map(type); |
1234 | const char *class = is_map ? "map" : "set"; | |
1235 | const struct json *inner; | |
1236 | unsigned int i; | |
1237 | size_t n; | |
1238 | ||
f85f8ebb BP |
1239 | error = unwrap_json(json, class, JSON_ARRAY, &inner); |
1240 | if (error) { | |
1241 | return error; | |
1242 | } | |
1243 | ||
fa37affa | 1244 | n = inner->array.n; |
f85f8ebb | 1245 | if (n < type->n_min || n > type->n_max) { |
bc9bbc4e BP |
1246 | if (type->n_min == 1 && type->n_max == 1) { |
1247 | return ovsdb_syntax_error(json, NULL, "%s must have exactly " | |
1248 | "one member but %"PRIuSIZE" " | |
1249 | "are present", class, n); | |
1250 | } else { | |
1251 | return ovsdb_syntax_error(json, NULL, "%s must have %u to " | |
1252 | "%u members but %"PRIuSIZE" are " | |
1253 | "present", | |
1254 | class, type->n_min, type->n_max, n); | |
1255 | } | |
f85f8ebb BP |
1256 | } |
1257 | ||
1258 | datum->n = 0; | |
1259 | datum->keys = xmalloc(n * sizeof *datum->keys); | |
1260 | datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL; | |
1261 | for (i = 0; i < n; i++) { | |
fa37affa | 1262 | const struct json *element = inner->array.elems[i]; |
f85f8ebb BP |
1263 | const struct json *key = NULL; |
1264 | const struct json *value = NULL; | |
1265 | ||
1266 | if (!is_map) { | |
1267 | key = element; | |
1268 | } else { | |
1269 | error = parse_json_pair(element, &key, &value); | |
1270 | if (error) { | |
1271 | goto error; | |
1272 | } | |
1273 | } | |
1274 | ||
bd76d25d | 1275 | error = ovsdb_atom_from_json(&datum->keys[i], &type->key, |
f85f8ebb BP |
1276 | key, symtab); |
1277 | if (error) { | |
1278 | goto error; | |
1279 | } | |
1280 | ||
1281 | if (is_map) { | |
1282 | error = ovsdb_atom_from_json(&datum->values[i], | |
bd76d25d | 1283 | &type->value, value, symtab); |
f85f8ebb | 1284 | if (error) { |
bd76d25d | 1285 | ovsdb_atom_destroy(&datum->keys[i], type->key.type); |
f85f8ebb BP |
1286 | goto error; |
1287 | } | |
1288 | } | |
1289 | ||
1290 | datum->n++; | |
1291 | } | |
f85f8ebb BP |
1292 | return NULL; |
1293 | ||
1294 | error: | |
1295 | ovsdb_datum_destroy(datum, type); | |
1296 | return error; | |
ae8f13e2 BP |
1297 | } else { |
1298 | datum->n = 1; | |
1299 | datum->keys = xmalloc(sizeof *datum->keys); | |
1300 | datum->values = NULL; | |
1301 | ||
1302 | error = ovsdb_atom_from_json(&datum->keys[0], &type->key, | |
1303 | json, symtab); | |
1304 | if (error) { | |
1305 | free(datum->keys); | |
1306 | } | |
1307 | return error; | |
f85f8ebb BP |
1308 | } |
1309 | } | |
1310 | ||
2b66469b BP |
1311 | /* Parses 'json' as a datum of the type described by 'type'. If successful, |
1312 | * returns NULL and initializes 'datum' with the parsed datum. On failure, | |
1313 | * returns an error and the contents of 'datum' are indeterminate. The caller | |
1314 | * is responsible for freeing the error or the datum that is returned. | |
1315 | * | |
1316 | * Violations of constraints expressed by 'type' are treated as errors. | |
1317 | * | |
1318 | * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to | |
c70a7767 BP |
1319 | * RFC 7047 for information about this, and for the syntax that this function |
1320 | * accepts. */ | |
65d9759c | 1321 | struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
2b66469b BP |
1322 | ovsdb_datum_from_json(struct ovsdb_datum *datum, |
1323 | const struct ovsdb_type *type, | |
1324 | const struct json *json, | |
1325 | struct ovsdb_symbol_table *symtab) | |
1326 | { | |
1327 | struct ovsdb_error *error; | |
1328 | ||
1329 | error = ovsdb_datum_from_json__(datum, type, json, symtab); | |
1330 | if (error) { | |
1331 | return error; | |
1332 | } | |
1333 | ||
1334 | error = ovsdb_datum_sort(datum, type->key.type); | |
1335 | if (error) { | |
1336 | ovsdb_datum_destroy(datum, type); | |
1337 | } | |
1338 | return error; | |
1339 | } | |
1340 | ||
aefd97b7 AZ |
1341 | /* Parses 'json' as a datum of the type described by 'type' for internal |
1342 | * use. This function is similar to 'ovsdb_datum_from_json', except the | |
1343 | * member size of set or map is not checked. | |
1344 | * | |
1345 | * The datum generated should be used then discard. It is not suitable | |
1346 | * for storing into IDL because of the possible member size violation. */ | |
65d9759c | 1347 | struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
aefd97b7 AZ |
1348 | ovsdb_transient_datum_from_json(struct ovsdb_datum *datum, |
1349 | const struct ovsdb_type *type, | |
1350 | const struct json *json) | |
1351 | { | |
1352 | struct ovsdb_type relaxed_type = *type; | |
1353 | ||
1354 | relaxed_type.n_min = 0; | |
1355 | relaxed_type.n_max = UINT_MAX; | |
1356 | ||
1357 | return ovsdb_datum_from_json(datum, &relaxed_type, json, NULL); | |
1358 | } | |
1359 | ||
9041097a BP |
1360 | /* Parses 'json' as a datum of the type described by 'type', but ignoring all |
1361 | * constraints. */ | |
1362 | struct ovsdb_error * OVS_WARN_UNUSED_RESULT | |
1363 | ovsdb_unconstrained_datum_from_json(struct ovsdb_datum *datum, | |
1364 | const struct ovsdb_type *type, | |
1365 | const struct json *json) | |
1366 | { | |
1367 | struct ovsdb_type relaxed_type; | |
1368 | ||
1369 | ovsdb_base_type_init(&relaxed_type.key, type->key.type); | |
1370 | ovsdb_base_type_init(&relaxed_type.value, type->value.type); | |
1371 | relaxed_type.n_min = 0; | |
1372 | relaxed_type.n_max = UINT_MAX; | |
1373 | ||
1374 | return ovsdb_datum_from_json(datum, &relaxed_type, json, NULL); | |
1375 | } | |
1376 | ||
fe0fb885 BP |
1377 | static struct json * |
1378 | ovsdb_base_to_json(const union ovsdb_atom *atom, | |
1379 | const struct ovsdb_base_type *base, | |
1380 | bool use_row_names) | |
1381 | { | |
1382 | if (!use_row_names | |
1383 | || base->type != OVSDB_TYPE_UUID | |
fa37affa | 1384 | || !base->uuid.refTableName) { |
fe0fb885 BP |
1385 | return ovsdb_atom_to_json(atom, base->type); |
1386 | } else { | |
1387 | return json_array_create_2( | |
1388 | json_string_create("named-uuid"), | |
1389 | json_string_create_nocopy(ovsdb_data_row_name(&atom->uuid))); | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | static struct json * | |
1394 | ovsdb_datum_to_json__(const struct ovsdb_datum *datum, | |
1395 | const struct ovsdb_type *type, | |
1396 | bool use_row_names) | |
f85f8ebb | 1397 | { |
6a6f8d16 | 1398 | if (ovsdb_type_is_map(type)) { |
f85f8ebb BP |
1399 | struct json **elems; |
1400 | size_t i; | |
1401 | ||
1402 | elems = xmalloc(datum->n * sizeof *elems); | |
1403 | for (i = 0; i < datum->n; i++) { | |
6a6f8d16 | 1404 | elems[i] = json_array_create_2( |
fe0fb885 BP |
1405 | ovsdb_base_to_json(&datum->keys[i], &type->key, |
1406 | use_row_names), | |
1407 | ovsdb_base_to_json(&datum->values[i], &type->value, | |
1408 | use_row_names)); | |
f85f8ebb BP |
1409 | } |
1410 | ||
6a6f8d16 BP |
1411 | return wrap_json("map", json_array_create(elems, datum->n)); |
1412 | } else if (datum->n == 1) { | |
fe0fb885 | 1413 | return ovsdb_base_to_json(&datum->keys[0], &type->key, use_row_names); |
f85f8ebb BP |
1414 | } else { |
1415 | struct json **elems; | |
1416 | size_t i; | |
1417 | ||
1418 | elems = xmalloc(datum->n * sizeof *elems); | |
1419 | for (i = 0; i < datum->n; i++) { | |
fe0fb885 BP |
1420 | elems[i] = ovsdb_base_to_json(&datum->keys[i], &type->key, |
1421 | use_row_names); | |
f85f8ebb BP |
1422 | } |
1423 | ||
6a6f8d16 | 1424 | return wrap_json("set", json_array_create(elems, datum->n)); |
f85f8ebb BP |
1425 | } |
1426 | } | |
1427 | ||
fe0fb885 BP |
1428 | /* Converts 'datum', of the specified 'type', to JSON format, and returns the |
1429 | * JSON. The caller is responsible for freeing the returned JSON. | |
1430 | * | |
1431 | * 'type' constraints on datum->n are ignored. | |
1432 | * | |
1433 | * Refer to RFC 7047 for the format of the JSON that this function produces. */ | |
1434 | struct json * | |
1435 | ovsdb_datum_to_json(const struct ovsdb_datum *datum, | |
1436 | const struct ovsdb_type *type) | |
1437 | { | |
1438 | return ovsdb_datum_to_json__(datum, type, false); | |
1439 | } | |
1440 | ||
1441 | struct json * | |
1442 | ovsdb_datum_to_json_with_row_names(const struct ovsdb_datum *datum, | |
1443 | const struct ovsdb_type *type) | |
1444 | { | |
1445 | return ovsdb_datum_to_json__(datum, type, true); | |
1446 | } | |
1447 | ||
0194f33a BP |
1448 | static const char * |
1449 | skip_spaces(const char *p) | |
1450 | { | |
cb456217 BP |
1451 | while (isspace((unsigned char) *p)) { |
1452 | p++; | |
1453 | } | |
1454 | return p; | |
0194f33a BP |
1455 | } |
1456 | ||
1bc6ff29 | 1457 | static char * |
bd76d25d | 1458 | parse_atom_token(const char **s, const struct ovsdb_base_type *base, |
1ab39058 LR |
1459 | union ovsdb_atom *atom, union ovsdb_atom **range_end_atom, |
1460 | struct ovsdb_symbol_table *symtab) | |
0194f33a | 1461 | { |
1bc6ff29 | 1462 | char *token, *error; |
0194f33a | 1463 | |
1bc6ff29 BP |
1464 | error = ovsdb_token_parse(s, &token); |
1465 | if (!error) { | |
1ab39058 LR |
1466 | error = ovsdb_atom_from_string(atom, range_end_atom, |
1467 | base, token, symtab); | |
1bc6ff29 BP |
1468 | free(token); |
1469 | } | |
1470 | return error; | |
1471 | } | |
0194f33a | 1472 | |
1bc6ff29 BP |
1473 | static char * |
1474 | parse_key_value(const char **s, const struct ovsdb_type *type, | |
ce5a3e38 | 1475 | union ovsdb_atom *key, union ovsdb_atom *value, |
1ab39058 LR |
1476 | struct ovsdb_symbol_table *symtab, |
1477 | union ovsdb_atom **range_end_key) | |
1bc6ff29 BP |
1478 | { |
1479 | const char *start = *s; | |
1480 | char *error; | |
1481 | ||
1ab39058 LR |
1482 | error = parse_atom_token(s, &type->key, key, range_end_key, symtab); |
1483 | ||
bd76d25d | 1484 | if (!error && type->value.type != OVSDB_TYPE_VOID) { |
9397a13f | 1485 | *s = skip_spaces(*s); |
1bc6ff29 BP |
1486 | if (**s == '=') { |
1487 | (*s)++; | |
9397a13f | 1488 | *s = skip_spaces(*s); |
1ab39058 | 1489 | error = parse_atom_token(s, &type->value, value, NULL, symtab); |
1bc6ff29 BP |
1490 | } else { |
1491 | error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"", | |
1492 | start, **s); | |
1493 | } | |
1494 | if (error) { | |
bd76d25d | 1495 | ovsdb_atom_destroy(key, type->key.type); |
1ab39058 LR |
1496 | if (range_end_key && *range_end_key) { |
1497 | ovsdb_atom_destroy(*range_end_key, type->key.type); | |
1498 | free(*range_end_key); | |
1499 | *range_end_key = NULL; | |
1500 | } | |
0194f33a | 1501 | } |
0194f33a | 1502 | } |
1bc6ff29 | 1503 | return error; |
0194f33a BP |
1504 | } |
1505 | ||
1506 | static void | |
1ab39058 LR |
1507 | free_key_value_range(const struct ovsdb_type *type, |
1508 | union ovsdb_atom *key, union ovsdb_atom *value, | |
1509 | union ovsdb_atom **range_end_atom) | |
0194f33a | 1510 | { |
bd76d25d BP |
1511 | ovsdb_atom_destroy(key, type->key.type); |
1512 | if (type->value.type != OVSDB_TYPE_VOID) { | |
1513 | ovsdb_atom_destroy(value, type->value.type); | |
0194f33a | 1514 | } |
1ab39058 LR |
1515 | if (range_end_atom && *range_end_atom) { |
1516 | ovsdb_atom_destroy(*range_end_atom, type->key.type); | |
1517 | free(*range_end_atom); | |
1518 | *range_end_atom = NULL; | |
1519 | } | |
0194f33a BP |
1520 | } |
1521 | ||
1522 | /* Initializes 'datum' as a datum of the given 'type', parsing its contents | |
1523 | * from 's'. The format of 's' is a series of space or comma separated atoms | |
1524 | * or, for a map, '='-delimited pairs of atoms. Each atom must in a format | |
1525 | * acceptable to ovsdb_atom_from_string(). Optionally, a set may be enclosed | |
1526 | * in "[]" or a map in "{}"; for an empty set or map these punctuators are | |
ce5a3e38 BP |
1527 | * required. |
1528 | * | |
1529 | * Optionally, a symbol table may be supplied as 'symtab'. It is passed to | |
1530 | * ovsdb_atom_to_string(). */ | |
1bc6ff29 | 1531 | char * |
0194f33a | 1532 | ovsdb_datum_from_string(struct ovsdb_datum *datum, |
ce5a3e38 BP |
1533 | const struct ovsdb_type *type, const char *s, |
1534 | struct ovsdb_symbol_table *symtab) | |
0194f33a BP |
1535 | { |
1536 | bool is_map = ovsdb_type_is_map(type); | |
1bc6ff29 | 1537 | struct ovsdb_error *dberror; |
0194f33a BP |
1538 | const char *p; |
1539 | int end_delim; | |
1bc6ff29 | 1540 | char *error; |
0194f33a BP |
1541 | |
1542 | ovsdb_datum_init_empty(datum); | |
1543 | ||
1544 | /* Swallow a leading delimiter if there is one. */ | |
1545 | p = skip_spaces(s); | |
1546 | if (*p == (is_map ? '{' : '[')) { | |
1547 | end_delim = is_map ? '}' : ']'; | |
1548 | p = skip_spaces(p + 1); | |
1549 | } else if (!*p) { | |
1550 | if (is_map) { | |
1bc6ff29 | 1551 | return xstrdup("use \"{}\" to specify the empty map"); |
0194f33a | 1552 | } else { |
1bc6ff29 | 1553 | return xstrdup("use \"[]\" to specify the empty set"); |
0194f33a BP |
1554 | } |
1555 | } else { | |
1556 | end_delim = 0; | |
1557 | } | |
1558 | ||
1559 | while (*p && *p != end_delim) { | |
1560 | union ovsdb_atom key, value; | |
1ab39058 | 1561 | union ovsdb_atom *range_end_key = NULL; |
0194f33a BP |
1562 | |
1563 | if (ovsdb_token_is_delim(*p)) { | |
9466d7d4 | 1564 | char *type_str = ovsdb_type_to_english(type); |
1bc6ff29 | 1565 | error = xasprintf("%s: unexpected \"%c\" parsing %s", |
9466d7d4 JP |
1566 | s, *p, type_str); |
1567 | free(type_str); | |
1bc6ff29 | 1568 | goto error; |
0194f33a BP |
1569 | } |
1570 | ||
1571 | /* Add to datum. */ | |
1ab39058 LR |
1572 | error = parse_key_value(&p, type, &key, &value, |
1573 | symtab, &range_end_key); | |
1bc6ff29 BP |
1574 | if (error) { |
1575 | goto error; | |
1576 | } | |
1ab39058 LR |
1577 | ovsdb_datum_add_unsafe(datum, &key, &value, type, range_end_key); |
1578 | free_key_value_range(type, &key, &value, &range_end_key); | |
0194f33a BP |
1579 | |
1580 | /* Skip optional white space and comma. */ | |
1581 | p = skip_spaces(p); | |
1582 | if (*p == ',') { | |
1583 | p = skip_spaces(p + 1); | |
1584 | } | |
1585 | } | |
1586 | ||
1587 | if (*p != end_delim) { | |
1bc6ff29 BP |
1588 | error = xasprintf("%s: missing \"%c\" at end of data", s, end_delim); |
1589 | goto error; | |
0194f33a BP |
1590 | } |
1591 | if (end_delim) { | |
1592 | p = skip_spaces(p + 1); | |
1593 | if (*p) { | |
1bc6ff29 BP |
1594 | error = xasprintf("%s: trailing garbage after \"%c\"", |
1595 | s, end_delim); | |
1596 | goto error; | |
0194f33a BP |
1597 | } |
1598 | } | |
1599 | ||
1600 | if (datum->n < type->n_min) { | |
1bc6ff29 BP |
1601 | error = xasprintf("%s: %u %s specified but the minimum number is %u", |
1602 | s, datum->n, is_map ? "pair(s)" : "value(s)", | |
1603 | type->n_min); | |
1604 | goto error; | |
0194f33a | 1605 | } else if (datum->n > type->n_max) { |
1bc6ff29 BP |
1606 | error = xasprintf("%s: %u %s specified but the maximum number is %u", |
1607 | s, datum->n, is_map ? "pair(s)" : "value(s)", | |
1608 | type->n_max); | |
1609 | goto error; | |
0194f33a BP |
1610 | } |
1611 | ||
bfc96d9b | 1612 | dberror = ovsdb_datum_sort(datum, type->key.type); |
1bc6ff29 BP |
1613 | if (dberror) { |
1614 | ovsdb_error_destroy(dberror); | |
0194f33a | 1615 | if (ovsdb_type_is_map(type)) { |
1bc6ff29 | 1616 | error = xasprintf("%s: map contains duplicate key", s); |
0194f33a | 1617 | } else { |
1bc6ff29 | 1618 | error = xasprintf("%s: set contains duplicate value", s); |
0194f33a | 1619 | } |
1bc6ff29 | 1620 | goto error; |
0194f33a | 1621 | } |
1bc6ff29 BP |
1622 | |
1623 | return NULL; | |
1624 | ||
1625 | error: | |
1626 | ovsdb_datum_destroy(datum, type); | |
1627 | ovsdb_datum_init_empty(datum); | |
1628 | return error; | |
0194f33a BP |
1629 | } |
1630 | ||
1631 | /* Appends to 'out' the 'datum' (with the given 'type') in a format acceptable | |
1632 | * to ovsdb_datum_from_string(). */ | |
1633 | void | |
1634 | ovsdb_datum_to_string(const struct ovsdb_datum *datum, | |
1635 | const struct ovsdb_type *type, struct ds *out) | |
1636 | { | |
1637 | bool is_map = ovsdb_type_is_map(type); | |
1638 | size_t i; | |
1639 | ||
1640 | if (type->n_max > 1 || !datum->n) { | |
1641 | ds_put_char(out, is_map ? '{' : '['); | |
1642 | } | |
1643 | for (i = 0; i < datum->n; i++) { | |
1644 | if (i > 0) { | |
1645 | ds_put_cstr(out, ", "); | |
1646 | } | |
1647 | ||
bd76d25d | 1648 | ovsdb_atom_to_string(&datum->keys[i], type->key.type, out); |
0194f33a BP |
1649 | if (is_map) { |
1650 | ds_put_char(out, '='); | |
bd76d25d | 1651 | ovsdb_atom_to_string(&datum->values[i], type->value.type, out); |
0194f33a BP |
1652 | } |
1653 | } | |
1654 | if (type->n_max > 1 || !datum->n) { | |
1655 | ds_put_char(out, is_map ? '}' : ']'); | |
1656 | } | |
1657 | } | |
1658 | ||
c6a41252 BP |
1659 | /* Appends to 'out' the 'datum' (with the given 'type') in a bare string format |
1660 | * that cannot be parsed uniformly back into a datum but is easier for shell | |
1661 | * scripts, etc., to deal with. */ | |
1662 | void | |
1663 | ovsdb_datum_to_bare(const struct ovsdb_datum *datum, | |
1664 | const struct ovsdb_type *type, struct ds *out) | |
1665 | { | |
1666 | bool is_map = ovsdb_type_is_map(type); | |
1667 | size_t i; | |
1668 | ||
1669 | for (i = 0; i < datum->n; i++) { | |
1670 | if (i > 0) { | |
1671 | ds_put_cstr(out, " "); | |
1672 | } | |
1673 | ||
1674 | ovsdb_atom_to_bare(&datum->keys[i], type->key.type, out); | |
1675 | if (is_map) { | |
1676 | ds_put_char(out, '='); | |
1677 | ovsdb_atom_to_bare(&datum->values[i], type->value.type, out); | |
1678 | } | |
1679 | } | |
1680 | } | |
1681 | ||
9b03e59d BP |
1682 | /* Initializes 'datum' as a string-to-string map whose contents are copied from |
1683 | * 'smap', which is not modified. */ | |
e4af5615 | 1684 | void |
9b03e59d | 1685 | ovsdb_datum_from_smap(struct ovsdb_datum *datum, const struct smap *smap) |
e4af5615 | 1686 | { |
57c8677b | 1687 | datum->n = smap_count(smap); |
e4af5615 BP |
1688 | datum->keys = xmalloc(datum->n * sizeof *datum->keys); |
1689 | datum->values = xmalloc(datum->n * sizeof *datum->values); | |
1690 | ||
9b03e59d BP |
1691 | struct smap_node *node; |
1692 | size_t i = 0; | |
1693 | SMAP_FOR_EACH (node, smap) { | |
1694 | datum->keys[i].string = xstrdup(node->key); | |
1695 | datum->values[i].string = xstrdup(node->value); | |
e4af5615 BP |
1696 | i++; |
1697 | } | |
cb22974d | 1698 | ovs_assert(i == datum->n); |
e4af5615 | 1699 | |
e4af5615 BP |
1700 | ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING); |
1701 | } | |
1702 | ||
53178986 BP |
1703 | struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
1704 | ovsdb_datum_convert(struct ovsdb_datum *dst, | |
1705 | const struct ovsdb_type *dst_type, | |
1706 | const struct ovsdb_datum *src, | |
1707 | const struct ovsdb_type *src_type) | |
1708 | { | |
1709 | struct json *json = ovsdb_datum_to_json(src, src_type); | |
1710 | struct ovsdb_error *error = ovsdb_datum_from_json(dst, dst_type, json, | |
1711 | NULL); | |
1712 | json_destroy(json); | |
1713 | return error; | |
1714 | } | |
1715 | ||
f85f8ebb BP |
1716 | static uint32_t |
1717 | hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms, | |
1718 | unsigned int n, uint32_t basis) | |
1719 | { | |
1720 | if (type != OVSDB_TYPE_VOID) { | |
1721 | unsigned int i; | |
1722 | ||
1723 | for (i = 0; i < n; i++) { | |
1724 | basis = ovsdb_atom_hash(&atoms[i], type, basis); | |
1725 | } | |
1726 | } | |
1727 | return basis; | |
1728 | } | |
1729 | ||
1730 | uint32_t | |
1731 | ovsdb_datum_hash(const struct ovsdb_datum *datum, | |
1732 | const struct ovsdb_type *type, uint32_t basis) | |
1733 | { | |
bd76d25d BP |
1734 | basis = hash_atoms(type->key.type, datum->keys, datum->n, basis); |
1735 | basis ^= (type->key.type << 24) | (type->value.type << 16) | datum->n; | |
1736 | basis = hash_atoms(type->value.type, datum->values, datum->n, basis); | |
f85f8ebb BP |
1737 | return basis; |
1738 | } | |
1739 | ||
1740 | static int | |
1741 | atom_arrays_compare_3way(const union ovsdb_atom *a, | |
2f47998b BP |
1742 | const union ovsdb_atom *b, |
1743 | enum ovsdb_atomic_type type, | |
1744 | size_t n) | |
f85f8ebb BP |
1745 | { |
1746 | unsigned int i; | |
1747 | ||
1748 | for (i = 0; i < n; i++) { | |
1749 | int cmp = ovsdb_atom_compare_3way(&a[i], &b[i], type); | |
1750 | if (cmp) { | |
1751 | return cmp; | |
1752 | } | |
1753 | } | |
1754 | ||
1755 | return 0; | |
1756 | } | |
1757 | ||
1758 | bool | |
1759 | ovsdb_datum_equals(const struct ovsdb_datum *a, | |
1760 | const struct ovsdb_datum *b, | |
1761 | const struct ovsdb_type *type) | |
1762 | { | |
1763 | return !ovsdb_datum_compare_3way(a, b, type); | |
1764 | } | |
1765 | ||
1766 | int | |
1767 | ovsdb_datum_compare_3way(const struct ovsdb_datum *a, | |
1768 | const struct ovsdb_datum *b, | |
1769 | const struct ovsdb_type *type) | |
1770 | { | |
1771 | int cmp; | |
1772 | ||
1773 | if (a->n != b->n) { | |
1774 | return a->n < b->n ? -1 : 1; | |
1775 | } | |
1776 | ||
bd76d25d | 1777 | cmp = atom_arrays_compare_3way(a->keys, b->keys, type->key.type, a->n); |
f85f8ebb BP |
1778 | if (cmp) { |
1779 | return cmp; | |
1780 | } | |
1781 | ||
bd76d25d BP |
1782 | return (type->value.type == OVSDB_TYPE_VOID ? 0 |
1783 | : atom_arrays_compare_3way(a->values, b->values, type->value.type, | |
f85f8ebb BP |
1784 | a->n)); |
1785 | } | |
1786 | ||
2f47998b | 1787 | /* If 'key' is one of the keys in 'datum', returns its index within 'datum', |
bd76d25d | 1788 | * otherwise UINT_MAX. 'key.type' must be the type of the atoms stored in the |
2f47998b BP |
1789 | * 'keys' array in 'datum'. |
1790 | */ | |
1791 | unsigned int | |
1792 | ovsdb_datum_find_key(const struct ovsdb_datum *datum, | |
1793 | const union ovsdb_atom *key, | |
1794 | enum ovsdb_atomic_type key_type) | |
1795 | { | |
1796 | unsigned int low = 0; | |
1797 | unsigned int high = datum->n; | |
1798 | while (low < high) { | |
1799 | unsigned int idx = (low + high) / 2; | |
1800 | int cmp = ovsdb_atom_compare_3way(key, &datum->keys[idx], key_type); | |
1801 | if (cmp < 0) { | |
1802 | high = idx; | |
1803 | } else if (cmp > 0) { | |
1804 | low = idx + 1; | |
1805 | } else { | |
1806 | return idx; | |
1807 | } | |
1808 | } | |
1809 | return UINT_MAX; | |
1810 | } | |
1811 | ||
1812 | /* If 'key' and 'value' is one of the key-value pairs in 'datum', returns its | |
bd76d25d | 1813 | * index within 'datum', otherwise UINT_MAX. 'key.type' must be the type of |
2f47998b BP |
1814 | * the atoms stored in the 'keys' array in 'datum'. 'value_type' may be the |
1815 | * type of the 'values' atoms or OVSDB_TYPE_VOID to compare only keys. | |
1816 | */ | |
1817 | unsigned int | |
1818 | ovsdb_datum_find_key_value(const struct ovsdb_datum *datum, | |
1819 | const union ovsdb_atom *key, | |
1820 | enum ovsdb_atomic_type key_type, | |
1821 | const union ovsdb_atom *value, | |
1822 | enum ovsdb_atomic_type value_type) | |
1823 | { | |
1824 | unsigned int idx = ovsdb_datum_find_key(datum, key, key_type); | |
1825 | if (idx != UINT_MAX | |
1826 | && value_type != OVSDB_TYPE_VOID | |
1827 | && !ovsdb_atom_equals(&datum->values[idx], value, value_type)) { | |
1828 | idx = UINT_MAX; | |
1829 | } | |
1830 | return idx; | |
1831 | } | |
1832 | ||
e9f8f936 BP |
1833 | /* If atom 'i' in 'a' is also in 'b', returns its index in 'b', otherwise |
1834 | * UINT_MAX. 'type' must be the type of 'a' and 'b', except that | |
bd76d25d | 1835 | * type->value.type may be set to OVSDB_TYPE_VOID to compare keys but not |
e9f8f936 BP |
1836 | * values. */ |
1837 | static unsigned int | |
1838 | ovsdb_datum_find(const struct ovsdb_datum *a, int i, | |
1839 | const struct ovsdb_datum *b, | |
1840 | const struct ovsdb_type *type) | |
f85f8ebb | 1841 | { |
2f47998b | 1842 | return ovsdb_datum_find_key_value(b, |
bd76d25d | 1843 | &a->keys[i], type->key.type, |
2f47998b | 1844 | a->values ? &a->values[i] : NULL, |
bd76d25d | 1845 | type->value.type); |
f85f8ebb BP |
1846 | } |
1847 | ||
1848 | /* Returns true if every element in 'a' is also in 'b', false otherwise. */ | |
1849 | bool | |
1850 | ovsdb_datum_includes_all(const struct ovsdb_datum *a, | |
1851 | const struct ovsdb_datum *b, | |
1852 | const struct ovsdb_type *type) | |
1853 | { | |
1854 | size_t i; | |
1855 | ||
ca261b65 BP |
1856 | if (a->n > b->n) { |
1857 | return false; | |
1858 | } | |
f85f8ebb | 1859 | for (i = 0; i < a->n; i++) { |
e9f8f936 | 1860 | if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) { |
f85f8ebb BP |
1861 | return false; |
1862 | } | |
1863 | } | |
1864 | return true; | |
1865 | } | |
1866 | ||
1867 | /* Returns true if no element in 'a' is also in 'b', false otherwise. */ | |
1868 | bool | |
1869 | ovsdb_datum_excludes_all(const struct ovsdb_datum *a, | |
1870 | const struct ovsdb_datum *b, | |
1871 | const struct ovsdb_type *type) | |
1872 | { | |
1873 | size_t i; | |
1874 | ||
1875 | for (i = 0; i < a->n; i++) { | |
e9f8f936 | 1876 | if (ovsdb_datum_find(a, i, b, type) != UINT_MAX) { |
f85f8ebb BP |
1877 | return false; |
1878 | } | |
1879 | } | |
1880 | return true; | |
1881 | } | |
e9f8f936 BP |
1882 | |
1883 | static void | |
1884 | ovsdb_datum_reallocate(struct ovsdb_datum *a, const struct ovsdb_type *type, | |
1885 | unsigned int capacity) | |
1886 | { | |
1887 | a->keys = xrealloc(a->keys, capacity * sizeof *a->keys); | |
bd76d25d | 1888 | if (type->value.type != OVSDB_TYPE_VOID) { |
e9f8f936 BP |
1889 | a->values = xrealloc(a->values, capacity * sizeof *a->values); |
1890 | } | |
1891 | } | |
1892 | ||
2f47998b BP |
1893 | /* Removes the element with index 'idx' from 'datum', which has type 'type'. |
1894 | * If 'idx' is not the last element in 'datum', then the removed element is | |
1895 | * replaced by the (former) last element. | |
1896 | * | |
1897 | * This function does not maintain ovsdb_datum invariants. Use | |
1898 | * ovsdb_datum_sort() to check and restore these invariants. */ | |
1899 | void | |
1900 | ovsdb_datum_remove_unsafe(struct ovsdb_datum *datum, size_t idx, | |
1901 | const struct ovsdb_type *type) | |
e9f8f936 | 1902 | { |
bd76d25d | 1903 | ovsdb_atom_destroy(&datum->keys[idx], type->key.type); |
2f47998b | 1904 | datum->keys[idx] = datum->keys[datum->n - 1]; |
bd76d25d BP |
1905 | if (type->value.type != OVSDB_TYPE_VOID) { |
1906 | ovsdb_atom_destroy(&datum->values[idx], type->value.type); | |
2f47998b BP |
1907 | datum->values[idx] = datum->values[datum->n - 1]; |
1908 | } | |
1909 | datum->n--; | |
1910 | } | |
1911 | ||
1912 | /* Adds the element with the given 'key' and 'value' to 'datum', which must | |
1ab39058 LR |
1913 | * have the specified 'type'. Optionally if 'range_end_atom' is not |
1914 | * a null pointer, adds a set of integers to 'datum' from inclusive | |
1915 | * range ['key', 'range_end_atom']. | |
2f47998b BP |
1916 | * |
1917 | * This function always allocates memory, so it is not an efficient way to add | |
1918 | * a number of elements to a datum. | |
1919 | * | |
1ab39058 LR |
1920 | * When adding a range of integers, this function allocates the memory once |
1921 | * for the whole range. | |
1922 | * | |
2f47998b BP |
1923 | * This function does not maintain ovsdb_datum invariants. Use |
1924 | * ovsdb_datum_sort() to check and restore these invariants. (But a datum with | |
1925 | * 0 or 1 elements cannot violate the invariants anyhow.) */ | |
1926 | void | |
1927 | ovsdb_datum_add_unsafe(struct ovsdb_datum *datum, | |
1928 | const union ovsdb_atom *key, | |
1929 | const union ovsdb_atom *value, | |
1ab39058 LR |
1930 | const struct ovsdb_type *type, |
1931 | const union ovsdb_atom *range_end_atom) | |
2f47998b | 1932 | { |
1ab39058 LR |
1933 | size_t idx = datum->n; |
1934 | datum->n += range_end_atom ? | |
1935 | (range_end_atom->integer - key->integer + 1) : 1; | |
2f47998b | 1936 | datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys); |
1ab39058 LR |
1937 | if (range_end_atom && key->integer <= range_end_atom->integer) { |
1938 | for (int64_t i = key->integer; i <= range_end_atom->integer; i++) { | |
1939 | datum->keys[idx++].integer = i; | |
1940 | } | |
1941 | } else { | |
1942 | ovsdb_atom_clone(&datum->keys[idx], key, type->key.type); | |
1943 | if (type->value.type != OVSDB_TYPE_VOID) { | |
1944 | datum->values = xrealloc(datum->values, | |
1945 | datum->n * sizeof *datum->values); | |
1946 | ovsdb_atom_clone(&datum->values[idx], value, type->value.type); | |
1947 | } | |
e9f8f936 | 1948 | } |
e9f8f936 BP |
1949 | } |
1950 | ||
1951 | void | |
2f47998b BP |
1952 | ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b, |
1953 | const struct ovsdb_type *type, bool replace) | |
e9f8f936 | 1954 | { |
e9f8f936 | 1955 | unsigned int n; |
2f47998b | 1956 | size_t bi; |
e9f8f936 | 1957 | |
e9f8f936 | 1958 | n = a->n; |
2f47998b BP |
1959 | for (bi = 0; bi < b->n; bi++) { |
1960 | unsigned int ai; | |
1961 | ||
bd76d25d | 1962 | ai = ovsdb_datum_find_key(a, &b->keys[bi], type->key.type); |
2f47998b | 1963 | if (ai == UINT_MAX) { |
e9f8f936 | 1964 | if (n == a->n) { |
2f47998b | 1965 | ovsdb_datum_reallocate(a, type, a->n + (b->n - bi)); |
e9f8f936 | 1966 | } |
bd76d25d BP |
1967 | ovsdb_atom_clone(&a->keys[n], &b->keys[bi], type->key.type); |
1968 | if (type->value.type != OVSDB_TYPE_VOID) { | |
2f47998b | 1969 | ovsdb_atom_clone(&a->values[n], &b->values[bi], |
bd76d25d | 1970 | type->value.type); |
e9f8f936 BP |
1971 | } |
1972 | n++; | |
bd76d25d BP |
1973 | } else if (replace && type->value.type != OVSDB_TYPE_VOID) { |
1974 | ovsdb_atom_destroy(&a->values[ai], type->value.type); | |
2f47998b | 1975 | ovsdb_atom_clone(&a->values[ai], &b->values[bi], |
bd76d25d | 1976 | type->value.type); |
e9f8f936 BP |
1977 | } |
1978 | } | |
1979 | if (n != a->n) { | |
e9f8f936 | 1980 | a->n = n; |
500db308 | 1981 | ovs_assert(!ovsdb_datum_sort(a, type->key.type)); |
e9f8f936 BP |
1982 | } |
1983 | } | |
1984 | ||
1985 | void | |
1986 | ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type, | |
1987 | const struct ovsdb_datum *b, | |
1988 | const struct ovsdb_type *b_type) | |
1989 | { | |
1990 | bool changed = false; | |
1991 | size_t i; | |
1992 | ||
cb22974d BP |
1993 | ovs_assert(a_type->key.type == b_type->key.type); |
1994 | ovs_assert(a_type->value.type == b_type->value.type | |
1995 | || b_type->value.type == OVSDB_TYPE_VOID); | |
e9f8f936 BP |
1996 | |
1997 | /* XXX The big-O of this could easily be improved. */ | |
1998 | for (i = 0; i < a->n; ) { | |
1999 | unsigned int idx = ovsdb_datum_find(a, i, b, b_type); | |
2000 | if (idx != UINT_MAX) { | |
2001 | changed = true; | |
2f47998b | 2002 | ovsdb_datum_remove_unsafe(a, i, a_type); |
e9f8f936 BP |
2003 | } else { |
2004 | i++; | |
2005 | } | |
2006 | } | |
2007 | if (changed) { | |
bfc96d9b | 2008 | ovsdb_datum_sort_assert(a, a_type->key.type); |
e9f8f936 BP |
2009 | } |
2010 | } | |
f85f8ebb | 2011 | \f |
f85f8ebb BP |
2012 | struct ovsdb_symbol_table * |
2013 | ovsdb_symbol_table_create(void) | |
2014 | { | |
2015 | struct ovsdb_symbol_table *symtab = xmalloc(sizeof *symtab); | |
2016 | shash_init(&symtab->sh); | |
2017 | return symtab; | |
2018 | } | |
2019 | ||
2020 | void | |
2021 | ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *symtab) | |
2022 | { | |
2023 | if (symtab) { | |
506051fc | 2024 | shash_destroy_free_data(&symtab->sh); |
f85f8ebb BP |
2025 | free(symtab); |
2026 | } | |
2027 | } | |
2028 | ||
2d2d6d4a | 2029 | struct ovsdb_symbol * |
f85f8ebb BP |
2030 | ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab, |
2031 | const char *name) | |
2032 | { | |
2033 | return shash_find_data(&symtab->sh, name); | |
2034 | } | |
2035 | ||
fbf925e4 | 2036 | struct ovsdb_symbol * |
f85f8ebb | 2037 | ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name, |
e9387de4 | 2038 | const struct uuid *uuid, bool created) |
f85f8ebb | 2039 | { |
2d2d6d4a BP |
2040 | struct ovsdb_symbol *symbol; |
2041 | ||
cb22974d | 2042 | ovs_assert(!ovsdb_symbol_table_get(symtab, name)); |
2d2d6d4a BP |
2043 | symbol = xmalloc(sizeof *symbol); |
2044 | symbol->uuid = *uuid; | |
e9387de4 | 2045 | symbol->created = created; |
c5f341ab BP |
2046 | symbol->strong_ref = false; |
2047 | symbol->weak_ref = false; | |
2d2d6d4a | 2048 | shash_add(&symtab->sh, name, symbol); |
fbf925e4 BP |
2049 | return symbol; |
2050 | } | |
2051 | ||
2052 | struct ovsdb_symbol * | |
2053 | ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab, | |
2054 | const char *name) | |
2055 | { | |
2056 | struct ovsdb_symbol *symbol; | |
2057 | ||
2058 | symbol = ovsdb_symbol_table_get(symtab, name); | |
2059 | if (!symbol) { | |
2060 | struct uuid uuid; | |
2061 | ||
2062 | uuid_generate(&uuid); | |
2063 | symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false); | |
2064 | } | |
2065 | return symbol; | |
f85f8ebb | 2066 | } |
1a6f1cef AZ |
2067 | \f |
2068 | /* APIs for Generating and apply diffs. */ | |
2069 | ||
2070 | /* Generate a difference ovsdb_dataum between 'old' and 'new'. | |
2071 | * 'new' can be regenerated by applying the difference to the 'old'. | |
2072 | * | |
2073 | * The diff operation is reversible. Given 'old', | |
2074 | * 'new' can be recreated by applying diff to 'old'. | |
2075 | * | |
2076 | * Thus | |
2077 | * Let d = 'old' diff 'new' | |
2078 | * then 'new' = 'old' diff d | |
2079 | * | |
2080 | * The 'diff' datum is always safe; the orders of keys are maintained | |
2081 | * since they are added in order. */ | |
2082 | void | |
2083 | ovsdb_datum_diff(struct ovsdb_datum *diff, | |
2084 | const struct ovsdb_datum *old, | |
2085 | const struct ovsdb_datum *new, | |
2086 | const struct ovsdb_type *type) | |
2087 | { | |
2088 | size_t oi, ni; | |
2089 | ||
2090 | ovsdb_datum_init_empty(diff); | |
2091 | if (!ovsdb_type_is_composite(type)) { | |
2092 | ovsdb_datum_clone(diff, new, type); | |
2093 | return; | |
2094 | } | |
2095 | ||
2096 | /* Generate the diff in O(n) time. */ | |
2097 | for (oi = ni = 0; oi < old->n && ni < new->n; ) { | |
2098 | int c = ovsdb_atom_compare_3way(&old->keys[oi], &new->keys[ni], | |
2099 | type->key.type); | |
2100 | if (c < 0) { | |
2101 | ovsdb_datum_add_unsafe(diff, &old->keys[oi], &old->values[oi], | |
1ab39058 | 2102 | type, NULL); |
1a6f1cef AZ |
2103 | oi++; |
2104 | } else if (c > 0) { | |
2105 | ovsdb_datum_add_unsafe(diff, &new->keys[ni], &new->values[ni], | |
1ab39058 | 2106 | type, NULL); |
1a6f1cef AZ |
2107 | ni++; |
2108 | } else { | |
2109 | if (type->value.type != OVSDB_TYPE_VOID && | |
2110 | ovsdb_atom_compare_3way(&old->values[oi], &new->values[ni], | |
2111 | type->value.type)) { | |
2112 | ovsdb_datum_add_unsafe(diff, &new->keys[ni], &new->values[ni], | |
1ab39058 | 2113 | type, NULL); |
1a6f1cef AZ |
2114 | } |
2115 | oi++; ni++; | |
2116 | } | |
2117 | } | |
2118 | ||
2119 | for (; oi < old->n; oi++) { | |
1ab39058 LR |
2120 | ovsdb_datum_add_unsafe(diff, &old->keys[oi], &old->values[oi], |
2121 | type, NULL); | |
1a6f1cef AZ |
2122 | } |
2123 | ||
2124 | for (; ni < new->n; ni++) { | |
1ab39058 LR |
2125 | ovsdb_datum_add_unsafe(diff, &new->keys[ni], &new->values[ni], |
2126 | type, NULL); | |
1a6f1cef AZ |
2127 | } |
2128 | } | |
2129 | ||
2130 | /* Apply 'diff' to 'old' to regenerate 'new'. | |
2131 | * | |
2132 | * Return NULL if the 'new' is successfully generated, otherwise, return | |
2133 | * ovsdb_error and the stat of 'new' is indeterministic. */ | |
2134 | struct ovsdb_error * | |
2135 | ovsdb_datum_apply_diff(struct ovsdb_datum *new, | |
2136 | const struct ovsdb_datum *old, | |
2137 | const struct ovsdb_datum *diff, | |
2138 | const struct ovsdb_type *type) | |
2139 | { | |
1a6f1cef AZ |
2140 | ovsdb_datum_diff(new, old, diff, type); |
2141 | ||
2142 | /* Make sure member size of 'new' conforms to type. */ | |
2143 | if (new->n < type->n_min || new->n > type->n_max) { | |
2144 | ovsdb_datum_destroy(new, type); | |
2145 | return ovsdb_error(NULL, "Datum crated by diff has size error"); | |
2146 | } | |
2147 | ||
2148 | return NULL; | |
2149 | } | |
2150 | ||
0194f33a BP |
2151 | \f |
2152 | /* Extracts a token from the beginning of 's' and returns a pointer just after | |
2153 | * the token. Stores the token itself into '*outp', which the caller is | |
2154 | * responsible for freeing (with free()). | |
2155 | * | |
2156 | * If 's[0]' is a delimiter, the returned token is the empty string. | |
2157 | * | |
2158 | * A token extends from 's' to the first delimiter, as defined by | |
2159 | * ovsdb_token_is_delim(), or until the end of the string. A delimiter can be | |
2160 | * escaped with a backslash, in which case the backslash does not appear in the | |
2161 | * output. Double quotes also cause delimiters to be ignored, but the double | |
2162 | * quotes are retained in the output. (Backslashes inside double quotes are | |
2163 | * not removed, either.) | |
2164 | */ | |
1bc6ff29 BP |
2165 | char * |
2166 | ovsdb_token_parse(const char **s, char **outp) | |
0194f33a BP |
2167 | { |
2168 | const char *p; | |
2169 | struct ds out; | |
2170 | bool in_quotes; | |
1bc6ff29 | 2171 | char *error; |
0194f33a BP |
2172 | |
2173 | ds_init(&out); | |
2174 | in_quotes = false; | |
1bc6ff29 | 2175 | for (p = *s; *p != '\0'; ) { |
0194f33a BP |
2176 | int c = *p++; |
2177 | if (c == '\\') { | |
2178 | if (in_quotes) { | |
2179 | ds_put_char(&out, '\\'); | |
2180 | } | |
2181 | if (!*p) { | |
1bc6ff29 BP |
2182 | error = xasprintf("%s: backslash at end of argument", *s); |
2183 | goto error; | |
0194f33a BP |
2184 | } |
2185 | ds_put_char(&out, *p++); | |
2186 | } else if (!in_quotes && ovsdb_token_is_delim(c)) { | |
2187 | p--; | |
2188 | break; | |
2189 | } else { | |
2190 | ds_put_char(&out, c); | |
2191 | if (c == '"') { | |
2192 | in_quotes = !in_quotes; | |
2193 | } | |
2194 | } | |
2195 | } | |
2196 | if (in_quotes) { | |
1bc6ff29 BP |
2197 | error = xasprintf("%s: quoted string extends past end of argument", |
2198 | *s); | |
2199 | goto error; | |
0194f33a BP |
2200 | } |
2201 | *outp = ds_cstr(&out); | |
1bc6ff29 BP |
2202 | *s = p; |
2203 | return NULL; | |
2204 | ||
2205 | error: | |
2206 | ds_destroy(&out); | |
2207 | *outp = NULL; | |
2208 | return error; | |
0194f33a BP |
2209 | } |
2210 | ||
2211 | /* Returns true if 'c' delimits tokens, or if 'c' is 0, and false otherwise. */ | |
2212 | bool | |
2213 | ovsdb_token_is_delim(unsigned char c) | |
2214 | { | |
e89e5374 | 2215 | return strchr(":=, []{}!<>", c) != NULL; |
0194f33a | 2216 | } |
1ab39058 LR |
2217 | |
2218 | struct ovsdb_error * | |
2219 | ovsdb_atom_range_check_size(int64_t range_start, int64_t range_end) | |
2220 | { | |
2221 | if ((uint64_t) range_end - (uint64_t) range_start | |
2222 | >= MAX_OVSDB_ATOM_RANGE_SIZE) { | |
2223 | return ovsdb_error("constraint violation", | |
2224 | "Range \"%"PRId64"-%"PRId64"\" is too big. " | |
2225 | "Maximum allowed size is %d.", | |
2226 | range_start, range_end, MAX_OVSDB_ATOM_RANGE_SIZE); | |
2227 | } | |
2228 | return NULL; | |
2229 | } | |
fe0fb885 BP |
2230 | \f |
2231 | char * | |
2232 | ovsdb_data_row_name(const struct uuid *uuid) | |
2233 | { | |
2234 | char *name; | |
2235 | char *p; | |
2236 | ||
2237 | name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid)); | |
2238 | for (p = name; *p != '\0'; p++) { | |
2239 | if (*p == '-') { | |
2240 | *p = '_'; | |
2241 | } | |
2242 | } | |
2243 | ||
2244 | return name; | |
2245 | } |