package.go 6.4 KB

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