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 --setopts handling #464

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
2 changes: 1 addition & 1 deletion ci/Dockerfile.fedora
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM fedora:34
FROM fedora:39

RUN dnf -y upgrade
RUN dnf -y install gcc make cmake libcurl-devel rpm-devel rpm-build \
Expand Down
71 changes: 1 addition & 70 deletions client/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,8 @@ TDNFOpenHandle(
uint32_t dwError = 0;
PTDNF pTdnf = NULL;
PSolvSack pSack = NULL;
char *pszCacheDir = NULL;
char *pszRepoDir = NULL;
char *pszConfFile = NULL;
char *pszConfFileInstallRoot = NULL;
int nHasOptReposDir = 0;
int nHasOptCacheDir = 0;
PTDNF_CMD_OPT pOpt = NULL;

if(!pArgs || !ppTdnf)
Expand Down Expand Up @@ -680,69 +676,6 @@ TDNFOpenHandle(

GlobalSetDnfCheckUpdateCompat(pTdnf->pConf->nCheckUpdateCompat);

dwError = TDNFHasOpt(pTdnf->pArgs, TDNF_CONF_KEY_REPOSDIR, &nHasOptReposDir);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFHasOpt(pTdnf->pArgs, TDNF_CONF_KEY_CACHEDIR, &nHasOptCacheDir);
BAIL_ON_TDNF_ERROR(dwError);

if (!IsNullOrEmptyString(pTdnf->pArgs->pszInstallRoot) &&
strcmp(pTdnf->pArgs->pszInstallRoot, "/"))
{
int nIsDir = 0;

if(!nHasOptCacheDir)
{
dwError = TDNFJoinPath(&pszCacheDir,
pTdnf->pArgs->pszInstallRoot,
pTdnf->pConf->pszCacheDir,
NULL);
BAIL_ON_TDNF_ERROR(dwError);

TDNF_SAFE_FREE_MEMORY(pTdnf->pConf->pszCacheDir);
pTdnf->pConf->pszCacheDir = pszCacheDir;
pszCacheDir = NULL;
}

if (!nHasOptReposDir)
{
dwError = TDNFJoinPath(&pszRepoDir,
pTdnf->pArgs->pszInstallRoot,
pTdnf->pConf->pszRepoDir,
NULL);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFIsDir(pszRepoDir, &nIsDir);
if (dwError == ERROR_TDNF_FILE_NOT_FOUND)
{
nIsDir = 0;
dwError = 0;
}
BAIL_ON_TDNF_ERROR(dwError);
if (nIsDir)
{
TDNF_SAFE_FREE_MEMORY(pTdnf->pConf->pszRepoDir);
pTdnf->pConf->pszRepoDir = pszRepoDir;
pszRepoDir = NULL;
}
}
}

/* cachedir and reoposdir when set with setopt are always relative to host */
if (nHasOptReposDir)
{
TDNF_SAFE_FREE_MEMORY(pTdnf->pConf->pszRepoDir);
dwError = TDNFGetCmdOptValue(pTdnf->pArgs, TDNF_CONF_KEY_REPOSDIR, &pTdnf->pConf->pszRepoDir);
BAIL_ON_TDNF_ERROR(dwError);
}

if (nHasOptCacheDir)
{
TDNF_SAFE_FREE_MEMORY(pTdnf->pConf->pszCacheDir);
dwError = TDNFGetCmdOptValue(pTdnf->pArgs, TDNF_CONF_KEY_CACHEDIR, &pTdnf->pConf->pszCacheDir);
BAIL_ON_TDNF_ERROR(dwError);
}

for (pOpt = pTdnf->pArgs->pSetOpt; pOpt; pOpt = pOpt->pNext)
{
/* set macros from command line */
Expand All @@ -769,7 +702,7 @@ TDNFOpenHandle(

if(!pArgs->nAllDeps)
{
dwError = SolvReadInstalledRpms(pSack->pPool->installed, pszCacheDir);
dwError = SolvReadInstalledRpms(pSack->pPool->installed, pTdnf->pConf->pszCacheDir);
BAIL_ON_TDNF_LIBSOLV_ERROR(dwError);
}

Expand All @@ -791,8 +724,6 @@ TDNFOpenHandle(
*ppTdnf = pTdnf;

cleanup:
TDNF_SAFE_FREE_MEMORY(pszCacheDir);
TDNF_SAFE_FREE_MEMORY(pszRepoDir);
TDNF_SAFE_FREE_MEMORY(pszConfFile);
TDNF_SAFE_FREE_MEMORY(pszConfFileInstallRoot);
return dwError;
Expand Down
138 changes: 126 additions & 12 deletions client/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
#include "../llconf/entry.h"
#include "../llconf/ini.h"


#define USERAGENT_HEADER_MAX_LENGTH 256
#define OS_CONF_FILE "/etc/os-release"


int
TDNFConfGetRpmVerbosity(
PTDNF pTdnf
Expand Down Expand Up @@ -82,6 +84,42 @@ static uint32_t TDNFParseOSInfo(PTDNF_CONF pConf)
goto cleanup;
}

struct {
char *name;
rpmtransFlags flag;
} rpmtransflags_map[] = {
{"noscripts", RPMTRANS_FLAG_NOSCRIPTS },
{"justdb", RPMTRANS_FLAG_JUSTDB },
{"notriggers", RPMTRANS_FLAG_NOTRIGGERS },
{"nodocs", RPMTRANS_FLAG_NODOCS },
{"allfiles", RPMTRANS_FLAG_ALLFILES },
{"noplugins", RPMTRANS_FLAG_NOPLUGINS },
{"nocontexts", RPMTRANS_FLAG_NOCONTEXTS },
{"nocaps", RPMTRANS_FLAG_NOCAPS},
{"nodb", RPMTRANS_FLAG_NODB},

// {"nopreuntrans", RPMTRANS_FLAG_NOPREUNTRANS },
// {"nopostuntrans", RPMTRANS_FLAG_NOPOSTUNTRANS },
{"notriggerprein", RPMTRANS_FLAG_NOTRIGGERPREIN },
{"nopre", RPMTRANS_FLAG_NOPRE },
{"nopost", RPMTRANS_FLAG_NOPOST },
{"notriggerin", RPMTRANS_FLAG_NOTRIGGERIN },
{"notriggerun", RPMTRANS_FLAG_NOTRIGGERUN },
{"nopreun", RPMTRANS_FLAG_NOPREUN },
{"nopostun", RPMTRANS_FLAG_NOPOSTUN },
{"notriggerpostun", RPMTRANS_FLAG_NOTRIGGERPOSTUN },
{"nopretrans", RPMTRANS_FLAG_NOPRETRANS },
{"noposttrans", RPMTRANS_FLAG_NOPOSTTRANS},
// {"nosysusers", RPMTRANS_FLAG_NOSYSUSERS},
{"nomd5", RPMTRANS_FLAG_NOMD5},
{"nofiledigest", RPMTRANS_FLAG_NOFILEDIGEST},

{"noartifacts", RPMTRANS_FLAG_NOARTIFACTS},
{"noconfigs", RPMTRANS_FLAG_NOCONFIGS},
{"deploops", RPMTRANS_FLAG_DEPLOOPS},
{NULL, RPMTRANS_FLAG_NONE}
};

static
uint32_t
TDNFConfigFromCnfTree(PTDNF_CONF pConf, struct cnfnode *cn_top)
Expand Down Expand Up @@ -137,14 +175,12 @@ TDNFConfigFromCnfTree(PTDNF_CONF pConf, struct cnfnode *cn_top)
}
else if (strcmp(cn->name, TDNF_CONF_KEY_EXCLUDE) == 0)
{
dwError = TDNFSplitStringToArray(cn->value,
" ", &pConf->ppszExcludes);
dwError = TDNFAddStringArray(&pConf->ppszExcludes, cn->value);
BAIL_ON_TDNF_ERROR(dwError);
}
else if (strcmp(cn->name, TDNF_CONF_KEY_MINVERSIONS) == 0)
{
dwError = TDNFSplitStringToArray(cn->value,
" ", &pConf->ppszMinVersions);
dwError = TDNFAddStringArray(&pConf->ppszMinVersions, cn->value);
BAIL_ON_TDNF_ERROR(dwError);
}
else if (strcmp(cn->name, TDNF_CONF_KEY_OPENMAX) == 0)
Expand Down Expand Up @@ -173,8 +209,7 @@ TDNFConfigFromCnfTree(PTDNF_CONF pConf, struct cnfnode *cn_top)
}
else if (strcmp(cn->name, TDNF_CONF_KEY_INSTALLONLYPKGS) == 0)
{
dwError = TDNFSplitStringToArray(cn->value,
" ", &pConf->ppszInstallOnlyPkgs);
dwError = TDNFAddStringArray(&pConf->ppszInstallOnlyPkgs, cn->value);
BAIL_ON_TDNF_ERROR(dwError);
}
else if (strcmp(cn->name, TDNF_CONF_KEY_VARS_DIRS) == 0)
Expand All @@ -195,10 +230,45 @@ TDNFConfigFromCnfTree(PTDNF_CONF pConf, struct cnfnode *cn_top)
{
SET_STRING(pConf->pszPluginPath, cn->value);
}
else if (strcmp(cn->name, TDNF_CONF_KEY_TSFLAGS) == 0)
{
if (cn->value == NULL || strcmp(cn->value, "") == 0) {
pConf->rpmTransFlags = RPMTRANS_FLAG_NONE;
} else {
char *value = strdup(cn->value);
char *saveptr = NULL, *str, *token;
int i;

for (str = value; ; str = NULL){
token = strtok_r(str, " ", &saveptr);
if (token == NULL)
break;

for (i = 0; rpmtransflags_map[i].name != NULL; i++) {
if (strcmp(token, rpmtransflags_map[i].name) == 0){
pConf->rpmTransFlags |= rpmtransflags_map[i].flag;
break;
}
}
if (rpmtransflags_map[i].name == NULL) {
pr_err("unknown tsflag '%s'\n", token);
free(value);
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}
/* in rpmts.h, deprecated flags will be set to 0. Warn user about it. */
if (rpmtransflags_map[i].flag == 0) {
pr_info("flag tsflag '%s' is not suported and has no effect\n", token);
}
}
free(value);
}
}
}

if (pszProxyUser && pszProxyPass)
{
TDNF_SAFE_FREE_MEMORY(pConf->pszProxyUserPass);
dwError = TDNFAllocateStringPrintf(
&pConf->pszProxyUserPass,
"%s:%s",
Expand Down Expand Up @@ -227,9 +297,9 @@ TDNFReadConfig(
char *pszMinVersionsDir = NULL;
char *pszPkgLocksDir = NULL;
char *pszProtectedDir = NULL;

char *pszCacheDir = NULL;
char *pszRepoDir = NULL;
const char *pszTdnfVersion = NULL;

struct cnfnode *cn_conf = NULL;
struct cnfmodule *mod_ini;

Expand Down Expand Up @@ -286,6 +356,51 @@ TDNFReadConfig(
dwError = TDNFConfigFromCnfTree(pConf, cn_conf->first_child);
BAIL_ON_TDNF_ERROR(dwError);

if (pConf->pszRepoDir == NULL)
pConf->pszRepoDir = strdup(TDNF_DEFAULT_REPO_LOCATION);
if (pConf->pszCacheDir == NULL)
pConf->pszCacheDir = strdup(TDNF_DEFAULT_CACHE_LOCATION);

if (!IsNullOrEmptyString(pTdnf->pArgs->pszInstallRoot) &&
strcmp(pTdnf->pArgs->pszInstallRoot, "/"))
{
int nIsDir = 0;
dwError = TDNFJoinPath(&pszCacheDir,
pTdnf->pArgs->pszInstallRoot,
pConf->pszCacheDir,
NULL);
BAIL_ON_TDNF_ERROR(dwError);

TDNF_SAFE_FREE_MEMORY(pConf->pszCacheDir);
pConf->pszCacheDir = pszCacheDir;
pszCacheDir = NULL;

dwError = TDNFJoinPath(&pszRepoDir,
pTdnf->pArgs->pszInstallRoot,
pConf->pszRepoDir,
NULL);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFIsDir(pszRepoDir, &nIsDir);
if (dwError == ERROR_TDNF_FILE_NOT_FOUND)
{
nIsDir = 0;
dwError = 0;
}
BAIL_ON_TDNF_ERROR(dwError);
if (nIsDir)
{
TDNF_SAFE_FREE_MEMORY(pConf->pszRepoDir);
pConf->pszRepoDir = pszRepoDir;
pszRepoDir = NULL;
}
}

if (pTdnf->pArgs->cn_setopts) {
dwError = TDNFConfigFromCnfTree(pConf, pTdnf->pArgs->cn_setopts);
BAIL_ON_TDNF_ERROR(dwError);
}

pszTdnfVersion = TDNFGetVersion();

if (pConf->pszOSName == NULL)
Expand All @@ -297,15 +412,12 @@ TDNFReadConfig(
dwError = TDNFAllocateStringPrintf(&pConf->pszUserAgentHeader, "tdnf/%s %s/%s", pszTdnfVersion, pConf->pszOSName, pConf->pszOSVersion);
BAIL_ON_TDNF_ERROR(dwError);

if (pConf->pszRepoDir == NULL)
pConf->pszRepoDir = strdup(TDNF_DEFAULT_REPO_LOCATION);
if (pConf->pszCacheDir == NULL)
pConf->pszCacheDir = strdup(TDNF_DEFAULT_CACHE_LOCATION);
if (pConf->ppszDistroVerPkgs == NULL) {
dwError = TDNFSplitStringToArray(TDNF_DEFAULT_DISTROVERPKGS,
" ", &pConf->ppszDistroVerPkgs);
BAIL_ON_TDNF_ERROR(dwError);
}

if (pConf->pszPersistDir == NULL)
pConf->pszPersistDir = strdup(TDNF_DEFAULT_DB_LOCATION);

Expand Down Expand Up @@ -345,6 +457,8 @@ TDNFReadConfig(

cleanup:
destroy_cnftree(cn_conf);
TDNF_SAFE_FREE_MEMORY(pszCacheDir);
TDNF_SAFE_FREE_MEMORY(pszRepoDir);
TDNF_SAFE_FREE_MEMORY(pszConfDir);
TDNF_SAFE_FREE_MEMORY(pszMinVersionsDir);
TDNF_SAFE_FREE_MEMORY(pszPkgLocksDir);
Expand Down
Loading
Loading