Arch Linux helpers and parsers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

77 lines
1.5 KiB

package archlinux
import (
"fmt"
"io"
"path/filepath"
"strings"
"maze.io/archlinux.v0/tarball"
"maze.io/archlinux.v0/version"
)
// Repository holds 0 or more Packages
type Repository struct {
// Name of the repository
Name string
// Packages in the repository
Packages []*Package
}
// ReadRepositoryDB reads a compressed repository tarball
func ReadRepositoryDB(r io.Reader) (*Repository, error) {
t, err := tarball.NewReader(r)
if err != nil {
return nil, err
}
repo := new(Repository)
if n, ok := r.(namer); ok {
repo.Name = strings.SplitN(filepath.Base(n.Name()), ".db", 2)[0]
}
for {
h, err := t.Next()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
var (
base = filepath.Base(strings.Trim(h.Name, "/"))
part = strings.Split(strings.Trim(h.Name, "/"), "/")
seen = make(map[string]*Package)
name string
)
if len(part) == 1 || base == "" || base[0] == '.' || h.FileInfo().IsDir() {
continue
}
name = part[0]
i := strings.LastIndexByte(name, '-')
if i == -1 {
return nil, fmt.Errorf("archlinux: invalid package name %q", name)
}
pkg, ok := seen[name]
if !ok {
v := version.Parse(name[i+1:])
pkg = &Package{
Name: name[:i],
Version: v.Version,
Release: v.Release,
}
seen[name] = pkg
repo.Packages = append(repo.Packages, pkg)
}
switch base {
case "desc", "depends", "files":
if err = pkg.readPackageDesc(t); err != nil {
return nil, err
}
}
}
return repo, nil
}