Code

store: Pass the right timestamp to newly created store objects.
[sysdb.git] / src / core / store.c
index 2088e73547c48e436d2de216003ca8bab9748430..385cd634d9271dcd63baaaccbb5d350816d8a3e6 100644 (file)
@@ -98,11 +98,10 @@ enum {
 #define _last_update super.last_update
 
 static int
-store_obj_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
+store_obj_init(sdb_object_t *obj, va_list ap)
 {
        store_obj_t *sobj = STORE_OBJ(obj);
-       sobj->last_update = sdb_gettime();
-       /* ignore errors -> last_update will be updated later */
+       sobj->last_update = va_arg(ap, sdb_time_t);
 
        sobj->parent = NULL;
        return 0;
@@ -117,13 +116,21 @@ store_obj_destroy(sdb_object_t *obj)
                sdb_object_deref(SDB_OBJ(sobj->parent));
 } /* store_obj_destroy */
 
+/* this may not be used as a type on its own but only as helper functions for
+ * the derived types; the function accepts a variadic list of arguments to
+ * pass to the sub-type's init function */
 static sdb_object_t *
-store_obj_clone(const sdb_object_t *obj)
+store_obj_clone(const sdb_object_t *obj, ...)
 {
        const store_obj_t *sobj = STORE_CONST_OBJ(obj);
        store_obj_t *new;
 
-       new = STORE_OBJ(sdb_object_create(obj->name, obj->type));
+       va_list ap;
+
+       va_start(ap, obj);
+
+       new = STORE_OBJ(sdb_object_vcreate(obj->name, obj->type, ap));
+       va_end(ap);
        if (! new)
                return NULL;
 
@@ -175,17 +182,12 @@ sdb_store_obj_clone(const sdb_object_t *obj)
        const sdb_store_obj_t *sobj = SDB_CONST_STORE_OBJ(obj);
        sdb_store_obj_t *new;
 
-       new = SDB_STORE_OBJ(store_obj_clone(obj));
+       new = SDB_STORE_OBJ(store_obj_clone(obj, sobj->_last_update, sobj->type));
        if (! new)
                return NULL;
 
-       new->type = sobj->type;
-
-       /* make sure these are initialized; else sdb_object_deref() might access
-        * arbitrary memory in case of an error */
-       new->children = new->attributes = NULL;
-
        if (sobj->children) {
+               sdb_llist_destroy(new->children);
                new->children = sdb_llist_clone(sobj->children);
                if (! new->children) {
                        sdb_object_deref(SDB_OBJ(new));
@@ -193,6 +195,7 @@ sdb_store_obj_clone(const sdb_object_t *obj)
                }
        }
        if (sobj->attributes) {
+               sdb_llist_destroy(new->attributes);
                new->attributes = sdb_llist_clone(sobj->attributes);
                if (! new->attributes) {
                        sdb_object_deref(SDB_OBJ(new));
@@ -200,19 +203,19 @@ sdb_store_obj_clone(const sdb_object_t *obj)
                }
        }
 
-       new->_last_update = sobj->_last_update;
        return SDB_OBJ(new);
 } /* sdb_store_obj_clone */
 
 static int
 sdb_attr_init(sdb_object_t *obj, va_list ap)
 {
-       const char *value = va_arg(ap, const char *);
+       const char *value;
        int ret;
 
        ret = store_obj_init(obj, ap);
        if (ret)
                return ret;
+       value = va_arg(ap, const char *);
 
        SDB_ATTR(obj)->value = strdup(value);
        if (! SDB_ATTR(obj)->value)
@@ -237,16 +240,9 @@ sdb_attr_clone(const sdb_object_t *obj)
        const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
        sdb_attribute_t *new;
 
-       new = SDB_ATTR(store_obj_clone(obj));
+       new = SDB_ATTR(store_obj_clone(obj, attr->_last_update, attr->value));
        if (! new)
                return NULL;
-
-       if (attr->value)
-               new->value = strdup(attr->value);
-       if (! new->value) {
-               sdb_object_deref(SDB_OBJ(new));
-               return NULL;
-       }
        return SDB_OBJ(new);
 } /* sdb_attr_clone */
 
@@ -370,7 +366,7 @@ store_obj(int parent_type, const char *parent_name,
        }
        else {
                store_obj_t *new = STORE_OBJ(sdb_object_create(name,
-                                       sdb_store_obj_type, type));
+                                       sdb_store_obj_type, last_update, type));
                if (! new) {
                        char errbuf[1024];
                        sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
@@ -469,7 +465,7 @@ sdb_store_attribute(const char *hostname, const char *key, const char *value,
        }
        else {
                sdb_attribute_t *new = SDB_ATTR(sdb_object_create(key,
-                                       sdb_attribute_type, value));
+                                       sdb_attribute_type, last_update, value));
                if (! new) {
                        char errbuf[1024];
                        sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "