Documentation
¶
Overview ¶
Package dnfcache is an in-memory index of DNF/YUM repository metadata.
It parses /etc/yum.repos.d/*.repo files, fetches repomd.xml from each enabled repository, downloads and parses primary.xml.gz to build a package index, and provides O(1) Lookup, transitive ResolveDeps with virtual-package (Provides) handling, and concurrent SHA256-verified downloads.
Typical use:
if err := dnfcache.Update(ctx); err != nil {
return err
}
if err := dnfcache.Install(ctx, []string{"gcc", "make"}); err != nil {
return err
}
Package dnfcache module-stream support for RHEL/Rocky AppStream.
RHEL 8 and derivatives ship multiple incompatible versions of the same package (perl, python, nodejs, ruby, postgresql, ...) as "module streams" in the AppStream repo. All streams are dumped into primary.xml, so a naive name-keyed index can land on a non-default stream and produce broken dependency closures (e.g. perl-5.24 installed without perl-libs-5.24, which lives in the modular providers we don't index).
This file loads `modules.yaml(.gz|.xz)` from each repo's `repomd.xml` `<data type="modules">` entry and constructs:
- defaultStream[moduleName] = default stream (e.g. "perl" -> "5.26")
- allowedModularNVRA = set of NVRA strings belonging to default streams
addPackage then rejects modular packages (Release contains ".module+") whose NVRA is not in allowedModularNVRA, leaving the non-modular variant (or no variant) — matching what `dnf install` would resolve to without an explicit `dnf module enable`.
Index ¶
- func DownloadRPM(ctx context.Context, pkg *PackageInfo, destDir string) (string, error)
- func ExpandBooleanDep(name string) []string
- func Install(ctx context.Context, names []string) error
- func IsBooleanDep(name string) bool
- func StripRPMConstraint(name string) string
- func Update(ctx context.Context) error
- type Cache
- type PackageInfo
- type RepoEntry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DownloadRPM ¶
DownloadRPM downloads a single .rpm package to destDir, verifying SHA256 against the cached metadata. Exported wrapper around the internal helper so callers outside the package (e.g. pkg/dnfinstall) can reuse the same download path used by Install.
func ExpandBooleanDep ¶ added in v2.1.10
ExpandBooleanDep extracts the candidate package names from an RPM rich/boolean dependency expression. Boolean operators (if/or/and/…) and version constraints are stripped; the remaining tokens are returned as soft (best-effort) dependencies. Returns nil for non-boolean inputs.
Example: "(gcc-plugin-annobin if gcc)" → ["gcc-plugin-annobin", "gcc"]
The resolver visits each result as a soft dep, so missing tokens (e.g. the conditional trigger that isn't actually being installed) are logged and ignored rather than reported as build-breaking unresolved deps.
func Install ¶
Install resolves the transitive closure of names, downloads the .rpm files, and installs them via rpm --install. This replaces "dnf install".
func IsBooleanDep ¶ added in v2.1.10
IsBooleanDep reports whether name is an RPM rich/boolean dependency expression like "(foo if bar)" or "(libA or libB)".
func StripRPMConstraint ¶
StripRPMConstraint strips RPM version constraint suffixes from a dep name. Examples: "glibc >= 2.17" → "glibc", "libfoo(x86-64)" → "libfoo(x86-64)" (parenthesised capability names are kept as-is).
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is an in-memory index of RPM package metadata keyed by package name. The zero value is not usable; call Load or Update.
func Load ¶
func Load() *Cache
Load returns the process-global Cache, loading it from disk on the first call. Subsequent calls return the cached result immediately. The cache is always non-nil; on non-RPM hosts it is empty.
func Reload ¶
func Reload() *Cache
Reload discards the cached result and re-reads repo metadata from disk. Call this after Update so newly fetched indexes are visible.
func (*Cache) Lookup ¶
func (c *Cache) Lookup(name string) (*PackageInfo, bool)
Lookup returns the PackageInfo for the named package and whether it was found.
func (*Cache) ResolveDeps ¶
ResolveDeps performs transitive dependency resolution starting from the given seed package names. Returns packages in dependency order (deps before dependents) and a list of unresolvable names.
Already-installed packages (detected via rpmdb) are skipped but their dependency edges are still walked so transitive-only packages are pulled in.
func (*Cache) ResolveVirtual ¶
ResolveVirtual returns the first concrete provider of a virtual package name (capability), or the original name if it is a real package or unknown.
type PackageInfo ¶
type PackageInfo struct {
// Name is the package name (e.g. "gcc", "glibc-devel").
Name string
// Arch is the package architecture (e.g. "x86_64", "noarch").
Arch string
// Version is the package version string.
Version string
// Release is the package release string.
Release string
// Epoch is the package epoch (0 if absent).
Epoch string
// LocationHref is the relative path of the .rpm in the repository
// (e.g. "Packages/g/gcc-12.2.0-1.el8.x86_64.rpm").
LocationHref string
// SHA256 is the expected SHA-256 checksum of the .rpm file.
SHA256 string
// Size is the expected size of the .rpm file in bytes.
Size int64
// BaseURL is the repo base URL where LocationHref is relative to.
BaseURL string
// Requires is the list of package names this package depends on.
Requires []string
// Provides is the list of capabilities this package provides.
Provides []string
// Recommends is the list of weak (Recommends) dependencies. dnf installs
// them by default; the resolver treats them as best-effort (missing
// recommends do not fail the build).
Recommends []string
}
PackageInfo holds the subset of primary.xml fields needed for dep resolution and download.
type RepoEntry ¶
type RepoEntry struct {
ID string
BaseURL string // first baseurl= value (kept for single-URL callers)
BaseURLs []string // all baseurl= values in listed order
MirrorList string // mirrorlist= / metalink= URL (used when no baseurl)
Enabled bool
}
RepoEntry holds a single enabled repository parsed from a .repo file.
func ParseRepoFileContent ¶
ParseRepoFileContent parses the INI-style content of a .repo file.