Skip to content
Permalink
Browse files

opts: review improvements

  • Loading branch information...
kodebach committed Apr 1, 2019
1 parent 6767523 commit f078edfcc400412c2f1aa6a2a840dc3833c532de
Showing with 33 additions and 17 deletions.
  1. +17 −1 examples/gopts.c
  2. +8 −8 src/libs/elektra/kdb.c
  3. +4 −4 src/libs/notification/notification.c
  4. +1 −1 src/plugins/gopts/README.md
  5. +3 −3 tests/kdb/testkdb_ensure.cpp
@@ -19,6 +19,18 @@ extern char ** environ;
#define BASE_KEY "/sw/org/erm/#0/current"
#define SPEC_BASE_KEY "spec" BASE_KEY

// -----------------
// Helper methods
// -----------------

/*
* The methods below are only used, so that this example is self-contained.
* If you actually develop an application, you may use the `specload` plugin,
* but in any case the specification should be mounted into the KDB using `kdb mount`.
*
* DO NOT set/unset the specification inside of your application.
*/

static KeySet * createSpec (void)
{
return ksNew (
@@ -94,6 +106,10 @@ static void removeSpec (void)
ksDel (ks);
}

// -----------------
// Main example
// -----------------

int main (void)
{
if (!setupSpec ())
@@ -105,7 +121,7 @@ int main (void)
Key * parentKey = keyNew (BASE_KEY, KEY_END);
KDB * kdb = kdbOpen (parentKey);

KeySet * contract = ksNew (1, keyNew ("system/plugins/global/gopts", KEY_VALUE, "mounted", KEY_END), KS_END);
KeySet * contract = ksNew (1, keyNew ("system/elektra/ensure/plugins/global/gopts", KEY_VALUE, "mounted", KEY_END), KS_END);
int rc = kdbEnsure (kdb, contract, parentKey);
if (rc == 1)
{
@@ -1624,16 +1624,16 @@ static int ensurePluginState (KDB * handle ELEKTRA_UNUSED, const char * mountpoi
* This function can be used the given KDB @p handle meets certain clauses,
* specified in @p contract. Currently the following clauses are supported:
*
* - `system/plugins/<mountpoint>/<pluginname>` defines the state of the plugin
* - `system/elektra/ensure/plugins/<mountpoint>/<pluginname>` defines the state of the plugin
* `<pluginname>` for the mountpoint `<mountpoint>`:
* - The value `unmounted` ensures the plugin is not mounted, at this mountpoint.
* - The value `mounted` ensures the plugin is mounted, at this mountpoint.
* If the plugin is not mounted, we will try to mount it.
* - The value `remount` always mounts the plugin, at this mountpoint.
* If it was already mounted, it will me unmounted and mounted again.
* This can be used to ensure the plugin is mounted with a certain configuration.
* - Keys below `system/plugins/<mountpoint>/<pluginname>/config` are extracted and used
* as the plugins config KeySet during mounting. `system/plugins/<mountpoint>/<pluginname>`
* - Keys below `system/elektra/ensure/plugins/<mountpoint>/<pluginname>/config` are extracted and used
* as the plugins config KeySet during mounting. `system/elektra/ensure/plugins/<mountpoint>/<pluginname>`
* will be repleced by `user` in the keynames. If no keys are given, an empty KeySet is used.
*
* There are a few special values for `<mountpoint>`:
@@ -1673,7 +1673,7 @@ int kdbEnsure (KDB * handle, KeySet * contract, Key * parentKey)
return -1;
}

Key * cutpoint = keyNew ("system/plugins", KEY_END);
Key * cutpoint = keyNew ("system/elektra/ensure/plugins", KEY_END);
KeySet * pluginsContract = ksCut (contract, cutpoint);

// delete unused part of contract immediately
@@ -1683,10 +1683,10 @@ int kdbEnsure (KDB * handle, KeySet * contract, Key * parentKey)
Key * clause = NULL;
while ((clause = ksNext (pluginsContract)) != NULL)
{
// only handle 'system/plugins/<mountpoint>/<pluginname>' keys
// only handle 'system/elektra/ensure/plugins/<mountpoint>/<pluginname>' keys
const char * condUNameBase = keyUnescapedName (clause);
const char * condUName = condUNameBase;
condUName += sizeof ("system\0plugins"); // skip known common part
condUName += sizeof ("system\0elektra\0ensure\0plugins"); // skip known common part

size_t condUSize = keyGetUnescapedNameSize (clause);
if (condUNameBase + condUSize <= condUName)
@@ -1703,11 +1703,11 @@ int kdbEnsure (KDB * handle, KeySet * contract, Key * parentKey)
condUName += strlen (condUName) + 1; // skip pluginname
if (condUNameBase + condUSize > condUName)
{
continue; // key below 'system/plugins/<mountpoint>/<pluginname>'
continue; // key below 'system/elektra/ensure/plugins/<mountpoint>/<pluginname>'
}

const char * mountpoint = keyUnescapedName (clause);
mountpoint += sizeof ("system\0plugins");
mountpoint += sizeof ("system\0elektra\0ensure\0plugins");
const char * pluginName = keyBaseName (clause);
const char * pluginStateString = keyString (clause);

@@ -105,8 +105,8 @@ int elektraNotificationOpen (KDB * kdb)
context->kdbUpdate = &elektraNotificationKdbUpdate;

Key * parent = keyNew ("", KEY_END);
KeySet * contract = ksNew (2, keyNew ("system/plugins/global/internalnotification", KEY_VALUE, "mounted", KEY_END),
keyNew ("system/plugins/global/internalnotification/config/context", KEY_BINARY, KEY_SIZE,
KeySet * contract = ksNew (2, keyNew ("system/elektra/ensure/plugins/global/internalnotification", KEY_VALUE, "mounted", KEY_END),
keyNew ("system/elektra/ensure/plugins/global/internalnotification/config/context", KEY_BINARY, KEY_SIZE,
sizeof (context), KEY_VALUE, &context, KEY_END),
KS_END);
if (kdbEnsure (kdb, contract, parent) != 0)
@@ -130,7 +130,7 @@ int elektraNotificationOpen (KDB * kdb)
if (!func)
{
// remove notification plugin again
contract = ksNew (1, keyNew ("system/plugins/global/internalnotification", KEY_VALUE, "unmounted", KEY_END), KS_END);
contract = ksNew (1, keyNew ("system/elektra/ensure/plugins/global/internalnotification", KEY_VALUE, "unmounted", KEY_END), KS_END);
if (kdbEnsure (kdb, contract, parent) != 0)
{
ELEKTRA_LOG_WARNING ("kdbEnsure failed");
@@ -171,7 +171,7 @@ int elektraNotificationClose (KDB * kdb)

// Unmount the plugin
Key * parent = keyNew ("", KEY_END);
KeySet * contract = ksNew (1, keyNew ("system/plugins/global/internalnotification", KEY_VALUE, "unmounted", KEY_END), KS_END);
KeySet * contract = ksNew (1, keyNew ("system/elektra/ensure/plugins/global/internalnotification", KEY_VALUE, "unmounted", KEY_END), KS_END);
if (kdbEnsure (kdb, contract, parent) != 0)
{
ELEKTRA_LOG_WARNING ("kdbEnsure failed");
@@ -21,7 +21,7 @@ The preferred way of using this plugin is via `kdbEnsure`:
```c
KDB * kdb = kdbOpen (parentKey);
KeySet * contract = ksNew (1, keyNew ("system/plugins/global/gopts", KEY_VALUE, "mounted", KEY_END), KS_END);
KeySet * contract = ksNew (1, keyNew ("system/elektra/ensure/plugins/global/gopts", KEY_VALUE, "mounted", KEY_END), KS_END);
int rc = kdbEnsure (kdb, contract, parentKey);
if (rc != 0)
{
@@ -71,7 +71,7 @@ TEST_F (Ensure, GlobalUnmount)

{
KeySet contract;
contract.append (Key ("system/plugins/global/spec", KEY_VALUE, "unmounted", KEY_END));
contract.append (Key ("system/elektra/ensure/plugins/global/spec", KEY_VALUE, "unmounted", KEY_END));
Key root (specRoot, KEY_END);
kdb.ensure (contract, root);

@@ -103,7 +103,7 @@ TEST_F (Ensure, Unmount)

{
KeySet contract;
contract.append (Key ("system/plugins/parent/error", KEY_VALUE, "unmounted", KEY_END));
contract.append (Key ("system/elektra/ensure/plugins/parent/error", KEY_VALUE, "unmounted", KEY_END));
Key uroot (userRoot, KEY_END);
kdb.ensure (contract, uroot);

@@ -158,7 +158,7 @@ TEST_F (Ensure, GlobalMount)

{
KeySet contract;
contract.append (Key ("system/plugins/global/tracer", KEY_VALUE, "mounted", KEY_END));
contract.append (Key ("system/elektra/ensure/plugins/global/tracer", KEY_VALUE, "mounted", KEY_END));
Key root (specRoot, KEY_END);
kdb.ensure (contract, root);

0 comments on commit f078edf

Please sign in to comment.
You can’t perform that action at this time.