Exemple #1
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]
}
Exemple #2
0
// 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;
}
Exemple #3
0
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;
}
Exemple #4
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);
}
Exemple #5
0
void benchmarkWriteout ()
{
	kdbSet (kdb, large, key);
}
Exemple #6
0
void benchmarkRewrite ()
{
	kdbSet (kdb, large, key);
}
Exemple #7
0
gint gelektra_kdb_set (GElektraKdb * kdb, GElektraKeySet * returned, GElektraKey * parent)
{
	return kdbSet (kdb->handle, returned->keyset, parent->key);
}
{
	Key * parent = keyNew ("user/test/race", KEY_END);
	KDB *h = kdbOpen(parent);
	char buffer[4096];
	unsigned long tid = (unsigned long) pthread_self();
	int pid = getpid();
	sprintf(buffer, "user/test/race/keys/%d/%lu", pid, tid);
	KeySet * ks = ksNew(20,
		KS_END);

	int retg = kdbGet(h, ks, parent);
	ksAppendKey(ks, 
		keyNew (buffer, KEY_VALUE, "a value", KEY_END));

	pthread_barrier_wait(bar);
	int rets = kdbSet (h, ks, parent);

	if (rets != -1)
	{
		int retg2 = kdbGet(h, ks, parent);
		printf ("I (%d/%lu) won the race! Got return values from first get %d,"
			" from set %d, from second get %d\n",
			pid, tid, retg, rets, retg2);
		ksRewind(ks);
		/*
		Key * c; 
		while((c = ksNext(ks)))
		{
			printf ("Got key: %s - %s\n",
				keyName(c), keyString(c));
		}