Пример #1
0
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;
}
Пример #2
0
static void test_lookupDefaultCascading ()
{
	printf ("Test lookup default with cascading\n");

	Key * specKey = keyNew ("/abc", KEY_CASCADING_NAME, KEY_END);
	Key * k = 0;
	KeySet * ks = ksNew (20, KS_END);

	succeed_if (ksGetSize (ks) == 0, "wrong size");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == 0, "found wrong key");

	keySetMeta (specKey, "default", "xyz");
	k = ksLookup (ks, specKey, KDB_O_SPEC);
	succeed_if (k != 0, "found no default key");
	succeed_if (ksGetSize (ks) == 1, "wrong size");
	succeed_if_same_string (keyName (k), "/abc");
	succeed_if_same_string (keyString (k), "xyz");

	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k, "did not find default key again");
	succeed_if (ksGetSize (ks) == 1, "wrong size");
	keySetMeta (specKey, "default", "");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k, "did not find default key again");
	succeed_if (ksGetSize (ks) == 1, "wrong size");

	keyDel (specKey);
	ksDel (ks);
}
Пример #3
0
static void test_cmpOrder()
{
	Key *k1 = keyNew ("user/a", KEY_META, "order", "20", KEY_END);
	Key *k2 = keyNew ("user/b", KEY_META, "order", "10", KEY_END);

	succeed_if (elektraKeyCmpOrder(0, 0) == 0, "null keys are not equal");
	succeed_if (elektraKeyCmpOrder(k1, 0) == 1, "not null key is not greater than null key");
	succeed_if (elektraKeyCmpOrder(0, k1) == -1, "null key is not smaller than not null key");

	succeed_if (elektraKeyCmpOrder(k1, k2) > 0, "user/a is not greater than user/b");
	succeed_if (elektraKeyCmpOrder(k2, k1) < 0, "user/b is not smaller than user/a");

	keySetMeta(k2, "order", "20");
	succeed_if (elektraKeyCmpOrder(k1, k2) == 0, "keys with same order are not equal");
	succeed_if (elektraKeyCmpOrder(k2, k1) == 0, "keys with same order are not equal");

	keySetMeta(k2, "order", 0);
	succeed_if (elektraKeyCmpOrder(k1, k2) > 0, "key with metadata is not greater than key without");
	succeed_if (elektraKeyCmpOrder(k2, k1) < 0, "key with metadata is not greater than key without");

	keySetMeta(k1, "order", 0);
	succeed_if (elektraKeyCmpOrder(k1, k2) == 0, "keys without metadata are not equal");
	succeed_if (elektraKeyCmpOrder(k2, k1) == 0, "keys without metadata are not equal");

	keyDel (k1);
	keyDel (k2);
}
Пример #4
0
static int iniSectionToElektraKey (void *vhandle, const char *section)
{
	CallbackHandle *handle = (CallbackHandle *)vhandle;
	Key *appendKey = keyDup (handle->parentKey);
	keySetMeta(appendKey, "ini/lastSection", 0);
	createUnescapedKey(appendKey, section);
	Key *existingKey = NULL;
	if ((existingKey = ksLookup(handle->result, appendKey, KDB_O_NONE)))
	{
		keyDel(appendKey);
		if(!handle->mergeSections)
		{
			ELEKTRA_SET_ERRORF(140, handle->parentKey, "Section name: %s\n", section);
			return 0;
		}
		keySetMeta(existingKey, "ini/duplicate", "");
		return 1;
	}
	setSectionNumber(handle->parentKey, appendKey, handle->result);
	setOrderNumber(handle->parentKey, appendKey);
	keySetBinary(appendKey, 0, 0);	
	flushCollectedComment (handle, appendKey);
	ksAppendKey(handle->result, appendKey);

	return 1;
}
Пример #5
0
static void test_lookupChain ()
{
	printf ("Test lookup chain\n");

	Key * specKey = keyNew ("user/4", KEY_META, "override/#0", "user/something", KEY_END);
	Key * k1 = 0;
	Key * k2 = 0;
	Key * k3 = 0;
	Key * k4 = 0;
	KeySet * ks = ksNew (20, k1 = keyNew ("user/1", KEY_END), k2 = keyNew ("user/2", KEY_END), k3 = keyNew ("user/3", KEY_END),
			     k4 = keyNew ("user/4", KEY_END), KS_END);

	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k4, "found wrong key");
	keySetMeta (specKey, "override/#0", "user/else");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k4, "found wrong key");
	keySetMeta (specKey, "override/#1", "user/wrong");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k4, "found wrong key");
	keySetMeta (specKey, "override/#2", "user/3");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k3, "did not find override key");
	keySetMeta (specKey, "override/#1", "user/2");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k2, "found wrong key");
	keySetMeta (specKey, "override/#0", "user/1");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k1, "found wrong key");

	keyDel (specKey);
	ksDel (ks);
}
Пример #6
0
/**
 * Set a comment for a key.
 *
 * A key comment is like a configuration file comment.
 * See keySetComment() for more information.
 *
 * @param key the key object to work with
 * @param newComment the comment, that can be freed after this call.
 * @return the number of bytes actually saved including final NULL
 * @retval 0 when the comment was freed (newComment NULL or empty string)
 * @retval -1 on NULL pointer or memory problems
 * @see keyGetComment()
 */
ssize_t keySetComment (Key * key, const char * newComment)
{
	if (!key) return -1;
	if (!newComment || *newComment == 0)
	{
		keySetMeta (key, "comment", 0);
		return 1;
	}

	keySetMeta (key, "comment", newComment);
	return keyGetCommentSize (key);
}
Пример #7
0
static Key * prefToKey (Key * parentKey, PrefType type, const char * pref)
{
	Key * key = keyNew (keyName (parentKey), KEY_END);
	keyAddBaseName (key, prefix[type]);
	char * localString = elektraStrDup (pref);
	char * cPtr = strstr (localString, ",");
	*cPtr = '\0';
	char * sPtr = localString;
	++sPtr;
	*sPtr++ = '\0';
	char * ePtr = cPtr - 1;
	elektraRstrip (sPtr, &ePtr);
	size_t keyLen = ePtr - sPtr;
	char * prefKey = elektraMalloc (keyLen + 1);
	snprintf (prefKey, keyLen + 1, "%s", sPtr);
	char * tPtr = strtok (prefKey, ".");
	if (tPtr) keyAddBaseName (key, tPtr);
	while ((tPtr = strtok (NULL, ".")) != NULL)
	{
		keyAddBaseName (key, tPtr);
	}
	elektraFree (prefKey);
	sPtr = cPtr + 1;
	sPtr = elektraLskip (sPtr);
	ePtr = strrchr (sPtr, ')');
	*ePtr-- = '\0';
	elektraRstrip (sPtr, &ePtr);
	size_t argLen = ePtr - sPtr + 1;
	char * prefArg = elektraMalloc (argLen + 1);
	snprintf (prefArg, argLen + 1, "%s", sPtr);
	if (!strcmp (prefArg, "true") || !(strcmp (prefArg, "false")))
	{
		keySetMeta (key, "type", "boolean");
		keySetString (key, prefArg);
	}
	else if (prefArg[0] == '"' && prefArg[strlen (prefArg) - 1] == '"')
	{
		// TODO: else if list
		keySetMeta (key, "type", "string");
		*prefArg = '\0';
		*(prefArg + (strlen (prefArg + 1))) = '\0';
		keySetString (key, (prefArg + 1));
	}
	else
	{
		keySetMeta (key, "type", "integer");
		keySetString (key, prefArg);
	}
	elektraFree (prefArg);
	elektraFree (localString);
	return key;
}
Пример #8
0
static void clearError (Key * key)
{
	keySetMeta (key, "error", 0);
	keySetMeta (key, "error/number", 0);
	keySetMeta (key, "error/description", 0);
	keySetMeta (key, "error/reason", 0);
	keySetMeta (key, "error/ingroup", 0);
	keySetMeta (key, "error/module", 0);
	keySetMeta (key, "error/file", 0);
	keySetMeta (key, "error/line", 0);
	keySetMeta (key, "error/configfile", 0);
	keySetMeta (key, "error/mountpoint", 0);
}
Пример #9
0
static int copyError (Key * dest, Key * src)
{
	keyRewindMeta (src);
	const Key * metaKey = keyGetMeta (src, "error");
	if (!metaKey) return 0;
	keySetMeta (dest, keyName (metaKey), keyString (metaKey));
	while ((metaKey = keyNextMeta (src)) != NULL)
	{
		if (strncmp (keyName (metaKey), "error/", 6) != 0) break;
		keySetMeta (dest, keyName (metaKey), keyString (metaKey));
	}
	return 1;
}
Пример #10
0
void test_new()
{
	Key *key;
	key = keyNew ("user/test",
		KEY_META, "hello", "hello_world",
		KEY_META, "mode", "0644",
		KEY_META, "time", "1271234264",
		KEY_META, "empty", "",
		KEY_META, "", "empty",
		KEY_END);

	succeed_if (!strcmp(keyValue(keyGetMeta(key, "hello")), "hello_world"),
			"could not receive previously set meta information");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "mode")), "0644"), "mode not set correctly");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "time")), "1271234264"), "time not set correctly");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "empty")), ""), "Problem with empty meta string");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "")), "empty"), "Problem with empty name");

	keySetMeta(key, "", "full");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "")), "full"), "Problem with empty name");

	keySetMeta(key, "", 0);
	succeed_if (keyValue(keyGetMeta(key, "")) == 0, "could not remove empty meta data");

	keyDel (key);

	key = keyNew ("user/test",
		KEY_META, "hello", "goodbye",
		KEY_META, "mode", "0775",
		KEY_META, "time", "1271939923",
		KEY_META, "empty", "",
		KEY_META, "", "",
		KEY_END);

	succeed_if (!strcmp(keyValue(keyGetMeta(key, "hello")), "goodbye"),
			"could not receive previously set meta information");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "mode")), "0775"), "mode not set correctly");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "time")), "1271939923"), "time not set correctly");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "empty")), ""), "Problem with empty meta string");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "")), ""), "Problem with empty name");

	keySetMeta(key, "", "full");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "")), "full"), "Problem with empty name");

	keySetMeta(key, "", 0);
	succeed_if (keyValue(keyGetMeta(key, "")) == 0, "could not remove empty meta data");

	keyDel (key);
}
Пример #11
0
void keySetOrderMeta(Key *key, int order)
{
	char *buffer;
	asprintf (&buffer, "%d", order);
	keySetMeta (key, "order", buffer);
	free (buffer);
}
Пример #12
0
void test_iterate()
{
	Key *key;

	key = keyNew ("user/test", KEY_END);
	exit_if_fail (key, "could not create new key");
	succeed_if (keyRewindMeta(key) == 0, "Could not rewind empty key");
	succeed_if (keyNextMeta(key) == 0, "Could get next meta name, even if it is empty");
	succeed_if (keyCurrentMeta(key) == 0, "Could get next meta value, even if it is empty");

	keySetMeta (key, "meta1", "meta_value");
	succeed_if (keyRewindMeta(key) == 0, "Could not rewind key");
	succeed_if (!strcmp(keyName(keyNextMeta(key)), "meta1"), "keyNextMeta does not work at 1. iteration");
	succeed_if (!strcmp(keyValue(keyCurrentMeta(key)), "meta_value"), "keyCurrentMeta does not work at 1. iteration");

	succeed_if (keyNextMeta(key) == 0, "Could get next meta name, even if it is empty at 2. iteration");
	succeed_if (keyCurrentMeta(key) == 0, "Could get next meta value, even if it is empty at 2. iteration");

	succeed_if (keyNextMeta(key) == 0, "Could get next meta name, even if it is empty at 3. iteration");
	succeed_if (keyCurrentMeta(key) == 0, "Could get next meta value, even if it is empty at 3. iteration");

	succeed_if (keyNextMeta(key) == 0, "Could get next meta name, even if it is empty at 4. iteration");
	succeed_if (keyCurrentMeta(key) == 0, "Could get next meta value, even if it is empty at 4. iteration");

	keyDel (key);
}
Пример #13
0
static void setSubOrderNumber(Key *key, const char *oldOrder)
{
	char *lastIndexPtr = NULL;
	char *newOrder = elektraMalloc(elektraStrLen(oldOrder)+ELEKTRA_MAX_ARRAY_SIZE);
	if ((lastIndexPtr = strrchr(oldOrder, '/')))
	{
		kdb_long_long_t subIndex = 0;
		char *ptr = lastIndexPtr;
		++ptr; //skip /
		++ptr; //skip #
		while (*ptr == '_')
		{
			++ptr;
		}
		elektraReadArrayNumber(ptr, &subIndex);
		++subIndex;
		int len = (lastIndexPtr+1) - oldOrder;
		char buffer[ELEKTRA_MAX_ARRAY_SIZE];
		elektraWriteArrayNumber(buffer, subIndex);
		sprintf(newOrder, "%.*s%s", len, oldOrder, buffer);
	}
	else
	{
		sprintf(newOrder, "%s/#1", oldOrder);
	}
	keySetMeta(key, "order", newOrder);
	elektraFree(newOrder);
}
Пример #14
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;
}
Пример #15
0
static void test_mmap_metacopy (const char * tmpFile)
{
	Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
	KeySet * conf = ksNew (0, KS_END);
	PLUGIN_OPEN ("mmapstorage");
	KeySet * ks = metaTestKeySet ();

	Key * shareMeta = keyNew (0);
	keySetMeta (shareMeta, "sharedmeta", "shared meta key test");

	Key * current;
	ksRewind (ks);
	while ((current = ksNext (ks)) != 0)
	{
		keyCopyMeta (current, shareMeta, "sharedmeta");
	}
	KeySet * expected = ksDeepDup (ks);


	succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");

	KeySet * returned = ksNew (0, KS_END);
	succeed_if (plugin->kdbGet (plugin, returned, parentKey) == 1, "kdbGet was not successful");


	compare_keyset (expected, returned);

	ksDel (expected);
	ksDel (returned);

	keyDel (parentKey);
	keyDel (shareMeta);
	ksDel (ks);
	PLUGIN_CLOSE ();
}
Пример #16
0
static void validateWildcardSubs (KeySet * ks, Key * key, Key * specKey)
{
	const Key * requiredMeta = keyGetMeta (specKey, "required");
	if (!requiredMeta) return;
	Key * tmpParent = keyDup (key);
	keySetBaseName (tmpParent, 0);
	Key * parent = ksLookup (ks, tmpParent, KDB_O_NONE);
	keyDel (tmpParent);
	if (parent == NULL) return;
	KeySet * ksCopy = ksDup (ks);
	KeySet * subKeys = ksCut (ksCopy, parent);
	Key * cur;
	long subCount = 0;
	while ((cur = ksNext (subKeys)) != NULL)
	{
		if (keyIsDirectBelow (parent, cur)) ++subCount;
	}
	long required = atol (keyString (requiredMeta));
	if (required != subCount)
	{
		char buffer[MAX_CHARS_IN_LONG + 1];
		snprintf (buffer, sizeof (buffer), "%ld", subCount);
		keySetMeta (parent, "conflict/invalid/subcount", buffer);
	}

	ksDel (subKeys);
	ksDel (ksCopy);
}
Пример #17
0
//! [Shared Meta All]
void o(KeySet *ks)
{
	Key *current;
	Key *shared = keyNew (0);
	keySetMeta(shared, "shared1", "this meta data should be shared among many keys");
	keySetMeta(shared, "shared2", "this meta data should be shared among many keys also");
	keySetMeta(shared, "shared3", "this meta data should be shared among many keys too");

	ksRewind(ks);
	while ((current = ksNext(ks)) != 0)
	{
		if (needsSharedData(current)) keyCopyAllMeta(current, shared);
	}

	keyDel(shared);
}
Пример #18
0
static void validateArrayRange (Key * parent, long validCount, Key * specKey)
{
	const Key * arrayRange = keyGetMeta (specKey, "array");
	if (arrayRange != NULL)
	{
		char * rangeString = elektraMalloc (keyGetValueSize (arrayRange));
		keyGetString (arrayRange, rangeString, keyGetValueSize (arrayRange));
		char * delimPtr = strchr (rangeString, '-');
		long min = 0;
		long max = 0;
		if (delimPtr)
		{
			char * maxString = delimPtr + 1;
			*delimPtr = '\0';
			char * minString = rangeString;
			min = atoi (minString);
			max = atoi (maxString);
		}
		else
		{
			min = max = atoi (rangeString);
		}
		if (validCount < min || validCount > max)
		{
			char buffer[MAX_CHARS_IN_LONG + 1];
			snprintf (buffer, sizeof (buffer), "%ld", validCount);
			keySetMeta (parent, "conflict/range", buffer);
		}
		elektraFree (rangeString);
	}
}
Пример #19
0
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;
}
Пример #20
0
/**
 * @brief Set a formatted string
 *
 * @param key the key to set the string value
 * @param format NULL-terminated text format string
 * @param ... more arguments
 *
 * @return the size of the string as set (with including 0)
 */
ssize_t keySetStringF (Key * key, const char * format, ...)
{
	va_list arg_list;

	keySetMeta (key, "binary", 0);

	va_start (arg_list, format);
	char * p = elektraVFormat (format, arg_list);
	va_end (arg_list);

	if (!p)
	{
		return -1;
	}

	if (key->data.c)
	{
		elektraFree (key->data.c);
	}

	key->data.c = p;
	key->dataSize = elektraStrLen (key->data.c);
	set_bit (key->flags, KEY_FLAG_SYNC);

	return key->dataSize;
}
Пример #21
0
static void test_lookupIndirect ()
{
	printf ("Test lookup by indirect spec\n");

	Key * s;
	Key * p;
	Key * d;
	Key * u;
	Key * y;
	Key * e;
	KeySet * ks =
		ksNew (20, s = keyNew ("spec/abc", KEY_END), p = keyNew ("proc/abc", KEY_END), d = keyNew ("dir/abc", KEY_END),
		       u = keyNew ("user/abc", KEY_END), y = keyNew ("system/abc", KEY_END), e = keyNew ("system/else", KEY_END), KS_END);
	succeed_if (ksGetSize (ks) == 6, "wrong size");

	Key * k = ksLookupByName (ks, "/abc", 0);
	succeed_if (k == p, "did not find proc key");

	keySetMeta (s, "namespace/#0", "no");
	keySetMeta (s, "default", "80");
	k = ksLookupByName (ks, "/abc", 0);
	succeed_if (k != 0, "should find default");
	succeed_if (ksGetSize (ks) == 7, "default key not added");
	succeed_if_same_string (keyString (k), "80");

	Key * k2 = ksLookupByName (ks, "/abc", 0);
	succeed_if (k == k2, "did not get same default");

	keySetMeta (s, "fallback/#0", "/else");
	k = ksLookupByName (ks, "/abc", 0);
	succeed_if (k == e, "did not find else");

	keySetMeta (s, "namespace/#0", "system");
	k = ksLookupByName (ks, "/abc", 0);
	succeed_if (k == y, "did not find system key");

	keySetMeta (s, "namespace/#0", "system");
	keySetMeta (s, "namespace/#1", "user");
	k = ksLookupByName (ks, "/abc", 0);
	succeed_if (k == y, "did not find system key");

	keySetMeta (s, "namespace/#0", "proc");
	keySetMeta (s, "namespace/#1", "user");
	k = ksLookupByName (ks, "/abc", 0);
	succeed_if (k == p, "did not find proc key");

	keySetMeta (s, "override/#0", "/else");
	k = ksLookupByName (ks, "/abc", 0);
	succeed_if (k == e, "did not find override key");

	ksDel (ks);
}
Пример #22
0
static void test_lookupLongChain ()
{
	printf ("Test lookup long chain\n");

	// clang-format off
	Key *specKey = keyNew("user/4",
			KEY_META, "override/#0", "user/something",
			KEY_META, "override/#1", "user/something",
			KEY_META, "override/#2", "user/something",
			KEY_META, "override/#3", "user/something",
			KEY_META, "override/#3", "user/something",
			KEY_META, "override/#4", "user/something",
			KEY_META, "override/#5", "user/something",
			KEY_META, "override/#6", "user/something",
			KEY_META, "override/#7", "user/something",
			KEY_META, "override/#8", "user/something",
			KEY_META, "override/#9", "user/something",
			KEY_META, "override/#_10", "user/something",
			KEY_META, "override/#_11", "user/something",
			KEY_META, "override/#_12", "user/something",
			KEY_META, "override/#_13", "user/something",
			KEY_META, "override/#_14", "user/something",
			KEY_META, "override/#_15", "user/something",
			KEY_END);
	// clang-format on
	Key * k1 = 0;
	Key * k2 = 0;
	Key * k3 = 0;
	Key * k4 = 0;
	KeySet * ks = ksNew (20, k1 = keyNew ("user/1", KEY_END), k2 = keyNew ("user/2", KEY_END), k3 = keyNew ("user/3", KEY_END),
			     k4 = keyNew ("user/4", KEY_END), KS_END);

	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k4, "found wrong key");
	keySetMeta (specKey, "override/#_16", "user/else");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k4, "found wrong key");
	keySetMeta (specKey, "override/#_17", "user/wrong");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k4, "found wrong key");
	keySetMeta (specKey, "override/#_18", "user/3");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k3, "did not find override key");
	keySetMeta (specKey, "override/#_10", "user/2");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k2, "found wrong key");
	keySetMeta (specKey, "override/#5", "user/1");
	succeed_if (ksLookup (ks, specKey, KDB_O_SPEC) == k1, "found wrong key");

	keyDel (specKey);
	ksDel (ks);
}
Пример #23
0
static int handleErrors (Key * parentKey, KeySet * ks, Key * key, Key * specKey, ConflictHandling * ch, Direction dir)
{
	cursor_t cursor = ksGetCursor (ks);
	int ret = 0;
	ConflictHandling * localCh = elektraMalloc (sizeof (ConflictHandling));
	memcpy (localCh, ch, sizeof (ConflictHandling));
	parseLocalConfig (specKey, localCh, dir);
	Key * parentLookup = keyDup (key);
	keySetBaseName (parentLookup, 0);
	Key * parent = ksLookup (ks, parentLookup, KDB_O_NONE);
	keyDel (parentLookup);
	keyRewindMeta (parent);
	Conflict conflict;
	Key * meta;
	while (keyNextMeta (parent) != NULL)
	{
		meta = (Key *) keyCurrentMeta (parent);
		conflict = getConflict (meta);
		if (conflict != NAC)
		{
			ret |= handleError (parentKey, parent, specKey, meta, conflict, localCh);
			keySetMeta (parent, keyName (meta), 0);
		}
		else if (!strncmp (keyName (meta), "conflict/#", 10) || !strncmp (keyName (meta), "conflict/invalid/hasmember/#", 28))
		{
			keySetMeta (parent, keyName (meta), 0);
		}
	}
	keyRewindMeta (key);
	while (keyNextMeta (key) != NULL)
	{
		meta = (Key *) keyCurrentMeta (key);
		conflict = getConflict (meta);
		if (conflict != NAC)
		{
			ret |= handleError (parentKey, key, specKey, meta, conflict, localCh);
			keySetMeta (key, keyName (meta), 0);
		}
		else if (!strncmp (keyName (meta), "conflict/#", 10) || !strncmp (keyName (meta), "conflict/invalid/hasmember/#", 28))
		{
			keySetMeta (key, keyName (meta), 0);
		}
	}
	elektraFree (localCh);
	ksSetCursor (ks, cursor);
	return ret;
}
Пример #24
0
void test_mode()
{
	Key *key;

	key = keyNew ("user/mode", KEY_MODE, 0100, KEY_END);
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "100"), "meta value for mode was not set correctly");
	succeed_if (keyGetMode(key) == 0100, "mode was not set correctly");

	succeed_if (keySetMode(key, 0101) == 0, "could not set mode");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "101"), "meta value for mode was not set correctly");
	succeed_if (keyGetMode(key) == 0101, "mode was not set correctly");

	succeed_if (keySetMode(key, 0) == 0, "could not set mode");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "0"), "meta value for mode was not set correctly");
	succeed_if (keyGetMode(key) == 0, "mode was not set correctly");

	succeed_if (keySetMeta (key, "mode", "102") == sizeof("102"), "could not set meta");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "102"), "meta value for mode was not set correctly");
	succeed_if (keyGetMode(key) == 0102, "mode was not set correctly");

	succeed_if (keySetMeta (key, "mode", "0103") == sizeof("0103"), "could not set meta");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "0103"), "meta value for mode was not set correctly");
	succeed_if (keyGetMode(key) == 0103, "mode was not set correctly with leading octal 0");

	succeed_if (keySetMeta (key, "mode", "x") == sizeof("x"), "could not set meta");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "x"), "meta value for mode was not set correctly");
	succeed_if (keyGetMode(key) == KDB_FILE_MODE, "mode was not set correctly");

	succeed_if (keySetMeta (key, "mode", "x1") == sizeof("x1"), "could not set meta");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "x1"), "meta value for mode was not set correctly");
	succeed_if (keyGetMode(key) == KDB_FILE_MODE, "mode was not set correctly");

	succeed_if (keySetMeta (key, "mode", "2000000") == sizeof("2000000"), "could not set large mode");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "2000000"), "meta value for large mode was not set correctly");
	succeed_if (keyGetMode(key) == 02000000, "large mode was not set correctly");

	succeed_if (keySetMeta (key, "mode", "1x") == sizeof("1x"), "could not set meta");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "1x"), "meta value for mode was not set correctly");
	succeed_if (keyGetMode(key) == KDB_FILE_MODE, "mode was not set correctly");

	succeed_if (keySetMeta (key, "mode", "50x") == sizeof("50x"), "could not set meta");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), "50x"), "meta value for mode was not set correctly");
	succeed_if (keyGetMode(key) == KDB_FILE_MODE, "mode was not set correctly");

	keyDel (key);

	key = keyNew ("user/mode", KEY_END);
	succeed_if (keyValue (keyGetMeta(key, "mode")) == 0, "got value, but mode was not set up to now");
	succeed_if (keyGetMode(key) == KDB_FILE_MODE, "KDB_FILE_MODE not default on new key");

	succeed_if (keySetMeta (key, "mode", "") == sizeof(""), "could not set large mode");
	succeed_if (!strcmp(keyValue (keyGetMeta(key, "mode")), ""), "meta value for large mode was not set correctly");
	succeed_if (keyGetMode(key) == KDB_FILE_MODE, "empty mode should also yield default");

	keyDel (key);
}
Пример #25
0
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);
}
Пример #26
0
void removeKeyFromResult (Key * convertKey, Key * target, KeySet * orig)
{
    /* remember which key this key was converted to
     * before removing it from the result
     */
    keySetMeta (convertKey, CONVERT_TARGET, keyName (target));
    Key * key = ksLookup (orig, convertKey, KDB_O_POP);
    keyDel (key);
}
Пример #27
0
static void flushCollectedComment (CallbackHandle *handle, Key *key)
{
	if (handle->collectedComment)
	{
		keySetMeta (key, "comment", handle->collectedComment);
		elektraFree (handle->collectedComment);
		handle->collectedComment = 0;
	}
}
Пример #28
0
static void writeCommentToMeta (Configuration *config, Key *key)
{
	if (config->collectedComment)
	{
		keySetMeta (key, "comment", config->collectedComment);
		free (config->collectedComment);
		config->collectedComment = 0;
	}
}
Пример #29
0
void test_basic()
{
	Key *key;
	key = keyNew("user/metakey", KEY_END);
	exit_if_fail (key, "could not create new key");
	succeed_if (keyGetMeta(key, "hello") == 0, "hello was not set up to now");

	keySetMeta(key, "hello", "hello_world");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "hello")), "hello_world"),
			"could not receive previously set meta information");

	keySetMeta(key, "mode", "0644");
	keySetMeta(key, "time", "1271234264");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "hello")), "hello_world"),
			"meta info changed unexpectly");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "mode")), "0644"), "mode not set correctly");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "time")), "1271234264"), "time not set correctly");

	keySetMeta(key, "hello", "between");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "hello")), "between"),
			"could not set meta information again");

	keySetMeta(key, "hello", 0);
	succeed_if (keyValue(keyGetMeta(key, "hello")) == 0, "could not remove meta data");

	keySetMeta(key, "hello", "goodbye");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "hello")), "goodbye"),
			"could not set meta information again (2x)");

	keySetMeta(key, "empty", "");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "empty")), ""), "Problem with empty meta string");

	keySetMeta(key, "", "empty");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "")), "empty"), "Problem with empty name");

	keySetMeta(key, "", "");
	succeed_if (!strcmp(keyValue(keyGetMeta(key, "")), ""), "Problem with empty name and string");

	keySetMeta(key, "", 0);
	succeed_if (keyValue(keyGetMeta(key, "")) == 0, "could not remove empty meta data");


	keyDel (key);
}
Пример #30
0
/**
 * Set the raw string value of an array element key.
 *
 * @param elektra The Elektra instance to use.
 * @param name    The (relative) name of the array.
 * @param index   The index of the array element.
 * @param value   The raw value to set.
 * @param type    The type to set in the metadata of the (array element) key.
 * @param error   Pointer to an ElektraError. Will be set in case saving fails.
 */
void elektraSetRawStringArrayElement (Elektra * elektra, const char * name, size_t index, const char * value, KDBType type,
				      ElektraError ** error)
{
	CHECK_ERROR (elektra, error);
	elektraSetArrayLookupKey (elektra, name, index);
	Key * const key = keyDup (elektra->lookupKey);
	keySetMeta (key, "type", type);
	keySetString (key, value);

	elektraSaveKey (elektra, key, error);
}