From: Sebastian Harl Date: Wed, 24 Jul 2013 01:42:04 +0000 (-0700) Subject: store: Simplified sdb_store_host(). X-Git-Tag: sysdb-0.1.0~394 X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=commitdiff_plain;h=85d24495a23b26c7a1af8f21a122dc304a70f54b store: Simplified sdb_store_host(). Let the function accept constant arguments describing the hostname and last update timestamp rather than a host object. This is more flexible for the caller. --- diff --git a/src/backend/collectd/unixsock.c b/src/backend/collectd/unixsock.c index c9b46db..7e4f73a 100644 --- a/src/backend/collectd/unixsock.c +++ b/src/backend/collectd/unixsock.c @@ -62,21 +62,13 @@ typedef struct { static int sdb_collectd_add_host(const char *hostname, sdb_time_t last_update) { - sdb_host_t host = SDB_HOST_INIT; - char name[strlen(hostname) + 1]; - int status; - strncpy(name, hostname, sizeof(name)); - - SDB_OBJ(&host)->name = name; - host._last_update = last_update; - - status = sdb_store_host(&host); + status = sdb_store_host(hostname, last_update); if (status < 0) { sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to " - "store/update host '%s'.", name); + "store/update host '%s'.", hostname); return -1; } else if (status > 0) /* value too old */ @@ -84,7 +76,7 @@ sdb_collectd_add_host(const char *hostname, sdb_time_t last_update) sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated " "host '%s' (last update timestamp = %"PRIscTIME").", - name, last_update); + hostname, last_update); return 0; } /* sdb_collectd_add_host */ diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index dea6794..da8c99c 100644 --- a/src/backend/mk-livestatus.c +++ b/src/backend/mk-livestatus.c @@ -52,10 +52,8 @@ sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client, size_t n, sdb_data_t *data, sdb_object_t __attribute__((unused)) *user_data) { - char *hostname = NULL; - sdb_time_t timestamp = 0; - - sdb_host_t host = SDB_HOST_INIT; + const char *hostname; + sdb_time_t timestamp; int status; @@ -63,18 +61,14 @@ sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client, assert((data[0].type == SDB_TYPE_STRING) && (data[1].type == SDB_TYPE_DATETIME)); - hostname = strdup(data[0].data.string); + hostname = data[0].data.string; timestamp = data[1].data.datetime; - SDB_OBJ(&host)->name = hostname; - host._last_update = timestamp; - - status = sdb_store_host(&host); + status = sdb_store_host(hostname, timestamp); if (status < 0) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to " "store/update host '%s'.", hostname); - free(hostname); return -1; } else if (status > 0) /* value too old */ @@ -83,7 +77,6 @@ sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client, sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated " "host '%s' (last update timestamp = %"PRIscTIME").", hostname, timestamp); - free(hostname); return 0; } /* sdb_livestatus_get_host */ diff --git a/src/backend/puppet/store-configs.c b/src/backend/puppet/store-configs.c index bde8a0a..6f70f49 100644 --- a/src/backend/puppet/store-configs.c +++ b/src/backend/puppet/store-configs.c @@ -52,7 +52,8 @@ sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client, size_t n, sdb_data_t *data, sdb_object_t __attribute__((unused)) *user_data) { - sdb_host_t host = SDB_HOST_INIT; + const char *hostname; + sdb_time_t timestamp; int status; @@ -60,22 +61,20 @@ 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)); - SDB_OBJ(&host)->name = strdup(data[0].data.string); - host._last_update = data[1].data.datetime; + hostname = data[0].data.string; + timestamp = data[1].data.datetime; - status = sdb_store_host(&host); + status = sdb_store_host(hostname, timestamp); if (status < 0) { sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to " - "store/update host '%s'.", SDB_OBJ(&host)->name); - free(SDB_OBJ(&host)->name); + "store/update host '%s'.", hostname); return -1; } else if (! status) sdb_log(SDB_LOG_DEBUG, "puppet::store-configs backend: " "Added/updated host '%s' (last update timestamp = " - "%"PRIscTIME").", SDB_OBJ(&host)->name, host._last_update); - free(SDB_OBJ(&host)->name); + "%"PRIscTIME").", hostname, timestamp); return 0; } /* sdb_puppet_stcfg_get_hosts */ diff --git a/src/core/store.c b/src/core/store.c index 92f3e51..a5f712e 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -218,25 +218,23 @@ const sdb_type_t sdb_service_type = { */ int -sdb_store_host(const sdb_host_t *host) +sdb_store_host(const char *name, sdb_time_t last_update) { - char *cname; - - sdb_time_t last_update; sdb_host_t *old; + char *cname; int status = 0; - if ((! host) || (! SDB_CONST_OBJ(host)->name)) + if (! name) return -1; - cname = sdb_plugin_cname(strdup(SDB_CONST_OBJ(host)->name)); + cname = sdb_plugin_cname(strdup(name)); if (! cname) { sdb_log(SDB_LOG_ERR, "store: strdup failed"); return -1; } - last_update = host->_last_update; + last_update = last_update; if (last_update <= 0) last_update = sdb_gettime(); @@ -265,7 +263,7 @@ sdb_store_host(const sdb_host_t *host) } } else { - sdb_host_t *new = SDB_HOST(sdb_object_clone(SDB_CONST_OBJ(host))); + sdb_host_t *new = SDB_HOST(sdb_object_create(name, sdb_host_type)); if (! new) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s", diff --git a/src/include/core/store.h b/src/include/core/store.h index 22b1315..563bd1a 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -86,11 +86,11 @@ typedef struct { /* * sdb_store_host: - * Add/update a host in the store. If the host, identified by its name, - * already exists, it will be updated according to the specified 'host' - * object. Else, a new entry will be created in the store. Any memory required - * for storing the entry will be allocated an managed by the store itself. The - * specified host-object will not be referenced or further accessed. + * Add/update a host in the store. If the host, identified by its + * canonicalized name, already exists, it will be updated according to the + * specified name and timestamp. Else, a new entry will be created in the + * store. Any memory required for storing the entry will be allocated an + * managed by the store itself. * * Returns: * - 0 on success @@ -99,7 +99,7 @@ typedef struct { * - a negative value on error */ int -sdb_store_host(const sdb_host_t *host); +sdb_store_host(const char *name, sdb_time_t last_update); const sdb_host_t * sdb_store_get_host(const char *name);