yay/pkg/db/ialpm/high_level.go
Andrew Geng d33bf8841d
Fix yay -Sc wiping ~/.cache/yay on 3rd question. (#2175)
If you answer yes to
    :: Do you want to remove all other AUR packages from cache? [Y/n]
then we run cleanAUR(), intending to remove subdirectories of
~/.cache/yay that do not share a name with installed packages not
found in the sync repositories.

Where this was going wrong was cleanAUR() was getting an empty map from
dbExecutor.InstalledRemotePackages()---because InstalledRemotePackages
only recomputes its result if installedRemotePkgMap is nil, whereas
NewExecutor initialized it to an empty map. The symptom was it emptied
my ~/.cache/yay.

We do want a non-nil, empty installedRemotePkgMap to block recomputing
(that is, to indicate the user really has no remote packages), so now
NewExecutor initializes it to nil, and getPackageNamesBySource is
responsible for making sure it's non-nil before writing to it.

Fixes #2152, which seems to have been introduced in
4626a0409c.
2023-05-22 20:34:51 +00:00

55 lines
1.4 KiB
Go

package ialpm
import (
alpm "github.com/Jguer/go-alpm/v2"
"github.com/Jguer/yay/v12/pkg/text"
)
// GetPackageNamesBySource returns package names with and without correspondence in SyncDBS respectively.
func (ae *AlpmExecutor) getPackageNamesBySource() {
if ae.installedRemotePkgMap == nil {
ae.installedRemotePkgMap = map[string]alpm.IPackage{}
}
for _, localpkg := range ae.LocalPackages() {
pkgName := localpkg.Name()
if ae.SyncPackage(pkgName) != nil {
ae.installedSyncPkgNames = append(ae.installedSyncPkgNames, pkgName)
} else {
ae.installedRemotePkgNames = append(ae.installedRemotePkgNames, pkgName)
ae.installedRemotePkgMap[pkgName] = localpkg
}
}
ae.log.Debugln("populating db executor package caches.",
"sync_len", len(ae.installedSyncPkgNames), "remote_len", len(ae.installedRemotePkgNames))
}
func (ae *AlpmExecutor) InstalledRemotePackages() map[string]alpm.IPackage {
if ae.installedRemotePkgMap == nil {
ae.getPackageNamesBySource()
}
return ae.installedRemotePkgMap
}
func (ae *AlpmExecutor) InstalledRemotePackageNames() []string {
if ae.installedRemotePkgNames == nil {
ae.getPackageNamesBySource()
}
return ae.installedRemotePkgNames
}
func (ae *AlpmExecutor) InstalledSyncPackageNames() []string {
if ae.installedSyncPkgNames == nil {
ae.getPackageNamesBySource()
}
return ae.installedSyncPkgNames
}
func (ae *AlpmExecutor) SetLogger(logger *text.Logger) {
ae.log = logger
}