cs/check.go

159 lines
2.7 KiB
Go
Raw Normal View History

2016-11-15 20:24:15 -08:00
package main
import (
"bufio"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"io"
"log"
"os"
"strings"
2016-11-15 20:50:04 -08:00
"sync"
2016-11-15 20:24:15 -08:00
)
2016-11-16 01:18:35 -08:00
// input contains a file-ish piece of work to perform
type input struct {
f io.ReadCloser
err error
}
// checksum contains the path to a file, a way to hash it, and the results of
// the hash
2016-11-15 20:24:15 -08:00
type checksum struct {
filename string
hash hash.Hash
checksum string
2016-11-16 01:18:35 -08:00
err error
2016-11-15 20:24:15 -08:00
}
2016-11-16 01:18:35 -08:00
// check is the entry point for -c operation.
func check(args []string) chan error {
jobs := make(chan checksum)
2016-11-15 20:24:15 -08:00
2016-11-16 01:18:35 -08:00
go func() {
for i := range toInput(args) {
if i.err != nil {
jobs <- checksum{err: i.err}
break
}
s := bufio.NewScanner(i.f)
for s.Scan() {
jobs <- parseCS(s.Text())
}
i.f.Close()
if s.Err() != nil {
jobs <- checksum{err: s.Err()}
}
}
close(jobs)
}()
2016-11-15 20:50:04 -08:00
2016-11-16 01:18:35 -08:00
results := []<-chan error{}
for w := 0; w < *ngo; w++ {
results = append(results, verify(jobs))
}
return merge(results)
2016-11-15 20:50:04 -08:00
}
2016-11-16 01:18:35 -08:00
// toInput converts args to a stream of input
func toInput(args []string) chan input {
2016-11-15 20:50:04 -08:00
r := make(chan input)
go func() {
2016-11-16 01:18:35 -08:00
for _, name := range args {
2016-11-15 20:50:04 -08:00
f, err := os.Open(name)
r <- input{f, err}
2016-11-15 20:24:15 -08:00
}
2016-11-16 01:18:35 -08:00
if len(args) == 0 {
2016-11-15 20:50:04 -08:00
r <- input{f: os.Stdin}
}
close(r)
2016-11-15 20:24:15 -08:00
}()
2016-11-15 20:50:04 -08:00
return r
}
2016-11-15 20:24:15 -08:00
2016-11-16 01:18:35 -08:00
// parseCS picks apart a line from a checksum file and returns everything
// needed to perform a checksum.
func parseCS(line string) checksum {
elems := strings.Fields(line)
2016-12-01 21:53:41 -08:00
if len(elems) < 1 {
return checksum{err: fmt.Errorf("couldn't find checksum in %q", line)}
2016-11-16 01:18:35 -08:00
}
2016-12-01 21:53:41 -08:00
cs := elems[0]
2016-11-16 01:18:35 -08:00
var hsh hash.Hash
switch len(cs) {
case 32:
hsh = md5.New()
case 40:
hsh = sha1.New()
case 64:
hsh = sha256.New()
case 128:
hsh = sha512.New()
default:
return checksum{err: fmt.Errorf("unknown format: %q", line)}
}
2016-12-01 21:53:41 -08:00
return checksum{filename: strings.TrimSpace(line[len(cs):]), hash: hsh, checksum: cs}
2016-11-16 01:18:35 -08:00
}
2016-11-15 20:50:04 -08:00
2016-11-16 01:18:35 -08:00
// verify does grunt work of verifying a stream of jobs (filenames).
func verify(jobs chan checksum) chan error {
r := make(chan error)
2016-11-15 20:50:04 -08:00
go func() {
2016-11-16 01:18:35 -08:00
for job := range jobs {
if job.err != nil {
log.Printf("%+v", job.err)
continue
2016-11-15 20:50:04 -08:00
}
2016-11-16 01:18:35 -08:00
f, err := os.Open(job.filename)
if err != nil {
r <- err
continue
2016-11-15 20:50:04 -08:00
}
2016-11-16 01:18:35 -08:00
if _, err := io.Copy(job.hash, f); err != nil {
r <- err
continue
}
f.Close()
if fmt.Sprintf("%x", job.hash.Sum(nil)) != job.checksum {
r <- fmt.Errorf("%s: bad", job.filename)
2016-11-15 20:24:15 -08:00
}
}
2016-11-16 01:18:35 -08:00
close(r)
2016-11-15 20:50:04 -08:00
}()
2016-11-16 01:18:35 -08:00
return r
2016-11-15 20:50:04 -08:00
}
2016-11-16 01:18:35 -08:00
// merge is simple error fan-in
2016-11-15 20:50:04 -08:00
func merge(cs []<-chan error) chan error {
out := make(chan error)
var wg sync.WaitGroup
output := func(c <-chan error) {
for n := range c {
out <- n
2016-11-15 20:24:15 -08:00
}
2016-11-15 20:50:04 -08:00
wg.Done()
2016-11-15 20:24:15 -08:00
}
2016-11-15 20:50:04 -08:00
wg.Add(len(cs))
for _, c := range cs {
go output(c)
2016-11-15 20:24:15 -08:00
}
2016-11-15 20:50:04 -08:00
go func() {
wg.Wait()
close(out)
}()
return out
}