Code

Merged branch 'master' of git://git.tokkee.org/sysdb.git.
[sysdb.git] / src / core / store.c
index 714a0b39ed18161b07b6bdd3bd856da335abd04d..4ec9f44f204fc5be018595cbc6a61ce950e7d6c2 100644 (file)
@@ -82,6 +82,10 @@ sdb_cmp_store_obj_with_name(const sdb_object_t *a, const sdb_object_t *b)
        return strcasecmp(obj->name, lookup->obj_name);
 } /* sdb_cmp_store_obj_with_name */
 
+/*
+ * public types
+ */
+
 static int
 sdb_host_init(sdb_object_t *obj, va_list ap)
 {
@@ -117,6 +121,39 @@ sdb_host_destroy(sdb_object_t *obj)
                sdb_llist_destroy(SDB_HOST(obj)->services);
 } /* sdb_host_destroy */
 
+static sdb_object_t *
+sdb_host_do_clone(const sdb_object_t *obj)
+{
+       const sdb_host_t *host = (const sdb_host_t *)obj;
+       sdb_host_t *new;
+
+       new = sdb_host_create(host->_name);
+       if (! new)
+               return NULL;
+
+       /* make sure these are initialized; else sdb_object_deref() might access
+        * arbitrary memory in case of an error */
+       new->services = new->attributes = NULL;
+
+       if (host->attributes) {
+               new->attributes = sdb_llist_clone(host->attributes);
+               if (! new->attributes) {
+                       sdb_object_deref(SDB_OBJ(new));
+                       return NULL;
+               }
+       }
+
+       new->_last_update = host->_last_update;
+       if (host->services) {
+               new->services = sdb_llist_clone(host->services);
+               if (! new->services) {
+                       sdb_object_deref(SDB_OBJ(new));
+                       return NULL;
+               }
+       }
+       return SDB_OBJ(new);
+} /* sdb_host_do_clone */
+
 static int
 sdb_attr_init(sdb_object_t *obj, va_list ap)
 {
@@ -148,6 +185,21 @@ sdb_attr_destroy(sdb_object_t *obj)
                free(SDB_ATTR(obj)->attr_value);
 } /* sdb_attr_destroy */
 
+static sdb_object_t *
+sdb_attr_clone(const sdb_object_t *obj)
+{
+       const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
+       sdb_attribute_t *new;
+
+       new = sdb_attribute_create(attr->hostname,
+                       attr->_name, attr->attr_value);
+       if (! new)
+               return NULL;
+
+       new->_last_update = attr->_last_update;
+       return SDB_OBJ(new);
+} /* sdb_attr_clone */
+
 static int
 sdb_svc_init(sdb_object_t *obj, va_list ap)
 {
@@ -175,6 +227,44 @@ sdb_svc_destroy(sdb_object_t *obj)
                free(SDB_SVC(obj)->_name);
 } /* sdb_svc_destroy */
 
+static sdb_object_t *
+sdb_svc_clone(const sdb_object_t *obj)
+{
+       const sdb_service_t *svc = (const sdb_service_t *)obj;
+       sdb_service_t *new;
+
+       new = sdb_service_create(svc->hostname, svc->_name);
+       if (! new)
+               return NULL;
+
+       new->_last_update = svc->_last_update;
+       return SDB_OBJ(new);
+} /* sdb_svc_clone */
+
+const sdb_type_t sdb_host_type = {
+       sizeof(sdb_host_t),
+
+       sdb_host_init,
+       sdb_host_destroy,
+       sdb_host_do_clone
+};
+
+const sdb_type_t sdb_attribute_type = {
+       sizeof(sdb_attribute_t),
+
+       sdb_attr_init,
+       sdb_attr_destroy,
+       sdb_attr_clone
+};
+
+const sdb_type_t sdb_service_type = {
+       sizeof(sdb_service_t),
+
+       sdb_svc_init,
+       sdb_svc_destroy,
+       sdb_svc_clone
+};
+
 /*
  * public API
  */
@@ -187,45 +277,12 @@ sdb_host_create(const char *name)
        if (! name)
                return NULL;
 
-       obj = sdb_object_create(sizeof(sdb_host_t), sdb_host_init,
-                       sdb_host_destroy, name);
+       obj = sdb_object_create(sdb_host_type, name);
        if (! obj)
                return NULL;
        return SDB_HOST(obj);
 } /* sdb_host_create */
 
-sdb_host_t *
-sdb_host_clone(const sdb_host_t *host)
-{
-       sdb_host_t *new;
-
-       new = sdb_host_create(host->_name);
-       if (! new)
-               return NULL;
-
-       /* make sure these are initialized; else sdb_object_deref() might access
-        * arbitrary memory in case of an error */
-       new->services = new->attributes = NULL;
-
-       if (host->attributes) {
-               new->attributes = sdb_llist_clone(host->attributes);
-               if (! new->attributes) {
-                       sdb_object_deref(SDB_OBJ(new));
-                       return NULL;
-               }
-       }
-
-       new->_last_update = host->_last_update;
-       if (host->services) {
-               new->services = sdb_llist_clone(host->services);
-               if (! new->services) {
-                       sdb_object_deref(SDB_OBJ(new));
-                       return NULL;
-               }
-       }
-       return new;
-} /* sdb_host_clone */
-
 int
 sdb_store_host(const sdb_host_t *host)
 {
@@ -269,7 +326,7 @@ sdb_store_host(const sdb_host_t *host)
                }
        }
        else {
-               sdb_host_t *new = sdb_host_clone(host);
+               sdb_host_t *new = SDB_HOST(sdb_object_clone(SDB_CONST_OBJ(host)));
                if (! new) {
                        char errbuf[1024];
                        sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s",
@@ -340,27 +397,12 @@ sdb_attribute_create(const char *hostname,
        if ((! hostname) || (! name) || (! value))
                return NULL;
 
-       obj = sdb_object_create(sizeof(sdb_attribute_t), sdb_attr_init,
-                       sdb_attr_destroy, hostname, name, value);
+       obj = sdb_object_create(sdb_attribute_type, hostname, name, value);
        if (! obj)
                return NULL;
        return SDB_ATTR(obj);
 } /* sdb_attribute_create */
 
-sdb_attribute_t *
-sdb_attribute_clone(const sdb_attribute_t *attr)
-{
-       sdb_attribute_t *new;
-
-       new = sdb_attribute_create(attr->hostname,
-                       attr->_name, attr->attr_value);
-       if (! new)
-               return NULL;
-
-       new->_last_update = attr->_last_update;
-       return new;
-} /* sdb_attribute_clone */
-
 int
 sdb_store_attribute(const sdb_attribute_t *attr)
 {
@@ -412,7 +454,7 @@ sdb_store_attribute(const sdb_attribute_t *attr)
                }
        }
        else {
-               sdb_attribute_t *new = sdb_attribute_clone(attr);
+               sdb_attribute_t *new = SDB_ATTR(sdb_object_clone(SDB_CONST_OBJ(attr)));
                if (! new) {
                        char errbuf[1024];
                        sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
@@ -440,26 +482,12 @@ sdb_service_create(const char *hostname, const char *name)
        if ((! hostname) || (! name))
                return NULL;
 
-       obj = sdb_object_create(sizeof(sdb_service_t), sdb_svc_init,
-                       sdb_svc_destroy, hostname, name);
+       obj = sdb_object_create(sdb_service_type, hostname, name);
        if (! obj)
                return NULL;
        return SDB_SVC(obj);
 } /* sdb_service_create */
 
-sdb_service_t *
-sdb_service_clone(const sdb_service_t *svc)
-{
-       sdb_service_t *new;
-
-       new = sdb_service_create(svc->hostname, svc->_name);
-       if (! new)
-               return NULL;
-
-       new->_last_update = svc->_last_update;
-       return new;
-} /* sdb_service_clone */
-
 int
 sdb_store_service(const sdb_service_t *svc)
 {
@@ -510,7 +538,7 @@ sdb_store_service(const sdb_service_t *svc)
                }
        }
        else {
-               sdb_service_t *new = sdb_service_clone(svc);
+               sdb_service_t *new = SDB_SVC(sdb_object_clone(SDB_CONST_OBJ(svc)));
                if (! new) {
                        char errbuf[1024];
                        sdb_log(SDB_LOG_ERR, "store: Failed to clone service "