Skip to content

Commit

Permalink
Support Usage, transactions and Implement alpm_db_search (#17)
Browse files Browse the repository at this point in the history
* Support Usage option

* Implement alpm_db_search

* Add basic support for transactions

* Test Usage
  • Loading branch information
Morganamilo authored and Jguer committed Jul 25, 2018
1 parent 1114f77 commit 047b539
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 2 deletions.
27 changes: 27 additions & 0 deletions conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type PacmanConfig struct {
type RepoConfig struct {
Name string
SigLevel SigLevel
Usage Usage
Servers []string
}

Expand Down Expand Up @@ -197,6 +198,24 @@ lineloop:
case "SigLevel":
// TODO: implement SigLevel parsing.
continue lineloop
case "Usage":
for _, usage := range line.Values {
switch usage {
case "Sync":
curRepo.Usage |= UsageSync
case "Search":
curRepo.Usage |= UsageSearch
case "Install":
curRepo.Usage |= UsageInstall
case "Upgrade":
curRepo.Usage |= UsageUpgrade
case "All":
curRepo.Usage |= UsageAll
default:
err = fmt.Errorf("unknown option at line %d: %s", rdr.Lineno, line.Name)
break lineloop
}
}
case "Server":
curRepo.Servers = append(curRepo.Servers, line.Values...)
continue lineloop
Expand Down Expand Up @@ -261,6 +280,13 @@ lineloop:
conf.CacheDir = []string{"/var/cache/pacman/pkg/"} //should only be set if the config does not specify this
}

for n, _ := range conf.Repos {
repo := &conf.Repos[n]
if repo.Usage == 0 {
repo.Usage = UsageAll
}
}

return conf, err
}

Expand Down Expand Up @@ -316,6 +342,7 @@ func (conf *PacmanConfig) CreateHandle() (*Handle, error) {
repoconf.Servers[i] = addr
}
db.SetServers(repoconf.Servers)
db.SetUsage(repoconf.Usage)
}
}

Expand Down
4 changes: 2 additions & 2 deletions conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ var pacmanConfRef = PacmanConfig{
Options: ConfColor | ConfCheckSpace | ConfVerbosePkgLists,

Repos: []RepoConfig{
{Name: "core", Servers: []string{"ftp://ftp.example.com/foobar/$repo/os/$arch/"}},
{Name: "custom", Servers: []string{"file:///home/custompkgs"}},
{Name: "core", Usage: UsageAll, Servers: []string{"ftp://ftp.example.com/foobar/$repo/os/$arch/"}},
{Name: "custom", Usage: UsageInstall | UsageUpgrade, Servers: []string{"file:///home/custompkgs"}},
},
}

Expand Down
25 changes: 25 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ func (db Db) SetServers(servers []string) {
}
}

// SetUsage sets the Usage of the database
func (db Db) SetUsage(usage Usage) {
C.alpm_db_set_usage(db.ptr, C.int(usage))
}

// PkgByName searches a package in db.
func (db Db) PkgByName(name string) (*Package, error) {
cName := C.CString(name)
Expand Down Expand Up @@ -151,3 +156,23 @@ func (db Db) PkgCache() PackageList {
pkgcache := (*list)(unsafe.Pointer(C.alpm_db_get_pkgcache(db.ptr)))
return PackageList{pkgcache, db.handle}
}

func (db Db) Search(targets []string) PackageList {
needles := &C.alpm_list_t{}
head := needles
needles.data = unsafe.Pointer(C.CString(targets[0]))

for _, str := range targets[1:] {
needles.next = &C.alpm_list_t{}
needles = needles.next
needles.data = unsafe.Pointer(C.CString(str))
}

pkglist := (*list)(unsafe.Pointer(C.alpm_db_search(db.ptr, needles)))

for needles = head; needles != nil; needles = needles.next {
C.free(needles.data)
}

return PackageList{pkglist, db.handle}
}
35 changes: 35 additions & 0 deletions enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,38 @@ const (
ValidationSignature
ValidationUnkown Validation = 0
)

type Usage int

const (
UsageSync Usage = 1 << iota
UsageSearch
UsageInstall
UsageUpgrade
UsageAll = (1 << 4) - 1
)

type TransFlag int

const (
TransFlagNoDeps TransFlag = 1 << iota
TransFlagForce
TransFlagNoSave
TransFlagNoDepVersion
TransFlagCascade
TransFlagRecurse
// 7 is missing
_
TransFlagDbOnly
TransFlagAllDeps
TransFlagDownloadOnly
TransFlagNoScriptlets
// 12 is missing
_
TransFlagNoConflicts
TransFlagNeeded
TransFlagAllExplicit
TransFlagUnneeded
TransFlagRecurseAll
TransFlagNoLock
)
6 changes: 6 additions & 0 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ int pkg_cmp(const void *v1, const void *v2)
import "C"

import (
"fmt"
"time"
"unsafe"
)
Expand Down Expand Up @@ -309,3 +310,8 @@ func (pkg Package) ShouldIgnore() bool {
result := C.alpm_pkg_should_ignore(pkg.handle.ptr, pkg.pmpkg)
return result == 1
}

func (pkg Package) OldPkg() Package {
fmt.Printf("%#v\n", pkg.pmpkg)
return Package{pkg.pmpkg, pkg.handle}
}
27 changes: 27 additions & 0 deletions sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// db.go - Functions for database handling.
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.

package alpm

/*
#include <alpm.h>
*/
import "C"

func (h *Handle) SyncSysupgrade(enableDowngrade bool) error {
intEnableDowngrade := C.int(0)

if enableDowngrade {
intEnableDowngrade = C.int(1)
}

ret := C.alpm_sync_sysupgrade(h.ptr, intEnableDowngrade)
if ret != 0 {
return h.LastError()
}

return nil
}
1 change: 1 addition & 0 deletions testing/conf/good_pacman.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ Server = ftp://ftp.example.com/foobar/$repo/os/$arch/

[custom]
Server = file:///home/custompkgs
Usage = Install Upgrade
54 changes: 54 additions & 0 deletions trans.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// db.go - Functions for database handling.
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.

package alpm

/*
#include <alpm.h>
*/
import "C"

import (
"unsafe"
)

func (h *Handle) TransInit(flags TransFlag) error {
ret := C.alpm_trans_init(h.ptr, C.int(flags))
if ret != 0 {
return h.LastError()
}

return nil
}

func (h *Handle) TransRelease() error {
ret := C.alpm_trans_release(h.ptr)
if ret != 0 {
return h.LastError()
}

return nil
}

func (h *Handle) TransGetAdd() PackageList {
pkgs := C.alpm_trans_get_add(h.ptr)
return PackageList{(*list)(unsafe.Pointer(pkgs)), *h}
}

func (h *Handle) TransGetRemove() PackageList {
pkgs := C.alpm_trans_get_remove(h.ptr)
return PackageList{(*list)(unsafe.Pointer(pkgs)), *h}
}

func (h *Handle) TransGetFalgs() (TransFlag, error) {
flags := C.alpm_trans_get_flags(h.ptr)

if flags == -1 {
return -1, h.LastError()
}

return TransFlag(flags), nil
}
8 changes: 8 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ func (question QuestionAny) QuestionSelectProvider() (QuestionSelectProvider, er
return QuestionSelectProvider{}, fmt.Errorf("Can not convert to QuestionInstallIgnorepkg")
}

func (question QuestionAny) QuestionReplace() (QuestionReplace, error) {
if question.Type() == QuestionTypeReplacePkg {
return *(*QuestionReplace)(unsafe.Pointer(&question)), nil
}

return QuestionReplace{}, fmt.Errorf("Can not convert to QuestionReplace")
}

func (question QuestionInstallIgnorepkg) SetInstall(install bool) {
if install {
question.ptr.install = 1
Expand Down

0 comments on commit 047b539

Please sign in to comment.