示例#1
0
文件: set.c 项目: intfrr/libelektra
// typical usage of Elektra
int main()
{
	Key * error_key = keyNew(KEY_END);
	KDB * kdb_handle = kdbOpen(error_key);
	Key * top = keyNew(KEY_END);
	keySetName(top, "user/sw/MyApp");

	KeySet * ks = ksNew(0);
	kdbGet(kdb_handle, ks, top);

	Key * key = keyNew(KEY_END);
	keySetName(key, "user/sw/MyApp/Tests/TestKey1"); // == 31
	keySetString(key, "NULLTestValue"); // == 14
	keySetMeta(key, "comment", "NULLTestComment"); // == 16
	ksAppendKey(ks, key); // == 1
	keyNeedSync(key);
	kdbSet(kdb_handle, ks, top); // == -1
	print_warnings(top);
	keyDel(top);
	ksDel(ks);
	kdbClose(kdb_handle, error_key);
	keyDel(error_key);

	check_key();

	return 0;
}
示例#2
0
int main()
{
	KeySet *myConfig = ksNew(0, KS_END);
	Key *key = keyNew("system/test/myapp",KEY_END);
	KDB *handle = kdbOpen(key);

	kdbGet(handle, myConfig, key);

	keySetName(key, "user/test/myapp");
	kdbGet(handle, myConfig, key);

	// check for errors in key
	keyDel(key);

	key = ksLookupByName(myConfig,"/test/myapp/key", 0);

	// check if key is not 0 and work with it...
	if (key)
	{
		printf("%s\n", keyString(key));
	}

	ksDel (myConfig); // delete the in-memory configuration


	// maybe you want kdbSet() myConfig here

	kdbClose(handle, 0); // no more affairs with the key database.
	return 0;
}
示例#3
0
int main()
{
	KeySet *myConfig = ksNew(0, KS_END);
	Key *key = keyNew("/sw/MyApp", KEY_CASCADING_NAME, KEY_END);
	KDB *handle = kdbOpen(key);

	kdbGet(handle, myConfig, key);
	// to get an intention of proper error handling see kdbget_error.c

	keyDel (key);

	Key * result = ksLookupByName (myConfig,"/sw/MyApp/Tests/TestKey1", 0);

	const char * key_name = keyName(result);
	const char * key_value = keyString(result);
	const char * key_comment = keyString(keyGetMeta(result, "comment"));
	printf("key: %s value: %s comment: %s\n", key_name, key_value, key_comment);

	ksDel (myConfig); // delete the in-memory configuration


	// maybe you want kdbSet() myConfig here

	kdbClose(handle, 0); // no more affairs with the key database.
}
示例#4
0
static int basicUse (int argc, const char ** argv)
{
	Key * parentKey = keyNew ("/sw/org/example/#0/current", KEY_END);
	//! [basic use]
	KDB * kdb = kdbOpen (parentKey);
	KeySet * ks = ksNew (0, KS_END);

	kdbGet (kdb, ks, parentKey);

	int result = elektraGetOpts (ks, argc, argv, (const char **) environ, parentKey);
	if (result == -1)
	{
		fprintf (stderr, "ERROR: %s\n", keyString (keyGetMeta (parentKey, "error/reason")));
		keyDel (parentKey);
		ksDel (ks);
		return EXIT_FAILURE;
	}

	if (result == 1)
	{
		char * help = elektraGetOptsHelpMessage (parentKey, NULL, NULL);
		fprintf (stderr, "%s\n", help);
		elektraFree (help);
		keyDel (parentKey);
		ksDel (ks);
		return EXIT_SUCCESS;
	}

	//! [basic use]
	ksDel (ks);
	kdbClose (kdb, parentKey);
	keyDel (parentKey);
	return EXIT_SUCCESS;
}
示例#5
0
static void test_registerInt (void)
{
	printf ("test elektraNotificationRegisterInt\n");

	Key * key = keyNew ("system/elektra/version/constants", KEY_END);
	Key * valueKey = keyNew ("system/elektra/version/constants/KDB_VERSION_MAJOR", KEY_END);

	int startValue = -1;
	int value = startValue;

	KDB * kdb = kdbOpen (key);

	succeed_if (elektraNotificationRegisterInt (kdb, valueKey, &value) == 0, "register should fail before open");

	elektraNotificationOpen (kdb);

	succeed_if (elektraNotificationRegisterInt (kdb, valueKey, &value), "register failed");

	// call kdbGet; value gets automatically updated
	KeySet * config = ksNew (0, KS_END);
	succeed_if (kdbGet (kdb, config, key), "kdbGet failed");

	succeed_if (value != startValue, "value was not changed");

	// cleanup
	ksDel (config);
	elektraNotificationClose (kdb);
	kdbClose (kdb, key);
	keyDel (key);
	keyDel (valueKey);
}
示例#6
0
void thread2 ()
{
	Key * parent = keyNew ("/app/part2", KEY_CASCADING_NAME, KEY_END);
	KDB * h = kdbOpen (parent);
	// fetch keys and work with them
	kdbClose (h, parent);
}
示例#7
0
int main ()
{
	// clang-format off
//! [set]
KeySet * myConfig = ksNew (0, KS_END);
Key * parentKey = keyNew ("system/sw/MyApp", KEY_END);
KDB * handle = kdbOpen (parentKey);

kdbGet (handle, myConfig, parentKey); // kdbGet needs to be called first!
KeySet * base = ksDup (myConfig);     // save a copy of original keyset

// change the keys within myConfig

KeySet * ours = ksDup (myConfig); // save a copy of our keyset
KeySet * theirs;		  // needed for 3-way merging
int ret = kdbSet (handle, myConfig, parentKey);
while (ret == -1) // as long as we have an error
{
	// We got an error. Warn user.
	Key * problemKey = ksCurrent (myConfig);
	// parentKey has the errorInformation
	// problemKey is the faulty key (may be null)
	int userInput = showElektraErrorDialog (parentKey, problemKey);
	switch (userInput)
	{
	case INPUT_USE_OURS:
		kdbGet (handle, myConfig, parentKey); // refresh key database
		ksDel (myConfig);
		myConfig = ours;
		break;
	case INPUT_DO_MERGE:
		theirs = ksDup (ours);
		kdbGet (handle, theirs, parentKey); // refresh key database
		KeySet * res = doElektraMerge (ours, theirs, base);
		ksDel (theirs);
		myConfig = res;
		break;
	case INPUT_USE_THEIRS:
		// should always work, we just write what we got
		// but to be sure always give the user another way
		// to exit the loop
		kdbGet (handle, myConfig, parentKey); // refresh key database
		break;
		// other cases ...
	}
	ret = kdbSet (handle, myConfig, parentKey);
}

ksDel (ours);
ksDel (base);
ksDel (myConfig); // delete the in-memory configuration

kdbClose (handle, parentKey); // no more affairs with the key database.
keyDel (parentKey);
//! [set]
}
示例#8
0
static void gelektra_kdb_finalize (GObject * object)
{
	GElektraKdb * self = GELEKTRA_KDB (object);

	if (self->handle) kdbClose (self->handle, NULL);
	self->handle = NULL;

	/* Always chain up to the parent class; as with dispose(), finalize()
	 * is guaranteed to exist on the parent's class virtual function table
	 */
	G_OBJECT_CLASS (gelektra_kdb_parent_class)->finalize (object);
}
示例#9
0
文件: lift.c 项目: intfrr/libelektra
int main(int argc, char**argv)
{
	Key *parentKey = keyNew("", KEY_END);
	KDB *kdb = kdbOpen(parentKey);
	KeySet *conf = ksNew(0);

	// get all config files
	kdbGetByName(kdb, conf, parentKey, "/test/lift");
	kdbGetByName(kdb, conf, parentKey, "/test/material_lift");
	kdbGetByName(kdb, conf, parentKey, "/test/heavy_material_lift");
	kdbGetByName(kdb, conf, parentKey, "/test/person_lift");

	// get by params
	int retval = ksGetOpt(argc, argv, conf);
	if (retval & 1)
	{
		printf("%s Version 0.1\n",
			argv[0]);
		return 0;
	}
	else if (retval & 2)
	{
		printf("Usage: %s [OPTIONS]\n"
			"%s\n"
			"Example that demonstrates elektra gen parameters\n",
			argv[0],
			elektraGenHelpText());
		return 0;
	}
	else if (retval != 0)
	{
		printf ("Error in parsing options %d\n", retval);
	}

	// write back to user/test/lift what we got by commandline
	// that means overrides in *_lift are still active, but
	// fallbacks will be overriden.
	if (lift(conf))
	{
		printf("Write out config\n");
		keySetName(parentKey, "user/test/lift");
		kdbSet(kdb, conf, parentKey);
	}

	ksDel(conf);
	kdbClose(kdb, parentKey);
	keyDel(parentKey);
	return retval;
}
示例#10
0
int main ()
{
	// clang-format off
//! [cut]
Key * parentKey = keyNew ("system/mountpoint/interest", KEY_END);
KDB * kdb = kdbOpen (parentKey);
KeySet * ks = ksNew (0, KS_END);
kdbGet (kdb, ks, parentKey);
KeySet * returned = ksCut (ks, parentKey);
kdbSet (kdb, ks, parentKey); // all keys below cutpoint are now removed
kdbClose (kdb, parentKey);
//! [cut]
outputKeySet (returned);
outputKeySet (ks);
}
示例#11
0
int main(int argc, char **argv)
{
	kdbOpen ( );
	QApplication app(argc, argv);	
	
	new EditorController();
	
	//app.setMainWidget(main);
	
	int ret = app.exec();
	
	kdbClose ( );
	return ret;
	
}
示例#12
0
文件: set.c 项目: intfrr/libelektra
/** After writing the key this function rereads the key and print it*/
void check_key()
{
	Key * error_key = keyNew(KEY_END);
	KDB * kdb_handle = kdbOpen(error_key);
	Key * top = keyNew(KEY_END);
	keySetName(top, "user/sw/MyApp"); // == 14
	KeySet * ks = ksNew(0);
	kdbGet(kdb_handle, ks, top);
	Key * key = keyNew(KEY_END);
	keySetName(key, "user/sw/MyApp/Tests/TestKey1"); // == 14
	Key * result = ksLookup(ks, key, KDB_O_NONE);
	const char * key_name = keyName(result);
	const char * key_value = keyString(result);
	const char * key_comment = keyString(keyGetMeta(result, "comment"));
	printf("key: %s value: %s comment: %s", key_name, key_value, key_comment);
	ksDel(ks);
	keyDel(key);
	keyDel(top);
	kdbClose(kdb_handle, error_key);
	keyDel(error_key);
}
示例#13
0
static void test_openclose (void)
{
	printf ("test open & close\n");

	// TODO test with ASAN and with & without cascading key
	Key * key = keyNew ("system/sw/tests/testlib_notification", KEY_END);
	KDB * kdb = kdbOpen (key);
	exit_if_fail (kdb, "opening kdb failed");

	succeed_if (!elektraNotificationClose (kdb), "could close notification system without open");

	succeed_if (elektraNotificationOpen (kdb), "could not open notification system");
	succeed_if (!elektraNotificationOpen (kdb), "could open notification system twice");

	succeed_if (elektraNotificationClose (kdb), "could not close notification system");
	succeed_if (elektraNotificationOpen (kdb), "could not re-open notification system");

	// cleanup
	succeed_if (elektraNotificationClose (kdb), "could not close notification system");
	succeed_if (kdbClose (kdb, key) == 0, "could not close kdb");
	keyDel (key);
}
示例#14
0
int main (void)
{
	KeySet * myConfig = ksNew (0, KS_END);
	Key * key = keyNew ("/sw/MyApp", KEY_CASCADING_NAME, KEY_END);
	KDB * handle = kdbOpen (key);

	if (!handle) printError (key);


	printWarnings (key);

	if (kdbGet (handle, myConfig, key) < 0) printError (key);


	printWarnings (key);

	keyDel (key);

	// lookup
	Key * result = ksLookupByName (myConfig, "/sw/MyApp/Tests/TestKey1", 0);
	if (!result)
		printf ("Key not found in KeySet\n");
	else
	{
		// do something with the key
		const char * key_name = keyName (result);
		const char * key_value = keyString (result);
		const char * key_comment = keyString (keyGetMeta (result, "comment"));
		printf ("key: %s value: %s comment: %s\n", key_name, key_value, key_comment);
	}

	ksDel (myConfig); // delete the in-memory configuration


	// maybe you want kdbSet() myConfig here

	kdbClose (handle, 0); // no more affairs with the key database.
}
示例#15
0
void benchmarkClose ()
{
	kdbClose (kdb, key);
}
示例#16
0
/* destructor */
gint gelektra_kdb_close (GElektraKdb * kdb, GElektraKey * error)
{
	int ret = kdbClose (kdb->handle, (error) ? error->key : NULL);
	kdb->handle = NULL;
	return ret;
}
示例#17
0
int main (void)
{
	printf ("lightweight zeromq message hub\n");

	// exit on SIGINT
	signal (SIGINT, onSignal);

	KeySet * config = ksNew (2, KS_END);

	Key * parentKey = keyNew ("/sw/elektra/hub-zeromq/#0/current", KEY_END);
	Key * configXSubEndpoint = keyDup (parentKey);
	keyAddBaseName (configXSubEndpoint, "bind_xsub");
	Key * configXPubEndpoint = keyDup (parentKey);
	keyAddBaseName (configXPubEndpoint, "bind_xpub");
	KDB * kdb = kdbOpen (parentKey);
	if (kdb == NULL)
	{
		printf ("could not open KDB. aborting\n");
		return -1;
	}

	const char * xSubEndpoint = "tcp://127.0.0.1:6000";
	const char * xPubEndpoint = "tcp://127.0.0.1:6001";
	kdbGet (kdb, config, parentKey);
	Key * xSubEndpointKey = ksLookup (config, configXSubEndpoint, 0);
	if (xSubEndpointKey)
	{
		xSubEndpoint = keyString (xSubEndpointKey);
	}
	Key * xPubEndpointKey = ksLookup (config, configXPubEndpoint, 0);
	if (xPubEndpointKey)
	{
		xPubEndpoint = keyString (xPubEndpointKey);
	}

	keyDel (configXSubEndpoint);
	keyDel (configXPubEndpoint);
	kdbClose (kdb, parentKey);
	keyDel (parentKey);

	context = zmq_ctx_new ();
	xSubSocket = zmq_socket (context, ZMQ_XSUB);
	xPubSocket = zmq_socket (context, ZMQ_XPUB);
	int result;
	result = zmq_bind (xSubSocket, xSubEndpoint);
	if (result != 0)
	{
		printf ("could not bind XSUB on %s socket: %s\n", xSubEndpoint, zmq_strerror (zmq_errno ()));
		zmq_close (xSubSocket);
		zmq_close (xPubSocket);
		zmq_ctx_destroy (context);
		return -1;
	}
	result = zmq_bind (xPubSocket, xPubEndpoint);
	if (result != 0)
	{
		printf ("could not bind XPUB on %s socket: %s\n", xPubEndpoint, zmq_strerror (zmq_errno ()));
		zmq_close (xSubSocket);
		zmq_close (xPubSocket);
		zmq_ctx_destroy (context);
		return -1;
	}

	printf ("listening on %s (XSUB for zeromqsend)\n", xSubEndpoint);
	printf ("listening on %s (XPUB for zeromqrecv)\n", xPubEndpoint);
	ksDel (config);

	// forward messages between sockets
	// will return on zmq_ctx_destroy()
	zmq_proxy (xPubSocket, xSubSocket, NULL);

	return 0;
}
示例#18
0
文件: kdb.c 项目: KurtMi/libelektra
/**
 * @brief Opens the session with the Key database.
 *
 * @pre errorKey must be a valid key, e.g. created with keyNew()
 *
 * The method will bootstrap itself the following way.
 * The first step is to open the default backend. With it
 * system/elektra/mountpoints will be loaded and all needed
 * libraries and mountpoints will be determined.
 * These libraries for backends will be loaded and with it the
 * @p KDB data structure will be initialized.
 *
 * You must always call this method before retrieving or committing any
 * keys to the database. In the end of the program,
 * after using the key database, you must not forget to kdbClose().
 *
 * The pointer to the @p KDB structure returned will be initialized
 * like described above, and it must be passed along on any kdb*()
 * method your application calls.
 *
 * Get a @p KDB handle for every thread using elektra. Don't share the
 * handle across threads, and also not the pointer accessing it:
 *
 * @snippet kdbopen.c open
 *
 * You don't need kdbOpen() if you only want to
 * manipulate plain in-memory Key or KeySet objects.
 *
 * @pre errorKey must be a valid key, e.g. created with keyNew()
 *
 * @param errorKey the key which holds errors and warnings which were issued
 * @see kdbGet(), kdbClose() to end all affairs to the key database.
 * @retval handle on success
 * @retval NULL on failure
 * @ingroup kdb
 */
KDB * kdbOpen (Key * errorKey)
{
	if (!errorKey)
	{
		ELEKTRA_LOG ("no error key passed");
		return 0;
	}

	ELEKTRA_LOG ("called with %s", keyName (errorKey));

	int errnosave = errno;
	KDB * handle = elektraCalloc (sizeof (struct _KDB));
	Key * initialParent = keyDup (errorKey);

	handle->modules = ksNew (0, KS_END);
	if (elektraModulesInit (handle->modules, errorKey) == -1)
	{
		ksDel (handle->modules);
		elektraFree (handle);
		ELEKTRA_SET_ERROR (94, errorKey, "elektraModulesInit returned with -1");

		keySetName (errorKey, keyName (initialParent));
		keySetString (errorKey, keyString (initialParent));
		keyDel (initialParent);
		errno = errnosave;
		return 0;
	}

	KeySet * keys = ksNew (0, KS_END);
	int inFallback = 0;
	switch (elektraOpenBootstrap (handle, keys, errorKey))
	{
	case -1:
		ksDel (handle->modules);
		elektraFree (handle);
		ELEKTRA_SET_ERROR (40, errorKey, "could not open default backend");

		keySetName (errorKey, keyName (initialParent));
		keySetString (errorKey, keyString (initialParent));
		keyDel (initialParent);
		errno = errnosave;
		return 0;
	case 0:
		ELEKTRA_ADD_WARNING (17, errorKey,
				     "Initial kdbGet() failed, you should either fix " KDB_DB_INIT " or the fallback " KDB_DB_FILE);
		break;
	case 2:
		ELEKTRA_LOG ("entered fallback code for bootstrapping");
		inFallback = 1;
		break;
	}

	keySetString (errorKey, "kdbOpen(): mountGlobals");

	if (mountGlobals (handle, ksDup (keys), handle->modules, errorKey) == -1)
	{
		// mountGlobals also sets a warning containing the name of the plugin that failed to load
		ELEKTRA_ADD_WARNING (139, errorKey, "Mounting global plugins failed");
	}

	keySetName (errorKey, keyName (initialParent));
	keySetString (errorKey, "kdbOpen(): backendClose");

	backendClose (handle->defaultBackend, errorKey);
	splitDel (handle->split);
	handle->defaultBackend = 0;
	handle->trie = 0;

#ifdef HAVE_LOGGER
	if (inFallback) ELEKTRA_LOG_WARNING ("fallback for bootstrapping: you might want to run `kdb upgrade-bootstrap`");

	Key * key;

	ksRewind (keys);
	for (key = ksNext (keys); key; key = ksNext (keys))
	{
		ELEKTRA_LOG_DEBUG ("config for createTrie name: %s value: %s", keyName (key), keyString (key));
	}
#endif

	handle->split = splitNew ();

	keySetString (errorKey, "kdbOpen(): mountOpen");
	// Open the trie, keys will be deleted within mountOpen
	if (mountOpen (handle, keys, handle->modules, errorKey) == -1)
	{
		ELEKTRA_ADD_WARNING (93, errorKey, "Initial loading of trie did not work");
	}

	keySetString (errorKey, "kdbOpen(): mountDefault");
	if (mountDefault (handle, handle->modules, inFallback, errorKey) == -1)
	{
		ELEKTRA_SET_ERROR (40, errorKey, "could not reopen and mount default backend");
		keySetString (errorKey, "kdbOpen(): close");
		kdbClose (handle, errorKey);

		keySetName (errorKey, keyName (initialParent));
		keySetString (errorKey, keyString (initialParent));
		keyDel (initialParent);
		errno = errnosave;
		return 0;
	}

	keySetString (errorKey, "kdbOpen(): mountVersion");
	mountVersion (handle, errorKey);

	keySetString (errorKey, "kdbOpen(): mountModules");
	if (mountModules (handle, handle->modules, errorKey) == -1)
	{
		ELEKTRA_ADD_WARNING (92, errorKey, "Mounting modules did not work");
	}

	keySetName (errorKey, keyName (initialParent));
	keySetString (errorKey, keyString (initialParent));
	keyDel (initialParent);
	errno = errnosave;
	return handle;
}