cs/hash.go

152 lines
2.8 KiB
Go
Raw Permalink Normal View History

2016-11-16 01:18:35 -08:00
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"io"
"os"
"sort"
2016-11-16 01:18:35 -08:00
"sync"
)
// result is a message or error payload
type result struct {
f string
cs string
2016-11-16 01:18:35 -08:00
err error
}
// results exists to sort a slice of result
type results []result
func (r results) Len() int { return len(r) }
func (r results) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r results) Less(i, j int) bool { return r[i].f < r[j].f }
2016-11-16 01:18:35 -08:00
// hashr exists so that we can make a thing that can return valid hash.Hash
// interfaces.
type hashr func() hash.Hash
// hsh figures out which hash algo to use, and distributes the work of hashing
2018-03-23 09:27:06 -07:00
func hsh(files []string, verbose, recursive bool) chan result {
2016-11-16 01:18:35 -08:00
var h hashr
switch *algo {
case "sha1", "1":
h = sha1.New
case "sha256", "256":
h = sha256.New
case "sha512", "512":
h = sha512.New
case "md5":
h = md5.New
default:
r := make(chan result)
go func() {
2017-01-16 19:02:41 -08:00
r <- result{err: fmt.Errorf("unsupported algorithm: %v (supported: md5, sha1, sha256, sha512)", *algo)}
2017-01-16 19:00:25 -08:00
close(r)
2016-11-16 01:18:35 -08:00
}()
return r
}
if len(files) == 0 {
2016-11-16 01:27:11 -08:00
r := make(chan result)
go func() {
hsh := h()
_, err := io.Copy(hsh, os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
r <- result{cs: fmt.Sprintf("%x", hsh.Sum(nil)), f: "-"}
2016-11-16 01:27:11 -08:00
close(r)
}()
return r
2016-11-16 01:18:35 -08:00
}
jobs := make(chan checksum)
go func() {
2018-03-23 09:27:06 -07:00
// if recursive then we gotta take every file that's a directory and
// recurse them babies
2016-11-16 01:18:35 -08:00
for _, name := range files {
jobs <- checksum{filename: name}
}
close(jobs)
}()
res := []<-chan result{}
for w := 0; w < *ngo; w++ {
2017-10-18 17:58:11 -07:00
res = append(res, compute(h, jobs, verbose))
2016-11-16 01:18:35 -08:00
}
o := make(chan result)
go func() {
rs := results{}
for r := range rmerge(res) {
rs = append(rs, r)
}
sort.Sort(rs)
for _, r := range rs {
o <- r
}
close(o)
}()
return o
2016-11-16 01:18:35 -08:00
}
// compute is the checksumming workhorse
2017-10-18 17:58:11 -07:00
func compute(h hashr, jobs chan checksum, verbose bool) chan result {
2016-11-16 01:18:35 -08:00
hsh := h()
r := make(chan result)
go func() {
for job := range jobs {
f, err := os.Open(job.filename)
if err != nil {
r <- result{err: err}
continue
}
hsh.Reset()
_, err = io.Copy(hsh, f)
f.Close()
if err != nil {
r <- result{err: err}
continue
}
2017-10-18 17:58:11 -07:00
if verbose {
fmt.Fprintf(os.Stderr, "%v\n", job.filename)
}
r <- result{f: job.filename, cs: fmt.Sprintf("%x", hsh.Sum(nil))}
2016-11-16 01:18:35 -08:00
}
close(r)
}()
return r
}
// rmerge implements fan-in
func rmerge(cs []<-chan result) chan result {
out := make(chan result)
var wg sync.WaitGroup
output := func(c <-chan result) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(len(cs))
for _, c := range cs {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}