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.

78 lines
1.5 KiB

  1. package archlinux
  2. import (
  3. "fmt"
  4. "io"
  5. "path/filepath"
  6. "strings"
  7. "maze.io/archlinux.v0/tarball"
  8. "maze.io/archlinux.v0/version"
  9. )
  10. // Repository holds 0 or more Packages
  11. type Repository struct {
  12. // Name of the repository
  13. Name string
  14. // Packages in the repository
  15. Packages []*Package
  16. }
  17. // ReadRepositoryDB reads a compressed repository tarball
  18. func ReadRepositoryDB(r io.Reader) (*Repository, error) {
  19. t, err := tarball.NewReader(r)
  20. if err != nil {
  21. return nil, err
  22. }
  23. repo := new(Repository)
  24. if n, ok := r.(namer); ok {
  25. repo.Name = strings.SplitN(filepath.Base(n.Name()), ".db", 2)[0]
  26. }
  27. for {
  28. h, err := t.Next()
  29. if err != nil {
  30. if err == io.EOF {
  31. break
  32. }
  33. return nil, err
  34. }
  35. var (
  36. base = filepath.Base(strings.Trim(h.Name, "/"))
  37. part = strings.Split(strings.Trim(h.Name, "/"), "/")
  38. seen = make(map[string]*Package)
  39. name string
  40. )
  41. if len(part) == 1 || base == "" || base[0] == '.' || h.FileInfo().IsDir() {
  42. continue
  43. }
  44. name = part[0]
  45. i := strings.LastIndexByte(name, '-')
  46. if i == -1 {
  47. return nil, fmt.Errorf("archlinux: invalid package name %q", name)
  48. }
  49. pkg, ok := seen[name]
  50. if !ok {
  51. v := version.Parse(name[i+1:])
  52. pkg = &Package{
  53. Name: name[:i],
  54. Version: v.Version,
  55. Release: v.Release,
  56. }
  57. seen[name] = pkg
  58. repo.Packages = append(repo.Packages, pkg)
  59. }
  60. switch base {
  61. case "desc", "depends", "files":
  62. if err = pkg.readPackageDesc(t); err != nil {
  63. return nil, err
  64. }
  65. }
  66. }
  67. return repo, nil
  68. }