forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovsdb-data.c
1613 lines (1402 loc) · 45.5 KB
/
ovsdb-data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2009, 2010 Nicira Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "ovsdb-data.h"
#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include "dynamic-string.h"
#include "hash.h"
#include "ovsdb-error.h"
#include "json.h"
#include "shash.h"
#include "sort.h"
#include "unicode.h"
static struct json *
wrap_json(const char *name, struct json *wrapped)
{
return json_array_create_2(json_string_create(name), wrapped);
}
void
ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
{
switch (type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
case OVSDB_TYPE_INTEGER:
atom->integer = 0;
break;
case OVSDB_TYPE_REAL:
atom->real = 0.0;
break;
case OVSDB_TYPE_BOOLEAN:
atom->boolean = false;
break;
case OVSDB_TYPE_STRING:
atom->string = xmemdup("", 1);
break;
case OVSDB_TYPE_UUID:
uuid_zero(&atom->uuid);
break;
case OVSDB_N_TYPES:
default:
NOT_REACHED();
}
}
bool
ovsdb_atom_is_default(const union ovsdb_atom *atom,
enum ovsdb_atomic_type type)
{
switch (type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return atom->integer == 0;
case OVSDB_TYPE_REAL:
return atom->real == 0.0;
case OVSDB_TYPE_BOOLEAN:
return atom->boolean == false;
case OVSDB_TYPE_STRING:
return atom->string[0] == '\0';
case OVSDB_TYPE_UUID:
return uuid_is_zero(&atom->uuid);
case OVSDB_N_TYPES:
default:
NOT_REACHED();
}
}
void
ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old,
enum ovsdb_atomic_type type)
{
switch (type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
case OVSDB_TYPE_INTEGER:
new->integer = old->integer;
break;
case OVSDB_TYPE_REAL:
new->real = old->real;
break;
case OVSDB_TYPE_BOOLEAN:
new->boolean = old->boolean;
break;
case OVSDB_TYPE_STRING:
new->string = xstrdup(old->string);
break;
case OVSDB_TYPE_UUID:
new->uuid = old->uuid;
break;
case OVSDB_N_TYPES:
default:
NOT_REACHED();
}
}
void
ovsdb_atom_swap(union ovsdb_atom *a, union ovsdb_atom *b)
{
union ovsdb_atom tmp = *a;
*a = *b;
*b = tmp;
}
uint32_t
ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
uint32_t basis)
{
switch (type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return hash_int(atom->integer, basis);
case OVSDB_TYPE_REAL:
return hash_double(atom->real, basis);
case OVSDB_TYPE_BOOLEAN:
return hash_boolean(atom->boolean, basis);
case OVSDB_TYPE_STRING:
return hash_string(atom->string, basis);
case OVSDB_TYPE_UUID:
return hash_int(uuid_hash(&atom->uuid), basis);
case OVSDB_N_TYPES:
default:
NOT_REACHED();
}
}
int
ovsdb_atom_compare_3way(const union ovsdb_atom *a,
const union ovsdb_atom *b,
enum ovsdb_atomic_type type)
{
switch (type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return a->integer < b->integer ? -1 : a->integer > b->integer;
case OVSDB_TYPE_REAL:
return a->real < b->real ? -1 : a->real > b->real;
case OVSDB_TYPE_BOOLEAN:
return a->boolean - b->boolean;
case OVSDB_TYPE_STRING:
return strcmp(a->string, b->string);
case OVSDB_TYPE_UUID:
return uuid_compare_3way(&a->uuid, &b->uuid);
case OVSDB_N_TYPES:
default:
NOT_REACHED();
}
}
static struct ovsdb_error *
unwrap_json(const struct json *json, const char *name,
enum json_type value_type, const struct json **value)
{
if (json->type != JSON_ARRAY
|| json->u.array.n != 2
|| json->u.array.elems[0]->type != JSON_STRING
|| (name && strcmp(json->u.array.elems[0]->u.string, name))
|| json->u.array.elems[1]->type != value_type)
{
return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name,
json_type_to_string(value_type));
}
*value = json->u.array.elems[1];
return NULL;
}
static struct ovsdb_error *
parse_json_pair(const struct json *json,
const struct json **elem0, const struct json **elem1)
{
if (json->type != JSON_ARRAY || json->u.array.n != 2) {
return ovsdb_syntax_error(json, NULL, "expected 2-element array");
}
*elem0 = json->u.array.elems[0];
*elem1 = json->u.array.elems[1];
return NULL;
}
static struct ovsdb_error * WARN_UNUSED_RESULT
ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
struct ovsdb_symbol_table *symtab)
{
struct ovsdb_error *error0;
const struct json *value;
error0 = unwrap_json(json, "uuid", JSON_STRING, &value);
if (!error0) {
const char *uuid_string = json_string(value);
if (!uuid_from_string(uuid, uuid_string)) {
return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
uuid_string);
}
} else if (symtab) {
struct ovsdb_error *error1;
error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
if (!error1) {
const char *name = json_string(value);
ovsdb_error_destroy(error0);
*uuid = ovsdb_symbol_table_insert(symtab, name)->uuid;
return NULL;
}
ovsdb_error_destroy(error1);
}
return error0;
}
static struct ovsdb_error * WARN_UNUSED_RESULT
ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
const struct json *json,
struct ovsdb_symbol_table *symtab)
{
switch (type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
case OVSDB_TYPE_INTEGER:
if (json->type == JSON_INTEGER) {
atom->integer = json->u.integer;
return NULL;
}
break;
case OVSDB_TYPE_REAL:
if (json->type == JSON_INTEGER) {
atom->real = json->u.integer;
return NULL;
} else if (json->type == JSON_REAL) {
atom->real = json->u.real;
return NULL;
}
break;
case OVSDB_TYPE_BOOLEAN:
if (json->type == JSON_TRUE) {
atom->boolean = true;
return NULL;
} else if (json->type == JSON_FALSE) {
atom->boolean = false;
return NULL;
}
break;
case OVSDB_TYPE_STRING:
if (json->type == JSON_STRING) {
atom->string = xstrdup(json->u.string);
return NULL;
}
break;
case OVSDB_TYPE_UUID:
return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab);
case OVSDB_N_TYPES:
default:
NOT_REACHED();
}
return ovsdb_syntax_error(json, NULL, "expected %s",
ovsdb_atomic_type_to_string(type));
}
struct ovsdb_error *
ovsdb_atom_from_json(union ovsdb_atom *atom,
const struct ovsdb_base_type *base,
const struct json *json,
struct ovsdb_symbol_table *symtab)
{
struct ovsdb_error *error;
error = ovsdb_atom_from_json__(atom, base->type, json, symtab);
if (error) {
return error;
}
error = ovsdb_atom_check_constraints(atom, base);
if (error) {
ovsdb_atom_destroy(atom, base->type);
}
return error;
}
struct json *
ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
{
switch (type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return json_integer_create(atom->integer);
case OVSDB_TYPE_REAL:
return json_real_create(atom->real);
case OVSDB_TYPE_BOOLEAN:
return json_boolean_create(atom->boolean);
case OVSDB_TYPE_STRING:
return json_string_create(atom->string);
case OVSDB_TYPE_UUID:
return wrap_json("uuid", json_string_create_nocopy(
xasprintf(UUID_FMT, UUID_ARGS(&atom->uuid))));
case OVSDB_N_TYPES:
default:
NOT_REACHED();
}
}
static 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);
}
/* Our JSON input routines map negative zero to zero, so do that here
* too for consistency. */
if (atom->real == 0.0) {
atom->real = 0.0;
}
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;
}
/* Initializes 'atom' to a value of type 'base' 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,
const struct ovsdb_base_type *base, const char *s)
{
struct ovsdb_error *error;
char *msg;
msg = ovsdb_atom_from_string__(atom, base->type, s);
if (msg) {
return msg;
}
error = ovsdb_atom_check_constraints(atom, base);
if (error) {
msg = ovsdb_error_to_string(error);
ovsdb_error_destroy(error);
}
return msg;
}
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 struct ovsdb_error *
check_string_constraints(const char *s,
const struct ovsdb_string_constraints *c)
{
size_t n_chars;
char *msg;
msg = utf8_validate(s, &n_chars);
if (msg) {
struct ovsdb_error *error;
error = ovsdb_error("constraint violation",
"\"%s\" is not a valid UTF-8 string: %s",
s, msg);
free(msg);
return error;
}
if (n_chars < c->minLen) {
return ovsdb_error(
"constraint violation",
"\"%s\" length %zu is less than minimum allowed "
"length %u", s, n_chars, c->minLen);
} else if (n_chars > c->maxLen) {
return ovsdb_error(
"constraint violation",
"\"%s\" length %zu is greater than maximum allowed "
"length %u", s, n_chars, c->maxLen);
}
#if HAVE_PCRE
if (c->re) {
int retval;
retval = pcre_exec(c->re, NULL, s, strlen(s), 0,
PCRE_ANCHORED | PCRE_NO_UTF8_CHECK, NULL, 0);
if (retval == PCRE_ERROR_NOMATCH) {
if (c->reComment) {
return ovsdb_error("constraint violation",
"\"%s\" is not a %s", s, c->reComment);
} else {
return ovsdb_error("constraint violation",
"\"%s\" does not match regular expression "
"/%s/", s, c->reMatch);
}
} else if (retval < 0) {
/* PCRE doesn't have a function to translate an error code to a
* description. Bizarre. See pcreapi(3) for error details. */
return ovsdb_error("internal error", "PCRE returned error %d",
retval);
}
}
#endif /* HAVE_PCRE */
return NULL;
}
/* Checks whether 'atom' meets the constraints (if any) defined in 'base'.
* (base->type must specify 'atom''s type.) Returns a null pointer if the
* constraints are met, otherwise an error that explains the violation.
*
* Checking UUID constraints is deferred to transaction commit time, so this
* function does nothing for UUID constraints. */
struct ovsdb_error *
ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
const struct ovsdb_base_type *base)
{
switch (base->type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
case OVSDB_TYPE_INTEGER:
if (atom->integer >= base->u.integer.min
&& atom->integer <= base->u.integer.max) {
return NULL;
} else if (base->u.integer.min != INT64_MIN) {
if (base->u.integer.max != INT64_MAX) {
return ovsdb_error("constraint violation",
"%"PRId64" is not in the valid range "
"%"PRId64" to %"PRId64" (inclusive)",
atom->integer,
base->u.integer.min, base->u.integer.max);
} else {
return ovsdb_error("constraint violation",
"%"PRId64" is less than minimum allowed "
"value %"PRId64,
atom->integer, base->u.integer.min);
}
} else {
return ovsdb_error("constraint violation",
"%"PRId64" is greater than maximum allowed "
"value %"PRId64,
atom->integer, base->u.integer.max);
}
NOT_REACHED();
case OVSDB_TYPE_REAL:
if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
return NULL;
} else if (base->u.real.min != -DBL_MAX) {
if (base->u.real.max != DBL_MAX) {
return ovsdb_error("constraint violation",
"%.*g is not in the valid range "
"%.*g to %.*g (inclusive)",
DBL_DIG, atom->real,
DBL_DIG, base->u.real.min,
DBL_DIG, base->u.real.max);
} else {
return ovsdb_error("constraint violation",
"%.*g is less than minimum allowed "
"value %.*g",
DBL_DIG, atom->real,
DBL_DIG, base->u.real.min);
}
} else {
return ovsdb_error("constraint violation",
"%.*g is greater than maximum allowed "
"value %.*g",
DBL_DIG, atom->real,
DBL_DIG, base->u.real.max);
}
NOT_REACHED();
case OVSDB_TYPE_BOOLEAN:
return NULL;
case OVSDB_TYPE_STRING:
return check_string_constraints(atom->string, &base->u.string);
case OVSDB_TYPE_UUID:
return NULL;
case OVSDB_N_TYPES:
default:
NOT_REACHED();
}
}
static union ovsdb_atom *
alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
{
if (type != OVSDB_TYPE_VOID && n) {
union ovsdb_atom *atoms;
unsigned int i;
atoms = xmalloc(n * sizeof *atoms);
for (i = 0; i < n; i++) {
ovsdb_atom_init_default(&atoms[i], type);
}
return atoms;
} else {
/* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
* treated as xmalloc(1). */
return NULL;
}
}
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)
{
datum->n = type->n_min;
datum->keys = alloc_default_atoms(type->key.type, datum->n);
datum->values = alloc_default_atoms(type->value.type, datum->n);
}
bool
ovsdb_datum_is_default(const struct ovsdb_datum *datum,
const struct ovsdb_type *type)
{
size_t i;
if (datum->n != type->n_min) {
return false;
}
for (i = 0; i < datum->n; i++) {
if (!ovsdb_atom_is_default(&datum->keys[i], type->key.type)) {
return false;
}
if (type->value.type != OVSDB_TYPE_VOID
&& !ovsdb_atom_is_default(&datum->values[i], type->value.type)) {
return false;
}
}
return true;
}
static union ovsdb_atom *
clone_atoms(const union ovsdb_atom *old, enum ovsdb_atomic_type type, size_t n)
{
if (type != OVSDB_TYPE_VOID && n) {
union ovsdb_atom *new;
unsigned int i;
new = xmalloc(n * sizeof *new);
for (i = 0; i < n; i++) {
ovsdb_atom_clone(&new[i], &old[i], type);
}
return new;
} else {
/* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
* treated as xmalloc(1). */
return NULL;
}
}
void
ovsdb_datum_clone(struct ovsdb_datum *new, const struct ovsdb_datum *old,
const struct ovsdb_type *type)
{
unsigned int n = old->n;
new->n = n;
new->keys = clone_atoms(old->keys, type->key.type, n);
new->values = clone_atoms(old->values, type->value.type, n);
}
static void
free_data(enum ovsdb_atomic_type type,
union ovsdb_atom *atoms, size_t n_atoms)
{
if (ovsdb_atom_needs_destruction(type)) {
unsigned int i;
for (i = 0; i < n_atoms; i++) {
ovsdb_atom_destroy(&atoms[i], type);
}
}
free(atoms);
}
void
ovsdb_datum_destroy(struct ovsdb_datum *datum, const struct ovsdb_type *type)
{
free_data(type->key.type, datum->keys, datum->n);
free_data(type->value.type, datum->values, datum->n);
}
void
ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b)
{
struct ovsdb_datum tmp = *a;
*a = *b;
*b = tmp;
}
struct ovsdb_datum_sort_cbdata {
const struct ovsdb_type *type;
struct ovsdb_datum *datum;
};
static int
ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
{
struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
return ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
&cbdata->datum->keys[b],
cbdata->type->key.type);
}
static void
ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
{
struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
if (cbdata->type->value.type != OVSDB_TYPE_VOID) {
ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
}
}
struct ovsdb_error *
ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type)
{
if (datum->n < 2) {
return NULL;
} else {
struct ovsdb_datum_sort_cbdata cbdata;
size_t i;
cbdata.type = type;
cbdata.datum = datum;
sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
&cbdata);
for (i = 0; i < datum->n - 1; i++) {
if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
type->key.type)) {
if (ovsdb_type_is_map(type)) {
return ovsdb_error(NULL, "map contains duplicate key");
} else {
return ovsdb_error(NULL, "set contains duplicate");
}
}
}
return NULL;
}
}
/* Checks that each of the atoms in 'datum' conforms to the constraints
* specified by its 'type'. Returns an error if a constraint is violated,
* otherwise a null pointer.
*
* This function is not commonly useful because the most ordinary way to obtain
* a datum is ultimately via ovsdb_atom_from_string() or
* ovsdb_atom_from_json(), which check constraints themselves. */
struct ovsdb_error *
ovsdb_datum_check_constraints(const struct ovsdb_datum *datum,
const struct ovsdb_type *type)
{
struct ovsdb_error *error;
unsigned int i;
for (i = 0; i < datum->n; i++) {
error = ovsdb_atom_check_constraints(&datum->keys[i], &type->key);
if (error) {
return error;
}
}
if (type->value.type != OVSDB_TYPE_VOID) {
for (i = 0; i < datum->n; i++) {
error = ovsdb_atom_check_constraints(&datum->values[i],
&type->value);
if (error) {
return error;
}
}
}
return NULL;
}
struct ovsdb_error *
ovsdb_datum_from_json(struct ovsdb_datum *datum,
const struct ovsdb_type *type,
const struct json *json,
struct ovsdb_symbol_table *symtab)
{
struct ovsdb_error *error;
if (ovsdb_type_is_map(type)
|| (json->type == JSON_ARRAY
&& json->u.array.n > 0
&& json->u.array.elems[0]->type == JSON_STRING
&& !strcmp(json->u.array.elems[0]->u.string, "set"))) {
bool is_map = ovsdb_type_is_map(type);
const char *class = is_map ? "map" : "set";
const struct json *inner;
unsigned int i;
size_t n;
error = unwrap_json(json, class, JSON_ARRAY, &inner);
if (error) {
return error;
}
n = inner->u.array.n;
if (n < type->n_min || n > type->n_max) {
return ovsdb_syntax_error(json, NULL, "%s must have %u to "
"%u members but %zu are present",
class, type->n_min, type->n_max, n);
}
datum->n = 0;
datum->keys = xmalloc(n * sizeof *datum->keys);
datum->values = is_map ? xmalloc(n * sizeof *datum->values) : NULL;
for (i = 0; i < n; i++) {
const struct json *element = inner->u.array.elems[i];
const struct json *key = NULL;
const struct json *value = NULL;
if (!is_map) {
key = element;
} else {
error = parse_json_pair(element, &key, &value);
if (error) {
goto error;
}
}
error = ovsdb_atom_from_json(&datum->keys[i], &type->key,
key, symtab);
if (error) {
goto error;
}
if (is_map) {
error = ovsdb_atom_from_json(&datum->values[i],
&type->value, value, symtab);
if (error) {
ovsdb_atom_destroy(&datum->keys[i], type->key.type);
goto error;
}
}
datum->n++;
}
error = ovsdb_datum_sort(datum, type);
if (error) {
goto error;
}
return NULL;
error:
ovsdb_datum_destroy(datum, type);
return error;
} else {
datum->n = 1;
datum->keys = xmalloc(sizeof *datum->keys);
datum->values = NULL;
error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
json, symtab);
if (error) {
free(datum->keys);
}
return error;
}
}
struct json *
ovsdb_datum_to_json(const struct ovsdb_datum *datum,
const struct ovsdb_type *type)
{
/* These tests somewhat tolerate a 'datum' that does not exactly match
* 'type', in particular a datum with 'n' not in the allowed range. */
if (datum->n == 1 && !ovsdb_type_is_map(type)) {
return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
} else if (type->value.type == OVSDB_TYPE_VOID) {
struct json **elems;
size_t i;
elems = xmalloc(datum->n * sizeof *elems);
for (i = 0; i < datum->n; i++) {
elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
}
return wrap_json("set", json_array_create(elems, datum->n));
} else {