Skip to content

Commit

Permalink
Merge pull request #246 from oliverkurth/topic/okurth/repoid
Browse files Browse the repository at this point in the history
add --repoid/--repo and --repofrompath options
  • Loading branch information
oliverkurth authored Nov 23, 2021
2 parents 76e296d + d050e53 commit 4626652
Show file tree
Hide file tree
Showing 15 changed files with 394 additions and 110 deletions.
8 changes: 4 additions & 4 deletions client/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ TDNFAddCmdLinePackages(
pTdnf,
pszPkgName,
basename(pszCopyOfPkgName),
"@cmdline",
CMDLINE_REPO_NAME,
&pszRPMPath
);
BAIL_ON_TDNF_ERROR(dwError);
Expand Down Expand Up @@ -1063,7 +1063,7 @@ TDNFRepoSync(
/* count enabled repos */
for (pRepo = pTdnf->pRepos; pRepo; pRepo = pRepo->pNext)
{
if ((strcmp(pRepo->pszName, "@cmdline") == 0) ||
if ((strcmp(pRepo->pszName, CMDLINE_REPO_NAME) == 0) ||
(!pRepo->nEnabled))
{
continue;
Expand Down Expand Up @@ -1250,7 +1250,7 @@ TDNFRepoSync(
marker file */
for (pRepo = pTdnf->pRepos; pRepo; pRepo = pRepo->pNext)
{
if ((strcmp(pRepo->pszName, "@cmdline") == 0) ||
if ((strcmp(pRepo->pszName, CMDLINE_REPO_NAME) == 0) ||
(!pRepo->nEnabled))
{
continue;
Expand Down Expand Up @@ -1280,7 +1280,7 @@ TDNFRepoSync(
{
for (pRepo = pTdnf->pRepos; pRepo; pRepo = pRepo->pNext)
{
if ((strcmp(pRepo->pszName, "@cmdline") == 0) ||
if ((strcmp(pRepo->pszName, CMDLINE_REPO_NAME) == 0) ||
(!pRepo->nEnabled))
{
continue;
Expand Down
7 changes: 5 additions & 2 deletions client/defines.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2020 VMware, Inc. All Rights Reserved.
* Copyright (C) 2015-2021 VMware, Inc. All Rights Reserved.
*
* Licensed under the GNU Lesser General Public License v2.1 (the "License");
* you may not use this file except in compliance with the License. The terms
Expand Down Expand Up @@ -49,6 +49,8 @@ typedef enum
} \
} while(0)

#define STR_IS_TRUE(s) ((s) && (!strcmp((s), "1") || !strcasecmp((s), "true")))

//Misc
#define TDNF_RPM_EXT ".rpm"
#define TDNF_NAME "tdnf"
Expand Down Expand Up @@ -136,7 +138,8 @@ typedef enum
#define TDNF_REPO_DEFAULT_SSLVERIFY 1
#define TDNF_REPO_DEFAULT_RETRIES 10
#define TDNF_REPO_DEFAULT_PRIORITY 50
#define TDNF_REPO_DEFAULT_METADATA_EXPIRE "172800" // 48 hours in seconds
#define TDNF_REPO_DEFAULT_METADATA_EXPIRE 172800 // 48 hours in seconds
#define TDNF_REPO_DEFAULT_METADATA_EXPIRE_STR STRINGIFYX(TDNF_REPO_DEFAULT_METADATA_EXPIRE)

// var names
#define TDNF_VAR_RELEASEVER "$releasever"
Expand Down
4 changes: 2 additions & 2 deletions client/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ TDNFRefreshSack(
{
/* skip the @cmdline repo - options do not apply, and it is
initialized. */
if ((strcmp(pRepo->pszName, "@cmdline") == 0) ||
if ((strcmp(pRepo->pszName, CMDLINE_REPO_NAME) == 0) ||
(!pRepo->nEnabled))
{
continue;
Expand All @@ -271,7 +271,7 @@ TDNFRefreshSack(

for(pRepo = pTdnf->pRepos; pRepo; pRepo = pRepo->pNext)
{
if ((strcmp(pRepo->pszName, "@cmdline") == 0) ||
if ((strcmp(pRepo->pszName, CMDLINE_REPO_NAME) == 0) ||
(!pRepo->nEnabled))
{
continue;
Expand Down
13 changes: 13 additions & 0 deletions client/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,19 @@ TDNFCreateCmdLineRepo(
PTDNF_REPO_DATA_INTERNAL* ppRepo
);

uint32_t
TDNFCreateRepoFromPath(
PTDNF_REPO_DATA_INTERNAL* ppRepo,
const char *pzsId,
const char *pszPath
);

uint32_t
TDNFCreateRepo(
PTDNF_REPO_DATA_INTERNAL* ppRepo,
const char *pszId
);

uint32_t
TDNFLoadRepoData(
PTDNF pTdnf,
Expand Down
2 changes: 1 addition & 1 deletion client/repo.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ TDNFInitCmdLineRepo(
(void**)&pSolvRepoInfo);
BAIL_ON_TDNF_ERROR(dwError);

pRepo = repo_create(pPool, "@cmdline");
pRepo = repo_create(pPool, CMDLINE_REPO_NAME);
if (!pRepo)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
Expand Down
158 changes: 151 additions & 7 deletions client/repolist.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ TDNFLoadRepoData(
PTDNF_REPO_DATA_INTERNAL pReposTemp = NULL;
PTDNF_REPO_DATA_INTERNAL pRepos = NULL;
PTDNF_CONF pConf = NULL;
PTDNF_CMD_OPT pSetOpt = NULL;
DIR *pDir = NULL;
struct dirent *pEnt = NULL;
int nLen = 0;
int nLenRepoExt = 0;
char **ppszUrlIdTuple = NULL;

if(!pTdnf || !pTdnf->pConf || !ppReposAll)
if(!pTdnf || !pTdnf->pConf || !pTdnf->pArgs || !ppReposAll)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
Expand All @@ -49,6 +51,28 @@ TDNFLoadRepoData(
dwError = TDNFCreateCmdLineRepo(&pReposAll);
BAIL_ON_TDNF_ERROR(dwError);

for(pSetOpt = pTdnf->pArgs->pSetOpt;
pSetOpt;
pSetOpt = pSetOpt->pNext)
{
if(strcmp(pSetOpt->pszOptName, "repofrompath") == 0)
{
TDNFSplitStringToArray(pSetOpt->pszOptValue, ",", &ppszUrlIdTuple);
if ((ppszUrlIdTuple[0] == NULL) || ppszUrlIdTuple[1] == NULL)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}
dwError = TDNFCreateRepoFromPath(&pReposAll,
ppszUrlIdTuple[0],
ppszUrlIdTuple[1]);
BAIL_ON_TDNF_ERROR(dwError);

TDNF_SAFE_FREE_STRINGARRAY(ppszUrlIdTuple);
ppszUrlIdTuple = NULL;
}
}

pDir = opendir(pConf->pszRepoDir);
if(pDir == NULL)
{
Expand Down Expand Up @@ -111,6 +135,7 @@ TDNFLoadRepoData(
closedir(pDir);
}
TDNF_SAFE_FREE_MEMORY(pszRepoFilePath);
TDNF_SAFE_FREE_STRINGARRAY(ppszUrlIdTuple);

return dwError;
error:
Expand Down Expand Up @@ -139,17 +164,120 @@ TDNFCreateCmdLineRepo(
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = TDNFCreateRepo(&pRepo, CMDLINE_REPO_NAME);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFSafeAllocateString(CMDLINE_REPO_NAME, &pRepo->pszName);
BAIL_ON_TDNF_ERROR(dwError);

*ppRepo = pRepo;
cleanup:
return dwError;
error:
if(pRepo)
{
TDNFFreeReposInternal(pRepo);
}
goto cleanup;
}

uint32_t
TDNFCreateRepoFromPath(
PTDNF_REPO_DATA_INTERNAL* ppRepo,
const char *pszId,
const char *pszPath
)
{
uint32_t dwError = 0;
PTDNF_REPO_DATA_INTERNAL pRepo = NULL;
int nIsDir = 0;
int nDummy = 0;

if(!ppRepo || !pszId || !pszPath)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = TDNFCreateRepo(&pRepo, pszId);
BAIL_ON_TDNF_ERROR(dwError);

/* we want it enabled, or there was no point in adding it */
pRepo->nEnabled = 1;

dwError = TDNFSafeAllocateString(pszId, &pRepo->pszName);
BAIL_ON_TDNF_ERROR(dwError);

/* '/some/dir' => 'file:///some/dir */
if (pszPath[0] == '/')
{
dwError = TDNFIsDir(pszPath, &nIsDir);
BAIL_ON_TDNF_ERROR(dwError);

if (nIsDir)
{
dwError = TDNFAllocateStringPrintf(&pRepo->pszBaseUrl, "file://%s", pszPath);
BAIL_ON_TDNF_ERROR(dwError);
}
}
else
{
/* valid prefixes including file:// will not return an error */
dwError = TDNFUriIsRemote(pszPath, &nDummy);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFSafeAllocateString(pszPath, &pRepo->pszBaseUrl);
BAIL_ON_TDNF_ERROR(dwError);
}
*ppRepo = pRepo;
cleanup:
return dwError;
error:
if(ppRepo)
{
*ppRepo = NULL;
}
if(pRepo)
{
TDNFFreeReposInternal(pRepo);
}
goto cleanup;
}

uint32_t
TDNFCreateRepo(
PTDNF_REPO_DATA_INTERNAL* ppRepo,
const char *pszId
)
{
uint32_t dwError = 0;
PTDNF_REPO_DATA_INTERNAL pRepo = NULL;

if(!ppRepo || !pszId)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = TDNFAllocateMemory(
1,
sizeof(TDNF_REPO_DATA_INTERNAL),
(void**)&pRepo);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFSafeAllocateString("@cmdline", &pRepo->pszName);
dwError = TDNFSafeAllocateString(pszId, &pRepo->pszId);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFSafeAllocateString("@cmdline", &pRepo->pszId);
BAIL_ON_TDNF_ERROR(dwError);
pRepo->nEnabled = TDNF_REPO_DEFAULT_ENABLED;
pRepo->nSkipIfUnavailable = TDNF_REPO_DEFAULT_SKIP;
pRepo->nGPGCheck = TDNF_REPO_DEFAULT_GPGCHECK;
pRepo->nSSLVerify = TDNF_REPO_DEFAULT_SSLVERIFY;
pRepo->lMetadataExpire = TDNF_REPO_DEFAULT_METADATA_EXPIRE;
pRepo->nPriority = TDNF_REPO_DEFAULT_PRIORITY;
pRepo->nTimeout = TDNF_REPO_DEFAULT_TIMEOUT;
pRepo->nMinrate = TDNF_REPO_DEFAULT_MINRATE;
pRepo->nThrottle = TDNF_REPO_DEFAULT_THROTTLE;
pRepo->nRetries = TDNF_REPO_DEFAULT_RETRIES;

*ppRepo = pRepo;
cleanup:
Expand Down Expand Up @@ -400,7 +528,7 @@ TDNFLoadReposFromFile(
dwError = TDNFReadKeyValue(
pSections,
TDNF_REPO_KEY_METADATA_EXPIRE,
TDNF_REPO_DEFAULT_METADATA_EXPIRE,
TDNF_REPO_DEFAULT_METADATA_EXPIRE_STR,
&pszMetadataExpire);
BAIL_ON_TDNF_ERROR(dwError);

Expand Down Expand Up @@ -451,6 +579,7 @@ TDNFRepoListFinalize(
uint32_t dwError = 0;
PTDNF_CMD_OPT pSetOpt = NULL;
PTDNF_REPO_DATA_INTERNAL pRepo = NULL;
int nRepoidSeen = 0;

if(!pTdnf || !pTdnf->pArgs || !pTdnf->pRepos)
{
Expand All @@ -472,8 +601,23 @@ TDNFRepoListFinalize(
pSetOpt->nType == CMDOPT_ENABLEREPO,
pSetOpt->pszOptValue);
BAIL_ON_TDNF_ERROR(dwError);
}
pSetOpt = pSetOpt->pNext;
}
else if(strcmp(pSetOpt->pszOptName, "repoid") == 0)
{
if (!nRepoidSeen)
{
dwError = TDNFAlterRepoState(
pTdnf->pRepos, 0, "*");
BAIL_ON_TDNF_ERROR(dwError);
nRepoidSeen = 1;
}
dwError = TDNFAlterRepoState(
pTdnf->pRepos,
1,
pSetOpt->pszOptValue);
BAIL_ON_TDNF_ERROR(dwError);
}
pSetOpt = pSetOpt->pNext;
}

//Now that the overrides are applied, replace config vars
Expand Down
3 changes: 3 additions & 0 deletions common/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

#pragma once

#define STRINGIFYX(N) STRINGIFY(N)
#define STRINGIFY(N) #N

#define MAX_CONFIG_LINE_LENGTH 1024

#define TDNF_SAFE_FREE_PKGINFO(pPkgInfo) \
Expand Down
20 changes: 20 additions & 0 deletions pytests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,26 @@ def _run(self, cmd, retvalonly=False, cwd=None):
ret['retval'] = retval
return ret

# helper to create directory tree without complains when it exists:
def makedirs(self, d):
try:
os.makedirs(d)
except OSError as e:
if e.errno != errno.EEXIST:
raise

def create_repoconf(self, filename, baseurl, name):
templ = """
[{name}]
name=Test Repo
baseurl={baseurl}
enabled=1
gpgcheck=0
metadata_expire=86400
ui_repoid_vars=basearch
"""
with open(filename, "w") as f:
f.write(templ.format(name=name, baseurl=baseurl))

@pytest.fixture(scope='session')
def utils():
Expand Down
Loading

0 comments on commit 4626652

Please sign in to comment.