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.
 

57 lines
1.0 KiB

package archlinux
import (
"bytes"
"fmt"
"hash"
"sort"
"strings"
)
// name is an interface for instances that have a name, such as *os.File
type namer interface {
// Name returns the instance name
Name() string
}
type multiHasher struct {
hash.Hash
Expect []byte
}
type multiHash map[string]multiHasher
func newMultiHash() multiHash {
return make(multiHash)
}
func (mh multiHash) Add(name string, fn func() hash.Hash, expect []byte) {
if len(expect) == 0 || bytes.Equal(expect, []byte(skip)) {
return
}
mh[name] = multiHasher{fn(), expect}
}
func (mh multiHash) Check() error {
var errors []string
for name, h := range mh {
sum := h.Sum(nil)
if !bytes.Equal(sum, h.Expect) {
errors = append(errors, fmt.Sprintf("%s check failed", name))
}
}
if len(errors) > 0 {
sort.Strings(errors)
return fmt.Errorf("archlinux: %s", strings.Join(errors, ", "))
}
return nil
}
func (mh multiHash) Write(p []byte) (int, error) {
for _, h := range mh {
if n, err := h.Write(p); err != nil {
return n, err
}
}
return len(p), nil
}