forked from CESNET/libyang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.c
More file actions
3511 lines (3054 loc) · 121 KB
/
diff.c
File metadata and controls
3511 lines (3054 loc) · 121 KB
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
/**
* @file diff.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief diff functions
*
* Copyright (c) 2020 - 2025 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE /* asprintf, strdup */
#include "diff.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "context.h"
#include "dict.h"
#include "log.h"
#include "ly_common.h"
#include "plugins_exts.h"
#include "plugins_exts/metadata.h"
#include "plugins_internal.h"
#include "plugins_types.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
#include "tree_data_internal.h"
#include "tree_edit.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
#define LOGERR_META(ctx, meta_name, node) \
{ \
char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
LOGERR(ctx, LY_EINVAL, "Failed to find metadata \"%s\" for node \"%s\".", meta_name, __path); \
free(__path); \
}
#define LOGERR_NOINST(ctx, node) \
{ \
char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
LOGERR(ctx, LY_EINVAL, "Failed to find node \"%s\" instance in data.", __path); \
free(__path); \
}
#define LOGERR_UNEXPVAL(ctx, node, data_source) \
{ \
char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
LOGERR(ctx, LY_EINVAL, "Unexpected value of node \"%s\" in %s.", __path, data_source); \
free(__path); \
}
#define LOGERR_MERGEOP(ctx, node, src_op, trg_op) \
{ \
char *__path = lyd_path(node, LYD_PATH_STD, NULL, 0); \
LOGERR(ctx, LY_EINVAL, "Unable to merge operation \"%s\" with \"%s\" for node \"%s\".", \
lyd_diff_op2str(trg_op), lyd_diff_op2str(src_op), __path); \
free(__path); \
}
static const char *
lyd_diff_op2str(enum lyd_diff_op op)
{
switch (op) {
case LYD_DIFF_OP_CREATE:
return "create";
case LYD_DIFF_OP_DELETE:
return "delete";
case LYD_DIFF_OP_REPLACE:
return "replace";
case LYD_DIFF_OP_NONE:
return "none";
}
LOGINT(NULL);
return NULL;
}
static enum lyd_diff_op
lyd_diff_str2op(const char *str)
{
switch (str[0]) {
case 'c':
assert(!strcmp(str, "create"));
return LYD_DIFF_OP_CREATE;
case 'd':
assert(!strcmp(str, "delete"));
return LYD_DIFF_OP_DELETE;
case 'r':
assert(!strcmp(str, "replace"));
return LYD_DIFF_OP_REPLACE;
case 'n':
assert(!strcmp(str, "none"));
return LYD_DIFF_OP_NONE;
}
LOGINT(NULL);
return 0;
}
/**
* @brief Create diff metadata for a nested user-ordered node with the effective operation "create".
*
* @param[in] node User-rodered node to update.
* @return LY_ERR value.
*/
static LY_ERR
lyd_diff_add_create_nested_userord(struct lyd_node *node)
{
LY_ERR rc = LY_SUCCESS;
const char *meta_name, *meta_val;
size_t buflen = 0, bufused = 0;
uint32_t pos;
char *dyn = NULL;
assert(lysc_is_userordered(node->schema));
/* get correct metadata name and value */
if (lysc_is_dup_inst_list(node->schema)) {
meta_name = "yang:position";
pos = lyd_list_pos(node);
if (pos > 1) {
if (asprintf(&dyn, "%" PRIu32, pos - 1) == -1) {
LOGMEM(LYD_CTX(node));
rc = LY_EMEM;
goto cleanup;
}
meta_val = dyn;
} else {
meta_val = "";
}
} else if (node->schema->nodetype == LYS_LIST) {
meta_name = "yang:key";
if (node->prev->next && (node->prev->schema == node->schema)) {
LY_CHECK_GOTO(rc = lyd_path_list_predicate(node->prev, &dyn, &buflen, &bufused, 0), cleanup);
meta_val = dyn;
} else {
meta_val = "";
}
} else {
meta_name = "yang:value";
if (node->prev->next && (node->prev->schema == node->schema)) {
meta_val = lyd_get_value(node->prev);
} else {
meta_val = "";
}
}
/* create the metadata */
LY_CHECK_GOTO(rc = lyd_new_meta(NULL, node, NULL, meta_name, meta_val, LYD_NEW_VAL_STORE_ONLY, NULL), cleanup);
cleanup:
free(dyn);
return rc;
}
/**
* @brief Find metadata/an attribute of a node.
*
* @param[in] node Node to search.
* @param[in] name Metadata/attribute name.
* @param[out] meta Metadata found, NULL if not found.
* @param[out] attr Attribute found, NULL if not found.
*/
static void
lyd_diff_find_meta(const struct lyd_node *node, const char *name, struct lyd_meta **meta, struct lyd_attr **attr)
{
struct lyd_meta *m;
struct lyd_attr *a;
if (meta) {
*meta = NULL;
}
if (attr) {
*attr = NULL;
}
if (node->schema) {
assert(meta);
LY_LIST_FOR(node->meta, m) {
if (!strcmp(m->name, name) && !strcmp(m->annotation->module->name, "yang")) {
*meta = m;
break;
}
}
} else {
assert(attr);
LY_LIST_FOR(((struct lyd_node_opaq *)node)->attr, a) {
/* name */
if (strcmp(a->name.name, name)) {
continue;
}
/* module */
switch (a->format) {
case LY_VALUE_JSON:
if (strcmp(a->name.module_name, "yang")) {
continue;
}
break;
case LY_VALUE_XML:
if (strcmp(a->name.module_ns, "urn:ietf:params:xml:ns:yang:1")) {
continue;
}
break;
default:
LOGINT(LYD_CTX(node));
return;
}
*attr = a;
break;
}
}
}
/**
* @brief Learn operation of a diff node.
*
* @param[in] diff_node Diff node.
* @param[out] op Operation.
* @param[out] found Whether any @p op was found. If not set, no found operation is an error.
* @return LY_ERR value.
*/
static LY_ERR
lyd_diff_get_op(const struct lyd_node *diff_node, enum lyd_diff_op *op, ly_bool *found)
{
struct lyd_meta *meta = NULL;
struct lyd_attr *attr = NULL;
const struct lyd_node *diff_parent;
const char *str;
char *path;
for (diff_parent = diff_node; diff_parent; diff_parent = diff_parent->parent) {
lyd_diff_find_meta(diff_parent, "operation", &meta, &attr);
if (!meta && !attr) {
continue;
}
str = meta ? lyd_get_meta_value(meta) : attr->value;
if ((str[0] == 'r') && (diff_parent != diff_node)) {
/* we do not care about this operation if it's in our parent */
continue;
}
*op = lyd_diff_str2op(str);
if (found) {
*found = 1;
}
return LY_SUCCESS;
}
/* operation not found */
if (found) {
*found = 0;
return LY_SUCCESS;
} else {
path = lyd_path(diff_node, LYD_PATH_STD, NULL, 0);
LOGERR(LYD_CTX(diff_node), LY_EINVAL, "Node \"%s\" without an operation.", path);
free(path);
return LY_EINT;
}
}
/**
* @brief Remove metadata/an attribute from a node.
*
* @param[in] node Node to update.
* @param[in] name Metadata/attribute name.
*/
static void
lyd_diff_del_meta(struct lyd_node *node, const char *name)
{
struct lyd_meta *meta;
struct lyd_attr *attr;
lyd_diff_find_meta(node, name, &meta, &attr);
if (meta) {
lyd_free_meta_single(meta);
} else if (attr) {
lyd_free_attr_single(LYD_CTX(node), attr);
}
}
/**
* @brief Insert a node into siblings.
*
* - if the node is part of some other tree, it is automatically unlinked.
* - difference with the lyd_insert_sibling() is that the subsequent nodes are never inserted.
* - insert ignores node ordering, which is fine since it's not desirable to sort diff nodes.
*
* @param[in] sibling Siblings to insert into, can even be NULL.
* @param[in] node Node to insert.
* @param[out] first Return the first sibling after insertion. Can be the address of @p sibling.
*/
static void
lyd_diff_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first_sibling)
{
assert(node && first_sibling);
lyd_unlink_ignore_lyds(NULL, node);
*first_sibling = lyd_first_sibling(sibling);
lyd_insert_node(NULL, first_sibling, node, LYD_INSERT_NODE_LAST_BY_SCHEMA);
}
LY_ERR
lyd_diff_add(const struct lyd_node *node, enum lyd_diff_op op, const char *orig_default, const char *orig_value,
const char *key, const char *value, const char *position, const char *orig_key, const char *orig_position,
struct lyd_node **diff, struct lyd_node **diff_node)
{
struct lyd_node *dup, *siblings, *match = NULL, *diff_parent = NULL, *elem;
const struct lyd_node *parent = NULL;
enum lyd_diff_op cur_op;
struct lyd_meta *meta;
uint32_t diff_opts;
ly_bool found;
assert(diff);
/* replace leaf always needs orig-default and orig-value */
assert((node->schema->nodetype != LYS_LEAF) || (op != LYD_DIFF_OP_REPLACE) || (orig_default && orig_value));
/* create on userord needs key/value */
assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_CREATE) ||
(lysc_is_dup_inst_list(node->schema) && position) || key);
assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
(op != LYD_DIFF_OP_CREATE) || (lysc_is_dup_inst_list(node->schema) && position) || value);
/* move on userord needs both key and orig-key/value and orig-value */
assert((node->schema->nodetype != LYS_LIST) || !(node->schema->flags & LYS_ORDBY_USER) || (op != LYD_DIFF_OP_REPLACE) ||
(lysc_is_dup_inst_list(node->schema) && position && orig_position) || (key && orig_key));
assert((node->schema->nodetype != LYS_LEAFLIST) || !(node->schema->flags & LYS_ORDBY_USER) ||
(op != LYD_DIFF_OP_REPLACE) || (lysc_is_dup_inst_list(node->schema) && position && orig_position) ||
(value && orig_value));
if (diff_node) {
*diff_node = NULL;
}
/* find the first existing parent */
siblings = *diff;
do {
/* find next node parent */
parent = node;
while (parent->parent && (!diff_parent || (parent->parent->schema != diff_parent->schema))) {
parent = parent->parent;
}
if (lysc_is_dup_inst_list(parent->schema)) {
/* assume it never exists, we are not able to distinguish whether it does or not */
match = NULL;
break;
}
/* check whether it exists in the diff */
if (lyd_find_sibling_first(siblings, parent, &match)) {
break;
}
/* another parent found */
diff_parent = match;
/* move down in the diff */
siblings = lyd_child_no_keys(match);
} while (parent != node);
if (match && (parent == node)) {
/* special case when there is already an operation on our descendant */
assert(!lyd_diff_get_op(diff_parent, &cur_op, NULL));
/* move it to the end where it is expected (matters for user-ordered lists) */
if (lysc_is_userordered(diff_parent->schema)) {
for (elem = diff_parent; elem->next && (elem->next->schema == elem->schema); elem = elem->next) {}
if (elem != diff_parent) {
LY_CHECK_RET(lyd_insert_after(elem, diff_parent));
}
}
/* will be replaced by the new operation but keep the current op for descendants */
lyd_diff_del_meta(diff_parent, "operation");
LY_LIST_FOR(lyd_child_no_keys(diff_parent), elem) {
lyd_diff_find_meta(elem, "operation", &meta, NULL);
if (meta) {
/* explicit operation, fine */
continue;
}
/* set the none operation */
LY_CHECK_RET(lyd_new_meta(NULL, elem, NULL, "yang:operation", "none", LYD_NEW_VAL_STORE_ONLY, NULL));
}
dup = diff_parent;
} else {
diff_opts = LYD_DUP_NO_META | LYD_DUP_WITH_PARENTS | LYD_DUP_WITH_FLAGS | LYD_DUP_NO_LYDS;
if ((op != LYD_DIFF_OP_REPLACE) || !lysc_is_userordered(node->schema) || (node->schema->flags & LYS_CONFIG_R)) {
/* move applies only to the user-ordered list, no descendants */
diff_opts |= LYD_DUP_RECURSIVE;
}
/* duplicate the subtree (and connect to the diff if possible) */
if (diff_parent && (LYD_CTX(diff_parent) != LYD_CTX(node))) {
LY_CHECK_RET(lyd_dup_single_to_ctx(node, LYD_CTX(diff_parent), diff_parent, diff_opts, &dup));
} else {
LY_CHECK_RET(lyd_dup_single(node, diff_parent, diff_opts, &dup));
}
/* find the first duplicated parent */
if (!diff_parent) {
diff_parent = dup->parent;
while (diff_parent && diff_parent->parent) {
diff_parent = diff_parent->parent;
}
} else {
diff_parent = dup;
while (diff_parent->parent && (diff_parent->parent->schema == parent->schema)) {
diff_parent = diff_parent->parent;
}
}
/* no parent existed, must be manually connected */
if (!diff_parent) {
/* there actually was no parent to duplicate */
lyd_diff_insert_sibling(*diff, dup, diff);
} else if (!diff_parent->parent) {
lyd_diff_insert_sibling(*diff, diff_parent, diff);
}
/* add parent operation, if any */
if (diff_parent && (diff_parent != dup)) {
LY_CHECK_RET(lyd_new_meta(NULL, diff_parent, NULL, "yang:operation", "none", LYD_NEW_VAL_STORE_ONLY, NULL));
}
}
/* add subtree operation if needed */
LY_CHECK_RET(lyd_diff_get_op(dup, &cur_op, &found));
if (!found || (cur_op != op)) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:operation", lyd_diff_op2str(op), LYD_NEW_VAL_STORE_ONLY, NULL));
}
if (op == LYD_DIFF_OP_CREATE) {
/* all nested user-ordered (leaf-)lists need special metadata for create op */
LYD_TREE_DFS_BEGIN(dup, elem) {
if ((elem != dup) && lysc_is_userordered(elem->schema)) {
LY_CHECK_RET(lyd_diff_add_create_nested_userord(elem));
}
LYD_TREE_DFS_END(dup, elem);
}
}
/* orig-default */
if (orig_default) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-default", orig_default, LYD_NEW_VAL_STORE_ONLY, NULL));
}
/* orig-value */
if (orig_value) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-value", orig_value, LYD_NEW_VAL_STORE_ONLY, NULL));
}
/* key */
if (key) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:key", key, LYD_NEW_VAL_STORE_ONLY, NULL));
}
/* value */
if (value) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:value", value, LYD_NEW_VAL_STORE_ONLY, NULL));
}
/* position */
if (position) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:position", position, LYD_NEW_VAL_STORE_ONLY, NULL));
}
/* orig-key */
if (orig_key) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-key", orig_key, LYD_NEW_VAL_STORE_ONLY, NULL));
}
/* orig-position */
if (orig_position) {
LY_CHECK_RET(lyd_new_meta(NULL, dup, NULL, "yang:orig-position", orig_position, LYD_NEW_VAL_STORE_ONLY, NULL));
}
if (diff_node) {
*diff_node = dup;
}
return LY_SUCCESS;
}
/**
* @brief Get a userord entry for a specific user-ordered list/leaf-list. Create if does not exist yet.
*
* @param[in] first Node from the first tree, can be NULL (on create).
* @param[in] schema Schema node of the list/leaf-list.
* @param[in,out] userord Sized array of userord items.
* @return Userord item for all the user-ordered list/leaf-list instances.
*/
static struct lyd_diff_userord *
lyd_diff_userord_get(const struct lyd_node *first, const struct lysc_node *schema, struct lyd_diff_userord **userord)
{
struct lyd_diff_userord *item;
struct lyd_node *iter;
const struct lyd_node **node;
LY_ARRAY_COUNT_TYPE u;
LY_ARRAY_FOR(*userord, u) {
if ((*userord)[u].schema == schema) {
return &(*userord)[u];
}
}
/* it was not added yet, add it now */
LY_ARRAY_NEW_RET(schema->module->ctx, *userord, item, NULL);
item->schema = schema;
item->pos = 0;
item->inst = NULL;
/* store all the instance pointers in the current order */
if (first) {
LYD_LIST_FOR_INST(lyd_first_sibling(first), first->schema, iter) {
LY_ARRAY_NEW_RET(schema->module->ctx, item->inst, node, NULL);
*node = iter;
}
}
return item;
}
/**
* @brief Check whether there are any metadata differences on 2 nodes.
*
* @param[in] first First node.
* @param[in] second Second node.
* @return 1 if there are some differences;
* @return 0 otherwise.
*/
static ly_bool
lyd_diff_node_metadata_check(const struct lyd_node *first, const struct lyd_node *second)
{
ly_bool rc = 0;
const struct lys_module *mod;
const struct lyd_meta *m, **meta_second = NULL;
uint32_t i, m_second_count = 0;
const struct lyd_node *first_ch, *second_ch;
assert(first && second);
mod = ly_ctx_get_module_implemented(LYD_CTX(first), "yang");
assert(mod);
/* collect second node metadata that we can delete from */
LY_LIST_FOR(second->meta, m) {
if (m->annotation->module == mod) {
continue;
}
meta_second = ly_realloc(meta_second, (m_second_count + 1) * sizeof *meta_second);
LY_CHECK_ERR_GOTO(!meta_second, LOGMEM(LYD_CTX(first)), cleanup);
meta_second[m_second_count] = m;
++m_second_count;
}
/* go through first metadata and search for a match in second */
LY_LIST_FOR(first->meta, m) {
if (m->annotation->module == mod) {
continue;
}
for (i = 0; i < m_second_count; ++i) {
if (!lyd_compare_meta(m, meta_second[i])) {
break;
}
}
if (i == m_second_count) {
/* not found */
rc = 1;
goto cleanup;
}
/* found, remove from the second metadata to consider */
--m_second_count;
if (i < m_second_count) {
memcpy(&meta_second[i], &meta_second[i + 1], (m_second_count - i) * sizeof *meta_second);
}
}
if (m_second_count) {
/* not found */
rc = 1;
goto cleanup;
}
/* for lists, we also need to check their keys */
if (first->schema->nodetype == LYS_LIST) {
first_ch = lyd_child(first);
second_ch = lyd_child(second);
while (first_ch && lysc_is_key(first_ch->schema)) {
/* check every key */
assert(second_ch && (first_ch->schema == second_ch->schema));
rc = lyd_diff_node_metadata_check(first_ch, second_ch);
LY_CHECK_GOTO(rc, cleanup);
first_ch = first_ch->next;
second_ch = second_ch->next;
}
}
cleanup:
free(meta_second);
return rc;
}
/**
* @brief Get all the metadata to be stored in a diff for the 2 nodes. Can be used only for user-ordered
* lists/leaf-lists.
*
* @param[in] first Node from the first tree, can be NULL (on create).
* @param[in] second Node from the second tree, can be NULL (on delete).
* @param[in] options Diff options.
* @param[in] userord_item Userord item of @p first and/or @p second node.
* @param[out] op Operation.
* @param[out] orig_default Original default metadata.
* @param[out] value Value metadata.
* @param[out] orig_value Original value metadata
* @param[out] key Key metadata.
* @param[out] orig_key Original key metadata.
* @param[out] position Position metadata.
* @param[out] orig_position Original position metadata.
* @return LY_SUCCESS on success,
* @return LY_ENOT if there is no change to be added into diff,
* @return LY_ERR value on other errors.
*/
static LY_ERR
lyd_diff_userord_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options,
struct lyd_diff_userord *userord_item, enum lyd_diff_op *op, const char **orig_default, char **value,
char **orig_value, char **key, char **orig_key, char **position, char **orig_position)
{
LY_ERR rc = LY_SUCCESS;
const struct lysc_node *schema;
size_t buflen, bufused;
uint32_t first_pos, second_pos, comp_opts;
assert(first || second);
*orig_default = NULL;
*value = NULL;
*orig_value = NULL;
*key = NULL;
*orig_key = NULL;
*position = NULL;
*orig_position = NULL;
schema = first ? first->schema : second->schema;
assert(lysc_is_userordered(schema));
/* find user-ordered first position */
if (first) {
for (first_pos = 0; first_pos < LY_ARRAY_COUNT(userord_item->inst); ++first_pos) {
if (userord_item->inst[first_pos] == first) {
break;
}
}
assert(first_pos < LY_ARRAY_COUNT(userord_item->inst));
} else {
first_pos = 0;
}
/* prepare position of the next instance */
second_pos = userord_item->pos++;
/* learn operation first */
if (!second) {
*op = LYD_DIFF_OP_DELETE;
} else if (!first) {
*op = LYD_DIFF_OP_CREATE;
} else {
comp_opts = lysc_is_dup_inst_list(second->schema) ? LYD_COMPARE_FULL_RECURSION : 0;
if (lyd_compare_single(second, userord_item->inst[second_pos], comp_opts)) {
/* in first, there is a different instance on the second position, we are going to move 'first' node */
*op = LYD_DIFF_OP_REPLACE;
} else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
/* default flag change */
*op = LYD_DIFF_OP_NONE;
} else if ((options & LYD_DIFF_META) && lyd_diff_node_metadata_check(first, second)) {
/* metadata changes */
*op = LYD_DIFF_OP_NONE;
} else {
/* no changes */
return LY_ENOT;
}
}
/*
* set each attribute correctly based on the operation and node type
*/
/* orig-default */
if ((schema->nodetype == LYS_LEAFLIST) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
if (first->flags & LYD_DEFAULT) {
*orig_default = "true";
} else {
*orig_default = "false";
}
}
/* value */
if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
if (second_pos) {
*value = strdup(lyd_get_value(userord_item->inst[second_pos - 1]));
LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
} else {
*value = strdup("");
LY_CHECK_ERR_GOTO(!*value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
}
}
/* orig-value */
if ((schema->nodetype == LYS_LEAFLIST) && !lysc_is_dup_inst_list(schema) &&
((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
if (first_pos) {
*orig_value = strdup(lyd_get_value(userord_item->inst[first_pos - 1]));
LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
} else {
*orig_value = strdup("");
LY_CHECK_ERR_GOTO(!*orig_value, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
}
}
/* key */
if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
if (second_pos) {
buflen = bufused = 0;
LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[second_pos - 1], key, &buflen, &bufused, 0), cleanup);
} else {
*key = strdup("");
LY_CHECK_ERR_GOTO(!*key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
}
}
/* orig-key */
if ((schema->nodetype == LYS_LIST) && !lysc_is_dup_inst_list(schema) &&
((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
if (first_pos) {
buflen = bufused = 0;
LY_CHECK_GOTO(rc = lyd_path_list_predicate(userord_item->inst[first_pos - 1], orig_key, &buflen, &bufused, 0), cleanup);
} else {
*orig_key = strdup("");
LY_CHECK_ERR_GOTO(!*orig_key, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
}
}
/* position */
if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_CREATE))) {
if (second_pos) {
if (asprintf(position, "%" PRIu32, second_pos) == -1) {
LOGMEM(schema->module->ctx);
rc = LY_EMEM;
goto cleanup;
}
} else {
*position = strdup("");
LY_CHECK_ERR_GOTO(!*position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
}
}
/* orig-position */
if (lysc_is_dup_inst_list(schema) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_DELETE))) {
if (first_pos) {
if (asprintf(orig_position, "%" PRIu32, first_pos) == -1) {
LOGMEM(schema->module->ctx);
rc = LY_EMEM;
goto cleanup;
}
} else {
*orig_position = strdup("");
LY_CHECK_ERR_GOTO(!*orig_position, LOGMEM(schema->module->ctx); rc = LY_EMEM, cleanup);
}
}
/*
* update our instances - apply the change
*/
if (*op == LYD_DIFF_OP_CREATE) {
/* insert the instance */
LY_ARRAY_CREATE_GOTO(schema->module->ctx, userord_item->inst, 1, rc, cleanup);
if (second_pos < LY_ARRAY_COUNT(userord_item->inst)) {
memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
(LY_ARRAY_COUNT(userord_item->inst) - second_pos) * sizeof *userord_item->inst);
}
LY_ARRAY_INCREMENT(userord_item->inst);
userord_item->inst[second_pos] = second;
} else if (*op == LYD_DIFF_OP_DELETE) {
/* remove the instance */
if (first_pos + 1 < LY_ARRAY_COUNT(userord_item->inst)) {
memmove(userord_item->inst + first_pos, userord_item->inst + first_pos + 1,
(LY_ARRAY_COUNT(userord_item->inst) - first_pos - 1) * sizeof *userord_item->inst);
}
LY_ARRAY_DECREMENT(userord_item->inst);
} else if (*op == LYD_DIFF_OP_REPLACE) {
/* move the instances */
memmove(userord_item->inst + second_pos + 1, userord_item->inst + second_pos,
(first_pos - second_pos) * sizeof *userord_item->inst);
userord_item->inst[second_pos] = first;
}
cleanup:
if (rc) {
free(*value);
*value = NULL;
free(*orig_value);
*orig_value = NULL;
free(*key);
*key = NULL;
free(*orig_key);
*orig_key = NULL;
free(*position);
*position = NULL;
free(*orig_position);
*orig_position = NULL;
}
return rc;
}
/**
* @brief Get all the metadata to be stored in a diff for the 2 nodes. Cannot be used for user-ordered
* lists/leaf-lists.
*
* @param[in] first Node from the first tree, can be NULL (on create).
* @param[in] second Node from the second tree, can be NULL (on delete).
* @param[in] options Diff options.
* @param[out] op Operation.
* @param[out] orig_default Original default metadata.
* @param[out] orig_value Original value metadata.
* @return LY_SUCCESS on success,
* @return LY_ENOT if there is no change to be added into diff,
* @return LY_ERR value on other errors.
*/
static LY_ERR
lyd_diff_attrs(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, enum lyd_diff_op *op,
const char **orig_default, char **orig_value)
{
const struct lysc_node *schema;
const char *str_val;
assert(first || second);
*orig_default = NULL;
*orig_value = NULL;
schema = first ? first->schema : second->schema;
assert(!lysc_is_userordered(schema));
/* learn operation first */
if (!second) {
*op = LYD_DIFF_OP_DELETE;
} else if (!first) {
*op = LYD_DIFF_OP_CREATE;
} else {
switch (schema->nodetype) {
case LYS_CONTAINER:
case LYS_RPC:
case LYS_ACTION:
case LYS_NOTIF:
if ((options & LYD_DIFF_META) && lyd_diff_node_metadata_check(first, second)) {
/* metadata changes */
*op = LYD_DIFF_OP_NONE;
} else {
/* no changes */
return LY_ENOT;
}
break;
case LYS_LIST:
case LYS_LEAFLIST:
if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
/* default flag change */
*op = LYD_DIFF_OP_NONE;
} else if ((options & LYD_DIFF_META) && lyd_diff_node_metadata_check(first, second)) {
/* metadata changes */
*op = LYD_DIFF_OP_NONE;
} else {
/* no changes */
return LY_ENOT;
}
break;
case LYS_LEAF:
case LYS_ANYXML:
case LYS_ANYDATA:
if (lyd_compare_single(first, second, 0)) {
/* different values */
*op = LYD_DIFF_OP_REPLACE;
} else if ((options & LYD_DIFF_DEFAULTS) && ((first->flags & LYD_DEFAULT) != (second->flags & LYD_DEFAULT))) {
/* default flag change */
*op = LYD_DIFF_OP_NONE;
} else if ((options & LYD_DIFF_META) && lyd_diff_node_metadata_check(first, second)) {
/* metadata changes */
*op = LYD_DIFF_OP_NONE;
} else {
/* no changes */
return LY_ENOT;
}
break;
default:
LOGINT_RET(schema->module->ctx);
}
}
/*
* set each attribute correctly based on the operation and node type
*/
/* orig-default */
if ((schema->nodetype & LYD_NODE_TERM) && ((*op == LYD_DIFF_OP_REPLACE) || (*op == LYD_DIFF_OP_NONE))) {
if (first->flags & LYD_DEFAULT) {
*orig_default = "true";
} else {
*orig_default = "false";
}
}
/* orig-value */
if ((schema->nodetype & (LYS_LEAF | LYS_ANYDATA)) && (*op == LYD_DIFF_OP_REPLACE)) {
if (schema->nodetype == LYS_LEAF) {
str_val = lyd_get_value(first);
*orig_value = strdup(str_val ? str_val : "");
LY_CHECK_ERR_RET(!*orig_value, LOGMEM(schema->module->ctx), LY_EMEM);
} else {
LY_CHECK_RET(lyd_any_value_str(first, orig_value));
}
}
return LY_SUCCESS;
}
/**
* @brief Find a matching instance of a node in a data tree.
*
* @param[in] siblings Siblings to search in.
* @param[in] target Target node to search for.
* @param[in] defaults Whether to consider (or ignore) default values.
* @param[in,out] dup_inst_ht Duplicate instance cache.
* @param[out] match Found match, NULL if no matching node found.
* @return LY_ERR value.
*/
static LY_ERR
lyd_diff_find_match(const struct lyd_node *siblings, const struct lyd_node *target, ly_bool defaults,
struct ly_ht **dup_inst_ht, struct lyd_node **match)
{
LY_ERR r;
if (!target->schema) {
/* try to find the same opaque node */
r = lyd_find_sibling_opaq_next(siblings, LYD_NAME(target), match);
} else if (target->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
/* try to find the exact instance */
r = lyd_find_sibling_first(siblings, target, match);
} else {
/* try to simply find the node, there cannot be more instances */
r = lyd_find_sibling_val(siblings, target->schema, NULL, 0, match);
}
if (r && (r != LY_ENOTFOUND)) {
return r;
}
/* update match as needed */
LY_CHECK_RET(lyd_dup_inst_next(match, dup_inst_ht));
if (*match && ((*match)->flags & LYD_DEFAULT) && !defaults) {
/* ignore default nodes */
*match = NULL;
}
return LY_SUCCESS;
}
/**
* @brief Create a diff metadata instance.
*
* @param[in,out] parent Parent node of the diff metadata.
* @param[in] diff_meta_name Diff metadata name with a mdoule name.