static void test_us () { printf ("Test mounting of user and system backends\n"); KDB * kdb = kdb_new (); KeySet * modules = ksNew (0, KS_END); elektraModulesInit (modules, 0); Key * mp; KeySet * config = set_us (); ksAppendKey (config, keyNew ("system/elektra/mountpoints", KEY_END)); succeed_if (mountOpen (kdb, config, modules, 0) == 0, "could not open mount"); succeed_if (mountDefault (kdb, modules, 1, 0) == 0, "could not mount default backend"); succeed_if (kdb->split->size == 5, "size of split not correct"); mp = keyNew ("system", KEY_VALUE, "system", KEY_END); compare_key (mp, kdb->split->parents[0]); keySetName (mp, "user"); keySetString (mp, "user"); compare_key (mp, kdb->split->parents[1]); keySetName (mp, "system/elektra"); keySetString (mp, "default"); compare_key (mp, kdb->split->parents[4]); keyDel (mp); Key * key = keyNew ("user/anywhere/backend/simple", KEY_END); Backend * backend = trieLookup (kdb->trie, key); keyAddBaseName (key, "somewhere"); keyAddBaseName (key, "deep"); keyAddBaseName (key, "below"); Backend * backend2 = trieLookup (kdb->trie, key); succeed_if (backend == backend2, "should be same backend"); succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName (mp), "user"); succeed_if_same_string (keyString (mp), "user"); keySetName (key, "system/anywhere/tests/backend/two"); Backend * two = trieLookup (kdb->trie, key); succeed_if (two != backend, "should be differnt backend"); succeed_if ((mp = two->mountpoint) != 0, "no mountpoint found"); succeed_if_same_string (keyName (mp), "system"); succeed_if_same_string (keyString (mp), "system"); keyDel (key); kdb_del (kdb); elektraModulesClose (modules, 0); ksDel (modules); }
static void test_defaultonly () { printf ("Test mounting with default only\n"); KDB * kdb = kdb_new (); Key * errorKey = keyNew (0); KeySet * modules = modules_config (); succeed_if (mountOpen (kdb, minimal_config (), modules, errorKey) == 0, "could not buildup mount"); succeed_if (mountDefault (kdb, modules, 1, errorKey) == 0, "could not mount default backend"); // output_split (kdb->split); succeed_if (kdb->split->size == 4, "size of split not correct"); Key * mp = keyNew ("spec", KEY_VALUE, "default", KEY_END); compare_key (mp, kdb->split->parents[0]); keySetName (mp, "dir"); keySetString (mp, "default"); compare_key (mp, kdb->split->parents[1]); keySetName (mp, "user"); keySetString (mp, "default"); compare_key (mp, kdb->split->parents[2]); keySetName (mp, "system"); keySetString (mp, "default"); compare_key (mp, kdb->split->parents[3]); succeed_if (output_warnings (errorKey), "warnings found"); succeed_if (output_error (errorKey), "error found"); succeed_if (!kdb->trie, "trie should be empty"); Key * searchKey = keyNew ("", KEY_END); Backend * b2 = 0; keySetName (searchKey, "user"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2 == 0, "should be default backend"); keySetName (searchKey, "user/tests/simple"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2 == 0, "should be default backend"); keySetName (searchKey, "user/tests/simple/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2 == 0, "should be default backend"); keySetName (searchKey, "user/tests/simple/deep/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2 == 0, "should be default backend"); keyDel (mp); keyDel (searchKey); kdb_del (kdb); keyDel (errorKey); ksDel (modules); }
static void test_modules () { printf ("Test mounting with modules\n"); KDB * kdb = kdb_new (); Key * errorKey = keyNew (0); KeySet * modules = modules_config (); succeed_if (mountOpen (kdb, root_config (), modules, errorKey) == 0, "could not buildup mount"); succeed_if (mountDefault (kdb, modules, 1, errorKey) == 0, "could not mount default backend"); succeed_if (mountModules (kdb, modules, errorKey) == 0, "could not mount modules"); succeed_if (output_warnings (errorKey), "warnings found"); succeed_if (output_error (errorKey), "error found"); // output_split (kdb->split); succeed_if (kdb->split->size == 8, "size of split not correct"); Key * mp = keyNew ("spec", KEY_VALUE, "root", KEY_END); compare_key (mp, kdb->split->parents[0]); keySetName (mp, "dir"); keySetString (mp, "root"); compare_key (mp, kdb->split->parents[1]); keySetName (mp, "user"); keySetString (mp, "root"); compare_key (mp, kdb->split->parents[2]); keySetName (mp, "system"); keySetString (mp, "root"); compare_key (mp, kdb->split->parents[3]); /* we cannot exactly know where resolver+dump is located *(depending on alphabet) keySetName(mp, "system/elektra/modules/"KDB_DEFAULT_RESOLVER); keySetString (mp, "modules"); compare_key(mp, kdb->split->parents[4]); */ keySetName (mp, "system/elektra"); keySetString (mp, "default"); compare_key (mp, kdb->split->parents[5]); keySetName (mp, "user/tests/simple"); keySetString (mp, "simple"); compare_key (mp, kdb->split->parents[4]); exit_if_fail (kdb->trie, "trie was not build up successfully"); // output_trie (kdb->trie); Key * searchKey = keyNew ("", KEY_END); Key * rmp = keyNew ("", KEY_VALUE, "root", KEY_END); elektraKeySetName (rmp, "/", KEY_CASCADING_NAME); Backend * b2 = 0; keySetName (searchKey, "user"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); compare_key (b2->mountpoint, rmp); Backend * backend = 0; keySetName (searchKey, "user/tests/simple"); backend = trieLookup (kdb->trie, searchKey); succeed_if (backend, "there should be a backend"); compare_key (backend->mountpoint, mp); keySetName (searchKey, "user/tests/simple/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key (b2->mountpoint, mp); keySetName (searchKey, "user/tests/simple/deep/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key (b2->mountpoint, mp); Key * dmp = keyNew ("", KEY_VALUE, "default", KEY_END); keySetName (searchKey, "system/elektra"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (b2 == kdb->defaultBackend, "should be the default backend"); compare_key (b2->mountpoint, dmp); keySetName (searchKey, "system/elektra/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (b2 == kdb->defaultBackend, "should be the default backend"); compare_key (b2->mountpoint, dmp); Key * mmp = keyNew ("system/elektra/modules", KEY_VALUE, "modules", KEY_END); keyAddBaseName (mmp, "default"); /* keySetName(searchKey, "system/elektra/modules/default"); b2 = trieLookup(kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (b2 != kdb->defaultBackend, "should not be the default backend"); compare_key(b2->mountpoint, mmp); */ keyDel (mmp); keyDel (dmp); keyDel (mp); keyDel (rmp); keyDel (searchKey); kdb_del (kdb); keyDel (errorKey); ksDel (modules); }
static void test_default () { printf ("Test mounting with default\n"); KDB * kdb = kdb_new (); Key * errorKey = keyNew (0); KeySet * modules = modules_config (); succeed_if (mountOpen (kdb, root_config (), modules, errorKey) == 0, "could not buildup mount"); succeed_if (mountDefault (kdb, modules, 1, errorKey) == 0, "could not mount default backend"); succeed_if (kdb->split->size == 6, "size of split not correct"); Key * mp = keyNew ("spec", KEY_VALUE, "root", KEY_END); compare_key (mp, kdb->split->parents[0]); keySetName (mp, "dir"); keySetString (mp, "root"); compare_key (mp, kdb->split->parents[1]); keySetName (mp, "user"); keySetString (mp, "root"); compare_key (mp, kdb->split->parents[2]); keySetName (mp, "system"); keySetString (mp, "root"); compare_key (mp, kdb->split->parents[3]); keySetName (mp, "system/elektra"); keySetString (mp, "default"); compare_key (mp, kdb->split->parents[5]); // must be last, needed later keySetName (mp, "user/tests/simple"); keySetString (mp, "simple"); compare_key (mp, kdb->split->parents[4]); succeed_if (output_warnings (errorKey), "warnings found"); succeed_if (output_error (errorKey), "error found"); exit_if_fail (kdb->trie, "trie was not build up successfully"); // output_trie (kdb->trie); Key * searchKey = keyNew ("", KEY_END); Key * rmp = keyNew ("", KEY_VALUE, "root", KEY_END); elektraKeySetName (rmp, "/", KEY_CASCADING_NAME); Backend * b2 = 0; keySetName (searchKey, "user"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); compare_key (b2->mountpoint, rmp); Backend * backend = 0; keySetName (searchKey, "user/tests/simple"); backend = trieLookup (kdb->trie, searchKey); succeed_if (backend, "there should be a backend"); compare_key (backend->mountpoint, mp); keySetName (searchKey, "user/tests/simple/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key (b2->mountpoint, mp); keySetName (searchKey, "user/tests/simple/deep/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key (b2->mountpoint, mp); Key * dmp = keyNew ("", KEY_VALUE, "default", KEY_END); keySetName (searchKey, "system/elektra"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (b2 == kdb->defaultBackend, "should be the default backend"); compare_key (b2->mountpoint, dmp); keySetName (searchKey, "system/elektra/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (b2 == kdb->defaultBackend, "should be the default backend"); compare_key (b2->mountpoint, dmp); keyDel (dmp); keyDel (mp); keyDel (rmp); keyDel (searchKey); kdb_del (kdb); keyDel (errorKey); ksDel (modules); }
static void test_cascading () { printf ("Test simple mount with cascading\n"); KDB * kdb = kdb_new (); Key * errorKey = keyNew (0); KeySet * modules = modules_config (); succeed_if (mountOpen (kdb, cascading_config (), modules, errorKey) == 0, "could not open trie"); succeed_if (mountDefault (kdb, modules, 1, errorKey) == 0, "could not mount default backend"); succeed_if (output_warnings (errorKey), "warnings found"); succeed_if (output_error (errorKey), "error found"); exit_if_fail (kdb->trie, "kdb->trie was not build up successfully"); succeed_if (kdb->split->size == 7, "size of split not correct"); Key * mp = keyNew ("dir/tests/simple", KEY_VALUE, "simple", KEY_END); compare_key (mp, kdb->split->parents[0]); keySetName (mp, "user/tests/simple"); keySetString (mp, "simple"); compare_key (mp, kdb->split->parents[1]); keySetName (mp, "system/tests/simple"); keySetString (mp, "simple"); compare_key (mp, kdb->split->parents[2]); keyDel (mp); // output_split (kdb->split); // output_trie (kdb->trie); Key * searchKey = keyNew ("user", KEY_END); Backend * backend = trieLookup (kdb->trie, searchKey); succeed_if (!backend, "there should be no backend"); keySetName (searchKey, "system"); backend = trieLookup (kdb->trie, searchKey); succeed_if (!backend, "there should be no backend"); mp = keyNew ("", KEY_VALUE, "simple", KEY_END); elektraKeySetName (mp, "/tests/simple", KEY_CASCADING_NAME); keySetName (searchKey, "user/tests/simple"); backend = trieLookup (kdb->trie, searchKey); succeed_if (backend, "there should be a backend"); compare_key (backend->mountpoint, mp); keySetName (searchKey, "user/tests/simple/below"); Backend * b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key (b2->mountpoint, mp); keySetName (searchKey, "user/tests/simple/deep/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key (b2->mountpoint, mp); keySetName (searchKey, "system/tests/simple"); backend = trieLookup (kdb->trie, searchKey); succeed_if (backend, "there should be a backend"); compare_key (backend->mountpoint, mp); keySetName (searchKey, "system/tests/simple/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key (b2->mountpoint, mp); keySetName (searchKey, "system/tests/simple/deep/below"); b2 = trieLookup (kdb->trie, searchKey); succeed_if (b2, "there should be a backend"); succeed_if (backend == b2, "should be same backend"); compare_key (b2->mountpoint, mp); keyDel (errorKey); ksDel (modules); keyDel (mp); keyDel (searchKey); kdb_del (kdb); }
/** * @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; }