Code

store: Use sdb_object_vcreate() when creating objects from super-class.
[sysdb.git] / src / core / store.c
index 1eb22d6c5e2c9be5e1942a711abc3126ce55fdfe..791a7602cbdd732266fee3105bc96c46941763b0 100644 (file)
@@ -117,13 +117,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,7 +183,7 @@ 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->type));
        if (! new)
                return NULL;
 
@@ -237,16 +245,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->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 */
 
@@ -313,7 +314,7 @@ store_obj(int parent_type, const char *parent_name,
                int type, const char *name, sdb_time_t last_update)
 {
        sdb_llist_t *parent_list;
-       sdb_store_obj_t *old;
+       store_obj_t *old;
        int status = 0;
 
        if (! name)
@@ -353,23 +354,23 @@ store_obj(int parent_type, const char *parent_name,
        }
 
        /* TODO: only look into direct children? */
-       old = sdb_store_lookup_in_list(parent_list, type, name);
+       old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
 
        if (old) {
-               if (old->_last_update > last_update) {
+               if (old->last_update > last_update) {
                        sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
                                        "value too old (%"PRIscTIME" < %"PRIscTIME")",
-                                       TYPE_TO_NAME(type), name, last_update, old->_last_update);
+                                       TYPE_TO_NAME(type), name, last_update, old->last_update);
                        /* don't report an error; the object may be updated by multiple
                         * backends */
                        status = 1;
                }
                else {
-                       old->_last_update = last_update;
+                       old->last_update = last_update;
                }
        }
        else {
-               sdb_store_obj_t *new = SDB_STORE_OBJ(sdb_object_create(name,
+               store_obj_t *new = STORE_OBJ(sdb_object_create(name,
                                        sdb_store_obj_type, type));
                if (! new) {
                        char errbuf[1024];
@@ -380,6 +381,7 @@ store_obj(int parent_type, const char *parent_name,
                        return -1;
                }
 
+               /* TODO: insert type-aware */
                status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
                                sdb_object_cmp_by_name);