Debian Codegraph @

collection/collection.c

raw symbols SCIP
1 /*
2 COLLECTION LIBRARY
3
4 Implementation of the collection library interface.
5
6 Copyright (C) Dmitri Pal <[email protected]> 2009
7
8 Collection Library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Collection Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with Collection Library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <time.h>
28 #include "trace.h"
29
30 /* The collection should use the real structures */
31 #include "collection_priv.h"
32 #include "collection.h"
33
34 #define INTERRUPT(d) ((d == COL_DSP_LASTDUPNS) || (d == COL_DSP_NDUPNS)) ? 0 : 1
35
36 /* Internal constants defined to denote actions that can be performed by find handler */
37 #define COLLECTION_ACTION_FIND 1
38 #define COLLECTION_ACTION_DEL 2
39 #define COLLECTION_ACTION_UPDATE 3
40 #define COLLECTION_ACTION_GET 4
41
42
43 /* Special internal error code to indicate that collection search was interrupted */
44 #define EINTR_INTERNAL 10000
45
46
47 /* Magic numbers for hashing */
48 #if SIZEOF_LONG == 8
49 #define FNV1a_prime 1099511628211ul
50 #define FNV1a_base 14695981039346656037ul
51 #elif SIZEOF_LONG_LONG == 8
52 #define FNV1a_prime 1099511628211ull
53 #define FNV1a_base 14695981039346656037ull
54 #else
55 #error "Platform cannot support 64-bit constant integers"
56 #endif
57
58 /* Struct used for passing parameter for update operation */
59 struct update_property {
60 int type;
61 void *data;
62 int length;
63 int found;
64 };
65
66 /* This struct is used to construct path
67 * to an item in the collection (tree)
68 */
69 struct path_data {
70 char *name;
71 int length;
72 struct path_data *previous_path;
73 };
74
75 /* Structure to keep data needed to
76 * copy collection
77 * while traversing it
78 */
79 struct col_copy {
80 int mode;
81 struct path_data *current_path;
82 char *given_name;
83 int given_len;
84 col_copy_cb copy_cb;
85 void *ext_data;
86 };
87
88 /******************** FUNCTION DECLARATIONS ****************************/
89
90 /* Have to declare those due to function cross referencing */
91 static int col_find_item_and_do(struct collection_item *ci,
92 const char *property_to_find,
93 int type,
94 int mode_flags,
95 col_item_fn item_handler,
96 void *custom_data,
97 int action);
98
99 /* Traverse callback for find & delete function */
100 static int col_act_traverse_handler(struct collection_item *head,
101 struct collection_item *previous,
102 struct collection_item *current,
103 void *passed_traverse_data,
104 col_item_fn user_item_handler,
105 void *custom_data,
106 int *stop);
107
108 /* Traverse handler to find parent of the item */
109 static int col_parent_traverse_handler(struct collection_item *head,
110 struct collection_item *previous,
111 struct collection_item *current,
112 void *traverse_data,
113 col_item_fn user_item_handler,
114 void *custom_data,
115 int *stop);
116
117 /* Traverse callback signature */
118 typedef int (*internal_item_fn)(struct collection_item *head,
119 struct collection_item *previous,
120 struct collection_item *current,
121 void *traverse_data,
122 col_item_fn user_item_handler,
123 void *custom_data,
124 int *stop);
125 /* Function to walk_items */
126 static int col_walk_items(struct collection_item *ci,
127 int mode_flags,
128 internal_item_fn traverse_handler,
129 void *traverse_data,
130 col_item_fn user_item_handler,
131 void *custom_data,
132 unsigned *depth);
133
134 /* Function to get sub collection */
135 static int col_get_subcollection(const char *property,
136 int property_len,
137 int type,
138 void *data,
139 int length,
140 void *found,
141 int *dummy);
142
143 /* Function to destroy collection */
144 void col_destroy_collection(struct collection_item *ci);
145
146 /******************** SUPPLEMENTARY FUNCTIONS ****************************/
147 /* BASIC OPERATIONS */
148
149 /* Function that checks if property can be added */
150 static int col_validate_property(const char *property)
151 {
152 TRACE_FLOW_STRING("col_validate_property", "Entry point.");
153 /* Only alpha numeric characters are allowed in names of the properties */
154 int invalid = 0;
155 const char *check;
156
157 check = property;
158 while (*check != '\0') {
159 /* It turned out that limiting collection charcters is bad */
160 if ((*check < ' ') || (*check == '!')) {
161 invalid = 1;
162 break;
163 }
164 check++;
165 }
166 TRACE_FLOW_NUMBER("col_validate_property. Returning ", invalid);
167 return invalid;
168 }
169
170
171 /* Function that cleans the item with callback */
172 void col_delete_item_with_cb(struct collection_item *item,
173 col_item_cleanup_fn cb,
174 void *custom_data)
175 {
176 struct collection_item *other_collection;
177
178 TRACE_FLOW_STRING("col_delete_item","Entry point.");
179
180 if (item == NULL) {
181 TRACE_FLOW_STRING("col_delete_item","Nothing to delete!");
182 return;
183 }
184
185 /* Handle external or embedded collection */
186 if(item->type == COL_TYPE_COLLECTIONREF) {
187 /* Our data is a pointer to a whole external collection so dereference
188 * it or delete */
189 other_collection = *((struct collection_item **)(item->data));
190 col_destroy_collection_with_cb(other_collection, cb, custom_data);
191 }
192
193 /* Call the callback */
194 if (cb) cb(item->property,
195 item->property_len,
196 item->type,
197 item->data,
198 item->length,
199 custom_data);
200
201 TRACE_INFO_STRING("Deleting property:", item->property);
202 TRACE_INFO_NUMBER("Type:", item->type);
203
204 if (item->property != NULL) free(item->property);
205 if (item->data != NULL) free(item->data);
206
207 free(item);
208
209 TRACE_FLOW_STRING("col_delete_item","Exit.");
210 }
211
212 /* Function that cleans the item */
213 void col_delete_item(struct collection_item *item)
214 {
215 TRACE_FLOW_STRING("col_delete_item","Entry point.");
216
217 col_delete_item_with_cb(item, NULL, NULL);
218
219 TRACE_FLOW_STRING("col_delete_item","Exit.");
220 }
221
222
223
224 /* A generic function to allocate a property item */
225 int col_allocate_item(struct collection_item **ci, const char *property,
226 const void *item_data, int length, int type)
227 {
228 struct collection_item *item = NULL;
229
230 TRACE_FLOW_STRING("col_allocate_item", "Entry point.");
231 TRACE_INFO_NUMBER("Will be using type:", type);
232
233 /* Check the length */
234 if ((length < 0) || (length >= COL_MAX_DATA) ||
235 ((type == COL_TYPE_STRING) && (length == 0))) {
236 TRACE_ERROR_STRING("col_allocate_item", "Bad data length.");
237 return EMSGSIZE;
238 }
239
240 if (col_validate_property(property)) {
241 TRACE_ERROR_STRING("Invalid chracters in the property name", property);
242 return EINVAL;
243 }
244
245 /* Allocate memory for the structure */
246 item = (struct collection_item *)malloc(sizeof(struct collection_item));
247 if (item == NULL) {
248 TRACE_ERROR_STRING("col_allocate_item", "Malloc failed.");
249 return ENOMEM;
250 }
251
252 /* After we initialize members we can use delete_item() in case of error */
253 item->next = NULL;
254 item->property = NULL;
255 item->data = NULL;
256 TRACE_INFO_NUMBER("About to set type to:", type);
257 item->type = type;
258
259 /* Copy property */
260 item->property = strdup(property);
261 if (item->property == NULL) {
262 TRACE_ERROR_STRING("col_allocate_item", "Failed to dup property.");
263 col_delete_item(item);
264 return ENOMEM;
265 }
266
267 item->phash = col_make_hash(property, 0, &(item->property_len));
268 TRACE_INFO_NUMBER("Item hash", item->phash);
269 TRACE_INFO_NUMBER("Item property length", item->property_len);
270 TRACE_INFO_NUMBER("Item property strlen", strlen(item->property));
271
272 /* Deal with data */
273 item->data = malloc(length);
274 if (length > 0) {
275 if (item->data == NULL) {
276 TRACE_ERROR_STRING("col_allocate_item", "Failed to dup data.");
277 col_delete_item(item);
278 return ENOMEM;
279 }
280
281 memcpy(item->data, item_data, length);
282 }
283 item->length = length;
284
285 /* Make sure that data is NULL terminated in case of string */
286 if (type == COL_TYPE_STRING) ((char *)(item->data))[length-1] = '\0';
287
288 *ci = item;
289
290 TRACE_INFO_STRING("Item property", item->property);
291 TRACE_INFO_NUMBER("Item property type", item->type);
292 TRACE_INFO_NUMBER("Item data length", item->length);
293 TRACE_FLOW_STRING("col_allocate_item", "Success exit.");
294 return EOK;
295 }
296
297 /* Structure used to find things in collection */
298 struct property_search {
299 const char *property;
300 uint64_t hash;
301 struct collection_item *parent;
302 int index;
303 int count;
304 int found;
305 int use_type;
306 int type;
307 int interrupt;
308 int exact;
309 };
310
311 /* Find the parent of the item with given name */
312 static int col_find_property_sub(struct collection_item *collection,
313 const char *subcollection,
314 int interrupt,
315 const char *refprop,
316 int idx,
317 int exact,
318 int use_type,
319 int type,
320 struct collection_item **parent)
321 {
322 struct property_search ps;
323 int i = 0;
324 unsigned depth = 0;
325 struct collection_item *sub = NULL;
326 int error = EOK;
327
328 TRACE_FLOW_ENTRY();
329
330 *parent = NULL;
331
332 ps.property = refprop;
333 ps.hash = FNV1a_base;
334 ps.parent = NULL;
335 ps.index = idx;
336 ps.count = 0;
337 ps.found = 0;
338 ps.use_type = use_type;
339 ps.type = type;
340 ps.interrupt = interrupt;
341 ps.exact = exact;
342
343 /* Create hash of the string to search */
344 while(refprop[i] != 0) {
345 ps.hash = ps.hash ^ toupper(refprop[i]);
346 ps.hash *= FNV1a_prime;
347 i++;
348 }
349
350 /* Add item to collection */
351 if (subcollection == NULL) {
352 sub = collection;
353 }
354 else {
355 TRACE_INFO_STRING("Subcollection is not null, searching",
356 subcollection);
357 error = col_find_item_and_do(collection, subcollection,
358 COL_TYPE_COLLECTIONREF,
359 COL_TRAVERSE_DEFAULT,
360 col_get_subcollection, (void *)(&sub),
361 COLLECTION_ACTION_FIND);
362 if (error) {
363 TRACE_ERROR_NUMBER("Search for subcollection returned error:",
364 error);
365 /* Not found */
366 return 0;
367 }
368
369 if (sub == NULL) {
370 TRACE_ERROR_STRING("Search for subcollection returned NULL", "");
371 /* Not found */
372 return 0;
373 }
374
375 }
376
377 /* We do not care about error here */
378 (void)col_walk_items(sub, COL_TRAVERSE_ONELEVEL,
379 col_parent_traverse_handler,
380 (void *)parent, NULL, (void *)&ps,
381 &depth);
382
383 if (*parent) {
384 /* Item is found in the collection */
385 TRACE_FLOW_STRING("col_find_property", "Exit - item found");
386 return 1;
387 }
388
389 /* Item is not found */
390 TRACE_FLOW_STRING("col_find_property", "Exit - item NOT found");
391 return 0;
392 }
393
394 /* Backward compatible wrapper */
395 static int col_find_property(struct collection_item *collection,
396 const char *refprop,
397 int idx,
398 int use_type,
399 int type,
400 struct collection_item **parent)
401 {
402 int error = EOK;
403
404 TRACE_FLOW_ENTRY();
405
406 error = col_find_property_sub(collection,
407 NULL,
408 1,
409 refprop,
410 idx,
411 0,
412 use_type,
413 type,
414 parent);
415
416 TRACE_FLOW_RETURN(error);
417 return error;
418 }
419
420
421
422 /* Find a duplicate item */
423 int col_get_dup_item(struct collection_item *ci,
424 const char *subcollection,
425 const char *property_to_find,
426 int type,
427 int idx,
428 int exact,
429 struct collection_item **item)
430 {
431 int error = EOK;
432 struct collection_item *parent = NULL;
433
434 TRACE_FLOW_ENTRY();
435
436 if (!ci) {
437 TRACE_ERROR_STRING("Passed in collection is invalid", "");
438 return EINVAL;
439 }
440
441 if (!item) {
442 TRACE_ERROR_STRING("Result storage is invalid", "");
443 return EINVAL;
444 }
445
446 if (!property_to_find) {
447 TRACE_ERROR_STRING("Passed in property to find is NULL", "");
448 return EINVAL;
449 }
450
451 /* Find the corresponding duplicate item */
452 if (col_find_property_sub(ci,
453 subcollection,
454 0,
455 property_to_find,
456 idx,
457 exact,
458 (type == COL_TYPE_ANY) ? 0 : 1,
459 type,
460 &parent)) {
461 *item = parent->next;
462 /* function col_get_dup_item with big value of idx should
463 * return the last item with the same property name. */
464 if (strncmp(property_to_find,
465 parent->next->property,
466 parent->next->property_len) != 0) {
467 *item = parent;
468 }
469 }
470 else error = ENOENT;
471
472 TRACE_FLOW_EXIT();
473 return error;
474 }
475
476 /* Insert item into the current collection */
477 int col_insert_item_into_current(struct collection_item *collection,
478 struct collection_item *item,
479 int disposition,
480 const char *refprop,
481 int idx,
482 unsigned flags)
483 {
484 struct collection_header *header = NULL;
485 struct collection_item *parent = NULL;
486 struct collection_item *current = NULL;
487 int refindex = 0;
488
489 TRACE_FLOW_STRING("col_insert_item_into_current", "Entry point");
490
491 /* Do best effort on the item */
492 if ((!item) || (item->next)) {
493 TRACE_ERROR_STRING("Passed in item is invalid", "");
494 return EINVAL;
495 }
496
497 if (collection == NULL) {
498 TRACE_INFO_STRING("col_insert_item_into_current",
499 "Collection accepting is NULL");
500 if (item->type == COL_TYPE_COLLECTION) {
501 /* This is a special case of self creation */
502 TRACE_INFO_STRING("col_insert_item_into_current",
503 "Adding header item to new collection.");
504 collection = item;
505 }
506 else {
507 TRACE_ERROR_STRING("Passed in item is invalid", "");
508 return EINVAL;
509 }
510 }
511 else {
512 /* We can add items only to collections */
513 if (collection->type != COL_TYPE_COLLECTION) {
514 TRACE_ERROR_STRING("Attempt to add item to non collection.","");
515 TRACE_ERROR_STRING("Collection name:", collection->property);
516 TRACE_ERROR_NUMBER("Collection type:", collection->type);
517 return EINVAL;
518 }
519 }
520
521 /* After processing flags we can process disposition */
522
523 header = (struct collection_header *)collection->data;
524
525 /* Check flags first */
526 switch(flags) {
527 case COL_INSERT_NOCHECK: /* No check - good just fall through */
528 TRACE_INFO_STRING("Insert without check", "");
529 break;
530 case COL_INSERT_DUPOVER: /* Find item and overwrite - ignore disposition */
531 if (col_find_property(collection, item->property, 0, 0, 0, &parent)) {
532 current = parent->next;
533 item->next = current->next;
534 parent->next = item;
535 if (header->last == current) header->last = item;
536 col_delete_item(current);
537 /* Deleted one added another - count stays the same! */
538 TRACE_FLOW_STRING("col_insert_item_into_current", "Dup overwrite exit");
539 return EOK;
540 }
541 /* Not found so we fall thorough and add as requested */
542 break;
543
544 case COL_INSERT_DUPOVERT: /* Find item by name and type and overwrite - ignore disposition */
545 if (col_find_property(collection, item->property, 0, 1, item->type, &parent)) {
546 current = parent->next;
547 item->next = current->next;
548 parent->next = item;
549 if (header->last == current) header->last = item;
550 col_delete_item(current);
551 /* Deleted one added another - count stays the same! */
552 TRACE_FLOW_STRING("col_insert_item_into_current", "Dup overwrite exit");
553 return EOK;
554 }
555 /* Not found so we fall thorough and add as requested */
556 break;
557
558 case COL_INSERT_DUPERROR: if (col_find_property(collection, item->property, 0, 0, 0, &parent)) {
559 /* Return error */
560 TRACE_ERROR_NUMBER("Duplicate property", EEXIST);
561 return EEXIST;
562 }
563 break;
564
565 case COL_INSERT_DUPERRORT: if (col_find_property(collection, item->property, 0, 1, item->type, &parent)) {
566 /* Return error */
567 TRACE_ERROR_NUMBER("Duplicate property of the same type", EEXIST);
568 return EEXIST;
569 }
570 break;
571
572 case COL_INSERT_DUPMOVE: /* Find item and delete */
573 if (col_find_property(collection, item->property, 0, 0, 0, &parent)) {
574 current = parent->next;
575 parent->next = current->next;
576 if (header->last == current) header->last = parent;
577 col_delete_item(current);
578 header->count--;
579 }
580 /* Now add item according to the disposition */
581 break;
582
583 case COL_INSERT_DUPMOVET: /* Find item and delete */
584 TRACE_INFO_STRING("Property:", item->property);
585 TRACE_INFO_NUMBER("Type:", item->type);
586 if (col_find_property(collection, item->property, 0, 1, item->type, &parent)) {
587 TRACE_INFO_LNUMBER("Current:", parent->next);
588 current = parent->next;
589 parent->next = current->next;
590 if (header->last == current) header->last = parent;
591 col_delete_item(current);
592 header->count--;
593 }
594 /* Now add item according to the disposition */
595 break;
596
597 default: /* The new ones should be added here */
598 TRACE_ERROR_NUMBER("Flag is not implemented", ENOSYS);
599 return ENOSYS;
600 }
601
602
603 switch (disposition) {
604 case COL_DSP_END: /* Link new item to the last item in the list if there any */
605 if (header->count != 0) header->last->next = item;
606 /* Make sure we save a new last element */
607 header->last = item;
608 header->count++;
609 break;
610
611 case COL_DSP_FRONT: /* Same as above if there is header only */
612 if (header->count == 1) {
613 header->last->next = item;
614 header->last = item;
615 }
616 else {
617 item->next = collection->next;
618 collection->next = item;
619 }
620 header->count++;
621 break;
622
623 case COL_DSP_BEFORE: /* Check argument */
624 if (!refprop) {
625 TRACE_ERROR_STRING("In this case property is required", "");
626 return EINVAL;
627 }
628
629 /* We need to find property */
630 if (col_find_property(collection, refprop, 0, 0, 0, &parent)) {
631 item->next = parent->next;
632 parent->next = item;
633 header->count++;
634 }
635 else {
636 TRACE_ERROR_STRING("Property not found", refprop);
637 return ENOENT;
638 }
639 break;
640
641 case COL_DSP_AFTER: /* Check argument */
642 if (!refprop) {
643 TRACE_ERROR_STRING("In this case property is required", "");
644 return EINVAL;
645 }
646
647 /* We need to find property */
648 if (col_find_property(collection, refprop, 0, 0, 0, &parent)) {
649 parent = parent->next;
650 if (parent->next) {
651 /* It is not the last item */
652 item->next = parent->next;
653 parent->next = item;
654 }
655 else {
656 /* It is the last item */
657 header->last->next = item;
658 header->last = item;
659 }
660 header->count++;
661 }
662 else {
663 TRACE_ERROR_STRING("Property not found", refprop);
664 return ENOENT;
665 }
666 break;
667
668 case COL_DSP_INDEX: if(idx == 0) {
669 /* Same is first */
670 if (header->count == 1) {
671 header->last->next = item;
672 header->last = item;
673 }
674 else {
675 item->next = collection->next;
676 collection->next = item;
677 }
678 }
679 else if(idx >= header->count - 1) {
680 /* In this case add to the end */
681 header->last->next = item;
682 /* Make sure we save a new last element */
683 header->last = item;
684 }
685 else {
686 /* In the middle */
687 parent = collection;
688 /* Move to the right position counting */
689 while (idx > 0) {
690 idx--;
691 if (parent->next) parent = parent->next;
692 }
693 item->next = parent->next;
694 parent->next = item;
695 }
696 header->count++;
697 break;
698
699 case COL_DSP_FIRSTDUP:
700 case COL_DSP_LASTDUP:
701 case COL_DSP_NDUP:
702 case COL_DSP_LASTDUPNS:
703 case COL_DSP_NDUPNS:
704
705 if (disposition == COL_DSP_FIRSTDUP) refindex = 0;
706 else if ((disposition == COL_DSP_LASTDUP) ||
707 (disposition == COL_DSP_LASTDUPNS))
708 refindex = -1;
709 else refindex = idx;
710
711 /* We need to find property based on index */
712 if (col_find_property_sub(collection,
713 NULL,
714 INTERRUPT(disposition),
715 item->property,
716 refindex,
717 0,
718 0,
719 0,
720 &parent)) {
721 item->next = parent->next;
722 parent->next = item;
723 header->count++;
724 if(header->last == parent) header->last = item;
725 }
726 else {
727 TRACE_ERROR_STRING("Property not found", refprop);
728 return ENOENT;
729 }
730 break;
731
732 default:
733 TRACE_ERROR_STRING("Disposition is not implemented", "");
734 return ENOSYS;
735
736 }
737
738
739 TRACE_INFO_STRING("Collection:", collection->property);
740 TRACE_INFO_STRING("Just added item is:", item->property);
741 TRACE_INFO_NUMBER("Item type.", item->type);
742 TRACE_INFO_NUMBER("Number of items in collection now is.", header->count);
743
744 TRACE_FLOW_STRING("col_insert_item_into_current", "Exit");
745 return EOK;
746 }
747
748 /* Extract item from the current collection */
749 int col_extract_item_from_current(struct collection_item *collection,
750 int disposition,
751 const char *refprop,
752 int idx,
753 int type,
754 struct collection_item **ret_ref)
755 {
756 struct collection_header *header = NULL;
757 struct collection_item *parent = NULL;
758 struct collection_item *current = NULL;
759 struct collection_item *found = NULL;
760 int refindex = 0;
761 int use_type = 0;
762
763 TRACE_FLOW_STRING("col_extract_item_from_current", "Entry point");
764
765 /* Check that collection is not empty */
766 if ((collection == NULL) || (collection->type != COL_TYPE_COLLECTION)) {
767 TRACE_ERROR_STRING("Collection can't be NULL", "");
768 return EINVAL;
769 }
770
771 header = (struct collection_header *)collection->data;
772
773 /* Before moving forward we need to check if there is anything to extract */
774 if (header->count <= 1) {
775 TRACE_ERROR_STRING("Collection is empty.", "Nothing to extract.");
776 return ENOENT;
777 }
778
779 if (type != 0) use_type = 1;
780
781 switch (disposition) {
782 case COL_DSP_END: /* Extract last item in the list. */
783 parent = collection;
784 current = collection->next;
785 while (current->next != NULL) {
786 parent = current;
787 current = current->next;
788 }
789 *ret_ref = parent->next;
790 parent->next = NULL;
791 /* Special case - one data element */
792 if (header->count == 2) header->last = collection;
793 else header->last = parent;
794 break;
795
796 case COL_DSP_FRONT: /* Extract first item in the list */
797 *ret_ref = collection->next;
798 collection->next = (*ret_ref)->next;
799 /* Special case - one data element */
800 if (header->count == 2) header->last = collection;
801 break;
802
803 case COL_DSP_BEFORE: /* Check argument */
804 if (!refprop) {
805 TRACE_ERROR_STRING("In this case property is required", "");
806 return EINVAL;
807 }
808
809 /* We have to do it in two steps */
810 /* First find the property that is mentioned */
811 if (col_find_property(collection, refprop, 0, use_type, type, &found)) {
812 /* We found the requested property */
813 if (found->next == collection->next) {
814 /* The referenced property is the first in the list */
815 TRACE_ERROR_STRING("Nothing to extract. Lists starts with property", refprop);
816 return ENOENT;
817 }
818 /* Get to the parent of the item that is before the one that is found */
819 parent = collection;
820 current = collection->next;
821 while (current != found) {
822 parent = current;
823 current = current->next;
824 }
825 *ret_ref = current;
826 parent->next = current->next;
827
828 }
829 else {
830 TRACE_ERROR_STRING("Property not found", refprop);
831 return ENOENT;
832 }
833 break;
834
835 case COL_DSP_AFTER: /* Check argument */
836 if (!refprop) {
837 TRACE_ERROR_STRING("In this case property is required", "");
838 return EINVAL;
839 }
840
841 /* We need to find property */
842 if (col_find_property(collection, refprop, 0, use_type, type, &parent)) {
843 current = parent->next;
844 if (current->next) {
845 *ret_ref = current->next;
846 current->next = (*ret_ref)->next;
847 /* If we removed the last element adjust header */
848 if(current->next == NULL) header->last = current;
849 }
850 else {
851 TRACE_ERROR_STRING("Property is last in the list", refprop);
852 return ENOENT;
853 }
854 }
855 else {
856 TRACE_ERROR_STRING("Property not found", refprop);
857 return ENOENT;
858 }
859 break;
860
861 case COL_DSP_INDEX: if (idx == 0) {
862 *ret_ref = collection->next;
863 collection->next = (*ret_ref)->next;
864 /* Special case - one data element */
865 if (header->count == 2) header->last = collection;
866 }
867 /* Index 0 stands for the first data element.
868 * Count includes header element.
869 */
870 else if (idx >= (header->count - 1)) {
871 TRACE_ERROR_STRING("Index is out of boundaries", refprop);
872 return ENOENT;
873 }
874 else {
875 /* Loop till the element with right index */
876 refindex = 0;
877 parent = collection;
878 current = collection->next;
879 while (refindex < idx) {
880 parent = current;
881 current = current->next;
882 refindex++;
883 }
884 *ret_ref = parent->next;
885 parent->next = (*ret_ref)->next;
886 /* If we removed the last element adjust header */
887 if (parent->next == NULL) header->last = parent;
888 }
889 break;
890
891 case COL_DSP_FIRSTDUP:
892 case COL_DSP_LASTDUP:
893 case COL_DSP_NDUP:
894 case COL_DSP_LASTDUPNS:
895 case COL_DSP_NDUPNS:
896
897 if (disposition == COL_DSP_FIRSTDUP) refindex = 0;
898 else if ((disposition == COL_DSP_LASTDUP) ||
899 (disposition == COL_DSP_LASTDUPNS))
900 refindex = -2;
901 else refindex = idx;
902
903 /* We need to find property based on index */
904 if (col_find_property_sub(collection,
905 NULL,
906 INTERRUPT(disposition),
907 refprop,
908 refindex,
909 1,
910 use_type,
911 type,
912 &parent)) {
913 *ret_ref = parent->next;
914 parent->next = (*ret_ref)->next;
915 /* If we removed the last element adjust header */
916 if(parent->next == NULL) header->last = parent;
917 }
918 else {
919 TRACE_ERROR_STRING("Property not found", refprop);
920 return ENOENT;
921 }
922 break;
923
924 default:
925 TRACE_ERROR_STRING("Disposition is not implemented", "");
926 return ENOSYS;
927
928 }
929
930
931 /* Clear item and reduce count */
932 (*ret_ref)->next = NULL;
933 header->count--;
934
935 TRACE_INFO_STRING("Collection:", (*ret_ref)->property);
936 TRACE_INFO_NUMBER("Item type.", (*ret_ref)->type);
937 TRACE_INFO_NUMBER("Number of items in collection now is.", header->count);
938
939 TRACE_FLOW_STRING("col_extract_item_from_current", "Exit");
940 return EOK;
941 }
942
943 /* Extract item from the collection */
944 int col_extract_item(struct collection_item *collection,
945 const char *subcollection,
946 int disposition,
947 const char *refprop,
948 int idx,
949 int type,
950 struct collection_item **ret_ref)
951 {
952 struct collection_item *col = NULL;
953 int error = EOK;
954
955 TRACE_FLOW_STRING("col_extract_item", "Entry point");
956
957 /* Check that collection is not empty */
958 if ((collection == NULL) || (collection->type != COL_TYPE_COLLECTION)) {
959 TRACE_ERROR_STRING("Collection can't be NULL", "");
960 return EINVAL;
961 }
962
963 /* Get subcollection if needed */
964 if (subcollection == NULL) {
965 col = collection;
966 }
967 else {
968 TRACE_INFO_STRING("Subcollection id not null, searching", subcollection);
969 error = col_find_item_and_do(collection, subcollection,
970 COL_TYPE_COLLECTIONREF,
971 COL_TRAVERSE_DEFAULT,
972 col_get_subcollection, (void *)(&col),
973 COLLECTION_ACTION_FIND);
974 if (error) {
975 TRACE_ERROR_NUMBER("Search for subcollection returned error:", error);
976 return error;
977 }
978
979 if (col == NULL) {
980 TRACE_ERROR_STRING("Search for subcollection returned NULL pointer", "");
981 return ENOENT;
982 }
983
984 }
985
986 /* Extract from the current collection */
987 error = col_extract_item_from_current(col,
988 disposition,
989 refprop,
990 idx,
991 type,
992 ret_ref);
993 if (error) {
994 TRACE_ERROR_NUMBER("Failed to extract item from the current collection", error);
995 return error;
996 }
997
998 TRACE_FLOW_STRING("col_extract_item", "Exit");
999 return EOK;
1000 }
1001
1002
1003 /* Remove item (property) from collection with callback.*/
1004 int col_remove_item_with_cb(struct collection_item *ci,
1005 const char *subcollection,
1006 int disposition,
1007 const char *refprop,
1008 int idx,
1009 int type,
1010 col_item_cleanup_fn cb,
1011 void *custom_data)
1012 {
1013 int error = EOK;
1014 struct collection_item *ret_ref = NULL;
1015
1016 TRACE_FLOW_STRING("col_remove_item", "Enter");
1017
1018 /* Extract from the current collection */
1019 error = col_extract_item(ci,
1020 subcollection,
1021 disposition,
1022 refprop,
1023 idx,
1024 type,
1025 &ret_ref);
1026 if (error) {
1027 TRACE_ERROR_NUMBER("Failed to extract item from the collection", error);
1028 return error;
1029 }
1030
1031 col_delete_item_with_cb(ret_ref, cb, custom_data);
1032
1033 TRACE_FLOW_STRING("col_remove_item", "Exit");
1034 return EOK;
1035 }
1036
1037 /* Remove item (property) from collection.*/
1038 int col_remove_item(struct collection_item *ci,
1039 const char *subcollection,
1040 int disposition,
1041 const char *refprop,
1042 int idx,
1043 int type)
1044 {
1045 return col_remove_item_with_cb(ci,
1046 subcollection,
1047 disposition,
1048 refprop,
1049 idx,
1050 type,
1051 NULL,
1052 NULL);
1053 }
1054
1055 /* Remove item (property) from current collection.
1056 * Just a simple wrapper.
1057 */
1058 int col_remove_item_from_current(struct collection_item *ci,
1059 int disposition,
1060 const char *refprop,
1061 int idx,
1062 int type)
1063 {
1064 int error = EOK;
1065
1066 TRACE_FLOW_STRING("col_remove_item_from_current", "Exit");
1067
1068 /* Remove item from current collection */
1069 error = col_remove_item(ci,
1070 NULL,
1071 disposition,
1072 refprop,
1073 idx,
1074 type);
1075
1076 TRACE_FLOW_NUMBER("col_remove_item_from_current. Exit. Returning", error);
1077 return error;
1078 }
1079
1080
1081 /* Insert the item into the collection or subcollection */
1082 int col_insert_item(struct collection_item *collection,
1083 const char *subcollection,
1084 struct collection_item *item,
1085 int disposition,
1086 const char *refprop,
1087 int idx,
1088 unsigned flags)
1089 {
1090 int error;
1091 struct collection_item *acceptor = NULL;
1092
1093 TRACE_FLOW_STRING("col_insert_item", "Entry point.");
1094
1095 /* Do best effort on the item */
1096 if ((!item) || (item->next)) {
1097 TRACE_ERROR_STRING("Passed in item is invalid", "");
1098 return EINVAL;
1099 }
1100
1101 /* Check that collection is not empty */
1102 if ((collection == NULL) && (item->type != COL_TYPE_COLLECTION)) {
1103 TRACE_ERROR_STRING("Collection can't be NULL", "");
1104 return EINVAL;
1105 }
1106
1107 /* Add item to collection */
1108 if (subcollection == NULL) {
1109 acceptor = collection;
1110 }
1111 else {
1112 TRACE_INFO_STRING("Subcollection id not null, searching", subcollection);
1113 error = col_find_item_and_do(collection, subcollection,
1114 COL_TYPE_COLLECTIONREF,
1115 COL_TRAVERSE_DEFAULT,
1116 col_get_subcollection, (void *)(&acceptor),
1117 COLLECTION_ACTION_FIND);
1118 if (error) {
1119 TRACE_ERROR_NUMBER("Search for subcollection returned error:", error);
1120 return error;
1121 }
1122
1123 if (acceptor == NULL) {
1124 TRACE_ERROR_STRING("Search for subcollection returned NULL pointer", "");
1125 return ENOENT;
1126 }
1127
1128 }
1129
1130 /* Instert item to the current collection */
1131 error = col_insert_item_into_current(acceptor,
1132 item,
1133 disposition,
1134 refprop,
1135 idx,
1136 flags);
1137
1138 if (error) {
1139 TRACE_ERROR_NUMBER("Failed to insert item into current collection", error);
1140 return error;
1141 }
1142
1143 TRACE_FLOW_STRING("insert_item", "Exit");
1144 return EOK;
1145 }
1146
1147
1148 /* Insert property with reference.
1149 * This is internal function so we do not check parameters.
1150 * See external wrapper below.
1151 */
1152 static int col_insert_property_with_ref_int(struct collection_item *collection,
1153 const char *subcollection,
1154 int disposition,
1155 const char *refprop,
1156 int idx,
1157 unsigned flags,
1158 const char *property,
1159 int type,
1160 const void *data,
1161 int length,
1162 struct collection_item **ret_ref)
1163 {
1164 struct collection_item *item = NULL;
1165 int error;
1166
1167 TRACE_FLOW_STRING("col_insert_property_with_ref_int", "Entry point.");
1168
1169 /* Create a new property out of the given parameters */
1170 error = col_allocate_item(&item, property, data, length, type);
1171 if (error) {
1172 TRACE_ERROR_NUMBER("Failed to allocate item", error);
1173 return error;
1174 }
1175
1176 /* Send the property to the insert_item function */
1177 error = col_insert_item(collection,
1178 subcollection,
1179 item,
1180 disposition,
1181 refprop,
1182 idx,
1183 flags);
1184 if (error) {
1185 TRACE_ERROR_NUMBER("Failed to insert item", error);
1186 col_delete_item(item);
1187 return error;
1188 }
1189
1190 if (ret_ref) *ret_ref = item;
1191
1192 TRACE_FLOW_STRING("col_insert_property_with_ref_int", "Exit");
1193 return EOK;
1194 }
1195
1196 /* Special function used to copy item from one
1197 * collection to another using caller's callback.
1198 */
1199 static int col_copy_item_with_cb(struct collection_item *collection,
1200 const char *property,
1201 int type,
1202 const void *data,
1203 int length,
1204 col_copy_cb copy_cb,
1205 void *ext_data)
1206 {
1207 struct collection_item *item = NULL;
1208 int skip = 0;
1209 int error = EOK;
1210
1211 TRACE_FLOW_STRING("col_copy_item_with_cb", "Entry point.");
1212
1213 /* Create a new property out of the given parameters */
1214 error = col_allocate_item(&item, property, data, length, type);
1215 if (error) {
1216 TRACE_ERROR_NUMBER("Failed to allocate item", error);
1217 return error;
1218 }
1219
1220 /* Call callback if any */
1221 if (copy_cb) {
1222 TRACE_INFO_STRING("Calling callback for item:", item->property);
1223 error = copy_cb(item, ext_data, &skip);
1224 if (error) {
1225 TRACE_ERROR_NUMBER("Callback failed", error);
1226 col_delete_item(item);
1227 return error;
1228 }
1229 }
1230
1231 /* Are we told to skip this item? */
1232 if (skip) col_delete_item(item);
1233 else {
1234 /* Insted property into the collection */
1235 error = col_insert_item(collection,
1236 NULL,
1237 item,
1238 COL_DSP_END,
1239 NULL,
1240 0,
1241 0);
1242 if (error) {
1243 TRACE_ERROR_NUMBER("Failed to insert item", error);
1244 col_delete_item(item);
1245 return error;
1246 }
1247 }
1248
1249 TRACE_FLOW_STRING("col_copy_item_with_cb", "Exit");
1250 return EOK;
1251 }
1252
1253
1254 /* This is public function so we need to check the validity
1255 * of the arguments.
1256 */
1257 int col_insert_property_with_ref(struct collection_item *collection,
1258 const char *subcollection,
1259 int disposition,
1260 const char *refprop,
1261 int idx,
1262 unsigned flags,
1263 const char *property,
1264 int type,
1265 const void *data,
1266 int length,
1267 struct collection_item **ret_ref)
1268 {
1269 int error;
1270
1271 TRACE_FLOW_STRING("col_insert_property_with_ref", "Entry point.");
1272
1273 /* Check that collection is not empty */
1274 if (collection == NULL) {
1275 TRACE_ERROR_STRING("Collection cant be NULL", "");
1276 return EINVAL;
1277 }
1278
1279 error = col_insert_property_with_ref_int(collection,
1280 subcollection,
1281 disposition,
1282 refprop,
1283 idx,
1284 flags,
1285 property,
1286 type,
1287 data,
1288 length,
1289 ret_ref);
1290
1291 TRACE_FLOW_NUMBER("col_insert_property_with_ref_int Returning:", error);
1292 return error;
1293 }
1294 /* TRAVERSE HANDLERS */
1295
1296 /* Special handler to just set a flag if the item is found */
1297 static int col_is_in_item_handler(const char *property,
1298 int property_len,
1299 int type,
1300 void *data,
1301 int length,
1302 void *found,
1303 int *dummy)
1304 {
1305 TRACE_FLOW_STRING("col_is_in_item_handler", "Entry.");
1306 TRACE_INFO_STRING("Property:", property);
1307 TRACE_INFO_NUMBER("Property length:", property_len);
1308 TRACE_INFO_NUMBER("Type:", type);
1309 TRACE_INFO_NUMBER("Length:", length);
1310
1311 *((int *)(found)) = COL_MATCH;
1312
1313 TRACE_FLOW_STRING("col_is_in_item_handler", "Success Exit.");
1314
1315 return EOK;
1316 }
1317
1318 /* Special handler to retrieve the sub collection */
1319 static int col_get_subcollection(const char *property,
1320 int property_len,
1321 int type,
1322 void *data,
1323 int length,
1324 void *found,
1325 int *dummy)
1326 {
1327 TRACE_FLOW_STRING("col_get_subcollection", "Entry.");
1328 TRACE_INFO_STRING("Property:", property);
1329 TRACE_INFO_NUMBER("Property length:", property_len);
1330 TRACE_INFO_NUMBER("Type:", type);
1331 TRACE_INFO_NUMBER("Length:", length);
1332
1333 *((struct collection_item **)(found)) = *((struct collection_item **)(data));
1334
1335 TRACE_FLOW_STRING("col_get_subcollection","Success Exit.");
1336
1337 return EOK;
1338
1339 }
1340
1341
1342
1343 /* CLEANUP */
1344
1345 /* Cleans the collection tree including current item. */
1346 /* The passed in variable should not be used after the call
1347 * as memory is freed!!! */
1348 static void col_delete_collection(struct collection_item *ci,
1349 col_item_cleanup_fn cb,
1350 void *custom_data)
1351 {
1352 TRACE_FLOW_STRING("col_delete_collection", "Entry.");
1353
1354 if (ci == NULL) {
1355 TRACE_FLOW_STRING("col_delete_collection", "Nothing to do Exit.");
1356 return;
1357 }
1358
1359 TRACE_INFO_STRING("Real work to do", "");
1360 TRACE_INFO_STRING("Property", ci->property);
1361 TRACE_INFO_NUMBER("Next item", ci->next);
1362
1363 col_delete_collection(ci->next, cb, custom_data);
1364
1365 /* Delete this item */
1366 col_delete_item_with_cb(ci, cb, custom_data);
1367 TRACE_FLOW_STRING("col_delete_collection", "Exit.");
1368 }
1369
1370
1371 /* NAME MANAGEMENT - used by search */
1372
1373 /* Internal data structures used for search */
1374
1375
1376 struct find_name {
1377 const char *name_to_find;
1378 int name_len_to_find;
1379 uint64_t hash;
1380 int type_to_match;
1381 char *given_name;
1382 int given_len;
1383 struct path_data *current_path;
1384 int action;
1385 };
1386
1387 /* Create a new name */
1388 static int col_create_path_data(struct path_data **name_path,
1389 const char *name, int length,
1390 const char *property, int property_len,
1391 char sep)
1392 {
1393 int error = EOK;
1394 struct path_data *new_name_path;
1395
1396 TRACE_FLOW_STRING("col_create_path_data", "Entry.");
1397
1398 TRACE_INFO_STRING("Constructing path from name:", name);
1399 TRACE_INFO_STRING("Constructing path from property:", property);
1400
1401 /* Allocate structure */
1402 new_name_path = (struct path_data *)malloc(sizeof(struct path_data));
1403 if (new_name_path == NULL) {
1404 TRACE_ERROR_NUMBER("Failed to allocate memory for new path struct.", ENOMEM);
1405 return ENOMEM;
1406 }
1407 new_name_path->name = malloc(length + property_len + 2);
1408 if (new_name_path->name == NULL) {
1409 TRACE_ERROR_NUMBER("Failed to allocate memory for new path name.", ENOMEM);
1410 free(new_name_path);
1411 return ENOMEM;
1412 }
1413
1414 /* Construct the new name */
1415 new_name_path->length = 0;
1416
1417 if(length > 0) {
1418 memcpy(new_name_path->name, name, length);
1419 new_name_path->length = length;
1420 new_name_path->name[new_name_path->length] = sep;
1421 new_name_path->length++;
1422 new_name_path->name[new_name_path->length] = '\0';
1423 TRACE_INFO_STRING("Name so far:", new_name_path->name);
1424 TRACE_INFO_NUMBER("Len so far:", new_name_path->length);
1425 }
1426 memcpy(&new_name_path->name[new_name_path->length], property, property_len);
1427 new_name_path->length += property_len;
1428 new_name_path->name[new_name_path->length] = '\0';
1429
1430 /* Link to the chain */
1431 new_name_path->previous_path = *name_path;
1432 *name_path = new_name_path;
1433
1434 TRACE_INFO_STRING("Constructed path", new_name_path->name);
1435
1436
1437 TRACE_FLOW_NUMBER("col_create_path_data. Returning:", error);
1438 return error;
1439 }
1440
1441 /* Matching item name and type */
1442 static int col_match_item(struct collection_item *current,
1443 struct find_name *traverse_data)
1444 {
1445
1446 const char *find_str;
1447 const char *start;
1448 const char *data_str;
1449
1450 TRACE_FLOW_STRING("col_match_item", "Entry");
1451
1452 if (traverse_data->type_to_match & current->type) {
1453
1454 /* Check if there is any value to match */
1455 if ((traverse_data->name_to_find == NULL) ||
1456 (*(traverse_data->name_to_find) == '\0')) {
1457 TRACE_INFO_STRING("col_match_item",
1458 "Returning MATCH because there is no search criteria!");
1459 return COL_MATCH;
1460 }
1461
1462 /* Check the hashes - if they do not match return */
1463 if (traverse_data->hash != current->phash) {
1464 TRACE_INFO_STRING("col_match_item","Returning NO match!");
1465 return COL_NOMATCH;
1466 }
1467
1468 /* We will do the actual string comparison only if the hashes matched */
1469
1470 /* Start comparing the two strings from the end */
1471 find_str = traverse_data->name_to_find + traverse_data->name_len_to_find;
1472 start = current->property;
1473 data_str = start + current->property_len;
1474
1475 TRACE_INFO_STRING("Searching for:", traverse_data->name_to_find);
1476 TRACE_INFO_STRING("Item name:", current->property);
1477 TRACE_INFO_STRING("Current path:", traverse_data->current_path->name);
1478 TRACE_INFO_NUMBER("Searching:", toupper(*find_str));
1479 TRACE_INFO_NUMBER("Have:", toupper(*data_str));
1480
1481 /* We start pointing to 0 so the loop will be executed at least once */
1482 while (toupper(*data_str) == toupper(*find_str)) {
1483
1484 TRACE_INFO_STRING("Loop iteration:","");
1485
1486 if (data_str == start) {
1487 if (find_str > traverse_data->name_to_find) {
1488 if (*(find_str-1) == '!') {
1489 /* We matched the property but the search string is
1490 * longer so we need to continue matching */
1491 TRACE_INFO_STRING("col_match_item",
1492 "Need to continue matching");
1493 start = traverse_data->current_path->name;
1494 data_str = &start[traverse_data->current_path->length - 1];
1495 find_str -= 2;
1496 continue;
1497 }
1498 else {
1499 TRACE_INFO_STRING("col_match_item","Returning NO match!");
1500 return COL_NOMATCH;
1501 }
1502 }
1503 else {
1504 TRACE_INFO_STRING("col_match_item","Returning MATCH!");
1505 return COL_MATCH;
1506 }
1507 }
1508 else if ((find_str == traverse_data->name_to_find) &&
1509 (*(data_str-1) == '!')) return COL_MATCH;
1510
1511 data_str--;
1512 find_str--;
1513 TRACE_INFO_NUMBER("Searching:", toupper(*find_str));
1514 TRACE_INFO_NUMBER("Have:", toupper(*data_str));
1515
1516 }
1517 }
1518
1519 TRACE_FLOW_STRING("col_match_item","Returning NO match!");
1520 return COL_NOMATCH;
1521
1522 }
1523
1524 /* Function to delete the data that contains search path */
1525 static void col_delete_path_data(struct path_data *path)
1526 {
1527 TRACE_FLOW_STRING("col_delete_path_data","Entry.");
1528
1529 if (path != NULL) {
1530 TRACE_INFO_STRING("col_delete_path_data", "Item to delete exits.");
1531 if (path->previous_path != NULL) {
1532 TRACE_INFO_STRING("col_delete_path_data",
1533 "But previous item to delete exits to. Nesting.");
1534 col_delete_path_data(path->previous_path);
1535 }
1536 if (path->name != NULL) {
1537 TRACE_INFO_STRING("col_delete_path_data Deleting path:", path->name);
1538 free(path->name);
1539 }
1540 TRACE_INFO_STRING("col_delete_path_data", "Deleting path element");
1541 free(path);
1542 }
1543 TRACE_FLOW_STRING("col_delete_path_data", "Exit");
1544 }
1545
1546
1547 /* MAIN TRAVERSAL FUNCTION */
1548
1549 /* Internal function to walk collection */
1550 /* For each item walked it will call traverse handler.
1551 Traverse handler accepts: current item,
1552 user provided item handler and user provided custom data. */
1553 /* See below different traverse handlers for different cases */
1554 static int col_walk_items(struct collection_item *ci,
1555 int mode_flags,
1556 internal_item_fn traverse_handler,
1557 void *traverse_data,
1558 col_item_fn user_item_handler,
1559 void *custom_data,
1560 unsigned *depth)
1561 {
1562 struct collection_item *current;
1563 struct collection_item *parent = NULL;
1564 struct collection_item *sub;
1565 int stop = 0;
1566 int error = EOK;
1567
1568 TRACE_FLOW_STRING("col_walk_items", "Entry.");
1569 TRACE_INFO_NUMBER("Mode flags:", mode_flags);
1570
1571 /* Increase depth */
1572 /* NOTE: The depth is increased at the entry to the function.
1573 * and decreased right before the exit so it is safe to decrease it.
1574 */
1575 (*depth)++;
1576
1577 current = ci;
1578
1579 while (current) {
1580
1581 TRACE_INFO_STRING("Processing item:", current->property);
1582 TRACE_INFO_NUMBER("Item type:", current->type);
1583
1584 if (current->type == COL_TYPE_COLLECTIONREF) {
1585
1586 TRACE_INFO_STRING("Subcollection:", current->property);
1587
1588 if ((mode_flags & COL_TRAVERSE_IGNORE) == 0) {
1589
1590 TRACE_INFO_STRING("Subcollection is not ignored.", "");
1591 /* We are not ignoring sub collections */
1592
1593 if ((mode_flags & COL_TRAVERSE_FLAT) == 0) {
1594
1595 TRACE_INFO_STRING("Subcollection is not flattened.", "");
1596 /* We are not flattening sub collections.
1597 * The flattening means that we are not going
1598 * to return reference and headers for sub collections.
1599 * We will also not do special end collection
1600 * invocation for sub collections.
1601 */
1602 error = traverse_handler(ci, parent, current, traverse_data,
1603 user_item_handler, custom_data, &stop);
1604 if (stop != 0) {
1605 TRACE_INFO_STRING("Traverse handler returned STOP.", "");
1606 error = EINTR_INTERNAL;
1607 }
1608 /* Check what error we got */
1609 if (error == EINTR_INTERNAL) {
1610 TRACE_FLOW_NUMBER("Internal error - means we are stopping.", error);
1611 (*depth)--;
1612 return error;
1613 }
1614 else if (error) {
1615 TRACE_ERROR_NUMBER("Traverse handler returned error.", error);
1616 (*depth)--;
1617 return error;
1618 }
1619 }
1620
1621 if ((mode_flags & COL_TRAVERSE_ONELEVEL) == 0) {
1622 TRACE_INFO_STRING("Before diving into sub collection","");
1623 sub = *((struct collection_item **)(current->data));
1624 TRACE_INFO_STRING("Sub collection name", sub->property);
1625 TRACE_INFO_NUMBER("Header type", sub->type);
1626 /* We need to go into sub collections */
1627 error = col_walk_items(sub, mode_flags,
1628 traverse_handler, traverse_data,
1629 user_item_handler, custom_data,
1630 depth);
1631 TRACE_INFO_STRING("Returned from sub collection processing", "");
1632 TRACE_INFO_STRING("Done processing item:", current->property);
1633 TRACE_INFO_NUMBER("Done processing item type:", current->type);
1634
1635 }
1636 }
1637 }
1638 else {
1639 /* Check if it is a header and we are not on the root level.
1640 * If we are flattening collection we need to skip headers
1641 * for sub collections.
1642 */
1643
1644 /* Call handler if:
1645 * a) It is not collection header
1646 * OR
1647 * b) It is header we are flattening but we are on top level
1648 * OR
1649 * c) It is header and we are not flattening.
1650 */
1651 if ((current->type != COL_TYPE_COLLECTION) ||
1652 (((mode_flags & COL_TRAVERSE_FLAT) != 0) && (*depth == 1)) ||
1653 ((mode_flags & COL_TRAVERSE_FLAT) == 0)) {
1654 /* Call handler then move on */
1655 error = traverse_handler(ci, parent, current,
1656 traverse_data, user_item_handler,
1657 custom_data, &stop);
1658
1659 }
1660 }
1661 /* If we are stopped - return EINTR_INTERNAL */
1662 if (stop != 0) {
1663 TRACE_INFO_STRING("Traverse handler returned STOP.", "");
1664 error = EINTR_INTERNAL;
1665 }
1666 /* Check what error we got */
1667 if (error == EINTR_INTERNAL) {
1668 TRACE_FLOW_NUMBER("Internal error - means we are stopping.", error);
1669 (*depth)--;
1670 return error;
1671 }
1672 else if (error) {
1673 TRACE_ERROR_NUMBER("Traverse handler returned error.", error);
1674 (*depth)--;
1675 return error;
1676 }
1677
1678 TRACE_INFO_NUMBER("Next element", current->next);
1679
1680 parent = current;
1681 current = current->next;
1682 }
1683
1684 TRACE_INFO_STRING("Out of loop", "");
1685
1686 /* Check if we need to have a special
1687 * call at the end of the collection.
1688 */
1689 if ((mode_flags & COL_TRAVERSE_END) != 0) {
1690
1691 /* Do this dummy invocation only:
1692 * a) If we are flattening and on the root level
1693 * b) We are not flattening
1694 */
1695 if ((((mode_flags & COL_TRAVERSE_FLAT) != 0) && (*depth == 1)) ||
1696 ((mode_flags & COL_TRAVERSE_FLAT) == 0)) {
1697
1698 TRACE_INFO_STRING("About to do the special end collection invocation of handler", "");
1699 error = traverse_handler(ci, parent, current,
1700 traverse_data, user_item_handler,
1701 custom_data, &stop);
1702 }
1703 }
1704
1705 TRACE_FLOW_NUMBER("col_walk_items. Returns: ", error);
1706 (*depth)--;
1707 return error;
1708 }
1709
1710
1711 /* ACTION */
1712
1713 /* Find an item by property name and perform an action on it. */
1714 /* No pattern matching supported in the first implementation. */
1715 /* To refer to child properties use notatation like this: */
1716 /* parent!child!subchild!subsubchild etc. */
1717 static int col_find_item_and_do(struct collection_item *ci,
1718 const char *property_to_find,
1719 int type,
1720 int mode_flags,
1721 col_item_fn item_handler,
1722 void *custom_data,
1723 int action)
1724 {
1725
1726 int error = EOK;
1727 struct find_name *traverse_data = NULL;
1728 unsigned depth = 0;
1729 int count = 0;
1730 const char *last_part;
1731 char *sep;
1732
1733 TRACE_FLOW_STRING("col_find_item_and_do", "Entry.");
1734
1735 /* Item handler is always required */
1736 if ((item_handler == NULL) &&
1737 (action == COLLECTION_ACTION_FIND)) {
1738 TRACE_ERROR_NUMBER("No item handler - returning error!", EINVAL);
1739 return EINVAL;
1740 }
1741
1742 /* Collection is requered */
1743 if (ci == NULL) {
1744 TRACE_ERROR_NUMBER("No collection to search!", EINVAL);
1745 return EINVAL;
1746 }
1747
1748 /* Make sure that there is anything to search */
1749 type &= COL_TYPE_ANY;
1750 if ((type == 0) &&
1751 ((property_to_find == NULL) ||
1752 ((property_to_find != NULL) && (*property_to_find == '\0')))) {
1753 TRACE_ERROR_NUMBER("No item search criteria specified - returning error!", ENOENT);
1754 return ENOENT;
1755 }
1756
1757 /* Prepare data for traversal */
1758 traverse_data = (struct find_name *)malloc(sizeof(struct find_name));
1759 if (traverse_data == NULL) {
1760 TRACE_ERROR_NUMBER("Failed to allocate traverse data memory - returning error!", ENOMEM);
1761 return ENOMEM;
1762 }
1763
1764 TRACE_INFO_STRING("col_find_item_and_do", "Filling in traverse data.");
1765
1766 traverse_data->name_to_find = property_to_find;
1767
1768 if (property_to_find != NULL) {
1769
1770 traverse_data->name_len_to_find = strlen(property_to_find);
1771
1772 /* Check if the search string ends with "!" - this is illegal */
1773 if (traverse_data->name_to_find[traverse_data->name_len_to_find - 1] == '!') {
1774 TRACE_ERROR_NUMBER("Search string is invalid.", EINVAL);
1775 free(traverse_data);
1776 return EINVAL;
1777 }
1778
1779 /* Find last ! if any */
1780 sep = strrchr(traverse_data->name_to_find, '!');
1781 if (sep != NULL) {
1782 sep++;
1783 last_part = sep;
1784 }
1785 else last_part = traverse_data->name_to_find;
1786
1787 TRACE_INFO_STRING("Last item", last_part);
1788
1789 /* Create hash of the last part */
1790 traverse_data->hash = FNV1a_base;
1791
1792 /* Create hash of the string to search */
1793 while(last_part[count] != 0) {
1794 traverse_data->hash = traverse_data->hash ^ toupper(last_part[count]);
1795 traverse_data->hash *= FNV1a_prime;
1796 count++;
1797 }
1798 }
1799 else {
1800 /* We a looking for a first element of a given type */
1801 TRACE_INFO_STRING("No search string", "");
1802 traverse_data->name_len_to_find = 0;
1803 }
1804
1805
1806 traverse_data->type_to_match = type;
1807 traverse_data->given_name = NULL;
1808 traverse_data->given_len = 0;
1809 traverse_data->current_path = NULL;
1810 traverse_data->action = action;
1811
1812 mode_flags |= COL_TRAVERSE_END;
1813
1814 TRACE_INFO_STRING("col_find_item_and_do", "About to walk the tree.");
1815 TRACE_INFO_NUMBER("Traverse flags", mode_flags);
1816
1817 error = col_walk_items(ci, mode_flags, col_act_traverse_handler,
1818 (void *)traverse_data, item_handler, custom_data,
1819 &depth);
1820
1821 if (traverse_data->current_path != NULL) {
1822 TRACE_INFO_STRING("find_item_and_do",
1823 "Path was not cleared - deleting");
1824 col_delete_path_data(traverse_data->current_path);
1825 }
1826
1827 free(traverse_data);
1828
1829 if (error && (error != EINTR_INTERNAL)) {
1830 TRACE_ERROR_NUMBER("Walk items returned error. Returning: ", error);
1831 return error;
1832 }
1833 else {
1834 TRACE_FLOW_STRING("Walk items returned SUCCESS.", "");
1835 return EOK;
1836 }
1837 }
1838
1839 /* Function to replace data in the item */
1840 static int col_update_current_item(struct collection_item *current,
1841 struct update_property *update_data)
1842 {
1843 TRACE_FLOW_STRING("col_update_current_item", "Entry");
1844
1845 /* If type is different or same but it is string or binary we need to
1846 * replace the storage */
1847 if ((current->type != update_data->type) ||
1848 ((current->type == update_data->type) &&
1849 ((current->type == COL_TYPE_STRING) ||
1850 (current->type == COL_TYPE_BINARY)))) {
1851 TRACE_INFO_STRING("Replacing item data buffer", "");
1852 free(current->data);
1853 current->data = malloc(update_data->length);
1854 if (current->data == NULL) {
1855 TRACE_ERROR_STRING("Failed to allocate memory", "");
1856 current->length = 0;
1857 return ENOMEM;
1858 }
1859 current->length = update_data->length;
1860 }
1861
1862 TRACE_INFO_STRING("Overwriting item data", "");
1863 memcpy(current->data, update_data->data, current->length);
1864 current->type = update_data->type;
1865
1866 if (current->type == COL_TYPE_STRING)
1867 ((char *)(current->data))[current->length-1] = '\0';
1868
1869 TRACE_FLOW_STRING("update_current_item", "Exit");
1870 return EOK;
1871 }
1872
1873 /* TRAVERSE CALLBACKS */
1874
1875 /* Traverse handler for simple traverse function */
1876 /* Handler must be able to deal with NULL current item */
1877 static int col_simple_traverse_handler(struct collection_item *head,
1878 struct collection_item *previous,
1879 struct collection_item *current,
1880 void *traverse_data,
1881 col_item_fn user_item_handler,
1882 void *custom_data,
1883 int *stop)
1884 {
1885 int error = EOK;
1886 struct collection_item end_item;
1887 char zero = '\0';
1888
1889 TRACE_FLOW_STRING("col_simple_traverse_handler", "Entry.");
1890
1891 if (current == NULL) {
1892 memset((void *)&end_item, 0, sizeof(struct collection_item));
1893 end_item.type = COL_TYPE_END;
1894 end_item.property = &zero;
1895 current = &end_item;
1896 }
1897
1898 error = user_item_handler(current->property,
1899 current->property_len,
1900 current->type,
1901 current->data,
1902 current->length,
1903 custom_data,
1904 stop);
1905
1906 TRACE_FLOW_NUMBER("col_simple_traverse_handler. Returning:", error);
1907 return error;
1908 }
1909
1910 /* Traverse handler for to find parent */
1911 static int col_parent_traverse_handler(struct collection_item *head,
1912 struct collection_item *previous,
1913 struct collection_item *current,
1914 void *traverse_data,
1915 col_item_fn user_item_handler,
1916 void *custom_data,
1917 int *stop)
1918 {
1919 struct property_search *to_find;
1920 int done = 0;
1921 int match = 0;
1922
1923 TRACE_FLOW_STRING("col_parent_traverse_handler", "Entry.");
1924
1925 to_find = (struct property_search *)custom_data;
1926
1927 TRACE_INFO_NUMBER("Looking for HASH:", (unsigned)(to_find->hash));
1928 TRACE_INFO_NUMBER("Current HASH:", (unsigned)(current->phash));
1929
1930 /* Check hashes first */
1931 if(to_find->hash == current->phash) {
1932
1933 /* Check type if we are asked to use type */
1934 if ((to_find->use_type) && (!(to_find->type & current->type))) {
1935 TRACE_FLOW_STRING("parent_traverse_handler. Returning:","Exit. Hash is Ok, type is not");
1936 return EOK;
1937 }
1938
1939 /* Validate property. Make sure we include terminating 0 in the comparison */
1940 if (strncasecmp(current->property, to_find->property, current->property_len + 1) == 0) {
1941
1942 match = 1;
1943 to_find->found = 1;
1944
1945 /* Do the right thing based on index */
1946 /* If index is 0 we are looking for the first value in the list of duplicate properties */
1947 if (to_find->index == 0) done = 1;
1948 /* If index is non zero we are looking for N-th instance of the dup property */
1949 else if (to_find->index > 0) {
1950 if (to_find->count == to_find->index) done = 1;
1951 else {
1952 /* Record found instance and move on */
1953 to_find->parent = previous;
1954 (to_find->count)++;
1955 }
1956 }
1957 /* If we are looking for last instance just record it */
1958 else to_find->parent = previous;
1959 }
1960 }
1961
1962 if (done) {
1963 *stop = 1;
1964 *((struct collection_item **)traverse_data) = previous;
1965 }
1966 else {
1967 if (to_find->interrupt) {
1968 /* As soon as we found first non matching one but there was a match
1969 * return the parent of the last found item.
1970 */
1971 if (((!match) || (current->next == NULL)) &&
1972 (to_find->index != 0) && (to_find->found)) {
1973 *stop = 1;
1974 if (to_find->index == -2)
1975 *((struct collection_item **)traverse_data) =
1976 to_find->parent;
1977 else
1978 if (to_find->exact) {
1979 /* This means that we need to match the exact
1980 * index but we did not */
1981 to_find->parent = NULL;
1982 to_find->found = 0;
1983 }
1984 else
1985 *((struct collection_item **)traverse_data) =
1986 to_find->parent->next;
1987 }
1988 }
1989 else if ((current->next == NULL) && (to_find->found)) {
1990 *stop = 1;
1991 if (to_find->index == -2)
1992 *((struct collection_item **)traverse_data) =
1993 to_find->parent;
1994 else
1995 if (to_find->exact) {
1996 /* This means that we need to match the exact
1997 * index but we did not */
1998 to_find->parent = NULL;
1999 to_find->found = 0;
2000 }
2001 else
2002 *((struct collection_item **)traverse_data) =
2003 to_find->parent->next;
2004 }
2005 }
2006
2007
2008 TRACE_FLOW_STRING("col_parent_traverse_handler. Returning:","Exit");
2009 return EOK;
2010 }
2011
2012
2013 /* Traverse callback for find & delete function */
2014 static int col_act_traverse_handler(struct collection_item *head,
2015 struct collection_item *previous,
2016 struct collection_item *current,
2017 void *passed_traverse_data,
2018 col_item_fn user_item_handler,
2019 void *custom_data,
2020 int *stop)
2021 {
2022 int error = EOK;
2023 struct find_name *traverse_data = NULL;
2024 char *name;
2025 int length;
2026 struct path_data *temp;
2027 struct collection_header *header;
2028 char *property;
2029 int property_len;
2030 struct update_property *update_data;
2031
2032 TRACE_FLOW_STRING("col_act_traverse_handler", "Entry.");
2033
2034 traverse_data = (struct find_name *)passed_traverse_data;
2035
2036 /* We can be called when current points to NULL */
2037 if (current == NULL) {
2038 TRACE_INFO_STRING("col_act_traverse_handler",
2039 "Special call at the end of the collection.");
2040 temp = traverse_data->current_path;
2041 traverse_data->current_path = temp->previous_path;
2042 temp->previous_path = NULL;
2043 col_delete_path_data(temp);
2044 traverse_data->given_name = NULL;
2045 traverse_data->given_len = 0;
2046 TRACE_FLOW_NUMBER("Handling end of collection - removed path. Returning:", error);
2047 return error;
2048 }
2049
2050 /* Create new path at the beginning of a new sub collection */
2051 if (current->type == COL_TYPE_COLLECTION) {
2052
2053 TRACE_INFO_STRING("col_act_traverse_handler",
2054 "Processing collection handle.");
2055
2056 /* Create new path */
2057 if (traverse_data->current_path != NULL) {
2058 TRACE_INFO_STRING("Already have part of the path", "");
2059 name = traverse_data->current_path->name;
2060 length = traverse_data->current_path->length;
2061 TRACE_INFO_STRING("Path:", name);
2062 TRACE_INFO_NUMBER("Path len:", length);
2063 }
2064 else {
2065 name = NULL;
2066 length = 0;
2067 }
2068
2069 if (traverse_data->given_name != NULL) {
2070 property = traverse_data->given_name;
2071 property_len = traverse_data->given_len;
2072 }
2073 else {
2074 property = current->property;
2075 property_len = current->property_len;
2076 }
2077
2078 TRACE_INFO_STRING("col_act_traverse_handler", "About to create path data.");
2079
2080 error = col_create_path_data(&(traverse_data->current_path),
2081 name, length,
2082 property, property_len, '!');
2083
2084 TRACE_INFO_NUMBER("col_create_path_data returned:", error);
2085 return error;
2086 }
2087
2088 /* Handle the collection pointers */
2089 if (current->type == COL_TYPE_COLLECTIONREF) {
2090 traverse_data->given_name = current->property;
2091 traverse_data->given_len = current->property_len;
2092 TRACE_INFO_STRING("Saved given name:", traverse_data->given_name);
2093 }
2094
2095 TRACE_INFO_STRING("Processing item with property:", current->property);
2096
2097 /* Do here what we do with items */
2098 if (col_match_item(current, traverse_data)) {
2099 TRACE_INFO_STRING("Matched item:", current->property);
2100 switch (traverse_data->action) {
2101 case COLLECTION_ACTION_FIND:
2102 TRACE_INFO_STRING("It is a find action - calling handler.", "");
2103 if (user_item_handler != NULL) {
2104 /* Call user handler */
2105 error = user_item_handler(current->property,
2106 current->property_len,
2107 current->type,
2108 current->data,
2109 current->length,
2110 custom_data,
2111 stop);
2112
2113 TRACE_INFO_NUMBER("Handler returned:", error);
2114 TRACE_INFO_NUMBER("Handler set STOP to:", *stop);
2115
2116 }
2117 break;
2118
2119 case COLLECTION_ACTION_GET:
2120 TRACE_INFO_STRING("It is a get action.", "");
2121 if (custom_data != NULL)
2122 *((struct collection_item **)(custom_data)) = current;
2123 break;
2124
2125 case COLLECTION_ACTION_DEL:
2126 TRACE_INFO_STRING("It is a delete action.", "");
2127 /* Make sure we tell the caller we found a match */
2128 if (custom_data != NULL)
2129 *(int *)custom_data = COL_MATCH;
2130
2131 /* Adjust header of the collection */
2132 header = (struct collection_header *)head->data;
2133 header->count--;
2134 if (current->next == NULL)
2135 header->last = previous;
2136
2137 /* Unlink and delete iteam */
2138 /* Previous can't be NULL here becuase we never delete
2139 * header elements */
2140 previous->next = current->next;
2141 col_delete_item(current);
2142 TRACE_INFO_STRING("Did the delete of the item.", "");
2143 break;
2144
2145 case COLLECTION_ACTION_UPDATE:
2146 TRACE_INFO_STRING("It is an update action.", "");
2147 if((current->type == COL_TYPE_COLLECTION) ||
2148 (current->type == COL_TYPE_COLLECTIONREF)) {
2149 TRACE_ERROR_STRING("Can't update collections it is an error for now", "");
2150 return EINVAL;
2151 }
2152
2153 /* Make sure we tell the caller we found a match */
2154 if (custom_data != NULL) {
2155 update_data = (struct update_property *)custom_data;
2156 update_data->found = COL_MATCH;
2157 error = col_update_current_item(current, update_data);
2158 }
2159 else {
2160 TRACE_ERROR_STRING("Error - update data is required", "");
2161 return EINVAL;
2162 }
2163
2164 TRACE_INFO_STRING("Did the delete of the item.", "");
2165 break;
2166 default:
2167 break;
2168 }
2169 /* Force interrupt if we found */
2170 *stop = 1;
2171 }
2172
2173 TRACE_FLOW_NUMBER("col_act_traverse_handler returning", error);
2174 return error;
2175 }
2176
2177
2178 /* Traverse handler for copy function */
2179 static int col_copy_traverse_handler(struct collection_item *head,
2180 struct collection_item *previous,
2181 struct collection_item *current,
2182 void *passed_traverse_data,
2183 col_item_fn user_item_handler,
2184 void *custom_data,
2185 int *stop)
2186 {
2187 int error = EOK;
2188 struct collection_item *parent;
2189 struct collection_item *other = NULL;
2190 struct col_copy *traverse_data;
2191 struct path_data *temp;
2192 char *name;
2193 int length;
2194 char *property = NULL;
2195 int property_len;
2196 struct collection_header *header;
2197 char *offset;
2198
2199 TRACE_FLOW_STRING("col_copy_traverse_handler", "Entry.");
2200
2201 parent = (struct collection_item *)custom_data;
2202 traverse_data = (struct col_copy *)passed_traverse_data;
2203
2204 /* We can be called when current points to NULL */
2205 /* This will happen only in the FLATDOT case */
2206 if (current == NULL) {
2207 TRACE_INFO_STRING("col_copy_traverse_handler",
2208 "Special call at the end of the collection.");
2209 temp = traverse_data->current_path;
2210 traverse_data->current_path = temp->previous_path;
2211 temp->previous_path = NULL;
2212 col_delete_path_data(temp);
2213 traverse_data->given_name = NULL;
2214 traverse_data->given_len = 0;
2215 TRACE_FLOW_NUMBER("Handling end of collection - removed path. Returning:", error);
2216 return error;
2217 }
2218
2219 /* Create new path at the beginning of a new sub collection */
2220 if (current->type == COL_TYPE_COLLECTION) {
2221
2222 TRACE_INFO_STRING("col_copy_traverse_handler",
2223 "Processing collection handle.");
2224 if (traverse_data->mode == COL_COPY_FLATDOT) {
2225 /* Create new path */
2226 if (traverse_data->current_path != NULL) {
2227 TRACE_INFO_STRING("Already have part of the path", "");
2228 name = traverse_data->current_path->name;
2229 length = traverse_data->current_path->length;
2230 TRACE_INFO_STRING("Path:", name);
2231 TRACE_INFO_NUMBER("Path len:", length);
2232 if (traverse_data->given_name != NULL) {
2233 property = traverse_data->given_name;
2234 property_len = traverse_data->given_len;
2235 }
2236 else {
2237 property = current->property;
2238 property_len = current->property_len;
2239 }
2240 }
2241 else {
2242 /* Do not create prefix for top collection
2243 * if there is no given name.
2244 */
2245 name = NULL;
2246 length = 0;
2247 if (traverse_data->given_name != NULL) {
2248 property = traverse_data->given_name;
2249 property_len = traverse_data->given_len;
2250 }
2251 else {
2252 property = NULL;
2253 property_len = 0;
2254 }
2255 }
2256
2257 TRACE_INFO_STRING("col_copy_traverse_handler", "About to create path data.");
2258
2259 error = col_create_path_data(&(traverse_data->current_path),
2260 name, length,
2261 property, property_len, '.');
2262
2263 TRACE_FLOW_NUMBER("col_copy_traverse_handler processed header:", error);
2264 return error;
2265 }
2266 else {
2267 TRACE_FLOW_NUMBER("col_copy_traverse_handler skipping the header:", error);
2268 return error;
2269 }
2270 }
2271
2272
2273 /* Check if this is a special case of sub collection */
2274 if (current->type == COL_TYPE_COLLECTIONREF) {
2275
2276 TRACE_INFO_STRING("Found a subcollection we need to copy. Name:",
2277 current->property);
2278
2279 /* Based on the mode we need to do different things */
2280 switch (traverse_data->mode) {
2281 case COL_COPY_NORMAL:
2282
2283 error = col_copy_collection_with_cb(&other,
2284 *((struct collection_item **)(current->data)),
2285 current->property,
2286 COL_COPY_NORMAL,
2287 traverse_data->copy_cb,
2288 traverse_data->ext_data);
2289 if (error) {
2290 TRACE_ERROR_NUMBER("Copy subcollection returned error:", error);
2291 return error;
2292 }
2293
2294 /* Add new item to a collection
2295 * all references are now sub collections */
2296 error = col_insert_property_with_ref_int(parent,
2297 NULL,
2298 COL_DSP_END,
2299 NULL,
2300 0,
2301 0,
2302 current->property,
2303 COL_TYPE_COLLECTIONREF,
2304 (void *)(&other),
2305 sizeof(struct collection_item **),
2306 NULL);
2307
2308 TRACE_FLOW_NUMBER("col_copy_traverse_handler returning in NORMAL mode:", error);
2309 return error;
2310
2311 case COL_COPY_KEEPREF:
2312
2313 /* Just increase reference count of the referenced collection */
2314 other = *((struct collection_item **)(current->data));
2315 header = (struct collection_header *)(other->data);
2316 header->reference_count++;
2317
2318 /* Add new item to a collection
2319 * all references are now sub collections */
2320 error = col_insert_property_with_ref_int(parent,
2321 NULL,
2322 COL_DSP_END,
2323 NULL,
2324 0,
2325 0,
2326 current->property,
2327 COL_TYPE_COLLECTIONREF,
2328 (void *)(&other),
2329 sizeof(struct collection_item **),
2330 NULL);
2331 TRACE_FLOW_NUMBER("col_copy_traverse_handler returning in KEEPREF mode:", error);
2332 return error;
2333
2334 case COL_COPY_TOP:
2335 /* Told to ignore sub collections */
2336 TRACE_FLOW_NUMBER("col_copy_traverse_handler returning in TOP mode:", error);
2337 return error;
2338
2339 case COL_COPY_FLATDOT:
2340
2341 traverse_data->given_name = current->property;
2342 traverse_data->given_len = current->property_len;
2343 TRACE_INFO_STRING("Saved given name:", traverse_data->given_name);
2344 TRACE_FLOW_NUMBER("col_copy_traverse_handler returning in FLATDOT mode:", error);
2345 return error;
2346
2347 /* NOTE: The mode COL_COPY_FLAT is not in the list of cases becuase
2348 * in this flat mode we traverse collection using COL_TRAVERSE_FLAT flag
2349 * thus we should not be called on referenced collections at all
2350 * by the col_walk_items() function.
2351 */
2352
2353 default:
2354 TRACE_ERROR_NUMBER("col_copy_traverse_handler bad mode error:", EINVAL);
2355 return EINVAL;
2356 }
2357 }
2358 else {
2359
2360 if (traverse_data->mode == COL_COPY_FLATDOT) {
2361 /* Since this code can't use asprintf have to do it hard way */
2362 property = malloc(traverse_data->current_path->length +
2363 current->property_len + 2);
2364 if (property == NULL) {
2365 TRACE_ERROR_NUMBER("Failed to allocate memory for a new name:", error);
2366 return error;
2367 }
2368 /* Add first part and dot only if we have prefix */
2369 offset = property;
2370 if (traverse_data->current_path->length) {
2371 memcpy(offset, traverse_data->current_path->name,
2372 traverse_data->current_path->length);
2373 offset[traverse_data->current_path->length] = '.';
2374 offset += traverse_data->current_path->length + 1;
2375 }
2376 memcpy(offset, current->property, current->property_len);
2377 offset[current->property_len] = '\0';
2378 }
2379 else property = current->property;
2380
2381 TRACE_INFO_STRING("Using property:", property);
2382
2383 error = col_copy_item_with_cb(parent,
2384 property,
2385 current->type,
2386 current->data,
2387 current->length,
2388 traverse_data->copy_cb,
2389 traverse_data->ext_data);
2390
2391 /* Free property if we allocated it */
2392 if (traverse_data->mode == COL_COPY_FLATDOT) free(property);
2393
2394 if (error) {
2395 TRACE_ERROR_NUMBER("Failed to copy property:", error);
2396 return error;
2397 }
2398 }
2399
2400 TRACE_FLOW_NUMBER("col_copy_traverse_handler returning", error);
2401 return error;
2402 }
2403
2404
2405
2406
2407 /********************* MAIN INTERFACE FUNCTIONS *****************************/
2408
2409
2410 /* CREATE */
2411
2412 /* Function that creates an named collection of a given class*/
2413 int col_create_collection(struct collection_item **ci, const char *name,
2414 unsigned cclass)
2415 {
2416 struct collection_item *handle = NULL;
2417 struct collection_header header;
2418 int error = EOK;
2419
2420 TRACE_FLOW_STRING("col_create_collection", "Entry.");
2421
2422 /* Prepare header */
2423 header.last = NULL;
2424 header.reference_count = 1;
2425 header.count = 0;
2426 header.cclass = cclass;
2427
2428 /* Create a collection type property */
2429 error = col_insert_property_with_ref_int(NULL,
2430 NULL,
2431 COL_DSP_END,
2432 NULL,
2433 0,
2434 0,
2435 name,
2436 COL_TYPE_COLLECTION,
2437 &header,
2438 sizeof(header),
2439 &handle);
2440
2441
2442 if (error) return error;
2443
2444 *ci = handle;
2445
2446 TRACE_FLOW_STRING("col_create_collection", "Success Exit.");
2447 return EOK;
2448 }
2449
2450
2451 /* DESTROY */
2452
2453 /* Function that destroys a collection */
2454 void col_destroy_collection_with_cb(struct collection_item *ci,
2455 col_item_cleanup_fn cb,
2456 void *custom_data)
2457 {
2458 struct collection_header *header;
2459
2460 TRACE_FLOW_STRING("col_destroy_collection_with_cb", "Entry.");
2461
2462 /* Do not try to delete NULL */
2463 if (ci == NULL) return;
2464
2465 /* You can delete only whole collection not a part of it */
2466 if (ci->type != COL_TYPE_COLLECTION) {
2467 TRACE_ERROR_STRING("Attempt to delete a non collection - BAD!", "");
2468 TRACE_ERROR_NUMBER("Actual type is:", ci->type);
2469 return;
2470 }
2471
2472 TRACE_INFO_STRING("Name:", ci->property);
2473
2474 /* Collection can be referenced by other collection */
2475 header = (struct collection_header *)(ci->data);
2476 TRACE_INFO_NUMBER("Reference count:", header->reference_count);
2477 if (header->reference_count > 1) {
2478 TRACE_INFO_STRING("Dereferencing a referenced collection.", "");
2479 header->reference_count--;
2480 TRACE_INFO_NUMBER("Number after dereferencing.",
2481 header->reference_count);
2482 }
2483 else {
2484 col_delete_collection(ci, cb, custom_data);
2485 }
2486
2487 TRACE_FLOW_STRING("col_destroy_collection_with_cb", "Exit.");
2488 }
2489
2490
2491 /* Function that destroys a collection */
2492 void col_destroy_collection(struct collection_item *ci)
2493 {
2494 TRACE_FLOW_STRING("col_destroy_collection", "Entry.");
2495
2496 col_destroy_collection_with_cb(ci, NULL, NULL);
2497
2498 TRACE_FLOW_STRING("col_destroy_collection", "Exit.");
2499 }
2500
2501 /* COPY */
2502
2503 /* Wrapper around a more advanced function */
2504 int col_copy_collection(struct collection_item **collection_copy,
2505 struct collection_item *collection_to_copy,
2506 const char *name_to_use,
2507 int copy_mode)
2508 {
2509 int error = EOK;
2510 TRACE_FLOW_STRING("col_copy_collection", "Entry.");
2511
2512 error = col_copy_collection_with_cb(collection_copy,
2513 collection_to_copy,
2514 name_to_use,
2515 copy_mode,
2516 NULL,
2517 NULL);
2518
2519 TRACE_FLOW_NUMBER("col_copy_collection. Exit. Returning", error);
2520 return error;
2521 }
2522
2523 /* Create a deep copy of the current collection. */
2524 /* Referenced collections of the donor are copied as sub collections. */
2525 int col_copy_collection_with_cb(struct collection_item **collection_copy,
2526 struct collection_item *collection_to_copy,
2527 const char *name_to_use,
2528 int copy_mode,
2529 col_copy_cb copy_cb,
2530 void *ext_data)
2531 {
2532 int error = EOK;
2533 struct collection_item *new_collection = NULL;
2534 const char *name;
2535 struct collection_header *header;
2536 unsigned depth = 0;
2537 struct col_copy traverse_data;
2538 int flags;
2539
2540 TRACE_FLOW_STRING("col_copy_collection_with_cb", "Entry.");
2541
2542 /* Collection is required */
2543 if (collection_to_copy == NULL) {
2544 TRACE_ERROR_NUMBER("No collection to search!", EINVAL);
2545 return EINVAL;
2546 }
2547
2548 /* Storage is required too */
2549 if (collection_copy == NULL) {
2550 TRACE_ERROR_NUMBER("No memory provided to receive collection copy!", EINVAL);
2551 return EINVAL;
2552 }
2553
2554 /* NOTE: Refine this check if adding a new copy mode */
2555 if ((copy_mode < 0) || (copy_mode > COL_COPY_TOP)) {
2556 TRACE_ERROR_NUMBER("Invalid copy mode:", copy_mode);
2557 return EINVAL;
2558 }
2559
2560 /* Determine what name to use */
2561 if (name_to_use != NULL)
2562 name = name_to_use;
2563 else
2564 name = collection_to_copy->property;
2565
2566 header = (struct collection_header *)collection_to_copy->data;
2567
2568 /* Create a new collection */
2569 error = col_create_collection(&new_collection, name, header->cclass);
2570 if (error) {
2571 TRACE_ERROR_NUMBER("col_create_collection failed returning", error);
2572 return error;
2573 }
2574
2575 traverse_data.mode = copy_mode;
2576 traverse_data.current_path = NULL;
2577 traverse_data.given_name = NULL;
2578 traverse_data.given_len = 0;
2579 traverse_data.copy_cb = copy_cb;
2580 traverse_data.ext_data = ext_data;
2581
2582 if (copy_mode == COL_COPY_FLATDOT) flags = COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END;
2583 else if (copy_mode == COL_COPY_FLAT) flags = COL_TRAVERSE_FLAT;
2584 else flags = COL_TRAVERSE_ONELEVEL;
2585
2586 error = col_walk_items(collection_to_copy, flags,
2587 col_copy_traverse_handler, (void *)(&traverse_data),
2588 NULL, new_collection, &depth);
2589
2590 if (!error) *collection_copy = new_collection;
2591 else col_destroy_collection(new_collection);
2592
2593 TRACE_FLOW_NUMBER("col_copy_collection_with_cb returning", error);
2594 return error;
2595
2596 }
2597
2598
2599 /* EXTRACTION */
2600
2601 /* Extract collection */
2602 int col_get_collection_reference(struct collection_item *ci,
2603 struct collection_item **acceptor,
2604 const char *collection_to_find)
2605 {
2606 struct collection_header *header;
2607 struct collection_item *subcollection = NULL;
2608 int error = EOK;
2609
2610 TRACE_FLOW_STRING("col_get_collection_reference", "Entry.");
2611
2612 if ((ci == NULL) ||
2613 (ci->type != COL_TYPE_COLLECTION) ||
2614 (acceptor == NULL)) {
2615 TRACE_ERROR_NUMBER("Invalid parameter - returning error",EINVAL);
2616 return EINVAL;
2617 }
2618
2619 if (collection_to_find) {
2620 /* Find a sub collection */
2621 TRACE_INFO_STRING("We are given subcollection name - search it:",
2622 collection_to_find);
2623 error = col_find_item_and_do(ci, collection_to_find,
2624 COL_TYPE_COLLECTIONREF,
2625 COL_TRAVERSE_DEFAULT,
2626 col_get_subcollection,
2627 (void *)(&subcollection),
2628 COLLECTION_ACTION_FIND);
2629 if (error) {
2630 TRACE_ERROR_NUMBER("Search failed returning error", error);
2631 return error;
2632 }
2633
2634 if (subcollection == NULL) {
2635 TRACE_ERROR_STRING("Search for subcollection returned NULL pointer", "");
2636 return ENOENT;
2637 }
2638 }
2639 else {
2640 /* Create reference to the same collection */
2641 TRACE_INFO_STRING("Creating reference to the top level collection.", "");
2642 subcollection = ci;
2643 }
2644
2645 header = (struct collection_header *)subcollection->data;
2646 TRACE_INFO_NUMBER("Count:", header->count);
2647 TRACE_INFO_NUMBER("Ref count:", header->reference_count);
2648 header->reference_count++;
2649 TRACE_INFO_NUMBER("Ref count after increment:", header->reference_count);
2650 *acceptor = subcollection;
2651
2652 TRACE_FLOW_STRING("col_get_collection_reference", "Success Exit.");
2653 return EOK;
2654 }
2655
2656 /* Get collection - if current item is a reference get a real collection from it. */
2657 int col_get_reference_from_item(struct collection_item *ci,
2658 struct collection_item **acceptor)
2659 {
2660 struct collection_header *header;
2661 struct collection_item *subcollection = NULL;
2662
2663 TRACE_FLOW_STRING("get_reference_from_item", "Entry.");
2664
2665 if ((ci == NULL) ||
2666 (ci->type != COL_TYPE_COLLECTIONREF) ||
2667 (acceptor == NULL)) {
2668 TRACE_ERROR_NUMBER("Invalid parameter - returning error",EINVAL);
2669 return EINVAL;
2670 }
2671
2672 subcollection = *((struct collection_item **)ci->data);
2673
2674 header = (struct collection_header *)subcollection->data;
2675 TRACE_INFO_NUMBER("Count:", header->count);
2676 TRACE_INFO_NUMBER("Ref count:", header->reference_count);
2677 header->reference_count++;
2678 TRACE_INFO_NUMBER("Ref count after increment:", header->reference_count);
2679 *acceptor = subcollection;
2680
2681 TRACE_FLOW_STRING("col_get_reference_from_item", "Success Exit.");
2682 return EOK;
2683 }
2684
2685 /* ADDITION */
2686
2687 /* Add collection to collection */
2688 int col_add_collection_to_collection(struct collection_item *ci,
2689 const char *sub_collection_name,
2690 const char *as_property,
2691 struct collection_item *collection_to_add,
2692 int mode)
2693 {
2694 struct collection_item *acceptor = NULL;
2695 const char *name_to_use;
2696 struct collection_header *header;
2697 struct collection_item *collection_copy;
2698 int error = EOK;
2699 struct col_copy traverse_data;
2700 unsigned depth = 0;
2701
2702
2703 TRACE_FLOW_STRING("col_add_collection_to_collection", "Entry.");
2704
2705 if ((ci == NULL) ||
2706 (ci->type != COL_TYPE_COLLECTION) ||
2707 (collection_to_add == NULL) ||
2708 (collection_to_add->type != COL_TYPE_COLLECTION)) {
2709 /* Need to debug here */
2710 TRACE_ERROR_NUMBER("Missing parameter - returning error", EINVAL);
2711 return EINVAL;
2712 }
2713
2714 if (sub_collection_name != NULL) {
2715 /* Find a sub collection */
2716 TRACE_INFO_STRING("We are given subcollection name - search it:",
2717 sub_collection_name);
2718 error = col_find_item_and_do(ci, sub_collection_name,
2719 COL_TYPE_COLLECTIONREF,
2720 COL_TRAVERSE_DEFAULT,
2721 col_get_subcollection,
2722 (void *)(&acceptor),
2723 COLLECTION_ACTION_FIND);
2724 if (error) {
2725 TRACE_ERROR_NUMBER("Search failed returning error", error);
2726 return error;
2727 }
2728
2729 if (acceptor == NULL) {
2730 TRACE_ERROR_STRING("Search for subcollection returned NULL pointer", "");
2731 return ENOENT;
2732 }
2733
2734 }
2735 else {
2736 acceptor = ci;
2737 }
2738
2739 if (as_property != NULL)
2740 name_to_use = as_property;
2741 else
2742 name_to_use = collection_to_add->property;
2743
2744
2745 TRACE_INFO_STRING("Going to use name:", name_to_use);
2746
2747
2748 switch (mode) {
2749 case COL_ADD_MODE_REFERENCE:
2750 TRACE_INFO_STRING("We are adding a reference.", "");
2751 TRACE_INFO_NUMBER("Type of the header element:",
2752 collection_to_add->type);
2753 TRACE_INFO_STRING("Header name we are adding.",
2754 collection_to_add->property);
2755 /* Create a pointer to external collection */
2756 /* For future thread safety: Transaction start -> */
2757 error = col_insert_property_with_ref_int(acceptor,
2758 NULL,
2759 COL_DSP_END,
2760 NULL,
2761 0,
2762 0,
2763 name_to_use,
2764 COL_TYPE_COLLECTIONREF,
2765 (void *)(&collection_to_add),
2766 sizeof(struct collection_item **),
2767 NULL);
2768
2769 TRACE_INFO_NUMBER("Type of the header element after adding property:",
2770 collection_to_add->type);
2771 TRACE_INFO_STRING("Header name we just added.",
2772 collection_to_add->property);
2773 if (error) {
2774 TRACE_ERROR_NUMBER("Adding property failed with error:", error);
2775 return error;
2776 }
2777 header = (struct collection_header *)collection_to_add->data;
2778 TRACE_INFO_NUMBER("Count:", header->count);
2779 TRACE_INFO_NUMBER("Ref count:", header->reference_count);
2780 header->reference_count++;
2781 TRACE_INFO_NUMBER("Ref count after increment:",
2782 header->reference_count);
2783 /* -> Transaction end */
2784 break;
2785
2786 case COL_ADD_MODE_EMBED:
2787 TRACE_INFO_STRING("We are embedding the collection.", "");
2788 /* First check if the passed in collection is referenced more than once */
2789 TRACE_INFO_NUMBER("Type of the header element we are adding:",
2790 collection_to_add->type);
2791 TRACE_INFO_STRING("Header name we are adding.",
2792 collection_to_add->property);
2793 TRACE_INFO_NUMBER("Type of the header element we are adding to:",
2794 acceptor->type);
2795 TRACE_INFO_STRING("Header name we are adding to.",
2796 acceptor->property);
2797
2798 error = col_insert_property_with_ref_int(acceptor,
2799 NULL,
2800 COL_DSP_END,
2801 NULL,
2802 0,
2803 0,
2804 name_to_use,
2805 COL_TYPE_COLLECTIONREF,
2806 (void *)(&collection_to_add),
2807 sizeof(struct collection_item **),
2808 NULL);
2809
2810
2811 TRACE_INFO_NUMBER("Adding property returned:", error);
2812 break;
2813
2814 case COL_ADD_MODE_CLONE:
2815 TRACE_INFO_STRING("We are cloning the collection.", "");
2816 TRACE_INFO_STRING("Name we will use.", name_to_use);
2817
2818 /* For future thread safety: Transaction start -> */
2819 error = col_copy_collection(&collection_copy,
2820 collection_to_add, name_to_use,
2821 COL_COPY_NORMAL);
2822 if (error) return error;
2823
2824 TRACE_INFO_STRING("We have a collection copy.", collection_copy->property);
2825 TRACE_INFO_NUMBER("Collection type.", collection_copy->type);
2826 TRACE_INFO_STRING("Acceptor collection.", acceptor->property);
2827 TRACE_INFO_NUMBER("Acceptor collection type.", acceptor->type);
2828
2829 error = col_insert_property_with_ref_int(acceptor,
2830 NULL,
2831 COL_DSP_END,
2832 NULL,
2833 0,
2834 0,
2835 name_to_use,
2836 COL_TYPE_COLLECTIONREF,
2837 (void *)(&collection_copy),
2838 sizeof(struct collection_item **),
2839 NULL);
2840
2841 /* -> Transaction end */
2842 TRACE_INFO_NUMBER("Adding property returned:", error);
2843 break;
2844
2845 case COL_ADD_MODE_FLAT:
2846 TRACE_INFO_STRING("We are flattening the collection.", "");
2847
2848 traverse_data.mode = COL_COPY_FLAT;
2849 traverse_data.current_path = NULL;
2850 traverse_data.copy_cb = NULL;
2851 traverse_data.ext_data = NULL;
2852
2853 if ((as_property) && (*as_property)) {
2854 /* The normal assignement generates a warning
2855 * becuase I am assigning const to a non const.
2856 * I can't make the structure member to be const
2857 * since it changes but it changes
2858 * to point to different stings at different time
2859 * This is just an initial sting it will use.
2860 * The logic does not change the content of the string.
2861 * To overcome the issue I use memcpy();
2862 */
2863 memcpy(&(traverse_data.given_name),
2864 &(as_property), sizeof(char *));
2865 traverse_data.given_len = strlen(as_property);
2866 }
2867 else {
2868 traverse_data.given_name = NULL;
2869 traverse_data.given_len = 0;
2870 }
2871
2872 error = col_walk_items(collection_to_add, COL_TRAVERSE_FLAT,
2873 col_copy_traverse_handler, (void *)(&traverse_data),
2874 NULL, acceptor, &depth);
2875
2876 TRACE_INFO_NUMBER("Copy collection flat returned:", error);
2877 break;
2878
2879 case COL_ADD_MODE_FLATDOT:
2880 TRACE_INFO_STRING("We are flattening the collection with dots.", "");
2881
2882 traverse_data.mode = COL_COPY_FLATDOT;
2883 traverse_data.current_path = NULL;
2884 traverse_data.copy_cb = NULL;
2885 traverse_data.ext_data = NULL;
2886
2887 if ((as_property) && (*as_property)) {
2888 /* The normal assignement generates a warning
2889 * becuase I am assigning const to a non const.
2890 * I can't make the structure member to be const
2891 * since it changes but it changes
2892 * to point to different stings at different time
2893 * This is just an initial sting it will use.
2894 * The logic does not change the content of the string.
2895 * To overcome the issue I use memcpy();
2896 */
2897 memcpy(&(traverse_data.given_name),
2898 &(as_property), sizeof(char *));
2899 traverse_data.given_len = strlen(as_property);
2900 }
2901 else {
2902 traverse_data.given_name = NULL;
2903 traverse_data.given_len = 0;
2904 }
2905
2906 error = col_walk_items(collection_to_add, COL_TRAVERSE_DEFAULT | COL_TRAVERSE_END,
2907 col_copy_traverse_handler, (void *)(&traverse_data),
2908 NULL, acceptor, &depth);
2909
2910 TRACE_INFO_NUMBER("Copy collection flatdot returned:", error);
2911 break;
2912
2913 default:
2914 error = EINVAL;
2915 }
2916
2917 TRACE_FLOW_NUMBER("col_add_collection_to_collection returning:", error);
2918 return error;
2919 }
2920
2921 /* TRAVERSING */
2922
2923 /* Function to traverse the entire collection including optionally
2924 * sub collections */
2925 int col_traverse_collection(struct collection_item *ci,
2926 int mode_flags,
2927 col_item_fn item_handler,
2928 void *custom_data)
2929 {
2930
2931 int error = EOK;
2932 unsigned depth = 0;
2933
2934 TRACE_FLOW_STRING("col_traverse_collection", "Entry.");
2935
2936 if (ci == NULL) {
2937 TRACE_ERROR_NUMBER("No collection to traverse!", EINVAL);
2938 return EINVAL;
2939 }
2940
2941 error = col_walk_items(ci, mode_flags, col_simple_traverse_handler,
2942 NULL, item_handler, custom_data, &depth);
2943
2944 if ((error != 0) && (error != EINTR_INTERNAL)) {
2945 TRACE_ERROR_NUMBER("Error walking tree", error);
2946 return error;
2947 }
2948
2949 TRACE_FLOW_STRING("col_traverse_collection", "Success exit.");
2950 return EOK;
2951 }
2952
2953 /* CHECK */
2954
2955 /* Convenience function to check if specific property is in the collection */
2956 int col_is_item_in_collection(struct collection_item *ci,
2957 const char *property_to_find,
2958 int type,
2959 int mode_flags,
2960 int *found)
2961 {
2962 int error;
2963
2964 TRACE_FLOW_STRING("col_is_item_in_collection","Entry.");
2965
2966 *found = COL_NOMATCH;
2967 error = col_find_item_and_do(ci, property_to_find,
2968 type, mode_flags,
2969 col_is_in_item_handler,
2970 (void *)found,
2971 COLLECTION_ACTION_FIND);
2972
2973 TRACE_FLOW_NUMBER("col_is_item_in_collection returning", error);
2974 return error;
2975 }
2976
2977 /* SEARCH */
2978 /* Search function. Looks up an item in the collection based on the property.
2979 Essentually it is a traverse function with spacial traversing logic.
2980 */
2981 int col_get_item_and_do(struct collection_item *ci,
2982 const char *property_to_find,
2983 int type,
2984 int mode_flags,
2985 col_item_fn item_handler,
2986 void *custom_data)
2987 {
2988 int error = EOK;
2989
2990 TRACE_FLOW_STRING("col_get_item_and_do","Entry.");
2991
2992 error = col_find_item_and_do(ci, property_to_find,
2993 type, mode_flags,
2994 item_handler,
2995 custom_data,
2996 COLLECTION_ACTION_FIND);
2997
2998 TRACE_FLOW_NUMBER("col_get_item_and_do returning", error);
2999 return error;
3000 }
3001
3002
3003 /* Get raw item */
3004 int col_get_item(struct collection_item *ci,
3005 const char *property_to_find,
3006 int type,
3007 int mode_flags,
3008 struct collection_item **item)
3009 {
3010
3011 int error = EOK;
3012
3013 TRACE_FLOW_STRING("col_get_item", "Entry.");
3014
3015 error = col_find_item_and_do(ci, property_to_find,
3016 type, mode_flags,
3017 NULL, (void *)item,
3018 COLLECTION_ACTION_GET);
3019
3020 TRACE_FLOW_NUMBER("col_get_item returning", error);
3021 return error;
3022 }
3023
3024 /* DELETE */
3025 /* Delete property from the collection */
3026 int col_delete_property(struct collection_item *ci,
3027 const char *property_to_find,
3028 int type,
3029 int mode_flags)
3030 {
3031 int error = EOK;
3032 int found;
3033
3034 TRACE_FLOW_STRING("col_delete_property", "Entry.");
3035 found = COL_NOMATCH;
3036
3037 error = col_find_item_and_do(ci, property_to_find,
3038 type, mode_flags,
3039 NULL, (void *)(&found),
3040 COLLECTION_ACTION_DEL);
3041
3042 if ((error == EOK) && (found == COL_NOMATCH))
3043 error = ENOENT;
3044
3045 TRACE_FLOW_NUMBER("col_delete_property returning", error);
3046 return error;
3047 }
3048
3049 /* UPDATE */
3050 /* Update property in the collection */
3051 int col_update_property(struct collection_item *ci,
3052 const char *property_to_find,
3053 int type,
3054 void *new_data,
3055 int length,
3056 int mode_flags)
3057 {
3058 int error = EOK;
3059 struct update_property update_data;
3060
3061 TRACE_FLOW_STRING("col_update_property", "Entry.");
3062 update_data.type = type;
3063 update_data.data = new_data;
3064 update_data.length = length;
3065 update_data.found = COL_NOMATCH;
3066
3067 error = col_find_item_and_do(ci, property_to_find,
3068 type, mode_flags,
3069 NULL, (void *)(&update_data),
3070 COLLECTION_ACTION_UPDATE);
3071
3072 if ((error == EOK) && (update_data.found == COL_NOMATCH))
3073 error = ENOENT;
3074
3075 TRACE_FLOW_NUMBER("col_update_property returning", error);
3076 return error;
3077 }
3078
3079
3080 /* Function to modify the item */
3081 int col_modify_item(struct collection_item *item,
3082 const char *property,
3083 int type,
3084 const void *data,
3085 int length)
3086 {
3087 TRACE_FLOW_STRING("col_modify_item", "Entry");
3088
3089 /* Allow renameing only */
3090 if ((item == NULL) ||
3091 ((item->type == COL_TYPE_COLLECTION) && (length != 0)) ||
3092 ((item->type == COL_TYPE_COLLECTIONREF) && (length != 0))) {
3093 TRACE_ERROR_NUMBER("Invalid argument or invalid argument type", EINVAL);
3094 return EINVAL;
3095 }
3096
3097 if (property != NULL) {
3098 if (col_validate_property(property)) {
3099 TRACE_ERROR_STRING("Invalid chracters in the property name", property);
3100 return EINVAL;
3101 }
3102 free(item->property);
3103 item->property = strdup(property);
3104 if (item->property == NULL) {
3105 TRACE_ERROR_STRING("Failed to allocate memory", "");
3106 return ENOMEM;
3107 }
3108
3109 /* Update property length and hash if we rename the property */
3110 item->phash = col_make_hash(property, 0, &(item->property_len));
3111 TRACE_INFO_NUMBER("Item hash", item->phash);
3112 TRACE_INFO_NUMBER("Item property length", item->property_len);
3113 TRACE_INFO_NUMBER("Item property strlen", strlen(item->property));
3114
3115 }
3116
3117 /* We need to change data ? */
3118 if(length) {
3119
3120 /* If type is different or same but it is string or binary we need to
3121 * replace the storage */
3122 if ((item->type != type) ||
3123 ((item->type == type) &&
3124 ((item->type == COL_TYPE_STRING) || (item->type == COL_TYPE_BINARY)))) {
3125 TRACE_INFO_STRING("Replacing item data buffer", "");
3126 free(item->data);
3127 item->data = malloc(length);
3128 if (item->data == NULL) {
3129 TRACE_ERROR_STRING("Failed to allocate memory", "");
3130 item->length = 0;
3131 return ENOMEM;
3132 }
3133 item->length = length;
3134 }
3135
3136 TRACE_INFO_STRING("Overwriting item data", "");
3137 memcpy(item->data, data, item->length);
3138 item->type = type;
3139
3140 if (item->type == COL_TYPE_STRING)
3141 ((char *)(item->data))[item->length - 1] = '\0';
3142 }
3143
3144 TRACE_FLOW_STRING("col_modify_item", "Exit");
3145 return EOK;
3146 }
3147
3148
3149 /* Set collection class */
3150 int col_set_collection_class(struct collection_item *item,
3151 unsigned cclass)
3152 {
3153 struct collection_header *header;
3154
3155 TRACE_FLOW_STRING("col_set_collection_class", "Entry");
3156
3157 if (item->type != COL_TYPE_COLLECTION) {
3158 TRACE_INFO_NUMBER("Not a collectin object. Type is", item->type);
3159 return EINVAL;
3160 }
3161
3162 header = (struct collection_header *)item->data;
3163 header->cclass = cclass;
3164 TRACE_FLOW_STRING("col_set_collection_class", "Exit");
3165 return EOK;
3166 }
3167
3168 /* Get collection class */
3169 int col_get_collection_class(struct collection_item *item,
3170 unsigned *cclass)
3171 {
3172 struct collection_header *header;
3173
3174 TRACE_FLOW_STRING("col_get_collection_class", "Entry");
3175
3176 if (item->type != COL_TYPE_COLLECTION) {
3177 TRACE_ERROR_NUMBER("Not a collection object. Type is", item->type);
3178 return EINVAL;
3179 }
3180
3181 header = (struct collection_header *)item->data;
3182 *cclass = header->cclass;
3183 TRACE_FLOW_STRING("col_get_collection_class", "Exit");
3184 return EOK;
3185 }
3186
3187 /* Get collection count */
3188 int col_get_collection_count(struct collection_item *item,
3189 unsigned *count)
3190 {
3191 struct collection_header *header;
3192
3193 TRACE_FLOW_STRING("col_get_collection_count", "Entry");
3194
3195 if (item->type != COL_TYPE_COLLECTION) {
3196 TRACE_ERROR_NUMBER("Not a collectin object. Type is", item->type);
3197 return EINVAL;
3198 }
3199
3200 header = (struct collection_header *)item->data;
3201 *count = header->count;
3202 TRACE_FLOW_STRING("col_get_collection_count", "Exit");
3203 return EOK;
3204
3205 }
3206
3207 /* Convinience function to check if the collection is of the specific class */
3208 /* In case of internal error assumes that collection is not of the right class */
3209 int col_is_of_class(struct collection_item *item, unsigned cclass)
3210 {
3211 int error = EOK;
3212 unsigned ret_class = 0;
3213
3214 TRACE_FLOW_STRING("col_is_of_class invoked", "");
3215
3216 error = col_get_collection_class(item, &ret_class);
3217 if (error || (ret_class != cclass))
3218 return 0;
3219 else
3220 return 1;
3221 }
3222
3223 /* Get propery */
3224 const char *col_get_item_property(struct collection_item *ci,
3225 int *property_len)
3226 {
3227 if (property_len != NULL) *property_len = ci->property_len;
3228 return ci->property;
3229 }
3230
3231 /* Get type */
3232 int col_get_item_type(struct collection_item *ci)
3233 {
3234 return ci->type;
3235 }
3236
3237 /* Get length */
3238 int col_get_item_length(struct collection_item *ci)
3239 {
3240 return ci->length;
3241 }
3242
3243 /* Get data */
3244 void *col_get_item_data(struct collection_item *ci)
3245 {
3246 return ci->data;
3247 }
3248
3249 /* Get hash */
3250 uint64_t col_get_item_hash(struct collection_item *ci)
3251 {
3252 return ci->phash;
3253 }
3254
3255 /* Calculates hash of the string using internal hashing
3256 * algorithm. Populates "length" with length
3257 * of the string not counting 0.
3258 * Length argument can be NULL.
3259 */
3260 uint64_t col_make_hash(const char *string, int sub_len, int *length)
3261 {
3262 uint64_t hash = 0;
3263 int str_len = 0;
3264
3265 TRACE_FLOW_STRING("col_make_hash called for string:", string);
3266
3267 if (string) {
3268 hash = FNV1a_base;
3269 while (string[str_len] != 0) {
3270
3271 /* Check if we need to stop */
3272 if ((sub_len > 0) && (str_len == sub_len)) break;
3273
3274 hash = hash ^ toupper(string[str_len]);
3275 hash *= FNV1a_prime;
3276 str_len++;
3277 }
3278 }
3279
3280 if (length) *length = str_len;
3281
3282 TRACE_FLOW_NUMBER("col_make_hash returning hash:", hash);
3283
3284 return hash;
3285 }