static void test_default() { printf ("Test default " KDB_DEFAULT_STORAGE "\n"); KeySet *modules = ksNew(0, KS_END); elektraModulesInit(modules, 0); Plugin *plugin = elektraPluginOpen(KDB_DEFAULT_STORAGE, modules, set_pluginconf(), 0); exit_if_fail (plugin, "KDB_DEFAULT_STORAGE: " KDB_DEFAULT_STORAGE " plugin could not be loaded"); KeySet *test_config = set_pluginconf(); KeySet *config = elektraPluginGetConfig (plugin); succeed_if (config != 0, "there should be a config"); compare_keyset(config, test_config); ksDel (test_config); succeed_if (plugin->kdbGet != 0, "no get pointer"); succeed_if (plugin->kdbSet != 0, "no set pointer"); elektraPluginClose(plugin, 0); Backend *backend = elektraBackendOpenDefault(modules, KDB_DB_FILE, 0); Key *mp; succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName(mp), ""); succeed_if_same_string (keyString(mp), "default"); elektraBackendClose(backend, 0); elektraModulesClose (modules, 0); ksDel (modules); }
static void kdb_del(KDB *kdb) { elektraBackendClose (kdb->defaultBackend, 0); elektraTrieClose(kdb->trie, 0); elektraSplitDel (kdb->split); elektraFree (kdb); }
/** * Closes the trie and all opened backends within. * * @param trie the trie to close * @param errorKey the key used to report warnings * @ingroup trie * @retval 0 on success */ int elektraTrieClose (Trie *trie, Key *errorKey) { size_t i; if (trie==NULL) return 0; for (i=0; i<KDB_MAX_UCHAR; ++i) { if (trie->text[i]!=NULL) { elektraTrieClose(trie->children[i], errorKey); if (trie->value[i]) elektraBackendClose(trie->value[i], errorKey); free(trie->text[i]); } } if (trie->empty_value) { elektraBackendClose(trie->empty_value, errorKey); } free(trie); return 0; }
static void test_backref() { printf ("Test back references\n"); KeySet *modules = ksNew(0, KS_END); elektraModulesInit(modules, 0); Backend *backend = elektraBackendOpen(set_backref(), modules, 0); succeed_if (backend != 0, "there should be a backend"); succeed_if (backend->getplugins[0] == 0, "there should be no plugin"); exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin"); succeed_if (backend->getplugins[2] == 0, "there should be no plugin"); succeed_if (backend->setplugins[0] == 0, "there should be no plugin"); exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin"); succeed_if (backend->setplugins[2] == 0, "there should be no plugin"); Key *mp; succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName(mp), "user/tests/backend/backref"); succeed_if_same_string (keyString(mp), "backref"); Plugin *plugin1 = backend->getplugins[1]; Plugin *plugin2 = backend->setplugins[1]; Plugin *plugin3 = backend->errorplugins[1]; succeed_if (plugin1 != 0, "there should be a plugin"); succeed_if (plugin2 != 0, "there should be a plugin"); succeed_if (plugin3 != 0, "there should be a plugin"); succeed_if (plugin1 == plugin2, "it should be the same plugin"); succeed_if (plugin2 == plugin3, "it should be the same plugin"); succeed_if (plugin1 == plugin3, "it should be the same plugin"); succeed_if (plugin1->refcounter == 3, "ref counter should be 3"); KeySet *test_config = set_pluginconf(); KeySet *config = elektraPluginGetConfig (plugin1); succeed_if (config != 0, "there should be a config"); compare_keyset(config, test_config); ksDel (test_config); succeed_if (plugin1->kdbGet != 0, "no get pointer"); succeed_if (plugin1->kdbSet != 0, "no set pointer"); succeed_if (plugin2->kdbGet != 0, "no get pointer"); succeed_if (plugin2->kdbSet != 0, "no set pointer"); elektraBackendClose (backend, 0); elektraModulesClose (modules, 0); ksDel (modules); }
/**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; }
static void test_simple() { printf ("Test simple building of backend\n"); KeySet *modules = ksNew(0, KS_END); elektraModulesInit(modules, 0); Key *errorKey = 0; Backend *backend = elektraBackendOpen(set_simple(), modules, errorKey); succeed_if (backend->errorplugins[0] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[2] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[3] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[4] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[5] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[6] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[7] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[8] == 0, "there should be no plugin"); succeed_if (backend->errorplugins[9] == 0, "there should be no plugin"); exit_if_fail (backend->errorplugins[1] != 0, "there should be a plugin"); succeed_if (backend->getplugins[0] == 0, "there should be no plugin"); succeed_if (backend->getplugins[2] == 0, "there should be no plugin"); succeed_if (backend->getplugins[3] == 0, "there should be no plugin"); succeed_if (backend->getplugins[4] == 0, "there should be no plugin"); succeed_if (backend->getplugins[5] == 0, "there should be no plugin"); succeed_if (backend->getplugins[6] == 0, "there should be no plugin"); succeed_if (backend->getplugins[7] == 0, "there should be no plugin"); succeed_if (backend->getplugins[8] == 0, "there should be no plugin"); succeed_if (backend->getplugins[9] == 0, "there should be no plugin"); exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin"); succeed_if (backend->setplugins[0] == 0, "there should be no plugin"); succeed_if (backend->setplugins[2] == 0, "there should be no plugin"); succeed_if (backend->setplugins[3] == 0, "there should be no plugin"); succeed_if (backend->setplugins[4] == 0, "there should be no plugin"); succeed_if (backend->setplugins[5] == 0, "there should be no plugin"); succeed_if (backend->setplugins[6] == 0, "there should be no plugin"); succeed_if (backend->setplugins[7] == 0, "there should be no plugin"); succeed_if (backend->setplugins[8] == 0, "there should be no plugin"); succeed_if (backend->setplugins[9] == 0, "there should be no plugin"); exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin"); Key *mp; succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName(mp), "user/tests/backend/simple"); succeed_if_same_string (keyString(mp), "simple"); Plugin *plugin = backend->getplugins[1]; KeySet *test_config = set_pluginconf(); KeySet *config = elektraPluginGetConfig (plugin); succeed_if (config != 0, "there should be a config"); compare_keyset(config, test_config); ksDel (test_config); succeed_if (plugin->kdbGet != 0, "no get pointer"); succeed_if (plugin->kdbSet != 0, "no set pointer"); elektraBackendClose (backend, errorKey); elektraModulesClose (modules, 0); ksDel (modules); }
/**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 * @param errorKey the key where an error and warnings are added * * @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; KeySet *referencePlugins = 0; KeySet *systemConfig = 0; int failure = 0; referencePlugins = ksNew(0, KS_END); ksRewind(elektraConfig); Key * root = ksNext (elektraConfig); Backend *backend = elektraBackendAllocate(); if (elektraBackendSetMountpoint(backend, elektraConfig, errorKey) == -1) { // warning already set failure = 1; } 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), "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 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")) { ksDel (cut); // already handled by elektraBackendSetMountpoint continue; } 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 { // 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; }