Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor plugin config logic #459

Merged
Merged
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
34 changes: 10 additions & 24 deletions client/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ TDNFReadConfig(
struct cnfnode *cn_conf = NULL, *cn_top, *cn;
struct cnfmodule *mod_ini;

int nPluginSet = 0;

if(!pTdnf ||
IsNullOrEmptyString(pszConfFile) ||
IsNullOrEmptyString(pszGroup))
Expand Down Expand Up @@ -238,24 +236,15 @@ TDNFReadConfig(
}
else if (strcmp(cn->name, TDNF_CONF_KEY_PLUGINS) == 0)
{
/* presence of option disables plugins, no matter the value */
if(!isTrue(cn->value)) {
dwError = TDNFSetOpt(
pTdnf->pArgs,
TDNF_CONF_KEY_NO_PLUGINS, "1");
BAIL_ON_TDNF_ERROR(dwError);
}
nPluginSet = 1;
pConf->nPluginsEnabled = isTrue(cn->value);
}
else if (strcmp(cn->name, TDNF_CONF_KEY_PLUGIN_CONF_PATH) == 0)
{
dwError = TDNFSetOpt(pTdnf->pArgs, TDNF_CONF_KEY_PLUGIN_CONF_PATH, cn->value);
BAIL_ON_TDNF_ERROR(dwError);
pConf->pszPluginConfPath = strdup(cn->value);
}
else if (strcmp(cn->name, TDNF_CONF_KEY_PLUGIN_PATH) == 0)
{
dwError = TDNFSetOpt(pTdnf->pArgs, TDNF_CONF_KEY_PLUGIN_PATH, cn->value);
BAIL_ON_TDNF_ERROR(dwError);
pConf->pszPluginPath = strdup(cn->value);
}
}

Expand All @@ -270,16 +259,6 @@ TDNFReadConfig(
dwError = TDNFAllocateStringPrintf(&pConf->pszUserAgentHeader, "tdnf/%s %s/%s", pszTdnfVersion, pConf->pszOSName, pConf->pszOSVersion);
BAIL_ON_TDNF_ERROR(dwError);

/* if plugins are not enabled explicitely,
we have to disable them because it's the default */
if (!nPluginSet) {
/* no plugins by default */
dwError = TDNFSetOpt(
pTdnf->pArgs,
TDNF_CONF_KEY_NO_PLUGINS, "1");
BAIL_ON_TDNF_ERROR(dwError);
}

if (pszProxyUser && pszProxyPass)
{
dwError = TDNFAllocateStringPrintf(
Expand All @@ -299,6 +278,11 @@ TDNFReadConfig(
if (pConf->pszPersistDir == NULL)
pConf->pszPersistDir = strdup(TDNF_DEFAULT_DB_LOCATION);

if (pConf->pszPluginPath == NULL)
pConf->pszPluginPath = strdup(TDNF_DEFAULT_PLUGIN_PATH);
if (pConf->pszPluginConfPath == NULL)
pConf->pszPluginConfPath = strdup(TDNF_DEFAULT_PLUGIN_CONF_PATH);

dwError = TDNFDirName(pszConfFile, &pszConfDir);
BAIL_ON_TDNF_ERROR(dwError);

Expand Down Expand Up @@ -407,6 +391,8 @@ TDNFFreeConfig(
TDNF_SAFE_FREE_MEMORY(pConf->pszUserAgentHeader);
TDNF_SAFE_FREE_MEMORY(pConf->pszOSName);
TDNF_SAFE_FREE_MEMORY(pConf->pszOSVersion);
TDNF_SAFE_FREE_MEMORY(pConf->pszPluginPath);
TDNF_SAFE_FREE_MEMORY(pConf->pszPluginConfPath);
TDNF_SAFE_FREE_STRINGARRAY(pConf->ppszExcludes);
TDNF_SAFE_FREE_STRINGARRAY(pConf->ppszMinVersions);
TDNF_SAFE_FREE_STRINGARRAY(pConf->ppszPkgLocks);
Expand Down
103 changes: 26 additions & 77 deletions client/plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@
#include "../llconf/entry.h"
#include "../llconf/ini.h"

struct plugin_config
{
PTDNF_CMD_ARGS pArgs;
char *pszPath;
char *pszConfPath;
};

static
uint32_t
_TDNFLoadPlugins(
PTDNF_CMD_ARGS pArgs,
PTDNF pTdnf,
PTDNF_PLUGIN *ppPlugins
);

Expand Down Expand Up @@ -65,7 +58,7 @@ TDNFLoadPlugins(
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = _TDNFLoadPlugins(pTdnf->pArgs, &pPlugins);
dwError = _TDNFLoadPlugins(pTdnf, &pPlugins);
BAIL_ON_TDNF_ERROR(dwError);

dwError = _TDNFInitPlugins(pTdnf, pPlugins);
Expand Down Expand Up @@ -139,57 +132,6 @@ _TDNFInitPlugins(
goto cleanup;
}

static
uint32_t
_TDNFGetPluginSettings(
struct plugin_config *pConf
)
{
uint32_t dwError = 0;
int nHasOpt = 0;
char *pszConfPath = NULL;
char *pszPath = NULL;

if(!pConf)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = TDNFHasOpt(pConf->pArgs, TDNF_CONF_KEY_NO_PLUGINS, &nHasOpt);
BAIL_ON_TDNF_ERROR(dwError);

if (nHasOpt)
{
dwError = ERROR_TDNF_PLUGINS_DISABLED;
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = TDNFGetOptWithDefault(
pConf->pArgs, TDNF_CONF_KEY_PLUGIN_CONF_PATH,
TDNF_DEFAULT_PLUGIN_CONF_PATH, &pszConfPath);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFGetOptWithDefault(
pConf->pArgs, TDNF_CONF_KEY_PLUGIN_PATH,
TDNF_DEFAULT_PLUGIN_PATH, &pszPath);
BAIL_ON_TDNF_ERROR(dwError);

TDNF_SAFE_FREE_MEMORY(pConf->pszConfPath);
TDNF_SAFE_FREE_MEMORY(pConf->pszPath);

pConf->pszConfPath = pszConfPath;
pConf->pszPath = pszPath;

cleanup:
return dwError;

error:
TDNF_SAFE_FREE_MEMORY(pszConfPath);
TDNF_SAFE_FREE_MEMORY(pszPath);
goto cleanup;
}

static
void
_TDNFClosePlugin(
Expand Down Expand Up @@ -302,7 +244,7 @@ _TDNFLoadPluginConfig(
static
uint32_t
_TDNFLoadPluginConfigs(
struct plugin_config *pConf,
PTDNF pTdnf,
PTDNF_PLUGIN *ppPlugins
)
{
Expand All @@ -315,13 +257,13 @@ _TDNFLoadPluginConfigs(
PTDNF_PLUGIN pLast = NULL;
char *pszPluginConfig = NULL;

if(!pConf || !ppPlugins)
if(!pTdnf || !ppPlugins)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

pDir = opendir(pConf->pszConfPath);
pDir = opendir(pTdnf->pConf->pszPluginConfPath);
if(pDir == NULL)
{
dwError = ERROR_TDNF_NO_PLUGIN_CONF_DIR;
Expand All @@ -339,7 +281,7 @@ _TDNFLoadPluginConfigs(

dwError = TDNFJoinPath(
&pszPluginConfig,
pConf->pszConfPath,
pTdnf->pConf->pszPluginConfPath,
pEnt->d_name,
NULL);
BAIL_ON_TDNF_ERROR(dwError);
Expand Down Expand Up @@ -439,21 +381,21 @@ _TDNFAlterPluginState(
static
uint32_t
_TDNFApplyPluginOverrides(
struct plugin_config *pConf,
PTDNF pTdnf,
PTDNF_PLUGIN pPlugins
)
{
uint32_t dwError = 0;
PTDNF_CMD_OPT pSetOpt = NULL;

if (!pConf || !pPlugins)
if (!pTdnf || !pPlugins)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

/* apply command line overrides to enable/deactivate specific plugins */
for (pSetOpt = pConf->pArgs->pSetOpt; pSetOpt; pSetOpt = pSetOpt->pNext)
for (pSetOpt = pTdnf->pArgs->pSetOpt; pSetOpt; pSetOpt = pSetOpt->pNext)
{
if (strcmp(pSetOpt->pszOptName, "enableplugin") == 0)
{
Expand Down Expand Up @@ -599,37 +541,44 @@ _TDNFLoadPluginLibs(
static
uint32_t
_TDNFLoadPlugins(
PTDNF_CMD_ARGS pArgs,
PTDNF pTdnf,
PTDNF_PLUGIN *ppPlugins
)
{
uint32_t dwError = 0;
PTDNF_PLUGIN pPlugins = NULL;
struct plugin_config stConf = {0};
int nHasOptNoPlugins = 0;

if(!pArgs || !ppPlugins)
if(!pTdnf || !ppPlugins)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

stConf.pArgs = pArgs;
dwError = _TDNFGetPluginSettings(&stConf);
if (!pTdnf->pConf->nPluginsEnabled) {
dwError = ERROR_TDNF_PLUGINS_DISABLED;
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = TDNFHasOpt(pTdnf->pArgs, TDNF_CONF_KEY_NO_PLUGINS, &nHasOptNoPlugins);
BAIL_ON_TDNF_ERROR(dwError);

dwError = _TDNFLoadPluginConfigs(&stConf, &pPlugins);
if (nHasOptNoPlugins) {
dwError = ERROR_TDNF_PLUGINS_DISABLED;
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = _TDNFLoadPluginConfigs(pTdnf, &pPlugins);
BAIL_ON_TDNF_ERROR(dwError);

dwError = _TDNFApplyPluginOverrides(&stConf, pPlugins);
dwError = _TDNFApplyPluginOverrides(pTdnf, pPlugins);
BAIL_ON_TDNF_ERROR(dwError);

dwError = _TDNFLoadPluginLibs(stConf.pszPath, pPlugins);
dwError = _TDNFLoadPluginLibs(pTdnf->pConf->pszPluginPath, pPlugins);
BAIL_ON_TDNF_ERROR(dwError);

*ppPlugins = pPlugins;
cleanup:
TDNF_SAFE_FREE_MEMORY(stConf.pszPath);
TDNF_SAFE_FREE_MEMORY(stConf.pszConfPath);
return dwError;

error:
Expand Down
3 changes: 3 additions & 0 deletions include/tdnftypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ typedef struct _TDNF_CONF
int nOpenMax; //set max number of open files
int nCheckUpdateCompat;
int nDistroSyncReinstallChanged;
int nPluginsEnabled;
char* pszRepoDir;
char* pszCacheDir;
char* pszPersistDir;
Expand All @@ -273,6 +274,8 @@ typedef struct _TDNF_CONF
char** ppszPkgLocks;
char** ppszProtectedPkgs;
char **ppszInstallOnlyPkgs;
char *pszPluginPath;
char *pszPluginConfPath;
}TDNF_CONF, *PTDNF_CONF;

typedef struct _TDNF_REPO_DATA
Expand Down
Loading