package.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. func (pkg *Package) FileName() string {
  81. return C.GoString(C.alpm_pkg_get_filename(pkg.pmpkg))
  82. }
  83. func (pkg *Package) Base() string {
  84. return C.GoString(C.alpm_pkg_get_base(pkg.pmpkg))
  85. }
  86. func (pkg *Package) Base64Signature() string {
  87. return C.GoString(C.alpm_pkg_get_base64_sig(pkg.pmpkg))
  88. }
  89. func (pkg *Package) Validation() Validation {
  90. return Validation(C.alpm_pkg_get_validation(pkg.pmpkg))
  91. }
  92. // Architecture returns the package target Architecture.
  93. func (pkg *Package) Architecture() string {
  94. return C.GoString(C.alpm_pkg_get_arch(pkg.pmpkg))
  95. }
  96. // Backup returns a list of package backups.
  97. func (pkg *Package) Backup() BackupList {
  98. ptr := unsafe.Pointer(C.alpm_pkg_get_backup(pkg.pmpkg))
  99. return BackupList{(*list)(ptr)}
  100. }
  101. // BuildDate returns the BuildDate of the package.
  102. func (pkg *Package) BuildDate() time.Time {
  103. t := C.alpm_pkg_get_builddate(pkg.pmpkg)
  104. return time.Unix(int64(t), 0)
  105. }
  106. // Conflicts returns the conflicts of the package as a DependList.
  107. func (pkg *Package) Conflicts() DependList {
  108. ptr := unsafe.Pointer(C.alpm_pkg_get_conflicts(pkg.pmpkg))
  109. return DependList{(*list)(ptr)}
  110. }
  111. // DB returns the package's origin database.
  112. func (pkg *Package) DB() *DB {
  113. ptr := C.alpm_pkg_get_db(pkg.pmpkg)
  114. if ptr == nil {
  115. return nil
  116. }
  117. return &DB{ptr, pkg.handle}
  118. }
  119. // Depends returns the package's dependency list.
  120. func (pkg *Package) Depends() DependList {
  121. ptr := unsafe.Pointer(C.alpm_pkg_get_depends(pkg.pmpkg))
  122. return DependList{(*list)(ptr)}
  123. }
  124. // Depends returns the package's optional dependency list.
  125. func (pkg *Package) OptionalDepends() DependList {
  126. ptr := unsafe.Pointer(C.alpm_pkg_get_optdepends(pkg.pmpkg))
  127. return DependList{(*list)(ptr)}
  128. }
  129. // Depends returns the package's check dependency list.
  130. func (pkg *Package) CheckDepends() DependList {
  131. ptr := unsafe.Pointer(C.alpm_pkg_get_checkdepends(pkg.pmpkg))
  132. return DependList{(*list)(ptr)}
  133. }
  134. // Depends returns the package's make dependency list.
  135. func (pkg *Package) MakeDepends() DependList {
  136. ptr := unsafe.Pointer(C.alpm_pkg_get_makedepends(pkg.pmpkg))
  137. return DependList{(*list)(ptr)}
  138. }
  139. // Description returns the package's description.
  140. func (pkg *Package) Description() string {
  141. return C.GoString(C.alpm_pkg_get_desc(pkg.pmpkg))
  142. }
  143. // Files returns the file list of the package.
  144. func (pkg *Package) Files() []File {
  145. cFiles := C.alpm_pkg_get_files(pkg.pmpkg)
  146. return convertFilelist(cFiles)
  147. }
  148. // ContainsFile checks if the path is in the package filelist
  149. func (pkg *Package) ContainsFile(path string) (File, error) {
  150. return convertFile(C.alpm_filelist_contains(C.alpm_pkg_get_files(pkg.pmpkg), C.CString(path)))
  151. }
  152. // Groups returns the groups the package belongs to.
  153. func (pkg *Package) Groups() StringList {
  154. ptr := unsafe.Pointer(C.alpm_pkg_get_groups(pkg.pmpkg))
  155. return StringList{(*list)(ptr)}
  156. }
  157. // ISize returns the package installed size.
  158. func (pkg *Package) ISize() int64 {
  159. t := C.alpm_pkg_get_isize(pkg.pmpkg)
  160. return int64(t)
  161. }
  162. // InstallDate returns the package install date.
  163. func (pkg *Package) InstallDate() time.Time {
  164. t := C.alpm_pkg_get_installdate(pkg.pmpkg)
  165. return time.Unix(int64(t), 0)
  166. }
  167. // Licenses returns the package license list.
  168. func (pkg *Package) Licenses() StringList {
  169. ptr := unsafe.Pointer(C.alpm_pkg_get_licenses(pkg.pmpkg))
  170. return StringList{(*list)(ptr)}
  171. }
  172. // SHA256Sum returns package SHA256Sum.
  173. func (pkg *Package) SHA256Sum() string {
  174. return C.GoString(C.alpm_pkg_get_sha256sum(pkg.pmpkg))
  175. }
  176. // MD5Sum returns package MD5Sum.
  177. func (pkg *Package) MD5Sum() string {
  178. return C.GoString(C.alpm_pkg_get_md5sum(pkg.pmpkg))
  179. }
  180. // Name returns package name.
  181. func (pkg *Package) Name() string {
  182. return C.GoString(C.alpm_pkg_get_name(pkg.pmpkg))
  183. }
  184. // Packager returns package packager name.
  185. func (pkg *Package) Packager() string {
  186. return C.GoString(C.alpm_pkg_get_packager(pkg.pmpkg))
  187. }
  188. // Provides returns DependList of packages provides by package.
  189. func (pkg *Package) Provides() DependList {
  190. ptr := unsafe.Pointer(C.alpm_pkg_get_provides(pkg.pmpkg))
  191. return DependList{(*list)(ptr)}
  192. }
  193. // Reason returns package install reason.
  194. func (pkg *Package) Reason() PkgReason {
  195. reason := C.alpm_pkg_get_reason(pkg.pmpkg)
  196. return PkgReason(reason)
  197. }
  198. // Origin returns package origin.
  199. func (pkg *Package) Origin() PkgFrom {
  200. origin := C.alpm_pkg_get_origin(pkg.pmpkg)
  201. return PkgFrom(origin)
  202. }
  203. // Replaces returns a DependList with the packages this package replaces.
  204. func (pkg *Package) Replaces() DependList {
  205. ptr := unsafe.Pointer(C.alpm_pkg_get_replaces(pkg.pmpkg))
  206. return DependList{(*list)(ptr)}
  207. }
  208. // Size returns the packed package size.
  209. func (pkg *Package) Size() int64 {
  210. t := C.alpm_pkg_get_size(pkg.pmpkg)
  211. return int64(t)
  212. }
  213. // URL returns the upstream URL of the package.
  214. func (pkg *Package) URL() string {
  215. return C.GoString(C.alpm_pkg_get_url(pkg.pmpkg))
  216. }
  217. // Version returns the package version.
  218. func (pkg *Package) Version() string {
  219. return C.GoString(C.alpm_pkg_get_version(pkg.pmpkg))
  220. }
  221. // ComputeRequiredBy returns the names of reverse dependencies of a package
  222. func (pkg *Package) ComputeRequiredBy() []string {
  223. result := C.alpm_pkg_compute_requiredby(pkg.pmpkg)
  224. requiredby := make([]string, 0)
  225. for i := (*list)(unsafe.Pointer(result)); i != nil; i = i.Next {
  226. defer C.free(unsafe.Pointer(i))
  227. if i.Data != nil {
  228. defer C.free(i.Data)
  229. name := C.GoString((*C.char)(i.Data))
  230. requiredby = append(requiredby, name)
  231. }
  232. }
  233. return requiredby
  234. }
  235. // ComputeOptionalFor returns the names of packages that optionally require the given package
  236. func (pkg *Package) ComputeOptionalFor() []string {
  237. result := C.alpm_pkg_compute_optionalfor(pkg.pmpkg)
  238. optionalfor := make([]string, 0)
  239. for i := (*list)(unsafe.Pointer(result)); i != nil; i = i.Next {
  240. defer C.free(unsafe.Pointer(i))
  241. if i.Data != nil {
  242. defer C.free(i.Data)
  243. name := C.GoString((*C.char)(i.Data))
  244. optionalfor = append(optionalfor, name)
  245. }
  246. }
  247. return optionalfor
  248. }
  249. func (pkg *Package) ShouldIgnore() bool {
  250. result := C.alpm_pkg_should_ignore(pkg.handle.ptr, pkg.pmpkg)
  251. return result == 1
  252. }