Skip to content

Commit

Permalink
cache: Don't use checksum as filename
Browse files Browse the repository at this point in the history
In some cases (e.g in Pungi), the metadata is changing all the time, fus
is run multiple times a day and the cache just grows. So instead of
using checksum we use the reponame passed in the command line invocation
and the metadata type to create a filename so that only one copy exists
for that reponame. Therefore the cache layout now is:

$CACHEDIR/fus/$reponame/$chksum.solv
$CACHEDIR/fus/$reponame/$chksum.solvx
$CACHEDIR/fus/$reponame/repodata/repomd.xml
$CACHEDIR/fus/$reponame/repodata/primary.xml.gz
$CACHEDIR/fus/$reponame/repodata/modules.xml.gz
$CACHEDIR/fus/$reponame/repodata/group_gz.x86_64.xml.xz
$CACHEDIR/fus/$reponame/repodata/filelists.xml.gz

Fixes #71

Signed-off-by: Rafael Fonseca <r4f4rfs@gmail.com>
  • Loading branch information
r4f4 committed Aug 14, 2019
1 parent 9cb602f commit 16bb360
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions repo.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,22 +510,25 @@ download_repo_metadata (SoupSession *session,
const char *cachedir)
{
Id chksumtype;
const char *fpath, *fname;
const unsigned char *chksum;

fname = repomd_find (repo, type, &chksum, &chksumtype);
if (!fname)
const char *mdname = repomd_find (repo, type, &chksum, &chksumtype);
if (!mdname)
return NULL;

fpath = pool_tmpjoin (repo->pool, cachedir, "/", fname);
/* mdname should be "repodata/$(chksum)-$(type).xml.gz" */
const char *ext = strchr (mdname, '.');
const char *fpath = pool_tmpjoin (repo->pool, cachedir, "/", type);
fpath = pool_tmpappend (repo->pool, fpath, ext, 0);

if (!g_file_test (fpath, G_FILE_TEST_IS_REGULAR) ||
!checksum_matches (fpath, chksum, chksumtype))
{
g_autoptr(GError) error = NULL;
const char *furl = pool_tmpjoin (repo->pool, repo_url, "/", fname);
if (!download_to_path (session, furl, fpath, &error))
const char *mdurl = pool_tmpjoin (repo->pool, repo_url, "/", mdname);
if (!download_to_path (session, mdurl, fpath, &error))
{
g_warning ("Could not download %s: %s", furl, error->message);
g_warning ("Could not download %s: %s", mdurl, error->message);
return NULL;
}
}
Expand Down Expand Up @@ -643,6 +646,25 @@ get_repo_cachedir (const char *name)
return g_build_filename (g_get_user_cache_dir (), "fus", name, NULL);
}

static void
remove_files_by_ext (const char *dirpath,
const char *ext)
{
g_autoptr(GDir) dir = g_dir_open (dirpath, 0, NULL);
if (!dir)
return;

const gchar *fname;
while ((fname = g_dir_read_name (dir)) != NULL)
{
if (g_str_has_suffix (fname, ext))
{
g_autofree gchar *path = g_build_filename (dirpath, fname, NULL);
g_unlink (path);
}
}
}

int
filelist_loadcb (Pool *pool,
Repodata *data,
Expand All @@ -668,11 +690,15 @@ filelist_loadcb (Pool *pool,
return 1;
}

/* Cleanup old libsolv cache files (if any) */
remove_files_by_ext (cachedir, ".solvx");

path = repodata_lookup_str (data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
if (!path)
return 0;

fname = download_repo_metadata (session, repo, type, path, cachedir);
const char *destdir = pool_tmpjoin (pool, cachedir, "/", "repodata");
fname = download_repo_metadata (session, repo, type, path, destdir);
fp = solv_xfopen (fname, 0);
if (!fp)
{
Expand Down Expand Up @@ -747,20 +773,23 @@ create_repo (Pool *pool,
return repo;
}

/* Cleanup old libsolv cache files (if any) */
remove_files_by_ext (cachedir, ".solv");

repo_add_repomdxml (repo, fp, 0);
fclose (fp);

fname = download_repo_metadata (session, repo, "primary", path, cachedir);
fname = download_repo_metadata (session, repo, "primary", path, destdir);
fp = solv_xfopen (fname, "r");
if (fp != NULL)
{
repo_add_rpmmd (repo, fp, NULL, 0);
fclose (fp);
}

fname = download_repo_metadata (session, repo, "group_gz", path, cachedir);
fname = download_repo_metadata (session, repo, "group_gz", path, destdir);
if (!fname)
fname = download_repo_metadata (session, repo, "group", path, cachedir);
fname = download_repo_metadata (session, repo, "group", path, destdir);
fp = solv_xfopen (fname, "r");
if (fp != NULL)
{
Expand All @@ -786,7 +815,7 @@ create_repo (Pool *pool,

pool_createwhatprovides (pool);

fname = download_repo_metadata (session, repo, "modules", path, cachedir);
fname = download_repo_metadata (session, repo, "modules", path, destdir);
fp = solv_xfopen (fname, "r");
if (fp != NULL)
{
Expand Down

0 comments on commit 16bb360

Please sign in to comment.