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
|
|
|
)
|
|
|
|
|
|
|
|
type checksum struct {
|
|
|
|
filename string
|
|
|
|
hash hash.Hash
|
|
|
|
checksum string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCS(line string) (checksum, error) {
|
|
|
|
elems := strings.Fields(line)
|
|
|
|
if len(elems) != 2 {
|
|
|
|
return checksum{}, fmt.Errorf("unexpected content: %d != 2", len(elems))
|
|
|
|
}
|
|
|
|
cs, f := elems[0], elems[1]
|
|
|
|
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{}, fmt.Errorf("unknown format: %q", line)
|
|
|
|
}
|
|
|
|
return checksum{filename: f, hash: hsh, checksum: cs}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-15 20:50:04 -08:00
|
|
|
type input struct {
|
|
|
|
f io.ReadCloser
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type work struct {
|
|
|
|
cs checksum
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func streams(files []string) chan input {
|
|
|
|
r := make(chan input)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for _, name := range files {
|
|
|
|
f, err := os.Open(name)
|
|
|
|
r <- input{f, err}
|
2016-11-15 20:24:15 -08:00
|
|
|
}
|
2016-11-15 20:50:04 -08:00
|
|
|
if len(files) == 0 {
|
|
|
|
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-15 20:50:04 -08:00
|
|
|
func check(files []string) chan error {
|
|
|
|
jobs := make(chan work)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for stream := range streams(files) {
|
|
|
|
if stream.err != nil {
|
|
|
|
jobs <- work{err: stream.err}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s := bufio.NewScanner(stream.f)
|
|
|
|
for s.Scan() {
|
|
|
|
cs, err := parseCS(s.Text())
|
|
|
|
jobs <- work{cs, err}
|
|
|
|
}
|
|
|
|
stream.f.Close()
|
|
|
|
if s.Err() != nil {
|
|
|
|
jobs <- work{err: s.Err()}
|
2016-11-15 20:24:15 -08:00
|
|
|
}
|
|
|
|
}
|
2016-11-15 20:50:04 -08:00
|
|
|
close(jobs)
|
|
|
|
}()
|
|
|
|
|
|
|
|
results := []<-chan error{}
|
|
|
|
|
2016-11-15 21:28:01 -08:00
|
|
|
for w := 0; w < *ngo; w++ {
|
2016-11-16 00:52:39 -08:00
|
|
|
results = append(results, verify(jobs))
|
2016-11-15 20:24:15 -08:00
|
|
|
}
|
|
|
|
|
2016-11-15 20:50:04 -08:00
|
|
|
return merge(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:52:39 -08:00
|
|
|
func verify(jobs chan work) chan error {
|
2016-11-15 20:50:04 -08:00
|
|
|
r := make(chan error)
|
|
|
|
go func() {
|
|
|
|
for job := range jobs {
|
|
|
|
if job.err != nil {
|
|
|
|
log.Printf("%+v", job.err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
f, err := os.Open(job.cs.filename)
|
|
|
|
if err != nil {
|
|
|
|
r <- fmt.Errorf("open: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(job.cs.hash, f); err != nil {
|
2016-11-15 21:02:03 -08:00
|
|
|
r <- err
|
|
|
|
continue
|
2016-11-15 20:50:04 -08:00
|
|
|
}
|
|
|
|
f.Close()
|
2016-11-15 21:02:03 -08:00
|
|
|
if fmt.Sprintf("%x", job.cs.hash.Sum(nil)) != job.cs.checksum {
|
2016-11-15 20:50:04 -08:00
|
|
|
r <- fmt.Errorf("%s: bad", job.cs.filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(r)
|
|
|
|
}()
|
|
|
|
return r
|
2016-11-15 20:24:15 -08:00
|
|
|
}
|