yay/pkg/db/ialpm/alpm.go

460 lines
10 KiB
Go
Raw Normal View History

2020-08-16 23:41:38 +02:00
package ialpm
2020-07-29 01:53:25 +02:00
import (
2020-08-16 23:41:38 +02:00
"bufio"
"errors"
2020-08-08 18:43:37 +02:00
"fmt"
"os"
2020-08-16 23:41:38 +02:00
"strconv"
2020-08-08 18:43:37 +02:00
"time"
2020-07-29 01:53:25 +02:00
alpm "github.com/Jguer/go-alpm"
2020-08-07 18:55:19 +02:00
pacmanconf "github.com/Morganamilo/go-pacmanconf"
"github.com/leonelquinteros/gotext"
2020-08-16 23:41:38 +02:00
"github.com/Jguer/yay/v10/pkg/db"
"github.com/Jguer/yay/v10/pkg/settings"
"github.com/Jguer/yay/v10/pkg/text"
2020-08-01 01:20:00 +02:00
"github.com/Jguer/yay/v10/pkg/upgrade"
2020-07-29 01:53:25 +02:00
)
type AlpmExecutor struct {
2020-08-16 23:41:38 +02:00
handle *alpm.Handle
localDB *alpm.DB
syncDB alpm.DBList
conf *pacmanconf.Config
2020-07-29 01:53:25 +02:00
}
2020-08-16 23:41:38 +02:00
func NewExecutor(pacamnConf *pacmanconf.Config) (*AlpmExecutor, error) {
ae := &AlpmExecutor{conf: pacamnConf}
2020-08-08 18:43:37 +02:00
err := ae.RefreshHandle()
2020-07-29 01:53:25 +02:00
if err != nil {
return nil, err
}
2020-08-08 18:43:37 +02:00
ae.localDB, err = ae.handle.LocalDB()
2020-07-29 01:53:25 +02:00
if err != nil {
return nil, err
}
2020-08-08 18:43:37 +02:00
ae.syncDB, err = ae.handle.SyncDBs()
if err != nil {
return nil, err
}
return ae, nil
}
func toUsage(usages []string) alpm.Usage {
if len(usages) == 0 {
return alpm.UsageAll
}
var ret alpm.Usage
for _, usage := range usages {
switch usage {
case "Sync":
ret |= alpm.UsageSync
case "Search":
ret |= alpm.UsageSearch
case "Install":
ret |= alpm.UsageInstall
case "Upgrade":
ret |= alpm.UsageUpgrade
case "All":
ret |= alpm.UsageAll
}
}
return ret
}
func configureAlpm(pacmanConf *pacmanconf.Config, alpmHandle *alpm.Handle) error {
// TODO: set SigLevel
// sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
// localFileSigLevel := alpm.SigUseDefault
// remoteFileSigLevel := alpm.SigUseDefault
for _, repo := range pacmanConf.Repos {
// TODO: set SigLevel
2020-08-16 23:41:38 +02:00
alpmDB, err := alpmHandle.RegisterSyncDB(repo.Name, 0)
if err != nil {
return err
}
2020-08-16 23:41:38 +02:00
alpmDB.SetServers(repo.Servers)
alpmDB.SetUsage(toUsage(repo.Usage))
}
if err := alpmHandle.SetCacheDirs(pacmanConf.CacheDir); err != nil {
return err
}
// add hook directories 1-by-1 to avoid overwriting the system directory
for _, dir := range pacmanConf.HookDir {
if err := alpmHandle.AddHookDir(dir); err != nil {
return err
}
}
if err := alpmHandle.SetGPGDir(pacmanConf.GPGDir); err != nil {
return err
}
if err := alpmHandle.SetLogFile(pacmanConf.LogFile); err != nil {
return err
}
if err := alpmHandle.SetIgnorePkgs(pacmanConf.IgnorePkg); err != nil {
return err
}
if err := alpmHandle.SetIgnoreGroups(pacmanConf.IgnoreGroup); err != nil {
return err
}
if err := alpmHandle.SetArch(pacmanConf.Architecture); err != nil {
return err
}
if err := alpmHandle.SetNoUpgrades(pacmanConf.NoUpgrade); err != nil {
return err
}
if err := alpmHandle.SetNoExtracts(pacmanConf.NoExtract); err != nil {
return err
}
/*if err := alpmHandle.SetDefaultSigLevel(sigLevel); err != nil {
return err
}
if err := alpmHandle.SetLocalFileSigLevel(localFileSigLevel); err != nil {
return err
}
if err := alpmHandle.SetRemoteFileSigLevel(remoteFileSigLevel); err != nil {
return err
}*/
if err := alpmHandle.SetUseSyslog(pacmanConf.UseSyslog); err != nil {
return err
}
return alpmHandle.SetCheckSpace(pacmanConf.CheckSpace)
}
func logCallback(level alpm.LogLevel, str string) {
switch level {
case alpm.LogWarning:
text.Warn(str)
case alpm.LogError:
text.Error(str)
}
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) questionCallback() func(question alpm.QuestionAny) {
return func(question alpm.QuestionAny) {
if qi, err := question.QuestionInstallIgnorepkg(); err == nil {
qi.SetInstall(true)
}
qp, err := question.QuestionSelectProvider()
if err != nil {
return
}
if settings.HideMenus {
return
}
size := 0
_ = qp.Providers(ae.handle).ForEach(func(pkg alpm.Package) error {
size++
return nil
})
str := text.Bold(gotext.Get("There are %d providers available for %s:\n", size, qp.Dep()))
size = 1
var dbName string
_ = qp.Providers(ae.handle).ForEach(func(pkg alpm.Package) error {
thisDB := pkg.DB().Name()
if dbName != thisDB {
dbName = thisDB
str += text.SprintOperationInfo(gotext.Get("Repository"), dbName, "\n ")
}
str += fmt.Sprintf("%d) %s ", size, pkg.Name())
size++
return nil
})
text.OperationInfoln(str)
for {
fmt.Print(gotext.Get("\nEnter a number (default=1): "))
// TODO: reenable noconfirm
// if config.NoConfirm {
// fmt.Println()
// break
// }
reader := bufio.NewReader(os.Stdin)
numberBuf, overflow, err := reader.ReadLine()
if err != nil {
text.Errorln(err)
break
}
if overflow {
text.Errorln(gotext.Get(" Input too long"))
continue
}
if string(numberBuf) == "" {
break
}
num, err := strconv.Atoi(string(numberBuf))
if err != nil {
text.Errorln(gotext.Get("invalid number: %s", string(numberBuf)))
continue
}
if num < 1 || num > size {
text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size))
continue
}
qp.SetUseIndex(num - 1)
break
}
}
}
func (ae *AlpmExecutor) RefreshHandle() error {
if ae.handle != nil {
if errRelease := ae.handle.Release(); errRelease != nil {
return errRelease
}
}
alpmHandle, err := alpm.Initialize(ae.conf.RootDir, ae.conf.DBPath)
if err != nil {
return errors.New(gotext.Get("unable to CreateHandle: %s", err))
}
if errConf := configureAlpm(ae.conf, alpmHandle); errConf != nil {
return errConf
}
2020-08-16 23:41:38 +02:00
alpmHandle.SetQuestionCallback(ae.questionCallback())
alpmHandle.SetLogCallback(logCallback)
ae.handle = alpmHandle
ae.syncDB, err = alpmHandle.SyncDBs()
if err != nil {
return err
}
ae.localDB, err = alpmHandle.LocalDB()
return err
2020-07-29 01:53:25 +02:00
}
func (ae *AlpmExecutor) LocalSatisfierExists(pkgName string) bool {
if _, err := ae.localDB.PkgCache().FindSatisfier(pkgName); err != nil {
2020-07-29 01:53:25 +02:00
return false
}
return true
}
2020-08-01 01:20:00 +02:00
func (ae *AlpmExecutor) SyncSatisfierExists(pkgName string) bool {
if _, err := ae.syncDB.FindSatisfier(pkgName); err != nil {
return false
}
return true
}
2020-07-29 01:53:25 +02:00
func (ae *AlpmExecutor) IsCorrectVersionInstalled(pkgName, versionRequired string) bool {
alpmPackage := ae.localDB.Pkg(pkgName)
2020-07-29 01:53:25 +02:00
if alpmPackage == nil {
return false
}
return alpmPackage.Version() == versionRequired
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) SyncSatisfier(pkgName string) db.RepoPackage {
foundPkg, err := ae.syncDB.FindSatisfier(pkgName)
2020-07-29 01:53:25 +02:00
if err != nil {
return nil
}
return foundPkg
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) PackagesFromGroup(groupName string) []db.RepoPackage {
groupPackages := []db.RepoPackage{}
_ = ae.syncDB.FindGroupPkgs(groupName).ForEach(func(pkg alpm.Package) error {
2020-07-29 01:53:25 +02:00
groupPackages = append(groupPackages, &pkg)
return nil
})
return groupPackages
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) LocalPackages() []db.RepoPackage {
localPackages := []db.RepoPackage{}
_ = ae.localDB.PkgCache().ForEach(func(pkg alpm.Package) error {
2020-08-16 23:41:38 +02:00
localPackages = append(localPackages, db.RepoPackage(&pkg))
2020-07-29 01:53:25 +02:00
return nil
})
return localPackages
}
// SyncPackages searches SyncDB for packages or returns all packages if no search param is given
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) SyncPackages(pkgNames ...string) []db.RepoPackage {
repoPackages := []db.RepoPackage{}
_ = ae.syncDB.ForEach(func(alpmDB alpm.DB) error {
if len(pkgNames) == 0 {
2020-08-16 23:41:38 +02:00
_ = alpmDB.PkgCache().ForEach(func(pkg alpm.Package) error {
repoPackages = append(repoPackages, db.RepoPackage(&pkg))
return nil
})
} else {
2020-08-16 23:41:38 +02:00
_ = alpmDB.Search(pkgNames).ForEach(func(pkg alpm.Package) error {
repoPackages = append(repoPackages, db.RepoPackage(&pkg))
return nil
})
}
return nil
})
return repoPackages
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) LocalPackage(pkgName string) db.RepoPackage {
pkg := ae.localDB.Pkg(pkgName)
if pkg == nil {
return nil
}
return pkg
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) PackageFromDB(pkgName, dbName string) db.RepoPackage {
singleDB, err := ae.handle.SyncDBByName(dbName)
2020-07-29 01:53:25 +02:00
if err != nil {
return nil
}
foundPkg, err := singleDB.PkgCache().FindSatisfier(pkgName)
if err != nil {
return nil
}
return foundPkg
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) PackageDepends(pkg db.RepoPackage) []alpm.Depend {
2020-07-29 01:53:25 +02:00
alpmPackage := pkg.(*alpm.Package)
return alpmPackage.Depends().Slice()
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) PackageOptionalDepends(pkg db.RepoPackage) []alpm.Depend {
2020-08-07 18:55:19 +02:00
alpmPackage := pkg.(*alpm.Package)
return alpmPackage.OptionalDepends().Slice()
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) PackageProvides(pkg db.RepoPackage) []alpm.Depend {
2020-07-29 01:53:25 +02:00
alpmPackage := pkg.(*alpm.Package)
return alpmPackage.Provides().Slice()
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) PackageConflicts(pkg db.RepoPackage) []alpm.Depend {
2020-07-29 01:53:25 +02:00
alpmPackage := pkg.(*alpm.Package)
return alpmPackage.Conflicts().Slice()
}
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) PackageGroups(pkg db.RepoPackage) []string {
alpmPackage := pkg.(*alpm.Package)
return alpmPackage.Groups().Slice()
}
2020-08-01 01:20:00 +02:00
// upRepo gathers local packages and checks if they have new versions.
// Output: Upgrade type package list.
func (ae *AlpmExecutor) RepoUpgrades(enableDowngrade bool) (upgrade.UpSlice, error) {
slice := upgrade.UpSlice{}
localDB, err := ae.handle.LocalDB()
if err != nil {
return slice, err
}
err = ae.handle.TransInit(alpm.TransFlagNoLock)
if err != nil {
return slice, err
}
defer func() {
err = ae.handle.TransRelease()
}()
err = ae.handle.SyncSysupgrade(enableDowngrade)
if err != nil {
return slice, err
}
_ = ae.handle.TransGetAdd().ForEach(func(pkg alpm.Package) error {
localVer := "-"
if localPkg := localDB.Pkg(pkg.Name()); localPkg != nil {
localVer = localPkg.Version()
}
slice = append(slice, upgrade.Upgrade{
Name: pkg.Name(),
Repository: pkg.DB().Name(),
LocalVersion: localVer,
RemoteVersion: pkg.Version(),
})
return nil
})
return slice, nil
}
func (ae *AlpmExecutor) AlpmArch() (string, error) {
return ae.handle.Arch()
}
2020-08-04 22:00:07 +02:00
2020-08-16 23:41:38 +02:00
func (ae *AlpmExecutor) BiggestPackages() []db.RepoPackage {
localPackages := []db.RepoPackage{}
2020-08-04 22:00:07 +02:00
_ = ae.localDB.PkgCache().SortBySize().ForEach(func(pkg alpm.Package) error {
2020-08-16 23:41:38 +02:00
localPackages = append(localPackages, db.RepoPackage(&pkg))
2020-08-04 22:00:07 +02:00
return nil
})
return localPackages
}
2020-08-08 18:43:37 +02:00
func (ae *AlpmExecutor) LastBuildTime() time.Time {
var lastTime time.Time
_ = ae.syncDB.ForEach(func(db alpm.DB) error {
_ = db.PkgCache().ForEach(func(pkg alpm.Package) error {
thisTime := pkg.BuildDate()
if thisTime.After(lastTime) {
lastTime = thisTime
}
return nil
})
return nil
})
return lastTime
}
func (ae *AlpmExecutor) Cleanup() {
if ae.handle != nil {
if err := ae.handle.Release(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}