Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

global-plugins: mark placements #874

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/Modules/LibAddMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ function (generate_readme p)
STRING(REGEX REPLACE ";" " " PROVIDES "${PROVIDES}")
STRING(REGEX REPLACE "\"- +infos/provides *= *([a-zA-Z0-9/ ]*)\\\\n\"" "keyNew(\"system/elektra/modules/${p}/infos/provides\",\nKEY_VALUE, \"${PROVIDES}\", KEY_END)," contents "${contents}")

STRING(REGEX REPLACE "\"- +infos/placements *= *([a-zA-Z0-9 ]*)\\\\n\"" "keyNew(\"system/elektra/modules/${p}/infos/placements\",\nKEY_VALUE, \"\\1\", KEY_END)," contents "${contents}")
STRING(REGEX REPLACE "\"- +infos/placements *= *([a-zA-Z0-9/ ]*)\\\\n\"" "keyNew(\"system/elektra/modules/${p}/infos/placements\",\nKEY_VALUE, \"\\1\", KEY_END)," contents "${contents}")
STRING(REGEX REPLACE "\"- +infos/recommends *= *([a-zA-Z0-9 ]*)\\\\n\"" "keyNew(\"system/elektra/modules/${p}/infos/recommends\",\nKEY_VALUE, \"\\1\", KEY_END)," contents "${contents}")
STRING(REGEX REPLACE "\"- +infos/ordering *= *([a-zA-Z0-9 ]*)\\\\n\"" "keyNew(\"system/elektra/modules/${p}/infos/ordering\",\nKEY_VALUE, \"\\1\", KEY_END)," contents "${contents}")
STRING(REGEX REPLACE "\"- +infos/stacking *= *([a-zA-Z0-9 ]*)\\\\n\"" "keyNew(\"system/elektra/modules/${p}/infos/stacking\",\nKEY_VALUE, \"\\1\", KEY_END)," contents "${contents}")
Expand Down
57 changes: 57 additions & 0 deletions src/include/kdbglobal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @file
*
* @brief Defines for global plugins
*
* @copyright BSD License (see doc/COPYING or http://www.libelektra.org)
*/

#ifndef ELEKTRA_KDBGLOBAL_H
#define ELEKTRA_KDBGLOBAL_H

#include <kdb.h>
#include <kdbconfig.h>
#include <kdbplugin.h>

/**
* Helper for identifying global plugin positions
*/
// clang-format off
#define FOREACH_POSITION(POSITION) \
POSITION(PREROLLBACK) \
POSITION(ROLLBACK) \
POSITION(POSTROLLBACK) \
POSITION(GETRESOLVER) \
POSITION(PREGETSTORAGE) \
POSITION(GETSTORAGE) \
POSITION(POSTGETSTORAGE) \
POSITION(SETRESOLVER) \
POSITION(POSTGETCLEANUP) \
POSITION(PRESETSTORAGE) \
POSITION(SETSTORAGE) \
POSITION(PRESETCLEANUP) \
POSITION(PRECOMMIT) \
POSITION(COMMIT) \
POSITION(POSTCOMMIT) \
POSITION(NR_GLOBAL_POSITIONS)

#define FOREACH_SUBPOSITION(SUBPOSITION) \
SUBPOSITION(INIT) \
SUBPOSITION(MAXONCE) \
SUBPOSITION(FOREACH) \
SUBPOSITION(DEINIT) \
SUBPOSITION(NR_GLOBAL_SUBPOSITIONS)

#define GENERATE_ENUM(ENUM) ENUM,
#define GENERATE_STRING(STRING) #STRING,
// clang-format on

typedef enum { FOREACH_POSITION (GENERATE_ENUM) } GlobalpluginPositions;

typedef enum { FOREACH_SUBPOSITION (GENERATE_ENUM) } GlobalpluginSubPositions;

static const char * GlobalpluginPositionsStr[] ELEKTRA_UNUSED = { FOREACH_POSITION (GENERATE_STRING) };

static const char * GlobalpluginSubPositionsStr[] ELEKTRA_UNUSED = { FOREACH_SUBPOSITION (GENERATE_STRING) };

#endif // ELEKTRA_KDBGLOBAL_H
25 changes: 7 additions & 18 deletions src/include/kdbprivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifdef ELEKTRA_ENABLE_OPTIMIZATIONS
#include <kdbopmphm.h>
#endif
#include <kdbglobal.h>

#include <limits.h>

Expand Down Expand Up @@ -240,23 +241,6 @@ struct _KeySet
};


/**
* Helper for identifying global plugin positions
*/

typedef enum {
PREROLLBACK = 0,
POSTROLLBACK,
PREGETSTORAGE,
POSTGETSTORAGE,
POSTGETCLEANUP,
PRESETSTORAGE,
PRESETCLEANUP,
PRECOMMIT,
POSTCOMMIT,
NR_GLOBAL_PLUGINS
} GlobalpluginPositions;

/**
* The access point to the key database.
*
Expand Down Expand Up @@ -289,7 +273,7 @@ struct _KDB

Backend * initBackend; /*!< The init backend for bootstrapping.*/

Plugin * globalPlugins[NR_GLOBAL_PLUGINS];
Plugin * globalPlugins[NR_GLOBAL_POSITIONS][NR_GLOBAL_SUBPOSITIONS];
};


Expand Down Expand Up @@ -552,6 +536,11 @@ int keyNameIsDir (const char * keyname);
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);

/** Test a bit. @see set_bit(), clear_bit() */
#define test_bit(var, bit) ((var) & (bit))
/** Set a bit. @see clear_bit() */
Expand Down
42 changes: 42 additions & 0 deletions src/libs/elektra/global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file
*
* @brief Helpers for global plugins
*
* @copyright BSD License (see doc/COPYING or http://www.libelektra.org)
*/

#include <kdbglobal.h>
#include <kdbprivate.h>

/**
* @internal
* Helper functions to execute global plugins
*/

void elektraGlobalGet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition)
{
Plugin * plugin;
if (handle && (plugin = handle->globalPlugins[position][subPosition]))
{
plugin->kdbGet (plugin, ks, parentKey);
}
}

void elektraGlobalSet (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition)
{
Plugin * plugin;
if (handle && (plugin = handle->globalPlugins[position][subPosition]))
{
plugin->kdbSet (plugin, ks, parentKey);
}
}

void elektraGlobalError (KDB * handle, KeySet * ks, Key * parentKey, int position, int subPosition)
{
Plugin * plugin;
if (handle && (plugin = handle->globalPlugins[position][subPosition]))
{
plugin->kdbError (plugin, ks, parentKey);
}
}
96 changes: 58 additions & 38 deletions src/libs/elektra/kdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,12 @@ int kdbClose (KDB * handle, Key * errorKey)
handle->initBackend = 0;
}

for (int i = 0; i < NR_GLOBAL_PLUGINS; ++i)
for (int i = 0; i < NR_GLOBAL_POSITIONS; ++i)
{
elektraPluginClose (handle->globalPlugins[i], errorKey);
for (int j = 0; j < NR_GLOBAL_SUBPOSITIONS; ++j)
{
elektraPluginClose (handle->globalPlugins[i][j], errorKey);
}
}

if (handle->modules)
Expand Down Expand Up @@ -567,6 +570,12 @@ static int elektraGetDoUpdateWithGlobalHooks (KDB * handle, Split * split, KeySe
const int bypassedSplits = 1;
int pgs_done = 0;
int pgc_done = 0;

elektraGlobalGet (handle, ks, parentKey, GETSTORAGE, INIT);
elektraGlobalGet (handle, ks, parentKey, GETSTORAGE, MAXONCE);

// elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, INIT);

Copy link
Contributor

@tom-wa tom-wa Aug 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for your information: elektraGetDoUpdatewithGlobalHooks gets called twice (thats why there's the run parameter)
you've used if statements for the set placements but not here, so i'm not sure if you've thought of that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I did think of that since we discussed it in a meeting.

All of these are just rough markers where I thought the hooks could be placed.

for (size_t i = 0; i < split->size - bypassedSplits; i++)
{
if (!test_bit (split->syncbits[i], SPLIT_FLAG_SYNC))
Expand All @@ -592,20 +601,23 @@ static int elektraGetDoUpdateWithGlobalHooks (KDB * handle, Split * split, KeySe
for (int p = start; p < end; ++p)
{
int ret = 0;
if (!pgs_done && (p == (STORAGE_PLUGIN + 1)) && handle->globalPlugins[POSTGETSTORAGE])

if (!pgs_done && (p == (STORAGE_PLUGIN + 1)) && handle->globalPlugins[POSTGETSTORAGE][FOREACH])
{
pgs_done = 1;
keySetName (parentKey, keyName (initialParent));
ksRewind (ks);
handle->globalPlugins[POSTGETSTORAGE]->kdbGet (handle->globalPlugins[POSTGETSTORAGE], ks, parentKey);
handle->globalPlugins[POSTGETSTORAGE][FOREACH]->kdbGet (handle->globalPlugins[POSTGETSTORAGE][FOREACH], ks,
parentKey);
keySetName (parentKey, keyName (split->parents[i]));
}
else if (!pgc_done && (p == (NR_OF_PLUGINS - 1)) && handle->globalPlugins[POSTGETCLEANUP])
else if (!pgc_done && (p == (NR_OF_PLUGINS - 1)) && handle->globalPlugins[POSTGETCLEANUP][FOREACH])
{
pgc_done = 1;
keySetName (parentKey, keyName (initialParent));
ksRewind (ks);
handle->globalPlugins[POSTGETCLEANUP]->kdbGet (handle->globalPlugins[POSTGETCLEANUP], ks, parentKey);
handle->globalPlugins[POSTGETCLEANUP][FOREACH]->kdbGet (handle->globalPlugins[POSTGETCLEANUP][FOREACH], ks,
parentKey);
keySetName (parentKey, keyName (split->parents[i]));
}

Expand All @@ -628,10 +640,14 @@ static int elektraGetDoUpdateWithGlobalHooks (KDB * handle, Split * split, KeySe
{
// Ohh, an error occurred,
// lets stop the process.
elektraGlobalError (handle, ks, parentKey, GETSTORAGE, DEINIT);
// elektraGlobalError (handle, ks, parentKey, POSTGETSTORAGE, DEINIT);
return -1;
}
}
}
elektraGlobalGet (handle, ks, parentKey, GETSTORAGE, DEINIT);
// elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, DEINIT);
return 0;
}

Expand Down Expand Up @@ -772,10 +788,11 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey)
ELEKTRA_SET_ERROR (37, parentKey, "handle or ks null pointer");
goto error;
}
if (handle->globalPlugins[PREGETSTORAGE])
{
handle->globalPlugins[PREGETSTORAGE]->kdbGet (handle->globalPlugins[PREGETSTORAGE], ks, parentKey);
}

elektraGlobalGet (handle, ks, parentKey, PREGETSTORAGE, INIT);
elektraGlobalGet (handle, ks, parentKey, PREGETSTORAGE, MAXONCE);
elektraGlobalGet (handle, ks, parentKey, PREGETSTORAGE, DEINIT);

if (splitBuildup (split, handle, parentKey) == -1)
{
clearError (parentKey);
Expand All @@ -788,6 +805,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey)
{
case 0: // We don't need an update so let's do nothing
keySetName (parentKey, keyName (initialParent));
elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, MAXONCE);
splitUpdateFileName (split, handle, parentKey);
keyDel (initialParent);
splitDel (split);
Expand All @@ -807,7 +825,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey)
goto error;
}

if (handle->globalPlugins[POSTGETSTORAGE] || handle->globalPlugins[POSTGETCLEANUP])
if (handle->globalPlugins[POSTGETSTORAGE][FOREACH] || handle->globalPlugins[POSTGETCLEANUP][FOREACH])
{
clearError (parentKey);
if (elektraGetDoUpdateWithGlobalHooks (NULL, split, NULL, parentKey, initialParent, FIRST) == -1)
Expand Down Expand Up @@ -865,6 +883,8 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey)
splitMerge (split, ks);
}

elektraGlobalGet (handle, ks, parentKey, POSTGETSTORAGE, MAXONCE);

ksRewind (ks);

keySetName (parentKey, keyName (initialParent));
Expand All @@ -878,10 +898,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey)

error:
keySetName (parentKey, keyName (initialParent));
if (handle && handle->globalPlugins[POSTGETSTORAGE])
{
handle->globalPlugins[POSTGETSTORAGE]->kdbGet (handle->globalPlugins[POSTGETSTORAGE], ks, parentKey);
}
elektraGlobalError (handle, ks, parentKey, POSTGETSTORAGE, MAXONCE);

keySetName (parentKey, keyName (initialParent));
if (handle) splitUpdateFileName (split, handle, parentKey);
Expand All @@ -903,7 +920,7 @@ int kdbGet (KDB * handle, KeySet * ks, Key * parentKey)
* @retval -1 on error
* @retval 0 on success
*/
static int elektraSetPrepare (Split * split, Key * parentKey, Key ** errorKey, Plugin ** hooks)
static int elektraSetPrepare (Split * split, Key * parentKey, Key ** errorKey, Plugin * hooks[][NR_GLOBAL_SUBPOSITIONS])
{
int any_error = 0;
for (size_t i = 0; i < split->size; i++)
Expand Down Expand Up @@ -948,19 +965,18 @@ static int elektraSetPrepare (Split * split, Key * parentKey, Key ** errorKey, P

if (p == 0)
{
if (hooks[PRESETSTORAGE])
if (hooks[PRESETSTORAGE][FOREACH])
{
// the only place global presetstorage hooks can be executed
ksRewind (split->keysets[i]);
hooks[PRESETSTORAGE]->kdbSet (hooks[PRESETSTORAGE], split->keysets[i], parentKey);
hooks[PRESETSTORAGE][FOREACH]->kdbSet (hooks[PRESETSTORAGE][FOREACH], split->keysets[i], parentKey);
}
}
else if (p == (STORAGE_PLUGIN - 1))
{
if (hooks[PRESETCLEANUP])
if (hooks[PRESETCLEANUP][FOREACH])
{
ksRewind (split->keysets[i]);
hooks[PRESETCLEANUP]->kdbSet (hooks[PRESETCLEANUP], split->keysets[i], parentKey);
hooks[PRESETCLEANUP][FOREACH]->kdbSet (hooks[PRESETCLEANUP][FOREACH], split->keysets[i], parentKey);
}
}

Expand Down Expand Up @@ -1163,7 +1179,11 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey)
int errnosave = errno;
Key * initialParent = keyDup (parentKey);

ELEKTRA_LOG ("now in new kdbSet (%s)\n", keyName (parentKey));
ELEKTRA_LOG ("now in new kdbSet (%s) %p %zd", keyName (parentKey), (void *)handle, ksGetSize (ks));

elektraGlobalSet (handle, ks, parentKey, PRESETSTORAGE, MAXONCE);

ELEKTRA_LOG ("after presetstorage maxonce(%s) %p %zd", keyName (parentKey), (void *)handle, ksGetSize (ks));

Split * split = splitNew ();
Key * errorKey = 0;
Expand Down Expand Up @@ -1222,20 +1242,24 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey)
copyError (parentKey, oldError);
}
keySetName (parentKey, keyName (initialParent));
if (handle->globalPlugins[PRECOMMIT])
{
handle->globalPlugins[PRECOMMIT]->kdbSet (handle->globalPlugins[PRECOMMIT], ks, parentKey);
}

elektraGlobalSet (handle, ks, parentKey, PRECOMMIT, INIT);
elektraGlobalSet (handle, ks, parentKey, PRECOMMIT, MAXONCE);
elektraGlobalSet (handle, ks, parentKey, PRECOMMIT, DEINIT);

elektraSetCommit (split, parentKey);

elektraGlobalSet (handle, ks, parentKey, COMMIT, INIT);
elektraGlobalSet (handle, ks, parentKey, COMMIT, MAXONCE);
elektraGlobalSet (handle, ks, parentKey, COMMIT, DEINIT);

splitUpdateSize (split);

keySetName (parentKey, keyName (initialParent));
if (handle->globalPlugins[POSTCOMMIT])
{
handle->globalPlugins[POSTCOMMIT]->kdbSet (handle->globalPlugins[POSTCOMMIT], ks, parentKey);
}

elektraGlobalSet (handle, ks, parentKey, POSTCOMMIT, INIT);
elektraGlobalSet (handle, ks, parentKey, POSTCOMMIT, MAXONCE);
elektraGlobalSet (handle, ks, parentKey, POSTCOMMIT, DEINIT);

for (size_t i = 0; i < ks->size; ++i)
{
Expand All @@ -1253,10 +1277,8 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey)

error:
keySetName (parentKey, keyName (initialParent));
if (handle->globalPlugins[PREROLLBACK])
{
handle->globalPlugins[PREROLLBACK]->kdbError (handle->globalPlugins[PREROLLBACK], ks, parentKey);
}

elektraGlobalError (handle, ks, parentKey, PREROLLBACK, MAXONCE);

elektraSetRollback (split, parentKey);

Expand All @@ -1270,10 +1292,8 @@ int kdbSet (KDB * handle, KeySet * ks, Key * parentKey)
}

keySetName (parentKey, keyName (initialParent));
if (handle->globalPlugins[POSTROLLBACK])
{
handle->globalPlugins[POSTROLLBACK]->kdbError (handle->globalPlugins[POSTROLLBACK], ks, parentKey);
}

elektraGlobalError (handle, ks, parentKey, POSTROLLBACK, MAXONCE);

keySetName (parentKey, keyName (initialParent));
keyDel (initialParent);
Expand Down
Loading