package.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // package.go - libalpm package type and methods.
  2. //
  3. // Copyright (c) 2013 The go-alpm Authors
  4. //
  5. // MIT Licensed. See LICENSE for details.
  6. package alpm
  7. /*
  8. #include <alpm.h>
  9. int pkg_cmp(const void *v1, const void *v2)
  10. {
  11. alpm_pkg_t *p1 = (alpm_pkg_t *)v1;
  12. alpm_pkg_t *p2 = (alpm_pkg_t *)v2;
  13. off_t s1 = alpm_pkg_get_isize(p1);
  14. off_t s2 = alpm_pkg_get_isize(p2);
  15. if (s1 > s2)
  16. return -1;
  17. else if (s1 < s2)
  18. return 1;
  19. else
  20. return 0;
  21. }
  22. */
  23. import "C"
  24. import (
  25. "time"
  26. "unsafe"
  27. )
  28. // Package describes a single package and associated handle.
  29. type Package struct {
  30. pmpkg *C.alpm_pkg_t
  31. handle Handle
  32. }
  33. // PackageList describes a linked list of packages and associated handle.
  34. type PackageList struct {
  35. *list
  36. handle Handle
  37. }
  38. // ForEach executes an action on each package of the PackageList.
  39. func (l PackageList) ForEach(f func(Package) error) error {
  40. return l.forEach(func(p unsafe.Pointer) error {
  41. return f(Package{(*C.alpm_pkg_t)(p), l.handle})
  42. })
  43. }
  44. // Slice converts the PackageList to a Package Slice.
  45. func (l PackageList) Slice() []Package {
  46. slice := []Package{}
  47. l.ForEach(func(p Package) error {
  48. slice = append(slice, p)
  49. return nil
  50. })
  51. return slice
  52. }
  53. // SortBySize returns a PackageList sorted by size.
  54. func (l PackageList) SortBySize() PackageList {
  55. pkgList := (*C.struct___alpm_list_t)(unsafe.Pointer(l.list))
  56. pkgCache := (*list)(unsafe.Pointer(
  57. C.alpm_list_msort(pkgList,
  58. C.alpm_list_count(pkgList),
  59. C.alpm_list_fn_cmp(C.pkg_cmp))))
  60. return PackageList{pkgCache, l.handle}
  61. }
  62. // DependList describes a linkedlist of dependency type packages.
  63. type DependList struct{ *list }
  64. // ForEach executes an action on each package of the DependList.
  65. func (l DependList) ForEach(f func(Depend) error) error {
  66. return l.forEach(func(p unsafe.Pointer) error {
  67. dep := convertDepend((*C.alpm_depend_t)(p))
  68. return f(dep)
  69. })
  70. }
  71. // Slice converts the DependList to a Depend Slice.
  72. func (l DependList) Slice() []Depend {
  73. slice := []Depend{}
  74. l.ForEach(func(dep Depend) error {
  75. slice = append(slice, dep)
  76. return nil
  77. })
  78. return slice
  79. }
  80. // Architecture returns the package target Architecture.
  81. func (pkg Package) Architecture() string {
  82. return C.GoString(C.alpm_pkg_get_arch(pkg.pmpkg))
  83. }
  84. // Backup returns a list of package backups.
  85. func (pkg Package) Backup() BackupList {
  86. ptr := unsafe.Pointer(C.alpm_pkg_get_backup(pkg.pmpkg))
  87. return BackupList{(*list)(ptr)}
  88. }
  89. // BuildDate returns the BuildDate of the package.
  90. func (pkg Package) BuildDate() time.Time {
  91. t := C.alpm_pkg_get_builddate(pkg.pmpkg)
  92. return time.Unix(int64(t), 0)
  93. }
  94. // Conflicts returns the conflicts of the package as a DependList.
  95. func (pkg Package) Conflicts() DependList {
  96. ptr := unsafe.Pointer(C.alpm_pkg_get_conflicts(pkg.pmpkg))
  97. return DependList{(*list)(ptr)}
  98. }
  99. // DB returns the package's origin database.
  100. func (pkg Package) DB() *Db {
  101. ptr := C.alpm_pkg_get_db(pkg.pmpkg)
  102. if ptr == nil {
  103. return nil
  104. }
  105. return &Db{ptr, pkg.handle}
  106. }
  107. // Depends returns the package's dependency list.
  108. func (pkg Package) Depends() DependList {
  109. ptr := unsafe.Pointer(C.alpm_pkg_get_depends(pkg.pmpkg))
  110. return DependList{(*list)(ptr)}
  111. }
  112. // Description returns the package's description.
  113. func (pkg Package) Description() string {
  114. return C.GoString(C.alpm_pkg_get_desc(pkg.pmpkg))
  115. }
  116. // Files returns the file list of the package.
  117. func (pkg Package) Files() []File {
  118. cFiles := C.alpm_pkg_get_files(pkg.pmpkg)
  119. return convertFilelist(cFiles)
  120. }
  121. // Groups returns the groups the package belongs to.
  122. func (pkg Package) Groups() StringList {
  123. ptr := unsafe.Pointer(C.alpm_pkg_get_groups(pkg.pmpkg))
  124. return StringList{(*list)(ptr)}
  125. }
  126. // ISize returns the package installed size.
  127. func (pkg Package) ISize() int64 {
  128. t := C.alpm_pkg_get_isize(pkg.pmpkg)
  129. return int64(t)
  130. }
  131. // InstallDate returns the package install date.
  132. func (pkg Package) InstallDate() time.Time {
  133. t := C.alpm_pkg_get_installdate(pkg.pmpkg)
  134. return time.Unix(int64(t), 0)
  135. }
  136. // Licenses returns the package license list.
  137. func (pkg Package) Licenses() StringList {
  138. ptr := unsafe.Pointer(C.alpm_pkg_get_licenses(pkg.pmpkg))
  139. return StringList{(*list)(ptr)}
  140. }
  141. // SHA256Sum returns package SHA256Sum.
  142. func (pkg Package) SHA256Sum() string {
  143. return C.GoString(C.alpm_pkg_get_sha256sum(pkg.pmpkg))
  144. }
  145. // MD5Sum returns package MD5Sum.
  146. func (pkg Package) MD5Sum() string {
  147. return C.GoString(C.alpm_pkg_get_md5sum(pkg.pmpkg))
  148. }
  149. // Name returns package name.
  150. func (pkg Package) Name() string {
  151. return C.GoString(C.alpm_pkg_get_name(pkg.pmpkg))
  152. }
  153. // Packager returns package packager name.
  154. func (pkg Package) Packager() string {
  155. return C.GoString(C.alpm_pkg_get_packager(pkg.pmpkg))
  156. }
  157. // Provides returns DependList of packages provides by package.
  158. func (pkg Package) Provides() DependList {
  159. ptr := unsafe.Pointer(C.alpm_pkg_get_provides(pkg.pmpkg))
  160. return DependList{(*list)(ptr)}
  161. }
  162. // Reason returns package install reason.
  163. func (pkg Package) Reason() PkgReason {
  164. reason := C.alpm_pkg_get_reason(pkg.pmpkg)
  165. return PkgReason(reason)
  166. }
  167. // Origin returns package origin.
  168. func (pkg Package) Origin() PkgFrom {
  169. origin := C.alpm_pkg_get_origin(pkg.pmpkg)
  170. return PkgFrom(origin)
  171. }
  172. // Replaces returns a DependList with the packages this package replaces.
  173. func (pkg Package) Replaces() DependList {
  174. ptr := unsafe.Pointer(C.alpm_pkg_get_replaces(pkg.pmpkg))
  175. return DependList{(*list)(ptr)}
  176. }
  177. // Size returns the packed package size.
  178. func (pkg Package) Size() int64 {
  179. t := C.alpm_pkg_get_size(pkg.pmpkg)
  180. return int64(t)
  181. }
  182. // URL returns the upstream URL of the package.
  183. func (pkg Package) URL() string {
  184. return C.GoString(C.alpm_pkg_get_url(pkg.pmpkg))
  185. }
  186. // Version returns the package version.
  187. func (pkg Package) Version() string {
  188. return C.GoString(C.alpm_pkg_get_version(pkg.pmpkg))
  189. }
  190. // ComputeRequiredBy returns the names of reverse dependencies of a package
  191. func (pkg Package) ComputeRequiredBy() []string {
  192. result := C.alpm_pkg_compute_requiredby(pkg.pmpkg)
  193. requiredby := make([]string, 0)
  194. for i := (*list)(unsafe.Pointer(result)); i != nil; i = i.Next {
  195. defer C.free(unsafe.Pointer(i))
  196. if i.Data != nil {
  197. defer C.free(unsafe.Pointer(i.Data))
  198. name := C.GoString((*C.char)(unsafe.Pointer(i.Data)))
  199. requiredby = append(requiredby, name)
  200. }
  201. }
  202. return requiredby
  203. }
  204. // NewVersion checks if there is a new version of the package in the Synced DBs.
  205. func (pkg Package) NewVersion(l DbList) *Package {
  206. ptr := C.alpm_sync_newversion(pkg.pmpkg,
  207. (*C.alpm_list_t)(unsafe.Pointer(l.list)))
  208. if ptr == nil {
  209. return nil
  210. }
  211. return &Package{ptr, l.handle}
  212. }
  213. func (pkg Package) ShouldIgnore() bool {
  214. result := C.alpm_pkg_should_ignore(pkg.handle.ptr, pkg.pmpkg)
  215. return result == 1
  216. }