Skip to content

Commit

Permalink
tdnf: Add check to identify duplicate repo id
Browse files Browse the repository at this point in the history
  • Loading branch information
shivania2 committed Sep 27, 2023
1 parent ed235f7 commit 5df0ea9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions client/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ typedef enum
{ERROR_TDNF_INVALID_INPUT, "ERROR_TDNF_INVALID_INPUT", "Invalid input."},\
{ERROR_TDNF_CACHE_DISABLED, "ERROR_TDNF_CACHE_DISABLED", "cache only is set, but no repo data found"},\
{ERROR_TDNF_CACHE_DIR_OUT_OF_DISK_SPACE, "ERROR_TDNF_CACHE_DIR_OUT_OF_DISK_SPACE", "Insufficient disk space at cache directory /var/cache/tdnf (unless specified differently in config). Try freeing space first."},\
{ERROR_TDNF_DUPLICATE_REPO_ID, "ERROR_TDNF_DUPLICATE_REPO_ID", "Duplicate repo id"}, \
{ERROR_TDNF_EVENT_CTXT_ITEM_NOT_FOUND, "ERROR_TDNF_EVENT_CTXT_ITEM_NOT_FOUND", "An event context item was not found. This is usually related to plugin events. Try --noplugins to deactivate all plugins or --disableplugin=<plugin> to deactivate a specific one. You can permanently deactivate an offending plugin by setting enable=0 in the plugin config file."},\
{ERROR_TDNF_EVENT_CTXT_ITEM_INVALID_TYPE, "ERROR_TDNF_EVENT_CTXT_ITEM_INVALID_TYPE", "An event item type had a mismatch. This is usually related to plugin events. Try --noplugins to deactivate all plugins or --disableplugin=<plugin> to deactivate a specific one. You can permanently deactivate an offending plugin by setting enable=0 in the plugin config file."},\
{ERROR_TDNF_NO_GPGKEY_CONF_ENTRY, "ERROR_TDNF_NO_GPGKEY_CONF_ENTRY", "gpgkey entry is missing for this repo. please add gpgkey in repo file or use --nogpgcheck to ignore."}, \
Expand Down
31 changes: 30 additions & 1 deletion client/repolist.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
#include "../llconf/entry.h"
#include "../llconf/ini.h"

static uint32_t hashString(const char* str) {
uint32_t hash = 0;
while (*str) {
hash = (hash * 31) + (*str);
str++;
}
return hash;
}

uint32_t
TDNFLoadRepoData(
PTDNF pTdnf,
Expand All @@ -28,6 +37,9 @@ TDNFLoadRepoData(
DIR *pDir = NULL;
struct dirent *pEnt = NULL;
char **ppszUrlIdTuple = NULL;
int nRepoId = 10000;
char **ppRepoId = NULL;
PTDNF_REPO_DATA pRepoParse = NULL;

if(!pTdnf || !pTdnf->pConf || !pTdnf->pArgs || !ppReposAll)
{
Expand Down Expand Up @@ -115,18 +127,35 @@ TDNFLoadRepoData(

TDNF_SAFE_FREE_MEMORY(pszRepoFilePath);
pszRepoFilePath = NULL;

/* may have added multiple repos, go to last one */
while (*ppRepoNext)
ppRepoNext = &((*ppRepoNext)->pNext);
}

dwError = TDNFAllocateMemory(nRepoId + 1, sizeof(char *), (void **)&ppRepoId);
BAIL_ON_TDNF_ERROR(dwError);

pRepoParse = pReposAll;

while (pRepoParse != NULL) {
uint32_t hash = hashString(pRepoParse->pszId) % nRepoId;
if (ppRepoId[hash] != NULL && strcmp(ppRepoId[hash], pRepoParse->pszId) == 0) {
dwError = ERROR_TDNF_DUPLICATE_REPO_ID;
BAIL_ON_TDNF_ERROR(dwError);
} else {
dwError = TDNFAllocateString(pRepoParse->pszId, &ppRepoId[hash]);
BAIL_ON_TDNF_ERROR(dwError);
}
pRepoParse = pRepoParse->pNext;
}

*ppReposAll = pReposAll;
cleanup:
if(pDir)
{
closedir(pDir);
}
TDNF_SAFE_FREE_STRINGARRAY(ppRepoId);
TDNF_SAFE_FREE_MEMORY(pszRepoFilePath);
TDNF_SAFE_FREE_STRINGARRAY(ppszUrlIdTuple);

Expand Down
2 changes: 2 additions & 0 deletions include/tdnferror.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ extern "C" {
#define ERROR_TDNF_DOWNGRADE_NOT_ALLOWED 1035
// cache directory out of memory
#define ERROR_TDNF_CACHE_DIR_OUT_OF_DISK_SPACE 1036
// There are duplicate repo id
#define ERROR_TDNF_DUPLICATE_REPO_ID 1037

//curl errors
#define ERROR_TDNF_CURL_INIT 1200
Expand Down
2 changes: 1 addition & 1 deletion pytests/tests/test_baseurls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def teardown_test(utils):


def test_multiple_baseurls(utils):
reponame = TESTREPO
reponame = REPONAME
workdir = WORKDIR
utils.makedirs(workdir)

Expand Down
21 changes: 21 additions & 0 deletions pytests/tests/test_repolist.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def setup_test(utils):
def teardown_test(utils):
os.remove(os.path.join(utils.config['repo_path'], 'yum.repos.d', 'foo.repo'))
os.remove(os.path.join(utils.config['repo_path'], 'yum.repos.d', 'bar.repo'))
os.remove(os.path.join(utils.config['repo_path'], "yum.repos.d", 'bar1.repo'))


def find_repo(repolist, id):
Expand Down Expand Up @@ -126,3 +127,23 @@ def test_repolist_invalid(utils):
def test_repolist_memcheck(utils):
ret = utils.run_memcheck(['tdnf', 'repolist'])
assert ret['retval'] == 0


# multiple repoid
def test_multiple_repoid(utils):
reponame = 'bar1.repo'
repofile_bar1 = os.path.join(utils.config['repo_path'], 'yum.repos.d', reponame)
utils.edit_config(
{
'name': 'Bar Repo',
'enabled': '1',
'baseurl': 'http://pkgs.bar1.org/bar1'
},
section='bar',
filename=repofile_bar1
)

ret = utils.run(['tdnf',
'--disablerepo=*', '--enablerepo={}'.format(reponame),
'makecache'])
assert ret['retval'] == 1037

0 comments on commit 5df0ea9

Please sign in to comment.