yay/vendor/github.com/Jguer/go-alpm/package.go

302 lines
8.0 KiB
Go
Raw Normal View History

2017-07-14 19:03:54 +02:00
// package.go - libalpm package type and methods.
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.
package alpm
/*
#include <alpm.h>
int pkg_cmp(const void *v1, const void *v2)
{
alpm_pkg_t *p1 = (alpm_pkg_t *)v1;
alpm_pkg_t *p2 = (alpm_pkg_t *)v2;
2018-01-14 18:21:50 +01:00
off_t s1 = alpm_pkg_get_isize(p1);
off_t s2 = alpm_pkg_get_isize(p2);
if (s1 > s2)
return -1;
else if (s1 < s2)
return 1;
else
return 0;
2017-07-14 19:03:54 +02:00
}
*/
import "C"
import (
"time"
"unsafe"
)
// Package describes a single package and associated handle.
type Package struct {
pmpkg *C.alpm_pkg_t
handle Handle
}
// PackageList describes a linked list of packages and associated handle.
type PackageList struct {
*list
handle Handle
}
// ForEach executes an action on each package of the PackageList.
func (l PackageList) ForEach(f func(Package) error) error {
return l.forEach(func(p unsafe.Pointer) error {
return f(Package{(*C.alpm_pkg_t)(p), l.handle})
})
}
// Slice converts the PackageList to a Package Slice.
func (l PackageList) Slice() []Package {
slice := []Package{}
l.ForEach(func(p Package) error {
slice = append(slice, p)
return nil
})
return slice
}
// SortBySize returns a PackageList sorted by size.
func (l PackageList) SortBySize() PackageList {
pkgList := (*C.struct___alpm_list_t)(unsafe.Pointer(l.list))
pkgCache := (*list)(unsafe.Pointer(
C.alpm_list_msort(pkgList,
C.alpm_list_count(pkgList),
C.alpm_list_fn_cmp(C.pkg_cmp))))
return PackageList{pkgCache, l.handle}
}
// DependList describes a linkedlist of dependency type packages.
type DependList struct{ *list }
// ForEach executes an action on each package of the DependList.
func (l DependList) ForEach(f func(Depend) error) error {
return l.forEach(func(p unsafe.Pointer) error {
dep := convertDepend((*C.alpm_depend_t)(p))
return f(dep)
})
}
// Slice converts the DependList to a Depend Slice.
func (l DependList) Slice() []Depend {
slice := []Depend{}
l.ForEach(func(dep Depend) error {
slice = append(slice, dep)
return nil
})
return slice
}
2019-02-04 17:56:02 +01:00
func (pkg *Package) FileName() string {
return C.GoString(C.alpm_pkg_get_filename(pkg.pmpkg))
}
2019-02-04 17:56:02 +01:00
func (pkg *Package) Base() string {
return C.GoString(C.alpm_pkg_get_base(pkg.pmpkg))
}
2019-02-04 17:56:02 +01:00
func (pkg *Package) Base64Signature() string {
return C.GoString(C.alpm_pkg_get_base64_sig(pkg.pmpkg))
}
2019-02-04 17:56:02 +01:00
func (pkg *Package) Validation() Validation {
return Validation(C.alpm_pkg_get_validation(pkg.pmpkg))
}
2017-07-14 19:03:54 +02:00
// Architecture returns the package target Architecture.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Architecture() string {
2017-07-14 19:03:54 +02:00
return C.GoString(C.alpm_pkg_get_arch(pkg.pmpkg))
}
// Backup returns a list of package backups.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Backup() BackupList {
2017-07-14 19:03:54 +02:00
ptr := unsafe.Pointer(C.alpm_pkg_get_backup(pkg.pmpkg))
return BackupList{(*list)(ptr)}
}
// BuildDate returns the BuildDate of the package.
2019-02-04 17:56:02 +01:00
func (pkg *Package) BuildDate() time.Time {
2017-07-14 19:03:54 +02:00
t := C.alpm_pkg_get_builddate(pkg.pmpkg)
return time.Unix(int64(t), 0)
}
// Conflicts returns the conflicts of the package as a DependList.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Conflicts() DependList {
2017-07-14 19:03:54 +02:00
ptr := unsafe.Pointer(C.alpm_pkg_get_conflicts(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// DB returns the package's origin database.
2019-02-04 17:56:02 +01:00
func (pkg *Package) DB() *DB {
2017-07-14 19:03:54 +02:00
ptr := C.alpm_pkg_get_db(pkg.pmpkg)
if ptr == nil {
return nil
}
2019-02-04 17:56:02 +01:00
return &DB{ptr, pkg.handle}
2017-07-14 19:03:54 +02:00
}
// Depends returns the package's dependency list.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Depends() DependList {
2017-07-14 19:03:54 +02:00
ptr := unsafe.Pointer(C.alpm_pkg_get_depends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Depends returns the package's optional dependency list.
2019-02-04 17:56:02 +01:00
func (pkg *Package) OptionalDepends() DependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_optdepends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Depends returns the package's check dependency list.
2019-02-04 17:56:02 +01:00
func (pkg *Package) CheckDepends() DependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_checkdepends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Depends returns the package's make dependency list.
2019-02-04 17:56:02 +01:00
func (pkg *Package) MakeDepends() DependList {
ptr := unsafe.Pointer(C.alpm_pkg_get_makedepends(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
2017-07-14 19:03:54 +02:00
// Description returns the package's description.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Description() string {
2017-07-14 19:03:54 +02:00
return C.GoString(C.alpm_pkg_get_desc(pkg.pmpkg))
}
// Files returns the file list of the package.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Files() []File {
2017-07-14 19:03:54 +02:00
cFiles := C.alpm_pkg_get_files(pkg.pmpkg)
return convertFilelist(cFiles)
}
// ContainsFile checks if the path is in the package filelist
func (pkg *Package) ContainsFile(path string) (File, error) {
return convertFile(C.alpm_filelist_contains(C.alpm_pkg_get_files(pkg.pmpkg), C.CString(path)))
}
2017-07-14 19:03:54 +02:00
// Groups returns the groups the package belongs to.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Groups() StringList {
2017-07-14 19:03:54 +02:00
ptr := unsafe.Pointer(C.alpm_pkg_get_groups(pkg.pmpkg))
return StringList{(*list)(ptr)}
}
// ISize returns the package installed size.
2019-02-04 17:56:02 +01:00
func (pkg *Package) ISize() int64 {
2017-07-14 19:03:54 +02:00
t := C.alpm_pkg_get_isize(pkg.pmpkg)
return int64(t)
}
// InstallDate returns the package install date.
2019-02-04 17:56:02 +01:00
func (pkg *Package) InstallDate() time.Time {
2017-07-14 19:03:54 +02:00
t := C.alpm_pkg_get_installdate(pkg.pmpkg)
return time.Unix(int64(t), 0)
}
// Licenses returns the package license list.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Licenses() StringList {
2017-07-14 19:03:54 +02:00
ptr := unsafe.Pointer(C.alpm_pkg_get_licenses(pkg.pmpkg))
return StringList{(*list)(ptr)}
}
// SHA256Sum returns package SHA256Sum.
2019-02-04 17:56:02 +01:00
func (pkg *Package) SHA256Sum() string {
2017-07-14 19:03:54 +02:00
return C.GoString(C.alpm_pkg_get_sha256sum(pkg.pmpkg))
}
// MD5Sum returns package MD5Sum.
2019-02-04 17:56:02 +01:00
func (pkg *Package) MD5Sum() string {
2017-07-14 19:03:54 +02:00
return C.GoString(C.alpm_pkg_get_md5sum(pkg.pmpkg))
}
// Name returns package name.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Name() string {
2017-07-14 19:03:54 +02:00
return C.GoString(C.alpm_pkg_get_name(pkg.pmpkg))
}
// Packager returns package packager name.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Packager() string {
2017-07-14 19:03:54 +02:00
return C.GoString(C.alpm_pkg_get_packager(pkg.pmpkg))
}
// Provides returns DependList of packages provides by package.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Provides() DependList {
2017-07-14 19:03:54 +02:00
ptr := unsafe.Pointer(C.alpm_pkg_get_provides(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Reason returns package install reason.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Reason() PkgReason {
2017-07-14 19:03:54 +02:00
reason := C.alpm_pkg_get_reason(pkg.pmpkg)
return PkgReason(reason)
}
// Origin returns package origin.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Origin() PkgFrom {
2017-07-14 19:03:54 +02:00
origin := C.alpm_pkg_get_origin(pkg.pmpkg)
return PkgFrom(origin)
}
// Replaces returns a DependList with the packages this package replaces.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Replaces() DependList {
2017-07-14 19:03:54 +02:00
ptr := unsafe.Pointer(C.alpm_pkg_get_replaces(pkg.pmpkg))
return DependList{(*list)(ptr)}
}
// Size returns the packed package size.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Size() int64 {
2017-07-14 19:03:54 +02:00
t := C.alpm_pkg_get_size(pkg.pmpkg)
return int64(t)
}
// URL returns the upstream URL of the package.
2019-02-04 17:56:02 +01:00
func (pkg *Package) URL() string {
2017-07-14 19:03:54 +02:00
return C.GoString(C.alpm_pkg_get_url(pkg.pmpkg))
}
// Version returns the package version.
2019-02-04 17:56:02 +01:00
func (pkg *Package) Version() string {
2017-07-14 19:03:54 +02:00
return C.GoString(C.alpm_pkg_get_version(pkg.pmpkg))
}
// ComputeRequiredBy returns the names of reverse dependencies of a package
2019-02-04 17:56:02 +01:00
func (pkg *Package) ComputeRequiredBy() []string {
2017-07-14 19:03:54 +02:00
result := C.alpm_pkg_compute_requiredby(pkg.pmpkg)
requiredby := make([]string, 0)
for i := (*list)(unsafe.Pointer(result)); i != nil; i = i.Next {
defer C.free(unsafe.Pointer(i))
if i.Data != nil {
defer C.free(i.Data)
name := C.GoString((*C.char)(i.Data))
2017-07-14 19:03:54 +02:00
requiredby = append(requiredby, name)
}
}
return requiredby
}
2018-03-24 00:36:45 +01:00
// ComputeOptionalFor returns the names of packages that optionally require the given package
2019-02-04 17:56:02 +01:00
func (pkg *Package) ComputeOptionalFor() []string {
2018-03-24 00:36:45 +01:00
result := C.alpm_pkg_compute_optionalfor(pkg.pmpkg)
optionalfor := make([]string, 0)
for i := (*list)(unsafe.Pointer(result)); i != nil; i = i.Next {
defer C.free(unsafe.Pointer(i))
if i.Data != nil {
defer C.free(i.Data)
name := C.GoString((*C.char)(i.Data))
2018-03-24 00:36:45 +01:00
optionalfor = append(optionalfor, name)
}
}
return optionalfor
}
2019-02-04 17:56:02 +01:00
func (pkg *Package) ShouldIgnore() bool {
2018-01-14 18:21:50 +01:00
result := C.alpm_pkg_should_ignore(pkg.handle.ptr, pkg.pmpkg)
return result == 1
}