-
Notifications
You must be signed in to change notification settings - Fork 233
/
collection.c
3054 lines (2548 loc) · 110 KB
/
collection.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
/*
COLLECTION LIBRARY
Implementation of the collection library interface.
Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
Collection Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Collection Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Collection Library. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include "config.h"
#include "trace.h"
/* The collection should use the real structures */
#include "collection_priv.h"
#include "collection.h"
/* Internal constants defined to denote actions that can be performed by find handler */
#define COLLECTION_ACTION_FIND 1
#define COLLECTION_ACTION_DEL 2
#define COLLECTION_ACTION_UPDATE 3
#define COLLECTION_ACTION_GET 4
/* Special internal error code to indicate that collection search was interrupted */
#define EINTR_INTERNAL 10000
/* Potential subject for management with libtools */
#define DATE_FORMAT "%c"
#define TIME_ARRAY_SIZE 100
/* Magic numbers for hashing */
#if SIZEOF_LONG == 8
#define FNV1a_prime 1099511628211ul
#define FNV1a_base 14695981039346656037ul
#elif SIZEOF_LONG_LONG == 8
#define FNV1a_prime 1099511628211ull
#define FNV1a_base 14695981039346656037ull
#else
#error "Platform cannot support 64-bit constant integers"
#endif
/* Struct used for passing parameter for update operation */
struct update_property {
int type;
void *data;
int length;
int found;
};
/* This struct is used to construct path
* to an item in the collection (tree)
*/
struct path_data {
char *name;
int length;
struct path_data *previous_path;
};
/* Structure to keep data needed to
* copy collection
* while traversing it
*/
struct col_copy {
int mode;
struct path_data *current_path;
char *given_name;
int given_len;
col_copy_cb copy_cb;
void *ext_data;
};
/******************** FUNCTION DECLARATIONS ****************************/
/* Have to declare those due to function cross referencing */
static int col_find_item_and_do(struct collection_item *ci,
const char *property_to_find,
int type,
int mode_flags,
col_item_fn item_handler,
void *custom_data,
int action);
/* Traverse callback for find & delete function */
static int col_act_traverse_handler(struct collection_item *head,
struct collection_item *previous,
struct collection_item *current,
void *passed_traverse_data,
col_item_fn user_item_handler,
void *custom_data,
int *stop);
/* Traverse handler to find parent of the item */
static int col_parent_traverse_handler(struct collection_item *head,
struct collection_item *previous,
struct collection_item *current,
void *traverse_data,
col_item_fn user_item_handler,
void *custom_data,
int *stop);
/* Traverse callback signature */
typedef int (*internal_item_fn)(struct collection_item *head,
struct collection_item *previous,
struct collection_item *current,
void *traverse_data,
col_item_fn user_item_handler,
void *custom_data,
int *stop);
/* Function to walk_items */
static int col_walk_items(struct collection_item *ci,
int mode_flags,
internal_item_fn traverse_handler,
void *traverse_data,
col_item_fn user_item_handler,
void *custom_data,
unsigned *depth);
/* Function to get sub collection */
static int col_get_subcollection(const char *property,
int property_len,
int type,
void *data,
int length,
void *found,
int *dummy);
/* Function to destroy collection */
void col_destroy_collection(struct collection_item *ci);
/******************** SUPPLEMENTARY FUNCTIONS ****************************/
/* BASIC OPERATIONS */
/* Function that checks if property can be added */
static int col_validate_property(const char *property)
{
TRACE_FLOW_STRING("col_validate_property", "Entry point.");
/* Only alpha numeric characters are allowed in names of the properties */
int invalid = 0;
const char *check;
check = property;
while (*check != '\0') {
/* It turned out that limiting collection charcters is bad */
if ((*check < ' ') || (*check == '!')) {
invalid = 1;
break;
}
check++;
}
TRACE_FLOW_NUMBER("col_validate_property. Returning ", invalid);
return invalid;
}
/* Function that cleans the item */
void col_delete_item(struct collection_item *item)
{
struct collection_item *other_collection;
TRACE_FLOW_STRING("col_delete_item","Entry point.");
if (item == NULL) {
TRACE_FLOW_STRING("col_delete_item","Nothing to delete!");
return;
}
/* Handle external or embedded collection */
if(item->type == COL_TYPE_COLLECTIONREF) {
/* Our data is a pointer to a whole external collection so dereference
* it or delete */
other_collection = *((struct collection_item **)(item->data));
col_destroy_collection(other_collection);
}
TRACE_INFO_STRING("Deleting property:", item->property);
TRACE_INFO_NUMBER("Type:", item->type);
if (item->property != NULL) free(item->property);
if (item->data != NULL) free(item->data);
free(item);
TRACE_FLOW_STRING("col_delete_item","Exit.");
}
/* A generic function to allocate a property item */
int col_allocate_item(struct collection_item **ci, const char *property,
const void *item_data, int length, int type)
{
struct collection_item *item = NULL;
TRACE_FLOW_STRING("col_allocate_item", "Entry point.");
TRACE_INFO_NUMBER("Will be using type:", type);
/* Check the length */
if (length >= COL_MAX_DATA) {
TRACE_ERROR_STRING("col_allocate_item", "Data to long.");
return EMSGSIZE;
}
if (col_validate_property(property)) {
TRACE_ERROR_STRING("Invalid chracters in the property name", property);
return EINVAL;
}
/* Allocate memory for the structure */
item = (struct collection_item *)malloc(sizeof(struct collection_item));
if (item == NULL) {
TRACE_ERROR_STRING("col_allocate_item", "Malloc failed.");
return ENOMEM;
}
/* After we initialize "next" we can use delete_item() in case of error */
item->next = NULL;
/* Copy property */
item->property = strdup(property);
if (item->property == NULL) {
TRACE_ERROR_STRING("col_allocate_item", "Failed to dup property.");
col_delete_item(item);
return ENOMEM;
}
item->phash = col_make_hash(property, 0, &(item->property_len));
TRACE_INFO_NUMBER("Item hash", item->phash);
TRACE_INFO_NUMBER("Item property length", item->property_len);
TRACE_INFO_NUMBER("Item property strlen", strlen(item->property));
/* Deal with data */
item->data = malloc(length);
if (item->data == NULL) {
TRACE_ERROR_STRING("col_allocate_item", "Failed to dup data.");
col_delete_item(item);
return ENOMEM;
}
memcpy(item->data, item_data, length);
/* Deal with other properties of the item */
TRACE_INFO_NUMBER("About to set type to:", type);
item->type = type;
item->length = length;
/* Make sure that data is NULL terminated in case of string */
if (type == COL_TYPE_STRING) ((char *)(item->data))[length-1] = '\0';
*ci = item;
TRACE_INFO_STRING("Item property", item->property);
TRACE_INFO_NUMBER("Item property type", item->type);
TRACE_INFO_NUMBER("Item data length", item->length);
TRACE_FLOW_STRING("col_allocate_item", "Success exit.");
return EOK;
}
/* Structure used to find things in collection */
struct property_search {
const char *property;
uint64_t hash;
struct collection_item *parent;
int index;
int count;
int found;
int use_type;
int type;
};
/* Find the parent of the item with given name */
static int col_find_property(struct collection_item *collection,
const char *refprop,
int idx,
int use_type,
int type,
struct collection_item **parent)
{
struct property_search ps;
int i = 0;
unsigned depth = 0;
TRACE_FLOW_STRING("col_find_property", "Entry.");
*parent = NULL;
ps.property = refprop;
ps.hash = FNV1a_base;
ps.parent = NULL;
ps.index = idx;
ps.count = 0;
ps.found = 0;
ps.use_type = use_type;
ps.type = type;
/* Create hash of the string to search */
while(refprop[i] != 0) {
ps.hash = ps.hash ^ toupper(refprop[i]);
ps.hash *= FNV1a_prime;
i++;
}
/* We do not care about error here */
(void)col_walk_items(collection, COL_TRAVERSE_ONELEVEL,
col_parent_traverse_handler,
(void *)parent, NULL, (void *)&ps,
&depth);
if (*parent) {
/* Item is found in the collection */
TRACE_FLOW_STRING("col_find_property", "Exit - item found");
return 1;
}
/* Item is not found */
TRACE_FLOW_STRING("col_find_property", "Exit - item NOT found");
return EOK;
}
/* Insert item into the current collection */
int col_insert_item_into_current(struct collection_item *collection,
struct collection_item *item,
int disposition,
const char *refprop,
int idx,
unsigned flags)
{
struct collection_header *header = NULL;
struct collection_item *parent = NULL;
struct collection_item *current = NULL;
int refindex = 0;
TRACE_FLOW_STRING("col_insert_item_into_current", "Entry point");
/* Do best effort on the item */
if ((!item) || (item->next)) {
TRACE_ERROR_STRING("Passed in item is invalid", "");
return EINVAL;
}
if (collection == NULL) {
TRACE_INFO_STRING("col_insert_item_into_current",
"Collection accepting is NULL");
if (item->type == COL_TYPE_COLLECTION) {
/* This is a special case of self creation */
TRACE_INFO_STRING("col_insert_item_into_current",
"Adding header item to new collection.");
collection = item;
}
else {
TRACE_ERROR_STRING("Passed in item is invalid", "");
return EINVAL;
}
}
else {
/* We can add items only to collections */
if (collection->type != COL_TYPE_COLLECTION) {
TRACE_ERROR_STRING("Attempt to add item to non collection.","");
TRACE_ERROR_STRING("Collection name:", collection->property);
TRACE_ERROR_NUMBER("Collection type:", collection->type);
return EINVAL;
}
}
/* After processing flags we can process disposition */
header = (struct collection_header *)collection->data;
/* Check flags first */
switch(flags) {
case COL_INSERT_NOCHECK: /* No check - good just fall through */
TRACE_INFO_STRING("Insert without check", "");
break;
case COL_INSERT_DUPOVER: /* Find item and overwrite - ignore disposition */
if (col_find_property(collection, item->property, 0, 0, 0, &parent)) {
current = parent->next;
item->next = current->next;
parent->next = item;
if (header->last == current) header->last = item;
col_delete_item(current);
/* Deleted one added another - count stays the same! */
TRACE_FLOW_STRING("col_insert_item_into_current", "Dup overwrite exit");
return EOK;
}
/* Not found so we fall thorough and add as requested */
break;
case COL_INSERT_DUPOVERT: /* Find item by name and type and overwrite - ignore disposition */
if (col_find_property(collection, item->property, 0, 1, item->type, &parent)) {
current = parent->next;
item->next = current->next;
parent->next = item;
if (header->last == current) header->last = item;
col_delete_item(current);
/* Deleted one added another - count stays the same! */
TRACE_FLOW_STRING("col_insert_item_into_current", "Dup overwrite exit");
return EOK;
}
/* Not found so we fall thorough and add as requested */
break;
case COL_INSERT_DUPERROR: if (col_find_property(collection, item->property, 0, 0, 0, &parent)) {
/* Return error */
TRACE_ERROR_NUMBER("Duplicate property", EEXIST);
return EEXIST;
}
break;
case COL_INSERT_DUPERRORT: if (col_find_property(collection, item->property, 0, 1, item->type, &parent)) {
/* Return error */
TRACE_ERROR_NUMBER("Duplicate property of the same type", EEXIST);
return EEXIST;
}
break;
case COL_INSERT_DUPMOVE: /* Find item and delete */
if (col_find_property(collection, item->property, 0, 0, 0, &parent)) {
current = parent->next;
parent->next = current->next;
col_delete_item(current);
header->count--;
}
/* Now add item according to the disposition */
break;
case COL_INSERT_DUPMOVET: /* Find item and delete */
TRACE_INFO_STRING("Property:", item->property);
TRACE_INFO_NUMBER("Type:", item->type);
if (col_find_property(collection, item->property, 0, 1, item->type, &parent)) {
TRACE_INFO_NUMBER("Current:", (unsigned)(parent->next));
current = parent->next;
parent->next = current->next;
col_delete_item(current);
header->count--;
}
/* Now add item according to the disposition */
break;
default: /* The new ones should be added here */
TRACE_ERROR_NUMBER("Flag is not implemented", ENOSYS);
return ENOSYS;
}
switch (disposition) {
case COL_DSP_END: /* Link new item to the last item in the list if there any */
if (header->last != NULL) header->last->next = item;
/* Make sure we save a new last element */
header->last = item;
header->count++;
break;
case COL_DSP_FRONT: /* Same as above if there is header only */
if (header->count == 1) {
header->last->next = item;
header->last = item;
}
else {
item->next = collection->next;
collection->next = item;
}
header->count++;
break;
case COL_DSP_BEFORE: /* Check argument */
if (!refprop) {
TRACE_ERROR_STRING("In this case property is required", "");
return EINVAL;
}
/* We need to find property */
if (col_find_property(collection, refprop, 0, 0, 0, &parent)) {
item->next = parent->next;
parent->next = item;
header->count++;
}
else {
TRACE_ERROR_STRING("Property not found", refprop);
return ENOENT;
}
break;
case COL_DSP_AFTER: /* Check argument */
if (!refprop) {
TRACE_ERROR_STRING("In this case property is required", "");
return EINVAL;
}
/* We need to find property */
if (col_find_property(collection, refprop, 0, 0, 0, &parent)) {
parent = parent->next;
if (parent->next) {
/* It is not the last item */
item->next = parent->next;
parent->next = item;
}
else {
/* It is the last item */
header->last->next = item;
header->last = item;
}
header->count++;
}
else {
TRACE_ERROR_STRING("Property not found", refprop);
return ENOENT;
}
break;
case COL_DSP_INDEX: if(idx == 0) {
/* Same is first */
if (header->count == 1) {
header->last->next = item;
header->last = item;
}
else {
item->next = collection->next;
collection->next = item;
}
}
else if(idx >= header->count - 1) {
/* In this case add to the end */
if (header->last != NULL) header->last->next = item;
/* Make sure we save a new last element */
header->last = item;
}
else {
/* In the middle */
parent = collection;
/* Move to the right position counting */
while (idx > 0) {
idx--;
parent = parent->next;
}
item->next = parent->next;
parent->next = item;
}
header->count++;
break;
case COL_DSP_FIRSTDUP:
case COL_DSP_LASTDUP:
case COL_DSP_NDUP:
if (disposition == COL_DSP_FIRSTDUP) refindex = 0;
else if (disposition == COL_DSP_LASTDUP) refindex = -1;
else refindex = idx;
/* We need to find property based on index */
if (col_find_property(collection, item->property, refindex, 0, 0, &parent)) {
item->next = parent->next;
parent->next = item;
header->count++;
if(header->last == parent) header->last = item;
}
else {
TRACE_ERROR_STRING("Property not found", refprop);
return ENOENT;
}
break;
default:
TRACE_ERROR_STRING("Disposition is not implemented", "");
return ENOSYS;
}
TRACE_INFO_STRING("Collection:", collection->property);
TRACE_INFO_STRING("Just added item is:", item->property);
TRACE_INFO_NUMBER("Item type.", item->type);
TRACE_INFO_NUMBER("Number of items in collection now is.", header->count);
TRACE_FLOW_STRING("col_insert_item_into_current", "Exit");
return EOK;
}
/* Extract item from the current collection */
int col_extract_item_from_current(struct collection_item *collection,
int disposition,
const char *refprop,
int idx,
int type,
struct collection_item **ret_ref)
{
struct collection_header *header = NULL;
struct collection_item *parent = NULL;
struct collection_item *current = NULL;
struct collection_item *found = NULL;
int refindex = 0;
int use_type = 0;
TRACE_FLOW_STRING("col_extract_item_from_current", "Entry point");
/* Check that collection is not empty */
if ((collection == NULL) || (collection->type != COL_TYPE_COLLECTION)) {
TRACE_ERROR_STRING("Collection can't be NULL", "");
return EINVAL;
}
header = (struct collection_header *)collection->data;
/* Before moving forward we need to check if there is anything to extract */
if (header->count <= 1) {
TRACE_ERROR_STRING("Collection is empty.", "Nothing to extract.");
return ENOENT;
}
if (type != 0) use_type = 1;
switch (disposition) {
case COL_DSP_END: /* Extract last item in the list. */
parent = collection;
current = collection->next;
while (current->next != NULL) {
parent = current;
current = current->next;
}
*ret_ref = parent->next;
parent->next = NULL;
/* Special case - one data element */
if (header->count == 2) header->last = NULL;
else header->last = parent;
break;
case COL_DSP_FRONT: /* Extract first item in the list */
*ret_ref = collection->next;
collection->next = (*ret_ref)->next;
/* Special case - one data element */
if (header->count == 2) header->last = NULL;
break;
case COL_DSP_BEFORE: /* Check argument */
if (!refprop) {
TRACE_ERROR_STRING("In this case property is required", "");
return EINVAL;
}
/* We have to do it in two steps */
/* First find the property that is mentioned */
if (col_find_property(collection, refprop, 0, use_type, type, &found)) {
/* We found the requested property */
if (found->next == collection->next) {
/* The referenced property is the first in the list */
TRACE_ERROR_STRING("Nothing to extract. Lists starts with property", refprop);
return ENOENT;
}
/* Get to the parent of the item that is before the one that is found */
parent = collection;
current = collection->next;
while (current != found) {
parent = current;
current = current->next;
}
*ret_ref = current;
parent->next = current->next;
}
else {
TRACE_ERROR_STRING("Property not found", refprop);
return ENOENT;
}
break;
case COL_DSP_AFTER: /* Check argument */
if (!refprop) {
TRACE_ERROR_STRING("In this case property is required", "");
return EINVAL;
}
/* We need to find property */
if (col_find_property(collection, refprop, 0, use_type, type, &parent)) {
current = parent->next;
if (current->next) {
*ret_ref = current->next;
current->next = (*ret_ref)->next;
/* If we removed the last element adjust header */
if(current->next == NULL) header->last = current;
}
else {
TRACE_ERROR_STRING("Property is last in the list", refprop);
return ENOENT;
}
}
else {
TRACE_ERROR_STRING("Property not found", refprop);
return ENOENT;
}
break;
case COL_DSP_INDEX: if (idx == 0) {
*ret_ref = collection->next;
collection->next = (*ret_ref)->next;
/* Special case - one data element */
if (header->count == 2) header->last = NULL;
}
/* Index 0 stands for the first data element.
* Count includes header element.
*/
else if (idx >= (header->count - 1)) {
TRACE_ERROR_STRING("Index is out of boundaries", refprop);
return ENOENT;
}
else {
/* Loop till the element with right index */
refindex = 0;
parent = collection;
current = collection->next;
while (refindex < idx) {
parent = current;
current = current->next;
refindex++;
}
*ret_ref = parent->next;
parent->next = (*ret_ref)->next;
/* If we removed the last element adjust header */
if (parent->next == NULL) header->last = parent;
}
break;
case COL_DSP_FIRSTDUP:
case COL_DSP_LASTDUP:
case COL_DSP_NDUP:
if (disposition == COL_DSP_FIRSTDUP) refindex = 0;
else if (disposition == COL_DSP_LASTDUP) refindex = -2;
else refindex = idx;
/* We need to find property based on index */
if (col_find_property(collection, refprop, refindex, use_type, type, &parent)) {
*ret_ref = parent->next;
parent->next = (*ret_ref)->next;
/* If we removed the last element adjust header */
if(parent->next == NULL) header->last = parent;
}
else {
TRACE_ERROR_STRING("Property not found", refprop);
return ENOENT;
}
break;
default:
TRACE_ERROR_STRING("Disposition is not implemented", "");
return ENOSYS;
}
/* Clear item and reduce count */
(*ret_ref)->next = NULL;
header->count--;
TRACE_INFO_STRING("Collection:", (*ret_ref)->property);
TRACE_INFO_NUMBER("Item type.", (*ret_ref)->type);
TRACE_INFO_NUMBER("Number of items in collection now is.", header->count);
TRACE_FLOW_STRING("col_extract_item_from_current", "Exit");
return EOK;
}
/* Extract item from the collection */
int col_extract_item(struct collection_item *collection,
const char *subcollection,
int disposition,
const char *refprop,
int idx,
int type,
struct collection_item **ret_ref)
{
struct collection_item *col = NULL;
int error = EOK;
TRACE_FLOW_STRING("col_extract_item", "Entry point");
/* Check that collection is not empty */
if ((collection == NULL) || (collection->type != COL_TYPE_COLLECTION)) {
TRACE_ERROR_STRING("Collection can't be NULL", "");
return EINVAL;
}
/* Get subcollection if needed */
if (subcollection == NULL) {
col = collection;
}
else {
TRACE_INFO_STRING("Subcollection id not null, searching", subcollection);
error = col_find_item_and_do(collection, subcollection,
COL_TYPE_COLLECTIONREF,
COL_TRAVERSE_DEFAULT,
col_get_subcollection, (void *)(&col),
COLLECTION_ACTION_FIND);
if (error) {
TRACE_ERROR_NUMBER("Search for subcollection returned error:", error);
return error;
}
if (col == NULL) {
TRACE_ERROR_STRING("Search for subcollection returned NULL pointer", "");
return ENOENT;
}
}
/* Extract from the current collection */
error = col_extract_item_from_current(col,
disposition,
refprop,
idx,
type,
ret_ref);
if (error) {
TRACE_ERROR_NUMBER("Failed to extract item from the current collection", error);
return error;
}
TRACE_FLOW_STRING("col_extract_item", "Exit");
return EOK;
}
/* Remove item (property) from collection.*/
int col_remove_item(struct collection_item *ci,
const char *subcollection,
int disposition,
const char *refprop,
int idx,
int type)
{
int error = EOK;
struct collection_item *ret_ref = NULL;
TRACE_FLOW_STRING("col_remove_item", "Exit");
/* Extract from the current collection */
error = col_extract_item(ci,
subcollection,
disposition,
refprop,
idx,
type,
&ret_ref);
if (error) {
TRACE_ERROR_NUMBER("Failed to extract item from the collection", error);
return error;
}
col_delete_item(ret_ref);
TRACE_FLOW_STRING("col_remove_item", "Exit");
return EOK;
}
/* Remove item (property) from current collection.
* Just a simple wrapper.
*/
int col_remove_item_from_current(struct collection_item *ci,
int disposition,
const char *refprop,
int idx,
int type)
{
int error = EOK;
TRACE_FLOW_STRING("col_remove_item_from_current", "Exit");
/* Remove item from current collection */
error = col_remove_item(ci,
NULL,
disposition,
refprop,
idx,
type);
TRACE_FLOW_NUMBER("col_remove_item_from_current. Exit. Returning", error);
return error;
}
/* Insert the item into the collection or subcollection */
int col_insert_item(struct collection_item *collection,
const char *subcollection,
struct collection_item *item,
int disposition,
const char *refprop,
int idx,
unsigned flags)
{
int error;
struct collection_item *acceptor = NULL;
TRACE_FLOW_STRING("col_insert_item", "Entry point.");
/* Do best effort on the item */
if ((!item) || (item->next)) {
TRACE_ERROR_STRING("Passed in item is invalid", "");
return EINVAL;
}
/* Check that collection is not empty */
if ((collection == NULL) && (item->type != COL_TYPE_COLLECTION)) {
TRACE_ERROR_STRING("Collection can't be NULL", "");
return EINVAL;
}
/* Add item to collection */
if (subcollection == NULL) {
acceptor = collection;
}
else {
TRACE_INFO_STRING("Subcollection id not null, searching", subcollection);
error = col_find_item_and_do(collection, subcollection,
COL_TYPE_COLLECTIONREF,
COL_TRAVERSE_DEFAULT,
col_get_subcollection, (void *)(&acceptor),
COLLECTION_ACTION_FIND);
if (error) {
TRACE_ERROR_NUMBER("Search for subcollection returned error:", error);
return error;
}
if (acceptor == NULL) {
TRACE_ERROR_STRING("Search for subcollection returned NULL pointer", "");
return ENOENT;
}
}
/* Instert item to the current collection */
error = col_insert_item_into_current(acceptor,
item,
disposition,
refprop,
idx,
flags);
if (error) {
TRACE_ERROR_NUMBER("Failed to insert item into current collection", error);
return error;
}
TRACE_FLOW_STRING("insert_item", "Exit");
return EOK;
}
/* Insert property with reference.
* This is internal function so we do not check parameters.
* See external wrapper below.
*/
static int col_insert_property_with_ref_int(struct collection_item *collection,
const char *subcollection,
int disposition,
const char *refprop,
int idx,
unsigned flags,
const char *property,
int type,
const void *data,
int length,
struct collection_item **ret_ref)
{
struct collection_item *item = NULL;
int error;
TRACE_FLOW_STRING("col_insert_property_with_ref_int", "Entry point.");
/* Create a new property out of the given parameters */
error = col_allocate_item(&item, property, data, length, type);
if (error) {
TRACE_ERROR_NUMBER("Failed to allocate item", error);
return error;
}
/* Send the property to the insert_item function */
error = col_insert_item(collection,
subcollection,
item,
disposition,
refprop,
idx,
flags);