static KeySet* createMergeTestkeys(void) { /* the keys to be converted are merged together * into a single metadata */ KeySet* ks = ksNew(0, KS_END); for (int i = 1; i <= 3; i++) { Key* key = createMergingKey (i); keySetMeta (key, "convert/metaname", "testmeta"); keySetMeta (key, "convert/append", "next"); ksAppendKey (ks, key); } ksAppendKey (ks, keyNew ("user/normalkey1", KEY_META, "order", "10", KEY_END)); ksAppendKey (ks, keyNew ("user/normalkey2", KEY_META, "order", "20", KEY_END)); for (int i = 30; i <= 32; i++) { Key* key = createMergingKey (i); keySetMeta (key, "convert/metaname", "testmeta"); keySetMeta (key, "convert/append", "previous"); ksAppendKey (ks, key); } return ks; }
static void test_enumdispatcher (void) { printf ("test_enumdispatcher\n"); Key * parentKey = keyNew ("user/tests/regexdispatcher", KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("regexdispatcher"); KeySet * ks = ksNew (1, KS_END); Key * key1 = keyNew ("/key1", KEY_META, "check/enum/#1", "a", KEY_META, "check/enum/#2", "b", KEY_END); Key * key2 = keyNew ("/key2", KEY_META, "check/enum/#1", "a", KEY_META, "check/enum/#2", "b", KEY_META, "check/enum/multi", ",", KEY_END); ksAppendKey (ks, key1); ksAppendKey (ks, key2); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful"); const Key * pKey1 = ksLookupByName (ks, "/key1", KDB_O_NONE); const Key * checkEnum1 = keyGetMeta (pKey1, "check/validation"); succeed_if (checkEnum1, "the single enum regex hasn't been generated"); succeed_if (0 == strcmp (keyString (checkEnum1), "a|b"), "the single enum regex is invalid"); const Key * pKey2 = ksLookupByName (ks, "/key2", KDB_O_NONE); const Key * checkEnum2 = keyGetMeta (pKey2, "check/validation"); succeed_if (checkEnum2, "the multi enum regex hasn't been generated"); succeed_if (0 == strcmp (keyString (checkEnum2), "(a,b)|(b,a)"), "the mutli enum regex is invalid"); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
void icase_test (void) { Key * parentKey = keyNew ("user/tests/validation", KEY_VALUE, "", KEY_END); Key * k1 = keyNew ("user/tests/validation/valid1", KEY_VALUE, "WORD", KEY_META, "check/validation", "word", KEY_META, "check/validation/word", "", KEY_META, "check/validation/ignorecase", "", KEY_END); Key * k2 = keyNew ("user/tests/validation/valid2", KEY_VALUE, "word1 word2 word3", KEY_META, "check/validation", "wORd2", KEY_META, "check/validation/word", "", KEY_META, "check/validation/ignorecase", "", KEY_END); KeySet * conf = ksNew (0, KS_END); KeySet * ks; PLUGIN_OPEN ("validation"); ks = ksNew (2, KS_END); ksAppendKey (ks, k1); ksRewind (ks); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed"); ksDel (ks); ks = ksNew (2, KS_END); ksAppendKey (ks, k2); ksRewind (ks); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (1), "kdbSet failed"); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); }
static void test_ksPop (const size_t storagePlugin, const char * tmpFile) { Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END); open_storage_plugin (storagePlugin); Plugin * plugin = plugins[storagePlugin]; KeySet * ks = simpleTestKeySet (); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); KeySet * poppedKeys = ksNew (0, KS_END); succeed_if (ksAppendKey (poppedKeys, ksPop (ks)) != -1, "ksAppendKey failed"); succeed_if (ksAppendKey (poppedKeys, ksPop (ks)) != -1, "ksAppendKey failed"); succeed_if (ksGetSize (ks) == 1, "ksGetSize after ksPop should be decremented"); succeed_if (ksAppendKey (poppedKeys, ksPop (ks)) != -1, "ksAppendKey failed"); succeed_if (ksGetSize (poppedKeys) == 3, "expecting three keys to be in ks"); succeed_if (ksPop (ks) == 0, "ks should be empty"); succeed_if (ksAppendKey (poppedKeys, ksPop (ks)) == -1, "ks should be empty, but is not"); KeySet * test = simpleTestKeySet (); compare_keyset (poppedKeys, test); ksDel (test); ksDel (poppedKeys); keyDel (parentKey); ksDel (ks); closeStoragePlugin (storagePlugin); }
static void test_withoutConfig() { Key *parentKey = keyNew ("user/tests/rename", KEY_END); Key *parentKeyCopy = keyDup(parentKey); KeySet *conf = ksNew (0, KS_END); PLUGIN_OPEN("rename"); KeySet *ks = createSimpleTestKeys(); ksAppendKey(ks, parentKey); succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); succeed_if(output_error (parentKey), "error in kdbGet"); succeed_if(output_warnings (parentKey), "warnings in kdbGet"); KeySet *expected = createSimpleTestKeys(); ksAppendKey(expected, parentKeyCopy); compareKeySets(ks, expected); keyDel (parentKey); keyDel (parentKeyCopy); ksDel(expected); ksDel(ks); PLUGIN_CLOSE (); }
/** @retval 0 if ksCurrent does not hold an array entry @retval 1 if the array entry will be used because its the first @retval 2 if a new array entry was created @retval -1 error in snprintf */ static int elektraYajlIncrementArrayEntry (KeySet * ks) { Key * current = ksCurrent (ks); const char * baseName = keyBaseName (current); if (baseName && *baseName == '#') { current = keyNew (keyName (current), KEY_END); if (!strcmp (baseName, "###empty_array")) { // get rid of previous key keyDel (ksLookup (ks, current, KDB_O_POP)); // we have a new array entry keySetBaseName (current, 0); keyAddName (current, "#0"); ksAppendKey (ks, current); return 1; } else { // we are in an array elektraArrayIncName (current); ksAppendKey (ks, current); return 2; } } else { // previous entry indicates this is not an array return 0; } }
KeySet * elektraMetaArrayToKS (Key * key, const char * metaName) { const Key * meta = keyGetMeta (key, metaName); if (!meta) return NULL; KeySet * result = ksNew (0, KS_END); if (keyString (meta)[0] != '#') { ksAppendKey (result, (Key *)meta); ksRewind (result); return result; } ksAppendKey (result, keyDup (meta)); Key * currentKey = keyDup (meta); keyAddName (currentKey, "#"); elektraArrayIncName (currentKey); Key * curMeta = NULL; while ((curMeta = (Key *)keyGetMeta (key, keyName (currentKey))) != NULL) { ksAppendKey (result, keyDup (curMeta)); elektraArrayIncName (currentKey); } keyDel (currentKey); ksRewind (result); return result; }
int elektraDbusSet (Plugin * handle, KeySet * returned, Key * parentKey) { KeySet * oldKeys = (KeySet *)elektraPluginGetData (handle); // because elektraLogchangeGet will always be executed before elektraLogchangeSet // we know that oldKeys must exist here! ksRewind (oldKeys); ksRewind (returned); KeySet * addedKeys = ksDup (returned); KeySet * changedKeys = ksNew (0, KS_END); KeySet * removedKeys = ksNew (0, KS_END); Key * k = 0; while ((k = ksNext (oldKeys)) != 0) { Key * p = ksLookup (addedKeys, k, KDB_O_POP); // Note: keyDel not needed, because at least two references exist if (p) { if (keyNeedSync (p)) { ksAppendKey (changedKeys, p); } } else { ksAppendKey (removedKeys, k); } } if (!strncmp (keyName (parentKey), "user", 4)) { announceKeys (addedKeys, "KeyAdded", DBUS_BUS_SESSION); announceKeys (changedKeys, "KeyChanged", DBUS_BUS_SESSION); announceKeys (removedKeys, "KeyDeleted", DBUS_BUS_SESSION); } else if (!strncmp (keyName (parentKey), "system", 6)) { announceKeys (addedKeys, "KeyAdded", DBUS_BUS_SYSTEM); announceKeys (changedKeys, "KeyChanged", DBUS_BUS_SYSTEM); announceKeys (removedKeys, "KeyDeleted", DBUS_BUS_SYSTEM); } ksDel (oldKeys); ksDel (addedKeys); ksDel (changedKeys); ksDel (removedKeys); // for next invocation of elektraLogchangeSet, remember our current keyset elektraPluginSetData (handle, ksDup (returned)); return 1; /* success */ }
static KeySet * nextPackage (FILE * fp, Key * parentKey) { char * line = elektraMalloc (DPKG_LINE_MAX); KeySet * package = ksNew (500, KS_END); Key * lastKey = NULL; Key * baseKey = NULL; int notDone = 0; while (fgets (line, DPKG_LINE_MAX, fp) != NULL) { if (*line == '\n') break; if (*line == ' ') { if (strchr (line, '\n')) notDone = 0; else notDone = 1; appendToKey (lastKey, line); } else if (notDone) { if (strchr (line, '\n')) notDone = 0; appendToKey (lastKey, line); } else { if (!strchr (line, '\n')) notDone = 1; char * section = line; char * data = strchr (line, ':'); if (data) *data = '\0'; ++data; // skip : ++data; // skip whitespace strtok (data, "\n"); // remove newline if (!strcmp (section, "Package")) { baseKey = keyDup (parentKey); keyAddBaseName (baseKey, data); lastKey = baseKey; ksAppendKey (package, baseKey); } else { Key * key = keyDup (baseKey); keyAddBaseName (key, section); keySetString (key, data); lastKey = key; ksAppendKey (package, key); } } memset (line, 0, DPKG_LINE_MAX); } elektraFree (line); return package; }
static void collect_mountpoints(Trie *trie, KeySet *mountpoints) { int i; for (i=0; i<KDB_MAX_UCHAR; ++i) { if (trie->value[i]) ksAppendKey(mountpoints, ((Backend*) trie->value[i])->mountpoint); if (trie->children[i]) collect_mountpoints(trie->children[i], mountpoints); } if (trie->empty_value) { ksAppendKey(mountpoints, ((Backend*) trie->empty_value)->mountpoint); } }
static void setSectionNumber(Key *parentKey, Key *key, KeySet *ks) { if (!strcmp(keyBaseName(key), INTERNAL_ROOT_SECTION)) { Key *tmpKey = keyDup(key); keySetMeta(tmpKey, "ini/section", "0"); keySetMeta(key, "ini/section", "0"); keySetString(tmpKey, 0); ksAppendKey(ks, tmpKey); keyDel(tmpKey); return; } Key *lookupKey = keyDup(key); Key *lastKey = keyDup(lookupKey); while (1) { if (!strcmp(keyName(lookupKey), keyName(parentKey))) { if (keyGetMeta(parentKey, "ini/lastSection")) { long previousSection = atol(keyString(keyGetMeta(parentKey, "ini/lastSection"))); ++previousSection; char buffer[21]; //20 digits (long) + \0 snprintf(buffer, sizeof (buffer), "%ld", previousSection); keySetMeta(parentKey, "ini/lastSection", buffer); keySetMeta(key, "ini/section", buffer); } else { keySetMeta(parentKey, "ini/lastSection", "1"); keySetMeta(parentKey, "ini/section", "0"); keySetMeta(key, "ini/section", "1"); } keySetMeta(lastKey, "ini/section", keyString(keyGetMeta(key, "ini/section"))); ksAppendKey(ks, lastKey); break; } if (keyGetMeta(ksLookup(ks, lookupKey, KDB_O_NONE), "ini/section")) { keySetMeta(key, "ini/section", keyString(keyGetMeta(ksLookup(ks, lookupKey, KDB_O_NONE), "ini/section"))); break; } keySetName(lastKey, keyName(lookupKey)); keyAddName(lookupKey, ".."); } keyDel(lookupKey); keyDel(lastKey); }
/** * @internal * Add plugin at placement to list plugin configuration and apply it. * * @param list List plugin * @param plugin Plugin to add * @param placement Placement name * @retval 0 on error * @retval 0 on success */ static int listAddPlugin (Plugin * list, Plugin * plugin, char * placement) { ELEKTRA_NOT_NULL (list); ELEKTRA_NOT_NULL (plugin); ELEKTRA_NOT_NULL (placement); KeySet * newConfig = ksDup (list->config); // Find name for next item in plugins array Key * configBase = keyNew ("user/plugins", KEY_END); KeySet * array = elektraArrayGet (configBase, newConfig); Key * pluginItem = elektraArrayGetNextKey (array); ELEKTRA_NOT_NULL (pluginItem); keySetString (pluginItem, plugin->name); keyDel (configBase); // Create key with plugin handle Key * pluginHandle = keyDup (pluginItem); keyAddName (pluginHandle, "handle"); keySetBinary (pluginHandle, &plugin, sizeof (plugin)); // Create key with plugin placement char * placementType = placementToListPositionType (placement); if (placementType == NULL) { keyDel (configBase); keyDel (pluginItem); keyDel (pluginHandle); return 0; } Key * pluginPlacements = keyDup (pluginItem); keyAddName (pluginPlacements, "placements/"); keyAddName (pluginPlacements, placementType); keySetString (pluginPlacements, placement); // Append keys to list plugin configuration ksAppendKey (newConfig, pluginItem); ksAppendKey (newConfig, pluginHandle); ksAppendKey (newConfig, pluginPlacements); ksDel (array); ksDel (list->config); // Apply new configuration list->config = newConfig; list->kdbOpen (list, NULL); return 1; }
static void test_keySetBinary (const size_t storagePlugin, const char * tmpFile) { Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END); open_storage_plugin (storagePlugin); Plugin * plugin = plugins[storagePlugin]; KeySet * ks = metaTestKeySet (); const char * name = "user/tests/storage/specialkey"; size_t realValueSize = 42; void * value = elektraMalloc (realValueSize); memset (value, 42, realValueSize); Key * key = keyNew (name, KEY_END); keySetBinary (key, value, realValueSize); ksAppendKey (ks, key); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); Key * found = ksLookupByName (ks, name, KDB_O_POP); succeed_if (found, "did not find key"); // now set a new key value to the Key _after_ kdbGet size_t newValueSize = 4096; void * newValue = elektraMalloc (newValueSize); memset (newValue, 253, newValueSize); succeed_if (keySetBinary (found, newValue, newValueSize) == (ssize_t) newValueSize, "Key binary could not be set"); ksAppendKey (ks, found); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); found = ksLookupByName (ks, name, 0); succeed_if (found, "did not find key"); ssize_t apiValueSize = keyGetValueSize (found); char * apiValue = elektraMalloc (apiValueSize); succeed_if (keyGetBinary (found, apiValue, apiValueSize) == (ssize_t) newValueSize, "Key binary has wrong size"); succeed_if (elektraStrNCmp (value, apiValue, realValueSize) != 0, "Key binary value is wrong"); succeed_if (elektraStrNCmp (newValue, apiValue, newValueSize) == 0, "Key binary value is wrong"); elektraFree (newValue); elektraFree (apiValue); elektraFree (value); keyDel (parentKey); ksDel (ks); closeStoragePlugin (storagePlugin); }
static void test_metaConfigTakesPrecedence() { Key *parentKey = keyNew ("user/tests/rename", KEY_END); KeySet *conf = ksNew (20, keyNew ("system/cut", KEY_VALUE, "will/be", KEY_END), KS_END); PLUGIN_OPEN("rename"); KeySet *ks = createSimpleMetaTestKeys(); ksAppendKey(ks, parentKey); succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); succeed_if(output_error (parentKey), "error in kdbGet"); succeed_if(output_warnings (parentKey), "warnings in kdbGet"); /* the first two keys should have been renamed by their metadata */ Key* key = ksLookupByName (ks, "user/tests/rename/key1", KDB_O_NONE); succeed_if(key, "key1 was not correctly renamed"); key = ksLookupByName (ks, "user/tests/rename/key2", KDB_O_NONE); succeed_if(key, "key2 was not correctly renamed"); /* the third key should have been renamed by the global config */ key = ksLookupByName (ks, "user/tests/rename/stripped", KDB_O_NONE); succeed_if(key, "key3 was renamed but would replace the parent key"); /* the fourth key was not renamed because the prefix did not match */ key = ksLookupByName (ks, "user/tests/rename/will/not/be/stripped/key4", KDB_O_NONE); succeed_if(key, "key4 was renamed although its prefix did not match"); keyDel (parentKey); ksDel(ks); PLUGIN_CLOSE (); }
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 (); }
static void test_keyValue (const size_t storagePlugin, const char * tmpFile) { Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END); open_storage_plugin (storagePlugin); Plugin * plugin = plugins[storagePlugin]; KeySet * ks = metaTestKeySet (); const char * name = "user/tests/storage/specialkey"; size_t valueSize = 42; void * value = elektraMalloc (valueSize); memset (value, 42, valueSize); Key * key = keyNew (name, KEY_END); keySetBinary (key, value, valueSize); ksAppendKey (ks, keyDup (key)); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); Key * found = ksLookupByName (ks, name, 0); succeed_if (found, "did not find key"); compare_key (key, found); elektraFree (value); keyDel (parentKey); ksDel (ks); keyDel (key); closeStoragePlugin (storagePlugin); }
static void test_keyGetString (const size_t storagePlugin, const char * tmpFile) { Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END); open_storage_plugin (storagePlugin); Plugin * plugin = plugins[storagePlugin]; KeySet * ks = metaTestKeySet (); const char * name = "user/tests/storage/specialkey"; const char * value = "special value"; size_t realValueSize = elektraStrLen (value); Key * key = keyNew (name, KEY_VALUE, value, KEY_END); ksAppendKey (ks, key); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); Key * found = ksLookupByName (ks, name, 0); succeed_if (found, "did not find key"); ssize_t apiValueSize = keyGetValueSize (found); char * apiString = elektraMalloc (apiValueSize); succeed_if (keyGetString (found, apiString, apiValueSize) == (ssize_t) realValueSize, "Key string has wrong size"); succeed_if (elektraStrNCmp (value, apiString, realValueSize) == 0, "Key string value is wrong"); elektraFree (apiString); keyDel (parentKey); ksDel (ks); closeStoragePlugin (storagePlugin); }
/** * Appoints all keys from ks to yet unsynced splits. * * @pre elektraSplitBuildup() need to be executed before. * * @param split the split object to work with * @param handle to determine to which backend a key belongs * @param ks the keyset to appoint to split * * @retval 1 on success * @retval -1 if no backend was found for a key * @ingroup split */ int elektraSplitAppoint (Split * split, KDB * handle, KeySet * ks) { ssize_t curFound = 0; /* If key could be appended to any of the existing split keysets */ Key * curKey = 0; Backend * curHandle = 0; ssize_t defFound = elektraSplitAppend (split, 0, 0, 0); ksRewind (ks); while ((curKey = ksNext (ks)) != 0) { curHandle = elektraMountGetBackend (handle, curKey); if (!curHandle) return -1; curFound = elektraSplitSearchBackend (split, curHandle, curKey); if (curFound == -1) curFound = defFound; if (split->syncbits[curFound] & SPLIT_FLAG_SYNC) { continue; } ksAppendKey (split->keysets[curFound], curKey); } return 1; }
static void test_replaceString() { Key *parentKey = keyNew("user/tests/rename", KEY_END); KeySet *conf = ksNew(20, keyNew("system/cut", KEY_VALUE, "will/be/stripped", KEY_END), keyNew("system/replacewith", KEY_VALUE, "stripped/it/is", KEY_END), KS_END); KeySet *ks = createSimpleTestKeys(); ksAppendKey(ks, parentKey); PLUGIN_OPEN("rename"); succeed_if(plugin->kdbGet(plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); Key *key = ksLookupByName(ks, "user/tests/rename/stripped/it/is/key1", KDB_O_NONE); succeed_if(key, "key1 was not correctly rename"); key = ksLookupByName(ks, "user/tests/rename/stripped/it/is/key2", KDB_O_NONE); succeed_if(key, "key2 was not correctly rename"); key = ksLookupByName(ks, "user/tests/rename/will/not/be/stripped/key4", KDB_O_NONE); succeed_if(key, "key4 was not correctly rename"); keyDel(parentKey); ksDel(ks); PLUGIN_CLOSE(); }
static void test_simpleCutOnGet () { Key *parentKey = keyNew ("user/tests/rename", KEY_END); KeySet *conf = ksNew (20, keyNew ("system/cut", KEY_VALUE, "will/be/stripped", KEY_END), KS_END); PLUGIN_OPEN("rename"); KeySet *ks = createSimpleTestKeys(); ksAppendKey(ks, parentKey); succeed_if(plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful"); succeed_if(output_error (parentKey), "error in kdbGet"); succeed_if(output_warnings (parentKey), "warnings in kdbGet"); checkSimpleTestKeys (ks); ksDel(ks); /* * this has to be done because the parentKey is not * part of ks anymore due to renaming */ keyDel(parentKey); PLUGIN_CLOSE (); }
static KeySet * getGlobKeys (Key * parentKey, KeySet * keys, enum GlobDirection direction) { KeySet * glob = ksNew (0, KS_END); Key * k = 0; size_t parentsize = keyGetNameSize (parentKey); Key * userGlobConfig = 0; Key * systemGlobConfig = 0; Key * userDirGlobConfig = 0; Key * systemDirGlobConfig = 0; userGlobConfig = keyNew ("user/glob", KEY_END); systemGlobConfig = keyNew ("system/glob", KEY_END); switch (direction) { case GET: userDirGlobConfig = keyNew ("user/glob/get", KEY_END); systemDirGlobConfig = keyNew ("system/glob/get", KEY_END); break; case SET: userDirGlobConfig = keyNew ("user/glob/set", KEY_END); systemDirGlobConfig = keyNew ("system/glob/set", KEY_END); break; } while ((k = ksNext (keys)) != 0) { /* use only glob keys for the current direction */ if (keyIsDirectBelow (userGlobConfig, k) || keyIsDirectBelow (systemGlobConfig, k) || keyIsDirectBelow (userDirGlobConfig, k) || keyIsDirectBelow (systemDirGlobConfig, k)) { keySetMeta (k, "glob/flags", getGlobFlags (keys, k)); /* Look if we have a string */ size_t valsize = keyGetValueSize (k); if (valsize < 2) continue; /* We now know we want that key. Dup it to not change the configuration. */ Key * ins = keyDup (k); /* Now look if we want cascading for the key */ if (keyString (k)[0] == '/') { char * newstring = elektraMalloc (valsize + parentsize); strcpy (newstring, keyName (parentKey)); strcat (newstring, keyString (k)); keySetString (ins, newstring); elektraFree (newstring); } ksAppendKey (glob, ins); } } keyDel (userGlobConfig); keyDel (systemGlobConfig); keyDel (userDirGlobConfig); keyDel (systemDirGlobConfig); return glob; }
/** * @brief return only those keys from the given * keyset that pass the supplied filter function * with the supplied argument * * @param result the keyset that should contain the filtered keys * @param input the keyset whose keys should be filtered * @param filter a function pointer to a function that will be used to * filter the keyset. A key will be taken if the function returns a value * greater than 0. * @param argument an argument that will be passed to the filter function * each time it is called * @return the number of filtered keys if the filter function always * returned a positive value, -1 otherwise * @retval NULL on NULL pointer */ int elektraKsFilter (KeySet *result, KeySet *input, int (*filter) (const Key *k, void *argument), void *argument) { if (!result) return -1; if (!input) return -1; if (!filter) return -1; int rc = 0; int ret = 0; Key *current; cursor_t cursor = ksGetCursor (input); ksRewind (input); while ((current = ksNext (input)) != 0) { rc = filter (current, argument); if (rc <= -1) return -1; else if (rc > 0) { ++ ret; ksAppendKey(result, keyDup (current)); } } ksSetCursor(input, cursor); return ret; }
static void test_defaultdispatcher (void) { printf ("test_defaultdispatcher\n"); Key * parentKey = keyNew ("user/tests/regexdispatcher", KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("regexdispatcher"); KeySet * ks = ksNew (1, KS_END); Key * key = keyNew ("/key", KEY_META, "default", ".\\+*?[^]$(){}=!<>|:-asfdjklö123", KEY_END); ksAppendKey (ks, key); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful"); const Key * pKey = ksLookupByName (ks, "/key", KDB_O_NONE); const Key * defaultValue = keyGetMeta (pKey, "defaultValue"); succeed_if (defaultValue, "the default value regex hasn't been generated"); succeed_if (0 == strcmp (keyString (defaultValue), "\\.\\\\\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:\\-asfdjklö123"), "the default value regex is invalid"); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
static void test_rangedispatcher (void) { printf ("test_rangedispatcher\n"); Key * parentKey = keyNew ("user/tests/regexdispatcher", KEY_END); KeySet * conf = ksNew (0, KS_END); PLUGIN_OPEN ("regexdispatcher"); KeySet * ks = ksNew (1, KS_END); Key * key1 = keyNew ("/key1", KEY_META, "check/range", "0-5000", KEY_END); ksAppendKey (ks, key1); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful"); const Key * pKey1 = ksLookupByName (ks, "/key1", KDB_O_NONE); const Key * checkRange1 = keyGetMeta (pKey1, "check/validation"); succeed_if (checkRange1, "the range regex hasn't been generated"); succeed_if (0 == strcmp (keyString (checkRange1), "[0-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-4][0-9][0-9][0-9]|5000"), "the range regex is invalid"); keyDel (parentKey); ksDel (ks); PLUGIN_CLOSE (); }
/** * @brief Takes the first key and cuts off this common part * for all other keys, instead name will be prepended * * @return a new allocated keyset with keys in user namespace. * * The first key is removed in the resulting keyset. */ KeySet* elektraRenameKeys(KeySet *config, const char* name) { Key *root; Key *cur; ssize_t rootSize = 0; ksRewind(config); root = ksNext (config); rootSize = keyGetNameSize(root); keyDel (ksLookup (config, root, KDB_O_POP)); KeySet *newConfig = ksNew(ksGetSize(config), KS_END); if (rootSize == -1) return newConfig; while ((cur = ksPop(config)) != 0) { Key *dupKey = keyDup(cur); keySetName(dupKey, name); keyAddName(dupKey, keyName(cur)+rootSize-1); ksAppendKey(newConfig, dupKey); keyDel(cur); } return newConfig; }
// 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; }
int elektraLineRead (FILE * fp, KeySet * returned) { char * value = NULL; size_t len = 0; ssize_t n = 0; Key * read = NULL; // Read in each line while ((n = getline (&value, &len, fp)) != -1) { // Remove trailing newline if (value[n - 1] == '\n') { value[n - 1] = '\0'; } read = keyDup (ksTail (returned)); if (elektraArrayIncName (read) == -1) { elektraFree (value); keyDel (read); return -1; } keySetString (read, value); ksAppendKey (returned, read); } elektraFree (value); return 1; }
static void test_ksAppendKey (const size_t storagePlugin, const char * tmpFile) { Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END); open_storage_plugin (storagePlugin); Plugin * plugin = plugins[storagePlugin]; KeySet * ks = simpleTestKeySet (); ssize_t origSize = ksGetSize (ks); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); ssize_t appendSize = 0; Key * toAppend = keyNew (TEST_ROOT_KEY "/my/new/key", KEY_END); if ((appendSize = ksAppendKey (ks, toAppend)) == -1) { yield_error ("ksAppendKey failed"); } succeed_if (appendSize == (origSize + 1), "ksAppendKey after append should be incremented"); succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful"); ksDel (ks); ks = ksNew (0, KS_END); succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful"); ssize_t returnedSize = ksGetSize (ks); succeed_if (returnedSize == (origSize + 1), "ksGetSize after append should be incremented"); keyDel (parentKey); ksDel (ks); closeStoragePlugin (storagePlugin); }
static void test_timeoutConnect (void) { printf ("test connect timeout\n"); Key * parentKey = keyNew ("system/tests/foo", KEY_END); Key * toAdd = keyNew ("system/tests/foo/bar", KEY_END); KeySet * ks = ksNew (0, KS_END); KeySet * conf = ksNew (3, keyNew ("/endpoint", KEY_VALUE, TEST_ENDPOINT, KEY_END), keyNew ("/connectTimeout", KEY_VALUE, TESTCONFIG_CONNECT_TIMEOUT, KEY_END), keyNew ("/subscribeTimeout", KEY_VALUE, TESTCONFIG_SUBSCRIBE_TIMEOUT, KEY_END), KS_END); PLUGIN_OPEN ("zeromqsend"); // initial get to save current state plugin->kdbGet (plugin, ks, parentKey); // add key to keyset ksAppendKey (ks, toAdd); plugin->kdbSet (plugin, ks, parentKey); char * expectedWarningNumber = elektraFormat ("%d", ELEKTRA_WARNING_ZEROMQSEND_TIMEOUT); succeed_if (keyGetMeta (parentKey, "warnings"), "warning meta key was not set"); succeed_if_same_string (expectedWarningNumber, keyValue (keyGetMeta (parentKey, "warnings/#00/number"))); ksDel (ks); keyDel (parentKey); PLUGIN_CLOSE (); elektraFree (expectedWarningNumber); }
/** * Splits up the keysets and search for a sync bit in every key. * * It does not check if there were removed keys, * see elektraSplitSync() for the next step. * * It does not create new backends, this has to be * done by buildup before. * * @pre elektraSplitBuildup() need to be executed before. * * @param split the split object to work with * @param handle to get information where the individual keys belong * @param ks the keyset to divide * * @retval 0 if there were no sync bits * @retval 1 if there were sync bits * @retval -1 if no backend was found for any key * @ingroup split */ int elektraSplitDivide (Split * split, KDB * handle, KeySet * ks) { ssize_t curFound = 0; /* If key could be appended to any of the existing split keysets */ int needsSync = 0; Key * curKey = 0; Backend * curHandle = 0; ksRewind (ks); while ((curKey = ksNext (ks)) != 0) { // TODO: handle keys in wrong namespaces curHandle = elektraMountGetBackend (handle, curKey); if (!curHandle) return -1; curFound = elektraSplitSearchBackend (split, curHandle, curKey); if (curFound == -1) continue; // key not relevant in this kdbSet ksAppendKey (split->keysets[curFound], curKey); if (keyNeedSync (curKey) == 1) { split->syncbits[curFound] |= 1; needsSync = 1; } } return needsSync; }