Skip to content

Commit

Permalink
add support for url var substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyesh Padmavilasom committed Dec 12, 2015
1 parent 021f902 commit fec206b
Show file tree
Hide file tree
Showing 10 changed files with 456 additions and 18 deletions.
8 changes: 5 additions & 3 deletions client/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,13 @@ TDNFOpenHandle(
dwError = TDNFCloneCmdArgs(pArgs, &pTdnf->pArgs);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFReadConfig(TDNF_CONF_FILE, TDNF_CONF_GROUP, &pTdnf->pConf);
dwError = TDNFReadConfig(pTdnf,
TDNF_CONF_FILE,
TDNF_CONF_GROUP);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFLoadRepoData(
pTdnf->pConf,
pTdnf,
REPOLISTFILTER_ENABLED,
&pTdnf->pRepos);
BAIL_ON_TDNF_ERROR(dwError);
Expand Down Expand Up @@ -623,7 +625,7 @@ TDNFRepoList(
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = TDNFLoadRepoData(pTdnf->pConf, nFilter, &pReposAll);
dwError = TDNFLoadRepoData(pTdnf, nFilter, &pReposAll);
BAIL_ON_TDNF_ERROR(dwError);

*ppReposAll = pReposAll;
Expand Down
133 changes: 127 additions & 6 deletions client/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ TDNFConfGetRpmVerbosity(

uint32_t
TDNFReadConfig(
PTDNF pTdnf,
char* pszFile,
char* pszGroup,
PTDNF_CONF* ppConf
char* pszGroup
)
{
uint32_t dwError = 0;
Expand All @@ -47,6 +47,14 @@ TDNFReadConfig(

PTDNF_CONF pConf = NULL;

if(!pTdnf ||
IsNullOrEmptyString(pszFile) ||
IsNullOrEmptyString(pszGroup))
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

pKeyFile = g_key_file_new();
if(!pKeyFile)
{
Expand Down Expand Up @@ -135,6 +143,21 @@ TDNFReadConfig(
&pConf->pszCacheDir);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFReadKeyValue(
pKeyFile,
pszGroup,
TDNF_CONF_KEY_DISTROVERPKG,
TDNF_DEFAULT_DISTROVERPKG,
&pConf->pszDistroVerPkg);
if(dwError == ERROR_TDNF_NO_DATA)
{
dwError = TDNFAllocateString(
TDNF_DEFAULT_DISTROVERPKG,
&pConf->pszDistroVerPkg);
BAIL_ON_TDNF_ERROR(dwError);
}
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFConfigReadProxySettings(
pKeyFile,
pszGroup,
Expand All @@ -143,7 +166,7 @@ TDNFReadConfig(

}

*ppConf = pConf;
pTdnf->pConf = pConf;

cleanup:
if(pKeyFile)
Expand All @@ -157,9 +180,9 @@ TDNFReadConfig(
return dwError;

error:
if(ppConf)
if(pTdnf)
{
*ppConf = NULL;
pTdnf->pConf = NULL;
}
if(pConf)
{
Expand All @@ -168,6 +191,44 @@ TDNFReadConfig(
goto cleanup;
}

uint32_t
TDNFConfigExpandVars(
PTDNF pTdnf
)
{
uint32_t dwError = 0;
PTDNF_CONF pConf = NULL;

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

if(!pConf->pszVarReleaseVer &&
!IsNullOrEmptyString(pConf->pszDistroVerPkg))
{
dwError = TDNFRawGetPackageVersion(
pTdnf->pArgs->pszInstallRoot,
pConf->pszDistroVerPkg,
&pConf->pszVarReleaseVer);
BAIL_ON_TDNF_ERROR(dwError);
}

if(!pConf->pszVarBaseArch)
{
dwError = TDNFGetKernelArch(&pConf->pszVarBaseArch);
BAIL_ON_TDNF_ERROR(dwError);
}

cleanup:
return dwError;

error:
goto cleanup;
}

uint32_t
TDNFConfigReadProxySettings(
GKeyFile* pKeyFile,
Expand Down Expand Up @@ -279,7 +340,7 @@ TDNFReadKeyValue(
pszVal = g_strdup(pszDefault);
if(!pszVal)
{
dwError = ERROR_TDNF_OUT_OF_MEMORY;
dwError = ERROR_TDNF_OUT_OF_MEMORY;
BAIL_ON_TDNF_ERROR(dwError);
}
}
Expand Down Expand Up @@ -320,6 +381,66 @@ TDNFFreeConfig(
TDNF_SAFE_FREE_MEMORY(pConf->pszProxyUserPass);
TDNF_SAFE_FREE_MEMORY(pConf->pszRepoDir);
TDNF_SAFE_FREE_MEMORY(pConf->pszCacheDir);
TDNF_SAFE_FREE_MEMORY(pConf->pszDistroVerPkg);
TDNF_SAFE_FREE_MEMORY(pConf->pszVarReleaseVer);
TDNF_SAFE_FREE_MEMORY(pConf->pszVarBaseArch);
TDNF_SAFE_FREE_MEMORY(pConf);
}
}

uint32_t
TDNFConfigReplaceVars(
PTDNF pTdnf,
char** ppszString
)
{
uint32_t dwError = 0;
char* pszDst = NULL;
char* pszReplacedTemp = NULL;
PTDNF_CONF pConf = NULL;

if(!pTdnf || !ppszString || IsNullOrEmptyString(*ppszString))
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

//fill variable values such as release and basearch
//if required
if(strstr(*ppszString, TDNF_VAR_RELEASEVER) ||
strstr(*ppszString, TDNF_VAR_BASEARCH))
{
dwError = TDNFConfigExpandVars(pTdnf);
BAIL_ON_TDNF_ERROR(dwError);
}
else
{
goto cleanup;
}

pConf = pTdnf->pConf;
dwError = TDNFReplaceString(
*ppszString,
TDNF_VAR_RELEASEVER,
pConf->pszVarReleaseVer,
&pszReplacedTemp);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFReplaceString(
pszReplacedTemp,
TDNF_VAR_BASEARCH,
pConf->pszVarBaseArch,
&pszDst);
BAIL_ON_TDNF_ERROR(dwError);

TDNFFreeMemory(*ppszString);
*ppszString = pszDst;

cleanup:
TDNF_SAFE_FREE_MEMORY(pszReplacedTemp);
return dwError;

error:
TDNF_SAFE_FREE_MEMORY(pszDst);
goto cleanup;
}
9 changes: 9 additions & 0 deletions client/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ typedef enum
#define TDNF_CONF_KEY_PROXY_USER "proxy_username"
#define TDNF_CONF_KEY_PROXY_PASS "proxy_password"
#define TDNF_CONF_KEY_KEEP_CACHE "keepcache"
#define TDNF_CONF_KEY_DISTROVERPKG "distroverpkg"
#define TDNF_CONF_KEY_DISTROARCHPKG "distroarchpkg"
//Repo file key names
#define TDNF_REPO_KEY_BASEURL "baseurl"
#define TDNF_REPO_KEY_ENABLED "enabled"
Expand All @@ -113,8 +115,13 @@ typedef enum
//Repo defaults
#define TDNF_DEFAULT_REPO_LOCATION "/etc/yum.repos.d"
#define TDNF_DEFAULT_CACHE_LOCATION "/var/cache/tdnf"
#define TDNF_DEFAULT_DISTROVERPKG "photon-release"
#define TDNF_DEFAULT_DISTROARCHPKG "x86_64"
#define TDNF_RPM_CACHE_DIR_NAME "rpms"
#define TDNF_REPODATA_DIR_NAME "repodata"
//var names
#define TDNF_VAR_RELEASEVER "$releasever"
#define TDNF_VAR_BASEARCH "$basearch"

#define TDNF_UNKNOWN_ERROR_STRING "Unknown error"
#define TDNF_ERROR_TABLE \
Expand All @@ -128,6 +135,8 @@ typedef enum
{ERROR_TDNF_NO_MATCH, "ERROR_TDNF_NO_MATCH", "No matching packages"}, \
{ERROR_TDNF_SET_PROXY, "ERROR_TDNF_SET_PROXY", "There was an error setting the proxy server."}, \
{ERROR_TDNF_SET_PROXY_USERPASS, "ERROR_TDNF_SET_PROXY_USERPASS", "There was an error setting the proxy server user and pass"}, \
{ERROR_TDNF_NO_DISTROVERPKG, "ERROR_TDNF_NO_DISTROVERPKG", "distroverpkg config entry is set to a package that is not installed. Check /etc/tdnf/tdnf.conf"}, \
{ERROR_TDNF_DISTROVERPKG_READ, "ERROR_TDNF_DISTROVERPKG_READ", "There was an error reading version of distroverpkg"}, \
{ERROR_TDNF_NO_ENABLED_REPOS, "ERROR_TDNF_NO_ENABLED_REPOS", "There are no enabled repos.\n Run ""tdnf repolist all"" to see the repos you have.\n You can enable repos by editing repo files in your repodir(usually /etc/yum.repos.d)"}, \
{ERROR_TDNF_PACKAGELIST_EMPTY, "ERROR_TDNF_PACKAGELIST_EMPTY", "Packagelist was empty"}, \
{ERROR_TDNF_GOAL_CREATE, "ERROR_TDNF_GOAL_CREATE", "Error creating goal"}, \
Expand Down
4 changes: 4 additions & 0 deletions client/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include <errno.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
//
#include <sys/utsname.h>
//glib
#include <glib.h>
#include <glib/gstdio.h>
Expand Down Expand Up @@ -54,6 +57,7 @@
#include <rpm/rpmps.h>
#include <rpm/rpmts.h>
#include <rpm/rpmkeyring.h>
#include <rpm/header.h>

#include <tdnfclient.h>

Expand Down
45 changes: 42 additions & 3 deletions client/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,14 @@ TDNFReadKeyValue(

uint32_t
TDNFReadConfig(
PTDNF pTdnf,
char* pszConfFile,
char* pszConfGroup,
PTDNF_CONF* ppConf
char* pszConfGroup
);

uint32_t
TDNFConfigExpandVars(
PTDNF pTdnf
);

uint32_t
Expand All @@ -371,6 +376,12 @@ TDNFFreeConfig(
PTDNF_CONF pConf
);

uint32_t
TDNFConfigReplaceVars(
PTDNF pTdnf,
char** pszString
);

//repo.c
uint32_t
TDNFPrintRepoMetadata(
Expand Down Expand Up @@ -407,13 +418,14 @@ TDNFGetRepoById(
//repolist.c
uint32_t
TDNFLoadReposFromFile(
PTDNF pTdnf,
char* pszRepoFile,
PTDNF_REPO_DATA* ppRepos
);

uint32_t
TDNFLoadRepoData(
PTDNF_CONF pConf,
PTDNF pTdnf,
TDNF_REPOLISTFILTER nFilter,
PTDNF_REPO_DATA* ppReposAll
);
Expand Down Expand Up @@ -684,6 +696,21 @@ TDNFAllocateStringPrintf(
const char* pszFmt,
...
);

uint32_t
TDNFAllocateStringN(
const char* pszSrc,
uint32_t dwNumElements,
char** ppszDst
);

uint32_t
TDNFReplaceString(
const char* pszSource,
const char* pszSearch,
const char* pszReplace,
char** ppszDst
);
//updateinfo.c
uint32_t
TDNFGetUpdateInfoPackages(
Expand Down Expand Up @@ -733,6 +760,18 @@ TDNFUtilsMakeDirs(
const char* pszPath
);

uint32_t
TDNFRawGetPackageVersion(
const char* pszRootDir,
const char* pszPkg,
char** ppszVersion
);

uint32_t
TDNFGetKernelArch(
char** ppszArch
);

//validate.c
uint32_t
TDNFValidateCmdArgs(
Expand Down
Loading

0 comments on commit fec206b

Please sign in to comment.