<commit_before>/***************************************************************************
            kdb.c  -  Low level functions for access the Key Database
                             -------------------
    begin                : Mon Dec 29 2003
    copyright            : (C) 2003 by <NAME>
    email                : <EMAIL>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the BSD License (revised).                      *
 *                                                                         *
 ***************************************************************************/



/**
 * @defgroup kdb KDB :: Low Level Methods
 * @brief General methods to access the Key database.
 *
 * To use them:
 * @code
 * #include <kdb.h>
 * @endcode
 *
 * The kdb*() class of methods are used to access the storage, to get and set
 * @link key Keys @endlink or @link keyset KeySets @endlink.
 *
 * The most important functions are:
 *  - kdbOpen()
 *  - kdbClose()
 *  - kdbGet()
 *  - kdbSet()
 *
 * The two essential functions for dynamic information about backends are:
 *  - kdbGetMountpoint()
 *  - kdbGetCapability()
 *
 * They use some backend implementation to know the details about how to access
 * the storage. Currently we have this backends:
 * - @c berkeleydb: the keys are stored in a Berkeley DB database, providing
 *   very small footprint, speed, and other advantages.
 * - @c filesys: the key hierarchy and data are saved as plain text files in 
 *   the filesystem.
 * - @c ini: the key hierarchy are saved into configuration files.
 *   @see http://www.libelektra.org/Ini
 * - @c fstab: a reference backend used to interpret the @c /etc/fstab file as
 *   a set of keys under @c system/filesystems .
 * - @c gconf: makes Elektra use the GConf daemon to access keys. Only the
 *   @c user/ tree is available since GConf is not system wide.
 *
 * Backends are physically a library named @c /lib/libelektra-{NAME}.so.
 *
 * See @link backend writing a new backend @endlink for information
 * about how to write a backend.
 *
 * Language binding writers should follow the same rules:
 * - You must relay completely on the backend-dependent methods.
 * - You may use or reimplement the second set of methods.
 * - You should completely reimplement in your language the higher
 *   lever methods.
 * - Many methods are just for comfort in C. These methods are marked
 *   and need not to be implemented if the binding language has e.g. string
 *   operators which can do the operation easily.
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if DEBUG && HAVE_STDIO_H
#include <stdio.h>
#endif

#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif

#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#include <kdbinternal.h>


KDB* kdbOpenBackend(const char *backendname, const char *mountpoint, KeySet *config)
{
	KDB * handle;
	char* backend_name;

	kdbLibHandle dlhandle=0;
	typedef KDB *(*KDBBackendFactory) (void);
	KDBBackendFactory kdbBackendFactory=0;

	backend_name = malloc(sizeof("libelektra-")+strlen(backendname));

	strncpy(backend_name,"libelektra-",sizeof("libelektra-"));
	strncat(backend_name,backendname,strlen(backendname));

	dlhandle=kdbLibLoad(backend_name);
	if (dlhandle == 0) {
		/*errno=KDB_ERR_EBACKEND;*/
		goto err_clup; /* error */
	}

	/* load the "kdbBackendFactory" symbol from backend */
	kdbBackendFactory=(KDBBackendFactory)kdbLibSym(dlhandle, "kdbBackendFactory");
	if (kdbBackendFactory == 0) {
		/*errno=KDB_ERR_NOSYS;*/
		goto err_clup; /* error */
	}
	
	handle=kdbBackendFactory();
	if (handle == 0)
	{
		/*errno=KDB_ERR_NOSYS;*/
		goto err_clup; /* error */
	}

	/* save the libloader handle for future use */
	handle->dlHandle=dlhandle;
	handle->trie	= 0;

	handle->mountpoint=keyNew(mountpoint,KEY_VALUE,backendname,0);

	/* let the backend initialize itself */
	if (handle->kdbOpen)
	{
		handle->config = config;
		handle->kdbOpen(handle);
	}
	else {
		/*errno=KDB_ERR_NOSYS;*/
		goto err_clup;
	}

#if DEBUG && VERBOSE
	printf("Finished loading Backend %s\n", backend_name);
#endif
	free(backend_name);
	return handle;

err_clup:
#if DEBUG<commit_msg>much more verbose on errors when loading backends<commit_after>/***************************************************************************
            kdb.c  -  Low level functions for access the Key Database
                             -------------------
    begin                : Mon Dec 29 2003
    copyright            : (C) 2003 by <NAME>
    email                : <EMAIL>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the BSD License (revised).                      *
 *                                                                         *
 ***************************************************************************/



/**
 * @defgroup kdb KDB :: Low Level Methods
 * @brief General methods to access the Key database.
 *
 * To use them:
 * @code
 * #include <kdb.h>
 * @endcode
 *
 * The kdb*() class of methods are used to access the storage, to get and set
 * @link key Keys @endlink or @link keyset KeySets @endlink.
 *
 * The most important functions are:
 *  - kdbOpen()
 *  - kdbClose()
 *  - kdbGet()
 *  - kdbSet()
 *
 * The two essential functions for dynamic information about backends are:
 *  - kdbGetMountpoint()
 *  - kdbGetCapability()
 *
 * They use some backend implementation to know the details about how to access
 * the storage. Currently we have this backends:
 * - @c berkeleydb: the keys are stored in a Berkeley DB database, providing
 *   very small footprint, speed, and other advantages.
 * - @c filesys: the key hierarchy and data are saved as plain text files in 
 *   the filesystem.
 * - @c ini: the key hierarchy are saved into configuration files.
 *   @see http://www.libelektra.org/Ini
 * - @c fstab: a reference backend used to interpret the @c /etc/fstab file as
 *   a set of keys under @c system/filesystems .
 * - @c gconf: makes Elektra use the GConf daemon to access keys. Only the
 *   @c user/ tree is available since GConf is not system wide.
 *
 * Backends are physically a library named @c /lib/libelektra-{NAME}.so.
 *
 * See @link backend writing a new backend @endlink for information
 * about how to write a backend.
 *
 * Language binding writers should follow the same rules:
 * - You must relay completely on the backend-dependent methods.
 * - You may use or reimplement the second set of methods.
 * - You should completely reimplement in your language the higher
 *   lever methods.
 * - Many methods are just for comfort in C. These methods are marked
 *   and need not to be implemented if the binding language has e.g. string
 *   operators which can do the operation easily.
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if DEBUG && HAVE_STDIO_H
#include <stdio.h>
#endif

#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif

#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#include <kdbinternal.h>


KDB* kdbOpenBackend(const char *backendname, const char *mountpoint, KeySet *config)
{
	KDB * handle;
	char* backend_name;

	kdbLibHandle dlhandle=0;
	typedef KDB *(*KDBBackendFactory) (void);
	KDBBackendFactory kdbBackendFactory=0;

	backend_name = malloc(sizeof("libelektra-")+strlen(backendname));

	strncpy(backend_name,"libelektra-",sizeof("libelektra-"));
	strncat(backend_name,backendname,strlen(backendname));

	dlhandle=kdbLibLoad(backend_name);
	if (dlhandle == 0) {
		/*errno=KDB_ERR_EBACKEND;*/
#if DEBUG && VERBOSE
		printf("kdbLibLoad(%s) failed\n", backend_name);
#endif
		goto err_clup; /* error */
	}

	/* load the "kdbBackendFactory" symbol from backend */
	kdbBackendFactory=(KDBBackendFactory)kdbLibSym(dlhandle, "kdbBackendFactory");
	if (kdbBackendFactory == 0) {
		/*errno=KDB_ERR_NOSYS;*/
#if DEBUG && VERBOSE
		printf("Could not kdbLibSym kdbBackendFactory for %s\n", backend_name);
#endif
		goto err_clup; /* error */
	}
	
	handle=kdbBackendFactory();
	if (handle == 0)
	{
		/*errno=KDB_ERR_NOSYS;*/
#if DEBUG && VERBOSE
		printf("Could not call kdbBackendFactory for %s\n", backend_name);
#endif
		goto err_clup; /* error */
	}

	/* save the libloader handle for future use */
	handle->dlHandle=dlhandle;
	handle->trie	= 0;

	handle->mountpoint=keyNew(mountpoint,KEY_VALUE,backendname,0);

	/* let the backend initialize itself */
	if (handle->kdbOpen)
	{
		handle->config = config;
		if (handle->kdbOpen(handle) == -1)
		{
#if DEBUG && VERBOSE
			printf("kdbOpen() failed for %s\n", backend_name);
#endif
		}
	}
	else {
		/*errno=KDB_ERR_NOSYS;*/
#if DEBUG && VERBOSE
			printf("No kdbOpen supplied in %s\n", backend_name);
#endif
		goto err_clup;
	}

#if DEBUG && VERBOSE
	printf("Finished loading Backend %s\n", backend_name);
#endif
	free(backend_name);
	return handle;

err_clup:
#if DEBUG
{'fraction_non_alphanumeric': 0.16370767960363336, 'fraction_numerical': 0.0036127167630057803, 'mean_word_length': 3.2236268526591108, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2808456', 'n_tokens_mistral': 3047, 'n_tokens_neox': 2907, 'n_words': 958}