Skip to content

Commit

Permalink
engage metadata_expire
Browse files Browse the repository at this point in the history
Change-Id: I87a52796f5639d758a60b4707769cc71a06ac362
  • Loading branch information
Priyesh Padmavilasom committed Nov 4, 2016
1 parent ef4f547 commit c675a4d
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 1 deletion.
7 changes: 7 additions & 0 deletions client/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ typedef enum
#define TDNF_REPO_KEY_GPGKEY "gpgkey"
#define TDNF_REPO_KEY_USERNAME "username"
#define TDNF_REPO_KEY_PASSWORD "password"
#define TDNF_REPO_KEY_METADATA_EXPIRE "metadata_expire"

//file names
#define TDNF_REPO_METADATA_MARKER "lastrefresh"

//Repo defaults
#define TDNF_DEFAULT_REPO_LOCATION "/etc/yum.repos.d"
Expand All @@ -136,6 +140,8 @@ typedef enum
#define TDNF_DEFAULT_DISTROARCHPKG "x86_64"
#define TDNF_RPM_CACHE_DIR_NAME "rpms"
#define TDNF_REPODATA_DIR_NAME "repodata"
#define TDNF_REPO_DEFAULT_METADATA_EXPIRE "8294400"//48 hours in seconds
#define TDNF_REPO_METADATA_EXPIRE_NEVER "never"
//var names
#define TDNF_VAR_RELEASEVER "$releasever"
#define TDNF_VAR_BASEARCH "$basearch"
Expand Down Expand Up @@ -192,4 +198,5 @@ typedef enum
{ERROR_TDNF_RPM_GPG_NO_MATCH, "ERROR_TDNF_RPM_GPG_NO_MATCH", "RPM is signed but failed to match with known keys. Use --nogpgcheck to ignore."}, \
{ERROR_TDNF_AUTOERASE_UNSUPPORTED,"ERROR_TDNF_AUTOERASE_UNSUPPORTED","autoerase / autoremove is not supported."}, \
{ERROR_TDNF_RPM_CHECK, "ERROR_TDNF_RPM_CHECK", "rpm check reported errors"}, \
{ERROR_TDNF_METADATA_EXPIRE_PARSE, "ERROR_TDNF_METADATA_EXPIRE_PARSE", "metadata_expire value could not be parsed. Check your repo files."},\
};
1 change: 1 addition & 0 deletions client/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <utime.h>
#include <fnmatch.h>
//
#include <sys/utsname.h>
Expand Down
25 changes: 24 additions & 1 deletion client/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ TDNFRefreshSack(
uint32_t dwError = 0;
HyRepo hRepo = NULL;
int nYumFlags = HY_LOAD_FILELISTS | HY_LOAD_UPDATEINFO;
char* pszRepoCacheDir = NULL;

if(!pTdnf)
{
Expand All @@ -272,9 +273,30 @@ TDNFRefreshSack(
PTDNF_REPO_DATA pTempRepo = pTdnf->pRepos;
while(pTempRepo)
{
int nMetadataExpired = 0;
if(pTempRepo->nEnabled)
{
if(nCleanMetadata)
//Check if expired since last sync per metadata_expire
if(!nCleanMetadata && pTempRepo->lMetadataExpire >= 0)
{
dwError = TDNFAllocateStringPrintf(
&pszRepoCacheDir,
"%s/%s",
pTdnf->pConf->pszCacheDir,
pTempRepo->pszId);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFShouldSyncMetadata(
pszRepoCacheDir,
pTempRepo->lMetadataExpire,
&nMetadataExpired);
BAIL_ON_TDNF_ERROR(dwError);

TDNF_SAFE_FREE_MEMORY(pszRepoCacheDir);
pszRepoCacheDir = NULL;
}

if(nCleanMetadata || nMetadataExpired)
{
fprintf(stdout,
"Refreshing metadata for: '%s'\n",
Expand Down Expand Up @@ -318,6 +340,7 @@ TDNFRefreshSack(
}

cleanup:
TDNF_SAFE_FREE_MEMORY(pszRepoCacheDir);
return dwError;

error:
Expand Down
23 changes: 23 additions & 0 deletions client/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,11 @@ TDNFUtilsMakeDirs(
const char* pszPath
);

uint32_t
TDNFTouchFile(
const char* pszFile
);

uint32_t
TDNFRawGetPackageVersion(
const char* pszRootDir,
Expand All @@ -782,6 +787,24 @@ TDNFGetKernelArch(
char** ppszArch
);

uint32_t
TDNFUpdateMetadataMarkerFile(
const char* pszRepoDataFolder
);

uint32_t
TDNFParseMetadataExpire(
const char* pszMetadataExpire,
long* plMetadataExpire
);

uint32_t
TDNFShouldSyncMetadata(
const char* pszRepoDataFolder,
long lMetadataExpire,
int* pnShouldSync
);

//validate.c
uint32_t
TDNFValidateCmdArgs(
Expand Down
12 changes: 12 additions & 0 deletions client/repo.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ TDNFInitRepo(
char* pszRepoCacheDir = NULL;
char* pszRepoDataDir = NULL;
char* pszUserPass = NULL;
char* pszLastRefreshMarker = NULL;

char* ppszRepoUrls[] = {NULL, NULL};
char* ppszLocalUrls[] = {NULL, NULL};
Expand Down Expand Up @@ -173,8 +174,19 @@ TDNFInitRepo(
dwError = TDNFInitRepoFromMetaData(hRepo, pRepo);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFAllocateStringPrintf(
&pszLastRefreshMarker,
"%s/%s",
pszRepoCacheDir,
TDNF_REPO_METADATA_MARKER);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFTouchFile(pszLastRefreshMarker);
BAIL_ON_TDNF_ERROR(dwError);

*phRepo = hRepo;
cleanup:
TDNF_SAFE_FREE_MEMORY(pszLastRefreshMarker);
if(pszRepoDataDir)
{
g_free(pszRepoDataDir);
Expand Down
28 changes: 28 additions & 0 deletions client/repolist.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ TDNFLoadReposFromFile(
char** ppszRepos = NULL;
char* pszRepo = NULL;
char* pszValue = NULL;
char* pszMetadataExpire = NULL;

PTDNF_REPO_DATA pRepos = NULL;
PTDNF_REPO_DATA pRepo = NULL;
Expand Down Expand Up @@ -238,6 +239,32 @@ TDNFLoadReposFromFile(
&pRepo->pszPass);
BAIL_ON_TDNF_ERROR(dwError);

dwError = TDNFRepoGetKeyValue(
pKeyFile,
pszRepo,
TDNF_REPO_KEY_METADATA_EXPIRE,
NULL,
&pszMetadataExpire);
BAIL_ON_TDNF_ERROR(dwError);

//Set
if(IsNullOrEmptyString(pszMetadataExpire))
{
TDNF_SAFE_FREE_MEMORY(pszMetadataExpire);
dwError = TDNFAllocateString(
TDNF_REPO_DEFAULT_METADATA_EXPIRE,
&pszMetadataExpire);
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = TDNFParseMetadataExpire(
pszMetadataExpire,
&pRepo->lMetadataExpire);
BAIL_ON_TDNF_ERROR(dwError);

TDNF_SAFE_FREE_MEMORY(pszMetadataExpire);
pszMetadataExpire = NULL;

pRepo->pNext = pRepos;
pRepos = pRepo;
pRepo = NULL;
Expand All @@ -252,6 +279,7 @@ TDNFLoadReposFromFile(
}
g_free(pszValue);
g_strfreev(ppszRepos);
TDNF_SAFE_FREE_MEMORY(pszMetadataExpire);
return dwError;

error:
Expand Down
186 changes: 186 additions & 0 deletions client/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,64 @@ TDNFIsDir(
goto cleanup;
}

//update time if file exists
//create if not
uint32_t
TDNFTouchFile(
const char* pszFile
)
{
uint32_t dwError = 0;
struct stat st = {0};
int fd = -1;
struct utimbuf times = {0};

if(IsNullOrEmptyString(pszFile))
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

if(stat(pszFile, &st) == -1)
{
if(errno == ENOENT)
{
fd = creat(pszFile,
S_IRUSR | S_IRGRP | S_IROTH);
if(fd == -1)
{
dwError = errno;
BAIL_ON_TDNF_SYSTEM_ERROR(dwError);
}
else
{
close(fd);
}
}
else
{
dwError = errno;
BAIL_ON_TDNF_SYSTEM_ERROR(dwError);
}
}
else
{
times.actime = st.st_atime;
times.modtime = time(NULL);
if(utime(pszFile, &times))
{
dwError = errno;
BAIL_ON_TDNF_SYSTEM_ERROR(dwError);
}
}

cleanup:
return dwError;

error:
goto cleanup;
}

//get package version using rpmlib
uint32_t
TDNFRawGetPackageVersion(
Expand Down Expand Up @@ -435,3 +493,131 @@ TDNFGetKernelArch(
TDNF_SAFE_FREE_MEMORY(pszArch);
goto cleanup;
}

uint32_t
TDNFParseMetadataExpire(
const char* pszMetadataExpire,
long* plMetadataExpire
)
{
uint32_t dwError = 0;
long lMetadataExpire = -1;
char* pszError = NULL;

if(!lMetadataExpire || IsNullOrEmptyString(pszMetadataExpire))
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

if(!strcasecmp(TDNF_REPO_METADATA_EXPIRE_NEVER, pszMetadataExpire))
{
lMetadataExpire = -1;
}
else
{
lMetadataExpire = strtol(pszMetadataExpire, &pszError, 10);
if(lMetadataExpire < 0)
{
lMetadataExpire = -1;
}
else if(lMetadataExpire > 0)
{
char chMultiplier = 's';
int nMultiplier = 1;
if(pszError && *pszError)
{
chMultiplier = *pszError;
}
switch(chMultiplier)
{
case 's': nMultiplier = 1; break;
case 'm': nMultiplier = 60; break;
case 'h': nMultiplier = 60*60; break;
case 'd': nMultiplier = 60*60*24; break;
default:
dwError = ERROR_TDNF_METADATA_EXPIRE_PARSE;
BAIL_ON_TDNF_ERROR(dwError);
}
lMetadataExpire *= nMultiplier;
}
else if(pszError && *pszError)
{
dwError = ERROR_TDNF_METADATA_EXPIRE_PARSE;
BAIL_ON_TDNF_ERROR(dwError);
}
}

*plMetadataExpire = lMetadataExpire;

cleanup:
return dwError;

error:
if(plMetadataExpire)
{
*plMetadataExpire = 0;
}
goto cleanup;
}

uint32_t
TDNFShouldSyncMetadata(
const char* pszRepoDataFolder,
long lMetadataExpire,
int* pnShouldSync
)
{
uint32_t dwError = 0;
int nShouldSync = 0;
struct stat st = {0};
time_t tCurrent = time(NULL);
char* pszMarkerFile = NULL;

if(!pnShouldSync || IsNullOrEmptyString(pszRepoDataFolder))
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

dwError = TDNFAllocateStringPrintf(
&pszMarkerFile,
"%s/%s",
pszRepoDataFolder,
TDNF_REPO_METADATA_MARKER);
BAIL_ON_TDNF_ERROR(dwError);

//Look for the metadata marker file
if(stat(pszMarkerFile, &st) == -1)
{
if(errno == ENOENT)
{
nShouldSync = 1;
}
else
{
dwError = errno;
BAIL_ON_TDNF_SYSTEM_ERROR(dwError);
}
}
else
{
if(difftime(tCurrent, st.st_ctime) > lMetadataExpire)
{
nShouldSync = 1;
}
}

*pnShouldSync = nShouldSync;

cleanup:
TDNF_SAFE_FREE_MEMORY(pszMarkerFile);
return dwError;

error:
if(pnShouldSync)
{
*pnShouldSync = 0;
}
goto cleanup;
}
Loading

0 comments on commit c675a4d

Please sign in to comment.