dnfjson: cache information and methods as a substruct

Move cache handling data and code to a substruct of the BaseSolver.
This is all internal to the dnfjson package.

Paves the way for cache management with a persistent state.
This commit is contained in:
Achilleas Koutsou 2022-05-30 18:32:44 +02:00 committed by Tom Gundersen
parent af4b474e89
commit b8d16bc395
2 changed files with 31 additions and 15 deletions

View file

@ -6,7 +6,22 @@ import (
"path/filepath"
)
func dirSize(path string) (uint64, error) {
type rpmCache struct {
// root path for the cache
root string
// max cache size
maxSize uint64
}
func newRPMCache(path string, maxSize uint64) *rpmCache {
return &rpmCache{
root: path,
maxSize: maxSize,
}
}
func (r *rpmCache) size() (uint64, error) {
var size uint64
sizer := func(path string, info fs.FileInfo, err error) error {
if err != nil {
@ -15,17 +30,17 @@ func dirSize(path string) (uint64, error) {
size += uint64(info.Size())
return nil
}
err := filepath.Walk(path, sizer)
err := filepath.Walk(r.root, sizer)
return size, err
}
func (bs *BaseSolver) CleanCache() error {
curSize, err := dirSize(bs.cacheDir)
func (r *rpmCache) clean() error {
curSize, err := r.size()
if err != nil {
return err
}
if curSize > bs.maxCacheSize {
return os.RemoveAll(bs.cacheDir)
if curSize > r.maxSize {
return os.RemoveAll(r.root)
}
return nil
}

View file

@ -33,13 +33,11 @@ import (
type BaseSolver struct {
subscriptions *rhsm.Subscriptions
// Cache directory for the DNF metadata
cacheDir string
// Cache information
cache *rpmCache
// Path to the dnf-json binary and optional args (default: "/usr/libexec/osbuild-composer/dnf-json")
dnfJsonCmd []string
maxCacheSize uint64
}
// Create a new unconfigured BaseSolver (without platform information). It can
@ -48,10 +46,9 @@ type BaseSolver struct {
func NewBaseSolver(cacheDir string) *BaseSolver {
subscriptions, _ := rhsm.LoadSystemSubscriptions()
return &BaseSolver{
cacheDir: cacheDir,
cache: newRPMCache(cacheDir, 524288000), // 500 MiB
subscriptions: subscriptions,
dnfJsonCmd: []string{"/usr/libexec/osbuild-composer/dnf-json"},
maxCacheSize: 524288000, // 500 MiB
}
}
@ -59,7 +56,7 @@ func NewBaseSolver(cacheDir string) *BaseSolver {
// cache. This is the maximum size of the cache after a CleanCache()
// call. Cache cleanup is never performed automatically.
func (s *BaseSolver) SetMaxCacheSize(size uint64) {
s.maxCacheSize = size
s.cache.maxSize = size
}
// SetDNFJSONPath sets the path to the dnf-json binary and optionally any command line arguments.
@ -78,6 +75,10 @@ func (bs *BaseSolver) NewWithConfig(modulePlatformID string, releaseVer string,
return s
}
func (bs *BaseSolver) CleanCache() error {
return bs.cache.clean()
}
// Solver is configured with system information in order to resolve
// dependencies for RPM packages using DNF.
type Solver struct {
@ -263,7 +264,7 @@ func (s *Solver) makeDepsolveRequest(pkgSets []rpmmd.PackageSet) (*Request, map[
Command: "depsolve",
ModulePlatformID: s.modulePlatformID,
Arch: s.arch,
CacheDir: s.cacheDir,
CacheDir: s.cache.root,
Arguments: args,
}
@ -280,7 +281,7 @@ func (s *Solver) makeDumpRequest(repos []rpmmd.RepoConfig) (*Request, error) {
Command: "dump",
ModulePlatformID: s.modulePlatformID,
Arch: s.arch,
CacheDir: s.cacheDir,
CacheDir: s.cache.root,
Arguments: arguments{
Repos: dnfRepos,
},