diff options
Diffstat (limited to 'collection/elapi_collection.c')
-rw-r--r-- | collection/elapi_collection.c | 229 |
1 files changed, 227 insertions, 2 deletions
diff --git a/collection/elapi_collection.c b/collection/elapi_collection.c index 50809ae..7045130 100644 --- a/collection/elapi_collection.c +++ b/collection/elapi_collection.c @@ -35,6 +35,13 @@ /* Special internal error code to indicate that collection search was interrupted */ #define EINTR_INTERNAL 10000 + +/* Potential subjest for management with libtools */ +#define DATE_FORMAT "%+" + +#define TIME_ARRAY_SIZE 100 + + /* Struct used for passing parameter for update operation */ struct update_property { int type; @@ -712,10 +719,10 @@ static int update_item(struct collection_item *current, int error = EOK; DEBUG_STRING("update_item","Entry"); - /* If type is different or samew but it is string or binary we need to replace the storage */ + /* If type is different or same but it is string or binary we need to replace the storage */ if((current->type != update_data->type) || ((current->type == update_data->type) && - ((current->type != ELAPI_TYPE_STRING) || (current->type != ELAPI_TYPE_BINARY)))) { + ((current->type == ELAPI_TYPE_STRING) || (current->type == ELAPI_TYPE_BINARY)))) { DEBUG_STRING("Replacing item data buffer",""); free(current->data); current->data = malloc(update_data->length); @@ -1174,6 +1181,224 @@ int add_any_property(struct collection_item *ci, return error; } +/* Add a string property. + If length equals 0, the length is determined based on the string. + Lenght INCLUDES the terminating 0 */ +int add_str_property_with_ref(struct collection_item *ci,char *subcollection, char *property,char *string,int length, + struct collection_item **ref_ret) +{ + int error = EOK; + struct collection_item *item; + + DEBUG_STRING("add_str_property_with_ref","Entry."); + + if(length == 0) length = strlen(string) + 1; + item = add_property(ci,subcollection,property,(void *)(string),length, ELAPI_TYPE_STRING, &error); + + if(ref_ret != (struct collection_item **)(NULL)) *ref_ret = item; + + DEBUG_NUMBER("add_str_property_with_ref returning",error); + return error; +} + +/* Add a binary property. */ +int add_binary_property_with_ref(struct collection_item *ci,char *subcollection, char *property,void *binary_data,int length, + struct collection_item **ref_ret) +{ + int error = EOK; + struct collection_item *item; + + DEBUG_STRING("add_binary_property_with_ref","Entry."); + + item = add_property(ci,subcollection,property,binary_data,length, ELAPI_TYPE_BINARY, &error); + + if(ref_ret != (struct collection_item **)(NULL)) *ref_ret = item; + + DEBUG_NUMBER("add_binary_property_with_ref returning",error); + return error; +} + +/* Add an int property. */ +int add_int_property_with_ref(struct collection_item *ci,char *subcollection, char *property,int number, + struct collection_item **ref_ret) +{ + int error = EOK; + struct collection_item *item; + + DEBUG_STRING("add_int_property_with_ref","Entry."); + + item = add_property(ci,subcollection,property,(void *)(&number),sizeof(int), ELAPI_TYPE_INTEGER, &error); + + if(ref_ret != (struct collection_item **)(NULL)) *ref_ret = item; + + DEBUG_NUMBER("add_int_property_with_ref returning",error); + return error; +} + +/* Add an unsigned int property. */ +int add_unsigned_property_with_ref(struct collection_item *ci,char *subcollection, char *property,unsigned int number, + struct collection_item **ref_ret) +{ + int error = EOK; + struct collection_item *item; + + DEBUG_STRING("add_unsigned_property_with_ref","Entry."); + + item = add_property(ci,subcollection,property,(void *)(&number),sizeof(int), ELAPI_TYPE_UNSIGNED, &error); + + if(ref_ret != (struct collection_item **)(NULL)) *ref_ret = item; + + DEBUG_NUMBER("add_unsigned_property_with_ref returning",error); + return error; +} + +/* Add an long property. */ +int add_long_property_with_ref(struct collection_item *ci,char *subcollection, char *property,long number, + struct collection_item **ref_ret) +{ + int error = EOK; + struct collection_item *item; + + DEBUG_STRING("add_long_property_with_ref","Entry."); + + item = add_property(ci,subcollection,property,(void *)(&number),sizeof(long), ELAPI_TYPE_LONG, &error); + + if(ref_ret != (struct collection_item **)(NULL)) *ref_ret = item; + + DEBUG_NUMBER("add_long_property_with_ref returning",error); + return error; +} + +/* Add an unsigned long property. */ +int add_ulong_property_with_ref(struct collection_item *ci,char *subcollection, char *property,unsigned long number, + struct collection_item **ref_ret) +{ + int error = EOK; + struct collection_item *item; + + DEBUG_STRING("add_ulong_property_with_ref","Entry."); + + item = add_property(ci,subcollection,property,(void *)(&number),sizeof(long), ELAPI_TYPE_ULONG, &error); + + if(ref_ret != (struct collection_item **)(NULL)) *ref_ret = item; + + DEBUG_NUMBER("add_ulong_property_with_ref returning",error); + return error; +} + +/* Add a double property. */ +int add_double_property_with_ref(struct collection_item *ci,char *subcollection, char *property,double number, + struct collection_item **ref_ret) +{ + int error = EOK; + struct collection_item *item; + + DEBUG_STRING("add_double_property_with_ref","Entry."); + + item = add_property(ci,subcollection,property,(void *)(&number),sizeof(double), ELAPI_TYPE_DOUBLE, &error); + + if(ref_ret != (struct collection_item **)(NULL)) *ref_ret = item; + + DEBUG_NUMBER("add_double_property_with_ref returning",error); + return error; +} + +/* A function to add a property */ +int add_any_property_with_ref(struct collection_item *ci, + char *subcollection, + char *property, + int type, + void *data, + int length, + struct collection_item **ref_ret) +{ + int error = EOK; + struct collection_item *item; + + DEBUG_STRING("add_any_property_with_ref","Entry."); + + item = add_property(ci,subcollection,property,data,length, type, &error); + + if(ref_ret != (struct collection_item **)(NULL)) *ref_ret = item; + + DEBUG_NUMBER("add_any_property_with_ref returning",error); + return error; +} + +/* Set time stamp in the collection */ +int set_timestamp(struct collection_item *ci,struct collection_item **timestr_ref,struct collection_item **timeint_ref) +{ + time_t utctime; + struct tm time_struct; + char time_array[TIME_ARRAY_SIZE+1]; + int len; + struct collection_item *timestr = (struct collection_item *)(NULL); + struct collection_item *timeint = (struct collection_item *)(NULL); + + DEBUG_STRING("set_timestamp","Entry point"); + + utctime = time(NULL); + localtime_r(&utctime,&time_struct); + + len = strftime(time_array, TIME_ARRAY_SIZE, date_format, &time_struct); + if(len == 0) { + DEBUG_STRING("add_time","CODING ERROR - INCREASE THE BUFFER"); + return EMSGSIZE; + } + + /* Check if we have the timestamp item already */ + error = get_item(event, TS_NAME, ELAPI_TYPE_STRING,ELAPI_TRAVERSE_IGNORE,×tr); + if(error) { + DEBUG_NUMBER("search failed with error:",error); + return error; + } + + if(timestr != (struct collection_item *)(NULL)) { + /* There is a timestamp */ + free(timestr->data); + timestr->data = strdup(time_array); + if(timestr->data == NULL) { + DEBUG_NUMBER("failed to add timestamp property:",error); + return ENOMEM; + } + timestr->length = len+1; + *timestr_ref = timestr; + } + else { + /* Add timestamp to the collection */ + error = add_str_property_with_ref(event,NULL, TS_NAME,time_array,len+1,timestr_ref); + if(error) { + DEBUG_NUMBER("failed to add timestamp property:",error); + return error; + } + } + + /* Check if we have the time item already */ + error = get_item(event, T_NAME, ELAPI_TYPE_INTEGER,ELAPI_TRAVERSE_IGNORE,&timeint); + if(error) { + DEBUG_NUMBER("search failed with error:",error); + return error; + } + + if(timeint != (struct collection_item *)(NULL)) { + /* There is a time property */ + *((int *)(timeint->data)) = utctime; + *timeint_ref = timeint; + } + else { + /* Add time to the collection */ + error = add_int_property_with_ref(event,NULL, T_NAME,utctime,timeint_ref); + if(error) { + DEBUG_NUMBER("failed to add time property:",error); + return error; + } + } + + DEBUG_STRING("set_timestamp","Exit point"); + return EOK; +} + + /* COPY */ |