diff --git a/internal/dnfjson/cache.go b/internal/dnfjson/cache.go index 4deca44584..f0527aa5d5 100644 --- a/internal/dnfjson/cache.go +++ b/internal/dnfjson/cache.go @@ -114,6 +114,10 @@ func newRPMCache(path string, maxSize uint64) *rpmCache { // later use of the cache. eg. touchRepo func (r *rpmCache) updateInfo() { dirs, _ := os.ReadDir(r.root) + // reset to make sure accumulation works properly + r.size = 0 + r.repoElements = make(map[string]pathInfo) + r.repoRecency = nil for _, d := range dirs { r.updateCacheDirInfo(filepath.Join(r.root, d.Name())) } @@ -186,9 +190,12 @@ func (r *rpmCache) updateCacheDirInfo(path string) { // sort IDs by mtime (oldest first) sort.Slice(repoIDs, sortFunc) - r.size = totalSize - r.repoElements = repos - r.repoRecency = repoIDs + r.size += totalSize + // TODO: replace loop with maps.Copy() introduced in Go 1.21 + for k, v := range repos { + r.repoElements[k] = v + } + copy(r.repoRecency, repoIDs) } func (r *rpmCache) shrink() error { diff --git a/internal/dnfjson/cache_test.go b/internal/dnfjson/cache_test.go index 7cbe2558c5..29aafde8a9 100644 --- a/internal/dnfjson/cache_test.go +++ b/internal/dnfjson/cache_test.go @@ -172,6 +172,24 @@ func TestCacheRead(t *testing.T) { } } +func TestMultiDirCacheRead(t *testing.T) { + assert := assert.New(t) + t.Run("MultiDir", func(t *testing.T) { + testCacheRoot := t.TempDir() + // Cache is now per-distro, use the name of the config as a distro name + size1 := createTestCache(filepath.Join(testCacheRoot, "rhel84-aarch64"), testCfgs["rhel84-aarch64"]) + size2 := createTestCache(filepath.Join(testCacheRoot, "fake-real"), testCfgs["fake-real"]) + + // Cache covers all distros, pass in top directory + cache := newRPMCache(testCacheRoot, 1048576) // 1 MiB, but doesn't matter for this test + + nrepos := len(getRepoIDs(testCfgs["rhel84-aarch64"])) + len(getRepoIDs(testCfgs["fake-real"])) + assert.Equal(size1+size2, cache.size) + assert.Equal(nrepos, len(cache.repoElements)) + assert.Equal(nrepos, len(cache.repoRecency)) + }) +} + func sizeSum(cfg testCache, repoIDFilter ...string) uint64 { var sum uint64 for path, info := range cfg {