Exemplo n.º 1
0
/**@return a backend which gives plugin configuration of the module
 * which is currently point to.
 *
 * @param modules the modules to work with
 * @param errorKey the key to issue warnings and errors to
 */
Backend* elektraBackendOpenModules(KeySet *modules, Key *errorKey)
{
	Backend *backend = elektraBackendAllocate();

	cursor_t save = ksGetCursor (modules);
	KeySet *defaultConfig = ksNew(5,
		keyNew("system/module", KEY_VALUE, "1", KEY_END),
		keyNew("user/module", KEY_VALUE, "1", KEY_END),
		KS_END);
	Key *cur = ksCurrent(modules);

	Plugin *plugin = elektraPluginOpen(keyBaseName(cur), modules, defaultConfig, errorKey);
	if (!plugin)
	{
		/* Error already set in plugin */
		elektraFree(backend);
		return 0;
	}

	Key *mp = keyNew ("system/elektra/modules", KEY_VALUE, "modules", KEY_END);
	keyAddBaseName (mp, keyBaseName(cur));

	backend->getplugins[0] = plugin;
	plugin->refcounter = 1;

	backend->mountpoint = mp;
	keyIncRef(backend->mountpoint);

	ksSetCursor (modules, save);

	return backend;
}
Exemplo n.º 2
0
static void test_addNewBaseToParentKey()
{
	Key *parentKey = keyNew ("user/tests/rename", KEY_END);
	KeySet *conf = ksNew (20,
			keyNew ("system/cut", KEY_VALUE, "new/base", KEY_END), KS_END);

	PLUGIN_OPEN("rename");

	KeySet *ks = ksNew(0, KS_END);
	keyIncRef(parentKey);
	ksAppendKey (ks, parentKey);

	succeed_if(plugin->kdbSet (plugin, ks, parentKey) >= 1,
			"call to kdbSet was not successful");
	succeed_if(output_error (parentKey), "error in kdbSet");
	succeed_if(output_warnings (parentKey), "warnings in kdbSet");

	Key *key = ksLookupByName (ks, "user/tests/rename/new/base", 0);
	succeed_if (key, "new base was not correctly appended to parent key");

	ksDel(ks);
	keyDecRef(parentKey);
	keyDel(parentKey);
	PLUGIN_CLOSE ();

}
Exemplo n.º 3
0
/**
 * @brief sets mountpoint
 *
 * @param backend where the mountpoint should be set
 * @param elektraConfig the config where the mountpoint can be found
 * @param [out] errorKey the name also has the mountpoint set
 *
 * @pre ksCurrent() is root key
 * @post ksCurrent() is root key
 *
 * @retval -1 if no mountpoint is found or memory allocation problem
 * @retval 0 on success
 */
int elektraBackendSetMountpoint(Backend *backend, KeySet *elektraConfig, Key *errorKey)
{
	Key * root = ksCurrent(elektraConfig);
	Key * searchMountpoint = keyDup(root);
	keyAddBaseName(searchMountpoint, "mountpoint");
	Key * foundMountpoint = ksLookup(elektraConfig, searchMountpoint, 0);
	keyDel (searchMountpoint);
	ksLookup(elektraConfig, root, 0); // reset ksCurrent()

	if (!foundMountpoint)
	{
		ELEKTRA_ADD_WARNINGF(14, errorKey,
			"Could not find mountpoint within root %s",
			keyName(root));
		return -1;
	}

	backend->mountpoint = keyNew("",
			KEY_VALUE, keyBaseName(root), KEY_END);
	elektraKeySetName(backend->mountpoint, keyString(foundMountpoint),
			KEY_CASCADING_NAME | KEY_EMPTY_NAME);

	keySetName(errorKey, keyName(backend->mountpoint));

	if (!backend->mountpoint)
	{
		ELEKTRA_ADD_WARNINGF(14, errorKey,
			"Could not create mountpoint with name %s and value %s",
			keyString(foundMountpoint), keyBaseName(root));
		return -1;
	}

	keyIncRef(backend->mountpoint);
	return 0;
}
Exemplo n.º 4
0
Trie *test_insert (Trie *trie, char *name, char* value)
{
	Backend *backend = elektraCalloc (sizeof (Backend));
	backend->mountpoint = keyNew (name, KEY_VALUE, value, KEY_END);
	backend->refcounter = 1;
	keyIncRef (backend->mountpoint);
	return elektraTrieInsert(trie, name, backend);
}
Backend *b_new(const char *name, const char *value)
{
	Backend *backend = elektraCalloc (sizeof (Backend));
	backend->refcounter = 1;

	backend->mountpoint = keyNew (name, KEY_VALUE, value, KEY_END);
	keyIncRef (backend->mountpoint);

	return backend;
}
Exemplo n.º 6
0
/**
 * Opens a default backend using the plugin named KDB_DEFAULT_RESOLVER
 * and KDB_DEFAULT_STORAGE.
 *
 * @param modules the modules to work with
 * @param errorKey the key to issue warnings and errors to
 * @return the fresh allocated default backend or 0 if it failed
 */
Backend* elektraBackendOpenDefault(KeySet *modules, Key *errorKey)
{
	Backend *backend = elektraBackendAllocate();

	KeySet *resolverConfig = ksNew(5,
		keyNew("system/path", KEY_VALUE, KDB_DB_FILE, KEY_END),
		KS_END);

	Plugin *resolver = elektraPluginOpen(KDB_DEFAULT_RESOLVER,
			modules, resolverConfig, errorKey);
	if (!resolver)
	{
		elektraFree(backend);
		/* error already set in elektraPluginOpen */
		return 0;
	}

	backend->getplugins[RESOLVER_PLUGIN] = resolver;
	backend->setplugins[RESOLVER_PLUGIN] = resolver;
	backend->setplugins[COMMIT_PLUGIN] = resolver;
	backend->errorplugins[STORAGE_PLUGIN] = resolver;
	resolver->refcounter = 4;

	KeySet *storageConfig = ksNew(5,
		KS_END);

	Plugin *storage = elektraPluginOpen(KDB_DEFAULT_STORAGE,
			modules, storageConfig, errorKey);
	if (!storage)
	{
		elektraPluginClose(resolver, errorKey);
		elektraFree(backend);
		/* error already set in elektraPluginOpen */
		return 0;
	}

	backend->getplugins[STORAGE_PLUGIN] = storage;
	backend->setplugins[STORAGE_PLUGIN] = storage;
	storage->refcounter = 2;

	Key *mp = keyNew ("", KEY_VALUE, "default", KEY_END);
	backend->mountpoint = mp;
	keyIncRef(backend->mountpoint);

	return backend;
}
Exemplo n.º 7
0
/**
 * Opens the internal backend that indicates that a backend
 * is missing at that place.
 *
 * @return the fresh allocated backend or 0 if no memory
 */
Backend* elektraBackendOpenMissing(Key *mp)
{
	Backend *backend = elektraBackendAllocate();

	Plugin *plugin = elektraPluginMissing();
	if (!plugin)
	{
		/* Could not allocate plugin */
		elektraFree(backend);
		return 0;
	}

	backend->getplugins[0] = plugin;
	backend->setplugins[0] = plugin;
	plugin->refcounter = 2;

	keySetString (mp, "missing");
	backend->mountpoint = mp;
	keyIncRef(backend->mountpoint);

	return backend;
}
Exemplo n.º 8
0
static void shortExamples()
{
{
//! [Simple]
Key *k = keyNew(0);
// work with it
keyDel (k);
//! [Simple]

}{

//! [Alternative]
Key *k =keyNew("", KEY_END); // Has the same effect as above
// work with it
keyDel (k);
//! [Alternative]

}{

//! [With Name]
// Create and initialize a key with a name and nothing else
Key *k=keyNew("user/some/example", KEY_END);
// work with it
keyDel (k);
//! [With Name]

}{

//! [With Value]
// Create and initialize a key with a name and nothing else
Key *k=keyNew("user/tmp/ex0",
	KEY_VALUE, "some data",    // set a string value
	KEY_END);                  // end of args
//! [With Value]
keyDel(k);

}{

//! [With Size]
// Create and initialize a key with a name and nothing else
Key *k=keyNew("user/tmp/ex1",
	KEY_SIZE, 4,               // has no effect on strings
	KEY_VALUE, "some data",    // set a string value
	KEY_END);                  // end of args
//! [With Size]
printf ("%s\n", keyString(k));
keyDel(k);

}{

//! [With Binary]
// Create and initialize a key with a name and nothing else
Key *k=keyNew("user/tmp/ex2",
	KEY_BINARY,
	KEY_SIZE, 4,               // now the size is important
	KEY_VALUE, "some data",    // sets the binary value ("some")
	KEY_END);                  // end of args
//! [With Binary]
printf ("%.4s\n", (char*)keyValue(k));
keyDel(k);


}{

//! [With Mode]
Key *k=keyNew("user/tmp/ex3",
	KEY_VALUE, "some data",    // with a simple value
	KEY_MODE, 0777,            // permissions
	KEY_END);                  // end of args
//! [With Mode]
keyDel(k);

}{

//! [With Meta]
Key *k=keyNew("user/tmp/ex3",
	KEY_META, "comment", "a comment",  // with a comment
	KEY_META, "owner", "root",         // and an owner
	KEY_META, "special", "yes",        // and any other meta data
	KEY_END);                  // end of args
//! [With Meta]
keyDel(k);

}{

//! [With Flags]
Key *k=keyNew("user/tmp/ex3",
	KEY_FLAGS, KEY_BINARY | KEY_CASCADING_NAME, // flags
	KEY_SIZE, 7,			// assume binary length 7
	KEY_VALUE, "some data",		// value that will be truncated in 7 bytes
	KEY_END);			// end of args
//! [With Flags]
printf ("%.7s\n", (char*)keyValue(k));
keyDel(k);

}{

//! [With Everything]
Key *k=keyNew("user/tmp/ex4",
	KEY_BINARY,			// key type
	KEY_SIZE, 7,			// assume binary length 7
	KEY_VALUE, "some data",		// value that will be truncated in 7 bytes
	KEY_COMMENT, "value is truncated",
	KEY_OWNER, "root",		// owner (not uid) is root
	KEY_UID, 0,			// root uid
	KEY_END);			// end of args
//! [With Everything]
printf ("%.7s\n", (char*)keyValue(k));
keyDel(k);

}{

//! [Ref in KeySet]
Key *k = keyNew("user/proper_name", KEY_END); // ref counter = 0
KeySet *ks = ksNew (1, k, KS_END);
keyDel(k); // key will not be deleted, because its in the keyset
ksDel(ks); // now the key will be deleted
//! [Ref in KeySet]

}{

//! [Ref in multiple KeySets]
Key *k = keyNew("user/proper_name", KEY_END); // ref counter 0
KeySet *ks1 = ksNew(1, k, KS_END); // ref counter of k 1
KeySet *ks2 = ksNew(1, k, KS_END); // ref counter of k 2
ksDel(ks1); // ref counter of k 1
ksDel(ks2); // k is now deleted
//! [Ref in multiple KeySets]

}{

//! [Ref]
Key *k = keyNew(0); // ref counter = 0
keyIncRef(k); // ref counter = 1
keyDel(k); // key will not be deleted
keyDecRef(k);
keyDel(k);
//! [Ref]

}{

//! [Multi Ref]
Key *k = keyNew(0); // ref counter 0
keyIncRef(k); // ref counter of key 1
keyDel (k);   // has no effect
keyIncRef(k); // ref counter of key 2
keyDel (k);   // has no effect
keyDecRef(k); // ref counter of key 1
keyDel (k);   // has no effect
keyDecRef(k); // ref counter is now 0
keyDel (k); // k is now deleted
//! [Multi Ref]

}

}
Exemplo n.º 9
0
/**Builds a backend out of the configuration supplied
 * from:
 *
@verbatim
system/elektra/mountpoints/<name>
@endverbatim
 *
 * The root key must be like the above example. You do
 * not need to rewind the keyset. But every key must be
 * below the root key.
 *
 * The internal consistency will be checked in this
 * function. If necessary parts are missing, like
 * no plugins, they cant be loaded or similar 0
 * will be returned.
 *
 * ksCut() is perfectly suitable for cutting out the
 * configuration like needed.
 *
 * @note The given KeySet will be deleted within the function,
 * don't use it afterwards.
 *
 * @param elektraConfig the configuration to work with.
 *        It is used to build up this backend.
 * @param modules used to load new modules or get references
 *        to existing one
 * @return a pointer to a freshly allocated backend
 *         this could be the requested backend or a so called
 *         "missing backend".
 * @retval 0 if out of memory
 * @ingroup backend
 */
Backend* elektraBackendOpen(KeySet *elektraConfig, KeySet *modules, Key *errorKey)
{
	Key * cur;
	Key * root;
	KeySet *referencePlugins = 0;
	KeySet *systemConfig = 0;
	int failure = 0;

	referencePlugins = ksNew(0, KS_END);
	ksRewind(elektraConfig);

	root = ksNext (elektraConfig);

	Backend *backend = elektraBackendAllocate();

	while ((cur = ksNext(elektraConfig)) != 0)
	{
		if (keyRel (root, cur) == 1)
		{
			// direct below root key
			KeySet *cut = ksCut (elektraConfig, cur);
			if (!strcmp(keyBaseName(cur), "config"))
			{
				systemConfig = elektraRenameKeys(cut, "system");
				ksDel (cut);
			}
			else if (!strcmp(keyBaseName(cur), "getplugins"))
			{
				if (elektraProcessPlugins(backend->getplugins, modules, referencePlugins,
							cut, systemConfig, errorKey) == -1)
				{
					if (!failure) ELEKTRA_ADD_WARNING(13, errorKey, "elektraProcessPlugins for get failed");
					failure = 1;
				}
			}
			else if (!strcmp(keyBaseName(cur), "mountpoint"))
			{
				backend->mountpoint = keyNew("",
						KEY_VALUE, keyBaseName(root), KEY_END);
				elektraKeySetName(backend->mountpoint, keyString(cur),
						KEY_CASCADING_NAME | KEY_EMPTY_NAME);

				if (!backend->mountpoint)
				{
					if (!failure) ELEKTRA_ADD_WARNINGF(14, errorKey,
						"Could not create mountpoint with name %s and value %s",
						keyString(cur), keyBaseName(root));
					failure = 1;
				}

				keyIncRef(backend->mountpoint);
				ksDel (cut);
			}
			else if (!strcmp(keyBaseName(cur), "setplugins"))
			{
				if (elektraProcessPlugins(backend->setplugins, modules, referencePlugins,
							cut, systemConfig, errorKey) == -1)
				{
					if (!failure) ELEKTRA_ADD_WARNING(15, errorKey, "elektraProcessPlugins for set failed");
					failure = 1;
				}
			}
			else if (!strcmp(keyBaseName(cur), "errorplugins"))
			{
				if (elektraProcessPlugins(backend->errorplugins, modules, referencePlugins,
							cut, systemConfig, errorKey) == -1)
				{
					if (!failure) ELEKTRA_ADD_WARNING(15, errorKey, "elektraProcessPlugins for error failed");
					failure = 1;
				}
			} else {
				// no one cares about that config
				if (!failure) ELEKTRA_ADD_WARNING(16, errorKey, keyBaseName(cur));
				ksDel (cut);
			}
		}
	}

	if (failure)
	{
		Backend *tmpBackend = elektraBackendOpenMissing(backend->mountpoint);
		elektraBackendClose(backend, errorKey);
		backend = tmpBackend;
	}

	ksDel (systemConfig);
	ksDel (elektraConfig);
	ksDel (referencePlugins);

	return backend;
}
Exemplo n.º 10
0
static void gelektra_key_init (GElektraKey * self)
{
	/* initialize the object */
	self->key = keyNew (NULL);
	keyIncRef (self->key);
}
Exemplo n.º 11
0
/**
 * Opens a default backend using the plugin named KDB_DEFAULT_RESOLVER
 * and KDB_DEFAULT_STORAGE.
 *
 * @param modules the modules to work with
 * @param errorKey the key to issue warnings and errors to
 * @return the fresh allocated default backend or 0 if it failed
 */
Backend* elektraBackendOpenDefault(KeySet *modules, const char * file, Key *errorKey)
{
	Backend *backend = elektraBackendAllocate();

	KeySet *resolverConfig = ksNew(5,
		keyNew("system/path", KEY_VALUE, file, KEY_END),
		KS_END);

	elektraKeySetName(errorKey, "", KEY_CASCADING_NAME | KEY_EMPTY_NAME);

	Plugin *resolver = elektraPluginOpen(KDB_DEFAULT_RESOLVER,
			modules, resolverConfig, errorKey);
	if (!resolver)
	{
		elektraFree(backend);
		/* error already set in elektraPluginOpen */
		return 0;
	}

#if DEBUG && VERBOSE
	KeySet *tracerConfig = ksNew(5,
		// does not matter because it is mounted differently in system/elektra/modules:
		// keyNew("system/logmodule", KEY_VALUE, "1", KEY_END),
		KS_END);
	Plugin *tracer = elektraPluginOpen("tracer",
		modules, tracerConfig, errorKey);
	if (tracer)
	{
		backend->getplugins[RESOLVER_PLUGIN+1] = tracer;
		backend->setplugins[RESOLVER_PLUGIN+1] = tracer;
		backend->errorplugins[RESOLVER_PLUGIN+1] = tracer;
		tracer->refcounter = 3;
	}
#endif

	backend->getplugins[RESOLVER_PLUGIN] = resolver;
	backend->setplugins[RESOLVER_PLUGIN] = resolver;
	backend->setplugins[COMMIT_PLUGIN] = resolver;
	backend->errorplugins[STORAGE_PLUGIN] = resolver;
	resolver->refcounter = 4;

	KeySet *storageConfig = ksNew(5,
		KS_END);

	Plugin *storage = elektraPluginOpen(KDB_DEFAULT_STORAGE,
			modules, storageConfig, errorKey);
	if (!storage)
	{
		elektraPluginClose(resolver, errorKey);
		elektraFree(backend);
		/* error already set in elektraPluginOpen */
		return 0;
	}

	backend->getplugins[STORAGE_PLUGIN] = storage;
	backend->setplugins[STORAGE_PLUGIN] = storage;
	storage->refcounter = 2;

	Key *mp = keyNew ("", KEY_VALUE, "default", KEY_END);
	backend->mountpoint = mp;
	keyIncRef(backend->mountpoint);

	return backend;
}