Permalink
Browse files

Add Db.SetServers and creation of Handle from pacman.conf

  • Loading branch information...
1 parent fc65f39 commit 2990b58470404e8c537e701e6127dea5af5a8236 Rémy Oudompheng committed Feb 25, 2012
Showing with 58 additions and 0 deletions.
  1. +49 −0 conf.go
  2. +9 −0 db.go
View
49 conf.go
@@ -8,6 +8,7 @@ import (
"os"
"reflect"
"strings"
+ "syscall"
)
// Parsing routines for pacman.conf format.
@@ -144,6 +145,7 @@ func (rdr *confReader) ParseLine() (tok iniToken, err error) {
func ParseConfig(r io.Reader) (conf PacmanConfig, err error) {
rdr := newConfReader(r)
rdrStack := []confReader{rdr}
+ conf.SetDefaults()
confReflect := reflect.ValueOf(&conf).Elem()
var currentSection string
var curRepo *RepoConfig
@@ -224,3 +226,50 @@ lineloop:
}
panic("impossible")
}
+
+func (conf *PacmanConfig) SetDefaults() {
+ conf.RootDir = "/"
+ conf.DBPath = "/var/lib/pacman"
+}
+
+func getArch() (string, error) {
+ var uname syscall.Utsname
+ err := syscall.Uname(&uname)
+ if err != nil {
+ return "", err
+ }
+ var arch [65]byte
+ for i, c := range uname.Machine {
+ if c == 0 {
+ return string(arch[:i]), nil
+ }
+ arch[i] = byte(c)
+ }
+ return string(arch[:]), nil
+}
+
+func (conf *PacmanConfig) CreateHandle() (*Handle, error) {
+ h, err := Init(conf.RootDir, conf.DBPath)
+ if err != nil {
+ return nil, err
+ }
+ if conf.Architecture == "auto" {
+ conf.Architecture, err = getArch()
+ if err != nil {
+ return nil, fmt.Errorf("architecture is 'auto' but couldn't uname()")
+ }
+ }
+ for _, repoconf := range conf.Repos {
+ // TODO: set SigLevel
+ db, err := h.RegisterSyncDb(repoconf.Name, 0)
+ if err == nil {
+ for i, addr := range repoconf.Servers {
+ addr = strings.Replace(addr, "$repo", repoconf.Name, -1)
+ addr = strings.Replace(addr, "$arch", conf.Architecture, -1)
+ repoconf.Servers[i] = addr
+ }
+ db.SetServers(repoconf.Servers)
+ }
+ }
+ return h, nil
+}
View
9 db.go
@@ -95,6 +95,15 @@ func (db Db) Servers() []string {
return StringList{(*list)(ptr)}.Slice()
}
+func (db Db) SetServers(servers []string) {
+ C.alpm_db_set_servers(db.ptr, nil)
+ for _, srv := range servers {
+ C_srv := C.CString(srv)
+ defer C.free(unsafe.Pointer(C_srv))
+ C.alpm_db_add_server(db.ptr, C_srv)
+ }
+}
+
func (db Db) PkgByName(name string) (*Package, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))

0 comments on commit 2990b58

Please sign in to comment.