<commit_before>/*
 * syscollector - src/core/store.c
 * Copyright (C) 2012 Sebastian 'tokkee' Harl <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "syscollector.h"
#include "core/store.h"
#include "utils/llist.h"
#include "utils/string.h"

#include <assert.h>

#include <errno.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pthread.h>

/*
 * private variables
 */

static sc_llist_t *host_list = NULL;
static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;

/*
 * private helper functions
 */

static int
sc_store_obj_cmp_by_name(const sc_object_t *a, const sc_object_t *b)
{
	const sc_store_obj_t *h1 = (const sc_store_obj_t *)a;
	const sc_store_obj_t *h2 = (const sc_store_obj_t *)b;

	assert(h1 && h2);
	return strcasecmp(h1->name, h2->name);
} /* sc_store_obj_cmp_by_name */

static int
sc_host_init(sc_object_t *obj, va_list ap)
{
	char *name = va_arg(ap, char *);

	SC_HOST(obj)->host_name = strdup(name);
	if (! SC_HOST(obj)->host_name)
		return -1;

	SC_HOST(obj)->host_last_update = sc_gettime();
	/* ignore errors -> last_update will be updated later */

	SC_HOST(obj)->services = sc_llist_create();
	if (! SC_HOST(obj)->services)
		return -1;
	return 0;
} /* sc_host_init */

static void
sc_host_destroy(sc_object_t *obj)
{
	assert(obj);

	if (SC_HOST(obj)->host_name)
		free(SC_HOST(obj)->host_name);

	if (SC_HOST(obj)->services)
		sc_llist_destroy(SC_HOST(obj)->services);
} /* sc_host_destroy */

static int
sc_svc_init(sc_object_t *obj, va_list ap)
{
	char *hostname = va_arg(ap, char *);
	char *name = va_arg(ap, char *);

	SC_SVC(obj)->hostname = strdup(hostname);
	SC_SVC(obj)->svc_name = strdup(name);
	if ((! SC_SVC(obj)->hostname) || (! SC_SVC(obj)->svc_name))
		return -1;

	SC_SVC(obj)->svc_last_update = sc_gettime();
	/* ignore errors -> last_update will be updated later */
	return 0;
} /* sc_svc_init */

static void
sc_svc_destroy(sc_object_t *obj)
{
	assert(obj);

	if (SC_SVC(obj)->hostname)
		free(SC_SVC(obj)->hostname);
	if (SC_SVC(obj)->svc_name)
		free(SC_SVC(obj)->svc_name);
} /* sc_svc_destroy */

/*
 * public API
 */

sc_host_t *
sc_host_create(char *name)
{
	sc_object_t *obj;

	if (! name)
		return NULL;

	obj = sc_object_create(sizeof(sc_host_t), sc_host_init,
			sc_host_destroy, name);
	if (! obj)
		return NULL;
	return SC_HOST(obj);
} /* sc_host_create */

sc_host_t *
sc_host_clone(const sc_host_t *host)
{
	sc_host_t *clone;

	clone = sc_host_create(host->host_name);
	if (! clone)
		return NULL;

	clone->host_last_update = host->host_last_update;
	if (host->services) {
		clone->services = sc_llist_clone(host->services);
		if (! clone->services) {
			sc_object_deref(SC_OBJ(clone));
			return NULL;
		}
	}
	else
		clone->services = NULL;
	return clone;
} /* sc_host_clone */

int
sc_store_host(const sc_host_t *host)
{
	sc_time_t last_update;

	sc_host_t *old;
	int status = 0;

	if ((! host) || (! host->host_name))
		return -1;

	last_update = host->host_last_update;
	if (last_update <= 0)
		last_update = sc_gettime();

	pthread_rwlock_wrlock(&host_lock);

	if (! host_list) {
		if (! (host_list = sc_llist_create())) {
			pthread_rwlock_unlock(&host_lock);
			return -1;
		}
	}

	old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)host,
				sc_store_obj_cmp_by_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);
			status = -1;
		}
		else {
			old->host_last_update = last_update;
		}
	}
	else {
		sc_host_t *new = sc_host_clone(host);
		if (! new) {
			char errbuf[1024];
			fprintf(stderr, "store: Failed to clone host object: %s\n",
					sc_strerror(errno, errbuf, sizeof(errbuf)));<commit_msg>store: Don't return an error if an updated host is too old.<commit_after>/*
 * syscollector - src/core/store.c
 * Copyright (C) 2012 Sebastian 'tokkee' Harl <<EMAIL>>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "syscollector.h"
#include "core/store.h"
#include "utils/llist.h"
#include "utils/string.h"

#include <assert.h>

#include <errno.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pthread.h>

/*
 * private variables
 */

static sc_llist_t *host_list = NULL;
static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;

/*
 * private helper functions
 */

static int
sc_store_obj_cmp_by_name(const sc_object_t *a, const sc_object_t *b)
{
	const sc_store_obj_t *h1 = (const sc_store_obj_t *)a;
	const sc_store_obj_t *h2 = (const sc_store_obj_t *)b;

	assert(h1 && h2);
	return strcasecmp(h1->name, h2->name);
} /* sc_store_obj_cmp_by_name */

static int
sc_host_init(sc_object_t *obj, va_list ap)
{
	char *name = va_arg(ap, char *);

	SC_HOST(obj)->host_name = strdup(name);
	if (! SC_HOST(obj)->host_name)
		return -1;

	SC_HOST(obj)->host_last_update = sc_gettime();
	/* ignore errors -> last_update will be updated later */

	SC_HOST(obj)->services = sc_llist_create();
	if (! SC_HOST(obj)->services)
		return -1;
	return 0;
} /* sc_host_init */

static void
sc_host_destroy(sc_object_t *obj)
{
	assert(obj);

	if (SC_HOST(obj)->host_name)
		free(SC_HOST(obj)->host_name);

	if (SC_HOST(obj)->services)
		sc_llist_destroy(SC_HOST(obj)->services);
} /* sc_host_destroy */

static int
sc_svc_init(sc_object_t *obj, va_list ap)
{
	char *hostname = va_arg(ap, char *);
	char *name = va_arg(ap, char *);

	SC_SVC(obj)->hostname = strdup(hostname);
	SC_SVC(obj)->svc_name = strdup(name);
	if ((! SC_SVC(obj)->hostname) || (! SC_SVC(obj)->svc_name))
		return -1;

	SC_SVC(obj)->svc_last_update = sc_gettime();
	/* ignore errors -> last_update will be updated later */
	return 0;
} /* sc_svc_init */

static void
sc_svc_destroy(sc_object_t *obj)
{
	assert(obj);

	if (SC_SVC(obj)->hostname)
		free(SC_SVC(obj)->hostname);
	if (SC_SVC(obj)->svc_name)
		free(SC_SVC(obj)->svc_name);
} /* sc_svc_destroy */

/*
 * public API
 */

sc_host_t *
sc_host_create(char *name)
{
	sc_object_t *obj;

	if (! name)
		return NULL;

	obj = sc_object_create(sizeof(sc_host_t), sc_host_init,
			sc_host_destroy, name);
	if (! obj)
		return NULL;
	return SC_HOST(obj);
} /* sc_host_create */

sc_host_t *
sc_host_clone(const sc_host_t *host)
{
	sc_host_t *clone;

	clone = sc_host_create(host->host_name);
	if (! clone)
		return NULL;

	clone->host_last_update = host->host_last_update;
	if (host->services) {
		clone->services = sc_llist_clone(host->services);
		if (! clone->services) {
			sc_object_deref(SC_OBJ(clone));
			return NULL;
		}
	}
	else
		clone->services = NULL;
	return clone;
} /* sc_host_clone */

int
sc_store_host(const sc_host_t *host)
{
	sc_time_t last_update;

	sc_host_t *old;
	int status = 0;

	if ((! host) || (! host->host_name))
		return -1;

	last_update = host->host_last_update;
	if (last_update <= 0)
		last_update = sc_gettime();

	pthread_rwlock_wrlock(&host_lock);

	if (! host_list) {
		if (! (host_list = sc_llist_create())) {
			pthread_rwlock_unlock(&host_lock);
			return -1;
		}
	}

	old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)host,
				sc_store_obj_cmp_by_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);
			/* don't report an error; the host may be updated by multiple
			 * backends */
			status = 0;
		}
		else {
			old->host_last_update = last_update;
		}
	}
	else {
		sc_host_t *new = sc_host_clone(host);
		if (! new) {
			char errbuf[1024];
			fprintf(stderr, "store: Failed to clone host object: %s\n",
					sc_strerror(errno, errbuf, sizeof(errbuf)));
{'fraction_non_alphanumeric': 0.12514540519581233, 'fraction_numerical': 0.005040713454827452, 'mean_word_length': 4.3734375, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5169606', 'n_tokens_mistral': 3992, 'n_tokens_neox': 3699, 'n_words': 990}