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

169 lines
3.8 KiB
Go
Raw Normal View History

2017-07-14 19:03:54 +02:00
// db.go - Functions for database handling.
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.
package alpm
/*
#include <alpm.h>
2018-08-18 21:57:39 +02:00
#include <alpm_list.h>
2017-07-14 19:03:54 +02:00
*/
import "C"
import (
"fmt"
"io"
"unsafe"
)
2019-02-04 17:56:02 +01:00
// DB structure representing a alpm database.
type DB struct {
2017-07-14 19:03:54 +02:00
ptr *C.alpm_db_t
handle Handle
}
2019-02-04 17:56:02 +01:00
// DBList structure representing a alpm database list.
type DBList struct {
2017-07-14 19:03:54 +02:00
*list
handle Handle
}
2019-02-04 17:56:02 +01:00
// ForEach executes an action on each DB.
func (l DBList) ForEach(f func(DB) error) error {
2017-07-14 19:03:54 +02:00
return l.forEach(func(p unsafe.Pointer) error {
2019-02-04 17:56:02 +01:00
return f(DB{(*C.alpm_db_t)(p), l.handle})
2017-07-14 19:03:54 +02:00
})
}
2019-02-04 17:56:02 +01:00
// Slice converst DB list to DB slice.
func (l DBList) Slice() []DB {
slice := []DB{}
l.ForEach(func(db DB) error {
2017-07-14 19:03:54 +02:00
slice = append(slice, db)
return nil
})
return slice
}
2019-02-04 17:56:02 +01:00
// SyncDBByName finds a registered database by name.
func (h *Handle) SyncDBByName(name string) (db *DB, err error) {
dblist, err := h.SyncDBs()
2017-07-14 19:03:54 +02:00
if err != nil {
return nil, err
}
2019-02-04 17:56:02 +01:00
dblist.ForEach(func(b DB) error {
2017-07-14 19:03:54 +02:00
if b.Name() == name {
db = &b
return io.EOF
}
return nil
})
if db != nil {
return db, nil
}
return nil, fmt.Errorf("database %s not found", name)
}
2019-02-04 17:56:02 +01:00
// RegisterSyncDB Loads a sync database with given name and signature check level.
func (h *Handle) RegisterSyncDB(dbname string, siglevel SigLevel) (*DB, error) {
2017-07-14 19:03:54 +02:00
cName := C.CString(dbname)
defer C.free(unsafe.Pointer(cName))
db := C.alpm_register_syncdb(h.ptr, cName, C.int(siglevel))
2017-07-14 19:03:54 +02:00
if db == nil {
return nil, h.LastError()
}
2019-02-04 17:56:02 +01:00
return &DB{db, *h}, nil
}
func (db *DB) Unregister() error {
ok := C.alpm_db_unregister(db.ptr)
if ok != 0 {
return db.handle.LastError()
}
return nil
}
func (h *Handle) UnregisterAllSyncDBs() error {
ok := C.alpm_unregister_all_syncdbs(h.ptr)
if ok != 0 {
return h.LastError()
}
return nil
2017-07-14 19:03:54 +02:00
}
// Name returns name of the db
2019-02-04 17:56:02 +01:00
func (db *DB) Name() string {
2017-07-14 19:03:54 +02:00
return C.GoString(C.alpm_db_get_name(db.ptr))
}
// Servers returns host server URL.
2019-02-04 17:56:02 +01:00
func (db *DB) Servers() []string {
2017-07-14 19:03:54 +02:00
ptr := unsafe.Pointer(C.alpm_db_get_servers(db.ptr))
return StringList{(*list)(ptr)}.Slice()
}
// SetServers sets server list to use.
2019-02-04 17:56:02 +01:00
func (db *DB) SetServers(servers []string) {
2017-07-14 19:03:54 +02:00
C.alpm_db_set_servers(db.ptr, nil)
for _, srv := range servers {
Csrv := C.CString(srv)
defer C.free(unsafe.Pointer(Csrv))
C.alpm_db_add_server(db.ptr, Csrv)
}
}
2019-02-04 17:56:02 +01:00
// AddServers adds a string to the server list.
func (db *DB) AddServer(server string) {
Csrv := C.CString(server)
defer C.free(unsafe.Pointer(Csrv))
C.alpm_db_add_server(db.ptr, Csrv)
}
2018-07-26 14:58:51 +02:00
// SetUsage sets the Usage of the database
2019-02-04 17:56:02 +01:00
func (db *DB) SetUsage(usage Usage) {
2018-07-26 14:58:51 +02:00
C.alpm_db_set_usage(db.ptr, C.int(usage))
}
2019-02-04 17:56:02 +01:00
// Name searches a package in db.
2019-02-14 21:44:39 +01:00
func (db *DB) Pkg(name string) *Package {
2017-07-14 19:03:54 +02:00
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
ptr := C.alpm_db_get_pkg(db.ptr, cName)
2019-03-02 22:15:33 +01:00
if ptr == nil {
return nil
}
2019-02-14 21:44:39 +01:00
return &Package{ptr, db.handle}
2017-07-14 19:03:54 +02:00
}
// PkgCachebyGroup returns a PackageList of packages belonging to a group
2019-02-14 21:44:39 +01:00
func (l DBList) FindGroupPkgs(name string) PackageList {
2017-07-14 19:03:54 +02:00
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
pkglist := (*C.struct___alpm_list_t)(unsafe.Pointer(l.list))
pkgcache := (*list)(unsafe.Pointer(C.alpm_find_group_pkgs(pkglist, cName)))
2019-02-14 21:44:39 +01:00
return PackageList{pkgcache, l.handle}
2017-07-14 19:03:54 +02:00
}
// PkgCache returns the list of packages of the database
2019-02-04 17:56:02 +01:00
func (db *DB) PkgCache() PackageList {
2017-07-14 19:03:54 +02:00
pkgcache := (*list)(unsafe.Pointer(C.alpm_db_get_pkgcache(db.ptr)))
return PackageList{pkgcache, db.handle}
}
2018-07-26 14:58:51 +02:00
2019-02-04 17:56:02 +01:00
func (db *DB) Search(targets []string) PackageList {
2018-08-18 21:57:39 +02:00
var needles *C.alpm_list_t
2018-07-26 14:58:51 +02:00
2018-08-18 21:57:39 +02:00
for _, str := range targets {
needles = C.alpm_list_add(needles, unsafe.Pointer(C.CString(str)))
2018-07-26 14:58:51 +02:00
}
2018-08-18 21:57:39 +02:00
pkglist := (*list)(unsafe.Pointer(C.alpm_db_search(db.ptr, needles)))
C.alpm_list_free(needles)
2018-07-26 14:58:51 +02:00
return PackageList{pkglist, db.handle}
}