Skip to content

Commit

Permalink
Move functions to correct file and name correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Morganamilo committed Oct 20, 2018
1 parent c5b7288 commit d886147
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 81 deletions.
48 changes: 37 additions & 11 deletions alpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,44 @@ import "C"

import "unsafe"

// Init initializes alpm handle
func Initialize(root, dbpath string) (*Handle, error) {
cRoot := C.CString(root)
cDBPath := C.CString(dbpath)
var cErr C.alpm_errno_t
h := C.alpm_initialize(cRoot, cDBPath, &cErr)

defer C.free(unsafe.Pointer(cRoot))
defer C.free(unsafe.Pointer(cDBPath))

if cErr != 0 {
return nil, Error(cErr)
}

return &Handle{h}, nil
}

// Release releases the alpm handle
func (h *Handle) Release() error {
if er := C.alpm_release(h.ptr); er != 0 {
return Error(er)
}
h.ptr = nil
return nil
}

// LastError gets the last pm_error
func (h *Handle) LastError() error {
if h.ptr != nil {
cErr := C.alpm_errno(h.ptr)
if cErr != 0 {
return Error(cErr)
}
}
return nil
}

// Version returns libalpm version string.
func Version() string {
return C.GoString(C.alpm_version())
}

// VerCmp performs version comparison according to Pacman conventions. Return
// value is <0 if and only if v1 is older than v2.
func VerCmp(v1, v2 string) int {
c1 := C.CString(v1)
c2 := C.CString(v2)
defer C.free(unsafe.Pointer(c1))
defer C.free(unsafe.Pointer(c2))
result := C.alpm_pkg_vercmp(c1, c2)
return int(result)
}
4 changes: 2 additions & 2 deletions alpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var h *Handle

func init() {
var err error
h, err = Init("/", "/var/lib/pacman")
h, err = Initialize("/", "/var/lib/pacman")
if err != nil {
fmt.Printf("failed to Init(): %s", err)
fmt.Printf("failed to Initialize(): %s", err)
os.Exit(1)
}
}
Expand Down
19 changes: 0 additions & 19 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,6 @@ func (l DBList) Slice() []DB {
return slice
}

// LocalDB returns the local database relative to the given handle.
func (h *Handle) LocalDB() (*DB, error) {
db := C.alpm_get_localdb(h.ptr)
if db == nil {
return nil, h.LastError()
}
return &DB{db, *h}, nil
}

// SyncDBs returns list of Synced DBs.
func (h *Handle) SyncDBs() (DBList, error) {
dblist := C.alpm_get_syncdbs(h.ptr)
if dblist == nil {
return DBList{nil, *h}, h.LastError()
}
dblistPtr := unsafe.Pointer(dblist)
return DBList{(*list)(dblistPtr), *h}, nil
}

// SyncDBByName finds a registered database by name.
func (h *Handle) SyncDBByName(name string) (db *DB, err error) {
dblist, err := h.SyncDBs()
Expand Down
File renamed without changes.
56 changes: 19 additions & 37 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,6 @@ type Handle struct {
ptr *C.alpm_handle_t
}

// Init initializes alpm handle
func Init(root, dbpath string) (*Handle, error) {
cRoot := C.CString(root)
cDBPath := C.CString(dbpath)
var cErr C.alpm_errno_t
h := C.alpm_initialize(cRoot, cDBPath, &cErr)

defer C.free(unsafe.Pointer(cRoot))
defer C.free(unsafe.Pointer(cDBPath))

if cErr != 0 {
return nil, Error(cErr)
}

return &Handle{h}, nil
}

// Release releases the alpm handle
func (h *Handle) Release() error {
if er := C.alpm_release(h.ptr); er != 0 {
return Error(er)
}
h.ptr = nil
return nil
}

// LastError gets the last pm_error
func (h *Handle) LastError() error {
if h.ptr != nil {
cErr := C.alpm_errno(h.ptr)
if cErr != 0 {
return Error(cErr)
}
}
return nil
}

//
//alpm options getters and setters
//
Expand Down Expand Up @@ -488,6 +451,25 @@ func (h *Handle) SetDeltaRatio(ratio float64) error {
return nil
}

// LocalDB returns the local database relative to the given handle.
func (h *Handle) LocalDB() (*DB, error) {
db := C.alpm_get_localdb(h.ptr)
if db == nil {
return nil, h.LastError()
}
return &DB{db, *h}, nil
}

// SyncDBs returns list of Synced DBs.
func (h *Handle) SyncDBs() (DBList, error) {
dblist := C.alpm_get_syncdbs(h.ptr)
if dblist == nil {
return DBList{nil, *h}, h.LastError()
}
dblistPtr := unsafe.Pointer(dblist)
return DBList{(*list)(dblistPtr), *h}, nil
}

func (h *Handle) CheckSpace() (bool, error) {
ok := C.alpm_option_get_checkspace(h.ptr)
b := false
Expand Down
10 changes: 0 additions & 10 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,6 @@ func (pkg *Package) ComputeOptionalFor() []string {
return optionalfor
}

// NewVersion checks if there is a new version of the package in the Synced DBs.
func (pkg *Package) NewVersion(l DBList) *Package {
ptr := C.alpm_sync_newversion(pkg.pmpkg,
(*C.alpm_list_t)(unsafe.Pointer(l.list)))
if ptr == nil {
return nil
}
return &Package{ptr, l.handle}
}

func (pkg *Package) ShouldIgnore() bool {
result := C.alpm_pkg_should_ignore(pkg.handle.ptr, pkg.pmpkg)
return result == 1
Expand Down
2 changes: 1 addition & 1 deletion package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func init() {

// Tests package attribute getters.
func TestPkginfo(t *testing.T) {
h, er := Init(root, dbpath)
h, er := Initialize(root, dbpath)
defer h.Release()
if er != nil {
t.Errorf("Failed at alpm initialization: %s", er)
Expand Down
12 changes: 12 additions & 0 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ package alpm
*/
import "C"

import "unsafe"

// NewVersion checks if there is a new version of the package in a given DBlist.
func (pkg *Package) SyncNewVersion(l DBList) *Package {
ptr := C.alpm_sync_newversion(pkg.pmpkg,
(*C.alpm_list_t)(unsafe.Pointer(l.list)))
if ptr == nil {
return nil
}
return &Package{ptr, l.handle}
}

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

Expand Down
2 changes: 1 addition & 1 deletion trans.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (h *Handle) TransGetRemove() PackageList {
return PackageList{(*list)(unsafe.Pointer(pkgs)), *h}
}

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

if flags == -1 {
Expand Down
17 changes: 17 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package alpm

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

import "unsafe"

// VerCmp performs version comparison according to Pacman conventions. Return
// value is <0 if and only if v1 is older than v2.
func VerCmp(v1, v2 string) int {
c1 := C.CString(v1)
c2 := C.CString(v2)
defer C.free(unsafe.Pointer(c1))
defer C.free(unsafe.Pointer(c2))
result := C.alpm_pkg_vercmp(c1, c2)
return int(result)
}

0 comments on commit d886147

Please sign in to comment.