From 39d3bb727ed9062f7893c03b6ded76b717acaf39 Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Thu, 3 Jan 2019 22:59:37 +0100 Subject: [PATCH 01/12] doc: add docu about global keyset handle and global cache positions --- doc/decisions/global_plugins.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/doc/decisions/global_plugins.md b/doc/decisions/global_plugins.md index e03f2b25a95..dfef65c2648 100644 --- a/doc/decisions/global_plugins.md +++ b/doc/decisions/global_plugins.md @@ -40,9 +40,11 @@ Configuration will be in arrays below the keys: /rollback /postrollback /getresolver + /pregetcache /pregetstorage /getstorage /postgetstorage + /postgetcache /setresolver /presetstorage /setstorage @@ -203,6 +205,17 @@ states. ## Related decisions +### Global KeySet handle + +Some global plugins need to communicate more data than is possible to do with metadata. +This can limit the functionality of global plugins. One example is a global cache plugin, +which needs to store internal information for the KDB, some of which is binary. + +To make the communication between global plugins easier, global plugins will additionally +get a handle to a global keyset. The global keyset is initialized at the beginning of kdbGet() and +kdbSet() and deinitialized at the end of the respective KDB function. In other words, the global +keyset is tied to a KDB handle and is ksClear()-ed before the KDB function returns. + ## Notes ### Open Points From 01ad9998f0b63d7200ed77711c64b7117764f97a Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Thu, 3 Jan 2019 23:11:20 +0100 Subject: [PATCH 02/12] global-plugins: get return value of global plugin calls --- src/include/kdbprivate.h | 6 +++--- src/libs/elektra/global.c | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/include/kdbprivate.h b/src/include/kdbprivate.h index 2670619b5b9..a2165ff3d61 100644 --- a/src/include/kdbprivate.h +++ b/src/include/kdbprivate.h @@ -587,9 +587,9 @@ int keyNameIsSystem (const char * keyname); int keyNameIsUser (const char * keyname); /* global plugin calls */ -void elektraGlobalGet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition); -void elektraGlobalSet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition); -void elektraGlobalError (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition); +int elektraGlobalGet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition); +int elektraGlobalSet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition); +int elektraGlobalError (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition); /** Test a bit. @see set_bit(), clear_bit() */ #define test_bit(var, bit) ((var) & (bit)) diff --git a/src/libs/elektra/global.c b/src/libs/elektra/global.c index 28f210ed3c7..b1d9b5201e8 100644 --- a/src/libs/elektra/global.c +++ b/src/libs/elektra/global.c @@ -14,29 +14,35 @@ * Helper functions to execute global plugins */ -void elektraGlobalGet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition) +int elektraGlobalGet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition) { + int ret = 0; Plugin * plugin; if (handle && (plugin = handle->globalPlugins[position][subPosition])) { - plugin->kdbGet (plugin, ks, parentKey); + ret = plugin->kdbGet (plugin, ks, parentKey); } + return ret; } -void elektraGlobalSet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition) +int elektraGlobalSet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition) { + int ret = 0; Plugin * plugin; if (handle && (plugin = handle->globalPlugins[position][subPosition])) { - plugin->kdbSet (plugin, ks, parentKey); + ret = plugin->kdbSet (plugin, ks, parentKey); } + return ret; } -void elektraGlobalError (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition) +int elektraGlobalError (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition) { + int ret = 0; Plugin * plugin; if (handle && (plugin = handle->globalPlugins[position][subPosition])) { - plugin->kdbError (plugin, ks, parentKey); + ret = plugin->kdbError (plugin, ks, parentKey); } + return ret; } From 2e3492074e55883c35959e6dcd73aebf9f773057 Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Fri, 4 Jan 2019 22:13:38 +0100 Subject: [PATCH 03/12] global keyset: add global keyset handle to KDB and global plugins --- src/include/kdbplugin.h | 3 ++- src/include/kdbprivate.h | 10 ++++++++++ src/libs/elektra/kdb.c | 3 +++ src/libs/elektra/mount.c | 3 +++ src/libs/plugin/plugin.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/include/kdbplugin.h b/src/include/kdbplugin.h index 9e07f12487b..f5dc5cfd314 100644 --- a/src/include/kdbplugin.h +++ b/src/include/kdbplugin.h @@ -108,7 +108,8 @@ Plugin * elektraPluginExport (const char * pluginName, ...); KeySet * elektraPluginGetConfig (Plugin * handle); void elektraPluginSetData (Plugin * plugin, void * handle); void * elektraPluginGetData (Plugin * plugin); - +void elektraPluginSetGlobalKeySet (Plugin * plugin, KeySet * ks); +KeySet * elektraPluginGetGlobalKeySet (Plugin * plugin); #define PLUGINVERSION "1" diff --git a/src/include/kdbprivate.h b/src/include/kdbprivate.h index a2165ff3d61..3d44e6c42f2 100644 --- a/src/include/kdbprivate.h +++ b/src/include/kdbprivate.h @@ -315,6 +315,11 @@ struct _KDB KeySet * modules; /*!< A list of all modules loaded at the moment.*/ + KeySet * global; /*!< This keyset can be used by global plugins to pass data through + the KDB and communicate with other global plugins during a KDB call. + It is initialized at beginning of kdbGet()/kdbSet() and is + ksClear()-ed before the functions return.*/ + Backend * defaultBackend; /*!< The default backend as fallback when nothing else is found.*/ Backend * initBackend; /*!< The init backend for bootstrapping.*/ @@ -413,6 +418,11 @@ struct _Plugin void * data; /*!< This handle can be used for a plugin to store any data its want to. */ + + KeySet * global; /*!< This keyset can be used by global plugins to pass data through + the KDB and communicate with other global plugins during a KDB call. + It is initialized at beginning of kdbGet()/kdbSet() and is + ksClear()-ed before the functions return.*/ }; diff --git a/src/libs/elektra/kdb.c b/src/libs/elektra/kdb.c index 3ee5d2e2051..694ac18e76f 100644 --- a/src/libs/elektra/kdb.c +++ b/src/libs/elektra/kdb.c @@ -263,6 +263,7 @@ KDB * kdbOpen (Key * errorKey) KDB * handle = elektraCalloc (sizeof (struct _KDB)); Key * initialParent = keyDup (errorKey); + handle->global = ksNew (0, KS_END); handle->modules = ksNew (0, KS_END); if (elektraModulesInit (handle->modules, errorKey) == -1) { @@ -433,6 +434,8 @@ int kdbClose (KDB * handle, Key * errorKey) ELEKTRA_ADD_WARNING (47, errorKey, "modules were not open"); } + if (handle->global) ksDel (handle->global); + elektraFree (handle); keySetName (errorKey, keyName (initialParent)); diff --git a/src/libs/elektra/mount.c b/src/libs/elektra/mount.c index 24d05f86cf3..de6f15f31f1 100644 --- a/src/libs/elektra/mount.c +++ b/src/libs/elektra/mount.c @@ -375,6 +375,9 @@ int mountGlobals (KDB * kdb, KeySet * keys, KeySet * modules, Key * errorKey) else kdb->globalPlugins[i][MAXONCE] = plugin; + // set handle to global keyset + plugin->global = kdb->global; + // load plugins in explicit placements const char * placementName = keyName (cur); Key * placementKey = ksLookupByName (global, placementName, 0); diff --git a/src/libs/plugin/plugin.c b/src/libs/plugin/plugin.c index c2da1456347..c94e51edab0 100644 --- a/src/libs/plugin/plugin.c +++ b/src/libs/plugin/plugin.c @@ -140,3 +140,35 @@ void * elektraPluginGetData (Plugin * plugin) { return plugin->data; } + +/** + * @brief Store a pointer to the global keyset. + * + * Only initialized for global plugins. + * + * @see elektraPluginGetGlobal + * @param plugin a pointer to the plugin + * @param ks the pointer to the global keyset + * @ingroup plugin + */ +void elektraPluginSetGlobal (Plugin * plugin, KeySet * ks) +{ + plugin->global = ks; +} + +/** + * @brief Get a pointer to the global keyset. + * + * Only initialized for global plugins. + * + * If elektraPluginSetGlobal() was not called earlier, NULL will be returned. + * + * @see elektraPluginSetGlobal + * @param plugin a pointer to the plugin + * @return a pointer to the global keyset + * @ingroup plugin + */ +KeySet * elektraPluginGetGlobal (Plugin * plugin) +{ + return plugin->global; +} From 1f47cb19c6e082d3f83b2684e56fa858b0cec9cf Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Mon, 7 Jan 2019 19:29:05 +0100 Subject: [PATCH 04/12] global keyset: clarify description --- doc/decisions/global_plugins.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/decisions/global_plugins.md b/doc/decisions/global_plugins.md index dfef65c2648..cd3d50b6d74 100644 --- a/doc/decisions/global_plugins.md +++ b/doc/decisions/global_plugins.md @@ -209,7 +209,8 @@ states. Some global plugins need to communicate more data than is possible to do with metadata. This can limit the functionality of global plugins. One example is a global cache plugin, -which needs to store internal information for the KDB, some of which is binary. +which needs to store internal information for the KDB. Some of the information is binary +and can not be stored in metadata. To make the communication between global plugins easier, global plugins will additionally get a handle to a global keyset. The global keyset is initialized at the beginning of kdbGet() and From b6ef391b44a27b8b4951347c7a8e77dccb7b5e23 Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Mon, 7 Jan 2019 19:35:41 +0100 Subject: [PATCH 05/12] global keyset: clear according to docs --- src/libs/elektra/kdb.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libs/elektra/kdb.c b/src/libs/elektra/kdb.c index 694ac18e76f..ade70be1466 100644 --- a/src/libs/elektra/kdb.c +++ b/src/libs/elektra/kdb.c @@ -784,6 +784,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) int errnosave = errno; Key * initialParent = keyDup (parentKey); + ksClear (handle->global); ELEKTRA_LOG ("now in new kdbGet (%s)", keyName (parentKey)); @@ -815,6 +816,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, INIT); elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, MAXONCE); elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, DEINIT); + ksClear (handle->global); splitUpdateFileName (split, handle, parentKey); keyDel (initialParent); splitDel (split); @@ -901,6 +903,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) keySetName (parentKey, keyName (initialParent)); splitUpdateFileName (split, handle, parentKey); + ksClear (handle->global); keyDel (initialParent); keyDel (oldError); splitDel (split); @@ -913,6 +916,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) elektraGlobalError (handle, ks, parentKey, POSTGETSTORAGE, MAXONCE); elektraGlobalError (handle, ks, parentKey, POSTGETSTORAGE, DEINIT); + ksClear (handle->global); keySetName (parentKey, keyName (initialParent)); if (handle) splitUpdateFileName (split, handle, parentKey); keyDel (initialParent); @@ -1191,6 +1195,7 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) int errnosave = errno; Key * initialParent = keyDup (parentKey); + ksClear (handle->global); ELEKTRA_LOG ("now in new kdbSet (%s) %p %zd", keyName (parentKey), (void *) handle, ksGetSize (ks)); @@ -1236,6 +1241,7 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) { ELEKTRA_SET_ERROR (107, parentKey, keyName (split->parents[-syncstate - 2])); } + ksClear (handle->global); keyDel (initialParent); splitDel (split); errno = errnosave; @@ -1282,6 +1288,7 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) clear_bit (ks->array[i]->flags, KEY_FLAG_SYNC); } + ksClear (handle->global); keySetName (parentKey, keyName (initialParent)); keyDel (initialParent); splitDel (split); @@ -1314,6 +1321,7 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) elektraGlobalError (handle, ks, parentKey, POSTROLLBACK, MAXONCE); elektraGlobalError (handle, ks, parentKey, POSTROLLBACK, DEINIT); + ksClear (handle->global); keySetName (parentKey, keyName (initialParent)); keyDel (initialParent); splitDel (split); From 90e779739dd990ac7e5aa827116288b14e2fdc95 Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Mon, 7 Jan 2019 20:04:28 +0100 Subject: [PATCH 06/12] news: add release notes for global keyset handle --- doc/news/_preparation_next_release.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/news/_preparation_next_release.md b/doc/news/_preparation_next_release.md index c70a9eded18..cd122d7b231 100644 --- a/doc/news/_preparation_next_release.md +++ b/doc/news/_preparation_next_release.md @@ -127,7 +127,8 @@ compiled against an older 0.8 version of Elektra will continue to work ### Core -- <> +- Global plugins now get a handle to a global keyset. The keyset is available throughout a kdbGet() and kdbSet() function, + but is cleared before the function returns. This makes communication between global plugins easier. *(Mihael Pranjić)* - <> - <> From a38087af880797891b6fb5bae9a7c6ca964cdead Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Mon, 7 Jan 2019 20:37:41 +0100 Subject: [PATCH 07/12] global keyset: fix plugin function name, reorder handle in KDB struct --- src/include/kdbplugin.h | 4 ++-- src/include/kdbprivate.h | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/include/kdbplugin.h b/src/include/kdbplugin.h index f5dc5cfd314..06012d7cc30 100644 --- a/src/include/kdbplugin.h +++ b/src/include/kdbplugin.h @@ -108,8 +108,8 @@ Plugin * elektraPluginExport (const char * pluginName, ...); KeySet * elektraPluginGetConfig (Plugin * handle); void elektraPluginSetData (Plugin * plugin, void * handle); void * elektraPluginGetData (Plugin * plugin); -void elektraPluginSetGlobalKeySet (Plugin * plugin, KeySet * ks); -KeySet * elektraPluginGetGlobalKeySet (Plugin * plugin); +void elektraPluginSetGlobal (Plugin * plugin, KeySet * ks); +KeySet * elektraPluginGetGlobal (Plugin * plugin); #define PLUGINVERSION "1" diff --git a/src/include/kdbprivate.h b/src/include/kdbprivate.h index 3d44e6c42f2..7982e6b5fd3 100644 --- a/src/include/kdbprivate.h +++ b/src/include/kdbprivate.h @@ -315,11 +315,6 @@ struct _KDB KeySet * modules; /*!< A list of all modules loaded at the moment.*/ - KeySet * global; /*!< This keyset can be used by global plugins to pass data through - the KDB and communicate with other global plugins during a KDB call. - It is initialized at beginning of kdbGet()/kdbSet() and is - ksClear()-ed before the functions return.*/ - Backend * defaultBackend; /*!< The default backend as fallback when nothing else is found.*/ Backend * initBackend; /*!< The init backend for bootstrapping.*/ @@ -330,6 +325,11 @@ struct _KDB Plugin * notificationPlugin; /*!< reference to global plugin for notifications.*/ ElektraNotificationCallbackContext * notificationCallbackContext; /*!< reference to context for notification callbacks.*/ + + KeySet * global; /*!< This keyset can be used by global plugins to pass data through + the KDB and communicate with other global plugins during a KDB call. + It is initialized at beginning of kdbGet()/kdbSet() and is + ksClear()-ed before the functions return.*/ }; From e37613811e7c378645b8bc85e2e50b401865e5c5 Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Mon, 7 Jan 2019 21:58:34 +0100 Subject: [PATCH 08/12] global keyset: add icheck suppression --- tests/icheck.suppression | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/icheck.suppression b/tests/icheck.suppression index 5b210b2b79e..3cb11ae64ca 100644 --- a/tests/icheck.suppression +++ b/tests/icheck.suppression @@ -2,3 +2,5 @@ // To ignore a function just add the function definition as regex here. // For example, to ignore the function `int elektraArrayDecName(Key * key)` you can use // the following entry: elektraArrayDecName +elektraPluginSetGlobal +elektraPluginGetGlobal From f1aed27fde02f6e950e8b5df4c4f5bb603c5635a Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Tue, 8 Jan 2019 01:13:04 +0100 Subject: [PATCH 09/12] global keyset: fix memproblems --- src/libs/elektra/kdb.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/libs/elektra/kdb.c b/src/libs/elektra/kdb.c index ade70be1466..1ea1da0b7ac 100644 --- a/src/libs/elektra/kdb.c +++ b/src/libs/elektra/kdb.c @@ -267,6 +267,7 @@ KDB * kdbOpen (Key * errorKey) handle->modules = ksNew (0, KS_END); if (elektraModulesInit (handle->modules, errorKey) == -1) { + ksDel (handle->global); ksDel (handle->modules); elektraFree (handle); ELEKTRA_SET_ERROR (94, errorKey, "elektraModulesInit returned with -1"); @@ -283,6 +284,7 @@ KDB * kdbOpen (Key * errorKey) switch (elektraOpenBootstrap (handle, keys, errorKey)) { case -1: + ksDel (handle->global); ksDel (handle->modules); elektraFree (handle); ELEKTRA_SET_ERROR (40, errorKey, "could not open default backend"); @@ -784,7 +786,6 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) int errnosave = errno; Key * initialParent = keyDup (parentKey); - ksClear (handle->global); ELEKTRA_LOG ("now in new kdbGet (%s)", keyName (parentKey)); @@ -797,6 +798,8 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) goto error; } + ksClear (handle->global); + elektraGlobalGet (handle, ks, parentKey, PREGETSTORAGE, INIT); elektraGlobalGet (handle, ks, parentKey, PREGETSTORAGE, MAXONCE); elektraGlobalGet (handle, ks, parentKey, PREGETSTORAGE, DEINIT); @@ -916,9 +919,12 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) elektraGlobalError (handle, ks, parentKey, POSTGETSTORAGE, MAXONCE); elektraGlobalError (handle, ks, parentKey, POSTGETSTORAGE, DEINIT); - ksClear (handle->global); keySetName (parentKey, keyName (initialParent)); - if (handle) splitUpdateFileName (split, handle, parentKey); + if (handle) + { + splitUpdateFileName (split, handle, parentKey); + if (handle->global) ksClear (handle->global); + } keyDel (initialParent); keyDel (oldError); splitDel (split); @@ -1321,7 +1327,7 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) elektraGlobalError (handle, ks, parentKey, POSTROLLBACK, MAXONCE); elektraGlobalError (handle, ks, parentKey, POSTROLLBACK, DEINIT); - ksClear (handle->global); + if (handle->global) ksClear (handle->global); keySetName (parentKey, keyName (initialParent)); keyDel (initialParent); splitDel (split); From 544782a31800dd57817a4c1f6f71d3027db2f3c1 Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Thu, 10 Jan 2019 22:32:32 +0100 Subject: [PATCH 10/12] global keyset: rework concept --- doc/decisions/global_plugins.md | 10 +++++++--- doc/news/_preparation_next_release.md | 3 +-- src/include/kdbplugin.h | 4 ++-- src/libs/elektra/kdb.c | 14 +------------- src/libs/plugin/plugin.c | 24 +++++------------------- tests/icheck.suppression | 3 +-- 6 files changed, 17 insertions(+), 41 deletions(-) diff --git a/doc/decisions/global_plugins.md b/doc/decisions/global_plugins.md index cd3d50b6d74..e552acdbb71 100644 --- a/doc/decisions/global_plugins.md +++ b/doc/decisions/global_plugins.md @@ -213,9 +213,13 @@ which needs to store internal information for the KDB. Some of the information i and can not be stored in metadata. To make the communication between global plugins easier, global plugins will additionally -get a handle to a global keyset. The global keyset is initialized at the beginning of kdbGet() and -kdbSet() and deinitialized at the end of the respective KDB function. In other words, the global -keyset is tied to a KDB handle and is ksClear()-ed before the KDB function returns. +get a handle to a global keyset. The global keyset is tied to a KDB handle, initialized on kdbOpen() and +deleted on kdbClose(). + +The resolver plugin is an exception and also gets a handle to the global keyset. This way it +can store and compare timestamps of config files and cache files. + +Plugins are responsible for cleaning up their part of the global keyset. ## Notes diff --git a/doc/news/_preparation_next_release.md b/doc/news/_preparation_next_release.md index cd122d7b231..deaf3062dd4 100644 --- a/doc/news/_preparation_next_release.md +++ b/doc/news/_preparation_next_release.md @@ -127,8 +127,7 @@ compiled against an older 0.8 version of Elektra will continue to work ### Core -- Global plugins now get a handle to a global keyset. The keyset is available throughout a kdbGet() and kdbSet() function, - but is cleared before the function returns. This makes communication between global plugins easier. *(Mihael Pranjić)* +- Global plugins now get a handle to a global keyset, which makes communication between global plugins easier. *(Mihael Pranjić)* - <> - <> diff --git a/src/include/kdbplugin.h b/src/include/kdbplugin.h index 06012d7cc30..f9a61c8e709 100644 --- a/src/include/kdbplugin.h +++ b/src/include/kdbplugin.h @@ -108,8 +108,8 @@ Plugin * elektraPluginExport (const char * pluginName, ...); KeySet * elektraPluginGetConfig (Plugin * handle); void elektraPluginSetData (Plugin * plugin, void * handle); void * elektraPluginGetData (Plugin * plugin); -void elektraPluginSetGlobal (Plugin * plugin, KeySet * ks); -KeySet * elektraPluginGetGlobal (Plugin * plugin); + +KeySet * elektraPluginGetGlobalKeySet (Plugin * plugin); #define PLUGINVERSION "1" diff --git a/src/libs/elektra/kdb.c b/src/libs/elektra/kdb.c index 1ea1da0b7ac..3fb24acb42b 100644 --- a/src/libs/elektra/kdb.c +++ b/src/libs/elektra/kdb.c @@ -798,8 +798,6 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) goto error; } - ksClear (handle->global); - elektraGlobalGet (handle, ks, parentKey, PREGETSTORAGE, INIT); elektraGlobalGet (handle, ks, parentKey, PREGETSTORAGE, MAXONCE); elektraGlobalGet (handle, ks, parentKey, PREGETSTORAGE, DEINIT); @@ -819,7 +817,6 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, INIT); elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, MAXONCE); elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, DEINIT); - ksClear (handle->global); splitUpdateFileName (split, handle, parentKey); keyDel (initialParent); splitDel (split); @@ -906,7 +903,6 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) keySetName (parentKey, keyName (initialParent)); splitUpdateFileName (split, handle, parentKey); - ksClear (handle->global); keyDel (initialParent); keyDel (oldError); splitDel (split); @@ -920,11 +916,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey) elektraGlobalError (handle, ks, parentKey, POSTGETSTORAGE, DEINIT); keySetName (parentKey, keyName (initialParent)); - if (handle) - { - splitUpdateFileName (split, handle, parentKey); - if (handle->global) ksClear (handle->global); - } + if (handle) splitUpdateFileName (split, handle, parentKey); keyDel (initialParent); keyDel (oldError); splitDel (split); @@ -1201,7 +1193,6 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) int errnosave = errno; Key * initialParent = keyDup (parentKey); - ksClear (handle->global); ELEKTRA_LOG ("now in new kdbSet (%s) %p %zd", keyName (parentKey), (void *) handle, ksGetSize (ks)); @@ -1247,7 +1238,6 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) { ELEKTRA_SET_ERROR (107, parentKey, keyName (split->parents[-syncstate - 2])); } - ksClear (handle->global); keyDel (initialParent); splitDel (split); errno = errnosave; @@ -1294,7 +1284,6 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) clear_bit (ks->array[i]->flags, KEY_FLAG_SYNC); } - ksClear (handle->global); keySetName (parentKey, keyName (initialParent)); keyDel (initialParent); splitDel (split); @@ -1327,7 +1316,6 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey) elektraGlobalError (handle, ks, parentKey, POSTROLLBACK, MAXONCE); elektraGlobalError (handle, ks, parentKey, POSTROLLBACK, DEINIT); - if (handle->global) ksClear (handle->global); keySetName (parentKey, keyName (initialParent)); keyDel (initialParent); splitDel (split); diff --git a/src/libs/plugin/plugin.c b/src/libs/plugin/plugin.c index c94e51edab0..d0874515cda 100644 --- a/src/libs/plugin/plugin.c +++ b/src/libs/plugin/plugin.c @@ -141,34 +141,20 @@ void * elektraPluginGetData (Plugin * plugin) return plugin->data; } -/** - * @brief Store a pointer to the global keyset. - * - * Only initialized for global plugins. - * - * @see elektraPluginGetGlobal - * @param plugin a pointer to the plugin - * @param ks the pointer to the global keyset - * @ingroup plugin - */ -void elektraPluginSetGlobal (Plugin * plugin, KeySet * ks) -{ - plugin->global = ks; -} - /** * @brief Get a pointer to the global keyset. * - * Only initialized for global plugins. + * Only initialized for global plugins and the resolver. * - * If elektraPluginSetGlobal() was not called earlier, NULL will be returned. + * Plugins using this keyset are responsible for cleaning up + * their parts of the keyset which they do not need any more. * - * @see elektraPluginSetGlobal + * If kdbOpen() was not called earlier, NULL will be returned. * @param plugin a pointer to the plugin * @return a pointer to the global keyset * @ingroup plugin */ -KeySet * elektraPluginGetGlobal (Plugin * plugin) +KeySet * elektraPluginGetGlobalKeySet (Plugin * plugin) { return plugin->global; } diff --git a/tests/icheck.suppression b/tests/icheck.suppression index 3cb11ae64ca..51b63242e00 100644 --- a/tests/icheck.suppression +++ b/tests/icheck.suppression @@ -2,5 +2,4 @@ // To ignore a function just add the function definition as regex here. // For example, to ignore the function `int elektraArrayDecName(Key * key)` you can use // the following entry: elektraArrayDecName -elektraPluginSetGlobal -elektraPluginGetGlobal +elektraPluginGetGlobalKeySet From 6275de2d332dd403f07295d17df8249d3af4d6ba Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Thu, 10 Jan 2019 22:57:07 +0100 Subject: [PATCH 11/12] doc: add example for global keyset usage --- src/plugins/doc/doc.c | 14 ++++++++++++++ src/plugins/doc/doc.h | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/plugins/doc/doc.c b/src/plugins/doc/doc.c index a7bf446b54a..85868a1ceaf 100644 --- a/src/plugins/doc/doc.c +++ b/src/plugins/doc/doc.c @@ -147,6 +147,20 @@ int elektraDocGet (Plugin * plugin ELEKTRA_UNUSED, KeySet * returned, Key * pare fclose (fp); //![get storage] + //![get global keyset] + KeySet * globalKS = elektraPluginGetGlobalKeySet (plugin); + // now we can read something from the global keyset + // or add something for us or others to read + Key * important = keyNew ("user/global/myDocKey", KEY_VALUE, "global plugins can see me", KEY_END); + ksAppendKey (globalKS, important); + //![get global keyset] + + //![get global keyset cleanup] + // clean up parts of the global keyset which we do not need + Key * cutKey = keyNew ("user/global/myDocKey", KEY_END); + KeySet * notNeeded = ksCut (globalKS, cutKey); + ksDel (notNeeded); + //![get global keyset cleanup] //![get filter] Key * k; diff --git a/src/plugins/doc/doc.h b/src/plugins/doc/doc.h index a4bcc65cf16..9e0c81cb50b 100644 --- a/src/plugins/doc/doc.h +++ b/src/plugins/doc/doc.h @@ -102,6 +102,20 @@ * Note that you also need to return -1 in the case of error. * See individual description of entry points to implement below. * + * @par Global KeySet Handle + * + * Global plugins and the resolver will get a handle to a global keyset. + * This allows them to exchange information with other global plugins. + * + * Obtain a handle to the global keyset and work with it: + * + * @snippet doc.c get global keyset + * + * Clean up keys which you do not need any more, + * to keep the global keyset compact: + * + * @snippet doc.c get global keyset cleanup + * * @par Further help * Do not hesitate to open an issue if anything * is unclear. From cec434d61e247a48de74fbe778ff6425f2056745 Mon Sep 17 00:00:00 2001 From: Mihael Pranjic Date: Sun, 20 Jan 2019 11:47:53 +0100 Subject: [PATCH 12/12] doc: clarify cleanup --- src/include/kdbprivate.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/include/kdbprivate.h b/src/include/kdbprivate.h index 7982e6b5fd3..cc8e615e8ad 100644 --- a/src/include/kdbprivate.h +++ b/src/include/kdbprivate.h @@ -326,10 +326,8 @@ struct _KDB Plugin * notificationPlugin; /*!< reference to global plugin for notifications.*/ ElektraNotificationCallbackContext * notificationCallbackContext; /*!< reference to context for notification callbacks.*/ - KeySet * global; /*!< This keyset can be used by global plugins to pass data through - the KDB and communicate with other global plugins during a KDB call. - It is initialized at beginning of kdbGet()/kdbSet() and is - ksClear()-ed before the functions return.*/ + KeySet * global; /*!< This keyset can be used by global plugins and the resolver + to pass data through the KDB and communicate with other global plugins.*/ }; @@ -419,10 +417,10 @@ struct _Plugin void * data; /*!< This handle can be used for a plugin to store any data its want to. */ - KeySet * global; /*!< This keyset can be used by global plugins to pass data through - the KDB and communicate with other global plugins during a KDB call. - It is initialized at beginning of kdbGet()/kdbSet() and is - ksClear()-ed before the functions return.*/ + KeySet * global; /*!< This keyset can be used by global plugins and the resolver + to pass data through the KDB and communicate with other global plugins. + Plugins shall clean up their parts of the global keyset, which + they do not need any more.*/ };