alpm.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // alpm.go - Implements exported libalpm functions.
  2. //
  3. // Copyright (c) 2013 The go-alpm Authors
  4. //
  5. // MIT Licensed. See LICENSE for details.
  6. package alpm
  7. // #cgo LDFLAGS: -lalpm
  8. // #include <alpm.h>
  9. import "C"
  10. import "unsafe"
  11. // Init initializes alpm handle
  12. func Initialize(root, dbpath string) (*Handle, error) {
  13. cRoot := C.CString(root)
  14. cDBPath := C.CString(dbpath)
  15. var cErr C.alpm_errno_t
  16. h := C.alpm_initialize(cRoot, cDBPath, &cErr)
  17. defer C.free(unsafe.Pointer(cRoot))
  18. defer C.free(unsafe.Pointer(cDBPath))
  19. if cErr != 0 {
  20. return nil, Error(cErr)
  21. }
  22. return &Handle{h}, nil
  23. }
  24. // Release releases the alpm handle
  25. func (h *Handle) Release() error {
  26. if er := C.alpm_release(h.ptr); er != 0 {
  27. return Error(er)
  28. }
  29. h.ptr = nil
  30. return nil
  31. }
  32. // LastError gets the last pm_error
  33. func (h *Handle) LastError() error {
  34. if h.ptr != nil {
  35. cErr := C.alpm_errno(h.ptr)
  36. if cErr != 0 {
  37. return Error(cErr)
  38. }
  39. }
  40. return nil
  41. }
  42. // Version returns libalpm version string.
  43. func Version() string {
  44. return C.GoString(C.alpm_version())
  45. }