forked from remyoudompheng/go-alpm
-
Notifications
You must be signed in to change notification settings - Fork 14
/
alpm.go
59 lines (47 loc) · 1.05 KB
/
alpm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// alpm.go - Implements exported libalpm functions.
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.
package alpm
// #cgo LDFLAGS: -lalpm
// #include <alpm.h>
import "C"
import "unsafe"
// Init initializes alpm handle
func Initialize(root, dbpath string) (*Handle, error) {
var (
cRoot = C.CString(root)
cDBPath = C.CString(dbpath)
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())
}