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.

58 lines
1.0 KiB

  1. package archlinux
  2. import (
  3. "bytes"
  4. "fmt"
  5. "hash"
  6. "sort"
  7. "strings"
  8. )
  9. // name is an interface for instances that have a name, such as *os.File
  10. type namer interface {
  11. // Name returns the instance name
  12. Name() string
  13. }
  14. type multiHasher struct {
  15. hash.Hash
  16. Expect []byte
  17. }
  18. type multiHash map[string]multiHasher
  19. func newMultiHash() multiHash {
  20. return make(multiHash)
  21. }
  22. func (mh multiHash) Add(name string, fn func() hash.Hash, expect []byte) {
  23. if len(expect) == 0 || bytes.Equal(expect, []byte(skip)) {
  24. return
  25. }
  26. mh[name] = multiHasher{fn(), expect}
  27. }
  28. func (mh multiHash) Check() error {
  29. var errors []string
  30. for name, h := range mh {
  31. sum := h.Sum(nil)
  32. if !bytes.Equal(sum, h.Expect) {
  33. errors = append(errors, fmt.Sprintf("%s check failed", name))
  34. }
  35. }
  36. if len(errors) > 0 {
  37. sort.Strings(errors)
  38. return fmt.Errorf("archlinux: %s", strings.Join(errors, ", "))
  39. }
  40. return nil
  41. }
  42. func (mh multiHash) Write(p []byte) (int, error) {
  43. for _, h := range mh {
  44. if n, err := h.Write(p); err != nil {
  45. return n, err
  46. }
  47. }
  48. return len(p), nil
  49. }