Code

Simplified inherited attribute naming in store objects.
authorSebastian Harl <sh@tokkee.org>
Fri, 22 Mar 2013 05:26:37 +0000 (22:26 -0700)
committerSebastian Harl <sh@tokkee.org>
Fri, 22 Mar 2013 05:26:37 +0000 (22:26 -0700)
This way, we avoid name clashes with other variables that would also be
replaced by the preprocessor.

src/backend/collectd/unixsock.c
src/backend/mk-livestatus.c
src/backend/puppet/store-configs.c
src/core/store.c
src/include/core/store.h

index f9741fe0c943a64614a4b3bd21f24a393d5df0d9..78c238de39d44264f85ee9d4b7f01d739f19cde9 100644 (file)
@@ -69,8 +69,8 @@ sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
 
        strncpy(name, hostname, sizeof(name));
 
-       host.host_name = name;
-       host.host_last_update = last_update;
+       host._name = name;
+       host._last_update = last_update;
 
        status = sdb_store_host(&host);
 
@@ -102,8 +102,8 @@ sdb_collectd_add_svc(const char *hostname, const char *plugin,
        snprintf(name, sizeof(name), "%s/%s", plugin, type);
 
        svc.hostname = host;
-       svc.svc_name = name;
-       svc.svc_last_update = last_update;
+       svc._name = name;
+       svc._last_update = last_update;
 
        status = sdb_store_service(&svc);
        if (status < 0) {
index 6de35cbe55298dc53154dd2db5fb73f7b4498282..200edc289fc4379d7fadb73cf2c6e662d81005d2 100644 (file)
@@ -66,8 +66,8 @@ sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client,
        hostname  = strdup(data[0].data.string);
        timestamp = data[1].data.datetime;
 
-       host.host_name = hostname;
-       host.host_last_update = timestamp;
+       host._name = hostname;
+       host._last_update = timestamp;
 
        status = sdb_store_host(&host);
 
@@ -110,8 +110,8 @@ sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
        timestamp = data[2].data.datetime;
 
        svc.hostname = hostname;
-       svc.svc_name = svcname;
-       svc.svc_last_update = timestamp;
+       svc._name = svcname;
+       svc._last_update = timestamp;
 
        status = sdb_store_service(&svc);
 
index 8d7a8ebbfd7778c10726b9a22018a01a03287ab0..84855c65a380e64a47451524bb9f5c10158de24b 100644 (file)
@@ -60,22 +60,22 @@ sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
        assert((data[0].type == SDB_TYPE_STRING)
                        && (data[1].type == SDB_TYPE_DATETIME));
 
-       host.host_name = strdup(data[0].data.string);
-       host.host_last_update = data[1].data.datetime;
+       host._name = strdup(data[0].data.string);
+       host._last_update = data[1].data.datetime;
 
        status = sdb_store_host(&host);
 
        if (status < 0) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
-                               "store/update host '%s'.", host.host_name);
-               free(host.host_name);
+                               "store/update host '%s'.", host._name);
+               free(host._name);
                return -1;
        }
        else if (! status)
                sdb_log(SDB_LOG_DEBUG, "puppet::store-configs backend: "
                                "Added/updated host '%s' (last update timestamp = "
-                               "%"PRIscTIME").", host.host_name, host.host_last_update);
-       free(host.host_name);
+                               "%"PRIscTIME").", host._name, host._last_update);
+       free(host._name);
        return 0;
 } /* sdb_puppet_stcfg_get_hosts */
 
@@ -95,24 +95,24 @@ sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
                        && (data[3].type == SDB_TYPE_DATETIME));
 
        attr.hostname = strdup(data[0].data.string);
-       attr.attr_name = strdup(data[1].data.string);
+       attr._name = strdup(data[1].data.string);
        attr.attr_value = strdup(data[2].data.string);
-       attr.attr_last_update = data[3].data.datetime;
+       attr._last_update = data[3].data.datetime;
 
        status = sdb_store_attribute(&attr);
 
        if (status < 0) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
                                "store/update host attribute '%s' for host '%s'.",
-                               attr.attr_name, attr.hostname);
+                               attr._name, attr.hostname);
                free(attr.hostname);
-               free(attr.attr_name);
+               free(attr._name);
                free(attr.attr_value);
                return -1;
        }
 
        free(attr.hostname);
-       free(attr.attr_name);
+       free(attr._name);
        free(attr.attr_value);
        return 0;
 } /* sdb_puppet_stcfg_get_attrs */
index cd53922781dcd29e85b471c6cd7a3c414ffd1767..4e841bac0022040ca77cd5fedcfa55aab1f4e2a1 100644 (file)
@@ -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 */
 
 /*
@@ -199,7 +199,7 @@ sdb_host_clone(const sdb_host_t *host)
 {
        sdb_host_t *new;
 
-       new = sdb_host_create(host->host_name);
+       new = sdb_host_create(host->_name);
        if (! new)
                return NULL;
 
@@ -215,7 +215,7 @@ sdb_host_clone(const sdb_host_t *host)
                }
        }
 
-       new->host_last_update = host->host_last_update;
+       new->_last_update = host->_last_update;
        if (host->services) {
                new->services = sdb_llist_clone(host->services);
                if (! new->services) {
@@ -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,21 +251,21 @@ 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) {
+               if (old->_last_update > last_update) {
                        sdb_log(SDB_LOG_DEBUG, "store: Cannot update host '%s' - "
                                        "value too old (%"PRIscTIME" < %"PRIscTIME")",
-                                       host->host_name, last_update, old->host_last_update);
+                                       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 {
@@ -282,7 +282,7 @@ sdb_store_host(const sdb_host_t *host)
                        if (! (new->attributes = sdb_llist_create())) {
                                char errbuf[1024];
                                sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
-                                               "host object '%s': %s", host->host_name,
+                                               "host object '%s': %s", host->_name,
                                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                                sdb_object_deref(SDB_OBJ(new));
                                pthread_rwlock_unlock(&host_lock);
@@ -294,7 +294,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->host_name,
+                                               "host object '%s': %s", host->_name,
                                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                                sdb_object_deref(SDB_OBJ(new));
                                pthread_rwlock_unlock(&host_lock);
@@ -353,11 +353,11 @@ sdb_attribute_clone(const sdb_attribute_t *attr)
        sdb_attribute_t *new;
 
        new = sdb_attribute_create(attr->hostname,
-                       attr->attr_name, attr->attr_value);
+                       attr->_name, attr->attr_value);
        if (! new)
                return NULL;
 
-       new->attr_last_update = attr->attr_last_update;
+       new->_last_update = attr->_last_update;
        return new;
 } /* sdb_attribute_clone */
 
@@ -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,21 +394,21 @@ 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) {
+               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->attr_name, last_update,
-                                       old->host_last_update);
+                                       attr->hostname, attr->_name, last_update,
+                                       old->_last_update);
                        status = 1;
                }
                else {
-                       old->attr_last_update = last_update;
+                       old->_last_update = last_update;
                }
        }
        else {
@@ -452,11 +452,11 @@ sdb_service_clone(const sdb_service_t *svc)
 {
        sdb_service_t *new;
 
-       new = sdb_service_create(svc->hostname, svc->svc_name);
+       new = sdb_service_create(svc->hostname, svc->_name);
        if (! new)
                return NULL;
 
-       new->svc_last_update = svc->svc_last_update;
+       new->_last_update = svc->_last_update;
        return new;
 } /* sdb_service_clone */
 
@@ -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,20 +493,20 @@ 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) {
+               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->svc_name, last_update,
-                                       old->host_last_update);
+                                       svc->hostname, svc->_name, last_update,
+                                       old->_last_update);
                        status = 1;
                }
                else {
-                       old->svc_last_update = last_update;
+                       old->_last_update = last_update;
                }
        }
        else {
@@ -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);
index 3c2b7a8e201d21eecf08da5836696844f8e3c184..46cd53643a7ec8b1d63a5431d4fde5dc3b94c2fb 100644 (file)
@@ -50,8 +50,6 @@ typedef struct {
 
 typedef struct {
        sdb_store_obj_t parent;
-#define svc_last_update parent.last_update
-#define svc_name parent.name
 
        char *hostname;
 } sdb_service_t;
@@ -60,8 +58,6 @@ typedef struct {
 
 typedef struct {
        sdb_store_obj_t parent;
-#define attr_last_update parent.last_update
-#define attr_name parent.name
 
        char *attr_value;
        char *hostname;
@@ -71,8 +67,6 @@ typedef struct {
 
 typedef struct {
        sdb_store_obj_t parent;
-#define host_last_update parent.last_update
-#define host_name parent.name
 
        sdb_llist_t *attributes;
        sdb_llist_t *services;
@@ -80,6 +74,11 @@ typedef struct {
 #define SDB_HOST_INIT { SDB_STORE_OBJ_INIT, NULL, NULL }
 #define SDB_HOST(obj) ((sdb_host_t *)(obj))
 
+/* shortcuts for accessing the sdb_store_obj_t attributes of inheriting
+ * objects */
+#define _last_update parent.last_update
+#define _name parent.name
+
 sdb_host_t *
 sdb_host_create(const char *name);