-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.c
735 lines (593 loc) · 14.8 KB
/
tree.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
/*
* tree.c - DT methods
*
* (C) Copyright Pantelis Antoniou <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* (3)The name of the author may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <stdbool.h>
#include <assert.h>
#include "utils.h"
#include "dt.h"
struct ref *ref_alloc(struct tree *t, enum ref_type type,
const void *data, int len,
const char *xtag)
{
struct ref *ref;
assert(t && data);
ref = t->ops->ref_alloc(t, type, data, len, xtag);
if (!ref)
return NULL;
return ref;
}
void ref_free(struct tree *t, struct ref *ref)
{
assert(t && ref);
/* either from the unresolved or node ref list */
list_del(&ref->node);
t->ops->ref_free(t, ref);
}
struct property *prop_alloc(struct tree *t, const char *name)
{
struct property *prop;
assert(t && name);
prop = t->ops->prop_alloc(t, name);
if (!prop)
return NULL;
INIT_LIST_HEAD(&prop->refs);
return prop;
}
void prop_free(struct tree *t, struct property *prop)
{
struct ref *ref, *refn;
assert(t && prop);
if (prop->np)
list_del(&prop->node);
for_each_ref_of_property_safe(prop, ref, refn)
ref_free(t, ref);
t->ops->prop_free(t, prop);
}
void prop_del(struct tree *t, struct property *prop)
{
prop->deleted = true;
}
void prop_ref_clear(struct tree *t, struct property *prop)
{
struct ref *ref, *refn;
for_each_ref_of_property_safe(prop, ref, refn)
ref_free(t, ref);
}
struct label *label_alloc(struct tree *t, const char *label)
{
assert(t && label);
return t->ops->label_alloc(t, label);
}
struct label *label_add_nolink(struct tree *t, struct node *np,
const char *label)
{
struct label *l;
struct list_head *lh;
assert(t && np && label);
/* do not add duplicate label in same node */
for_each_label_of_node(np, l) {
if (!strcmp(l->label, label))
return NULL;
}
l = label_alloc(t, label);
l->np = np;
l->len = strlen(label);
l->hash = label_hash(label, strlen(label));
l->duplicate = false;
lh = &t->lhash[l->hash % ARRAY_SIZE(t->lhash)];
list_add_tail(&l->hnode, lh);
return l;
}
void label_add(struct tree *t, struct node *np,
const char *label)
{
struct label *l;
l = label_add_nolink(t, np, label);
if (l)
list_add_tail(&l->node, &np->labels);
}
void label_free(struct tree *t, struct label *l)
{
assert(t && l);
if (l->np)
list_del(&l->node);
list_del(&l->hnode);
t->ops->label_free(t, l);
}
struct node *node_alloc(struct tree *t, const char *name,
const char *label)
{
struct node *np;
assert(t && name);
np = t->ops->node_alloc(t, name, label);
if (!np)
return NULL;
INIT_LIST_HEAD(&np->children);
INIT_LIST_HEAD(&np->properties);
INIT_LIST_HEAD(&np->labels);
if (label)
label_add(t, np, label);
return np;
}
void node_clear(struct tree *t, struct node *np)
{
struct node *child, *childn;
struct property *prop, *propn;
struct label *l, *ln;
assert(t && np);
for_each_child_of_node_safe_withdel(np, child, childn)
node_free(t, child);
for_each_property_of_node_safe_withdel(np, prop, propn)
prop_free(t, prop);
for_each_label_of_node_safe(np, l, ln)
label_free(t, l);
}
void node_free(struct tree *t, struct node *np)
{
assert(t && np);
node_clear(t, np);
if (np->parent)
list_del(&np->node);
t->ops->node_free(t, np);
}
void node_del(struct tree *t, struct node *np)
{
struct node *child;
struct property *prop;
struct label *l, *ln;
assert(t && np);
for_each_child_of_node(np, child)
node_del(t, child);
for_each_property_of_node(np, prop)
prop_del(t, prop);
/* labels get deleted */
for_each_label_of_node_safe(np, l, ln)
label_free(t, l);
np->deleted = true;
}
struct node *node_lookup_by_label(struct tree *t,
const char *label, int len)
{
uint32_t hval;
struct list_head *lh;
struct label *l;
if (!t || !label)
return NULL;
if (len < 0)
len = strlen(label);
hval = label_hash(label, len);
lh = &t->lhash[hval % ARRAY_SIZE(t->lhash)];
list_for_each_entry(l, lh, hnode) {
if (l->hash == hval && l->len == len &&
!memcmp(l->label, label, len))
return l->np;
}
return NULL;
}
struct node *node_get_child_by_name(struct tree *t,
struct node *np, const char *name,
int index)
{
struct node *child;
if (!t || !np || !name)
return NULL;
for_each_child_of_node(np, child) {
if (strcmp(child->name, name))
continue;
if (index == 0)
return child;
index--;
}
return NULL;
}
struct node *node_lookup_by_path(struct tree *t,
const char *path, int len)
{
const char *s, *name;
struct node *np, *child;
int namelen;
bool found;
if (!t || !path)
return NULL;
if (len < 0)
len = strlen(path);
if (!len || *path != '/')
return NULL;
/* skip over '/' */
path++;
len--;
np = t->root;
while (len > 0) {
name = path;
s = memchr(path, '/', len);
namelen = !s ? len : (s - path);
found = false;
for_each_child_of_node(np, child) {
if (strlen(child->name) == namelen &&
!memcmp(child->name, name, namelen)) {
found = true;
break;
}
}
if (!found)
return false;
np = child;
len -= namelen;
path += namelen;
while (len > 0 && *path == '/') {
len--;
path++;
}
}
return np;
}
struct node *node_lookup(struct tree *t, const char *key, int len)
{
if (!t || !key)
return NULL;
if (len < 0)
len = strlen(key);
if (!len)
return NULL;
if (is_node_ref_char(*key))
return node_lookup_by_label(t, key + 1, len - 1);
if (*key == '/')
return node_lookup_by_path(t, key, len);
return NULL;
}
struct property *prop_get_by_name(struct tree *t,
struct node *np, const char *name,
int index)
{
struct property *prop;
if (!t || !np || !name)
return NULL;
for_each_property_of_node(np, prop) {
if (strcmp(prop->name, name))
continue;
if (index == 0)
return prop;
index--;
}
return NULL;
}
struct ref *ref_get_by_index(struct tree *t,
struct property *prop, int index)
{
struct ref *ref;
if (!t || !prop)
return NULL;
for_each_ref_of_property(prop, ref) {
if (ref->type == r_seq_start || ref->type == r_seq_end)
continue;
if (index == 0)
return ref;
index--;
}
return NULL;
}
const char *dn_fullname_multi(struct node *np, char **buf, int *size)
{
struct node *npt;
char *p;
int len, tlen;
const char *ret;
const char *name;
/* special case for root */
if (np->parent == NULL) {
/* either / or ref node (which shouldn't have / at start) */
if (!np->name || !strcmp(np->name, "/") || strlen(np->name) == 0)
name = "/";
else
name = np->name;
ret = *buf;
len = strlen(name) + 1;
/* truncated */
if (len > *size)
return "<>";
strcpy(*buf, name);
*size -= len;
*buf += len;
return ret;
}
npt = np;
tlen = 0;
while (npt != NULL) {
if (npt != np)
tlen++;
len = strlen(npt->name);
tlen += len;
npt = npt->parent;
}
tlen++;
/* truncated */
if (tlen > *size)
return "<>";
ret = *buf;
p = *buf + tlen;
*--p = '\0';
npt = np;
while (npt != NULL) {
if (npt != np)
*--p = '/';
len = strlen(npt->name);
memcpy(p - len, npt->name, len);
p -= len;
npt = npt->parent;
}
/* verify that we are correct */
assert(ret == *buf);
*size -= tlen;
*buf += tlen;
return ret;
}
const char *dn_fullname(struct node *np, char *buf, int size)
{
return dn_fullname_multi(np, &buf, &size);
}
void tree_init(struct tree *t, const struct tree_ops *ops)
{
int i;
assert(t && ops &&
ops->ref_alloc &&
ops->ref_free &&
ops->prop_alloc &&
ops->prop_free &&
ops->label_alloc &&
ops->label_free &&
ops->node_alloc &&
ops->node_free);
t->root = NULL;
INIT_LIST_HEAD(&t->ref_nodes);
t->ops = ops;
for (i = 0; i < ARRAY_SIZE(t->lhash); i++)
INIT_LIST_HEAD(&t->lhash[i]);
}
void tree_cleanup(struct tree *t)
{
struct node *np, *npn;
/* free the root */
if (t->root)
node_free(t, t->root);
t->root = NULL;
/* free the ref nodes */
list_for_each_entry_safe(np, npn, &t->ref_nodes, node) {
list_del(&np->node);
assert(!np->parent);
node_free(t, np);
}
}
/* clear any crud that shouldn't be part of the base tree */
static void sanitize_base(struct tree *t, struct node *np)
{
struct node *child;
struct property *prop, *propn;
char namebuf[NODE_FULLNAME_MAX];
for_each_property_of_node_safe_withdel(np, prop, propn) {
if (prop->is_delete || !strcmp(prop->name, "~")) {
tree_debug(t, "removing property %s @%s\n",
prop->name, dn_fullname(np, namebuf, sizeof(namebuf)));
prop_free(t, prop);
}
}
for_each_child_of_node(np, child)
sanitize_base(t, child);
}
void tree_apply_ref_node(struct tree *t, struct node *npref,
struct node *np, bool compatible)
{
struct property *prop, *propn;
struct property *propref, *proprefn;
struct label *l, *ln, *lt;
struct node *child, *childn, *childref, *childrefn;
bool found;
char namebuf[2][NODE_FULLNAME_MAX];
if (np->is_delete) {
tree_debug(t, "deleting node @%s\n",
dn_fullname(npref, &namebuf[0][0], sizeof(namebuf[0])));
node_del(t, npref);
return;
}
/* add label to noderef */
for_each_label_of_node_safe(np, l, ln) {
lt = label_add_nolink(t, npref, l->label);
if (lt) {
if (!compatible)
list_add_tail(<->node, &npref->labels);
else
list_add(<->node, &npref->labels);
}
}
for_each_property_of_node_safe_withdel(np, prop, propn) {
tree_debug(t, "using property %s @%s\n",
prop->name,
dn_fullname(np, &namebuf[0][0], sizeof(namebuf[0])));
if (prop->is_delete) {
for_each_property_of_node_safe(npref, propref, proprefn) {
if (strcmp(propref->name, prop->name))
continue;
tree_debug(t, "deleting property %s at %s\n",
propref->name,
dn_fullname(npref, &namebuf[0][0], sizeof(namebuf[0])));
prop_del(t, propref);
}
for_each_child_of_node_safe(npref, childref, childrefn) {
if (strcmp(childref->name, prop->name))
continue;
tree_debug(t, "deleting child %s at %s\n",
dn_fullname(childref, &namebuf[0][0], sizeof(namebuf[0])),
dn_fullname(npref, &namebuf[1][0], sizeof(namebuf[1])));
node_del(t, childref);
}
prop_free(t, prop);
continue;
}
found = false;
for_each_property_of_node_safe_withdel(npref, propref, proprefn) {
/* note this takes into account deleted properties */
if (!strcmp(propref->name, prop->name)) {
found = true;
break;
}
}
list_del(&prop->node);
prop->np = npref;
/* if found, free old copy */
if (found) {
if (propref->deleted)
propref->deleted = false;
/* carefully put it at the same point in the list */
list_add(&prop->node, &propref->node);
list_del(&propref->node);
propref->np = NULL;
prop_free(t, propref);
} else /* move property over to new parent */
list_add_tail(&prop->node, &npref->properties);
}
for_each_child_of_node_safe(np, child, childn) {
/* find matching child */
found = false;
/* note, we match on deleted nodes as well */
for_each_child_of_node_withdel(npref, childref) {
if (!strcmp(childref->name, child->name)) {
/* ressurect? */
if (childref->deleted)
childref->deleted = false;
found = true;
break;
}
}
if (!found) {
/* child at ref does not exist, just move self over */
list_del(&child->node);
child->parent = npref;
list_add_tail(&child->node, &npref->children);
sanitize_base(t, child);
} else
tree_apply_ref_node(t, childref, child, compatible);
}
}
bool tree_apply_single_ref_node(struct tree *t, struct node *np,
bool object, bool compatible)
{
struct node *npref;
npref = node_lookup(t, np->name, -1);
if (!npref)
return false;
tree_apply_ref_node(t, npref, np, compatible);
return true;
}
void tree_apply_ref_nodes(struct tree *t, bool object, bool compatible)
{
struct node *np, *npn, *npref;
struct list_head *ref_nodes = tree_ref_nodes(t);
list_for_each_entry_safe(np, npn, ref_nodes, node) {
npref = node_lookup(t, np->name, -1);
if (!npref && !object) {
if (is_node_ref_char(np->name[0]))
tree_error_at_node(t, np,
"reference to unknown label %s\n",
np->name + 1);
else
tree_error_at_node(t, np,
"reference to unknown path %s\n",
np->name);
}
if (npref)
tree_apply_ref_node(t, npref, np, compatible);
/* free everything now */
if (npref || !object) {
list_del(&np->node);
node_free(t, np);
}
}
if (!object)
return;
/* move all remaining unref nodes to root */
list_for_each_entry_safe(np, npn, ref_nodes, node) {
if (tree_root(t)) {
list_del(&np->node);
np->parent = tree_root(t);
list_add_tail(&np->node, &np->parent->children);
} else
node_free(t, np);
}
}
int tree_detect_duplicate_labels(struct tree *t, struct node *np)
{
struct label *l, *ln;
struct list_head *lh;
char namebuf[NODE_FULLNAME_MAX];
char namebufn[NODE_FULLNAME_MAX];
int i, err = 0;
if (!np)
return 0;
/* duplicate labels hash to the same chain */
for (i = 0; i < ARRAY_SIZE(t->lhash); i++) {
lh = &t->lhash[i];
list_for_each_entry(l, lh, hnode) {
if (l->duplicate)
continue;
list_for_each_entry(ln, lh, hnode) {
if (ln->duplicate)
continue;
if (ln == l ||
l->hash != ln->hash ||
l->len != ln->len ||
memcmp(l->label, ln->label, l->len))
continue;
dn_fullname(l->np, namebuf, sizeof(namebuf));
dn_fullname(ln->np, namebufn, sizeof(namebufn));
tree_error_at_label(t, ln,
"duplicate label %s at \"%s\"\n",
l->label, namebufn);
tree_error_at_label(t, l,
"duplicate label %s is defined also at \"%s\"\n",
l->label, namebuf);
l->duplicate = true;
ln->duplicate = true;
if (!err)
err = -1;
}
}
}
return err;
}