diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/collection/collection.c | 40 | ||||
-rw-r--r-- | common/collection/collection.h | 15 | ||||
-rw-r--r-- | common/collection/collection_ut.c | 66 | ||||
-rw-r--r-- | common/ini/ini_config.c | 22 | ||||
-rw-r--r-- | common/ini/ini_config_ut.c | 56 |
5 files changed, 113 insertions, 86 deletions
diff --git a/common/collection/collection.c b/common/collection/collection.c index 31b47a05..d6fd872f 100644 --- a/common/collection/collection.c +++ b/common/collection/collection.c @@ -140,7 +140,8 @@ static int col_validate_property(const char *property) check = property; while (*check != '\0') { - if (((!isalnum(*check)) && (!ispunct(*check))) || (*check == '.')) { + /* It turned out that limiting collection charcters is bad */ + if ((*check < ' ') || (*check == '!')) { invalid = 1; break; } @@ -1165,7 +1166,7 @@ static int col_create_path_data(struct path_data **name_path, if(length > 0) { memcpy(new_name_path->name, name, length); new_name_path->length = length; - new_name_path->name[new_name_path->length] = '.'; + new_name_path->name[new_name_path->length] = '!'; new_name_path->length++; new_name_path->name[new_name_path->length] = '\0'; TRACE_INFO_STRING("Name so far:", new_name_path->name); @@ -1233,7 +1234,7 @@ static int col_match_item(struct collection_item *current, if (data_str == start) { if (find_str > traverse_data->name_to_find) { - if (*(find_str-1) == '.') { + if (*(find_str-1) == '!') { /* We matched the property but the search string is * longer so we need to continue matching */ TRACE_INFO_STRING("col_match_item", @@ -1254,7 +1255,7 @@ static int col_match_item(struct collection_item *current, } } else if ((find_str == traverse_data->name_to_find) && - (*(data_str-1) == '.')) return COL_MATCH; + (*(data_str-1) == '!')) return COL_MATCH; data_str--; find_str--; @@ -1459,8 +1460,8 @@ static int col_walk_items(struct collection_item *ci, /* Find an item by property name and perform an action on it. */ /* No pattern matching supported in the first implementation. */ -/* To refer to child properties use dotted notatation like this: */ -/* parent.child.subchild.subsubchild etc. */ +/* To refer to child properties use notatation like this: */ +/* parent!child!subchild!subsubchild etc. */ static int col_find_item_and_do(struct collection_item *ci, const char *property_to_find, int type, @@ -1475,7 +1476,7 @@ static int col_find_item_and_do(struct collection_item *ci, unsigned depth = 0; int count = 0; const char *last_part; - char *dot; + char *sep; TRACE_FLOW_STRING("col_find_item_and_do", "Entry."); @@ -1511,18 +1512,18 @@ static int col_find_item_and_do(struct collection_item *ci, traverse_data->name_len_to_find = strlen(property_to_find); - /* Check if the search string ends with dot - this is ellegal */ - if (traverse_data->name_to_find[traverse_data->name_len_to_find - 1] == '.') { + /* Check if the search string ends with "!" - this is illegal */ + if (traverse_data->name_to_find[traverse_data->name_len_to_find - 1] == '!') { TRACE_ERROR_NUMBER("Search string is invalid.", EINVAL); free(traverse_data); return EINVAL; } - /* Find last dot if any */ - dot = strrchr(traverse_data->name_to_find, '.'); - if (dot != NULL) { - dot++; - last_part = dot; + /* Find last ! if any */ + sep = strrchr(traverse_data->name_to_find, '!'); + if (sep != NULL) { + sep++; + last_part = sep; } else last_part = traverse_data->name_to_find; @@ -2600,6 +2601,17 @@ int col_modify_item(struct collection_item *item, TRACE_ERROR_STRING("Failed to allocate memory", ""); return ENOMEM; } + + /* Update property length and hash if we rename the property */ + item->phash = FNV1a_base; + item->property_len = 0; + + while (property[item->property_len] != 0) { + item->phash = item->phash ^ toupper(property[item->property_len]); + item->phash *= FNV1a_prime; + item->property_len++; + } + } /* We need to change data ? */ diff --git a/common/collection/collection.h b/common/collection/collection.h index 73ac7b34..6868cb6e 100644 --- a/common/collection/collection.h +++ b/common/collection/collection.h @@ -165,20 +165,20 @@ struct collection_iterator; * get_item function will return you the item that is your "wallet". * You can then change something or just get information about the item you * retrieved. But in most cases you do not the wallet itself. You want to get - * something from the vallet or put something into it. IMO money would be an + * something from the wallet or put something into it. IMO money would be an * obvious choice. To do this you use update_xxx_property functions. * There might be a bag somewhere deep and you might want to add something to * it. add_xxx_property_xxx functions allow you to specify sub collection you * want the item to be added to. If this sub collection argument is NULL top * level collection is assumed. - * The search in the collections users a dotted notation to refer to an item (or + * The search in the collections users a "x!y!z" notation to refer to an item (or * property). You can search for "wallet" and it will find any first instance of * the "wallet" in your luggage. But you might have two wallets. One is yours and - * another is your significant other's. So you might say find "my.wallet". + * another is your significant other's. So you might say find "my!wallet". * It will find wallet in your bad (collection) named "my". This collection can * be many levels deep inside other collections. You do not need to know the * full path to get to it. But if you have the full path you can use the fill - * path like this "luggage.newbags.my.wallet". + * path like this "luggage!newbags!my!wallet". * It is useful to be able to put bags into bags as well as get them out of each * other. When the collection is created the header keeps a reference count on * how many copies of the collection are known to the world. So one can put a @@ -188,6 +188,9 @@ struct collection_iterator; * By extracting reference from an internal collection the caller gains access * to the collection directly and thus has responsibility to destroy it after * use. + * Characters with codes less than space in ASCII table are illegal for property + * names. + * Character '!' also illegal in property name and reserved for "x!y!z" notation. */ /* Function that creates a named collection */ @@ -324,7 +327,7 @@ int col_set_timestamp(struct collection_item *ci, /* Update functions */ /* All update functions search the property using the search algorithm * described at the top of the header file. - * Use same dotted notation to specify a property. + * Use same "x!y" notation to specify a property. */ /* Update a string property in the collection. * Length should include the terminating 0 */ @@ -450,7 +453,7 @@ int col_get_item(struct collection_item *ci, /* Collection to find things /* Group of functions that allows retrieving individual elements of the collection_item * hiding the internal implementation. */ -const char *col_get_item_property(struct collection_item *ci,int *property_len); +const char *col_get_item_property(struct collection_item *ci, int *property_len); int col_get_item_type(struct collection_item *ci); int col_get_item_length(struct collection_item *ci); void *col_get_item_data(struct collection_item *ci); diff --git a/common/collection/collection_ut.c b/common/collection/collection_ut.c index a3da2e20..6d913f9a 100644 --- a/common/collection/collection_ut.c +++ b/common/collection/collection_ut.c @@ -124,7 +124,7 @@ int single_collection_test(void) return error; } - error = col_add_str_property(handle, NULL, "property 1", "some data", 0); + error = col_add_str_property(handle, NULL, "property 1!", "some data", 0); if (error) printf("Expected error adding bad property to collection %d\n", error); else { printf("Expected error but got success\n"); @@ -362,7 +362,7 @@ int mixed_collection_test(void) col_debug_collection(socket1, COL_TRAVERSE_DEFAULT); printf("SOCKET1 MUST NO BE USED AFTER THIS POINT!!!\n"); - socket1 = (struct collection_item *)(NULL); + socket1 = NULL; printf("Add collection PEER as PEER1 to subcollection SOCKET1 of the EVENT.\n"); @@ -414,7 +414,7 @@ int mixed_collection_test(void) /* Check if the property in the collection */ found = 0; - error = col_is_item_in_collection(event, "peer1.delay", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); + error = col_is_item_in_collection(event, "peer1!delay", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); if (error) { col_destroy_collection(peer); col_destroy_collection(host); @@ -428,13 +428,13 @@ int mixed_collection_test(void) else printf("Error property is not found!\n"); - col_print_item(event, "peer1.IPv6"); - col_print_item(event, "event.socket1.peer1.IPv6"); - col_print_item(event, "event.peer1.IPv6"); - col_print_item(event, "speer1.IPv6"); - col_print_item(event, "eer1.IPv6"); - col_print_item(event, ".peer1.IPv6"); - col_print_item(event, "t.peer1.IPv6"); + col_print_item(event, "peer1!IPv6"); + col_print_item(event, "event!socket1!peer1!IPv6"); + col_print_item(event, "event!peer1!IPv6"); + col_print_item(event, "speer1!IPv6"); + col_print_item(event, "eer1!IPv6"); + col_print_item(event, "!peer1!IPv6"); + col_print_item(event, "t!peer1!IPv6"); /* Traverse collection */ error = col_print_collection2(event); @@ -443,9 +443,9 @@ int mixed_collection_test(void) return error; } - printf("Delete property PEER1.DELAY from the EVENT collection.\n"); + printf("Delete property PEER1!DELAY from the EVENT collection.\n"); - error = col_delete_property(event, "peer1.delay", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT); + error = col_delete_property(event, "peer1!delay", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT); if (error) { col_destroy_collection(peer); col_destroy_collection(host); @@ -558,9 +558,9 @@ int iterator_test(void) struct collection_item *socket1; struct collection_item *socket2; struct collection_item *socket3; - struct collection_iterator *iterator = (struct collection_iterator *)(NULL); + struct collection_iterator *iterator = NULL; int error = EOK; - struct collection_item *item = (struct collection_item *)(NULL); + struct collection_item *item = NULL; char binary_dump[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; int depth = 0; int idepth = 0; @@ -677,7 +677,7 @@ int iterator_test(void) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; depth = 0; col_get_item_depth(iterator, &depth); @@ -757,7 +757,7 @@ int iterator_test(void) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; depth = 0; col_get_item_depth(iterator, &depth); @@ -792,7 +792,7 @@ int iterator_test(void) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; depth = 0; col_get_item_depth(iterator, &depth); @@ -827,7 +827,7 @@ int iterator_test(void) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; depth = 0; col_get_item_depth(iterator, &depth); @@ -863,7 +863,7 @@ int iterator_test(void) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; depth = 0; col_get_item_depth(iterator, &depth); @@ -899,7 +899,7 @@ int iterator_test(void) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; depth = 0; col_get_item_depth(iterator, &depth); @@ -934,7 +934,7 @@ int iterator_test(void) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; depth = 0; col_get_item_depth(iterator, &depth); @@ -971,7 +971,7 @@ int iterator_test(void) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; depth = 0; col_get_item_depth(iterator, &depth); @@ -1015,7 +1015,7 @@ int insert_extract_test(void) struct collection_item *col; struct collection_item *col2; int error = EOK; - struct collection_item *item = (struct collection_item *)(NULL); + struct collection_item *item = NULL; printf("\n\n==== INSERTION TEST ====\n\n"); @@ -1279,7 +1279,7 @@ int search_test(void) col_debug_collection(level1, COL_TRAVERSE_DEFAULT); - error = col_is_item_in_collection(level1, "level1.level2.level3.level4.", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); + error = col_is_item_in_collection(level1, "level1!level2!level3!level4!", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); if (!error) { col_destroy_collection(level1); col_destroy_collection(level2); @@ -1291,13 +1291,13 @@ int search_test(void) found = 0; error = 0; - error = col_is_item_in_collection(level1, "level1.level2.level3.level4.id", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); + error = col_is_item_in_collection(level1, "level1!level2!level3!level4!id", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); if ((error) || (!found)) { col_destroy_collection(level1); col_destroy_collection(level2); col_destroy_collection(level3); col_destroy_collection(level4); - printf("Failed to find item [level1.level2.level3.level4.id]. Error %d\n", error); + printf("Failed to find item [level1!level2!level3!level4!id]. Error %d\n", error); return error ? error : ENOENT; } else printf("Expected item is found\n"); @@ -1305,20 +1305,20 @@ int search_test(void) found = 0; error = 0; - error = col_is_item_in_collection(level1, "level3.level4.id", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); + error = col_is_item_in_collection(level1, "level3!level4!id", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); if ((error) || (!found)) { col_destroy_collection(level1); col_destroy_collection(level2); col_destroy_collection(level3); col_destroy_collection(level4); - printf("Failed to find item [level3.level4.id]. Error %d\n", error); + printf("Failed to find item [level3!level4!id]. Error %d\n", error); return error ? error : ENOENT; } else printf("Expected item is found\n"); found = 0; error = 0; - error = col_is_item_in_collection(level1, "level3.packets", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); + error = col_is_item_in_collection(level1, "level3!packets", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); if ((error) || (!found)) { col_destroy_collection(level1); col_destroy_collection(level2); @@ -1331,26 +1331,26 @@ int search_test(void) found = 0; error = 0; - error = col_is_item_in_collection(level1, "level1.level2.stack", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); + error = col_is_item_in_collection(level1, "level1!level2!stack", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); if ((error) || (!found)) { col_destroy_collection(level1); col_destroy_collection(level2); col_destroy_collection(level3); col_destroy_collection(level4); - printf("Failed to find item [level1.level2.stack]. Error %d\n", error); + printf("Failed to find item [level1!level2!stack]. Error %d\n", error); return error ? error : ENOENT; } else printf("Expected item is found\n"); found = 0; error = 0; - error = col_is_item_in_collection(level1, "level1.level2.level3", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); + error = col_is_item_in_collection(level1, "level1!level2!level3", COL_TYPE_ANY, COL_TRAVERSE_DEFAULT, &found); if ((error) || (!found)) { col_destroy_collection(level1); col_destroy_collection(level2); col_destroy_collection(level3); col_destroy_collection(level4); - printf("Failed to find item [level1.level2.level3]. Error %d\n", error); + printf("Failed to find item [level1!level2!level3]. Error %d\n", error); return error ? error : ENOENT; } else printf("Expected item is found\n"); diff --git a/common/ini/ini_config.c b/common/ini/ini_config.c index 43f6d3b4..e4b1f365 100644 --- a/common/ini/ini_config.c +++ b/common/ini/ini_config.c @@ -73,6 +73,9 @@ #define RET_EOF 5 #define RET_ERROR 6 +#define INI_ERROR "errors" +#define INI_ERROR_NAME "errname" + /* Different error string functions can be passed as callbacks */ typedef const char * (*error_fn)(int error); @@ -175,7 +178,7 @@ static int ini_to_collection(const char *filename, TRACE_FLOW_STRING("ini_to_collection", "Entry"); /* Open file for reading */ - file = fopen(filename,"r"); + file = fopen(filename, "r"); if (file == NULL) { error = errno; TRACE_ERROR_NUMBER("Failed to open file - but this is OK", error); @@ -185,12 +188,20 @@ static int ini_to_collection(const char *filename, /* Open the collection of errors */ if (error_list != NULL) { *error_list = NULL; - error = col_create_collection(error_list, filename, COL_CLASS_INI_PERROR); + error = col_create_collection(error_list, INI_ERROR, COL_CLASS_INI_PERROR); if (error) { TRACE_ERROR_NUMBER("Failed to create error collection", error); fclose(file); return error; } + /* Add file name as the first item */ + error = col_add_str_property(*error_list, NULL, INI_ERROR_NAME, filename, 0); + if (error) { + TRACE_ERROR_NUMBER("Failed to and name to collection", error); + fclose(file); + col_destroy_collection(*error_list); + return error; + } created = 1; } @@ -891,9 +902,9 @@ static void print_error_list(FILE *file, /* Process collection header */ if (col_get_item_type(item) == COL_TYPE_COLLECTION) { col_get_collection_count(item, &count); - if (count > 1) - fprintf(file, error_header, col_get_item_property(item, NULL)); - else break; + if (count <= 2) break; + } else if (col_get_item_type(item) == COL_TYPE_STRING) { + fprintf(file, error_header, (char *)col_get_item_data(item)); } else { /* Put error into provided format */ @@ -1009,6 +1020,7 @@ void print_config_parsing_errors(FILE *file, if (error) { TRACE_ERROR_STRING("Error (extract):", FAILED_TO_PROCCESS); fprintf(file, "%s\n", FAILED_TO_PROCCESS); + col_unbind_iterator(iterator); return; } print_file_parsing_errors(file, file_errors); diff --git a/common/ini/ini_config_ut.c b/common/ini/ini_config_ut.c index bcb47e6d..3ce08aea 100644 --- a/common/ini/ini_config_ut.c +++ b/common/ini/ini_config_ut.c @@ -217,7 +217,7 @@ int real_test(const char *file) } /* Are we done ? */ - if (item == (struct collection_item *)(NULL)) break; + if (item == NULL) break; type = col_get_item_type(item); @@ -246,9 +246,9 @@ int get_test(void) { int error; - struct collection_item *ini_config = (struct collection_item *)(NULL); - struct collection_item *error_set = (struct collection_item *)(NULL); - struct collection_item *item = (struct collection_item *)(NULL); + struct collection_item *ini_config = NULL; + struct collection_item *error_set = NULL; + struct collection_item *item = NULL; int number; long number_long; double number_double; @@ -286,7 +286,7 @@ int get_test(void) printf("Negtive test - trying to get non existing key-value pair.\n"); /* Negative test */ - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("monitor1", "description1", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -295,14 +295,14 @@ int get_test(void) } /* Item should not be found */ - if (item != (struct collection_item *)(NULL)) { + if (item != NULL) { printf("Expected NULL but got something else!\n"); free_ini_config(ini_config); return -1; } /* Another negative test but section exists this time */ - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("monitor", "description1", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -311,7 +311,7 @@ int get_test(void) } /* Item should not be found */ - if(item != (struct collection_item *)(NULL)) { + if(item != NULL) { printf("Expected NULL but got something else!\n"); free_ini_config(ini_config); return -1; @@ -320,7 +320,7 @@ int get_test(void) printf("Trying to get an item.\n"); /* Positive test */ - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("monitor", "description", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -329,7 +329,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected item but got something NULL!\n"); free_ini_config(ini_config); return -1; @@ -380,7 +380,7 @@ int get_test(void) /* Get a badly formated number */ printf("Convert item to number with strict conversion.\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("monitor", "bad_number", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -389,7 +389,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected item but got something NULL!\n"); free_ini_config(ini_config); return -1; @@ -434,7 +434,7 @@ int get_test(void) printf("Fetch another item from section \"domains/LOCAL\" named \"enumerate\".\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/LOCAL","enumerate", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -443,7 +443,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -563,7 +563,7 @@ int get_test(void) /* Get real bool item and convert it */ printf("Get real bool item \"legacy\" and convert it.\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/LOCAL","legacy", ini_config, &item); if (error) { printf("Expected success but got error! %d\n",error); @@ -572,7 +572,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -597,7 +597,7 @@ int get_test(void) printf("Get binary item\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/EXAMPLE.COM","binary_test", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -606,7 +606,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -632,7 +632,7 @@ int get_test(void) printf("Get string array item\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains", "domainsorder", ini_config, &item); if(error) { printf("Expected success but got error! %d\n",error); @@ -641,7 +641,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -686,7 +686,7 @@ int get_test(void) printf("Get long array item\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/EXAMPLE.COM", "long_array", ini_config, &item); if(error) { printf("Expected success but got error! %d\n", error); @@ -695,7 +695,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -719,7 +719,7 @@ int get_test(void) printf("Get double array item\n"); - item = (struct collection_item *)(NULL); + item = NULL; error = get_config_item("domains/EXAMPLE.COM", "double_array", ini_config, &item); if (error) { printf("Expected success but got error! %d\n", error); @@ -728,7 +728,7 @@ int get_test(void) } /* Item should be found */ - if (item == (struct collection_item *)(NULL)) { + if (item == NULL) { printf("Expected success but got NULL.\n"); free_ini_config(ini_config); return -1; @@ -800,14 +800,14 @@ int get_test(void) int main(int argc, char *argv[]) { - int error; - - char *srcdir; + int error = EOK; + char *srcdir = NULL; srcdir = getenv("srcdir"); if(srcdir) { - if(chdir(srcdir) == 0) { + if(chdir(srcdir) != 0) { error = errno; + printf("Failed to change directory, error %d\n", error); return error; } } |