Code

store: Removed unused sdb_host_create() function.
[sysdb.git] / src / core / store.c
index 4e6eb4804aad042621dd267f642f4c8a445014b8..92f3e510b9bccd6c57076bfbbf920c09c9754fbf 100644 (file)
@@ -27,7 +27,8 @@
 
 #include "sysdb.h"
 #include "core/store.h"
-#include "utils/error.h"
+#include "core/error.h"
+#include "core/plugin.h"
 #include "utils/llist.h"
 
 #include <assert.h>
 
 #include <pthread.h>
 
-/*
- * private data types
- */
-
-/* type used for looking up named objects */
-typedef struct {
-       sdb_object_t parent;
-       const char *obj_name;
-} sdb_store_lookup_obj_t;
-#define SDB_STORE_LOOKUP_OBJ_INIT { SDB_OBJECT_INIT, NULL }
-
 /*
  * private variables
  */
@@ -59,42 +49,12 @@ static sdb_llist_t *host_list = NULL;
 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
 
 /*
- * private helper functions
+ * public types
  */
 
 static int
-sdb_store_obj_cmp_by_name(const sdb_object_t *a, const sdb_object_t *b)
-{
-       const sdb_store_obj_t *h1 = (const sdb_store_obj_t *)a;
-       const sdb_store_obj_t *h2 = (const sdb_store_obj_t *)b;
-
-       assert(h1 && h2);
-       return strcasecmp(h1->name, h2->name);
-} /* sdb_store_obj_cmp_by_name */
-
-static int
-sdb_cmp_store_obj_with_name(const sdb_object_t *a, const sdb_object_t *b)
+sdb_host_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
 {
-       const sdb_store_obj_t *obj = (const sdb_store_obj_t *)a;
-       const sdb_store_lookup_obj_t *lookup = (const sdb_store_lookup_obj_t *)b;
-
-       assert(obj && lookup);
-       return strcasecmp(obj->name, lookup->obj_name);
-} /* sdb_cmp_store_obj_with_name */
-
-/*
- * private types
- */
-
-static int
-sdb_host_init(sdb_object_t *obj, va_list ap)
-{
-       const char *name = va_arg(ap, const char *);
-
-       SDB_HOST(obj)->_name = strdup(name);
-       if (! SDB_HOST(obj)->_name)
-               return -1;
-
        SDB_HOST(obj)->_last_update = sdb_gettime();
        /* ignore errors -> last_update will be updated later */
 
@@ -112,9 +72,6 @@ sdb_host_destroy(sdb_object_t *obj)
 {
        assert(obj);
 
-       if (SDB_HOST(obj)->_name)
-               free(SDB_HOST(obj)->_name);
-
        if (SDB_HOST(obj)->attributes)
                sdb_llist_destroy(SDB_HOST(obj)->attributes);
        if (SDB_HOST(obj)->services)
@@ -127,7 +84,7 @@ 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);
+       new = SDB_HOST(sdb_object_create(obj->name, sdb_host_type));
        if (! new)
                return NULL;
 
@@ -158,14 +115,11 @@ static int
 sdb_attr_init(sdb_object_t *obj, va_list ap)
 {
        const char *hostname = va_arg(ap, const char *);
-       const char *name = va_arg(ap, const char *);
        const char *value = va_arg(ap, const char *);
 
        SDB_ATTR(obj)->hostname = strdup(hostname);
-       SDB_ATTR(obj)->_name = strdup(name);
        SDB_ATTR(obj)->attr_value = strdup(value);
-       if ((! SDB_ATTR(obj)->hostname)
-                       || (! SDB_ATTR(obj)->_name) || (! SDB_ATTR(obj)->attr_value))
+       if ((! SDB_ATTR(obj)->hostname) || (! SDB_ATTR(obj)->attr_value))
                return -1;
 
        SDB_ATTR(obj)->_last_update = sdb_gettime();
@@ -179,8 +133,6 @@ sdb_attr_destroy(sdb_object_t *obj)
 
        if (SDB_ATTR(obj)->hostname)
                free(SDB_ATTR(obj)->hostname);
-       if (SDB_ATTR(obj)->_name)
-               free(SDB_ATTR(obj)->_name);
        if (SDB_ATTR(obj)->attr_value)
                free(SDB_ATTR(obj)->attr_value);
 } /* sdb_attr_destroy */
@@ -192,7 +144,7 @@ sdb_attr_clone(const sdb_object_t *obj)
        sdb_attribute_t *new;
 
        new = sdb_attribute_create(attr->hostname,
-                       attr->_name, attr->attr_value);
+                       obj->name, attr->attr_value);
        if (! new)
                return NULL;
 
@@ -204,11 +156,9 @@ static int
 sdb_svc_init(sdb_object_t *obj, va_list ap)
 {
        const char *hostname = va_arg(ap, const char *);
-       const char *name = va_arg(ap, const char *);
 
        SDB_SVC(obj)->hostname = strdup(hostname);
-       SDB_SVC(obj)->_name = strdup(name);
-       if ((! SDB_SVC(obj)->hostname) || (! SDB_SVC(obj)->_name))
+       if (! SDB_SVC(obj)->hostname)
                return -1;
 
        SDB_SVC(obj)->_last_update = sdb_gettime();
@@ -223,8 +173,6 @@ sdb_svc_destroy(sdb_object_t *obj)
 
        if (SDB_SVC(obj)->hostname)
                free(SDB_SVC(obj)->hostname);
-       if (SDB_SVC(obj)->_name)
-               free(SDB_SVC(obj)->_name);
 } /* sdb_svc_destroy */
 
 static sdb_object_t *
@@ -233,7 +181,7 @@ 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);
+       new = sdb_service_create(svc->hostname, obj->name);
        if (! new)
                return NULL;
 
@@ -241,7 +189,7 @@ sdb_svc_clone(const sdb_object_t *obj)
        return SDB_OBJ(new);
 } /* sdb_svc_clone */
 
-static sdb_type_t sdb_host_type = {
+const sdb_type_t sdb_host_type = {
        sizeof(sdb_host_t),
 
        sdb_host_init,
@@ -249,7 +197,7 @@ static sdb_type_t sdb_host_type = {
        sdb_host_do_clone
 };
 
-static sdb_type_t sdb_attr_type = {
+const sdb_type_t sdb_attribute_type = {
        sizeof(sdb_attribute_t),
 
        sdb_attr_init,
@@ -257,7 +205,7 @@ static sdb_type_t sdb_attr_type = {
        sdb_attr_clone
 };
 
-static sdb_type_t sdb_svc_type = {
+const sdb_type_t sdb_service_type = {
        sizeof(sdb_service_t),
 
        sdb_svc_init,
@@ -269,32 +217,25 @@ static sdb_type_t sdb_svc_type = {
  * public API
  */
 
-sdb_host_t *
-sdb_host_create(const char *name)
-{
-       sdb_object_t *obj;
-
-       if (! name)
-               return NULL;
-
-       obj = sdb_object_create(sdb_host_type, name);
-       if (! obj)
-               return NULL;
-       return SDB_HOST(obj);
-} /* sdb_host_create */
-
 int
 sdb_store_host(const sdb_host_t *host)
 {
-       sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
-       sdb_time_t last_update;
+       char *cname;
 
+       sdb_time_t last_update;
        sdb_host_t *old;
+
        int status = 0;
 
-       if ((! host) || (! host->_name))
+       if ((! host) || (! SDB_CONST_OBJ(host)->name))
                return -1;
 
+       cname = sdb_plugin_cname(strdup(SDB_CONST_OBJ(host)->name));
+       if (! cname) {
+               sdb_log(SDB_LOG_ERR, "store: strdup failed");
+               return -1;
+       }
+
        last_update = host->_last_update;
        if (last_update <= 0)
                last_update = sdb_gettime();
@@ -308,15 +249,13 @@ sdb_store_host(const sdb_host_t *host)
                }
        }
 
-       lookup.obj_name = host->_name;
-       old = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup,
-                               sdb_cmp_store_obj_with_name));
+       old = SDB_HOST(sdb_llist_search_by_name(host_list, cname));
 
        if (old) {
                if (old->_last_update > last_update) {
                        sdb_log(SDB_LOG_DEBUG, "store: Cannot update host '%s' - "
                                        "value too old (%"PRIscTIME" < %"PRIscTIME")",
-                                       host->_name, last_update, old->_last_update);
+                                       cname, last_update, old->_last_update);
                        /* don't report an error; the host may be updated by multiple
                         * backends */
                        status = 1;
@@ -326,7 +265,7 @@ sdb_store_host(const sdb_host_t *host)
                }
        }
        else {
-               sdb_host_t *new = SDB_HOST(sdb_host_do_clone(SDB_CONST_OBJ(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",
@@ -335,11 +274,14 @@ sdb_store_host(const sdb_host_t *host)
                        return -1;
                }
 
+               free(SDB_OBJ(new)->name);
+               SDB_OBJ(new)->name = cname;
+
                if (! new->attributes) {
                        if (! (new->attributes = sdb_llist_create())) {
                                char errbuf[1024];
                                sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
-                                               "host object '%s': %s", host->_name,
+                                               "host object '%s': %s", SDB_OBJ(new)->name,
                                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                                sdb_object_deref(SDB_OBJ(new));
                                pthread_rwlock_unlock(&host_lock);
@@ -351,7 +293,7 @@ sdb_store_host(const sdb_host_t *host)
                        if (! (new->services = sdb_llist_create())) {
                                char errbuf[1024];
                                sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
-                                               "host object '%s': %s", host->_name,
+                                               "host object '%s': %s", SDB_OBJ(new)->name,
                                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                                sdb_object_deref(SDB_OBJ(new));
                                pthread_rwlock_unlock(&host_lock);
@@ -360,7 +302,7 @@ sdb_store_host(const sdb_host_t *host)
                }
 
                status = sdb_llist_insert_sorted(host_list, SDB_OBJ(new),
-                               sdb_store_obj_cmp_by_name);
+                               sdb_object_cmp_by_name);
 
                /* pass control to the list or destroy in case of an error */
                sdb_object_deref(SDB_OBJ(new));
@@ -373,16 +315,12 @@ sdb_store_host(const sdb_host_t *host)
 const sdb_host_t *
 sdb_store_get_host(const char *name)
 {
-       sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
        sdb_host_t *host;
 
        if (! name)
                return NULL;
 
-       lookup.obj_name = name;
-       host = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup,
-                               sdb_cmp_store_obj_with_name));
-
+       host = SDB_HOST(sdb_llist_search_by_name(host_list, name));
        if (! host)
                return NULL;
        return host;
@@ -397,7 +335,7 @@ sdb_attribute_create(const char *hostname,
        if ((! hostname) || (! name) || (! value))
                return NULL;
 
-       obj = sdb_object_create(sdb_attr_type, hostname, name, value);
+       obj = sdb_object_create(name, sdb_attribute_type, hostname, value);
        if (! obj)
                return NULL;
        return SDB_ATTR(obj);
@@ -406,11 +344,8 @@ sdb_attribute_create(const char *hostname,
 int
 sdb_store_attribute(const sdb_attribute_t *attr)
 {
-       sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
        sdb_host_t *host;
-
        sdb_attribute_t *old;
-
        sdb_time_t last_update;
 
        int status = 0;
@@ -427,25 +362,19 @@ sdb_store_attribute(const sdb_attribute_t *attr)
 
        pthread_rwlock_wrlock(&host_lock);
 
-       lookup.obj_name = attr->hostname;
-       host = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup,
-                               sdb_cmp_store_obj_with_name));
-
+       host = SDB_HOST(sdb_llist_search_by_name(host_list, attr->hostname));
        if (! host) {
                pthread_rwlock_unlock(&host_lock);
                return -1;
        }
 
-       lookup.obj_name = attr->_name;
-       old = SDB_ATTR(sdb_llist_search(host->attributes,
-                               (const sdb_object_t *)&lookup,
-                               sdb_cmp_store_obj_with_name));
-
+       old = SDB_ATTR(sdb_llist_search_by_name(host->attributes,
+                               SDB_CONST_OBJ(attr)->name));
        if (old) {
                if (old->_last_update > last_update) {
                        sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute "
                                        "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
-                                       attr->hostname, attr->_name, last_update,
+                                       attr->hostname, SDB_CONST_OBJ(attr)->name, last_update,
                                        old->_last_update);
                        status = 1;
                }
@@ -454,7 +383,7 @@ sdb_store_attribute(const sdb_attribute_t *attr)
                }
        }
        else {
-               sdb_attribute_t *new = SDB_ATTR(sdb_attr_clone(SDB_CONST_OBJ(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 "
@@ -464,7 +393,7 @@ sdb_store_attribute(const sdb_attribute_t *attr)
                }
 
                status = sdb_llist_insert_sorted(host->attributes, SDB_OBJ(new),
-                               sdb_store_obj_cmp_by_name);
+                               sdb_object_cmp_by_name);
 
                /* pass control to the list or destroy in case of an error */
                sdb_object_deref(SDB_OBJ(new));
@@ -482,7 +411,7 @@ sdb_service_create(const char *hostname, const char *name)
        if ((! hostname) || (! name))
                return NULL;
 
-       obj = sdb_object_create(sdb_svc_type, hostname, name);
+       obj = sdb_object_create(name, sdb_service_type, hostname);
        if (! obj)
                return NULL;
        return SDB_SVC(obj);
@@ -491,11 +420,8 @@ sdb_service_create(const char *hostname, const char *name)
 int
 sdb_store_service(const sdb_service_t *svc)
 {
-       sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
        sdb_host_t *host;
-
        sdb_service_t *old;
-
        sdb_time_t last_update;
 
        int status = 0;
@@ -512,24 +438,19 @@ sdb_store_service(const sdb_service_t *svc)
 
        pthread_rwlock_wrlock(&host_lock);
 
-       lookup.obj_name = svc->hostname;
-       host = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup,
-                               sdb_cmp_store_obj_with_name));
-
+       host = SDB_HOST(sdb_llist_search_by_name(host_list, svc->hostname));
        if (! host) {
                pthread_rwlock_unlock(&host_lock);
                return -1;
        }
 
-       lookup.obj_name = svc->_name;
-       old = SDB_SVC(sdb_llist_search(host->services, (const sdb_object_t *)&lookup,
-                               sdb_cmp_store_obj_with_name));
-
+       old = SDB_SVC(sdb_llist_search_by_name(host->services,
+                               SDB_CONST_OBJ(svc)->name));
        if (old) {
                if (old->_last_update > last_update) {
                        sdb_log(SDB_LOG_DEBUG, "store: Cannot update service "
                                        "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
-                                       svc->hostname, svc->_name, last_update,
+                                       svc->hostname, SDB_CONST_OBJ(svc)->name, last_update,
                                        old->_last_update);
                        status = 1;
                }
@@ -538,7 +459,7 @@ sdb_store_service(const sdb_service_t *svc)
                }
        }
        else {
-               sdb_service_t *new = SDB_SVC(sdb_svc_clone(SDB_CONST_OBJ(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 "
@@ -548,7 +469,7 @@ sdb_store_service(const sdb_service_t *svc)
                }
 
                status = sdb_llist_insert_sorted(host->services, SDB_OBJ(new),
-                               sdb_store_obj_cmp_by_name);
+                               sdb_object_cmp_by_name);
 
                /* pass control to the list or destroy in case of an error */
                sdb_object_deref(SDB_OBJ(new));
@@ -561,17 +482,12 @@ sdb_store_service(const sdb_service_t *svc)
 const sdb_service_t *
 sdb_store_get_service(const sdb_host_t *host, const char *name)
 {
-       sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
        sdb_service_t *svc;
 
        if ((! host) || (! name))
                return NULL;
 
-       lookup.obj_name = name;
-       svc = SDB_SVC(sdb_llist_search(host->services,
-                               (const sdb_object_t *)&lookup,
-                               sdb_cmp_store_obj_with_name));
-
+       svc = SDB_SVC(sdb_llist_search_by_name(host->services, name));
        if (! svc)
                return NULL;
        return svc;
@@ -608,7 +524,7 @@ sdb_store_dump(FILE *fh)
                time_str[sizeof(time_str) - 1] = '\0';
 
                fprintf(fh, "Host '%s' (last updated: %s):\n",
-                               host->_name, time_str);
+                               SDB_OBJ(host)->name, time_str);
 
                attr_iter = sdb_llist_get_iter(host->attributes);
                if (! attr_iter) {
@@ -628,7 +544,7 @@ sdb_store_dump(FILE *fh)
                        time_str[sizeof(time_str) - 1] = '\0';
 
                        fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
-                                       attr->_name, attr->attr_value, time_str);
+                                       SDB_OBJ(attr)->name, attr->attr_value, time_str);
                }
 
                sdb_llist_iter_destroy(attr_iter);
@@ -651,7 +567,7 @@ sdb_store_dump(FILE *fh)
                        time_str[sizeof(time_str) - 1] = '\0';
 
                        fprintf(fh, "\tService '%s' (last updated: %s)\n",
-                                       svc->_name, time_str);
+                                       SDB_OBJ(svc)->name, time_str);
                }
 
                sdb_llist_iter_destroy(svc_iter);