handle.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // handle.go - libalpm handle type and methods.
  2. //
  3. // Copyright (c) 2013 The go-alpm Authors
  4. //
  5. // MIT Licensed. See LICENSE for details.
  6. // Package alpm implements Go bindings to the libalpm library used by Pacman,
  7. // the Arch Linux package manager. Libalpm allows the creation of custom front
  8. // ends to the Arch Linux package ecosystem.
  9. //
  10. // Libalpm does not include support for the Arch User Repository (AUR).
  11. package alpm
  12. // #include <alpm.h>
  13. import "C"
  14. import (
  15. "unsafe"
  16. )
  17. type Handle struct {
  18. ptr *C.alpm_handle_t
  19. }
  20. // Initialize
  21. func Init(root, dbpath string) (*Handle, error) {
  22. c_root := C.CString(root)
  23. defer C.free(unsafe.Pointer(c_root))
  24. c_dbpath := C.CString(dbpath)
  25. defer C.free(unsafe.Pointer(c_dbpath))
  26. var c_err C.alpm_errno_t
  27. h := C.alpm_initialize(c_root, c_dbpath, &c_err)
  28. if c_err != 0 {
  29. return nil, Error(c_err)
  30. }
  31. return &Handle{h}, nil
  32. }
  33. func (h *Handle) Release() error {
  34. if er := C.alpm_release(h.ptr); er != 0 {
  35. return Error(er)
  36. }
  37. h.ptr = nil
  38. return nil
  39. }
  40. func (h Handle) Root() string {
  41. return C.GoString(C.alpm_option_get_root(h.ptr))
  42. }
  43. func (h Handle) DbPath() string {
  44. return C.GoString(C.alpm_option_get_dbpath(h.ptr))
  45. }
  46. // LastError gets the last pm_error
  47. func (h Handle) LastError() error {
  48. if h.ptr != nil {
  49. c_err := C.alpm_errno(h.ptr)
  50. if c_err != 0 {
  51. return Error(c_err)
  52. }
  53. }
  54. return nil
  55. }
  56. func (h Handle) UseSyslog() bool {
  57. value := C.alpm_option_get_usesyslog(h.ptr)
  58. return (value != 0)
  59. }
  60. func (h Handle) SetUseSyslog(value bool) error {
  61. var int_value C.int
  62. if value {
  63. int_value = 1
  64. } else {
  65. int_value = 0
  66. }
  67. ok := C.alpm_option_set_usesyslog(h.ptr, int_value)
  68. if ok < 0 {
  69. return h.LastError()
  70. }
  71. return nil
  72. }