From ed74c87cecba66a08b43f8386a5a3a65021e65e9 Mon Sep 17 00:00:00 2001 From: Oliver Kurth Date: Thu, 23 Jun 2022 10:23:59 -0700 Subject: [PATCH 1/2] add clean options and refactor --- client/api.c | 81 +++++++++++++++------------------- client/repoutils.c | 2 +- common/utils.c | 12 ----- include/tdnf.h | 8 +--- include/tdnfclitypes.h | 3 +- include/tdnftypes.h | 12 +---- pytests/tests/test_clean.py | 8 ++-- tools/cli/lib/api.c | 24 +--------- tools/cli/lib/parsecleanargs.c | 1 + tools/cli/main.c | 5 +-- tools/cli/prototypes.h | 3 +- 11 files changed, 50 insertions(+), 109 deletions(-) diff --git a/client/api.c b/client/api.c index 6b0ae7da..6be7b4ad 100644 --- a/client/api.c +++ b/client/api.c @@ -410,76 +410,67 @@ TDNFCheckUpdates( uint32_t TDNFClean( PTDNF pTdnf, - TDNF_CLEANTYPE nCleanType, - PTDNF_CLEAN_INFO* ppCleanInfo + TDNF_CLEANTYPE nCleanType ) { uint32_t dwError = 0; - PTDNF_CLEAN_INFO pCleanInfo = NULL; - char** ppszReposUsed = NULL; + PTDNF_REPO_DATA pRepo = NULL; - if(!pTdnf || !ppCleanInfo) + if(!pTdnf) { dwError = ERROR_TDNF_INVALID_PARAMETER; BAIL_ON_TDNF_ERROR(dwError); } - //Rest of the clean options will be - //supported in the next release - if(nCleanType != CLEANTYPE_ALL) + if(nCleanType == CLEANTYPE_PLUGINS) { dwError = ERROR_TDNF_CLEAN_UNSUPPORTED; BAIL_ON_TDNF_ERROR(dwError); } - dwError = TDNFAllocateMemory( - 1, - sizeof(TDNF_CLEAN_INFO), - (void**)&pCleanInfo); - BAIL_ON_TDNF_ERROR(dwError); - - dwError = TDNFCopyEnabledRepos(pTdnf->pRepos, &pCleanInfo->ppszReposUsed); - BAIL_ON_TDNF_ERROR(dwError); - - ppszReposUsed = pCleanInfo->ppszReposUsed; - if(nCleanType == CLEANTYPE_ALL) + for (pRepo = pTdnf->pRepos; pRepo; pRepo = pRepo->pNext) { - while(*ppszReposUsed) + if (strcmp(pRepo->pszId, CMDLINE_REPO_NAME) == 0) { - dwError = TDNFRepoRemoveCache(pTdnf,*ppszReposUsed); + continue; + } + pr_info("cleaning %s:", pRepo->pszId); + if (nCleanType == CLEANTYPE_METADATA || nCleanType == CLEANTYPE_ALL) + { + pr_info(" metadata"); + dwError = TDNFRepoRemoveCache(pTdnf, pRepo->pszId); BAIL_ON_TDNF_ERROR(dwError); - - dwError = TDNFRemoveSolvCache(pTdnf, *ppszReposUsed); + } + if (nCleanType == CLEANTYPE_DBCACHE || nCleanType == CLEANTYPE_ALL) + { + pr_info(" dbcache"); + dwError = TDNFRemoveSolvCache(pTdnf, pRepo->pszId); BAIL_ON_TDNF_ERROR(dwError); - - if (!pTdnf->pConf->nKeepCache) - { - dwError = TDNFRemoveRpmCache(pTdnf, *ppszReposUsed); - BAIL_ON_TDNF_ERROR(dwError); - } - - dwError = TDNFRemoveKeysCache(pTdnf, *ppszReposUsed); + } + if (nCleanType == CLEANTYPE_PACKAGES || nCleanType == CLEANTYPE_ALL) + { + pr_info(" packages"); + dwError = TDNFRemoveRpmCache(pTdnf, pRepo->pszId); BAIL_ON_TDNF_ERROR(dwError); - - ++ppszReposUsed; } + if (nCleanType == CLEANTYPE_KEYS || nCleanType == CLEANTYPE_ALL) + { + pr_info(" keys"); + dwError = TDNFRemoveKeysCache(pTdnf, pRepo->pszId); + BAIL_ON_TDNF_ERROR(dwError); + } + if (nCleanType == CLEANTYPE_EXPIRE_CACHE || nCleanType == CLEANTYPE_ALL) + { + pr_info(" expire-cache"); + dwError = TDNFRemoveLastRefreshMarker(pTdnf, pRepo->pszId); + BAIL_ON_TDNF_ERROR(dwError); + } + pr_info("\n"); } - - pCleanInfo->nCleanAll = (nCleanType == CLEANTYPE_ALL); - - *ppCleanInfo = pCleanInfo; cleanup: return dwError; error: - if(ppCleanInfo) - { - *ppCleanInfo = NULL; - } - if(pCleanInfo) - { - TDNFFreeCleanInfo(pCleanInfo); - } goto cleanup; } diff --git a/client/repoutils.c b/client/repoutils.c index 5171c931..99c6062e 100644 --- a/client/repoutils.c +++ b/client/repoutils.c @@ -351,7 +351,7 @@ TDNFRemoveLastRefreshMarker( BAIL_ON_TDNF_ERROR(dwError); if (pszLastRefreshMarker) { - if(unlink(pszLastRefreshMarker)) + if(unlink(pszLastRefreshMarker) && errno != ENOENT) { dwError = errno; BAIL_ON_TDNF_SYSTEM_ERROR(dwError); diff --git a/common/utils.c b/common/utils.c index 18ed5ab8..525d428d 100644 --- a/common/utils.c +++ b/common/utils.c @@ -408,18 +408,6 @@ TDNFFreeRepos( } } -void -TDNFFreeCleanInfo( - PTDNF_CLEAN_INFO pCleanInfo - ) -{ - if(pCleanInfo) - { - TDNF_SAFE_FREE_STRINGARRAY(pCleanInfo->ppszReposUsed); - TDNFFreeMemory(pCleanInfo); - } -} - uint32_t TDNFYesOrNo( PTDNF_CMD_ARGS pArgs, diff --git a/include/tdnf.h b/include/tdnf.h index 6c7e7990..b06c8398 100644 --- a/include/tdnf.h +++ b/include/tdnf.h @@ -51,8 +51,7 @@ TDNFCheckUpdates( uint32_t TDNFClean( PTDNF pTdnf, - TDNF_CLEANTYPE nCleanType, - PTDNF_CLEAN_INFO* ppCleanInfo + TDNF_CLEANTYPE nCleanType ); //show list of packages filtered by scope, name @@ -206,11 +205,6 @@ TDNFCloseHandle( PTDNF pTdnf ); -void -TDNFFreeCleanInfo( - PTDNF_CLEAN_INFO pCleanInfo - ); - void TDNFFreeCmdArgs( PTDNF_CMD_ARGS pCmdArgs diff --git a/include/tdnfclitypes.h b/include/tdnfclitypes.h index a0a340ff..8190d667 100644 --- a/include/tdnfclitypes.h +++ b/include/tdnfclitypes.h @@ -69,8 +69,7 @@ typedef uint32_t typedef uint32_t (*PFN_TDNF_CLEAN)( PTDNF_CLI_CONTEXT, - TDNF_CLEANTYPE, - PTDNF_CLEAN_INFO * + TDNF_CLEANTYPE ); typedef uint32_t diff --git a/include/tdnftypes.h b/include/tdnftypes.h index 9400fdaf..aeb64fdf 100644 --- a/include/tdnftypes.h +++ b/include/tdnftypes.h @@ -117,7 +117,7 @@ typedef enum CLEANTYPE_DBCACHE, CLEANTYPE_PLUGINS, CLEANTYPE_EXPIRE_CACHE, - CLEANTYPE_RPMDB, + CLEANTYPE_KEYS, CLEANTYPE_ALL }TDNF_CLEANTYPE; @@ -282,16 +282,6 @@ typedef struct _TDNF_REPO_DATA struct _TDNF_REPO_DATA* pNext; }TDNF_REPO_DATA, *PTDNF_REPO_DATA; -typedef struct _TDNF_CLEAN_INFO -{ - int nCleanAll; - char** ppszReposUsed; - int nRpmDbFilesRemoved; - int nMetadataFilesRemoved; - int nDbCacheFilesRemoved; - int nPackageFilesRemoved; -}TDNF_CLEAN_INFO, *PTDNF_CLEAN_INFO; - typedef struct _TDNF_ERROR_DESC { int nCode; diff --git a/pytests/tests/test_clean.py b/pytests/tests/test_clean.py index 31411478..33fe7302 100644 --- a/pytests/tests/test_clean.py +++ b/pytests/tests/test_clean.py @@ -31,22 +31,22 @@ def test_clean_invalid_arg(utils): def test_clean_packages(utils): ret = utils.run(['tdnf', 'clean', 'packages']) - assert(ret['retval'] == 1016) + assert(ret['retval'] == 0) def test_clean_dbcache(utils): ret = utils.run(['tdnf', 'clean', 'dbcache']) - assert(ret['retval'] == 1016) + assert(ret['retval'] == 0) def test_clean_metadata(utils): ret = utils.run(['tdnf', 'clean', 'metadata']) - assert(ret['retval'] == 1016) + assert(ret['retval'] == 0) def test_clean_expire_cache(utils): ret = utils.run(['tdnf', 'clean', 'expire-cache']) - assert(ret['retval'] == 1016) + assert(ret['retval'] == 0) def test_clean_plugins(utils): diff --git a/tools/cli/lib/api.c b/tools/cli/lib/api.c index c1a0637e..1fca71ca 100644 --- a/tools/cli/lib/api.c +++ b/tools/cli/lib/api.c @@ -56,8 +56,6 @@ TDNFCliCleanCommand( { uint32_t dwError = 0; TDNF_CLEANTYPE nCleanType = CLEANTYPE_NONE; - PTDNF_CLEAN_INFO pTDNFCleanInfo = NULL; - char** ppszReposUsed = NULL; if(!pContext || !pContext->hTdnf || !pContext->pFnClean) { @@ -68,30 +66,12 @@ TDNFCliCleanCommand( dwError = TDNFCliParseCleanArgs(pCmdArgs, &nCleanType); BAIL_ON_CLI_ERROR(dwError); - dwError = pContext->pFnClean(pContext, nCleanType, &pTDNFCleanInfo); + dwError = pContext->pFnClean(pContext, nCleanType); BAIL_ON_CLI_ERROR(dwError); - //Print clean info - pr_info("Cleaning repos:"); - ppszReposUsed = pTDNFCleanInfo->ppszReposUsed; - while(*ppszReposUsed) - { - pr_info(" %s", *ppszReposUsed); - ++ppszReposUsed; - } - - pr_info("\n"); - - if(pTDNFCleanInfo->nCleanAll) - { - pr_info("Cleaning up everything\n"); - } + pr_info("Done.\n"); cleanup: - if(pTDNFCleanInfo) - { - TDNFFreeCleanInfo(pTDNFCleanInfo); - } return dwError; error: diff --git a/tools/cli/lib/parsecleanargs.c b/tools/cli/lib/parsecleanargs.c index fc1587d4..d0cc9323 100644 --- a/tools/cli/lib/parsecleanargs.c +++ b/tools/cli/lib/parsecleanargs.c @@ -81,6 +81,7 @@ TDNFCliParseCleanType( {"metadata", CLEANTYPE_METADATA}, {"dbcache", CLEANTYPE_DBCACHE}, {"plugins", CLEANTYPE_PLUGINS}, + {"keys", CLEANTYPE_KEYS}, {"expire-cache", CLEANTYPE_EXPIRE_CACHE}, {"all", CLEANTYPE_ALL}, }; diff --git a/tools/cli/main.c b/tools/cli/main.c index 20fc4106..89d2bc73 100644 --- a/tools/cli/main.c +++ b/tools/cli/main.c @@ -337,11 +337,10 @@ TDNFCliInvokeCheckUpdate( uint32_t TDNFCliInvokeClean( PTDNF_CLI_CONTEXT pContext, - TDNF_CLEANTYPE nCleanType, - PTDNF_CLEAN_INFO *ppTDNFCleanInfo + TDNF_CLEANTYPE nCleanType ) { - return TDNFClean(pContext->hTdnf, nCleanType, ppTDNFCleanInfo); + return TDNFClean(pContext->hTdnf, nCleanType); } uint32_t diff --git a/tools/cli/prototypes.h b/tools/cli/prototypes.h index be64fca8..801f42f6 100644 --- a/tools/cli/prototypes.h +++ b/tools/cli/prototypes.h @@ -51,8 +51,7 @@ TDNFCliInvokeCheckUpdate( uint32_t TDNFCliInvokeClean( PTDNF_CLI_CONTEXT pContext, - TDNF_CLEANTYPE nCleanType, - PTDNF_CLEAN_INFO *ppTDNFCleanInfo + TDNF_CLEANTYPE nCleanType ); uint32_t From 15add0e9c9c973208b0c9cc8cce5b2837c63f592 Mon Sep 17 00:00:00 2001 From: Oliver Kurth Date: Fri, 24 Jun 2022 10:57:49 -0700 Subject: [PATCH 2/2] use bitmask for clean types --- client/api.c | 12 ++++++------ include/tdnf.h | 2 +- include/tdnfcli.h | 4 ++-- include/tdnfclitypes.h | 2 +- include/tdnftypes.h | 21 +++++++++------------ tools/cli/lib/api.c | 2 +- tools/cli/lib/parsecleanargs.c | 8 ++++---- tools/cli/main.c | 2 +- tools/cli/prototypes.h | 6 +++--- 9 files changed, 28 insertions(+), 31 deletions(-) diff --git a/client/api.c b/client/api.c index 6be7b4ad..0e4ef75f 100644 --- a/client/api.c +++ b/client/api.c @@ -410,7 +410,7 @@ TDNFCheckUpdates( uint32_t TDNFClean( PTDNF pTdnf, - TDNF_CLEANTYPE nCleanType + uint32_t nCleanType ) { uint32_t dwError = 0; @@ -435,31 +435,31 @@ TDNFClean( continue; } pr_info("cleaning %s:", pRepo->pszId); - if (nCleanType == CLEANTYPE_METADATA || nCleanType == CLEANTYPE_ALL) + if (nCleanType & CLEANTYPE_METADATA) { pr_info(" metadata"); dwError = TDNFRepoRemoveCache(pTdnf, pRepo->pszId); BAIL_ON_TDNF_ERROR(dwError); } - if (nCleanType == CLEANTYPE_DBCACHE || nCleanType == CLEANTYPE_ALL) + if (nCleanType & CLEANTYPE_DBCACHE) { pr_info(" dbcache"); dwError = TDNFRemoveSolvCache(pTdnf, pRepo->pszId); BAIL_ON_TDNF_ERROR(dwError); } - if (nCleanType == CLEANTYPE_PACKAGES || nCleanType == CLEANTYPE_ALL) + if (nCleanType & CLEANTYPE_PACKAGES) { pr_info(" packages"); dwError = TDNFRemoveRpmCache(pTdnf, pRepo->pszId); BAIL_ON_TDNF_ERROR(dwError); } - if (nCleanType == CLEANTYPE_KEYS || nCleanType == CLEANTYPE_ALL) + if (nCleanType & CLEANTYPE_KEYS) { pr_info(" keys"); dwError = TDNFRemoveKeysCache(pTdnf, pRepo->pszId); BAIL_ON_TDNF_ERROR(dwError); } - if (nCleanType == CLEANTYPE_EXPIRE_CACHE || nCleanType == CLEANTYPE_ALL) + if (nCleanType & CLEANTYPE_EXPIRE_CACHE) { pr_info(" expire-cache"); dwError = TDNFRemoveLastRefreshMarker(pTdnf, pRepo->pszId); diff --git a/include/tdnf.h b/include/tdnf.h index b06c8398..80a0e309 100644 --- a/include/tdnf.h +++ b/include/tdnf.h @@ -51,7 +51,7 @@ TDNFCheckUpdates( uint32_t TDNFClean( PTDNF pTdnf, - TDNF_CLEANTYPE nCleanType + uint32_t nCleanType ); //show list of packages filtered by scope, name diff --git a/include/tdnfcli.h b/include/tdnfcli.h index e4be1d50..e2b412f4 100644 --- a/include/tdnfcli.h +++ b/include/tdnfcli.h @@ -51,13 +51,13 @@ TDNFCliFreeListArgs( uint32_t TDNFCliParseCleanType( const char* pszCleanType, - TDNF_CLEANTYPE* pnCleanType + uint32_t* pnCleanType ); uint32_t TDNFCliParseCleanArgs( PTDNF_CMD_ARGS pCmdArgs, - TDNF_CLEANTYPE* pnCleanType + uint32_t* pnCleanType ); uint32_t diff --git a/include/tdnfclitypes.h b/include/tdnfclitypes.h index 8190d667..79cbbb62 100644 --- a/include/tdnfclitypes.h +++ b/include/tdnfclitypes.h @@ -69,7 +69,7 @@ typedef uint32_t typedef uint32_t (*PFN_TDNF_CLEAN)( PTDNF_CLI_CONTEXT, - TDNF_CLEANTYPE + uint32_t ); typedef uint32_t diff --git a/include/tdnftypes.h b/include/tdnftypes.h index aeb64fdf..f794f626 100644 --- a/include/tdnftypes.h +++ b/include/tdnftypes.h @@ -108,18 +108,15 @@ typedef enum UPDATE_ENHANCEMENT }TDNF_UPDATEINFO_TYPE; -//Clean command type -typedef enum -{ - CLEANTYPE_NONE = -1, - CLEANTYPE_PACKAGES, - CLEANTYPE_METADATA, - CLEANTYPE_DBCACHE, - CLEANTYPE_PLUGINS, - CLEANTYPE_EXPIRE_CACHE, - CLEANTYPE_KEYS, - CLEANTYPE_ALL -}TDNF_CLEANTYPE; +#define CLEANTYPE_NONE 0x00 +#define CLEANTYPE_PACKAGES 0x01 +#define CLEANTYPE_METADATA 0x02 +#define CLEANTYPE_DBCACHE 0x04 +#define CLEANTYPE_PLUGINS 0x08 +#define CLEANTYPE_EXPIRE_CACHE 0x10 +#define CLEANTYPE_KEYS 0x20 +#define CLEANTYPE_ALL 0xff + //RepoList command filter typedef enum diff --git a/tools/cli/lib/api.c b/tools/cli/lib/api.c index 1fca71ca..777e0d5e 100644 --- a/tools/cli/lib/api.c +++ b/tools/cli/lib/api.c @@ -55,7 +55,7 @@ TDNFCliCleanCommand( ) { uint32_t dwError = 0; - TDNF_CLEANTYPE nCleanType = CLEANTYPE_NONE; + uint32_t nCleanType = CLEANTYPE_NONE; if(!pContext || !pContext->hTdnf || !pContext->pFnClean) { diff --git a/tools/cli/lib/parsecleanargs.c b/tools/cli/lib/parsecleanargs.c index d0cc9323..da428c9d 100644 --- a/tools/cli/lib/parsecleanargs.c +++ b/tools/cli/lib/parsecleanargs.c @@ -24,11 +24,11 @@ uint32_t TDNFCliParseCleanArgs( PTDNF_CMD_ARGS pCmdArgs, - TDNF_CLEANTYPE* pnCleanType + uint32_t* pnCleanType ) { uint32_t dwError = 0; - TDNF_CLEANTYPE nCleanType = CLEANTYPE_NONE; + uint32_t nCleanType = CLEANTYPE_NONE; if(!pCmdArgs) { @@ -64,12 +64,12 @@ TDNFCliParseCleanArgs( uint32_t TDNFCliParseCleanType( const char* pszCleanType, - TDNF_CLEANTYPE* pnCleanType + uint32_t* pnCleanType ) { uint32_t dwError = 0; int nIndex = 0; - TDNF_CLEANTYPE nCleanType = CLEANTYPE_ALL; + uint32_t nCleanType = CLEANTYPE_ALL; struct stTemp { char* pszTypeName; diff --git a/tools/cli/main.c b/tools/cli/main.c index 89d2bc73..dc2f9caa 100644 --- a/tools/cli/main.c +++ b/tools/cli/main.c @@ -337,7 +337,7 @@ TDNFCliInvokeCheckUpdate( uint32_t TDNFCliInvokeClean( PTDNF_CLI_CONTEXT pContext, - TDNF_CLEANTYPE nCleanType + uint32_t nCleanType ) { return TDNFClean(pContext->hTdnf, nCleanType); diff --git a/tools/cli/prototypes.h b/tools/cli/prototypes.h index 801f42f6..9263213f 100644 --- a/tools/cli/prototypes.h +++ b/tools/cli/prototypes.h @@ -51,7 +51,7 @@ TDNFCliInvokeCheckUpdate( uint32_t TDNFCliInvokeClean( PTDNF_CLI_CONTEXT pContext, - TDNF_CLEANTYPE nCleanType + uint32_t nCleanType ); uint32_t @@ -284,13 +284,13 @@ TDNFCliParseArgs( uint32_t ParseCleanType( const char* pszCleanType, - TDNF_CLEANTYPE* pnCleanType + uint32_t* pnCleanType ); uint32_t TDNFCliParseCleanArgs( PTDNF_CMD_ARGS pCmdArgs, - TDNF_CLEANTYPE* pnCleanType + uint32_t* pnCleanType ); //parselistargs.c