Skip to content

Commit

Permalink
Add SyncDBListByDBName method (#62)
Browse files Browse the repository at this point in the history
It allows to get a database list consisting of a single database.
Also added Append method for DBList required to implement the method above.
  • Loading branch information
smolx authored Jul 6, 2023
1 parent 134dbc7 commit 46242f8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
24 changes: 24 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ func (l DBList) Slice() []IDB {
return slice
}

// Append modifies a DB list by appending the given DB.
func (l *DBList) Append(db IDB) {
cdblist := (*C.alpm_list_t)(unsafe.Pointer(l.list))
cdb := unsafe.Pointer(db.(*DB).ptr)

cdblist = C.alpm_list_add(cdblist, cdb)

l.list = (*list)(unsafe.Pointer(cdblist))
}

// SyncDBByName finds a registered database by name.
func (h *Handle) SyncDBByName(name string) (db IDB, err error) {
dblist, err := h.SyncDBs()
Expand All @@ -70,6 +80,20 @@ func (h *Handle) SyncDBByName(name string) (db IDB, err error) {
return nil, fmt.Errorf("database %s not found", name)
}

// SyncDBListByDBName creates and returns a database list with a single
// database given by name.
func (h *Handle) SyncDBListByDBName(name string) (IDBList, error) {
db, err := h.SyncDBByName(name)
if err != nil {
return nil, err
}

dblist := h.NewDBList()
dblist.Append(db)

return dblist, nil
}

// RegisterSyncDB Loads a sync database with given name and signature check level.
func (h *Handle) RegisterSyncDB(dbname string, siglevel SigLevel) (IDB, error) {
cName := C.CString(dbname)
Expand Down
5 changes: 5 additions & 0 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ func (h *Handle) SyncDBs() (IDBList, error) {
return &DBList{(*list)(unsafe.Pointer(dblist)), *h}, nil
}

// NewDBList returns a new empty DB list.
func (h *Handle) NewDBList() IDBList {
return &DBList{nil, *h}
}

func (h *Handle) CheckSpace() (bool, error) {
ok := C.alpm_option_get_checkspace(h.ptr)

Expand Down
2 changes: 2 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ type IDBList interface {
ForEach(func(IDB) error) error
// Slice converts DB list to DB slice.
Slice() []IDB
// Append modifies DB list with given DB appended.
Append(IDB)
// PkgCachebyGroup returns a PackageList of packages belonging to a group
FindGroupPkgs(string) IPackageList
// FindSatisfier searches a DBList for a package that satisfies depstring
Expand Down

0 comments on commit 46242f8

Please sign in to comment.