Code

Simplified inherited attribute naming in store objects.
[sysdb.git] / src / core / store.c
index e5cd886aae31be9bb1d35be92e9f42b7d729e4b5..4e841bac0022040ca77cd5fedcfa55aab1f4e2a1 100644 (file)
@@ -27,8 +27,8 @@
 
 #include "sysdb.h"
 #include "core/store.h"
+#include "utils/error.h"
 #include "utils/llist.h"
-#include "utils/string.h"
 
 #include <assert.h>
 
@@ -87,11 +87,11 @@ sdb_host_init(sdb_object_t *obj, va_list ap)
 {
        const char *name = va_arg(ap, const char *);
 
-       SDB_HOST(obj)->host_name = strdup(name);
-       if (! SDB_HOST(obj)->host_name)
+       SDB_HOST(obj)->_name = strdup(name);
+       if (! SDB_HOST(obj)->_name)
                return -1;
 
-       SDB_HOST(obj)->host_last_update = sdb_gettime();
+       SDB_HOST(obj)->_last_update = sdb_gettime();
        /* ignore errors -> last_update will be updated later */
 
        SDB_HOST(obj)->attributes = sdb_llist_create();
@@ -108,8 +108,8 @@ sdb_host_destroy(sdb_object_t *obj)
 {
        assert(obj);
 
-       if (SDB_HOST(obj)->host_name)
-               free(SDB_HOST(obj)->host_name);
+       if (SDB_HOST(obj)->_name)
+               free(SDB_HOST(obj)->_name);
 
        if (SDB_HOST(obj)->attributes)
                sdb_llist_destroy(SDB_HOST(obj)->attributes);
@@ -125,13 +125,13 @@ sdb_attr_init(sdb_object_t *obj, va_list ap)
        const char *value = va_arg(ap, const char *);
 
        SDB_ATTR(obj)->hostname = strdup(hostname);
-       SDB_ATTR(obj)->attr_name = strdup(name);
+       SDB_ATTR(obj)->_name = strdup(name);
        SDB_ATTR(obj)->attr_value = strdup(value);
        if ((! SDB_ATTR(obj)->hostname)
-                       || (! SDB_ATTR(obj)->attr_name) || (! SDB_ATTR(obj)->attr_value))
+                       || (! SDB_ATTR(obj)->_name) || (! SDB_ATTR(obj)->attr_value))
                return -1;
 
-       SDB_ATTR(obj)->attr_last_update = sdb_gettime();
+       SDB_ATTR(obj)->_last_update = sdb_gettime();
        return 0;
 } /* sdb_attr_init */
 
@@ -142,8 +142,8 @@ sdb_attr_destroy(sdb_object_t *obj)
 
        if (SDB_ATTR(obj)->hostname)
                free(SDB_ATTR(obj)->hostname);
-       if (SDB_ATTR(obj)->attr_name)
-               free(SDB_ATTR(obj)->attr_name);
+       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 */
@@ -155,11 +155,11 @@ sdb_svc_init(sdb_object_t *obj, va_list ap)
        const char *name = va_arg(ap, const char *);
 
        SDB_SVC(obj)->hostname = strdup(hostname);
-       SDB_SVC(obj)->svc_name = strdup(name);
-       if ((! SDB_SVC(obj)->hostname) || (! SDB_SVC(obj)->svc_name))
+       SDB_SVC(obj)->_name = strdup(name);
+       if ((! SDB_SVC(obj)->hostname) || (! SDB_SVC(obj)->_name))
                return -1;
 
-       SDB_SVC(obj)->svc_last_update = sdb_gettime();
+       SDB_SVC(obj)->_last_update = sdb_gettime();
        /* ignore errors -> last_update will be updated later */
        return 0;
 } /* sdb_svc_init */
@@ -171,8 +171,8 @@ sdb_svc_destroy(sdb_object_t *obj)
 
        if (SDB_SVC(obj)->hostname)
                free(SDB_SVC(obj)->hostname);
-       if (SDB_SVC(obj)->svc_name)
-               free(SDB_SVC(obj)->svc_name);
+       if (SDB_SVC(obj)->_name)
+               free(SDB_SVC(obj)->_name);
 } /* sdb_svc_destroy */
 
 /*
@@ -197,33 +197,33 @@ sdb_host_create(const char *name)
 sdb_host_t *
 sdb_host_clone(const sdb_host_t *host)
 {
-       sdb_host_t *clone;
+       sdb_host_t *new;
 
-       clone = sdb_host_create(host->host_name);
-       if (! clone)
+       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 */
-       clone->services = clone->attributes = NULL;
+       new->services = new->attributes = NULL;
 
        if (host->attributes) {
-               clone->attributes = sdb_llist_clone(host->attributes);
-               if (! clone->attributes) {
-                       sdb_object_deref(SDB_OBJ(clone));
+               new->attributes = sdb_llist_clone(host->attributes);
+               if (! new->attributes) {
+                       sdb_object_deref(SDB_OBJ(new));
                        return NULL;
                }
        }
 
-       clone->host_last_update = host->host_last_update;
+       new->_last_update = host->_last_update;
        if (host->services) {
-               clone->services = sdb_llist_clone(host->services);
-               if (! clone->services) {
-                       sdb_object_deref(SDB_OBJ(clone));
+               new->services = sdb_llist_clone(host->services);
+               if (! new->services) {
+                       sdb_object_deref(SDB_OBJ(new));
                        return NULL;
                }
        }
-       return clone;
+       return new;
 } /* sdb_host_clone */
 
 int
@@ -235,10 +235,10 @@ sdb_store_host(const sdb_host_t *host)
        sdb_host_t *old;
        int status = 0;
 
-       if ((! host) || (! host->host_name))
+       if ((! host) || (! host->_name))
                return -1;
 
-       last_update = host->host_last_update;
+       last_update = host->_last_update;
        if (last_update <= 0)
                last_update = sdb_gettime();
 
@@ -251,28 +251,28 @@ sdb_store_host(const sdb_host_t *host)
                }
        }
 
-       lookup.obj_name = host->host_name;
+       lookup.obj_name = host->_name;
        old = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup,
                                sdb_cmp_store_obj_with_name));
 
        if (old) {
-               if (old->host_last_update > last_update) {
-                       fprintf(stderr, "store: Cannot update host '%s' - "
-                                       "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
-                                       host->host_name, last_update, old->host_last_update);
+               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);
                        /* don't report an error; the host may be updated by multiple
                         * backends */
                        status = 1;
                }
                else {
-                       old->host_last_update = last_update;
+                       old->_last_update = last_update;
                }
        }
        else {
                sdb_host_t *new = sdb_host_clone(host);
                if (! new) {
                        char errbuf[1024];
-                       fprintf(stderr, "store: Failed to clone host object: %s\n",
+                       sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s",
                                        sdb_strerror(errno, errbuf, sizeof(errbuf)));
                        pthread_rwlock_unlock(&host_lock);
                        return -1;
@@ -281,8 +281,8 @@ sdb_store_host(const sdb_host_t *host)
                if (! new->attributes) {
                        if (! (new->attributes = sdb_llist_create())) {
                                char errbuf[1024];
-                               fprintf(stderr, "store: Failed to initialize "
-                                               "host object '%s': %s\n", host->host_name,
+                               sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
+                                               "host object '%s': %s", host->_name,
                                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                                sdb_object_deref(SDB_OBJ(new));
                                pthread_rwlock_unlock(&host_lock);
@@ -293,8 +293,8 @@ sdb_store_host(const sdb_host_t *host)
                if (! new->services) {
                        if (! (new->services = sdb_llist_create())) {
                                char errbuf[1024];
-                               fprintf(stderr, "store: Failed to initialize "
-                                               "host object '%s': %s\n", host->host_name,
+                               sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
+                                               "host object '%s': %s", host->_name,
                                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                                sdb_object_deref(SDB_OBJ(new));
                                pthread_rwlock_unlock(&host_lock);
@@ -350,15 +350,15 @@ sdb_attribute_create(const char *hostname,
 sdb_attribute_t *
 sdb_attribute_clone(const sdb_attribute_t *attr)
 {
-       sdb_attribute_t *clone;
+       sdb_attribute_t *new;
 
-       clone = sdb_attribute_create(attr->hostname,
-                       attr->attr_name, attr->attr_value);
-       if (! clone)
+       new = sdb_attribute_create(attr->hostname,
+                       attr->_name, attr->attr_value);
+       if (! new)
                return NULL;
 
-       clone->attr_last_update = attr->attr_last_update;
-       return clone;
+       new->_last_update = attr->_last_update;
+       return new;
 } /* sdb_attribute_clone */
 
 int
@@ -376,7 +376,7 @@ sdb_store_attribute(const sdb_attribute_t *attr)
        if (! attr)
                return -1;
 
-       last_update = attr->attr_last_update;
+       last_update = attr->_last_update;
        if (last_update <= 0)
                last_update = sdb_gettime();
 
@@ -394,29 +394,29 @@ sdb_store_attribute(const sdb_attribute_t *attr)
                return -1;
        }
 
-       lookup.obj_name = attr->attr_name;
+       lookup.obj_name = attr->_name;
        old = SDB_ATTR(sdb_llist_search(host->attributes,
                                (const sdb_object_t *)&lookup,
                                sdb_cmp_store_obj_with_name));
 
        if (old) {
-               if (old->host_last_update > last_update) {
-                       fprintf(stderr, "store: Cannot update attribute '%s/%s' - "
-                                       "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
-                                       attr->hostname, attr->attr_name, last_update,
-                                       old->host_last_update);
+               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,
+                                       old->_last_update);
                        status = 1;
                }
                else {
-                       old->attr_last_update = last_update;
+                       old->_last_update = last_update;
                }
        }
        else {
                sdb_attribute_t *new = sdb_attribute_clone(attr);
                if (! new) {
                        char errbuf[1024];
-                       fprintf(stderr, "store: Failed to clone attribute object: %s\n",
-                                       sdb_strerror(errno, errbuf, sizeof(errbuf)));
+                       sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
+                                       "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
                        pthread_rwlock_unlock(&host_lock);
                        return -1;
                }
@@ -450,14 +450,14 @@ sdb_service_create(const char *hostname, const char *name)
 sdb_service_t *
 sdb_service_clone(const sdb_service_t *svc)
 {
-       sdb_service_t *clone;
+       sdb_service_t *new;
 
-       clone = sdb_service_create(svc->hostname, svc->svc_name);
-       if (! clone)
+       new = sdb_service_create(svc->hostname, svc->_name);
+       if (! new)
                return NULL;
 
-       clone->svc_last_update = svc->svc_last_update;
-       return clone;
+       new->_last_update = svc->_last_update;
+       return new;
 } /* sdb_service_clone */
 
 int
@@ -475,7 +475,7 @@ sdb_store_service(const sdb_service_t *svc)
        if (! svc)
                return -1;
 
-       last_update = svc->svc_last_update;
+       last_update = svc->_last_update;
        if (last_update <= 0)
                last_update = sdb_gettime();
 
@@ -493,28 +493,28 @@ sdb_store_service(const sdb_service_t *svc)
                return -1;
        }
 
-       lookup.obj_name = svc->svc_name;
+       lookup.obj_name = svc->_name;
        old = SDB_SVC(sdb_llist_search(host->services, (const sdb_object_t *)&lookup,
                                sdb_cmp_store_obj_with_name));
 
        if (old) {
-               if (old->host_last_update > last_update) {
-                       fprintf(stderr, "store: Cannot update service '%s/%s' - "
-                                       "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
-                                       svc->hostname, svc->svc_name, last_update,
-                                       old->host_last_update);
+               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,
+                                       old->_last_update);
                        status = 1;
                }
                else {
-                       old->svc_last_update = last_update;
+                       old->_last_update = last_update;
                }
        }
        else {
                sdb_service_t *new = sdb_service_clone(svc);
                if (! new) {
                        char errbuf[1024];
-                       fprintf(stderr, "store: Failed to clone service object: %s\n",
-                                       sdb_strerror(errno, errbuf, sizeof(errbuf)));
+                       sdb_log(SDB_LOG_ERR, "store: Failed to clone service "
+                                       "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
                        pthread_rwlock_unlock(&host_lock);
                        return -1;
                }
@@ -575,12 +575,12 @@ sdb_store_dump(FILE *fh)
                assert(host);
 
                if (! sdb_strftime(time_str, sizeof(time_str),
-                                       "%F %T %z", host->host_last_update))
+                                       "%F %T %z", host->_last_update))
                        snprintf(time_str, sizeof(time_str), "<error>");
                time_str[sizeof(time_str) - 1] = '\0';
 
                fprintf(fh, "Host '%s' (last updated: %s):\n",
-                               host->host_name, time_str);
+                               host->_name, time_str);
 
                attr_iter = sdb_llist_get_iter(host->attributes);
                if (! attr_iter) {
@@ -595,12 +595,12 @@ sdb_store_dump(FILE *fh)
                        assert(attr);
 
                        if (! sdb_strftime(time_str, sizeof(time_str),
-                                               "%F %T %z", attr->attr_last_update))
+                                               "%F %T %z", attr->_last_update))
                                snprintf(time_str, sizeof(time_str), "<error>");
                        time_str[sizeof(time_str) - 1] = '\0';
 
                        fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
-                                       attr->attr_name, attr->attr_value, time_str);
+                                       attr->_name, attr->attr_value, time_str);
                }
 
                sdb_llist_iter_destroy(attr_iter);
@@ -618,12 +618,12 @@ sdb_store_dump(FILE *fh)
                        assert(svc);
 
                        if (! sdb_strftime(time_str, sizeof(time_str),
-                                               "%F %T %z", svc->svc_last_update))
+                                               "%F %T %z", svc->_last_update))
                                snprintf(time_str, sizeof(time_str), "<error>");
                        time_str[sizeof(time_str) - 1] = '\0';
 
                        fprintf(fh, "\tService '%s' (last updated: %s)\n",
-                                       svc->svc_name, time_str);
+                                       svc->_name, time_str);
                }
 
                sdb_llist_iter_destroy(svc_iter);